DPsim
EMT_Ph3_SeriesResistor.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_SeriesResistor.h>
10 
11 using namespace CPS;
12 
13 // !!! TODO: Adaptions to use in EMT_Ph3 models phase-to-ground peak variables
14 // !!! with initialization from phase-to-phase RMS variables
15 
17  Logger::Level logLevel)
18  : MNASimPowerComp<Real>(uid, name, false, true, logLevel),
19  Base::Ph1::Resistor(mAttributes) {
20 
21  mPhaseType = PhaseType::ABC;
22  setTerminalNumber(2);
23 }
24 
26  auto copy = SeriesResistor::make(name, mLogLevel);
27  copy->setParameters(**mResistance);
28  return copy;
29 }
30 
32 
33  Complex phasorA = initialSingleVoltage(1) - initialSingleVoltage(0);
34  (**mIntfVoltage)(0, 0) = phasorA.real();
35  Complex alpha(cos(2. / 3. * PI), sin(2. / 3. * PI));
36  (**mIntfVoltage)(1, 0) = Complex(phasorA * pow(alpha, 2)).real();
37  (**mIntfVoltage)(2, 0) = Complex(phasorA * alpha).real();
38 
39  **mIntfCurrent = **mIntfVoltage / **mResistance;
40 
41  SPDLOG_LOGGER_INFO(mSLog,
42  "\n--- Initialization from powerflow ---"
43  "\nVoltage across amplitude and phase: \n{}"
44  "\nCurrent amplitude and phase: \n{}"
45  "\nTerminal 0 voltage amplitude and phase: \n{}"
46  "\nTerminal 1 voltage amplitude and phase: \n{}"
47  "\n--- Initialization from powerflow finished ---",
48  Logger::phasorMatrixToString(**mIntfVoltage),
49  Logger::phasorMatrixToString(**mIntfCurrent),
50  Logger::phasorMatrixToString(initialVoltage(0)),
51  Logger::phasorMatrixToString(initialVoltage(1)));
52 }
53 
55  Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
56  updateMatrixNodeIndices();
57  **mRightVector = Matrix::Zero(0, 0);
58 }
59 
61  SparseMatrixRow &systemMatrix) {
62  Real conductance = 1. / **mResistance;
63 
64  // Set diagonal entries
65  if (terminalNotGrounded(0))
66  Math::addToMatrixElement(systemMatrix, matrixNodeIndices(0),
67  matrixNodeIndices(0), conductance);
68  if (terminalNotGrounded(1))
69  Math::addToMatrixElement(systemMatrix, matrixNodeIndices(1),
70  matrixNodeIndices(1), conductance);
71  // Set off diagonal entries
72  if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
73  Math::addToMatrixElement(systemMatrix, matrixNodeIndices(0),
74  matrixNodeIndices(1), -conductance);
75  Math::addToMatrixElement(systemMatrix, matrixNodeIndices(1),
76  matrixNodeIndices(0), -conductance);
77  }
78 
79  if (terminalNotGrounded(0))
80  SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", conductance,
81  matrixNodeIndex(0, 0), matrixNodeIndex(0, 0));
82  if (terminalNotGrounded(1))
83  SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", conductance,
84  matrixNodeIndex(1, 0), matrixNodeIndex(1, 0));
85  if (terminalNotGrounded(0) && terminalNotGrounded(1)) {
86  SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", -conductance,
87  matrixNodeIndex(0, 0), matrixNodeIndex(1, 0));
88  SPDLOG_LOGGER_INFO(mSLog, "Add {} to {}, {}", -conductance,
89  matrixNodeIndex(1, 0), matrixNodeIndex(0, 0));
90  }
91 }
92 
94  AttributeBase::List &prevStepDependencies,
95  AttributeBase::List &attributeDependencies,
96  AttributeBase::List &modifiedAttributes,
97  Attribute<Matrix>::Ptr &leftVector) {
98 
99  attributeDependencies.push_back(leftVector);
100  modifiedAttributes.push_back(mIntfVoltage);
101  modifiedAttributes.push_back(mIntfCurrent);
102 }
103 
104 void EMT::Ph3::SeriesResistor::mnaCompPostStep(
105  Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
106  mnaCompUpdateVoltage(**leftVector);
107  mnaCompUpdateCurrent(**leftVector);
108 }
109 
110 void EMT::Ph3::SeriesResistor::mnaCompUpdateVoltage(const Matrix &leftVector) {
111  // Voltage across component is defined as V1 - V0
112  **mIntfVoltage = Matrix::Zero(3, 1);
113  if (terminalNotGrounded(1)) {
114  (**mIntfVoltage)(0, 0) =
115  Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 0));
116  (**mIntfVoltage)(1, 0) =
117  Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 1));
118  (**mIntfVoltage)(2, 0) =
119  Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 2));
120  }
121  if (terminalNotGrounded(0)) {
122  (**mIntfVoltage)(0, 0) =
123  (**mIntfVoltage)(0, 0) -
124  Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 0));
125  (**mIntfVoltage)(1, 0) =
126  (**mIntfVoltage)(1, 0) -
127  Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 1));
128  (**mIntfVoltage)(2, 0) =
129  (**mIntfVoltage)(2, 0) -
130  Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 2));
131  }
132 
133  SPDLOG_LOGGER_DEBUG(mSLog, "Voltage A: {}", (**mIntfVoltage)(0, 0));
134 }
135 
136 void EMT::Ph3::SeriesResistor::mnaCompUpdateCurrent(const Matrix &leftVector) {
137  **mIntfCurrent = **mIntfVoltage / **mResistance;
138 
139  SPDLOG_LOGGER_DEBUG(mSLog, "Current A: {} < {}", (**mIntfCurrent)(0, 0));
140 }
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Update interface voltage from MNA system results.
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
Initializes MNA specific variables.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
SimPowerComp< Real >::Ptr clone(String name) override
Return new instance with the same parameters.
SeriesResistor(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and log level.
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamps system matrix.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes states from power flow data.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Update interface voltage from MNA system results.
Base class for all MNA components that are transmitting power.
Base class for all components that are transmitting power.
Definition: SimPowerComp.h:17