Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 36 additions & 36 deletions chapter2/amr.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "4535be91",
"id": "0",
"metadata": {},
"source": [
"# Adaptive mesh refinement with NetGen and DOLFINx\n",
Expand All @@ -18,7 +18,7 @@
},
{
"cell_type": "markdown",
"id": "b0282fe2",
"id": "1",
"metadata": {},
"source": [
"In this tutorial, we will consider an adaptive mesh refinement method, applied to\n",
Expand All @@ -31,7 +31,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "32fe6a58",
"id": "2",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -50,7 +50,7 @@
},
{
"cell_type": "markdown",
"id": "4782ba3b",
"id": "3",
"metadata": {},
"source": [
"## Generating a higher-order mesh with NetGen\n",
Expand All @@ -60,7 +60,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "d6046434",
"id": "4",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -80,7 +80,7 @@
},
{
"cell_type": "markdown",
"id": "e041abef",
"id": "5",
"metadata": {},
"source": [
"## Loading a mesh into DOLFINx\n",
Expand All @@ -92,7 +92,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "fc64bf64",
"id": "6",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -101,7 +101,7 @@
},
{
"cell_type": "markdown",
"id": "67cded8b",
"id": "7",
"metadata": {},
"source": [
"Next, we generate the mesh with the function :py:func:`ngsPETSc.utils.fenicsx.GeometricModel.model_to_mesh`.\n",
Expand All @@ -112,7 +112,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "dbae564a",
"id": "8",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -121,7 +121,7 @@
},
{
"cell_type": "markdown",
"id": "f90ca932",
"id": "9",
"metadata": {},
"source": [
"We use pyvista to visualize the mesh."
Expand All @@ -130,7 +130,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "34407248",
"id": "10",
"metadata": {
"tags": [
"hide-input"
Expand All @@ -154,7 +154,7 @@
},
{
"cell_type": "markdown",
"id": "6a18b03f",
"id": "11",
"metadata": {},
"source": [
"We have read in any cell and facet markers that have been defined in the NetGen model,\n",
Expand All @@ -166,7 +166,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "adc50da0",
"id": "12",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -176,7 +176,7 @@
},
{
"cell_type": "markdown",
"id": "0a3449b0",
"id": "13",
"metadata": {},
"source": [
"Again, we visualize the curved mesh with pyvista."
Expand All @@ -185,7 +185,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "1eb97f0f",
"id": "14",
"metadata": {
"tags": [
"hide-input"
Expand All @@ -206,7 +206,7 @@
},
{
"cell_type": "markdown",
"id": "7613cc7f",
"id": "15",
"metadata": {},
"source": [
"## Solving the eigenvalue problem\n",
Expand All @@ -224,7 +224,7 @@
},
{
"cell_type": "markdown",
"id": "717f48a2",
"id": "16",
"metadata": {
"lines_to_next_cell": 2
},
Expand All @@ -236,7 +236,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "d59bc925",
"id": "17",
"metadata": {
"lines_to_next_cell": 2
},
Expand Down Expand Up @@ -310,17 +310,17 @@
},
{
"cell_type": "markdown",
"id": "1c251b79",
"id": "18",
"metadata": {
"lines_to_next_cell": 2
},
"source": [
"## Error-indicator\n",
"In this example, we will use an error-indicator $\\eta$ to decide what cells should be refined.\n",
"Specifically, the estimator is:\n",
"Specifically, the estimator $\\eta$ is defined as:\n",
"\n",
"\\begin{align*}\n",
" \\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",
" \\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",
"+ \\sum_{E\\in\\mathcal{F}_i}\\frac{h}{2} \\vert [\\nabla \\cdot \\mathbf{n}_E ]\\vert^2~\\mathrm{d}s\n",
"\\end{align*}\n",
"\n",
Expand All @@ -330,15 +330,15 @@
{
"cell_type": "code",
"execution_count": null,
"id": "95f75bd8",
"id": "19",
"metadata": {},
"outputs": [],
"source": [
"def mark_cells(uh_r: dolfinx.fem.Function, lam: float):\n",
" mesh = uh_r.function_space.mesh\n",
" W = dolfinx.fem.functionspace(mesh, (\"DG\", 0))\n",
" w = ufl.TestFunction(W)\n",
" eta = dolfinx.fem.Function(W)\n",
" eta_squared = dolfinx.fem.Function(W)\n",
" f = dolfinx.fem.Constant(mesh, 1.0)\n",
" h = dolfinx.fem.Function(W)\n",
" h.x.array[:] = mesh.h(mesh.topology.dim, np.arange(len(h.x.array), dtype=np.int32))\n",
Expand All @@ -349,14 +349,14 @@
" + ufl.inner(h(\"+\") / 2 * ufl.jump(ufl.grad(uh_r), n) ** 2, w(\"+\")) * ufl.dS\n",
" + ufl.inner(h(\"-\") / 2 * ufl.jump(ufl.grad(uh_r), n) ** 2, w(\"-\")) * ufl.dS\n",
" )\n",
" dolfinx.fem.petsc.assemble_vector(eta.x.petsc_vec, dolfinx.fem.form(G))\n",
" sqrt_eta = dolfinx.fem.Function(W)\n",
" sqrt_eta.x.array[:] = np.sqrt(eta.x.array[:])\n",
" dolfinx.fem.petsc.assemble_vector(eta_squared.x.petsc_vec, dolfinx.fem.form(G))\n",
" eta = dolfinx.fem.Function(W)\n",
" eta.x.array[:] = np.sqrt(eta_squared.x.array[:])\n",
"\n",
" sqrt_eta_max = sqrt_eta.x.petsc_vec.max()[1]\n",
" eta_max = eta.x.petsc_vec.max()[1]\n",
"\n",
" theta = 0.5\n",
" should_refine = ufl.conditional(ufl.gt(sqrt_eta, theta * sqrt_eta_max), 1, 0)\n",
" should_refine = ufl.conditional(ufl.gt(eta, theta * eta_max), 1, 0)\n",
" markers = dolfinx.fem.Function(W)\n",
" ip = W.element.interpolation_points\n",
" if Version(dolfinx.__version__) < Version(\"0.10.0\"):\n",
Expand All @@ -367,7 +367,7 @@
},
{
"cell_type": "markdown",
"id": "d9243490",
"id": "20",
"metadata": {},
"source": [
"## Running the adaptive refinement algorithm\n",
Expand All @@ -376,7 +376,7 @@
},
{
"cell_type": "markdown",
"id": "ab8f6701",
"id": "21",
"metadata": {},
"source": [
"We will track the progress of the adaptive mesh refinement as a GIF."
Expand All @@ -385,7 +385,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "293b959a",
"id": "22",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -395,7 +395,7 @@
},
{
"cell_type": "markdown",
"id": "5fe0ac8b",
"id": "23",
"metadata": {
"lines_to_next_cell": 2
},
Expand All @@ -407,7 +407,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "969aaaa5",
"id": "24",
"metadata": {
"tags": [
"hide-input"
Expand Down Expand Up @@ -444,7 +444,7 @@
},
{
"cell_type": "markdown",
"id": "d798464c",
"id": "25",
"metadata": {},
"source": [
"We set some parameters for checking convergence of the algorithm, and provide the exact eigenvalue\n",
Expand All @@ -460,7 +460,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "ebd3fef7",
"id": "26",
"metadata": {
"tags": [
"scroll-output"
Expand Down Expand Up @@ -492,7 +492,7 @@
},
{
"cell_type": "markdown",
"id": "5bac3843",
"id": "27",
"metadata": {},
"source": [
"<img src=\"./amr.gif\" alt=\"gif\" class=\"bg-primary mb-1\" width=\"800px\">"
Expand Down
16 changes: 8 additions & 8 deletions chapter2/amr.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ def solve(

# ## Error-indicator
# In this example, we will use an error-indicator $\eta$ to decide what cells should be refined.
# Specifically, the estimator is:
# Specifically, the estimator $\eta$ is defined as:
#
# \begin{align*}
# \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)
# \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)
# + \sum_{E\in\mathcal{F}_i}\frac{h}{2} \vert [\nabla \cdot \mathbf{n}_E ]\vert^2~\mathrm{d}s
# \end{align*}
#
Expand All @@ -206,7 +206,7 @@ def mark_cells(uh_r: dolfinx.fem.Function, lam: float):
mesh = uh_r.function_space.mesh
W = dolfinx.fem.functionspace(mesh, ("DG", 0))
w = ufl.TestFunction(W)
eta = dolfinx.fem.Function(W)
eta_squared = dolfinx.fem.Function(W)
f = dolfinx.fem.Constant(mesh, 1.0)
h = dolfinx.fem.Function(W)
h.x.array[:] = mesh.h(mesh.topology.dim, np.arange(len(h.x.array), dtype=np.int32))
Expand All @@ -217,14 +217,14 @@ def mark_cells(uh_r: dolfinx.fem.Function, lam: float):
+ ufl.inner(h("+") / 2 * ufl.jump(ufl.grad(uh_r), n) ** 2, w("+")) * ufl.dS
+ ufl.inner(h("-") / 2 * ufl.jump(ufl.grad(uh_r), n) ** 2, w("-")) * ufl.dS
)
dolfinx.fem.petsc.assemble_vector(eta.x.petsc_vec, dolfinx.fem.form(G))
sqrt_eta = dolfinx.fem.Function(W)
sqrt_eta.x.array[:] = np.sqrt(eta.x.array[:])
dolfinx.fem.petsc.assemble_vector(eta_squared.x.petsc_vec, dolfinx.fem.form(G))
eta = dolfinx.fem.Function(W)
eta.x.array[:] = np.sqrt(eta_squared.x.array[:])

sqrt_eta_max = sqrt_eta.x.petsc_vec.max()[1]
eta_max = eta.x.petsc_vec.max()[1]

theta = 0.5
should_refine = ufl.conditional(ufl.gt(sqrt_eta, theta * sqrt_eta_max), 1, 0)
should_refine = ufl.conditional(ufl.gt(eta, theta * eta_max), 1, 0)
markers = dolfinx.fem.Function(W)
ip = W.element.interpolation_points
if Version(dolfinx.__version__) < Version("0.10.0"):
Expand Down