Skip to content

Commit 11179ed

Browse files
Updating tutorials that combine fields (#2740)
* Updating tutorials that combine fields The particle release was not very good; too close to the boundary. Also made a few other fixes, such as using RK2 for wind advection and improving wind field * Update tutorial_manipulating_field_data.ipynb * Remove time and depth dimension from field
1 parent a6bb153 commit 11179ed

2 files changed

Lines changed: 23 additions & 21 deletions

File tree

docs/user_guide/examples/explanation_kernelloop.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ ds_fields.load() # load the dataset into memory
6262
# Create an idealised wind field and add it to the dataset
6363
tdim, ydim, xdim = (len(ds_fields.time),len(ds_fields.latitude), len(ds_fields.longitude))
6464
ds_fields["UWind"] = xr.DataArray(
65-
data=np.ones((tdim, ydim, xdim)) * np.sin(ds_fields.latitude.values)[None, :, None],
65+
data=0.5 * np.ones((tdim, ydim, xdim)) * np.sin(ds_fields.latitude.values - ds_fields.latitude.values.mean())[None, :, None],
6666
coords=[ds_fields.time, ds_fields.latitude, ds_fields.longitude])
6767
6868
ds_fields["VWind"] = xr.DataArray(
@@ -88,10 +88,12 @@ fieldset = parcels.FieldSet.from_sgrid_conventions(
8888
Now we define a wind Kernel that uses a forward Euler method to apply the wind forcing. Note that we update the `particles.dx` and `particles.dy` variables, rather than `particles.x` and `particles.y` directly.
8989

9090
```{code-cell}
91-
def wind_kernel(particles, fieldset):
92-
uwind, vwind = fieldset.Wind[particles]
93-
particles.dx += uwind * particles.dt
94-
particles.dy += vwind * particles.dt
91+
def windRK2(particles, fieldset):
92+
(u1, v1) = fieldset.Wind[particles]
93+
x1, y1 = (particles.x + u1 * 0.5 * particles.dt, particles.y + v1 * 0.5 * particles.dt)
94+
(u2, v2) = fieldset.Wind[particles.t + 0.5 * particles.dt, particles.z, y1, x1, particles]
95+
particles.dx += u2 * particles.dt
96+
particles.dy += v2 * particles.dt
9597
```
9698

9799
First run a simulation where we apply Kernels as `[AdvectionRK2, wind_kernel]`
@@ -100,22 +102,22 @@ First run a simulation where we apply Kernels as `[AdvectionRK2, wind_kernel]`
100102
:tags: [hide-output]
101103
npart = 10
102104
z = np.repeat(ds_fields.depth[0].values, npart)
103-
lons = np.repeat(31, npart)
105+
lons = np.repeat(32.2, npart)
104106
lats = np.linspace(-32.5, -30.5, npart)
105107
106108
pset = parcels.ParticleSet(fieldset, pclass=parcels.Particle, z=z, y=lats, x=lons)
107109
output_file = parcels.ParticleFile(
108110
path="advection_then_wind.parquet", outputdt=np.timedelta64(6,'h')
109111
)
110112
pset.execute(
111-
[parcels.kernels.AdvectionRK2, wind_kernel],
113+
[parcels.kernels.AdvectionRK2, windRK2],
112114
runtime=np.timedelta64(5,'D'),
113115
dt=np.timedelta64(1,'h'),
114116
output_file=output_file,
115117
)
116118
```
117119

118-
Then also run a simulation where we apply the Kernels in the reverse order as `[wind_kernel, AdvectionRK2]`
120+
Then also run a simulation where we apply the Kernels in the reverse order as `[windRK2, AdvectionRK2]`
119121

120122
```{code-cell}
121123
:tags: [hide-output]
@@ -126,7 +128,7 @@ output_file_reverse = parcels.ParticleFile(
126128
path="wind_then_advection.parquet", outputdt=np.timedelta64(6,"h")
127129
)
128130
pset_reverse.execute(
129-
[wind_kernel, parcels.kernels.AdvectionRK2],
131+
[windRK2, parcels.kernels.AdvectionRK2],
130132
runtime=np.timedelta64(5,"D"),
131133
dt=np.timedelta64(1,"h"),
132134
output_file=output_file_reverse,

docs/user_guide/examples/tutorial_manipulating_field_data.ipynb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,17 @@
5252
"ds_fields.load() # load the dataset into memory\n",
5353
"\n",
5454
"# Create an idealised wind field and add it to the dataset\n",
55-
"tdim, ydim, xdim = (\n",
56-
" len(ds_fields.time),\n",
57-
" len(ds_fields.latitude),\n",
58-
" len(ds_fields.longitude),\n",
59-
")\n",
55+
"ydim, xdim = len(ds_fields.latitude), len(ds_fields.longitude)\n",
6056
"ds_fields[\"UWind\"] = xr.DataArray(\n",
61-
" data=np.ones((tdim, ydim, xdim)) * np.sin(ds_fields.latitude.values)[None, :, None],\n",
62-
" coords=[ds_fields.time, ds_fields.latitude, ds_fields.longitude],\n",
57+
" data=0.5\n",
58+
" * np.ones((ydim, xdim))\n",
59+
" * np.sin(ds_fields.latitude.values - ds_fields.latitude.values.mean())[:, None],\n",
60+
" coords=[ds_fields.latitude, ds_fields.longitude],\n",
6361
")\n",
6462
"\n",
6563
"ds_fields[\"VWind\"] = xr.DataArray(\n",
66-
" data=np.zeros((tdim, ydim, xdim)),\n",
67-
" coords=[ds_fields.time, ds_fields.latitude, ds_fields.longitude],\n",
64+
" data=np.zeros((ydim, xdim)),\n",
65+
" coords=[ds_fields.latitude, ds_fields.longitude],\n",
6866
")"
6967
]
7068
},
@@ -126,12 +124,13 @@
126124
"\n",
127125
"npart = 10\n",
128126
"z = np.repeat(ds_fields.depth[0].values, npart)\n",
129-
"lons = np.repeat(31, npart)\n",
127+
"lons = np.repeat(32.2, npart)\n",
130128
"lats = np.linspace(-32.5, -30.5, npart)\n",
131129
"\n",
132130
"pset = parcels.ParticleSet(fieldset, pclass=parcels.Particle, z=z, y=lats, x=lons)\n",
133131
"output_file = parcels.ParticleFile(\n",
134-
" path=\"summed_advection_wind.parquet\", outputdt=np.timedelta64(6, \"h\")\n",
132+
" path=\"summed_advection_wind.parquet\",\n",
133+
" outputdt=np.timedelta64(6, \"h\"),\n",
135134
")\n",
136135
"pset.execute(\n",
137136
" [parcels.kernels.AdvectionRK2],\n",
@@ -158,8 +157,9 @@
158157
"source": [
159158
"# Plot the resulting particle trajectories overlapped for both cases\n",
160159
"summed_advection_wind = parcels.read_particlefile(\"summed_advection_wind.parquet\")\n",
160+
"fig, ax = plt.subplots(figsize=(5, 3))\n",
161161
"for traj in summed_advection_wind.partition_by(\"particle_id\", maintain_order=True):\n",
162-
" plt.plot(traj[\"x\"], traj[\"y\"], \"-\")\n",
162+
" ax.plot(traj[\"x\"], traj[\"y\"], \"-\")\n",
163163
"plt.show()"
164164
]
165165
}

0 commit comments

Comments
 (0)