diff --git a/tests/test_data/test_interpolation_data_random_cgrid_velocity.nc b/tests/test_data/test_interpolation_data_random_cgrid_velocity.nc new file mode 100644 index 0000000000..947d1d343c Binary files /dev/null and b/tests/test_data/test_interpolation_data_random_cgrid_velocity.nc differ diff --git a/tests/test_data/test_interpolation_data_random_freeslip.nc b/tests/test_data/test_interpolation_data_random_freeslip.nc new file mode 100644 index 0000000000..b8746f6814 Binary files /dev/null and b/tests/test_data/test_interpolation_data_random_freeslip.nc differ diff --git a/tests/test_data/test_interpolation_data_random_linear.nc b/tests/test_data/test_interpolation_data_random_linear.nc new file mode 100644 index 0000000000..947d1d343c Binary files /dev/null and b/tests/test_data/test_interpolation_data_random_linear.nc differ diff --git a/tests/test_data/test_interpolation_data_random_nearest.nc b/tests/test_data/test_interpolation_data_random_nearest.nc new file mode 100644 index 0000000000..947d1d343c Binary files /dev/null and b/tests/test_data/test_interpolation_data_random_nearest.nc differ diff --git a/tests/test_data/test_interpolation_jit_cgrid_velocity.zarr/.zattrs b/tests/test_data/test_interpolation_jit_cgrid_velocity.zarr/.zattrs new file mode 100644 index 0000000000..b750e26cfb --- /dev/null +++ b/tests/test_data/test_interpolation_jit_cgrid_velocity.zarr/.zattrs @@ -0,0 +1,8 @@ +{ + "Conventions": "CF-1.6/CF-1.7", + "feature_type": "trajectory", + "ncei_template_version": "NCEI_NetCDF_Trajectory_Template_v2.0", + "parcels_kernels": "JITParticleAdvectionRK4_3DDeleteParticle", + "parcels_mesh": "flat", + "parcels_version": "v3.1.2-12-gee4dd32a" +} diff --git a/tests/test_data/test_interpolation_jit_cgrid_velocity.zarr/.zgroup b/tests/test_data/test_interpolation_jit_cgrid_velocity.zarr/.zgroup new file mode 100644 index 0000000000..3f3fad2d17 --- /dev/null +++ b/tests/test_data/test_interpolation_jit_cgrid_velocity.zarr/.zgroup @@ -0,0 +1,3 @@ +{ + "zarr_format": 2 +} diff --git a/tests/test_data/test_interpolation_jit_cgrid_velocity.zarr/.zmetadata b/tests/test_data/test_interpolation_jit_cgrid_velocity.zarr/.zmetadata new file mode 100644 index 0000000000..4534d4476d --- /dev/null +++ b/tests/test_data/test_interpolation_jit_cgrid_velocity.zarr/.zmetadata @@ -0,0 +1,194 @@ +{ + "metadata": { + ".zattrs": { + "Conventions": "CF-1.6/CF-1.7", + "feature_type": "trajectory", + "ncei_template_version": "NCEI_NetCDF_Trajectory_Template_v2.0", + "parcels_kernels": "JITParticleAdvectionRK4_3DDeleteParticle", + "parcels_mesh": "flat", + "parcels_version": "v3.1.2-12-gee4dd32a" + }, + ".zgroup": { + "zarr_format": 2 + }, + "lat/.zarray": { + "chunks": [ + 455, + 1 + ], + "compressor": { + "blocksize": 0, + "clevel": 5, + "cname": "lz4", + "id": "blosc", + "shuffle": 1 + }, + "dtype": " xr.Dataset: - tdim, zdim, ydim, xdim = 20, 5, 10, 10 - ds = xr.Dataset( - { - "U": (("time", "depth", "lat", "lon"), np.random.random((tdim, zdim, ydim, xdim)) / 1e3), - "V": (("time", "depth", "lat", "lon"), np.random.random((tdim, zdim, ydim, xdim)) / 1e3), - "W": (("time", "depth", "lat", "lon"), np.random.random((tdim, zdim, ydim, xdim)) / 1e3), - }, - coords={ - "time": np.linspace(0, tdim - 1, tdim), - "depth": np.linspace(0, 1, zdim), - "lat": np.linspace(0, 1, ydim), - "lon": np.linspace(0, 1, xdim), - }, - ) - # Set a land point (for testing freeslip) - if with_land_point: - ds["U"][:, :, 2, 5] = 0.0 - ds["V"][:, :, 2, 5] = 0.0 - ds["W"][:, :, 2, 5] = 0.0 - - return ds - - @pytest.fixture def data_2d(): """2D slice of the reference data at depth=0.""" @@ -126,3 +103,48 @@ def test_interpolator2(ctx: interpolation.InterpolationContext3D): return 0 fieldset.U[0.5, 0.5, 0.5, 0.5] + + +@pytest.mark.parametrize( + "interp_method", + [ + "linear", + "freeslip", + "nearest", + "cgrid_velocity", + ], +) +def test_interp_regression_v3(interp_method): + """Test that the v4 versions of the interpolation are the same as the v3 versions.""" + variables = {"U": "U", "V": "V", "W": "W"} + dimensions = {"time": "time", "lon": "lon", "lat": "lat", "depth": "depth"} + ds = xr.open_dataset(str(TEST_DATA / f"test_interpolation_data_random_{interp_method}.nc")) + fieldset = FieldSet.from_xarray_dataset( + ds, + variables, + dimensions, + mesh="flat", + ) + + for field in [fieldset.U, fieldset.V, fieldset.W]: # Set a land point (for testing freeslip) + field.interp_method = interp_method + + x, y, z = np.meshgrid(np.linspace(0, 1, 7), np.linspace(0, 1, 13), np.linspace(0, 1, 5)) + + TestP = ScipyParticle.add_variable("pid", dtype=np.int32, initial=0) + pset = ParticleSet(fieldset, pclass=TestP, lon=x, lat=y, depth=z, pid=np.arange(x.size)) + + def DeleteParticle(particle, fieldset, time): + if particle.state >= 50: + particle.delete() + + outfile = pset.ParticleFile(f"test_interpolation_v4_{interp_method}", outputdt=1) + pset.execute([AdvectionRK4_3D, DeleteParticle], runtime=4, dt=1, output_file=outfile) + + ds_v3 = xr.open_zarr(str(TEST_DATA / f"test_interpolation_jit_{interp_method}.zarr")) + ds_v4 = xr.open_zarr(f"test_interpolation_v4_{interp_method}.zarr") + + tol = 1e-6 + np.testing.assert_allclose(ds_v3.lon, ds_v4.lon, atol=tol) + np.testing.assert_allclose(ds_v3.lat, ds_v4.lat, atol=tol) + np.testing.assert_allclose(ds_v3.z, ds_v4.z, atol=tol)