22import pytest
33
44from parcels import FieldSet , ParticleSet , SphericalMesh
5+ from parcels ._core .mesh import EARTH_RADIUS
56from parcels ._datasets .structured .generated import simple_UV_dataset
67from parcels .kernels import AdvectionRK4
78
8- EARTH_DEG2M = 1852 * 60.0
9-
9+ EARTH_DEG2M = EARTH_RADIUS * np .pi / 180
1010
1111def test_spherical_mesh_deg2m ():
12- assert SphericalMesh ().radius is None
12+ assert SphericalMesh ().radius == EARTH_RADIUS
1313 assert SphericalMesh ().deg2m == EARTH_DEG2M
1414 r = 3389500.0 # Mars radius
1515 assert SphericalMesh (radius = r ).deg2m == pytest .approx (r * np .pi / 180 )
1616
17-
1817@pytest .mark .parametrize (
1918 "mesh, exp_radius, exp_deg2m" ,
2019 [
21- ("spherical" , None , EARTH_DEG2M ),
22- (SphericalMesh (), None , EARTH_DEG2M ),
20+ ("spherical" , EARTH_RADIUS , EARTH_DEG2M ),
21+ (SphericalMesh (), EARTH_RADIUS , EARTH_DEG2M ),
2322 (SphericalMesh (radius = 3389500.0 ), 3389500.0 , 3389500.0 * np .pi / 180 ),
2423 ],
2524)
@@ -29,31 +28,49 @@ def test_xgrid_radius_and_deg2m(mesh, exp_radius, exp_deg2m):
2928 assert grid ._radius == exp_radius
3029 assert grid .deg2m == pytest .approx (exp_deg2m )
3130
32-
33- @pytest .mark .parametrize ("radius" , [None , 3389500.0 , 6051800.0 , 6371000.0 ]) # Mars, Venus, Earth
34- def test_advection_uses_custom_radius (radius , npart = 10 ):
31+ @pytest .mark .parametrize ("mesh, deg2m" ,
32+ [
33+ (SphericalMesh (), EARTH_DEG2M ), # No radius entered
34+ (SphericalMesh (radius = 3389500.0 ), 3389500.0 * np .pi / 180 ), # Mars
35+ (SphericalMesh (radius = 6051800.0 ), 6051800.0 * np .pi / 180 ), # Venus
36+ (SphericalMesh (radius = EARTH_RADIUS ), EARTH_DEG2M ), # Explicit Earth
37+ ],
38+ )
39+ def test_advection_uses_custom_radius (mesh , deg2m , npart = 10 ):
3540 ds = simple_UV_dataset ()
3641 ds ["U" ].data [:] = 1.0
37- fieldset = FieldSet .from_sgrid_conventions (ds , mesh = SphericalMesh ( radius = radius ) )
42+ fieldset = FieldSet .from_sgrid_conventions (ds , mesh = mesh )
3843
3944 runtime = 7200
4045 startlat = np .linspace (0 , 80 , npart )
4146 startlon = 20.0 + np .zeros (npart )
4247 pset = ParticleSet (fieldset , x = startlon , y = startlat )
4348 pset .execute (AdvectionRK4 , runtime = runtime , dt = np .timedelta64 (15 , "m" ))
4449
45- deg2m = EARTH_DEG2M if radius is None else radius * np .pi / 180
4650 expected_dlon = runtime / (deg2m * np .cos (np .deg2rad (pset .y )))
4751 np .testing .assert_allclose (pset .x - startlon , expected_dlon , atol = 1e-5 )
4852 np .testing .assert_allclose (pset .y , startlat , atol = 1e-5 )
4953
54+ def test_advection_flat_mesh (npart = 10 ):
55+ ds = simple_UV_dataset (mesh = "flat" )
56+ ds ["U" ].data [:] = 1.0
57+ fieldset = FieldSet .from_sgrid_conventions (ds , mesh = "flat" )
58+
59+ runtime = 7200
60+ startlat = np .linspace (0 , 80 , npart )
61+ startlon = 20.0 + np .zeros (npart )
62+ pset = ParticleSet (fieldset , x = startlon , y = startlat )
63+ pset .execute (AdvectionRK4 , runtime = runtime , dt = np .timedelta64 (15 , "m" ))
64+
65+ assert fieldset .U .grid .deg2m == 1.0 # flat mesh deg2m
66+ np .testing .assert_allclose (pset .x - startlon , runtime , atol = 1e-5 )
67+ np .testing .assert_allclose (pset .y , startlat , atol = 1e-5 )
5068
5169@pytest .mark .parametrize ("bad_radius" , ["6371000" , [6371000 ], (1 , 2 ), {}])
5270def test_spherical_mesh_rejects_non_numeric_radius (bad_radius ):
5371 with pytest .raises (TypeError ):
5472 SphericalMesh (radius = bad_radius )
5573
56-
5774@pytest .mark .parametrize ("bad_radius" , [0 , - 1.0 , - 6371000 ])
5875def test_spherical_mesh_rejects_nonpos_radius (bad_radius ):
5976 with pytest .raises (ValueError ):
0 commit comments