DPsim
EMT_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/EMT/EMT_Ph1_Resistor.h>
10 
11 using namespace CPS;
12 
13 EMT::Ph1::Resistor::Resistor(String uid, String name, Logger::Level logLevel)
14  : MNASimPowerComp<Real>(uid, name, false, true, logLevel),
15  Base::Ph1::Resistor(mAttributes) {
16  setTerminalNumber(2);
17  **mIntfVoltage = Matrix::Zero(1, 1);
18  **mIntfCurrent = Matrix::Zero(1, 1);
19 }
20 
22  auto copy = Resistor::make(name, mLogLevel);
23  copy->setParameters(**mResistance);
24  return copy;
25 }
26 
28  Complex voltage =
29  RMS3PH_TO_PEAK1PH * (initialSingleVoltage(1) - initialSingleVoltage(0));
30  (**mIntfVoltage)(0, 0) = voltage.real();
31  (**mIntfCurrent)(0, 0) = (**mIntfVoltage)(0, 0) / **mResistance;
32 
33  SPDLOG_LOGGER_INFO(mSLog,
34  "\n--- Initialization from powerflow ---"
35  "\nVoltage across: {:f}"
36  "\nCurrent: {:f}"
37  "\nTerminal 0 voltage: {:f}"
38  "\nTerminal 1 voltage: {:f}"
39  "\n--- Initialization from powerflow finished ---",
40  (**mIntfVoltage)(0, 0), (**mIntfCurrent)(0, 0),
41  (RMS3PH_TO_PEAK1PH * initialSingleVoltage(0)).real(),
42  (RMS3PH_TO_PEAK1PH * initialSingleVoltage(1)).real());
43  mSLog->flush();
44 }
45 
46 void EMT::Ph1::Resistor::mnaCompInitialize(Real omega, Real timeStep,
47  Attribute<Matrix>::Ptr leftVector) {
48  updateMatrixNodeIndices();
49  **mRightVector = Matrix::Zero(0, 0);
50 }
51 
53  SparseMatrixRow &systemMatrix) {
54  Real conductance = 1. / **mResistance;
55  MNAStampUtils::stampConductance(conductance, systemMatrix, matrixNodeIndex(0),
56  matrixNodeIndex(1), terminalNotGrounded(0),
57  terminalNotGrounded(1), mSLog);
58 }
59 
61  AttributeBase::List &prevStepDependencies,
62  AttributeBase::List &attributeDependencies,
63  AttributeBase::List &modifiedAttributes,
64  Attribute<Matrix>::Ptr &leftVector) {
65  attributeDependencies.push_back(leftVector);
66  modifiedAttributes.push_back(mIntfVoltage);
67  modifiedAttributes.push_back(mIntfCurrent);
68 }
69 
70 void EMT::Ph1::Resistor::mnaCompPostStep(Real time, Int timeStepCount,
71  Attribute<Matrix>::Ptr &leftVector) {
72  mnaCompUpdateVoltage(**leftVector);
73  mnaCompUpdateCurrent(**leftVector);
74 }
75 
76 void EMT::Ph1::Resistor::mnaCompUpdateVoltage(const Matrix &leftVector) {
77  // v1 - v0
78  (**mIntfVoltage)(0, 0) = 0;
79  if (terminalNotGrounded(1))
80  (**mIntfVoltage)(0, 0) =
81  Math::realFromVectorElement(leftVector, matrixNodeIndex(1));
82  if (terminalNotGrounded(0))
83  (**mIntfVoltage)(0, 0) =
84  (**mIntfVoltage)(0, 0) -
85  Math::realFromVectorElement(leftVector, matrixNodeIndex(0));
86 }
87 
88 void EMT::Ph1::Resistor::mnaCompUpdateCurrent(const Matrix &leftVector) {
89  (**mIntfCurrent)(0, 0) = (**mIntfVoltage)(0, 0) / **mResistance;
90 }
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftSideVector) override
Initializes internal variables of the component.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Update interface current from MNA system result.
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
Returns a modified copy of the component with the given suffix added to the name and without.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Update interface voltage from MNA system result.
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamps system matrix.
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