DPsim
EMT_Ph3_Resistor.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_Resistor.h>
10 
11 using namespace CPS;
12 
13 EMT::Ph3::Resistor::Resistor(String uid, String name, Logger::Level logLevel)
14  : MNASimPowerComp<Real>(uid, name, false, true, logLevel),
15  Base::Ph3::Resistor(mAttributes) {
16  mPhaseType = PhaseType::ABC;
17  setTerminalNumber(2);
18  **mIntfVoltage = Matrix::Zero(3, 1);
19  **mIntfCurrent = Matrix::Zero(3, 1);
20 }
21 
23  auto copy = Resistor::make(name, mLogLevel);
24  copy->setParameters(**mResistance);
25  return copy;
26 }
27 
29 
30  // IntfVoltage initialization for each phase
31  MatrixComp vInitABC = Matrix::Zero(3, 1);
32  vInitABC(0, 0) = RMS3PH_TO_PEAK1PH * initialSingleVoltage(1) -
33  RMS3PH_TO_PEAK1PH * initialSingleVoltage(0);
34  vInitABC(1, 0) = vInitABC(0, 0) * SHIFT_TO_PHASE_B;
35  vInitABC(2, 0) = vInitABC(0, 0) * SHIFT_TO_PHASE_C;
36  **mIntfVoltage = vInitABC.real();
37 
38  // TODO: here, the custom implementation for matrix inversion is still used, because mResistance is still a generic matrix.
39  // Change the definition in future, if valid. The author currently doesn't know if this is allowed (mResistance is defined in Base)
40  // This is also done in the following
41  Matrix mResistanceInv = Matrix::Zero(3, 3);
42  Math::invertMatrix(**mResistance, mResistanceInv);
43  **mIntfCurrent = (mResistanceInv * vInitABC).real();
44 
45  SPDLOG_LOGGER_INFO(mSLog,
46  "\nResistance [Ohm]: {:s}"
47  "\nConductance [S]: {:s}",
48  Logger::matrixToString(**mResistance),
49  Logger::matrixToString(mResistanceInv));
50  SPDLOG_LOGGER_INFO(
51  mSLog,
52  "\n--- Initialization from powerflow ---"
53  "\nVoltage across: {:s}"
54  "\nCurrent: {:s}"
55  "\nTerminal 0 voltage: {:s}"
56  "\nTerminal 1 voltage: {:s}"
57  "\n--- Initialization from powerflow finished ---",
58  Logger::matrixToString(**mIntfVoltage),
59  Logger::matrixToString(**mIntfCurrent),
60  Logger::phasorToString(RMS3PH_TO_PEAK1PH * initialSingleVoltage(0)),
61  Logger::phasorToString(RMS3PH_TO_PEAK1PH * initialSingleVoltage(1)));
62 }
63 
64 void EMT::Ph3::Resistor::mnaCompInitialize(Real omega, Real timeStep,
65  Attribute<Matrix>::Ptr leftVector) {
66  updateMatrixNodeIndices();
67  **mRightVector = Matrix::Zero(0, 0);
68 }
69 
71  SparseMatrixRow &systemMatrix) {
72  MatrixFixedSize<3, 3> conductance = Matrix::Zero(3, 3);
73  conductance = (**mResistance).inverse();
74 
75  MNAStampUtils::stampConductanceMatrix(
76  conductance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1),
77  terminalNotGrounded(0), terminalNotGrounded(1), mSLog);
78 
79  SPDLOG_LOGGER_INFO(mSLog, "\nConductance matrix: {:s}",
80  Logger::matrixToString(conductance));
81 }
82 
84  AttributeBase::List &prevStepDependencies,
85  AttributeBase::List &attributeDependencies,
86  AttributeBase::List &modifiedAttributes,
87  Attribute<Matrix>::Ptr &leftVector) {
88  attributeDependencies.push_back(leftVector);
89  modifiedAttributes.push_back(mIntfVoltage);
90  modifiedAttributes.push_back(mIntfCurrent);
91 }
92 
93 void EMT::Ph3::Resistor::mnaCompPostStep(Real time, Int timeStepCount,
94  Attribute<Matrix>::Ptr &leftVector) {
95  mnaCompUpdateVoltage(**leftVector);
96  mnaCompUpdateCurrent(**leftVector);
97 }
98 
99 void EMT::Ph3::Resistor::mnaCompUpdateVoltage(const Matrix &leftVector) {
100  // v1 - v0
101  **mIntfVoltage = Matrix::Zero(3, 1);
102  if (terminalNotGrounded(1)) {
103  (**mIntfVoltage)(0, 0) =
104  Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 0));
105  (**mIntfVoltage)(1, 0) =
106  Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 1));
107  (**mIntfVoltage)(2, 0) =
108  Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 2));
109  }
110  if (terminalNotGrounded(0)) {
111  (**mIntfVoltage)(0, 0) =
112  (**mIntfVoltage)(0, 0) -
113  Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 0));
114  (**mIntfVoltage)(1, 0) =
115  (**mIntfVoltage)(1, 0) -
116  Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 1));
117  (**mIntfVoltage)(2, 0) =
118  (**mIntfVoltage)(2, 0) -
119  Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 2));
120  }
121  SPDLOG_LOGGER_DEBUG(mSLog, "\nVoltage: {:s}",
122  Logger::matrixToString(**mIntfVoltage));
123  mSLog->flush();
124 }
125 
126 void EMT::Ph3::Resistor::mnaCompUpdateCurrent(const Matrix &leftVector) {
127  Matrix resistanceInv = Matrix::Zero(3, 3);
128  Math::invertMatrix(**mResistance, resistanceInv);
129  **mIntfCurrent = resistanceInv * **mIntfVoltage;
130  SPDLOG_LOGGER_DEBUG(mSLog, "\nCurrent: {:s}",
131  Logger::matrixToString(**mIntfCurrent));
132  mSLog->flush();
133 }
void initializeFromNodesAndTerminals(Real frequency)
Initializes component from power flow data.
void mnaCompUpdateCurrent(const Matrix &leftVector)
Update interface current from MNA system result.
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftSideVector)
Initializes internal variables of the component.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector)
add MNA pre and post step dependencies
void mnaCompUpdateVoltage(const Matrix &leftVector)
Update interface voltage from MNA system result.
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix)
Stamps system matrix.
SimPowerComp< Real >::Ptr clone(String name)
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)
MNA pre and post step operations.
Resistor(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