Skip to content

Commit c839c0f

Browse files
Fixing tutorial_interaction with ParticleSetView
1 parent dd43291 commit c839c0f

2 files changed

Lines changed: 12 additions & 14 deletions

File tree

docs/user_guide/examples/tutorial_interaction.ipynb

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -293,18 +293,9 @@
293293
" larger_idx = np.where(mass_j > mass_i, pair_j, pair_i)\n",
294294
" smaller_idx = np.where(mass_j > mass_i, pair_i, pair_j)\n",
295295
"\n",
296-
" # perform transfer and mark deletions\n",
297-
" # TODO note that we use temporary arrays for indexing because of KernelParticle bug (GH #2143)\n",
298-
" masses = particles.mass\n",
299-
" states = particles.state\n",
300-
"\n",
301296
" # transfer mass from smaller to larger and mark smaller for deletion\n",
302-
" masses[larger_idx] += particles.mass[smaller_idx]\n",
303-
" states[smaller_idx] = parcels.StatusCode.Delete\n",
304-
"\n",
305-
" # TODO use particle variables directly after KernelParticle bug (GH #2143) is fixed\n",
306-
" particles.mass = masses\n",
307-
" particles.state = states"
297+
" particles.mass[larger_idx] += particles.mass[smaller_idx]\n",
298+
" particles.state[smaller_idx] = parcels.StatusCode.Delete"
308299
]
309300
},
310301
{

src/parcels/_core/particle.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,17 @@ def _to_global_index(self, subindex=None):
343343
if isinstance(subindex, (slice, int)):
344344
rel = np.flatnonzero(base)[subindex]
345345
return rel
346-
# otherwise assume subindex is an integer/array selection relative
347-
# to the local view and map to global indices
346+
# If subindex is an integer/array selection (relative to the
347+
# local view) map those to global integer indices.
348+
arr = np.asarray(subindex)
349+
if arr.dtype != bool:
350+
particle_idxs = np.flatnonzero(base)
351+
sel = particle_idxs[arr]
352+
return sel
353+
# Otherwise treat subindex as a boolean mask relative to the
354+
# local view and expand to a global boolean mask.
348355
global_mask = np.zeros_like(base, dtype=bool)
349-
global_mask[base] = subindex
356+
global_mask[base] = arr
350357
return global_mask
351358

352359
# If base is an array of integer indices

0 commit comments

Comments
 (0)