DPsim
Loading...
Searching...
No Matches
SP_Ph1_PiLine.cpp
1/* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
2 * EONERC, RWTH Aachen University
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7 *********************************************************************************/
8
9#include <dpsim-models/SP/SP_Ph1_PiLine.h>
10
11using namespace CPS;
12
13SP::Ph1::PiLine::PiLine(String uid, String name, Logger::Level logLevel)
14 : Base::Ph1::PiLine(mAttributes),
15 CompositePowerComp<Complex>(uid, name, false, true, logLevel),
16 mCurrent(mAttributes->create<MatrixComp>("current_vector")),
17 mActivePowerBranch(mAttributes->create<Matrix>("p_branch_vector")),
18 mReactivePowerBranch(mAttributes->create<Matrix>("q_branch_vector")),
19 mActivePowerInjection(mAttributes->create<Real>("p_inj")),
20 mReactivePowerInjection(mAttributes->create<Real>("q_inj")) {
21
22 SPDLOG_LOGGER_INFO(mSLog, "Create {} {}", this->type(), name);
23 mSLog->flush();
24
25 setVirtualNodeNumber(1);
26 setTerminalNumber(2);
27 **mIntfVoltage = MatrixComp::Zero(1, 1);
28 **mIntfCurrent = MatrixComp::Zero(1, 1);
29 **mCurrent = MatrixComp::Zero(2, 1);
30 **mActivePowerBranch = Matrix::Zero(2, 1);
31 **mReactivePowerBranch = Matrix::Zero(2, 1);
32}
33
34void SP::Ph1::PiLine::setParameters(Real resistance, Real inductance,
35 Real capacitance, Real conductance) {
36
37 **mSeriesRes = resistance;
38 **mSeriesInd = inductance;
39 SPDLOG_LOGGER_INFO(mSLog, "Resistance={} [Ohm] Inductance={} [H]",
40 **mSeriesRes, **mSeriesInd);
41
42 if (capacitance > 0) {
43 **mParallelCap = capacitance;
44 } else {
45 **mParallelCap = 1e-12;
46 SPDLOG_LOGGER_WARN(
47 mSLog, "Zero value for Capacitance, setting default value of C={} [F]",
48 **mParallelCap);
49 }
50 if (conductance > 0) {
51 **mParallelCond = conductance;
52 } else {
53 if (mBehaviour == Behaviour::Initialization)
54 **mParallelCond =
55 (conductance >= 0)
56 ? conductance
57 : 1e-6; // init mode for initFromPowerFlow of mna system components
58 else
59 **mParallelCond = (conductance > 0) ? conductance : 1e-6;
60 SPDLOG_LOGGER_WARN(
61 mSLog, "Zero value for Conductance, setting default value of G={} [S]",
62 **mParallelCond);
63 }
64 SPDLOG_LOGGER_INFO(mSLog, "Capacitance={} [F] Conductance={} [S]",
65 **mParallelCap, **mParallelCond);
66 mSLog->flush();
67 mParametersSet = true;
68}
69
71SimPowerComp<Complex>::Ptr SP::Ph1::PiLine::clone(String name) {
72 auto copy = PiLine::make(name, mLogLevel);
73 copy->setParameters(**mSeriesRes, **mSeriesInd, **mParallelCap,
75 return copy;
76}
77
78// #### Powerflow section ####
79Real SP::Ph1::PiLine::getBaseVoltage() const { return mBaseVoltage; }
80
81void SP::Ph1::PiLine::setBaseVoltage(Real baseVoltage) {
82 mBaseVoltage = baseVoltage;
83}
84
86 Real baseOmega) {
87 SPDLOG_LOGGER_INFO(mSLog, "#### Calculate Per Unit Parameters for {}",
88 **mName);
89 mBaseApparentPower = baseApparentPower;
90 mBaseOmega = baseOmega;
91 SPDLOG_LOGGER_INFO(mSLog, "Base Power={} [VA] Base Omega={} [1/s]",
92 baseApparentPower, baseOmega);
93
94 mBaseImpedance = (mBaseVoltage * mBaseVoltage) / mBaseApparentPower;
98 mBaseCurrent = baseApparentPower /
99 (mBaseVoltage *
100 sqrt(3)); // I_base=(S_threephase/3)/(V_line_to_line/sqrt(3))
101 SPDLOG_LOGGER_INFO(mSLog, "Base Voltage={} [V] Base Impedance={} [Ohm]",
102 mBaseVoltage, mBaseImpedance);
103
108
109 SPDLOG_LOGGER_INFO(mSLog, "Resistance={} [pu] Reactance={} [pu]",
111 SPDLOG_LOGGER_INFO(mSLog, "Susceptance={} [pu] Conductance={} [pu]",
113 mSLog->flush();
114}
115
117 int bus1 = this->matrixNodeIndex(0);
118 int bus2 = this->matrixNodeIndex(1);
119
120 //create the element admittance matrix
121 Complex y =
122 Complex(1, 0) / Complex(mSeriesResPerUnit, 1. * mSeriesIndPerUnit);
123 Complex ys =
124 Complex(mParallelCondPerUnit, 1. * mParallelCapPerUnit) / Complex(2, 0);
125
126 //Fill the internal matrix
127 mY_element = MatrixComp(2, 2);
128 mY_element(0, 0) = y + ys;
129 mY_element(0, 1) = -y;
130 mY_element(1, 0) = -y;
131 mY_element(1, 1) = y + ys;
132
133 //check for inf or nan
134 for (int i = 0; i < 2; i++)
135 for (int j = 0; j < 2; j++)
136 if (std::isinf(mY_element.coeff(i, j).real()) ||
137 std::isinf(mY_element.coeff(i, j).imag())) {
138 std::cout << mY_element << std::endl;
139 std::stringstream ss;
140 ss << "Line>>" << this->name()
141 << ": infinite or nan values in the element Y at: " << i << "," << j;
142 throw std::invalid_argument(ss.str());
143 std::cout << "Line>>" << this->name()
144 << ": infinite or nan values in the element Y at: " << i
145 << "," << j << std::endl;
146 }
147
148 //set the circuit matrix values
149 Y.coeffRef(bus1, bus1) += mY_element.coeff(0, 0);
150 Y.coeffRef(bus1, bus2) += mY_element.coeff(0, 1);
151 Y.coeffRef(bus2, bus2) += mY_element.coeff(1, 1);
152 Y.coeffRef(bus2, bus1) += mY_element.coeff(1, 0);
153
154 SPDLOG_LOGGER_INFO(mSLog, "#### PF Y matrix stamping #### ");
155 SPDLOG_LOGGER_INFO(mSLog, "{}", mY_element);
156 mSLog->flush();
157}
158
159void SP::Ph1::PiLine::updateBranchFlow(VectorComp &current,
160 VectorComp &powerflow) {
161 **mCurrent = current * mBaseCurrent;
162 **mActivePowerBranch = powerflow.real() * mBaseApparentPower;
163 **mReactivePowerBranch = powerflow.imag() * mBaseApparentPower;
164}
165
166void SP::Ph1::PiLine::storeNodalInjection(Complex powerInjection) {
167 **mActivePowerInjection = std::real(powerInjection) * mBaseApparentPower;
168 **mReactivePowerInjection = std::imag(powerInjection) * mBaseApparentPower;
169}
170
171MatrixComp SP::Ph1::PiLine::Y_element() { return mY_element; }
172
174
175 // By default there is always a small conductance to ground to
176 // avoid problems with floating nodes.
177 **mParallelCond = (**mParallelCond >= 0) ? **mParallelCond : 1e-6;
178
179 // Static calculation
180 Real omega = 2. * PI * frequency;
181 Complex impedance = {**mSeriesRes, omega * **mSeriesInd};
182 (**mIntfVoltage)(0, 0) = initialSingleVoltage(1) - initialSingleVoltage(0);
183 (**mIntfCurrent)(0, 0) = (**mIntfVoltage)(0, 0) / impedance;
184
185 // Initialization of virtual node
186 mVirtualNodes[0]->setInitialVoltage(initialSingleVoltage(0) +
187 (**mIntfCurrent)(0, 0) * **mSeriesRes);
188
189 // Create series sub components
191 std::make_shared<SP::Ph1::Resistor>(**mName + "_res", mLogLevel);
192 mSubSeriesResistor->setParameters(**mSeriesRes);
193 mSubSeriesResistor->connect({mTerminals[0]->node(), mVirtualNodes[0]});
194 mSubSeriesResistor->initialize(mFrequencies);
195 mSubSeriesResistor->initializeFromNodesAndTerminals(frequency);
196 addMNASubComponent(mSubSeriesResistor, MNA_SUBCOMP_TASK_ORDER::NO_TASK,
197 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, false);
198
200 std::make_shared<SP::Ph1::Inductor>(**mName + "_ind", mLogLevel);
201 mSubSeriesInductor->setParameters(**mSeriesInd);
202 mSubSeriesInductor->connect({mVirtualNodes[0], mTerminals[1]->node()});
203 mSubSeriesInductor->initialize(mFrequencies);
204 mSubSeriesInductor->initializeFromNodesAndTerminals(frequency);
205 addMNASubComponent(mSubSeriesInductor, MNA_SUBCOMP_TASK_ORDER::NO_TASK,
206 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
207
208 // Create parallel sub components
209 if (**mParallelCond >= 0) {
211 std::make_shared<SP::Ph1::Resistor>(**mName + "_con0", mLogLevel);
212 mSubParallelResistor0->setParameters(2. / **mParallelCond);
213 mSubParallelResistor0->connect(
214 SimNode::List{SimNode::GND, mTerminals[0]->node()});
216 mSubParallelResistor0->initializeFromNodesAndTerminals(frequency);
217 addMNASubComponent(mSubParallelResistor0, MNA_SUBCOMP_TASK_ORDER::NO_TASK,
218 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, false);
219
221 std::make_shared<SP::Ph1::Resistor>(**mName + "_con1", mLogLevel);
222 mSubParallelResistor1->setParameters(2. / **mParallelCond);
223 mSubParallelResistor1->connect(
224 SimNode::List{SimNode::GND, mTerminals[1]->node()});
226 mSubParallelResistor1->initializeFromNodesAndTerminals(frequency);
227 addMNASubComponent(mSubParallelResistor1, MNA_SUBCOMP_TASK_ORDER::NO_TASK,
228 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, false);
229 }
230
231 if (**mParallelCap >= 0) {
232 mSubParallelCapacitor0 =
233 std::make_shared<SP::Ph1::Capacitor>(**mName + "_cap0", mLogLevel);
234 mSubParallelCapacitor0->setParameters(**mParallelCap / 2.);
235 mSubParallelCapacitor0->connect(
236 SimNode::List{SimNode::GND, mTerminals[0]->node()});
237 mSubParallelCapacitor0->initialize(mFrequencies);
238 mSubParallelCapacitor0->initializeFromNodesAndTerminals(frequency);
239 addMNASubComponent(mSubParallelCapacitor0, MNA_SUBCOMP_TASK_ORDER::NO_TASK,
240 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
241
243 std::make_shared<SP::Ph1::Capacitor>(**mName + "_cap1", mLogLevel);
244 mSubParallelCapacitor1->setParameters(**mParallelCap / 2.);
245 mSubParallelCapacitor1->connect(
246 SimNode::List{SimNode::GND, mTerminals[1]->node()});
248 mSubParallelCapacitor1->initializeFromNodesAndTerminals(frequency);
249 addMNASubComponent(mSubParallelCapacitor1, MNA_SUBCOMP_TASK_ORDER::NO_TASK,
250 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
251 }
252
253 SPDLOG_LOGGER_INFO(
254 mSLog,
255 "\n--- Initialization from powerflow ---"
256 "\nVoltage across: {:s}"
257 "\nCurrent: {:s}"
258 "\nTerminal 0 voltage: {:s}"
259 "\nTerminal 1 voltage: {:s}"
260 "\nVirtual Node 1 voltage: {:s}"
261 "\n--- Initialization from powerflow finished ---",
262 Logger::phasorToString((**mIntfVoltage)(0, 0)),
263 Logger::phasorToString((**mIntfCurrent)(0, 0)),
264 Logger::phasorToString(initialSingleVoltage(0)),
265 Logger::phasorToString(initialSingleVoltage(1)),
266 Logger::phasorToString(mVirtualNodes[0]->initialSingleVoltage()));
267}
268
270 AttributeBase::List &prevStepDependencies,
271 AttributeBase::List &attributeDependencies,
272 AttributeBase::List &modifiedAttributes,
273 Attribute<Matrix>::Ptr &leftVector) {
274 attributeDependencies.push_back(leftVector);
275 modifiedAttributes.push_back(mIntfVoltage);
276 modifiedAttributes.push_back(mIntfCurrent);
277}
278
279void SP::Ph1::PiLine::mnaParentPostStep(Real time, Int timeStepCount,
280 Attribute<Matrix>::Ptr &leftVector) {
281 this->mnaUpdateVoltage(**leftVector);
282 this->mnaUpdateCurrent(**leftVector);
283}
284
285void SP::Ph1::PiLine::mnaCompUpdateVoltage(const Matrix &leftVector) {
286 (**mIntfVoltage)(0, 0) = 0;
287 if (terminalNotGrounded(1))
288 (**mIntfVoltage)(0, 0) =
289 Math::complexFromVectorElement(leftVector, matrixNodeIndex(1));
290 if (terminalNotGrounded(0))
291 (**mIntfVoltage)(0, 0) =
292 (**mIntfVoltage)(0, 0) -
293 Math::complexFromVectorElement(leftVector, matrixNodeIndex(0));
294}
295
296void SP::Ph1::PiLine::mnaCompUpdateCurrent(const Matrix &leftVector) {
297 (**mIntfCurrent)(0, 0) = mSubSeriesInductor->intfCurrent()(0, 0);
298}
299
300MNAInterface::List SP::Ph1::PiLine::mnaTearGroundComponents() {
301 MNAInterface::List gndComponents;
302
303 gndComponents.push_back(mSubParallelResistor0);
304 gndComponents.push_back(mSubParallelResistor1);
305
306 if (**mParallelCap >= 0) {
307 gndComponents.push_back(mSubParallelCapacitor0);
308 gndComponents.push_back(mSubParallelCapacitor1);
309 }
310
311 return gndComponents;
312}
313
314void SP::Ph1::PiLine::mnaTearInitialize(Real omega, Real timeStep) {
315 mSubSeriesResistor->mnaTearSetIdx(mTearIdx);
316 mSubSeriesResistor->mnaTearInitialize(omega, timeStep);
317 mSubSeriesInductor->mnaTearSetIdx(mTearIdx);
318 mSubSeriesInductor->mnaTearInitialize(omega, timeStep);
319}
320
321void SP::Ph1::PiLine::mnaTearApplyMatrixStamp(SparseMatrixRow &tearMatrix) {
322 mSubSeriesResistor->mnaTearApplyMatrixStamp(tearMatrix);
323 mSubSeriesInductor->mnaTearApplyMatrixStamp(tearMatrix);
324}
325
326void SP::Ph1::PiLine::mnaTearApplyVoltageStamp(Matrix &voltageVector) {
327 mSubSeriesInductor->mnaTearApplyVoltageStamp(voltageVector);
328}
329
330void SP::Ph1::PiLine::mnaTearPostStep(Complex voltage, Complex current) {
331 mSubSeriesInductor->mnaTearPostStep(voltage - current * **mSeriesRes,
332 current);
333}
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)
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.
void mnaUpdateCurrent(const Matrix &leftVector) final
void mnaUpdateVoltage(const Matrix &leftVector) final
void setBaseVoltage(Real baseVoltage)
Set base voltage.
Real mBaseCapacitance
base capacitance [F]
void calculatePerUnitParameters(Real baseApparentPower, Real baseOmega)
Calculates component's parameters in specified per-unit system.
std::shared_ptr< Resistor > mSubSeriesResistor
Series Resistor submodel.
const Attribute< Matrix >::Ptr mActivePowerBranch
branch active powerflow [W], coef(0) has data from node 0, coef(1) from node 1.
Real mSeriesIndPerUnit
Inductance of the line in [pu].
const Attribute< Real >::Ptr mReactivePowerInjection
nodal reactive power injection
const Attribute< MatrixComp >::Ptr mCurrent
branch Current flow [A], coef(0) has data from node 0, coef(1) from node 1.
Real mBaseAdmittance
base admittance [S]
Real mBaseInductance
base inductance [H]
Real mParallelCapPerUnit
Capacitance of the line in [pu].
std::shared_ptr< Resistor > mSubParallelResistor1
Parallel resistor submodel at Terminal 1.
const Attribute< Matrix >::Ptr mReactivePowerBranch
branch reactive powerflow [Var], coef(0) has data from node 0, coef(1) from node 1.
void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
add MNA post-step dependencies
void updateBranchFlow(VectorComp &current, VectorComp &powerflow)
updates branch current and power flow, input pu value, update with real value
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post-step operations.
MatrixComp Y_element()
get admittance matrix
Real mSeriesResPerUnit
resistance in [pu]
std::shared_ptr< Resistor > mSubParallelResistor0
Parallel Resistor submodel at Terminal 0.
void pfApplyAdmittanceMatrixStamp(SparseMatrixCompRow &Y) override
Stamps admittance matrix.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Updates internal current variable of the component.
std::shared_ptr< Capacitor > mSubParallelCapacitor1
Parallel capacitor submodel at Terminal 1.
Real getBaseVoltage() const
Get base voltage.
PiLine(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
SimPowerComp< Complex >::Ptr clone(String copySuffix) override
DEPRECATED: Delete method.
std::shared_ptr< Inductor > mSubSeriesInductor
Series Inductance submodel.
const Attribute< Real >::Ptr mActivePowerInjection
nodal active power injection
Real mParallelCondPerUnit
Conductance of the line in [pu].
Real mBaseApparentPower
base apparent power [VA]
Real mBaseImpedance
base impedance [Ohm]
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Updates internal voltage variable of the component.
Real mBaseOmega
base omega [1/s]
void storeNodalInjection(Complex powerInjection)
stores nodal injection power in this line object
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.