9 #include <dpsim-models/DP/DP_Ph1_Capacitor.h>
12 using namespace CPS::DP::Ph1;
24 auto copy = Capacitor::make(name, mLogLevel);
25 copy->setParameters(**mCapacitance);
32 mEquivCurrent = MatrixComp::Zero(mNumFreqs, 1);
33 mEquivCond = MatrixComp::Zero(mNumFreqs, 1);
34 mPrevVoltCoeff = MatrixComp::Zero(mNumFreqs, 1);
39 Real omega = 2 * PI * frequency;
40 Complex impedance = {0, -1. / (omega * **mCapacitance)};
41 (**mIntfVoltage)(0, 0) = initialSingleVoltage(1) - initialSingleVoltage(0);
42 (**mIntfCurrent)(0, 0) = (**mIntfVoltage)(0, 0) / impedance;
44 SPDLOG_LOGGER_INFO(mSLog,
45 "\nCapacitance [F]: {:s}"
46 "\nImpedance [Ohm]: {:s}",
47 Logger::realToString(**mCapacitance),
48 Logger::complexToString(impedance));
49 SPDLOG_LOGGER_INFO(mSLog,
50 "\n--- Initialization from powerflow ---"
51 "\nVoltage across: {:s}"
53 "\nTerminal 0 voltage: {:s}"
54 "\nTerminal 1 voltage: {:s}"
55 "\n--- Initialization from powerflow finished ---",
56 Logger::phasorToString((**mIntfVoltage)(0, 0)),
57 Logger::phasorToString((**mIntfCurrent)(0, 0)),
58 Logger::phasorToString(initialSingleVoltage(0)),
59 Logger::phasorToString(initialSingleVoltage(1)));
65 updateMatrixNodeIndices();
67 Real equivCondReal = 2.0 * **mCapacitance / timeStep;
68 Real prevVoltCoeffReal = 2.0 * **mCapacitance / timeStep;
70 for (UInt freq = 0; freq < mNumFreqs; freq++) {
71 Real equivCondImag = 2. * PI * mFrequencies(freq, 0) * **mCapacitance;
72 mEquivCond(freq, 0) = {equivCondReal, equivCondImag};
73 Real prevVoltCoeffImag = -2. * PI * mFrequencies(freq, 0) * **mCapacitance;
74 mPrevVoltCoeff(freq, 0) = {prevVoltCoeffReal, prevVoltCoeffImag};
76 mEquivCurrent(freq, 0) =
77 -(**mIntfCurrent)(0, freq) +
78 -mPrevVoltCoeff(freq, 0) * (**mIntfVoltage)(0, freq);
79 (**mIntfCurrent)(0, freq) =
80 mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
81 mEquivCurrent(freq, 0);
84 SPDLOG_LOGGER_INFO(mSLog,
85 "\n--- MNA initialization ---"
86 "\nInitial voltage {:s}"
87 "\nInitial current {:s}"
88 "\nEquiv. current {:s}"
89 "\n--- MNA initialization finished ---",
90 Logger::phasorToString((**mIntfVoltage)(0, 0)),
91 Logger::phasorToString((**mIntfCurrent)(0, 0)),
92 Logger::complexToString(mEquivCurrent(0, 0)));
96 void DP::Ph1::Capacitor::mnaCompInitializeHarm(
97 Real omega, Real timeStep,
99 updateMatrixNodeIndices();
101 Real equivCondReal = 2.0 * **mCapacitance / timeStep;
102 Real prevVoltCoeffReal = 2.0 * **mCapacitance / timeStep;
104 for (UInt freq = 0; freq < mNumFreqs; freq++) {
105 Real equivCondImag = 2. * PI * mFrequencies(freq, 0) * **mCapacitance;
106 mEquivCond(freq, 0) = {equivCondReal, equivCondImag};
107 Real prevVoltCoeffImag = -2. * PI * mFrequencies(freq, 0) * **mCapacitance;
108 mPrevVoltCoeff(freq, 0) = {prevVoltCoeffReal, prevVoltCoeffImag};
110 mEquivCurrent(freq, 0) =
111 -(**mIntfCurrent)(0, freq) +
112 -mPrevVoltCoeff(freq, 0) * (**mIntfVoltage)(0, freq);
113 (**mIntfCurrent)(0, freq) =
114 mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
115 mEquivCurrent(freq, 0);
118 mMnaTasks.push_back(std::make_shared<MnaPreStepHarm>(*
this));
119 mMnaTasks.push_back(std::make_shared<MnaPostStepHarm>(*
this, leftVectors));
120 **mRightVector = Matrix::Zero(leftVectors[0]->get().rows(), mNumFreqs);
124 SparseMatrixRow &systemMatrix) {
125 for (UInt freq = 0; freq < mNumFreqs; freq++) {
126 MNAStampUtils::stampAdmittance(
127 mEquivCond(freq, 0), systemMatrix, matrixNodeIndex(0),
128 matrixNodeIndex(1), terminalNotGrounded(0), terminalNotGrounded(1),
129 mSLog, mNumFreqs, freq);
133 void DP::Ph1::Capacitor::mnaCompApplySystemMatrixStampHarm(
134 SparseMatrixRow &systemMatrix, Int freqIdx) {
135 MNAStampUtils::stampAdmittance(mEquivCond(freqIdx, 0), systemMatrix,
136 matrixNodeIndex(0), matrixNodeIndex(1),
137 terminalNotGrounded(0), terminalNotGrounded(1),
142 for (UInt freq = 0; freq < mNumFreqs; freq++) {
145 mEquivCurrent(freq, 0) =
146 -(**mIntfCurrent)(0, freq) +
147 -mPrevVoltCoeff(freq, 0) * (**mIntfVoltage)(0, freq);
149 if (terminalNotGrounded(0))
150 Math::setVectorElement(rightVector, matrixNodeIndex(0),
151 mEquivCurrent(freq, 0), mNumFreqs, freq);
152 if (terminalNotGrounded(1))
153 Math::setVectorElement(rightVector, matrixNodeIndex(1),
154 -mEquivCurrent(freq, 0), mNumFreqs, freq);
156 SPDLOG_LOGGER_DEBUG(mSLog,
"MNA EquivCurrent {:f}+j{:f}",
157 mEquivCurrent(freq, 0).real(),
158 mEquivCurrent(freq, 0).imag());
159 if (terminalNotGrounded(0))
160 SPDLOG_LOGGER_DEBUG(mSLog,
"Add {:f}+j{:f} to source vector at {:d}",
161 mEquivCurrent(freq, 0).real(),
162 mEquivCurrent(freq, 0).imag(), matrixNodeIndex(0));
163 if (terminalNotGrounded(1))
164 SPDLOG_LOGGER_DEBUG(mSLog,
"Add {:f}+j{:f} to source vector at {:d}",
165 -mEquivCurrent(freq, 0).real(),
166 -mEquivCurrent(freq, 0).imag(), matrixNodeIndex(1));
170 void DP::Ph1::Capacitor::mnaCompApplyRightSideVectorStampHarm(
171 Matrix &rightVector) {
172 for (UInt freq = 0; freq < mNumFreqs; freq++) {
175 mEquivCurrent(freq, 0) =
176 -(**mIntfCurrent)(0, freq) +
177 -mPrevVoltCoeff(freq, 0) * (**mIntfVoltage)(0, freq);
179 if (terminalNotGrounded(0))
180 Math::setVectorElement(rightVector, matrixNodeIndex(0),
181 mEquivCurrent(freq, 0), 1, 0, freq);
182 if (terminalNotGrounded(1))
183 Math::setVectorElement(rightVector, matrixNodeIndex(1),
184 -mEquivCurrent(freq, 0), 1, 0, freq);
188 void DP::Ph1::Capacitor::mnaCompApplyRightSideVectorStampHarm(
189 Matrix &rightVector, Int freq) {
192 mEquivCurrent(freq, 0) = -(**mIntfCurrent)(0, freq) +
193 -mPrevVoltCoeff(freq, 0) * (**mIntfVoltage)(0, freq);
195 if (terminalNotGrounded(0))
196 Math::setVectorElement(rightVector, matrixNodeIndex(0),
197 mEquivCurrent(freq, 0));
198 if (terminalNotGrounded(1))
199 Math::setVectorElement(rightVector, matrixNodeIndex(1),
200 -mEquivCurrent(freq, 0));
204 AttributeBase::List &prevStepDependencies,
205 AttributeBase::List &attributeDependencies,
206 AttributeBase::List &modifiedAttributes) {
208 prevStepDependencies.push_back(mIntfCurrent);
209 prevStepDependencies.push_back(mIntfVoltage);
210 modifiedAttributes.push_back(mRightVector);
214 this->mnaApplyRightSideVectorStamp(**this->mRightVector);
218 AttributeBase::List &prevStepDependencies,
219 AttributeBase::List &attributeDependencies,
220 AttributeBase::List &modifiedAttributes,
222 attributeDependencies.push_back(leftVector);
223 modifiedAttributes.push_back(mIntfVoltage);
224 modifiedAttributes.push_back(mIntfCurrent);
229 this->mnaUpdateVoltage(**leftVector);
230 this->mnaUpdateCurrent(**leftVector);
233 void DP::Ph1::Capacitor::MnaPreStepHarm::execute(Real time, Int timeStepCount) {
234 mCapacitor.mnaCompApplyRightSideVectorStampHarm(**mCapacitor.mRightVector);
237 void DP::Ph1::Capacitor::MnaPostStepHarm::execute(Real time,
239 for (UInt freq = 0; freq < mCapacitor.mNumFreqs; freq++)
240 mCapacitor.mnaCompUpdateVoltageHarm(**mLeftVectors[freq], freq);
241 mCapacitor.mnaCompUpdateCurrentHarm();
246 for (UInt freq = 0; freq < mNumFreqs; freq++) {
247 (**mIntfVoltage)(0, freq) = 0;
248 if (terminalNotGrounded(1))
249 (**mIntfVoltage)(0, freq) = Math::complexFromVectorElement(
250 leftVector, matrixNodeIndex(1), mNumFreqs, freq);
251 if (terminalNotGrounded(0))
252 (**mIntfVoltage)(0, freq) =
253 (**mIntfVoltage)(0, freq) -
254 Math::complexFromVectorElement(leftVector, matrixNodeIndex(0),
257 SPDLOG_LOGGER_DEBUG(mSLog,
"Voltage {:e}<{:e}",
258 std::abs((**mIntfVoltage)(0, freq)),
259 std::arg((**mIntfVoltage)(0, freq)));
263 void DP::Ph1::Capacitor::mnaCompUpdateVoltageHarm(
const Matrix &leftVector,
266 (**mIntfVoltage)(0, freqIdx) = 0;
267 if (terminalNotGrounded(1))
268 (**mIntfVoltage)(0, freqIdx) =
269 Math::complexFromVectorElement(leftVector, matrixNodeIndex(1));
270 if (terminalNotGrounded(0))
271 (**mIntfVoltage)(0, freqIdx) =
272 (**mIntfVoltage)(0, freqIdx) -
273 Math::complexFromVectorElement(leftVector, matrixNodeIndex(0));
275 SPDLOG_LOGGER_DEBUG(mSLog,
"Voltage {:s}",
276 Logger::phasorToString((**mIntfVoltage)(0, freqIdx)));
280 for (UInt freq = 0; freq < mNumFreqs; freq++) {
281 (**mIntfCurrent)(0, freq) =
282 mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
283 mEquivCurrent(freq, 0);
284 SPDLOG_LOGGER_DEBUG(mSLog,
"Current {:s}",
285 Logger::phasorToString((**mIntfCurrent)(0, freq)));
289 void DP::Ph1::Capacitor::mnaCompUpdateCurrentHarm() {
290 for (UInt freq = 0; freq < mNumFreqs; freq++) {
291 (**mIntfCurrent)(0, freq) =
292 mEquivCond(freq, 0) * (**mIntfVoltage)(0, freq) +
293 mEquivCurrent(freq, 0);
294 SPDLOG_LOGGER_DEBUG(mSLog,
"Current {:s}",
295 Logger::phasorToString((**mIntfCurrent)(0, freq)));
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamps system matrix.
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 initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
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 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.
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.
Base class for all MNA components that are transmitting power.
Base class for all components that are transmitting power.
virtual void initialize(Matrix frequencies)
Initialize components with correct network frequencies.
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
Current through component.
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
Voltage between terminals.