DPsim
Loading...
Searching...
No Matches
SP_Ph1_Load.cpp
Go to the documentation of this file.
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
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.
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);
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
67 switch (powerflowBusType) {
69 throw std::invalid_argument(
70 " Power flow bus type error, load currently cannot be set as PVNode ");
71 break;
73 mPowerflowBusType = powerflowBusType;
74 break;
76 throw std::invalid_argument(
77 " Power flow bus type error, load cannot be set as VDNode ");
78 break;
80 break;
81 default:
82 throw std::invalid_argument(" Invalid power flow bus type ");
83 break;
84 }
85};
86
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()});
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()});
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()});
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 ---",
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
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
202 Attribute<Matrix>::Ptr &leftVector) {
203 mnaCompUpdateVoltage(**leftVector);
204 mnaCompUpdateCurrent(**leftVector);
205}
206
208 (**mIntfVoltage)(0, 0) =
210}
211
213 (**mIntfCurrent)(0, 0) = 0;
214
215 for (auto &subc : mSubComponents) {
216 (**mIntfCurrent)(0, 0) += subc->intfCurrent()(0, 0);
217 }
218}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:250
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.
spdlog::level::level_enum Level
Definition Logger.h:33
static String phasorToString(const Complex &num)
Definition Logger.cpp:57
static Complex complexFromVectorElement(const Matrix &mat, Matrix::Index row, Int maxFreq=1, Int freqIdx=0)
Definition MathUtils.cpp:94
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 setParameters(Real activePower, Real reactivePower, Real nominalVoltage)
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 mnaParentAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
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.
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Complex > >::Ptr mIntfCurrent
SimTerminal< Complex >::List mTerminals
const Attribute< MatrixVar< Complex > >::Ptr mIntfVoltage
std::vector< std::shared_ptr< SimPowerComp< Complex > > > mSubComponents
Complex initialSingleVoltage(UInt index)
bool mParametersSet
Flag indicating that parameters are set via setParameters() function.
Logger::Log mSLog
Component logger.
#define PI
Definition Definitions.h:43
PowerflowBusType
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
Dense matrix for real numbers.
Definition Definitions.h:81
std::string String
Definition Definitions.h:65
double Real
Definition Definitions.h:62
int Int
Definition Definitions.h:61
std::complex< Real > Complex
Definition Definitions.h:63