Skip to content

Commit 7e3e842

Browse files
Implementing use of particle.ei in curvilinear search
1 parent ed657e1 commit 7e3e842

2 files changed

Lines changed: 28 additions & 15 deletions

File tree

parcels/_index_search.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,18 @@ def curvilinear_point_in_cell(grid, y: np.ndarray, x: np.ndarray, yi: np.ndarray
7575

7676

7777
def _search_indices_curvilinear_2d(
78-
grid: XGrid, y: np.ndarray, x: np.ndarray, yi_guess: np.ndarray | None = None, xi_guess: np.ndarray | None = None
78+
grid: XGrid, y: np.ndarray, x: np.ndarray, yi: np.ndarray | None = None, xi: np.ndarray | None = None
7979
):
80-
yi_guess = np.array(yi_guess)
81-
xi_guess = np.array(xi_guess)
82-
xi = np.full(len(x), GRID_SEARCH_ERROR, dtype=np.int32)
83-
yi = np.full(len(y), GRID_SEARCH_ERROR, dtype=np.int32)
84-
if np.any(xi_guess):
80+
if np.any(xi):
8581
# If an initial guess is provided, we first perform a point in cell check for all guessed indices
86-
is_in_cell, coords = curvilinear_point_in_cell(grid, y, x, yi_guess, xi_guess)
82+
is_in_cell, coords = curvilinear_point_in_cell(grid, y, x, yi, xi)
8783
y_check = y[is_in_cell == 0]
8884
x_check = x[is_in_cell == 0]
8985
zero_indices = np.where(is_in_cell == 0)[0]
9086
else:
9187
# Otherwise, we need to check all points
88+
yi = np.full(len(y), GRID_SEARCH_ERROR, dtype=np.int32)
89+
xi = np.full(len(x), GRID_SEARCH_ERROR, dtype=np.int32)
9290
y_check = y
9391
x_check = x
9492
coords = -1.0 * np.ones((len(y), 2), dtype=np.float32)

parcels/field.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,14 @@ def eval(self, time: datetime, z, y, x, particle=None, applyConversion=True):
212212
conversion to the result. Note that we defer to
213213
scipy.interpolate to perform spatial interpolation.
214214
"""
215-
# if particle is None:
216-
_ei = None
217-
# else:
218-
# _ei = particle.ei[self.igrid]
215+
if particle is None:
216+
_ei = None
217+
else:
218+
_ei = particle.ei[:, self.igrid]
219219

220220
tau, ti = _search_time_index(self, time)
221221
position = self.grid.search(z, y, x, ei=_ei)
222+
_update_particles_ei(particle, position, self)
222223
_update_particle_states_position(particle, position)
223224

224225
value = self._interp_method(self, ti, position, tau, time, z, y, x)
@@ -251,6 +252,7 @@ def __init__(
251252
self.V = V
252253
self.W = W
253254
self.grid = U.grid
255+
self.igrid = U.igrid
254256

255257
if W is None:
256258
_assert_same_time_interval((U, V))
@@ -294,13 +296,14 @@ def eval(self, time: datetime, z, y, x, particle=None, applyConversion=True):
294296
conversion to the result. Note that we defer to
295297
scipy.interpolate to perform spatial interpolation.
296298
"""
297-
# if particle is None:
298-
_ei = None
299-
# else:
300-
# _ei = particle.ei[self.igrid]
299+
if particle is None:
300+
_ei = None
301+
else:
302+
_ei = particle.ei[:, self.igrid]
301303

302304
tau, ti = _search_time_index(self.U, time)
303305
position = self.grid.search(z, y, x, ei=_ei)
306+
_update_particles_ei(particle, position, self)
304307
_update_particle_states_position(particle, position)
305308

306309
if self._vector_interp_method is None:
@@ -339,6 +342,18 @@ def __getitem__(self, key):
339342
return _deal_with_errors(error, key, vector_type=self.vector_type)
340343

341344

345+
def _update_particles_ei(particles, position, field):
346+
"""Update the element index (ei) of the particles"""
347+
if particles is not None:
348+
particles.ei[:, field.igrid] = field.grid.ravel_index(
349+
{
350+
"X": position["X"][0],
351+
"Y": position["Y"][0],
352+
"Z": position["Z"][0],
353+
}
354+
)
355+
356+
342357
def _update_particle_states_position(particle, position):
343358
"""Update the particle states based on the position dictionary."""
344359
if particle: # TODO also support uxgrid search

0 commit comments

Comments
 (0)