build_solar_thermal_profiles: parallelize

This commit is contained in:
Fabian Neumann 2022-12-28 12:21:46 +01:00
parent 494d3010eb
commit d37ffddd4f
2 changed files with 19 additions and 26 deletions

View File

@ -207,16 +207,13 @@ rule build_cop_profiles:
rule build_solar_thermal_profiles: rule build_solar_thermal_profiles:
input: input:
pop_layout_total="resources/pop_layout_total.nc", pop_layout="resources/pop_layout_{scope}.nc",
pop_layout_urban="resources/pop_layout_urban.nc",
pop_layout_rural="resources/pop_layout_rural.nc",
regions_onshore=pypsaeur("resources/regions_onshore_elec_s{simpl}_{clusters}.geojson") regions_onshore=pypsaeur("resources/regions_onshore_elec_s{simpl}_{clusters}.geojson")
output: output:
solar_thermal_total="resources/solar_thermal_total_elec_s{simpl}_{clusters}.nc", solar_thermal="resources/solar_thermal_{scope}_elec_s{simpl}_{clusters}.nc",
solar_thermal_urban="resources/solar_thermal_urban_elec_s{simpl}_{clusters}.nc",
solar_thermal_rural="resources/solar_thermal_rural_elec_s{simpl}_{clusters}.nc"
resources: mem_mb=20000 resources: mem_mb=20000
benchmark: "benchmarks/build_solar_thermal_profiles/s{simpl}_{clusters}" threads: 16
benchmark: "benchmarks/build_solar_thermal_profiles/{scope}_s{simpl}_{clusters}"
script: "scripts/build_solar_thermal_profiles.py" script: "scripts/build_solar_thermal_profiles.py"

View File

@ -5,6 +5,7 @@ import atlite
import pandas as pd import pandas as pd
import xarray as xr import xarray as xr
import numpy as np import numpy as np
from dask.distributed import Client, LocalCluster
if __name__ == '__main__': if __name__ == '__main__':
if 'snakemake' not in globals(): if 'snakemake' not in globals():
@ -15,14 +16,9 @@ if __name__ == '__main__':
clusters=48, clusters=48,
) )
if 'snakemake' not in globals(): nprocesses = int(snakemake.threads)
from vresutils import Dict cluster = LocalCluster(n_workers=nprocesses, threads_per_worker=1)
import yaml client = Client(cluster, asynchronous=True)
snakemake = Dict()
with open('config.yaml') as f:
snakemake.config = yaml.safe_load(f)
snakemake.input = Dict()
snakemake.output = Dict()
config = snakemake.config['solar_thermal'] config = snakemake.config['solar_thermal']
@ -35,9 +31,7 @@ if __name__ == '__main__':
I = cutout.indicatormatrix(clustered_regions) I = cutout.indicatormatrix(clustered_regions)
for area in ["total", "rural", "urban"]: pop_layout = xr.open_dataarray(snakemake.input.pop_layout)
pop_layout = xr.open_dataarray(snakemake.input[f'pop_layout_{area}'])
stacked_pop = pop_layout.stack(spatial=('y', 'x')) stacked_pop = pop_layout.stack(spatial=('y', 'x'))
M = I.T.dot(np.diag(I.dot(stacked_pop))) M = I.T.dot(np.diag(I.dot(stacked_pop)))
@ -47,6 +41,8 @@ if __name__ == '__main__':
M_tilde = M / nonzero_sum M_tilde = M / nonzero_sum
solar_thermal = cutout.solar_thermal(**config, matrix=M_tilde.T, solar_thermal = cutout.solar_thermal(**config, matrix=M_tilde.T,
index=clustered_regions.index) index=clustered_regions.index,
dask_kwargs=dict(scheduler=client),
show_progress=False)
solar_thermal.to_netcdf(snakemake.output[f"solar_thermal_{area}"]) solar_thermal.to_netcdf(snakemake.output.solar_thermal)