DPsim
Loading...
Searching...
No Matches
DP_Ph1_Capacitor.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;
12using namespace CPS::DP::Ph1;
13
15 : MNASimPowerComp<Complex>(uid, name, true, true, logLevel),
17 mEquivCurrent = {0, 0};
18 **mIntfVoltage = MatrixComp::Zero(1, 1);
19 **mIntfCurrent = MatrixComp::Zero(1, 1);
21}
22
24 auto copy = Capacitor::make(name, mLogLevel);
25 copy->setParameters(**mCapacitance);
26 return copy;
27}
28
31
32 mEquivCurrent = MatrixComp::Zero(mNumFreqs, 1);
33 mEquivCond = MatrixComp::Zero(mNumFreqs, 1);
34 mPrevVoltCoeff = MatrixComp::Zero(mNumFreqs, 1);
35}
36
40
42
43 Real omega = 2 * PI * frequency;
44 Complex impedance = {0, -1. / (omega * **mCapacitance)};
45 (**mIntfVoltage)(0, 0) = initialSingleVoltage(1) - initialSingleVoltage(0);
46
47 // Admittance form (I = V*jwC): a zero capacitance gives 0 current, not a NaN from 1/(omega*C).
48 (**mIntfCurrent)(0, 0) =
49 (**mIntfVoltage)(0, 0) * Complex(0, omega * **mCapacitance);
50
51 SPDLOG_LOGGER_INFO(mSLog,
52 "\nCapacitance [F]: {:s}"
53 "\nImpedance [Ohm]: {:s}",
55 Logger::complexToString(impedance));
56 SPDLOG_LOGGER_INFO(mSLog,
57 "\n--- Initialization from powerflow ---"
58 "\nVoltage across: {:s}"
59 "\nCurrent: {:s}"
60 "\nTerminal 0 voltage: {:s}"
61 "\nTerminal 1 voltage: {:s}"
62 "\n--- Initialization from powerflow finished ---",
67 mSLog->flush();
68}
69
71 Attribute<Matrix>::Ptr leftVector) {
73
74 Real equivCondReal = 2.0 * **mCapacitance / timeStep;
75 Real prevVoltCoeffReal = 2.0 * **mCapacitance / timeStep;
76
77 for (UInt freq = 0; freq < mNumFreqs; freq++) {
78 Real equivCondImag = 2. * PI * mFrequencies(freq, 0) * **mCapacitance;
79 mEquivCond(freq, 0) = {equivCondReal, equivCondImag};
80 Real prevVoltCoeffImag = -2. * PI * mFrequencies(freq, 0) * **mCapacitance;
81 mPrevVoltCoeff(freq, 0) = {prevVoltCoeffReal, prevVoltCoeffImag};
82
83 mEquivCurrent(freq, 0) =
84 -(**mIntfCurrent)(0, freq) +
85 -mPrevVoltCoeff(freq, 0) * (**mIntfVoltage)(0, freq);
86 (**mIntfCurrent)(0, freq) =
87 mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
88 mEquivCurrent(freq, 0);
89 }
90
91 SPDLOG_LOGGER_INFO(mSLog,
92 "\n--- MNA initialization ---"
93 "\nInitial voltage {:s}"
94 "\nInitial current {:s}"
95 "\nEquiv. current {:s}"
96 "\n--- MNA initialization finished ---",
100 mSLog->flush();
101}
102
104 Real omega, Real timeStep,
105 std::vector<Attribute<Matrix>::Ptr> leftVectors) {
107
108 Real equivCondReal = 2.0 * **mCapacitance / timeStep;
109 Real prevVoltCoeffReal = 2.0 * **mCapacitance / timeStep;
110
111 for (UInt freq = 0; freq < mNumFreqs; freq++) {
112 Real equivCondImag = 2. * PI * mFrequencies(freq, 0) * **mCapacitance;
113 mEquivCond(freq, 0) = {equivCondReal, equivCondImag};
114 Real prevVoltCoeffImag = -2. * PI * mFrequencies(freq, 0) * **mCapacitance;
115 mPrevVoltCoeff(freq, 0) = {prevVoltCoeffReal, prevVoltCoeffImag};
116
117 mEquivCurrent(freq, 0) =
118 -(**mIntfCurrent)(0, freq) +
119 -mPrevVoltCoeff(freq, 0) * (**mIntfVoltage)(0, freq);
120 (**mIntfCurrent)(0, freq) =
121 mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
122 mEquivCurrent(freq, 0);
123 }
124
125 mMnaTasks.push_back(std::make_shared<MnaPreStepHarm>(*this));
126 mMnaTasks.push_back(std::make_shared<MnaPostStepHarm>(*this, leftVectors));
127 **mRightVector = Matrix::Zero(leftVectors[0]->get().rows(), mNumFreqs);
128}
129
131 SparseMatrixRow &systemMatrix) {
132 for (UInt freq = 0; freq < mNumFreqs; freq++) {
134 mEquivCond(freq, 0), systemMatrix, matrixNodeIndex(0),
136 mSLog, mNumFreqs, freq);
137 }
138}
139
147
149 for (UInt freq = 0; freq < mNumFreqs; freq++) {
150 //mCureqr = mCurrr + mGcr * mDeltavr + mGci * mDeltavi;
151 //mCureqi = mCurri + mGcr * mDeltavi - mGci * mDeltavr;
152 mEquivCurrent(freq, 0) =
153 -(**mIntfCurrent)(0, freq) +
154 -mPrevVoltCoeff(freq, 0) * (**mIntfVoltage)(0, freq);
155
156 if (terminalNotGrounded(0))
158 mEquivCurrent(freq, 0), mNumFreqs, freq);
159 if (terminalNotGrounded(1))
161 -mEquivCurrent(freq, 0), mNumFreqs, freq);
162
163 SPDLOG_LOGGER_DEBUG(mSLog, "MNA EquivCurrent {:f}+j{:f}",
164 mEquivCurrent(freq, 0).real(),
165 mEquivCurrent(freq, 0).imag());
166 if (terminalNotGrounded(0))
167 SPDLOG_LOGGER_DEBUG(mSLog, "Add {:f}+j{:f} to source vector at {:d}",
168 mEquivCurrent(freq, 0).real(),
169 mEquivCurrent(freq, 0).imag(), matrixNodeIndex(0));
170 if (terminalNotGrounded(1))
171 SPDLOG_LOGGER_DEBUG(mSLog, "Add {:f}+j{:f} to source vector at {:d}",
172 -mEquivCurrent(freq, 0).real(),
173 -mEquivCurrent(freq, 0).imag(), matrixNodeIndex(1));
174 }
175}
176
178 Matrix &rightVector) {
179 for (UInt freq = 0; freq < mNumFreqs; freq++) {
180 //mCureqr = mCurrr + mGcr * mDeltavr + mGci * mDeltavi;
181 //mCureqi = mCurri + mGcr * mDeltavi - mGci * mDeltavr;
182 mEquivCurrent(freq, 0) =
183 -(**mIntfCurrent)(0, freq) +
184 -mPrevVoltCoeff(freq, 0) * (**mIntfVoltage)(0, freq);
185
186 if (terminalNotGrounded(0))
188 mEquivCurrent(freq, 0), 1, 0, freq);
189 if (terminalNotGrounded(1))
191 -mEquivCurrent(freq, 0), 1, 0, freq);
192 }
193}
194
196 Matrix &rightVector, Int freq) {
197 //mCureqr = mCurrr + mGcr * mDeltavr + mGci * mDeltavi;
198 //mCureqi = mCurri + mGcr * mDeltavi - mGci * mDeltavr;
199 mEquivCurrent(freq, 0) = -(**mIntfCurrent)(0, freq) +
200 -mPrevVoltCoeff(freq, 0) * (**mIntfVoltage)(0, freq);
201
202 if (terminalNotGrounded(0))
204 mEquivCurrent(freq, 0));
205 if (terminalNotGrounded(1))
207 -mEquivCurrent(freq, 0));
208}
209
211 AttributeBase::List &prevStepDependencies,
212 AttributeBase::List &attributeDependencies,
213 AttributeBase::List &modifiedAttributes) {
214 // actually depends on C, but then we'd have to modify the system matrix anyway
215 prevStepDependencies.push_back(mIntfCurrent);
216 prevStepDependencies.push_back(mIntfVoltage);
217 modifiedAttributes.push_back(mRightVector);
218}
219
220void DP::Ph1::Capacitor::mnaCompPreStep(Real time, Int timeStepCount) {
222}
223
225 AttributeBase::List &prevStepDependencies,
226 AttributeBase::List &attributeDependencies,
227 AttributeBase::List &modifiedAttributes,
228 Attribute<Matrix>::Ptr &leftVector) {
229 attributeDependencies.push_back(leftVector);
230 modifiedAttributes.push_back(mIntfVoltage);
231 modifiedAttributes.push_back(mIntfCurrent);
232}
233
235 Attribute<Matrix>::Ptr &leftVector) {
236 this->mnaUpdateVoltage(**leftVector);
237 this->mnaUpdateCurrent(**leftVector);
238}
239
241 mCapacitor.mnaCompApplyRightSideVectorStampHarm(**mCapacitor.mRightVector);
242}
243
245 Int timeStepCount) {
246 for (UInt freq = 0; freq < mCapacitor.mNumFreqs; freq++)
247 mCapacitor.mnaCompUpdateVoltageHarm(**mLeftVectors[freq], freq);
248 mCapacitor.mnaCompUpdateCurrentHarm();
249}
250
252 // v1 - v0
253 for (UInt freq = 0; freq < mNumFreqs; freq++) {
254 (**mIntfVoltage)(0, freq) = 0;
255 if (terminalNotGrounded(1))
256 (**mIntfVoltage)(0, freq) = Math::complexFromVectorElement(
257 leftVector, matrixNodeIndex(1), mNumFreqs, freq);
258 if (terminalNotGrounded(0))
259 (**mIntfVoltage)(0, freq) =
260 (**mIntfVoltage)(0, freq) -
262 mNumFreqs, freq);
263
264 SPDLOG_LOGGER_DEBUG(mSLog, "Voltage {:e}<{:e}",
265 std::abs((**mIntfVoltage)(0, freq)),
266 std::arg((**mIntfVoltage)(0, freq)));
267 }
268}
269
271 Int freqIdx) {
272 // v1 - v0
273 (**mIntfVoltage)(0, freqIdx) = 0;
274 if (terminalNotGrounded(1))
275 (**mIntfVoltage)(0, freqIdx) =
277 if (terminalNotGrounded(0))
278 (**mIntfVoltage)(0, freqIdx) =
279 (**mIntfVoltage)(0, freqIdx) -
281
282 SPDLOG_LOGGER_DEBUG(mSLog, "Voltage {:s}",
283 Logger::phasorToString((**mIntfVoltage)(0, freqIdx)));
284}
285
287 for (UInt freq = 0; freq < mNumFreqs; freq++) {
288 (**mIntfCurrent)(0, freq) =
289 mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
290 mEquivCurrent(freq, 0);
291 SPDLOG_LOGGER_DEBUG(mSLog, "Current {:s}",
293 }
294}
295
297 for (UInt freq = 0; freq < mNumFreqs; freq++) {
298 (**mIntfCurrent)(0, freq) =
299 mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
300 mEquivCurrent(freq, 0);
301 SPDLOG_LOGGER_DEBUG(mSLog, "Current {:s}",
303 }
304}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:250
const CPS::Attribute< Real >::Ptr mCapacitance
Capacitance [F].
void execute(Real time, Int timeStepCount)
void execute(Real time, Int timeStepCount)
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamps system matrix.
void mnaCompUpdateVoltageHarm(const Matrix &leftVector, Int freqIdx)
Capacitor(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
void mnaCompPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
void mnaCompApplySystemMatrixStampHarm(SparseMatrixRow &systemMatrix, Int freqIdx) override
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
MatrixComp mPrevVoltCoeff
Coefficient in front of previous voltage value for harmonics.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
void initialize(Matrix frequencies) override
Initialize components with correct network frequencies.
MatrixComp mEquivCurrent
DC equivalent current source for harmonics [A].
void mnaCompInitializeHarm(Real omega, Real timeStep, std::vector< Attribute< Matrix >::Ptr > leftVector) override
void mnaCompApplyRightSideVectorStampHarm(Matrix &rightVector) override
Complex getMNAConductance() const
Return single-frequency MNA companion conductance.
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
Initializes internal variables of the component.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Update interface voltage from MNA system result.
MatrixComp mEquivCond
Equivalent conductance for harmonics [S].
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Update interface current from MNA system result.
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
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 mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
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)
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
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< Capacitor > 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
unsigned int UInt
Definition Definitions.h:60
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:74