DPsim
Loading...
Searching...
No Matches
SP_Ph1_varResSwitch.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_varResSwitch.h>
10
11using namespace CPS;
12
14 Logger::Level logLevel)
15 : MNASimPowerComp<Complex>(uid, name, false, true, logLevel),
16 Base::Ph1::Switch(mAttributes) {
17 setTerminalNumber(2);
18 **mIntfVoltage = MatrixComp::Zero(1, 1);
19 **mIntfCurrent = MatrixComp::Zero(1, 1);
20}
21
22SimPowerComp<Complex>::Ptr SP::Ph1::varResSwitch::clone(String name) {
23 auto copy = varResSwitch::make(name, mLogLevel);
24 copy->setParameters(**mOpenResistance, **mClosedResistance, **mIsClosed);
25 return copy;
26}
27
29
30 // // This function is not used!!!!!!
31
32 //Switch Resistance
33 Real impedance = (**mIsClosed) ? **mClosedResistance : **mOpenResistance;
34
35 (**mIntfVoltage)(0, 0) = initialSingleVoltage(1) - initialSingleVoltage(0);
36 (**mIntfCurrent)(0, 0) = (**mIntfVoltage)(0, 0) / impedance;
37}
38
39// #### MNA functions ####
41 Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
43}
44
46
48 SparseMatrixRow &systemMatrix) {
49 Complex conductance = (**mIsClosed) ? Complex(1. / **mClosedResistance, 0)
50 : Complex(1. / **mOpenResistance, 0);
51
52 MNAStampUtils::stampAdmittance(conductance, systemMatrix, matrixNodeIndex(0),
53 matrixNodeIndex(1), terminalNotGrounded(0),
54 terminalNotGrounded(1), mSLog);
55}
56
58 Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) {
59 Complex conductance = (closed) ? Complex(1. / **mClosedResistance, 0)
60 : Complex(1. / **mOpenResistance, 0);
61
62 MNAStampUtils::stampAdmittance(conductance, systemMatrix, matrixNodeIndex(0),
63 matrixNodeIndex(1), terminalNotGrounded(0),
64 terminalNotGrounded(1), mSLog);
65}
66
69
71 AttributeBase::List &prevStepDependencies,
72 AttributeBase::List &attributeDependencies,
73 AttributeBase::List &modifiedAttributes,
74 Attribute<Matrix>::Ptr &leftVector) {
75
76 attributeDependencies.push_back(leftVector);
77 modifiedAttributes.push_back(mIntfVoltage);
78 modifiedAttributes.push_back(mIntfCurrent);
79}
80
82 Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
83 mnaCompUpdateVoltage(**leftVector);
84 mnaCompUpdateCurrent(**leftVector);
85}
86
87void SP::Ph1::varResSwitch::mnaCompUpdateVoltage(const Matrix &leftVector) {
88 // Voltage across component is defined as V1 - V0
89 (**mIntfVoltage)(0, 0) = 0;
90 if (terminalNotGrounded(1))
91 (**mIntfVoltage)(0, 0) =
92 Math::complexFromVectorElement(leftVector, matrixNodeIndex(1));
93 if (terminalNotGrounded(0))
94 (**mIntfVoltage)(0, 0) =
95 (**mIntfVoltage)(0, 0) -
96 Math::complexFromVectorElement(leftVector, matrixNodeIndex(0));
97}
98
99void SP::Ph1::varResSwitch::mnaCompUpdateCurrent(const Matrix &leftVector) {
100 (**mIntfCurrent)(0, 0) = (**mIsClosed)
101 ? (**mIntfVoltage)(0, 0) / **mClosedResistance
102 : (**mIntfVoltage)(0, 0) / **mOpenResistance;
103}
104
106 //Get present state
107 Bool presentState = this->mnaIsClosed();
108
109 // Check if state of switch changed from open to closed
110 if (!(mPrevState == presentState)) {
111 // Switch is closed : change with 1/mDeltaRes
112 if (this->mnaIsClosed() == true) {
113 // mClosedResistance= 1./mDeltaRes*mPrevRes;
114 **mClosedResistance = mDeltaResClosed * mPrevRes;
115 mPrevRes = **mClosedResistance;
116 // check if target value is reached
117 if (**mClosedResistance < mInitClosedRes) {
118 **mClosedResistance = mInitClosedRes;
119 mPrevRes = **mClosedResistance;
120 mPrevState = this->mnaIsClosed();
121 }
122 }
123 // Switch is opened : change with mDeltaRes
124 else if (this->mnaIsClosed() == false) {
125 **mOpenResistance = mDeltaResOpen * mPrevRes;
126 mPrevRes = **mOpenResistance;
127 // check if target value is reached
128 if (**mOpenResistance > mInitOpenRes) {
129 **mOpenResistance = mInitOpenRes;
130 mPrevRes = **mOpenResistance;
131 mPrevState = this->mnaIsClosed();
132 }
133 }
134 return 1; //recompute system matrix
135 } else {
136 return 0; // do not recompute system matrix
137 }
138}
139
140void SP::Ph1::varResSwitch::setInitParameters(Real timestep) {
141 //Define variables for the transition
142 mDeltaResClosed = 0;
143 // mDeltaResOpen = 1.5; // assumption for 1ms step size
144 mDeltaResOpen = 0.5 * timestep / 0.001 + 1;
145 mPrevState = **mIsClosed;
146 mPrevRes = (**mIsClosed) ? **mClosedResistance : **mOpenResistance;
147 mInitClosedRes = **mClosedResistance;
148 mInitOpenRes = **mOpenResistance;
149}
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)
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix)
Stamps system matrix.
Bool mnaIsClosed()
Check if switch is closed.
void mnaCompApplySwitchSystemMatrixStamp(Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx)
Stamps system matrix considering the defined switch position.
SimPowerComp< Complex >::Ptr clone(String name)
Returns a modified copy of the component with the given suffix added to the name and without.
Bool hasParameterChanged()
Returns true if one of the element paramters has changed.
void initializeFromNodesAndTerminals(Real frequency)
Initializes states from power flow data.
void mnaCompUpdateCurrent(const Matrix &leftVector)
Update interface current from MNA system results.
varResSwitch(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and log level.
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector)
Initializes MNA specific variables.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector)
void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector)
MNA pre step operations.
void mnaCompUpdateVoltage(const Matrix &leftVector)
Update interface voltage from MNA system results.
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector)
Stamps right side (source) vector.
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.