Code Reference
toop-engine-benchmark.benchmark_toop.main
#
Hydra entry point benchmarking a single grid.
Configuration groups expected: grid: provides grid_file (+ optional grid_type) ga_config: genetic algorithm parameters lf_config: lightflow / solver parameters area_settings: voltage and area constraints
The grid config should be provided via group grid=config_grid_node_breaker or similar.
Source code in toop-engine-benchmark/benchmark_toop.py
toop-engine-benchmark.assess_benchmarks.main
#
Run benchmark assessment based on configuration.
| PARAMETER | DESCRIPTION |
|---|---|
cfg
|
Hydra configuration containing root path, save path, and print options.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
Assessment report dictionary. |
Source code in toop-engine-benchmark/assess_benchmarks.py
toop_engine_topology_optimizer.benchmark.benchmark_utils
#
A module that contains methods to run the full end-to-end pipeline from grid file to optimization results.
Note that these methods are simply wrappers to use existing functionality in a sequence.
PipelineConfig
dataclass
#
PipelineConfig(
root_path=Path("./"),
iteration_name="",
file_name="grid.xiidm",
static_info_relpath=PREPROCESSING_PATHS[
"static_information_file_path"
],
initial_topology_subpath=PREPROCESSING_PATHS[
"initial_topology_path"
],
omp_num_threads=1,
num_cuda_devices=1,
grid_type="powsybl",
)
Configuration class for the pipeline, specifying file paths and runtime parameters.
root_path
class-attribute
instance-attribute
#
Root directory for data storage.
iteration_name
class-attribute
instance-attribute
#
Name of the current iteration or experiment.
file_name
class-attribute
instance-attribute
#
Name of the primary data file, a ZIP archive for CGMES or an xiidm file.
static_info_relpath
class-attribute
instance-attribute
#
static_info_relpath = PREPROCESSING_PATHS[
"static_information_file_path"
]
Relative path to the static information file, as defined in PREPROCESSING_PATHS.
initial_topology_subpath
class-attribute
instance-attribute
#
initial_topology_subpath = PREPROCESSING_PATHS[
"initial_topology_path"
]
Relative path to the initial topology file, as defined in PREPROCESSING_PATHS.
omp_num_threads
class-attribute
instance-attribute
#
Number of OpenMP threads to use for parallel processing.
num_cuda_devices
class-attribute
instance-attribute
#
Number of CUDA devices (GPUs) to utilize.
grid_type
class-attribute
instance-attribute
#
Type of grid model being used (power system analysis libraries).
suppress_jax_logs
#
Disables jax debug logs spamming the console
Source code in packages/topology_optimizer_pkg/src/toop_engine_topology_optimizer/benchmark/benchmark_utils.py
set_environment_variables
#
Set environment variables for OpenMP, CUDA, and XLA based on the provided configuration.
| PARAMETER | DESCRIPTION |
|---|---|
cfg
|
Configuration object containing the following keys: - omp_num_threads (int): Number of OpenMP threads. - num_cuda_devices (int): Number of CUDA devices. - xla_force_host_platform_device_count (Optional[int]): Number of XLA host platform devices.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
|
Source code in packages/topology_optimizer_pkg/src/toop_engine_topology_optimizer/benchmark/benchmark_utils.py
run_task_process
#
Execute a task process based on the provided configuration.
This function initializes parameters for a genetic algorithm and a load flow solver from the given configuration, constructs command-line arguments, and runs the main optimization function. If a connection object is provided, the result is sent through the connection; otherwise, the result is returned directly. Args: cfg (DictConfig): Configuration object containing parameters for the task. conn (Connection, optional): Connection object for inter-process communication. Defaults to None.
| RETURNS | DESCRIPTION |
|---|---|
Optional[dict]
|
The result of the optimization process if no connection is provided; otherwise, None. |
Source code in packages/topology_optimizer_pkg/src/toop_engine_topology_optimizer/benchmark/benchmark_utils.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
get_paths
#
Generate and return key file and directory paths for a pipeline iteration.
This function constructs paths for the iteration directory, a specific file within that directory, a data folder based on the file name, and an optimizer snapshot directory within the data folder. It ensures that the optimizer snapshot directory exists, and raises an error if the specified file does not exist.
| PARAMETER | DESCRIPTION |
|---|---|
cfg
|
Configuration object containing root_path, iteration_name, and file_name attributes.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tuple[Path, Path, Path, Path]
|
A tuple containing: - iteration_path (Path): Path to the iteration directory. - file_path (Path): Path to the specified file within the iteration directory. - data_folder (Path): Path to the data folder derived from the file name. - optimizer_snapshot_dir (Path): Path to the optimizer snapshot directory within the data folder. |
| RAISES | DESCRIPTION |
|---|---|
FileNotFoundError
|
If the specified file does not exist at the constructed file_path. |
Source code in packages/topology_optimizer_pkg/src/toop_engine_topology_optimizer/benchmark/benchmark_utils.py
prepare_importer_parameters
#
Build importer parameter objects depending on file suffix.
| PARAMETER | DESCRIPTION |
|---|---|
file_path
|
The path to the grid model file. The file suffix determines the type of importer parameters to create.
TYPE:
|
data_folder
|
The path to the folder containing additional data required for the import process.
TYPE:
|
area_settings
|
The area settings to use for the importer. If not provided, a default AreaSettings object is created.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
UcteImporterParameters | CgmesImporterParameters
|
An instance of either |
Notes
If file_path has a .uct suffix, a UcteImporterParameters object is returned.
Otherwise, a CgmesImporterParameters object is returned.
Source code in packages/topology_optimizer_pkg/src/toop_engine_topology_optimizer/benchmark/benchmark_utils.py
run_preprocessing
#
run_preprocessing(
importer_parameters,
data_folder,
preprocessing_parameters,
is_pandapower_net=False,
)
Run importer preprocessing and extract static information.
| PARAMETER | DESCRIPTION |
|---|---|
importer_parameters
|
Parameters required by the importer for file conversion. |
data_folder
|
Path to the folder where data files are stored and processed.
TYPE:
|
preprocessing_parameters
|
Parameters for preprocessing and loading the grid.
TYPE:
|
is_pandapower_net
|
Whether to use pandapower for loading the grid. Default is False.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
info
|
Statistics and metadata about the static information extracted from the grid.
TYPE:
|
static_information
|
The extracted static information from the grid.
TYPE:
|
Source code in packages/topology_optimizer_pkg/src/toop_engine_topology_optimizer/benchmark/benchmark_utils.py
run_dc_optimization
#
Run the DC optimization process with the specified configuration.
This function sets up the necessary environment variables for optimization, including the number of OpenMP threads, CUDA devices, and optionally the XLA host platform device count. It then calls the optimization routine with the provided configuration.
| PARAMETER | DESCRIPTION |
|---|---|
dc_optim_config
|
Configuration object containing optimization parameters. Expected keys: - "omp_num_threads": int, number of OpenMP threads to use. - "num_cuda_devices": int, number of CUDA devices to make visible. - "xla_force_host_platform_device_count": int or None, optional XLA device count.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
The result of the optimization process. |
Source code in packages/topology_optimizer_pkg/src/toop_engine_topology_optimizer/benchmark/benchmark_utils.py
load_action_set
#
Load an ActionSet object from JSON metadata and HDF5 station diffs.
| PARAMETER | DESCRIPTION |
|---|---|
action_set_path
|
The file path to the JSON file containing action set metadata.
TYPE:
|
action_set_diff_path
|
The file path to the HDF5 file containing action set station diffs.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ActionSet
|
An instance of ActionSet initialized with the data from the JSON file. |
| RAISES | DESCRIPTION |
|---|---|
FileNotFoundError
|
If the specified file does not exist. |
FileNotFoundError
|
If one of the specified files does not exist. |
Source code in packages/topology_optimizer_pkg/src/toop_engine_topology_optimizer/benchmark/benchmark_utils.py
apply_topology_and_save
#
apply_topology_and_save(
grid_path,
actions,
disconnections,
action_set,
save_path,
is_pandapower_grid=False,
)
Apply a set of topology actions and disconnections to the grid, then saves the modified network to a specified path.
| PARAMETER | DESCRIPTION |
|---|---|
grid_path
|
Path to the input grid file to be loaded.
TYPE:
|
actions
|
Iterable of actions to be applied to the network topology.
TYPE:
|
disconnections
|
Iterable of disconnections to be applied to the network.
TYPE:
|
action_set
|
Set of possible actions that can be applied to the network.
TYPE:
|
save_path
|
Path where the modified network will be saved.
TYPE:
|
is_pandapower_grid
|
Whether the grid is a pandapower grid. Default is False (powsybl grid).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Network
|
The modified network after applying the topology changes and disconnections. |
Source code in packages/topology_optimizer_pkg/src/toop_engine_topology_optimizer/benchmark/benchmark_utils.py
calculate_and_save_loadflow_results
#
Calculate AC and DC loadflow results using the provided runner and saves the results as CSV files.
| PARAMETER | DESCRIPTION |
|---|---|
runner
|
An instance capable of running AC and DC loadflow calculations.
TYPE:
|
topology_path
|
The directory path where the result CSV files will be saved.
TYPE:
|
actions
|
A list of action indices to apply during the loadflow calculation.
TYPE:
|
disconnections
|
A list of disconnection indices to apply during the loadflow calculation.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[LoadflowResultsPolars, Optional[AdditionalActionInfo]]
|
The computed AC loadflow results and the action metadata captured for the AC run.
This function also saves the results to CSV files:
- "ac_loadflow_results.csv"
- "dc_loadflow_results.csv"
in the specified |
Source code in packages/topology_optimizer_pkg/src/toop_engine_topology_optimizer/benchmark/benchmark_utils.py
save_ac_metrics_summary
#
save_ac_metrics_summary(
runner,
topology_path,
loadflow_results,
actions,
disconnections,
additional_info,
dc_info,
output_file_name="ac_metrics.json",
)
Save the AC metrics for a topology as JSON.
| PARAMETER | DESCRIPTION |
|---|---|
runner
|
The loadflow runner used to compute the loadflow results.
TYPE:
|
topology_path
|
The output directory for the topology artifacts.
TYPE:
|
loadflow_results
|
The AC loadflow results for the topology.
TYPE:
|
actions
|
The topology actions applied to the grid.
TYPE:
|
disconnections
|
The branch disconnections applied to the grid.
TYPE:
|
additional_info
|
Additional action metadata captured during the AC loadflow.
TYPE:
|
dc_info
|
Any additional data from the DC run that is added for awareness
TYPE:
|
output_file_name
|
The name of the JSON file to write, by default "ac_metrics.json".
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Metrics
|
The computed AC metrics for the topology. |
Source code in packages/topology_optimizer_pkg/src/toop_engine_topology_optimizer/benchmark/benchmark_utils.py
create_loadflow_runner
#
Create and configure a PowsyblRunner instance for loadflow analysis.
This function initializes a PowsyblRunner with a single process, loads the n-1 definition and action set
from the specified data_folder, and loads the base grid from the provided grid_file_path.
If the required files are not found, it raises a FileNotFoundError.
| PARAMETER | DESCRIPTION |
|---|---|
data_folder
|
The directory containing the n-1 definition and action set files.
TYPE:
|
grid_file_path
|
The path to the base grid file to be loaded.
TYPE:
|
n_processes
|
The number of processes to use for the loadflow runner. Default is 1.
TYPE:
|
pandaflow_runner
|
Whether to use a PandapowerRunner instead of PowsyblRunner. Default is False.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
AbstractLoadflowRunner
|
An instance of |
| RAISES | DESCRIPTION |
|---|---|
FileNotFoundError
|
If the n-1 definition file or action set file is not found in the specified |
Source code in packages/topology_optimizer_pkg/src/toop_engine_topology_optimizer/benchmark/benchmark_utils.py
save_orao_summary
#
Save the switching actions for a topology in ORAO-compatible JSON format.
| PARAMETER | DESCRIPTION |
|---|---|
action_set
|
The action set that contains the switch update definitions.
TYPE:
|
output_dir
|
The directory where the ORAO summary JSON file should be written.
TYPE:
|
actions
|
The topology actions applied to the grid. Defaults to an empty list.
TYPE:
|
disconnections
|
The branch disconnections applied to the grid. Defaults to an empty list.
TYPE:
|
Source code in packages/topology_optimizer_pkg/src/toop_engine_topology_optimizer/benchmark/benchmark_utils.py
save_slds_of_split_stations
#
Save single line diagrams (SLDs) as SVG files for a set of split stations.
For each station specified by the given actions, this function generates a single line diagram (SLD) using the provided network and saves it as an SVG file in the specified output directory.
Note that this method is applicable only for Powsybl networks.
| PARAMETER | DESCRIPTION |
|---|---|
action_set
|
The set of available actions, containing local actions and their associated grid model IDs.
TYPE:
|
actions
|
List of action indices corresponding to the stations for which SLDs should be generated and saved.
TYPE:
|
output_dir
|
The directory where the SLD SVG files will be saved. Files are saved under the 'sld' subdirectory.
TYPE:
|
network
|
The network object used to generate the single line diagrams.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
This function does not return anything. It saves SVG files to disk as a side effect. |
Notes
- Each SLD is saved with the filename format '{station_id}_sld.svg' under the 'sld' subdirectory of
output_dir. - Assumes that
get_single_line_diagram_customreturns an object with a_contentattribute containing SVG data. - Requires that the logger is properly configured.
Source code in packages/topology_optimizer_pkg/src/toop_engine_topology_optimizer/benchmark/benchmark_utils.py
perform_ac_analysis
#
perform_ac_analysis(
data_folder,
optimisation_run_path,
ac_validation_cfg,
pandapower_runner=False,
)
Perform AC loadflow and n-1 analysis on the best k topologies from the optimization results.
| PARAMETER | DESCRIPTION |
|---|---|
data_folder
|
Path to the folder containing the input grid data (e.g., 'grid.xiidm').
TYPE:
|
optimisation_run_path
|
Path to the optimizer snapshot directory containing 'res.json' and where results will be saved.
TYPE:
|
ac_validation_cfg
|
Configuration dictionary for AC validation, including: - 'n_processes': int, number of processes to use for loadflow analysis. - 'k_best_topos': int, number of best topologies to validate.
TYPE:
|
pandapower_runner
|
Whether to use a PandapowerRunner for loadflow analysis. Default is False (PowsyblRunner).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Path]
|
List of paths to the directories where the results for the specified topologies are saved. If no paths are generated (e.g., no topologies found), returns an empty list. |
| RAISES | DESCRIPTION |
|---|---|
FileNotFoundError
|
If the result JSON file ('res.json') is not found in the specified optimisation_run_path. |
Notes
This function applies the selected topology, runs AC loadflow analysis, and saves the results, including SLDs (Single Line Diagrams) of split stations, in the specified output directory.
Source code in packages/topology_optimizer_pkg/src/toop_engine_topology_optimizer/benchmark/benchmark_utils.py
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 | |
get_run_dir
#
Get the next available run directory inside the optimizer snapshot directory.
This function checks for existing run directories (named 'run_0', 'run_1', etc.) inside the specified optimizer snapshot directory and creates a new directory with the next available index.
| PARAMETER | DESCRIPTION |
|---|---|
optimizer_snapshot_dir
|
The path to the optimizer snapshot directory where run directories are stored.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Path
|
The path to the newly created run directory (e.g., 'run_0', 'run_1', etc.). |
Source code in packages/topology_optimizer_pkg/src/toop_engine_topology_optimizer/benchmark/benchmark_utils.py
run_dc_optimization_stage
#
Run the DC optimization stage and save the results.
| PARAMETER | DESCRIPTION |
|---|---|
dc_optim_config
|
Configuration dictionary for the DC optimization stage.
TYPE:
|
run_dir
|
The directory where the results of this optimization run will be saved. This should be an empty folder, otherwise data will be overwritten.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Path
|
Path to the directory of this run's results. |
Source code in packages/topology_optimizer_pkg/src/toop_engine_topology_optimizer/benchmark/benchmark_utils.py
run_pipeline
#
run_pipeline(
pipeline_cfg,
importer_parameters,
preprocessing_parameters,
dc_optim_config,
ac_validation_cfg,
run_preprocessing_stage=True,
run_optimization_stage=True,
run_ac_validation_stage=True,
optimisation_run_dir=None,
)
Run the end-to-end pipeline including topology copying, preprocessing, DC optimization, and AC validation.
| PARAMETER | DESCRIPTION |
|---|---|
pipeline_cfg
|
Configuration object for the pipeline, containing paths and settings.
TYPE:
|
importer_parameters
|
Parameters for the grid model importer, specifying the source grid model file and related options. |
preprocessing_parameters
|
Parameters for the preprocessing stage, controlling data preparation and transformation.
TYPE:
|
dc_optim_config
|
Configuration dictionary for the DC optimization stage.
TYPE:
|
ac_validation_cfg
|
Configuration dictionary for the AC validation stage, including: - 'n_processes': int, number of processes to use for loadflow analysis. - 'k_best_topos': int, number of best topologies to validate.
TYPE:
|
run_preprocessing_stage
|
Whether to run the preprocessing stage. Default is True.
TYPE:
|
run_optimization_stage
|
Whether to run the DC optimization stage. Default is True.
TYPE:
|
run_ac_validation_stage
|
Whether to run the AC validation stage. Default is True.
TYPE:
|
optimisation_run_dir
|
If provided, this directory will be used for AC validation instead of running optimization.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Path
|
Path to the directory where the results of the AC validation stage are saved. |
Notes
- The function logs progress at each major step.
- The optimizer results are saved as a JSON file in a new run directory under the optimizer snapshot directory.
- AC validation is performed if optimization results are available.
Source code in packages/topology_optimizer_pkg/src/toop_engine_topology_optimizer/benchmark/benchmark_utils.py
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 | |