DPsim
EMT_Ph1_Switch.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_Switch.h>
10 
11 using namespace CPS;
12 
13 EMT::Ph1::Switch::Switch(String uid, String name, Logger::Level logLevel)
14  : MNASimPowerComp<Real>(uid, name, false, true, logLevel),
15  Base::Ph1::Switch(mAttributes) {
16  setTerminalNumber(2);
17  **mIntfVoltage = Matrix::Zero(1, 1);
18  **mIntfCurrent = Matrix::Zero(1, 1);
19 }
20 
22  auto copy = Switch::make(name, mLogLevel);
23  copy->setParameters(**mOpenResistance, **mClosedResistance, **mIsClosed);
24  return copy;
25 }
26 
28 
29  Real resistance = (**mIsClosed) ? **mClosedResistance : **mOpenResistance;
30 
31  (**mIntfVoltage)(0, 0) =
32  RMS3PH_TO_PEAK1PH * (initialSingleVoltage(1) - initialSingleVoltage(0)).real();
33  (**mIntfCurrent)(0, 0) = (**mIntfVoltage)(0, 0) / resistance;
34 
35  SPDLOG_LOGGER_INFO(mSLog,
36  "\n--- Initialization from powerflow ---"
37  "\nVoltage across: {:s}"
38  "\nCurrent: {:s}"
39  "\nTerminal 0 voltage: {:s}"
40  "\nTerminal 1 voltage: {:s}"
41  "\n--- Initialization from powerflow finished ---",
42  Logger::matrixToString(**mIntfVoltage),
43  Logger::matrixToString(**mIntfCurrent),
44  Logger::phasorToString(RMS3PH_TO_PEAK1PH * initialSingleVoltage(0).real()),
45  Logger::phasorToString(RMS3PH_TO_PEAK1PH * initialSingleVoltage(1).real()));
46 }
47 
48 void EMT::Ph1::Switch::mnaCompInitialize(Real omega, Real timeStep,
49  Attribute<Matrix>::Ptr leftVector) {
50  updateMatrixNodeIndices();
51  mMnaTasks.push_back(std::make_shared<MnaPostStep>(*this, leftVector));
52 }
53 
54 Bool EMT::Ph1::Switch::mnaIsClosed() { return **mIsClosed; }
55 
57  SparseMatrixRow &systemMatrix) {
58  Real conductance;
59 
60  conductance =
61  (**mIsClosed) ? 1. / (**mClosedResistance) : 1. / (**mOpenResistance);
62 
63  MNAStampUtils::stampConductance(conductance, systemMatrix, matrixNodeIndex(0),
64  matrixNodeIndex(1), terminalNotGrounded(0),
65  terminalNotGrounded(1), mSLog);
66 }
67 
69  Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) {
70  Real conductance;
71 
72  conductance =
73  (closed) ? 1. / (**mClosedResistance) : 1. / (**mOpenResistance);
74 
75  MNAStampUtils::stampConductance(conductance, systemMatrix, matrixNodeIndex(0),
76  matrixNodeIndex(1), terminalNotGrounded(0),
77  terminalNotGrounded(1), mSLog);
78 }
79 
81 
83  AttributeBase::List &prevStepDependencies,
84  AttributeBase::List &attributeDependencies,
85  AttributeBase::List &modifiedAttributes,
86  Attribute<Matrix>::Ptr &leftVector) {
87  attributeDependencies.push_back(leftVector);
88  modifiedAttributes.push_back(mIntfVoltage);
89  modifiedAttributes.push_back(mIntfCurrent);
90 }
91 
92 void EMT::Ph1::Switch::mnaCompPostStep(Real time, Int timeStepCount,
93  Attribute<Matrix>::Ptr &leftVector) {
94  mnaCompUpdateVoltage(**leftVector);
95  mnaCompUpdateCurrent(**leftVector);
96 }
97 
98 void EMT::Ph1::Switch::mnaCompUpdateVoltage(const Matrix &leftVector) {
99  // Voltage across component is defined as V1 - V0
100  **mIntfVoltage = Matrix::Zero(1, 1);
101  if (terminalNotGrounded(1)) {
102  (**mIntfVoltage)(0, 0) =
103  Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 0));
104  }
105  if (terminalNotGrounded(0)) {
106  (**mIntfVoltage)(0, 0) =
107  (**mIntfVoltage)(0, 0) -
108  Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 0));
109  }
110 }
111 
112 void EMT::Ph1::Switch::mnaCompUpdateCurrent(const Matrix &leftVector) {
113  (**mIntfCurrent)(0, 0) = (**mIsClosed)
114  ? (**mIntfVoltage)(0, 0) / (**mClosedResistance)
115  : (**mIntfVoltage)(0, 0) / (**mOpenResistance);
116 }
One phase EMT switch.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Update interface current from MNA system result.
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamps system matrix.
void mnaCompApplySwitchSystemMatrixStamp(Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) override
Stamps system matrix considering the defined switch position.
Bool mnaIsClosed() override
Check if switch is closed.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Update interface voltage 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.
Switch(String uid, String name, Logger::Level loglevel=Logger::Level::off)
Defines UID, name, component parameters and logging level.
void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
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