DPsim
EMT_Ph3_Inductor.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_Inductor.h>
10 
11 using namespace CPS;
12 
13 EMT::Ph3::Inductor::Inductor(String uid, String name, Logger::Level logLevel)
14  : MNASimPowerComp<Real>(uid, name, true, true, logLevel),
15  Base::Ph3::Inductor(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 = Inductor::make(name, mLogLevel);
25  copy->setParameters(**mInductance);
26  return copy;
27 }
28 
30 
31  Real omega = 2 * PI * frequency;
32  MatrixComp impedance = MatrixComp::Zero(3, 3);
33  impedance << Complex(0, omega * (**mInductance)(0, 0)),
34  Complex(0, omega * (**mInductance)(0, 1)),
35  Complex(0, omega * (**mInductance)(0, 2)),
36  Complex(0, omega * (**mInductance)(1, 0)),
37  Complex(0, omega * (**mInductance)(1, 1)),
38  Complex(0, omega * (**mInductance)(1, 2)),
39  Complex(0, omega * (**mInductance)(2, 0)),
40  Complex(0, omega * (**mInductance)(2, 1)),
41  Complex(0, omega * (**mInductance)(2, 2));
42 
43  MatrixComp vInitABC = Matrix::Zero(3, 1);
44  vInitABC(0, 0) = RMS3PH_TO_PEAK1PH * initialSingleVoltage(1) -
45  RMS3PH_TO_PEAK1PH * initialSingleVoltage(0);
46  vInitABC(1, 0) = vInitABC(0, 0) * SHIFT_TO_PHASE_B;
47  vInitABC(2, 0) = vInitABC(0, 0) * SHIFT_TO_PHASE_C;
48  **mIntfVoltage = vInitABC.real();
49  MatrixComp admittance = impedance.inverse();
50  **mIntfCurrent = (admittance * vInitABC).real();
51 
52  SPDLOG_LOGGER_INFO(mSLog,
53  "\nInductance [H]: {:s}"
54  "\nImpedance [Ohm]: {:s}",
55  Logger::matrixToString(**mInductance),
56  Logger::matrixCompToString(impedance));
57  SPDLOG_LOGGER_INFO(
58  mSLog,
59  "\n--- Initialization from powerflow ---"
60  "\nVoltage across: {:s}"
61  "\nCurrent: {:s}"
62  "\nTerminal 0 voltage: {:s}"
63  "\nTerminal 1 voltage: {:s}"
64  "\n--- Initialization from powerflow finished ---",
65  Logger::matrixToString(**mIntfVoltage),
66  Logger::matrixToString(**mIntfCurrent),
67  Logger::phasorToString(RMS3PH_TO_PEAK1PH * initialSingleVoltage(0)),
68  Logger::phasorToString(RMS3PH_TO_PEAK1PH * initialSingleVoltage(1)));
69 }
70 
71 void EMT::Ph3::Inductor::mnaCompInitialize(Real omega, Real timeStep,
72  Attribute<Matrix>::Ptr leftVector) {
73 
74  updateMatrixNodeIndices();
75  mEquivCond = timeStep / 2. * (**mInductance).inverse();
76  // Update internal state
77  mEquivCurrent = mEquivCond * **mIntfVoltage + **mIntfCurrent;
78 
79  SPDLOG_LOGGER_INFO(mSLog,
80  "\n--- MNA initialization ---"
81  "\nInitial voltage {:s}"
82  "\nInitial current {:s}"
83  "\nEquiv. current {:s}"
84  "\n--- MNA initialization finished ---",
85  Logger::matrixToString(**mIntfVoltage),
86  Logger::matrixToString(**mIntfCurrent),
87  Logger::matrixToString(mEquivCurrent));
88  mSLog->flush();
89 }
90 
92  SparseMatrixRow &systemMatrix) {
93  MNAStampUtils::stampConductanceMatrix(
94  mEquivCond, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1),
95  terminalNotGrounded(0), terminalNotGrounded(1), mSLog);
96 
97  SPDLOG_LOGGER_INFO(mSLog, "\nEquivalent Conductance: {:s}",
98  Logger::matrixToString(mEquivCond));
99 }
100 
102  // Update internal state
103  mEquivCurrent = mEquivCond * **mIntfVoltage + **mIntfCurrent;
104  if (terminalNotGrounded(0)) {
105  Math::setVectorElement(rightVector, matrixNodeIndex(0, 0),
106  mEquivCurrent(0, 0));
107  Math::setVectorElement(rightVector, matrixNodeIndex(0, 1),
108  mEquivCurrent(1, 0));
109  Math::setVectorElement(rightVector, matrixNodeIndex(0, 2),
110  mEquivCurrent(2, 0));
111  }
112  if (terminalNotGrounded(1)) {
113  Math::setVectorElement(rightVector, matrixNodeIndex(1, 0),
114  -mEquivCurrent(0, 0));
115  Math::setVectorElement(rightVector, matrixNodeIndex(1, 1),
116  -mEquivCurrent(1, 0));
117  Math::setVectorElement(rightVector, matrixNodeIndex(1, 2),
118  -mEquivCurrent(2, 0));
119  }
120  SPDLOG_LOGGER_DEBUG(
121  mSLog, "\nEquivalent Current (mnaCompApplyRightSideVectorStamp): {:s}",
122  Logger::matrixToString(mEquivCurrent));
123  mSLog->flush();
124 }
125 
127  AttributeBase::List &prevStepDependencies,
128  AttributeBase::List &attributeDependencies,
129  AttributeBase::List &modifiedAttributes) {
130  // actually depends on L, but then we'd have to modify the system matrix anyway
131  prevStepDependencies.push_back(mIntfVoltage);
132  prevStepDependencies.push_back(mIntfCurrent);
133  modifiedAttributes.push_back(mRightVector);
134 }
135 
136 void EMT::Ph3::Inductor::mnaCompPreStep(Real time, Int timeStepCount) {
137  mnaCompApplyRightSideVectorStamp(**mRightVector);
138 }
139 
141  AttributeBase::List &prevStepDependencies,
142  AttributeBase::List &attributeDependencies,
143  AttributeBase::List &modifiedAttributes,
144  Attribute<Matrix>::Ptr &leftVector) {
145  attributeDependencies.push_back(leftVector);
146  modifiedAttributes.push_back(mIntfVoltage);
147  modifiedAttributes.push_back(mIntfCurrent);
148 }
149 
150 void EMT::Ph3::Inductor::mnaCompPostStep(Real time, Int timeStepCount,
151  Attribute<Matrix>::Ptr &leftVector) {
152  mnaCompUpdateVoltage(**leftVector);
153  mnaCompUpdateCurrent(**leftVector);
154 }
155 
156 void EMT::Ph3::Inductor::mnaCompUpdateVoltage(const Matrix &leftVector) {
157  // v1 - v0
158  **mIntfVoltage = Matrix::Zero(3, 1);
159  if (terminalNotGrounded(1)) {
160  (**mIntfVoltage)(0, 0) =
161  Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 0));
162  (**mIntfVoltage)(1, 0) =
163  Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 1));
164  (**mIntfVoltage)(2, 0) =
165  Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 2));
166  }
167  if (terminalNotGrounded(0)) {
168  (**mIntfVoltage)(0, 0) =
169  (**mIntfVoltage)(0, 0) -
170  Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 0));
171  (**mIntfVoltage)(1, 0) =
172  (**mIntfVoltage)(1, 0) -
173  Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 1));
174  (**mIntfVoltage)(2, 0) =
175  (**mIntfVoltage)(2, 0) -
176  Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 2));
177  }
178  SPDLOG_LOGGER_DEBUG(mSLog, "\nUpdate Voltage: {:s}",
179  Logger::matrixToString(**mIntfVoltage));
180 }
181 
182 void EMT::Ph3::Inductor::mnaCompUpdateCurrent(const Matrix &leftVector) {
183  **mIntfCurrent = mEquivCond * **mIntfVoltage + mEquivCurrent;
184  SPDLOG_LOGGER_DEBUG(mSLog, "\nUpdate Current: {:s}",
185  Logger::matrixToString(**mIntfCurrent));
186  mSLog->flush();
187 }
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 mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
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 mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Update interface current from MNA system result.
Matrix mEquivCurrent
DC equivalent current source [A].
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamps system matrix.
void mnaCompPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
Inductor(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name, component parameters and logging level.
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