Skip to content

Commit 58bedac

Browse files
Update explanation_interpolation.md
Adding more information on Interpolator signature
1 parent c316e95 commit 58bedac

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

docs/user_guide/examples/explanation_interpolation.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
Interpolation is an important functionality of Parcels. On this page we will discuss the way it is
44
implemented in **Parcels** and how to write a custom interpolator function.
55

6-
```{note}
7-
TODO: expand explanation (similar to Kernel loop explanation)
8-
```
9-
106
When we want to know the state of particles in an environmental field, such as temperature or velocity,
117
we _evaluate_ the `parcels.Field` at the particles real position in time and space (`t`, `z`, `lat`, `lon`).
128
In Parcels we can do this using square brackets:
@@ -26,7 +22,7 @@ If you want to sample at a different location, or time, that is not necessarily
2622
```python
2723
particles.temperature = fieldset.temperature[time, depth, lat, lon]
2824
```
29-
but this could be slower for curvilinear and unstructured because the entire grid needs to be searched.
25+
but this could be slower for curvilinear and unstructured Grids because the entire grid needs to be searched.
3026
````
3127

3228
The values of the `temperature` field at the particles' positions are determined using an interpolation
@@ -36,15 +32,23 @@ relate to the value at any point within a grid cell.
3632
Each `parcels.Field` is defined on a (structured) `parcels.XGrid` or (unstructured) `parcels.UXGrid`.
3733
The interpolation function takes information about the particles position relative to this grid (`grid_positions`),
3834
as well as the values of the grid points of the `parcels.Field` in time and space, to calculate
39-
the requested value at the particles location. Note that all grid values are available so that higher-order interpolation is possible.
35+
the requested value at the particles location.
4036

4137
## Interpolator API
4238

43-
The interpolators included in Parcels are designed for common interpolation schemes in Parcels simulations.
44-
If we want to add a custom interpolation method, we need to look at the interpolator API:
39+
The interpolators included in Parcels are designed for common interpolation schemes in Parcels simulations; see the [Using the built-in interpolators tutorial](./tutorial_interpolation.ipynb).
4540

46-
We can write an interpolator function that takes a `parcels.Field` (or `parcels.VectorField`), a dictionary with the `particle_positions`
47-
in real space and time, and a dictionary with the `grid_positions`.
41+
If we want to create a custom interpolation method, we need to look at the interpolator API. Each interpolator is a class that inherits from either the `ScalarInterpolator` or `VectorInterpolator` class. The `ScalarInterpolator` class is used for scalar fields, such as temperature or salinity, while the `VectorInterpolator` class is used for vector fields, such as velocity. An interpolator class than has to have a `.interp()` method with the following signature:
42+
43+
```python
44+
def interp(
45+
self,
46+
particle_positions: dict[str, float | np.ndarray],
47+
grid_positions: dict[ptyping.XgridAxis, dict[str, int | float | np.ndarray]],
48+
field: Field,
49+
):
50+
...
51+
```
4852

4953
The `particle_positions` dictionary contains:
5054

@@ -74,3 +78,7 @@ grid_positions = {
7478
"FACE": {"index": fi, "bcoord": bcoord}
7579
}
7680
```
81+
82+
The `.interp()` method should return a float (in the case o a `ScalarInterpolator` or a tuple of three floats `(u, v, w)` in the case of a `VectorInterpolator`).
83+
84+
Writing custom interpolators is not trivial, so we recommend that you have a look at the built-in interpolators in `parcels.interpolators._xinterpolators` or `parcels.interpolators._uxinterpolators` to see how they are implemented.

0 commit comments

Comments
 (0)