Skip to content

Commit 46785a7

Browse files
reint-fischerreint-fischer
authored andcommitted
update user_guide/index and add interpolation explanation
1 parent bda3062 commit 46785a7

3 files changed

Lines changed: 88 additions & 12 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Interpolator explanation
2+
Interpolation is an important functionality of Parcels. On this page we will discuss the way it is
3+
implemented in **Parcels** and how to write a custom interpolator function.
4+
5+
```{note}
6+
TODO: expand explanation (similar to Kernel loop explanation)
7+
```
8+
9+
When we want to know the state of particles in an environmental field, such as temperature or velocity,
10+
we _evaluate_ the `parcels.Field` at the particles real position in time and space (`t`, `z`, `lat`, `lon`).
11+
In Parcels we can do this using square brackets:
12+
```
13+
particles.temperature = fieldset.temperature[particles.time, particles.z, particles.lat, particles.lon]
14+
```
15+
The values of the `temperature` field at the particles' positions are determined using an interpolation
16+
method. This interpolation method defines how the discretized values of the `parcels.Field` should
17+
relate to the value at any point within a grid cell.
18+
19+
Each `parcels.Field` is defined on a (structured) `parcels.XGrid` or (unstructured) `parcels.UXGrid`.
20+
The interpolation function takes information about the particles position relative to this grid (`grid_positions`),
21+
as well as the `parcels.Field` values of the surrounding grid points in time and space, to calculate
22+
the requested value at the particles location.
23+
24+
## Interpolator API
25+
The interpolators included in Parcels are designed for common interpolation schemes in Parcels simulations.
26+
If we want to add a custom interpolation method, we need to look at the interpolator API:
27+
28+
We can write an interpolator function that takes a `parcels.Field`, a dictionary with the `particle_positions`
29+
in real space and time, and a dictionary with the `grid_positions`.
30+
31+
The `particle_positions` dictionary contains:
32+
```
33+
particle_positions = {"time", time, "z", z, "lat", lat, "lon", lon}
34+
```
35+
36+
For structured (`X`) grids, the `grid_positions` dictionary looks like:
37+
```
38+
grid_positions = {
39+
"T": {"index": ti, "bcoord": tau},
40+
"Z": {"index": zi, "bcoord": zeta},
41+
"Y": {"index": yi, "bcoord": eta},
42+
"X": {"index": xi, "bcoord", xsi},
43+
}
44+
```
45+
where `index` is the grid index in the corresponding dimension, and `bcoord` is the barycentric coordinate in the grid cell.
46+
47+
For unstructured (`UX`) grids, the same dictionary is defined as:
48+
```
49+
grid_positions = {
50+
"T": {"index": ti, "bcoord": tau},
51+
"Z": {"index": zi, "bcoord": zeta},
52+
"FACE": {"index": fi, "bcoord": bcoord}
53+
}
54+
```

docs/user_guide/examples/tutorial_interpolation.ipynb

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,8 @@
55
"cell_type": "markdown",
66
"metadata": {},
77
"source": [
8-
"# Configure Field Interpolation\n"
9-
]
10-
},
11-
{
12-
"attachments": {},
13-
"cell_type": "markdown",
14-
"metadata": {},
15-
"source": [
16-
"Parcels comes with a number of different interpolation methods for tracer fields, such as temperature. Here, we will show how these work, in an idealised example. For more guidance on the sampling of such fields, check out the [sampling guide](./tutorial_sampling).\n",
8+
"# Using `parcels.interpolators`\n",
9+
"Parcels comes with a number of different interpolation methods for tracer fields, such as temperature. Here, we will look at a few common `parcels.interpolators` for structured (`X`) grids, and how to configure them in an idealised example. For more guidance on the sampling of such fields, check out the [sampling guide](./tutorial_sampling).\n",
1710
"\n",
1811
"We first import the relevant modules"
1912
]
@@ -36,7 +29,7 @@
3629
"cell_type": "markdown",
3730
"metadata": {},
3831
"source": [
39-
"We create a small 2D grid where `P` is a tracer that we want to interpolate. In each grid cell, `P` has a random value between 0.1 and 1.1. We then set `P[1,1]` to `0`, which for Parcels specifies that this is a land cell\n"
32+
"We create a small 2D structured grid where `P` is a tracer that we want to interpolate. In each grid cell, `P` has a random value between 0.1 and 1.1. We then set `P[1,1]` to `0`, which for Parcels specifies that this is a land cell\n"
4033
]
4134
},
4235
{
@@ -111,7 +104,11 @@
111104
{
112105
"cell_type": "code",
113106
"execution_count": null,
114-
"metadata": {},
107+
"metadata": {
108+
"tags": [
109+
"hide-output"
110+
]
111+
},
115112
"outputs": [],
116113
"source": [
117114
"pset = {}\n",
@@ -217,13 +214,30 @@
217214
"cell_type": "markdown",
218215
"metadata": {},
219216
"source": [
220-
"So in summary, Parcels has four different interpolation schemes for tracers:\n",
217+
"So in summary, Parcels has four different interpolation schemes for tracers on structured grids:\n",
221218
"\n",
222219
"1. `interp_method=parcels.interpolators.XLinear`: compute linear interpolation\n",
223220
"2. `interp_method=parcels.interpolators.XLinearInvdistLandTracer`: compute linear interpolation except near land (where field value is zero). In that case, inverse distance weighting interpolation is computed, weighting by squares of the distance.\n",
224221
"3. `interp_method=parcels.interpolators.XNearest`: return nearest field value\n",
225222
"4. `interp_method=parcels.interpolators.CGridTracer`: return nearest field value supposing C cells\n"
226223
]
224+
},
225+
{
226+
"cell_type": "markdown",
227+
"metadata": {},
228+
"source": [
229+
"```{note}\n",
230+
"TODO: link to reference API with all `parcels.interpolators`\n",
231+
"```"
232+
]
233+
},
234+
{
235+
"cell_type": "markdown",
236+
"metadata": {},
237+
"source": [
238+
"## Write your own interpolator\n",
239+
"The interpolators included in Parcels are designed for common interpolation schemes in Parcels simulations. If we want to add a custom interpolation method, we can write our own interpolator function (e.g. spline) using the [interpolator API](./explanation_interpolation.md#interpolator-api)."
240+
]
227241
}
228242
],
229243
"metadata": {

docs/user_guide/index.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ examples/tutorial_gsw_density.ipynb
4949
examples/tutorial_Argofloats.ipynb
5050
```
5151

52+
```{toctree}
53+
:caption: Configure interpolation
54+
:titlesonly:
55+
56+
examples/explanation_interpolation.md
57+
examples/tutorial_interpolation.ipynb
58+
```
59+
5260
<!-- examples/tutorial_diffusion.ipynb -->
5361
<!-- examples/tutorial_particle_field_interaction.ipynb -->
5462
<!-- examples/tutorial_interaction.ipynb -->

0 commit comments

Comments
 (0)