anon-iclr-submission/benchname-bug-localization huggingface dataset:
Repository: oceanparcels/parcels
Issue: Combining different fields and indices fails

If one combines 3D and 2D fields and makes use of indices, only the surface velocity currents are used to advect particles.  A quick fix is to add a line indices = indices.copy() in field.py:

diff --git a/parcels/field.py b/parcels/field.py
index 163c325..bb67c86 100644
--- a/parcels/field.py
+++ b/parcels/field.py
@@ -273,19 +273,32 @@ class Field(object):
                full_load is however sometimes necessary for plotting the fields.
         """
 
+        indices = indices.copy()
         if not isinstance(filenames, Iterable) or isinstance(filenames, str):
             filenames = [filenames]
         dimension_filename = dimension_filename if dimension_filename else filenames[0]
         if indices is None:
             indices = {}
         with NetcdfFileBuffer(dimension_filename, dimensions, indices) as filebuffer:


Changed files:
parcels/field.py, tests/test_fieldset.py

diff --git a/parcels/field.py b/parcels/field.py
index f49b351a..fa1851e0 100644
--- a/parcels/field.py
+++ b/parcels/field.py
@@ -152,8 +152,7 @@ class Field(object):
         if not isinstance(filenames, Iterable) or isinstance(filenames, str):
             filenames = [filenames]
         dimension_filename = dimension_filename if dimension_filename else filenames[0]
-        if indices is None:
-            indices = {}
+        indices = {} if indices is None else indices.copy()
         with NetcdfFileBuffer(dimension_filename, dimensions, indices) as filebuffer:
             lon, lat = filebuffer.read_lonlat
             depth = filebuffer.read_depth
diff --git a/tests/test_fieldset.py b/tests/test_fieldset.py
index c7ffa424..253cf9f9 100644
--- a/tests/test_fieldset.py
+++ b/tests/test_fieldset.py
@@ -92,7 +92,9 @@ def test_fieldset_from_file_subsets(indslon, indslat, tmpdir, filename='test_sub
     fieldsetfull = FieldSet.from_data(data, dimensions)
     fieldsetfull.write(filepath)
     indices = {'lon': indslon, 'lat': indslat}
+    indices_back = indices.copy()
     fieldsetsub = FieldSet.from_parcels(filepath, indices=indices)
+    assert indices == indices_back
     assert np.allclose(fieldsetsub.U.lon, fieldsetfull.U.grid.lon[indices['lon']])
     assert np.allclose(fieldsetsub.U.lat, fieldsetfull.U.grid.lat[indices['lat']])
     assert np.allclose(fieldsetsub.V.lon, fieldsetfull.V.grid.lon[indices['lon']])
Repository: oceanparcels/parcels
Issue: Duplicate variable names across kernels in JIT mode

At present, local variables defined in Kernels that share the same name as a local variable in another Kernel crash PARCELS during the kernel compiler. No indication is given to the user that this is cause of the problem. It would be nice if this could be handled and an appropriate error message given in this case.

More generally, if different variables sharing names across kernels are present, it would be neat if they were renamed at runtime during kernel compilation to avoid this issue.

Changed files:
parcels/codegenerator.py, tests/test_kernel_execution.py

diff --git a/parcels/codegenerator.py b/parcels/codegenerator.py
index 98032718..c7c6db70 100644
--- a/parcels/codegenerator.py
+++ b/parcels/codegenerator.py
@@ -276,6 +276,15 @@ class KernelGenerator(ast.NodeVisitor):
         self.ccode = py_ast.ccode
 
         # Insert variable declarations for non-instrinsics
+        # Make sure that repeated variables are not declared more than
+        # once. If variables occur in multiple Kernels, give a warning
+        used_vars = []
+        for kvar in funcvars:
+            if kvar in used_vars:
+                print("WARNING: "+kvar+" declared in multiple Kernels")
+                funcvars.remove(kvar)
+            else:
+                used_vars.append(kvar)
         for kvar in self.kernel_vars + self.array_vars:
             if kvar in funcvars:
                 funcvars.remove(kvar)
diff --git a/tests/test_kernel_execution.py b/tests/test_kernel_execution.py
index 9896834c..632da3eb 100644
--- a/tests/test_kernel_execution.py
+++ b/tests/test_kernel_execution.py
@@ -171,3 +171,21 @@ def test_kernel_add_no_new_variables(grid, mode):
                  starttime=0., endtime=1., dt=1.)
     assert np.allclose([p.lon for p in pset], 0.6, rtol=1e-5)
     assert np.allclose([p.lat for p in pset], 0.6, rtol=1e-5)
+
+
+@pytest.mark.parametrize('mode', ['scipy', 'jit'])
+def test_multi_kernel_duplicate_varnames(grid, mode):
+    # Testing for merging of two Kernels with the same variable declared
+    # Should throw a warning, but go ahead regardless
+    def MoveEast(particle, grid, time, dt):
+        add_lon = 0.1
+        particle.lon += add_lon
+
+    def MoveWest(particle, grid, time, dt):
+        add_lon = -0.3
+        particle.lon += add_lon
+
+    pset = ParticleSet(grid, pclass=ptype[mode], lon=[0.5], lat=[0.5])
+    pset.execute(pset.Kernel(MoveEast) + pset.Kernel(MoveWest),
+                 starttime=0., endtime=1., dt=1.)
+    assert np.allclose([p.lon for p in pset], 0.3, rtol=1e-5)