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
103 if (!mParametersSet) {
104 setParameters(mTerminals[0]->singleActivePower(),
105 mTerminals[0]->singleReactivePower(),
106 std::abs(mTerminals[0]->initialSingleVoltage()));
107 }
108
109 // instantiate subResistor for active power consumption
110 if (**mActivePower != 0) {
111 mResistance = std::pow(mNomVoltage, 2) / **mActivePower;
112 mConductance = 1.0 / mResistance;
113 mSubResistor = std::make_shared<SP::Ph1::Resistor>(
114 **mUID + "_res", **mName + "_res", Logger::Level::off);
115 mSubResistor->setParameters(mResistance);
116 mSubResistor->connect({SimNode::GND, mTerminals[0]->node()});
117 mSubResistor->initialize(mFrequencies);
118 mSubResistor->initializeFromNodesAndTerminals(frequency);
119 addMNASubComponent(mSubResistor, MNA_SUBCOMP_TASK_ORDER::NO_TASK,
120 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, false);
121 }
122
123 if (**mReactivePower != 0)
124 mReactance = std::pow(mNomVoltage, 2) / **mReactivePower;
125 else
126 mReactance = 0;
127
128 // instantiate subInductor or subCapacitor for reactive power consumption
129 if (mReactance > 0) {
130 mInductance = mReactance / (2 * PI * frequency);
131 mSubInductor = std::make_shared<SP::Ph1::Inductor>(
132 **mUID + "_res", **mName + "_ind", Logger::Level::off);
133 mSubInductor->setParameters(mInductance);
134 mSubInductor->connect({SimNode::GND, mTerminals[0]->node()});
135 mSubInductor->initialize(mFrequencies);
136 mSubInductor->initializeFromNodesAndTerminals(frequency);
137 addMNASubComponent(mSubInductor, MNA_SUBCOMP_TASK_ORDER::NO_TASK,
138 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, false);
139 } else if (mReactance < 0) {
140 mCapacitance = -1 / (2 * PI * frequency) / mReactance;
141 mSubCapacitor = std::make_shared<SP::Ph1::Capacitor>(
142 **mUID + "_res", **mName + "_cap", Logger::Level::off);
143 mSubCapacitor->setParameters(mCapacitance);
144 mSubCapacitor->connect({SimNode::GND, mTerminals[0]->node()});
145 mSubCapacitor->initialize(mFrequencies);
146 mSubCapacitor->initializeFromNodesAndTerminals(frequency);
147 addMNASubComponent(mSubCapacitor, MNA_SUBCOMP_TASK_ORDER::NO_TASK,
148 MNA_SUBCOMP_TASK_ORDER::TASK_BEFORE_PARENT, false);
149 }
150
151 (**mIntfVoltage)(0, 0) = mTerminals[0]->initialSingleVoltage();
152 (**mIntfCurrent)(0, 0) = std::conj(Complex(attributeTyped<Real>("P")->get(),
153 attributeTyped<Real>("Q")->get()) /
154 (**mIntfVoltage)(0, 0));
155
156 SPDLOG_LOGGER_INFO(mSLog,
157 "\n--- Initialization from powerflow ---"
158 "\nVoltage across: {:s}"
159 "\nCurrent: {:s}"
160 "\nTerminal 0 voltage: {:s}"
161 "\n--- Initialization from powerflow finished ---",
162 Logger::phasorToString((**mIntfVoltage)(0, 0)),
163 Logger::phasorToString((**mIntfCurrent)(0, 0)),
164 Logger::phasorToString(initialSingleVoltage(0)));
165 SPDLOG_LOGGER_INFO(mSLog,
166 "Updated parameters according to powerflow:\n"
167 "Active Power={} [W] Reactive Power={} [VAr]",
168 attributeTyped<Real>("P")->get(),
169 attributeTyped<Real>("Q")->get());
170 mSLog->flush();
171}
172
173// #### MNA section ####
174
175void SP::Ph1::Load::mnaParentAddPostStepDependencies(
176 AttributeBase::List &prevStepDependencies,
177 AttributeBase::List &attributeDependencies,
178 AttributeBase::List &modifiedAttributes,
179 Attribute<Matrix>::Ptr &leftVector) {
180 attributeDependencies.push_back(leftVector);
181 modifiedAttributes.push_back(mIntfCurrent);
182 modifiedAttributes.push_back(mIntfVoltage);
183};
184
185void SP::Ph1::Load::mnaParentPostStep(Real time, Int timeStepCount,
186 Attribute<Matrix>::Ptr &leftVector) {
187 mnaCompUpdateVoltage(**leftVector);
188 mnaCompUpdateCurrent(**leftVector);
189}
190
191void SP::Ph1::Load::mnaCompUpdateVoltage(const Matrix &leftVector) {
192 (**mIntfVoltage)(0, 0) =
193 Math::complexFromVectorElement(leftVector, matrixNodeIndex(0));
194}
195
196void SP::Ph1::Load::mnaCompUpdateCurrent(const Matrix &leftVector) {
197 (**mIntfCurrent)(0, 0) = 0;
198
199 for (auto &subc : mSubComponents) {
200 (**mIntfCurrent)(0, 0) += subc->intfCurrent()(0, 0);
201 }
202}
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.
void initializeFromNodesAndTerminals(Real frequency) override
Initializes component from power flow data.
PowerProfile mLoadProfile
Load profile data.
Definition SP_Ph1_Load.h:75
Load(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
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.
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.