deltaFlow
Admittance.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 <complex>
27#include <string>
28
29#include <fmt/core.h>
30
31#include "Admittance.H"
32#include "Data.H"
33
34Eigen::MatrixXcd Admittance::build(const BusData& busData, const BranchData& branchData, ISolverObserver* observer) {
35 int nLine = branchData.From.size();
36 int N = std::max(branchData.From.maxCoeff(), branchData.To.maxCoeff()); // 1-based bus indexing
37
38 Eigen::MatrixXcd Ybus = Eigen::MatrixXcd::Zero(N, N); // Size N x N, 0-based indexing internally
39
40 for (int k = 0; k < nLine; ++k) {
41 int from = branchData.From(k) - 1; // Convert to 0-based
42 int to = branchData.To(k) - 1; // Convert to 0-based
43
44 std::complex<double> Z(branchData.R(k), branchData.X(k));
45 std::complex<double> Y = 1.0 / Z;
46
47 std::complex<double> B(0.0, 0.5 * branchData.B(k));
48
49 double a = branchData.tapRatio(k);
50 if (a == 0.0) {
51 a = 1.0;
52 }
53
54 // Off-diagonal
55 Ybus(from, to) -= Y / a;
56 Ybus(to, from) = Ybus(from, to); // Symmetric
57
58 // Diagonal
59 Ybus(from, from) += Y / (a * a) + B;
60 Ybus(to, to) += Y + B;
61 }
62
63 // Add shunt admittances
64 int nBuses = busData.ID.size(); // Should match Gs and Bs size
65
66 for (int n = 0; n < nBuses; ++n) {
67 int busIndex = busData.ID(n) - 1; // Convert to 0-based
68
69 if (busIndex < 0 || busIndex >= Ybus.rows()) {
70 if (observer) {
71 std::string msg = fmt::format("Warning: Bus ID {} out of bounds in Ybus", busIndex + 1);
72 observer->onMessage("Admittance", msg.c_str());
73 }
74 continue;
75 }
76
77 std::complex<double> Y_shunt(busData.Gs(n), busData.Bs(n));
78 Ybus(busIndex, busIndex) += Y_shunt;
79 }
80
81 return Ybus;
82}
Declaration of a class for constructing the bus admittance matrix ($$ Y_{bus} $$) in power system ana...
Data structures and utility functions for reading and displaying bus and branch data in power system ...
static Eigen::MatrixXcd build(const BusData &busData, const BranchData &branchData, ISolverObserver *observer=nullptr)
Computes the complex bus admittance matrix ($$ Y_{bus} $$).
Definition Admittance.C:34
Sink for solver progress/diagnostics (Dependency Inversion / SRP).
Definition ISolver.H:93
virtual void onMessage(const char *solverName, const char *message)=0
Called for solver-specific diagnostic/warning messages (e.g.
Contains all relevant data for each transmission line or transformer branch.
Definition Data.H:83
Eigen::VectorXd tapRatio
Transformer tap ratio ($$ a $$)
Definition Data.H:90
Eigen::VectorXd B
Line susceptance ($$ B $$) [p.u.].
Definition Data.H:89
Eigen::VectorXd X
Reactance ($$ X $$) [p.u.].
Definition Data.H:87
Eigen::VectorXi From
From bus indices.
Definition Data.H:84
Eigen::VectorXd R
Resistance ($$ R $$) [p.u.].
Definition Data.H:86
Eigen::VectorXi To
To bus indices.
Definition Data.H:85
Contains all relevant data for each bus in the power system.
Definition Data.H:55
Eigen::VectorXi ID
Bus numbers.
Definition Data.H:56
Eigen::VectorXd Gs
Shunt conductance ($$ G_{sh} $$) [p.u.].
Definition Data.H:68
Eigen::VectorXd Bs
Shunt susceptance ($$ B_{sh} $$) [p.u.].
Definition Data.H:69