Skip to content

Commit 586689d

Browse files
reint-fischerreint-fischer
authored andcommitted
fix adding field to fieldset
1 parent 05afbc2 commit 586689d

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

docs/user_guide/examples/tutorial_diffusion.ipynb

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,17 @@
7676
"\n",
7777
"The spatial derivatives in the EM and M1 schemes can be approximated by a central difference. Higher order numerical schemes (see [Gräwe et al., 2012](https://doi.org/10.1007/s10236-012-0523-y)) include higher order derivatives.\n",
7878
"\n",
79+
"```{note}\n",
80+
"TODO: explore higher order derivatives and numerical schemes (using cubic spline interpolation) in v4 \n",
81+
"```\n",
82+
"\n",
7983
"An overview of numerical approximations for SDEs in a particle tracking setting can be found in [Gräwe (2011)](https://doi.org/10.1016/j.ocemod.2010.10.002).\n",
8084
"\n",
8185
"## Using Advection-Diffusion Kernels in Parcels\n",
8286
"\n",
8387
"The EM and M1 advection-diffusion approximations are available as `AdvectionDiffusionEM` and `AdvectionDiffusionM1`, respectively. The `AdvectionDiffusionM1` kernel should be the default choice, as the increased accuracy comes at negligible computational cost.\n",
8488
"\n",
85-
"The advection component of these kernels is similar to that of the Explicit Euler advection kernel (`AdvectionEE`). In the special case where diffusivity is constant over the entire domain, the diffusion-only kernel `DiffusionUniformKh` can be used in combination with an advection kernel of choice. Since the diffusivity here is space-independent, gradients are not calculated, increasing efficiency. The diffusion-step can in this case be computed after or before advection, thus allowing you to chain kernels using the `+` operator.\n",
89+
"The advection component of these kernels is similar to that of the Explicit Euler advection kernel (`AdvectionEE`). In the special case where diffusivity is constant over the entire domain, the diffusion-only kernel `DiffusionUniformKh` can be used in combination with an advection kernel of choice. Since the diffusivity here is space-independent, gradients are not calculated, increasing efficiency. The diffusion-step can in this case be computed after or before advection, thus allowing you to chain kernels in a list.\n",
8690
"\n",
8791
"Just like velocities, diffusivities are passed to Parcels in the form of `Field` objects. When using `DiffusionUniformKh`, they should be added to the `FieldSet` object as constant fields, e.g. `fieldset.add_constant_field(\"Kh_zonal\", 1, mesh=\"flat\")`.\n",
8892
"\n",
@@ -193,8 +197,6 @@
193197
"ds = simple_UV_dataset(dims=(1, 1, Ny, 1), mesh=\"flat\").isel(time=0, depth=0)\n",
194198
"ds[\"lat\"][:] = np.linspace(-0.01, 1.01, Ny)\n",
195199
"ds[\"lon\"][:] = np.ones(len(ds.XG))\n",
196-
"# dx, dy = 1.0 / len(ds.XG), 1.0 / len(ds.YG)\n",
197-
"ds[\"Kh_zonal\"] = ds[\"U\"] + K_bar * np.ones((Ny, 1))\n",
198200
"ds[\"Kh_meridional\"] = ds[\"U\"] + Kh_meridional[:, None]\n",
199201
"ds"
200202
]
@@ -209,16 +211,15 @@
209211
"U = parcels.Field(\"U\", ds[\"U\"], grid, interp_method=parcels.interpolators.XLinear)\n",
210212
"V = parcels.Field(\"V\", ds[\"V\"], grid, interp_method=parcels.interpolators.XLinear)\n",
211213
"UV = parcels.VectorField(\"UV\", U, V)\n",
212-
"Kh_zonal_field = parcels.Field(\n",
213-
" \"Kh_zonal\", ds[\"Kh_zonal\"], grid, interp_method=parcels.interpolators.XLinear\n",
214-
")\n",
214+
"\n",
215215
"Kh_meridional_field = parcels.Field(\n",
216216
" \"Kh_meridional\",\n",
217217
" ds[\"Kh_meridional\"],\n",
218218
" grid,\n",
219219
" interp_method=parcels.interpolators.XLinear,\n",
220220
")\n",
221-
"fieldset = parcels.FieldSet([U, V, UV, Kh_zonal_field, Kh_meridional_field])\n",
221+
"fieldset = parcels.FieldSet([U, V, UV, Kh_meridional_field])\n",
222+
"fieldset.add_constant_field(\"Kh_zonal\", 1, mesh=\"flat\")\n",
222223
"\n",
223224
"fieldset.add_constant(\"dres\", 0.00005)"
224225
]
@@ -244,7 +245,6 @@
244245
" lon=np.zeros(100),\n",
245246
" lat=np.ones(100) * 0.75,\n",
246247
" time=np.repeat(np.timedelta64(0, \"s\"), 100),\n",
247-
" # lonlatdepth_dtype=np.float64,\n",
248248
" )"
249249
]
250250
},
@@ -539,22 +539,21 @@
539539
"da_cell_areas = xr.DataArray(\n",
540540
" data=calc_cell_areas(fieldset.U.grid),\n",
541541
" coords=dict(\n",
542-
" latitude=([\"latitude\"], ds_fields.latitude.values),\n",
543-
" longitude=([\"longitude\"], ds_fields.longitude.values),\n",
542+
" latitude=([\"lat\"], ds_fields.latitude.values),\n",
543+
" longitude=([\"lon\"], ds_fields.longitude.values),\n",
544544
" ),\n",
545-
" dims=[\"latitude\", \"longitude\"],\n",
545+
" dims=[\"lat\", \"lon\"],\n",
546546
")\n",
547547
"\n",
548-
"ds_fields[\"cell_areas\"] = da_cell_areas\n",
549-
"ds_fields[\"latitude\"].attrs[\"axis\"] = \"Y\"\n",
550-
"ds_fields[\"longitude\"].attrs[\"axis\"] = \"X\"\n",
551-
"\n",
552-
"# TODO: add parcels.Field instead of creating new fieldset once defining from existing XGrid is implemented\n",
553-
"fieldset = parcels.FieldSet.from_copernicusmarine(ds_fields)\n",
554-
"\n",
555-
"fieldset.add_constant(\"Cs\", 0.1)\n",
548+
"cell_areas_field = parcels.Field(\n",
549+
" \"cell_areas\",\n",
550+
" da_cell_areas,\n",
551+
" fieldset.U.grid,\n",
552+
" interp_method=parcels.interpolators.XNearest,\n",
553+
")\n",
554+
"fieldset.add_field(cell_areas_field)\n",
556555
"\n",
557-
"ds_fields"
556+
"fieldset.add_constant(\"Cs\", 0.1)"
558557
]
559558
},
560559
{
@@ -645,11 +644,6 @@
645644
"\n",
646645
"Smagorinsky, J. (1963). “General circulation experiments with primitive equations. 1. The basic experiment.” _Monthly Weather Review_, 91, 99–164. https://doi.org/10.1175/1520-0493(1963)091%3C0099:GCEWTP%3E2.3.CO;2\n"
647646
]
648-
},
649-
{
650-
"cell_type": "markdown",
651-
"metadata": {},
652-
"source": []
653647
}
654648
],
655649
"metadata": {

0 commit comments

Comments
 (0)