DPsim
SP_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/SP/SP_Ph3_Capacitor.h>
10 
11 using namespace CPS;
12 
13 SP::Ph3::Capacitor::Capacitor(String uid, String name, Logger::Level logLevel)
14  : MNASimPowerComp<Complex>(uid, name, false, true, logLevel),
15  Base::Ph3::Capacitor(mAttributes) {
16  mPhaseType = PhaseType::ABC;
17  setTerminalNumber(2);
18  **mIntfVoltage = MatrixComp::Zero(3, 1);
19  **mIntfCurrent = MatrixComp::Zero(3, 1);
20 }
21 
23  auto copy = Capacitor::make(name, mLogLevel);
24  copy->setParameters(**mCapacitance);
25  return copy;
26 }
27 
29 
30  Real omega = 2 * PI * frequency;
31  mSusceptance = Matrix::Zero(3, 3);
32  mSusceptance << 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  // IntfVoltage initialization for each phase
43  (**mIntfVoltage)(0, 0) = initialSingleVoltage(1) - initialSingleVoltage(0);
44  (**mIntfVoltage)(1, 0) = (**mIntfVoltage)(0, 0) *
45  Complex(cos(-2. / 3. * M_PI), sin(-2. / 3. * M_PI));
46  (**mIntfVoltage)(2, 0) = (**mIntfVoltage)(0, 0) *
47  Complex(cos(2. / 3. * M_PI), sin(2. / 3. * M_PI));
48 
49  **mIntfCurrent = mSusceptance * **mIntfVoltage;
50  // TODO: add updated logger
51  /*
52  mLog.info() << "\n--- Initialize from power flow ---" << std::endl
53  << "Impedance: " << impedance << std::endl
54  << "Voltage across: " << std::abs((**mIntfVoltage)(0, 0))
55  << "<" << Math::phaseDeg((**mIntfVoltage)(0, 0)) << std::endl
56  << "Current: " << std::abs((**mIntfCurrent)(0, 0))
57  << "<" << Math::phaseDeg((**mIntfCurrent)(0, 0)) << std::endl
58  << "Terminal 0 voltage: " << std::abs(initialSingleVoltage(0))
59  << "<" << Math::phaseDeg(initialSingleVoltage(0)) << std::endl
60  << "Terminal 1 voltage: " << std::abs(initialSingleVoltage(1))
61  << "<" << Math::phaseDeg(initialSingleVoltage(1)) << std::endl
62  << "--- Power flow initialization finished ---" << std::endl;*/
63 }
64 
65 void SP::Ph3::Capacitor::mnaCompInitialize(Real omega, Real timeStep,
66  Attribute<Matrix>::Ptr leftVector) {
67  updateMatrixNodeIndices();
68  // TODO add updated logger
69  /*mLog.info() << "\n--- MNA Initialization ---" << std::endl
70  << "Initial voltage " << Math::abs((**mIntfVoltage)(0, 0))
71  << "<" << Math::phaseDeg((**mIntfVoltage)(0, 0)) << std::endl
72  << "Initial current " << Math::abs((**mIntfCurrent)(0, 0))
73  << "<" << Math::phaseDeg((**mIntfCurrent)(0, 0)) << std::endl
74  << "--- MNA initialization finished ---" << std::endl;*/
75 }
76 
78  SparseMatrixRow &systemMatrix) {
79  MNAStampUtils::stampAdmittanceMatrix(
80  mSusceptance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1),
81  terminalNotGrounded(0), terminalNotGrounded(1), mSLog);
82 }
83 
85  AttributeBase::List &prevStepDependencies,
86  AttributeBase::List &attributeDependencies,
87  AttributeBase::List &modifiedAttributes,
88  Attribute<Matrix>::Ptr &leftVector) {
89  attributeDependencies.push_back(leftVector);
90  modifiedAttributes.push_back(mIntfVoltage);
91  modifiedAttributes.push_back(mIntfCurrent);
92 }
93 
94 void SP::Ph3::Capacitor::mnaCompPostStep(Real time, Int timeStepCount,
95  Attribute<Matrix>::Ptr &leftVector) {
96  mnaCompUpdateVoltage(**leftVector);
97  mnaCompUpdateCurrent(**leftVector);
98 }
99 
100 void SP::Ph3::Capacitor::mnaCompUpdateVoltage(const Matrix &leftVector) {
101  // v1 - v0
102  **mIntfVoltage = Matrix::Zero(3, 1);
103  if (terminalNotGrounded(1)) {
104  (**mIntfVoltage)(0, 0) =
105  Math::complexFromVectorElement(leftVector, matrixNodeIndex(1, 0));
106  (**mIntfVoltage)(1, 0) =
107  Math::complexFromVectorElement(leftVector, matrixNodeIndex(1, 1));
108  (**mIntfVoltage)(2, 0) =
109  Math::complexFromVectorElement(leftVector, matrixNodeIndex(1, 2));
110  }
111  if (terminalNotGrounded(0)) {
112  (**mIntfVoltage)(0, 0) =
113  (**mIntfVoltage)(0, 0) -
114  Math::complexFromVectorElement(leftVector, matrixNodeIndex(0, 0));
115  (**mIntfVoltage)(1, 0) =
116  (**mIntfVoltage)(1, 0) -
117  Math::complexFromVectorElement(leftVector, matrixNodeIndex(0, 1));
118  (**mIntfVoltage)(2, 0) =
119  (**mIntfVoltage)(2, 0) -
120  Math::complexFromVectorElement(leftVector, matrixNodeIndex(0, 2));
121  }
122 }
123 
124 void SP::Ph3::Capacitor::mnaCompUpdateCurrent(const Matrix &leftVector) {
125  **mIntfCurrent = mSusceptance * **mIntfVoltage;
126 }
Base class for all MNA components that are transmitting power.
Capacitor(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Update interface voltage from MNA system result.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
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 mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamps system matrix.
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.
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