-
Notifications
You must be signed in to change notification settings - Fork 120
/
test_grids.py
378 lines (288 loc) · 14.7 KB
/
test_grids.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
from parcels import FieldSet, Field, ParticleSet, ScipyParticle, JITParticle, Variable, AdvectionRK4, AdvectionRK4_3D
from parcels import RectilinearZGrid, RectilinearSGrid, CurvilinearZGrid
import numpy as np
import math
import pytest
from os import path
from datetime import timedelta as delta
ptype = {'scipy': ScipyParticle, 'jit': JITParticle}
@pytest.mark.parametrize('mode', ['scipy', 'jit'])
def test_multi_structured_grids(mode):
def temp_func(lon, lat):
return 20 + lat/1000. + 2 * np.sin(lon*2*np.pi/5000.)
a = 10000
b = 10000
# Grid 0
xdim_g0 = 201
ydim_g0 = 201
# Coordinates of the test fieldset (on A-grid in deg)
lon_g0 = np.linspace(0, a, xdim_g0, dtype=np.float32)
lat_g0 = np.linspace(0, b, ydim_g0, dtype=np.float32)
time_g0 = np.linspace(0., 1000., 2, dtype=np.float64)
grid_0 = RectilinearZGrid(lon_g0, lat_g0, time=time_g0)
# Grid 1
xdim_g1 = 51
ydim_g1 = 51
# Coordinates of the test fieldset (on A-grid in deg)
lon_g1 = np.linspace(0, a, xdim_g1, dtype=np.float32)
lat_g1 = np.linspace(0, b, ydim_g1, dtype=np.float32)
time_g1 = np.linspace(0., 1000., 2, dtype=np.float64)
grid_1 = RectilinearZGrid(lon_g1, lat_g1, time=time_g1)
u_data = np.ones((lon_g0.size, lat_g0.size, time_g0.size), dtype=np.float32)
u_data = 2*u_data
u_field = Field('U', u_data, grid=grid_0, transpose=True)
temp0_data = np.empty((lon_g0.size, lat_g0.size, time_g0.size), dtype=np.float32)
for i in range(lon_g0.size):
for j in range(lat_g0.size):
temp0_data[i, j, :] = temp_func(lon_g0[i], lat_g0[j])
temp0_field = Field('temp0', temp0_data, grid=grid_0, transpose=True)
v_data = np.zeros((lon_g1.size, lat_g1.size, time_g1.size), dtype=np.float32)
v_field = Field('V', v_data, grid=grid_1, transpose=True)
temp1_data = np.empty((lon_g1.size, lat_g1.size, time_g1.size), dtype=np.float32)
for i in range(lon_g1.size):
for j in range(lat_g1.size):
temp1_data[i, j, :] = temp_func(lon_g1[i], lat_g1[j])
temp1_field = Field('temp1', temp1_data, grid=grid_1, transpose=True)
other_fields = {}
other_fields['temp0'] = temp0_field
other_fields['temp1'] = temp1_field
field_set = FieldSet(u_field, v_field, fields=other_fields)
def sampleTemp(particle, fieldset, time, dt):
# Note that fieldset.temp is interpolated at time=time+dt.
# Indeed, sampleTemp is called at time=time, but the result is written
# at time=time+dt, after the Kernel update
particle.temp0 = fieldset.temp0[time+dt, particle.lon, particle.lat, particle.depth]
particle.temp1 = fieldset.temp1[time+dt, particle.lon, particle.lat, particle.depth]
class MyParticle(ptype[mode]):
temp0 = Variable('temp0', dtype=np.float32, initial=20.)
temp1 = Variable('temp1', dtype=np.float32, initial=20.)
pset = ParticleSet.from_list(field_set, MyParticle, lon=[3001], lat=[5001])
pset.execute(AdvectionRK4 + pset.Kernel(sampleTemp), runtime=1, dt=1)
assert np.allclose(pset.particles[0].temp0, pset.particles[0].temp1, atol=1e-3)
@pytest.mark.xfail(reason="Grid cannot be computed using a time vector which is neither float nor int", strict=True)
def test_time_format_in_grid():
lon = np.linspace(0, 1, 2, dtype=np.float32)
lat = np.linspace(0, 1, 2, dtype=np.float32)
time = np.array([np.datetime64('2000-01-01')]*2)
RectilinearZGrid(lon, lat, time=time)
def test_avoid_repeated_grids():
lon_g0 = np.linspace(0, 1000, 11, dtype=np.float32)
lat_g0 = np.linspace(0, 1000, 11, dtype=np.float32)
time_g0 = np.linspace(0, 1000, 2, dtype=np.float64)
grid_0 = RectilinearZGrid(lon_g0, lat_g0, time=time_g0)
lon_g1 = np.linspace(0, 1000, 21, dtype=np.float32)
lat_g1 = np.linspace(0, 1000, 21, dtype=np.float32)
time_g1 = np.linspace(0, 1000, 2, dtype=np.float64)
grid_1 = RectilinearZGrid(lon_g1, lat_g1, time=time_g1)
u_data = np.zeros((lon_g0.size, lat_g0.size, time_g0.size), dtype=np.float32)
u_field = Field('U', u_data, grid=grid_0, transpose=True)
v_data = np.zeros((lon_g1.size, lat_g1.size, time_g1.size), dtype=np.float32)
v_field = Field('V', v_data, grid=grid_1, transpose=True)
temp0_field = Field('temp', u_data, lon=lon_g0, lat=lat_g0, time=time_g0, transpose=True)
other_fields = {}
other_fields['temp'] = temp0_field
field_set = FieldSet(u_field, v_field, fields=other_fields)
assert field_set.gridset.size == 2
assert field_set.U.grid is field_set.temp.grid
assert field_set.V.grid is not field_set.U.grid
@pytest.mark.parametrize('mode', ['scipy', 'jit'])
def test_multigrids_pointer(mode):
lon_g0 = np.linspace(0, 1e4, 21, dtype=np.float32)
lat_g0 = np.linspace(0, 1000, 2, dtype=np.float32)
depth_g0 = np.zeros((5, lat_g0.size, lon_g0.size), dtype=np.float32)
def bath_func(lon):
return lon / 1000. + 10
bath = bath_func(lon_g0)
zdim = depth_g0.shape[0]
for i in range(lon_g0.size):
for k in range(zdim):
depth_g0[k, :, i] = bath[i] * k / (zdim-1)
grid_0 = RectilinearSGrid(lon_g0, lat_g0, depth=depth_g0)
grid_1 = RectilinearSGrid(lon_g0, lat_g0, depth=depth_g0)
u_data = np.zeros((zdim, lat_g0.size, lon_g0.size), dtype=np.float32)
v_data = np.zeros((zdim, lat_g0.size, lon_g0.size), dtype=np.float32)
w_data = np.zeros((zdim, lat_g0.size, lon_g0.size), dtype=np.float32)
u_field = Field('U', u_data, grid=grid_0)
v_field = Field('V', v_data, grid=grid_0)
w_field = Field('W', w_data, grid=grid_1)
field_set = FieldSet(u_field, v_field, fields={'W': w_field})
assert(u_field.grid == v_field.grid)
assert(u_field.grid == w_field.grid) # w_field.grid is now supposed to be grid_1
pset = ParticleSet.from_list(field_set, ptype[mode], lon=[0], lat=[0], depth=[1])
for i in range(10):
pset.execute(AdvectionRK4_3D, runtime=1000, dt=500)
@pytest.mark.parametrize('mode', ['scipy', 'jit'])
@pytest.mark.parametrize('z4d', ['True', 'False'])
def test_rectilinear_s_grid_sampling(mode, z4d):
lon_g0 = np.linspace(-3e4, 3e4, 61, dtype=np.float32)
lat_g0 = np.linspace(0, 1000, 2, dtype=np.float32)
time_g0 = np.linspace(0, 1000, 2, dtype=np.float64)
if z4d:
depth_g0 = np.zeros((time_g0.size, 5, lat_g0.size, lon_g0.size), dtype=np.float32)
else:
depth_g0 = np.zeros((5, lat_g0.size, lon_g0.size), dtype=np.float32)
def bath_func(lon):
bath = (lon <= -2e4) * 20.
bath += (lon > -2e4) * (lon < 2e4) * (110. + 90 * np.sin(lon/2e4 * np.pi/2.))
bath += (lon >= 2e4) * 200.
return bath
bath = bath_func(lon_g0)
zdim = depth_g0.shape[-3]
for i in range(depth_g0.shape[-1]):
for k in range(zdim):
if z4d:
depth_g0[:, k, :, i] = bath[i] * k / (zdim-1)
else:
depth_g0[k, :, i] = bath[i] * k / (zdim-1)
grid = RectilinearSGrid(lon_g0, lat_g0, depth=depth_g0, time=time_g0)
u_data = np.zeros((grid.tdim, grid.zdim, grid.ydim, grid.xdim), dtype=np.float32)
v_data = np.zeros((grid.tdim, grid.zdim, grid.ydim, grid.xdim), dtype=np.float32)
temp_data = np.zeros((grid.tdim, grid.zdim, grid.ydim, grid.xdim), dtype=np.float32)
for k in range(1, zdim):
temp_data[:, k, :, :] = k / (zdim-1.)
u_field = Field('U', u_data, grid=grid)
v_field = Field('V', v_data, grid=grid)
temp_field = Field('temp', temp_data, grid=grid)
other_fields = {}
other_fields['temp'] = temp_field
field_set = FieldSet(u_field, v_field, fields=other_fields)
def sampleTemp(particle, fieldset, time, dt):
particle.temp = fieldset.temp[time, particle.lon, particle.lat, particle.depth]
class MyParticle(ptype[mode]):
temp = Variable('temp', dtype=np.float32, initial=20.)
lon = 400
lat = 0
ratio = .3
pset = ParticleSet.from_list(field_set, MyParticle, lon=[lon], lat=[lat], depth=[bath_func(lon)*ratio])
pset.execute(pset.Kernel(sampleTemp), runtime=0, dt=0)
assert np.allclose(pset.particles[0].temp, ratio, atol=1e-4)
@pytest.mark.parametrize('mode', ['scipy', 'jit'])
def test_rectilinear_s_grids_advect1(mode):
# Constant water transport towards the east. check that the particle stays at the same relative depth (z/bath)
lon_g0 = np.linspace(0, 1e4, 21, dtype=np.float32)
lat_g0 = np.linspace(0, 1000, 2, dtype=np.float32)
depth_g0 = np.zeros((lon_g0.size, lat_g0.size, 5), dtype=np.float32)
def bath_func(lon):
return lon / 1000. + 10
bath = bath_func(lon_g0)
for i in range(depth_g0.shape[0]):
for k in range(depth_g0.shape[2]):
depth_g0[i, :, k] = bath[i] * k / (depth_g0.shape[2]-1)
depth_g0 = depth_g0.transpose() # we don't change it on purpose, to check if the transpose op if fixed in jit
grid = RectilinearSGrid(lon_g0, lat_g0, depth=depth_g0)
zdim = depth_g0.shape[0]
u_data = np.zeros((zdim, lat_g0.size, lon_g0.size), dtype=np.float32)
v_data = np.zeros((zdim, lat_g0.size, lon_g0.size), dtype=np.float32)
w_data = np.zeros((zdim, lat_g0.size, lon_g0.size), dtype=np.float32)
for i in range(lon_g0.size):
u_data[:, :, i] = 1 * 10 / bath[i]
for k in range(zdim):
w_data[k, :, i] = u_data[k, :, i] * depth_g0[k, :, i] / bath[i] * 1e-3
u_field = Field('U', u_data, grid=grid)
v_field = Field('V', v_data, grid=grid)
w_field = Field('W', w_data, grid=grid)
field_set = FieldSet(u_field, v_field, fields={'W': w_field})
lon = np.zeros((11))
lat = np.zeros((11))
ratio = [min(i/10., .99) for i in range(11)]
depth = bath_func(lon)*ratio
pset = ParticleSet.from_list(field_set, ptype[mode], lon=lon, lat=lat, depth=depth)
pset.execute(AdvectionRK4_3D, runtime=10000, dt=500)
assert np.allclose([p.depth/bath_func(p.lon) for p in pset], ratio)
@pytest.mark.parametrize('mode', ['scipy', 'jit'])
def test_rectilinear_s_grids_advect2(mode):
# Move particle towards the east, check relative depth evolution
lon_g0 = np.linspace(0, 1e4, 21, dtype=np.float32)
lat_g0 = np.linspace(0, 1000, 2, dtype=np.float32)
depth_g0 = np.zeros((5, lat_g0.size, lon_g0.size), dtype=np.float32)
def bath_func(lon):
return lon / 1000. + 10
bath = bath_func(lon_g0)
zdim = depth_g0.shape[0]
for i in range(lon_g0.size):
for k in range(zdim):
depth_g0[k, :, i] = bath[i] * k / (zdim-1)
grid = RectilinearSGrid(lon_g0, lat_g0, depth=depth_g0)
u_data = np.zeros((zdim, lat_g0.size, lon_g0.size), dtype=np.float32)
v_data = np.zeros((zdim, lat_g0.size, lon_g0.size), dtype=np.float32)
rel_depth_data = np.zeros((zdim, lat_g0.size, lon_g0.size), dtype=np.float32)
for k in range(1, zdim):
rel_depth_data[k, :, :] = k / (zdim-1.)
u_field = Field('U', u_data, grid=grid)
v_field = Field('V', v_data, grid=grid)
rel_depth_field = Field('relDepth', rel_depth_data, grid=grid)
field_set = FieldSet(u_field, v_field, fields={'relDepth': rel_depth_field})
class MyParticle(ptype[mode]):
relDepth = Variable('relDepth', dtype=np.float32, initial=20.)
def moveEast(particle, fieldset, time, dt):
particle.lon += 5 * dt
particle.relDepth = fieldset.relDepth[time, particle.lon, particle.lat, particle.depth]
depth = .9
pset = ParticleSet.from_list(field_set, MyParticle, lon=[0], lat=[0], depth=[depth])
kernel = pset.Kernel(moveEast)
for _ in range(10):
pset.execute(kernel, runtime=100, dt=50)
assert np.allclose(pset[0].relDepth, depth/bath_func(pset[0].lon))
@pytest.mark.parametrize('mode', ['scipy', 'jit'])
def test_curvilinear_grids(mode):
x = np.linspace(0, 1e3, 7, dtype=np.float32)
y = np.linspace(0, 1e3, 5, dtype=np.float32)
(xx, yy) = np.meshgrid(x, y)
r = np.sqrt(xx*xx+yy*yy)
theta = np.arctan2(yy, xx)
theta = theta + np.pi/6.
lon = r * np.cos(theta)
lat = r * np.sin(theta)
time = np.array([0, 86400], dtype=np.float64)
grid = CurvilinearZGrid(lon, lat, time=time)
u_data = np.ones((2, y.size, x.size), dtype=np.float32)
v_data = np.zeros((2, y.size, x.size), dtype=np.float32)
u_data[0, :, :] = lon[:, :] + lat[:, :]
u_field = Field('U', u_data, grid=grid, transpose=False)
v_field = Field('V', v_data, grid=grid, transpose=False)
field_set = FieldSet(u_field, v_field)
def sampleSpeed(particle, fieldset, time, dt):
u = fieldset.U[time, particle.lon, particle.lat, particle.depth]
v = fieldset.V[time, particle.lon, particle.lat, particle.depth]
particle.speed = math.sqrt(u*u+v*v)
class MyParticle(ptype[mode]):
speed = Variable('speed', dtype=np.float32, initial=0.)
pset = ParticleSet.from_list(field_set, MyParticle, lon=[400], lat=[600])
pset.execute(pset.Kernel(sampleSpeed), runtime=0, dt=0)
assert(np.allclose(pset[0].speed, 1000))
@pytest.mark.parametrize('mode', ['scipy', 'jit'])
def test_nemo_grid(mode):
data_path = path.join(path.dirname(__file__), 'test_data/')
filenames = {'U': data_path + 'Uu_eastward_nemo_cross_180lon.nc',
'V': data_path + 'Vv_eastward_nemo_cross_180lon.nc',
'mesh_mask': data_path + 'mask_nemo_cross_180lon.nc'}
variables = {'U': 'U', 'V': 'V'}
dimensions = {'lon': 'glamf', 'lat': 'gphif'}
field_set = FieldSet.from_nemo(filenames, variables, dimensions)
def sampleVel(particle, fieldset, time, dt):
(particle.zonal, particle.meridional) = fieldset.UV[time, particle.lon, particle.lat, particle.depth]
class MyParticle(ptype[mode]):
zonal = Variable('zonal', dtype=np.float32, initial=0.)
meridional = Variable('meridional', dtype=np.float32, initial=0.)
lonp = 175.5
latp = 81.5
pset = ParticleSet.from_list(field_set, MyParticle, lon=[lonp], lat=[latp])
pset.execute(pset.Kernel(sampleVel), runtime=0, dt=0)
u = field_set.U.units.to_source(pset[0].zonal, lonp, latp, 0)
v = field_set.V.units.to_source(pset[0].meridional, lonp, latp, 0)
assert abs(u - 1) < 1e-4
assert abs(v) < 1e-4
@pytest.mark.parametrize('mode', ['scipy', 'jit'])
def test_advect_nemo(mode):
data_path = path.join(path.dirname(__file__), 'test_data/')
filenames = {'U': data_path + 'Uu_eastward_nemo_cross_180lon.nc',
'V': data_path + 'Vv_eastward_nemo_cross_180lon.nc',
'mesh_mask': data_path + 'mask_nemo_cross_180lon.nc'}
variables = {'U': 'U', 'V': 'V'}
dimensions = {'lon': 'glamf', 'lat': 'gphif'}
field_set = FieldSet.from_nemo(filenames, variables, dimensions)
lonp = 175.5
latp = 81.5
pset = ParticleSet.from_list(field_set, ptype[mode], lon=[lonp], lat=[latp])
pset.execute(AdvectionRK4, runtime=delta(days=2), dt=delta(hours=6))
assert abs(pset[0].lat - latp) < 1e-3