DPsim
DP_Ph3_Capacitor.cpp
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 
9 #include <dpsim-models/DP/DP_Ph3_Capacitor.h>
10 
11 using namespace CPS;
12 using namespace CPS::DP::Ph3;
13 
14 DP::Ph3::Capacitor::Capacitor(String uid, String name, Logger::Level logLevel)
15  : MNASimPowerComp<Complex>(uid, name, true, true, logLevel),
16  Base::Ph3::Capacitor(mAttributes) {
17  mPhaseType = PhaseType::ABC;
18  setTerminalNumber(2);
19  mEquivCurrent = MatrixComp::Zero(3, 1);
20  **mIntfVoltage = MatrixComp::Zero(3, 1);
21  **mIntfCurrent = MatrixComp::Zero(3, 1);
22 }
23 
25  auto copy = Capacitor::make(name, mLogLevel);
26  copy->setParameters(**mCapacitance);
27  return copy;
28 }
29 
31 
32  Real omega = 2 * PI * frequency;
33  MatrixComp susceptance = Matrix::Zero(3, 3);
34 
35  susceptance << Complex(0, omega * (**mCapacitance)(0, 0)),
36  Complex(0, omega * (**mCapacitance)(0, 1)),
37  Complex(0, omega * (**mCapacitance)(0, 2)),
38  Complex(0, omega * (**mCapacitance)(1, 0)),
39  Complex(0, omega * (**mCapacitance)(1, 1)),
40  Complex(0, omega * (**mCapacitance)(1, 2)),
41  Complex(0, omega * (**mCapacitance)(2, 0)),
42  Complex(0, omega * (**mCapacitance)(2, 1)),
43  Complex(0, omega * (**mCapacitance)(2, 2));
44 
45  // IntfVoltage initialization for each phase
46  (**mIntfVoltage)(0, 0) = initialSingleVoltage(1) - initialSingleVoltage(0);
47  Real voltMag = Math::abs((**mIntfVoltage)(0, 0));
48  Real voltPhase = Math::phase((**mIntfVoltage)(0, 0));
49  (**mIntfVoltage)(1, 0) = Complex(voltMag * cos(voltPhase - 2. / 3. * M_PI),
50  voltMag * sin(voltPhase - 2. / 3. * M_PI));
51  (**mIntfVoltage)(2, 0) = Complex(voltMag * cos(voltPhase + 2. / 3. * M_PI),
52  voltMag * sin(voltPhase + 2. / 3. * M_PI));
53 
54  **mIntfCurrent = susceptance * **mIntfVoltage;
55 
56  SPDLOG_LOGGER_INFO(mSLog, "\n--- Initialize from power flow ---");
57  // << "Impedance: " << impedance << std::endl
58  // << "Voltage across: " << std::abs((**mIntfVoltage)(0,0))
59  // << "<" << Math::phaseDeg((**mIntfVoltage)(0,0)) << std::endl
60  // << "Current: " << std::abs((**mIntfCurrent)(0,0))
61  // << "<" << Math::phaseDeg((**mIntfCurrent)(0,0)) << std::endl
62  // << "Terminal 0 voltage: " << std::abs(initialSingleVoltage(0))
63  // << "<" << Math::phaseDeg(initialSingleVoltage(0)) << std::endl
64  // << "Terminal 1 voltage: " << std::abs(initialSingleVoltage(1))
65  // << "<" << Math::phaseDeg(initialSingleVoltage(1)) << std::endl
66  // << "--- Power flow initialization finished ---" << std::endl;
67 }
68 
69 void DP::Ph3::Capacitor::initVars(Real omega, Real timeStep) {
70  Matrix a = timeStep / 2 * (**mCapacitance).inverse();
71  Real b = timeStep * omega / 2.;
72 
73  Matrix equivCondReal = a.inverse();
74  Matrix equivCondImag = b * equivCondReal;
75  mEquivCond = Matrix::Zero(3, 3);
76  mEquivCond << Complex(equivCondReal(0, 0), equivCondImag(0, 0)),
77  Complex(equivCondReal(0, 1), equivCondImag(0, 1)),
78  Complex(equivCondReal(0, 2), equivCondImag(0, 2)),
79  Complex(equivCondReal(1, 0), equivCondImag(1, 0)),
80  Complex(equivCondReal(1, 1), equivCondImag(1, 1)),
81  Complex(equivCondReal(1, 2), equivCondImag(1, 2)),
82  Complex(equivCondReal(2, 0), equivCondImag(2, 0)),
83  Complex(equivCondReal(2, 1), equivCondImag(2, 1)),
84  Complex(equivCondReal(2, 2), equivCondImag(2, 2));
85 
86  // since equivCondReal == a.inverse()
87  // and
88  /*Matrix mPrevVoltCoeffReal = a.inverse();
89  Matrix mPrevVoltCoeffImag = -b * a.inverse();*/
90  Matrix mPrevVoltCoeffReal = equivCondReal;
91  Matrix mPrevVoltCoeffImag = -b * equivCondReal;
92 
93  mPrevVoltCoeff = Matrix::Zero(3, 3);
94  mPrevVoltCoeff << Complex(mPrevVoltCoeffReal(0, 0), mPrevVoltCoeffImag(0, 0)),
95  Complex(mPrevVoltCoeffReal(0, 1), mPrevVoltCoeffImag(0, 1)),
96  Complex(mPrevVoltCoeffReal(0, 2), mPrevVoltCoeffImag(0, 2)),
97  Complex(mPrevVoltCoeffReal(1, 0), mPrevVoltCoeffImag(1, 0)),
98  Complex(mPrevVoltCoeffReal(1, 1), mPrevVoltCoeffImag(1, 1)),
99  Complex(mPrevVoltCoeffReal(1, 2), mPrevVoltCoeffImag(1, 2)),
100  Complex(mPrevVoltCoeffReal(2, 0), mPrevVoltCoeffImag(2, 0)),
101  Complex(mPrevVoltCoeffReal(2, 1), mPrevVoltCoeffImag(2, 1)),
102  Complex(mPrevVoltCoeffReal(2, 2), mPrevVoltCoeffImag(2, 2));
103 
104  mEquivCurrent = -mPrevVoltCoeff * **mIntfVoltage - **mIntfCurrent;
105 }
106 
107 void DP::Ph3::Capacitor::mnaCompInitialize(Real omega, Real timeStep,
108  Attribute<Matrix>::Ptr leftVector) {
109  updateMatrixNodeIndices();
110  initVars(omega, timeStep);
111  //Matrix equivCondReal = 2.0 * mCapacitance / timeStep;
112  //Matrix equivCondImag = omega * mCapacitance;
113  //mEquivCond <<
114  // Complex(equivCondReal(0, 0), equivCondImag(0, 0)),
115  // Complex(equivCondReal(1, 0), equivCondImag(1, 0)),
116  // Complex(equivCondReal(2, 0), equivCondImag(2, 0));
117 
118  // TODO: something is wrong here -- from Ph1_Capacitor
119  /*Matrix prevVoltCoeffReal = 2.0 * mCapacitance / timeStep;
120  Matrix prevVoltCoeffImag = - omega * mCapacitance;
121  mPrevVoltCoeff = Matrix::Zero(3, 1);
122  mPrevVoltCoeff <<
123  Complex(prevVoltCoeffReal(0, 0), prevVoltCoeffImag(0, 0)),
124  Complex(prevVoltCoeffReal(1, 0), prevVoltCoeffImag(1, 0)),
125  Complex(prevVoltCoeffReal(2, 0), prevVoltCoeffImag(2, 0));
126 
127  mEquivCurrent = -**mIntfCurrent + -mPrevVoltCoeff.cwiseProduct( **mIntfVoltage);*/
128  // no need to update current now
129  //**mIntfCurrent = mEquivCond.cwiseProduct(**mIntfVoltage) + mEquivCurrent;
130 
131  // mLog.info() << "\n--- MNA Initialization ---" << std::endl
132  // << "Initial voltage " << Math::abs((**mIntfVoltage)(0,0))
133  // << "<" << Math::phaseDeg((**mIntfVoltage)(0,0)) << std::endl
134  // << "Initial current " << Math::abs((**mIntfCurrent)(0,0))
135  // << "<" << Math::phaseDeg((**mIntfCurrent)(0,0)) << std::endl
136  // << "--- MNA initialization finished ---" << std::endl;
137 }
138 
140  SparseMatrixRow &systemMatrix) {
141  MNAStampUtils::stampAdmittanceMatrix(
142  mEquivCond, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1),
143  terminalNotGrounded(0), terminalNotGrounded(1), mSLog);
144 }
145 
147  //mCureqr = mCurrr + mGcr * mDeltavr + mGci * mDeltavi;
148  //mCureqi = mCurri + mGcr * mDeltavi - mGci * mDeltavr;
149 
150  mEquivCurrent = -**mIntfCurrent + -mPrevVoltCoeff * **mIntfVoltage;
151 
152  if (terminalNotGrounded(0)) {
153  Math::setVectorElement(rightVector, matrixNodeIndex(0, 0),
154  mEquivCurrent(0, 0));
155  Math::setVectorElement(rightVector, matrixNodeIndex(0, 1),
156  mEquivCurrent(1, 0));
157  Math::setVectorElement(rightVector, matrixNodeIndex(0, 2),
158  mEquivCurrent(2, 0));
159  }
160  if (terminalNotGrounded(1)) {
161  Math::setVectorElement(rightVector, matrixNodeIndex(1, 0),
162  -mEquivCurrent(0, 0));
163  Math::setVectorElement(rightVector, matrixNodeIndex(1, 1),
164  -mEquivCurrent(1, 0));
165  Math::setVectorElement(rightVector, matrixNodeIndex(1, 2),
166  -mEquivCurrent(2, 0));
167  }
168 }
169 
171  AttributeBase::List &prevStepDependencies,
172  AttributeBase::List &attributeDependencies,
173  AttributeBase::List &modifiedAttributes) {
174  // actually depends on C, but then we'd have to modify the system matrix anyway
175  modifiedAttributes.push_back(mRightVector);
176  prevStepDependencies.push_back(mIntfCurrent);
177  prevStepDependencies.push_back(mIntfVoltage);
178 }
179 
180 void DP::Ph3::Capacitor::mnaCompPreStep(Real time, Int timeStepCount) {
181  mnaCompApplyRightSideVectorStamp(**mRightVector);
182 }
183 
185  AttributeBase::List &prevStepDependencies,
186  AttributeBase::List &attributeDependencies,
187  AttributeBase::List &modifiedAttributes,
188  Attribute<Matrix>::Ptr &leftVector) {
189  attributeDependencies.push_back(leftVector);
190  modifiedAttributes.push_back(mIntfVoltage);
191  modifiedAttributes.push_back(mIntfCurrent);
192 }
193 
194 void DP::Ph3::Capacitor::mnaCompPostStep(Real time, Int timeStepCount,
195  Attribute<Matrix>::Ptr &leftVector) {
196  mnaCompUpdateVoltage(**leftVector);
197  mnaCompUpdateCurrent(**leftVector);
198 }
199 
200 void DP::Ph3::Capacitor::mnaCompUpdateVoltage(const Matrix &leftVector) {
201  // v1 - v0
202  **mIntfVoltage = Matrix::Zero(3, 1);
203  if (terminalNotGrounded(1)) {
204  (**mIntfVoltage)(0, 0) =
205  Math::complexFromVectorElement(leftVector, matrixNodeIndex(1, 0));
206  (**mIntfVoltage)(1, 0) =
207  Math::complexFromVectorElement(leftVector, matrixNodeIndex(1, 1));
208  (**mIntfVoltage)(2, 0) =
209  Math::complexFromVectorElement(leftVector, matrixNodeIndex(1, 2));
210  }
211  if (terminalNotGrounded(0)) {
212  (**mIntfVoltage)(0, 0) =
213  (**mIntfVoltage)(0, 0) -
214  Math::complexFromVectorElement(leftVector, matrixNodeIndex(0, 0));
215  (**mIntfVoltage)(1, 0) =
216  (**mIntfVoltage)(1, 0) -
217  Math::complexFromVectorElement(leftVector, matrixNodeIndex(0, 1));
218  (**mIntfVoltage)(2, 0) =
219  (**mIntfVoltage)(2, 0) -
220  Math::complexFromVectorElement(leftVector, matrixNodeIndex(0, 2));
221  }
222 }
223 
224 void DP::Ph3::Capacitor::mnaCompUpdateCurrent(const Matrix &leftVector) {
225  **mIntfCurrent = mEquivCond * **mIntfVoltage + mEquivCurrent;
226 }
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 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.
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 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 mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
void initVars(Real omega, Real timeStep)
init resistive companion model of capacitor
MatrixComp mEquivCurrent
DC equivalent current source [A].
Base class for all MNA components that are transmitting power.
Base class for all components that are transmitting power.
Definition: SimPowerComp.h:17
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
Current through component.
Definition: SimPowerComp.h:47
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
Voltage between terminals.
Definition: SimPowerComp.h:45