Skip to content

Commit 7bbdcd6

Browse files
author
Lizarbe
committed
Argo fixed
1 parent 51bf80a commit 7bbdcd6

File tree

1 file changed

+53
-29
lines changed

1 file changed

+53
-29
lines changed

asv_bench/benchmarks/Argofloat_benchmark.py renamed to asv_bench/benchmarks/benchmark_argofloat.py

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
## Argo float benchmark
2-
31
from datetime import timedelta
42

53
import numpy as np
6-
4+
import pandas as pd
5+
import xarray as xr
76
from parcels import AdvectionRK4, FieldSet, JITParticle, ParticleSet, StatusCode, Variable
87

98

@@ -56,42 +55,67 @@ def ArgoVerticalMovement(particle, fieldset, time):
5655

5756
class ArgoFloatJIT:
5857
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)
6061

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",
69101
}
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
72105
fieldset.mindepth = fieldset.U.depth[0]
73-
74106
# Define a new Particle type including extra Variables
75-
self.ArgoParticle = JITParticle.add_variables(
107+
ArgoParticle = JITParticle.add_variables(
76108
[
77-
# Phase of cycle:
78-
# init_descend=0,
79-
# drift=1,
80-
# profile_descend=2,
81-
# profile_ascend=3,
82-
# transmit=4
83109
Variable("cycle_phase", dtype=np.int32, initial=0.0),
84110
Variable("cycle_age", dtype=np.float32, initial=0.0),
85111
Variable("drift_age", dtype=np.float32, initial=0.0),
86-
# if fieldset has temperature
87-
# Variable('temp', dtype=np.float32, initial=np.nan),
88112
]
89113
)
90114

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])
92116

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))
95120

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

Comments
 (0)