DPsim
Loading...
Searching...
No Matches
EMT_Ph1_PiLine.cpp
Go to the documentation of this file.
1/* Author: Christoph Wirtz <christoph.wirtz@fgh-ma.de>
2 * SPDX-FileCopyrightText: 2025 FGH e.V.
3 * SPDX-License-Identifier: MPL-2.0
4 */
5
8
9using namespace CPS;
10
13
15 Logger::Level logLevel)
17 CompositePowerComp<Real>(uid, name, true, true, logLevel) {
18 mUseSubcomponents = useSubcomponents;
19 // Keep the legacy virtual-node allocation so the matrix graph has the same
20 // dimensions in either mode; the direct realization leaves that variable
21 // isolated with a unit diagonal.
24
25 SPDLOG_LOGGER_INFO(mSLog, "Create {} {}", this->type(), name);
26 **mIntfVoltage = Matrix::Zero(1, 1);
27 **mIntfCurrent = Matrix::Zero(1, 1);
28
29 mSLog->flush();
30}
31
39
42 return;
43 mSubCompCreated = true;
44
45 // By default there is always a small conductance to ground to
46 // avoid problems with floating nodes.
47 Real defaultParallelCond = 1e-6;
49 (**mParallelCond > 0) ? **mParallelCond : defaultParallelCond;
50
52 return;
53
54 // Create series sub components
56 std::make_shared<EMT::Ph1::Resistor>(**mName + "_res", mLogLevel);
57 mSubSeriesResistor->setParameters(**mSeriesRes);
58 mSubSeriesResistor->connect({mTerminals[0]->node(), mVirtualNodes[0]});
62
64 std::make_shared<EMT::Ph1::Inductor>(**mName + "_ind", mLogLevel);
65 mSubSeriesInductor->setParameters(**mSeriesInd);
66 mSubSeriesInductor->connect({mVirtualNodes[0], mTerminals[1]->node()});
70
71 // Create parallel sub components
73 std::make_shared<EMT::Ph1::Resistor>(**mName + "_con0", mLogLevel);
74 mSubParallelResistor0->setParameters(2. / (**mParallelCond));
75 mSubParallelResistor0->connect(
80
82 std::make_shared<EMT::Ph1::Resistor>(**mName + "_con1", mLogLevel);
83 mSubParallelResistor1->setParameters(2. / (**mParallelCond));
84 mSubParallelResistor1->connect(
89
90 if ((**mParallelCap) > 0) {
92 std::make_shared<EMT::Ph1::Capacitor>(**mName + "_cap0", mLogLevel);
93 mSubParallelCapacitor0->setParameters(**mParallelCap / 2.);
99
101 std::make_shared<EMT::Ph1::Capacitor>(**mName + "_cap1", mLogLevel);
102 mSubParallelCapacitor1->setParameters(**mParallelCap / 2.);
103 mSubParallelCapacitor1->connect(
108 }
109}
110
112 // Static calculation
113 Real omega = 2. * PI * frequency;
114 Complex impedance = {**mSeriesRes, omega * **mSeriesInd};
115 Complex voltage =
117 Complex current = voltage / impedance;
118 (**mIntfVoltage)(0, 0) = voltage.real();
119 (**mIntfCurrent)(0, 0) = current.real();
120
121 // Initialization of virtual node for the legacy series R-L realization.
123 mVirtualNodes[0]->setInitialVoltage(
125
126 SPDLOG_LOGGER_DEBUG(mSLog,
127 "\n--debug--"
128 "\n seriesRes: {:s}"
129 "\n seriesInd: {:s}"
130 "\n Impedance: {:s}",
133 Logger::complexToString(impedance));
134
135 SPDLOG_LOGGER_INFO(
136 mSLog,
137 "\n--- Initialization from powerflow ---"
138 "\nVoltage across: {:s}"
139 "\nCurrent: {:s}"
140 "\nTerminal 0 voltage: {:s}"
141 "\nTerminal 1 voltage: {:s}"
142 "\nVirtual Node 1 voltage: {:s}"
143 "\n--- Initialization from powerflow finished ---",
149 mSLog->flush();
150}
151
153 Attribute<Matrix>::Ptr leftVector) {
155 return;
156
158 mDirectInductorConductance = timeStep / (2. * **mSeriesInd);
163
164 const Real initialSeriesCurrent = (**mIntfCurrent)(0, 0);
165 const Real initialInductorVoltage =
166 (**mIntfVoltage)(0, 0) - **mSeriesRes * initialSeriesCurrent;
167 mDirectInductorHistory = mDirectInductorConductance * initialInductorVoltage +
168 initialSeriesCurrent;
171
172 if (**mParallelCap > 0.) {
173 const Real capacitance = **mParallelCap / 2.;
174 const Complex capacitorImpedance(0., -1. / (omega * capacitance));
175 const Complex initial0 = RMS3PH_TO_PEAK1PH * initialSingleVoltage(0);
176 const Complex initial1 = RMS3PH_TO_PEAK1PH * initialSingleVoltage(1);
177 const Real current0 = (initial0 / capacitorImpedance).real();
178 const Real current1 = (initial1 / capacitorImpedance).real();
180 current0 + mDirectCapacitorConductance * initial0.real();
182 current1 + mDirectCapacitorConductance * initial1.real();
183 }
184}
185
187 AttributeBase::List &prevStepDependencies,
188 AttributeBase::List &attributeDependencies,
189 AttributeBase::List &modifiedAttributes) {
190 prevStepDependencies.push_back(mIntfCurrent);
191 prevStepDependencies.push_back(mIntfVoltage);
192 if (!mUseSubcomponents) {
193 modifiedAttributes.push_back(mRightVector);
194 return;
195 }
196 modifiedAttributes.push_back(mRightVector);
197}
198
202
223
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 Attribute<Matrix>::Ptr &leftVector) {
254 mnaCompUpdateVoltage(**leftVector);
255 mnaCompUpdateCurrent(**leftVector);
256
257 if (!mUseSubcomponents) {
258 const Real seriesCurrent = (**mIntfCurrent)(0, 0);
259 const Real inductorVoltage =
260 (**mIntfVoltage)(0, 0) - **mSeriesRes * seriesCurrent;
262 mDirectInductorConductance * inductorVoltage + seriesCurrent;
265 const Real terminal0Voltage =
268 : 0.;
269 const Real terminal1Voltage =
272 : 0.;
274 2. * mDirectCapacitorConductance * terminal0Voltage -
277 2. * mDirectCapacitorConductance * terminal1Voltage -
279 }
280}
281
283 (**mIntfVoltage)(0, 0) = 0;
284 if (terminalNotGrounded(1))
285 (**mIntfVoltage)(0, 0) =
287 if (terminalNotGrounded(0))
288 (**mIntfVoltage)(0, 0) =
289 (**mIntfVoltage)(0, 0) -
291}
292
295 **mIntfCurrent = mSubSeriesInductor->intfCurrent();
296 else
297 (**mIntfCurrent)(0, 0) = mDirectSeriesConductance * (**mIntfVoltage)(0, 0) +
299}
300
301// #### Tear Methods ####
303 MNAInterface::List gndComponents;
304
306 return gndComponents;
307
308 gndComponents.push_back(mSubParallelResistor0);
309 gndComponents.push_back(mSubParallelResistor1);
310
311 if ((**mParallelCap) > 0) {
312 gndComponents.push_back(mSubParallelCapacitor0);
313 gndComponents.push_back(mSubParallelCapacitor1);
314 }
315
316 return gndComponents;
317}
318
320 if (!mUseSubcomponents) {
321 mDirectInductorConductance = timeStep / (2. * **mSeriesInd);
324 return;
325 }
326 mSubSeriesResistor->mnaTearSetIdx(mTearIdx);
327 mSubSeriesResistor->mnaTearInitialize(omega, timeStep);
328 mSubSeriesInductor->mnaTearSetIdx(mTearIdx);
329 mSubSeriesInductor->mnaTearInitialize(omega, timeStep);
330}
331
333 if (!mUseSubcomponents) {
336 return;
337 }
338 mSubSeriesResistor->mnaTearApplyMatrixStamp(tearMatrix);
339 mSubSeriesInductor->mnaTearApplyMatrixStamp(tearMatrix);
340}
341
343 if (!mUseSubcomponents) {
344 Math::addToVectorElement(voltageVector, mTearIdx,
347 return;
348 }
349 mSubSeriesInductor->mnaTearApplyVoltageStamp(voltageVector);
350}
351
353 if (!mUseSubcomponents) {
354 const Real realCurrent = current.real();
355 const Real inductorVoltage = voltage.real() - realCurrent * **mSeriesRes;
357 mDirectInductorConductance * inductorVoltage + realCurrent;
358 (**mIntfVoltage)(0, 0) = voltage.real();
359 (**mIntfCurrent)(0, 0) = realCurrent;
360 return;
361 }
362 mSubSeriesInductor->mnaTearPostStep(voltage - current * **mSeriesRes,
363 current);
364 (**mIntfCurrent) = mSubSeriesInductor->intfCurrent();
365}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
const Attribute< Real >::Ptr mParallelCap
Capacitance in parallel to the line [F].
const Attribute< Real >::Ptr mParallelCond
Conductance in parallel to the line [S].
const Attribute< Real >::Ptr mSeriesInd
Inductance along the line [H].
const Attribute< Real >::Ptr mSeriesRes
Resistance along the line [ohms].
void addMNASubComponent(typename SimPowerComp< Real >::Ptr subc, MNA_SUBCOMP_TASK_ORDER preStepOrder, MNA_SUBCOMP_TASK_ORDER postStepOrder, Bool contributeToRightVector)
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
CompositePowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
void mnaParentApplyRightSideVectorStamp(Matrix &rightVector) override
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
void mnaTearApplyMatrixStamp(SparseMatrixRow &tearMatrix) override
std::shared_ptr< Inductor > mSubSeriesInductor
MNAInterface::List mnaTearGroundComponents() override
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Updates internal voltage variable of the component.
std::shared_ptr< Resistor > mSubParallelResistor1
Parallel resistor submodel at Terminal 1.
void mnaTearPostStep(Complex voltage, Complex current) override
Bool mUseSubcomponents
Select the legacy component hierarchy or direct parent MNA stamps.
void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
void mnaTearApplyVoltageStamp(Matrix &voltageVector) override
void createSubComponents() override
Constructs and registers MNA subcomponents; idempotent.
Real mDirectInductorConductance
Companion-model coefficients used when subcomponents are disabled.
void mnaParentInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
std::shared_ptr< Capacitor > mSubParallelCapacitor0
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Updates internal current variable of the component.
std::shared_ptr< Resistor > mSubSeriesResistor
Series Resistor submodel.
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
SimPowerComp< Real >::Ptr clone(String copySuffix) override
DEPRECATED: Delete method.
std::shared_ptr< Capacitor > mSubParallelCapacitor1
void mnaParentApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
void mnaTearInitialize(Real omega, Real timeStep) override
std::shared_ptr< Resistor > mSubParallelResistor0
Parallel Resistor submodel at Terminal 0.
PiLine(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
void initializeParentFromNodesAndTerminals(Real frequency) override
Derives values from power flow data and pushes them to subcomponents.
void mnaParentPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
const Attribute< String >::Ptr mName
Human readable name.
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
static String matrixToString(const Matrix &mat)
Definition Logger.cpp:31
static String phasorToString(const Complex &num)
Definition Logger.cpp:57
std::vector< Ptr > List
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 addToVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0)
static void addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row, Matrix::Index column, Complex value, Int maxFreq=1, Int freqIdx=0)
std::vector< Ptr > List
Definition SimNode.h:35
static Ptr GND
Definition SimNode.h:37
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Real > >::Ptr mIntfCurrent
SimTerminal< Real >::List mTerminals
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.
Logger::Log mSLog
Component logger.
static std::shared_ptr< PiLine > make(Args &&...args)
Definition PtrFactory.h:19
#define PI
Definition Definitions.h:43
#define RMS3PH_TO_PEAK1PH
Definition Definitions.h:50
#define PEAK1PH_TO_RMS3PH
Definition Definitions.h:51
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
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:72