DPsim
Loading...
Searching...
No Matches
DP_Ph1_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),
16 mEquivCurrent = {0, 0};
17 **mIntfVoltage = MatrixComp::Zero(1, 1);
18 **mIntfCurrent = MatrixComp::Zero(1, 1);
20}
21
23 auto copy = Inductor::make(name, mLogLevel);
24 copy->setParameters(**mInductance);
25 return copy;
26}
27
30
31 mEquivCurrent = MatrixComp::Zero(mNumFreqs, 1);
32 mEquivCond = MatrixComp::Zero(mNumFreqs, 1);
33 mPrevCurrFac = MatrixComp::Zero(mNumFreqs, 1);
34}
35
39
40void DP::Ph1::Inductor::updateInductance(Real inductance, Real timeStep) {
41 setParameters(inductance);
42 initVars(timeStep);
43}
44
46
47 Real omega = 2. * PI * frequency;
48 Complex impedance = {0, omega * **mInductance};
49 (**mIntfVoltage)(0, 0) = initialSingleVoltage(1) - initialSingleVoltage(0);
50 (**mIntfCurrent)(0, 0) = (**mIntfVoltage)(0, 0) / impedance;
51
52 SPDLOG_LOGGER_INFO(mSLog,
53 "\nInductance [H]: {:s}"
54 "\nImpedance [Ohm]: {:s}",
56 Logger::complexToString(impedance));
57 SPDLOG_LOGGER_INFO(mSLog,
58 "\n--- Initialization from powerflow ---"
59 "\nVoltage across: {:s}"
60 "\nCurrent: {:s}"
61 "\nTerminal 0 voltage: {:s}"
62 "\nTerminal 1 voltage: {:s}"
63 "\n--- Initialization from powerflow finished ---",
68}
69
70// #### MNA functions ####
71
73 for (UInt freq = 0; freq < mNumFreqs; freq++) {
74 Real a = timeStep / (2. * **mInductance);
75 Real b = timeStep * 2. * PI * mFrequencies(freq, 0) / 2.;
76
77 Real equivCondReal = a / (1. + b * b);
78 Real equivCondImag = -a * b / (1. + b * b);
79 mEquivCond(freq, 0) = {equivCondReal, equivCondImag};
80 Real preCurrFracReal = (1. - b * b) / (1. + b * b);
81 Real preCurrFracImag = (-2. * b) / (1. + b * b);
82 mPrevCurrFac(freq, 0) = {preCurrFracReal, preCurrFracImag};
83
84 // In steady-state, these variables should not change
85 mEquivCurrent(freq, 0) = mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
86 mPrevCurrFac(freq, 0) * (**mIntfCurrent)(0, freq);
87 (**mIntfCurrent)(0, freq) =
88 mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
89 mEquivCurrent(freq, 0);
90 }
91}
92
94 Attribute<Matrix>::Ptr leftVector) {
96 initVars(timeStep);
97
98 SPDLOG_LOGGER_INFO(mSLog,
99 "\n--- MNA initialization ---"
100 "\nInitial voltage {:s}"
101 "\nInitial current {:s}"
102 "\nEquiv. current {:s}"
103 "\n--- MNA initialization finished ---",
107}
108
110 Real omega, Real timeStep,
111 std::vector<Attribute<Matrix>::Ptr> leftVectors) {
113
114 initVars(timeStep);
115
116 mMnaTasks.push_back(std::make_shared<MnaPreStepHarm>(*this));
117 mMnaTasks.push_back(std::make_shared<MnaPostStepHarm>(*this, leftVectors));
118 **mRightVector = Matrix::Zero(leftVectors[0]->get().rows(), mNumFreqs);
119}
120
122 SparseMatrixRow &systemMatrix) {
123 for (UInt freq = 0; freq < mNumFreqs; freq++) {
125 mEquivCond(freq, 0), systemMatrix, matrixNodeIndex(0),
127 mSLog, mNumFreqs, freq);
128 }
129}
130
138
140 for (UInt freq = 0; freq < mNumFreqs; freq++) {
141 // Calculate equivalent current source for next time step
142 mEquivCurrent(freq, 0) = mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
143 mPrevCurrFac(freq, 0) * (**mIntfCurrent)(0, freq);
144
145 if (terminalNotGrounded(0))
147 mEquivCurrent(freq, 0), mNumFreqs, freq);
148 if (terminalNotGrounded(1))
150 -mEquivCurrent(freq, 0), mNumFreqs, freq);
151
152 SPDLOG_LOGGER_DEBUG(mSLog, "MNA EquivCurrent {:s}",
154 if (terminalNotGrounded(0))
155 SPDLOG_LOGGER_DEBUG(mSLog, "Add {:s} to source vector at {:d}",
157 matrixNodeIndex(0));
158 if (terminalNotGrounded(1))
159 SPDLOG_LOGGER_DEBUG(mSLog, "Add {:s} to source vector at {:d}",
161 matrixNodeIndex(1));
162 }
163}
164
166 Matrix &rightVector) {
167 for (UInt freq = 0; freq < mNumFreqs; freq++) {
168 // Calculate equivalent current source for next time step
169 mEquivCurrent(freq, 0) = mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
170 mPrevCurrFac(freq, 0) * (**mIntfCurrent)(0, freq);
171
172 if (terminalNotGrounded(0))
174 mEquivCurrent(freq, 0), 1, 0, freq);
175 if (terminalNotGrounded(1))
177 -mEquivCurrent(freq, 0), 1, 0, freq);
178 }
179}
180
182 AttributeBase::List &prevStepDependencies,
183 AttributeBase::List &attributeDependencies,
184 AttributeBase::List &modifiedAttributes) {
185 // actually depends on L, but then we'd have to modify the system matrix anyway
186 prevStepDependencies.push_back(mIntfVoltage);
187 prevStepDependencies.push_back(mIntfCurrent);
188 modifiedAttributes.push_back(mRightVector);
189}
190
191void DP::Ph1::Inductor::mnaCompPreStep(Real time, Int timeStepCount) {
193}
194
196 AttributeBase::List &prevStepDependencies,
197 AttributeBase::List &attributeDependencies,
198 AttributeBase::List &modifiedAttributes,
199 Attribute<Matrix>::Ptr &leftVector) {
200 attributeDependencies.push_back(leftVector);
201 modifiedAttributes.push_back(mIntfVoltage);
202 modifiedAttributes.push_back(mIntfCurrent);
203}
204
206 Attribute<Matrix>::Ptr &leftVector) {
207 this->mnaUpdateVoltage(**leftVector);
208 this->mnaUpdateCurrent(**leftVector);
209}
210
212 mInductor.mnaCompApplyRightSideVectorStampHarm(**mInductor.mRightVector);
213}
214
216 for (UInt freq = 0; freq < mInductor.mNumFreqs; freq++)
217 mInductor.mnaCompUpdateVoltageHarm(**mLeftVectors[freq], freq);
218 mInductor.mnaCompUpdateCurrentHarm();
219}
220
222 // v1 - v0
223 for (UInt freq = 0; freq < mNumFreqs; freq++) {
224 (**mIntfVoltage)(0, freq) = 0;
225 if (terminalNotGrounded(1))
226 (**mIntfVoltage)(0, freq) = Math::complexFromVectorElement(
227 leftVector, matrixNodeIndex(1), mNumFreqs, freq);
228 if (terminalNotGrounded(0))
229 (**mIntfVoltage)(0, freq) =
230 (**mIntfVoltage)(0, freq) -
232 mNumFreqs, freq);
233
234 SPDLOG_LOGGER_DEBUG(mSLog, "Voltage {:s}",
236 }
237}
238
240 Int freqIdx) {
241 // v1 - v0
242 (**mIntfVoltage)(0, freqIdx) = 0;
243 if (terminalNotGrounded(1))
244 (**mIntfVoltage)(0, freqIdx) =
246 if (terminalNotGrounded(0))
247 (**mIntfVoltage)(0, freqIdx) =
248 (**mIntfVoltage)(0, freqIdx) -
250
251 SPDLOG_LOGGER_DEBUG(mSLog, "Voltage {:s}",
252 Logger::phasorToString((**mIntfVoltage)(0, freqIdx)));
253}
254
256 for (UInt freq = 0; freq < mNumFreqs; freq++) {
257 (**mIntfCurrent)(0, freq) =
258 mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
259 mEquivCurrent(freq, 0);
260 SPDLOG_LOGGER_DEBUG(mSLog, "Current {:s}",
262 }
263}
264
266 for (UInt freq = 0; freq < mNumFreqs; freq++) {
267 (**mIntfCurrent)(0, freq) =
268 mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
269 mEquivCurrent(freq, 0);
270 SPDLOG_LOGGER_DEBUG(mSLog, "Current {:s}",
272 }
273}
274
275// #### Tear Methods ####
277 initVars(timeStep);
278}
279
284
286 mEquivCurrent(0, 0) = mEquivCond(0, 0) * (**mIntfVoltage)(0, 0) +
287 mPrevCurrFac(0, 0) * (**mIntfCurrent)(0, 0);
288 Math::addToVectorElement(voltageVector, mTearIdx,
289 mEquivCurrent(0, 0) / mEquivCond(0, 0));
290}
291
293 (**mIntfVoltage)(0, 0) = voltage;
294 (**mIntfCurrent)(0, 0) = mEquivCond(0, 0) * voltage + mEquivCurrent(0, 0);
295}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
void setParameters(Real inductance)
Sets model specific parameters.
const CPS::Attribute< Real >::Ptr mInductance
Inductance [H].
void execute(Real time, Int timeStepCount)
void execute(Real time, Int timeStepCount)
void mnaTearApplyVoltageStamp(Matrix &voltageVector) override
MatrixComp mEquivCond
Equivalent conductance for harmonics [S].
void mnaTearApplyMatrixStamp(SparseMatrixRow &tearMatrix) override
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Update interface current from MNA system results.
void mnaCompInitializeHarm(Real omega, Real timeStep, std::vector< Attribute< Matrix >::Ptr > leftVectors) override
void updateInductance(Real inductance, Real timeStep)
Change inductance and refresh trapezoidal companion coefficients.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
void mnaCompApplySystemMatrixStampHarm(SparseMatrixRow &systemMatrix, Int freqIdx) override
void mnaCompUpdateVoltageHarm(const Matrix &leftVector, Int freqIdx)
void initVars(Real timeStep)
void mnaTearInitialize(Real omega, Real timestep) override
SimPowerComp< Complex >::Ptr clone(String name) override
Return new instance with the same parameters.
MatrixComp mEquivCurrent
DC equivalent current source for harmonics [A].
void mnaTearPostStep(Complex voltage, Complex current) override
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
Initializes MNA specific variables.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Update interface voltage from MNA system results.
Complex getMNAConductance() const
Return single-frequency MNA companion conductance.
void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
MatrixComp mPrevCurrFac
Coefficient in front of previous current value for harmonics.
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes states from power flow data.
void initialize(Matrix frequencies) override
Initializes state variables considering the number of frequencies.
void mnaCompApplyRightSideVectorStampHarm(Matrix &rightVector) override
void mnaCompPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamps system matrix.
Inductor(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and log level.
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
void mnaApplyRightSideVectorStamp(Matrix &rightVector) final
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 setVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0, Matrix::Index colOffset=0)
static void addToVectorElement(Matrix &mat, Matrix::Index row, Complex value, 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
virtual void initialize(Matrix frequencies)
Initialize components with correct network frequencies.
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
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