DPsim
Loading...
Searching...
No Matches
EMT_Ph1_PiLine.cpp
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
6#include <dpsim-models/EMT/EMT_Ph1_PiLine.h>
7
8using namespace CPS;
9
10EMT::Ph1::PiLine::PiLine(String uid, String name, Logger::Level logLevel)
11 : Base::Ph1::PiLine(mAttributes),
12 CompositePowerComp<Real>(uid, name, true, true, logLevel) {
13 setVirtualNodeNumber(1);
14 setTerminalNumber(2);
15
16 SPDLOG_LOGGER_INFO(mSLog, "Create {} {}", this->type(), name);
17 **mIntfVoltage = Matrix::Zero(1, 1);
18 **mIntfCurrent = Matrix::Zero(1, 1);
19
20 mSLog->flush();
21}
22
24SimPowerComp<Real>::Ptr EMT::Ph1::PiLine::clone(String name) {
25 auto copy = PiLine::make(name, mLogLevel);
26 copy->setParameters(**mSeriesRes, **mSeriesInd, **mParallelCap,
28 return copy;
29}
30
32
33 // By default there is always a small conductance to ground to
34 // avoid problems with floating nodes.
35
36 Real defaultParallelCond = 1e-6;
38 (**mParallelCond > 0) ? **mParallelCond : defaultParallelCond;
39
40 // Static calculation
41 Real omega = 2. * PI * frequency;
42 Complex impedance = {**mSeriesRes, omega * **mSeriesInd};
43 Complex voltage =
44 RMS3PH_TO_PEAK1PH * (initialSingleVoltage(1) - initialSingleVoltage(0));
45 (**mIntfVoltage)(0, 0) = voltage.real();
46 (**mIntfCurrent)(0, 0) = (voltage / impedance).real();
47
48 // Initialization of virtual node
49 mVirtualNodes[0]->setInitialVoltage(initialSingleVoltage(0) +
50 (**mIntfCurrent)(0, 0) * **mSeriesRes);
51
52 // Create series sub components
54 std::make_shared<EMT::Ph1::Resistor>(**mName + "_res", mLogLevel);
55 mSubSeriesResistor->setParameters(**mSeriesRes);
56 mSubSeriesResistor->connect({mTerminals[0]->node(), mVirtualNodes[0]});
58 mSubSeriesResistor->initializeFromNodesAndTerminals(frequency);
60 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
61 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, false);
62
64 std::make_shared<EMT::Ph1::Inductor>(**mName + "_ind", mLogLevel);
65 mSubSeriesInductor->setParameters(**mSeriesInd);
66 mSubSeriesInductor->connect({mVirtualNodes[0], mTerminals[1]->node()});
68 mSubSeriesInductor->initializeFromNodesAndTerminals(frequency);
70 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
71 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
72
73 // Create parallel sub components
75 std::make_shared<EMT::Ph1::Resistor>(**mName + "_con0", mLogLevel);
76 mSubParallelResistor0->setParameters(2. / (**mParallelCond));
77 mSubParallelResistor0->connect(
78 SimNode::List{SimNode::GND, mTerminals[0]->node()});
80 mSubParallelResistor0->initializeFromNodesAndTerminals(frequency);
82 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
83 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, false);
84
86 std::make_shared<EMT::Ph1::Resistor>(**mName + "_con1", mLogLevel);
87 mSubParallelResistor1->setParameters(2. / (**mParallelCond));
88 mSubParallelResistor1->connect(
89 SimNode::List{SimNode::GND, mTerminals[1]->node()});
91 mSubParallelResistor1->initializeFromNodesAndTerminals(frequency);
93 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
94 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, false);
95
96 if ((**mParallelCap) > 0) {
97 mSubParallelCapacitor0 =
98 std::make_shared<EMT::Ph1::Capacitor>(**mName + "_cap0", mLogLevel);
99 mSubParallelCapacitor0->setParameters(**mParallelCap / 2.);
100 mSubParallelCapacitor0->connect(
101 SimNode::List{SimNode::GND, mTerminals[0]->node()});
102 mSubParallelCapacitor0->initialize(mFrequencies);
103 mSubParallelCapacitor0->initializeFromNodesAndTerminals(frequency);
104 addMNASubComponent(mSubParallelCapacitor0,
105 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
106 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
107
108 mSubParallelCapacitor1 =
109 std::make_shared<EMT::Ph1::Capacitor>(**mName + "_cap1", mLogLevel);
110 mSubParallelCapacitor1->setParameters(**mParallelCap / 2.);
111 mSubParallelCapacitor1->connect(
112 SimNode::List{SimNode::GND, mTerminals[1]->node()});
113 mSubParallelCapacitor1->initialize(mFrequencies);
114 mSubParallelCapacitor1->initializeFromNodesAndTerminals(frequency);
115 addMNASubComponent(mSubParallelCapacitor1,
116 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
117 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
118 }
119
120 SPDLOG_LOGGER_DEBUG(mSLog,
121 "\n--debug--"
122 "\n seriesRes: {:s}"
123 "\n seriesInd: {:s}"
124 "\n Impedance: {:s}",
125 Logger::matrixToString(**mSeriesRes),
126 Logger::matrixToString(**mSeriesInd),
127 Logger::complexToString(impedance));
128
129 SPDLOG_LOGGER_INFO(
130 mSLog,
131 "\n--- Initialization from powerflow ---"
132 "\nVoltage across: {:s}"
133 "\nCurrent: {:s}"
134 "\nTerminal 0 voltage: {:s}"
135 "\nTerminal 1 voltage: {:s}"
136 "\nVirtual Node 1 voltage: {:s}"
137 "\n--- Initialization from powerflow finished ---",
138 Logger::matrixToString(**mIntfVoltage),
139 Logger::matrixToString(**mIntfCurrent),
140 Logger::phasorToString(RMS3PH_TO_PEAK1PH * initialSingleVoltage(0)),
141 Logger::phasorToString(RMS3PH_TO_PEAK1PH * initialSingleVoltage(1)),
142 Logger::phasorToString(mVirtualNodes[0]->initialSingleVoltage()));
143 mSLog->flush();
144}
145
147 AttributeBase::List &prevStepDependencies,
148 AttributeBase::List &attributeDependencies,
149 AttributeBase::List &modifiedAttributes) {
150 prevStepDependencies.push_back(mIntfCurrent);
151 prevStepDependencies.push_back(mIntfVoltage);
152 modifiedAttributes.push_back(mRightVector);
153}
154
158
160 AttributeBase::List &prevStepDependencies,
161 AttributeBase::List &attributeDependencies,
162 AttributeBase::List &modifiedAttributes,
163 Attribute<Matrix>::Ptr &leftVector) {
164 attributeDependencies.push_back(leftVector);
165 modifiedAttributes.push_back(mIntfVoltage);
166 modifiedAttributes.push_back(mIntfCurrent);
167}
168
169void EMT::Ph1::PiLine::mnaParentPostStep(Real time, Int timeStepCount,
170 Attribute<Matrix>::Ptr &leftVector) {
171 mnaCompUpdateVoltage(**leftVector);
172 mnaCompUpdateCurrent(**leftVector);
173}
174
175void EMT::Ph1::PiLine::mnaCompUpdateVoltage(const Matrix &leftVector) {
176 (**mIntfVoltage)(0, 0) = 0;
177 if (terminalNotGrounded(1))
178 (**mIntfVoltage)(0, 0) =
179 Math::realFromVectorElement(leftVector, matrixNodeIndex(1));
180 if (terminalNotGrounded(0))
181 (**mIntfVoltage)(0, 0) =
182 (**mIntfVoltage)(0, 0) -
183 Math::realFromVectorElement(leftVector, matrixNodeIndex(0));
184}
185
186void EMT::Ph1::PiLine::mnaCompUpdateCurrent(const Matrix &leftVector) {
187 **mIntfCurrent = mSubSeriesInductor->intfCurrent();
188}
189
190// #### Tear Methods ####
191MNAInterface::List EMT::Ph1::PiLine::mnaTearGroundComponents() {
192 MNAInterface::List gndComponents;
193
194 gndComponents.push_back(mSubParallelResistor0);
195 gndComponents.push_back(mSubParallelResistor1);
196
197 if ((**mParallelCap) > 0) {
198 gndComponents.push_back(mSubParallelCapacitor0);
199 gndComponents.push_back(mSubParallelCapacitor1);
200 }
201
202 return gndComponents;
203}
204
205void EMT::Ph1::PiLine::mnaTearInitialize(Real omega, Real timeStep) {
206 mSubSeriesResistor->mnaTearSetIdx(mTearIdx);
207 mSubSeriesResistor->mnaTearInitialize(omega, timeStep);
208 mSubSeriesInductor->mnaTearSetIdx(mTearIdx);
209 mSubSeriesInductor->mnaTearInitialize(omega, timeStep);
210}
211
212void EMT::Ph1::PiLine::mnaTearApplyMatrixStamp(SparseMatrixRow &tearMatrix) {
213 mSubSeriesResistor->mnaTearApplyMatrixStamp(tearMatrix);
214 mSubSeriesInductor->mnaTearApplyMatrixStamp(tearMatrix);
215}
216
217void EMT::Ph1::PiLine::mnaTearApplyVoltageStamp(Matrix &voltageVector) {
218 mSubSeriesInductor->mnaTearApplyVoltageStamp(voltageVector);
219}
220
221void EMT::Ph1::PiLine::mnaTearPostStep(Complex voltage, Complex current) {
222 mSubSeriesInductor->mnaTearPostStep(voltage - current * **mSeriesRes,
223 current);
224 (**mIntfCurrent) = mSubSeriesInductor->intfCurrent();
225}
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 mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
std::shared_ptr< Inductor > mSubSeriesInductor
Series Inductance submodel.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
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 mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
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< 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 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.
Attribute< Matrix >::Ptr mRightVector
const Attribute< MatrixVar< Real > >::Ptr mIntfCurrent
SimTerminal< Real >::List mTerminals
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
SimNode< Real >::List mVirtualNodes
Logger::Level mLogLevel
Component logger control for internal variables.
Logger::Log mSLog
Component logger.