DPsim
EMT_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/EMT/EMT_Ph3_Capacitor.h>
10 
11 using namespace CPS;
12 
13 EMT::Ph3::Capacitor::Capacitor(String uid, String name, Logger::Level logLevel)
14  : MNASimPowerComp<Real>(uid, name, true, true, logLevel),
15  Base::Ph3::Capacitor(mAttributes) {
16  mPhaseType = PhaseType::ABC;
17  setTerminalNumber(2);
18  mEquivCurrent = Matrix::Zero(3, 1);
19  **mIntfVoltage = Matrix::Zero(3, 1);
20  **mIntfCurrent = Matrix::Zero(3, 1);
21 }
22 
24  auto copy = Capacitor::make(name, mLogLevel);
25  copy->setParameters(**mCapacitance);
26  return copy;
27 }
29 
30  Real omega = 2 * PI * frequency;
31  MatrixComp admittance = MatrixComp::Zero(3, 3);
32  admittance << Complex(0, omega * (**mCapacitance)(0, 0)),
33  Complex(0, omega * (**mCapacitance)(0, 1)),
34  Complex(0, omega * (**mCapacitance)(0, 2)),
35  Complex(0, omega * (**mCapacitance)(1, 0)),
36  Complex(0, omega * (**mCapacitance)(1, 1)),
37  Complex(0, omega * (**mCapacitance)(1, 2)),
38  Complex(0, omega * (**mCapacitance)(2, 0)),
39  Complex(0, omega * (**mCapacitance)(2, 1)),
40  Complex(0, omega * (**mCapacitance)(2, 2));
41 
42  MatrixComp vInitABC = Matrix::Zero(3, 1);
43  vInitABC(0, 0) = RMS3PH_TO_PEAK1PH * initialSingleVoltage(1) -
44  RMS3PH_TO_PEAK1PH * initialSingleVoltage(0);
45  vInitABC(1, 0) = vInitABC(0, 0) * SHIFT_TO_PHASE_B;
46  vInitABC(2, 0) = vInitABC(0, 0) * SHIFT_TO_PHASE_C;
47  **mIntfVoltage = vInitABC.real();
48  **mIntfCurrent = (admittance * vInitABC).real();
49 
50  SPDLOG_LOGGER_INFO(mSLog,
51  "\nCapacitance [F]: {:s}"
52  "\nAdmittance [S]: {:s}",
53  Logger::matrixToString(**mCapacitance),
54  Logger::matrixCompToString(admittance));
55  SPDLOG_LOGGER_INFO(
56  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 ---",
63  Logger::matrixToString(**mIntfVoltage),
64  Logger::matrixToString(**mIntfCurrent),
65  Logger::phasorToString(RMS3PH_TO_PEAK1PH * initialSingleVoltage(0)),
66  Logger::phasorToString(RMS3PH_TO_PEAK1PH * initialSingleVoltage(1)));
67 }
68 
69 void EMT::Ph3::Capacitor::mnaCompInitialize(Real omega, Real timeStep,
70  Attribute<Matrix>::Ptr leftVector) {
71  updateMatrixNodeIndices();
72  mEquivCond = (2.0 * **mCapacitance) / timeStep;
73  // Update internal state
74  mEquivCurrent = -**mIntfCurrent + -mEquivCond * **mIntfVoltage;
75 }
76 
78  SparseMatrixRow &systemMatrix) {
79  MNAStampUtils::stampConductanceMatrix(
80  mEquivCond, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1),
81  terminalNotGrounded(0), terminalNotGrounded(1), mSLog);
82 
83  SPDLOG_LOGGER_INFO(mSLog, "\nEquivalent Conductance: {:s}",
84  Logger::matrixToString(mEquivCond));
85 }
86 
88  Matrix &rightVector) {
89  mEquivCurrent = -**mIntfCurrent + -mEquivCond * **mIntfVoltage;
90  if (terminalNotGrounded(0)) {
91  Math::setVectorElement(rightVector, matrixNodeIndex(0, 0),
92  mEquivCurrent(0, 0));
93  Math::setVectorElement(rightVector, matrixNodeIndex(0, 1),
94  mEquivCurrent(1, 0));
95  Math::setVectorElement(rightVector, matrixNodeIndex(0, 2),
96  mEquivCurrent(2, 0));
97  }
98  if (terminalNotGrounded(1)) {
99  Math::setVectorElement(rightVector, matrixNodeIndex(1, 0),
100  -mEquivCurrent(0, 0));
101  Math::setVectorElement(rightVector, matrixNodeIndex(1, 1),
102  -mEquivCurrent(1, 0));
103  Math::setVectorElement(rightVector, matrixNodeIndex(1, 2),
104  -mEquivCurrent(2, 0));
105  }
106  SPDLOG_LOGGER_DEBUG(mSLog, "\nEquivalent Current: {:s}",
107  Logger::matrixToString(mEquivCurrent));
108 }
109 
111  AttributeBase::List &prevStepDependencies,
112  AttributeBase::List &attributeDependencies,
113  AttributeBase::List &modifiedAttributes) {
114  // actually depends on C, but then we'd have to modify the system matrix anyway
115  prevStepDependencies.push_back(mIntfCurrent);
116  prevStepDependencies.push_back(mIntfVoltage);
117  modifiedAttributes.push_back(mRightVector);
118 }
119 
120 void EMT::Ph3::Capacitor::mnaCompPreStep(Real time, Int timeStepCount) {
121  mnaCompApplyRightSideVectorStamp(**mRightVector);
122 }
123 
125  AttributeBase::List &prevStepDependencies,
126  AttributeBase::List &attributeDependencies,
127  AttributeBase::List &modifiedAttributes,
128  Attribute<Matrix>::Ptr &leftVector) {
129  attributeDependencies.push_back(leftVector);
130  modifiedAttributes.push_back(mIntfVoltage);
131  modifiedAttributes.push_back(mIntfCurrent);
132 }
133 
134 void EMT::Ph3::Capacitor::mnaCompPostStep(Real time, Int timeStepCount,
135  Attribute<Matrix>::Ptr &leftVector) {
136  mnaCompUpdateVoltage(**leftVector);
137  mnaCompUpdateCurrent(**leftVector);
138 }
139 
140 void EMT::Ph3::Capacitor::mnaCompUpdateVoltage(const Matrix &leftVector) {
141  // v1 - v0
142  **mIntfVoltage = Matrix::Zero(3, 1);
143  if (terminalNotGrounded(1)) {
144  (**mIntfVoltage)(0, 0) =
145  Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 0));
146  (**mIntfVoltage)(1, 0) =
147  Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 1));
148  (**mIntfVoltage)(2, 0) =
149  Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 2));
150  }
151  if (terminalNotGrounded(0)) {
152  (**mIntfVoltage)(0, 0) =
153  (**mIntfVoltage)(0, 0) -
154  Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 0));
155  (**mIntfVoltage)(1, 0) =
156  (**mIntfVoltage)(1, 0) -
157  Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 1));
158  (**mIntfVoltage)(2, 0) =
159  (**mIntfVoltage)(2, 0) -
160  Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 2));
161  }
162 }
163 
164 void EMT::Ph3::Capacitor::mnaCompUpdateCurrent(const Matrix &leftVector) {
165  **mIntfCurrent = mEquivCond * **mIntfVoltage + mEquivCurrent;
166  SPDLOG_LOGGER_DEBUG(mSLog, "\nCurrent: {:s}",
167  Logger::matrixToString(**mIntfCurrent));
168 }
Capacitor(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Update interface voltage from MNA system result.
void mnaCompPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Update interface current from MNA system result.
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
Initializes internal variables of the component.
SimPowerComp< Real >::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.
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamps system matrix.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
Matrix 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< Real > >::Ptr mIntfCurrent
Current through component.
Definition: SimPowerComp.h:47
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
Voltage between terminals.
Definition: SimPowerComp.h:45