DPsim
Loading...
Searching...
No Matches
EMT_Ph3_PQLoad.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Institute for Automation of Complex Power Systems, EONERC, RWTH Aachen University
2// SPDX-License-Identifier: MPL-2.0
3
5
6#include <algorithm>
7#include <cmath>
8#include <stdexcept>
9
10using namespace CPS;
11
12namespace {
13
14bool isFinitePhaseVector(const Matrix &matrix) {
15 return matrix.rows() == 3 && matrix.cols() == 1 &&
16 Math::isFinite(matrix(0, 0)) && Math::isFinite(matrix(1, 0)) &&
17 Math::isFinite(matrix(2, 0));
18}
19
20} // namespace
21
23 : MNASimPowerComp<Real>(uid, name, true, true, logLevel),
24 mActivePower(mAttributes->create<Real>("P", 0.0)),
25 mReactivePower(mAttributes->create<Real>("Q", 0.0)),
26 mNomVoltage(mAttributes->create<Real>("V_nom", 0.0)),
27 mMinimumVoltagePerUnit(mAttributes->create<Real>("V_min_pu", 0.1)),
28 mMeasuredActivePower(mAttributes->create<Real>("p_inst", 0.0)),
29 mMeasuredReactivePower(mAttributes->create<Real>("q_inst", 0.0)) {
32
33 **mIntfVoltage = Matrix::Zero(3, 1);
34 **mIntfCurrent = Matrix::Zero(3, 1);
35 mNumIter = mAttributes->create<Int>("NIterations", 0);
36
37 SPDLOG_LOGGER_INFO(mSLog, "Create {} {}", this->type(), name);
38}
39
42
43EMT::Ph3::PQLoad::PQLoad(String name, Real activePower, Real reactivePower,
44 Real nominalVoltage, Logger::Level logLevel)
45 : PQLoad(name, name, activePower, reactivePower, nominalVoltage, logLevel) {
46}
47
49 Real reactivePower, Real nominalVoltage,
50 Logger::Level logLevel)
51 : PQLoad(uid, name, logLevel) {
52 setParameters(activePower, reactivePower, nominalVoltage);
53}
54
55void EMT::Ph3::PQLoad::setParameters(Real activePower, Real reactivePower,
56 Real nominalVoltage,
57 Real minimumVoltagePerUnit) {
58 if (!Math::isFinite(activePower) || !Math::isFinite(reactivePower))
59 throw std::invalid_argument("PQLoad power set points must be finite.");
60 if (!Math::isFinite(nominalVoltage) || nominalVoltage <= 0.0)
61 throw std::invalid_argument("PQLoad nominal voltage must be positive.");
62 if (!Math::isFinite(minimumVoltagePerUnit) || minimumVoltagePerUnit <= 0.0)
63 throw std::invalid_argument(
64 "PQLoad minimum per-unit voltage must be positive.");
65
66 **mActivePower = activePower;
67 **mReactivePower = reactivePower;
68 **mNomVoltage = nominalVoltage;
69 **mMinimumVoltagePerUnit = minimumVoltagePerUnit;
70 mParametersSet = true;
71
72 SPDLOG_LOGGER_INFO(mSLog,
73 "\nActive power={} [W]"
74 "\nReactive power={} [var]"
75 "\nNominal line-to-line voltage={} [V]"
76 "\nMinimum voltage={} [pu]",
79}
80
82 auto copy = PQLoad::make(name, mLogLevel);
83 if (mParametersSet) {
84 copy->setParameters(**mActivePower, **mReactivePower, **mNomVoltage,
86 } else {
87 **copy->mMinimumVoltagePerUnit = **mMinimumVoltagePerUnit;
88 }
89 copy->setMaxIterations(mMaxIter);
90 copy->setTolerance(mTolerance);
91 return copy;
92}
93
95 Matrix voltageQuadrature = Matrix::Zero(3, 1);
96 voltageQuadrature << (voltage(1, 0) - voltage(2, 0)) / std::sqrt(3.0),
97 (voltage(2, 0) - voltage(0, 0)) / std::sqrt(3.0),
98 (voltage(0, 0) - voltage(1, 0)) / std::sqrt(3.0);
99 return voltageQuadrature;
100}
101
104 if (!isFinitePhaseVector(voltage))
106
107 const Real phaseA = voltage(0, 0);
108 const Real phaseB = voltage(1, 0);
109 const Real phaseC = voltage(2, 0);
110 const Real meanVoltage = (phaseA + phaseB + phaseC) / 3.0;
111 const Real voltageA = phaseA - meanVoltage;
112 const Real voltageB = phaseB - meanVoltage;
113 const Real voltageC = phaseC - meanVoltage;
114 constexpr Real inverseSqrtThree = 0.57735026918962576451;
115 const Real quadratureA = (voltageB - voltageC) * inverseSqrtThree;
116 const Real quadratureB = (voltageC - voltageA) * inverseSqrtThree;
117 const Real quadratureC = (voltageA - voltageB) * inverseSqrtThree;
118 const Real activePower = **mActivePower;
119 const Real reactivePower = **mReactivePower;
120 const Real minimumVoltage = **mMinimumVoltagePerUnit * **mNomVoltage;
121 const Real denominator =
122 std::max(voltageA * voltageA + voltageB * voltageB + voltageC * voltageC,
123 minimumVoltage * minimumVoltage);
124
125 MatrixFixedSize<3, 1> current;
126 current(0, 0) =
127 (activePower * voltageA + reactivePower * quadratureA) / denominator;
128 current(1, 0) =
129 (activePower * voltageB + reactivePower * quadratureB) / denominator;
130 current(2, 0) =
131 (activePower * voltageC + reactivePower * quadratureC) / denominator;
132 return current;
133}
134
136 const Matrix &voltage = **mIntfVoltage;
137 const Matrix &current = **mIntfCurrent;
138 const Real meanVoltage = voltage.mean();
139 const Real voltageA = voltage(0, 0) - meanVoltage;
140 const Real voltageB = voltage(1, 0) - meanVoltage;
141 const Real voltageC = voltage(2, 0) - meanVoltage;
142 constexpr Real inverseSqrtThree = 0.57735026918962576451;
143 const Real quadratureA = (voltageB - voltageC) * inverseSqrtThree;
144 const Real quadratureB = (voltageC - voltageA) * inverseSqrtThree;
145 const Real quadratureC = (voltageA - voltageB) * inverseSqrtThree;
146
147 **mMeasuredActivePower = voltageA * current(0, 0) + voltageB * current(1, 0) +
148 voltageC * current(2, 0);
149 **mMeasuredReactivePower = quadratureA * current(0, 0) +
150 quadratureB * current(1, 0) +
151 quadratureC * current(2, 0);
152}
153
155 if (!mParametersSet) {
156 **mActivePower = mTerminals[0]->singleActivePower();
157 **mReactivePower = mTerminals[0]->singleReactivePower();
158 **mNomVoltage = std::abs(mTerminals[0]->initialSingleVoltage());
159
160 if (**mNomVoltage <= 0.0) {
161 throw std::invalid_argument(
162 "PQLoad requires a positive nominal voltage when initialized from "
163 "terminal data.");
164 }
165 }
166
167 MatrixComp initialVoltageABC = MatrixComp::Zero(3, 1);
168 initialVoltageABC(0, 0) =
169 RMS3PH_TO_PEAK1PH * mTerminals[0]->initialSingleVoltage();
170 initialVoltageABC(1, 0) = initialVoltageABC(0, 0) * SHIFT_TO_PHASE_B;
171 initialVoltageABC(2, 0) = initialVoltageABC(0, 0) * SHIFT_TO_PHASE_C;
172
173 **mIntfVoltage = initialVoltageABC.real();
176
177 SPDLOG_LOGGER_INFO(mSLog,
178 "\n--- Initialization ---"
179 "\nVoltage: {:s}"
180 "\nCurrent: {:s}"
181 "\nActive-power set point: {} [W]"
182 "\nReactive-power set point: {} [var]"
183 "\n--- Initialization finished ---",
187}
188
193
195 SparseMatrixRow &systemMatrix) {
196 // The PQ load is represented by an ideal current sink and therefore has no
197 // system-matrix stamp.
198}
199
201 if (!terminalNotGrounded(0))
202 return;
203
204 for (UInt phase = 0; phase < 3; ++phase) {
205 // Positive interface current is consumed by the load and therefore is a
206 // negative injection into the network node.
207 Math::setVectorElement(rightVector, matrixNodeIndex(0, phase),
208 -(**mIntfCurrent)(phase, 0));
209 }
210}
211
213 if (!terminalNotGrounded(0))
214 return;
215
216 for (UInt phase = 0; phase < 3; ++phase)
217 Math::addToVectorElement(rightVector, matrixNodeIndex(0, phase),
218 -(**mIntfCurrent)(phase, 0));
219}
220
222 (**mIntfVoltage).setZero();
223 if (!terminalNotGrounded(0))
224 return;
225
226 for (UInt phase = 0; phase < 3; ++phase) {
227 (**mIntfVoltage)(phase, 0) =
228 Math::realFromVectorElement(leftVector, matrixNodeIndex(0, phase));
229 }
230}
231
232void EMT::Ph3::PQLoad::mnaCompPreStep(Real time, Int timeStepCount) {
233 **mNumIter = 0;
235}
236
238 Attribute<Matrix>::Ptr &leftVector) {
239 mnaCompUpdateVoltage(**leftVector);
241}
242
244 AttributeBase::List &prevStepDependencies,
245 AttributeBase::List &attributeDependencies,
246 AttributeBase::List &modifiedAttributes) {
247 prevStepDependencies.push_back(mIntfVoltage);
248 attributeDependencies.push_back(mActivePower);
249 attributeDependencies.push_back(mReactivePower);
250 attributeDependencies.push_back(mNomVoltage);
251 attributeDependencies.push_back(mMinimumVoltagePerUnit);
252 modifiedAttributes.push_back(mIntfCurrent);
253 modifiedAttributes.push_back(mNumIter);
254 modifiedAttributes.push_back(mRightVector);
255}
256
258 AttributeBase::List &prevStepDependencies,
259 AttributeBase::List &attributeDependencies,
260 AttributeBase::List &modifiedAttributes,
261 Attribute<Matrix>::Ptr &leftVector) {
262 attributeDependencies.push_back(leftVector);
263 attributeDependencies.push_back(mIntfCurrent);
264 modifiedAttributes.push_back(mIntfVoltage);
265 modifiedAttributes.push_back(mMeasuredActivePower);
266 modifiedAttributes.push_back(mMeasuredReactivePower);
267}
268
275
277 mnaCompUpdateVoltage(leftVector);
278}
279
281 if (!isFinitePhaseVector(**mIntfVoltage) ||
282 !isFinitePhaseVector(**mIntfCurrent))
283 return false;
284
285 const MatrixFixedSize<3, 1> targetCurrent = calculateCurrent(**mIntfVoltage);
286 const Real currentBase = std::hypot(**mActivePower, **mReactivePower) /
287 std::max(**mNomVoltage, 1.0);
288 const Real currentScale =
289 std::max({targetCurrent.norm(), currentBase, 1e-12});
290 const Matrix &current = **mIntfCurrent;
291 const Real currentError =
292 std::hypot(targetCurrent(0, 0) - current(0, 0),
293 std::hypot(targetCurrent(1, 0) - current(1, 0),
294 targetCurrent(2, 0) - current(2, 0)));
295 const Real relativeError = currentError / currentScale;
296
297 if (relativeError <= mTolerance)
298 return false;
299
300 if (**mNumIter >= mMaxIter) {
301 SPDLOG_LOGGER_WARN(mSLog,
302 "PQLoad corrector reached its iteration limit ({}): "
303 "relative current residual={} (tolerance={}).",
304 mMaxIter, relativeError, mTolerance);
305 return false;
306 }
307
308 return true;
309}
std::vector< Ptr > List
Definition Attribute.h:123
AttributePointer< Attribute< T > > Ptr
Definition Attribute.h:249
void mnaCompInitialize(Real omega, Real timeStep, Attribute< Matrix >::Ptr leftVector) override
const Attribute< Real >::Ptr mActivePower
Three-phase total active-power set point [W], positive for consumption.
void mnaAddCompactRightSideVectorStamp(Matrix &rightVector) override
void correctorStep() override
void mnaCompApplyRightSideVectorStamp(Matrix &rightVector) override
void updateVoltage(const Matrix &leftVector) override
void mnaCompPreStep(Real time, Int timeStepCount) override
void updateMeasuredPower()
Updates the instantaneous measured powers.
void setParameters(Real activePower, Real reactivePower, Real nominalVoltage, Real minimumVoltagePerUnit=0.1)
Sets total three-phase P/Q, nominal line-to-line voltage and voltage floor.
const Attribute< Real >::Ptr mMeasuredReactivePower
Instantaneous measured three-phase reactive power [var].
PQLoad(String uid, String name, Logger::Level logLevel=Logger::Level::off)
Defines UID, name and logging level.
void mnaCompAddPostStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes, Attribute< Matrix >::Ptr &leftVector) override
SimPowerComp< Real >::Ptr clone(String name) override
Returns a modified copy of the component with the given suffix added to the name and without.
static Matrix quadratureVoltage(const Matrix &voltage)
Calculates the voltage vector shifted by 90 degrees for positive sequence.
void mnaCompUpdateVoltage(const Matrix &leftVector) override
const Attribute< Real >::Ptr mReactivePower
bool requiresIteration() override
void mnaCompApplySystemMatrixStamp(SparseMatrixRow &systemMatrix) override
void initializeFromNodesAndTerminals(Real frequency) override
Initializes set points and instantaneous values from terminal data.
const Attribute< Real >::Ptr mNomVoltage
Nominal line-to-line RMS voltage [V].
const Attribute< Real >::Ptr mMeasuredActivePower
Instantaneous measured three-phase active power [W].
void mnaCompAddPreStepDependencies(AttributeBase::List &prevStepDependencies, AttributeBase::List &attributeDependencies, AttributeBase::List &modifiedAttributes) override
const Attribute< Real >::Ptr mMinimumVoltagePerUnit
Minimum line-to-line voltage in per unit of mNomVoltage.
MatrixFixedSize< 3, 1 > calculateCurrent(const Matrix &voltage) const
Calculates the load current for a given instantaneous abc voltage.
void mnaCompPostStep(Real time, Int timeStepCount, Attribute< Matrix >::Ptr &leftVector) override
String uid()
Returns unique id.
String type()
Get component type (cross-platform)
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
static String matrixToString(const Matrix &mat)
Definition Logger.cpp:31
MNASimPowerComp(String uid, String name, Bool hasPreStep, Bool hasPostStep, Logger::Level logLevel)
Attribute< Matrix >::Ptr mRightVector
Attribute< Int >::Ptr mNumIter
static Real realFromVectorElement(const Matrix &mat, Matrix::Index row)
static void setVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0, Matrix::Index colOffset=0)
static void addToVectorElement(Matrix &mat, Matrix::Index row, Complex value, Int maxFreq=1, Int freqIdx=0)
static bool isFinite(Real value)
UInt matrixNodeIndex(UInt nodeIndex)
const Attribute< MatrixVar< Real > >::Ptr mIntfCurrent
SimTerminal< Real >::List mTerminals
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
std::shared_ptr< SimPowerComp< VarType > > Ptr
Bool terminalNotGrounded(UInt index)
Complex initialSingleVoltage(UInt index)
void setTerminalNumber(UInt num)
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.
static std::shared_ptr< PQLoad > make(Args &&...args)
Definition PtrFactory.h:19
#define RMS3PH_TO_PEAK1PH
Definition Definitions.h:50
#define SHIFT_TO_PHASE_C
Definition Definitions.h:47
#define SHIFT_TO_PHASE_B
Definition Definitions.h:46
Eigen::Matrix< Real, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > Matrix
Dense matrix for real numbers.
Definition Definitions.h:79
std::string String
Definition Definitions.h:63
Eigen::Matrix< Real, rows, cols, Eigen::ColMajor > MatrixFixedSize
Dense matrix for real numbers with fixed dimension.
Definition Definitions.h:99
double Real
Definition Definitions.h:60
int Int
Definition Definitions.h:59
Eigen::Matrix< Complex, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixComp
Dense matrix for complex numbers.
Definition Definitions.h:82
unsigned int UInt
Definition Definitions.h:58
Eigen::SparseMatrix< Real, Eigen::RowMajor > SparseMatrixRow
Sparse matrix for real numbers (row major).
Definition Definitions.h:72