From 55dada95017b89c1d889b87c9e98dfcb18754c32 Mon Sep 17 00:00:00 2001 From: Michael Denes Date: Fri, 31 Oct 2025 10:33:19 +0100 Subject: [PATCH 1/4] Adding test for PolyTEOS kernel --- tests/test_kernels.py | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/test_kernels.py b/tests/test_kernels.py index 0cf758c..320ddab 100644 --- a/tests/test_kernels.py +++ b/tests/test_kernels.py @@ -243,3 +243,50 @@ def test_mixing(): # Assert that the particles move from their initial location assert (np.sum(np.abs(pset.lon - pset_mixing.lon)) > 0.) & (np.sum(np.abs(pset.lat - pset_mixing.lat)) > 0.) + +@pytest.mark.parametrize("mode", ["scipy", "jit"]) +def test_TEOSdensity_kernels(mode): + """ Adapted test from Parcels v3 codebase. + """ + settings_file = 'tests/test_data/test_settings.json' + settings = pp.utils.load_settings(settings_file) + + settings['simulation'] = make_standard_simulation_settings() + settings['plastictype'] = make_standard_plastictype_settings() + + # Turn off all other processes + settings['use_3D'] = False + settings['use_biofouling'] = False + settings['use_stokes'] = False + settings['use_wind'] = False + settings['use_mixing'] = False + + def generate_fieldset(xdim=2, ydim=2, zdim=2, tdim=1): + lon = np.linspace(0.0, 10.0, xdim, dtype=np.float32) + lat = np.linspace(0.0, 10.0, ydim, dtype=np.float32) + depth = np.linspace(0, 2000, zdim, dtype=np.float32) + time = np.zeros(tdim, dtype=np.float64) + U = np.ones((tdim, zdim, ydim, xdim)) + V = np.ones((tdim, zdim, ydim, xdim)) + abs_salinity = 30 * np.ones((tdim, zdim, ydim, xdim)) + cons_temperature = 10 * np.ones((tdim, zdim, ydim, xdim)) + dimensions = {"lat": lat, "lon": lon, "depth": depth, "time": time} + data = { + "U": np.array(U, dtype=np.float32), + "V": np.array(V, dtype=np.float32), + "absolute_salinity": np.array(abs_salinity, dtype=np.float32), + "conservative_temperature": np.array(cons_temperature, dtype=np.float32), + } + return (data, dimensions) + + data, dimensions = generate_fieldset() + fieldset = parcels.FieldSet.from_data(data, dimensions) + + release_locations = {'lons': [5], 'lats': [5], + 'plastic_amount': [1]} + + pset = pp.constructors.create_particleset(fieldset, settings, release_locations) + + pset.execute(pp.kernels.PolyTEOS10_bsq, runtime=1) + + assert np.allclose(pset[0].seawater_density, 1027.45140) From c17943bbd9b5f6fb63b6b838fae7ba665d3aca71 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 31 Oct 2025 09:33:47 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_kernels.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_kernels.py b/tests/test_kernels.py index 320ddab..96bab66 100644 --- a/tests/test_kernels.py +++ b/tests/test_kernels.py @@ -284,7 +284,7 @@ def generate_fieldset(xdim=2, ydim=2, zdim=2, tdim=1): release_locations = {'lons': [5], 'lats': [5], 'plastic_amount': [1]} - + pset = pp.constructors.create_particleset(fieldset, settings, release_locations) pset.execute(pp.kernels.PolyTEOS10_bsq, runtime=1) From cf064b4c147bddf1f36f54eed6f6ac98b23f9525 Mon Sep 17 00:00:00 2001 From: Michael Denes Date: Fri, 31 Oct 2025 10:52:15 +0100 Subject: [PATCH 3/4] Particle should be at 1000m depth for unit test --- tests/test_kernels.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_kernels.py b/tests/test_kernels.py index 96bab66..89b644f 100644 --- a/tests/test_kernels.py +++ b/tests/test_kernels.py @@ -282,7 +282,7 @@ def generate_fieldset(xdim=2, ydim=2, zdim=2, tdim=1): data, dimensions = generate_fieldset() fieldset = parcels.FieldSet.from_data(data, dimensions) - release_locations = {'lons': [5], 'lats': [5], + release_locations = {'lons': [5], 'lats': [5], 'depths': [1000], 'plastic_amount': [1]} pset = pp.constructors.create_particleset(fieldset, settings, release_locations) From 8f73e8781a0da376b6c81f16622370cca772b5d2 Mon Sep 17 00:00:00 2001 From: Michael Denes Date: Fri, 31 Oct 2025 11:29:08 +0100 Subject: [PATCH 4/4] Depths was missing from the `create_particleset` function, necessary for testing the PolyTEOS kernel (and for flexibility) --- plasticparcels/constructors.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plasticparcels/constructors.py b/plasticparcels/constructors.py index 62dfa7b..04cd5e7 100644 --- a/plasticparcels/constructors.py +++ b/plasticparcels/constructors.py @@ -232,6 +232,11 @@ def create_particleset(fieldset, settings, release_locations): else: plastic_amounts = np.full_like(lons, np.nan) + if 'depths' in release_locations.keys(): + depths = release_locations['depths'] + else: + depths = np.full_like(lons, 0.) + # Set particle properties plastic_densities = np.full(lons.shape, settings['plastictype']['plastic_density']) plastic_diameters = np.full(lons.shape, settings['plastictype']['plastic_diameter']) @@ -254,6 +259,7 @@ def create_particleset(fieldset, settings, release_locations): PlasticParticle, lon=lons, lat=lats, + depth=depths, plastic_diameter=plastic_diameters, plastic_density=plastic_densities, wind_coefficient=wind_coefficients,