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
41void DP::Ph1::Capacitor::updateCapacitance(Real capacitance, Real timeStep) {
42 setParameters(capacitance);
43 const Real equivCondReal = 2. * capacitance / timeStep;
44 const Real prevVoltCoeffReal = equivCondReal;
45 for (UInt freq = 0; freq < mNumFreqs; ++freq) {
46 const Real harmonicOmega = 2. * PI * mFrequencies(freq, 0);
47 mEquivCond(freq, 0) = Complex(equivCondReal, harmonicOmega * capacitance);
48 mPrevVoltCoeff(freq, 0) =
49 Complex(prevVoltCoeffReal, -harmonicOmega * capacitance);
50 mEquivCurrent(freq, 0) =
51 -(**mIntfCurrent)(0, freq) -
52 mPrevVoltCoeff(freq, 0) * (**mIntfVoltage)(0, freq);
53 }
54}
55
57
58 Real omega = 2 * PI * frequency;
59 Complex impedance = {0, -1. / (omega * **mCapacitance)};
60 (**mIntfVoltage)(0, 0) = initialSingleVoltage(1) - initialSingleVoltage(0);
61
62 // Admittance form (I = V*jwC): a zero capacitance gives 0 current, not a NaN from 1/(omega*C).
63 (**mIntfCurrent)(0, 0) =
64 (**mIntfVoltage)(0, 0) * Complex(0, omega * **mCapacitance);
65
66 SPDLOG_LOGGER_INFO(mSLog,
67 "\nCapacitance [F]: {:s}"
68 "\nImpedance [Ohm]: {:s}",
70 Logger::complexToString(impedance));
71 SPDLOG_LOGGER_INFO(mSLog,
72 "\n--- Initialization from powerflow ---"
73 "\nVoltage across: {:s}"
74 "\nCurrent: {:s}"
75 "\nTerminal 0 voltage: {:s}"
76 "\nTerminal 1 voltage: {:s}"
77 "\n--- Initialization from powerflow finished ---",
82 mSLog->flush();
83}
84
86 Attribute<Matrix>::Ptr leftVector) {
88
89 Real equivCondReal = 2.0 * **mCapacitance / timeStep;
90 Real prevVoltCoeffReal = 2.0 * **mCapacitance / timeStep;
91
92 for (UInt freq = 0; freq < mNumFreqs; freq++) {
93 Real equivCondImag = 2. * PI * mFrequencies(freq, 0) * **mCapacitance;
94 mEquivCond(freq, 0) = {equivCondReal, equivCondImag};
95 Real prevVoltCoeffImag = -2. * PI * mFrequencies(freq, 0) * **mCapacitance;
96 mPrevVoltCoeff(freq, 0) = {prevVoltCoeffReal, prevVoltCoeffImag};
97
98 mEquivCurrent(freq, 0) =
99 -(**mIntfCurrent)(0, freq) +
100 -mPrevVoltCoeff(freq, 0) * (**mIntfVoltage)(0, freq);
101 (**mIntfCurrent)(0, freq) =
102 mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
103 mEquivCurrent(freq, 0);
104 }
105
106 SPDLOG_LOGGER_INFO(mSLog,
107 "\n--- MNA initialization ---"
108 "\nInitial voltage {:s}"
109 "\nInitial current {:s}"
110 "\nEquiv. current {:s}"
111 "\n--- MNA initialization finished ---",
115 mSLog->flush();
116}
117
119 Real omega, Real timeStep,
120 std::vector<Attribute<Matrix>::Ptr> leftVectors) {
122
123 Real equivCondReal = 2.0 * **mCapacitance / timeStep;
124 Real prevVoltCoeffReal = 2.0 * **mCapacitance / timeStep;
125
126 for (UInt freq = 0; freq < mNumFreqs; freq++) {
127 Real equivCondImag = 2. * PI * mFrequencies(freq, 0) * **mCapacitance;
128 mEquivCond(freq, 0) = {equivCondReal, equivCondImag};
129 Real prevVoltCoeffImag = -2. * PI * mFrequencies(freq, 0) * **mCapacitance;
130 mPrevVoltCoeff(freq, 0) = {prevVoltCoeffReal, prevVoltCoeffImag};
131
132 mEquivCurrent(freq, 0) =
133 -(**mIntfCurrent)(0, freq) +
134 -mPrevVoltCoeff(freq, 0) * (**mIntfVoltage)(0, freq);
135 (**mIntfCurrent)(0, freq) =
136 mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
137 mEquivCurrent(freq, 0);
138 }
139
140 mMnaTasks.push_back(std::make_shared<MnaPreStepHarm>(*this));
141 mMnaTasks.push_back(std::make_shared<MnaPostStepHarm>(*this, leftVectors));
142 **mRightVector = Matrix::Zero(leftVectors[0]->get().rows(), mNumFreqs);
143}
144
146 SparseMatrixRow &systemMatrix) {
147 for (UInt freq = 0; freq < mNumFreqs; freq++) {
149 mEquivCond(freq, 0), systemMatrix, matrixNodeIndex(0),
151 mSLog, mNumFreqs, freq);
152 }
153}
154
162
164 for (UInt freq = 0; freq < mNumFreqs; freq++) {
165 //mCureqr = mCurrr + mGcr * mDeltavr + mGci * mDeltavi;
166 //mCureqi = mCurri + mGcr * mDeltavi - mGci * mDeltavr;
167 mEquivCurrent(freq, 0) =
168 -(**mIntfCurrent)(0, freq) +
169 -mPrevVoltCoeff(freq, 0) * (**mIntfVoltage)(0, freq);
170
171 if (terminalNotGrounded(0))
173 mEquivCurrent(freq, 0), mNumFreqs, freq);
174 if (terminalNotGrounded(1))
176 -mEquivCurrent(freq, 0), mNumFreqs, freq);
177
178 SPDLOG_LOGGER_DEBUG(mSLog, "MNA EquivCurrent {:f}+j{:f}",
179 mEquivCurrent(freq, 0).real(),
180 mEquivCurrent(freq, 0).imag());
181 if (terminalNotGrounded(0))
182 SPDLOG_LOGGER_DEBUG(mSLog, "Add {:f}+j{:f} to source vector at {:d}",
183 mEquivCurrent(freq, 0).real(),
184 mEquivCurrent(freq, 0).imag(), matrixNodeIndex(0));
185 if (terminalNotGrounded(1))
186 SPDLOG_LOGGER_DEBUG(mSLog, "Add {:f}+j{:f} to source vector at {:d}",
187 -mEquivCurrent(freq, 0).real(),
188 -mEquivCurrent(freq, 0).imag(), matrixNodeIndex(1));
189 }
190}
191
193 Matrix &rightVector) {
194 for (UInt freq = 0; freq < mNumFreqs; freq++) {
195 //mCureqr = mCurrr + mGcr * mDeltavr + mGci * mDeltavi;
196 //mCureqi = mCurri + mGcr * mDeltavi - mGci * mDeltavr;
197 mEquivCurrent(freq, 0) =
198 -(**mIntfCurrent)(0, freq) +
199 -mPrevVoltCoeff(freq, 0) * (**mIntfVoltage)(0, freq);
200
201 if (terminalNotGrounded(0))
203 mEquivCurrent(freq, 0), 1, 0, freq);
204 if (terminalNotGrounded(1))
206 -mEquivCurrent(freq, 0), 1, 0, freq);
207 }
208}
209
211 Matrix &rightVector, Int freq) {
212 //mCureqr = mCurrr + mGcr * mDeltavr + mGci * mDeltavi;
213 //mCureqi = mCurri + mGcr * mDeltavi - mGci * mDeltavr;
214 mEquivCurrent(freq, 0) = -(**mIntfCurrent)(0, freq) +
215 -mPrevVoltCoeff(freq, 0) * (**mIntfVoltage)(0, freq);
216
217 if (terminalNotGrounded(0))
219 mEquivCurrent(freq, 0));
220 if (terminalNotGrounded(1))
222 -mEquivCurrent(freq, 0));
223}
224
226 AttributeBase::List &prevStepDependencies,
227 AttributeBase::List &attributeDependencies,
228 AttributeBase::List &modifiedAttributes) {
229 // actually depends on C, but then we'd have to modify the system matrix anyway
230 prevStepDependencies.push_back(mIntfCurrent);
231 prevStepDependencies.push_back(mIntfVoltage);
232 modifiedAttributes.push_back(mRightVector);
233}
234
235void DP::Ph1::Capacitor::mnaCompPreStep(Real time, Int timeStepCount) {
237}
238
240 AttributeBase::List &prevStepDependencies,
241 AttributeBase::List &attributeDependencies,
242 AttributeBase::List &modifiedAttributes,
243 Attribute<Matrix>::Ptr &leftVector) {
244 attributeDependencies.push_back(leftVector);
245 modifiedAttributes.push_back(mIntfVoltage);
246 modifiedAttributes.push_back(mIntfCurrent);
247}
248
250 Attribute<Matrix>::Ptr &leftVector) {
251 this->mnaUpdateVoltage(**leftVector);
252 this->mnaUpdateCurrent(**leftVector);
253}
254
256 mCapacitor.mnaCompApplyRightSideVectorStampHarm(**mCapacitor.mRightVector);
257}
258
260 Int timeStepCount) {
261 for (UInt freq = 0; freq < mCapacitor.mNumFreqs; freq++)
262 mCapacitor.mnaCompUpdateVoltageHarm(**mLeftVectors[freq], freq);
263 mCapacitor.mnaCompUpdateCurrentHarm();
264}
265
267 // v1 - v0
268 for (UInt freq = 0; freq < mNumFreqs; freq++) {
269 (**mIntfVoltage)(0, freq) = 0;
270 if (terminalNotGrounded(1))
271 (**mIntfVoltage)(0, freq) = Math::complexFromVectorElement(
272 leftVector, matrixNodeIndex(1), mNumFreqs, freq);
273 if (terminalNotGrounded(0))
274 (**mIntfVoltage)(0, freq) =
275 (**mIntfVoltage)(0, freq) -
277 mNumFreqs, freq);
278
279 SPDLOG_LOGGER_DEBUG(mSLog, "Voltage {:e}<{:e}",
280 std::abs((**mIntfVoltage)(0, freq)),
281 std::arg((**mIntfVoltage)(0, freq)));
282 }
283}
284
286 Int freqIdx) {
287 // v1 - v0
288 (**mIntfVoltage)(0, freqIdx) = 0;
289 if (terminalNotGrounded(1))
290 (**mIntfVoltage)(0, freqIdx) =
292 if (terminalNotGrounded(0))
293 (**mIntfVoltage)(0, freqIdx) =
294 (**mIntfVoltage)(0, freqIdx) -
296
297 SPDLOG_LOGGER_DEBUG(mSLog, "Voltage {:s}",
298 Logger::phasorToString((**mIntfVoltage)(0, freqIdx)));
299}
300
302 for (UInt freq = 0; freq < mNumFreqs; freq++) {
303 (**mIntfCurrent)(0, freq) =
304 mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
305 mEquivCurrent(freq, 0);
306 SPDLOG_LOGGER_DEBUG(mSLog, "Current {:s}",
308 }
309}
310
312 for (UInt freq = 0; freq < mNumFreqs; freq++) {
313 (**mIntfCurrent)(0, freq) =
314 mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
315 mEquivCurrent(freq, 0);
316 SPDLOG_LOGGER_DEBUG(mSLog, "Current {:s}",
318 }
319}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
void setParameters(Real capacitance)
Sets model specific parameters.
const CPS::Attribute< Real >::Ptr mCapacitance
Capacitance [F].
void execute(Real time, Int timeStepCount)
void execute(Real time, Int timeStepCount)
void updateCapacitance(Real capacitance, Real timeStep)
Change capacitance and refresh trapezoidal companion coefficients.
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)
static void setVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0, Matrix::Index colOffset=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< 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: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