DPsim
Loading...
Searching...
No Matches
EMT_Ph3_Switch.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
9#include <algorithm>
10#include <cmath>
11#include <stdexcept>
12
14
15using namespace CPS;
16
18 : MNASimPowerComp<Real>(uid, name, false, true, logLevel),
20 mOpeningRequested(mAttributes->create<Bool>("opening_requested")),
21 mPoleClosedA(mAttributes->create<Bool>("pole_closed_a")),
22 mPoleClosedB(mAttributes->create<Bool>("pole_closed_b")),
23 mPoleClosedC(mAttributes->create<Bool>("pole_closed_c")),
24 mZeroCrossingTimeA(mAttributes->create<Real>("zero_crossing_time_a")),
25 mZeroCrossingTimeB(mAttributes->create<Real>("zero_crossing_time_b")),
26 mZeroCrossingTimeC(mAttributes->create<Real>("zero_crossing_time_c")),
28 mAttributes->create<Bool>("exponential_transition_active")),
30 mAttributes->create<Bool>("exponential_transition_closing")),
31 mExponentialProgress(mAttributes->create<Real>("exponential_progress")),
33 mAttributes->create<Real>("exponential_transition_start_time")),
35 mAttributes->create<Real>("exponential_transition_end_time")),
37 mAttributes->create<Real>("effective_resistance_a")),
39 mAttributes->create<Real>("effective_resistance_b")),
41 mAttributes->create<Real>("effective_resistance_c")) {
43
44 // IMPORTANT:
45 // Keep the three-phase interface dimensions fixed from construction.
46 // DataLogger inspects matrix dimensions when attributes are registered,
47 // before the simulation starts.
48 **mIntfVoltage = Matrix::Zero(3, 1);
49 **mIntfCurrent = Matrix::Zero(3, 1);
50
51 **mOpeningRequested = false;
52 setAllPoles(false);
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 // Synchronize physical poles with the configured initial command state.
78 setAllPoles(**mIsClosed);
79 **mOpeningRequested = false;
80 resetZeroCrossingTimes();
81
82 mPreviousCurrentValid = false;
83 mResetZeroCrossingHistory = false;
84
85 resetExponentialTransition();
86 synchronizeEffectiveResistance(**mIsClosed);
87}
88
90 if (tolerance < 0.0) {
91 throw std::invalid_argument(
92 "EMT::Ph3::Switch zero-crossing tolerance must be non-negative.");
93 }
94
95 mZeroCrossingTolerance = tolerance;
96}
97
99 if (switchingTime <= 0.0) {
100 throw std::invalid_argument(
101 "EMT::Ph3::Switch exponential switching time must be positive.");
102 }
103
104 mExponentialSwitchingTime = switchingTime;
105}
106
109
110 // An opening command during a closing ramp abandons that ramp. The poles are
111 // still open at that point, so the switch simply stays open.
113 resetExponentialTransition();
114 setAllPoles(false);
115 synchronizeEffectiveResistance(false);
116 **mOpeningRequested = false;
117
118 SPDLOG_LOGGER_INFO(mSLog, "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(mSLog, "Current-zero opening command received. Waiting "
144 "for phase-current zeros.");
145 return;
146 }
147
148 validateExponentialResistanceParameters();
149 **mOpeningRequested = true;
150 resetZeroCrossingTimes();
151 resetExponentialTransition();
153 synchronizeEffectiveResistance(true);
154 setAllPoles(true);
155
156 SPDLOG_LOGGER_INFO(mSLog,
157 "Exponential ZCS-emulation opening command received. "
158 "Switching duration={:.6e}s.",
159 mExponentialSwitchingTime);
160}
161
164
165 **mOpeningRequested = false;
166 resetZeroCrossingTimes();
167 mResetZeroCrossingHistory = false;
168
169 if (mSwitchingMode != SwitchingMode::ExponentialZCSEmulation) {
170 setAllPoles(true);
171 resetExponentialTransition();
172 synchronizeEffectiveResistance(true);
173
174 SPDLOG_LOGGER_INFO(mSLog,
175 "Switch closing command: all three poles closed.");
176 return;
177 }
178
179 if (allPolesClosed() && !**mExponentialTransitionActive) {
180 resetExponentialTransition();
181 synchronizeEffectiveResistance(true);
182 return;
183 }
184
185 // Energising a branch is the mirror image of interrupting it: the same
186 // resistance path is traversed from R_open towards R_closed. The poles stay
187 // open until the ramp completes.
188 validateExponentialResistanceParameters();
189 resetExponentialTransition();
192 **mExponentialProgress = 1.0;
193 synchronizeEffectiveResistance(false);
194 setAllPoles(false);
195
196 SPDLOG_LOGGER_INFO(mSLog,
197 "Exponential ZCS-emulation closing command received. "
198 "Switching duration={:.6e}s.",
199 mExponentialSwitchingTime);
200}
201
203 Matrix impedance = (**mIsClosed) ? **mClosedResistance : **mOpenResistance;
204
205 MatrixComp vInitABC = MatrixComp::Zero(3, 1);
206 vInitABC(0, 0) =
208 vInitABC(1, 0) = vInitABC(0, 0) * SHIFT_TO_PHASE_B;
209 vInitABC(2, 0) = vInitABC(0, 0) * SHIFT_TO_PHASE_C;
210
211 **mIntfVoltage = vInitABC.real();
212 **mIntfCurrent = (impedance.inverse() * vInitABC).real();
213
214 // Power-flow initialization defines the initial physical breaker state.
215 setAllPoles(**mIsClosed);
216 **mOpeningRequested = false;
217 resetZeroCrossingTimes();
218 resetExponentialTransition();
219 synchronizeEffectiveResistance(**mIsClosed);
220
221 SPDLOG_LOGGER_INFO(mSLog,
222 "\n--- Initialization from powerflow ---"
223 "\nVoltage across: {:s}"
224 "\nCurrent: {:s}"
225 "\nTerminal 0 voltage: {:s}"
226 "\nTerminal 1 voltage: {:s}"
227 "\nSwitching mode: {:s}"
228 "\nInitial breaker state: {:s}"
229 "\n--- Initialization from powerflow finished ---",
234 mSwitchingMode == SwitchingMode::Ideal
235 ? "Ideal"
236 : (mSwitchingMode == SwitchingMode::CurrentZero
237 ? "CurrentZero"
238 : "ExponentialZCSEmulation"),
239 **mIsClosed ? "closed" : "open");
240}
241
243 Attribute<Matrix>::Ptr leftVector) {
245 mTimeStep = timeStep;
246 **mRightVector = Matrix::Zero(0, 0);
247
248 // setParameters() belongs to the shared base class. Synchronize here as a
249 // final safeguard in case setSwitchingMode() was called before setParameters().
250 setAllPoles(**mIsClosed);
251 synchronizeEffectiveResistance(**mIsClosed);
252 resetExponentialTransition();
253
254 mPoleClosedPrev = {
255 **mPoleClosedA,
256 **mPoleClosedB,
257 **mPoleClosedC,
258 };
259
260 if ((**mIntfCurrent).rows() == 3) {
261 mPreviousCurrent = **mIntfCurrent;
262 mPreviousCurrentValid = true;
263 } else {
264 mPreviousCurrent = Matrix::Zero(3, 1);
265 mPreviousCurrentValid = false;
266 }
267
268 mEffectiveResistancePrev = {{
272 }};
273
274 mPreviousCurrentTime = 0.0;
275 mResetZeroCrossingHistory = false;
276}
277
279 if (mSwitchingMode == SwitchingMode::Ideal)
280 return **mIsClosed;
281
282 return allPolesClosed();
283}
284
288
290 SparseMatrixRow &systemMatrix) {
291 const MatrixFixedSize<3, 3> conductance = currentConductanceMatrix();
292
294 conductance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1),
296
297 SPDLOG_LOGGER_TRACE(mSLog, "\nConductance matrix: {:s}",
298 Logger::matrixToString(conductance));
299}
300
302 Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) {
303 // This method is used by the conventional precomputed two-state switch path.
304 // CurrentZero mode advertises supportsPrecomputedSystemMatrices() == false
305 // and therefore uses mnaCompApplySystemMatrixStamp() through the variable
306 // matrix recomputation path instead.
307 MatrixFixedSize<3, 3> conductance =
308 closed ? (**mClosedResistance).inverse() : (**mOpenResistance).inverse();
309
311 conductance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1),
313
314 SPDLOG_LOGGER_TRACE(mSLog, "\nConductance matrix: {:s}",
315 Logger::matrixToString(conductance));
316}
317
319
321 AttributeBase::List &prevStepDependencies,
322 AttributeBase::List &attributeDependencies,
323 AttributeBase::List &modifiedAttributes,
324 Attribute<Matrix>::Ptr &leftVector) {
325 attributeDependencies.push_back(leftVector);
326
327 modifiedAttributes.push_back(mIntfVoltage);
328 modifiedAttributes.push_back(mIntfCurrent);
329
330 modifiedAttributes.push_back(mOpeningRequested);
331 modifiedAttributes.push_back(mPoleClosedA);
332 modifiedAttributes.push_back(mPoleClosedB);
333 modifiedAttributes.push_back(mPoleClosedC);
334 modifiedAttributes.push_back(mZeroCrossingTimeA);
335 modifiedAttributes.push_back(mZeroCrossingTimeB);
336 modifiedAttributes.push_back(mZeroCrossingTimeC);
337
338 modifiedAttributes.push_back(mExponentialTransitionActive);
339 modifiedAttributes.push_back(mExponentialTransitionClosing);
340 modifiedAttributes.push_back(mExponentialProgress);
341 modifiedAttributes.push_back(mExponentialTransitionStartTime);
342 modifiedAttributes.push_back(mExponentialTransitionEndTime);
343 modifiedAttributes.push_back(mEffectiveResistanceA);
344 modifiedAttributes.push_back(mEffectiveResistanceB);
345 modifiedAttributes.push_back(mEffectiveResistanceC);
346}
347
349 Attribute<Matrix>::Ptr &leftVector) {
350 mnaCompUpdateVoltage(**leftVector);
351 mnaCompUpdateCurrent(**leftVector);
352
353 if (mSwitchingMode == SwitchingMode::CurrentZero) {
354 updateZeroCrossingState(time);
355 } else if (mSwitchingMode == SwitchingMode::ExponentialZCSEmulation) {
356 updateExponentialTransition(time);
357 }
358}
359
361 // Voltage across component is defined as V1 - V0.
362 (**mIntfVoltage).setZero();
363
364 if (terminalNotGrounded(1)) {
365 (**mIntfVoltage)(0, 0) =
367 (**mIntfVoltage)(1, 0) =
369 (**mIntfVoltage)(2, 0) =
371 }
372
373 if (terminalNotGrounded(0)) {
374 (**mIntfVoltage)(0, 0) -=
376 (**mIntfVoltage)(1, 0) -=
378 (**mIntfVoltage)(2, 0) -=
380 }
381}
382
384 **mIntfCurrent = currentConductanceMatrix() * **mIntfVoltage;
385}
386
388 if (mSwitchingMode == SwitchingMode::Ideal) {
389 // Preserve the previous DPsim switch behavior.
390 if (mIsClosedPrev != mnaIsClosed()) {
392 return true;
393 }
394
395 return false;
396 }
397
398 if (mSwitchingMode == SwitchingMode::ExponentialZCSEmulation) {
399 const std::array<Real, 3> resistanceNow{{
403 }};
404
405 Bool changed = false;
406 for (UInt phase = 0; phase < 3; ++phase) {
407 if (resistanceNow[phase] != mEffectiveResistancePrev[phase]) {
408 mEffectiveResistancePrev[phase] = resistanceNow[phase];
409 changed = true;
410 }
411 }
412 return changed;
413 }
414
415 const std::array<Bool, 3> poleClosedNow{{
416 **mPoleClosedA,
417 **mPoleClosedB,
418 **mPoleClosedC,
419 }};
420
421 Bool changed = false;
422
423 for (UInt phase = 0; phase < 3; ++phase) {
424 if (poleClosedNow[phase] != mPoleClosedPrev[phase]) {
425 changed = true;
426 mPoleClosedPrev[phase] = poleClosedNow[phase];
427 }
428 }
429
430 return changed;
431}
432
433Bool EMT::Ph3::Switch::poleClosed(UInt phase) const {
434 switch (phase) {
435 case 0:
436 return **mPoleClosedA;
437 case 1:
438 return **mPoleClosedB;
439 case 2:
440 return **mPoleClosedC;
441 default:
442 throw std::out_of_range("EMT::Ph3::Switch phase index must be 0, 1, or 2.");
443 }
444}
445
446void EMT::Ph3::Switch::setPoleClosed(UInt phase, Bool closed) {
447 switch (phase) {
448 case 0:
449 **mPoleClosedA = closed;
450 break;
451 case 1:
452 **mPoleClosedB = closed;
453 break;
454 case 2:
455 **mPoleClosedC = closed;
456 break;
457 default:
458 throw std::out_of_range("EMT::Ph3::Switch phase index must be 0, 1, or 2.");
459 }
460
461 // Keep the resistance diagnostics synchronized with the discrete
462 // CurrentZero pole state. Exponential mode manages R(t) independently.
463 if (mSwitchingMode == SwitchingMode::CurrentZero &&
464 (**mClosedResistance).rows() >= 3 && (**mOpenResistance).rows() >= 3) {
465 setEffectiveResistance(phase, closed ? (**mClosedResistance)(phase, phase)
466 : (**mOpenResistance)(phase, phase));
467 }
468}
469
470void EMT::Ph3::Switch::setAllPoles(Bool closed) {
471 **mPoleClosedA = closed;
472 **mPoleClosedB = closed;
473 **mPoleClosedC = closed;
474}
475
476Bool EMT::Ph3::Switch::allPolesClosed() const {
477 return **mPoleClosedA && **mPoleClosedB && **mPoleClosedC;
478}
479
480Bool EMT::Ph3::Switch::allPolesOpen() const {
481 return !**mPoleClosedA && !**mPoleClosedB && !**mPoleClosedC;
482}
483
484void EMT::Ph3::Switch::resetZeroCrossingTimes() {
485 **mZeroCrossingTimeA = -1.0;
486 **mZeroCrossingTimeB = -1.0;
487 **mZeroCrossingTimeC = -1.0;
488}
489
490Real EMT::Ph3::Switch::effectiveResistance(UInt phase) const {
491 switch (phase) {
492 case 0:
493 return **mEffectiveResistanceA;
494 case 1:
495 return **mEffectiveResistanceB;
496 case 2:
497 return **mEffectiveResistanceC;
498 default:
499 throw std::out_of_range("EMT::Ph3::Switch phase index must be 0, 1, or 2.");
500 }
501}
502
503void EMT::Ph3::Switch::setEffectiveResistance(UInt phase, Real resistance) {
504 switch (phase) {
505 case 0:
506 **mEffectiveResistanceA = resistance;
507 return;
508 case 1:
509 **mEffectiveResistanceB = resistance;
510 return;
511 case 2:
512 **mEffectiveResistanceC = resistance;
513 return;
514 default:
515 throw std::out_of_range("EMT::Ph3::Switch phase index must be 0, 1, or 2.");
516 }
517}
518
519void EMT::Ph3::Switch::synchronizeEffectiveResistance(Bool closed) {
520 if ((**mClosedResistance).rows() < 3 || (**mOpenResistance).rows() < 3)
521 return;
522
523 for (UInt phase = 0; phase < 3; ++phase) {
524 setEffectiveResistance(phase, closed ? (**mClosedResistance)(phase, phase)
525 : (**mOpenResistance)(phase, phase));
526 }
527}
528
529void EMT::Ph3::Switch::resetExponentialTransition() {
530 **mExponentialTransitionActive = false;
531 **mExponentialTransitionClosing = false;
532 **mExponentialProgress = 0.0;
533 **mExponentialTransitionStartTime = -1.0;
534 **mExponentialTransitionEndTime = -1.0;
535 mExponentialTransitionStarted = false;
536}
537
538void EMT::Ph3::Switch::validateExponentialResistanceParameters() const {
539 if ((**mClosedResistance).rows() < 3 || (**mOpenResistance).rows() < 3) {
540 throw std::invalid_argument(
541 "EMT::Ph3::Switch exponential mode requires 3x3 resistance matrices.");
542 }
543
544 if (mExponentialSwitchingTime <= 0.0) {
545 throw std::invalid_argument(
546 "EMT::Ph3::Switch exponential switching time must be positive.");
547 }
548
549 for (UInt phase = 0; phase < 3; ++phase) {
550 const Real rClosed = (**mClosedResistance)(phase, phase);
551 const Real rOpen = (**mOpenResistance)(phase, phase);
552
553 if (rClosed <= 0.0 || rOpen <= 0.0) {
554 throw std::invalid_argument(
555 "EMT::Ph3::Switch exponential mode requires positive diagonal "
556 "open and closed resistances.");
557 }
558 }
559}
560
561Real EMT::Ph3::Switch::exponentialResistance(UInt phase, Real alpha) const {
562 const Real rClosed = (**mClosedResistance)(phase, phase);
563 const Real rOpen = (**mOpenResistance)(phase, phase);
564 const Real boundedAlpha = std::max(0.0, std::min(1.0, alpha));
565
566 return std::exp(std::log(rClosed) +
567 boundedAlpha * (std::log(rOpen) - std::log(rClosed)));
568}
569
570void EMT::Ph3::Switch::updateExponentialTransition(Real time) {
571 if (!**mExponentialTransitionActive)
572 return;
573
574 if (!mExponentialTransitionStarted) {
575 mExponentialTransitionStarted = true;
576 **mExponentialTransitionStartTime = time;
577 **mExponentialTransitionEndTime = time + mExponentialSwitchingTime;
578 }
579
580 // Prepare R(t+dt) in post-step so that the next MNA solve at t+dt stamps
581 // the intended resistance trajectory without requiring a time-aware event API.
582 const Real targetTime = time + mTimeStep;
583 const Real elapsed = (targetTime - **mExponentialTransitionStartTime) /
584 mExponentialSwitchingTime;
585 const Real boundedElapsed = std::max(0.0, std::min(1.0, elapsed));
586
587 const Bool closing = **mExponentialTransitionClosing;
588
589 // alpha is the position on the resistance path: 0 at R_closed, 1 at R_open.
590 const Real alpha = closing ? 1.0 - boundedElapsed : boundedElapsed;
591
592 **mExponentialProgress = alpha;
593
594 for (UInt phase = 0; phase < 3; ++phase)
595 setEffectiveResistance(phase, exponentialResistance(phase, alpha));
596
597 if (boundedElapsed >= 1.0) {
598 synchronizeEffectiveResistance(closing);
599 setAllPoles(closing);
600 **mOpeningRequested = false;
601 **mExponentialTransitionActive = false;
602 **mExponentialTransitionClosing = false;
603
604 SPDLOG_LOGGER_INFO(
605 mSLog,
606 "Exponential ZCS-emulation {:s} completed: start={:.9f}s, "
607 "end={:.9f}s, duration={:.6e}s.",
608 closing ? "closing" : "opening", **mExponentialTransitionStartTime,
609 **mExponentialTransitionEndTime, mExponentialSwitchingTime);
610 }
611}
612
613MatrixFixedSize<3, 3> EMT::Ph3::Switch::currentConductanceMatrix() const {
614 if (mSwitchingMode == SwitchingMode::Ideal) {
615 const Matrix &resistance =
616 **mIsClosed ? **mClosedResistance : **mOpenResistance;
617 MatrixFixedSize<3, 3> conductance = MatrixFixedSize<3, 3>::Zero();
618 // Per-phase diagonal resistance is the common ideal-switch case. Avoid a
619 // general dynamic 3x3 inverse in each post-step, retaining the inverse
620 // fallback for callers that configure mutual phase coupling.
621 if (resistance(0, 1) == 0.0 && resistance(0, 2) == 0.0 &&
622 resistance(1, 0) == 0.0 && resistance(1, 2) == 0.0 &&
623 resistance(2, 0) == 0.0 && resistance(2, 1) == 0.0) {
624 conductance(0, 0) = 1.0 / resistance(0, 0);
625 conductance(1, 1) = 1.0 / resistance(1, 1);
626 conductance(2, 2) = 1.0 / resistance(2, 2);
627 } else {
628 conductance = resistance.inverse();
629 }
630 return conductance;
631 }
632
633 MatrixFixedSize<3, 3> conductance = MatrixFixedSize<3, 3>::Zero();
634
635 for (UInt phase = 0; phase < 3; ++phase) {
636 Real resistance = 0.0;
637
638 if (mSwitchingMode == SwitchingMode::CurrentZero) {
639 resistance = poleClosed(phase) ? (**mClosedResistance)(phase, phase)
640 : (**mOpenResistance)(phase, phase);
641 } else {
642 resistance = effectiveResistance(phase);
643 }
644
645 conductance(phase, phase) = 1.0 / resistance;
646 }
647
648 return conductance;
649}
650
651Bool EMT::Ph3::Switch::currentCrossedZero(Real previousCurrent,
652 Real current) const {
653 if (std::abs(current) <= mZeroCrossingTolerance)
654 return true;
655
656 return (previousCurrent > mZeroCrossingTolerance &&
657 current < -mZeroCrossingTolerance) ||
658 (previousCurrent < -mZeroCrossingTolerance &&
659 current > mZeroCrossingTolerance);
660}
661
662Real EMT::Ph3::Switch::interpolateZeroCrossingTime(Real previousCurrent,
663 Real current,
664 Real previousTime,
665 Real currentTime) const {
666 const Real denominator = std::abs(previousCurrent) + std::abs(current);
667
668 if (denominator <= mZeroCrossingTolerance)
669 return currentTime;
670
671 const Real fraction = std::abs(previousCurrent) / denominator;
672
673 return previousTime + fraction * (currentTime - previousTime);
674}
675
676void EMT::Ph3::Switch::setZeroCrossingTime(UInt phase, Real time) {
677 switch (phase) {
678 case 0:
679 **mZeroCrossingTimeA = time;
680 return;
681 case 1:
682 **mZeroCrossingTimeB = time;
683 return;
684 case 2:
685 **mZeroCrossingTimeC = time;
686 return;
687 default:
688 throw std::out_of_range("EMT::Ph3::Switch phase index must be 0, 1, or 2.");
689 }
690}
691
692void EMT::Ph3::Switch::updateZeroCrossingState(Real time) {
693 const Matrix current = **mIntfCurrent;
694
695 if (current.rows() != 3)
696 return;
697
698 // While no opening request is active, continuously maintain a clean
699 // current history for diagnostics and for a later opening command.
700 if (!**mOpeningRequested) {
701 mPreviousCurrent = current;
702 mPreviousCurrentTime = time;
703 mPreviousCurrentValid = true;
704 return;
705 }
706
707 // The first sample after the opening command must not use a sample from
708 // before the command for a sign-change test. It may still open a pole if
709 // that first post-command sample itself is already at zero.
710 if (mResetZeroCrossingHistory || !mPreviousCurrentValid) {
711 for (UInt phase = 0; phase < 3; ++phase) {
712 if (poleClosed(phase) &&
713 std::abs(current(phase, 0)) <= mZeroCrossingTolerance) {
714 setPoleClosed(phase, false);
715 setZeroCrossingTime(phase, time);
716
717 SPDLOG_LOGGER_INFO(mSLog,
718 "Phase {} opened at sampled current zero: "
719 "t={:.9f}s, i={:.6e}A",
720 phase, time, current(phase, 0));
721 }
722 }
723
724 mPreviousCurrent = current;
725 mPreviousCurrentTime = time;
726 mPreviousCurrentValid = true;
727 mResetZeroCrossingHistory = false;
728
729 if (allPolesOpen())
730 **mOpeningRequested = false;
731
732 return;
733 }
734
735 for (UInt phase = 0; phase < 3; ++phase) {
736 if (!poleClosed(phase))
737 continue;
738
739 const Real previous = mPreviousCurrent(phase, 0);
740 const Real present = current(phase, 0);
741
742 if (!currentCrossedZero(previous, present))
743 continue;
744
745 const Real zeroTime = interpolateZeroCrossingTime(
746 previous, present, mPreviousCurrentTime, time);
747
748 setPoleClosed(phase, false);
749 setZeroCrossingTime(phase, zeroTime);
750
751 SPDLOG_LOGGER_INFO(mSLog,
752 "Phase {} current zero detected: "
753 "t_z={:.9f}s, i_prev={:.6e}A, i={:.6e}A. "
754 "Pole opened.",
755 phase, zeroTime, previous, present);
756 }
757
758 mPreviousCurrent = current;
759 mPreviousCurrentTime = time;
760 mPreviousCurrentValid = true;
761
762 if (allPolesOpen()) {
763 **mOpeningRequested = false;
764
765 SPDLOG_LOGGER_INFO(mSLog, "Current-zero interruption completed: "
766 "all three breaker poles are open.");
767 }
768}
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.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
const Attribute< Bool >::Ptr mPoleClosedB
void setSwitchingMode(SwitchingMode mode)
Select switching model. Configure before simulation initialization.
const Attribute< Real >::Ptr mExponentialTransitionStartTime
const Attribute< Real >::Ptr mExponentialProgress
Bool hasParameterChanged() override
Returns true if one of the element paramters has changed.
const Attribute< Real >::Ptr mEffectiveResistanceC
SimPowerComp< Real >::Ptr clone(String name) override
Returns a modified copy of the component with the given suffix added to the name and without.
const Attribute< Bool >::Ptr mOpeningRequested
const Attribute< Bool >::Ptr mPoleClosedC
const Attribute< Real >::Ptr mExponentialTransitionEndTime
void mnaCompApplySwitchSystemMatrixStamp(Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) override
const Attribute< Real >::Ptr mEffectiveResistanceB
void closeSwitch() override
void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
const Attribute< Real >::Ptr mZeroCrossingTimeB
Switch(String uid, String name, Logger::Level logLevel=Logger::Level::off)
void setExponentialSwitchingTime(Real switchingTime)
Set total transition time for ExponentialZCSEmulation [s].
void mnaCompUpdateVoltage(const Matrix &leftVector) override
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
void initializeFromNodesAndTerminals(Real frequency) override
Initializes Component variables according to power flow data stored in Nodes.
Bool supportsPrecomputedSystemMatrices() const override
Only Ideal is representable by the conventional two precomputed matrices.
void openSwitch() override
const Attribute< Bool >::Ptr mExponentialTransitionClosing
True while the active exponential transition is a closing transition.
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
void setZeroCrossingTolerance(Real tolerance)
Absolute current tolerance used by CurrentZero mode [A].
const Attribute< Real >::Ptr mEffectiveResistanceA
Effective phase resistances currently stamped into the MNA matrix [ohm].
const Attribute< Bool >::Ptr mExponentialTransitionActive
Exponential-transition diagnostics.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
const Attribute< Bool >::Ptr mPoleClosedA
Physical pole states.
const Attribute< Real >::Ptr mZeroCrossingTimeC
Bool mnaIsClosed() override
Check if switch is closed.
const Attribute< Real >::Ptr mZeroCrossingTimeA
Estimated physical zero-crossing times [s], -1 if none has been detected.
String uid()
Returns unique id.
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
MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
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)
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Real > >::Ptr mIntfCurrent
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
std::shared_ptr< SimPowerComp< VarType > > Ptr
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< 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
Eigen::Matrix< Real, rows, cols, Eigen::ColMajor > MatrixFixedSize
Dense matrix for real numbers with fixed dimension.
Definition Definitions.h:99
double Real
Definition Definitions.h:60
int Int
Definition Definitions.h:59
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