deltaFlow
Jacobian.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
28#include "Jacobian.H"
29
30Eigen::MatrixXd computeJacobian(
31 const Eigen::VectorXd& V,
32 const Eigen::VectorXd& delta,
33 int n_bus,
34 int n_pq,
35 const std::vector<int>& pq_bus_id,
36 const Eigen::MatrixXd& G,
37 const Eigen::MatrixXd& B,
38 const Eigen::VectorXd& P,
39 const Eigen::VectorXd& Q
40) {
41 // J11: (n_bus-1) x (n_bus-1) - dP/ddelta, non-slack buses
42 Eigen::MatrixXd J11 = Eigen::MatrixXd::Zero(n_bus - 1, n_bus - 1);
43
44 for (int i = 1; i < n_bus; ++i) {
45 for (int k = 1; k < n_bus; ++k) {
46 if (i == k) {
47 J11(i - 1, k - 1) = -Q(i) - V(i) * V(i) * B(i, i);
48 } else {
49 double dik = delta(i) - delta(k);
50 J11(i - 1, k - 1) = V(i) * V(k) * (G(i, k) * std::sin(dik)
51 - B(i, k) * std::cos(dik));
52 }
53 }
54 }
55
56 // J21: n_pq x (n_bus-1) - dQ/ddelta, PQ rows, non-slack columns
57 Eigen::MatrixXd J21 = Eigen::MatrixXd::Zero(n_pq, n_bus - 1);
58
59 for (int i = 0; i < n_pq; ++i) {
60 int j = pq_bus_id[i];
61 for (int k = 1; k < n_bus; ++k) {
62 if (j == k) {
63 J21(i, k - 1) = P(j) - V(j) * V(j) * G(j, j);
64 } else {
65 double djk = delta(j) - delta(k);
66 J21(i, k - 1) = -V(j) * V(k) * (G(j, k) * std::cos(djk)
67 + B(j, k) * std::sin(djk));
68 }
69 }
70 }
71
72 // J12: (n_bus-1) x n_pq - dP/dV, non-slack rows, PQ columns
73 Eigen::MatrixXd J12 = Eigen::MatrixXd::Zero(n_bus - 1, n_pq);
74
75 for (int i = 1; i < n_bus; ++i) {
76 for (int k = 0; k < n_pq; ++k) {
77 int j = pq_bus_id[k];
78 if (i == j) {
79 J12(i - 1, k) = P(j) / V(j) + V(j) * G(j, j);
80 } else {
81 double dij = delta(i) - delta(j);
82 J12(i - 1, k) = V(i) * (G(i, j) * std::cos(dij)
83 + B(i, j) * std::sin(dij));
84 }
85 }
86 }
87
88 // J22: n_pq x n_pq - dQ/dV, PQ rows and columns
89 Eigen::MatrixXd J22 = Eigen::MatrixXd::Zero(n_pq, n_pq);
90
91 for (int i = 0; i < n_pq; ++i) {
92 int j = pq_bus_id[i];
93 for (int k = 0; k < n_pq; ++k) {
94 int l = pq_bus_id[k];
95 if (j == l) {
96 J22(i, k) = Q(j) / V(j) - V(j) * B(j, j);
97 } else {
98 double djl = delta(j) - delta(l);
99 J22(i, k) = V(j) * (G(j, l) * std::sin(djl)
100 - B(j, l) * std::cos(djl));
101 }
102 }
103 }
104
105 // Assemble full Jacobian: [J11 J12; J21 J22]
106 int dim = (n_bus - 1) + n_pq;
107 Eigen::MatrixXd J(dim, dim);
108 J.topLeftCorner(n_bus - 1, n_bus - 1) = J11;
109 J.topRightCorner(n_bus - 1, n_pq) = J12;
110 J.bottomLeftCorner(n_pq, n_bus - 1) = J21;
111 J.bottomRightCorner(n_pq, n_pq) = J22;
112
113 return J;
114}
Eigen::MatrixXd computeJacobian(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
Jacobian matrix computation for Newton-Raphson power flow analysis.