DPsim
Loading...
Searching...
No Matches
DP_Ph1_RXLoadSwitch.cpp
Go to the documentation of this file.
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
11
12using namespace CPS;
13
17
19 Bool useSubcomponents,
20 Logger::Level logLevel)
21 : CompositePowerComp<Complex>(uid, name, true, true, logLevel),
22 mUseSubcomponents(useSubcomponents) {
25 **mIntfVoltage = MatrixComp::Zero(1, 1);
26 **mIntfCurrent = MatrixComp::Zero(1, 1);
27
30 std::make_shared<DP::Ph1::RXLoad>(**mName + "_rxload", mLogLevel);
32 std::make_shared<DP::Ph1::Switch>(**mName + "_switch", mLogLevel);
38 true);
39 }
40}
41
44
48
50 Real frequency) {
51 if (!mUseSubcomponents) {
53 mDirectActivePower = mTerminals[0]->singleActivePower();
54 mDirectReactivePower = mTerminals[0]->singleReactivePower();
56 }
57 const Real omega = 2. * PI * frequency;
61 : 0.;
62 const Real reactance =
65 : 0.;
66 mDirectUseInductor = reactance > 0.;
67 mDirectUseCapacitor = reactance < 0.;
69 mDirectInductance = reactance / omega;
70 else if (mDirectUseCapacitor)
71 mDirectCapacitance = -1. / (omega * reactance);
72 const Complex voltage = initialSingleVoltage(0);
73 const Complex current =
74 std::abs(voltage) > 0.
76 voltage)
77 : Complex(0., 0.);
78 const Real switchResistance =
80 mVirtualNodes[0]->setInitialVoltage(voltage - switchResistance * current);
81 (**mIntfVoltage)(0, 0) = voltage;
82 (**mIntfCurrent)(0, 0) = current;
83 return;
84 }
85
86 if (!mParametersSet) {
87 // use powerflow results
88 mSubRXLoad->setParameters(mTerminals[0]->singleActivePower(),
89 mTerminals[0]->singleReactivePower(),
90 std::abs(mTerminals[0]->initialSingleVoltage()));
91 }
92
93 // The nested RXLoad reads its connected node's initial voltage during its
94 // own initialization. Seed the internal node from the terminal phasor and
95 // the closed/open switch drop before initializing that child.
96 const Complex terminalVoltage = initialSingleVoltage(0);
97 const Complex loadPower(**mSubRXLoad->mActivePower,
98 **mSubRXLoad->mReactivePower);
99 const Complex initialCurrent = std::abs(terminalVoltage) > 0.
100 ? std::conj(loadPower / terminalVoltage)
101 : Complex(0., 0.);
102 const Real initialSwitchResistance =
104 mVirtualNodes[0]->setInitialVoltage(terminalVoltage -
105 initialSwitchResistance * initialCurrent);
106
107 mSubRXLoad->connect({virtualNode(0)});
108 mSubSwitch->connect({node(0), virtualNode(0)});
109 (**mIntfVoltage)(0, 0) = virtualNode(0)->initialSingleVoltage();
110 (**mIntfCurrent)(0, 0) = initialCurrent;
111
112 SPDLOG_LOGGER_INFO(mSLog,
113 "\n--- Initialization from powerflow ---"
114 "\nVoltage across: {:s}"
115 "\nCurrent: {:s}"
116 "\nTerminal 0 voltage: {:s}"
117 "\n--- Initialization from powerflow finished ---",
121}
122
123void DP::Ph1::RXLoadSwitch::setParameters(Real activePower, Real reactivePower,
124 Real nomVolt, Real openResistance,
125 Real closedResistance, Bool closed) {
126 mParametersSet = true;
127 if (!mUseSubcomponents) {
129 (mDirectOpenResistance != openResistance) ||
130 (mDirectClosedResistance != closedResistance);
132 mDirectActivePower = activePower;
133 mDirectReactivePower = reactivePower;
134 mDirectNomVoltage = nomVolt;
135 mDirectOpenResistance = openResistance;
136 mDirectClosedResistance = closedResistance;
137 mDirectSwitchClosed = closed;
138 return;
139 }
140 mSubRXLoad->setParameters(activePower, reactivePower, nomVolt);
141 mSubSwitch->setParameters(openResistance, closedResistance, closed);
142}
143
145 Real closedResistance,
146 Bool closed) {
147 if (!mUseSubcomponents) {
149 (mDirectOpenResistance != openResistance) ||
150 (mDirectClosedResistance != closedResistance);
151 mDirectOpenResistance = openResistance;
152 mDirectClosedResistance = closedResistance;
153 mDirectSwitchClosed = closed;
154 return;
155 }
156 mSubSwitch->setParameters(openResistance, closedResistance, closed);
157}
158
160 Real omega, Real timeStep, Attribute<Matrix>::Ptr leftVector) {
162 return;
163 mDirectEquivalentConductance = MatrixComp::Zero(mNumFreqs, 1);
164 mDirectHistory = MatrixComp::Zero(mNumFreqs, 1);
165 mDirectPreviousCurrentFactor = MatrixComp::Zero(mNumFreqs, 1);
166 mDirectPreviousVoltageFactor = MatrixComp::Zero(mNumFreqs, 1);
167 mDirectBranchCurrent = MatrixComp::Zero(mNumFreqs, 1);
168 mDirectBranchVoltage = MatrixComp::Zero(mNumFreqs, 1);
169 const Real switchResistance =
171 for (UInt freq = 0; freq < mNumFreqs; ++freq) {
172 mDirectBranchVoltage(freq, 0) =
173 (**mIntfVoltage)(0, freq) -
174 switchResistance * (**mIntfCurrent)(0, freq);
176 mDirectBranchCurrent(freq, 0) =
177 (**mIntfCurrent)(0, freq) -
179 const Real harmonicOmega = 2. * PI * mFrequencies(freq, 0);
180 if (mDirectUseInductor) {
181 const Real b = timeStep * harmonicOmega / 2.;
182 const Real a = timeStep / (2. * mDirectInductance);
184 Complex(a / (1. + b * b), -a * b / (1. + b * b));
186 Complex((1. - b * b) / (1. + b * b), -2. * b / (1. + b * b));
187 } else if (mDirectUseCapacitor) {
188 const Real capG = 2. * mDirectCapacitance / timeStep;
189 const Real capB = harmonicOmega * mDirectCapacitance;
190 mDirectEquivalentConductance(freq, 0) = Complex(capG, capB);
191 mDirectPreviousVoltageFactor(freq, 0) = Complex(capG, -capB);
192 }
193 }
194 mDirectPreviousSwitchResistance = switchResistance;
195}
196
198 Matrix &rightVector) {
200 return;
201 for (UInt freq = 0; freq < mNumFreqs; ++freq) {
202 const Complex branchVoltage =
203 (**mIntfVoltage)(0, freq) -
206 mDirectHistory(freq, 0) =
207 mDirectEquivalentConductance(freq, 0) * branchVoltage +
209 else
210 mDirectHistory(freq, 0) =
211 -mDirectBranchCurrent(freq, 0) -
212 mDirectPreviousVoltageFactor(freq, 0) * branchVoltage;
214 -mDirectHistory(freq, 0), mNumFreqs, freq);
215 }
216}
217
219 Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) {
220 if (!mUseSubcomponents) {
223 systemMatrix,
224 virtualNode(0)->matrixNodeIndex(), 0, true,
225 false, mSLog, mNumFreqs, freqIdx);
226 const Real resistance =
229 Complex(1. / resistance, 0.), systemMatrix, matrixNodeIndex(0),
231 mNumFreqs, freqIdx);
232 return;
233 }
234 mSubRXLoad->mnaApplySystemMatrixStamp(systemMatrix);
235 mSubSwitch->mnaCompApplySwitchSystemMatrixStamp(closed, systemMatrix,
236 freqIdx);
237}
238
240 AttributeBase::List &prevStepDependencies,
241 AttributeBase::List &attributeDependencies,
242 AttributeBase::List &modifiedAttributes) {
243 prevStepDependencies.push_back(mIntfCurrent);
244 prevStepDependencies.push_back(mIntfVoltage);
245 modifiedAttributes.push_back(mRightVector);
246}
247
252
254 AttributeBase::List &prevStepDependencies,
255 AttributeBase::List &attributeDependencies,
256 AttributeBase::List &modifiedAttributes,
257 Attribute<Matrix>::Ptr &leftVector) {
258 attributeDependencies.push_back(leftVector);
259 modifiedAttributes.push_back(mIntfVoltage);
260 modifiedAttributes.push_back(mIntfCurrent);
261}
262
264 Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
265 if (!mUseSubcomponents) {
266 for (UInt freq = 0; freq < mNumFreqs; ++freq) {
267 (**mIntfVoltage)(0, freq) = Math::complexFromVectorElement(
268 **leftVector, matrixNodeIndex(0), mNumFreqs, freq);
269 const Complex branchVoltage = Math::complexFromVectorElement(
270 **leftVector, virtualNode(0)->matrixNodeIndex(), mNumFreqs, freq);
271 const Complex branchCurrent =
272 mDirectEquivalentConductance(freq, 0) * branchVoltage +
273 mDirectHistory(freq, 0);
275 mDirectBranchCurrent(freq, 0) = branchCurrent;
276 (**mIntfCurrent)(0, freq) =
277 Complex(mDirectConductance, 0.) * branchVoltage + branchCurrent;
278 mDirectBranchVoltage(freq, 0) = branchVoltage;
279 }
282 return;
283 }
284 **mIntfVoltage = **mSubRXLoad->mIntfVoltage;
285 **mIntfCurrent = **mSubRXLoad->mIntfCurrent;
286}
287
289
290 if (time > mSwitchTimeOffset && mnaIsClosed()) {
291 Real VRef = mUseSubcomponents ? Math::abs(**mSubRXLoad->mNomVoltage)
293 Real V = Math::abs((**mIntfVoltage)(0, 0));
294
295 Real deltaV = Math::abs((V - VRef) / VRef);
296
297 if (deltaV > 0.1) {
299 mSubSwitch->open();
300 else {
301 mDirectSwitchClosed = false;
303 }
304 SPDLOG_LOGGER_INFO(mSLog, "Opened Switch at {}", (float)time);
305 }
306 }
307}
308
310 if (!mUseSubcomponents) {
311 const Bool changed = mDirectSwitchChanged;
312 mDirectSwitchChanged = false;
313 return changed;
314 }
315 return mSubSwitch->hasParameterChanged();
316};
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
void addMNASubComponent(typename SimPowerComp< Complex >::Ptr subc, MNA_SUBCOMP_TASK_ORDER preStepOrder, MNA_SUBCOMP_TASK_ORDER postStepOrder, Bool contributeToRightVector)
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
CompositePowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
void mnaParentInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
void mnaParentApplyRightSideVectorStamp(Matrix &rightVector) override
void updateSwitchState(Real time)
built-in logic for protection switch
void initializeParentFromNodesAndTerminals(Real frequency) override
Derives values from power flow data and pushes them to subcomponents.
void setParameters(Real activePower, Real reactivePower, Real nomVolt, Real openResistance, Real closedResistance, Bool closed=false)
Sets model specific parameters.
Real mSwitchTimeOffset
internal switch is only opened after this time offset
void mnaCompApplySwitchSystemMatrixStamp(Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) override
Stamps system matrix considering the defined switch position.
void setSwitchParameters(Real openResistance, Real closedResistance, Bool closed=false)
Sets only switch parameters so that load parameters could be calculated from powerflow.
RXLoadSwitch(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
Bool mnaIsClosed() override
Check if switch is closed.
Bool hasParameterChanged() override
Returns true if one of the element paramters has changed.
void mnaParentPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
std::shared_ptr< DP::Ph1::RXLoad > mSubRXLoad
Internal RXLoad.
std::shared_ptr< DP::Ph1::Switch > mSubSwitch
Internal protection switch.
const Attribute< String >::Ptr mName
Human readable name.
String uid()
Returns unique id.
spdlog::level::level_enum Level
Definition Logger.h:33
static String phasorToString(const Complex &num)
Definition Logger.cpp:57
Attribute< Matrix >::Ptr mRightVector
static void stampAdmittance(Complex admittance, SparseMatrixRow &mat, UInt node1Index, UInt node2Index, Bool isTerminal1NotGrounded, Bool isTerminal2NotGrounded, const Logger::Log &mSLog, Int maxFreq=1, Int freqIdx=0)
static Complex complexFromVectorElement(const Matrix &mat, Matrix::Index row, Int maxFreq=1, Int freqIdx=0)
static void setVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0, Matrix::Index colOffset=0)
static Real abs(Complex value)
Definition MathUtils.cpp:71
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
SimTerminal< Complex >::List mTerminals
SimNode< Complex >::Ptr node(UInt index)
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
SimNode< Complex >::Ptr virtualNode(UInt index)
SimNode< Complex >::List mVirtualNodes
Bool terminalNotGrounded(UInt index)
Complex initialSingleVoltage(UInt index)
Logger::Level mLogLevel
Component logger control for internal variables.
bool mParametersSet
Flag indicating that parameters are set via setParameters() function.
Logger::Log mSLog
Component logger.
#define PI
Definition Definitions.h:43
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
Dense matrix for real numbers.
Definition Definitions.h:79
std::string String
Definition Definitions.h:63
double Real
Definition Definitions.h:60
int Int
Definition Definitions.h:59
std::complex< Real > Complex
Definition Definitions.h:61
bool Bool
Definition Definitions.h:62
unsigned int UInt
Definition Definitions.h:58
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:72