DPsim
Loading...
Searching...
No Matches
EMT_DC_PiLine.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
6
7using namespace CPS;
8
10 : PiLine(uid, name, true, logLevel) {}
11
13 Logger::Level logLevel)
14 : CompositePowerComp<Real>(uid, name, true, true, logLevel),
15 mSeriesResistance(mAttributes->create<Real>("R_series")),
16 mSeriesInductance(mAttributes->create<Real>("L_series")),
17 mParallelCapacitance(mAttributes->create<Real>("C_parallel", 0.0)),
18 mParallelConductance(mAttributes->create<Real>("G_parallel", 0.0)),
19 mInitialCurrent(mAttributes->create<Real>("i_init", 0.0)) {
20 mUseSubcomponents = useSubcomponents;
21 mDirectState = mAttributes->create<Matrix>("direct_state");
22 **mDirectState = Matrix::Zero(6, 1);
24 setVirtualNodeNumber(mUseSubcomponents ? 1 : 0);
26 **mIntfVoltage = Matrix::Zero(1, 1);
27 **mIntfCurrent = Matrix::Zero(1, 1);
28}
29
31 auto copy = PiLine::make(name, mUseSubcomponents, mLogLevel);
32 copy->setParameters(**mSeriesResistance, **mSeriesInductance,
35 return copy;
36}
37
39 Real seriesInductance,
40 Real parallelCapacitance,
41 Real parallelConductance,
42 Real initialCurrent) {
43 const Real epsilon = std::numeric_limits<Real>::epsilon();
44 if (!Math::isFinite(seriesResistance) || seriesResistance <= DOUBLE_EPSILON)
45 throw std::invalid_argument(
46 "DC pi-line series resistance must be finite and positive.");
47 if (!Math::isFinite(seriesInductance) || seriesInductance <= DOUBLE_EPSILON)
48 throw std::invalid_argument(
49 "DC pi-line series inductance must be finite and positive.");
50 if (!Math::isFinite(parallelCapacitance) || parallelCapacitance < 0.0 ||
51 (parallelCapacitance > 0.0 &&
52 parallelCapacitance / 2.0 <= DOUBLE_EPSILON))
53 throw std::invalid_argument(
54 "DC pi-line shunt capacitance must be zero or safely positive.");
55 if (!Math::isFinite(parallelConductance) || parallelConductance < 0.0 ||
56 (parallelConductance > 0.0 && parallelConductance <= DOUBLE_EPSILON))
57 throw std::invalid_argument(
58 "DC pi-line shunt conductance must be zero or safely positive.");
59 if (!Math::isFinite(initialCurrent))
60 throw std::invalid_argument("DC pi-line initial current must be finite.");
61
62 **mSeriesResistance = seriesResistance;
63 **mSeriesInductance = seriesInductance;
64 **mParallelCapacitance = parallelCapacitance;
65 **mParallelConductance = parallelConductance;
66 **mInitialCurrent = initialCurrent;
67 mParametersSet = true;
68}
69
70void EMT::DC::PiLine::validateDCTerminals() const {
71 for (UInt terminalIdx = 0; terminalIdx < 2; ++terminalIdx) {
72 const auto terminalNode = const_cast<PiLine *>(this)->node(terminalIdx);
73 if (!terminalNode->isGround() && terminalNode->phaseType() != PhaseType::DC)
74 throw std::invalid_argument(
75 "DC pi-line requires DC nodes or ground at both terminals.");
76 }
77}
78
81 return;
82 if (!mParametersSet)
83 throw std::logic_error(
84 "setParameters() must be called before pi-line construction.");
85 validateDCTerminals();
86 mSubCompCreated = true;
87
88 if (!mUseSubcomponents)
89 return;
90
91 mSeriesResistor = Resistor::make(**mName + "_series_R", mLogLevel);
92 mSeriesResistor->setParameters(**mSeriesResistance);
93 mSeriesResistor->connect({mTerminals[0]->node(), mVirtualNodes[0]});
94 addMNASubComponent(mSeriesResistor,
97
98 mSeriesInductor = Inductor::make(**mName + "_series_L", mLogLevel);
99 mSeriesInductor->setParameters(**mSeriesInductance, **mInitialCurrent);
100 mSeriesInductor->connect({mVirtualNodes[0], mTerminals[1]->node()});
101 addMNASubComponent(mSeriesInductor,
104
105 if (**mParallelConductance > 0.0) {
106 const Real halfShuntResistance = 2.0 / **mParallelConductance;
107 if (!Math::isFinite(halfShuntResistance))
108 throw std::invalid_argument("DC pi-line shunt resistance is non-finite.");
109
110 mShuntResistor0 = Resistor::make(**mName + "_shunt_R0", mLogLevel);
111 mShuntResistor0->setParameters(halfShuntResistance);
112 mShuntResistor0->connect({EMT::SimNode::GND, mTerminals[0]->node()});
113 addMNASubComponent(mShuntResistor0,
116
117 mShuntResistor1 = Resistor::make(**mName + "_shunt_R1", mLogLevel);
118 mShuntResistor1->setParameters(halfShuntResistance);
119 mShuntResistor1->connect({EMT::SimNode::GND, mTerminals[1]->node()});
120 addMNASubComponent(mShuntResistor1,
123 }
124
125 if (**mParallelCapacitance > 0.0) {
126 const Real halfCapacitance = **mParallelCapacitance / 2.0;
127 mShuntCapacitor0 = Capacitor::make(**mName + "_shunt_C0", mLogLevel);
128 mShuntCapacitor0->setParameters(halfCapacitance);
129 mShuntCapacitor0->connect({EMT::SimNode::GND, mTerminals[0]->node()});
130 addMNASubComponent(mShuntCapacitor0,
133
134 mShuntCapacitor1 = Capacitor::make(**mName + "_shunt_C1", mLogLevel);
135 mShuntCapacitor1->setParameters(halfCapacitance);
136 mShuntCapacitor1->connect({EMT::SimNode::GND, mTerminals[1]->node()});
137 addMNASubComponent(mShuntCapacitor1,
140 }
141}
142
144 validateDCTerminals();
145 const Complex voltage0 = initialSingleVoltage(0);
146 const Complex voltage1 = initialSingleVoltage(1);
147 if (!Math::isFinite(voltage0) || !Math::isFinite(voltage1) ||
148 std::abs(voltage0.imag()) > std::numeric_limits<Real>::epsilon() ||
149 std::abs(voltage1.imag()) > std::numeric_limits<Real>::epsilon())
150 throw std::invalid_argument(
151 "DC pi-line initial node voltages must be finite real values.");
152
153 (**mIntfVoltage)(0, 0) = voltage1.real() - voltage0.real();
154 (**mIntfCurrent)(0, 0) = **mInitialCurrent;
155
156 if (!mUseSubcomponents) {
157 (**mDirectState).setZero();
158 (**mDirectState)(0, 0) = **mInitialCurrent;
159 (**mDirectState)(1, 0) = voltage1.real() - voltage0.real() -
161 (**mDirectState)(2, 0) = voltage0.real();
162 (**mDirectState)(4, 0) = voltage1.real();
163 } else {
164 const Real virtualVoltage =
165 voltage0.real() + **mSeriesResistance * **mInitialCurrent;
166 mVirtualNodes[0]->setInitialVoltage(Complex(virtualVoltage, 0.0));
167 }
168}
169
172 if (mUseSubcomponents)
173 return;
174 mDirectInductorAdmittance = timeStep / (2. * **mSeriesInductance);
175 mDirectSeriesAdmittance =
176 mDirectInductorAdmittance /
177 (1. + **mSeriesResistance * mDirectInductorAdmittance);
178 mDirectCapAdmittance = **mParallelCapacitance / timeStep;
179 const Real denominator = 1. + **mSeriesResistance * mDirectInductorAdmittance;
180 mDirectSeriesHistory = (mDirectInductorAdmittance * (**mDirectState)(1, 0) +
181 (**mDirectState)(0, 0)) /
182 denominator;
183}
184
186 SparseMatrixRow &systemMatrix) {
187 if (mUseSubcomponents)
188 return;
190 mDirectTerminalRows = {matrixNodeIndex(0), matrixNodeIndex(1)};
191 mDirectTerminalNotGrounded = {terminalNotGrounded(0), terminalNotGrounded(1)};
193 mDirectSeriesAdmittance, systemMatrix, mDirectTerminalRows[0],
194 mDirectTerminalRows[1], mDirectTerminalNotGrounded[0],
195 mDirectTerminalNotGrounded[1], mSLog);
196 const Real shuntAdmittance =
197 mDirectCapAdmittance + **mParallelConductance / 2.;
198 for (UInt terminal = 0; terminal < 2; ++terminal)
200 shuntAdmittance, systemMatrix, matrixNodeIndex(terminal), 0,
201 mDirectTerminalNotGrounded[terminal], false, mSLog);
202}
203
205 if (mUseSubcomponents)
206 return;
207 if (mDirectTerminalNotGrounded[0])
208 Math::setVectorElement(rightVector, mDirectTerminalRows[0],
209 mDirectSeriesHistory - mDirectCapHistory0);
210 if (mDirectTerminalNotGrounded[1])
211 Math::setVectorElement(rightVector, mDirectTerminalRows[1],
212 -mDirectSeriesHistory - mDirectCapHistory1);
213}
214
219
221 if (mUseSubcomponents) {
223 return;
224 }
225 for (UInt terminal = 0; terminal < 2; ++terminal)
226 if (mDirectTerminalNotGrounded[terminal]) {
227 const UInt index = mDirectTerminalRows[terminal];
229 rightVector, index,
231 }
232}
233
235 (**mIntfVoltage)(0, 0) = 0.0;
236 if (terminalNotGrounded(1))
237 (**mIntfVoltage)(0, 0) =
239 if (terminalNotGrounded(0))
240 (**mIntfVoltage)(0, 0) -=
242 if (!Math::isFinite((**mIntfVoltage)(0, 0)))
243 throw std::runtime_error(
244 "DC pi-line voltage update produced a non-finite value.");
245}
246
248 if (!mUseSubcomponents) {
249 (**mIntfCurrent)(0, 0) =
250 mDirectSeriesAdmittance * (**mIntfVoltage)(0, 0) + mDirectSeriesHistory;
251 return;
252 }
253 **mIntfCurrent = mSeriesInductor->intfCurrent();
254 if (!(**mIntfCurrent).allFinite())
255 throw std::runtime_error(
256 "DC pi-line current update produced a non-finite value.");
257}
258
260 if (!mUseSubcomponents) {
261 const Real denominator =
262 1. + **mSeriesResistance * mDirectInductorAdmittance;
263 mDirectSeriesHistory = (mDirectInductorAdmittance * (**mDirectState)(1, 0) +
264 (**mDirectState)(0, 0)) /
265 denominator;
266 mDirectCapHistory0 = -((**mDirectState)(3, 0) +
267 mDirectCapAdmittance * (**mDirectState)(2, 0));
268 mDirectCapHistory1 = -((**mDirectState)(5, 0) +
269 mDirectCapAdmittance * (**mDirectState)(4, 0));
270 }
272}
273
275 Attribute<Matrix>::Ptr &leftVector) {
276 if (mUseSubcomponents) {
277 mnaCompUpdateVoltage(**leftVector);
278 mnaCompUpdateCurrent(**leftVector);
279 return;
280 }
281
282 const Real voltage0 =
283 mDirectTerminalNotGrounded[0]
284 ? Math::realFromVectorElement(**leftVector, mDirectTerminalRows[0])
285 : 0.;
286 const Real voltage1 =
287 mDirectTerminalNotGrounded[1]
288 ? Math::realFromVectorElement(**leftVector, mDirectTerminalRows[1])
289 : 0.;
290 (**mIntfVoltage)(0, 0) = voltage1;
291 (**mIntfVoltage)(0, 0) -= voltage0;
292 if (!Math::isFinite((**mIntfVoltage)(0, 0)))
293 throw std::runtime_error(
294 "DC pi-line voltage update produced a non-finite value.");
295 mnaCompUpdateCurrent(**leftVector);
296 (**mDirectState)(0, 0) = (**mIntfCurrent)(0, 0);
297 (**mDirectState)(1, 0) =
298 (**mIntfVoltage)(0, 0) - **mSeriesResistance * (**mIntfCurrent)(0, 0);
299 (**mDirectState)(2, 0) = voltage0;
300 (**mDirectState)(3, 0) = mDirectCapAdmittance * voltage0 + mDirectCapHistory0;
301 (**mDirectState)(4, 0) = voltage1;
302 (**mDirectState)(5, 0) = mDirectCapAdmittance * voltage1 + mDirectCapHistory1;
303}
304
306 AttributeBase::List &prevStepDependencies, AttributeBase::List &,
307 AttributeBase::List &modifiedAttributes) {
308 if (!mUseSubcomponents)
309 prevStepDependencies.push_back(mDirectState);
310 modifiedAttributes.push_back(mRightVector);
311}
312
314 AttributeBase::List &, AttributeBase::List &attributeDependencies,
315 AttributeBase::List &modifiedAttributes,
316 Attribute<Matrix>::Ptr &leftVector) {
317 attributeDependencies.push_back(leftVector);
318 modifiedAttributes.push_back(mIntfVoltage);
319 modifiedAttributes.push_back(mIntfCurrent);
320 if (!mUseSubcomponents)
321 modifiedAttributes.push_back(mDirectState);
322}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
void addMNASubComponent(typename SimPowerComp< Real >::Ptr subc, MNA_SUBCOMP_TASK_ORDER preStepOrder, MNA_SUBCOMP_TASK_ORDER postStepOrder, Bool contributeToRightVector)
Bool mnaHasCompactRightSideVectorStamp() const override
Composite stamps touch only their cached terminal and virtual-node rows.
void mnaAddCompactRightSideVectorStamp(Matrix &rightVector) override
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
CompositePowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
const Attribute< Real >::Ptr mSeriesResistance
Series resistance [ohm].
Bool mnaHasCompactRightSideVectorStamp() const override final
Composite stamps touch only their cached terminal and virtual-node rows.
void initializeParentFromNodesAndTerminals(Real frequency) override final
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override final
const Attribute< Real >::Ptr mInitialCurrent
Initial series current from terminal 1 to terminal 0 [A].
void mnaCompUpdateVoltage(const Matrix &leftVector) override final
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override final
void mnaParentInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override final
void mnaParentApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override final
const Attribute< Real >::Ptr mParallelCapacitance
Total terminal-to-ground shunt capacitance [F].
const Attribute< Real >::Ptr mSeriesInductance
Series inductance [H].
void mnaAddCompactRightSideVectorStamp(Matrix &rightVector) override final
void createSubComponents() override final
Constructs and registers MNA subcomponents; called before collectVirtualNodes so virtual nodes exist ...
void mnaParentPreStep(Real time, Int timeStepCount) override final
void setParameters(Real seriesResistance, Real seriesInductance, Real parallelCapacitance=0.0, Real parallelConductance=0.0, Real initialCurrent=0.0)
void mnaCompUpdateCurrent(const Matrix &leftVector) override final
const Attribute< Real >::Ptr mParallelConductance
Total terminal-to-ground shunt conductance [S].
SimPowerComp< Real >::Ptr clone(String name) override final
Returns a modified copy of the component with the given suffix added to the name and without.
void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override final
void mnaParentApplyRightSideVectorStamp(Matrix &rightVector) override final
PiLine(String uid, String name, Logger::Level logLevel=Logger::Level::off)
const Attribute< String >::Ptr mName
Human readable name.
String uid()
Returns unique id.
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
Attribute< Matrix >::Ptr mRightVector
static void stampConductance(Real conductance, 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 setVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0, Matrix::Index colOffset=0)
static void addToVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0)
static bool isFinite(Real value)
static Ptr GND
Definition SimNode.h:37
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Real > >::Ptr mIntfCurrent
SimTerminal< Real >::List mTerminals
SimTerminal< Real >::Ptr terminal(UInt index)
void setVirtualNodeNumber(UInt num)
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
std::shared_ptr< SimPowerComp< VarType > > Ptr
SimNode< Real >::List mVirtualNodes
Bool terminalNotGrounded(UInt index)
Complex initialSingleVoltage(UInt index)
void setTerminalNumber(UInt num)
Logger::Level mLogLevel
Component logger control for internal variables.
bool mParametersSet
Flag indicating that parameters are set via setParameters() function.
Logger::Log mSLog
Component logger.
static std::shared_ptr< PiLine > make(Args &&...args)
Definition PtrFactory.h:19
#define DOUBLE_EPSILON
Definition Definitions.h:14
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
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