DPsim
SP_Ph1_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_Ph1_Capacitor.h>
10 
11 using namespace CPS;
12 
13 SP::Ph1::Capacitor::Capacitor(String uid, String name, Logger::Level logLevel)
14  : MNASimPowerComp<Complex>(uid, name, false, true, logLevel),
15  Base::Ph1::Capacitor(mAttributes) {
16  **mIntfVoltage = MatrixComp::Zero(1, 1);
17  **mIntfCurrent = MatrixComp::Zero(1, 1);
18  setTerminalNumber(2);
19 }
20 
22  auto copy = Capacitor::make(name, mLogLevel);
23  copy->setParameters(**mCapacitance);
24  return copy;
25 }
26 
28 
29  Real omega = 2 * PI * frequency;
30  mSusceptance = Complex(0, omega * **mCapacitance);
31  mImpedance = Complex(1, 0) / mSusceptance;
32  mAdmittance = Complex(1, 0) / mImpedance;
33  (**mIntfVoltage)(0, 0) = initialSingleVoltage(1) - initialSingleVoltage(0);
34  **mIntfCurrent = mSusceptance * **mIntfVoltage;
35 
36  SPDLOG_LOGGER_INFO(mSLog,
37  "\nCapacitance [F]: {:s}"
38  "\nImpedance [Ohm]: {:s}"
39  "\nAdmittance [S]: {:s}",
40  Logger::realToString(**mCapacitance),
41  Logger::complexToString(mImpedance),
42  Logger::complexToString(mAdmittance));
43  SPDLOG_LOGGER_INFO(mSLog,
44  "\n--- Initialization from powerflow ---"
45  "\nVoltage across: {:s}"
46  "\nCurrent: {:s}"
47  "\nTerminal 0 voltage: {:s}"
48  "\nTerminal 1 voltage: {:s}"
49  "\n--- Initialization from powerflow finished ---",
50  Logger::phasorToString((**mIntfVoltage)(0, 0)),
51  Logger::phasorToString((**mIntfCurrent)(0, 0)),
52  Logger::phasorToString(initialSingleVoltage(0)),
53  Logger::phasorToString(initialSingleVoltage(1)));
54 }
55 
56 // #### MNA section ####
57 
58 void SP::Ph1::Capacitor::mnaCompInitialize(Real omega, Real timeStep,
59  Attribute<Matrix>::Ptr leftVector) {
60  updateMatrixNodeIndices();
61  SPDLOG_LOGGER_INFO(mSLog,
62  "\n--- MNA initialization ---"
63  "\nInitial voltage {:s}"
64  "\nInitial current {:s}"
65  "\n--- MNA initialization finished ---",
66  Logger::phasorToString((**mIntfVoltage)(0, 0)),
67  Logger::phasorToString((**mIntfCurrent)(0, 0)));
68 }
69 
71  SparseMatrixRow &systemMatrix) {
72  MNAStampUtils::stampAdmittance(mSusceptance, systemMatrix, matrixNodeIndex(0),
73  matrixNodeIndex(1), terminalNotGrounded(0),
74  terminalNotGrounded(1), mSLog);
75 }
76 
78  AttributeBase::List &prevStepDependencies,
79  AttributeBase::List &attributeDependencies,
80  AttributeBase::List &modifiedAttributes,
81  Attribute<Matrix>::Ptr &leftVector) {
82  attributeDependencies.push_back(leftVector);
83  modifiedAttributes.push_back(mIntfVoltage);
84  modifiedAttributes.push_back(mIntfCurrent);
85 }
86 
87 void SP::Ph1::Capacitor::mnaCompPostStep(Real time, Int timeStepCount,
88  Attribute<Matrix>::Ptr &leftVector) {
89  this->mnaUpdateVoltage(**leftVector);
90  this->mnaUpdateCurrent(**leftVector);
91 }
92 
93 void SP::Ph1::Capacitor::mnaCompUpdateVoltage(const Matrix &leftVector) {
94  // v1 - v0
95  **mIntfVoltage = Matrix::Zero(3, 1);
96  if (terminalNotGrounded(1)) {
97  (**mIntfVoltage)(0, 0) =
98  Math::complexFromVectorElement(leftVector, matrixNodeIndex(1));
99  }
100  if (terminalNotGrounded(0)) {
101  (**mIntfVoltage)(0, 0) =
102  (**mIntfVoltage)(0, 0) -
103  Math::complexFromVectorElement(leftVector, matrixNodeIndex(0));
104  }
105 }
106 
107 void SP::Ph1::Capacitor::mnaCompUpdateCurrent(const Matrix &leftVector) {
108  **mIntfCurrent = mSusceptance * **mIntfVoltage;
109 }
110 
111 // #### Powerflow section ####
112 
113 void SP::Ph1::Capacitor::setBaseVoltage(Real baseVoltage) {
114  mBaseVoltage = baseVoltage;
115 }
116 
118  SPDLOG_LOGGER_INFO(mSLog, "#### Calculate Per Unit Parameters for {}",
119  **mName);
120  mBaseApparentPower = baseApparentPower;
121  SPDLOG_LOGGER_INFO(mSLog, "Base Power={} [VA]", baseApparentPower);
122 
123  mBaseImpedance = mBaseVoltage * mBaseVoltage / mBaseApparentPower;
124  mBaseAdmittance = 1.0 / mBaseImpedance;
125  mBaseCurrent = baseApparentPower /
126  (mBaseVoltage *
127  sqrt(3)); // I_base=(S_threephase/3)/(V_line_to_line/sqrt(3))
128  SPDLOG_LOGGER_INFO(mSLog, "Base Voltage={} [V] Base Impedance={} [Ohm]",
129  mBaseVoltage, mBaseImpedance);
130 
131  mImpedancePerUnit = mImpedance / mBaseImpedance;
132  mAdmittancePerUnit = 1. / mImpedancePerUnit;
133  SPDLOG_LOGGER_INFO(mSLog, "Impedance={} [pu] Admittance={} [pu]",
134  Logger::complexToString(mImpedancePerUnit),
135  Logger::complexToString(mAdmittancePerUnit));
136 }
137 
139  int bus1 = this->matrixNodeIndex(0);
140 
141  if (std::isinf(mAdmittancePerUnit.real()) ||
142  std::isinf(mAdmittancePerUnit.imag())) {
143  std::cout << "Y:" << mAdmittancePerUnit << std::endl;
144  std::stringstream ss;
145  ss << "Capacitor >>" << this->name()
146  << ": infinite or nan values at node: " << bus1;
147  throw std::invalid_argument(ss.str());
148  }
149 
150  //set the circuit matrix values
151  Y.coeffRef(bus1, bus1) += mAdmittancePerUnit;
152  SPDLOG_LOGGER_INFO(mSLog, "#### Y matrix stamping: {}", mAdmittancePerUnit);
153 }
Base class for all MNA components that are transmitting power.
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 mnaCompUpdateVoltage(const Matrix &leftVector) override
Update interface voltage from MNA system result.
void setBaseVoltage(Real baseVoltage)
Set base voltage.
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.
Capacitor(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
void calculatePerUnitParameters(Real baseApparentPower)
Initializes component from power flow data.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
Initializes internal variables of the component.
void pfApplyAdmittanceMatrixStamp(SparseMatrixCompRow &Y) override
Stamps admittance matrix.
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