Skip to content

Commit 66d3410

Browse files
reint-fischerreint-fischer
authored andcommitted
expand kernel explanation
1 parent 564204c commit 66d3410

File tree

3 files changed

+49
-43
lines changed

3 files changed

+49
-43
lines changed

docs/getting_started/explanation_concepts.md

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ A Parcels simulation is generally built up from four different components:
1313
1. [**FieldSet**](#1-fieldset). The input dataset of gridded fields (e.g. ocean current velocity, temperature) in which virtual particles are defined.
1414
2. [**ParticleSet**](#2-particleset). The dataset of virtual particles. These always contain time, z, lat, and lon, for which initial values must be defined, and may contain other variables.
1515
3. [**Kernels**](#3-kernels). Kernels perform some specific operation on the particles every time step (e.g. interpolate the temperature from the temperature field to the particle location).
16-
4. [**Execute**](#4-execution). Execute the simulation. The core method which integrates the operations defined in Kernels for a given time and timestep, and writes output to a ParticleFile.
16+
4. [**Execute**](#4-execute). Execute the simulation. The core method which integrates the operations defined in Kernels for a given time and timestep, and writes output to a ParticleFile.
1717

1818
We discuss each component in more detail below. The subsections titled **"Learn how to"** link to more detailed [how-to guide notebooks](../user_guide/index.md) and more detailed _explanations_ of Parcels functionality are included under **"Read more about"** subsections. The full list of classes and methods is in the [API reference](../reference.md). If you want to learn by doing, check out the [quickstart tutorial](./tutorial_quickstart.md) to start creating your first Parcels simulation.
1919

@@ -57,91 +57,97 @@ Each `parcels.Field` is defined on a grid. With Parcels we can simulate particle
5757

5858
### Interpolation
5959

60-
To find the value of a `parcels.Field` at any particle location, Parcels uses interpolation. Depending on the variable, grid, and required accuracy, different interpolation methods may be appropriate. Parcels comes with a number of built-in **`parcels.interpolators`**:
60+
To find the value of a `parcels.Field` at any particle location, Parcels interpolates the gridded field. Depending on the variable, grid, and required accuracy, different interpolation methods may be appropriate. Parcels comes with a number of built-in **`parcels.interpolators`**.
6161

62-
```{code-cell}
63-
import parcels
64-
65-
for interpolator in parcels.interpolators.__all__:
66-
print(f"{interpolator}")
67-
```
68-
69-
### Read more about
62+
### Read more about interpolation
7063

7164
- [Interpolation explanation](../user_guide/examples/explanation_interpolation.md)
7265

73-
### Learn how to
66+
### Learn how to use Parcels interpolators
7467

75-
- [Use interpolation methods](../user_guide/examples/tutorial_interpolation.ipynb)
68+
- [Interpolators guide](../user_guide/examples/tutorial_interpolation.ipynb)
7669

7770
## 2. ParticleSet
7871

7972
Once the environment has a `parcels.FieldSet` object, you can start defining your particles in a **`parcels.ParticleSet`** object. This object requires:
8073

8174
1. The `parcels.FieldSet` object in which the particles will be released.
82-
2. The type of `parcels.Particle`: A default `Particle` or a custom `Particle`-type with additional `Variable`s.
75+
2. The type of `parcels.Particle`: A default `Particle` or a custom `Particle`-type with additional `Variable`s (see the [custom kernel example](custom-kernel)).
8376
3. Initial conditions for each `Variable` defined in the `Particle`, most notably the release coordinates of `time`, `z`, `lat` and `lon`.
8477

85-
### Learn how to
78+
### Learn how to create ParticleSets
8679

8780
- [Release particles at different times](../user_guide/examples/tutorial_delaystart.ipynb)
8881

8982
## 3. Kernels
9083

91-
**`parcels.Kernel`** objects are little snippets of code, which are applied to the particles in the `ParticleSet`, for every time step during a simulation. They define the computation or numerical integration done by Parcels, and can represent many processes such as advection, ageing, growth, or simply the sampling of a field.
84+
A **`parcels.Kernel`** object is a little snippet of code, which is applied to the particles in the `ParticleSet`, for every time step during a simulation. Kernels define the computation or numerical integration done by Parcels, and can represent many processes such as advection, ageing, growth, or simply the sampling of a field.
9285

93-
Basic kernels are included in Parcels, among which several types of advection kernels:
86+
Advection of a particle by the flow, the change in position $\mathbf{x}(t) = (lon(t), lat(t))$ at time $t$, can be described by the equation:
9487

95-
```{code-cell}
96-
for kernel in parcels.kernels.advection.__all__:
97-
print(f"{kernel}")
98-
```
88+
$$
89+
\begin{aligned}
90+
\frac{\text{d}\mathbf{x}(t)}{\text{d}t} = \mathbf{v}(\mathbf{x}(t),t),
91+
\end{aligned}
92+
$$
9993

100-
We can also write custom kernels, to add certain types of 'behaviour' to the particles. To do so we write a function with two arguments: `particles` and `fieldset`. We can then write any computation as a function of any variables defined in the `Particle` and any `Field` defined in the `FieldSet`. Kernels can then be combined by creating a `list` of the kernels. Note that the kernels are executed in order:
94+
where $\mathbf{v}(\mathbf{x},t) = (u(\mathbf{x},t), v(\mathbf{x},t))$ describes the ocean velocity field at position $\mathbf{x}$ at time $t$.
95+
96+
In Parcels, we can write a kernel function which integrates this equation at each timestep `particles.dt`. To do so, we need the ocean velocity field `fieldset.UV` at the `particles` location, and compute the change in position, `particles.dlon` and `particles.dlat`.
10197

10298
```python
103-
# Create a custom kernel which displaces each particle southward
99+
def AdvectionEE(particles, fieldset):
100+
"""Advection of particles using Explicit Euler (aka Euler Forward) integration."""
101+
(u1, v1) = fieldset.UV[particles]
102+
particles.dlon += u1 * particles.dt
103+
particles.dlat += v1 * particles.dt
104+
```
104105

105-
def NorthVel(particles, fieldset):
106-
vvel = -1e-4
107-
particles.dlat += vvel * particles.dt
106+
Basic kernels are included in Parcels to compute advection and diffusion. The standard advection kernel is `parcels.kernels.AdvectionRK4`, a [4-th order Runge-Kutta integrator](https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods#The_Runge%E2%80%93Kutta_method) of the advection function.
108107

108+
```{warning}
109+
It is advised _not_ to update the particle coordinates (`particles.time`, `particles.z`, `particles.lat`, or `particles.lon`) directly, as that can negatively interfere with the way that particle movements by different kernels are vectorially added. Use a change in the coordinates: `particles.dlon`, `particles.dlat`, `particles.dz`, and/or `particles.dt` instead, and be careful with `particles.dt`. Read the [kernel loop tutorial](https://docs.oceanparcels.org/en/latest/examples/tutorial_kernelloop.html) to understand why.
110+
```
109111

110-
# Create a custom kernel which keeps track of the particle age
112+
(custom-kernel)=
113+
We can write custom kernels to add many different types of 'behaviour' to the particles. To do so we write a function with two arguments: `particles` and `fieldset`. We can then write any computation as a function of any variables defined in the `Particle` and any `Field` defined in the `FieldSet`. Kernels can then be combined by creating a `list` of the kernels. The kernels are executed in order:
114+
115+
```python
116+
# Create a custom Particle object with an "age" variable
117+
CustomParticle = parcels.Particle.add_variable(
118+
parcels.Variable("age", initial=np.nan)
119+
)
111120

121+
# Create a custom kernel which keeps track of the particle age
112122
def Age(particles, fieldset):
113123
particles.age += particles.dt
114124

115125
# define all kernels to be executed on particles using an (ordered) list
116-
kernels = [Age, NorthVel, parcels.kernels.AdvectionRK4]
126+
kernels = [Age, parcels.kernels.AdvectionRK4]
117127
```
118128

119129
```{note}
120130
Every Kernel must be a function with the following (and only those) arguments: `(particles, fieldset)`
121131
```
122132

123-
```{warning}
124-
It is advised _not_ to update the particle coordinates (`particles.time`, `particles.z`, `particles.lat`, or `particles.lon`) directly, as that can negatively interfere with the way that particle movements by different kernels are vectorially added. Use a change in the coordinates: `particles.dlon`, `particles.dlat`, `particles.dz`, and/or `particles.dt` instead, and be careful with `particles.dt`. See also the [kernel loop tutorial](https://docs.oceanparcels.org/en/latest/examples/tutorial_kernelloop.html).
125-
```
126-
127133
```{warning}
128134
We have to be careful with writing kernels for vector fields on Curvilinear grids. While Parcels automatically rotates the "U" and "V" field when necessary, this is not the case for other fields such as Stokes drift. [This guide](../user_guide/examples/tutorial_nemo_curvilinear.ipynb) describes how to use a curvilinear grid in Parcels.
129135
```
130136

131-
### Read more about
137+
### Read more about the Kernel loop
132138

133139
- [The Kernel loop](../user_guide/examples/explanation_kernelloop.md)
134140

135-
### Learn how to
141+
### Learn how to write kernels
136142

137143
- [Sample fields like temperature](../user_guide/examples/tutorial_sampling.ipynb).
138144
- [Mimic the behaviour of ARGO floats](../user_guide/examples/tutorial_Argofloats.ipynb).
139145
- [Add diffusion to approximate subgrid-scale processes and unresolved physics](../user_guide/examples/tutorial_diffusion.ipynb).
140-
- [Convert between units in m/s and degree/s](../user_guide/examples/tutorial_unitconverters.ipynb).
146+
- [Convert between units in m/s and degrees](../user_guide/examples/tutorial_unitconverters.ipynb).
141147

142-
## 4. Execution
148+
## 4. Execute
143149

144-
The execution of the simulation, given the `FieldSet`, `ParticleSet`, and `Kernels` defined in the previous steps, is done using the method **`parcels.ParticleSet.execute()`**. This method requires the following arguments:
150+
The execution of the simulation is done using the method **`parcels.ParticleSet.execute()`**, given the `FieldSet`, `ParticleSet`, and `Kernels` defined in the previous steps. This method requires the following arguments:
145151

146152
1. The kernels to be executed.
147153
2. The `runtime` defining how long the execution loop runs. Alternatively, you may define the `endtime` at which the execution loop stops.
@@ -156,5 +162,5 @@ There are many ways to analyze particle output, and although we provide [a short
156162

157163
#### Learn how to
158164

159-
- [choose an appropriate timestep](../user_guide/examples/tutorial_numerical_accuracy.ipynb)
160-
- [work with Parcels output](./tutorial_output.ipynb)
165+
- [Choose an appropriate timestep](../user_guide/examples/tutorial_numerical_accuracy.ipynb)
166+
- [Work with Parcels output](./tutorial_output.ipynb)

docs/getting_started/index.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,5 @@ Concepts explanation <explanation_concepts.md>
1111
```
1212

1313
```{note}
14-
TODO: Add links to Reference API in quickstart tutorial
15-
```
16-
17-
```{note}
18-
TODO: Rewrite parcels concepts overview. This .md file should contain most of what is currently in the tutorial_parcels_structure notebook.
14+
TODO: Add links to Reference API in quickstart tutorial and concepts explanation
1915
```

docs/user_guide/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ The tutorials written for Parcels v3 are currently being updated for Parcels v4.
1515

1616
## How to
1717

18+
```{note}
19+
TODO: Add links to Reference API throughout
20+
```
21+
1822
```{note}
1923
**Migrate from v3 to v4** using [this migration guide](v4-migration.md)
2024
```

0 commit comments

Comments
 (0)