|
| 1 | +import urllib.request |
| 2 | +import yaml |
| 3 | +import subprocess |
| 4 | + |
| 5 | + |
| 6 | +def execute_and_live_output(cmd) -> None: |
| 7 | + subprocess.run(cmd, text=True, shell=True, check=True) |
| 8 | + |
| 9 | + |
| 10 | +def _base_config() -> dict: |
| 11 | + """ |
| 12 | + Download the default config from gridfm-datakit and apply common test parameters. |
| 13 | + """ |
| 14 | + config_url = ( |
| 15 | + "https://raw.githubusercontent.com/gridfm/gridfm-datakit/refs/heads/main" |
| 16 | + "/scripts/config/default.yaml" |
| 17 | + ) |
| 18 | + |
| 19 | + print(f"Downloading config from {config_url}...") |
| 20 | + with urllib.request.urlopen(config_url) as response: |
| 21 | + config_content = response.read().decode("utf-8") |
| 22 | + |
| 23 | + config = yaml.safe_load(config_content) |
| 24 | + |
| 25 | + config["network"]["name"] = "case14_ieee" |
| 26 | + config["load"]["scenarios"] = 10000 |
| 27 | + config["topology_perturbation"]["n_topology_variants"] = 2 |
| 28 | + |
| 29 | + return config |
| 30 | + |
| 31 | + |
| 32 | +def generate_pf_test_data(config_path: str = "integrationtests/default_pf.yaml") -> None: |
| 33 | + """ |
| 34 | + Generate power-flow (PF) test data for case14_ieee with 10 000 scenarios |
| 35 | + and 2 topology variants. |
| 36 | + """ |
| 37 | + config = _base_config() |
| 38 | + |
| 39 | + with open(config_path, "w") as f: |
| 40 | + yaml.dump(config, f, default_flow_style=False, sort_keys=False) |
| 41 | + |
| 42 | + print(f"PF config written to {config_path}") |
| 43 | + print(f" network.name : {config['network']['name']}") |
| 44 | + print(f" load.scenarios : {config['load']['scenarios']}") |
| 45 | + print(f" topology_perturbation.n_topology_variants: {config['topology_perturbation']['n_topology_variants']}") |
| 46 | + |
| 47 | + execute_and_live_output(f"gridfm_datakit generate {config_path}") |
| 48 | + |
| 49 | + |
| 50 | +def generate_opf_test_data(config_path: str = "integrationtests/default_opf.yaml") -> None: |
| 51 | + """ |
| 52 | + Generate optimal power-flow (OPF) test data for case14_ieee with 10 000 scenarios |
| 53 | + and 2 topology variants. |
| 54 | + """ |
| 55 | + config = _base_config() |
| 56 | + config["settings"]["mode"] = "opf" |
| 57 | + |
| 58 | + with open(config_path, "w") as f: |
| 59 | + yaml.dump(config, f, default_flow_style=False, sort_keys=False) |
| 60 | + |
| 61 | + print(f"OPF config written to {config_path}") |
| 62 | + print(f" network.name : {config['network']['name']}") |
| 63 | + print(f" load.scenarios : {config['load']['scenarios']}") |
| 64 | + print(f" topology_perturbation.n_topology_variants: {config['topology_perturbation']['n_topology_variants']}") |
| 65 | + print(f" settings.mode : {config['settings']['mode']}") |
| 66 | + |
| 67 | + execute_and_live_output(f"gridfm_datakit generate {config_path}") |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + #generate_pf_test_data() |
| 72 | + generate_opf_test_data() |
0 commit comments