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
10
11using namespace CPS;
12
13// !!! TODO: Adaptions to use in EMT_Ph3 models phase-to-ground peak variables
14// !!! with initialization from phase-to-phase RMS variables
15
17 : MNASimPowerComp<Real>(uid, name, false, true, logLevel),
20 **mIntfVoltage = Matrix::Zero(1, 1);
21 **mIntfCurrent = Matrix::Zero(1, 1);
22}
23
25 auto copy = Switch::make(name, mLogLevel);
26 copy->setParameters(**mOpenResistance, **mClosedResistance, **mIsClosed);
27 return copy;
28}
29
31
32 Matrix impedance = (**mIsClosed) ? **mClosedResistance : **mOpenResistance;
33 MatrixComp vInitABC = MatrixComp::Zero(3, 1);
34 vInitABC(0, 0) = initialSingleVoltage(1) - initialSingleVoltage(0);
35 vInitABC(1, 0) = vInitABC(0, 0) * SHIFT_TO_PHASE_B;
36 vInitABC(2, 0) = vInitABC(0, 0) * SHIFT_TO_PHASE_C;
37 **mIntfVoltage = vInitABC.real();
38 **mIntfCurrent = (impedance.inverse() * vInitABC).real();
39
40 SPDLOG_LOGGER_INFO(mSLog,
41 "\n--- Initialization from powerflow ---"
42 "\nVoltage across: {:s}"
43 "\nCurrent: {:s}"
44 "\nTerminal 0 voltage: {:s}"
45 "\nTerminal 1 voltage: {:s}"
46 "\n--- Initialization from powerflow finished ---",
51}
52
54 Attribute<Matrix>::Ptr leftVector) {
56 **mRightVector = Matrix::Zero(0, 0);
57}
58
60
62 SparseMatrixRow &systemMatrix) {
63 MatrixFixedSize<3, 3> conductance = (**mIsClosed)
64 ? (**mClosedResistance).inverse()
65 : (**mOpenResistance).inverse();
66
68 conductance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1),
70
71 SPDLOG_LOGGER_TRACE(mSLog, "\nConductance matrix: {:s}",
72 Logger::matrixToString(conductance));
73}
74
76 Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) {
77 MatrixFixedSize<3, 3> conductance = (closed) ? (**mClosedResistance).inverse()
78 : (**mOpenResistance).inverse();
79
81 conductance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1),
83
84 SPDLOG_LOGGER_TRACE(mSLog, "\nConductance matrix: {:s}",
85 Logger::matrixToString(conductance));
86}
87
89
91 AttributeBase::List &prevStepDependencies,
92 AttributeBase::List &attributeDependencies,
93 AttributeBase::List &modifiedAttributes,
94 Attribute<Matrix>::Ptr &leftVector) {
95 attributeDependencies.push_back(leftVector);
96 modifiedAttributes.push_back(mIntfVoltage);
97 modifiedAttributes.push_back(mIntfCurrent);
98}
99
101 Attribute<Matrix>::Ptr &leftVector) {
102 mnaCompUpdateVoltage(**leftVector);
103 mnaCompUpdateCurrent(**leftVector);
104}
105
107 // Voltage across component is defined as V1 - V0
108 **mIntfVoltage = Matrix::Zero(3, 1);
109 if (terminalNotGrounded(1)) {
110 (**mIntfVoltage)(0, 0) =
112 (**mIntfVoltage)(1, 0) =
114 (**mIntfVoltage)(2, 0) =
116 }
117 if (terminalNotGrounded(0)) {
118 (**mIntfVoltage)(0, 0) =
119 (**mIntfVoltage)(0, 0) -
121 (**mIntfVoltage)(1, 0) =
122 (**mIntfVoltage)(1, 0) -
124 (**mIntfVoltage)(2, 0) =
125 (**mIntfVoltage)(2, 0) -
127 }
128}
129
131 Matrix conductance = Matrix::Zero(3, 3);
132 (**mIsClosed) ? Math::invertMatrix(**mClosedResistance, conductance)
133 : Math::invertMatrix(**mOpenResistance, conductance);
134
135 **mIntfCurrent = conductance * **mIntfVoltage;
136}
137
139 // Check if state of switch changed
140 if (!(mIsClosedPrev == this->mnaIsClosed())) {
141 mIsClosedPrev = this->mnaIsClosed();
142 return 1; //recompute system matrix
143 } else {
144 return 0; // do not recompute system matrix
145 }
146};
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:250
const CPS::Attribute< Matrix >::Ptr mClosedResistance
Resistance if switch is closed [ohm].
const CPS::Attribute< Matrix >::Ptr mOpenResistance
Resistance if switch is open [ohm].
const CPS::Attribute< Bool >::Ptr mIsClosed
Defines if Switch is open or closed.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Update interface current from MNA system result.
Bool hasParameterChanged() override
Returns true if one of the element paramters has changed.
SimPowerComp< Real >::Ptr clone(String name) override
Returns a modified copy of the component with the given suffix added to the name and without.
void mnaCompApplySwitchSystemMatrixStamp(Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) override
Stamps system matrix considering the defined switch position.
void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
Switch(String uid, String name, Logger::Level loglevel=Logger::Level::off)
Defines UID, name, component parameters and logging level.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Update interface voltage from MNA system result.
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamps system matrix.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
Bool mnaIsClosed() override
Check if switch is closed.
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)
static void invertMatrix(const Matrix &mat, Matrix &matInv)
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 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:81
std::string String
Definition Definitions.h:65
Eigen::Matrix< Real, rows, cols, Eigen::ColMajor > MatrixFixedSize
Dense matrix for real numbers with fixed dimension.
double Real
Definition Definitions.h:62
int Int
Definition Definitions.h:61
Eigen::Matrix< Complex, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixComp
Dense matrix for complex numbers.
Definition Definitions.h:84
bool Bool
Definition Definitions.h:64
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:74