Skip to content

Commit 49a0a82

Browse files
Making unit tests of zonal and meridional flow stricter
Following the discussion around zonal and meridional spherical conversion in #2455
1 parent 1e17a46 commit 49a0a82

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/parcels/_datasets/structured/generated.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
def simple_UV_dataset(dims=(360, 2, 30, 4), maxdepth=1, mesh="spherical"):
1616
max_lon = 180.0 if mesh == "spherical" else 1e6
17+
max_lat = 90.0 if mesh == "spherical" else 1e6
1718

1819
return xr.Dataset(
1920
{"U": (["time", "depth", "YG", "XG"], np.zeros(dims)), "V": (["time", "depth", "YG", "XG"], np.zeros(dims))},
@@ -24,7 +25,7 @@ def simple_UV_dataset(dims=(360, 2, 30, 4), maxdepth=1, mesh="spherical"):
2425
"YG": (["YG"], np.arange(dims[2]), {"axis": "Y", "c_grid_axis_shift": -0.5}),
2526
"XC": (["XC"], np.arange(dims[3]) + 0.5, {"axis": "X"}),
2627
"XG": (["XG"], np.arange(dims[3]), {"axis": "X", "c_grid_axis_shift": -0.5}),
27-
"lat": (["YG"], np.linspace(-90, 90, dims[2]), {"axis": "Y", "c_grid_axis_shift": 0.5}),
28+
"lat": (["YG"], np.linspace(-max_lat, max_lat, dims[2]), {"axis": "Y", "c_grid_axis_shift": 0.5}),
2829
"lon": (["XG"], np.linspace(-max_lon, max_lon, dims[3]), {"axis": "X", "c_grid_axis_shift": -0.5}),
2930
},
3031
).pipe(

tests/test_advection.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,27 @@
3030

3131
@pytest.mark.parametrize("mesh", ["spherical", "flat"])
3232
def test_advection_zonal(mesh, npart=10):
33-
"""Particles at high latitude move geographically faster due to the pole correction in `GeographicPolar`."""
33+
"""Particles at high latitude move geographically faster due to the pole correction."""
3434
ds = simple_UV_dataset(mesh=mesh)
3535
ds["U"].data[:] = 1.0
3636
fieldset = FieldSet.from_sgrid_conventions(ds, mesh=mesh)
3737

38-
pset = ParticleSet(fieldset, lon=np.zeros(npart) + 20.0, lat=np.linspace(0, 80, npart))
39-
pset.execute(AdvectionRK4, runtime=np.timedelta64(2, "h"), dt=np.timedelta64(15, "m"))
38+
runtime = 7200
39+
startlat = np.linspace(0, 80, npart)
40+
startlon = 20.0 + np.zeros(npart)
41+
pset = ParticleSet(fieldset, lon=startlon, lat=startlat)
42+
pset.execute(AdvectionRK4, runtime=runtime, dt=np.timedelta64(15, "m"))
4043

44+
expected_dlon = runtime
4145
if mesh == "spherical":
42-
assert (np.diff(pset.lon) > 1.0e-4).all()
43-
else:
44-
assert (np.diff(pset.lon) < 1.0e-4).all()
46+
expected_dlon /= 1852 * 60 * np.cos(np.deg2rad(pset.lat))
47+
48+
np.testing.assert_allclose(pset.lon - startlon, expected_dlon, atol=1e-5)
49+
np.testing.assert_allclose(pset.lat, startlat, atol=1e-5)
4550

4651

4752
def test_advection_zonal_with_particlefile(tmp_store):
48-
"""Particles at high latitude move geographically faster due to the pole correction in `GeographicPolar`."""
53+
"""Particles at high latitude move geographically faster due to the pole correction."""
4954
npart = 10
5055
ds = simple_UV_dataset(mesh="flat")
5156
ds["U"].data[:] = 1.0
@@ -88,6 +93,27 @@ def test_advection_zonal_periodic():
8893
np.testing.assert_allclose(pset.lat, 0.5, atol=1e-5)
8994

9095

96+
@pytest.mark.parametrize("mesh", ["spherical", "flat"])
97+
def test_advection_meridional(mesh, npart=10):
98+
"""All particles move the same in meridional direction, regardless of latitude."""
99+
ds = simple_UV_dataset(mesh=mesh)
100+
ds["V"].data[:] = 1.0
101+
fieldset = FieldSet.from_sgrid_conventions(ds, mesh=mesh)
102+
103+
runtime = 7200
104+
startlat = np.linspace(0, 80, npart)
105+
startlon = 20.0 + np.zeros(npart)
106+
pset = ParticleSet(fieldset, lon=startlon, lat=startlat)
107+
pset.execute(AdvectionRK4, runtime=runtime, dt=np.timedelta64(15, "m"))
108+
109+
expected_dlat = runtime
110+
if mesh == "spherical":
111+
expected_dlat /= 1852 * 60
112+
113+
np.testing.assert_allclose(pset.lon, startlon, atol=1e-5)
114+
np.testing.assert_allclose(pset.lat - startlat, expected_dlat, atol=1e-4)
115+
116+
91117
def test_horizontal_advection_in_3D_flow(npart=10):
92118
"""Flat 2D zonal flow that increases linearly with z from 0 m/s to 1 m/s."""
93119
ds = simple_UV_dataset(mesh="flat")

0 commit comments

Comments
 (0)