Skip to content

Commit 9b2d0b7

Browse files
committed
Updates to the idealised example to use 'from_modulefile' method rather than importing the functions and creating in script.
1 parent ba08b05 commit 9b2d0b7

2 files changed

Lines changed: 64 additions & 53 deletions

File tree

docs/examples/example_idealised_flow.ipynb

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
"metadata": {},
3737
"source": [
3838
"## Create an idealised flow\n",
39-
"Here, we create a 3D version of the Bickley Jet (see [Hadjighasem et al (2017)](https://aip.scitation.org/doi/10.1063/1.4982720) for details), where the velocities linearly decay with depth. In order to compute a settling velocity of plastic particles (via density differences), we create a spatially-uniform temperature and salinity field. We also include spatiotemporally varying wind, wave, and biogeochemical fields used for windage drift, Stokes drift, and biofouling kernels, respectively.\n",
39+
"Here, we create a 3D version of the Bickley Jet (see [Hadjighasem et al (2017)](https://aip.scitation.org/doi/10.1063/1.4982720) for details), where the velocities linearly decay with depth. In order to compute a settling velocity of plastic particles (via density differences), we create a spatially-uniform temperature and salinity field. We also include spatiotemporally varying wind, wave, and biogeochemical fields used for windage drift, Stokes drift, and biofouling kernels, respectively. We also include an 'unbeaching' field, where velocities along the top and bottom of the domain will 'kick-back' any particles that are advected across these two boundaries. The code to generate the idealised flow fields can be found in the `idealised_flow.py` file.\n",
4040
"\n",
41-
"Since the flow is periodic in the zonal direction, we write a custom zonal boundary conditions kernel to overwrite the `plasticparcels` periodic boundary condition kernel used for longitudinal coordinates. We also include an 'unbeaching' field, where velocities along the top and bottom of the domain will 'kick-back' any particles that are advected across these two boundaries. The code to generate the idealised flow fields can be found in the `example_idealised_flow_functions.py` file."
41+
"Since the flow is periodic in the zonal direction, we write a custom zonal boundary conditions kernel to overwrite the `plasticparcels` periodic boundary condition kernel used for longitudinal coordinates."
4242
]
4343
},
4444
{
@@ -47,25 +47,29 @@
4747
"metadata": {},
4848
"outputs": [],
4949
"source": [
50-
"# Import functions to create the idealised flow fields\n",
51-
"from example_idealised_flow_functions import bickleyjet_fieldset_3d, add_uniform_temp_salt_field, add_biogeochemistry_field, add_wind_field, add_stokes_field, ZonalBC\n",
52-
"\n",
53-
"# List of times the analytic fieldset is evaluated on\n",
54-
"times = np.arange(0, 5.1, 0.1) * 86400\n",
55-
"\n",
56-
"# Create the fieldset\n",
57-
"fieldset = bickleyjet_fieldset_3d(times=times)\n",
58-
"fieldset = add_uniform_temp_salt_field(fieldset, times)\n",
59-
"fieldset = add_biogeochemistry_field(fieldset, times)\n",
60-
"fieldset = add_wind_field(fieldset, times)\n",
61-
"fieldset = add_stokes_field(fieldset, times)\n",
50+
"# Load the fieldset\n",
51+
"fieldset = parcels.FieldSet.from_modulefile('idealised_flow.py')\n",
6252
"\n",
6353
"# Add a periodic halo in the zonal direction\n",
6454
"fieldset.add_constant(\"halo_west\", fieldset.U.grid.lon[0])\n",
6555
"fieldset.add_constant(\"halo_east\", fieldset.U.grid.lon[-1])\n",
6656
"fieldset.add_periodic_halo(zonal=True)"
6757
]
6858
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": 3,
62+
"metadata": {},
63+
"outputs": [],
64+
"source": [
65+
"# Define a custom kernel to handle the periodic boundary conditions\n",
66+
"def ZonalBC(particle, fieldset, time):\n",
67+
" if particle.lon < fieldset.halo_west:\n",
68+
" particle_dlon += fieldset.halo_east - fieldset.halo_west\n",
69+
" elif particle.lon > fieldset.halo_east:\n",
70+
" particle_dlon -= fieldset.halo_east - fieldset.halo_west"
71+
]
72+
},
6973
{
7074
"cell_type": "markdown",
7175
"metadata": {},
@@ -76,7 +80,7 @@
7680
},
7781
{
7882
"cell_type": "code",
79-
"execution_count": 3,
83+
"execution_count": 4,
8084
"metadata": {},
8185
"outputs": [
8286
{
@@ -129,7 +133,7 @@
129133
},
130134
{
131135
"cell_type": "code",
132-
"execution_count": 4,
136+
"execution_count": 5,
133137
"metadata": {},
134138
"outputs": [],
135139
"source": [
@@ -187,7 +191,7 @@
187191
},
188192
{
189193
"cell_type": "code",
190-
"execution_count": 5,
194+
"execution_count": 6,
191195
"metadata": {},
192196
"outputs": [],
193197
"source": [
@@ -198,7 +202,7 @@
198202
},
199203
{
200204
"cell_type": "code",
201-
"execution_count": 6,
205+
"execution_count": 7,
202206
"metadata": {},
203207
"outputs": [],
204208
"source": [
@@ -210,7 +214,7 @@
210214
},
211215
{
212216
"cell_type": "code",
213-
"execution_count": 7,
217+
"execution_count": 8,
214218
"metadata": {},
215219
"outputs": [],
216220
"source": [
@@ -263,7 +267,7 @@
263267
},
264268
{
265269
"cell_type": "code",
266-
"execution_count": 8,
270+
"execution_count": 9,
267271
"metadata": {},
268272
"outputs": [],
269273
"source": [
@@ -278,7 +282,7 @@
278282
},
279283
{
280284
"cell_type": "code",
281-
"execution_count": 9,
285+
"execution_count": 10,
282286
"metadata": {},
283287
"outputs": [],
284288
"source": [
@@ -293,15 +297,15 @@
293297
},
294298
{
295299
"cell_type": "code",
296-
"execution_count": 10,
300+
"execution_count": 11,
297301
"metadata": {},
298302
"outputs": [
299303
{
300304
"name": "stdout",
301305
"output_type": "stream",
302306
"text": [
303307
"INFO: Output files are stored in example_idealised_flow_experiment_1.zarr.\n",
304-
"100%|██████████| 345600.0/345600.0 [00:39<00:00, 8722.69it/s]\n"
308+
"100%|██████████| 345600.0/345600.0 [00:38<00:00, 9005.98it/s]\n"
305309
]
306310
}
307311
],
@@ -312,7 +316,7 @@
312316
},
313317
{
314318
"cell_type": "code",
315-
"execution_count": 11,
319+
"execution_count": 12,
316320
"metadata": {},
317321
"outputs": [
318322
{
@@ -342,7 +346,7 @@
342346
},
343347
{
344348
"cell_type": "code",
345-
"execution_count": 12,
349+
"execution_count": 13,
346350
"metadata": {},
347351
"outputs": [],
348352
"source": [
@@ -358,7 +362,7 @@
358362
},
359363
{
360364
"cell_type": "code",
361-
"execution_count": 13,
365+
"execution_count": 14,
362366
"metadata": {},
363367
"outputs": [],
364368
"source": [
@@ -373,15 +377,15 @@
373377
},
374378
{
375379
"cell_type": "code",
376-
"execution_count": 14,
380+
"execution_count": 15,
377381
"metadata": {},
378382
"outputs": [
379383
{
380384
"name": "stdout",
381385
"output_type": "stream",
382386
"text": [
383387
"INFO: Output files are stored in example_idealised_flow_experiment_2.zarr.\n",
384-
"100%|██████████| 345600.0/345600.0 [00:49<00:00, 7022.17it/s]\n"
388+
"100%|██████████| 345600.0/345600.0 [00:47<00:00, 7294.32it/s]\n"
385389
]
386390
}
387391
],
@@ -392,7 +396,7 @@
392396
},
393397
{
394398
"cell_type": "code",
395-
"execution_count": 15,
399+
"execution_count": 16,
396400
"metadata": {},
397401
"outputs": [
398402
{
@@ -422,7 +426,7 @@
422426
},
423427
{
424428
"cell_type": "code",
425-
"execution_count": 16,
429+
"execution_count": 17,
426430
"metadata": {},
427431
"outputs": [],
428432
"source": [
@@ -438,7 +442,7 @@
438442
},
439443
{
440444
"cell_type": "code",
441-
"execution_count": 17,
445+
"execution_count": 18,
442446
"metadata": {},
443447
"outputs": [],
444448
"source": [
@@ -453,15 +457,15 @@
453457
},
454458
{
455459
"cell_type": "code",
456-
"execution_count": 18,
460+
"execution_count": 19,
457461
"metadata": {},
458462
"outputs": [
459463
{
460464
"name": "stdout",
461465
"output_type": "stream",
462466
"text": [
463467
"INFO: Output files are stored in example_idealised_flow_experiment_3.zarr.\n",
464-
"100%|██████████| 345600.0/345600.0 [00:55<00:00, 6241.77it/s]\n"
468+
"100%|██████████| 345600.0/345600.0 [00:55<00:00, 6224.79it/s]\n"
465469
]
466470
}
467471
],
@@ -472,7 +476,7 @@
472476
},
473477
{
474478
"cell_type": "code",
475-
"execution_count": 19,
479+
"execution_count": 20,
476480
"metadata": {},
477481
"outputs": [
478482
{
@@ -502,7 +506,7 @@
502506
},
503507
{
504508
"cell_type": "code",
505-
"execution_count": 20,
509+
"execution_count": 21,
506510
"metadata": {},
507511
"outputs": [],
508512
"source": [
@@ -518,7 +522,7 @@
518522
},
519523
{
520524
"cell_type": "code",
521-
"execution_count": 21,
525+
"execution_count": 22,
522526
"metadata": {},
523527
"outputs": [],
524528
"source": [
@@ -533,15 +537,15 @@
533537
},
534538
{
535539
"cell_type": "code",
536-
"execution_count": 22,
540+
"execution_count": 23,
537541
"metadata": {},
538542
"outputs": [
539543
{
540544
"name": "stdout",
541545
"output_type": "stream",
542546
"text": [
543547
"INFO: Output files are stored in example_idealised_flow_experiment_4.zarr.\n",
544-
"100%|██████████| 345600.0/345600.0 [00:49<00:00, 6957.41it/s]\n"
548+
"100%|██████████| 345600.0/345600.0 [00:48<00:00, 7094.99it/s]\n"
545549
]
546550
}
547551
],
@@ -552,7 +556,7 @@
552556
},
553557
{
554558
"cell_type": "code",
555-
"execution_count": 23,
559+
"execution_count": 24,
556560
"metadata": {},
557561
"outputs": [
558562
{
@@ -582,7 +586,7 @@
582586
},
583587
{
584588
"cell_type": "code",
585-
"execution_count": 24,
589+
"execution_count": 25,
586590
"metadata": {},
587591
"outputs": [],
588592
"source": [
@@ -598,7 +602,7 @@
598602
},
599603
{
600604
"cell_type": "code",
601-
"execution_count": 25,
605+
"execution_count": 26,
602606
"metadata": {},
603607
"outputs": [],
604608
"source": [
@@ -613,15 +617,15 @@
613617
},
614618
{
615619
"cell_type": "code",
616-
"execution_count": 26,
620+
"execution_count": 27,
617621
"metadata": {},
618622
"outputs": [
619623
{
620624
"name": "stdout",
621625
"output_type": "stream",
622626
"text": [
623627
"INFO: Output files are stored in example_idealised_flow_experiment_5.zarr.\n",
624-
"100%|██████████| 345600.0/345600.0 [01:08<00:00, 5059.36it/s]\n"
628+
"100%|██████████| 345600.0/345600.0 [01:07<00:00, 5113.66it/s]\n"
625629
]
626630
}
627631
],
@@ -632,7 +636,7 @@
632636
},
633637
{
634638
"cell_type": "code",
635-
"execution_count": 27,
639+
"execution_count": 28,
636640
"metadata": {},
637641
"outputs": [
638642
{
@@ -662,7 +666,7 @@
662666
},
663667
{
664668
"cell_type": "code",
665-
"execution_count": 28,
669+
"execution_count": 29,
666670
"metadata": {},
667671
"outputs": [
668672
{

docs/examples/example_idealised_flow_functions.py renamed to docs/examples/idealised_flow.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@
55
import numpy as np
66
import xarray as xr
77

8+
def create_fieldset(indices=None):
9+
""" Create a fieldset with a Bickley Jet, temperature/salinity, biogeochemistry, wind and Stokes drift fields.
10+
To be used with the parcels.FieldSet.from_modulefile() method."""
11+
# List of times the analytic fieldset is evaluated on
12+
times = np.arange(0, 5.1, 0.1) * 86400
13+
14+
# Create the fieldset object
15+
fieldset = bickleyjet_fieldset_3d(times=times)
16+
fieldset = add_uniform_temp_salt_field(fieldset, times)
17+
fieldset = add_biogeochemistry_field(fieldset, times)
18+
fieldset = add_wind_field(fieldset, times)
19+
fieldset = add_stokes_field(fieldset, times)
20+
21+
return fieldset
822

923
def bickleyjet_fieldset_3d(times, xdim=51, ydim=51, zdim=11):
1024
""" A Bickley Jet Field based on Hadjighasem et al 2017, 10.1063/1.4982720,
@@ -244,11 +258,4 @@ def add_stokes_field(fieldset, times):
244258
fieldset.add_field(fieldsetStokes.Stokes_V)
245259
fieldset.add_field(fieldsetStokes.wave_Tp)
246260

247-
return fieldset
248-
249-
# Define a custom kernel to handle the periodic boundary conditions
250-
def ZonalBC(particle, fieldset, time):
251-
if particle.lon < fieldset.halo_west:
252-
particle_dlon += fieldset.halo_east - fieldset.halo_west
253-
elif particle.lon > fieldset.halo_east:
254-
particle_dlon -= fieldset.halo_east - fieldset.halo_west
261+
return fieldset

0 commit comments

Comments
 (0)