DPsim
Loading...
Searching...
No Matches
SP_Ph1_RXLine.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_RXLine.h"
10#include "dpsim-models/MathUtils.h"
11
12using namespace CPS;
13
14SP::Ph1::RXLine::RXLine(String uid, String name, Real baseVoltage,
15 Real resistance, Real inductance,
16 Logger::Level logLevel)
17 : Base::Ph1::PiLine(mAttributes),
18 CompositePowerComp<Complex>(uid, name, true, true, logLevel),
19 mInductance(mAttributes->create<Real>("L_series")),
20 mActivePowerInjection(mAttributes->create<Real>("p_inj")),
21 mReactivePowerInjection(mAttributes->create<Real>("q_inj")),
22 mCurrent(mAttributes->create<MatrixComp>("current_vector")),
23 mActivePowerBranch(mAttributes->create<Matrix>("p_branch_vector")),
24 mReactivePowerBranch(mAttributes->create<Matrix>("q_branch_vector")) {
25
26 setTerminalNumber(2);
27
28 **mSeriesRes = resistance;
29 **mInductance = inductance;
30
31 **mCurrent = MatrixComp::Zero(2, 1);
32 **mActivePowerBranch = Matrix::Zero(2, 1);
33 **mReactivePowerBranch = Matrix::Zero(2, 1);
34}
35
36SP::Ph1::RXLine::RXLine(String uid, String name, Logger::Level logLevel)
37 : Base::Ph1::PiLine(mAttributes),
38 CompositePowerComp<Complex>(uid, name, true, true, logLevel),
39 mInductance(mAttributes->create<Real>("L_series")),
40 mActivePowerInjection(mAttributes->create<Real>("p_inj")),
41 mReactivePowerInjection(mAttributes->create<Real>("q_inj")),
42 mCurrent(mAttributes->create<MatrixComp>("current_vector")),
43 mActivePowerBranch(mAttributes->create<Matrix>("p_branch_vector")),
44 mReactivePowerBranch(mAttributes->create<Matrix>("q_branch_vector")) {
45
46 setVirtualNodeNumber(1);
47 setTerminalNumber(2);
48 **mIntfVoltage = MatrixComp::Zero(1, 1);
49 **mIntfCurrent = MatrixComp::Zero(1, 1);
50}
51
52void SP::Ph1::RXLine::setPerUnitSystem(Real baseApparentPower, Real baseOmega) {
53 mBaseApparentPower = baseApparentPower;
54 mBaseOmega = baseOmega;
55 mBaseImpedance = (mBaseVoltage * mBaseVoltage) / mBaseApparentPower;
58 mBaseCurrent = baseApparentPower / (mBaseVoltage * sqrt(3));
59
62}
63
64Real SP::Ph1::RXLine::getBaseVoltage() const { return mBaseVoltage; }
65
68 int bus1 = this->matrixNodeIndex(0);
69 int bus2 = this->matrixNodeIndex(1);
70
71 //create the element admittance matrix
72 Complex y =
73 Complex(1, 0) / Complex(mSeriesResPerUnit, 1. * mSeriesIndPerUnit);
74
75 //Fill the internal matrix
76 mY_element = MatrixComp(2, 2);
77 mY_element(0, 0) = y;
78 mY_element(0, 1) = -y;
79 mY_element(1, 0) = -y;
80 mY_element(1, 1) = y;
81
82 //check for inf or nan
83 for (int i = 0; i < 2; i++)
84 for (int j = 0; j < 2; j++)
85 if (!Math::isFinite(mY_element.coeff(i, j))) {
86 SPDLOG_LOGGER_ERROR(
87 mSLog,
88 "Line {}: non-finite per-unit admittance {} in element Y({},{})",
89 this->name(), Logger::complexToString(mY_element.coeff(i, j)), i,
90 j);
92 }
93
94 //set the circuit matrix values
95 Y.coeffRef(bus1, bus1) += mY_element.coeff(0, 0);
96 Y.coeffRef(bus1, bus2) += mY_element.coeff(0, 1);
97 Y.coeffRef(bus2, bus1) += mY_element.coeff(1, 0);
98 Y.coeffRef(bus2, bus2) += mY_element.coeff(1, 1);
99}
100
101void SP::Ph1::RXLine::updateBranchFlow(VectorComp &current,
102 VectorComp &powerflow) {
103 **mCurrent = current * mBaseCurrent;
104 **mActivePowerBranch = powerflow.real() * mBaseApparentPower;
105 **mReactivePowerBranch = powerflow.imag() * mBaseApparentPower;
106}
107
108void SP::Ph1::RXLine::storeNodalInjection(Complex powerInjection) {
109 **mActivePowerInjection = std::real(powerInjection) * mBaseApparentPower;
110 **mReactivePowerInjection = std::imag(powerInjection) * mBaseApparentPower;
111}
112
113MatrixComp SP::Ph1::RXLine::Y_element() { return mY_element; }
114
116SimPowerComp<Complex>::Ptr SP::Ph1::RXLine::clone(String name) {
117 auto copy = RXLine::make(name, mLogLevel);
118 copy->setParameters(**mSeriesRes, **mSeriesInd);
119 return copy;
120}
121
123 if (mSubCompCreated)
124 return;
125 mSubCompCreated = true;
126
127 // Default model with virtual node in between
129 std::make_shared<SP::Ph1::Resistor>(**mName + "_res", mLogLevel);
130 mSubResistor->setParameters(**mSeriesRes);
131 mSubResistor->connect({mTerminals[0]->node(), mVirtualNodes[0]});
132 addMNASubComponent(mSubResistor, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
133 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
134
136 std::make_shared<SP::Ph1::Inductor>(**mName + "_ind", mLogLevel);
137 mSubInductor->setParameters(**mSeriesInd);
138 mSubInductor->connect({mVirtualNodes[0], mTerminals[1]->node()});
139 addMNASubComponent(mSubInductor, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
140 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
141
143 std::make_shared<SP::Ph1::Resistor>(**mName + "_snubber_res", mLogLevel);
144 mInitialResistor->setParameters(1e6);
145 mInitialResistor->connect({SimNode::GND, mTerminals[1]->node()});
147 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
148 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
149}
150
152 (**mIntfVoltage)(0, 0) = initialSingleVoltage(1) - initialSingleVoltage(0);
153 Complex impedance = {**mSeriesRes, **mSeriesInd * 2. * PI * frequency};
154 (**mIntfCurrent)(0, 0) = (**mIntfVoltage)(0, 0) / impedance;
155 mVirtualNodes[0]->setInitialVoltage(initialSingleVoltage(0) +
156 (**mIntfCurrent)(0, 0) * **mSeriesRes);
157
158 SPDLOG_LOGGER_INFO(mSLog,
159 "\n--- Initialization from powerflow ---"
160 "\nVoltage across: {:s}"
161 "\nCurrent: {:s}"
162 "\nTerminal 0 voltage: {:s}"
163 "\nTerminal 1 voltage: {:s}"
164 "\n--- Initialization from powerflow finished ---",
165 Logger::phasorToString((**mIntfVoltage)(0, 0)),
166 Logger::phasorToString((**mIntfCurrent)(0, 0)),
167 Logger::phasorToString(initialSingleVoltage(0)),
168 Logger::phasorToString(initialSingleVoltage(1)));
169}
170
171void SP::Ph1::RXLine::mnaParentAddPreStepDependencies(
172 AttributeBase::List &prevStepDependencies,
173 AttributeBase::List &attributeDependencies,
174 AttributeBase::List &modifiedAttributes) {
175 modifiedAttributes.push_back(mRightVector);
176};
177
178void SP::Ph1::RXLine::mnaParentAddPostStepDependencies(
179 AttributeBase::List &prevStepDependencies,
180 AttributeBase::List &attributeDependencies,
181 AttributeBase::List &modifiedAttributes,
182 Attribute<Matrix>::Ptr &leftVector) {
183 attributeDependencies.push_back(leftVector);
184 modifiedAttributes.push_back(mIntfCurrent);
185 modifiedAttributes.push_back(mIntfVoltage);
186};
187
188void SP::Ph1::RXLine::mnaParentPreStep(Real time, Int timeStepCount) {
189 mnaCompApplyRightSideVectorStamp(**mRightVector);
190}
191
192void SP::Ph1::RXLine::mnaParentPostStep(Real time, Int timeStepCount,
193 Attribute<Matrix>::Ptr &leftVector) {
194 mnaCompUpdateVoltage(**leftVector);
195 mnaCompUpdateCurrent(**leftVector);
196}
197
198void SP::Ph1::RXLine::mnaCompUpdateVoltage(const Matrix &leftVector) {
199 (**mIntfVoltage)(0, 0) = 0;
200 if (terminalNotGrounded(1))
201 (**mIntfVoltage)(0, 0) =
202 Math::complexFromVectorElement(leftVector, matrixNodeIndex(1));
203 if (terminalNotGrounded(0))
204 (**mIntfVoltage)(0, 0) =
205 (**mIntfVoltage)(0, 0) -
206 Math::complexFromVectorElement(leftVector, matrixNodeIndex(0));
207}
208
209void SP::Ph1::RXLine::mnaCompUpdateCurrent(const Matrix &leftVector) {
210 (**mIntfCurrent)(0, 0) = mSubInductor->intfCurrent()(0, 0);
211}
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.
AttributeList::Ptr mAttributes
Attribute List.
std::shared_ptr< Resistor > mInitialResistor
Inductor end to ground resistor to facilitate initialization.
const Attribute< Real >::Ptr mInductance
Real getBaseVoltage() const
Get base voltage.
Real mBaseApparentPower
base apparent power [VA]
void initializeParentFromNodesAndTerminals(Real frequency) override
Derives values from power flow data and pushes them to subcomponents.
std::shared_ptr< Resistor > mSubResistor
Resistor submodel.
const Attribute< Real >::Ptr mActivePowerInjection
nodal active power injection
SimPowerComp< Complex >::Ptr clone(String name) override
DEPRECATED: Delete method.
void createSubComponents() override
Constructs and registers MNA subcomponents; idempotent.
Real mSeriesResPerUnit
resistance in [pu]
Real mBaseAdmittance
base admittance [S]
Real mBaseImpedance
base impedance [Ohm]
void setPerUnitSystem(Real baseApparentPower, Real baseOmega)
Specify per-unit system by base voltage, base apparent power and omega (rms value for voltage expecte...
const Attribute< Matrix >::Ptr mActivePowerBranch
branch active powerflow [W], coef(0) has data from node 0, coef(1) from node 1.
void pfApplyAdmittanceMatrixStamp(SparseMatrixCompRow &Y) override
Stamps admittance matrix.
void updateBranchFlow(VectorComp &current, VectorComp &powerflow)
updates branch current and power flow, input pu value, update with real value
const Attribute< MatrixComp >::Ptr mCurrent
branch Current flow [A], coef(0) has data from node 0, coef(1) from node 1.
Real mBaseInductance
base inductance [H]
const Attribute< Matrix >::Ptr mReactivePowerBranch
branch reactive powerflow [Var], coef(0) has data from node 0, coef(1) from node 1.
RXLine(String uid, String name, Real baseVoltage, Real resistance, Real inductance, Logger::Level logLevel=Logger::Level::off)
Defines UID, name, base voltage, component parameters and logging level.
const Attribute< Real >::Ptr mReactivePowerInjection
nodal reactive power injection
Real mSeriesIndPerUnit
Inductance of the line in [pu].
std::shared_ptr< Inductor > mSubInductor
Real mBaseCurrent
base current [V]
Real mBaseOmega
base omega [1/s]
void storeNodalInjection(Complex powerInjection)
stores nodal injection power in this line object
MatrixComp Y_element()
get admittance matrix
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.