|
13 | 13 | AdvectionRK4, |
14 | 14 | AdvectionRK4_3D, |
15 | 15 | AdvectionRK45, |
16 | | - Field, |
17 | 16 | FieldSet, |
18 | 17 | Particle, |
19 | 18 | ParticleSet, |
@@ -274,119 +273,6 @@ def test_length1dimensions(u, v, w): |
274 | 273 | assert ((np.array([p.depth - y0 for p in pset]) - 4 * w) < 1e-6).all() |
275 | 274 |
|
276 | 275 |
|
277 | | -def truth_stationary(x_0, y_0, t): |
278 | | - lat = y_0 - u_0 / f * (1 - math.cos(f * t)) |
279 | | - lon = x_0 + u_0 / f * math.sin(f * t) |
280 | | - return lon, lat |
281 | | - |
282 | | - |
283 | | -def create_fieldset_stationary(xdim=100, ydim=100, maxtime=timedelta(hours=6)): |
284 | | - """Generate a FieldSet encapsulating the flow field of a stationary eddy. |
285 | | -
|
286 | | - Reference: N. Fabbroni, 2009, "Numerical simulations of passive |
287 | | - tracers dispersion in the sea" |
288 | | - """ |
289 | | - time = np.arange(0.0, maxtime.total_seconds() + 1e-5, 60.0, dtype=np.float64) |
290 | | - dimensions = { |
291 | | - "lon": np.linspace(0, 25000, xdim, dtype=np.float32), |
292 | | - "lat": np.linspace(0, 25000, ydim, dtype=np.float32), |
293 | | - "time": time, |
294 | | - } |
295 | | - data = { |
296 | | - "U": np.transpose(np.ones((xdim, ydim, 1), dtype=np.float32) * u_0 * np.cos(f * time)), |
297 | | - "V": np.transpose(np.ones((xdim, ydim, 1), dtype=np.float32) * -u_0 * np.sin(f * time)), |
298 | | - } |
299 | | - fieldset = FieldSet.from_data(data, dimensions, mesh="flat") |
300 | | - # setting some constants for AdvectionRK45 kernel |
301 | | - fieldset.RK45_min_dt = 1e-3 |
302 | | - fieldset.RK45_max_dt = 1e2 |
303 | | - fieldset.RK45_tol = 1e-5 |
304 | | - return fieldset |
305 | | - |
306 | | - |
307 | | -@pytest.fixture |
308 | | -def fieldset_stationary(): |
309 | | - return create_fieldset_stationary() |
310 | | - |
311 | | - |
312 | | -@pytest.mark.v4alpha |
313 | | -@pytest.mark.xfail(reason="GH1946") |
314 | | -@pytest.mark.parametrize( |
315 | | - "method, rtol, diffField", |
316 | | - [ |
317 | | - ("EE", 1e-2, False), |
318 | | - ("AdvDiffEM", 1e-2, True), |
319 | | - ("AdvDiffM1", 1e-2, True), |
320 | | - ("RK4", 1e-5, False), |
321 | | - ("RK45", 1e-5, False), |
322 | | - ], |
323 | | -) |
324 | | -def test_stationary_eddy(fieldset_stationary, method, rtol, diffField): |
325 | | - npart = 1 |
326 | | - fieldset = fieldset_stationary |
327 | | - if diffField: |
328 | | - fieldset.add_field(Field("Kh_zonal", np.zeros(fieldset.U.data.shape), grid=fieldset.U.grid)) |
329 | | - fieldset.add_field(Field("Kh_meridional", np.zeros(fieldset.V.data.shape), grid=fieldset.V.grid)) |
330 | | - fieldset.add_constant("dres", 0.1) |
331 | | - lon = np.linspace(12000, 21000, npart) |
332 | | - lat = np.linspace(12500, 12500, npart) |
333 | | - dt = timedelta(minutes=3).total_seconds() |
334 | | - endtime = timedelta(hours=6).total_seconds() |
335 | | - |
336 | | - RK45Particles = Particle.add_variable("next_dt", dtype=np.float32, initial=dt) |
337 | | - |
338 | | - pclass = RK45Particles if method == "RK45" else Particle |
339 | | - pset = ParticleSet(fieldset, pclass=pclass, lon=lon, lat=lat) |
340 | | - pset.execute(kernel[method], dt=dt, endtime=endtime) |
341 | | - |
342 | | - exp_lon = [truth_stationary(x, y, pset[0].time)[0] for x, y in zip(lon, lat, strict=True)] |
343 | | - exp_lat = [truth_stationary(x, y, pset[0].time)[1] for x, y in zip(lon, lat, strict=True)] |
344 | | - assert np.allclose(pset.lon, exp_lon, rtol=rtol) |
345 | | - assert np.allclose(pset.lat, exp_lat, rtol=rtol) |
346 | | - |
347 | | - |
348 | | -@pytest.mark.v4alpha |
349 | | -@pytest.mark.xfail(reason="GH1946") |
350 | | -def test_stationary_eddy_vertical(): |
351 | | - npart = 1 |
352 | | - lon = np.linspace(12000, 21000, npart) |
353 | | - lat = np.linspace(10000, 20000, npart) |
354 | | - depth = np.linspace(12500, 12500, npart) |
355 | | - endtime = timedelta(hours=6).total_seconds() |
356 | | - dt = timedelta(minutes=3).total_seconds() |
357 | | - |
358 | | - xdim = ydim = 100 |
359 | | - lon_data = np.linspace(0, 25000, xdim, dtype=np.float32) |
360 | | - lat_data = np.linspace(0, 25000, ydim, dtype=np.float32) |
361 | | - time_data = np.arange(0.0, 6 * 3600 + 1e-5, 60.0, dtype=np.float64) |
362 | | - fld1 = np.transpose(np.ones((xdim, ydim, 1), dtype=np.float32) * u_0 * np.cos(f * time_data)) |
363 | | - fld2 = np.transpose(np.ones((xdim, ydim, 1), dtype=np.float32) * -u_0 * np.sin(f * time_data)) |
364 | | - fldzero = np.transpose(np.zeros((xdim, ydim, 1), dtype=np.float32) * time_data) |
365 | | - |
366 | | - dimensions = {"lon": lon_data, "lat": lat_data, "time": time_data} |
367 | | - data = {"U": fld1, "V": fldzero, "W": fld2} |
368 | | - fieldset = FieldSet.from_data(data, dimensions, mesh="flat") |
369 | | - |
370 | | - pset = ParticleSet(fieldset, pclass=Particle, lon=lon, lat=lat, depth=depth) |
371 | | - pset.execute(AdvectionRK4_3D, dt=dt, endtime=endtime) |
372 | | - exp_lon = [truth_stationary(x, z, pset[0].time)[0] for x, z in zip(lon, depth, strict=True)] |
373 | | - exp_depth = [truth_stationary(x, z, pset[0].time)[1] for x, z in zip(lon, depth, strict=True)] |
374 | | - assert np.allclose(pset.lon, exp_lon, rtol=1e-5) |
375 | | - assert np.allclose(pset.lat, lat, rtol=1e-5) |
376 | | - assert np.allclose(pset.depth, exp_depth, rtol=1e-5) |
377 | | - |
378 | | - data = {"U": fldzero, "V": fld2, "W": fld1} |
379 | | - fieldset = FieldSet.from_data(data, dimensions, mesh="flat") |
380 | | - |
381 | | - pset = ParticleSet(fieldset, pclass=Particle, lon=lon, lat=lat, depth=depth) |
382 | | - pset.execute(AdvectionRK4_3D, dt=dt, endtime=endtime) |
383 | | - exp_depth = [truth_stationary(z, y, pset[0].time)[0] for z, y in zip(depth, lat, strict=True)] |
384 | | - exp_lat = [truth_stationary(z, y, pset[0].time)[1] for z, y in zip(depth, lat, strict=True)] |
385 | | - assert np.allclose(pset.lon, lon, rtol=1e-5) |
386 | | - assert np.allclose(pset.lat, exp_lat, rtol=1e-5) |
387 | | - assert np.allclose(pset.depth, exp_depth, rtol=1e-5) |
388 | | - |
389 | | - |
390 | 276 | @pytest.mark.v4alpha |
391 | 277 | @pytest.mark.xfail(reason="GH1946") |
392 | 278 | def test_analyticalAgrid(): |
|
0 commit comments