Skip to content

Commit 27f9c84

Browse files
committed
Strip output and adapt eta definition, ref comment from Jack Hale (#269)
1 parent 1e66dc1 commit 27f9c84

File tree

2 files changed

+44
-44
lines changed

2 files changed

+44
-44
lines changed

chapter2/amr.ipynb

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"id": "4535be91",
5+
"id": "0",
66
"metadata": {},
77
"source": [
88
"# Adaptive mesh refinement with NetGen and DOLFINx\n",
@@ -18,7 +18,7 @@
1818
},
1919
{
2020
"cell_type": "markdown",
21-
"id": "b0282fe2",
21+
"id": "1",
2222
"metadata": {},
2323
"source": [
2424
"In this tutorial, we will consider an adaptive mesh refinement method, applied to\n",
@@ -31,7 +31,7 @@
3131
{
3232
"cell_type": "code",
3333
"execution_count": null,
34-
"id": "32fe6a58",
34+
"id": "2",
3535
"metadata": {},
3636
"outputs": [],
3737
"source": [
@@ -50,7 +50,7 @@
5050
},
5151
{
5252
"cell_type": "markdown",
53-
"id": "4782ba3b",
53+
"id": "3",
5454
"metadata": {},
5555
"source": [
5656
"## Generating a higher-order mesh with NetGen\n",
@@ -60,7 +60,7 @@
6060
{
6161
"cell_type": "code",
6262
"execution_count": null,
63-
"id": "d6046434",
63+
"id": "4",
6464
"metadata": {},
6565
"outputs": [],
6666
"source": [
@@ -80,7 +80,7 @@
8080
},
8181
{
8282
"cell_type": "markdown",
83-
"id": "e041abef",
83+
"id": "5",
8484
"metadata": {},
8585
"source": [
8686
"## Loading a mesh into DOLFINx\n",
@@ -92,7 +92,7 @@
9292
{
9393
"cell_type": "code",
9494
"execution_count": null,
95-
"id": "fc64bf64",
95+
"id": "6",
9696
"metadata": {},
9797
"outputs": [],
9898
"source": [
@@ -101,7 +101,7 @@
101101
},
102102
{
103103
"cell_type": "markdown",
104-
"id": "67cded8b",
104+
"id": "7",
105105
"metadata": {},
106106
"source": [
107107
"Next, we generate the mesh with the function :py:func:`ngsPETSc.utils.fenicsx.GeometricModel.model_to_mesh`.\n",
@@ -112,7 +112,7 @@
112112
{
113113
"cell_type": "code",
114114
"execution_count": null,
115-
"id": "dbae564a",
115+
"id": "8",
116116
"metadata": {},
117117
"outputs": [],
118118
"source": [
@@ -121,7 +121,7 @@
121121
},
122122
{
123123
"cell_type": "markdown",
124-
"id": "f90ca932",
124+
"id": "9",
125125
"metadata": {},
126126
"source": [
127127
"We use pyvista to visualize the mesh."
@@ -130,7 +130,7 @@
130130
{
131131
"cell_type": "code",
132132
"execution_count": null,
133-
"id": "34407248",
133+
"id": "10",
134134
"metadata": {
135135
"tags": [
136136
"hide-input"
@@ -154,7 +154,7 @@
154154
},
155155
{
156156
"cell_type": "markdown",
157-
"id": "6a18b03f",
157+
"id": "11",
158158
"metadata": {},
159159
"source": [
160160
"We have read in any cell and facet markers that have been defined in the NetGen model,\n",
@@ -166,7 +166,7 @@
166166
{
167167
"cell_type": "code",
168168
"execution_count": null,
169-
"id": "adc50da0",
169+
"id": "12",
170170
"metadata": {},
171171
"outputs": [],
172172
"source": [
@@ -176,7 +176,7 @@
176176
},
177177
{
178178
"cell_type": "markdown",
179-
"id": "0a3449b0",
179+
"id": "13",
180180
"metadata": {},
181181
"source": [
182182
"Again, we visualize the curved mesh with pyvista."
@@ -185,7 +185,7 @@
185185
{
186186
"cell_type": "code",
187187
"execution_count": null,
188-
"id": "1eb97f0f",
188+
"id": "14",
189189
"metadata": {
190190
"tags": [
191191
"hide-input"
@@ -206,7 +206,7 @@
206206
},
207207
{
208208
"cell_type": "markdown",
209-
"id": "7613cc7f",
209+
"id": "15",
210210
"metadata": {},
211211
"source": [
212212
"## Solving the eigenvalue problem\n",
@@ -224,7 +224,7 @@
224224
},
225225
{
226226
"cell_type": "markdown",
227-
"id": "717f48a2",
227+
"id": "16",
228228
"metadata": {
229229
"lines_to_next_cell": 2
230230
},
@@ -236,7 +236,7 @@
236236
{
237237
"cell_type": "code",
238238
"execution_count": null,
239-
"id": "d59bc925",
239+
"id": "17",
240240
"metadata": {
241241
"lines_to_next_cell": 2
242242
},
@@ -310,17 +310,17 @@
310310
},
311311
{
312312
"cell_type": "markdown",
313-
"id": "1c251b79",
313+
"id": "18",
314314
"metadata": {
315315
"lines_to_next_cell": 2
316316
},
317317
"source": [
318318
"## Error-indicator\n",
319319
"In this example, we will use an error-indicator $\\eta$ to decide what cells should be refined.\n",
320-
"Specifically, the estimator is:\n",
320+
"Specifically, the estimator $\\eta$ is defined as:\n",
321321
"\n",
322322
"\\begin{align*}\n",
323-
" \\eta = \\sum_{K\\in \\mathcal{T}_h(\\Omega)}\\left(h^2\\int_K \\vert \\lambda u_h + \\Delta u_h\\vert^2~\\mathrm{d}x\\right)\n",
323+
" \\eta^2 = \\sum_{K\\in \\mathcal{T}_h(\\Omega)}\\left(h^2\\int_K \\vert \\lambda u_h + \\Delta u_h\\vert^2~\\mathrm{d}x\\right)\n",
324324
"+ \\sum_{E\\in\\mathcal{F}_i}\\frac{h}{2} \\vert [\\nabla \\cdot \\mathbf{n}_E ]\\vert^2~\\mathrm{d}s\n",
325325
"\\end{align*}\n",
326326
"\n",
@@ -330,15 +330,15 @@
330330
{
331331
"cell_type": "code",
332332
"execution_count": null,
333-
"id": "95f75bd8",
333+
"id": "19",
334334
"metadata": {},
335335
"outputs": [],
336336
"source": [
337337
"def mark_cells(uh_r: dolfinx.fem.Function, lam: float):\n",
338338
" mesh = uh_r.function_space.mesh\n",
339339
" W = dolfinx.fem.functionspace(mesh, (\"DG\", 0))\n",
340340
" w = ufl.TestFunction(W)\n",
341-
" eta = dolfinx.fem.Function(W)\n",
341+
" eta_squared = dolfinx.fem.Function(W)\n",
342342
" f = dolfinx.fem.Constant(mesh, 1.0)\n",
343343
" h = dolfinx.fem.Function(W)\n",
344344
" h.x.array[:] = mesh.h(mesh.topology.dim, np.arange(len(h.x.array), dtype=np.int32))\n",
@@ -349,14 +349,14 @@
349349
" + ufl.inner(h(\"+\") / 2 * ufl.jump(ufl.grad(uh_r), n) ** 2, w(\"+\")) * ufl.dS\n",
350350
" + ufl.inner(h(\"-\") / 2 * ufl.jump(ufl.grad(uh_r), n) ** 2, w(\"-\")) * ufl.dS\n",
351351
" )\n",
352-
" dolfinx.fem.petsc.assemble_vector(eta.x.petsc_vec, dolfinx.fem.form(G))\n",
353-
" sqrt_eta = dolfinx.fem.Function(W)\n",
354-
" sqrt_eta.x.array[:] = np.sqrt(eta.x.array[:])\n",
352+
" dolfinx.fem.petsc.assemble_vector(eta_squared.x.petsc_vec, dolfinx.fem.form(G))\n",
353+
" eta = dolfinx.fem.Function(W)\n",
354+
" eta.x.array[:] = np.sqrt(eta_squared.x.array[:])\n",
355355
"\n",
356-
" sqrt_eta_max = sqrt_eta.x.petsc_vec.max()[1]\n",
356+
" eta_max = eta.x.petsc_vec.max()[1]\n",
357357
"\n",
358358
" theta = 0.5\n",
359-
" should_refine = ufl.conditional(ufl.gt(sqrt_eta, theta * sqrt_eta_max), 1, 0)\n",
359+
" should_refine = ufl.conditional(ufl.gt(eta, theta * eta_max), 1, 0)\n",
360360
" markers = dolfinx.fem.Function(W)\n",
361361
" ip = W.element.interpolation_points\n",
362362
" if Version(dolfinx.__version__) < Version(\"0.10.0\"):\n",
@@ -367,7 +367,7 @@
367367
},
368368
{
369369
"cell_type": "markdown",
370-
"id": "d9243490",
370+
"id": "20",
371371
"metadata": {},
372372
"source": [
373373
"## Running the adaptive refinement algorithm\n",
@@ -376,7 +376,7 @@
376376
},
377377
{
378378
"cell_type": "markdown",
379-
"id": "ab8f6701",
379+
"id": "21",
380380
"metadata": {},
381381
"source": [
382382
"We will track the progress of the adaptive mesh refinement as a GIF."
@@ -385,7 +385,7 @@
385385
{
386386
"cell_type": "code",
387387
"execution_count": null,
388-
"id": "293b959a",
388+
"id": "22",
389389
"metadata": {},
390390
"outputs": [],
391391
"source": [
@@ -395,7 +395,7 @@
395395
},
396396
{
397397
"cell_type": "markdown",
398-
"id": "5fe0ac8b",
398+
"id": "23",
399399
"metadata": {
400400
"lines_to_next_cell": 2
401401
},
@@ -407,7 +407,7 @@
407407
{
408408
"cell_type": "code",
409409
"execution_count": null,
410-
"id": "969aaaa5",
410+
"id": "24",
411411
"metadata": {
412412
"tags": [
413413
"hide-input"
@@ -444,7 +444,7 @@
444444
},
445445
{
446446
"cell_type": "markdown",
447-
"id": "d798464c",
447+
"id": "25",
448448
"metadata": {},
449449
"source": [
450450
"We set some parameters for checking convergence of the algorithm, and provide the exact eigenvalue\n",
@@ -460,7 +460,7 @@
460460
{
461461
"cell_type": "code",
462462
"execution_count": null,
463-
"id": "ebd3fef7",
463+
"id": "26",
464464
"metadata": {
465465
"tags": [
466466
"scroll-output"
@@ -492,7 +492,7 @@
492492
},
493493
{
494494
"cell_type": "markdown",
495-
"id": "5bac3843",
495+
"id": "27",
496496
"metadata": {},
497497
"source": [
498498
"<img src=\"./amr.gif\" alt=\"gif\" class=\"bg-primary mb-1\" width=\"800px\">"

chapter2/amr.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,10 @@ def solve(
192192

193193
# ## Error-indicator
194194
# In this example, we will use an error-indicator $\eta$ to decide what cells should be refined.
195-
# Specifically, the estimator is:
195+
# Specifically, the estimator $\eta$ is defined as:
196196
#
197197
# \begin{align*}
198-
# \eta = \sum_{K\in \mathcal{T}_h(\Omega)}\left(h^2\int_K \vert \lambda u_h + \Delta u_h\vert^2~\mathrm{d}x\right)
198+
# \eta^2 = \sum_{K\in \mathcal{T}_h(\Omega)}\left(h^2\int_K \vert \lambda u_h + \Delta u_h\vert^2~\mathrm{d}x\right)
199199
# + \sum_{E\in\mathcal{F}_i}\frac{h}{2} \vert [\nabla \cdot \mathbf{n}_E ]\vert^2~\mathrm{d}s
200200
# \end{align*}
201201
#
@@ -206,7 +206,7 @@ def mark_cells(uh_r: dolfinx.fem.Function, lam: float):
206206
mesh = uh_r.function_space.mesh
207207
W = dolfinx.fem.functionspace(mesh, ("DG", 0))
208208
w = ufl.TestFunction(W)
209-
eta = dolfinx.fem.Function(W)
209+
eta_squared = dolfinx.fem.Function(W)
210210
f = dolfinx.fem.Constant(mesh, 1.0)
211211
h = dolfinx.fem.Function(W)
212212
h.x.array[:] = mesh.h(mesh.topology.dim, np.arange(len(h.x.array), dtype=np.int32))
@@ -217,14 +217,14 @@ def mark_cells(uh_r: dolfinx.fem.Function, lam: float):
217217
+ ufl.inner(h("+") / 2 * ufl.jump(ufl.grad(uh_r), n) ** 2, w("+")) * ufl.dS
218218
+ ufl.inner(h("-") / 2 * ufl.jump(ufl.grad(uh_r), n) ** 2, w("-")) * ufl.dS
219219
)
220-
dolfinx.fem.petsc.assemble_vector(eta.x.petsc_vec, dolfinx.fem.form(G))
221-
sqrt_eta = dolfinx.fem.Function(W)
222-
sqrt_eta.x.array[:] = np.sqrt(eta.x.array[:])
220+
dolfinx.fem.petsc.assemble_vector(eta_squared.x.petsc_vec, dolfinx.fem.form(G))
221+
eta = dolfinx.fem.Function(W)
222+
eta.x.array[:] = np.sqrt(eta_squared.x.array[:])
223223

224-
sqrt_eta_max = sqrt_eta.x.petsc_vec.max()[1]
224+
eta_max = eta.x.petsc_vec.max()[1]
225225

226226
theta = 0.5
227-
should_refine = ufl.conditional(ufl.gt(sqrt_eta, theta * sqrt_eta_max), 1, 0)
227+
should_refine = ufl.conditional(ufl.gt(eta, theta * eta_max), 1, 0)
228228
markers = dolfinx.fem.Function(W)
229229
ip = W.element.interpolation_points
230230
if Version(dolfinx.__version__) < Version("0.10.0"):

0 commit comments

Comments
 (0)