DPsim
Loading...
Searching...
No Matches
DP_Ph3_Inductor.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, true, true, logLevel),
18 mEquivCurrent = MatrixComp::Zero(3, 1);
19 **mIntfVoltage = MatrixComp::Zero(3, 1);
20 **mIntfCurrent = MatrixComp::Zero(3, 1);
21}
22
24 auto copy = Inductor::make(name, mLogLevel);
25 copy->setParameters(**mInductance);
26 return copy;
27}
28
30
31 Real omega = 2 * PI * frequency;
32
33 MatrixComp reactance = MatrixComp::Zero(3, 3);
34 reactance << Complex(0, omega * (**mInductance)(0, 0)),
35 Complex(0, omega * (**mInductance)(0, 1)),
36 Complex(0, omega * (**mInductance)(0, 2)),
37 Complex(0, omega * (**mInductance)(1, 0)),
38 Complex(0, omega * (**mInductance)(1, 1)),
39 Complex(0, omega * (**mInductance)(1, 2)),
40 Complex(0, omega * (**mInductance)(2, 0)),
41 Complex(0, omega * (**mInductance)(2, 1)),
42 Complex(0, omega * (**mInductance)(2, 2));
43 MatrixComp susceptance = reactance.inverse();
44 // IntfVoltage initialization for each phase
45 (**mIntfVoltage)(0, 0) = initialSingleVoltage(1) - initialSingleVoltage(0);
46 Real voltMag = Math::abs((**mIntfVoltage)(0, 0));
47 Real voltPhase = Math::phase((**mIntfVoltage)(0, 0));
48 (**mIntfVoltage)(1, 0) = Complex(voltMag * cos(voltPhase - 2. / 3. * M_PI),
49 voltMag * sin(voltPhase - 2. / 3. * M_PI));
50 (**mIntfVoltage)(2, 0) = Complex(voltMag * cos(voltPhase + 2. / 3. * M_PI),
51 voltMag * sin(voltPhase + 2. / 3. * M_PI));
52
53 **mIntfCurrent = susceptance * **mIntfVoltage;
54
55 //TODO
56 SPDLOG_LOGGER_INFO(mSLog, "--- Initialize according to power flow ---");
57}
58
59void DP::Ph3::Inductor::initVars(Real omega, Real timeStep) {
60 Matrix a = timeStep / 2. * (**mInductance).inverse();
61 Real b = timeStep * omega / 2.;
62
63 Matrix equivCondReal = a / (1. + b * b);
64 Matrix equivCondImag = -a * b / (Real(1.) + b * b);
65 mEquivCond = MatrixComp::Zero(3, 3);
66 mEquivCond << Complex(equivCondReal(0, 0), equivCondImag(0, 0)),
67 Complex(equivCondReal(0, 1), equivCondImag(0, 1)),
68 Complex(equivCondReal(0, 2), equivCondImag(0, 2)),
69 Complex(equivCondReal(1, 0), equivCondImag(1, 0)),
70 Complex(equivCondReal(1, 1), equivCondImag(1, 1)),
71 Complex(equivCondReal(1, 2), equivCondImag(1, 2)),
72 Complex(equivCondReal(2, 0), equivCondImag(2, 0)),
73 Complex(equivCondReal(2, 1), equivCondImag(2, 1)),
74 Complex(equivCondReal(2, 2), equivCondImag(2, 2));
75
76 Real preCurrFracReal = (1. - b * b) / (1. + b * b);
77 Real preCurrFracImag = (-2. * b) / (1. + b * b);
78 mPrevCurrFac = Complex(preCurrFracReal, preCurrFracImag);
79
80 // TODO: check if this is correct or if it should be only computed before the step
82}
83
85 Attribute<Matrix>::Ptr leftVector) {
87 initVars(omega, timeStep);
88
89 SPDLOG_LOGGER_INFO(mSLog, "Initial voltage {}",
90 Math::abs((**mIntfVoltage)(0, 0)));
91}
92
99
101
102 // Calculate equivalent current source for next time step
104
105 if (terminalNotGrounded(0)) {
106 Math::setVectorElement(rightVector, matrixNodeIndex(0, 0),
107 mEquivCurrent(0, 0));
108 Math::setVectorElement(rightVector, matrixNodeIndex(0, 1),
109 mEquivCurrent(1, 0));
110 Math::setVectorElement(rightVector, matrixNodeIndex(0, 2),
111 mEquivCurrent(2, 0));
112 }
113 if (terminalNotGrounded(1)) {
114 Math::setVectorElement(rightVector, matrixNodeIndex(1, 0),
115 -mEquivCurrent(0, 0));
116 Math::setVectorElement(rightVector, matrixNodeIndex(1, 1),
117 -mEquivCurrent(1, 0));
118 Math::setVectorElement(rightVector, matrixNodeIndex(1, 2),
119 -mEquivCurrent(2, 0));
120 }
121}
122
124 AttributeBase::List &prevStepDependencies,
125 AttributeBase::List &attributeDependencies,
126 AttributeBase::List &modifiedAttributes) {
127 // actually depends on L, but then we'd have to modify the system matrix anyway
128 modifiedAttributes.push_back(mRightVector);
129 prevStepDependencies.push_back(mIntfVoltage);
130 prevStepDependencies.push_back(mIntfCurrent);
131}
132
136
138 AttributeBase::List &prevStepDependencies,
139 AttributeBase::List &attributeDependencies,
140 AttributeBase::List &modifiedAttributes,
141 Attribute<Matrix>::Ptr &leftVector) {
142 attributeDependencies.push_back(leftVector);
143 modifiedAttributes.push_back(mIntfVoltage);
144 modifiedAttributes.push_back(mIntfCurrent);
145}
146
148 Attribute<Matrix>::Ptr &leftVector) {
149 mnaCompUpdateVoltage(**leftVector);
150 mnaCompUpdateCurrent(**leftVector);
151}
152
154 // v1 - v0
155 **mIntfVoltage = Matrix::Zero(3, 1);
156 if (terminalNotGrounded(1)) {
157 (**mIntfVoltage)(0, 0) =
159 (**mIntfVoltage)(1, 0) =
161 (**mIntfVoltage)(2, 0) =
163 }
164 if (terminalNotGrounded(0)) {
165 (**mIntfVoltage)(0, 0) =
166 (**mIntfVoltage)(0, 0) -
168 (**mIntfVoltage)(1, 0) =
169 (**mIntfVoltage)(1, 0) -
171 (**mIntfVoltage)(2, 0) =
172 (**mIntfVoltage)(2, 0) -
174 }
175}
176
180
181// #### Tear Methods ####
183 initVars(omega, timeStep);
184}
185
187 // Set diagonal entries
188 Math::addToMatrixElement(tearMatrix, mTearIdx * 3, mTearIdx * 3,
189 1. / mEquivCond(0, 0)); // 1 /
190 Math::addToMatrixElement(tearMatrix, mTearIdx * 3 + 1, mTearIdx * 3 + 1,
191 1. / mEquivCond(1, 1));
192 Math::addToMatrixElement(tearMatrix, mTearIdx * 3 + 2, mTearIdx * 3 + 2,
193 1. / mEquivCond(2, 2));
194}
195
198 mEquivCond * (**mIntfVoltage) + mPrevCurrFac * (**mIntfCurrent);
199 Math::addToVectorElement(voltageVector, mTearIdx * 3,
200 mEquivCurrent(0, 0) / mEquivCond(0, 0));
201 Math::addToVectorElement(voltageVector, mTearIdx * 3 + 1,
202 mEquivCurrent(1, 0) / mEquivCond(1, 1));
203 Math::addToVectorElement(voltageVector, mTearIdx * 3 + 2,
204 mEquivCurrent(2, 0) / mEquivCond(2, 2));
205}
206
208 MatrixComp current) {
209 (**mIntfVoltage) = voltage;
210 (**mIntfCurrent) = mEquivCond * voltage + mEquivCurrent;
211}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:250
const CPS::Attribute< Matrix >::Ptr mInductance
Inductance [H].
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
Initializes internal variables of the component.
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamps system matrix.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
Complex mPrevCurrFac
Coefficient in front of previous current value.
void mnaTearPostStep(MatrixComp voltage, MatrixComp current) override
MatrixComp mEquivCurrent
DC equivalent current source [A].
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Update interface voltage from MNA system result.
Inductor(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name, component parameters and logging level.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Update interface current from MNA system result.
void mnaTearApplyMatrixStamp(SparseMatrixRow &tearMatrix) override
MatrixComp mEquivCond
Equivalent conductance [S].
void initVars(Real omega, Real timeStep)
void mnaTearInitialize(Real omega, Real timestep) override
SimPowerComp< Complex >::Ptr clone(String name) override
Returns a modified copy of the component with the given suffix added to the name and without.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
void mnaCompPreStep(Real time, Int timeStepCount) override
void mnaTearApplyVoltageStamp(Matrix &voltageVector) override
void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
String uid()
Returns unique id.
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
Attribute< Matrix >::Ptr mRightVector
static void stampAdmittanceMatrix(const MatrixComp &admittanceMat, 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)
Definition MathUtils.cpp:94
static void setVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0, Matrix::Index colOffset=0)
Definition MathUtils.cpp:73
static void addToVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0)
Definition MathUtils.cpp:83
static Real phase(Complex value)
Definition MathUtils.cpp:23
static Real abs(Complex value)
Definition MathUtils.cpp:27
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< Inductor > make(Args &&...args)
Definition PtrFactory.h:19
#define PI
Definition Definitions.h:43
#define M_PI
Definition Definitions.h:41
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
Dense matrix for real numbers.
Definition Definitions.h:81
std::string String
Definition Definitions.h:65
double Real
Definition Definitions.h:62
int Int
Definition Definitions.h:61
std::complex< Real > Complex
Definition Definitions.h:63
Eigen::Matrix< Complex, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixComp
Dense matrix for complex numbers.
Definition Definitions.h:84
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:74