DPsim
Loading...
Searching...
No Matches
DP_Ph1_Resistor.cpp
Go to the documentation of this file.
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
10
11using namespace CPS;
12
14 : MNASimPowerComp<Complex>(uid, name, false, true, logLevel),
16 **mIntfVoltage = MatrixComp::Zero(1, 1);
17 **mIntfCurrent = MatrixComp::Zero(1, 1);
19}
20
22 auto copy = Resistor::make(name, mLogLevel);
23 copy->setParameters(**mResistance);
24 return copy;
25}
26
28
29 Complex impedance = {**mResistance, 0};
30 (**mIntfVoltage)(0, 0) = initialSingleVoltage(1) - initialSingleVoltage(0);
31 (**mIntfCurrent)(0, 0) = (**mIntfVoltage)(0, 0) / impedance;
32
33 SPDLOG_LOGGER_INFO(mSLog,
34 "\nResistance [Ohm]: {:s}"
35 "\nImpedance [Ohm]: {:s}",
37 Logger::complexToString(impedance));
38 SPDLOG_LOGGER_INFO(mSLog,
39 "\n--- Initialization from powerflow ---"
40 "\nVoltage across: {:s}"
41 "\nCurrent: {:s}"
42 "\nTerminal 0 voltage: {:s}"
43 "\nTerminal 1 voltage: {:s}"
44 "\n--- Initialization from powerflow finished ---",
49}
50
51// #### MNA functions ####
53 Attribute<Matrix>::Ptr leftVector) {
55
56 **mRightVector = Matrix::Zero(0, 0);
57
58 SPDLOG_LOGGER_INFO(mSLog,
59 "\n--- MNA initialization ---"
60 "\nInitial voltage {:s}"
61 "\nInitial current {:s}"
62 "\n--- MNA initialization finished ---",
65}
66
68 Real omega, Real timeStep,
69 std::vector<Attribute<Matrix>::Ptr> leftVectors) {
71
72 mMnaTasks.push_back(std::make_shared<MnaPostStepHarm>(*this, leftVectors));
73}
74
76 SparseMatrixRow &systemMatrix) {
77 const Complex conductance = Complex(1. / **mResistance, 0);
78 for (UInt freq = 0; freq < mNumFreqs; freq++) {
80 conductance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1),
82 }
83}
84
86 SparseMatrixRow &systemMatrix, Int freqIdx) {
87 const Complex conductance = Complex(1. / **mResistance, 0);
88 MNAStampUtils::stampAdmittance(conductance, systemMatrix, matrixNodeIndex(0),
91}
92
94 AttributeBase::List &prevStepDependencies,
95 AttributeBase::List &attributeDependencies,
96 AttributeBase::List &modifiedAttributes,
97 Attribute<Matrix>::Ptr &leftVector) {
98 attributeDependencies.push_back(leftVector);
99 modifiedAttributes.push_back(this->mIntfVoltage);
100 modifiedAttributes.push_back(this->mIntfCurrent);
101}
102
104 Attribute<Matrix>::Ptr &leftVector) {
105 this->mnaUpdateVoltage(**leftVector);
106 this->mnaUpdateCurrent(**leftVector);
107}
108
110 for (UInt freq = 0; freq < mResistor.mNumFreqs; freq++)
111 mResistor.mnaCompUpdateVoltageHarm(**mLeftVectors[freq], freq);
112 mResistor.mnaCompUpdateCurrentHarm();
113}
114
116 // v1 - v0
117 for (UInt freq = 0; freq < mNumFreqs; freq++) {
118 (**mIntfVoltage)(0, freq) = 0;
119 if (terminalNotGrounded(1))
120 (**mIntfVoltage)(0, freq) = Math::complexFromVectorElement(
121 leftVector, matrixNodeIndex(1), mNumFreqs, freq);
122 if (terminalNotGrounded(0))
123 (**mIntfVoltage)(0, freq) =
124 (**mIntfVoltage)(0, freq) -
126 mNumFreqs, freq);
127
128 SPDLOG_LOGGER_DEBUG(mSLog, "Voltage {:s}",
130 }
131}
132
134 for (UInt freq = 0; freq < mNumFreqs; freq++) {
135 (**mIntfCurrent)(0, freq) = (**mIntfVoltage)(0, freq) / **mResistance;
136 SPDLOG_LOGGER_DEBUG(mSLog, "Current {:s}",
138 }
139}
140
142 Int freqIdx) {
143 // v1 - v0
144 (**mIntfVoltage)(0, freqIdx) = 0;
145 if (terminalNotGrounded(1))
146 (**mIntfVoltage)(0, freqIdx) =
148 if (terminalNotGrounded(0))
149 (**mIntfVoltage)(0, freqIdx) =
150 (**mIntfVoltage)(0, freqIdx) -
152
153 SPDLOG_LOGGER_DEBUG(mSLog, "Voltage {:s}",
154 Logger::phasorToString((**mIntfVoltage)(0, freqIdx)));
155}
156
158 for (UInt freq = 0; freq < mNumFreqs; freq++) {
159 (**mIntfCurrent)(0, freq) = (**mIntfVoltage)(0, freq) / **mResistance;
160 SPDLOG_LOGGER_DEBUG(mSLog, "Current {:s}",
162 }
163}
164
165// #### Tear Methods ####
170
171// #### DAE functions ####
172
173void DP::Ph1::Resistor::daeResidual(double ttime, const double state[],
174 const double dstate_dt[], double resid[],
175 std::vector<int> &off) {
176 int Pos1 = matrixNodeIndex(0);
177 int Pos2 = matrixNodeIndex(1);
178 int c_offset = off[0] + off[1]; // Current offset for component
179 int n_offset_1 =
180 c_offset + Pos1 + 1; // Current offset for first nodal equation
181 int n_offset_2 =
182 c_offset + Pos2 + 1; // Current offset for second nodal equation
183 resid[c_offset] = (state[Pos2] - state[Pos1]) -
184 state[c_offset]; // Voltage equation for Resistor
185 resid[n_offset_1] += 1.0 / **mResistance * state[c_offset];
186 resid[n_offset_2] += 1.0 / **mResistance * state[c_offset];
187 off[1] += 1;
188}
189
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
const CPS::Attribute< Real >::Ptr mResistance
Resistance [ohm].
void execute(Real time, Int timeStepCount)
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector)
Complex daeInitialize()
Voltage Getter.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector)
add MNA pre and post step dependencies
void mnaCompInitializeHarm(Real omega, Real timeStep, std::vector< Attribute< Matrix >::Ptr > leftVector)
void mnaCompApplySystemMatrixStampHarm(SparseMatrixRow &systemMatrix, Int freqIdx)
Stamps system matrix considering the frequency index.
Resistor(String uid, String name, Logger::Level loglevel=Logger::Level::off)
Defines UID, name and logging level.
void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector)
MNA pre and post step operations.
void daeResidual(double ttime, const double state[], const double dstate_dt[], double resid[], std::vector< int > &off)
Residual Function for DAE Solver.
void mnaCompUpdateCurrent(const Matrix &leftVector)
Update interface current from MNA system result.
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix)
Stamps system matrix.
SimPowerComp< Complex >::Ptr clone(String name)
Returns a modified copy of the component with the given suffix added to the name and without.
void mnaCompUpdateVoltageHarm(const Matrix &leftVector, Int freqIdx)
void mnaTearApplyMatrixStamp(SparseMatrixRow &tearMatrix)
void mnaCompUpdateVoltage(const Matrix &leftVector)
Update interface voltage from MNA system result.
void initializeFromNodesAndTerminals(Real frequency)
Initializes component from power flow data.
String uid()
Returns unique id.
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
static String complexToString(const Complex &num)
Definition Logger.cpp:63
static String realToString(const Real &num)
Definition Logger.cpp:69
static String phasorToString(const Complex &num)
Definition Logger.cpp:57
void mnaUpdateCurrent(const Matrix &leftVector) final
void mnaUpdateVoltage(const Matrix &leftVector) final
MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
Attribute< Matrix >::Ptr mRightVector
static void stampAdmittance(Complex admittance, SparseMatrixRow &mat, UInt node1Index, UInt node2Index, Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded, const Logger::Log &mSLog, Int maxFreq=1, Int freqIdx=0)
static Complex complexFromVectorElement(const Matrix &mat, Matrix::Index row, Int maxFreq=1, Int freqIdx=0)
static void addToMatrixElement(SparseMatrixRow &mat, Matrix::Index row, Matrix::Index column, Complex value, Int maxFreq=1, Int freqIdx=0)
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
std::shared_ptr< SimPowerComp< VarType > > Ptr
Bool terminalNotGrounded(UInt index)
Complex initialSingleVoltage(UInt index)
Logger::Level mLogLevel
Component logger control for internal variables.
Logger::Log mSLog
Component logger.
static std::shared_ptr< Resistor > make(Args &&...args)
Definition PtrFactory.h:19
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
Dense matrix for real numbers.
Definition Definitions.h:79
std::string String
Definition Definitions.h:63
double Real
Definition Definitions.h:60
int Int
Definition Definitions.h:59
std::complex< Real > Complex
Definition Definitions.h:61
unsigned int UInt
Definition Definitions.h:58
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:72