DPsim
Loading...
Searching...
No Matches
SP_Ph1_Load.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_Load.h>
10
11using namespace CPS;
12
13// #### General ####
14// please note that P,Q values can not be passed inside constructor since P,Q are currently read from the terminal,
15// and these values are not yet assigned to the terminals when this constructor was called in reader.
16SP::Ph1::Load::Load(String uid, String name, Logger::Level logLevel)
17 : CompositePowerComp<Complex>(uid, name, false, true, logLevel),
18 mActivePowerPerUnit(mAttributes->create<Real>("P_pu")),
19 mReactivePowerPerUnit(mAttributes->create<Real>("Q_pu")),
20 mActivePower(mAttributes->createDynamic<Real>(
21 "P")), //Made dynamic so it can be imported through InterfaceVillas
22 mReactivePower(mAttributes->createDynamic<Real>(
23 "Q")) //Made dynamic so it can be imported through InterfaceVillas
24{
25 SPDLOG_LOGGER_INFO(mSLog, "Create {} of type {}", **mName, this->type());
26 mSLog->flush();
27 **mIntfVoltage = MatrixComp::Zero(1, 1);
28 **mIntfCurrent = MatrixComp::Zero(1, 1);
29 setTerminalNumber(1);
30};
31
32void SP::Ph1::Load::setParameters(Real activePower, Real reactivePower,
33 Real nominalVoltage) {
34 **mActivePower = activePower;
35 **mReactivePower = reactivePower;
36 mNomVoltage = nominalVoltage;
37
38 SPDLOG_LOGGER_INFO(
39 mSLog,
40 "Active Power={} [W] Reactive Power={} [VAr] Nominal Voltage={} [V]",
41 **mActivePower, **mReactivePower, mNomVoltage);
42 mSLog->flush();
43
44 mParametersSet = true;
45}
46
47// #### Powerflow section ####
48Real SP::Ph1::Load::getNomVoltage() const { return mNomVoltage; }
49
51 Real baseOmega) {
52 SPDLOG_LOGGER_INFO(mSLog, "#### Calculate Per Unit Parameters for {}",
53 **mName);
54 mBaseApparentPower = baseApparentPower;
55 mBaseOmega = baseOmega;
56 SPDLOG_LOGGER_INFO(mSLog, "Base Power={} [VA] Base Omega={} [1/s]",
57 mBaseApparentPower, mBaseOmega);
58
59 **mActivePowerPerUnit = **mActivePower / mBaseApparentPower;
60 **mReactivePowerPerUnit = **mReactivePower / mBaseApparentPower;
61 SPDLOG_LOGGER_INFO(mSLog, "Active Power={} [pu] Reactive Power={} [pu]",
63 mSLog->flush();
64}
65
66void SP::Ph1::Load::modifyPowerFlowBusType(PowerflowBusType powerflowBusType) {
67 switch (powerflowBusType) {
68 case CPS::PowerflowBusType::PV:
69 throw std::invalid_argument(
70 " Power flow bus type error, load currently cannot be set as PVNode ");
71 break;
72 case CPS::PowerflowBusType::PQ:
73 mPowerflowBusType = powerflowBusType;
74 break;
75 case CPS::PowerflowBusType::VD:
76 throw std::invalid_argument(
77 " Power flow bus type error, load cannot be set as VDNode ");
78 break;
79 case CPS::PowerflowBusType::None:
80 break;
81 default:
82 throw std::invalid_argument(" Invalid power flow bus type ");
83 break;
84 }
85};
86
87void SP::Ph1::Load::updatePQ(Real time) {
88 if (mLoadProfile.weightingFactors.empty()) {
89 **mActivePower = mLoadProfile.pqData.find(time)->second.p;
90 **mReactivePower = mLoadProfile.pqData.find(time)->second.q;
91 } else {
92 Real wf = mLoadProfile.weightingFactors.find(time)->second;
94 Real P_new = this->attributeTyped<Real>("P_nom")->get() * wf;
95 Real Q_new = this->attributeTyped<Real>("Q_nom")->get() * wf;
96 **mActivePower = P_new;
97 **mReactivePower = Q_new;
98 }
99};
100
102 if (mSubCompCreated)
103 return;
104 mSubCompCreated = true;
105
106 // Intentionally empty: which of R/L/C exist depends on the load power sign and omega, known only in
107 // initializeParentFromNodesAndTerminals(), where the sub-components are created. Safe: no new virtual nodes.
108}
109
111 Real omega = 2. * PI * frequency;
112
113 // Read load parameters from the terminal when not set explicitly.
114 if (!mParametersSet) {
115 setParameters(mTerminals[0]->singleActivePower(),
116 mTerminals[0]->singleReactivePower(),
117 std::abs(mTerminals[0]->initialSingleVoltage()));
118 }
119
120 if (mNomVoltage == 0) {
121 SPDLOG_LOGGER_WARN(
122 mSLog,
123 "Nominal voltage of load {} is 0; resulting impedance will be "
124 "degenerate (zero resistance/inductance/capacitance).",
125 **mName);
126 }
127
128 // Compute derived impedance values needed to parametrize sub-components.
129 if (**mActivePower != 0) {
130 mResistance = std::pow(mNomVoltage, 2) / **mActivePower;
131 mConductance = 1.0 / mResistance;
132 }
133 if (**mReactivePower != 0)
134 mReactance = std::pow(mNomVoltage, 2) / **mReactivePower;
135 else
136 mReactance = 0;
137
138 // Instantiate subResistor for active power consumption
139 if (**mActivePower != 0) {
140 mSubResistor = std::make_shared<SP::Ph1::Resistor>(
141 **mUID + "_res", **mName + "_res", Logger::Level::off);
142 mSubResistor->setParameters(mResistance);
143 mSubResistor->connect({SimNode::GND, mTerminals[0]->node()});
144 addMNASubComponent(mSubResistor, MNA_SUBCOMP_TASK_ORDER::NO_TASK,
145 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, false);
146 }
147
148 // Instantiate subInductor or subCapacitor for reactive power consumption
149 if (mReactance > 0) {
150 mInductance = mReactance / omega;
151 mSubInductor = std::make_shared<SP::Ph1::Inductor>(
152 **mUID + "_res", **mName + "_ind", Logger::Level::off);
153 mSubInductor->setParameters(mInductance);
154 mSubInductor->connect({SimNode::GND, mTerminals[0]->node()});
155 addMNASubComponent(mSubInductor, MNA_SUBCOMP_TASK_ORDER::NO_TASK,
156 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, false);
157 } else if (mReactance < 0) {
158 mCapacitance = -1. / omega / mReactance;
159 mSubCapacitor = std::make_shared<SP::Ph1::Capacitor>(
160 **mUID + "_res", **mName + "_cap", Logger::Level::off);
161 mSubCapacitor->setParameters(mCapacitance);
162 mSubCapacitor->connect({SimNode::GND, mTerminals[0]->node()});
163 addMNASubComponent(mSubCapacitor, MNA_SUBCOMP_TASK_ORDER::NO_TASK,
164 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, false);
165 }
166
167 (**mIntfVoltage)(0, 0) = mTerminals[0]->initialSingleVoltage();
168 (**mIntfCurrent)(0, 0) = std::conj(Complex(attributeTyped<Real>("P")->get(),
169 attributeTyped<Real>("Q")->get()) /
170 (**mIntfVoltage)(0, 0));
171
172 SPDLOG_LOGGER_INFO(mSLog,
173 "\n--- Initialization from powerflow ---"
174 "\nVoltage across: {:s}"
175 "\nCurrent: {:s}"
176 "\nTerminal 0 voltage: {:s}"
177 "\n--- Initialization from powerflow finished ---",
178 Logger::phasorToString((**mIntfVoltage)(0, 0)),
179 Logger::phasorToString((**mIntfCurrent)(0, 0)),
180 Logger::phasorToString(initialSingleVoltage(0)));
181 SPDLOG_LOGGER_INFO(mSLog,
182 "Updated parameters according to powerflow:\n"
183 "Active Power={} [W] Reactive Power={} [VAr]",
184 attributeTyped<Real>("P")->get(),
185 attributeTyped<Real>("Q")->get());
186 mSLog->flush();
187}
188
189// #### MNA section ####
190
191void SP::Ph1::Load::mnaParentAddPostStepDependencies(
192 AttributeBase::List &prevStepDependencies,
193 AttributeBase::List &attributeDependencies,
194 AttributeBase::List &modifiedAttributes,
195 Attribute<Matrix>::Ptr &leftVector) {
196 attributeDependencies.push_back(leftVector);
197 modifiedAttributes.push_back(mIntfCurrent);
198 modifiedAttributes.push_back(mIntfVoltage);
199};
200
201void SP::Ph1::Load::mnaParentPostStep(Real time, Int timeStepCount,
202 Attribute<Matrix>::Ptr &leftVector) {
203 mnaCompUpdateVoltage(**leftVector);
204 mnaCompUpdateCurrent(**leftVector);
205}
206
207void SP::Ph1::Load::mnaCompUpdateVoltage(const Matrix &leftVector) {
208 (**mIntfVoltage)(0, 0) =
209 Math::complexFromVectorElement(leftVector, matrixNodeIndex(0));
210}
211
212void SP::Ph1::Load::mnaCompUpdateCurrent(const Matrix &leftVector) {
213 (**mIntfCurrent)(0, 0) = 0;
214
215 for (auto &subc : mSubComponents) {
216 (**mIntfCurrent)(0, 0) += subc->intfCurrent()(0, 0);
217 }
218}
void addMNASubComponent(typename SimPowerComp< Complex >::Ptr subc, MNA_SUBCOMP_TASK_ORDER preStepOrder, MNA_SUBCOMP_TASK_ORDER postStepOrder, Bool contributeToRightVector)
CompositePowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
const Attribute< String >::Ptr mName
Human readable name.
Attribute< T >::Ptr attributeTyped(const String &name) const
Return pointer to an attribute.
String uid()
Returns unique id.
String type()
Get component type (cross-platform)
const Attribute< String >::Ptr mUID
Unique identifier.
AttributeList::Ptr mAttributes
Attribute List.
PowerflowBusType mPowerflowBusType
Define the type of bus the component is modelled by.
const Attribute< Real >::Ptr mReactivePower
Reactive power [VAr].
Definition SP_Ph1_Load.h:31
void mnaCompUpdateVoltage(const Matrix &leftVector) override
Updates internal voltage variable of the component.
void modifyPowerFlowBusType(PowerflowBusType powerflowBusType) override
Modify powerflow bus type.
const Attribute< Real >::Ptr mActivePower
Active power [Watt].
Definition SP_Ph1_Load.h:29
const Attribute< Real >::Ptr mReactivePowerPerUnit
Reactive power [pu].
Definition SP_Ph1_Load.h:35
Real getNomVoltage() const
Get nominal voltage.
PowerProfile mLoadProfile
Load profile data.
Definition SP_Ph1_Load.h:78
Load(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
void createSubComponents() override
Constructs and registers MNA subcomponents; idempotent.
void updatePQ(Real time)
Update PQ for this load for power flow calculation at next time step.
void mnaParentPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
MNA post step operations.
const Attribute< Real >::Ptr mActivePowerPerUnit
Active power [pu].
Definition SP_Ph1_Load.h:33
void mnaCompUpdateCurrent(const Matrix &leftVector) override
Updates internal current variable of the component.
void calculatePerUnitParameters(Real baseApparentPower, Real baseOmega)
Calculates component's parameters in specified per-unit system.
void initializeParentFromNodesAndTerminals(Real frequency) override
Derives values from power flow data and pushes them to subcomponents.
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
SimTerminal< Complex >::List mTerminals
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
std::vector< std::shared_ptr< SimPowerComp< Complex > > > mSubComponents
bool mParametersSet
Flag indicating that parameters are set via setParameters() function.
Logger::Log mSLog
Component logger.