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
11using namespace CPS;
12
13SP::Ph1::RXLine::RXLine(String uid, String name, Real baseVoltage,
14 Real resistance, Real inductance,
15 Logger::Level logLevel)
16 : Base::Ph1::PiLine(mAttributes),
17 CompositePowerComp<Complex>(uid, name, true, true, logLevel),
18 mInductance(mAttributes->create<Real>("L_series")),
19 mActivePowerInjection(mAttributes->create<Real>("p_inj")),
20 mReactivePowerInjection(mAttributes->create<Real>("q_inj")),
21 mCurrent(mAttributes->create<MatrixComp>("current_vector")),
22 mActivePowerBranch(mAttributes->create<Matrix>("p_branch_vector")),
23 mReactivePowerBranch(mAttributes->create<Matrix>("q_branch_vector")) {
24
25 setTerminalNumber(2);
26
27 **mSeriesRes = resistance;
28 **mInductance = inductance;
29
30 **mCurrent = MatrixComp::Zero(2, 1);
31 **mActivePowerBranch = Matrix::Zero(2, 1);
32 **mReactivePowerBranch = Matrix::Zero(2, 1);
33}
34
35SP::Ph1::RXLine::RXLine(String uid, String name, Logger::Level logLevel)
36 : Base::Ph1::PiLine(mAttributes),
37 CompositePowerComp<Complex>(uid, name, true, true, logLevel),
38 mInductance(mAttributes->create<Real>("L_series")),
39 mActivePowerInjection(mAttributes->create<Real>("p_inj")),
40 mReactivePowerInjection(mAttributes->create<Real>("q_inj")),
41 mCurrent(mAttributes->create<MatrixComp>("current_vector")),
42 mActivePowerBranch(mAttributes->create<Matrix>("p_branch_vector")),
43 mReactivePowerBranch(mAttributes->create<Matrix>("q_branch_vector")) {
44
45 setVirtualNodeNumber(1);
46 setTerminalNumber(2);
47 **mIntfVoltage = MatrixComp::Zero(1, 1);
48 **mIntfCurrent = MatrixComp::Zero(1, 1);
49}
50
51void SP::Ph1::RXLine::setPerUnitSystem(Real baseApparentPower, Real baseOmega) {
52 mBaseApparentPower = baseApparentPower;
53 mBaseOmega = baseOmega;
54 mBaseImpedance = (mBaseVoltage * mBaseVoltage) / mBaseApparentPower;
57 mBaseCurrent = baseApparentPower / (mBaseVoltage * sqrt(3));
58
61}
62
63Real SP::Ph1::RXLine::getBaseVoltage() const { return mBaseVoltage; }
64
67 int bus1 = this->matrixNodeIndex(0);
68 int bus2 = this->matrixNodeIndex(1);
69
70 //create the element admittance matrix
71 Complex y =
72 Complex(1, 0) / Complex(mSeriesResPerUnit, 1. * mSeriesIndPerUnit);
73
74 //Fill the internal matrix
75 mY_element = MatrixComp(2, 2);
76 mY_element(0, 0) = y;
77 mY_element(0, 1) = -y;
78 mY_element(1, 0) = -y;
79 mY_element(1, 1) = y;
80
81 //check for inf or nan
82 for (int i = 0; i < 2; i++)
83 for (int j = 0; j < 2; j++)
84 if (std::isinf(mY_element.coeff(i, j).real()) ||
85 std::isinf(mY_element.coeff(i, j).imag())) {
86 std::cout << mY_element << std::endl;
87 std::stringstream ss;
88 ss << "Line>>" << this->name()
89 << ": infinite or nan values in the element Y at: " << i << "," << j;
90 throw std::invalid_argument(ss.str());
91 std::cout << "Line>>" << this->name()
92 << ": infinite or nan values in the element Y at: " << i
93 << "," << j << std::endl;
94 }
95
96 //set the circuit matrix values
97 Y.coeffRef(bus1, bus1) += mY_element.coeff(0, 0);
98 Y.coeffRef(bus1, bus2) += mY_element.coeff(0, 1);
99 Y.coeffRef(bus2, bus1) += mY_element.coeff(1, 0);
100 Y.coeffRef(bus2, bus2) += mY_element.coeff(1, 1);
101}
102
103void SP::Ph1::RXLine::updateBranchFlow(VectorComp &current,
104 VectorComp &powerflow) {
105 **mCurrent = current * mBaseCurrent;
106 **mActivePowerBranch = powerflow.real() * mBaseApparentPower;
107 **mReactivePowerBranch = powerflow.imag() * mBaseApparentPower;
108}
109
110void SP::Ph1::RXLine::storeNodalInjection(Complex powerInjection) {
111 **mActivePowerInjection = std::real(powerInjection) * mBaseApparentPower;
112 **mReactivePowerInjection = std::imag(powerInjection) * mBaseApparentPower;
113}
114
115MatrixComp SP::Ph1::RXLine::Y_element() { return mY_element; }
116
118SimPowerComp<Complex>::Ptr SP::Ph1::RXLine::clone(String name) {
119 auto copy = RXLine::make(name, mLogLevel);
120 copy->setParameters(**mSeriesRes, **mSeriesInd);
121 return copy;
122}
123
125 if (mSubCompCreated)
126 return;
127 mSubCompCreated = true;
128
129 // Default model with virtual node in between
131 std::make_shared<SP::Ph1::Resistor>(**mName + "_res", mLogLevel);
132 mSubResistor->setParameters(**mSeriesRes);
133 mSubResistor->connect({mTerminals[0]->node(), mVirtualNodes[0]});
134 addMNASubComponent(mSubResistor, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
135 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
136
138 std::make_shared<SP::Ph1::Inductor>(**mName + "_ind", mLogLevel);
139 mSubInductor->setParameters(**mSeriesInd);
140 mSubInductor->connect({mVirtualNodes[0], mTerminals[1]->node()});
141 addMNASubComponent(mSubInductor, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
142 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
143
145 std::make_shared<SP::Ph1::Resistor>(**mName + "_snubber_res", mLogLevel);
146 mInitialResistor->setParameters(1e6);
147 mInitialResistor->connect({SimNode::GND, mTerminals[1]->node()});
149 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
150 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
151}
152
154 (**mIntfVoltage)(0, 0) = initialSingleVoltage(1) - initialSingleVoltage(0);
155 Complex impedance = {**mSeriesRes, **mSeriesInd * 2. * PI * frequency};
156 (**mIntfCurrent)(0, 0) = (**mIntfVoltage)(0, 0) / impedance;
157 mVirtualNodes[0]->setInitialVoltage(initialSingleVoltage(0) +
158 (**mIntfCurrent)(0, 0) * **mSeriesRes);
159
160 SPDLOG_LOGGER_INFO(mSLog,
161 "\n--- Initialization from powerflow ---"
162 "\nVoltage across: {:s}"
163 "\nCurrent: {:s}"
164 "\nTerminal 0 voltage: {:s}"
165 "\nTerminal 1 voltage: {:s}"
166 "\n--- Initialization from powerflow finished ---",
167 Logger::phasorToString((**mIntfVoltage)(0, 0)),
168 Logger::phasorToString((**mIntfCurrent)(0, 0)),
169 Logger::phasorToString(initialSingleVoltage(0)),
170 Logger::phasorToString(initialSingleVoltage(1)));
171}
172
173void SP::Ph1::RXLine::mnaParentAddPreStepDependencies(
174 AttributeBase::List &prevStepDependencies,
175 AttributeBase::List &attributeDependencies,
176 AttributeBase::List &modifiedAttributes) {
177 modifiedAttributes.push_back(mRightVector);
178};
179
180void SP::Ph1::RXLine::mnaParentAddPostStepDependencies(
181 AttributeBase::List &prevStepDependencies,
182 AttributeBase::List &attributeDependencies,
183 AttributeBase::List &modifiedAttributes,
184 Attribute<Matrix>::Ptr &leftVector) {
185 attributeDependencies.push_back(leftVector);
186 modifiedAttributes.push_back(mIntfCurrent);
187 modifiedAttributes.push_back(mIntfVoltage);
188};
189
190void SP::Ph1::RXLine::mnaParentPreStep(Real time, Int timeStepCount) {
191 mnaCompApplyRightSideVectorStamp(**mRightVector);
192}
193
194void SP::Ph1::RXLine::mnaParentPostStep(Real time, Int timeStepCount,
195 Attribute<Matrix>::Ptr &leftVector) {
196 mnaCompUpdateVoltage(**leftVector);
197 mnaCompUpdateCurrent(**leftVector);
198}
199
200void SP::Ph1::RXLine::mnaCompUpdateVoltage(const Matrix &leftVector) {
201 (**mIntfVoltage)(0, 0) = 0;
202 if (terminalNotGrounded(1))
203 (**mIntfVoltage)(0, 0) =
204 Math::complexFromVectorElement(leftVector, matrixNodeIndex(1));
205 if (terminalNotGrounded(0))
206 (**mIntfVoltage)(0, 0) =
207 (**mIntfVoltage)(0, 0) -
208 Math::complexFromVectorElement(leftVector, matrixNodeIndex(0));
209}
210
211void SP::Ph1::RXLine::mnaCompUpdateCurrent(const Matrix &leftVector) {
212 (**mIntfCurrent)(0, 0) = mSubInductor->intfCurrent()(0, 0);
213}
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.