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
35
36 if (!mParametersSet) {
37 setParameters(mTerminals[0]->singleActivePower(),
38 mTerminals[0]->singleReactivePower(),
39 std::abs(mTerminals[0]->initialSingleVoltage()));
40 }
41
42 if (**mActivePower != 0) {
43 mResistance = std::pow(**mNomVoltage, 2) / **mActivePower;
45 std::make_shared<DP::Ph1::Resistor>(**mName + "_res", mLogLevel);
46 mSubResistor->setParameters(mResistance);
47 mSubResistor->connect({SimNode::GND, mTerminals[0]->node()});
48 mSubResistor->initialize(mFrequencies);
49 mSubResistor->initializeFromNodesAndTerminals(frequency);
50 addMNASubComponent(mSubResistor, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
51 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, false);
52 } else {
53 mResistance = 0;
54 }
55
56 if (**mReactivePower != 0)
57 mReactance = std::pow(**mNomVoltage, 2) / **mReactivePower;
58 else
59 mReactance = 0;
60
61 if (mReactance > 0) {
62 mInductance = mReactance / (2. * PI * frequency);
64 std::make_shared<DP::Ph1::Inductor>(**mName + "_ind", mLogLevel);
65 mSubInductor->setParameters(mInductance);
66 mSubInductor->connect({SimNode::GND, mTerminals[0]->node()});
67 mSubInductor->initialize(mFrequencies);
68 mSubInductor->initializeFromNodesAndTerminals(frequency);
69 addMNASubComponent(mSubInductor, MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
70 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
71 } else if (mReactance < 0) {
72 mCapacitance = -1. / (2. * PI * frequency) / mReactance;
74 std::make_shared<DP::Ph1::Capacitor>(**mName + "_cap", mLogLevel);
75 mSubCapacitor->setParameters(mCapacitance);
76 mSubCapacitor->connect({SimNode::GND, mTerminals[0]->node()});
77 mSubCapacitor->initialize(mFrequencies);
78 mSubCapacitor->initializeFromNodesAndTerminals(frequency);
80 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT,
81 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, true);
82 }
83
84 (**mIntfVoltage)(0, 0) = mTerminals[0]->initialSingleVoltage();
85 (**mIntfCurrent)(0, 0) = std::conj(Complex(**mActivePower, **mReactivePower) /
86 (**mIntfVoltage)(0, 0));
87
88 SPDLOG_LOGGER_INFO(mSLog,
89 "\n--- Initialization from powerflow ---"
90 "\nVoltage across: {:s}"
91 "\nCurrent: {:s}"
92 "\nTerminal 0 voltage: {:s}"
93 "\nResistance: {:f}"
94 "\nReactance: {:f}"
95 "\n--- Initialization from powerflow finished ---",
96 Logger::phasorToString((**mIntfVoltage)(0, 0)),
97 Logger::phasorToString((**mIntfCurrent)(0, 0)),
98 Logger::phasorToString(initialSingleVoltage(0)),
100}
101
102void DP::Ph1::RXLoad::setParameters(Real activePower, Real reactivePower,
103 Real volt) {
104 mParametersSet = true;
105 **mActivePower = activePower;
106 **mReactivePower = reactivePower;
107 **mNomVoltage = volt;
108
109 SPDLOG_LOGGER_INFO(mSLog, "Active Power={} [W] Reactive Power={} [VAr]",
111 SPDLOG_LOGGER_INFO(mSLog, "Nominal Voltage={} [V]", **mNomVoltage);
112}
113
114void DP::Ph1::RXLoad::mnaCompUpdateVoltage(const Matrix &leftVector) {
115 (**mIntfVoltage)(0, 0) =
116 Math::complexFromVectorElement(leftVector, matrixNodeIndex(0));
117}
118
119void DP::Ph1::RXLoad::mnaCompUpdateCurrent(const Matrix &leftVector) {
120 (**mIntfCurrent)(0, 0) = 0;
121
122 for (auto subComp : mSubComponents) {
123 (**mIntfCurrent)(0, 0) += subComp->intfCurrent()(0, 0);
124 }
125}
126
128 AttributeBase::List &prevStepDependencies,
129 AttributeBase::List &attributeDependencies,
130 AttributeBase::List &modifiedAttributes) {
131 modifiedAttributes.push_back(mRightVector);
132}
133
137
139 AttributeBase::List &prevStepDependencies,
140 AttributeBase::List &attributeDependencies,
141 AttributeBase::List &modifiedAttributes,
142 Attribute<Matrix>::Ptr &leftVector) {
143 attributeDependencies.push_back(leftVector);
144 modifiedAttributes.push_back(mIntfVoltage);
145 modifiedAttributes.push_back(mIntfCurrent);
146}
147
148void DP::Ph1::RXLoad::mnaParentPostStep(Real time, Int timeStepCount,
149 Attribute<Matrix>::Ptr &leftVector) {
150 mnaCompUpdateVoltage(**leftVector);
151 mnaCompUpdateCurrent(**leftVector);
152}
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
Active power [Watt].
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 mnaParentPreStep(Real time, Int timeStepCount) override
MNA pre step operations.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Update interface voltage from MNA system result.
void initializeFromNodesAndTerminals(Real frequency) override
Initialize component from power flow data.
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.