DPsim
Loading...
Searching...
No Matches
EMT_Ph3_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_Ph3_Switch.h>
10
11using 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
16EMT::Ph3::Switch::Switch(String uid, String name, Logger::Level logLevel)
17 : MNASimPowerComp<Real>(uid, name, false, true, logLevel),
18 Base::Ph3::Switch(mAttributes) {
19 setTerminalNumber(2);
20 **mIntfVoltage = Matrix::Zero(1, 1);
21 **mIntfCurrent = Matrix::Zero(1, 1);
22}
23
24SimPowerComp<Real>::Ptr EMT::Ph3::Switch::clone(String name) {
25 auto copy = Switch::make(name, mLogLevel);
26 copy->setParameters(**mOpenResistance, **mClosedResistance, **mSwitchClosed);
27 return copy;
28}
29
31
32 Matrix impedance =
33 (**mSwitchClosed) ? **mClosedResistance : **mOpenResistance;
34 MatrixComp vInitABC = MatrixComp::Zero(3, 1);
35 vInitABC(0, 0) = initialSingleVoltage(1) - initialSingleVoltage(0);
36 vInitABC(1, 0) = vInitABC(0, 0) * SHIFT_TO_PHASE_B;
37 vInitABC(2, 0) = vInitABC(0, 0) * SHIFT_TO_PHASE_C;
38 **mIntfVoltage = vInitABC.real();
39 **mIntfCurrent = (impedance.inverse() * vInitABC).real();
40
41 SPDLOG_LOGGER_INFO(mSLog,
42 "\n--- Initialization from powerflow ---"
43 "\nVoltage across: {:s}"
44 "\nCurrent: {:s}"
45 "\nTerminal 0 voltage: {:s}"
46 "\nTerminal 1 voltage: {:s}"
47 "\n--- Initialization from powerflow finished ---",
48 Logger::matrixToString(**mIntfVoltage),
49 Logger::matrixToString(**mIntfCurrent),
50 Logger::phasorToString(initialSingleVoltage(0)),
51 Logger::phasorToString(initialSingleVoltage(1)));
52}
53
54void EMT::Ph3::Switch::mnaCompInitialize(Real omega, Real timeStep,
55 Attribute<Matrix>::Ptr leftVector) {
56 updateMatrixNodeIndices();
57 **mRightVector = Matrix::Zero(0, 0);
58}
59
61
63 SparseMatrixRow &systemMatrix) {
64 MatrixFixedSize<3, 3> conductance = (**mSwitchClosed)
65 ? (**mClosedResistance).inverse()
66 : (**mOpenResistance).inverse();
67
68 MNAStampUtils::stampConductanceMatrix(
69 conductance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1),
70 terminalNotGrounded(0), terminalNotGrounded(1), mSLog);
71
72 SPDLOG_LOGGER_TRACE(mSLog, "\nConductance matrix: {:s}",
73 Logger::matrixToString(conductance));
74}
75
77 Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) {
78 MatrixFixedSize<3, 3> conductance = (closed) ? (**mClosedResistance).inverse()
79 : (**mOpenResistance).inverse();
80
81 MNAStampUtils::stampConductanceMatrix(
82 conductance, systemMatrix, matrixNodeIndex(0), matrixNodeIndex(1),
83 terminalNotGrounded(0), terminalNotGrounded(1), mSLog);
84
85 SPDLOG_LOGGER_TRACE(mSLog, "\nConductance matrix: {:s}",
86 Logger::matrixToString(conductance));
87}
88
90
92 AttributeBase::List &prevStepDependencies,
93 AttributeBase::List &attributeDependencies,
94 AttributeBase::List &modifiedAttributes,
95 Attribute<Matrix>::Ptr &leftVector) {
96 attributeDependencies.push_back(leftVector);
97 modifiedAttributes.push_back(mIntfVoltage);
98 modifiedAttributes.push_back(mIntfCurrent);
99}
100
101void EMT::Ph3::Switch::mnaCompPostStep(Real time, Int timeStepCount,
102 Attribute<Matrix>::Ptr &leftVector) {
103 mnaCompUpdateVoltage(**leftVector);
104 mnaCompUpdateCurrent(**leftVector);
105}
106
107void EMT::Ph3::Switch::mnaCompUpdateVoltage(const Matrix &leftVector) {
108 // Voltage across component is defined as V1 - V0
109 **mIntfVoltage = Matrix::Zero(3, 1);
110 if (terminalNotGrounded(1)) {
111 (**mIntfVoltage)(0, 0) =
112 Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 0));
113 (**mIntfVoltage)(1, 0) =
114 Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 1));
115 (**mIntfVoltage)(2, 0) =
116 Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 2));
117 }
118 if (terminalNotGrounded(0)) {
119 (**mIntfVoltage)(0, 0) =
120 (**mIntfVoltage)(0, 0) -
121 Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 0));
122 (**mIntfVoltage)(1, 0) =
123 (**mIntfVoltage)(1, 0) -
124 Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 1));
125 (**mIntfVoltage)(2, 0) =
126 (**mIntfVoltage)(2, 0) -
127 Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 2));
128 }
129}
130
131void EMT::Ph3::Switch::mnaCompUpdateCurrent(const Matrix &leftVector) {
132 Matrix conductance = Matrix::Zero(3, 3);
133 (**mSwitchClosed) ? Math::invertMatrix(**mClosedResistance, conductance)
134 : Math::invertMatrix(**mOpenResistance, conductance);
135
136 **mIntfCurrent = conductance * **mIntfVoltage;
137}
const CPS::Attribute< Matrix >::Ptr mClosedResistance
Resistance if switch is closed [ohm].
const CPS::Attribute< Bool >::Ptr mSwitchClosed
Defines if Switch is open or closed.
const CPS::Attribute< Matrix >::Ptr mOpenResistance
Resistance if switch is open [ohm].
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Update interface current from MNA system result.
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 mnaCompApplySwitchSystemMatrixStamp(Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) override
Stamps system matrix considering the defined switch position.
Switch(String uid, String name, Logger::Level loglevel=Logger::Level::off)
Defines UID, name, component parameters and logging level.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Update interface voltage from MNA system result.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamps system matrix.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
Bool mnaIsClosed() override
Check if switch is closed.
String uid()
Returns unique id.
AttributeList::Ptr mAttributes
Attribute List.
MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
const Attribute< MatrixVar< Real > >::Ptr mIntfCurrent
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
Logger::Level mLogLevel
Component logger control for internal variables.
Logger::Log mSLog
Component logger.