DPsim
Loading...
Searching...
No Matches
SP_Ph1_Resistor.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/MathUtils.h>
10#include <dpsim-models/SP/SP_Ph1_Resistor.h>
11
12using namespace CPS;
13
14SP::Ph1::Resistor::Resistor(String uid, String name, Logger::Level logLevel)
15 : MNASimPowerComp<Complex>(uid, name, false, true, logLevel),
16 Base::Ph1::Resistor(mAttributes) {
17 setTerminalNumber(2);
18 **mIntfVoltage = MatrixComp::Zero(1, 1);
19 **mIntfCurrent = MatrixComp::Zero(1, 1);
20}
21
22SimPowerComp<Complex>::Ptr SP::Ph1::Resistor::clone(String name) {
23 auto copy = Resistor::make(name, mLogLevel);
24 copy->setParameters(**mResistance);
25 return copy;
26}
27
29
30 (**mIntfVoltage)(0, 0) = initialSingleVoltage(1) - initialSingleVoltage(0);
32
33 SPDLOG_LOGGER_INFO(mSLog, "\nResistance [Ohm]: {:s}",
34 Logger::realToString(**mResistance));
35 SPDLOG_LOGGER_INFO(mSLog,
36 "\n--- Initialization from powerflow ---"
37 "\nVoltage across: {:s}"
38 "\nCurrent: {:s}"
39 "\nTerminal 0 voltage: {:s}"
40 "\nTerminal 1 voltage: {:s}"
41 "\n--- Initialization from powerflow finished ---",
42 Logger::phasorToString((**mIntfVoltage)(0, 0)),
43 Logger::phasorToString((**mIntfCurrent)(0, 0)),
44 Logger::phasorToString(initialSingleVoltage(0)),
45 Logger::phasorToString(initialSingleVoltage(1)));
46}
47
48// #### Powerflow section ####
49
50void SP::Ph1::Resistor::setBaseVoltage(Real baseVoltage) {
51 mBaseVoltage = baseVoltage;
52}
53
55 SPDLOG_LOGGER_INFO(mSLog, "#### Calculate Per Unit Parameters for {}",
56 **mName);
57 mBaseApparentPower = baseApparentPower;
58 SPDLOG_LOGGER_INFO(mSLog, "Base Power={} [VA]", baseApparentPower);
59
60 mBaseImpedance = mBaseVoltage * mBaseVoltage / mBaseApparentPower;
61 mBaseAdmittance = 1.0 / mBaseImpedance;
62 mBaseCurrent = baseApparentPower /
63 (mBaseVoltage *
64 sqrt(3)); // I_base=(S_threephase/3)/(V_line_to_line/sqrt(3))
65 SPDLOG_LOGGER_INFO(mSLog, "Base Voltage={} [V] Base Impedance={} [Ohm]",
66 mBaseVoltage, mBaseImpedance);
67
68 mResistancePerUnit = **mResistance / mBaseImpedance;
69 mConductancePerUnit = 1. / mResistancePerUnit;
70 SPDLOG_LOGGER_INFO(mSLog, "Resistance={} [pu] Conductance={} [pu]",
71 mResistancePerUnit, mConductancePerUnit);
72}
73
75 int bus1 = this->matrixNodeIndex(0);
76 Complex Y_element = Complex(mConductancePerUnit, 0);
77
78 if (!Math::isFinite(Y_element)) {
79 SPDLOG_LOGGER_ERROR(mSLog,
80 "Resistor {}: non-finite per-unit admittance {} at "
81 "node {}",
82 this->name(), Logger::complexToString(Y_element), bus1);
84 }
85
86 //set the circuit matrix values
87 Y.coeffRef(bus1, bus1) += Y_element;
88 SPDLOG_LOGGER_INFO(mSLog, "#### Y matrix stamping: {}", Y_element);
89}
90
91// #### MNA section ####
92
93void SP::Ph1::Resistor::mnaCompInitialize(Real omega, Real timeStep,
94 Attribute<Matrix>::Ptr leftVector) {
95 updateMatrixNodeIndices();
96 **mRightVector = Matrix::Zero(0, 0);
97
98 SPDLOG_LOGGER_INFO(mSLog,
99 "\n--- MNA initialization ---"
100 "\nInitial voltage {:s}"
101 "\nInitial current {:s}"
102 "\n--- MNA initialization finished ---",
103 Logger::phasorToString((**mIntfVoltage)(0, 0)),
104 Logger::phasorToString((**mIntfCurrent)(0, 0)));
105}
106
108 SparseMatrixRow &systemMatrix) {
109 Complex conductance = Complex(1. / **mResistance, 0);
110
111 MNAStampUtils::stampAdmittance(conductance, systemMatrix, matrixNodeIndex(0),
112 matrixNodeIndex(1), terminalNotGrounded(0),
113 terminalNotGrounded(1), mSLog);
114}
115
117 AttributeBase::List &prevStepDependencies,
118 AttributeBase::List &attributeDependencies,
119 AttributeBase::List &modifiedAttributes,
120 Attribute<Matrix>::Ptr &leftVector) {
121 attributeDependencies.push_back(leftVector);
122 modifiedAttributes.push_back(mIntfVoltage);
123 modifiedAttributes.push_back(mIntfCurrent);
124}
125
126void SP::Ph1::Resistor::mnaCompPostStep(Real time, Int timeStepCount,
127 Attribute<Matrix>::Ptr &leftVector) {
128 this->mnaUpdateVoltage(**leftVector);
129 this->mnaUpdateCurrent(**leftVector);
130}
131
132void SP::Ph1::Resistor::mnaCompUpdateVoltage(const Matrix &leftVector) {
133 // v1 - v0
134 for (UInt freq = 0; freq < mNumFreqs; freq++) {
135 (**mIntfVoltage)(0, freq) = 0;
136 if (terminalNotGrounded(1))
137 (**mIntfVoltage)(0, freq) = Math::complexFromVectorElement(
138 leftVector, matrixNodeIndex(1), mNumFreqs, freq);
139 if (terminalNotGrounded(0))
140 (**mIntfVoltage)(0, freq) =
141 (**mIntfVoltage)(0, freq) -
142 Math::complexFromVectorElement(leftVector, matrixNodeIndex(0),
143 mNumFreqs, freq);
144
145 SPDLOG_LOGGER_DEBUG(mSLog, "Voltage {:s}",
146 Logger::phasorToString((**mIntfVoltage)(0, freq)));
147 }
148}
149
150void SP::Ph1::Resistor::mnaCompUpdateCurrent(const Matrix &leftVector) {
151 for (UInt freq = 0; freq < mNumFreqs; freq++) {
152 (**mIntfCurrent)(0, freq) = (**mIntfVoltage)(0, freq) / **mResistance;
153 SPDLOG_LOGGER_DEBUG(mSLog, "Current {:s}",
154 Logger::phasorToString((**mIntfCurrent)(0, freq)));
155 }
156}
157
158void SP::Ph1::Resistor::mnaTearApplyMatrixStamp(SparseMatrixRow &tearMatrix) {
159 Math::addToMatrixElement(tearMatrix, mTearIdx, mTearIdx,
160 Complex(**mResistance, 0));
161}
const CPS::Attribute< Real >::Ptr mResistance
Resistance [ohm].
const Attribute< String >::Ptr mName
Human readable name.
String uid()
Returns unique id.
AttributeList::Ptr mAttributes
Attribute List.
void mnaUpdateCurrent(const Matrix &leftVector) final
void mnaUpdateVoltage(const Matrix &leftVector) final
MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
void mnaCompUpdateCurrent(const Matrix &leftVector)
Update interface current from MNA system result.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector)
add MNA pre and post step dependencies
SimPowerComp< Complex >::Ptr clone(String name)
Returns a modified copy of the component with the given suffix added to the name and without.
void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector)
MNA pre and post step operations.
void mnaCompUpdateVoltage(const Matrix &leftVector)
Update interface voltage from MNA system result.
void setBaseVoltage(Real baseVoltage)
Set base voltage.
Resistor(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
void initializeFromNodesAndTerminals(Real frequency)
Initializes component from power flow data.
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix)
Stamps system matrix.
void pfApplyAdmittanceMatrixStamp(SparseMatrixCompRow &Y)
Stamps admittance matrix.
void calculatePerUnitParameters(Real baseApparentPower)
Initializes component from power flow data.
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
Logger::Level mLogLevel
Component logger control for internal variables.
Logger::Log mSLog
Component logger.