Harmony
HARMONic stabilitY assessment of PE-penetrated power systems
Loading...
Searching...
No Matches
PowerFlow Class Reference

Combined AC/DC optimal power flow solver. More...

#include <Powerflow.h>

Public Member Functions

 PowerFlow ()
 Default constructor.
 
std::unordered_map< std::string, Eigen::MatrixXd > create_ac (const std::string &case_name)
 Loads AC grid case data from bundled CSV files.
 
std::unordered_map< std::string, Eigen::MatrixXd > create_dc (const std::string &case_name)
 Loads DC grid case data from bundled CSV files.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
void make_Load (Element *element, std::map< std::string, double > &global_params, bool print_info=false)
 Adds a load entry to the OPF data.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
const auto & getNetData () const
 Returns the nested OPF result dictionary (read-only).
 
auto & getNetData ()
 Returns the nested OPF result dictionary (mutable).
 
DCBusResult getDCBusResult (const std::string &dcBusName, const std::map< std::string, double > &global_params) const
 Retrieves solved DC bus quantities by name.
 
bool opfSolved () const
 True when the most recent solve_opf completed with a usable solution.
 

Detailed Description

Combined AC/DC optimal power flow solver.

Manages case-data loading, network parameter extraction from Harmony elements, OPF optimization, result storage, and optional visualization.

Constructor & Destructor Documentation

◆ PowerFlow()

PowerFlow::PowerFlow ( )
inline

Default constructor.

Member Function Documentation

◆ addBusAC()

void PowerFlow::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.

Add an AC bus entry to the system data table.

Parameters
dict_acAC bus table being assembled.
busHarmony bus object.
global_paramsShared network parameters (base MVA, etc.).
print_infoIf true, prints diagnostic information.

Creates a new AC bus in data["busAC"], initializes default parameters, and updates the bus dictionary dict_ac and mapping busName2Id_.

Parameters
dict_acAC bus dictionary.
busPointer to the Bus object.
global_paramsGlobal parameters.
print_infoIf true, print formatted bus data.

References Bus::computePowerFlowAC(), and Bus::getBusName().

Referenced by make_OPF().

◆ addBusDC()

void PowerFlow::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.

Add a DC bus entry to the system data table.

Parameters
dict_dcDC bus table being assembled.
busHarmony bus object.
global_paramsShared network parameters.
print_infoIf true, prints diagnostic information.

Creates and initializes a new DC bus in data["busDC"] with default parameters, The function also updates the DC bus dictionary dict_dc and mapping busName2Id_.

Parameters
dict_dcDC bus dictionary.
busPointer to the Bus object.
global_paramsGlobal parameters.
print_infoIf true, print formatted DC bus data.

References Bus::computePowerFlowDC(), and Bus::getBusName().

Referenced by make_OPF().

◆ create_ac()

std::unordered_map< std::string, Eigen::MatrixXd > PowerFlow::create_ac ( const std::string &  case_name)

Loads AC grid case data from bundled CSV files.

Parameters
case_nameCase identifier (e.g. "case57").
Returns
Map of matrix name to data (bus, branch, gen, gencost, res).

Referenced by load_params_ac().

◆ create_dc()

std::unordered_map< std::string, Eigen::MatrixXd > PowerFlow::create_dc ( const std::string &  case_name)

Loads DC grid case data from bundled CSV files.

Parameters
case_nameCase identifier.
Returns
Map of matrix name to data (bus, branch, conv).

Referenced by load_params_dc().

◆ getDCBusResult()

DCBusResult PowerFlow::getDCBusResult ( const std::string &  dcBusName,
const std::map< std::string, double > &  global_params 
) const

Retrieves solved DC bus quantities by name.

Parameters
dcBusNameName of the DC bus.
global_paramsShared network parameters (base values, polarity).
Returns
Populated DCBusResult structure.

References DCBusResult::busIndex, DCBusResult::busName, DCBusResult::pc, DCBusResult::pn, DCBusResult::ps, DCBusResult::qc, DCBusResult::qs, DCBusResult::thetac, DCBusResult::thetas, DCBusResult::vc, DCBusResult::vn, and DCBusResult::vs.

◆ getNetData() [1/2]

auto & PowerFlow::getNetData ( )
inline

Returns the nested OPF result dictionary (mutable).

Returns
Reference to the result data map.

◆ getNetData() [2/2]

const auto & PowerFlow::getNetData ( ) const
inline

Returns the nested OPF result dictionary (read-only).

Returns
Map of grid → element → parameter → value.

◆ load_params_ac()

void PowerFlow::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.

========================================== debug end

Parameters
acgrid_nameName of the AC sub-grid.
dataOPFSolved OPF data matrices.

Load and preprocess AC network parameters for OPF formulation.

This function loads AC-side network data either from a predefined AC test case or from an externally constructed OPF data map. The loaded data include AC bus, branch, generator, generator cost, and renewable energy source (RES) information.

After loading the data, the function identifies the number of AC grids based on the grid identifier stored in the bus data. It then partitions the full AC network dataset into individual AC subsystems and initializes the corresponding member variables used by the OPF model.

