DPsim
Loading...
Searching...
No Matches
SP_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/SP/SP_Ph1_Switch.h>
10
11using namespace CPS;
12
13SP::Ph1::Switch::Switch(String uid, String name, Logger::Level logLevel)
14 : MNASimPowerComp<Complex>(uid, name, false, true, logLevel),
15 Base::Ph1::Switch(mAttributes) {
16 setTerminalNumber(2);
17 **mIntfVoltage = MatrixComp::Zero(1, 1);
18 **mIntfCurrent = MatrixComp::Zero(1, 1);
19}
20
21SimPowerComp<Complex>::Ptr SP::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 impedance = (**mIsClosed) ? **mClosedResistance : **mOpenResistance;
30 (**mIntfVoltage)(0, 0) = initialSingleVoltage(1) - initialSingleVoltage(0);
31 (**mIntfCurrent)(0, 0) = (**mIntfVoltage)(0, 0) / impedance;
32
33 SPDLOG_LOGGER_INFO(mSLog,
34 "\n--- Initialization from powerflow ---"
35 "\nVoltage across: {:s}"
36 "\nCurrent: {:s}"
37 "\nTerminal 0 voltage: {:s}"
38 "\nTerminal 1 voltage: {:s}"
39 "\n--- Initialization from powerflow finished ---",
40 Logger::phasorToString((**mIntfVoltage)(0, 0)),
41 Logger::phasorToString((**mIntfCurrent)(0, 0)),
42 Logger::phasorToString(initialSingleVoltage(0)),
43 Logger::phasorToString(initialSingleVoltage(1)));
44}
45
46void SP::Ph1::Switch::mnaCompInitialize(Real omega, Real timeStep,
47 Attribute<Matrix>::Ptr leftVector) {
48 updateMatrixNodeIndices();
49 **mRightVector = Matrix::Zero(0, 0);
50}
51
53
55 SparseMatrixRow &systemMatrix) {
56 Complex conductance = (**mIsClosed) ? Complex(1. / **mClosedResistance, 0)
57 : Complex(1. / **mOpenResistance, 0);
58
59 MNAStampUtils::stampAdmittance(conductance, systemMatrix, matrixNodeIndex(0),
60 matrixNodeIndex(1), terminalNotGrounded(0),
61 terminalNotGrounded(1), mSLog);
62}
63
65 Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) {
66 Complex conductance = (closed) ? Complex(1. / **mClosedResistance, 0)
67 : Complex(1. / **mOpenResistance, 0);
68
69 MNAStampUtils::stampAdmittance(conductance, systemMatrix, matrixNodeIndex(0),
70 matrixNodeIndex(1), terminalNotGrounded(0),
71 terminalNotGrounded(1), mSLog);
72}
73
75
76void SP::Ph1::Switch::mnaCompUpdateVoltage(const Matrix &leftVector) {
77 // Voltage across component is defined as V1 - V0
78 (**mIntfVoltage)(0, 0) = 0;
79 if (terminalNotGrounded(1))
80 (**mIntfVoltage)(0, 0) =
81 Math::complexFromVectorElement(leftVector, matrixNodeIndex(1, 0));
82 if (terminalNotGrounded(0))
83 (**mIntfVoltage)(0, 0) =
84 (**mIntfVoltage)(0, 0) -
85 Math::complexFromVectorElement(leftVector, matrixNodeIndex(0));
86}
87
88void SP::Ph1::Switch::mnaCompUpdateCurrent(const Matrix &leftVector) {
89 (**mIntfCurrent)(0, 0) = (**mIsClosed)
90 ? (**mIntfVoltage)(0, 0) / **mClosedResistance
91 : (**mIntfVoltage)(0, 0) / **mOpenResistance;
92}
93
95 AttributeBase::List &prevStepDependencies,
96 AttributeBase::List &attributeDependencies,
97 AttributeBase::List &modifiedAttributes,
98 Attribute<Matrix>::Ptr &leftVector) {
99
100 attributeDependencies.push_back(leftVector);
101 modifiedAttributes.push_back(mIntfVoltage);
102 modifiedAttributes.push_back(mIntfCurrent);
103}
104
105void SP::Ph1::Switch::mnaCompPostStep(Real time, Int timeStepCount,
106 Attribute<Matrix>::Ptr &leftVector) {
107 mnaCompUpdateVoltage(**leftVector);
108 mnaCompUpdateCurrent(**leftVector);
109}
const Attribute< Real >::Ptr mClosedResistance
Resistance if switch is closed [ohm].
Bool isClosed()
Check if switch is closed.
const Attribute< Real >::Ptr mOpenResistance
Resistance if switch is open [ohm].
const Attribute< Bool >::Ptr mIsClosed
Defines if Switch is open or closed.
String uid()
Returns unique id.
AttributeList::Ptr mAttributes
Attribute List.
MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
Bool mnaIsClosed() override
Check if switch is closed.
void mnaCompApplySwitchSystemMatrixStamp(Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) override
Stamps system matrix considering the defined switch position.
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
Stamps right side (source) vector.
SimPowerComp< Complex >::Ptr clone(String name) override
Returns a modified copy of the component with the given suffix added to the name and without.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
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 mnaCompUpdateCurrent(const Matrix &leftVector) override
Update interface current from MNA system result.
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
Stamps system matrix.
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.
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
Logger::Level mLogLevel
Component logger control for internal variables.
Logger::Log mSLog
Component logger.