DPsim
Loading...
Searching...
No Matches
DP_Ph1_RXLoad.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_RXLoad.h>
10
11using namespace CPS;
12
13DP::Ph1::RXLoad::RXLoad(String uid, String name, Logger::Level logLevel)
14 : CompositePowerComp<Complex>(uid, name, true, true, logLevel),
15 mActivePower(mAttributes->create<Real>("P")),
16 mReactivePower(mAttributes->create<Real>("Q")),
17 mNomVoltage(mAttributes->create<Real>("V_nom")) {
18 setTerminalNumber(1);
19
20 SPDLOG_LOGGER_INFO(mSLog, "Create {} {}", this->type(), name);
21 **mIntfVoltage = MatrixComp::Zero(1, 1);
22 **mIntfCurrent = MatrixComp::Zero(1, 1);
23}
24
25DP::Ph1::RXLoad::RXLoad(String name, Logger::Level logLevel)
26 : RXLoad(name, name, logLevel) {}
27
28SimPowerComp<Complex>::Ptr DP::Ph1::RXLoad::clone(String name) {
29 auto copy = RXLoad::make(name, mLogLevel);
30 copy->setParameters(**mActivePower, **mReactivePower, **mNomVoltage);
31 return copy;
32}
33
36 return;
37 mSubCompCreated = true;
38
39 // Intentionally empty: which of R/L/C exist depends on the load power sign, known only in
40 // initializeParentFromNodesAndTerminals(), where the sub-components are created. Safe: no new virtual nodes.
41}
42
44 if (!mParametersSet) {
45 setParameters(mTerminals[0]->singleActivePower(),
46 mTerminals[0]->singleReactivePower(),
47 std::abs(mTerminals[0]->initialSingleVoltage()));
48 }
49
50 Real omega = 2. * PI * frequency;
51
52 if (**mActivePower != 0) {
53 mResistance = std::pow(**mNomVoltage, 2) / **mActivePower;
55 std::make_shared<DP::Ph1::Resistor>(**mName + "_res", mLogLevel);
56 mSubResistor->setParameters(mResistance);
57 mSubResistor->connect({SimNode::GND, mTerminals[0]->node()});
58 addMNASubComponent(mSubResistor, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
59 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, false);
60 } else {
61 mResistance = 0;
62 }
63
64 mReactance = (**mReactivePower != 0)
65 ? std::pow(**mNomVoltage, 2) / **mReactivePower
66 : 0;
67
68 if (mReactance > 0) {
69 mInductance = mReactance / omega;
71 std::make_shared<DP::Ph1::Inductor>(**mName + "_ind", mLogLevel);
72 mSubInductor->setParameters(mInductance);
73 mSubInductor->connect({SimNode::GND, mTerminals[0]->node()});
74 addMNASubComponent(mSubInductor, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
75 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
76 } else if (mReactance < 0) {
77 mCapacitance = -1. / omega / mReactance;
79 std::make_shared<DP::Ph1::Capacitor>(**mName + "_cap", mLogLevel);
80 mSubCapacitor->setParameters(mCapacitance);
81 mSubCapacitor->connect({SimNode::GND, mTerminals[0]->node()});
83 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
84 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
85 }
86
87 (**mIntfVoltage)(0, 0) = mTerminals[0]->initialSingleVoltage();
88 (**mIntfCurrent)(0, 0) = std::conj(Complex(**mActivePower, **mReactivePower) /
89 (**mIntfVoltage)(0, 0));
90
91 SPDLOG_LOGGER_INFO(mSLog,
92 "\n--- Initialization from powerflow ---"
93 "\nVoltage across: {:s}"
94 "\nCurrent: {:s}"
95 "\nTerminal 0 voltage: {:s}"
96 "\nResistance: {:f}"
97 "\nReactance: {:f}"
98 "\n--- Initialization from powerflow finished ---",
99 Logger::phasorToString((**mIntfVoltage)(0, 0)),
100 Logger::phasorToString((**mIntfCurrent)(0, 0)),
101 Logger::phasorToString(initialSingleVoltage(0)),
103}
104
105void DP::Ph1::RXLoad::setParameters(Real activePower, Real reactivePower,
106 Real volt) {
107 mParametersSet = true;
108 **mActivePower = activePower;
109 **mReactivePower = reactivePower;
110 **mNomVoltage = volt;
111
112 SPDLOG_LOGGER_INFO(mSLog, "Active Power={} [W] Reactive Power={} [VAr]",
114 SPDLOG_LOGGER_INFO(mSLog, "Nominal Voltage={} [V]", **mNomVoltage);
115}
116
117void DP::Ph1::RXLoad::mnaCompUpdateVoltage(const Matrix &leftVector) {
118 (**mIntfVoltage)(0, 0) =
119 Math::complexFromVectorElement(leftVector, matrixNodeIndex(0));
120}
121
122void DP::Ph1::RXLoad::mnaCompUpdateCurrent(const Matrix &leftVector) {
123 (**mIntfCurrent)(0, 0) = 0;
124
125 for (auto subComp : mSubComponents) {
126 (**mIntfCurrent)(0, 0) += subComp->intfCurrent()(0, 0);
127 }
128}
129
131 AttributeBase::List &prevStepDependencies,
132 AttributeBase::List &attributeDependencies,
133 AttributeBase::List &modifiedAttributes) {
134 modifiedAttributes.push_back(mRightVector);
135}
136
140
142 AttributeBase::List &prevStepDependencies,
143 AttributeBase::List &attributeDependencies,
144 AttributeBase::List &modifiedAttributes,
145 Attribute<Matrix>::Ptr &leftVector) {
146 attributeDependencies.push_back(leftVector);
147 modifiedAttributes.push_back(mIntfVoltage);
148 modifiedAttributes.push_back(mIntfCurrent);
149}
150
151void DP::Ph1::RXLoad::mnaParentPostStep(Real time, Int timeStepCount,
152 Attribute<Matrix>::Ptr &leftVector) {
153 mnaCompUpdateVoltage(**leftVector);
154 mnaCompUpdateCurrent(**leftVector);
155}
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)
std::shared_ptr< DP::Ph1::Inductor > mSubInductor
Internal inductor.
RXLoad(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
SimPowerComp< Complex >::Ptr clone(String name) override
Returns a modified copy of the component with the given suffix added to the name and without.
Real mInductance
Inductance [H].
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
const Attribute< Real >::Ptr mActivePower
True after createSubComponents() runs; prevents double-construction.
Real mReactance
Reactance [Ohm].
const Attribute< Real >::Ptr mReactivePower
Reactive power [VAr].
Real mCapacitance
Capacitance [F].
void mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
Add MNA post step dependencies.
std::shared_ptr< DP::Ph1::Capacitor > mSubCapacitor
Internal capacitor.
const Attribute< Real >::Ptr mNomVoltage
Nominal voltage [V].
void setParameters(Real activePower, Real ReactivePower, Real volt)
Set model specific parameters.
std::shared_ptr< DP::Ph1::Resistor > mSubResistor
Internal resistance.
void mnaParentAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
Add MNA pre step dependencies.
void initializeParentFromNodesAndTerminals(Real frequency) override
Derives values from power flow data and pushes them to subcomponents.
void mnaParentPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Update interface voltage from MNA system result.
void createSubComponents() override
Constructs and registers MNA subcomponents; idempotent.
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Update interface current from MNA system result.
const Attribute< String >::Ptr mName
Human readable name.
String uid()
Returns unique id.
String type()
Get component type (cross-platform)
AttributeList::Ptr mAttributes
Attribute List.
Attribute< Matrix >::Ptr mRightVector
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
SimTerminal< Complex >::List mTerminals
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
std::vector< std::shared_ptr< SimPowerComp< Complex > > > mSubComponents
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.