deltaFlow
NewtonRaphson.C
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Saud Zahir
3 *
4 * This file is part of deltaFlow.
5 *
6 * deltaFlow is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * deltaFlow is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public
17 * License along with deltaFlow. If not, see
18 * <https://www.gnu.org/licenses/>.
19 */
20
26#include <cmath>
27#include <limits>
28#include <iostream>
29#include <vector>
30
31#include "NewtonRaphson.H"
32#include "Jacobian.H"
33#include "PowerMismatch.H"
34#include "Data.H"
35#include "Utils.H"
36
38 return NewtonRaphson::run(
39 *ctx.G,
40 *ctx.B,
41 *ctx.P,
42 *ctx.Q,
43 *ctx.V,
44 *ctx.delta,
45 ctx.N,
46 ctx.n_pq,
47 *ctx.pq_bus_id,
48 ctx.maxIter,
49 ctx.tolerance,
50 ctx.iterHistory,
51 ctx.observer
52 );
53}
54
56 const Eigen::MatrixXd& G,
57 const Eigen::MatrixXd& B,
58 const Eigen::VectorXd& Ps,
59 const Eigen::VectorXd& Qs,
60 Eigen::VectorXd& V,
61 Eigen::VectorXd& delta,
62 int n_bus,
63 int n_pq,
64 const std::vector<int>& pq_bus_id,
65 int maxIter,
66 double tolerance,
67 std::vector<std::pair<int, double>>* iterHistory,
68 ISolverObserver* observer
69) {
70 // Compute initial mismatch
71 Eigen::VectorXd P(n_bus), Q(n_bus);
72 Eigen::VectorXd mismatch = PowerMismatch::compute(Ps, Qs, G, B, V, delta, n_bus, pq_bus_id, P, Q);
73
74 double error = mismatch.cwiseAbs().maxCoeff();
75 int iter = 0;
76
77 if (iterHistory) {
78 iterHistory->clear();
79 iterHistory->emplace_back(0, error);
80 }
81
82 while (error >= tolerance) {
83 if (iter >= maxIter) {
84 if (observer) observer->onFinished("Newton-Raphson", false, iter, maxIter, error, tolerance);
85 return false;
86 }
87 iter++;
88
89 // Build Jacobian
90 Eigen::MatrixXd J = Jacobian::compute(V, delta, n_bus, n_pq, pq_bus_id, G, B, P, Q);
91
92 // Solve J * correction = mismatch
93 Eigen::VectorXd correction = J.colPivHouseholderQr().solve(mismatch);
94
95 // Update delta for non-slack buses (indices 1..N-1)
96 for (int i = 1; i < n_bus; ++i) {
97 delta(i) += correction(i - 1);
98 }
99
100 // Update V for PQ buses only
101 for (int k = 0; k < n_pq; ++k) {
102 V(pq_bus_id[k]) += correction(n_bus - 1 + k);
103 }
104
105 // Recompute mismatch
106 mismatch = PowerMismatch::compute(Ps, Qs, G, B, V, delta, n_bus, pq_bus_id, P, Q);
107
108 error = mismatch.cwiseAbs().maxCoeff();
109 if (iterHistory) iterHistory->emplace_back(iter, error);
110 if (observer) observer->onIteration("Newton-Raphson", iter, maxIter, error, tolerance);
111 }
112
113 if (observer) observer->onFinished("Newton-Raphson", true, iter, maxIter, error, tolerance);
114 return true;
115}
Data structures and utility functions for reading and displaying bus and branch data in power system ...
Jacobian matrix computation for Newton-Raphson power flow analysis.
Declaration of the Newton-Raphson load flow solver for power system analysis.
Power mismatch computation for Newton-Raphson power flow analysis.
Utility functions and helpers for deltaFlow.
Sink for solver progress/diagnostics (Dependency Inversion / SRP).
Definition ISolver.H:93
virtual void onIteration(const char *solverName, int iter, int maxIter, double error, double tolerance)=0
Called after every iteration with the current error/tolerance.
virtual void onFinished(const char *solverName, bool converged, int iter, int maxIter, double error, double tolerance)=0
Called once after the iteration loop ends (converged or not).
static Eigen::MatrixXd compute(const Eigen::VectorXd &V, const Eigen::VectorXd &delta, int n_bus, int n_pq, const std::vector< int > &pq_bus_id, const Eigen::MatrixXd &G, const Eigen::MatrixXd &B, const Eigen::VectorXd &P, const Eigen::VectorXd &Q)
Computes the Jacobian matrix for the Newton-Raphson power flow solver.
Definition Jacobian.C:30
static bool run(const Eigen::MatrixXd &G, const Eigen::MatrixXd &B, const Eigen::VectorXd &Ps, const Eigen::VectorXd &Qs, Eigen::VectorXd &V, Eigen::VectorXd &delta, int n_bus, int n_pq, const std::vector< int > &pq_bus_id, int maxIter=1024, double tolerance=1E-8, std::vector< std::pair< int, double > > *iterHistory=nullptr, ISolverObserver *observer=nullptr)
Solves the power flow equations using the Newton-Raphson iterative method.
bool solve(SolverContext &ctx) override
Run the Newton-Raphson solver using the given context.
static Eigen::VectorXd compute(const Eigen::VectorXd &Ps, const Eigen::VectorXd &Qs, const Eigen::MatrixXd &G, const Eigen::MatrixXd &B, const Eigen::VectorXd &V, const Eigen::VectorXd &delta, int n_bus, const std::vector< int > &pq_bus_id, Eigen::VectorXd &P, Eigen::VectorXd &Q)
Computes the power mismatch vector for use in Newton-Raphson iterations.
Shared input/output parameters for power-flow solvers.
Definition ISolver.H:49
const Eigen::VectorXd * Q
Scheduled reactive power injections (Qg - Ql).
Definition ISolver.H:65
double tolerance
Convergence tolerance.
Definition ISolver.H:73
Eigen::VectorXd * V
Voltage magnitudes [p.u.].
Definition ISolver.H:56
const Eigen::VectorXd * P
Scheduled active power injections (Pg - Pl).
Definition ISolver.H:64
int maxIter
Maximum number of iterations.
Definition ISolver.H:72
int N
Total number of buses.
Definition ISolver.H:68
Eigen::VectorXd * delta
Voltage angles [rad].
Definition ISolver.H:57
const Eigen::MatrixXd * G
Conductance matrix (used by Newton-Raphson).
Definition ISolver.H:52
class ISolverObserver * observer
Optional injected progress/log observer (DIP); nullptr = silent.
Definition ISolver.H:78
const Eigen::MatrixXd * B
Susceptance matrix (used by Newton-Raphson).
Definition ISolver.H:53
std::vector< std::pair< int, double > > * iterHistory
Optional iteration history sink.
Definition ISolver.H:77
int n_pq
Number of PQ buses (Newton-Raphson).
Definition ISolver.H:69
const std::vector< int > * pq_bus_id
0-based PQ bus indices (Newton-Raphson).
Definition ISolver.H:61