Tutorial on how to combine different Fields for advection into a FieldList object

In some oceanographic applications, you may want to advect particles using a combination of different velocity data sets. For example, particles at the surface are transported by a combination of geostrophic, Ekman and Stokes flow. And often, these flows are not even on the same grid.

One option would be to write a Kernel that computes the movement of particles due to each of these flows. However, in Parcels it is possible to directly combine different flows (without interpolation) and feed them into the built-in AdvectionRK4 kernel. For that, we use so-called FieldList objects.

This tutorial shows how to use these FieldLists with a very idealised example. We start by importing the relevant modules.

In [1]:
from parcels import Field, FieldSet, ParticleSet, JITParticle, plotTrajectoriesFile, AdvectionRK4
import numpy as np

Now, let's first define a zonal and meridional velocity field on a 1kmx1km grid with a flat mesh. The zonal velocity is uniform and 1 m/s, and the meridional velocity is zero everywhere.

In [2]:
xdim, ydim = (10, 20)
U = Field('U', np.ones((ydim, xdim), dtype=np.float32),
          lon=np.linspace(0., 1e3, xdim, dtype=np.float32),
          lat=np.linspace(0., 1e3, ydim, dtype=np.float32))
V = Field('V', np.zeros((ydim, xdim), dtype=np.float32), grid=U.grid)
fieldset = FieldSet(U, V)

We then run a particle and plot its trajectory

In [3]:
pset = ParticleSet(fieldset, pclass=JITParticle, lon=[0], lat=[900])
output_file = pset.ParticleFile(name='FieldListParticle_adv.nc', outputdt=1)
pset.execute(AdvectionRK4, runtime=10, dt=1, output_file=output_file)
plotTrajectoriesFile('FieldListParticle_adv.nc')
INFO: Compiled JITParticleAdvectionRK4 ==> /var/folders/r2/8593q8z93kd7t4j9kbb_f7p00000gn/T/parcels-501/2fccd08be708344bcd942ed95f41d66c.so
Out[3]:
<module 'matplotlib.pyplot' from '/Users/erik/miniconda2/envs/py2_parcels/lib/python2.7/site-packages/matplotlib/pyplot.pyc'>

The trajectory plot shows a particle moving eastward on the 1 m/s flow, as expected

Now, let's define another set of velocities (Ustokes, Vstokes) on a different, higher-resolution grid. This flow is southwestward at -0.2 m/s in each direction.

Note that it is very important to specify the fieldtype of the fields, as Parcels will otherwise not perform the unit conversion correctly.

In [4]:
gf = 10  # factor by which the resolution of this grid is higher than of the original one.
Ustokes = Field('Ustokes', -0.2*np.ones((ydim*gf, xdim*gf), dtype=np.float32),
                lon=np.linspace(0., 1e3, xdim*gf, dtype=np.float32),
                lat=np.linspace(0., 1e3, ydim*gf, dtype=np.float32),
                fieldtype='U')
Vstokes = Field('Vstokes', -0.2*np.ones((ydim*gf, xdim*gf), dtype=np.float32), grid=Ustokes.grid, 
                fieldtype='V')

Now comes the trick of the FieldLists. We can simply define a new FieldSet with a list of different Fields, as in U=[U, Ustokes].

In [5]:
fieldset = FieldSet(U=[U, Ustokes], V=[V, Vstokes])

And if we then run the particle again and plot its trajectory, we see that it moves slightly southward too (and less far eastward), because of the Stokes drift.

In [6]:
pset = ParticleSet(fieldset, pclass=JITParticle, lon=[0], lat=[900])
output_file = pset.ParticleFile(name='FieldListParticle_adv_stokes.nc', outputdt=1)
pset.execute(AdvectionRK4, runtime=10, dt=1, output_file=output_file)
plotTrajectoriesFile('FieldListParticle_adv_stokes.nc')
INFO: Compiled JITParticleAdvectionRK4 ==> /var/folders/r2/8593q8z93kd7t4j9kbb_f7p00000gn/T/parcels-501/2c8d709ac096936d044d226a1fbf84ad.so
Out[6]:
<module 'matplotlib.pyplot' from '/Users/erik/miniconda2/envs/py2_parcels/lib/python2.7/site-packages/matplotlib/pyplot.pyc'>

What happens under the hood is that each Field in the FieldList is interpolated separately to the particle location, and that the different velocities are added in each step of the RK4 advection. So FieldLists are effortless to users

Note that FieldLists work for any type of Field, not only for velocities. Any call to a Field interpolation (fieldset.fld[time, lon, lat, depth]) will return the sum of all Fields in the list if fld is a FieldList.