Skip to content

Commit 6541d28

Browse files
committed
Add particle execution timings
1 parent 5c41d41 commit 6541d28

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from parcels import AdvectionRK4, FieldSet, ParticleSet, ScipyParticle, JITParticle
2+
import numpy as np
3+
from datetime import timedelta
4+
5+
6+
class ParticleExecutionJIT:
7+
def setup(self):
8+
xdim = ydim = zdim = 2
9+
npart = 1_000
10+
11+
dimensions = {
12+
"lon": np.linspace(0.0, 1e4, xdim, dtype=np.float32),
13+
"lat": np.linspace(0.0, 1e4, ydim, dtype=np.float32),
14+
"depth": np.linspace(0.0, 1.0, zdim, dtype=np.float32),
15+
}
16+
data = {
17+
"U": np.ones((xdim, ydim, zdim), dtype=np.float32),
18+
"V": np.zeros((xdim, ydim, zdim), dtype=np.float32),
19+
}
20+
data["U"][:, :, 0] = 0.0
21+
fieldset = FieldSet.from_data(data, dimensions, mesh="flat", transpose=True)
22+
23+
self.pset = ParticleSet(
24+
fieldset,
25+
pclass=JITParticle,
26+
lon=np.zeros(npart),
27+
lat=np.zeros(npart) + 1e2,
28+
depth=np.linspace(0, 1, npart),
29+
)
30+
# trigger compilation
31+
self.pset.execute(AdvectionRK4, runtime=0, dt=timedelta(seconds=30))
32+
33+
def time_run_single_timestep(self):
34+
self.pset.execute(
35+
AdvectionRK4, runtime=timedelta(seconds=1 * 30), dt=timedelta(seconds=30)
36+
)
37+
38+
def time_run_many_timesteps(self):
39+
self.pset.execute(
40+
AdvectionRK4, runtime=timedelta(seconds=100 * 30), dt=timedelta(seconds=30)
41+
)
42+
43+
44+
class ParticleExecutionScipy:
45+
def setup(self):
46+
xdim = ydim = zdim = 2
47+
npart = 1_000
48+
49+
dimensions = {
50+
"lon": np.linspace(0.0, 1e4, xdim, dtype=np.float32),
51+
"lat": np.linspace(0.0, 1e4, ydim, dtype=np.float32),
52+
"depth": np.linspace(0.0, 1.0, zdim, dtype=np.float32),
53+
}
54+
data = {
55+
"U": np.ones((xdim, ydim, zdim), dtype=np.float32),
56+
"V": np.zeros((xdim, ydim, zdim), dtype=np.float32),
57+
}
58+
data["U"][:, :, 0] = 0.0
59+
fieldset = FieldSet.from_data(data, dimensions, mesh="flat", transpose=True)
60+
61+
self.pset = ParticleSet(
62+
fieldset,
63+
pclass=ScipyParticle,
64+
lon=np.zeros(npart),
65+
lat=np.zeros(npart) + 1e2,
66+
depth=np.linspace(0, 1, npart),
67+
)
68+
69+
def time_run_single_timestep(self):
70+
self.pset.execute(
71+
AdvectionRK4, runtime=timedelta(seconds=1 * 30), dt=timedelta(seconds=30)
72+
)
73+
74+
def time_run_many_timesteps(self):
75+
self.pset.execute(
76+
AdvectionRK4, runtime=timedelta(seconds=100 * 30), dt=timedelta(seconds=30)
77+
)

0 commit comments

Comments
 (0)