Skip to content

Commit

Permalink
Merge pull request #1630 from OceanParcels/fix_bug_empty_pset_execute
Browse files Browse the repository at this point in the history
Fixing bug when executing a breaking pset
  • Loading branch information
erikvansebille authored Jul 31, 2024
2 parents 4f55da7 + 746519e commit 8bfa17f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions parcels/particleset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/test_particlesets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8bfa17f

Please sign in to comment.