DPsim
Loading...
Searching...
No Matches
EMT_Ph3_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<Real>(uid, name, true, true, logLevel),
20 mActivePower(mAttributes->create<Matrix>("P")),
21 mReactivePower(mAttributes->create<Matrix>("Q")),
22 mNomVoltage(mAttributes->create<Real>("V_nom")),
23 mReactanceInSeries(false) {
24 mUseSubcomponents = useSubcomponents;
25 mDirectState = mAttributes->create<Matrix>("direct_state");
26 **mDirectState = Matrix::Zero(12, 1);
29
30 SPDLOG_LOGGER_INFO(mSLog, "Create {} {}", this->type(), name);
31 **mIntfVoltage = Matrix::Zero(3, 1);
32 **mIntfCurrent = Matrix::Zero(3, 1);
33 mSLog->flush();
34}
35
38
39EMT::Ph3::RXLoad::RXLoad(String name, Matrix activePower, Matrix reactivePower,
40 Real volt, Logger::Level logLevel)
41 : RXLoad(name, logLevel) {
42 **mActivePower = activePower;
43 **mReactivePower = reactivePower;
44 mPower = MatrixComp::Zero(3, 3);
45 mPower << Complex((**mActivePower)(0, 0), (**mReactivePower)(0, 0)),
46 Complex((**mActivePower)(0, 1), (**mReactivePower)(0, 1)),
47 Complex((**mActivePower)(0, 2), (**mReactivePower)(0, 2)),
48 Complex((**mActivePower)(1, 0), (**mReactivePower)(1, 0)),
49 Complex((**mActivePower)(1, 1), (**mReactivePower)(1, 1)),
50 Complex((**mActivePower)(1, 2), (**mReactivePower)(1, 2)),
51 Complex((**mActivePower)(2, 0), (**mReactivePower)(2, 0)),
52 Complex((**mActivePower)(2, 1), (**mReactivePower)(2, 1)),
53 Complex((**mActivePower)(2, 2), (**mReactivePower)(2, 2));
54
55 **mNomVoltage = volt;
57}
58
66
67void EMT::Ph3::RXLoad::setParameters(Matrix activePower, Matrix reactivePower,
68 Real volt, bool reactanceInSeries) {
69 if (!mUseSubcomponents && reactanceInSeries)
70 throw std::invalid_argument(
71 "Direct RXLoad mode currently supports parallel R-X loads only.");
72 **mActivePower = activePower;
73 **mReactivePower = reactivePower;
74 mReactanceInSeries = reactanceInSeries;
75
78 }
79 // complex power
80 mPower = MatrixComp::Zero(3, 3);
81 mPower(0, 0) = {(**mActivePower)(0, 0), (**mReactivePower)(0, 0)};
82 mPower(1, 1) = {(**mActivePower)(1, 1), (**mReactivePower)(1, 1)};
83 mPower(2, 2) = {(**mActivePower)(2, 2), (**mReactivePower)(2, 2)};
84
85 **mNomVoltage = volt;
86
87 SPDLOG_LOGGER_INFO(mSLog,
88 "\nActive Power [W]: {}"
89 "\nReactive Power [VAr]: {}",
92 SPDLOG_LOGGER_INFO(mSLog, "Nominal Voltage={} [V]", **mNomVoltage);
93
95}
96
99 return;
100 mSubCompCreated = true;
102 return;
103
104 // Intentionally empty: which of R/L/C exist depends on the load power sign, known only in
105 // initializeParentFromNodesAndTerminals(), where the sub-components are created. Safe: any
106 // series-mode virtual node is already declared in setParameters().
107}
108
110 Real omega = 2. * PI * frequency;
111
113 **mActivePower = Matrix::Zero(3, 3);
114 (**mActivePower)(0, 0) = mTerminals[0]->singleActivePower() / 3.;
115 (**mActivePower)(1, 1) = mTerminals[0]->singleActivePower() / 3.;
116 (**mActivePower)(2, 2) = mTerminals[0]->singleActivePower() / 3.;
117
118 **mReactivePower = Matrix::Zero(3, 3);
119 (**mReactivePower)(0, 0) = mTerminals[0]->singleReactivePower() / 3.;
120 (**mReactivePower)(1, 1) = mTerminals[0]->singleReactivePower() / 3.;
121 (**mReactivePower)(2, 2) = mTerminals[0]->singleReactivePower() / 3.;
122
123 // complex power
124 mPower = MatrixComp::Zero(3, 3);
125 mPower(0, 0) = {(**mActivePower)(0, 0), (**mReactivePower)(0, 0)};
126 mPower(1, 1) = {(**mActivePower)(1, 1), (**mReactivePower)(1, 1)};
127 mPower(2, 2) = {(**mActivePower)(2, 2), (**mReactivePower)(2, 2)};
128
129 **mNomVoltage = std::abs(mTerminals[0]->initialSingleVoltage());
130
131 SPDLOG_LOGGER_INFO(mSLog,
132 "\nActive Power [W]: {}"
133 "\nReactive Power [VAr]: {}",
136 SPDLOG_LOGGER_INFO(mSLog, "Nominal Voltage={} [V]", **mNomVoltage);
137 }
138
139 // Compute derived impedance values and create+parametrize sub-components
140 // now that power and voltage are guaranteed to be known.
141 if ((**mActivePower)(0, 0) != 0)
143 std::pow(**mNomVoltage / sqrt(3), 2) * (**mActivePower).inverse();
144 else
145 mResistance = Matrix::Zero(3, 3);
146
147 if ((**mReactivePower)(0, 0) != 0)
148 mReactance =
149 std::pow(**mNomVoltage / sqrt(3), 2) * (**mReactivePower).inverse();
150 else
151 mReactance = Matrix::Zero(3, 3);
152
153 if (!mUseSubcomponents) {
154 const Real omega = 2. * PI * frequency;
155 if (mReactance(0, 0) > 0.)
156 mInductance = mReactance / omega;
157 else if (mReactance(0, 0) < 0.)
158 mCapacitance = -mReactance.inverse() / omega;
160 mResistance.norm() > 0. ? mResistance.inverse()(0, 0) : 0.;
161 mDirectHasInductor = mReactance(0, 0) > 0.;
162 mDirectHasCapacitor = mReactance(0, 0) < 0.;
163 MatrixComp voltage = MatrixComp::Zero(3, 1);
164 voltage(0) = RMS3PH_TO_PEAK1PH * mTerminals[0]->initialSingleVoltage();
165 voltage(1) = voltage(0) * SHIFT_TO_PHASE_B;
166 voltage(2) = voltage(0) * SHIFT_TO_PHASE_C;
167 MatrixComp loadG = MatrixComp::Zero(3, 3);
168 if (mResistance.norm() > 0.)
169 loadG = mResistance.inverse().cast<Complex>();
170 **mIntfVoltage = voltage.real();
171 (**mDirectState).setZero();
172 if (mDirectHasInductor) {
173 const MatrixComp yL =
174 (Complex(0., omega) * mInductance.cast<Complex>()).inverse();
175 (**mDirectState).block<3, 1>(0, 0) = voltage.real();
176 (**mDirectState).block<3, 1>(3, 0) = (yL * voltage).real();
177 }
179 const MatrixComp yC = Complex(0., omega) * mCapacitance.cast<Complex>();
180 (**mDirectState).block<3, 1>(6, 0) = voltage.real();
181 (**mDirectState).block<3, 1>(9, 0) = (yC * voltage).real();
182 }
183 **mIntfCurrent = loadG.real() * voltage.real();
185 **mIntfCurrent += (**mDirectState).block<3, 1>(3, 0);
187 **mIntfCurrent += (**mDirectState).block<3, 1>(9, 0);
188 return;
189 }
190
191 if ((**mActivePower)(0, 0) != 0) {
193 std::make_shared<EMT::Ph3::Resistor>(**mName + "_res", mLogLevel);
194 mSubResistor->setParameters(mResistance);
195 if (mReactanceInSeries) {
196 mSubResistor->connect({mTerminals[0]->node(), mVirtualNodes[0]});
197 } else {
198 mSubResistor->connect({SimNode::GND, mTerminals[0]->node()});
199 }
202 }
203
204 if (mReactance(0, 0) > 0) {
205 mInductance = mReactance / omega;
206
208 std::make_shared<EMT::Ph3::Inductor>(**mName + "_ind", mLogLevel);
209 mSubInductor->setParameters(mInductance);
210 if (mReactanceInSeries) {
212 } else {
213 mSubInductor->connect({SimNode::GND, mTerminals[0]->node()});
214 }
217 } else if (mReactance(0, 0) < 0) {
218 mCapacitance = -1. / omega * mReactance.inverse();
219
221 std::make_shared<EMT::Ph3::Capacitor>(**mName + "_cap", mLogLevel);
222 mSubCapacitor->setParameters(mCapacitance);
223 if (mReactanceInSeries) {
225 } else {
226 mSubCapacitor->connect({SimNode::GND, mTerminals[0]->node()});
227 }
231 }
232
233 MatrixComp vInitABC = MatrixComp::Zero(3, 1);
234 vInitABC(0, 0) = RMS3PH_TO_PEAK1PH * mTerminals[0]->initialSingleVoltage();
235 vInitABC(1, 0) = vInitABC(0, 0) * SHIFT_TO_PHASE_B;
236 vInitABC(2, 0) = vInitABC(0, 0) * SHIFT_TO_PHASE_C;
237 **mIntfVoltage = vInitABC.real();
238
239 if (mReactanceInSeries) {
240 MatrixComp impedance = MatrixComp::Zero(3, 3);
241 impedance << Complex(mResistance(0, 0), mReactance(0, 0)),
242 Complex(mResistance(0, 1), mReactance(0, 1)),
243 Complex(mResistance(0, 2), mReactance(0, 2)),
244 Complex(mResistance(1, 0), mReactance(1, 0)),
245 Complex(mResistance(1, 1), mReactance(1, 1)),
246 Complex(mResistance(1, 2), mReactance(1, 2)),
247 Complex(mResistance(2, 0), mReactance(2, 0)),
248 Complex(mResistance(2, 1), mReactance(2, 1)),
249 Complex(mResistance(2, 2), mReactance(2, 2));
250 MatrixComp iInit = impedance.inverse() * vInitABC;
251 **mIntfCurrent = iInit.real();
252
253 // Initialization of virtual node
254 // Initial voltage of phase B,C is set after A
255 MatrixComp vInitTerm0 = MatrixComp::Zero(3, 1);
256 vInitTerm0(0, 0) = RMS3PH_TO_PEAK1PH * initialSingleVoltage(0);
257 vInitTerm0(1, 0) = vInitTerm0(0, 0) * SHIFT_TO_PHASE_B;
258 vInitTerm0(2, 0) = vInitTerm0(0, 0) * SHIFT_TO_PHASE_C;
259 mVirtualNodes[0]->setInitialVoltage(PEAK1PH_TO_RMS3PH *
260 (vInitTerm0 - mResistance * iInit));
261 }
262
263 if ((**mActivePower)(0, 0) != 0) {
264 mSubResistor->initializeFromNodesAndTerminals(frequency);
265 if (!mReactanceInSeries) {
266 **mIntfCurrent += mSubResistor->intfCurrent();
267 }
268 }
269
270 if (mReactance(0, 0) > 0) {
271 mSubInductor->initializeFromNodesAndTerminals(frequency);
272 if (!mReactanceInSeries) {
273 **mIntfCurrent += mSubInductor->intfCurrent();
274 }
275 } else if (mReactance(0, 0) < 0) {
276 mSubCapacitor->initializeFromNodesAndTerminals(frequency);
277 if (!mReactanceInSeries) {
278 **mIntfCurrent += mSubCapacitor->intfCurrent();
279 }
280 }
281
282 SPDLOG_LOGGER_INFO(
283 mSLog,
284 "\n--- Initialization from powerflow ---"
285 "\nVoltage across: {:s}"
286 "\nCurrent: {:s}"
287 "\nTerminal 0 voltage: {:s}"
288 "\nActive Power: {:s}"
289 "\nReactive Power: {:s}"
290 "\nResistance: {:s}"
291 "\nReactance: {:s}"
292 "\n--- Initialization from powerflow finished ---",
299 mSLog->flush();
300}
301
303 AttributeBase::List &prevStepDependencies,
304 AttributeBase::List &attributeDependencies,
305 AttributeBase::List &modifiedAttributes) {
307 prevStepDependencies.push_back(mDirectState);
308 modifiedAttributes.push_back(mRightVector);
309};
310
312 AttributeBase::List &prevStepDependencies,
313 AttributeBase::List &attributeDependencies,
314 AttributeBase::List &modifiedAttributes,
315 Attribute<Matrix>::Ptr &leftVector) {
316 attributeDependencies.push_back(leftVector);
317 modifiedAttributes.push_back(mIntfCurrent);
318 modifiedAttributes.push_back(mIntfVoltage);
320 modifiedAttributes.push_back(mDirectState);
321};
322
323void EMT::Ph3::RXLoad::mnaParentPreStep(Real time, Int timeStepCount) {
324 if (!mUseSubcomponents) {
325 const Matrix &state = **mDirectState;
326 for (Int phase = 0; phase < 3; ++phase) {
328 mDirectHistoryLoad(phase, 0) =
329 mDirectInductorAdmittance * state(phase, 0) + state(phase + 3, 0);
331 mDirectHistoryShunt(phase, 0) =
332 -state(phase + 9, 0) -
333 mDirectCapacitorAdmittance * state(phase + 6, 0);
334 }
335 }
337}
338
340 Attribute<Matrix>::Ptr &leftVector) {
341 if (!mUseSubcomponents) {
342 mnaCompUpdateVoltage(**leftVector);
343 const Matrix &voltage = **mIntfVoltage;
344 Matrix &state = **mDirectState;
345 Matrix &current = **mIntfCurrent;
346 for (Int phase = 0; phase < 3; ++phase) {
347 Real inductorCurrent = 0.;
348 Real capacitorCurrent = 0.;
349 if (mDirectHasInductor) {
350 inductorCurrent = mDirectInductorAdmittance * voltage(phase, 0) +
351 mDirectHistoryLoad(phase, 0);
352 state(phase, 0) = voltage(phase, 0);
353 state(phase + 3, 0) = inductorCurrent;
354 }
356 capacitorCurrent = mDirectCapacitorAdmittance * voltage(phase, 0) +
357 mDirectHistoryShunt(phase, 0);
358 state(phase + 6, 0) = voltage(phase, 0);
359 state(phase + 9, 0) = capacitorCurrent;
360 }
361 current(phase, 0) = mDirectTotalConductance * voltage(phase, 0) +
362 inductorCurrent + capacitorCurrent;
363 }
364 return;
365 }
366 mnaCompUpdateVoltage(**leftVector);
367 mnaCompUpdateCurrent(**leftVector);
368}
369
371 (**mIntfVoltage).setZero();
372 (**mIntfVoltage)(0, 0) =
374 (**mIntfVoltage)(1, 0) =
376 (**mIntfVoltage)(2, 0) =
378}
379
381 if (!mUseSubcomponents) {
382 const Matrix &voltage = **mIntfVoltage;
383 Matrix &current = **mIntfCurrent;
384 for (Int phase = 0; phase < 3; ++phase) {
385 Real inductorCurrent = 0.;
386 Real capacitorCurrent = 0.;
388 inductorCurrent = mDirectInductorAdmittance * voltage(phase, 0) +
389 mDirectHistoryLoad(phase, 0);
391 capacitorCurrent = mDirectCapacitorAdmittance * voltage(phase, 0) +
392 mDirectHistoryShunt(phase, 0);
393 current(phase, 0) = mDirectTotalConductance * voltage(phase, 0) +
394 inductorCurrent + capacitorCurrent;
395 }
396 return;
397 }
398 if (mReactanceInSeries) {
399 **mIntfCurrent = mSubInductor->intfCurrent();
400 } else {
401 (**mIntfCurrent).setZero();
402 for (auto &subc : mSubComponents) {
403 **mIntfCurrent += subc->intfCurrent();
404 }
405 }
406}
407
421
431
434 for (UInt phase = 0; phase < 3; ++phase)
436 rightVector, matrixNodeIndex(0, phase),
437 -(mDirectHistoryLoad(phase) + mDirectHistoryShunt(phase)));
438}
439
444
446 if (mUseSubcomponents) {
448 return;
449 }
450 if (!terminalNotGrounded(0))
451 return;
452 for (UInt phase = 0; phase < 3; ++phase) {
453 const UInt idx = matrixNodeIndex(0, phase);
454 Math::addToVectorElement(rightVector, idx,
456 }
457}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
void addMNASubComponent(typename SimPowerComp< Real >::Ptr subc, MNA_SUBCOMP_TASK_ORDER preStepOrder, MNA_SUBCOMP_TASK_ORDER postStepOrder, Bool contributeToRightVector)
Bool mnaHasCompactRightSideVectorStamp() const override
Composite stamps touch only their cached terminal and virtual-node rows.
void mnaAddCompactRightSideVectorStamp(Matrix &rightVector) override
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
CompositePowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
const Attribute< Matrix >::Ptr mActivePower
Active power [Watt].
Attribute< Matrix >::Ptr mDirectState
Bool mReactanceInSeries
If set to true, the reactance is in series with the resistor. Otherwise it is parallel to the resisto...
void setParameters(Matrix activePower, Matrix reactivePower, Real volt, bool reactanceInSeries=false)
RXLoad(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
std::shared_ptr< EMT::Ph3::Resistor > mSubResistor
Internal resistance.
std::shared_ptr< EMT::Ph3::Inductor > mSubInductor
Internal inductor.
void createSubComponents() override
Constructs and registers MNA subcomponents; idempotent.
std::shared_ptr< EMT::Ph3::Capacitor > mSubCapacitor
Internal capacitor.
Bool mnaHasCompactRightSideVectorStamp() const override
Composite stamps touch only their cached terminal and virtual-node rows.
void mnaParentApplyRightSideVectorStamp(Matrix &rightVector) override
Matrix mResistance
Resistance [Ohm].
void initializeParentFromNodesAndTerminals(Real frequency) override
Derives values from power flow data and pushes them to subcomponents.
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Matrix mReactance
Reactance [Ohm].
void mnaParentInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
const Attribute< Real >::Ptr mNomVoltage
Nominal voltage [V].
void mnaCompUpdateVoltage(const Matrix &leftVector) override
void mnaParentApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
SimPowerComp< Real >::Ptr clone(String name) override
Returns a modified copy of the component with the given suffix added to the name and without.
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
void mnaParentPreStep(Real time, Int timeStepCount) override
MNA pre and post step operations.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Matrix mCapacitance
Capacitance [F].
const Attribute< Matrix >::Ptr mReactivePower
Reactive power [VAr].
void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
void mnaAddCompactRightSideVectorStamp(Matrix &rightVector) override
Matrix mInductance
Inductance [H].
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 matrixToString(const Matrix &mat)
Definition Logger.cpp:31
static String phasorToString(const Complex &num)
Definition Logger.cpp:57
Attribute< Matrix >::Ptr mRightVector
static void stampConductanceMatrix(const Matrix &conductanceMat, SparseMatrixRow &mat, UInt node1Index, UInt node2Index, Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded, const Logger::Log &mSLog)
static Real realFromVectorElement(const Matrix &mat, Matrix::Index row)
static void setVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0, Matrix::Index colOffset=0)
static void addToVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0)
static Ptr GND
Definition SimNode.h:37
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Real > >::Ptr mIntfCurrent
SimTerminal< Real >::List mTerminals
void setVirtualNodeNumber(UInt num)
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
std::shared_ptr< SimPowerComp< VarType > > Ptr
SimNode< Real >::List mVirtualNodes
std::vector< std::shared_ptr< SimPowerComp< Real > > > mSubComponents
Bool terminalNotGrounded(UInt index)
Complex initialSingleVoltage(UInt index)
void setTerminalNumber(UInt num)
Logger::Level mLogLevel
Component logger control for internal variables.
Logger::Log mSLog
Component logger.
static std::shared_ptr< RXLoad > make(Args &&...args)
Definition PtrFactory.h:19
#define PI
Definition Definitions.h:43
#define RMS3PH_TO_PEAK1PH
Definition Definitions.h:50
#define SHIFT_TO_PHASE_C
Definition Definitions.h:47
#define PEAK1PH_TO_RMS3PH
Definition Definitions.h:51
#define SHIFT_TO_PHASE_B
Definition Definitions.h:46
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
Eigen::Matrix< Complex, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixComp
Dense matrix for complex numbers.
Definition Definitions.h:82
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