4
4
import numpy as np
5
5
import pytest
6
6
import xarray as xr
7
+ from numpy .testing import assert_allclose
7
8
8
9
from parcels import (
9
10
AdvectionAnalytical ,
@@ -98,7 +99,7 @@ def test_advection_meridional(lon, lat):
98
99
pset = ParticleSet (fieldset , pclass = Particle , lon = np .linspace (- 60 , 60 , npart ), lat = np .linspace (0 , 30 , npart ))
99
100
delta_lat = np .diff (pset .lat )
100
101
pset .execute (AdvectionRK4 , runtime = timedelta (hours = 2 ), dt = timedelta (seconds = 30 ))
101
- assert np . allclose (np .diff (pset .lat ), delta_lat , rtol = 1.0e-4 )
102
+ assert_allclose (np .diff (pset .lat ), delta_lat , rtol = 1.0e-4 )
102
103
103
104
104
105
def test_advection_3D ():
@@ -119,7 +120,7 @@ def test_advection_3D():
119
120
)
120
121
time = timedelta (hours = 2 ).total_seconds ()
121
122
pset .execute (AdvectionRK4 , runtime = time , dt = timedelta (seconds = 30 ))
122
- assert np . allclose (pset .depth * pset .time , pset .lon , atol = 1.0e-1 )
123
+ assert_allclose (pset .depth * pset .time , pset .lon , atol = 1.0e-1 )
123
124
124
125
125
126
@pytest .mark .parametrize ("direction" , ["up" , "down" ])
@@ -161,8 +162,8 @@ def SubmergeParticle(particle, fieldset, time): # pragma: no cover
161
162
pset .execute (kernels , runtime = 11.0 , dt = 1 )
162
163
163
164
if direction == "up" and wErrorThroughSurface :
164
- assert np . allclose (pset .lon [0 ], 0.6 )
165
- assert np . allclose (pset .depth [0 ], 0 )
165
+ assert_allclose (pset .lon [0 ], 0.6 )
166
+ assert_allclose (pset .depth [0 ], 0 )
166
167
else :
167
168
assert len (pset ) == 0
168
169
@@ -225,7 +226,7 @@ def test_conversion_3DCROCO():
225
226
for zi , z in enumerate (z_xroms ):
226
227
sigma [zi ] = _croco_from_z_to_sigma_scipy (fieldset , 0 , z , lat , lon , None )
227
228
228
- assert np . allclose (sigma , s_xroms , atol = 1e-3 )
229
+ assert_allclose (sigma , s_xroms , atol = 1e-3 )
229
230
230
231
231
232
def test_advection_3DCROCO ():
@@ -242,8 +243,8 @@ def SampleW(particle, fieldset, time): # pragma: no cover
242
243
particle .w = fieldset .W [time , particle .depth , particle .lat , particle .lon ]
243
244
244
245
pset .execute ([AdvectionRK4_3D , SampleW ], runtime = runtime , dt = 100 )
245
- assert np . allclose (pset .depth , Z .flatten (), atol = 5 ) # TODO lower this atol
246
- assert np . allclose (pset .lon_nextloop , [x + runtime for x in X .flatten ()], atol = 1e-3 )
246
+ assert_allclose (pset .depth , Z .flatten (), atol = 5 ) # TODO lower this atol
247
+ assert_allclose (pset .lon_nextloop , [x + runtime for x in X .flatten ()], atol = 1e-3 )
247
248
248
249
249
250
def test_advection_2DCROCO ():
@@ -256,8 +257,8 @@ def test_advection_2DCROCO():
256
257
pset = ParticleSet (fieldset = fieldset , pclass = Particle , lon = X , lat = Y , depth = Z )
257
258
258
259
pset .execute ([AdvectionRK4 ], runtime = runtime , dt = 100 )
259
- assert np . allclose (pset .depth , Z .flatten (), atol = 1e-3 )
260
- assert np . allclose (pset .lon_nextloop , [x + runtime for x in X ], atol = 1e-3 )
260
+ assert_allclose (pset .depth , Z .flatten (), atol = 1e-3 )
261
+ assert_allclose (pset .lon_nextloop , [x + runtime for x in X ], atol = 1e-3 )
261
262
262
263
263
264
def create_periodic_fieldset (xdim , ydim , uvel , vvel ):
@@ -309,8 +310,8 @@ def test_advection_periodic_zonal_meridional():
309
310
fieldset .add_periodic_halo (zonal = True , meridional = True )
310
311
assert len (fieldset .U .lat ) == ydim + 10 # default halo size is 5 grid points
311
312
assert len (fieldset .U .lon ) == xdim + 10 # default halo size is 5 grid points
312
- assert np . allclose (np .diff (fieldset .U .lat ), fieldset .U .lat [1 ] - fieldset .U .lat [0 ], rtol = 0.001 )
313
- assert np . allclose (np .diff (fieldset .U .lon ), fieldset .U .lon [1 ] - fieldset .U .lon [0 ], rtol = 0.001 )
313
+ assert_allclose (np .diff (fieldset .U .lat ), fieldset .U .lat [1 ] - fieldset .U .lat [0 ], rtol = 0.001 )
314
+ assert_allclose (np .diff (fieldset .U .lon ), fieldset .U .lon [1 ] - fieldset .U .lon [0 ], rtol = 0.001 )
314
315
315
316
pset = ParticleSet (fieldset , pclass = Particle , lon = [0.4 ], lat = [0.5 ])
316
317
pset .execute (AdvectionRK4 + pset .Kernel (periodicBC ), runtime = timedelta (hours = 20 ), dt = timedelta (seconds = 30 ))
@@ -425,8 +426,8 @@ def test_stationary_eddy(fieldset_stationary, method, rtol, diffField):
425
426
426
427
exp_lon = [truth_stationary (x , y , pset [0 ].time )[0 ] for x , y in zip (lon , lat , strict = True )]
427
428
exp_lat = [truth_stationary (x , y , pset [0 ].time )[1 ] for x , y in zip (lon , lat , strict = True )]
428
- assert np . allclose (pset .lon , exp_lon , rtol = rtol )
429
- assert np . allclose (pset .lat , exp_lat , rtol = rtol )
429
+ assert_allclose (pset .lon , exp_lon , rtol = rtol )
430
+ assert_allclose (pset .lat , exp_lat , rtol = rtol )
430
431
431
432
432
433
def test_stationary_eddy_vertical ():
@@ -454,9 +455,9 @@ def test_stationary_eddy_vertical():
454
455
exp_lon = [truth_stationary (x , z , pset [0 ].time )[0 ] for x , z in zip (lon , depth , strict = True )]
455
456
exp_depth = [truth_stationary (x , z , pset [0 ].time )[1 ] for x , z in zip (lon , depth , strict = True )]
456
457
print (pset , exp_lon )
457
- assert np . allclose (pset .lon , exp_lon , rtol = 1e-5 )
458
- assert np . allclose (pset .lat , lat , rtol = 1e-5 )
459
- assert np . allclose (pset .depth , exp_depth , rtol = 1e-5 )
458
+ assert_allclose (pset .lon , exp_lon , rtol = 1e-5 )
459
+ assert_allclose (pset .lat , lat , rtol = 1e-5 )
460
+ assert_allclose (pset .depth , exp_depth , rtol = 1e-5 )
460
461
461
462
data = {"U" : fldzero , "V" : fld2 , "W" : fld1 }
462
463
fieldset = FieldSet .from_data (data , dimensions , mesh = "flat" )
@@ -465,9 +466,9 @@ def test_stationary_eddy_vertical():
465
466
pset .execute (AdvectionRK4_3D , dt = dt , endtime = endtime )
466
467
exp_depth = [truth_stationary (z , y , pset [0 ].time )[0 ] for z , y in zip (depth , lat , strict = True )]
467
468
exp_lat = [truth_stationary (z , y , pset [0 ].time )[1 ] for z , y in zip (depth , lat , strict = True )]
468
- assert np . allclose (pset .lon , lon , rtol = 1e-5 )
469
- assert np . allclose (pset .lat , exp_lat , rtol = 1e-5 )
470
- assert np . allclose (pset .depth , exp_depth , rtol = 1e-5 )
469
+ assert_allclose (pset .lon , lon , rtol = 1e-5 )
470
+ assert_allclose (pset .lat , exp_lat , rtol = 1e-5 )
471
+ assert_allclose (pset .depth , exp_depth , rtol = 1e-5 )
471
472
472
473
473
474
def truth_moving (x_0 , y_0 , t ):
@@ -530,8 +531,8 @@ def test_moving_eddy(fieldset_moving, method, rtol, diffField):
530
531
531
532
exp_lon = [truth_moving (x , y , t )[0 ] for x , y , t in zip (lon , lat , pset .time , strict = True )]
532
533
exp_lat = [truth_moving (x , y , t )[1 ] for x , y , t in zip (lon , lat , pset .time , strict = True )]
533
- assert np . allclose (pset .lon , exp_lon , rtol = rtol )
534
- assert np . allclose (pset .lat , exp_lat , rtol = rtol )
534
+ assert_allclose (pset .lon , exp_lon , rtol = rtol )
535
+ assert_allclose (pset .lat , exp_lat , rtol = rtol )
535
536
536
537
537
538
def truth_decaying (x_0 , y_0 , t ):
@@ -613,8 +614,8 @@ def test_decaying_eddy(fieldset_decaying, method, rtol, diffField):
613
614
614
615
exp_lon = [truth_decaying (x , y , t )[0 ] for x , y , t in zip (lon , lat , pset .time , strict = True )]
615
616
exp_lat = [truth_decaying (x , y , t )[1 ] for x , y , t in zip (lon , lat , pset .time , strict = True )]
616
- assert np . allclose (pset .lon , exp_lon , rtol = rtol )
617
- assert np . allclose (pset .lat , exp_lat , rtol = rtol )
617
+ assert_allclose (pset .lon , exp_lon , rtol = rtol )
618
+ assert_allclose (pset .lat , exp_lat , rtol = rtol )
618
619
619
620
620
621
def test_analyticalAgrid ():
@@ -663,6 +664,6 @@ def test_uniform_analytical(u, v, w, direction, tmp_zarrfile):
663
664
ds = xr .open_zarr (tmp_zarrfile )
664
665
times = (direction * ds ["time" ][:]).values .astype ("timedelta64[s]" )[0 ]
665
666
timeref = np .arange (1 , 5 ).astype ("timedelta64[s]" )
666
- assert np . allclose (times , timeref , atol = np .timedelta64 (1 , "ms" ))
667
+ assert_allclose (times , timeref , atol = np .timedelta64 (1 , "ms" ))
667
668
lons = ds ["lon" ][:].values
668
- assert np . allclose (lons , x0 + direction * u * np .arange (1 , 5 ))
669
+ assert_allclose (lons , x0 + direction * u * np .arange (1 , 5 ))
0 commit comments