Skip to content

Commit a231b8a

Browse files
authored
Merge pull request #1683 from manuelaghito/dev
Adding gallery example of parallel execution in multiple CPUs
2 parents 531228d + 9aacdb6 commit a231b8a

2 files changed

Lines changed: 96 additions & 1 deletion

File tree

environment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dependencies:
3131
- cmocean
3232
- utm
3333
- roaring-landmask>=0.7
34-
- adios_db>=1.2
34+
- adios_db>=1.2,<1.2.7
3535
- copernicusmarine>=2.0
3636
- bottleneck
3737
- pip
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/env python
2+
"""
3+
Multiprocessing - Parallel execution of multiple OpenDrift instances in distinct CPUs
4+
======================================================================================
5+
"""
6+
7+
from datetime import datetime
8+
import numpy as np
9+
import xarray as xr
10+
from opendrift.readers import reader_netCDF_CF_generic
11+
from opendrift.models.oceandrift import OceanDrift
12+
from multiprocessing import Pool
13+
import glob
14+
import opendrift
15+
16+
def concatenate_outputs(input_files, output_file):
17+
"""
18+
Concatenate OpenDrift NetCDF outputs from multiple workers into one single output file.
19+
20+
"""
21+
22+
ds_list = []
23+
trajectory_offset = 0
24+
for f in input_files:
25+
dsi = xr.open_dataset(f)
26+
traj_vars = [var for var in list(dsi.variables) if "trajectory" in dsi[var].dims]
27+
ds_traj = dsi[traj_vars]
28+
ds_traj = ds_traj.assign_coords(trajectory=ds_traj.trajectory + trajectory_offset)
29+
30+
trajectory_offset += ds_traj.dims["trajectory"]
31+
ds_list.append(ds_traj)
32+
33+
ds_conc = xr.concat(ds_list, dim="trajectory")
34+
ds_conc.to_netcdf(output_file)
35+
36+
return ds_conc
37+
38+
def RunOceanDrift(pool_number):
39+
"""
40+
Configure and run one OpenDrift instance
41+
42+
"""
43+
44+
o = OceanDrift(loglevel=0, logfile="output_W"+str(pool_number)+".log", seed=0)
45+
46+
reader_topaz4 = reader_netCDF_CF_generic.Reader("https://thredds.met.no/thredds/dodsC/topaz/dataset-topaz4-arc-myoceanv2-be")
47+
o.add_reader(reader_topaz4, variables=['x_sea_water_velocity', 'y_sea_water_velocity','sea_water_temperature','sea_water_salinity','sea_floor_depth_below_sea_level'])
48+
49+
o.set_config('drift:horizontal_diffusivity', 50)
50+
51+
time = datetime(2023,1,1)
52+
53+
ntraj=500
54+
iniz=np.random.rand(ntraj) * -10. # seeding the chemicals in the upper 10m
55+
56+
o.seed_elements(lat=positions[pool_number][0], lon=positions[pool_number][1], z=iniz, radius=2000, number=ntraj, time=time, origin_marker=np.ones(ntraj)*(pool_number))
57+
58+
o.run(steps=7*4, time_step=3600*6, time_step_output=3600*6, outfile = "output_W"+str(pool_number)+".nc")
59+
60+
61+
positions=[(58.5,3),(58.2, 2.5),(58,1),(57.8,1.2),(57,1),(56.8,1.8),(56.5,2),(56,2.2)]
62+
pool_size=len(positions)
63+
64+
#%% Run pool of OpenDrift instances in parallel using distinct CPUs and concatenate results
65+
with Pool(pool_size) as p:
66+
p.starmap(RunOceanDrift, [(i,) for i in range(pool_size)])
67+
68+
concatenate_outputs(input_files=glob.glob("output_W*.nc"), output_file="output_total.nc")
69+
70+
71+
#%% Generates animation of the first instance and the concatenated output
72+
73+
o0 = opendrift.open("output_W0.nc")
74+
75+
#%%
76+
# .. image:: /gallery/animations/example_multiprocessing_0.gif
77+
78+
o0.animation(color='origin_marker',
79+
markersize=3,
80+
vmin=0,vmax=pool_size-1,
81+
colorbar=False,
82+
fast = True,
83+
lscale = 'l')
84+
85+
o = opendrift.open("output_total.nc")
86+
87+
#%%
88+
# .. image:: /gallery/animations/example_multiprocessing_1.gif
89+
90+
o.animation(color='origin_marker',
91+
markersize=3,
92+
vmin=0,vmax=pool_size-1,
93+
colorbar=False,
94+
fast = True,
95+
lscale = 'l')

0 commit comments

Comments
 (0)