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
@@ -227,7 +228,7 @@ def test_conversion_3DCROCO():
227
228
for zi , z in enumerate (z_xroms ):
228
229
sigma [zi ] = _croco_from_z_to_sigma_scipy (fieldset , 0 , z , lat , lon , None )
229
230
230
- assert np . allclose (sigma , s_xroms , atol = 1e-3 )
231
+ assert_allclose (sigma , s_xroms , atol = 1e-3 )
231
232
232
233
233
234
@pytest .mark .v4alpha
@@ -246,8 +247,8 @@ def SampleW(particle, fieldset, time): # pragma: no cover
246
247
particle .w = fieldset .W [time , particle .depth , particle .lat , particle .lon ]
247
248
248
249
pset .execute ([AdvectionRK4_3D , SampleW ], runtime = runtime , dt = 100 )
249
- assert np . allclose (pset .depth , Z .flatten (), atol = 5 ) # TODO lower this atol
250
- assert np . allclose (pset .lon_nextloop , [x + runtime for x in X .flatten ()], atol = 1e-3 )
250
+ assert_allclose (pset .depth , Z .flatten (), atol = 5 ) # TODO lower this atol
251
+ assert_allclose (pset .lon_nextloop , [x + runtime for x in X .flatten ()], atol = 1e-3 )
251
252
252
253
253
254
@pytest .mark .v4alpha
@@ -262,8 +263,8 @@ def test_advection_2DCROCO():
262
263
pset = ParticleSet (fieldset = fieldset , pclass = Particle , lon = X , lat = Y , depth = Z )
263
264
264
265
pset .execute ([AdvectionRK4 ], runtime = runtime , dt = 100 )
265
- assert np . allclose (pset .depth , Z .flatten (), atol = 1e-3 )
266
- assert np . allclose (pset .lon_nextloop , [x + runtime for x in X ], atol = 1e-3 )
266
+ assert_allclose (pset .depth , Z .flatten (), atol = 1e-3 )
267
+ assert_allclose (pset .lon_nextloop , [x + runtime for x in X ], atol = 1e-3 )
267
268
268
269
269
270
def create_periodic_fieldset (xdim , ydim , uvel , vvel ):
@@ -315,8 +316,8 @@ def test_advection_periodic_zonal_meridional():
315
316
fieldset .add_periodic_halo (zonal = True , meridional = True )
316
317
assert len (fieldset .U .lat ) == ydim + 10 # default halo size is 5 grid points
317
318
assert len (fieldset .U .lon ) == xdim + 10 # default halo size is 5 grid points
318
- assert np . allclose (np .diff (fieldset .U .lat ), fieldset .U .lat [1 ] - fieldset .U .lat [0 ], rtol = 0.001 )
319
- assert np . allclose (np .diff (fieldset .U .lon ), fieldset .U .lon [1 ] - fieldset .U .lon [0 ], rtol = 0.001 )
319
+ assert_allclose (np .diff (fieldset .U .lat ), fieldset .U .lat [1 ] - fieldset .U .lat [0 ], rtol = 0.001 )
320
+ assert_allclose (np .diff (fieldset .U .lon ), fieldset .U .lon [1 ] - fieldset .U .lon [0 ], rtol = 0.001 )
320
321
321
322
pset = ParticleSet (fieldset , pclass = Particle , lon = [0.4 ], lat = [0.5 ])
322
323
pset .execute (AdvectionRK4 + pset .Kernel (periodicBC ), runtime = timedelta (hours = 20 ), dt = timedelta (seconds = 30 ))
@@ -431,8 +432,8 @@ def test_stationary_eddy(fieldset_stationary, method, rtol, diffField):
431
432
432
433
exp_lon = [truth_stationary (x , y , pset [0 ].time )[0 ] for x , y in zip (lon , lat , strict = True )]
433
434
exp_lat = [truth_stationary (x , y , pset [0 ].time )[1 ] for x , y in zip (lon , lat , strict = True )]
434
- assert np . allclose (pset .lon , exp_lon , rtol = rtol )
435
- assert np . allclose (pset .lat , exp_lat , rtol = rtol )
435
+ assert_allclose (pset .lon , exp_lon , rtol = rtol )
436
+ assert_allclose (pset .lat , exp_lat , rtol = rtol )
436
437
437
438
438
439
def test_stationary_eddy_vertical ():
@@ -460,9 +461,9 @@ def test_stationary_eddy_vertical():
460
461
exp_lon = [truth_stationary (x , z , pset [0 ].time )[0 ] for x , z in zip (lon , depth , strict = True )]
461
462
exp_depth = [truth_stationary (x , z , pset [0 ].time )[1 ] for x , z in zip (lon , depth , strict = True )]
462
463
print (pset , exp_lon )
463
- assert np . allclose (pset .lon , exp_lon , rtol = 1e-5 )
464
- assert np . allclose (pset .lat , lat , rtol = 1e-5 )
465
- assert np . allclose (pset .depth , exp_depth , rtol = 1e-5 )
464
+ assert_allclose (pset .lon , exp_lon , rtol = 1e-5 )
465
+ assert_allclose (pset .lat , lat , rtol = 1e-5 )
466
+ assert_allclose (pset .depth , exp_depth , rtol = 1e-5 )
466
467
467
468
data = {"U" : fldzero , "V" : fld2 , "W" : fld1 }
468
469
fieldset = FieldSet .from_data (data , dimensions , mesh = "flat" )
@@ -471,9 +472,9 @@ def test_stationary_eddy_vertical():
471
472
pset .execute (AdvectionRK4_3D , dt = dt , endtime = endtime )
472
473
exp_depth = [truth_stationary (z , y , pset [0 ].time )[0 ] for z , y in zip (depth , lat , strict = True )]
473
474
exp_lat = [truth_stationary (z , y , pset [0 ].time )[1 ] for z , y in zip (depth , lat , strict = True )]
474
- assert np . allclose (pset .lon , lon , rtol = 1e-5 )
475
- assert np . allclose (pset .lat , exp_lat , rtol = 1e-5 )
476
- assert np . allclose (pset .depth , exp_depth , rtol = 1e-5 )
475
+ assert_allclose (pset .lon , lon , rtol = 1e-5 )
476
+ assert_allclose (pset .lat , exp_lat , rtol = 1e-5 )
477
+ assert_allclose (pset .depth , exp_depth , rtol = 1e-5 )
477
478
478
479
479
480
def truth_moving (x_0 , y_0 , t ):
@@ -536,8 +537,8 @@ def test_moving_eddy(fieldset_moving, method, rtol, diffField):
536
537
537
538
exp_lon = [truth_moving (x , y , t )[0 ] for x , y , t in zip (lon , lat , pset .time , strict = True )]
538
539
exp_lat = [truth_moving (x , y , t )[1 ] for x , y , t in zip (lon , lat , pset .time , strict = True )]
539
- assert np . allclose (pset .lon , exp_lon , rtol = rtol )
540
- assert np . allclose (pset .lat , exp_lat , rtol = rtol )
540
+ assert_allclose (pset .lon , exp_lon , rtol = rtol )
541
+ assert_allclose (pset .lat , exp_lat , rtol = rtol )
541
542
542
543
543
544
def truth_decaying (x_0 , y_0 , t ):
@@ -619,8 +620,8 @@ def test_decaying_eddy(fieldset_decaying, method, rtol, diffField):
619
620
620
621
exp_lon = [truth_decaying (x , y , t )[0 ] for x , y , t in zip (lon , lat , pset .time , strict = True )]
621
622
exp_lat = [truth_decaying (x , y , t )[1 ] for x , y , t in zip (lon , lat , pset .time , strict = True )]
622
- assert np . allclose (pset .lon , exp_lon , rtol = rtol )
623
- assert np . allclose (pset .lat , exp_lat , rtol = rtol )
623
+ assert_allclose (pset .lon , exp_lon , rtol = rtol )
624
+ assert_allclose (pset .lat , exp_lat , rtol = rtol )
624
625
625
626
626
627
def test_analyticalAgrid ():
@@ -669,6 +670,6 @@ def test_uniform_analytical(u, v, w, direction, tmp_zarrfile):
669
670
ds = xr .open_zarr (tmp_zarrfile )
670
671
times = (direction * ds ["time" ][:]).values .astype ("timedelta64[s]" )[0 ]
671
672
timeref = np .arange (1 , 5 ).astype ("timedelta64[s]" )
672
- assert np . allclose (times , timeref , atol = np .timedelta64 (1 , "ms" ))
673
+ assert_allclose (times , timeref , atol = np .timedelta64 (1 , "ms" ))
673
674
lons = ds ["lon" ][:].values
674
- assert np . allclose (lons , x0 + direction * u * np .arange (1 , 5 ))
675
+ assert_allclose (lons , x0 + direction * u * np .arange (1 , 5 ))
0 commit comments