DPsim
Loading...
Searching...
No Matches
SP_Ph3_Transformer.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
5
6using namespace CPS;
7
9 Logger::Level logLevel,
10 Bool withResistiveLosses)
12 CompositePowerComp<Complex>(uid, name, true, true, logLevel) {
14 mWithResistiveLosses = withResistiveLosses;
15 if (withResistiveLosses)
17 else
20
21 SPDLOG_LOGGER_INFO(mSLog, "Create {} {}", this->type(), name);
22 **mIntfVoltage = MatrixComp::Zero(3, 1);
23 **mIntfCurrent = MatrixComp::Zero(3, 1);
24}
25
33
35 Real nomVoltageSecondary,
36 Real ratedPower, Real ratioAbs,
37 Real ratioPhase, Matrix resistance,
38 Matrix inductance) {
39 Base::Ph3::Transformer::setParameters(nomVoltagePrimary, nomVoltageSecondary,
40 ratedPower, ratioAbs, ratioPhase,
41 resistance, inductance);
42 mParametersSet = true;
43}
44
45void SP::Ph3::Transformer::resolveWindingRoles() {
46 switch (mReferenceWinding) {
48 mReferenceTerminal = 0;
49 break;
51 mReferenceTerminal = 1;
52 break;
54 SPDLOG_LOGGER_ERROR(mSLog,
55 "Transformer {}: three-winding transformers are "
56 "not implemented",
57 this->name());
60 mReferenceTerminal =
61 (mNominalVoltagePrimary >= mNominalVoltageSecondary) ? 0 : 1;
62 break;
63 }
64
65 mRatioFromReference = (mReferenceTerminal == 0) ? **mRatio : 1. / **mRatio;
66 mOrientationSign = (mReferenceTerminal == 0) ? 1. : -1.;
67
68 SPDLOG_LOGGER_INFO(
69 mSLog,
70 "Impedance referred to winding {} ({} [V]), remaining winding "
71 "at terminal {} ({} [V]), ratio = {}",
72 mReferenceTerminal + 1, nominalVoltageAt(mReferenceTerminal),
73 nonReferenceTerminal(), nominalVoltageAt(nonReferenceTerminal()),
74 Logger::complexToString(mRatioFromReference));
75}
76
77void SP::Ph3::Transformer::createSubComponents() {
78 if (mSubCompCreated)
79 return;
80 mSubCompCreated = true;
81
82 resolveWindingRoles();
83
84 auto midpoint = mVirtualNodes[2];
85
86 mSubInductor =
87 std::make_shared<SP::Ph3::Inductor>(**mName + "_ind", mLogLevel);
88 mSubInductor->setParameters(mInductance / 2.);
89 addMNASubComponent(mSubInductor, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
91
92 mSubInductor2 =
93 std::make_shared<SP::Ph3::Inductor>(**mName + "_ind2", mLogLevel);
94 mSubInductor2->setParameters(mInductance / 2.);
95 addMNASubComponent(mSubInductor2, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
97
98 if (mNumVirtualNodes == 5) {
99 mSubResistor =
100 std::make_shared<SP::Ph3::Resistor>(**mName + "_res", mLogLevel);
101 mSubResistor->setParameters(mResistance / 2.);
102 mSubResistor->connect({node(mReferenceTerminal), mVirtualNodes[3]});
103 mSubInductor->connect({mVirtualNodes[3], midpoint});
104 addMNASubComponent(mSubResistor, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
106
107 mSubResistor2 =
108 std::make_shared<SP::Ph3::Resistor>(**mName + "_res2", mLogLevel);
109 mSubResistor2->setParameters(mResistance / 2.);
110 mSubResistor2->connect({midpoint, mVirtualNodes[4]});
111 mSubInductor2->connect({mVirtualNodes[4], mVirtualNodes[0]});
112 addMNASubComponent(mSubResistor2,
115 } else {
116 mSubInductor->connect({node(mReferenceTerminal), midpoint});
117 mSubInductor2->connect({midpoint, mVirtualNodes[0]});
118 }
119
120 if (mRatedPower <= 0) {
121 SPDLOG_LOGGER_WARN(mSLog,
122 "Transformer {}: rated power is {} [VA], so the "
123 "magnetizing branch cannot be sized and is omitted",
124 this->name(), mRatedPower);
125 return;
126 }
127
128 if (mNoLoadCurrent <= mNoLoadLoss) {
129 SPDLOG_LOGGER_ERROR(mSLog,
130 "Transformer {}: no-load current {} must exceed the "
131 "no-load loss {}",
132 this->name(), mNoLoadCurrent, mNoLoadLoss);
133 throw InvalidArgumentException();
134 }
135
136 Real magnetizingResistance =
137 std::pow(nominalVoltageAt(mReferenceTerminal), 2) /
138 (mNoLoadLoss * mRatedPower);
139 mMagnetizingResistance =
140 Math::singlePhaseParameterToThreePhase(magnetizingResistance);
141 mSubMagnetizingResistor =
142 std::make_shared<SP::Ph3::Resistor>(**mName + "_mag_res", mLogLevel);
143 mSubMagnetizingResistor->setParameters(mMagnetizingResistance);
144 mSubMagnetizingResistor->connect({midpoint, SP::SimNode::GND});
145 addMNASubComponent(mSubMagnetizingResistor,
148
149 mSubMagnetizingInductor =
150 std::make_shared<SP::Ph3::Inductor>(**mName + "_mag_ind", mLogLevel);
151 mSubMagnetizingInductor->connect({midpoint, SP::SimNode::GND});
152 addMNASubComponent(mSubMagnetizingInductor,
155}
156
158 Real frequency) {
159 Real omega = 2. * PI * frequency;
160
161 if (mSubMagnetizingInductor) {
162 Real magnetizingSusceptance =
163 std::sqrt(std::pow(mNoLoadCurrent, 2) - std::pow(mNoLoadLoss, 2)) *
164 mRatedPower / std::pow(nominalVoltageAt(mReferenceTerminal), 2);
165 Real magnetizingInductance = 1. / (omega * magnetizingSusceptance);
166 mMagnetizingInductance =
167 Math::singlePhaseParameterToThreePhase(magnetizingInductance);
168 mSubMagnetizingInductor->setParameters(mMagnetizingInductance);
169 }
170
171 MatrixComp secondaryVoltage = initialVoltage(nonReferenceTerminal());
172 mVirtualNodes[0]->setInitialVoltage(secondaryVoltage * mRatioFromReference);
173
174 MatrixComp midpointVoltage = 0.5 * (initialVoltage(mReferenceTerminal) +
175 mVirtualNodes[0]->initialVoltage());
176 mVirtualNodes[2]->setInitialVoltage(midpointVoltage);
177
178 if (mNumVirtualNodes == 5) {
179 mVirtualNodes[3]->setInitialVoltage(initialVoltage(mReferenceTerminal));
180 mVirtualNodes[4]->setInitialVoltage(midpointVoltage);
181 }
182
184
185 MatrixComp impedance = MatrixComp::Zero(3, 3);
186 for (UInt row = 0; row < 3; row++)
187 for (UInt col = 0; col < 3; col++)
188 impedance(row, col) =
189 Complex(mResistance(row, col), omega * mInductance(row, col));
190
191 MatrixComp impedanceVoltage =
192 mOrientationSign *
193 (mVirtualNodes[0]->initialVoltage() - initialVoltage(mReferenceTerminal));
194 **mIntfCurrent = impedance.inverse() * impedanceVoltage;
195}
196
198 SparseMatrixRow &systemMatrix) {
199 if (terminalNotGrounded(mReferenceTerminal)) {
200 for (UInt phase = 0; phase < 3; phase++) {
202 systemMatrix,
203 mVirtualNodes[0]->matrixNodeIndex(static_cast<PhaseType>(phase)),
204 mVirtualNodes[1]->matrixNodeIndex(static_cast<PhaseType>(phase)),
205 Complex(-1.0, 0));
207 systemMatrix,
208 mVirtualNodes[1]->matrixNodeIndex(static_cast<PhaseType>(phase)),
209 mVirtualNodes[0]->matrixNodeIndex(static_cast<PhaseType>(phase)),
210 Complex(1.0, 0));
211 }
212 }
213 if (terminalNotGrounded(nonReferenceTerminal())) {
214 for (UInt phase = 0; phase < 3; phase++) {
216 systemMatrix, matrixNodeIndex(nonReferenceTerminal(), phase),
217 mVirtualNodes[1]->matrixNodeIndex(static_cast<PhaseType>(phase)),
218 std::conj(mRatioFromReference));
220 systemMatrix,
221 mVirtualNodes[1]->matrixNodeIndex(static_cast<PhaseType>(phase)),
222 matrixNodeIndex(nonReferenceTerminal(), phase), -mRatioFromReference);
223 }
224 }
225}
226
228 AttributeBase::List &prevStepDependencies,
229 AttributeBase::List &attributeDependencies,
230 AttributeBase::List &modifiedAttributes) {
231 prevStepDependencies.push_back(mIntfCurrent);
232 prevStepDependencies.push_back(mIntfVoltage);
233 modifiedAttributes.push_back(mRightVector);
234}
235
239
241 AttributeBase::List &prevStepDependencies,
242 AttributeBase::List &attributeDependencies,
243 AttributeBase::List &modifiedAttributes,
244 Attribute<Matrix>::Ptr &leftVector) {
245 attributeDependencies.push_back(leftVector);
246 modifiedAttributes.push_back(mIntfVoltage);
247 modifiedAttributes.push_back(mIntfCurrent);
248}
249
251 Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
252 mnaCompUpdateVoltage(**leftVector);
253 mnaCompUpdateCurrent(**leftVector);
254}
255
257 **mIntfVoltage = MatrixComp::Zero(3, 1);
258 for (UInt phase = 0; phase < 3; phase++) {
259 if (terminalNotGrounded(1))
260 (**mIntfVoltage)(phase, 0) =
261 Math::complexFromVectorElement(leftVector, matrixNodeIndex(1, phase));
262 if (terminalNotGrounded(0))
263 (**mIntfVoltage)(phase, 0) =
264 (**mIntfVoltage)(phase, 0) -
265 Math::complexFromVectorElement(leftVector, matrixNodeIndex(0, phase));
266 }
267}
268
270 **mIntfCurrent = mOrientationSign * mSubInductor->intfCurrent();
271}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
Real mNominalVoltagePrimary
Nominal voltage of primary side.
Real mNominalVoltageSecondary
Nominal voltage of secondary side.
const Attribute< Complex >::Ptr mRatio
Transformer ratio.
void setParameters(Real nomVoltagePrimary, Real nomVoltageSecondary, Real ratedPower, Real ratioAbs, Real ratioPhase, Matrix resistance, Matrix inductance)
Matrix mResistance
Resistance [Ohm].
Matrix mInductance
Inductance [H].
Real nominalVoltageAt(UInt terminal) const
Nominal voltage of the winding at the given terminal.
Real mRatedPower
Rated Apparent Power [VA].
CompositePowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
String uid()
Returns unique id.
String type()
Get component type (cross-platform)
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
static String complexToString(const Complex &num)
Definition Logger.cpp:63
Attribute< Matrix >::Ptr mRightVector
void mnaApplyRightSideVectorStamp(Matrix &rightVector) final
static Complex complexFromVectorElement(const Matrix &mat, Matrix::Index row, Int maxFreq=1, Int freqIdx=0)
Definition MathUtils.cpp:94
static void setMatrixElement(SparseMatrixRow &mat, Matrix::Index row, Matrix::Index column, Complex value, Int maxFreq=1, Int freqIdx=0)
static Matrix singlePhaseParameterToThreePhase(Real parameter)
To convert single phase parameters to symmetrical three phase ones.
void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Transformer(String uid, String name, Logger::Level logLevel=Logger::Level::off, Bool withResistiveLosses=false)
Defines UID, name and logging level.
void initializeParentFromNodesAndTerminals(Real frequency) override
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
void mnaParentApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
void mnaCompUpdateVoltage(const Matrix &leftVector) override
void setParameters(Real nomVoltagePrimary, Real nomVoltageSecondary, Real ratedPower, Real ratioAbs, Real ratioPhase, Matrix resistance, Matrix inductance)
Defines component parameters.
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 mnaCompUpdateCurrent(const Matrix &leftVector) override
void mnaParentPreStep(Real time, Int timeStepCount) override
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
MatrixComp initialVoltage(UInt index)
std::shared_ptr< SimPowerComp< VarType > > Ptr
SimNode< Complex >::List mVirtualNodes
Bool terminalNotGrounded(UInt index)
Logger::Level mLogLevel
Component logger control for internal variables.
UInt mNumVirtualNodes
Determines the number of virtual or internal Nodes.
bool mParametersSet
Flag indicating that parameters are set via setParameters() function.
Logger::Log mSLog
Component logger.
static std::shared_ptr< Transformer > make(Args &&...args)
Definition PtrFactory.h:19
#define PI
Definition Definitions.h:43
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::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:72