DPsim
Loading...
Searching...
No Matches
DP_Ph1_RXLoad.cpp
Go to the documentation of this file.
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
11
12using namespace CPS;
13
16
18 Logger::Level logLevel)
19 : CompositePowerComp<Complex>(uid, name, true, true, logLevel),
20 mUseSubcomponents(useSubcomponents),
21 mActivePower(mAttributes->create<Real>("P")),
22 mReactivePower(mAttributes->create<Real>("Q")),
23 mNomVoltage(mAttributes->create<Real>("V_nom")) {
25
26 SPDLOG_LOGGER_INFO(mSLog, "Create {} {}", this->type(), name);
27 **mIntfVoltage = MatrixComp::Zero(1, 1);
28 **mIntfCurrent = MatrixComp::Zero(1, 1);
29}
30
33
35 auto copy = RXLoad::make(name, mLogLevel);
37 copy->setParameters(**mActivePower, **mReactivePower, **mNomVoltage);
38 return copy;
39}
40
43 return;
45 return;
46 mSubCompCreated = true;
47
48 // Intentionally empty: which of R/L/C exist depends on the load power sign, known only in
49 // initializeParentFromNodesAndTerminals(), where the sub-components are created. Safe: no new virtual nodes.
50}
51
53 if (!mParametersSet) {
54 setParameters(mTerminals[0]->singleActivePower(),
55 mTerminals[0]->singleReactivePower(),
56 std::abs(mTerminals[0]->initialSingleVoltage()));
57 }
58
59 Real omega = 2. * PI * frequency;
60
61 if (**mActivePower != 0) {
62 mResistance = std::pow(**mNomVoltage, 2) / **mActivePower;
66 std::make_shared<DP::Ph1::Resistor>(**mName + "_res", mLogLevel);
67 mSubResistor->setParameters(mResistance);
68 mSubResistor->connect({SimNode::GND, mTerminals[0]->node()});
72 }
73 } else {
74 mResistance = 0;
76 }
77
78 mReactance = (**mReactivePower != 0)
79 ? std::pow(**mNomVoltage, 2) / **mReactivePower
80 : 0;
81
82 if (mReactance > 0) {
83 mInductance = mReactance / omega;
84 mDirectUseInductor = true;
85 mDirectUseCapacitor = false;
88 std::make_shared<DP::Ph1::Inductor>(**mName + "_ind", mLogLevel);
89 mSubInductor->setParameters(mInductance);
90 mSubInductor->connect({SimNode::GND, mTerminals[0]->node()});
94 }
95 } else if (mReactance < 0) {
96 mCapacitance = -1. / omega / mReactance;
97 mDirectUseInductor = false;
100 if (mUseSubcomponents) {
102 std::make_shared<DP::Ph1::Capacitor>(**mName + "_cap", mLogLevel);
103 mSubCapacitor->setParameters(mCapacitance);
104 mSubCapacitor->connect({SimNode::GND, mTerminals[0]->node()});
108 }
109 } else {
110 mDirectUseInductor = false;
111 mDirectUseCapacitor = false;
112 }
113
114 (**mIntfVoltage)(0, 0) = mTerminals[0]->initialSingleVoltage();
115 (**mIntfCurrent)(0, 0) =
116 (std::abs((**mIntfVoltage)(0, 0)) > 0)
117 ? std::conj(Complex(**mActivePower, **mReactivePower) /
118 (**mIntfVoltage)(0, 0))
119 : Complex(0, 0);
120 if (!mUseSubcomponents && std::abs((**mIntfVoltage)(0, 0)) > 0.) {
122 mDirectBranchCurrent = MatrixComp::Zero(mNumFreqs, 1);
123 else if (mDirectUseCapacitor)
124 mDirectBranchCurrent = MatrixComp::Zero(mNumFreqs, 1);
125 }
126
127 SPDLOG_LOGGER_INFO(mSLog,
128 "\n--- Initialization from powerflow ---"
129 "\nVoltage across: {:s}"
130 "\nCurrent: {:s}"
131 "\nTerminal 0 voltage: {:s}"
132 "\nResistance: {:f}"
133 "\nReactance: {:f}"
134 "\n--- Initialization from powerflow finished ---",
139}
140
142 Attribute<Matrix>::Ptr leftVector) {
144 return;
145 mDirectEquivCond = MatrixComp::Zero(mNumFreqs, 1);
146 mDirectHistory = MatrixComp::Zero(mNumFreqs, 1);
147 mDirectPrevCurrentFactor = MatrixComp::Zero(mNumFreqs, 1);
148 mDirectPrevVoltageFactor = MatrixComp::Zero(mNumFreqs, 1);
149 mDirectBranchCurrent = MatrixComp::Zero(mNumFreqs, 1);
151 return;
152
153 for (UInt freq = 0; freq < mNumFreqs; ++freq) {
154 const Real harmonicOmega = 2. * PI * mFrequencies(freq, 0);
155 if (mDirectUseInductor) {
156 const Real b = timeStep * harmonicOmega / 2.;
157 const Real a = timeStep / (2. * mInductance);
158 mDirectEquivCond(freq, 0) =
159 Complex(a / (1. + b * b), -a * b / (1. + b * b));
160 mDirectPrevCurrentFactor(freq, 0) =
161 Complex((1. - b * b) / (1. + b * b), -2. * b / (1. + b * b));
162 if (freq == 0 && harmonicOmega != 0.)
163 mDirectBranchCurrent(freq, 0) =
164 (**mIntfVoltage)(0, 0) / Complex(0., harmonicOmega * mInductance);
165 } else {
166 const Real capConductance = 2. * mDirectCapacitance / timeStep;
167 const Real capSusceptance = harmonicOmega * mDirectCapacitance;
168 mDirectEquivCond(freq, 0) = Complex(capConductance, capSusceptance);
169 mDirectPrevVoltageFactor(freq, 0) =
170 Complex(capConductance, -capSusceptance);
171 if (freq == 0)
172 mDirectBranchCurrent(freq, 0) =
173 Complex(0., harmonicOmega * mDirectCapacitance) *
174 (**mIntfVoltage)(0, 0);
175 }
176 }
177}
178
180 SparseMatrixRow &systemMatrix) {
182 return;
183 for (UInt freq = 0; freq < mNumFreqs; ++freq)
186 systemMatrix, matrixNodeIndex(0), 0, terminalNotGrounded(0), false,
187 mSLog, mNumFreqs, freq);
188}
189
192 return;
193 for (UInt freq = 0; freq < mNumFreqs; ++freq) {
195 mDirectHistory(freq, 0) =
196 mDirectEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
198 else
199 mDirectHistory(freq, 0) =
200 -mDirectBranchCurrent(freq, 0) -
201 mDirectPrevVoltageFactor(freq, 0) * (**mIntfVoltage)(0, freq);
202 if (terminalNotGrounded(0))
204 -mDirectHistory(freq, 0), mNumFreqs, freq);
205 }
206}
207
208void DP::Ph1::RXLoad::setParameters(Real activePower, Real reactivePower,
209 Real volt) {
210 mParametersSet = true;
211 **mActivePower = activePower;
212 **mReactivePower = reactivePower;
213 **mNomVoltage = volt;
214
215 SPDLOG_LOGGER_INFO(mSLog, "Active Power={} [W] Reactive Power={} [VAr]",
217 SPDLOG_LOGGER_INFO(mSLog, "Nominal Voltage={} [V]", **mNomVoltage);
218}
219
221 for (UInt freq = 0; freq < mNumFreqs; ++freq)
223 leftVector, matrixNodeIndex(0), mNumFreqs, freq);
224}
225
227 if (!mUseSubcomponents) {
228 for (UInt freq = 0; freq < mNumFreqs; ++freq) {
229 const Complex voltage = (**mIntfVoltage)(0, freq);
230 const Complex branchCurrent =
231 mDirectEquivCond(freq, 0) * voltage + mDirectHistory(freq, 0);
233 mDirectBranchCurrent(freq, 0) = branchCurrent;
234 (**mIntfCurrent)(0, freq) =
235 Complex(mDirectConductance, 0.) * voltage +
236 ((mDirectUseInductor || mDirectUseCapacitor) ? branchCurrent
237 : Complex(0., 0.));
238 }
239 return;
240 }
241 (**mIntfCurrent)(0, 0) = 0;
242
243 for (auto subComp : mSubComponents) {
244 (**mIntfCurrent)(0, 0) += subComp->intfCurrent()(0, 0);
245 }
246}
247
249 AttributeBase::List &prevStepDependencies,
250 AttributeBase::List &attributeDependencies,
251 AttributeBase::List &modifiedAttributes) {
252 if (!mUseSubcomponents) {
253 prevStepDependencies.push_back(mIntfVoltage);
254 prevStepDependencies.push_back(mIntfCurrent);
255 }
256 modifiedAttributes.push_back(mRightVector);
257}
258
262
264 AttributeBase::List &prevStepDependencies,
265 AttributeBase::List &attributeDependencies,
266 AttributeBase::List &modifiedAttributes,
267 Attribute<Matrix>::Ptr &leftVector) {
268 attributeDependencies.push_back(leftVector);
269 modifiedAttributes.push_back(mIntfVoltage);
270 modifiedAttributes.push_back(mIntfCurrent);
271}
272
274 Attribute<Matrix>::Ptr &leftVector) {
275 mnaCompUpdateVoltage(**leftVector);
276 mnaCompUpdateCurrent(**leftVector);
277}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
void addMNASubComponent(typename SimPowerComp< Complex >::Ptr subc, MNA_SUBCOMP_TASK_ORDER preStepOrder, MNA_SUBCOMP_TASK_ORDER postStepOrder, Bool contributeToRightVector)
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
CompositePowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
std::shared_ptr< DP::Ph1::Inductor > mSubInductor
Internal inductor.
void mnaParentApplyRightSideVectorStamp(Matrix &rightVector) override
RXLoad(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
SimPowerComp< Complex >::Ptr clone(String name) override
Returns a modified copy of the component with the given suffix added to the name and without.
Real mInductance
Inductance [H].
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
const Attribute< Real >::Ptr mActivePower
True after createSubComponents() runs; prevents double-construction.
Real mReactance
Reactance [Ohm].
MatrixComp mDirectBranchCurrent
const Attribute< Real >::Ptr mReactivePower
Reactive power [VAr].
Real mCapacitance
Capacitance [F].
void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
std::shared_ptr< DP::Ph1::Capacitor > mSubCapacitor
Internal capacitor.
MatrixComp mDirectPrevVoltageFactor
void mnaParentInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
const Attribute< Real >::Ptr mNomVoltage
Nominal voltage [V].
void setParameters(Real activePower, Real ReactivePower, Real volt)
Set model specific parameters.
MatrixComp mDirectEquivCond
void mnaParentApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
MatrixComp mDirectHistory
std::shared_ptr< DP::Ph1::Resistor > mSubResistor
Internal resistance.
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
void initializeParentFromNodesAndTerminals(Real frequency) override
Derives values from power flow data and pushes them to subcomponents.
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 createSubComponents() override
Constructs and registers MNA subcomponents; idempotent.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Update interface current from MNA system result.
MatrixComp mDirectPrevCurrentFactor
const Attribute< String >::Ptr mName
Human readable name.
String uid()
Returns unique id.
String type()
Get component type (cross-platform)
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
static String phasorToString(const Complex &num)
Definition Logger.cpp:57
Attribute< Matrix >::Ptr mRightVector
static void stampAdmittance(Complex admittance, SparseMatrixRow &mat, UInt node1Index, UInt node2Index, Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded, const Logger::Log &mSLog, Int maxFreq=1, Int freqIdx=0)
static Complex complexFromVectorElement(const Matrix &mat, Matrix::Index row, Int maxFreq=1, Int freqIdx=0)
static void setVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0, Matrix::Index colOffset=0)
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
SimTerminal< Complex >::List mTerminals
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
std::shared_ptr< SimPowerComp< VarType > > Ptr
std::vector< std::shared_ptr< SimPowerComp< Complex > > > mSubComponents
Bool terminalNotGrounded(UInt index)
Complex initialSingleVoltage(UInt index)
Logger::Level mLogLevel
Component logger control for internal variables.
bool mParametersSet
Flag indicating that parameters are set via setParameters() function.
Logger::Log mSLog
Component logger.
static std::shared_ptr< RXLoad > make(Args &&...args)
Definition PtrFactory.h:19
#define PI
Definition Definitions.h:43
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
Dense matrix for real numbers.
Definition Definitions.h:79
std::string String
Definition Definitions.h:63
double Real
Definition Definitions.h:60
int Int
Definition Definitions.h:59
std::complex< Real > Complex
Definition Definitions.h:61
bool Bool
Definition Definitions.h:62
unsigned int UInt
Definition Definitions.h:58
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:72