DPsim
Loading...
Searching...
No Matches
PFSolverPowerPolarSparse.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#if defined(WITH_KLU)
7#include <dpsim/KLUAdapter.h>
8#elif defined(WITH_SPARSE)
10#else
12#endif
13
14using namespace DPsim;
15using namespace CPS;
16
18 CPS::String name, const CPS::SystemTopology &system, CPS::Real timeStep,
19 CPS::Logger::Level logLevel)
20 : PFSolverPowerPolar(name, system, timeStep, logLevel) {}
21
23 return G(k, j) != 0.0 || B(k, j) != 0.0;
24}
25
28 std::vector<Eigen::Triplet<Real>> triplets;
29
30 // With zero skipping enabled, the pattern mirrors bus admittance sparsity.
31 // Disabling it retains the dense off-diagonal pattern for comparison runs.
32 for (UInt a = 0; a < npqpv; ++a) {
33 UInt k = mPQPVBusIndices[a];
34 triplets.emplace_back(a, a, 0.0); // J1 diagonal
35 if (a < mNumPQBuses)
36 triplets.emplace_back(a, a + npqpv, 0.0); // J2 diagonal
37 for (UInt b = 0; b < npqpv; ++b) { // J1 off-diagonal
38 if (b != a && (!mZeroSkipping || isConnected(k, mPQPVBusIndices[b])))
39 triplets.emplace_back(a, b, 0.0);
40 }
41 for (UInt b = 0; b < mNumPQBuses; ++b) { // J2 off-diagonal
42 if (b != a && (!mZeroSkipping || isConnected(k, mPQPVBusIndices[b])))
43 triplets.emplace_back(a, b + npqpv, 0.0);
44 }
45 }
46 for (UInt a = 0; a < mNumPQBuses; ++a) {
47 UInt k = mPQPVBusIndices[a];
48 triplets.emplace_back(a + npqpv, a, 0.0); // J3 diagonal
49 triplets.emplace_back(a + npqpv, a + npqpv, 0.0); // J4 diagonal
50 for (UInt b = 0; b < npqpv; ++b) { // J3 off-diagonal
51 if (b != a && (!mZeroSkipping || isConnected(k, mPQPVBusIndices[b])))
52 triplets.emplace_back(a + npqpv, b, 0.0);
53 }
54 for (UInt b = 0; b < mNumPQBuses; ++b) { // J4 off-diagonal
55 if (b != a && (!mZeroSkipping || isConnected(k, mPQPVBusIndices[b])))
56 triplets.emplace_back(a + npqpv, b + npqpv, 0.0);
57 }
58 }
59
61 mJsparse.setFromTriplets(triplets.begin(), triplets.end());
62 mJsparse.makeCompressed();
63}
64
66 if (mNumUnknowns == 0)
67 return;
68
70
71#if defined(WITH_KLU)
72 mLinearSolver = std::make_shared<KLUAdapter>(mSLog);
73#elif defined(WITH_SPARSE)
74 mLinearSolver = std::make_shared<SparseLUAdapter>(mSLog);
75#else
76 mLinearSolver = std::make_shared<DenseLUAdapter>(mSLog);
77#endif
78
79 // Analyze the fixed pattern once; values are refactorized each iteration.
81}
82
85 double val;
86 UInt k, j;
87
88 // Pattern is fixed; clear values and refill the existing entries in place.
89 mJsparse.coeffs().setZero();
90
91 // J1: dP/dTheta
92 for (UInt a = 0; a < npqpv; ++a) {
93 k = mPQPVBusIndices[a];
94 mJsparse.coeffRef(a, a) = -Q(k) - B(k, k) * sol_V.coeff(k) * sol_V.coeff(k);
95 for (UInt b = 0; b < npqpv; ++b) {
96 if (b == a)
97 continue;
98 j = mPQPVBusIndices[b];
99 if (mZeroSkipping && !isConnected(k, j))
100 continue;
101 val = sol_V.coeff(k) * sol_V.coeff(j) *
102 (G(k, j) * sin(sol_D.coeff(k) - sol_D.coeff(j)) -
103 B(k, j) * cos(sol_D.coeff(k) - sol_D.coeff(j)));
104 mJsparse.coeffRef(a, b) = val;
105 }
106 }
107
108 // J2: dP/dV
109 for (UInt a = 0; a < npqpv; ++a) {
110 k = mPQPVBusIndices[a];
111 if (a < mNumPQBuses)
112 mJsparse.coeffRef(a, a + npqpv) =
113 P(k) + G(k, k) * sol_V.coeff(k) * sol_V.coeff(k);
114 for (UInt b = 0; b < mNumPQBuses; ++b) {
115 if (b == a)
116 continue;
117 j = mPQPVBusIndices[b];
118 if (mZeroSkipping && !isConnected(k, j))
119 continue;
120 val = sol_V.coeff(k) * sol_V.coeff(j) *
121 (G(k, j) * cos(sol_D.coeff(k) - sol_D.coeff(j)) +
122 B(k, j) * sin(sol_D.coeff(k) - sol_D.coeff(j)));
123 mJsparse.coeffRef(a, b + npqpv) = val;
124 }
125 }
126
127 // J3: dQ/dTheta
128 for (UInt a = 0; a < mNumPQBuses; ++a) {
129 k = mPQPVBusIndices[a];
130 mJsparse.coeffRef(a + npqpv, a) =
131 P(k) - G(k, k) * sol_V.coeff(k) * sol_V.coeff(k);
132 for (UInt b = 0; b < npqpv; ++b) {
133 if (b == a)
134 continue;
135 j = mPQPVBusIndices[b];
136 if (mZeroSkipping && !isConnected(k, j))
137 continue;
138 val = sol_V.coeff(k) * sol_V.coeff(j) *
139 (G(k, j) * cos(sol_D.coeff(k) - sol_D.coeff(j)) +
140 B(k, j) * sin(sol_D.coeff(k) - sol_D.coeff(j)));
141 mJsparse.coeffRef(a + npqpv, b) = -val;
142 }
143 }
144
145 // J4: dQ/dV
146 for (UInt a = 0; a < mNumPQBuses; ++a) {
147 k = mPQPVBusIndices[a];
148 mJsparse.coeffRef(a + npqpv, a + npqpv) =
149 Q(k) - B(k, k) * sol_V.coeff(k) * sol_V.coeff(k);
150 for (UInt b = 0; b < mNumPQBuses; ++b) {
151 if (b == a)
152 continue;
153 j = mPQPVBusIndices[b];
154 if (mZeroSkipping && !isConnected(k, j))
155 continue;
156 val = sol_V.coeff(k) * sol_V.coeff(j) *
157 (G(k, j) * sin(sol_D.coeff(k) - sol_D.coeff(j)) -
158 B(k, j) * cos(sol_D.coeff(k) - sol_D.coeff(j)));
159 mJsparse.coeffRef(a + npqpv, b + npqpv) = val;
160 }
161 }
162}
163
165 // Full numeric factorization with pivoting on the first iteration of each run;
166 // refactorize (reuse ordering + symbolic) afterwards. mIterations is 0 only on
167 // the first iteration (set after the solve in PFSolver::solvePowerflow).
168 if (mIterations == 0)
169 mLinearSolver->factorize(mJsparse);
170 else
171 mLinearSolver->refactorize(mJsparse);
172
173 Matrix rhs = mF;
174 mX = mLinearSolver->solve(rhs);
175}
spdlog::level::level_enum Level
Definition Logger.h:33
UInt mNumPQBuses
Number of PQ nodes.
Definition PFSolver.h:24
CPS::Vector mX
Solution vector.
Definition PFSolver.h:55
UInt mNumPVBuses
Number of PV nodes.
Definition PFSolver.h:26
CPS::Vector mF
Vector of mismatch values.
Definition PFSolver.h:57
CPS::UInt mIterations
Actual number of iterations.
Definition PFSolver.h:88
UInt mNumUnknowns
Number of unknowns, defining system dimension.
Definition PFSolver.h:30
CPS::Real G(int i, int j)
Gets the real part of admittance matrix element.
Definition PFSolver.cpp:569
std::vector< CPS::UInt > mPQPVBusIndices
Vector with indices of both PQ and PV buses.
Definition PFSolver.h:47
PFSolverPowerPolar(CPS::String name, const CPS::SystemTopology &system, CPS::Real timeStep, CPS::Logger::Level logLevel)
Constructor to be used in simulation examples.
CPS::Vector sol_D
Solution vector of voltage angle.
CPS::Real Q(CPS::UInt k)
Calculate the reactive power at a bus from current solution.
CPS::Real P(CPS::UInt k)
Calculate active power at a bus from current solution.
CPS::Vector sol_V
Solution vector of voltage magnitude.
std::shared_ptr< DirectLinearSolver > mLinearSolver
Direct linear solver reused across iterations (symbolic factorization computed once)
CPS::SparseMatrixRow mJsparse
Sparse Jacobian (row-major, fixed pattern, values updated in place each iteration)
bool isConnected(CPS::UInt k, CPS::UInt j)
Whether buses k and j are connected (off-diagonal Jacobian entry exists)
void setUpJacobianStorage() override
Build the fixed sparsity pattern, create the linear solver and analyze the pattern once.
std::vector< std::pair< CPS::UInt, CPS::UInt > > mVariableSystemMatrixEntries
Empty list: PF uses full refactorization, not partial refactorization.
void calculateJacobian() override
Fill the sparse Jacobian values in place (pattern unchanged)
PFSolverPowerPolarSparse(CPS::String name, const CPS::SystemTopology &system, CPS::Real timeStep, CPS::Logger::Level logLevel)
Constructor to be used in simulation examples.
void solveJacobianSystem() override
Factorize (first iteration of a run) or refactorize, then solve.
void buildJacobianPattern()
Construct the structural sparsity pattern of the Jacobian from the bus admittance matrix.
CPS::Logger::Log mSLog
Logger.
Definition Solver.h:60
Bool mZeroSkipping
Skip exact zero contributions during matrix assembly (enabled by default).
Definition Solver.h:84
std::string String
Definition Definitions.h:63
double Real
Definition Definitions.h:60
CPS::Matrix Matrix
Definition Definitions.h:24
CPS::UInt UInt
Definition Definitions.h:23