DPsim
SP_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/SP/SP_Ph3_Resistor.h>
10 
11 using namespace CPS;
12 
13 SP::Ph3::Resistor::Resistor(String uid, String name, Logger::Level logLevel)
14  : MNASimPowerComp<Complex>(uid, name, false, true, logLevel),
15  Base::Ph3::Resistor(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 = Resistor::make(name, mLogLevel);
24  copy->setParameters(**mResistance);
25  return copy;
26 }
27 
29 
30  Real voltMag = Math::abs((**mIntfVoltage)(0, 0));
31  Real voltPhase = Math::phase((**mIntfVoltage)(0, 0));
32  (**mIntfVoltage)(1, 0) = Complex(voltMag * cos(voltPhase - 2. / 3. * M_PI),
33  voltMag * sin(voltPhase - 2. / 3. * M_PI));
34  (**mIntfVoltage)(2, 0) = Complex(voltMag * cos(voltPhase + 2. / 3. * M_PI),
35  voltMag * sin(voltPhase + 2. / 3. * M_PI));
36  **mIntfCurrent = (**mResistance).inverse() * **mIntfVoltage;
37 
38  SPDLOG_LOGGER_INFO(mSLog, "Node 1 : {}",
39  Logger::phasorToString(initialVoltage(0)(0, 0)));
40  SPDLOG_LOGGER_INFO(mSLog, "Node 2 : {}",
41  Logger::phasorToString(initialVoltage(1)(0, 0)));
42  SPDLOG_LOGGER_INFO(mSLog, "initialize {} {} voltage to {} and current to {}",
43  this->type(), this->name(),
44  Logger::phasorToString((**mIntfVoltage)(0, 0)),
45  Logger::phasorToString((**mIntfCurrent)(0, 0)));
46 }
47 
48 void SP::Ph3::Resistor::mnaCompInitialize(Real omega, Real timeStep,
49  Attribute<Matrix>::Ptr leftVector) {
50  updateMatrixNodeIndices();
51  **mRightVector = Matrix::Zero(0, 0);
52 }
53 
55  SparseMatrixRow &systemMatrix) {
56  MatrixFixedSizeComp<3, 3> conductance = Matrix::Zero(3, 3);
57  conductance.real() = (**mResistance).inverse();
58 
59  MNAStampUtils::stampAdmittanceMatrix(
60  conductance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1),
61  terminalNotGrounded(0), terminalNotGrounded(1), mSLog);
62 }
63 
65  AttributeBase::List &prevStepDependencies,
66  AttributeBase::List &attributeDependencies,
67  AttributeBase::List &modifiedAttributes,
68  Attribute<Matrix>::Ptr &leftVector) {
69  attributeDependencies.push_back(leftVector);
70  modifiedAttributes.push_back(mIntfVoltage);
71  modifiedAttributes.push_back(mIntfCurrent);
72 }
73 
74 void SP::Ph3::Resistor::mnaCompPostStep(Real time, Int timeStepCount,
75  Attribute<Matrix>::Ptr &leftVector) {
76  mnaCompUpdateVoltage(**leftVector);
77  mnaCompUpdateCurrent(**leftVector);
78 }
79 
80 void SP::Ph3::Resistor::mnaCompUpdateVoltage(const Matrix &leftVector) {
81  // Voltage across component is defined as V1 - V0
82  **mIntfVoltage = MatrixComp::Zero(3, 1);
83  if (terminalNotGrounded(1)) {
84  (**mIntfVoltage)(0, 0) =
85  Math::complexFromVectorElement(leftVector, matrixNodeIndex(1, 0));
86  (**mIntfVoltage)(1, 0) =
87  Math::complexFromVectorElement(leftVector, matrixNodeIndex(1, 1));
88  (**mIntfVoltage)(2, 0) =
89  Math::complexFromVectorElement(leftVector, matrixNodeIndex(1, 2));
90  }
91  if (terminalNotGrounded(0)) {
92  (**mIntfVoltage)(0, 0) =
93  (**mIntfVoltage)(0, 0) -
94  Math::complexFromVectorElement(leftVector, matrixNodeIndex(0, 0));
95  (**mIntfVoltage)(1, 0) =
96  (**mIntfVoltage)(1, 0) -
97  Math::complexFromVectorElement(leftVector, matrixNodeIndex(0, 1));
98  (**mIntfVoltage)(2, 0) =
99  (**mIntfVoltage)(2, 0) -
100  Math::complexFromVectorElement(leftVector, matrixNodeIndex(0, 2));
101  }
102 
103  //mLog.debug() << "Voltage A: " << std::abs((**mIntfVoltage)(0, 0))
104  // << "<" << std::arg((**mIntfVoltage)(0, 0)) << std::endl;
105 }
106 
107 void SP::Ph3::Resistor::mnaCompUpdateCurrent(const Matrix &leftVector) {
108  **mIntfCurrent = (**mResistance).inverse() * **mIntfVoltage;
109  //mLog.debug() << "Current A: " << std::abs((**mIntfCurrent)(0, 0))
110  // << "<" << std::arg((**mIntfCurrent)(0, 0)) << std::endl;
111 }
112 
113 void SP::Ph3::Resistor::mnaTearApplyMatrixStamp(SparseMatrixRow &tearMatrix) {
114  // TODO
115  Math::addToMatrixElement(tearMatrix, mTearIdx, mTearIdx,
116  Complex((**mResistance)(0, 0), 0));
117  Math::addToMatrixElement(tearMatrix, mTearIdx, mTearIdx,
118  Complex((**mResistance)(1, 1), 0));
119  Math::addToMatrixElement(tearMatrix, mTearIdx, mTearIdx,
120  Complex((**mResistance)(2, 2), 0));
121 }
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 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.
Resistor(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamps system 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