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
27 auto copy = Transformer::make(name, mLogLevel);
29 mRatedPower, std::abs(**mRatio), std::arg(**mRatio),
31 copy->setTapRatio(tapRatio());
32 return copy;
33}
34
36 Real nomVoltageSecondary,
37 Real ratedPower, Real ratioAbs,
38 Real ratioPhase, Matrix resistance,
39 Matrix inductance) {
40 Base::Ph3::Transformer::setParameters(nomVoltagePrimary, nomVoltageSecondary,
41 ratedPower, ratioAbs, ratioPhase,
42 resistance, inductance);
43 mParametersSet = true;
44}
45
46void SP::Ph3::Transformer::resolveWindingRoles() {
47 switch (mReferenceWinding) {
49 mReferenceTerminal = 0;
50 break;
52 mReferenceTerminal = 1;
53 break;
55 SPDLOG_LOGGER_ERROR(mSLog,
56 "Transformer {}: three-winding transformers are "
57 "not implemented",
58 this->name());
61 mReferenceTerminal =
62 (mNominalVoltagePrimary >= mNominalVoltageSecondary) ? 0 : 1;
63 break;
64 }
65
66 mRatioFromReference =
67 (mReferenceTerminal == 0) ? effectiveRatio() : 1. / effectiveRatio();
68 mOrientationSign = (mReferenceTerminal == 0) ? 1. : -1.;
69
70 SPDLOG_LOGGER_INFO(
71 mSLog,
72 "Impedance referred to winding {} ({} [V]), remaining winding "
73 "at terminal {} ({} [V]), ratio = {}",
74 mReferenceTerminal + 1, nominalVoltageAt(mReferenceTerminal),
75 nonReferenceTerminal(), nominalVoltageAt(nonReferenceTerminal()),
76 Logger::complexToString(mRatioFromReference));
77}
78
79void SP::Ph3::Transformer::createSubComponents() {
80 if (mSubCompCreated)
81 return;
82 mSubCompCreated = true;
83
84 resolveWindingRoles();
85
86 auto midpoint = mVirtualNodes[2];
87
88 mSubInductor =
89 std::make_shared<SP::Ph3::Inductor>(**mName + "_ind", mLogLevel);
90 mSubInductor->setParameters(mInductance / 2.);
91 addMNASubComponent(mSubInductor, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
93
94 mSubInductor2 =
95 std::make_shared<SP::Ph3::Inductor>(**mName + "_ind2", mLogLevel);
96 mSubInductor2->setParameters(mInductance / 2.);
97 addMNASubComponent(mSubInductor2, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
99
100 if (mNumVirtualNodes == 5) {
101 mSubResistor =
102 std::make_shared<SP::Ph3::Resistor>(**mName + "_res", mLogLevel);
103 mSubResistor->setParameters(mResistance / 2.);
104 mSubResistor->connect({node(mReferenceTerminal), mVirtualNodes[3]});
105 mSubInductor->connect({mVirtualNodes[3], midpoint});
106 addMNASubComponent(mSubResistor, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
108
109 mSubResistor2 =
110 std::make_shared<SP::Ph3::Resistor>(**mName + "_res2", mLogLevel);
111 mSubResistor2->setParameters(mResistance / 2.);
112 mSubResistor2->connect({midpoint, mVirtualNodes[4]});
113 mSubInductor2->connect({mVirtualNodes[4], mVirtualNodes[0]});
114 addMNASubComponent(mSubResistor2,
117 } else {
118 mSubInductor->connect({node(mReferenceTerminal), midpoint});
119 mSubInductor2->connect({midpoint, mVirtualNodes[0]});
120 }
121
122 if (mRatedPower <= 0) {
123 SPDLOG_LOGGER_WARN(mSLog,
124 "Transformer {}: rated power is {} [VA], so the "
125 "magnetizing branch cannot be sized and is omitted",
126 this->name(), mRatedPower);
127 return;
128 }
129
130 if (mNoLoadCurrent <= mNoLoadLoss) {
131 SPDLOG_LOGGER_ERROR(mSLog,
132 "Transformer {}: no-load current {} must exceed the "
133 "no-load loss {}",
134 this->name(), mNoLoadCurrent, mNoLoadLoss);
135 throw InvalidArgumentException();
136 }
137
138 Real magnetizingResistance =
139 std::pow(nominalVoltageAt(mReferenceTerminal), 2) /
140 (mNoLoadLoss * mRatedPower);
141 mMagnetizingResistance =
142 Math::singlePhaseParameterToThreePhase(magnetizingResistance);
143 mSubMagnetizingResistor =
144 std::make_shared<SP::Ph3::Resistor>(**mName + "_mag_res", mLogLevel);
145 mSubMagnetizingResistor->setParameters(mMagnetizingResistance);
146 mSubMagnetizingResistor->connect({midpoint, SP::SimNode::GND});
147 addMNASubComponent(mSubMagnetizingResistor,
150
151 mSubMagnetizingInductor =
152 std::make_shared<SP::Ph3::Inductor>(**mName + "_mag_ind", mLogLevel);
153 mSubMagnetizingInductor->connect({midpoint, SP::SimNode::GND});
154 addMNASubComponent(mSubMagnetizingInductor,
157}
158
160 Real frequency) {
161 Real omega = 2. * PI * frequency;
162
163 if (mSubMagnetizingInductor) {
164 Real magnetizingSusceptance =
165 std::sqrt(std::pow(mNoLoadCurrent, 2) - std::pow(mNoLoadLoss, 2)) *
166 mRatedPower / std::pow(nominalVoltageAt(mReferenceTerminal), 2);
167 Real magnetizingInductance = 1. / (omega * magnetizingSusceptance);
168 mMagnetizingInductance =
169 Math::singlePhaseParameterToThreePhase(magnetizingInductance);
170 mSubMagnetizingInductor->setParameters(mMagnetizingInductance);
171 }
172
173 MatrixComp secondaryVoltage = initialVoltage(nonReferenceTerminal());
174 mVirtualNodes[0]->setInitialVoltage(secondaryVoltage * mRatioFromReference);
175
176 MatrixComp midpointVoltage = 0.5 * (initialVoltage(mReferenceTerminal) +
177 mVirtualNodes[0]->initialVoltage());
178 mVirtualNodes[2]->setInitialVoltage(midpointVoltage);
179
180 if (mNumVirtualNodes == 5) {
181 mVirtualNodes[3]->setInitialVoltage(initialVoltage(mReferenceTerminal));
182 mVirtualNodes[4]->setInitialVoltage(midpointVoltage);
183 }
184
186
187 MatrixComp impedance = MatrixComp::Zero(3, 3);
188 for (UInt row = 0; row < 3; row++)
189 for (UInt col = 0; col < 3; col++)
190 impedance(row, col) =
191 Complex(mResistance(row, col), omega * mInductance(row, col));
192
193 MatrixComp impedanceVoltage =
194 mOrientationSign *
195 (mVirtualNodes[0]->initialVoltage() - initialVoltage(mReferenceTerminal));
196 **mIntfCurrent = impedance.inverse() * impedanceVoltage;
197}
198
200 SparseMatrixRow &systemMatrix) {
201 if (terminalNotGrounded(mReferenceTerminal)) {
202 for (UInt phase = 0; phase < 3; phase++) {
204 systemMatrix,
205 mVirtualNodes[0]->matrixNodeIndex(static_cast<PhaseType>(phase)),
206 mVirtualNodes[1]->matrixNodeIndex(static_cast<PhaseType>(phase)),
207 Complex(-1.0, 0));
209 systemMatrix,
210 mVirtualNodes[1]->matrixNodeIndex(static_cast<PhaseType>(phase)),
211 mVirtualNodes[0]->matrixNodeIndex(static_cast<PhaseType>(phase)),
212 Complex(1.0, 0));
213 }
214 }
215 if (terminalNotGrounded(nonReferenceTerminal())) {
216 for (UInt phase = 0; phase < 3; phase++) {
218 systemMatrix, matrixNodeIndex(nonReferenceTerminal(), phase),
219 mVirtualNodes[1]->matrixNodeIndex(static_cast<PhaseType>(phase)),
220 std::conj(mRatioFromReference));
222 systemMatrix,
223 mVirtualNodes[1]->matrixNodeIndex(static_cast<PhaseType>(phase)),
224 matrixNodeIndex(nonReferenceTerminal(), phase), -mRatioFromReference);
225 }
226 }
227}
228
230 AttributeBase::List &prevStepDependencies,
231 AttributeBase::List &attributeDependencies,
232 AttributeBase::List &modifiedAttributes) {
233 prevStepDependencies.push_back(mIntfCurrent);
234 prevStepDependencies.push_back(mIntfVoltage);
235 modifiedAttributes.push_back(mRightVector);
236}
237
241
243 AttributeBase::List &prevStepDependencies,
244 AttributeBase::List &attributeDependencies,
245 AttributeBase::List &modifiedAttributes,
246 Attribute<Matrix>::Ptr &leftVector) {
247 attributeDependencies.push_back(leftVector);
248 modifiedAttributes.push_back(mIntfVoltage);
249 modifiedAttributes.push_back(mIntfCurrent);
250}
251
253 Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
254 mnaCompUpdateVoltage(**leftVector);
255 mnaCompUpdateCurrent(**leftVector);
256}
257
259 (**mIntfVoltage).setZero();
260 for (UInt phase = 0; phase < 3; phase++) {
261 if (terminalNotGrounded(1))
262 (**mIntfVoltage)(phase, 0) =
263 Math::complexFromVectorElement(leftVector, matrixNodeIndex(1, phase));
264 if (terminalNotGrounded(0))
265 (**mIntfVoltage)(phase, 0) =
266 (**mIntfVoltage)(phase, 0) -
267 Math::complexFromVectorElement(leftVector, matrixNodeIndex(0, phase));
268 }
269}
270
272 **mIntfCurrent = mOrientationSign * mSubInductor->intfCurrent();
273}
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)
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