Skip to content

Commit a66fc3b

Browse files
Fix flat face coloring and add particle interpolation test section
Still need to clean up the particle color plots. Almost there..
1 parent 182688a commit a66fc3b

2 files changed

Lines changed: 98 additions & 14 deletions

File tree

docs/user_guide/examples/tutorial_interpolation.ipynb

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,70 @@
294294
"fieldset = parcels.FieldSet([U, V, UV, Tnode, Tface])"
295295
]
296296
},
297+
{
298+
"cell_type": "markdown",
299+
"metadata": {},
300+
"source": [
301+
"We can now create particles to sample data using either the node registered or face registered data."
302+
]
303+
},
304+
{
305+
"cell_type": "code",
306+
"execution_count": null,
307+
"metadata": {},
308+
"outputs": [],
309+
"source": [
310+
"SampleParticle = parcels.Particle.add_variable(\n",
311+
" parcels.Variable(\"Tracer\", dtype=np.float32, initial=np.nan)\n",
312+
")\n",
313+
"\n",
314+
"\n",
315+
"def SampleTracer_Node(particles, fieldset):\n",
316+
" particles.Tracer = fieldset.T_node[particles]\n",
317+
"\n",
318+
"\n",
319+
"def SampleTracer_Face(particles, fieldset):\n",
320+
" particles.Tracer = fieldset.T_face[particles]"
321+
]
322+
},
323+
{
324+
"cell_type": "markdown",
325+
"metadata": {},
326+
"source": []
327+
},
328+
{
329+
"cell_type": "code",
330+
"execution_count": null,
331+
"metadata": {},
332+
"outputs": [],
333+
"source": [
334+
"print(fieldset.fields)"
335+
]
336+
},
337+
{
338+
"cell_type": "code",
339+
"execution_count": null,
340+
"metadata": {},
341+
"outputs": [],
342+
"source": [
343+
"pset = {}\n",
344+
"\n",
345+
"xv, yv = np.meshgrid(np.linspace(0.2, 0.8, 8), np.linspace(0.2, 0.9, 8))\n",
346+
"pset[\"node\"] = parcels.ParticleSet(\n",
347+
" fieldset, pclass=SampleParticle, lon=xv.flatten(), lat=yv.flatten()\n",
348+
")\n",
349+
"pset[\"node\"].execute(\n",
350+
" SampleTracer_Node, runtime=np.timedelta64(1, \"D\"), dt=np.timedelta64(1, \"D\")\n",
351+
")\n",
352+
"\n",
353+
"pset[\"face\"] = parcels.ParticleSet(\n",
354+
" fieldset, pclass=SampleParticle, lon=xv.flatten(), lat=yv.flatten()\n",
355+
")\n",
356+
"pset[\"face\"].execute(\n",
357+
" SampleTracer_Face, runtime=np.timedelta64(1, \"D\"), dt=np.timedelta64(1, \"D\")\n",
358+
")"
359+
]
360+
},
297361
{
298362
"cell_type": "code",
299363
"execution_count": null,
@@ -330,6 +394,17 @@
330394
" x, y, c=T_node, cmap=\"viridis\", s=150, edgecolors=\"black\", vmin=-1.0, vmax=1.0\n",
331395
")\n",
332396
"\n",
397+
"ax[0].scatter(\n",
398+
" pset[\"node\"].lon,\n",
399+
" pset[\"node\"].lat,\n",
400+
" c=pset[\"node\"].Tracer,\n",
401+
" cmap=\"viridis\",\n",
402+
" edgecolors=\"k\",\n",
403+
" s=50,\n",
404+
" vmin=-1.0,\n",
405+
" vmax=1.0,\n",
406+
")\n",
407+
"\n",
333408
"cbar = fig.colorbar(sc1, ax=ax[0])\n",
334409
"cbar.set_label(\"T\")\n",
335410
"\n",
@@ -340,9 +415,10 @@
340415
"\n",
341416
"# # Plot the face registered data\n",
342417
"T_face = np.squeeze(ds[\"T_face\"][0, 0, :].values)\n",
343-
"tpc = ax[1].tripcolor(\n",
418+
"assert triang.triangles.shape[0] == T_face.shape[0]\n",
419+
"tpc2 = ax[1].tripcolor(\n",
344420
" triang,\n",
345-
" T_face,\n",
421+
" facecolors=T_face,\n",
346422
" shading=\"flat\",\n",
347423
" cmap=\"viridis\",\n",
348424
" edgecolors=\"k\",\n",
@@ -353,14 +429,22 @@
353429
"# Plot the element edges\n",
354430
"ax[1].triplot(triang, color=\"black\", linewidth=0.5)\n",
355431
"\n",
356-
"# Plot the node registered data\n",
432+
"# Plot the face registered data\n",
357433
"xf = ds.uxgrid.face_lon.values\n",
358434
"yf = ds.uxgrid.face_lat.values\n",
359-
"sc2 = ax[1].scatter(\n",
360-
" xf, yf, c=T_face, cmap=\"viridis\", s=150, edgecolors=\"black\", vmin=-1.0, vmax=1.0\n",
435+
"\n",
436+
"ax[1].scatter(\n",
437+
" pset[\"face\"].lon,\n",
438+
" pset[\"face\"].lat,\n",
439+
" c=pset[\"face\"].Tracer,\n",
440+
" cmap=\"viridis\",\n",
441+
" edgecolors=\"k\",\n",
442+
" s=50,\n",
443+
" vmin=-1.0,\n",
444+
" vmax=1.0,\n",
361445
")\n",
362446
"\n",
363-
"cbar = fig.colorbar(sc2, ax=ax[1])\n",
447+
"cbar = fig.colorbar(tpc, ax=ax[1])\n",
364448
"cbar.set_label(\"T\")\n",
365449
"\n",
366450
"ax[1].set_aspect(\"equal\", \"box\")\n",

src/parcels/_datasets/unstructured/generated.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ def simple_small_delaunay(nx=10, ny=10):
4343
Tface = np.zeros((1, nz1, uxgrid.n_face), dtype=np.float64)
4444

4545
for i, (x, y) in enumerate(zip(uxgrid.face_lon, uxgrid.face_lat, strict=False)):
46-
P[0, 0, i] = -vmax * delta * (1 - x) * (math.exp(-x / delta) - 1) * np.sin(math.pi * y)
47-
U[0, 0, i] = -vmax * (1 - math.exp(-x / delta) - x) * np.cos(math.pi * y)
48-
V[0, 0, i] = vmax * ((2.0 - x) * math.exp(-x / delta) - 1) * np.sin(math.pi * y)
49-
Tface[0, 0, i] = np.sin(math.pi * y) * np.cos(math.pi * x)
46+
P[0, :, i] = -vmax * delta * (1 - x) * (math.exp(-x / delta) - 1) * np.sin(math.pi * y)
47+
U[0, :, i] = -vmax * (1 - math.exp(-x / delta) - x) * np.cos(math.pi * y)
48+
V[0, :, i] = vmax * ((2.0 - x) * math.exp(-x / delta) - 1) * np.sin(math.pi * y)
49+
Tface[0, :, i] = np.sin(math.pi * y) * np.cos(math.pi * x)
5050

51-
Tnode = np.zeros((1, nz1, uxgrid.n_node), dtype=np.float64)
51+
Tnode = np.zeros((1, nz, uxgrid.n_node), dtype=np.float64)
5252
for i, (x, y) in enumerate(zip(uxgrid.node_lon, uxgrid.node_lat, strict=False)):
53-
Tnode[0, 0, i] = np.sin(math.pi * y) * np.cos(math.pi * x)
53+
Tnode[0, :, i] = np.sin(math.pi * y) * np.cos(math.pi * x)
5454

5555
u = ux.UxDataArray(
5656
data=U,
@@ -124,10 +124,10 @@ def simple_small_delaunay(nx=10, ny=10):
124124
data=Tnode,
125125
name="T_node",
126126
uxgrid=uxgrid,
127-
dims=["time", "nz1", "n_node"],
127+
dims=["time", "nz", "n_node"],
128128
coords=dict(
129129
time=(["time"], [TIME[0]]),
130-
nz1=(["nz1"], zc),
130+
nz1=(["nz"], zf),
131131
),
132132
attrs=dict(
133133
description="Tracer field sampled on face vertices",

0 commit comments

Comments
 (0)