DPsim
DP_Ph1_RXLoadSwitch.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/DP/DP_Ph1_RXLoadSwitch.h>
10 
11 using namespace CPS;
12 
13 DP::Ph1::RXLoadSwitch::RXLoadSwitch(String uid, String name,
14  Logger::Level logLevel)
15  : CompositePowerComp<Complex>(uid, name, true, true, logLevel) {
16  setTerminalNumber(1);
17  setVirtualNodeNumber(1);
18  **mIntfVoltage = MatrixComp::Zero(1, 1);
19  **mIntfCurrent = MatrixComp::Zero(1, 1);
20 
21  // Create sub components
22  mSubRXLoad =
23  std::make_shared<DP::Ph1::RXLoad>(**mName + "_rxload", mLogLevel);
24  mSubSwitch =
25  std::make_shared<DP::Ph1::Switch>(**mName + "_switch", mLogLevel);
26  addMNASubComponent(mSubRXLoad, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
27  MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
28  addMNASubComponent(mSubSwitch, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
29  MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, false);
30  // Set switch default values
31  mSubSwitch->setParameters(1e9, 1e-9, true);
32 }
33 
34 DP::Ph1::RXLoadSwitch::RXLoadSwitch(String name, Logger::Level logLevel)
35  : RXLoadSwitch(name, name, logLevel) {}
36 
37 Bool DP::Ph1::RXLoadSwitch::mnaIsClosed() { return mSubSwitch->isClosed(); }
38 
40 
41  if (!mParametersSet) {
42  // use powerflow results
43  mSubRXLoad->setParameters(mTerminals[0]->singleActivePower(),
44  mTerminals[0]->singleReactivePower(),
45  std::abs(mTerminals[0]->initialSingleVoltage()));
46  }
47 
48  mSubRXLoad->connect({virtualNode(0)});
49  mSubRXLoad->initialize(mFrequencies);
50  mSubRXLoad->initializeFromNodesAndTerminals(frequency);
51 
52  mSubSwitch->connect({node(0)});
53  mSubSwitch->initialize(mFrequencies);
54  mSubSwitch->initializeFromNodesAndTerminals(frequency);
55 
56  **mIntfVoltage = **mSubRXLoad->mIntfVoltage;
57  **mIntfCurrent = **mSubRXLoad->mIntfCurrent;
58 
59  SPDLOG_LOGGER_INFO(mSLog,
60  "\n--- Initialization from powerflow ---"
61  "\nVoltage across: {:s}"
62  "\nCurrent: {:s}"
63  "\nTerminal 0 voltage: {:s}"
64  "\n--- Initialization from powerflow finished ---",
65  Logger::phasorToString((**mIntfVoltage)(0, 0)),
66  Logger::phasorToString((**mIntfCurrent)(0, 0)),
67  Logger::phasorToString(initialSingleVoltage(0)));
68 }
69 
70 void DP::Ph1::RXLoadSwitch::setParameters(Real activePower, Real reactivePower,
71  Real nomVolt, Real openResistance,
72  Real closedResistance, Bool closed) {
73  mParametersSet = true;
74  mSubRXLoad->setParameters(activePower, reactivePower, nomVolt);
75  mSubSwitch->setParameters(openResistance, closedResistance, closed);
76 }
77 
79  Real closedResistance,
80  Bool closed) {
81  mSubSwitch->setParameters(openResistance, closedResistance, closed);
82 }
83 
85  Bool closed, SparseMatrixRow &systemMatrix, Int freqIdx) {
86  mSubRXLoad->mnaApplySystemMatrixStamp(systemMatrix);
87  mSubSwitch->mnaCompApplySwitchSystemMatrixStamp(closed, systemMatrix,
88  freqIdx);
89 }
90 
92  AttributeBase::List &prevStepDependencies,
93  AttributeBase::List &attributeDependencies,
94  AttributeBase::List &modifiedAttributes) {
95  prevStepDependencies.push_back(mIntfCurrent);
96  prevStepDependencies.push_back(mIntfVoltage);
97  modifiedAttributes.push_back(mRightVector);
98 }
99 
100 void DP::Ph1::RXLoadSwitch::mnaParentPreStep(Real time, Int timeStepCount) {
101  updateSwitchState(time);
102  mnaCompApplyRightSideVectorStamp(**mRightVector);
103 }
104 
106  AttributeBase::List &prevStepDependencies,
107  AttributeBase::List &attributeDependencies,
108  AttributeBase::List &modifiedAttributes,
109  Attribute<Matrix>::Ptr &leftVector) {
110  attributeDependencies.push_back(leftVector);
111  modifiedAttributes.push_back(mIntfVoltage);
112  modifiedAttributes.push_back(mIntfCurrent);
113 }
114 
116  Real time, Int timeStepCount, Attribute<Matrix>::Ptr &leftVector) {
117  **mIntfVoltage = **mSubRXLoad->mIntfVoltage;
118  **mIntfCurrent = **mSubRXLoad->mIntfCurrent;
119 }
120 
122 
123  if (time > mSwitchTimeOffset && mSubSwitch->isClosed()) {
124  Real VRef = Math::abs(**mSubRXLoad->mNomVoltage);
125  Real V = Math::abs((**mIntfVoltage)(0, 0));
126 
127  Real deltaV = Math::abs((V - VRef) / VRef);
128 
129  if (deltaV > 0.1) {
130  mSubSwitch->open();
131  SPDLOG_LOGGER_INFO(mSLog, "Opened Switch at {}", (float)time);
132  }
133  }
134 }
Base class for composite power components.
void addMNASubComponent(typename SimPowerComp< Complex >::Ptr subc, MNA_SUBCOMP_TASK_ORDER preStepOrder, MNA_SUBCOMP_TASK_ORDER postStepOrder, Bool contributeToRightVector)
Add a new subcomponent implementing MNA methods.
Constant impedance load model consisting of RLC elements.
void updateSwitchState(Real time)
built-in logic for protection switch
void setParameters(Real activePower, Real reactivePower, Real nomVolt, Real openResistance, Real closedResistance, Bool closed=false)
Sets model specific parameters.
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.
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.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
std::shared_ptr< DP::Ph1::Switch > mSubSwitch
Internal protection switch.
const Attribute< String >::Ptr mName
Human readable name.
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
Current through component.
Definition: SimPowerComp.h:47
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
Voltage between terminals.
Definition: SimPowerComp.h:45
Logger::Level mLogLevel
Component logger control for internal variables.