Skip to content

Commit f2d2ea4

Browse files
Removing lastID variable, and adding more tests
1 parent c33b049 commit f2d2ea4

3 files changed

Lines changed: 22 additions & 11 deletions

File tree

parcels/particle.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,6 @@ def set_lonlatdepth_dtype(cls, dtype):
193193
cls.lat_nextloop.dtype = dtype
194194
cls.depth_nextloop.dtype = dtype
195195

196-
@classmethod
197-
def setLastID(cls, offset): # TODO v4: check if we can implement this in another way
198-
Particle.lastID = offset
199-
200196

201197
InteractionParticle = Particle.add_variables(
202198
[Variable("vert_dist", dtype=np.float32), Variable("horiz_dist", dtype=np.float32)]

parcels/particleset.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,11 @@ def add(self, particles):
248248
249249
"""
250250
if isinstance(particles, type(self)):
251-
particles._data["trajectory"] = (
252-
particles._data["trajectory"].values + self._data["trajectory"].values.max() + 1
253-
)
251+
if len(self._data["trajectory"]) > 0:
252+
offset = self._data["trajectory"].values.max() + 1
253+
else:
254+
offset = 0
255+
particles._data["trajectory"] = particles._data["trajectory"].values + offset
254256
self._data = xr.concat([self._data, particles._data], dim="trajectory")
255257
# Adding particles invalidates the neighbor search structure.
256258
self._dirty_neighbor = True
@@ -646,10 +648,7 @@ def from_particlefile(
646648
if v not in ["lon", "lat", "depth", "time", "id"]:
647649
kwargs[v] = vars[v]
648650

649-
if restart:
650-
pclass.setLastID(0) # reset to zero offset
651-
else:
652-
vars["id"] = None
651+
vars["id"] = None
653652

654653
return cls(
655654
fieldset=fieldset,

tests/v4/test_particleset.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,22 @@ def test_pset_add_implicit(fieldset):
202202
assert np.allclose(np.diff(pset._data.trajectory), np.ones(6), atol=1e-12)
203203

204204

205+
def test_pset_add_execute(fieldset, npart=10):
206+
pset = ParticleSet(fieldset, lon=[], lat=[])
207+
for _ in range(npart):
208+
pset += ParticleSet(pclass=Particle, lon=0.1, lat=0.1, fieldset=fieldset)
209+
assert pset.size == npart
210+
211+
212+
def test_pset_merge_inplace(fieldset, npart=100):
213+
pset1 = ParticleSet(fieldset, lon=np.linspace(0, 1, npart), lat=np.linspace(1, 0, npart))
214+
pset2 = ParticleSet(fieldset, lon=np.linspace(0, 1, npart), lat=np.linspace(0, 1, npart))
215+
assert pset1.size == npart
216+
assert pset2.size == npart
217+
pset1.add(pset2)
218+
assert pset1.size == 2 * npart
219+
220+
205221
def test_pset_iterator(fieldset):
206222
npart = 10
207223
pset = ParticleSet(fieldset, lon=np.zeros(npart), lat=np.ones(npart))

0 commit comments

Comments
 (0)