Repository navigation
Description
Parcels version
Currently https://github.com/jemmajeffree/Parcels/tree/big_memory_window but it's up to date with main
Description
mom6_to_sgrid fieldsets get unhappy when there's particles between the last two c-grid points
I haven't been able to trace exactly what's causing this, but I presume it's related to this being the symmetric bit that NEMO doesn't have. Possibly also related to #2062?
At the moment, I'm working around this by just throwing out any particles that go within 10 gridcells of the boundary of our regional setup.
The obvious transient solution would be for mom6_to_sgrid to chuck out the boundary? But it feels like this should be solveable more cleanly than that.
Code sample
The following code drops parcels right onto the problem location, but I found the issue because parcels are also able to wander there naturally
# Riffing on the MOM6 tutorial for clarity
import xarray as xr
import numpy as np
import parcels
import parcels.tutorial
U = parcels.tutorial.open_dataset("MOM6_example_data/U")["ssu"]
V = parcels.tutorial.open_dataset("MOM6_example_data/V")["ssv"]
coords = parcels.tutorial.open_dataset("MOM6_example_data/static")
ds_fset = parcels.convert.mom6_to_sgrid(
fields={"U": U.sortby("time"), "V": V.sortby("time")}, coords=coords
)
fieldset = parcels.FieldSet.from_sgrid_conventions(ds_fset)
# I'm pretty sure we've got problems from dropping stuff RIGHT on the edge in the symmetric border
# xh/yh are on the T grid (ie temperature not velocities.) Should have C grid (velocity) points around them
X = coords.geolon.isel(xh=-1).load()
Y = coords.geolat.isel(xh=-1).load()
Z = np.zeros_like(X)
def DeleteParticle(particles, fieldset):
any_error = particles.state >= 50
particles[any_error].state = parcels.StatusCode.Delete
pset = parcels.ParticleSet(fieldset=fieldset, x=X, y=Y, z=Z)
outputfile = parcels.ParticleFile(
path="mom6_particles.parquet",
outputdt=np.timedelta64(3, "h"),
)
pset.execute(
[parcels.kernels.AdvectionRK2, DeleteParticle],
runtime=np.timedelta64(2, "D"),
dt=np.timedelta64(30, "m"),
output_file=outputfile,
)IndexError Traceback (most recent call last)
Cell In[7], line 1
----> 1 pset.execute(
2 [parcels.kernels.AdvectionRK2, DeleteParticle],
3 runtime=np.timedelta64(2, "D"),
4 dt=np.timedelta64(30, "m"),
File ~/Large_Datasets/Parcels/src/parcels/_core/particleset.py:449, in ParticleSet.execute(self, kernels, dt, endtime, runtime, output_file, verbose_progress)
446 else:
447 next_time = end_time
--> 449 self._kernel.execute(self, endtime=next_time)
451 if next_output is not None:
452 if np.abs(next_time - next_output) < 0.001:
File ~/Large_Datasets/Parcels/src/parcels/_core/kernel.py:221, in Kernel.execute(self, pset, endtime)
218 with warnings.catch_warnings():
219 warnings.simplefilter("ignore", FieldEvalWarning)
--> 221 f(pset[evaluate_particles], self._fieldset)
223 # check for particles that have to be repeated
224 repeat_particles = pset.state == StatusCode.Repeat
File ~/Large_Datasets/Parcels/src/parcels/kernels/_advection.py:31, in AdvectionRK2(particles, fieldset)
29 def AdvectionRK2(particles, fieldset): # pragma: no cover
30 """Advection of particles using second-order Runge-Kutta integration."""
---> 31 u, v = RK2(particles, fieldset, rhs_uv)
32 particles.dx += u * particles.dt
33 particles.dy += v * particles.dt
File ~/Large_Datasets/Parcels/src/parcels/kernels/_integration.py:18, in RK2(particles, fieldset, rhs)
16 def RK2(particles, fieldset, rhs):
17 z, y, x = particles.z, particles.y, particles.x
---> 18 fields = rhs(fieldset, particles.t, z, y, x, particles)
19 _validate_rhs_output(fields, "RK2")
20 x = particles.x + fields[0] * 0.5 * particles.dt
File ~/Large_Datasets/Parcels/src/parcels/kernels/_advection.py:22, in rhs_uv(fieldset, t, z, y, x, particles)
21 def rhs_uv(fieldset, t, z, y, x, particles):
---> 22 return fieldset.UV[t, z, y, x, particles]
File ~/Large_Datasets/Parcels/src/parcels/_core/field.py:311, in VectorField.__getitem__(self, key)
309 return self.eval(key.t, key.z, key.y, key.x, key)
310 else:
--> 311 return self.eval(*key)
312 except tuple(AllParcelsErrorCodes.keys()) as error:
313 return _deal_with_errors(error, key, vector_type=self.vector_type)
File ~/Large_Datasets/Parcels/src/parcels/_core/field.py:293, in VectorField.eval(self, t, z, y, x, particles)
290 y = np.atleast_1d(y)
291 x = np.atleast_1d(x)
--> 293 particle_positions, grid_positions = _get_positions(self.U, t, z, y, x, particles, _ei)
295 (u, v, w) = self._interp_method.interp(particle_positions, grid_positions, self)
297 for vel in (u, v, w):
File ~/Large_Datasets/Parcels/src/parcels/_core/field.py:413, in _get_positions(field, t, z, y, x, particles, _ei)
410 time_positions = _search_time_index(field, t)
411 grid_positions.update(time_positions)
412 grid_positions.update(
--> 413 field.grid.search(z, y, x, ei=_ei, ti=time_positions["T"]["index"], tau=time_positions["T"]["bcoord"])
414 )
415 _update_particles_ei(particles, grid_positions, field)
416 _update_particle_states_position(particles, grid_positions)
File ~/Large_Datasets/Parcels/src/parcels/_core/xgrid.py:377, in XGrid.search(self, z, y, x, ei, ti, tau)
374 xi = axis_indices.get("X")
375 yi = axis_indices.get("Y")
--> 377 yi, eta, xi, xsi = _search_indices_curvilinear_2d(self, y, x, yi, xi)
378 else:
379 raise NotImplementedError("Searching in >2D lon/lat arrays is not implemented yet.")
File ~/Large_Datasets/Parcels/src/parcels/_core/index_search.py:328, in _search_indices_curvilinear_2d(grid, y, x, yi, xi)
302 """Searches a grid for particle locations in 2D curvilinear coordinates.
303
304 Parameters
(...) 324 - xsi (np.ndarray): Array of barycentric coordinates in the i-direction within the found grid cells.
325 """
326 if np.any(xi):
327 # If an initial guess is provided, we first perform a point in cell check for all guessed indices
--> 328 is_in_cell, coords = curvilinear_point_in_cell(grid, y, x, yi, xi)
329 y_check = y[is_in_cell == 0]
330 x_check = x[is_in_cell == 0]
File ~/Large_Datasets/Parcels/src/parcels/_core/index_search.py:143, in curvilinear_point_in_cell(grid, y, x, yi, xi)
133 def curvilinear_point_in_cell(grid, y: np.ndarray, x: np.ndarray, yi: np.ndarray, xi: np.ndarray):
134 """Locate points within 2D curvilinear grid cells.
135
136 For spherical grids, the bilinear inverse runs in a plane tangent to the sphere at
(...) 140 must agree on that same space.
141 """
142 clon = np.asarray(
--> 143 [grid.lon[yi, xi], grid.lon[yi, xi + 1], grid.lon[yi + 1, xi + 1], grid.lon[yi + 1, xi]],
144 dtype=float,
145 )
146 clat = np.asarray(
147 [grid.lat[yi, xi], grid.lat[yi, xi + 1], grid.lat[yi + 1, xi + 1], grid.lat[yi + 1, xi]],
148 dtype=float,
149 )
151 if grid.mesh.is_spherical():
IndexError: index 41 is out of bounds for axis 0 with size 41
Integration time: 1993-01-01T15:00:00 12%|▉ | [00:20<00:05, 25834.93it/s]
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsBacklog
Activity
erikvansebille commentedon Nov 17, 2026
Thanks for reporting this bug, @jemmajeffree; and also for the very useful minimal example! I just pushed a fix in #3070 - can you check whether it works for you?