DPsim
Loading...
Searching...
No Matches
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
11using namespace CPS;
12
13EMT::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
21SimPowerComp<Real>::Ptr EMT::Ph1::Switch::clone(String name) {
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 *
33 (initialSingleVoltage(1) - initialSingleVoltage(0)).real();
34 (**mIntfCurrent)(0, 0) = (**mIntfVoltage)(0, 0) / resistance;
35
36 SPDLOG_LOGGER_INFO(mSLog,
37 "\n--- Initialization from powerflow ---"
38 "\nVoltage across: {:s}"
39 "\nCurrent: {:s}"
40 "\nTerminal 0 voltage: {:s}"
41 "\nTerminal 1 voltage: {:s}"
42 "\n--- Initialization from powerflow finished ---",
43 Logger::matrixToString(**mIntfVoltage),
44 Logger::matrixToString(**mIntfCurrent),
45 Logger::phasorToString(RMS3PH_TO_PEAK1PH *
46 initialSingleVoltage(0).real()),
47 Logger::phasorToString(RMS3PH_TO_PEAK1PH *
48 initialSingleVoltage(1).real()));
49}
50
51void EMT::Ph1::Switch::mnaCompInitialize(Real omega, Real timeStep,
52 Attribute<Matrix>::Ptr leftVector) {
53 updateMatrixNodeIndices();
54 mMnaTasks.push_back(std::make_shared<MnaPostStep>(*this, leftVector));
55}
56
58
60 SparseMatrixRow &systemMatrix) {
61 Real conductance;
62
63 conductance =
64 (**mIsClosed) ? 1. / (**mClosedResistance) : 1. / (**mOpenResistance);
65
66 MNAStampUtils::stampConductance(conductance, systemMatrix, matrixNodeIndex(0),
67 matrixNodeIndex(1), terminalNotGrounded(0),
68 terminalNotGrounded(1), mSLog);
69}
70
72 Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) {
73 Real conductance;
74
75 conductance =
76 (closed) ? 1. / (**mClosedResistance) : 1. / (**mOpenResistance);
77
78 MNAStampUtils::stampConductance(conductance, systemMatrix, matrixNodeIndex(0),
79 matrixNodeIndex(1), terminalNotGrounded(0),
80 terminalNotGrounded(1), mSLog);
81}
82
84
86 AttributeBase::List &prevStepDependencies,
87 AttributeBase::List &attributeDependencies,
88 AttributeBase::List &modifiedAttributes,
89 Attribute<Matrix>::Ptr &leftVector) {
90 attributeDependencies.push_back(leftVector);
91 modifiedAttributes.push_back(mIntfVoltage);
92 modifiedAttributes.push_back(mIntfCurrent);
93}
94
95void EMT::Ph1::Switch::mnaCompPostStep(Real time, Int timeStepCount,
96 Attribute<Matrix>::Ptr &leftVector) {
97 mnaCompUpdateVoltage(**leftVector);
98 mnaCompUpdateCurrent(**leftVector);
99}
100
101void EMT::Ph1::Switch::mnaCompUpdateVoltage(const Matrix &leftVector) {
102 // Voltage across component is defined as V1 - V0
103 **mIntfVoltage = Matrix::Zero(1, 1);
104 if (terminalNotGrounded(1)) {
105 (**mIntfVoltage)(0, 0) =
106 Math::realFromVectorElement(leftVector, matrixNodeIndex(1, 0));
107 }
108 if (terminalNotGrounded(0)) {
109 (**mIntfVoltage)(0, 0) =
110 (**mIntfVoltage)(0, 0) -
111 Math::realFromVectorElement(leftVector, matrixNodeIndex(0, 0));
112 }
113}
114
115void EMT::Ph1::Switch::mnaCompUpdateCurrent(const Matrix &leftVector) {
116 (**mIntfCurrent)(0, 0) = (**mIsClosed)
117 ? (**mIntfVoltage)(0, 0) / (**mClosedResistance)
118 : (**mIntfVoltage)(0, 0) / (**mOpenResistance);
119}
const Attribute< Real >::Ptr mClosedResistance
Resistance if switch is closed [ohm].
const Attribute< Real >::Ptr mOpenResistance
Resistance if switch is open [ohm].
const Attribute< Bool >::Ptr mIsClosed
Defines if Switch is open or closed.
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.
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.