Skip to content

Commit 0dfb97f

Browse files
Supporting particle.delete in Kernel
1 parent f2d2ea4 commit 0dfb97f

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

parcels/kernel.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ def remove_deleted(self, pset):
7272
"""Utility to remove all particles that signalled deletion."""
7373
bool_indices = pset._data["state"] == StatusCode.Delete
7474
indices = np.where(bool_indices)[0]
75-
if len(indices) > 0 and self.fieldset.particlefile is not None:
76-
self.fieldset.particlefile.write(pset, None, indices=indices)
75+
# TODO v4: need to implement ParticleFile writing of deleted particles
76+
# if len(indices) > 0 and self.fieldset.particlefile is not None:
77+
# self.fieldset.particlefile.write(pset, None, indices=indices)
7778
pset.remove_indices(indices)
7879

7980

parcels/particle.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import numpy as np
44

5+
from parcels.tools.statuscodes import StatusCode
6+
57
__all__ = ["InteractionParticle", "Particle", "Variable"]
68

79

@@ -136,6 +138,10 @@ def __repr__(self):
136138
p_string += f"{var}={getattr(self, var):f}, "
137139
return p_string + f"time={time_string})"
138140

141+
def delete(self):
142+
"""Signal the particle for deletion."""
143+
self.state = StatusCode.Delete
144+
139145
@classmethod
140146
def add_variable(cls, var, *args, **kwargs):
141147
"""Add a new variable to the Particle class

tests/v4/test_particleset.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,27 @@ def test_pset_merge_inplace(fieldset, npart=100):
218218
assert pset1.size == 2 * npart
219219

220220

221+
def test_pset_remove_index(fieldset, npart=100):
222+
lon = np.linspace(0, 1, npart)
223+
lat = np.linspace(1, 0, npart)
224+
pset = ParticleSet(fieldset, lon=lon, lat=lat, lonlatdepth_dtype=np.float64)
225+
indices_to_remove = [0, 10, 20]
226+
pset.remove_indices(indices_to_remove)
227+
assert pset.size == 97
228+
assert not np.any(np.in1d(pset.trajectory, indices_to_remove))
229+
230+
231+
def test_pset_remove_particle_in_kernel(fieldset, npart=100):
232+
pset = ParticleSet(fieldset, lon=np.linspace(0, 1, npart), lat=np.linspace(1, 0, npart))
233+
234+
def DeleteKernel(particle, fieldset, time): # pragma: no cover
235+
if particle.lon >= 0.4:
236+
particle.delete()
237+
238+
pset.execute(pset.Kernel(DeleteKernel), runtime=np.timedelta64(1, "s"), dt=np.timedelta64(1, "s"))
239+
assert pset.size == 40
240+
241+
221242
def test_pset_iterator(fieldset):
222243
npart = 10
223244
pset = ParticleSet(fieldset, lon=np.zeros(npart), lat=np.ones(npart))

0 commit comments

Comments
 (0)