DPsim
Loading...
Searching...
No Matches
DP_Ph3_Switch.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Institute for Automation of Complex Power Systems, EONERC, RWTH Aachen University
2// SPDX-License-Identifier: MPL-2.0
3
4#include <algorithm>
5#include <cmath>
6#include <stdexcept>
7
9
10using namespace CPS;
11
13 : MNASimPowerComp<Complex>(uid, name, false, true, logLevel),
15 mOpeningRequested(mAttributes->create<Bool>("opening_requested")),
16 mPoleClosedA(mAttributes->create<Bool>("pole_closed_a")),
17 mPoleClosedB(mAttributes->create<Bool>("pole_closed_b")),
18 mPoleClosedC(mAttributes->create<Bool>("pole_closed_c")),
19 mInstantCurrentA(mAttributes->create<Real>("i_instantaneous_a")),
20 mInstantCurrentB(mAttributes->create<Real>("i_instantaneous_b")),
21 mInstantCurrentC(mAttributes->create<Real>("i_instantaneous_c")),
22 mZeroCrossingTimeA(mAttributes->create<Real>("zero_crossing_time_a")),
23 mZeroCrossingTimeB(mAttributes->create<Real>("zero_crossing_time_b")),
24 mZeroCrossingTimeC(mAttributes->create<Real>("zero_crossing_time_c")),
26 mAttributes->create<Bool>("exponential_transition_active")),
28 mAttributes->create<Bool>("exponential_transition_closing")),
29 mExponentialProgress(mAttributes->create<Real>("exponential_progress")),
31 mAttributes->create<Real>("exponential_transition_start_time")),
33 mAttributes->create<Real>("exponential_transition_end_time")),
35 mAttributes->create<Real>("effective_resistance_a")),
37 mAttributes->create<Real>("effective_resistance_b")),
39 mAttributes->create<Real>("effective_resistance_c")) {
42
43 **mIntfVoltage = MatrixComp::Zero(3, 1);
44 **mIntfCurrent = MatrixComp::Zero(3, 1);
45
46 **mOpeningRequested = false;
47 setAllPoles(false);
48
49 **mInstantCurrentA = 0.0;
50 **mInstantCurrentB = 0.0;
51 **mInstantCurrentC = 0.0;
52
53 resetZeroCrossingTimes();
54
63}
64
66 auto copy = Switch::make(name, mLogLevel);
67 copy->setParameters(**mOpenResistance, **mClosedResistance, **mIsClosed);
68 copy->setSwitchingMode(mSwitchingMode);
69 copy->setZeroCrossingTolerance(mZeroCrossingTolerance);
70 copy->setExponentialSwitchingTime(mExponentialSwitchingTime);
71 return copy;
72}
73
75 mSwitchingMode = mode;
76
77 setAllPoles(**mIsClosed);
78 **mOpeningRequested = false;
79 resetZeroCrossingTimes();
80
81 mPreviousCurrentValid = false;
82 mResetZeroCrossingHistory = false;
83
84 resetExponentialTransition();
85 synchronizeEffectiveResistance(**mIsClosed);
86}
87
89 if (tolerance < 0.0) {
90 throw std::invalid_argument(
91 "DP::Ph3::Switch zero-crossing tolerance must be non-negative.");
92 }
93
94 mZeroCrossingTolerance = tolerance;
95}
96
98 if (switchingTime <= 0.0) {
99 throw std::invalid_argument(
100 "DP::Ph3::Switch exponential switching time must be positive.");
101 }
102
103 mExponentialSwitchingTime = switchingTime;
104}
105
108
109 // An opening command during a closing ramp abandons that ramp. The poles are
110 // still open at that point, so the switch simply stays open.
112 resetExponentialTransition();
113 setAllPoles(false);
114 synchronizeEffectiveResistance(false);
115 **mOpeningRequested = false;
116
117 SPDLOG_LOGGER_INFO(mSLog,
118 "DP opening command received during an exponential "
119 "closing transition. Transition abandoned.");
120 return;
121 }
122
123 if (mSwitchingMode == SwitchingMode::Ideal) {
124 **mOpeningRequested = false;
125 setAllPoles(false);
126 resetZeroCrossingTimes();
127 resetExponentialTransition();
128 synchronizeEffectiveResistance(false);
129 return;
130 }
131
132 if (allPolesOpen()) {
133 **mOpeningRequested = false;
134 return;
135 }
136
137 if (mSwitchingMode == SwitchingMode::CurrentZero) {
138 **mOpeningRequested = true;
139 resetZeroCrossingTimes();
140 resetExponentialTransition();
141 mResetZeroCrossingHistory = true;
142
143 SPDLOG_LOGGER_INFO(
144 mSLog, "DP current-zero opening command received. "
145 "Waiting for reconstructed physical phase-current zeros.");
146 return;
147 }
148
149 validateExponentialResistanceParameters();
150 **mOpeningRequested = true;
151 resetZeroCrossingTimes();
152 resetExponentialTransition();
154 synchronizeEffectiveResistance(true);
155 setAllPoles(true);
156
157 SPDLOG_LOGGER_INFO(mSLog,
158 "DP exponential ZCS-emulation opening command received. "
159 "Switching duration={:.6e}s.",
160 mExponentialSwitchingTime);
161}
162
165
166 **mOpeningRequested = false;
167 resetZeroCrossingTimes();
168 mResetZeroCrossingHistory = false;
169
170 if (mSwitchingMode != SwitchingMode::ExponentialZCSEmulation) {
171 setAllPoles(true);
172 resetExponentialTransition();
173 synchronizeEffectiveResistance(true);
174
175 SPDLOG_LOGGER_INFO(
176 mSLog, "DP switch closing command: all three physical poles closed.");
177 return;
178 }
179
180 if (allPolesClosed() && !**mExponentialTransitionActive) {
181 resetExponentialTransition();
182 synchronizeEffectiveResistance(true);
183 return;
184 }
185
186 // Energising a branch is the mirror image of interrupting it: the same
187 // resistance path is traversed from R_open towards R_closed. The DP envelope
188 // cannot represent the voltage step across a capacitance any more than it can
189 // represent an interrupted inductive current, so closing is ramped as well.
190 validateExponentialResistanceParameters();
191 resetExponentialTransition();
194 **mExponentialProgress = 1.0;
195 synchronizeEffectiveResistance(false);
196 setAllPoles(false);
197
198 SPDLOG_LOGGER_INFO(mSLog,
199 "DP exponential ZCS-emulation closing command received. "
200 "Switching duration={:.6e}s.",
201 mExponentialSwitchingTime);
202}
203
205 mTerminals[0]->setPhaseType(PhaseType::ABC);
206 mTerminals[1]->setPhaseType(PhaseType::ABC);
207
208 Matrix impedance = (**mIsClosed) ? **mClosedResistance : **mOpenResistance;
209
210 // DP::Ph3 MNA quantities are phase-peak complex envelopes. Power-flow
211 // node voltages are line-line RMS, so convert the A-phase voltage to
212 // phase peak and construct the balanced B/C envelopes explicitly.
213 MatrixComp vInitABC = MatrixComp::Zero(3, 1);
214 vInitABC(0, 0) =
216 vInitABC(1, 0) = vInitABC(0, 0) * SHIFT_TO_PHASE_B;
217 vInitABC(2, 0) = vInitABC(0, 0) * SHIFT_TO_PHASE_C;
218
219 **mIntfVoltage = vInitABC;
220 **mIntfCurrent = impedance.inverse() * vInitABC;
221
222 setAllPoles(**mIsClosed);
223 **mOpeningRequested = false;
224 resetZeroCrossingTimes();
225 resetExponentialTransition();
226 synchronizeEffectiveResistance(**mIsClosed);
227
228 SPDLOG_LOGGER_INFO(mSLog,
229 "\n--- Initialization from powerflow ---"
230 "\nVoltage envelope:"
231 "\n{:s}"
232 "\nCurrent envelope:"
233 "\n{:s}"
234 "\nTerminal 0 voltage envelope:"
235 "\n{:s}"
236 "\nTerminal 1 voltage envelope:"
237 "\n{:s}"
238 "\nSwitching mode: {:s}"
239 "\nInitial breaker state: {:s}"
240 "\n--- Initialization from powerflow finished ---",
245 mSwitchingMode == SwitchingMode::Ideal
246 ? "Ideal"
247 : (mSwitchingMode == SwitchingMode::CurrentZero
248 ? "CurrentZero"
249 : "ExponentialZCSEmulation"),
250 **mIsClosed ? "closed" : "open");
251}
252
254 Attribute<Matrix>::Ptr leftVector) {
256 mTimeStep = timeStep;
257 **mRightVector = Matrix::Zero(0, 0);
258
259 // omega is the DP reference-frame angular frequency supplied by the solver.
260 mShiftOmega = omega;
261
262 // Final safeguard in case setSwitchingMode() was called before setParameters().
263 setAllPoles(**mIsClosed);
264 synchronizeEffectiveResistance(**mIsClosed);
265 resetExponentialTransition();
266
267 mPoleClosedPrev = {{
268 **mPoleClosedA,
269 **mPoleClosedB,
270 **mPoleClosedC,
271 }};
272
274
275 const Matrix instantaneous = reconstructInstantaneousCurrent(0.0);
276
277 setInstantaneousCurrentAttributes(instantaneous);
278
279 mPreviousInstantaneousCurrent = instantaneous;
280 mEffectiveResistancePrev = {{
284 }};
285
286 mPreviousCurrentTime = 0.0;
287 mPreviousCurrentValid = true;
288 mResetZeroCrossingHistory = false;
289}
290
292 Real omega, Real timeStep,
293 std::vector<Attribute<Matrix>::Ptr> leftVectors) {
294 // Harmonic initialization bypasses mnaCompInitialize() in the shared MNA
295 // wrapper. Reuse its setup so the switch has valid node indices before the
296 // solver stamps each precomputed switch/frequency matrix.
297 mnaCompInitialize(omega, timeStep,
298 leftVectors.empty() ? Attribute<Matrix>::Ptr{}
299 : leftVectors.front());
300}
301
303 if (mSwitchingMode == SwitchingMode::Ideal)
304 return **mIsClosed;
305
306 return allPolesClosed();
307}
308
312
314 SparseMatrixRow &systemMatrix) {
315 const MatrixFixedSizeComp<3, 3> conductance = currentConductanceMatrix();
316
318 conductance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1),
320}
321
323 Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) {
324 // Conventional precomputed two-state path. CurrentZero mode bypasses this
325 // by returning supportsPrecomputedSystemMatrices() == false.
327 conductance.real() =
328 closed ? (**mClosedResistance).inverse() : (**mOpenResistance).inverse();
329
331 conductance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1),
333}
334
336 AttributeBase::List &prevStepDependencies,
337 AttributeBase::List &attributeDependencies,
338 AttributeBase::List &modifiedAttributes,
339 Attribute<Matrix>::Ptr &leftVector) {
340 attributeDependencies.push_back(leftVector);
341
342 modifiedAttributes.push_back(mIntfVoltage);
343 modifiedAttributes.push_back(mIntfCurrent);
344
345 modifiedAttributes.push_back(mOpeningRequested);
346 modifiedAttributes.push_back(mPoleClosedA);
347 modifiedAttributes.push_back(mPoleClosedB);
348 modifiedAttributes.push_back(mPoleClosedC);
349
350 modifiedAttributes.push_back(mInstantCurrentA);
351 modifiedAttributes.push_back(mInstantCurrentB);
352 modifiedAttributes.push_back(mInstantCurrentC);
353
354 modifiedAttributes.push_back(mZeroCrossingTimeA);
355 modifiedAttributes.push_back(mZeroCrossingTimeB);
356 modifiedAttributes.push_back(mZeroCrossingTimeC);
357
358 modifiedAttributes.push_back(mExponentialTransitionActive);
359 modifiedAttributes.push_back(mExponentialTransitionClosing);
360 modifiedAttributes.push_back(mExponentialProgress);
361 modifiedAttributes.push_back(mExponentialTransitionStartTime);
362 modifiedAttributes.push_back(mExponentialTransitionEndTime);
363 modifiedAttributes.push_back(mEffectiveResistanceA);
364 modifiedAttributes.push_back(mEffectiveResistanceB);
365 modifiedAttributes.push_back(mEffectiveResistanceC);
366}
367
369 Attribute<Matrix>::Ptr &leftVector) {
370 mnaCompUpdateVoltage(**leftVector);
371 mnaCompUpdateCurrent(**leftVector);
372
373 const Matrix instantaneous = reconstructInstantaneousCurrent(time);
374
375 setInstantaneousCurrentAttributes(instantaneous);
376
377 if (mSwitchingMode == SwitchingMode::CurrentZero) {
378 updateZeroCrossingState(time);
379 } else if (mSwitchingMode == SwitchingMode::ExponentialZCSEmulation) {
380 updateExponentialTransition(time);
381 }
382}
383
385 (**mIntfVoltage).setZero();
386
387 if (terminalNotGrounded(1)) {
388 (**mIntfVoltage)(0, 0) =
390 (**mIntfVoltage)(1, 0) =
392 (**mIntfVoltage)(2, 0) =
394 }
395
396 if (terminalNotGrounded(0)) {
397 (**mIntfVoltage)(0, 0) -=
399 (**mIntfVoltage)(1, 0) -=
401 (**mIntfVoltage)(2, 0) -=
403 }
404}
405
407 **mIntfCurrent = currentConductanceMatrix() * **mIntfVoltage;
408}
409
411 if (mSwitchingMode == SwitchingMode::Ideal) {
412 if (mIsClosedPrev != **mIsClosed) {
414 return true;
415 }
416
417 return false;
418 }
419
420 if (mSwitchingMode == SwitchingMode::ExponentialZCSEmulation) {
421 const std::array<Real, 3> resistanceNow{{
425 }};
426
427 Bool changed = false;
428 for (UInt phase = 0; phase < 3; ++phase) {
429 if (resistanceNow[phase] != mEffectiveResistancePrev[phase]) {
430 mEffectiveResistancePrev[phase] = resistanceNow[phase];
431 changed = true;
432 }
433 }
434 return changed;
435 }
436
437 const std::array<Bool, 3> poleClosedNow{{
438 **mPoleClosedA,
439 **mPoleClosedB,
440 **mPoleClosedC,
441 }};
442
443 Bool changed = false;
444
445 for (UInt phase = 0; phase < 3; ++phase) {
446 if (poleClosedNow[phase] != mPoleClosedPrev[phase]) {
447 mPoleClosedPrev[phase] = poleClosedNow[phase];
448 changed = true;
449 }
450 }
451
452 return changed;
453}
454
455Bool DP::Ph3::Switch::poleClosed(UInt phase) const {
456 switch (phase) {
457 case 0:
458 return **mPoleClosedA;
459 case 1:
460 return **mPoleClosedB;
461 case 2:
462 return **mPoleClosedC;
463 default:
464 throw std::out_of_range("DP::Ph3::Switch phase index must be 0, 1, or 2.");
465 }
466}
467
468void DP::Ph3::Switch::setPoleClosed(UInt phase, Bool closed) {
469 switch (phase) {
470 case 0:
471 **mPoleClosedA = closed;
472 break;
473 case 1:
474 **mPoleClosedB = closed;
475 break;
476 case 2:
477 **mPoleClosedC = closed;
478 break;
479 default:
480 throw std::out_of_range("DP::Ph3::Switch phase index must be 0, 1, or 2.");
481 }
482
483 // Keep the resistance diagnostics synchronized with the discrete
484 // CurrentZero pole state. Exponential mode manages R(t) independently.
485 if (mSwitchingMode == SwitchingMode::CurrentZero &&
486 (**mClosedResistance).rows() >= 3 && (**mOpenResistance).rows() >= 3) {
487 setEffectiveResistance(phase, closed ? (**mClosedResistance)(phase, phase)
488 : (**mOpenResistance)(phase, phase));
489 }
490}
491
492void DP::Ph3::Switch::setAllPoles(Bool closed) {
493 **mPoleClosedA = closed;
494 **mPoleClosedB = closed;
495 **mPoleClosedC = closed;
496}
497
498Bool DP::Ph3::Switch::allPolesClosed() const {
499 return **mPoleClosedA && **mPoleClosedB && **mPoleClosedC;
500}
501
502Bool DP::Ph3::Switch::allPolesOpen() const {
503 return !**mPoleClosedA && !**mPoleClosedB && !**mPoleClosedC;
504}
505
506void DP::Ph3::Switch::resetZeroCrossingTimes() {
507 **mZeroCrossingTimeA = -1.0;
508 **mZeroCrossingTimeB = -1.0;
509 **mZeroCrossingTimeC = -1.0;
510}
511
512Real DP::Ph3::Switch::effectiveResistance(UInt phase) const {
513 switch (phase) {
514 case 0:
515 return **mEffectiveResistanceA;
516 case 1:
517 return **mEffectiveResistanceB;
518 case 2:
519 return **mEffectiveResistanceC;
520 default:
521 throw std::out_of_range("DP::Ph3::Switch phase index must be 0, 1, or 2.");
522 }
523}
524
525void DP::Ph3::Switch::setEffectiveResistance(UInt phase, Real resistance) {
526 switch (phase) {
527 case 0:
528 **mEffectiveResistanceA = resistance;
529 return;
530 case 1:
531 **mEffectiveResistanceB = resistance;
532 return;
533 case 2:
534 **mEffectiveResistanceC = resistance;
535 return;
536 default:
537 throw std::out_of_range("DP::Ph3::Switch phase index must be 0, 1, or 2.");
538 }
539}
540
541void DP::Ph3::Switch::synchronizeEffectiveResistance(Bool closed) {
542 if ((**mClosedResistance).rows() < 3 || (**mOpenResistance).rows() < 3)
543 return;
544
545 for (UInt phase = 0; phase < 3; ++phase) {
546 setEffectiveResistance(phase, closed ? (**mClosedResistance)(phase, phase)
547 : (**mOpenResistance)(phase, phase));
548 }
549}
550
551void DP::Ph3::Switch::resetExponentialTransition() {
552 **mExponentialTransitionActive = false;
553 **mExponentialTransitionClosing = false;
554 **mExponentialProgress = 0.0;
555 **mExponentialTransitionStartTime = -1.0;
556 **mExponentialTransitionEndTime = -1.0;
557 mExponentialTransitionStarted = false;
558}
559
560void DP::Ph3::Switch::validateExponentialResistanceParameters() const {
561 if ((**mClosedResistance).rows() < 3 || (**mOpenResistance).rows() < 3) {
562 throw std::invalid_argument(
563 "DP::Ph3::Switch exponential mode requires 3x3 resistance matrices.");
564 }
565
566 if (mExponentialSwitchingTime <= 0.0) {
567 throw std::invalid_argument(
568 "DP::Ph3::Switch exponential switching time must be positive.");
569 }
570
571 for (UInt phase = 0; phase < 3; ++phase) {
572 const Real rClosed = (**mClosedResistance)(phase, phase);
573 const Real rOpen = (**mOpenResistance)(phase, phase);
574
575 if (rClosed <= 0.0 || rOpen <= 0.0) {
576 throw std::invalid_argument(
577 "DP::Ph3::Switch exponential mode requires positive diagonal "
578 "open and closed resistances.");
579 }
580 }
581}
582
583Real DP::Ph3::Switch::exponentialResistance(UInt phase, Real alpha) const {
584 const Real rClosed = (**mClosedResistance)(phase, phase);
585 const Real rOpen = (**mOpenResistance)(phase, phase);
586 const Real boundedAlpha = std::max(0.0, std::min(1.0, alpha));
587
588 return std::exp(std::log(rClosed) +
589 boundedAlpha * (std::log(rOpen) - std::log(rClosed)));
590}
591
592void DP::Ph3::Switch::updateExponentialTransition(Real time) {
593 if (!**mExponentialTransitionActive)
594 return;
595
596 if (!mExponentialTransitionStarted) {
597 mExponentialTransitionStarted = true;
598 **mExponentialTransitionStartTime = time;
599 **mExponentialTransitionEndTime = time + mExponentialSwitchingTime;
600 }
601
602 const Real targetTime = time + mTimeStep;
603 const Real elapsed = (targetTime - **mExponentialTransitionStartTime) /
604 mExponentialSwitchingTime;
605 const Real boundedElapsed = std::max(0.0, std::min(1.0, elapsed));
606
607 const Bool closing = **mExponentialTransitionClosing;
608
609 // alpha is the position on the resistance path: 0 at R_closed, 1 at R_open.
610 const Real alpha = closing ? 1.0 - boundedElapsed : boundedElapsed;
611
612 **mExponentialProgress = alpha;
613
614 for (UInt phase = 0; phase < 3; ++phase)
615 setEffectiveResistance(phase, exponentialResistance(phase, alpha));
616
617 if (boundedElapsed >= 1.0) {
618 synchronizeEffectiveResistance(closing);
619 setAllPoles(closing);
620 **mOpeningRequested = false;
621 **mExponentialTransitionActive = false;
622 **mExponentialTransitionClosing = false;
623
624 SPDLOG_LOGGER_INFO(
625 mSLog,
626 "DP exponential ZCS-emulation {:s} completed: start={:.9f}s, "
627 "end={:.9f}s, duration={:.6e}s.",
628 closing ? "closing" : "opening", **mExponentialTransitionStartTime,
629 **mExponentialTransitionEndTime, mExponentialSwitchingTime);
630 }
631}
632
633MatrixFixedSizeComp<3, 3> DP::Ph3::Switch::currentConductanceMatrix() const {
634 MatrixFixedSizeComp<3, 3> conductance = MatrixFixedSizeComp<3, 3>::Zero();
635
636 if (mSwitchingMode == SwitchingMode::Ideal) {
637 const Matrix &resistance =
638 **mIsClosed ? **mClosedResistance : **mOpenResistance;
639 // Ideal switches are commonly configured with independent phase
640 // resistances. Avoid a general dynamic 3x3 inverse in every post-step for
641 // that case, while preserving full mutual-resistance support below.
642 if (resistance(0, 1) == 0.0 && resistance(0, 2) == 0.0 &&
643 resistance(1, 0) == 0.0 && resistance(1, 2) == 0.0 &&
644 resistance(2, 0) == 0.0 && resistance(2, 1) == 0.0) {
645 conductance(0, 0) = Complex(1.0 / resistance(0, 0), 0.0);
646 conductance(1, 1) = Complex(1.0 / resistance(1, 1), 0.0);
647 conductance(2, 2) = Complex(1.0 / resistance(2, 2), 0.0);
648 } else {
649 conductance.real() = resistance.inverse();
650 }
651 return conductance;
652 }
653
654 for (UInt phase = 0; phase < 3; ++phase) {
655 Real resistance = 0.0;
656
657 if (mSwitchingMode == SwitchingMode::CurrentZero) {
658 resistance = poleClosed(phase) ? (**mClosedResistance)(phase, phase)
659 : (**mOpenResistance)(phase, phase);
660 } else {
661 resistance = effectiveResistance(phase);
662 }
663
664 conductance(phase, phase) = Complex(1.0 / resistance, 0.0);
665 }
666
667 return conductance;
668}
669
670Matrix DP::Ph3::Switch::reconstructInstantaneousCurrent(Real time) const {
671 Matrix instantaneous = Matrix::Zero(3, 1);
672
673 // DP::Ph3 quantities are phase-peak complex envelopes. Restore the removed
674 // reference-frequency carrier; no additional RMS-to-peak scaling is needed.
675 const Complex carrier = std::polar<Real>(1.0, mShiftOmega * time);
676
677 for (UInt phase = 0; phase < 3; ++phase) {
678 instantaneous(phase, 0) = std::real((**mIntfCurrent)(phase, 0) * carrier);
679 }
680
681 return instantaneous;
682}
683
684void DP::Ph3::Switch::setInstantaneousCurrentAttributes(const Matrix &current) {
685 **mInstantCurrentA = current(0, 0);
686 **mInstantCurrentB = current(1, 0);
687 **mInstantCurrentC = current(2, 0);
688}
689
690Bool DP::Ph3::Switch::currentCrossedZero(Real previousCurrent,
691 Real current) const {
692 if (std::abs(current) <= mZeroCrossingTolerance)
693 return true;
694
695 return (previousCurrent > mZeroCrossingTolerance &&
696 current < -mZeroCrossingTolerance) ||
697 (previousCurrent < -mZeroCrossingTolerance &&
698 current > mZeroCrossingTolerance);
699}
700
701Real DP::Ph3::Switch::interpolateZeroCrossingTime(Real previousCurrent,
702 Real current,
703 Real previousTime,
704 Real currentTime) const {
705 const Real denominator = std::abs(previousCurrent) + std::abs(current);
706
707 if (denominator <= mZeroCrossingTolerance)
708 return currentTime;
709
710 const Real fraction = std::abs(previousCurrent) / denominator;
711
712 return previousTime + fraction * (currentTime - previousTime);
713}
714
715void DP::Ph3::Switch::setZeroCrossingTime(UInt phase, Real time) {
716 switch (phase) {
717 case 0:
718 **mZeroCrossingTimeA = time;
719 return;
720 case 1:
721 **mZeroCrossingTimeB = time;
722 return;
723 case 2:
724 **mZeroCrossingTimeC = time;
725 return;
726 default:
727 throw std::out_of_range("DP::Ph3::Switch phase index must be 0, 1, or 2.");
728 }
729}
730
731void DP::Ph3::Switch::updateZeroCrossingState(Real time) {
732 const Matrix current = reconstructInstantaneousCurrent(time);
733
734 // Keep a current history while no opening command is active.
735 if (!**mOpeningRequested) {
736 mPreviousInstantaneousCurrent = current;
737 mPreviousCurrentTime = time;
738 mPreviousCurrentValid = true;
739 return;
740 }
741
742 // First post-command sample: do not compare against the pre-command sample.
743 if (mResetZeroCrossingHistory || !mPreviousCurrentValid) {
744 for (UInt phase = 0; phase < 3; ++phase) {
745 if (poleClosed(phase) &&
746 std::abs(current(phase, 0)) <= mZeroCrossingTolerance) {
747 setPoleClosed(phase, false);
748 setZeroCrossingTime(phase, time);
749
750 SPDLOG_LOGGER_INFO(
751 mSLog,
752 "DP phase {} opened at sampled physical current zero: "
753 "t={:.9f}s, i={:.6e}A",
754 phase, time, current(phase, 0));
755 }
756 }
757
758 mPreviousInstantaneousCurrent = current;
759 mPreviousCurrentTime = time;
760 mPreviousCurrentValid = true;
761 mResetZeroCrossingHistory = false;
762
763 if (allPolesOpen())
764 **mOpeningRequested = false;
765
766 return;
767 }
768
769 for (UInt phase = 0; phase < 3; ++phase) {
770 if (!poleClosed(phase))
771 continue;
772
773 const Real previous = mPreviousInstantaneousCurrent(phase, 0);
774 const Real present = current(phase, 0);
775
776 if (!currentCrossedZero(previous, present))
777 continue;
778
779 const Real zeroTime = interpolateZeroCrossingTime(
780 previous, present, mPreviousCurrentTime, time);
781
782 setPoleClosed(phase, false);
783 setZeroCrossingTime(phase, zeroTime);
784
785 SPDLOG_LOGGER_INFO(mSLog,
786 "DP phase {} physical current zero detected: "
787 "t_z={:.9f}s, i_prev={:.6e}A, i={:.6e}A. "
788 "Pole opened.",
789 phase, zeroTime, previous, present);
790 }
791
792 mPreviousInstantaneousCurrent = current;
793 mPreviousCurrentTime = time;
794 mPreviousCurrentValid = true;
795
796 if (allPolesOpen()) {
797 **mOpeningRequested = false;
798
799 SPDLOG_LOGGER_INFO(mSLog, "DP current-zero interruption completed: "
800 "all three physical breaker poles are open.");
801 }
802}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
const CPS::Attribute< Matrix >::Ptr mClosedResistance
Resistance if switch is closed [ohm].
virtual void closeSwitch()
const CPS::Attribute< Matrix >::Ptr mOpenResistance
Resistance if switch is open [ohm].
virtual void openSwitch()
const CPS::Attribute< Bool >::Ptr mIsClosed
Commanded/global switch state.
const Attribute< Bool >::Ptr mPoleClosedA
Bool hasParameterChanged() override
Returns true if one of the element paramters has changed.
void closeSwitch() override
const Attribute< Bool >::Ptr mPoleClosedB
const Attribute< Real >::Ptr mInstantCurrentA
Reconstructed physical instantaneous currents [A].
void mnaCompUpdateCurrent(const Matrix &leftVector) override
void setZeroCrossingTolerance(Real tolerance)
void setExponentialSwitchingTime(Real switchingTime)
const Attribute< Bool >::Ptr mExponentialTransitionActive
Exponential-transition diagnostics.
const Attribute< Real >::Ptr mEffectiveResistanceA
Effective phase resistances currently stamped into the MNA matrix [ohm].
const Attribute< Real >::Ptr mExponentialTransitionStartTime
const Attribute< Bool >::Ptr mExponentialTransitionClosing
True while the active exponential transition is a closing transition.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Switch(String uid, String name, Logger::Level loglevel=Logger::Level::off)
const Attribute< Real >::Ptr mZeroCrossingTimeB
Bool mnaIsClosed() override
Check if switch is closed.
void setSwitchingMode(SwitchingMode mode)
const Attribute< Real >::Ptr mExponentialTransitionEndTime
const Attribute< Real >::Ptr mZeroCrossingTimeA
const Attribute< Bool >::Ptr mPoleClosedC
void openSwitch() override
Bool supportsPrecomputedSystemMatrices() const override
SimPowerComp< Complex >::Ptr clone(String name) override
Returns a modified copy of the component with the given suffix added to the name and without.
void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
const Attribute< Real >::Ptr mInstantCurrentC
const Attribute< Real >::Ptr mEffectiveResistanceB
const Attribute< Real >::Ptr mInstantCurrentB
void initializeFromNodesAndTerminals(Real frequency) override
Initializes Component variables according to power flow data stored in Nodes.
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
const Attribute< Bool >::Ptr mOpeningRequested
void mnaCompInitializeHarm(Real omega, Real timeStep, std::vector< Attribute< Matrix >::Ptr > leftVectors) override
void mnaCompApplySwitchSystemMatrixStamp(Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) override
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
const Attribute< Real >::Ptr mEffectiveResistanceC
const Attribute< Real >::Ptr mExponentialProgress
const Attribute< Real >::Ptr mZeroCrossingTimeC
String uid()
Returns unique id.
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
static String phasorMatrixToString(const MatrixComp &mat)
Definition Logger.cpp:51
MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
Attribute< Matrix >::Ptr mRightVector
static void stampAdmittanceMatrix(const MatrixComp &admittanceMat, 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)
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
SimTerminal< Complex >::List mTerminals
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
MatrixComp initialVoltage(UInt index)
std::shared_ptr< SimPowerComp< VarType > > Ptr
Bool terminalNotGrounded(UInt index)
Complex initialSingleVoltage(UInt index)
Logger::Level mLogLevel
Component logger control for internal variables.
Logger::Log mSLog
Component logger.
static std::shared_ptr< Switch > make(Args &&...args)
Definition PtrFactory.h:19
#define RMS3PH_TO_PEAK1PH
Definition Definitions.h:50
#define SHIFT_TO_PHASE_C
Definition Definitions.h:47
#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::Matrix< Complex, rows, cols, Eigen::ColMajor > MatrixFixedSizeComp
Dense matrix for complex numbers with fixed dimension.
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:72