DPsim
Loading...
Searching...
No Matches
EMT_Ph3_Transformer.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
15 Logger::Level logLevel,
16 Bool withResistiveLosses)
17 : Transformer(uid, name, true, logLevel, withResistiveLosses) {}
18
20 Bool useSubcomponents,
21 Logger::Level logLevel,
22 Bool withResistiveLosses)
24 CompositePowerComp<Real>(uid, name, true, true, logLevel) {
25 mUseSubcomponents = useSubcomponents;
26 mDirectState = mAttributes->create<Matrix>("direct_state");
27 **mDirectState = Matrix::Zero(18, 1);
29 mWithResistiveLosses = withResistiveLosses;
30 if (withResistiveLosses)
32 else
34
36
37 SPDLOG_LOGGER_INFO(mSLog, "Create {} {}", this->type(), name);
38 **mIntfVoltage = Matrix::Zero(3, 1);
39 **mIntfCurrent = Matrix::Zero(1, 1);
40}
41
44 auto copy = Transformer::make(name, mUseSubcomponents, mLogLevel,
45 mWithResistiveLosses);
47 mRatedPower, std::abs(**mRatio), std::arg(**mRatio),
49 copy->setTapRatio(tapRatio());
50 return copy;
51}
52
54 Real nomVoltageSecondary,
55 Real ratedPower, Real ratioAbs,
56 Real ratioPhase, Matrix resistance,
57 Matrix inductance) {
58
59 Base::Ph3::Transformer::setParameters(nomVoltagePrimary, nomVoltageSecondary,
60 ratedPower, ratioAbs, ratioPhase,
61 resistance, inductance);
62
63 SPDLOG_LOGGER_INFO(
64 mSLog,
65 "Nominal Voltage Primary = {} [V] Nominal Voltage Secondary = {} [V]",
67 SPDLOG_LOGGER_INFO(mSLog, "Rated Apparent Power = {} [VA]", mRatedPower);
68 SPDLOG_LOGGER_INFO(mSLog, "Tap Ratio = {} [ ] Phase Shift = {} [deg]",
69 std::abs(effectiveRatio()), std::arg(effectiveRatio()));
70
71 mParametersSet = true;
72}
73
74void EMT::Ph3::Transformer::resolveWindingRoles() {
75 switch (mReferenceWinding) {
77 mReferenceTerminal = 0;
78 break;
80 mReferenceTerminal = 1;
81 break;
83 SPDLOG_LOGGER_ERROR(mSLog,
84 "Transformer {}: three-winding transformers are "
85 "not implemented",
86 this->name());
89 mReferenceTerminal =
90 (mNominalVoltagePrimary >= mNominalVoltageSecondary) ? 0 : 1;
91 break;
92 }
93 mRatioFromReference =
94 (mReferenceTerminal == 0) ? effectiveRatio() : 1. / effectiveRatio();
95 mOrientationSign = (mReferenceTerminal == 0) ? 1. : -1.;
96
97 if (Math::abs(std::arg(mRatioFromReference)) > 1e-9) {
98 SPDLOG_LOGGER_ERROR(mSLog,
99 "Turns ratio {} has a phase shift of {} rad. "
100 "EMT::Ph3::Transformer models in-phase turns ratios "
101 "only; a phase-shifting winding connection is not "
102 "implemented.",
103 Logger::complexToString(mRatioFromReference),
104 std::arg(mRatioFromReference));
106 }
107}
108
110 if (mSubCompCreated)
111 return;
112 mSubCompCreated = true;
113
114 if (!mUseSubcomponents) {
115 resolveWindingRoles();
116 return;
117 }
118
119 resolveWindingRoles();
120
121 auto midpoint = mVirtualNodes[2];
122
123 mSubInductor =
124 std::make_shared<EMT::Ph3::Inductor>(**mName + "_ind", mLogLevel);
125 mSubInductor->setParameters(mInductance / 2.);
128
129 mSubInductor2 =
130 std::make_shared<EMT::Ph3::Inductor>(**mName + "_ind2", mLogLevel);
131 mSubInductor2->setParameters(mInductance / 2.);
134
135 if (mNumVirtualNodes == 5) {
136 mSubResistor =
137 std::make_shared<EMT::Ph3::Resistor>(**mName + "_res", mLogLevel);
138 mSubResistor->setParameters(mResistance / 2.);
139 mSubResistor->connect({node(mReferenceTerminal), mVirtualNodes[3]});
140 mSubInductor->connect({mVirtualNodes[3], midpoint});
143
144 mSubResistor2 =
145 std::make_shared<EMT::Ph3::Resistor>(**mName + "_res2", mLogLevel);
146 mSubResistor2->setParameters(mResistance / 2.);
147 mSubResistor2->connect({midpoint, mVirtualNodes[4]});
148 mSubInductor2->connect({mVirtualNodes[4], mVirtualNodes[0]});
149 addMNASubComponent(mSubResistor2,
152 } else {
153 mSubInductor->connect({node(mReferenceTerminal), midpoint});
154 mSubInductor2->connect({midpoint, mVirtualNodes[0]});
155 }
156
157 if (mRatedPower <= 0) {
158 SPDLOG_LOGGER_WARN(mSLog,
159 "Transformer {}: rated power is {} [VA], so the "
160 "magnetizing branch cannot be sized and is omitted",
161 this->name(), mRatedPower);
162 return;
163 }
164
165 if (mNoLoadCurrent <= mNoLoadLoss) {
166 SPDLOG_LOGGER_ERROR(mSLog,
167 "Transformer {}: no-load current {} must exceed the "
168 "no-load loss {}",
169 this->name(), mNoLoadCurrent, mNoLoadLoss);
171 }
172
173 Real magnetizingResistance =
174 std::pow(nominalVoltageAt(mReferenceTerminal), 2) /
175 (mNoLoadLoss * mRatedPower);
176 mMagnetizingResistance =
177 Math::singlePhaseParameterToThreePhase(magnetizingResistance);
178 mSubMagnetizingResistor =
179 std::make_shared<EMT::Ph3::Resistor>(**mName + "_mag_res", mLogLevel);
180 mSubMagnetizingResistor->setParameters(mMagnetizingResistance);
181 mSubMagnetizingResistor->connect({midpoint, EMT::SimNode::GND});
182 addMNASubComponent(mSubMagnetizingResistor,
185
186 mSubMagnetizingInductor =
187 std::make_shared<EMT::Ph3::Inductor>(**mName + "_mag_ind", mLogLevel);
188 mSubMagnetizingInductor->connect({midpoint, EMT::SimNode::GND});
189 addMNASubComponent(mSubMagnetizingInductor,
192}
193
195 Real frequency) {
196 if (!mUseSubcomponents) {
197 resolveWindingRoles();
198 const Real omega = 2. * PI * frequency;
199 mSeriesInductanceHalf = mInductance / 2.;
200 mSeriesResistanceHalf.setZero();
201 if (mWithResistiveLosses)
202 mSeriesResistanceHalf = mResistance / 2.;
203 if (mRatedPower > 0.) {
204 const Real bmag = std::sqrt(mNoLoadCurrent * mNoLoadCurrent -
205 mNoLoadLoss * mNoLoadLoss) *
207 std::pow(nominalVoltageAt(mReferenceTerminal), 2);
208 const Real rmag = std::pow(nominalVoltageAt(mReferenceTerminal), 2) /
209 (mNoLoadLoss * mRatedPower);
210 mMagnetizingInductance =
211 Math::singlePhaseParameterToThreePhase(1. / (omega * bmag));
212 mMagnetizingResistance = Math::singlePhaseParameterToThreePhase(rmag);
213 mDirectMagnetizingConductance = mMagnetizingResistance.inverse();
214 } else {
215 mMagnetizingInductance = Matrix::Zero(3, 3);
216 mDirectMagnetizingConductance = Matrix::Zero(3, 3);
217 }
218 MatrixComp v0 = MatrixComp::Zero(3, 1), v1 = MatrixComp::Zero(3, 1);
221 v0(1) = v0(0) * SHIFT_TO_PHASE_B;
222 v0(2) = v0(0) * SHIFT_TO_PHASE_C;
223 v1(1) = v1(0) * SHIFT_TO_PHASE_B;
224 v1(2) = v1(0) * SHIFT_TO_PHASE_C;
225 const MatrixComp vref = mReferenceTerminal == 0 ? v0 : v1;
226 const MatrixComp vother = mReferenceTerminal == 0 ? v1 : v0;
227 const MatrixComp vreferred = mRatioFromReference.real() * vother;
228 const MatrixComp zhalf =
229 mSeriesResistanceHalf.cast<Complex>() +
230 Complex(0., omega) * mSeriesInductanceHalf.cast<Complex>();
231 const MatrixComp yseries = zhalf.inverse();
232 MatrixComp ymag = mDirectMagnetizingConductance.cast<Complex>();
233 if (mRatedPower > 0.)
234 ymag += Complex(0., -1. / omega) *
235 mMagnetizingInductance.inverse().cast<Complex>();
236 const MatrixComp midpointInv = (2. * yseries + ymag).inverse();
237 const MatrixComp vmid = midpointInv * yseries * (vref + vreferred);
238 const MatrixComp i0 = yseries * (vmid - vref);
239 const MatrixComp i1 = yseries * (vreferred - vmid);
240 const MatrixComp imag =
241 (ymag - mDirectMagnetizingConductance.cast<Complex>()) * (-vmid);
242 (**mDirectState).setZero();
243 (**mDirectState).block<3, 1>(0, 0) =
244 (Complex(0., omega) * mSeriesInductanceHalf.cast<Complex>() * i0)
245 .real();
246 (**mDirectState).block<3, 1>(3, 0) = i0.real();
247 (**mDirectState).block<3, 1>(6, 0) =
248 (Complex(0., omega) * mSeriesInductanceHalf.cast<Complex>() * i1)
249 .real();
250 (**mDirectState).block<3, 1>(9, 0) = i1.real();
251 const Complex ratioNodeVoltage =
252 initialSingleVoltage(nonReferenceTerminal()) * mRatioFromReference;
253 mVirtualNodes[0]->setInitialVoltage(ratioNodeVoltage);
254 mVirtualNodes[2]->setInitialVoltage(PEAK1PH_TO_RMS3PH * vmid(0));
255 if (mWithResistiveLosses) {
256 const Complex halfImpedance(mResistance(0, 0) / 2.,
257 omega * mInductance(0, 0) / 2.);
258 const Complex referenceCurrent = (vref(0) - vmid(0)) / halfImpedance;
259 const Complex nonReferenceCurrent =
260 (vmid(0) - vreferred(0)) / halfImpedance;
261 mVirtualNodes[3]->setInitialVoltage(
262 initialSingleVoltage(mReferenceTerminal) -
263 PEAK1PH_TO_RMS3PH * referenceCurrent * mResistance(0, 0) / 2.);
264 mVirtualNodes[4]->setInitialVoltage(
265 PEAK1PH_TO_RMS3PH * vmid(0) -
266 PEAK1PH_TO_RMS3PH * nonReferenceCurrent * mResistance(0, 0) / 2.);
267 }
268 if (mRatedPower > 0.) {
269 (**mDirectState).block<3, 1>(12, 0) =
270 (Complex(0., omega) * mMagnetizingInductance.cast<Complex>() * imag)
271 .real();
272 (**mDirectState).block<3, 1>(15, 0) = imag.real();
273 }
274 **mIntfVoltage = (v1 - v0).real();
275 // Match the legacy parent trace at t=0. The direct companion states above
276 // retain the initialized branch currents and are used by the first MNA
277 // step; the legacy composite does not publish a terminal current until its
278 // first post-step.
279 **mIntfCurrent = Matrix::Zero(3, 1);
280 return;
281 }
282 // Set initial voltage of virtual node in between
283 mVirtualNodes[0]->setInitialVoltage(
284 initialSingleVoltage(nonReferenceTerminal()) * mRatioFromReference);
285
286 // Static calculations from load flow data
287 Real omega = 2. * PI * frequency;
288
289 if (mSubMagnetizingInductor) {
290 Real magnetizingSusceptance =
291 std::sqrt(std::pow(mNoLoadCurrent, 2) - std::pow(mNoLoadLoss, 2)) *
292 mRatedPower / std::pow(nominalVoltageAt(mReferenceTerminal), 2);
293 Real magnetizingInductance = 1. / (omega * magnetizingSusceptance);
294 mMagnetizingInductance =
295 Math::singlePhaseParameterToThreePhase(magnetizingInductance);
296 mSubMagnetizingInductor->setParameters(mMagnetizingInductance);
297 SPDLOG_LOGGER_INFO(mSLog, "Magnetizing inductance = {} [H]",
298 Logger::matrixToString(mMagnetizingInductance));
299 }
300
301 MatrixComp impedance = MatrixComp::Zero(3, 3);
302 impedance << Complex(mResistance(0, 0), omega * mInductance(0, 0)),
303 Complex(mResistance(0, 1), omega * mInductance(0, 1)),
304 Complex(mResistance(0, 2), omega * mInductance(0, 2)),
305 Complex(mResistance(1, 0), omega * mInductance(1, 0)),
306 Complex(mResistance(1, 1), omega * mInductance(1, 1)),
307 Complex(mResistance(1, 2), omega * mInductance(1, 2)),
308 Complex(mResistance(2, 0), omega * mInductance(2, 0)),
309 Complex(mResistance(2, 1), omega * mInductance(2, 1)),
310 Complex(mResistance(2, 2), omega * mInductance(2, 2));
311
312 SPDLOG_LOGGER_INFO(mSLog,
313 "Resistance (referred to higher voltage side) = {} [Ohm]",
315 SPDLOG_LOGGER_INFO(mSLog,
316 "Inductance (referred to higher voltage side) = {} [H]",
318 SPDLOG_LOGGER_INFO(mSLog,
319 "Reactance (referred to higher voltage side) = {} [Ohm]",
321
322 MatrixComp vInitABC = MatrixComp::Zero(3, 1);
323 vInitABC(0, 0) =
324 RMS3PH_TO_PEAK1PH * (mVirtualNodes[0]->initialSingleVoltage() -
325 initialSingleVoltage(mReferenceTerminal));
326 vInitABC(1, 0) = vInitABC(0, 0) * SHIFT_TO_PHASE_B;
327 vInitABC(2, 0) = vInitABC(0, 0) * SHIFT_TO_PHASE_C;
328
329 MatrixComp vTerminalABC = MatrixComp::Zero(3, 1);
330 vTerminalABC(0, 0) =
332 vTerminalABC(1, 0) = vTerminalABC(0, 0) * SHIFT_TO_PHASE_B;
333 vTerminalABC(2, 0) = vTerminalABC(0, 0) * SHIFT_TO_PHASE_C;
334
335 MatrixComp iInit = impedance.inverse() * vInitABC;
336 **mIntfCurrent = iInit.real();
337 **mIntfVoltage = vTerminalABC.real();
338
339 Complex halfImpedance =
340 Complex(mResistance(0, 0) / 2., omega * mInductance(0, 0) / 2.);
341 Complex magnetizingAdmittance = Complex(0, 0);
342 if (mSubMagnetizingInductor)
343 magnetizingAdmittance =
344 1. / mMagnetizingResistance(0, 0) +
345 1. / Complex(0, omega * mMagnetizingInductance(0, 0));
346
347 Complex referenceVoltage = initialSingleVoltage(mReferenceTerminal);
348 Complex midpointVoltage =
349 (referenceVoltage + mVirtualNodes[0]->initialSingleVoltage()) /
350 (2. + halfImpedance * magnetizingAdmittance);
351 mVirtualNodes[2]->setInitialVoltage(midpointVoltage);
352
353 if (mNumVirtualNodes == 5) {
354 Complex referenceCurrent =
355 (referenceVoltage - midpointVoltage) / halfImpedance;
356 Complex nonReferenceCurrent =
357 (midpointVoltage - mVirtualNodes[0]->initialSingleVoltage()) /
358 halfImpedance;
359 mVirtualNodes[3]->setInitialVoltage(
360 referenceVoltage - referenceCurrent * mResistance(0, 0) / 2.);
361 mVirtualNodes[4]->setInitialVoltage(
362 midpointVoltage - nonReferenceCurrent * mResistance(0, 0) / 2.);
363 }
364
365 SPDLOG_LOGGER_INFO(
366 mSLog,
367 "\n--- Initialization from powerflow ---"
368 "\nVoltage across: {:s}"
369 "\nCurrent: {:s}"
370 "\nTerminal 0 voltage: {:s}"
371 "\nTerminal 1 voltage: {:s}"
372 "\nVirtual Node 1 voltage: {:s}"
373 "\n--- Initialization from powerflow finished ---",
380}
381
383 Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
384 if (!mUseSubcomponents) {
386 mDirectInductorAdmittance =
387 (timeStep / 2.) * mSeriesInductanceHalf.inverse();
388 mDirectMagnetizingInductorAdmittance.setZero();
389 if (mRatedPower > 0.)
390 mDirectMagnetizingInductorAdmittance =
391 (timeStep / 2.) * mMagnetizingInductance.inverse();
392 return;
393 }
394 SPDLOG_LOGGER_INFO(
395 mSLog,
396 "\nTerminal 0 connected to {:s} = sim node {:d}"
397 "\nTerminal 1 connected to {:s} = sim node {:d}",
400}
401
403 SparseMatrixRow &systemMatrix) {
404 if (!mUseSubcomponents) {
406 auto stampIdealTransformer = [&]() {
407 if (terminalNotGrounded(mReferenceTerminal))
408 for (UInt phase = 0; phase < 3; ++phase) {
409 const UInt row0 =
410 mVirtualNodes[0]->matrixNodeIndex(static_cast<PhaseType>(phase));
411 const UInt row1 =
412 mVirtualNodes[1]->matrixNodeIndex(static_cast<PhaseType>(phase));
413 Math::setMatrixElement(systemMatrix, row0, row1, -1.);
414 Math::setMatrixElement(systemMatrix, row1, row0, 1.);
415 }
416 const UInt other = nonReferenceTerminal();
417 if (terminalNotGrounded(other))
418 for (UInt phase = 0; phase < 3; ++phase) {
419 const UInt terminalIndex = matrixNodeIndex(other, phase);
420 const UInt idealIndex =
421 mVirtualNodes[1]->matrixNodeIndex(static_cast<PhaseType>(phase));
422 Math::setMatrixElement(systemMatrix, terminalIndex, idealIndex,
423 mRatioFromReference.real());
424 Math::setMatrixElement(systemMatrix, idealIndex, terminalIndex,
425 -mRatioFromReference.real());
426 }
427 };
428 stampIdealTransformer();
429 const UInt midpoint = mVirtualNodes[2]->matrixNodeIndex(PhaseType::A);
430 const UInt ratioNode = mVirtualNodes[0]->matrixNodeIndex(PhaseType::A);
431 const bool withLosses = mWithResistiveLosses;
432 const UInt firstInductor =
433 withLosses ? mVirtualNodes[3]->matrixNodeIndex(PhaseType::A)
434 : matrixNodeIndex(mReferenceTerminal);
435 const UInt secondInductor =
436 withLosses ? mVirtualNodes[4]->matrixNodeIndex(PhaseType::A) : midpoint;
437 if (withLosses) {
438 const Matrix halfResistanceConductance = (mResistance / 2.).inverse();
440 halfResistanceConductance, systemMatrix,
441 matrixNodeIndex(mReferenceTerminal), firstInductor,
442 terminalNotGrounded(mReferenceTerminal), true, mSLog);
443 MNAStampUtils::stampConductanceMatrix(halfResistanceConductance,
444 systemMatrix, midpoint,
445 secondInductor, true, true, mSLog);
446 }
447 MNAStampUtils::stampConductanceMatrix(mDirectInductorAdmittance,
448 systemMatrix, firstInductor, midpoint,
449 true, true, mSLog);
450 MNAStampUtils::stampConductanceMatrix(mDirectInductorAdmittance,
451 systemMatrix, secondInductor,
452 ratioNode, true, true, mSLog);
453 MNAStampUtils::stampConductanceMatrix(mDirectMagnetizingConductance,
454 systemMatrix, midpoint, 0, true,
455 false, mSLog);
456 MNAStampUtils::stampConductanceMatrix(mDirectMagnetizingInductorAdmittance,
457 systemMatrix, midpoint, 0, true,
458 false, mSLog);
459 return;
460 }
461 // Ideal transformer equations
462 if (terminalNotGrounded(mReferenceTerminal)) {
464 systemMatrix, mVirtualNodes[0]->matrixNodeIndex(PhaseType::A),
467 systemMatrix, mVirtualNodes[0]->matrixNodeIndex(PhaseType::B),
470 systemMatrix, mVirtualNodes[0]->matrixNodeIndex(PhaseType::C),
472
473 Math::setMatrixElement(systemMatrix,
476 Math::setMatrixElement(systemMatrix,
479 Math::setMatrixElement(systemMatrix,
482 }
483 if (terminalNotGrounded(nonReferenceTerminal())) {
484 Math::setMatrixElement(systemMatrix,
485 matrixNodeIndex(nonReferenceTerminal(), 0),
487 mRatioFromReference.real());
488 Math::setMatrixElement(systemMatrix,
489 matrixNodeIndex(nonReferenceTerminal(), 1),
491 mRatioFromReference.real());
492 Math::setMatrixElement(systemMatrix,
493 matrixNodeIndex(nonReferenceTerminal(), 2),
495 mRatioFromReference.real());
496 Math::setMatrixElement(systemMatrix,
498 matrixNodeIndex(nonReferenceTerminal(), 0),
499 -mRatioFromReference.real());
500 Math::setMatrixElement(systemMatrix,
502 matrixNodeIndex(nonReferenceTerminal(), 1),
503 -mRatioFromReference.real());
504 Math::setMatrixElement(systemMatrix,
506 matrixNodeIndex(nonReferenceTerminal(), 2),
507 -mRatioFromReference.real());
508 }
509
510 // Add subcomps to system matrix
511 for (auto subcomp : mSubComponents)
512 if (auto mnasubcomp = std::dynamic_pointer_cast<MNAInterface>(subcomp))
513 mnasubcomp->mnaApplySystemMatrixStamp(systemMatrix);
514
515 if (terminalNotGrounded(0)) {
516 SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
520 SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
524 SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
528
529 SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
533 SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
537 SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
541 }
542 if (terminalNotGrounded(1)) {
543 SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
544 Logger::complexToString(mRatioFromReference),
545 matrixNodeIndex(1, 0),
547 SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
548 Logger::complexToString(mRatioFromReference),
549 matrixNodeIndex(1, 1),
551 SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
552 Logger::complexToString(mRatioFromReference),
553 matrixNodeIndex(1, 2),
555
556 SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
557 Logger::complexToString(-mRatioFromReference),
559 matrixNodeIndex(1, 0));
560 SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
561 Logger::complexToString(-mRatioFromReference),
563 matrixNodeIndex(1, 1));
564 SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
565 Logger::complexToString(-mRatioFromReference),
567 matrixNodeIndex(1, 2));
568 }
569}
570
572 AttributeBase::List &prevStepDependencies,
573 AttributeBase::List &attributeDependencies,
574 AttributeBase::List &modifiedAttributes) {
575 if (!mUseSubcomponents)
576 prevStepDependencies.push_back(mDirectState);
577 else {
578 prevStepDependencies.push_back(mIntfCurrent);
579 prevStepDependencies.push_back(mIntfVoltage);
580 }
581 modifiedAttributes.push_back(mRightVector);
582}
583
585 if (!mUseSubcomponents) {
586 const auto &state = **mDirectState;
587 mDirectHistory0 = mDirectInductorAdmittance * state.block<3, 1>(0, 0) +
588 state.block<3, 1>(3, 0);
589 mDirectHistory1 = mDirectInductorAdmittance * state.block<3, 1>(6, 0) +
590 state.block<3, 1>(9, 0);
591 mDirectHistoryMag =
592 mDirectMagnetizingInductorAdmittance * state.block<3, 1>(12, 0) +
593 state.block<3, 1>(15, 0);
594 }
596}
597
599 Matrix &rightVector) {
600 if (mUseSubcomponents)
601 return;
602 const UInt midpoint = mVirtualNodes[2]->matrixNodeIndex(PhaseType::A);
603 const UInt ratioNode = mVirtualNodes[0]->matrixNodeIndex(PhaseType::A);
604 const UInt firstInductor =
605 mWithResistiveLosses ? mVirtualNodes[3]->matrixNodeIndex(PhaseType::A)
606 : matrixNodeIndex(mReferenceTerminal);
607 const UInt secondInductor =
608 mWithResistiveLosses ? mVirtualNodes[4]->matrixNodeIndex(PhaseType::A)
609 : midpoint;
610 for (UInt phase = 0; phase < 3; ++phase) {
611 const UInt midpointIndex = midpoint + phase;
612 const UInt ratioIndex = ratioNode + phase;
613 const UInt firstIndex = firstInductor + phase;
614 const UInt secondIndex = secondInductor + phase;
615 Math::setVectorElement(rightVector, firstIndex, mDirectHistory0(phase));
616 // Without explicit series-resistance nodes, the second inductor starts
617 // at the midpoint too. Accumulate both branch-history contributions into
618 // that one row; writing secondIndex separately would overwrite the first
619 // inductor's midpoint contribution (secondIndex == midpointIndex).
621 rightVector, midpointIndex,
622 -mDirectHistory0(phase) +
623 (mWithResistiveLosses ? 0. : mDirectHistory1(phase)) +
624 mDirectHistoryMag(phase));
625 if (mWithResistiveLosses)
626 Math::setVectorElement(rightVector, secondIndex, mDirectHistory1(phase));
627 Math::setVectorElement(rightVector, ratioIndex, -mDirectHistory1(phase));
628 }
629}
630
635
637 Matrix &rightVector) {
638 if (mUseSubcomponents) {
640 return;
641 }
642 const UInt midpoint = mVirtualNodes[2]->matrixNodeIndex(PhaseType::A);
643 const UInt ratioNode = mVirtualNodes[0]->matrixNodeIndex(PhaseType::A);
644 const UInt firstInductor =
645 mWithResistiveLosses ? mVirtualNodes[3]->matrixNodeIndex(PhaseType::A)
646 : matrixNodeIndex(mReferenceTerminal);
647 const UInt secondInductor =
648 mWithResistiveLosses ? mVirtualNodes[4]->matrixNodeIndex(PhaseType::A)
649 : midpoint;
650 for (UInt phase = 0; phase < 3; ++phase) {
651 const UInt midpointIndex = midpoint + phase;
652 const UInt ratioIndex = ratioNode + phase;
653 const UInt firstIndex = firstInductor + phase;
654 const UInt secondIndex = secondInductor + phase;
655 if (mWithResistiveLosses || terminalNotGrounded(mReferenceTerminal))
656 Math::addToVectorElement(rightVector, firstIndex, mDirectHistory0(phase));
658 rightVector, midpointIndex,
659 -mDirectHistory0(phase) +
660 (mWithResistiveLosses ? 0. : mDirectHistory1(phase)) +
661 mDirectHistoryMag(phase));
662 if (mWithResistiveLosses)
663 Math::addToVectorElement(rightVector, secondIndex,
664 mDirectHistory1(phase));
665 Math::addToVectorElement(rightVector, ratioIndex, -mDirectHistory1(phase));
666 }
667}
668
670 AttributeBase::List &prevStepDependencies,
671 AttributeBase::List &attributeDependencies,
672 AttributeBase::List &modifiedAttributes,
673 Attribute<Matrix>::Ptr &leftVector) {
674 attributeDependencies.push_back(leftVector);
675 modifiedAttributes.push_back(mIntfVoltage);
676 modifiedAttributes.push_back(mIntfCurrent);
677 if (!mUseSubcomponents)
678 modifiedAttributes.push_back(mDirectState);
679}
680
682 Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
683 if (!mUseSubcomponents) {
684 Eigen::Matrix<Real, 3, 1> v0 = Eigen::Matrix<Real, 3, 1>::Zero();
685 Eigen::Matrix<Real, 3, 1> v1 = Eigen::Matrix<Real, 3, 1>::Zero();
686 Eigen::Matrix<Real, 3, 1> virtualVoltage[5];
687 for (auto &voltage : virtualVoltage)
688 voltage.setZero();
689 for (UInt phase = 0; phase < 3; ++phase) {
690 if (terminalNotGrounded(0))
691 v0(phase) = Math::realFromVectorElement(**leftVector,
692 matrixNodeIndex(0, phase));
693 if (terminalNotGrounded(1))
694 v1(phase) = Math::realFromVectorElement(**leftVector,
695 matrixNodeIndex(1, phase));
696 for (UInt node = 0; node < mVirtualNodes.size(); ++node)
697 virtualVoltage[node](phase) = Math::realFromVectorElement(
698 **leftVector, mVirtualNodes[node]->matrixNodeIndex(
699 static_cast<PhaseType>(phase)));
700 }
701 const auto vmid = virtualVoltage[2];
702 const auto firstEndpoint = mWithResistiveLosses
703 ? virtualVoltage[3]
704 : (mReferenceTerminal == 0 ? v0 : v1);
705 const auto secondEndpoint = mWithResistiveLosses ? virtualVoltage[4] : vmid;
706 const auto vratio = virtualVoltage[0];
707 const auto vInductor0 = vmid - firstEndpoint;
708 const auto vInductor1 = vratio - secondEndpoint;
709 const auto i0 = mDirectInductorAdmittance * vInductor0 + mDirectHistory0;
710 const auto i1 = mDirectInductorAdmittance * vInductor1 + mDirectHistory1;
711 const auto imag =
712 mDirectMagnetizingInductorAdmittance * (-vmid) + mDirectHistoryMag;
713 (**mDirectState).block<3, 1>(0, 0) = vInductor0;
714 (**mDirectState).block<3, 1>(3, 0) = i0;
715 (**mDirectState).block<3, 1>(6, 0) = vInductor1;
716 (**mDirectState).block<3, 1>(9, 0) = i1;
717 (**mDirectState).block<3, 1>(12, 0) = -vmid;
718 (**mDirectState).block<3, 1>(15, 0) = imag;
719 **mIntfVoltage = v1 - v0;
720 **mIntfCurrent = mOrientationSign * i0;
721 return;
722 }
723 mnaCompUpdateVoltage(**leftVector);
724 mnaCompUpdateCurrent(**leftVector);
725}
726
728 if (!mUseSubcomponents) {
729 **mIntfCurrent = mOrientationSign * (**mDirectState).block<3, 1>(3, 0);
730 return;
731 }
732 **mIntfCurrent = mOrientationSign * mSubInductor->intfCurrent();
733}
734
736 // v1 - v0
737 (**mIntfVoltage).setZero();
738 if (terminalNotGrounded(1)) {
739 (**mIntfVoltage)(0, 0) =
741 (**mIntfVoltage)(1, 0) =
743 (**mIntfVoltage)(2, 0) =
745 }
746 if (terminalNotGrounded(0)) {
747 (**mIntfVoltage)(0, 0) =
748 (**mIntfVoltage)(0, 0) -
750 (**mIntfVoltage)(1, 0) =
751 (**mIntfVoltage)(1, 0) -
753 (**mIntfVoltage)(2, 0) =
754 (**mIntfVoltage)(2, 0) -
756 }
757}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
Real mNominalVoltagePrimary
Nominal voltage of primary side.
Real mNominalVoltageSecondary
Nominal voltage of secondary side.
const Attribute< Complex >::Ptr mRatio
Transformer ratio.
void setParameters(Real nomVoltagePrimary, Real nomVoltageSecondary, Real ratedPower, Real ratioAbs, Real ratioPhase, Matrix resistance, Matrix inductance)
Matrix mResistance
Resistance [Ohm].
Matrix mInductance
Inductance [H].
Real nominalVoltageAt(UInt terminal) const
Nominal voltage of the winding at the given terminal.
Real mRatedPower
Rated Apparent Power [VA].
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)
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
Transformer(String uid, String name, Logger::Level logLevel=Logger::Level::off, Bool withResistiveLosses=false)
Defines UID, name and logging level.
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamps system matrix.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Updates internal voltage variable of the component.
SimPowerComp< Real >::Ptr clone(String name) override
DEPRECATED: Delete method.
void initializeParentFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
void createSubComponents() override
Constructs and registers MNA subcomponents; idempotent.
void setParameters(Real nomVoltagePrimary, Real nomVoltageSecondary, Real ratedPower, Real ratioAbs, Real ratioPhase, Matrix resistance, Matrix inductance)
Defines component parameters.
void mnaParentApplyRightSideVectorStamp(Matrix &rightVector) override
void mnaParentPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Updates internal current variable of the component.
void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
Bool mnaHasCompactRightSideVectorStamp() const override
Composite stamps touch only their cached terminal and virtual-node rows.
void mnaAddCompactRightSideVectorStamp(Matrix &rightVector) override
void mnaParentInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
Initializes internal variables of the component.
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 complexToString(const Complex &num)
Definition Logger.cpp:63
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 void setMatrixElement(SparseMatrixRow &mat, Matrix::Index row, Matrix::Index column, Complex value, Int maxFreq=1, Int freqIdx=0)
static Matrix singlePhaseParameterToThreePhase(Real parameter)
To convert single phase parameters to symmetrical three phase ones.
static Real abs(Complex value)
Definition MathUtils.cpp:71
static Ptr GND
Definition SimNode.h:37
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Real > >::Ptr mIntfCurrent
SimTerminal< Real >::List mTerminals
SimNode< Real >::Ptr node(UInt index)
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.
UInt mNumVirtualNodes
Determines the number of virtual or internal Nodes.
bool mParametersSet
Flag indicating that parameters are set via setParameters() function.
Logger::Log mSLog
Component logger.
static std::shared_ptr< Transformer > 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