53 resetZeroCrossingTimes();
68 copy->setSwitchingMode(mSwitchingMode);
69 copy->setZeroCrossingTolerance(mZeroCrossingTolerance);
70 copy->setExponentialSwitchingTime(mExponentialSwitchingTime);
75 mSwitchingMode = mode;
79 resetZeroCrossingTimes();
81 mPreviousCurrentValid =
false;
82 mResetZeroCrossingHistory =
false;
84 resetExponentialTransition();
85 synchronizeEffectiveResistance(**
mIsClosed);
89 if (tolerance < 0.0) {
90 throw std::invalid_argument(
91 "DP::Ph3::Switch zero-crossing tolerance must be non-negative.");
94 mZeroCrossingTolerance = tolerance;
98 if (switchingTime <= 0.0) {
99 throw std::invalid_argument(
100 "DP::Ph3::Switch exponential switching time must be positive.");
103 mExponentialSwitchingTime = switchingTime;
112 resetExponentialTransition();
114 synchronizeEffectiveResistance(
false);
117 SPDLOG_LOGGER_INFO(
mSLog,
118 "DP 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;
144 mSLog,
"DP current-zero opening command received. "
145 "Waiting for reconstructed physical phase-current zeros.");
149 validateExponentialResistanceParameters();
151 resetZeroCrossingTimes();
152 resetExponentialTransition();
154 synchronizeEffectiveResistance(
true);
157 SPDLOG_LOGGER_INFO(
mSLog,
158 "DP exponential ZCS-emulation opening command received. "
159 "Switching duration={:.6e}s.",
160 mExponentialSwitchingTime);
167 resetZeroCrossingTimes();
168 mResetZeroCrossingHistory =
false;
172 resetExponentialTransition();
173 synchronizeEffectiveResistance(
true);
176 mSLog,
"DP switch closing command: all three physical poles closed.");
181 resetExponentialTransition();
182 synchronizeEffectiveResistance(
true);
190 validateExponentialResistanceParameters();
191 resetExponentialTransition();
195 synchronizeEffectiveResistance(
false);
198 SPDLOG_LOGGER_INFO(
mSLog,
199 "DP exponential ZCS-emulation closing command received. "
200 "Switching duration={:.6e}s.",
201 mExponentialSwitchingTime);
224 resetZeroCrossingTimes();
225 resetExponentialTransition();
226 synchronizeEffectiveResistance(**
mIsClosed);
228 SPDLOG_LOGGER_INFO(
mSLog,
229 "\n--- Initialization from powerflow ---"
230 "\nVoltage envelope:"
232 "\nCurrent envelope:"
234 "\nTerminal 0 voltage envelope:"
236 "\nTerminal 1 voltage envelope:"
238 "\nSwitching mode: {:s}"
239 "\nInitial breaker state: {:s}"
240 "\n--- Initialization from powerflow finished ---",
249 :
"ExponentialZCSEmulation"),
256 mTimeStep = timeStep;
264 synchronizeEffectiveResistance(**
mIsClosed);
265 resetExponentialTransition();
275 const Matrix instantaneous = reconstructInstantaneousCurrent(0.0);
277 setInstantaneousCurrentAttributes(instantaneous);
279 mPreviousInstantaneousCurrent = instantaneous;
280 mEffectiveResistancePrev = {{
286 mPreviousCurrentTime = 0.0;
287 mPreviousCurrentValid =
true;
288 mResetZeroCrossingHistory =
false;
299 : leftVectors.front());
306 return allPolesClosed();
328 closed ? (**mClosedResistance).inverse() : (**mOpenResistance).inverse();
340 attributeDependencies.push_back(leftVector);
373 const Matrix instantaneous = reconstructInstantaneousCurrent(time);
375 setInstantaneousCurrentAttributes(instantaneous);
378 updateZeroCrossingState(time);
380 updateExponentialTransition(time);
385 (**mIntfVoltage).setZero();
388 (**mIntfVoltage)(0, 0) =
390 (**mIntfVoltage)(1, 0) =
392 (**mIntfVoltage)(2, 0) =
397 (**mIntfVoltage)(0, 0) -=
399 (**mIntfVoltage)(1, 0) -=
401 (**mIntfVoltage)(2, 0) -=
421 const std::array<Real, 3> resistanceNow{{
427 Bool changed =
false;
428 for (
UInt phase = 0; phase < 3; ++phase) {
429 if (resistanceNow[phase] != mEffectiveResistancePrev[phase]) {
430 mEffectiveResistancePrev[phase] = resistanceNow[phase];
437 const std::array<Bool, 3> poleClosedNow{{
443 Bool changed =
false;
445 for (
UInt phase = 0; phase < 3; ++phase) {
446 if (poleClosedNow[phase] != mPoleClosedPrev[phase]) {
447 mPoleClosedPrev[phase] = poleClosedNow[phase];
455Bool DP::Ph3::Switch::poleClosed(
UInt phase)
const {
458 return **mPoleClosedA;
460 return **mPoleClosedB;
462 return **mPoleClosedC;
464 throw std::out_of_range(
"DP::Ph3::Switch phase index must be 0, 1, or 2.");
468void DP::Ph3::Switch::setPoleClosed(
UInt phase,
Bool closed) {
471 **mPoleClosedA = closed;
474 **mPoleClosedB = closed;
477 **mPoleClosedC = closed;
480 throw std::out_of_range(
"DP::Ph3::Switch phase index must be 0, 1, or 2.");
485 if (mSwitchingMode == SwitchingMode::CurrentZero &&
486 (**mClosedResistance).rows() >= 3 && (**mOpenResistance).rows() >= 3) {
487 setEffectiveResistance(phase, closed ? (**mClosedResistance)(phase, phase)
488 : (**mOpenResistance)(phase, phase));
492void DP::Ph3::Switch::setAllPoles(
Bool closed) {
493 **mPoleClosedA = closed;
494 **mPoleClosedB = closed;
495 **mPoleClosedC = closed;
498Bool DP::Ph3::Switch::allPolesClosed()
const {
499 return **mPoleClosedA && **mPoleClosedB && **mPoleClosedC;
502Bool DP::Ph3::Switch::allPolesOpen()
const {
503 return !**mPoleClosedA && !**mPoleClosedB && !**mPoleClosedC;
506void DP::Ph3::Switch::resetZeroCrossingTimes() {
507 **mZeroCrossingTimeA = -1.0;
508 **mZeroCrossingTimeB = -1.0;
509 **mZeroCrossingTimeC = -1.0;
512Real DP::Ph3::Switch::effectiveResistance(
UInt phase)
const {
515 return **mEffectiveResistanceA;
517 return **mEffectiveResistanceB;
519 return **mEffectiveResistanceC;
521 throw std::out_of_range(
"DP::Ph3::Switch phase index must be 0, 1, or 2.");
525void DP::Ph3::Switch::setEffectiveResistance(
UInt phase,
Real resistance) {
528 **mEffectiveResistanceA = resistance;
531 **mEffectiveResistanceB = resistance;
534 **mEffectiveResistanceC = resistance;
537 throw std::out_of_range(
"DP::Ph3::Switch phase index must be 0, 1, or 2.");
541void DP::Ph3::Switch::synchronizeEffectiveResistance(
Bool closed) {
542 if ((**mClosedResistance).rows() < 3 || (**mOpenResistance).rows() < 3)
545 for (
UInt phase = 0; phase < 3; ++phase) {
546 setEffectiveResistance(phase, closed ? (**mClosedResistance)(phase, phase)
547 : (**mOpenResistance)(phase, phase));
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;
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.");
566 if (mExponentialSwitchingTime <= 0.0) {
567 throw std::invalid_argument(
568 "DP::Ph3::Switch exponential switching time must be positive.");
571 for (
UInt phase = 0; phase < 3; ++phase) {
572 const Real rClosed = (**mClosedResistance)(phase, phase);
573 const Real rOpen = (**mOpenResistance)(phase, phase);
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.");
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));
588 return std::exp(std::log(rClosed) +
589 boundedAlpha * (std::log(rOpen) - std::log(rClosed)));
592void DP::Ph3::Switch::updateExponentialTransition(
Real time) {
593 if (!**mExponentialTransitionActive)
596 if (!mExponentialTransitionStarted) {
597 mExponentialTransitionStarted =
true;
598 **mExponentialTransitionStartTime = time;
599 **mExponentialTransitionEndTime = time + mExponentialSwitchingTime;
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));
607 const Bool closing = **mExponentialTransitionClosing;
610 const Real alpha = closing ? 1.0 - boundedElapsed : boundedElapsed;
612 **mExponentialProgress = alpha;
614 for (
UInt phase = 0; phase < 3; ++phase)
615 setEffectiveResistance(phase, exponentialResistance(phase, alpha));
617 if (boundedElapsed >= 1.0) {
618 synchronizeEffectiveResistance(closing);
619 setAllPoles(closing);
620 **mOpeningRequested =
false;
621 **mExponentialTransitionActive =
false;
622 **mExponentialTransitionClosing =
false;
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);
636 if (mSwitchingMode == SwitchingMode::Ideal) {
637 const Matrix &resistance =
638 **mIsClosed ? **mClosedResistance : **mOpenResistance;
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);
649 conductance.real() = resistance.inverse();
654 for (
UInt phase = 0; phase < 3; ++phase) {
655 Real resistance = 0.0;
657 if (mSwitchingMode == SwitchingMode::CurrentZero) {
658 resistance = poleClosed(phase) ? (**mClosedResistance)(phase, phase)
659 : (**mOpenResistance)(phase, phase);
661 resistance = effectiveResistance(phase);
664 conductance(phase, phase) =
Complex(1.0 / resistance, 0.0);
670Matrix DP::Ph3::Switch::reconstructInstantaneousCurrent(
Real time)
const {
671 Matrix instantaneous = Matrix::Zero(3, 1);
675 const Complex carrier = std::polar<Real>(1.0, mShiftOmega * time);
677 for (
UInt phase = 0; phase < 3; ++phase) {
678 instantaneous(phase, 0) = std::real((**mIntfCurrent)(phase, 0) * carrier);
681 return instantaneous;
684void DP::Ph3::Switch::setInstantaneousCurrentAttributes(
const Matrix ¤t) {
685 **mInstantCurrentA = current(0, 0);
686 **mInstantCurrentB = current(1, 0);
687 **mInstantCurrentC = current(2, 0);
690Bool DP::Ph3::Switch::currentCrossedZero(
Real previousCurrent,
691 Real current)
const {
692 if (std::abs(current) <= mZeroCrossingTolerance)
695 return (previousCurrent > mZeroCrossingTolerance &&
696 current < -mZeroCrossingTolerance) ||
697 (previousCurrent < -mZeroCrossingTolerance &&
698 current > mZeroCrossingTolerance);
701Real DP::Ph3::Switch::interpolateZeroCrossingTime(
Real previousCurrent,
704 Real currentTime)
const {
705 const Real denominator = std::abs(previousCurrent) + std::abs(current);
707 if (denominator <= mZeroCrossingTolerance)
710 const Real fraction = std::abs(previousCurrent) / denominator;
712 return previousTime + fraction * (currentTime - previousTime);
715void DP::Ph3::Switch::setZeroCrossingTime(
UInt phase,
Real time) {
718 **mZeroCrossingTimeA = time;
721 **mZeroCrossingTimeB = time;
724 **mZeroCrossingTimeC = time;
727 throw std::out_of_range(
"DP::Ph3::Switch phase index must be 0, 1, or 2.");
731void DP::Ph3::Switch::updateZeroCrossingState(
Real time) {
732 const Matrix current = reconstructInstantaneousCurrent(time);
735 if (!**mOpeningRequested) {
736 mPreviousInstantaneousCurrent = current;
737 mPreviousCurrentTime = time;
738 mPreviousCurrentValid =
true;
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);
752 "DP phase {} opened at sampled physical current zero: "
753 "t={:.9f}s, i={:.6e}A",
754 phase, time, current(phase, 0));
758 mPreviousInstantaneousCurrent = current;
759 mPreviousCurrentTime = time;
760 mPreviousCurrentValid =
true;
761 mResetZeroCrossingHistory =
false;
764 **mOpeningRequested =
false;
769 for (
UInt phase = 0; phase < 3; ++phase) {
770 if (!poleClosed(phase))
773 const Real previous = mPreviousInstantaneousCurrent(phase, 0);
774 const Real present = current(phase, 0);
776 if (!currentCrossedZero(previous, present))
779 const Real zeroTime = interpolateZeroCrossingTime(
780 previous, present, mPreviousCurrentTime, time);
782 setPoleClosed(phase,
false);
783 setZeroCrossingTime(phase, zeroTime);
785 SPDLOG_LOGGER_INFO(mSLog,
786 "DP phase {} physical current zero detected: "
787 "t_z={:.9f}s, i_prev={:.6e}A, i={:.6e}A. "
789 phase, zeroTime, previous, present);
792 mPreviousInstantaneousCurrent = current;
793 mPreviousCurrentTime = time;
794 mPreviousCurrentValid =
true;
796 if (allPolesOpen()) {
797 **mOpeningRequested =
false;
799 SPDLOG_LOGGER_INFO(mSLog,
"DP current-zero interruption completed: "
800 "all three physical 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.
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
@ ExponentialZCSEmulation
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
static String phasorMatrixToString(const MatrixComp &mat)
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)
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.
std::complex< Real > Complex
Eigen::Matrix< Complex, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixComp
Dense matrix for complex numbers.
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).