|
1 | | -## Argo float benchmark |
2 | | - |
3 | 1 | from datetime import timedelta |
4 | 2 |
|
5 | 3 | import numpy as np |
6 | | - |
| 4 | +import pandas as pd |
| 5 | +import xarray as xr |
7 | 6 | from parcels import AdvectionRK4, FieldSet, JITParticle, ParticleSet, StatusCode, Variable |
8 | 7 |
|
9 | 8 |
|
@@ -56,42 +55,67 @@ def ArgoVerticalMovement(particle, fieldset, time): |
56 | 55 |
|
57 | 56 | class ArgoFloatJIT: |
58 | 57 | def setup(self): |
59 | | - xdim = ydim = zdim = 2 |
| 58 | + time = pd.date_range(start="2025-01-01", end="2025-02-16", freq="D") |
| 59 | + lon = np.linspace(-180,180,120) |
| 60 | + lat = np.linspace(-90,90,100) |
60 | 61 |
|
61 | | - dimensions = { |
62 | | - "lon": "lon", |
63 | | - "lat": "lat", |
64 | | - "depth": "depth", |
65 | | - } |
66 | | - data = { |
67 | | - "U": np.ones((xdim, ydim, zdim), dtype=np.float32), |
68 | | - "V": np.zeros((xdim, ydim, zdim), dtype=np.float32), |
| 62 | + Lon,Lat = np.meshgrid(lon,lat) |
| 63 | + |
| 64 | + # Create large-scale gyre flow |
| 65 | + U_gyre = np.cos(np.radians(Lat)) * np.sin(np.radians(Lon)) # Zonal flow |
| 66 | + V_gyre = -np.sin(np.radians(Lat)) * np.cos(np.radians(Lon)) # Meridional flow |
| 67 | + |
| 68 | + f = 2 * 7.2921e-5 * np.sin(np.radians(Lat)) |
| 69 | + |
| 70 | + U_coriolis = U_gyre * (1 - 0.5 * np.abs(f)) |
| 71 | + V_coriolis = V_gyre * (1 - 0.5 * np.abs(f)) |
| 72 | + |
| 73 | + noise_level = 0.1 # Adjust for more or less variability |
| 74 | + U_noise = noise_level * np.random.randn(*U_coriolis.shape) |
| 75 | + V_noise = noise_level * np.random.randn(*V_coriolis.shape) |
| 76 | + |
| 77 | + # Final realistic U and V velocity fields |
| 78 | + U_final = U_coriolis + U_noise |
| 79 | + V_final = V_coriolis + V_noise |
| 80 | + |
| 81 | + depth = np.linspace(0,2000,100) |
| 82 | + |
| 83 | + U_val = np.tile(U_final[None,None, :, :], (len(time), len(depth),1, 1)) # Repeat for each time step |
| 84 | + V_val = np.tile(V_final[None,None, :, :], (len(time), len(depth),1, 1)) |
| 85 | + |
| 86 | + U = xr.DataArray(U_val, |
| 87 | + dims = ['time','depth','lat','lon'], |
| 88 | + coords = {'time':time, 'depth':depth,'lat':lat, 'lon':lon}, |
| 89 | + name='U_velocity') |
| 90 | + |
| 91 | + V = xr.DataArray(V_val, |
| 92 | + dims = ['time','depth','lat','lon'], |
| 93 | + coords = {'time':time, 'depth':depth,'lat':lat, 'lon':lon}, |
| 94 | + name='V_velocity') |
| 95 | + |
| 96 | + ds = xr.Dataset({"U":U, "V":V}) |
| 97 | + |
| 98 | + variables = { |
| 99 | + "U": "U", |
| 100 | + "V": "V", |
69 | 101 | } |
70 | | - data["U"][:, :, 0] = 0.0 |
71 | | - fieldset = FieldSet.from_data(data, dimensions, mesh="flat", transpose=True) |
| 102 | + dimensions = {"lat": "lat", "lon": "lon", "time": "time", "depth":"depth"} |
| 103 | + fieldset = FieldSet.from_xarray_dataset(ds, variables, dimensions) |
| 104 | + # uppermost layer in the hydrodynamic data |
72 | 105 | fieldset.mindepth = fieldset.U.depth[0] |
73 | | - |
74 | 106 | # Define a new Particle type including extra Variables |
75 | | - self.ArgoParticle = JITParticle.add_variables( |
| 107 | + ArgoParticle = JITParticle.add_variables( |
76 | 108 | [ |
77 | | - # Phase of cycle: |
78 | | - # init_descend=0, |
79 | | - # drift=1, |
80 | | - # profile_descend=2, |
81 | | - # profile_ascend=3, |
82 | | - # transmit=4 |
83 | 109 | Variable("cycle_phase", dtype=np.int32, initial=0.0), |
84 | 110 | Variable("cycle_age", dtype=np.float32, initial=0.0), |
85 | 111 | Variable("drift_age", dtype=np.float32, initial=0.0), |
86 | | - # if fieldset has temperature |
87 | | - # Variable('temp', dtype=np.float32, initial=np.nan), |
88 | 112 | ] |
89 | 113 | ) |
90 | 114 |
|
91 | | - self.pset = ParticleSet(fieldset=fieldset, pclass=ArgoParticle, lon=[0], lat=[0], depth=[0]) |
| 115 | + self.pset = ParticleSet(fieldset=fieldset, pclass=ArgoParticle, lon=[32], lat=[-31], depth=[0]) |
92 | 116 |
|
93 | | - # combine Argo vertical movement kernel with built-in Advection kernel |
94 | | - self.kernels = [ArgoVerticalMovement, AdvectionRK4] |
| 117 | + |
| 118 | + def time_run_many_timesteps(self): |
| 119 | + self.pset.execute([ArgoVerticalMovement, AdvectionRK4], runtime=timedelta(seconds=1 * 30), dt=timedelta(seconds=30)) |
95 | 120 |
|
96 | | - def time_run_single_timestep(self): |
97 | | - self.pset.execute(AdvectionRK4, runtime=timedelta(seconds=1 * 30), dt=timedelta(seconds=30)) |
| 121 | + |
0 commit comments