53 resetZeroCrossingTimes();
68 copy->setSwitchingMode(mSwitchingMode);
69 copy->setZeroCrossingTolerance(mZeroCrossingTolerance);
70 copy->setExponentialSwitchingTime(mExponentialSwitchingTime);
75 mSwitchingMode = mode;
80 resetZeroCrossingTimes();
82 mPreviousCurrentValid =
false;
83 mResetZeroCrossingHistory =
false;
85 resetExponentialTransition();
86 synchronizeEffectiveResistance(**
mIsClosed);
90 if (tolerance < 0.0) {
91 throw std::invalid_argument(
92 "EMT::Ph3::Switch zero-crossing tolerance must be non-negative.");
95 mZeroCrossingTolerance = tolerance;
99 if (switchingTime <= 0.0) {
100 throw std::invalid_argument(
101 "EMT::Ph3::Switch exponential switching time must be positive.");
104 mExponentialSwitchingTime = switchingTime;
113 resetExponentialTransition();
115 synchronizeEffectiveResistance(
false);
118 SPDLOG_LOGGER_INFO(
mSLog,
"Opening command received during an exponential "
119 "closing transition. Transition abandoned.");
126 resetZeroCrossingTimes();
127 resetExponentialTransition();
128 synchronizeEffectiveResistance(
false);
132 if (allPolesOpen()) {
139 resetZeroCrossingTimes();
140 resetExponentialTransition();
141 mResetZeroCrossingHistory =
true;
143 SPDLOG_LOGGER_INFO(
mSLog,
"Current-zero opening command received. Waiting "
144 "for phase-current zeros.");
148 validateExponentialResistanceParameters();
150 resetZeroCrossingTimes();
151 resetExponentialTransition();
153 synchronizeEffectiveResistance(
true);
156 SPDLOG_LOGGER_INFO(
mSLog,
157 "Exponential ZCS-emulation opening command received. "
158 "Switching duration={:.6e}s.",
159 mExponentialSwitchingTime);
166 resetZeroCrossingTimes();
167 mResetZeroCrossingHistory =
false;
171 resetExponentialTransition();
172 synchronizeEffectiveResistance(
true);
174 SPDLOG_LOGGER_INFO(
mSLog,
175 "Switch closing command: all three poles closed.");
180 resetExponentialTransition();
181 synchronizeEffectiveResistance(
true);
188 validateExponentialResistanceParameters();
189 resetExponentialTransition();
193 synchronizeEffectiveResistance(
false);
196 SPDLOG_LOGGER_INFO(
mSLog,
197 "Exponential ZCS-emulation closing command received. "
198 "Switching duration={:.6e}s.",
199 mExponentialSwitchingTime);
212 **
mIntfCurrent = (impedance.inverse() * vInitABC).real();
217 resetZeroCrossingTimes();
218 resetExponentialTransition();
219 synchronizeEffectiveResistance(**
mIsClosed);
221 SPDLOG_LOGGER_INFO(
mSLog,
222 "\n--- Initialization from powerflow ---"
223 "\nVoltage across: {: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 ---",
238 :
"ExponentialZCSEmulation"),
245 mTimeStep = timeStep;
251 synchronizeEffectiveResistance(**
mIsClosed);
252 resetExponentialTransition();
260 if ((**mIntfCurrent).rows() == 3) {
262 mPreviousCurrentValid =
true;
264 mPreviousCurrent = Matrix::Zero(3, 1);
265 mPreviousCurrentValid =
false;
268 mEffectiveResistancePrev = {{
274 mPreviousCurrentTime = 0.0;
275 mResetZeroCrossingHistory =
false;
282 return allPolesClosed();
297 SPDLOG_LOGGER_TRACE(
mSLog,
"\nConductance matrix: {:s}",
308 closed ? (**mClosedResistance).inverse() : (**mOpenResistance).inverse();
314 SPDLOG_LOGGER_TRACE(
mSLog,
"\nConductance matrix: {:s}",
325 attributeDependencies.push_back(leftVector);
354 updateZeroCrossingState(time);
356 updateExponentialTransition(time);
362 (**mIntfVoltage).setZero();
365 (**mIntfVoltage)(0, 0) =
367 (**mIntfVoltage)(1, 0) =
369 (**mIntfVoltage)(2, 0) =
374 (**mIntfVoltage)(0, 0) -=
376 (**mIntfVoltage)(1, 0) -=
378 (**mIntfVoltage)(2, 0) -=
399 const std::array<Real, 3> resistanceNow{{
405 Bool changed =
false;
406 for (
UInt phase = 0; phase < 3; ++phase) {
407 if (resistanceNow[phase] != mEffectiveResistancePrev[phase]) {
408 mEffectiveResistancePrev[phase] = resistanceNow[phase];
415 const std::array<Bool, 3> poleClosedNow{{
421 Bool changed =
false;
423 for (
UInt phase = 0; phase < 3; ++phase) {
424 if (poleClosedNow[phase] != mPoleClosedPrev[phase]) {
426 mPoleClosedPrev[phase] = poleClosedNow[phase];
433Bool EMT::Ph3::Switch::poleClosed(
UInt phase)
const {
436 return **mPoleClosedA;
438 return **mPoleClosedB;
440 return **mPoleClosedC;
442 throw std::out_of_range(
"EMT::Ph3::Switch phase index must be 0, 1, or 2.");
446void EMT::Ph3::Switch::setPoleClosed(
UInt phase,
Bool closed) {
449 **mPoleClosedA = closed;
452 **mPoleClosedB = closed;
455 **mPoleClosedC = closed;
458 throw std::out_of_range(
"EMT::Ph3::Switch phase index must be 0, 1, or 2.");
463 if (mSwitchingMode == SwitchingMode::CurrentZero &&
464 (**mClosedResistance).rows() >= 3 && (**mOpenResistance).rows() >= 3) {
465 setEffectiveResistance(phase, closed ? (**mClosedResistance)(phase, phase)
466 : (**mOpenResistance)(phase, phase));
470void EMT::Ph3::Switch::setAllPoles(
Bool closed) {
471 **mPoleClosedA = closed;
472 **mPoleClosedB = closed;
473 **mPoleClosedC = closed;
476Bool EMT::Ph3::Switch::allPolesClosed()
const {
477 return **mPoleClosedA && **mPoleClosedB && **mPoleClosedC;
480Bool EMT::Ph3::Switch::allPolesOpen()
const {
481 return !**mPoleClosedA && !**mPoleClosedB && !**mPoleClosedC;
484void EMT::Ph3::Switch::resetZeroCrossingTimes() {
485 **mZeroCrossingTimeA = -1.0;
486 **mZeroCrossingTimeB = -1.0;
487 **mZeroCrossingTimeC = -1.0;
490Real EMT::Ph3::Switch::effectiveResistance(
UInt phase)
const {
493 return **mEffectiveResistanceA;
495 return **mEffectiveResistanceB;
497 return **mEffectiveResistanceC;
499 throw std::out_of_range(
"EMT::Ph3::Switch phase index must be 0, 1, or 2.");
503void EMT::Ph3::Switch::setEffectiveResistance(
UInt phase,
Real resistance) {
506 **mEffectiveResistanceA = resistance;
509 **mEffectiveResistanceB = resistance;
512 **mEffectiveResistanceC = resistance;
515 throw std::out_of_range(
"EMT::Ph3::Switch phase index must be 0, 1, or 2.");
519void EMT::Ph3::Switch::synchronizeEffectiveResistance(
Bool closed) {
520 if ((**mClosedResistance).rows() < 3 || (**mOpenResistance).rows() < 3)
523 for (
UInt phase = 0; phase < 3; ++phase) {
524 setEffectiveResistance(phase, closed ? (**mClosedResistance)(phase, phase)
525 : (**mOpenResistance)(phase, phase));
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;
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.");
544 if (mExponentialSwitchingTime <= 0.0) {
545 throw std::invalid_argument(
546 "EMT::Ph3::Switch exponential switching time must be positive.");
549 for (
UInt phase = 0; phase < 3; ++phase) {
550 const Real rClosed = (**mClosedResistance)(phase, phase);
551 const Real rOpen = (**mOpenResistance)(phase, phase);
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.");
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));
566 return std::exp(std::log(rClosed) +
567 boundedAlpha * (std::log(rOpen) - std::log(rClosed)));
570void EMT::Ph3::Switch::updateExponentialTransition(
Real time) {
571 if (!**mExponentialTransitionActive)
574 if (!mExponentialTransitionStarted) {
575 mExponentialTransitionStarted =
true;
576 **mExponentialTransitionStartTime = time;
577 **mExponentialTransitionEndTime = time + mExponentialSwitchingTime;
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));
587 const Bool closing = **mExponentialTransitionClosing;
590 const Real alpha = closing ? 1.0 - boundedElapsed : boundedElapsed;
592 **mExponentialProgress = alpha;
594 for (
UInt phase = 0; phase < 3; ++phase)
595 setEffectiveResistance(phase, exponentialResistance(phase, alpha));
597 if (boundedElapsed >= 1.0) {
598 synchronizeEffectiveResistance(closing);
599 setAllPoles(closing);
600 **mOpeningRequested =
false;
601 **mExponentialTransitionActive =
false;
602 **mExponentialTransitionClosing =
false;
606 "Exponential ZCS-emulation {:s} completed: start={:.9f}s, "
607 "end={:.9f}s, duration={:.6e}s.",
608 closing ?
"closing" :
"opening", **mExponentialTransitionStartTime,
609 **mExponentialTransitionEndTime, mExponentialSwitchingTime);
614 if (mSwitchingMode == SwitchingMode::Ideal) {
615 const Matrix &resistance =
616 **mIsClosed ? **mClosedResistance : **mOpenResistance;
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);
628 conductance = resistance.inverse();
635 for (
UInt phase = 0; phase < 3; ++phase) {
636 Real resistance = 0.0;
638 if (mSwitchingMode == SwitchingMode::CurrentZero) {
639 resistance = poleClosed(phase) ? (**mClosedResistance)(phase, phase)
640 : (**mOpenResistance)(phase, phase);
642 resistance = effectiveResistance(phase);
645 conductance(phase, phase) = 1.0 / resistance;
651Bool EMT::Ph3::Switch::currentCrossedZero(
Real previousCurrent,
652 Real current)
const {
653 if (std::abs(current) <= mZeroCrossingTolerance)
656 return (previousCurrent > mZeroCrossingTolerance &&
657 current < -mZeroCrossingTolerance) ||
658 (previousCurrent < -mZeroCrossingTolerance &&
659 current > mZeroCrossingTolerance);
662Real EMT::Ph3::Switch::interpolateZeroCrossingTime(
Real previousCurrent,
665 Real currentTime)
const {
666 const Real denominator = std::abs(previousCurrent) + std::abs(current);
668 if (denominator <= mZeroCrossingTolerance)
671 const Real fraction = std::abs(previousCurrent) / denominator;
673 return previousTime + fraction * (currentTime - previousTime);
676void EMT::Ph3::Switch::setZeroCrossingTime(
UInt phase,
Real time) {
679 **mZeroCrossingTimeA = time;
682 **mZeroCrossingTimeB = time;
685 **mZeroCrossingTimeC = time;
688 throw std::out_of_range(
"EMT::Ph3::Switch phase index must be 0, 1, or 2.");
692void EMT::Ph3::Switch::updateZeroCrossingState(
Real time) {
693 const Matrix current = **mIntfCurrent;
695 if (current.rows() != 3)
700 if (!**mOpeningRequested) {
701 mPreviousCurrent = current;
702 mPreviousCurrentTime = time;
703 mPreviousCurrentValid =
true;
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);
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));
724 mPreviousCurrent = current;
725 mPreviousCurrentTime = time;
726 mPreviousCurrentValid =
true;
727 mResetZeroCrossingHistory =
false;
730 **mOpeningRequested =
false;
735 for (
UInt phase = 0; phase < 3; ++phase) {
736 if (!poleClosed(phase))
739 const Real previous = mPreviousCurrent(phase, 0);
740 const Real present = current(phase, 0);
742 if (!currentCrossedZero(previous, present))
745 const Real zeroTime = interpolateZeroCrossingTime(
746 previous, present, mPreviousCurrentTime, time);
748 setPoleClosed(phase,
false);
749 setZeroCrossingTime(phase, zeroTime);
751 SPDLOG_LOGGER_INFO(mSLog,
752 "Phase {} current zero detected: "
753 "t_z={:.9f}s, i_prev={:.6e}A, i={:.6e}A. "
755 phase, zeroTime, previous, present);
758 mPreviousCurrent = current;
759 mPreviousCurrentTime = time;
760 mPreviousCurrentValid =
true;
762 if (allPolesOpen()) {
763 **mOpeningRequested =
false;
765 SPDLOG_LOGGER_INFO(mSLog,
"Current-zero interruption completed: "
766 "all three breaker poles are open.");
AttributePointer< Attribute< T > > Ptr
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].
@ ExponentialZCSEmulation
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
static String matrixToString(const Matrix &mat)
static String phasorToString(const Complex &num)
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 updateMatrixNodeIndices()
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)
#define RMS3PH_TO_PEAK1PH
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
Dense matrix for real numbers.
Eigen::Matrix< Real, rows, cols, Eigen::ColMajor > MatrixFixedSize
Dense matrix for real numbers with fixed dimension.
Eigen::Matrix< Complex, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixComp
Dense matrix for complex numbers.
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).