Harmony
HARMONic stabilitY assessment of PE-penetrated power systems
Loading...
Searching...
No Matches
Powerflow.h
Go to the documentation of this file.
1#ifndef POWERFLOW_H
2#define POWERFLOW_H
3
13#include "../../Constants.h"
14#include "../../Bus.h"
15
16#include <string>
17#include <unordered_map>
18#include <map>
19#include <vector>
20#include <utility>
21#include <stdexcept>
22#include <cmath>
23#include <algorithm>
24#include <cctype>
25
26class Element;
27class Network;
28class Bus;
29
34 std::string busName;
36 double vn;
37 double pn;
38 double ps, qs, vs, thetas;
39 double pc, qc, vc, thetac;
40};
41
49class PowerFlow {
50public:
51
54
60 std::unordered_map<std::string, Eigen::MatrixXd> create_ac(const std::string& case_name);
61
67 std::unordered_map<std::string, Eigen::MatrixXd> create_dc(const std::string& case_name);
68
76 void addBusAC(std::vector<std::vector<std::string>>& dict_ac,
77 Bus* bus, std::map<std::string, double>& global_params, bool print_info = false);
78
86 void addBusDC(std::vector<std::vector<std::string>>& dict_dc,
87 Bus* bus, std::map<std::string, double>& global_params, bool print_info = false);
88
95 void make_BranchAC(Element* element, std::map<std::string, double>& global_params,
96 bool print_info = false);
97
104 void make_BranchDC(Element* element, std::map<std::string, double>& global_params,
105 bool print_info = false);
106
113 void make_Generator(Element* element, std::map<std::string, double>& global_params,
114 bool print_info = false);
115
122 void make_RES(Element* element, std::map<std::string, double>& global_params,
123 bool print_info = false);
124
131 void make_Load(Element* element, std::map<std::string, double>& global_params,
132 bool print_info = false);
133
140 void make_Converter(Element* element, std::map<std::string, double>& global_params,
141 bool print_info = false);
142
152 void make_OPF(Network* net, std::map<std::string, double>& global_params, bool vscControl = true, bool writeTxt = false,
153 bool plotResult = false, bool print_info = false, bool include_dc = true);
154
161 void make_AC_OPF(Network* net, std::map<std::string, double>& global_params, bool writeTxt = false,
162 bool plotResult = false, bool print_info = false);
163
169 void load_params_ac(const std::string& acgrid_name, const std::unordered_map<std::string, Eigen::MatrixXd>& dataOPF);
170
176 void load_params_dc(const std::string& dcgrid_name, const std::unordered_map<std::string, Eigen::MatrixXd>& dataOPF);
177
188 void solve_opf(const std::string& dc_name,
189 const std::string& ac_name, std::unordered_map<std::string, Eigen::MatrixXd>* dataOPF,
190 bool vscControl, bool writeTxt, bool plotResult, bool print_info);
191
196 const auto& getNetData() const { return data; }
197
202 auto& getNetData() { return data; }
203
210 DCBusResult getDCBusResult(const std::string& dcBusName,
211 const std::map<std::string, double>& global_params) const;
212
214 bool opfSolved() const { return opf_solved_; }
215
216private:
217 std::unordered_map<std::string, Eigen::MatrixXd> network_ac;
218 double baseMVA_ac;
219 Eigen::MatrixXd bus_entire_ac, branch_entire_ac, gen_entire_ac, gencost_entire_ac, res_entire_ac;
220 int ngrids;
221 std::vector<Eigen::MatrixXd> bus_ac, branch_ac, generator_ac, gencost_ac, res_ac;
222 std::vector<std::vector<int>> recRef;
223 std::vector<Eigen::VectorXd> pd_ac, qd_ac, sres_ac;
224 std::vector<int> nbuses_ac, nbranches_ac, ngens_ac, nress_ac;
225 std::vector<Eigen::SparseMatrix<double>> GG_ac, BB_ac, GG_ft_ac, BB_ft_ac, GG_tf_ac, BB_tf_ac;
226 std::vector<Eigen::VectorXi> fbus_ac, tbus_ac;
227 std::vector<Eigen::MatrixXd> anglelim_rad;
228 std::vector<Eigen::VectorXi> IDtoCountmap;
229 std::vector<int> refbuscount_ac;
230
231 std::unordered_map<std::string, Eigen::MatrixXd> network_dc;
232 double baseMW_dc, pol_dc;
233 int nbuses_dc, nbranches_dc, nconvs_dc;
234 Eigen::MatrixXd bus_dc, branch_dc, conv_dc;
235 Eigen::SparseMatrix<std::complex<double>> Y_dc;
236 Eigen::SparseMatrix<double> y_dc;
237 Eigen::VectorXcd ztfc_dc;
238 Eigen::VectorXd rtf_dc, xtf_dc, bf_dc, rc_dc, xc_dc;
239 Eigen::VectorXd gtfc_dc, btfc_dc, aloss_dc, bloss_dc, closs_dc;
240 Eigen::VectorXd basekV_dc;
241 Eigen::VectorXi convState_dc;
242 Eigen::VectorXi fbus_dc, tbus_dc;
243
244 Eigen::VectorXd pn_dc, Ic_dc, lc_dc;
245 Eigen::VectorXd Ctt_dc, Ccc_dc, Ctc_dc, Stc_dc, Cct_dc, Sct_dc, convPloss_dc;
246
247 std::vector<int> nbuses_ac_viz, ngens_ac_viz;
248 int nconvs_dc_viz, nbuses_dc_viz, ngrids_viz;
249
250 std::map<std::string, std::map<std::string, std::map<std::string, double>>> data;
251
252 std::unordered_map<std::string, int> busName2Id_;
253
254 Eigen::MatrixXd readCSVtoCpp(const std::string& filename);
255 Eigen::SparseMatrix<std::complex<double>> makeYbus(double baseMVA, const Eigen::MatrixXd& bus, const Eigen::MatrixXd& branch);
256
257 Eigen::VectorXd vn2_dc_k;
258 Eigen::VectorXd pn_dc_k;
259 Eigen::MatrixXd pij_dc_k;
260 Eigen::MatrixXd lij_dc_k;
261 Eigen::VectorXd ps_dc_k, qs_dc_k;
262 Eigen::VectorXd pc_dc_k, qc_dc_k;
263 Eigen::VectorXd theta_s_k, theta_c_k;
264 Eigen::VectorXd v2s_dc_k, v2c_dc_k;
265 Eigen::VectorXd convPloss_dc_k;
266
267 std::vector<Eigen::VectorXd> vn2_ac_k;
268 std::vector<Eigen::VectorXd> theta_ac_k;
269 std::vector<Eigen::VectorXd> pn_ac_k;
270 std::vector<Eigen::VectorXd> qn_ac_k;
271 std::vector<Eigen::VectorXd> pgen_ac_k;
272 std::vector<Eigen::VectorXd> qgen_ac_k;
273 std::vector<Eigen::MatrixXd> pij_ac_k;
274 std::vector<Eigen::MatrixXd> qij_ac_k;
275 std::vector<Eigen::MatrixXd> ss_ac_k;
276 std::vector<Eigen::MatrixXd> cc_ac_k;
277
278 std::vector<Element*> conv_point;
279
280 double opf_user_base_mva_ = 100.0;
281 bool opf_solved_ = false;
282
283};
284
285#endif // POWERFLOW_H
A network node connecting one or more element terminals.
Definition Bus.h:21
Base class for multi-phase network elements with Y-parameters and MNA stamping hooks.
Definition Element.h:29
Root network model holding buses, elements, and AC/DC grid hierarchy.
Definition network.h:36
Combined AC/DC optimal power flow solver.
Definition Powerflow.h:49
std::unordered_map< std::string, Eigen::MatrixXd > create_ac(const std::string &case_name)
Loads AC grid case data from bundled CSV files.
Definition Powerflow_data.cpp:89
bool opfSolved() const
True when the most recent solve_opf completed with a usable solution.
Definition Powerflow.h:214
void make_BranchAC(Element *element, std::map< std::string, double > &global_params, bool print_info=false)
Adds an AC branch (line/transformer) entry to the OPF data.
Definition Powerflow_network_params.cpp:310
void make_OPF(Network *net, std::map< std::string, double > &global_params, bool vscControl=true, bool writeTxt=false, bool plotResult=false, bool print_info=false, bool include_dc=true)
Builds OPF data structures from a Harmony network and runs the solver.
Definition Powerflow_network_params.cpp:924
void addBusDC(std::vector< std::vector< std::string > > &dict_dc, Bus *bus, std::map< std::string, double > &global_params, bool print_info=false)
Appends a DC bus row to the OPF bus dictionary.
Definition Powerflow_network_params.cpp:219
void make_BranchDC(Element *element, std::map< std::string, double > &global_params, bool print_info=false)
Adds a DC branch entry to the OPF data.
Definition Powerflow_network_params.cpp:393
void load_params_ac(const std::string &acgrid_name, const std::unordered_map< std::string, Eigen::MatrixXd > &dataOPF)
Applies solved AC OPF parameters back to a named AC grid.
Definition Powerflow_network_params.cpp:1590
void make_Generator(Element *element, std::map< std::string, double > &global_params, bool print_info=false)
Adds a synchronous generator entry to the OPF data.
Definition Powerflow_network_params.cpp:588
auto & getNetData()
Returns the nested OPF result dictionary (mutable).
Definition Powerflow.h:202
void make_Converter(Element *element, std::map< std::string, double > &global_params, bool print_info=false)
Adds a VSC/MMC converter entry to the OPF data.
Definition Powerflow_network_params.cpp:476
void solve_opf(const std::string &dc_name, const std::string &ac_name, std::unordered_map< std::string, Eigen::MatrixXd > *dataOPF, bool vscControl, bool writeTxt, bool plotResult, bool print_info)
Runs the combined AC/DC OPF optimization.
Definition Powerflow_solver.cpp:70
void make_Load(Element *element, std::map< std::string, double > &global_params, bool print_info=false)
Adds a load entry to the OPF data.
Definition Powerflow_network_params.cpp:871
void addBusAC(std::vector< std::vector< std::string > > &dict_ac, Bus *bus, std::map< std::string, double > &global_params, bool print_info=false)
Appends an AC bus row to the OPF bus dictionary.
Definition Powerflow_network_params.cpp:132
std::unordered_map< std::string, Eigen::MatrixXd > create_dc(const std::string &case_name)
Loads DC grid case data from bundled CSV files.
Definition Powerflow_data.cpp:115
void make_AC_OPF(Network *net, std::map< std::string, double > &global_params, bool writeTxt=false, bool plotResult=false, bool print_info=false)
Builds AC-only OPF data from a Harmony network and runs the solver.
Definition Powerflow_network_params.cpp:918
const auto & getNetData() const
Returns the nested OPF result dictionary (read-only).
Definition Powerflow.h:196
DCBusResult getDCBusResult(const std::string &dcBusName, const std::map< std::string, double > &global_params) const
Retrieves solved DC bus quantities by name.
Definition Powerflow_solver.cpp:1172
PowerFlow()
Default constructor.
Definition Powerflow.h:53
void make_RES(Element *element, std::map< std::string, double > &global_params, bool print_info=false)
Adds a renewable energy source (RES) entry to the OPF data.
Definition Powerflow_network_params.cpp:761
void load_params_dc(const std::string &dcgrid_name, const std::unordered_map< std::string, Eigen::MatrixXd > &dataOPF)
Applies solved DC OPF parameters back to a named DC grid.
Definition Powerflow_network_params.cpp:1772
Per-bus DC-side OPF solution quantities.
Definition Powerflow.h:33
double pc
Definition Powerflow.h:39
int busIndex
Definition Powerflow.h:35
double thetas
Definition Powerflow.h:38
double qs
Definition Powerflow.h:38
double ps
Definition Powerflow.h:38
std::string busName
Definition Powerflow.h:34
double vc
Definition Powerflow.h:39
double thetac
Definition Powerflow.h:39
double vn
Definition Powerflow.h:36
double pn
Definition Powerflow.h:37
double qc
Definition Powerflow.h:39
double vs
Definition Powerflow.h:38