DPsim
Loading...
Searching...
No Matches
DP_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/DP/DP_Ph1_PiLine.h>
7
8using namespace CPS;
9
10DP::Ph1::PiLine::PiLine(String uid, String name, Logger::Level logLevel)
11 : Base::Ph1::PiLine(mAttributes),
12 CompositePowerComp<Complex>(uid, name, true, true, logLevel) {
13 setVirtualNodeNumber(1);
14 setTerminalNumber(2);
15
16 SPDLOG_LOGGER_INFO(mSLog, "Create {} {}", this->type(), name);
17 **mIntfVoltage = MatrixComp::Zero(1, 1);
18 **mIntfCurrent = MatrixComp::Zero(1, 1);
19}
20
22SimPowerComp<Complex>::Ptr DP::Ph1::PiLine::clone(String name) {
23 auto copy = PiLine::make(name, mLogLevel);
24 copy->setParameters(**mSeriesRes, **mSeriesInd, **mParallelCap,
26 return copy;
27}
28
30
31 // Static calculation
32 Real omega = 2. * PI * frequency;
33 Complex impedance = {**mSeriesRes, omega * **mSeriesInd};
34 (**mIntfVoltage)(0, 0) = initialSingleVoltage(1) - initialSingleVoltage(0);
35 (**mIntfCurrent)(0, 0) = (**mIntfVoltage)(0, 0) / impedance;
36
37 // Initialization of virtual node
38 mVirtualNodes[0]->setInitialVoltage(initialSingleVoltage(0) +
39 (**mIntfCurrent)(0, 0) * **mSeriesRes);
40
41 // Create series sub components
43 std::make_shared<DP::Ph1::Resistor>(**mName + "_res", mLogLevel);
44 mSubSeriesResistor->setParameters(**mSeriesRes);
45 mSubSeriesResistor->connect({mTerminals[0]->node(), mVirtualNodes[0]});
47 mSubSeriesResistor->initializeFromNodesAndTerminals(frequency);
49 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
50 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, false);
51
53 std::make_shared<DP::Ph1::Inductor>(**mName + "_ind", mLogLevel);
54 mSubSeriesInductor->setParameters(**mSeriesInd);
55 mSubSeriesInductor->connect({mVirtualNodes[0], mTerminals[1]->node()});
57 mSubSeriesInductor->initializeFromNodesAndTerminals(frequency);
59 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
60 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
61
62 // By default there is always a small conductance to ground to
63 // avoid problems with floating nodes.
64 Real defaultParallelCond = 1e-6;
66 (**mParallelCond > 0) ? **mParallelCond : defaultParallelCond;
67
68 // Create parallel sub components
70 std::make_shared<DP::Ph1::Resistor>(**mName + "_con0", mLogLevel);
71 mSubParallelResistor0->setParameters(2. / **mParallelCond);
72 mSubParallelResistor0->connect(
73 SimNode::List{SimNode::GND, mTerminals[0]->node()});
75 mSubParallelResistor0->initializeFromNodesAndTerminals(frequency);
77 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
78 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, false);
79
81 std::make_shared<DP::Ph1::Resistor>(**mName + "_con1", mLogLevel);
82 mSubParallelResistor1->setParameters(2. / **mParallelCond);
83 mSubParallelResistor1->connect(
84 SimNode::List{SimNode::GND, mTerminals[1]->node()});
86 mSubParallelResistor1->initializeFromNodesAndTerminals(frequency);
88 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
89 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, false);
90
91 if (**mParallelCap >= 0) {
92 mSubParallelCapacitor0 =
93 std::make_shared<DP::Ph1::Capacitor>(**mName + "_cap0", mLogLevel);
94 mSubParallelCapacitor0->setParameters(**mParallelCap / 2.);
95 mSubParallelCapacitor0->connect(
96 SimNode::List{SimNode::GND, mTerminals[0]->node()});
97 mSubParallelCapacitor0->initialize(mFrequencies);
98 mSubParallelCapacitor0->initializeFromNodesAndTerminals(frequency);
99 addMNASubComponent(mSubParallelCapacitor0,
100 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
101 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
102
104 std::make_shared<DP::Ph1::Capacitor>(**mName + "_cap1", mLogLevel);
105 mSubParallelCapacitor1->setParameters(**mParallelCap / 2.);
106 mSubParallelCapacitor1->connect(
107 SimNode::List{SimNode::GND, mTerminals[1]->node()});
109 mSubParallelCapacitor1->initializeFromNodesAndTerminals(frequency);
111 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
112 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
113 }
114
115 SPDLOG_LOGGER_INFO(
116 mSLog,
117 "\n--- Initialization from powerflow ---"
118 "\nVoltage across: {:s}"
119 "\nCurrent: {:s}"
120 "\nTerminal 0 voltage: {:s}"
121 "\nTerminal 1 voltage: {:s}"
122 "\nVirtual Node 1 voltage: {:s}"
123 "\n--- Initialization from powerflow finished ---",
124 Logger::phasorToString((**mIntfVoltage)(0, 0)),
125 Logger::phasorToString((**mIntfCurrent)(0, 0)),
126 Logger::phasorToString(initialSingleVoltage(0)),
127 Logger::phasorToString(initialSingleVoltage(1)),
128 Logger::phasorToString(mVirtualNodes[0]->initialSingleVoltage()));
129}
130
132 AttributeBase::List &prevStepDependencies,
133 AttributeBase::List &attributeDependencies,
134 AttributeBase::List &modifiedAttributes) {
135 // add pre-step dependencies of component itself
136 prevStepDependencies.push_back(mIntfCurrent);
137 prevStepDependencies.push_back(mIntfVoltage);
138 modifiedAttributes.push_back(mRightVector);
139}
140
141void DP::Ph1::PiLine::mnaParentPreStep(Real time, Int timeStepCount) {
142 // pre-step of component itself
144}
145
146void DP::Ph1::PiLine::mnaParentAddPostStepDependencies(
147 AttributeBase::List &prevStepDependencies,
148 AttributeBase::List &attributeDependencies,
149 AttributeBase::List &modifiedAttributes,
150 Attribute<Matrix>::Ptr &leftVector) {
151 // add post-step dependencies of component itself
152 attributeDependencies.push_back(leftVector);
153 modifiedAttributes.push_back(mIntfVoltage);
154 modifiedAttributes.push_back(mIntfCurrent);
155}
156
157void DP::Ph1::PiLine::mnaParentPostStep(Real time, Int timeStepCount,
158 Attribute<Matrix>::Ptr &leftVector) {
159 // post-step of component itself
160 this->mnaUpdateVoltage(**leftVector);
161 this->mnaUpdateCurrent(**leftVector);
162}
163
164void DP::Ph1::PiLine::mnaCompUpdateVoltage(const Matrix &leftVector) {
165 (**mIntfVoltage)(0, 0) = 0;
166 if (terminalNotGrounded(1))
167 (**mIntfVoltage)(0, 0) =
168 Math::complexFromVectorElement(leftVector, matrixNodeIndex(1));
169 if (terminalNotGrounded(0))
170 (**mIntfVoltage)(0, 0) =
171 (**mIntfVoltage)(0, 0) -
172 Math::complexFromVectorElement(leftVector, matrixNodeIndex(0));
173}
174
175void DP::Ph1::PiLine::mnaCompUpdateCurrent(const Matrix &leftVector) {
176 (**mIntfCurrent)(0, 0) = mSubSeriesInductor->intfCurrent()(0, 0);
177}
178
179// #### Tear Methods ####
180MNAInterface::List DP::Ph1::PiLine::mnaTearGroundComponents() {
181 MNAInterface::List gndComponents;
182
183 gndComponents.push_back(mSubParallelResistor0);
184 gndComponents.push_back(mSubParallelResistor1);
185
186 if (**mParallelCap >= 0) {
187 gndComponents.push_back(mSubParallelCapacitor0);
188 gndComponents.push_back(mSubParallelCapacitor1);
189 }
190
191 return gndComponents;
192}
193
194void DP::Ph1::PiLine::mnaTearInitialize(Real omega, Real timeStep) {
195 mSubSeriesResistor->mnaTearSetIdx(mTearIdx);
196 mSubSeriesResistor->mnaTearInitialize(omega, timeStep);
197 mSubSeriesInductor->mnaTearSetIdx(mTearIdx);
198 mSubSeriesInductor->mnaTearInitialize(omega, timeStep);
199}
200
201void DP::Ph1::PiLine::mnaTearApplyMatrixStamp(SparseMatrixRow &tearMatrix) {
202 mSubSeriesResistor->mnaTearApplyMatrixStamp(tearMatrix);
203 mSubSeriesInductor->mnaTearApplyMatrixStamp(tearMatrix);
204}
205
206void DP::Ph1::PiLine::mnaTearApplyVoltageStamp(Matrix &voltageVector) {
207 mSubSeriesInductor->mnaTearApplyVoltageStamp(voltageVector);
208}
209
210void DP::Ph1::PiLine::mnaTearPostStep(Complex voltage, Complex current) {
211 mSubSeriesInductor->mnaTearPostStep(voltage - current * **mSeriesRes,
212 current);
213 (**mIntfCurrent)(0, 0) = mSubSeriesInductor->intfCurrent()(0, 0);
214}
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< Complex >::Ptr subc, MNA_SUBCOMP_TASK_ORDER preStepOrder, MNA_SUBCOMP_TASK_ORDER postStepOrder, Bool contributeToRightVector)
CompositePowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
void mnaParentPreStep(Real time, Int timeStepCount) override
MNA pre and post step operations.
std::shared_ptr< Capacitor > mSubParallelCapacitor1
Parallel capacitor submodel at Terminal 1.
SimPowerComp< Complex >::Ptr clone(String copySuffix) override
DEPRECATED: Remove method.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Updates internal current variable of the component.
std::shared_ptr< Resistor > mSubParallelResistor1
Parallel resistor submodel at Terminal 1.
std::shared_ptr< Resistor > mSubSeriesResistor
Series Resistor submodel.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
std::shared_ptr< Inductor > mSubSeriesInductor
Series Inductance submodel.
PiLine(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
std::shared_ptr< Resistor > mSubParallelResistor0
Parallel Resistor submodel at Terminal 0.
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
add MNA pre and post step dependencies
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Updates internal voltage variable of the component.
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
void mnaApplyRightSideVectorStamp(Matrix &rightVector) final
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
SimTerminal< Complex >::List mTerminals
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
SimNode< Complex >::List mVirtualNodes
Logger::Level mLogLevel
Component logger control for internal variables.
Logger::Log mSLog
Component logger.