DPsim
Loading...
Searching...
No Matches
EMT_Ph3_SSN_InductionMotor.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
4#include <algorithm>
5#include <cmath>
6#include <stdexcept>
7
8#include <Eigen/Eigenvalues>
9#include <Eigen/LU>
10
12
13using namespace CPS;
14
16 Logger::Level logLevel)
19 mStatorResistance(0.0), mRotorResistance(0.0), mLs(0.0), mLr_dash(0.0),
20 mLm(0.0), mRotorInertia(0.0), mMechanicalDamping(0.0),
42 mElectricalPower(mAttributes->create<Real>("electrical_power")),
43 mReactivePower(mAttributes->create<Real>("reactive_power")),
44 mElectricalTorque(mAttributes->create<Real>("electrical_torque")),
46 mAttributes->create<Real>("mechanical_load_torque")),
47 mMechanicalSpeedLog(mAttributes->create<Real>("mechanical_speed")),
48 mMechanicalSpeedPu(mAttributes->create<Real>("mechanical_speed_pu")),
49 mElectricalAngleLog(mAttributes->create<Real>("electrical_angle")),
50 mSlip(mAttributes->create<Real>("slip")),
51 mStatorCurrentD(mAttributes->create<Real>("stator_current_d")),
52 mStatorCurrentQ(mAttributes->create<Real>("stator_current_q")),
53 mStatorVoltageD(mAttributes->create<Real>("stator_voltage_d")),
54 mStatorVoltageQ(mAttributes->create<Real>("stator_voltage_q")),
56 mAttributes->create<Real>("stator_current_magnitude")),
58 mAttributes->create<Real>("stator_voltage_magnitude")) {
59
60 **mIntfVoltage = Matrix::Zero(mInputSize, 1);
61 **mIntfCurrent = Matrix::Zero(mOutputSize, 1);
62
63 **mElectricalPower = 0.0;
64 **mReactivePower = 0.0;
65 **mElectricalTorque = 0.0;
67 **mMechanicalSpeedLog = 0.0;
68 **mMechanicalSpeedPu = 0.0;
69 **mElectricalAngleLog = 0.0;
70 **mSlip = 0.0;
71 **mStatorCurrentD = 0.0;
72 **mStatorCurrentQ = 0.0;
73 **mStatorVoltageD = 0.0;
74 **mStatorVoltageQ = 0.0;
77}
78
80 return {"psi_sd", "psi_sq", "psi_dr",
81 "psi_qr", "mechanical_speed", "electrical_angle"};
82}
83
85 Real nominalFrequency, Int polePairs, Real statorResistance,
86 Real rotorResistance, Real statorInductance, Real rotorInductance,
87 Real mutualInductance, Real rotorInertia, Real mechanicalDamping,
88 Real mechanicalTorque, Real initialElectricalAngle,
89 Bool autoInitializeMechanicalTorque) {
90
91 if (nominalFrequency <= 0.0)
92 throw std::invalid_argument("Nominal frequency must be positive.");
93 if (polePairs <= 0)
94 throw std::invalid_argument("The number of pole pairs must be positive.");
95
96 if (statorResistance < 0.0)
97 throw std::invalid_argument("Stator resistance is invalid.");
98 if (rotorResistance < 0.0)
99 throw std::invalid_argument("Rotor resistance is invalid.");
100
101 if (statorInductance <= 0.0 || rotorInductance <= 0.0 ||
102 mutualInductance <= 0.0)
103 throw std::invalid_argument("Motor winding inductances must be positive.");
104
105 if (statorInductance <= mutualInductance ||
106 rotorInductance <= mutualInductance)
107 throw std::invalid_argument(
108 "Each total winding inductance must exceed its mutual inductance.");
109
110 if (rotorInertia <= 0.0)
111 throw std::invalid_argument("Rotor inertia must be positive.");
112 if (mechanicalDamping < 0.0)
113 throw std::invalid_argument("Mechanical damping must be non-negative.");
114
115 mNominalFrequency = nominalFrequency;
116 mPolePairs = polePairs;
118 2.0 * PI * mNominalFrequency / static_cast<Real>(mPolePairs);
119
120 mLm = mutualInductance;
121 mLs = statorInductance;
122 mLr_dash = rotorInductance;
123
124 mRotorResistance = rotorResistance;
125 mStatorResistance = statorResistance;
126
127 mRotorInertia = rotorInertia;
128 mMechanicalDamping = mechanicalDamping;
129 mMechanicalTorque = mechanicalTorque;
130 mInitialElectricalAngle = initialElectricalAngle;
131 mAutoInitializeMechanicalTorque = autoInitializeMechanicalTorque;
132
134
135 // Establish dimensions here. The actual local affine model is built after
136 // initialization at a physical operating point.
138 Matrix::Zero(mStateSize, mInputSize),
139 Matrix::Zero(mOutputSize, mStateSize),
140 Matrix::Zero(mOutputSize, mInputSize),
141 Matrix::Zero(mStateSize, 1),
142 Matrix::Zero(mOutputSize, 1));
143}
144
146 mInductanceMatrix.setZero();
147
148 Real denom = mLr_dash * mLs - mLm * mLm;
149 if (denom < DOUBLE_EPSILON)
150 throw std::invalid_argument(
151 "The induction motor inductance matrix is singular.");
152
153 // State order: [sd, sq, rd', kq'].
154 mInverseInductanceMatrix << mLr_dash, 0.0, -mLm, 0.0, 0.0, mLr_dash, 0.0,
155 -mLm, -mLm, 0.0, mLs, 0.0, 0.0, -mLm, 0.0, mLs;
156
157 mInverseInductanceMatrix *= 1. / denom;
158
159 Eigen::FullPivLU<Matrix> decomposition(mInverseInductanceMatrix);
160 if (!decomposition.isInvertible())
161 throw std::invalid_argument(
162 "The induction motor inverse inductance matrix is singular.");
163
164 mInductanceMatrix = decomposition.inverse();
165
166 Eigen::SelfAdjointEigenSolver<Matrix> eigenSolver(mInductanceMatrix);
167 if (eigenSolver.info() != Eigen::Success ||
168 eigenSolver.eigenvalues().minCoeff() <= 0.0)
169 throw std::invalid_argument(
170 "The induction motor inductance matrix is not positive definite.");
171
172 mResistanceMatrix.setZero();
175 mResistiveFluxMatrix.noalias() =
177}
178
180 mMechanicalTorque = mechanicalTorque;
182}
183
185 Real relativeStep, Real absoluteStep) {
186 if (relativeStep <= 0.0)
187 throw std::invalid_argument(
188 "Relative finite-difference step must be positive.");
189 if (absoluteStep <= 0.0)
190 throw std::invalid_argument(
191 "Absolute finite-difference step must be positive.");
192
193 mJacobianRelativeStep = relativeStep;
194 mJacobianAbsoluteStep = absoluteStep;
195}
196
198 Real electricalAngle) const {
199 electricalAngle = std::remainder(electricalAngle, 2.0 * PI);
200
201 MatrixFixedSize<2, 3> transform;
202 const Real scale = std::sqrt(2.0 / 3.0);
203
204 transform.row(0) << scale * std::cos(electricalAngle),
205 scale * std::cos(electricalAngle - 2.0 * PI / 3.0),
206 scale * std::cos(electricalAngle + 2.0 * PI / 3.0);
207
208 transform.row(1) << -scale * std::sin(electricalAngle),
209 -scale * std::sin(electricalAngle - 2.0 * PI / 3.0),
210 -scale * std::sin(electricalAngle + 2.0 * PI / 3.0);
211
212 return transform;
213}
214
217 Real electricalAngle) const {
218 // For balanced abc quantities, the inverse of the orthonormal two-axis
219 // transform is its transpose.
220 return getParkTransformMatrix(electricalAngle).transpose();
221}
222
224 const Matrix &x, const Matrix &u, Matrix &stateDerivative) const {
225 if (x.rows() != mStateSize || x.cols() != 1)
226 throw std::invalid_argument(
227 "Induction motor state vector has an invalid dimension.");
228 if (u.rows() != mInputSize || u.cols() != 1)
229 throw std::invalid_argument(
230 "Induction motor input vector has an invalid dimension.");
231
232 stateDerivative.setZero(mStateSize, 1);
233
234 const MatrixFixedSize<4, 1> flux = x.block(0, 0, mElectricalStateSize, 1);
235 const Real mechanicalSpeed = x(MechanicalSpeed, 0);
236 const Real electricalSpeed = static_cast<Real>(mPolePairs) * mechanicalSpeed;
237
238 const MatrixFixedSize<4, 1> current = mInverseInductanceMatrix * flux;
239 const Real statorCurrentD = current(0, 0);
240 const Real statorCurrentQ = current(1, 0);
241
242 const MatrixFixedSize<2, 1> voltageDq =
243 getParkTransformMatrix(0.0) * u; // squirrel-cage
244
246 windingVoltage(0, 0) = voltageDq(0, 0);
247 windingVoltage(1, 0) = voltageDq(1, 0);
248 windingVoltage(2, 0) = 0.0; // rotor short-circuited
249 windingVoltage(3, 0) = 0.0;
250
251 auto fluxDerivative = stateDerivative.block(0, 0, mElectricalStateSize, 1);
252 fluxDerivative.noalias() = mResistiveFluxMatrix * flux;
253 fluxDerivative += windingVoltage;
254 fluxDerivative(2, 0) -= electricalSpeed * flux(3, 0);
255 fluxDerivative(3, 0) += electricalSpeed * flux(2, 0);
256
257 const Real electricalTorque =
258 static_cast<Real>(mPolePairs) *
259 (flux(PsiSd, 0) * statorCurrentQ - flux(PsiSq, 0) * statorCurrentD);
260
261 // Current is positive entering the machine. In generator operation the
262 // electromagnetic torque is therefore normally negative. The applied shaft
263 // torque is positive in the direction of rotation.
264 stateDerivative(MechanicalSpeed, 0) =
265 (mMechanicalTorque + electricalTorque -
267 (mechanicalSpeed -
268 mNominalMechanicalSpeed)) / //steady state: synchronous speed as equilibrium point or slip?
270
271 stateDerivative(ElectricalAngle, 0) = electricalSpeed;
272}
273
275 const Matrix &u,
276 Matrix &output) const {
277 (void)u;
278
279 if (x.rows() != mStateSize || x.cols() != 1)
280 throw std::invalid_argument(
281 "Induction motor state vector has an invalid dimension.");
282
283 const MatrixFixedSize<4, 1> flux = x.block(0, 0, mElectricalStateSize, 1);
284 const MatrixFixedSize<4, 1> current = mInverseInductanceMatrix * flux;
285
286 MatrixFixedSize<2, 1> statorCurrentDq;
287 statorCurrentDq << current(0, 0), current(1, 0);
288
289 // Current entering the stator terminals, as required by the V-type stamp.
290 output = getInverseParkTransformMatrix(0.0) * statorCurrentDq;
291}
292
294 const Matrix &x, const Matrix &u, Matrix &A, Matrix &B, Matrix &C,
295 Matrix &D) const {
296 A.setZero(mStateSize, mStateSize);
297 B.setZero(mStateSize, mInputSize);
298 C.setZero(mOutputSize, mStateSize);
299 D.setZero(mOutputSize, mInputSize);
300
301 for (Int column = 0; column < mStateSize; ++column) {
302 const Real step =
304 mJacobianRelativeStep * std::max(1.0, std::abs(x(column, 0)));
305
307 mJacobianPerturbation(column, 0) += step;
310
312 mJacobianPerturbation(column, 0) -= step;
315
316 A.col(column) =
318 C.col(column) = (mJacobianOutputPlus - mJacobianOutputMinus) / (2.0 * step);
319 }
320
321 for (Int column = 0; column < mInputSize; ++column) {
322 const Real step =
324 mJacobianRelativeStep * std::max(1.0, std::abs(u(column, 0)));
325
327 mJacobianInputPerturbation(column, 0) += step;
331 mJacobianInputPerturbation(column, 0) -= step;
334 // The stator-current output depends on state only; D is exactly zero.
335 // Avoid re-evaluating that same output for each input perturbation.
336 B.col(column) =
338 }
339}
340
342 const Matrix &x, const Matrix &u, Matrix &A, Matrix &B, Matrix &C,
343 Matrix &D, Matrix &E, Matrix &F) const {
344 calculateNumericalJacobians(x, u, A, B, C, D);
345
348
349 E = mStateDerivativeScratch - A * x - B * u;
350 F = mOutputScratch - C * x - D * u;
351}
352
362
364 const Matrix &x = **mX;
365 const MatrixFixedSize<4, 1> flux = x.block(0, 0, mElectricalStateSize, 1);
366 const MatrixFixedSize<4, 1> current = mInverseInductanceMatrix * flux;
367 const MatrixFixedSize<2, 1> voltageDq = getParkTransformMatrix(0.0) * u;
368
369 MatrixFixedSize<2, 1> statorCurrentDq;
370 statorCurrentDq << current(0, 0), current(1, 0);
371
372 const Real electricalTorque =
373 static_cast<Real>(mPolePairs) *
374 (flux(PsiSd, 0) * current(1, 0) - flux(PsiSq, 0) * current(0, 0));
375
376 const Real electricalPower = voltageDq(0, 0) * statorCurrentDq(0, 0) +
377 voltageDq(1, 0) * statorCurrentDq(1, 0);
378 const Real reactivePower = voltageDq(1, 0) * statorCurrentDq(0, 0) -
379 voltageDq(0, 0) * statorCurrentDq(1, 0);
380 const Real mechanicalLoadTorque =
383
384 **mElectricalPower = electricalPower;
385 **mReactivePower = reactivePower;
386 **mElectricalTorque = electricalTorque;
387 **mMechanicalLoadTorque = mechanicalLoadTorque;
390 **mElectricalAngleLog = std::remainder(x(ElectricalAngle, 0), 2.0 * PI);
393 **mStatorCurrentD = current(0, 0);
394 **mStatorCurrentQ = current(1, 0);
395 **mStatorVoltageD = voltageDq(0, 0);
396 **mStatorVoltageQ = voltageDq(1, 0);
397 **mStatorCurrentMagnitude = statorCurrentDq.norm();
398 **mStatorVoltageMagnitude = voltageDq.norm();
399}
400
402 Real frequency) {
403 if (!mParametersSet)
404 throw std::logic_error(
405 "setParameters() must be called before initialization.");
406
407 const MatrixComp initialVoltagePhasor = buildInitialInputFromNodes(frequency);
408 const Matrix initialVoltageAbc = initialVoltagePhasor.real();
409
410 Matrix x0 = Matrix::Zero(mStateSize, 1);
411 const Complex terminalPower =
412 terminal(1)->singlePower() - terminal(0)->singlePower();
413 const MatrixComp voltageDq =
414 getParkTransformMatrix(0.0) * initialVoltagePhasor;
415 const Complex voltageSpacePhasor = voltageDq(0, 0);
416
418 if (std::abs(voltageSpacePhasor) <= DOUBLE_EPSILON ||
419 !std::isfinite(terminalPower.real()) ||
420 !std::isfinite(terminalPower.imag()))
421 throw std::logic_error(
422 "Cannot derive induction-motor steady state from empty or invalid "
423 "power-flow data.");
424
425 // The orthonormal alpha-beta transform preserves three-phase complex
426 // power. The PF terminal powers are in the consumer convention here.
427 const Complex statorCurrentPhasor =
428 std::conj(terminalPower / voltageSpacePhasor);
429
430 // In the stationary frame, the stator flux equation at synchronous
431 // frequency is j*w*psi_s = v_s - Rs*i_s. The rotor is short-circuited;
432 // its steady-state equation determines slip from the same PF operating
433 // point and the configured rotor resistance.
434 const Real synchronousElectricalSpeed = 2.0 * PI * frequency;
435 const Complex statorFluxPhasor =
436 (voltageSpacePhasor - mStatorResistance * statorCurrentPhasor) /
437 Complex(0.0, synchronousElectricalSpeed);
438 const Complex rotorCurrentPhasor =
439 (statorFluxPhasor - mLs * statorCurrentPhasor) / mLm;
440 const Complex rotorFluxPhasor =
441 mLm * statorCurrentPhasor + mLr_dash * rotorCurrentPhasor;
442
443 if (std::abs(rotorFluxPhasor) <= DOUBLE_EPSILON)
444 throw std::logic_error(
445 "Cannot derive induction-motor slip: rotor flux is zero.");
446
447 // Minimize |Rr*ir + j*w_slip*psi_r| over real w_slip. A large residual
448 // means the PF P/Q point is incompatible with the configured motor data.
449 const Complex slipCoefficient = Complex(0.0, 1.0) * rotorFluxPhasor;
450 const Real slipElectricalSpeed =
451 -std::real(std::conj(slipCoefficient) *
452 (mRotorResistance * rotorCurrentPhasor)) /
453 std::norm(slipCoefficient);
454 const Complex rotorEquationResidual =
455 mRotorResistance * rotorCurrentPhasor +
456 Complex(0.0, slipElectricalSpeed) * rotorFluxPhasor;
457 if (std::abs(rotorEquationResidual) >
458 1e-3 * std::max(1.0, std::abs(mRotorResistance * rotorCurrentPhasor)))
459 throw std::logic_error(
460 "Power-flow operating point is inconsistent with the configured "
461 "induction-motor parameters.");
462
463 const Real mechanicalSpeed =
464 (synchronousElectricalSpeed - slipElectricalSpeed) /
465 static_cast<Real>(mPolePairs);
466 if (!std::isfinite(mechanicalSpeed) || mechanicalSpeed <= 0.0)
467 throw std::logic_error(
468 "Power-flow operating point implies a non-positive induction-motor "
469 "speed.");
470
471 // A rotating complex space vector z = d + j*q has instantaneous initial
472 // state [real(z), imag(z)] at t=0 in this stationary reference frame.
473 x0(PsiSd, 0) = statorFluxPhasor.real();
474 x0(PsiSq, 0) = statorFluxPhasor.imag();
475 x0(PsiRotorD, 0) = rotorFluxPhasor.real();
476 x0(PsiRotorQ, 0) = rotorFluxPhasor.imag();
477 x0(MechanicalSpeed, 0) = mechanicalSpeed;
479
480 const Matrix initialCurrent =
482 const Real initialTorque =
483 static_cast<Real>(mPolePairs) * (x0(PsiSd, 0) * initialCurrent(1, 0) -
484 x0(PsiSq, 0) * initialCurrent(0, 0));
486 -initialTorque +
487 mMechanicalDamping * (mechanicalSpeed - mNominalMechanicalSpeed);
488 } else {
489 // Preserve the historical cold-start operating point when automatic
490 // power-flow initialization is disabled.
491 x0(MechanicalSpeed, 0) = 0.0;
493 }
494
495 **mX = x0;
496 **mIntfVoltage = initialVoltageAbc;
497
500 **mIntfCurrent = mW * (**mIntfVoltage) + mYHist;
502
503 SPDLOG_LOGGER_INFO(
504 mSLog,
505 "Initialized induction motor at the PF operating point: "
506 "P={:.6g} W, Q={:.6g} var, speed={:.6g} rad/s, slip={:.6g}, "
507 "shaft torque={:.6g} Nm.",
508 terminalPower.real(), terminalPower.imag(), **mMechanicalSpeedLog,
510}
511
513
515 Matrix stateDerivative = Matrix::Zero(mStateSize, 1);
516 evaluateStateDerivative(**mX, **mIntfVoltage, stateDerivative);
517 return stateDerivative;
518}
519
523
void evaluateOutput(const Matrix &x, const Matrix &u, Matrix &output) const
std::vector< String > getLocalStateNames() const override
void initializeFromNodesAndTerminals(Real frequency) override
Initializes Component variables according to power flow data stored in Nodes.
void evaluateStateDerivative(const Matrix &x, const Matrix &u, Matrix &stateDerivative) const
void setParameters(Real nominalFrequency, Int polePairs, Real statorResistance, Real rotorResistance, Real statorInductance, Real rotorInductance, Real mutualInductance, Real rotorInertia, Real mechanicalDamping, Real mechanicalTorque, Real initialElectricalAngle=0.0, Bool autoInitializeMechanicalTorque=true)
Configure the sixth-order machine.
SSN_InductionMotor(String uid, String name, Logger::Level logLevel=Logger::Level::off)
void updateLogAttributes(const Matrix &u) const override
Update derived attributes used for logging/inspection.
Matrix mJacobianPerturbation
Pre-sized scratch storage for the per-step finite-difference Jacobian.
void buildStateSpaceModel(const Matrix &x, const Matrix &u, Matrix &A, Matrix &B, Matrix &C, Matrix &D, Matrix &E, Matrix &F) const
MatrixFixedSize< 3, 2 > getInverseParkTransformMatrix(Real electricalAngle) const
MatrixFixedSize< 2, 3 > getParkTransformMatrix(Real electricalAngle) const
void setNumericalLinearizationParameters(Real relativeStep, Real absoluteStep)
void calculateNumericalJacobians(const Matrix &x, const Matrix &u, Matrix &A, Matrix &B, Matrix &C, Matrix &D) const
TwoTerminalVTypeVariableSSNComp(String uid, String name, Logger::Level logLevel=Logger::Level::off)
const Attribute< Matrix >::Ptr mX
Definition EMT_SSNComp.h:48
void calculateHistoryVector(Matrix &history) const override final
void setParameters(const Matrix &A, const Matrix &B, const Matrix &C, const Matrix &D)
void updateStateSpaceModel() override final
Hook for variable/time-varying SSN components.
String uid()
Returns unique id.
AttributeList::Ptr mAttributes
Attribute List.
spdlog::level::level_enum Level
Definition Logger.h:33
const Attribute< MatrixVar< Real > >::Ptr mIntfCurrent
SimTerminal< Real >::Ptr terminal(UInt index)
const Attribute< MatrixVar< Real > >::Ptr mIntfVoltage
bool mParametersSet
Flag indicating that parameters are set via setParameters() function.
Logger::Log mSLog
Component logger.
#define PI
Definition Definitions.h:43
#define DOUBLE_EPSILON
Definition Definitions.h:14
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
std::complex< Real > Complex
Definition Definitions.h:61
Eigen::Matrix< Complex, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixComp
Dense matrix for complex numbers.
Definition Definitions.h:82
bool Bool
Definition Definitions.h:62