Skip to content

Commit

Permalink
Merge pull request #25 from pypsa-meets-earth/scenario_management
Browse files Browse the repository at this point in the history
scenario management
  • Loading branch information
martacki authored Oct 12, 2023
2 parents 2ee5b60 + ae90de0 commit 1fc0931
Show file tree
Hide file tree
Showing 31 changed files with 287 additions and 65 deletions.
45 changes: 36 additions & 9 deletions .github/workflows/ci-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ name: CI-Linux
on:
push:
branches:
- main
- scenario_management
pull_request:
branches:
- main
- scenario_management
schedule:
- cron: "0 5 * * TUE"

Expand Down Expand Up @@ -72,20 +72,47 @@ jobs:
- name: Move test config
run: |
cd ../pypsa-earth
cp pypsa-kz-data/test/config_kz_test.yaml ./test/config.custom.yaml
cp pypsa-kz-data/test/config_kz_default_test.yaml ./test/config.landlock.yaml
- name: Create test configs
run: |
cd ../pypsa-earth
snakemake --cores all build_test_configs
- name: Test pypsa-kz-data workflow
- name: Clean scenarios folder
run: |
cd ../pypsa-earth
cp ./test/tmp/config.custom_tmp.yaml ./config.yaml
snakemake -j1 retrieve_databundle_light # we first retrieve data
rm -r pypsa-kz-data/scenarios/*
- name: Move test configs to scenario folder
run: |
cd ../pypsa-earth
cp pypsa-kz-data/test/config_kz_test1.yaml pypsa-kz-data/scenarios/config.kz_2013.yaml
cp ./test/tmp/config.landlock_tmp.yaml pypsa-kz-data/config.kz_default.yaml
- name : Change Snakefile lines
run: |
cd ../pypsa-earth
sed -i 's\os.system("snakemake -j all solve_all_networks --rerun-incomplete")\os.system(f"snakemake -j1 networks/{wildcards.scenario_name}/base.nc")\g' Snakefile
sed -i 's,os.system("snakemake -j1 make_statistics --force"),os.system("cp pypsa-kz-data/data/custom_powerplants.csv data/custom_powerplants.csv")\n\tos.system("snakemake -j1 solve_all_networks --rerun-incomplete"),g' Snakefile
cat Snakefile
- name: Replace Default config
run: |
cd ../pypsa-earth
cp pypsa-kz-data/config.kz_default.yaml config.default.yaml
cp config.default.yaml config.yaml
- name: Prepare KZ-scenarios
run: |
cd ../pypsa-earth
snakemake -j1 prepare_kz_scenarios
cp -r pypsa-kz-data/test/gadm ./data/gadm
find data -type f
cp pypsa-kz-data/data/custom_powerplants.csv ./data/custom_powerplants.csv # overwrite powerplantdata
snakemake -j1 solve_all_networks
cat configs/scenarios/config.kz_2013.yaml
- name: Run all scenarios
run: |
cd ../pypsa-earth
snakemake -j1 run_all_scenarios
65 changes: 47 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Monthly electricity demand data with monthly aggregation provided by Kazakhstan

# Model Execution

## Setting up the general repositories

The provided workflow builds on [PyPSA-Earth](https://github.com/pypsa-meets-earth/pypsa-earth). Therefore, first, the PyPSA-Earth repository must be forked and the fork should then be cloned. A fork can be created by navigating to the [PyPSA-Earth](https://github.com/pypsa-meets-earth/pypsa-earth) website. By clicking on the fork-symbol in the upper right corner, a fork is created and linked to the specific user.

Next, we also need to fork the [pypsa-kz-data](https://github.com/pypsa-meets-earth/pypsa-kz-data) repository. A fork can be created by navigating to the [pypsa-kz-data](https://github.com/pypsa-meets-earth/pypsa-kz-data) website and clicking the fork symbol in the upper right corner.
Expand All @@ -63,45 +65,72 @@ After installing the environment, activate it using
```bash
conda activate pypsa-earth
```
Before the whole workflow can be executed, the databundle must be retrieved. This can be done via:

## Modeling adaptations for KZ study

To adapt the overall workflow for kz, only two further changes are necessary.

Firstly, open the Snakefile (in `pypsa-earth/`) and navigate to line [1057-1058](https://github.com/pypsa-meets-earth/pypsa-earth/blob/main/Snakefile#L1057-L1058), which should read
```bash
snakemake -j 1 retrieve_databundle_light
os.system("snakemake -j all solve_all_networks --rerun-incomplete")
os.system("snakemake -j1 make_statistics --force")
```
This step can optionally be skipped if the `data/` folder with all relevant subfolders already exists.

To adapt the overall workflow for kz, only three further changes are necessary:
1. replace the `pypsa-earth/data/custom_powerplants.csv` with the provided `pypsa-kz-data/data/custom_powerplants.csv`. This can be done using the command:
and replace these two lines with
```bash
cp pypsa-kz-data/data/custom_powerplants.csv data/custom_powerplants.csv
os.system(f"snakemake -j1 networks/{wildcards.scenario_name}/base.nc")
os.system("cp pypsa-kz-data/data/custom_powerplants.csv data/custom_powerplants.csv")
os.system("snakemake -j1 solve_everything --rerun-incomplete")
```

2. Open the Snakefile (in `pypsa-earth/`) and navigate to line 25, which should read `configfile: "config.yaml"`. Replace this line with `configfile: "pypsa-kz-data/config_kz_default.yaml"`. To run the default scenario for different weather years (2011, 2013, 2018), add a new line below (line 26) with `configfile: "pypsa-kz-data/config_kz_<year>.yaml"`, where `<year>` represents an existing weather year (2011, 2013, 2018).

The whole workflow can be reproduced by executing
Secondly, copy the default configuration file to the pypsa-earth folder using:
```bash
cp pypsa-kz-data/config.kz_default.yaml config.default.yaml
```
In case you already have a custom config file, make sure to replace it as well, using
```bash
snakemake -j 1 solve_everything
cp pypsa-kz-data/config.kz_default.yaml config.yaml
```

3. Only for scenarios: To run a certain scenario, make sure to update the *2nd* config file, i.e. navigate to line 26 in the Snakefile, which now should read: `configfile: "pypsa-kz-data/config_kz_<year>.yaml"` and replace this with the scenario you want to execute: `configfile: "pypsa-kz-data/config_kz_<year>_discount<p>.yaml"`. `<year>` and `<p>` must be replaced with existing years (2011, 2013, 2018) and discount rates (10 for optimistic scenario, 15 for BAU and 20 for pessimistic scenario). To run the coal exit scenario, the corresponding config file also must be referred in line 26 of the `Snakemake`. For this setting, the line must read `configfile: "pypsa-kz-data/config_kz_<year>_discount<p>_coalexit.yaml"`, where `<year>` and `<p>` are the weather year (2011, 2013, 2018) and discount rates (10, 15, and 20).
You are now all set to run all scenarios!

Again, the whole workflow can be reproduced by executing the same command as above:
## Running KZ scenarios

To prepare running all scenarios, execute
```bash
snakemake -j1 prepare_kz_scenarios
```
Optionally, to save time for future runs, you can now set `enable: retrieve_databundle: True` in the `config.yaml` to `False`. If you already have build all cutouts for 2011, 2013 and 2018, you can also set `enable: build_cutout: True` to `False`.
Finally, to run all scenarios, execute
```bash
snakemake -j 1 solve_everything
snakemake -j1 run_all_scenarios
```

Results are generated and locally saved in `pypsa-earth/results/<scenario_folder>/networks/`.
After all scenarios have executed successfully, all results are generated and locally saved in `pypsa-earth/results/<scenario_folder>/networks/`.

## Potential errors:
# Potential errors
- A rule is killed. In this case, open the `Snakefile` in `pypsa-earth` or open `kz.smk` in `pypsa-kz-data` (depending on the rule which is killed), navigate to the rule that is being killed in the workflow and increase the memory assignment (for example, add a 0 at the end).

- The workflow runs into an error during the `build_powerplants` rule. In this case, try to repeat step 1. of the workflow using the command
```bash
cp pypsa-kz-data/data/custom_powerplants.csv data/custom_powerplants.csv
```

- Unusual error arising from either Snakemake or the `Snakefile` and proving to be challenging to comprehend: Inspect all indentation. Ensure there is no tab spacing; employ only spaces, i.e., ` `. It is probable that the indentations before `configfile: 'pypsa-kz-data/config_kz_<...>.yaml'` are tabs instead of four spaces.
- Unusual error arising from either Snakemake or the `Snakefile` and proving to be challenging to comprehend: Inspect all indentation. Ensure there is no tab spacing; employ only spaces, i.e., ` `. It is probable that the indentations before
```bash
os.system(f"snakemake -j1 networks/{wildcards.scenario_name}/base.nc")
os.system("cp pypsa-kz-data/data/custom_powerplants.csv data/custom_powerplants.csv")
os.system("snakemake -j1 solve_everything --rerun-incomplete")
```
are tabs instead of four spaces.

- Missing `data/` folder or some relevant subfolders. This should normally be executed automatically when executing the rule `prepare_kz_scenarios`, however might be missing due to incorrect execution. The databundle can be also retrieved manually via:
```bash
snakemake -j 1 retrieve_databundle_light
```

- The rule `retrieve_databundle_light` always executes with an error. To avoid this, try setting `enable: build_cutout: False` to `True`.

## Comes in handy:
# Comes in handy
After all cutouts were generated (i.e. the three files `asia-<year>-era5.nc` exist in the folder `pypsa-earth/cutouts/`, where `<year>` is 2011, 2013, and 2018, navigate to `pypsa-earth/pypsa-kz-data`, open the default config file, navigate to line 36, which should read `build_cutout: True`, and set it to `build_cutout: false`. This will save you a lot of time when (re-)runnig scenarios. But remember to set it back to `true` in case one of the cutouts was deleted!

## Acknowledgement
Expand Down
9 changes: 8 additions & 1 deletion config_kz_default.yaml → config.kz_default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ scenario:
simpl: ['']
ll: ['v1.0']
clusters: [14]
opts: [RES0.2-1H, RES0.25-1H, RES0.3-1H, RES0.35-1H, RES0.4-1H]
opts: [BAU-RES0.2-1H, BAU-RES0.25-1H, BAU-RES0.3-1H, BAU-RES0.35-1H, BAU-RES0.4-1H]

countries: ["KZ"]
# Can be replaced by country ["NG", "BJ"], continent ["Africa"] or user-specific region, see more at https://pypsa-earth.readthedocs.io/en/latest/configuration.html#top-level-configuration
Expand Down Expand Up @@ -128,6 +128,13 @@ electricity:
co2base: 3.17e+8 # Kazakhstan default for 1990
agg_p_nom_limits: data/agg_p_nom_minmax.csv
hvdc_as_lines: false # should HVDC lines be modeled as `Line` or as `Link` component?
BAU_maxcapacities:
solar: 1e9 # essentially no limit
onwind: 1e9 # essentially no limit
offwind-ac: 1e9 # essentially no limit
offwind-dc: 1e9 # essentially no limit
coal: 1e9 # essentially no limit (only relevant for coal exit scenarios)
ror: 2.133e3 # limit in MW (2030 projection taken from https://kea.kz/storage/app/media/NPA%20NTD/ministra-energetiki-respubliki-kazakhstan-ot-24-marta-2022-goda-104-ob-utverzhdenii-energeticheskogo-balansa-respubliki-ka.pdf)

operational_reserve: # like https://genxproject.github.io/GenX/dev/core/#Reserves
activate: false
Expand Down
9 changes: 9 additions & 0 deletions kz.smk
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,12 @@ rule solve_everything:
["results/" + RDIR + "networks/elec_s{simpl}_{clusters}_ec_l{ll}_{opts}_dle.nc"],
**config["scenario"]
),


rule prepare_kz_scenarios:
run:
import os
os.system("rm configs/scenarios/config.NG.yaml")
os.system("cp -r pypsa-kz-data/scenarios/ configs/")
os.system("snakemake -j1 retrieve_databundle_light")

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ snapshots:

load_options:
weather_year: 2011 # Load scenarios available with different weather year (different renewable potentials)
scale_kz: 1.3705

electricity:
extendable_carriers:
Generator: [solar, onwind, offwind-ac, offwind-dc]
Generator: [solar, onwind, offwind-ac, offwind-dc, ror]
Store: [battery, H2]

atlite:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ run:

scenario:
ll: ['v1.0','copt']
opts: [1H, RES0.4-1H]
opts: [BAU-1H, BAU-RES0.4-1H]

snapshots:
start: "2011-01-01"
end: "2012-01-01"

load_options:
weather_year: 2011 # Load scenarios available with different weather year (different renewable potentials)
scale_kz: 1.3705

electricity:
extendable_carriers:
Generator: [solar, onwind, offwind-ac, offwind-dc, coal]
Generator: [solar, onwind, offwind-ac, offwind-dc, coal, ror]
Store: [battery, H2]

atlite:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ snapshots:

load_options:
weather_year: 2011 # Load scenarios available with different weather year (different renewable potentials)
scale_kz: 1.3705

electricity:
extendable_carriers:
Generator: [solar, onwind, offwind-ac, offwind-dc]
Generator: [solar, onwind, offwind-ac, offwind-dc, ror]
Store: [battery, H2]

atlite:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ run:

scenario:
ll: ['v1.0','copt']
opts: [1H, RES0.4-1H]
opts: [BAU-1H, BAU-RES0.4-1H]

snapshots:
start: "2011-01-01"
end: "2012-01-01"

load_options:
weather_year: 2011 # Load scenarios available with different weather year (different renewable potentials)
scale_kz: 1.3705

electricity:
extendable_carriers:
Generator: [solar, onwind, offwind-ac, offwind-dc, coal]
Generator: [solar, onwind, offwind-ac, offwind-dc, coal, ror]
Store: [battery, H2]

atlite:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ snapshots:

load_options:
weather_year: 2011 # Load scenarios available with different weather year (different renewable potentials)
scale_kz: 1.3705

electricity:
extendable_carriers:
Generator: [solar, onwind, offwind-ac, offwind-dc]
Generator: [solar, onwind, offwind-ac, offwind-dc, ror]
Store: [battery, H2]

atlite:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ run:

scenario:
ll: ['v1.0','copt']
opts: [1H, RES0.4-1H]
opts: [BAU-1H, BAU-RES0.4-1H]

snapshots:
start: "2011-01-01"
end: "2012-01-01"

load_options:
weather_year: 2011 # Load scenarios available with different weather year (different renewable potentials)
scale: 1.3705

electricity:
extendable_carriers:
Generator: [solar, onwind, offwind-ac, offwind-dc, coal]
Generator: [solar, onwind, offwind-ac, offwind-dc, coal, ror]
Store: [battery, H2]

atlite:
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ snapshots:

load_options:
weather_year: 2013 # Load scenarios available with different weather year (different renewable potentials)
scale: 1.3705

electricity:
extendable_carriers:
Generator: [solar, onwind, offwind-ac, offwind-dc]
Generator: [solar, onwind, offwind-ac, offwind-dc, ror]
Store: [battery, H2]

atlite:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ run:

scenario:
ll: ['v1.0','copt']
opts: [1H, RES0.4-1H]
opts: [BAU-1H, BAU-RES0.4-1H]

snapshots:
start: "2013-01-01"
end: "2014-01-01"

load_options:
weather_year: 2013 # Load scenarios available with different weather year (different renewable potentials)
scale: 1.3705

electricity:
extendable_carriers:
Generator: [solar, onwind, offwind-ac, offwind-dc, coal]
Generator: [solar, onwind, offwind-ac, offwind-dc, coal, ror]
Store: [battery, H2]

atlite:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ snapshots:

load_options:
weather_year: 2013 # Load scenarios available with different weather year (different renewable potentials)
scale: 1.3705

electricity:
extendable_carriers:
Generator: [solar, onwind, offwind-ac, offwind-dc]
Generator: [solar, onwind, offwind-ac, offwind-dc, ror]
Store: [battery, H2]

atlite:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ run:

scenario:
ll: ['v1.0','copt']
opts: [1H, RES0.4-1H]
opts: [BAU-1H, BAU-RES0.4-1H]

snapshots:
start: "2013-01-01"
end: "2014-01-01"

load_options:
weather_year: 2013 # Load scenarios available with different weather year (different renewable potentials)
scale: 1.3705

electricity:
extendable_carriers:
Generator: [solar, onwind, offwind-ac, offwind-dc, coal]
Generator: [solar, onwind, offwind-ac, offwind-dc, coal, ror]
Store: [battery, H2]

atlite:
Expand Down
Loading

0 comments on commit 1fc0931

Please sign in to comment.