DPsim
Loading...
Searching...
No Matches
EMT_Ph3_PiLine.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)
20 CompositePowerComp<Real>(uid, name, true, true, logLevel) {
21 mUseSubcomponents = useSubcomponents;
22 mDirectState = mAttributes->create<Matrix>("direct_state");
23 **mDirectState = Matrix::Zero(18, 1);
25 // The direct path still keeps the series midpoint as an MNA unknown. This
26 // preserves the exact conventional R-L companion equations while removing
27 // the separate child components and their scheduler tasks.
30
31 SPDLOG_LOGGER_INFO(mSLog, "Create {} {}", this->type(), name);
32 **mIntfVoltage = Matrix::Zero(3, 1);
33 **mIntfCurrent = Matrix::Zero(3, 1);
34
35 mSLog->flush();
36}
37
45
48 return;
49 mSubCompCreated = true;
50
51 if (!mUseSubcomponents) {
52 if ((**mParallelCond)(0, 0) <= 0.) {
53 **mParallelCond = Matrix::Zero(3, 3);
54 (**mParallelCond).diagonal().setConstant(1e-6);
55 }
56 return;
57 }
58
59 // By default there is always a small conductance to ground to
60 // avoid problems with floating nodes.
61 Matrix defaultParallelCond = Matrix::Zero(3, 3);
62 defaultParallelCond << 1e-6, 0, 0, 0, 1e-6, 0, 0, 0, 1e-6;
64 ((**mParallelCond)(0, 0) > 0) ? **mParallelCond : defaultParallelCond;
65
66 // Create series sub components
68 std::make_shared<EMT::Ph3::Resistor>(**mName + "_res", mLogLevel);
69 mSubSeriesResistor->setParameters(**mSeriesRes);
70 mSubSeriesResistor->connect({mTerminals[0]->node(), mVirtualNodes[0]});
74
76 std::make_shared<EMT::Ph3::Inductor>(**mName + "_ind", mLogLevel);
77 mSubSeriesInductor->setParameters(**mSeriesInd);
78 mSubSeriesInductor->connect({mVirtualNodes[0], mTerminals[1]->node()});
82
83 // Create parallel sub components
85 std::make_shared<EMT::Ph3::Resistor>(**mName + "_con0", mLogLevel);
86 mSubParallelResistor0->setParameters(2. * (**mParallelCond).inverse());
87 mSubParallelResistor0->connect(
92
94 std::make_shared<EMT::Ph3::Resistor>(**mName + "_con1", mLogLevel);
95 mSubParallelResistor1->setParameters(2. * (**mParallelCond).inverse());
96 mSubParallelResistor1->connect(
101
102 if ((**mParallelCap)(0, 0) > 0) {
104 std::make_shared<EMT::Ph3::Capacitor>(**mName + "_cap0", mLogLevel);
105 mSubParallelCapacitor0->setParameters(**mParallelCap / 2.);
106 mSubParallelCapacitor0->connect(
111
113 std::make_shared<EMT::Ph3::Capacitor>(**mName + "_cap1", mLogLevel);
114 mSubParallelCapacitor1->setParameters(**mParallelCap / 2.);
115 mSubParallelCapacitor1->connect(
120 }
121}
122
124 if (!mUseSubcomponents) {
125 const Real omega = 2. * PI * frequency;
126 MatrixComp v0 = MatrixComp::Zero(3, 1), v1 = MatrixComp::Zero(3, 1);
129 v0(1) = v0(0) * SHIFT_TO_PHASE_B;
130 v0(2) = v0(0) * SHIFT_TO_PHASE_C;
131 v1(1) = v1(0) * SHIFT_TO_PHASE_B;
132 v1(2) = v1(0) * SHIFT_TO_PHASE_C;
133 const MatrixComp voltage = v1 - v0;
134 const MatrixComp impedance =
135 (**mSeriesRes).cast<Complex>() +
136 Complex(0., omega) * (**mSeriesInd).cast<Complex>();
137 const MatrixComp current = impedance.inverse() * voltage;
138 const MatrixComp halfCap = ((**mParallelCap) / 2.).cast<Complex>();
139 **mIntfVoltage = voltage.real();
140 **mIntfCurrent = current.real();
141 mVirtualNodes[0]->setInitialVoltage(
143 (v0 + (**mSeriesRes).cast<Complex>() * current).real());
144 (**mDirectState).setZero();
145 (**mDirectState).block<3, 1>(0, 0) = current.real();
146 (**mDirectState).block<3, 1>(3, 0) =
147 (voltage - (**mSeriesRes).cast<Complex>() * current).real();
148 (**mDirectState).block<3, 1>(6, 0) = v0.real();
149 (**mDirectState).block<3, 1>(9, 0) =
150 (Complex(0., omega) * halfCap * v0).real();
151 (**mDirectState).block<3, 1>(12, 0) = v1.real();
152 (**mDirectState).block<3, 1>(15, 0) =
153 (Complex(0., omega) * halfCap * v1).real();
154 return;
155 }
156 // Static calculation
157 Real omega = 2. * PI * frequency;
158 MatrixComp impedance = MatrixComp::Zero(3, 3);
159 impedance << Complex((**mSeriesRes)(0, 0), omega * (**mSeriesInd)(0, 0)),
160 Complex((**mSeriesRes)(0, 1), omega * (**mSeriesInd)(0, 1)),
161 Complex((**mSeriesRes)(0, 2), omega * (**mSeriesInd)(0, 2)),
162 Complex((**mSeriesRes)(1, 0), omega * (**mSeriesInd)(1, 0)),
163 Complex((**mSeriesRes)(1, 1), omega * (**mSeriesInd)(1, 1)),
164 Complex((**mSeriesRes)(1, 2), omega * (**mSeriesInd)(1, 2)),
165 Complex((**mSeriesRes)(2, 0), omega * (**mSeriesInd)(2, 0)),
166 Complex((**mSeriesRes)(2, 1), omega * (**mSeriesInd)(2, 1)),
167 Complex((**mSeriesRes)(2, 2), omega * (**mSeriesInd)(2, 2));
168
169 MatrixComp vInitABC = MatrixComp::Zero(3, 1);
170 vInitABC(0, 0) = RMS3PH_TO_PEAK1PH * initialSingleVoltage(1) -
172 vInitABC(1, 0) = vInitABC(0, 0) * SHIFT_TO_PHASE_B;
173 vInitABC(2, 0) = vInitABC(0, 0) * SHIFT_TO_PHASE_C;
174 MatrixComp iInit = impedance.inverse() * vInitABC;
175 **mIntfCurrent = iInit.real();
176 **mIntfVoltage = vInitABC.real();
177
178 // Initialization of virtual node
179 // Initial voltage of phase B,C is set after A
180 MatrixComp vInitTerm0 = MatrixComp::Zero(3, 1);
181 vInitTerm0(0, 0) = RMS3PH_TO_PEAK1PH * initialSingleVoltage(0);
182 vInitTerm0(1, 0) = vInitTerm0(0, 0) * SHIFT_TO_PHASE_B;
183 vInitTerm0(2, 0) = vInitTerm0(0, 0) * SHIFT_TO_PHASE_C;
184
185 mVirtualNodes[0]->setInitialVoltage(PEAK1PH_TO_RMS3PH *
186 (vInitTerm0 + **mSeriesRes * iInit));
187
188 SPDLOG_LOGGER_DEBUG(mSLog,
189 "\n--debug--"
190 "\n seriesRes: {:s}"
191 "\n seriesInd: {:s}"
192 "\n Impedance: {:s}"
193 "\n vInit: {:s}"
194 "\n iInit: {:s}",
200
201 SPDLOG_LOGGER_INFO(
202 mSLog,
203 "\n--- Initialization from powerflow ---"
204 "\nVoltage across: {:s}"
205 "\nCurrent: {:s}"
206 "\nTerminal 0 voltage: {:s}"
207 "\nTerminal 1 voltage: {:s}"
208 "\nVirtual Node 1 voltage: {:s}"
209 "\n--- Initialization from powerflow finished ---",
215 mSLog->flush();
216}
217
221 return;
222 mDirectResistanceConductance = (**mSeriesRes).inverse();
223 mDirectInductorAdmittance = (timeStep / 2.) * (**mSeriesInd).inverse();
224 mDirectCapAdmittance = (**mParallelCap) / timeStep;
225}
226
228 SparseMatrixRow &systemMatrix) {
230 return;
232 const UInt midpoint = mVirtualNodes[0]->matrixNodeIndex(PhaseType::A);
234 mDirectResistanceConductance, systemMatrix, matrixNodeIndex(0), midpoint,
235 terminalNotGrounded(0), true, mSLog);
237 midpoint, matrixNodeIndex(1), true,
239 const Matrix shunt = mDirectCapAdmittance + (**mParallelCond) / 2.;
240 for (UInt terminal = 0; terminal < 2; ++terminal)
242 shunt, systemMatrix, matrixNodeIndex(terminal), 0,
244}
245
248 return;
249 const UInt midpoint = mVirtualNodes[0]->matrixNodeIndex(PhaseType::A);
250 for (UInt phase = 0; phase < 3; ++phase)
251 Math::setVectorElement(rightVector, midpoint + phase,
252 mDirectSeriesHistory(phase));
253 if (terminalNotGrounded(1))
254 for (UInt phase = 0; phase < 3; ++phase)
255 Math::setVectorElement(rightVector, matrixNodeIndex(1, phase),
256 -mDirectSeriesHistory(phase) -
257 mDirectCapHistory1(phase));
258 if (terminalNotGrounded(0))
259 for (UInt phase = 0; phase < 3; ++phase)
260 Math::setVectorElement(rightVector, matrixNodeIndex(0, phase),
261 -mDirectCapHistory0(phase));
262}
263
268
270 if (mUseSubcomponents) {
272 return;
273 }
274 const UInt midpoint = mVirtualNodes[0]->matrixNodeIndex(PhaseType::A);
275 for (UInt phase = 0; phase < 3; ++phase)
277 rightVector, midpoint + phase,
278 Math::realFromVectorElement(**mRightVector, midpoint + phase));
279 for (UInt terminal = 0; terminal < 2; ++terminal) {
281 continue;
282 for (UInt phase = 0; phase < 3; ++phase) {
283 const UInt idx = matrixNodeIndex(terminal, phase);
285 rightVector, idx, Math::realFromVectorElement(**mRightVector, idx));
286 }
287 }
288}
289
291 AttributeBase::List &prevStepDependencies,
292 AttributeBase::List &attributeDependencies,
293 AttributeBase::List &modifiedAttributes) {
295 prevStepDependencies.push_back(mDirectState);
296 else {
297 prevStepDependencies.push_back(mIntfCurrent);
298 prevStepDependencies.push_back(mIntfVoltage);
299 }
300 modifiedAttributes.push_back(mRightVector);
301}
302
303void EMT::Ph3::PiLine::mnaParentPreStep(Real time, Int timeStepCount) {
304 if (!mUseSubcomponents) {
305 mDirectInductorHistory.noalias() =
306 mDirectInductorAdmittance * (**mDirectState).block<3, 1>(3, 0);
307 mDirectInductorHistory += (**mDirectState).block<3, 1>(0, 0);
310 -(**mDirectState).block<3, 1>(9, 0) -
311 mDirectCapAdmittance * (**mDirectState).block<3, 1>(6, 0);
313 -(**mDirectState).block<3, 1>(15, 0) -
314 mDirectCapAdmittance * (**mDirectState).block<3, 1>(12, 0);
315 }
317}
318
320 AttributeBase::List &prevStepDependencies,
321 AttributeBase::List &attributeDependencies,
322 AttributeBase::List &modifiedAttributes,
323 Attribute<Matrix>::Ptr &leftVector) {
324 attributeDependencies.push_back(leftVector);
325 modifiedAttributes.push_back(mIntfVoltage);
326 modifiedAttributes.push_back(mIntfCurrent);
328 modifiedAttributes.push_back(mDirectState);
329}
330
332 Attribute<Matrix>::Ptr &leftVector) {
333 if (!mUseSubcomponents) {
334 const auto voltage = [&](UInt terminal) {
335 Eigen::Matrix<Real, 3, 1> result = Eigen::Matrix<Real, 3, 1>::Zero();
337 for (UInt phase = 0; phase < 3; ++phase)
338 result(phase) = Math::realFromVectorElement(
339 **leftVector, matrixNodeIndex(terminal, phase));
340 return result;
341 };
342 const auto v0 = voltage(0), v1 = voltage(1);
343 Eigen::Matrix<Real, 3, 1> vmid;
344 for (UInt phase = 0; phase < 3; ++phase)
345 vmid(phase) = Math::realFromVectorElement(
346 **leftVector,
347 mVirtualNodes[0]->matrixNodeIndex(static_cast<PhaseType>(phase)));
348 **mIntfVoltage = v1 - v0;
349 **mIntfCurrent =
351 (**mDirectState).block<3, 1>(0, 0) = **mIntfCurrent;
352 (**mDirectState).block<3, 1>(3, 0) = v1 - vmid;
353 (**mDirectState).block<3, 1>(6, 0) = v0;
354 (**mDirectState).block<3, 1>(9, 0) =
356 (**mDirectState).block<3, 1>(12, 0) = v1;
357 (**mDirectState).block<3, 1>(15, 0) =
359 return;
360 }
361 mnaCompUpdateVoltage(**leftVector);
362 mnaCompUpdateCurrent(**leftVector);
363}
364
366 // v1 - v0
367 (**mIntfVoltage).setZero();
368 if (terminalNotGrounded(1)) {
369 (**mIntfVoltage)(0, 0) =
371 (**mIntfVoltage)(1, 0) =
373 (**mIntfVoltage)(2, 0) =
375 }
376 if (terminalNotGrounded(0)) {
377 (**mIntfVoltage)(0, 0) =
378 (**mIntfVoltage)(0, 0) -
380 (**mIntfVoltage)(1, 0) =
381 (**mIntfVoltage)(1, 0) -
383 (**mIntfVoltage)(2, 0) =
384 (**mIntfVoltage)(2, 0) -
386 }
387}
388
390 if (!mUseSubcomponents) {
391 Eigen::Matrix<Real, 3, 1> midpointVoltage;
392 for (UInt phase = 0; phase < 3; ++phase)
393 midpointVoltage(phase) = Math::realFromVectorElement(
394 leftVector,
395 mVirtualNodes[0]->matrixNodeIndex(static_cast<PhaseType>(phase)));
396 Eigen::Matrix<Real, 3, 1> terminal1 = Eigen::Matrix<Real, 3, 1>::Zero();
397 if (terminalNotGrounded(1))
398 for (UInt phase = 0; phase < 3; ++phase)
399 terminal1(phase) =
400 Math::realFromVectorElement(leftVector, matrixNodeIndex(1, phase));
401 **mIntfCurrent = mDirectInductorAdmittance * (terminal1 - midpointVoltage) +
403 return;
404 }
405 **mIntfCurrent = mSubSeriesInductor->intfCurrent();
406}
407
408// #### Tear Methods ####
410 MNAInterface::List gndComponents;
411
412 gndComponents.push_back(mSubParallelResistor0);
413 gndComponents.push_back(mSubParallelResistor1);
414
415 if ((**mParallelCap)(0, 0) > 0) {
416 gndComponents.push_back(mSubParallelCapacitor0);
417 gndComponents.push_back(mSubParallelCapacitor1);
418 }
419
420 return gndComponents;
421}
422
424 mSubSeriesResistor->mnaTearSetIdx(mTearIdx);
425 mSubSeriesResistor->mnaTearInitialize(omega, timeStep);
426 mSubSeriesInductor->mnaTearSetIdx(mTearIdx);
427 mSubSeriesInductor->mnaTearInitialize(omega, timeStep);
428}
429
431 mSubSeriesResistor->mnaTearApplyMatrixStamp(tearMatrix);
432 mSubSeriesInductor->mnaTearApplyMatrixStamp(tearMatrix);
433}
434
436 mSubSeriesInductor->mnaTearApplyVoltageStamp(voltageVector);
437}
438
440 mSubSeriesInductor->mnaTearPostStep(voltage - (**mSeriesRes * current),
441 current);
442 (**mIntfCurrent) = mSubSeriesInductor->intfCurrent();
443}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
const Attribute< Matrix >::Ptr mParallelCond
Conductance in parallel to the line [S].
const Attribute< Matrix >::Ptr mSeriesRes
Resistance along the line [ohms].
const Attribute< Matrix >::Ptr mSeriesInd
Inductance along the line [H].
const Attribute< Matrix >::Ptr mParallelCap
Capacitance in parallel to the line [F].
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)
PiLine(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
SimPowerComp< Real >::Ptr clone(String copySuffix) override
DEPRECATED: Delete method.
MatrixFixedSize< 3, 3 > mDirectInductorAdmittance
void mnaAddCompactRightSideVectorStamp(Matrix &rightVector) override
void mnaTearApplyMatrixStamp(SparseMatrixRow &tearMatrix) override
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
MatrixFixedSize< 3, 3 > mDirectResistanceConductance
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Updates internal voltage variable of the component.
std::shared_ptr< Resistor > mSubParallelResistor0
Parallel Resistor submodel at Terminal 0.
void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
MNAInterface::List mnaTearGroundComponents() override
void initializeParentFromNodesAndTerminals(Real frequency) override
Derives values from power flow data and pushes them to subcomponents.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Updates internal current variable of the component.
void mnaParentPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
std::shared_ptr< Capacitor > mSubParallelCapacitor1
MatrixFixedSize< 3, 1 > mDirectCapHistory0
MatrixFixedSize< 3, 1 > mDirectInductorHistory
Bool mnaHasCompactRightSideVectorStamp() const override
Composite stamps touch only their cached terminal and virtual-node rows.
std::shared_ptr< Inductor > mSubSeriesInductor
MatrixFixedSize< 3, 3 > mDirectCapAdmittance
void mnaParentApplyRightSideVectorStamp(Matrix &rightVector) override
std::shared_ptr< Capacitor > mSubParallelCapacitor0
Attribute< Matrix >::Ptr mDirectState
MatrixFixedSize< 3, 1 > mDirectCapHistory1
void mnaTearApplyVoltageStamp(Matrix &voltageVector) override
MatrixFixedSize< 3, 1 > mDirectSeriesHistory
std::shared_ptr< Resistor > mSubSeriesResistor
Series Resistor submodel.
void createSubComponents() override
Constructs and registers MNA subcomponents; idempotent.
void mnaTearInitialize(Real omega, Real timeStep) override
void mnaParentInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
void mnaTearPostStep(MatrixComp voltage, MatrixComp current) override
void mnaParentApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
std::shared_ptr< Resistor > mSubParallelResistor1
Parallel resistor submodel at Terminal 1.
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 matrixCompToString(const MatrixComp &mat)
Definition Logger.cpp:37
static String matrixToString(const Matrix &mat)
Definition Logger.cpp:31
static String phasorToString(const Complex &num)
Definition Logger.cpp:57
std::vector< Ptr > List
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)
std::vector< Ptr > List
Definition SimNode.h:35
static Ptr GND
Definition SimNode.h:37
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Real > >::Ptr mIntfCurrent
SimTerminal< Real >::List mTerminals
SimTerminal< Real >::Ptr terminal(UInt index)
void setVirtualNodeNumber(UInt num)
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
std::shared_ptr< SimPowerComp< VarType > > Ptr
SimNode< Real >::List mVirtualNodes
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< PiLine > 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