DPsim
DP_Ph1_RXLoad.cpp
1 /* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
2  * EONERC, RWTH Aachen University
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7  *********************************************************************************/
8 
9 #include <dpsim-models/DP/DP_Ph1_RXLoad.h>
10 
11 using namespace CPS;
12 
13 DP::Ph1::RXLoad::RXLoad(String uid, String name, Logger::Level logLevel)
14  : CompositePowerComp<Complex>(uid, name, true, true, logLevel),
15  mActivePower(mAttributes->create<Real>("P")),
16  mReactivePower(mAttributes->create<Real>("Q")),
17  mNomVoltage(mAttributes->create<Real>("V_nom")) {
18  setTerminalNumber(1);
19 
20  SPDLOG_LOGGER_INFO(mSLog, "Create {} {}", this->type(), name);
21  **mIntfVoltage = MatrixComp::Zero(1, 1);
22  **mIntfCurrent = MatrixComp::Zero(1, 1);
23 }
24 
25 DP::Ph1::RXLoad::RXLoad(String name, Logger::Level logLevel)
26  : RXLoad(name, name, logLevel) {}
27 
29 
30  if (!mParametersSet) {
31  setParameters(mTerminals[0]->singleActivePower(),
32  mTerminals[0]->singleReactivePower(),
33  std::abs(mTerminals[0]->initialSingleVoltage()));
34  }
35 
36  if (**mActivePower != 0) {
37  mResistance = std::pow(**mNomVoltage, 2) / **mActivePower;
38  mSubResistor =
39  std::make_shared<DP::Ph1::Resistor>(**mName + "_res", mLogLevel);
40  mSubResistor->setParameters(mResistance);
41  mSubResistor->connect({SimNode::GND, mTerminals[0]->node()});
42  mSubResistor->initialize(mFrequencies);
43  mSubResistor->initializeFromNodesAndTerminals(frequency);
44  addMNASubComponent(mSubResistor, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
45  MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, false);
46  } else {
47  mResistance = 0;
48  }
49 
50  if (**mReactivePower != 0)
51  mReactance = std::pow(**mNomVoltage, 2) / **mReactivePower;
52  else
53  mReactance = 0;
54 
55  if (mReactance > 0) {
56  mInductance = mReactance / (2. * PI * frequency);
57  mSubInductor =
58  std::make_shared<DP::Ph1::Inductor>(**mName + "_ind", mLogLevel);
59  mSubInductor->setParameters(mInductance);
60  mSubInductor->connect({SimNode::GND, mTerminals[0]->node()});
61  mSubInductor->initialize(mFrequencies);
62  mSubInductor->initializeFromNodesAndTerminals(frequency);
63  addMNASubComponent(mSubInductor, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
64  MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
65  } else if (mReactance < 0) {
66  mCapacitance = -1. / (2. * PI * frequency) / mReactance;
67  mSubCapacitor =
68  std::make_shared<DP::Ph1::Capacitor>(**mName + "_cap", mLogLevel);
69  mSubCapacitor->setParameters(mCapacitance);
70  mSubCapacitor->connect({SimNode::GND, mTerminals[0]->node()});
71  mSubCapacitor->initialize(mFrequencies);
72  mSubCapacitor->initializeFromNodesAndTerminals(frequency);
73  addMNASubComponent(mSubCapacitor,
74  MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
75  MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
76  }
77 
78  (**mIntfVoltage)(0, 0) = mTerminals[0]->initialSingleVoltage();
79  (**mIntfCurrent)(0, 0) = std::conj(Complex(**mActivePower, **mReactivePower) /
80  (**mIntfVoltage)(0, 0));
81 
82  SPDLOG_LOGGER_INFO(mSLog,
83  "\n--- Initialization from powerflow ---"
84  "\nVoltage across: {:s}"
85  "\nCurrent: {:s}"
86  "\nTerminal 0 voltage: {:s}"
87  "\nResistance: {:f}"
88  "\nReactance: {:f}"
89  "\n--- Initialization from powerflow finished ---",
90  Logger::phasorToString((**mIntfVoltage)(0, 0)),
91  Logger::phasorToString((**mIntfCurrent)(0, 0)),
92  Logger::phasorToString(initialSingleVoltage(0)),
93  mResistance, mReactance);
94 }
95 
96 void DP::Ph1::RXLoad::setParameters(Real activePower, Real reactivePower,
97  Real volt) {
98  mParametersSet = true;
99  **mActivePower = activePower;
100  **mReactivePower = reactivePower;
101  **mNomVoltage = volt;
102 
103  SPDLOG_LOGGER_INFO(mSLog, "Active Power={} [W] Reactive Power={} [VAr]",
104  **mActivePower, **mReactivePower);
105  SPDLOG_LOGGER_INFO(mSLog, "Nominal Voltage={} [V]", **mNomVoltage);
106 }
107 
108 void DP::Ph1::RXLoad::mnaCompUpdateVoltage(const Matrix &leftVector) {
109  (**mIntfVoltage)(0, 0) =
110  Math::complexFromVectorElement(leftVector, matrixNodeIndex(0));
111 }
112 
113 void DP::Ph1::RXLoad::mnaCompUpdateCurrent(const Matrix &leftVector) {
114  (**mIntfCurrent)(0, 0) = 0;
115 
116  for (auto subComp : mSubComponents) {
117  (**mIntfCurrent)(0, 0) += subComp->intfCurrent()(0, 0);
118  }
119 }
120 
122  AttributeBase::List &prevStepDependencies,
123  AttributeBase::List &attributeDependencies,
124  AttributeBase::List &modifiedAttributes) {
125  modifiedAttributes.push_back(mRightVector);
126 }
127 
128 void DP::Ph1::RXLoad::mnaParentPreStep(Real time, Int timeStepCount) {
129  mnaCompApplyRightSideVectorStamp(**mRightVector);
130 }
131 
133  AttributeBase::List &prevStepDependencies,
134  AttributeBase::List &attributeDependencies,
135  AttributeBase::List &modifiedAttributes,
136  Attribute<Matrix>::Ptr &leftVector) {
137  attributeDependencies.push_back(leftVector);
138  modifiedAttributes.push_back(mIntfVoltage);
139  modifiedAttributes.push_back(mIntfCurrent);
140 }
141 
142 void DP::Ph1::RXLoad::mnaParentPostStep(Real time, Int timeStepCount,
143  Attribute<Matrix>::Ptr &leftVector) {
144  mnaCompUpdateVoltage(**leftVector);
145  mnaCompUpdateCurrent(**leftVector);
146 }
Base class for composite power components.
Constant impedance load model consisting of RLC elements.
Definition: DP_Ph1_RXLoad.h:22
RXLoad(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
void setParameters(Real activePower, Real ReactivePower, Real volt)
Set model specific parameters.
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
void mnaParentPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Update interface voltage from MNA system result.
void initializeFromNodesAndTerminals(Real frequency) override
Initialize component from power flow data.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Update interface current from MNA system result.
String type()
Get component type (cross-platform)
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
Current through component.
Definition: SimPowerComp.h:47
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
Voltage between terminals.
Definition: SimPowerComp.h:45
Logger::Log mSLog
Component logger.