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