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
126 (**mIntfVoltage)(0, 0) = initialSingleVoltage(1) - initialSingleVoltage(0);
127 Complex impedance = {**mSeriesRes, **mSeriesInd * 2. * PI * frequency};
128 (**mIntfCurrent)(0, 0) = (**mIntfVoltage)(0, 0) / impedance;
129 mVirtualNodes[0]->setInitialVoltage(initialSingleVoltage(0) +
130 (**mIntfCurrent)(0, 0) * **mSeriesRes);
131
132 // Default model with virtual node in between
134 std::make_shared<SP::Ph1::Resistor>(**mName + "_res", mLogLevel);
135 mSubResistor->setParameters(**mSeriesRes);
136 mSubResistor->connect({mTerminals[0]->node(), mVirtualNodes[0]});
137 mSubResistor->initializeFromNodesAndTerminals(frequency);
138 addMNASubComponent(mSubResistor, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
139 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
140
142 std::make_shared<SP::Ph1::Inductor>(**mName + "_ind", mLogLevel);
143 mSubInductor->setParameters(**mSeriesInd);
144 mSubInductor->connect({mVirtualNodes[0], mTerminals[1]->node()});
145 mSubInductor->initializeFromNodesAndTerminals(frequency);
146 addMNASubComponent(mSubInductor, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
147 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
148
150 std::make_shared<SP::Ph1::Resistor>(**mName + "_snubber_res", mLogLevel);
151 mInitialResistor->setParameters(1e6);
152 mInitialResistor->connect({SimNode::GND, mTerminals[1]->node()});
153 mInitialResistor->initializeFromNodesAndTerminals(frequency);
155 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
156 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
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.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
const Attribute< Real >::Ptr mInductance
Real getBaseVoltage() const
Get base voltage.
Real mBaseApparentPower
base apparent power [VA]
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.
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
Inductance submodel.
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.