parcels icon indicating copy to clipboard operation
parcels copied to clipboard

Rewrite Particle loops to be over objects, instead of integers

Open erikvansebille opened this issue 4 years ago • 4 comments

Make the loops in Field and FieldSet that access Particles (in particular those two classes; possibly others too) independent of of the order - hence: migrate loops like

for i in range(numerical):
       new_value = array[i]

to either:

for obj in array:
      new_value = obj

or:

for i, obj in enumerate(array):
      new_array[i] = obj

This would simplify the integration of the linked list of nodes and the lists-of-arrays of particles (#862) later. This is because their indices are dynamic and could rearrange - a classic reason why to use iterators.

Thanks to @CKehl for bringing this up

erikvansebille avatar Jun 24 '20 06:06 erikvansebille

@CKehl, I've been looking into this Issue, but I don't see where these problematic loops are. Can you point them out to me?

erikvansebille avatar Jul 03 '20 09:07 erikvansebille

it's most likely in the 'Kernel', but those kind of loops exist in other classes too. Let me see where they are and collect them.

CKehl avatar Jul 03 '20 12:07 CKehl

Following lines are relevant: 'for p in range(pset.size)' (kernel.py; l. 291 and 372) list comprehension 'for i in range(pset.size)': (example_peninsula.py; ;. 150 and l. 187) other examples that have list comprehensions in the style 'for i in range(pset.size)', where either i or pset is named differently 'for i in range(self.size)' and 'for i in range(pset[_new].size)' (particleset.py; l. 265 and l. 687 and l. 776)

generally problem: if particle.id is not the same as it's position in the list (e.g. due to deletion), this is prone to failure.

CKehl avatar Jul 03 '20 13:07 CKehl

see #913 and follow-ups - issue resolved, progressively.

CKehl avatar Jan 26 '21 13:01 CKehl