DPsim
Loading...
Searching...
No Matches
DP_Ph1_ResIndSeries.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 Logger::Level logLevel)
15 : MNASimPowerComp<Complex>(uid, name, true, true, logLevel),
16 mInductance(mAttributes->create<Real>("L")),
18 mResistance(mAttributes->create<Real>("R")) {
19 mEquivCurrent = {0, 0};
20 **mIntfVoltage = MatrixComp::Zero(1, 1);
21 **mIntfCurrent = MatrixComp::Zero(1, 1);
23}
24
27 auto copy = ResIndSeries::make(name, mLogLevel);
28 copy->setParameters(**mResistance, **mInductance);
29 return copy;
30}
31
32void DP::Ph1::ResIndSeries::setParameters(Real resistance, Real inductance) {
33 **mResistance = resistance;
34 **mInductance = inductance;
35}
36
39
40 mEquivCurrent = MatrixComp::Zero(mNumFreqs, 1);
41 mEquivCond = MatrixComp::Zero(mNumFreqs, 1);
42 mPrevCurrFac = MatrixComp::Zero(mNumFreqs, 1);
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 "\n--- Initialization from powerflow ---"
54 "\nVoltage across: {:s}"
55 "\nCurrent: {:s}"
56 "\nTerminal 0 voltage: {:s}"
57 "\nTerminal 1 voltage: {:s}"
58 "\n--- Initialization from powerflow finished ---",
63}
64
65// #### MNA functions ####
66
67void DP::Ph1::ResIndSeries::initVars(Real timeStep) {
68 for (Int freq = 0; freq < mNumFreqs; freq++) {
69 Real a = timeStep / (2. * **mInductance);
70 Real b = timeStep * 2. * PI * mFrequencies(freq, 0) / 2.;
71
72 Real equivCondReal = a / (1. + b * b);
73 Real equivCondImag = -a * b / (1. + b * b);
74 mEquivCond(freq, 0) = {equivCondReal, equivCondImag};
75 Real preCurrFracReal = (1. - b * b) / (1. + b * b);
76 Real preCurrFracImag = (-2. * b) / (1. + b * b);
77 mPrevCurrFac(freq, 0) = {preCurrFracReal, preCurrFracImag};
78
79 // TODO: check if this is correct or if it should be only computed before the step
80 mEquivCurrent(freq, 0) = mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
81 mPrevCurrFac(freq, 0) * (**mIntfCurrent)(0, freq);
82 (**mIntfCurrent)(0, freq) =
83 mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
84 mEquivCurrent(freq, 0);
85 }
86}
87
89 Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
91 initVars(timeStep);
92
93 SPDLOG_LOGGER_INFO(mSLog,
94 "\n--- MNA initialization ---"
95 "\nInitial voltage {:s}"
96 "\nInitial current {:s}"
97 "\nEquiv. current {:s}"
98 "\n--- MNA initialization finished ---",
102}
103
105 Real omega, Real timeStep,
106 std::vector<Attribute<Matrix>::Ptr> leftVectors) {
107 updateMatrixNodeIndices();
108
109 initVars(timeStep);
110
111 mMnaTasks.push_back(std::make_shared<MnaPreStepHarm>(*this));
112 mMnaTasks.push_back(std::make_shared<MnaPostStepHarm>(*this, leftVectors));
113 **mRightVector = Matrix::Zero(leftVectors[0]->get().rows(), mNumFreqs);
114}
115
117 SparseMatrixRow &systemMatrix) {
118 for (Int freq = 0; freq < mNumFreqs; freq++) {
119 if (terminalNotGrounded(0))
121 matrixNodeIndex(0), mEquivCond(freq, 0),
122 mNumFreqs, freq);
123 if (terminalNotGrounded(1))
125 matrixNodeIndex(1), mEquivCond(freq, 0),
126 mNumFreqs, freq);
129 matrixNodeIndex(1), -mEquivCond(freq, 0),
130 mNumFreqs, freq);
132 matrixNodeIndex(0), -mEquivCond(freq, 0),
133 mNumFreqs, freq);
134 }
135
136 SPDLOG_LOGGER_INFO(mSLog, "-- Stamp frequency {:d} ---", freq);
137 if (terminalNotGrounded(0))
138 SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
141 if (terminalNotGrounded(1))
142 SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
146 SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
149 SPDLOG_LOGGER_INFO(mSLog, "Add {:s} to system at ({:d},{:d})",
152 }
153 }
154}
155
157 SparseMatrixRow &systemMatrix, Int freqIdx) {
158 if (terminalNotGrounded(0))
159 Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0),
160 matrixNodeIndex(0), mEquivCond(freqIdx, 0));
161 if (terminalNotGrounded(1))
162 Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1),
163 matrixNodeIndex(1), mEquivCond(freqIdx, 0));
164 if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
165 Math::addToMatrixElement(systemMatrix, matrixNodeIndex(0),
166 matrixNodeIndex(1), -mEquivCond(freqIdx, 0));
167 Math::addToMatrixElement(systemMatrix, matrixNodeIndex(1),
168 matrixNodeIndex(0), -mEquivCond(freqIdx, 0));
169 }
170
171 SPDLOG_LOGGER_INFO(mSLog, "-- Stamp frequency {:d} ---", freqIdx);
172 if (terminalNotGrounded(0))
173 SPDLOG_LOGGER_INFO(mSLog, "Add {:f}+j{:f} to system at ({:d},{:d})",
174 mEquivCond(freqIdx, 0).real(),
175 mEquivCond(freqIdx, 0).imag(), matrixNodeIndex(0),
176 matrixNodeIndex(0));
177 if (terminalNotGrounded(1))
178 SPDLOG_LOGGER_INFO(mSLog, "Add {:f}+j{:f} to system at ({:d},{:d})",
179 mEquivCond(freqIdx, 0).real(),
180 mEquivCond(freqIdx, 0).imag(), matrixNodeIndex(1),
181 matrixNodeIndex(1));
182 if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
183 SPDLOG_LOGGER_INFO(mSLog, "Add {:f}+j{:f} to system at ({:d},{:d})",
184 -mEquivCond(freqIdx, 0).real(),
185 -mEquivCond(freqIdx, 0).imag(), matrixNodeIndex(0),
186 matrixNodeIndex(1));
187 SPDLOG_LOGGER_INFO(mSLog, "Add {:f}+j{:f} to system at ({:d},{:d})",
188 -mEquivCond(freqIdx, 0).real(),
189 -mEquivCond(freqIdx, 0).imag(), matrixNodeIndex(1),
190 matrixNodeIndex(0));
191 }
192}
193
195 Matrix &rightVector) {
196 for (Int freq = 0; freq < mNumFreqs; freq++) {
197 // Calculate equivalent current source for next time step
198 mEquivCurrent(freq, 0) = mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
199 mPrevCurrFac(freq, 0) * (**mIntfCurrent)(0, freq);
200
201 if (terminalNotGrounded(0))
203 mEquivCurrent(freq, 0), mNumFreqs, freq);
204 if (terminalNotGrounded(1))
206 -mEquivCurrent(freq, 0), mNumFreqs, freq);
207
208 SPDLOG_LOGGER_DEBUG(mSLog, "MNA EquivCurrent {:s}",
210 if (terminalNotGrounded(0))
211 SPDLOG_LOGGER_DEBUG(mSLog, "Add {:s} to source vector at {:d}",
213 matrixNodeIndex(0));
214 if (terminalNotGrounded(1))
215 SPDLOG_LOGGER_DEBUG(mSLog, "Add {:s} to source vector at {:d}",
217 matrixNodeIndex(1));
218 }
219}
220
222 Matrix &rightVector) {
223 for (Int freq = 0; freq < mNumFreqs; freq++) {
224 // Calculate equivalent current source for next time step
225 mEquivCurrent(freq, 0) = mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
226 mPrevCurrFac(freq, 0) * (**mIntfCurrent)(0, freq);
227
228 if (terminalNotGrounded(0))
229 Math::setVectorElement(rightVector, matrixNodeIndex(0),
230 mEquivCurrent(freq, 0), 1, 0, freq);
231 if (terminalNotGrounded(1))
232 Math::setVectorElement(rightVector, matrixNodeIndex(1),
233 -mEquivCurrent(freq, 0), 1, 0, freq);
234 }
235}
236
238 AttributeBase::List &attributeDependencies,
239 AttributeBase::List &modifiedAttributes) {
240 // actually depends on L, but then we'd have to modify the system matrix anyway
241 modifiedAttributes.push_back(mRightVector);
242 prevStepDependencies.push_back(mIntfVoltage);
243 prevStepDependencies.push_back(mIntfCurrent);
244}
245
247 mResIndSeries.mnaCompApplyRightSideVectorStamp(**mRightVector);
248}
249
250void DP::Ph1::ResIndSeries::MnaPreStepHarm::execute(Real time,
251 Int timeStepCount) {
252 mResIndSeries.mnaCompApplyRightSideVectorStampHarm(
253 **mResIndSeries.mRightVector);
254}
255
257 AttributeBase::List &attributeDependencies,
258 AttributeBase::List &modifiedAttributes,
259 Attribute<Matrix>::Ptr &leftVector) {
260 attributeDependencies.push_back(leftVector);
261 modifiedAttributes.push_back(mIntfVoltage);
262 modifiedAttributes.push_back(mIntfCurrent);
263}
264
266 Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
267 mnaCompUpdateVoltage(**leftVector);
268 mnaCompUpdateCurrent(**leftVector);
269}
270
271void DP::Ph1::ResIndSeries::MnaPostStepHarm::execute(Real time,
272 Int timeStepCount) {
273 for (Int freq = 0; freq < mResIndSeries.mNumFreqs; freq++)
274 mResIndSeries.mnaCompUpdateVoltageHarm(**mLeftVectors[freq], freq);
275 mResIndSeries.mnaCompUpdateCurrentHarm();
276}
277
279 // v1 - v0
280 for (Int freq = 0; freq < mNumFreqs; freq++) {
281 (**mIntfVoltage)(0, freq) = 0;
282 if (terminalNotGrounded(1))
283 (**mIntfVoltage)(0, freq) = Math::complexFromVectorElement(
284 leftVector, matrixNodeIndex(1), mNumFreqs, freq);
285 if (terminalNotGrounded(0))
286 (**mIntfVoltage)(0, freq) =
287 (**mIntfVoltage)(0, freq) -
289 mNumFreqs, freq);
290
291 SPDLOG_LOGGER_DEBUG(mSLog, "Voltage {:s}",
293 }
294}
295
296void DP::Ph1::ResIndSeries::mnaCompUpdateVoltageHarm(const Matrix &leftVector,
297 Int freqIdx) {
298 // v1 - v0
299 (**mIntfVoltage)(0, freqIdx) = 0;
300 if (terminalNotGrounded(1))
301 (**mIntfVoltage)(0, freqIdx) =
302 Math::complexFromVectorElement(leftVector, matrixNodeIndex(1));
303 if (terminalNotGrounded(0))
304 (**mIntfVoltage)(0, freqIdx) =
305 (**mIntfVoltage)(0, freqIdx) -
306 Math::complexFromVectorElement(leftVector, matrixNodeIndex(0));
307
308 SPDLOG_LOGGER_DEBUG(mSLog, "Voltage {:s}",
309 Logger::phasorToString((**mIntfVoltage)(0, freqIdx)));
310}
311
313 for (Int freq = 0; freq < mNumFreqs; freq++) {
314 (**mIntfCurrent)(0, freq) =
315 mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
316 mEquivCurrent(freq, 0);
317 SPDLOG_LOGGER_DEBUG(mSLog, "Current {:s}",
319 }
320}
321
322void DP::Ph1::ResIndSeries::mnaCompUpdateCurrentHarm() {
323 for (Int freq = 0; freq < mNumFreqs; freq++) {
324 (**mIntfCurrent)(0, freq) =
325 mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
326 mEquivCurrent(freq, 0);
327 SPDLOG_LOGGER_DEBUG(mSLog, "Current {:s}",
328 Logger::phasorToString((**mIntfCurrent)(0, freq)));
329 }
330}
331
332// #### Tear Methods ####
334 initVars(timeStep);
335}
336
338 SparseMatrixRow &tearMatrix) {
339 Math::addToMatrixElement(tearMatrix, mTearIdx, mTearIdx,
340 1. / mEquivCond(0, 0));
341}
342
344 mEquivCurrent(0, 0) = mEquivCond(0, 0) * (**mIntfVoltage)(0, 0) +
345 mPrevCurrFac(0, 0) * (**mIntfCurrent)(0, 0);
346 Math::addToVectorElement(voltageVector, mTearIdx,
347 mEquivCurrent(0, 0) / mEquivCond(0, 0));
348}
349
351 (**mIntfVoltage)(0, 0) = voltage;
352 (**mIntfCurrent)(0, 0) = mEquivCond(0, 0) * voltage + mEquivCurrent(0, 0);
353}
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes)
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector)
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:250
const Attribute< Real >::Ptr mInductance
Inductance [H].
void initialize(Matrix frequencies)
Initializes state variables considering the number of frequencies.
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector)
Initializes MNA specific variables.
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector)
Stamps right side (source) vector.
void mnaCompPreStep(Real time, Int timeStepCount) override
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix)
Stamps system matrix.
void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
void initializeFromNodesAndTerminals(Real frequency)
Initializes states from power flow data.
const Attribute< Real >::Ptr mResistance
Resistance [ohm].
MatrixComp mEquivCurrent
DC equivalent current source for harmonics [A].
void mnaCompUpdateVoltage(const Matrix &leftVector)
Update interface voltage from MNA system results.
void setParameters(Real resistance, Real inductance)
Sets model specific parameters.
ResIndSeries(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and log level.
SimPowerComp< Complex >::Ptr clone(String name)
Return new instance with the same parameters.
MatrixComp mPrevCurrFac
Coefficient in front of previous current value for harmonics.
MatrixComp mEquivCond
Equivalent conductance for harmonics [S].
void mnaCompUpdateCurrent()
Update interface current from MNA system results.
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 phasorToString(const Complex &num)
Definition Logger.cpp:57
virtual void mnaCompInitializeHarm(Real omega, Real timeStep, std::vector< Attribute< Matrix >::Ptr > leftVector)
virtual void mnaCompApplyRightSideVectorStampHarm(Matrix &sourceVector)
MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
Attribute< Matrix >::Ptr mRightVector
virtual void mnaCompApplySystemMatrixStampHarm(SparseMatrixRow &systemMatrix, Int freqIdx)
virtual void mnaTearInitialize(Real omega, Real timeStep)
virtual void mnaTearApplyMatrixStamp(SparseMatrixRow &tearMatrix)=0
virtual void mnaTearPostStep(Complex voltage, Complex current)
virtual void mnaTearApplyVoltageStamp(Matrix &currentVector)
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 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< ResIndSeries > 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: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::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:74