For each AC grid, the function extracts local buses, branches, generators, generation costs, RES units, active/reactive loads, branch terminal indices, reference bus information, and bus-index mapping. It also builds the AC bus admittance matrix and stores its real and imaginary parts for later use in the OPF constraints.

Parameters
acgrid_nameName of the predefined AC grid case to be loaded when dataOPF is empty.
dataOPFOptional OPF input data map. If non-empty, AC data are loaded directly from this map instead of external files.
Note
The function assumes that the last column of busAC and branchAC stores the AC grid identifier, that column 21 of genAC stores the generator grid identifier, and that column 11 of resAC stores the RES grid identifier.
AC load values and RES capacities are normalized by baseMVA_ac.

References create_ac().

Referenced by solve_opf().

◆ load_params_dc()

void PowerFlow::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.

Load and preprocess DC network parameters for OPF formulation.

Parameters
dcgrid_nameName of the DC sub-grid.
dataOPFSolved OPF data matrices.

This function loads the DC-side network data either from a predefined DC test case or from an externally constructed OPF data map. The loaded data include DC buses, DC branches, and converter information.

After loading the data, the function initializes the fundamental network dimensions and extracts key converter parameters, including transformer impedance, converter impedance, filter susceptance, and converter loss coefficients. It also constructs the DC bus admittance matrix and computes the corresponding conductance matrix used in the OPF formulation.

Furthermore, the function determines the operating direction of each converter (rectifier or inverter) according to its active power injection and selects the corresponding converter loss coefficients. Finally, all converter loss parameters are normalized into the per-unit system.

Parameters
dcgrid_nameName of the predefined DC grid case to be loaded when dataOPF is empty.
dataOPFOptional OPF input data map. If dcgrid_name is empty, the DC data are loaded directly from this map.
Note
The converter operating state (convState_dc) is determined from the sign of the active power injection (P_g), where a non-negative value denotes rectifier operation and a negative value denotes inverter operation.
The converter loss coefficients (aloss_dc, bloss_dc, and closs_dc) are converted into the per-unit system using the system base power and converter base voltage.

debug

References absoluteSparseMatrix(), and create_dc().

Referenced by solve_opf().

◆ make_AC_OPF()

void PowerFlow::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.

Same as make_OPF with include_dc=false; DC buses, branches, and converters are excluded from the optimization (hybrid elements in the network are ignored).

References make_OPF().

◆ make_BranchAC()

void PowerFlow::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.

Add an AC branch entry to the system data table.

Parameters
elementBranch element (Line, Transformer, etc.).
global_paramsShared network parameters.
print_infoIf true, prints diagnostic information.

Creates and initializes one new AC branch row in data["branchAC"] with default values, then fills branch electrical parameters by calling element->computePowerFlow(...). The function identifies the from-bus and to-bus IDs using busName2Id_ based on the two terminal buses returned by element->getBuses().

If print_info is true, the function prints the formatted data["branchAC"] table.

Parameters
elementPointer to the branch element object (should connect exactly two buses).
global_paramsGlobal parameters used in power flow calculations (e.g., baseMVA, baseKV).
print_infoIf true, print the formatted AC branch data table.
Exceptions
std::runtime_errorIf the element is not connected to exactly two buses.

References Element::computePowerFlow(), Element::getBuses(), and Element::getElementSymbol().

Referenced by make_OPF().

◆ make_BranchDC()

void PowerFlow::make_BranchDC ( Element element,
std::map< std::string, double > &  global_params,
bool  print_info = false 
)

Adds a DC branch entry to the OPF data.

Add a DC branch entry to the system data table.

Parameters
elementDC branch element.
global_paramsShared network parameters.
print_infoIf true, prints diagnostic information.

Creates and initializes one new DC branch row in data["branchDC"] with default values, then computes the branch electrical parameters by calling element->computePowerFlow(...).

The function assumes that the DC branch element connects exactly two DC buses. The from-bus and to-bus indices are obtained via busName2Id_ using the bus names returned by element->getBuses().

If print_info is enabled, the formatted DC branch data table is printed to stdout.

Parameters
elementPointer to the DC branch element object (must connect exactly two DC buses).
global_paramsGlobal parameters used in DC power flow calculations.
print_infoIf true, print the formatted DC branch data table.
Exceptions
std::runtime_errorIf the branch element is not connected to exactly two buses.

References Element::computePowerFlow(), Element::getBuses(), and Element::getElementSymbol().

Referenced by make_OPF().

◆ make_Converter()

void PowerFlow::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.

Add a converter entry to the system data table.

Parameters
elementConverter element.
global_paramsShared network parameters.
print_infoIf true, prints diagnostic information.

Creates and initializes one new converter row in data["conv"] with default parameters, then updates converter electrical parameters by calling element->computePowerFlow(...).

The function requires the converter element to connect exactly two buses: one AC bus and one DC bus. It identifies the AC/DC bus by Bus::getPinNumber() (pin = 3 treated as AC bus in current logic), maps bus names to indices using busName2Id_, and writes busac_i / busdc_i into the converter row.

