diff --git a/parcels/particleset.py b/parcels/particleset.py index 09010f650..5ae9b4025 100644 --- a/parcels/particleset.py +++ b/parcels/particleset.py @@ -840,6 +840,10 @@ def execute(self, pyfunc=AdvectionRK4, pyfunc_inter=None, endtime=None, runtime= delete_cfiles : bool Whether to delete the C-files after compilation in JIT mode (default is True) """ + # check if particleset is empty. If so, return immediately + if len(self) == 0: + return + # check if pyfunc has changed since last compile. If so, recompile if self.kernel is None or (self.kernel.pyfunc is not pyfunc and self.kernel is not pyfunc): # Generate and store Kernel diff --git a/tests/test_particlesets.py b/tests/test_particlesets.py index 9e1d64fdf..35551bd7a 100644 --- a/tests/test_particlesets.py +++ b/tests/test_particlesets.py @@ -52,6 +52,18 @@ def test_pset_create_line(fieldset, mode, lonlatdepth_dtype, npart=100): assert isinstance(pset[0].lat, lonlatdepth_dtype) +@pytest.mark.parametrize('mode', ['scipy', 'jit']) +def test_create_empty_pset(fieldset, mode): + pset = ParticleSet(fieldset, pclass=ptype[mode]) + assert pset.size == 0 + + def DoNothing(particle, fieldset, time): + pass + + pset.execute(DoNothing, endtime=1., dt=1.) + assert pset.size == 0 + + @pytest.mark.parametrize('mode', ['scipy', 'jit']) def test_pset_create_list_with_customvariable(fieldset, mode, npart=100): lon = np.linspace(0, 1, npart, dtype=np.float32)