The converter element pointer is also stored in conv_point for later use. If print_info is enabled, the function prints the formatted data["conv"] table.

Parameters
elementPointer to the converter element object (must connect exactly one AC bus and one DC bus).
global_paramsGlobal parameters used in converter power flow calculations (e.g., baseKV, baseMVA).
print_infoIf true, print the formatted converter data table.
Exceptions
std::runtime_errorIf the converter is not connected to exactly two buses.
std::runtime_errorIf the two buses cannot be identified as one AC bus and one DC bus.

References Element::computePowerFlow(), Element::getBuses(), and Bus::getBusName().

Referenced by make_OPF().

◆ make_Generator()

void PowerFlow::make_Generator ( Element element,
std::map< std::string, double > &  global_params,
bool  print_info = false 
)

Adds a synchronous generator entry to the OPF data.

Add a generator entry and its cost entry to the system data tables.

Parameters
elementGenerator element.
global_paramsShared network parameters.
print_infoIf true, prints diagnostic information.

Creates and initializes one generator row in data["genAC"] and one cost row in data["genCostAC"], then updates parameters using element->computePowerFlow(...) and OPF data from element->getOPFInfo().

The generator is assumed to be connected to one AC bus and ground. The non-ground bus is mapped to an internal bus index via busName2Id_. Bus voltage limits and bus type (PV or SLACK) may be updated according to OPF information.

Parameters
elementPointer to the generator element object.
global_paramsGlobal parameters used in power flow and OPF data construction.
print_infoIf true, print the formatted generator and cost tables.
Exceptions
std::runtime_errorIf the generator is not properly connected to a bus.
std::invalid_argumentIf generator power limits are inconsistent.

References Element::computePowerFlow(), Element::getBuses(), Bus::getBusName(), Element::getElementSymbol(), and Element::getOPFInfo().

Referenced by make_OPF().

◆ make_Load()

void PowerFlow::make_Load ( Element element,
std::map< std::string, double > &  global_params,
bool  print_info = false 
)

Adds a load entry to the OPF data.

Register an AC load by updating the corresponding busAC entry.

Parameters
elementLoad element.
global_paramsShared network parameters.
print_infoIf true, prints diagnostic information.

Identifies the non-ground bus connected to the load element and updates its load parameters in the busAC table via power flow computation.

Parameters
elementPointer to the load element.
global_paramsGlobal parameters used in power flow calculation.
print_infoIf true, print the updated busAC table.
Exceptions
std::runtime_errorIf no valid AC bus is connected or the bus entry is not found.

References Element::computePowerFlow(), Element::getBuses(), and Bus::getBusName().

Referenced by make_OPF().

◆ make_OPF()

void PowerFlow::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.

Parameters
netNetwork to optimize.
global_paramsShared network parameters.
vscControlEnable VSC droop/control constraints (default true).
writeTxtWrite text output files (default false).
plotResultOpen OPF visualization (default false).
print_infoPrint diagnostic information (default false).

References addBusAC(), addBusDC(), ensureRESGen(), extendBranchAC(), extendBusAC(), extendGenAC(), Network::getBuses(), Bus::getBusName(), Network::getElements(), Element::getElementSymbol(), Bus::getPinNumber(), make_BranchAC(), make_BranchDC(), make_Converter(), make_Generator(), make_Load(), make_RES(), map2dense(), reNumberBusAC(), and solve_opf().

Referenced by example_OPF_double_area(), example_OPF_PV(), example_OPF_single_area(), example_OPF_WT(), example_point2point_case(), example_stability_check(), and make_AC_OPF().

◆ make_RES()

void PowerFlow::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.

Add a renewable energy source entry to the system data table.

Parameters
elementRES element.
global_paramsShared network parameters.
print_infoIf true, prints diagnostic information.

This function registers a renewable energy source (RES), such as a PV plant or a type-3/type-4 wind turbine, in data["resAC"]. The RES is assumed to be connected to one AC bus and ground. The non-ground bus is identified and mapped to its internal bus index through busName2Id_.

The function first obtains the grid ID from the corresponding busAC entry, then determines the rated active power of the RES according to its specific component type. If multiple RES units are connected to the same bus, their maximum active power capacities are aggregated into a single resAC row. Otherwise, a new RES row is created with default cost and capacity parameters.

Parameters
elementPointer to the RES element object.
global_paramsGlobal parameters used in power flow and OPF data construction.
print_infoIf true, print the formatted RES data table.
Exceptions
std::runtime_errorIf the RES is not connected to any bus.
std::runtime_errorIf the connected bus cannot be found in data["busAC"].
std::runtime_errorIf the RES type is not supported.

References Element::getBuses(), Bus::getBusName(), and Element::getElementSymbol().

Referenced by make_OPF().

◆ opfSolved()

bool PowerFlow::opfSolved ( ) const
inline

True when the most recent solve_opf completed with a usable solution.

◆ solve_opf()

void PowerFlow::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 
)

The documentation for this class was generated from the following files: