Skip to content

Commit 80ca89a

Browse files
committed
further updated notebooks
1 parent 1e7baf4 commit 80ca89a

11 files changed

+203
-55
lines changed

01-spd-helmholtz.ipynb

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"outputs": [],
2121
"source": [
2222
"# Code in this cell makes plots appear an appropriate size and resolution in the browser window\n",
23+
"# If the following line fails then the user needs to install ipympl.\n",
2324
"%config InlineBackend.figure_format = 'svg'\n",
2425
"\n",
2526
"import matplotlib.pyplot as plt\n",
@@ -303,7 +304,7 @@
303304
"cell_type": "markdown",
304305
"metadata": {},
305306
"source": [
306-
"Since we know that the Helmholtz equation defines a symmetric problem, we instruct PETSc to employ the conjugate gradient method. We do not consider preconditioning, for now."
307+
"We now solve the variational problem. Since we don't specify any solver parameters, the default direct solver will be used."
307308
]
308309
},
309310
{
@@ -364,28 +365,6 @@
364365
"fig.colorbar(collection);"
365366
]
366367
},
367-
{
368-
"cell_type": "markdown",
369-
"metadata": {},
370-
"source": [
371-
"# Exercises\n",
372-
"\n",
373-
"## Exercise 1: \n",
374-
"\n",
375-
"### 1a: use a higher approximation degree\n",
376-
"\n",
377-
"Solve the same problem, only this time, use a piecewise quadratic approximation space.\n",
378-
"\n",
379-
"- Hint: check the help for `FunctionSpace` to see how to specify the degree.\n",
380-
"\n",
381-
"### 1b: use a quadrilateral mesh\n",
382-
"\n",
383-
"Solve the same problem, but using quadrilateral, rather than triangular, cells.\n",
384-
"\n",
385-
"- Hint 1: check the help for `UnitSquareMesh` to see how to make a quadrilateral mesh\n",
386-
"- Hint 2: To specify a piecewise continuous space on quadrilaterals, use the family name `\"Q\"`."
387-
]
388-
},
389368
{
390369
"cell_type": "code",
391370
"execution_count": null,
@@ -427,7 +406,8 @@
427406
"L = f * v * dx\n",
428407
"\n",
429408
"uh = Function(V)\n",
430-
"solve(a == L, uh)\n",
409+
"solve(a == L, uh, solver_parameters={'ksp_type': 'cg',\n",
410+
" 'pc_type': 'none'})\n",
431411
"\n",
432412
"u_exact = cos(2*pi*x)*cos(2*pi*y)"
433413
]

02-poisson.ipynb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535
"\n",
3636
"for some known function $f$. To have a well-posed problem, we must impose Dirichlet conditions over at least part of the domain boundary:\n",
3737
"\n",
38-
"$$u(x) = g(x) \\quad \\forall x \\in \\Gamma_D,\\\\\n",
39-
"\\nabla u(x)\\cdot \\vec{n} = h(x) \\quad \\forall x \\in \\Gamma_N.$$\n",
38+
"$$\\begin{gather*}\n",
39+
"u(x) = g(x) \\quad \\forall x \\in \\Gamma_D,\\\\\n",
40+
"\\nabla u(x)\\cdot \\vec{n} = h(x) \\quad \\forall x \\in \\Gamma_N.\n",
41+
"\\end{gather*}$$\n",
4042
"\n",
4143
"As before, the Neumann condition is imposed weakly by setting the boundary integral over the relevant part of the boundary. The Dirichlet condition is imposed strongly by modifying the function space from which $u$ is drawn.\n",
4244
"\n",

03-elasticity.ipynb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@
1919
"source": [
2020
"# Linear elasticity\n",
2121
"\n",
22-
"*This work is adapted from an earlier FEniCS tutorial.*\n",
22+
"*This work is adapted from a previous FEniCS tutorial no longer online*\n",
2323
"\n",
2424
"Having studied a few scalar-valued problems, we now move on to a vector-valued problem. The equations of linear elasticity. Here, we'll treat the isotropic case.\n",
2525
"\n",
2626
"For small deformations, the governing equation is:\n",
2727
"\n",
2828
"$$ -\\nabla \\cdot \\sigma = f \\text{ in } \\Omega, $$\n",
29-
"with\n",
30-
"$$ \\DeclareMathOperator{\\Tr}{Tr}\n",
31-
"\\text{the stress tensor}\\quad \\sigma := \\lambda \\Tr(\\epsilon)\\mathbb{I} + 2\\mu\\epsilon\\\\\n",
32-
"\\text{and the symmetric strain rate tensor}\\quad \\epsilon := \\frac{1}{2}\\left(\\nabla u + (\\nabla u)^T\\right), $$\n",
29+
"with the stress tensor:\n",
30+
"$$ \\sigma := \\lambda \\Tr(\\epsilon)\\mathbb{I} + 2\\mu\\epsilon$$\n",
31+
"and the symmetric strain rate tensor:\n",
32+
"$$\\epsilon := \\frac{1}{2}\\left(\\nabla u + (\\nabla u)^T\\right), $$\n",
3333
"where $u$ is the unknown vector displacement field, and $\\mu$ and $\\lambda$ are the Lamè parameters.\n",
3434
"\n",
3535
"As before, the variational formulation consists of multiplying by a test function in some suitable finite element space, $v \\in V$, and integrating. Note that this time, the solution $u$, and hence the test space $V$ are *vector*-valued (so multiplication actually means taking the inner product).\n",
@@ -110,7 +110,7 @@
110110
"metadata": {},
111111
"outputs": [],
112112
"source": [
113-
"bc = DirichletBC(V, as_vector([0., 0.]), 1)"
113+
"bc = DirichletBC(V, Constant([0, 0]), 1)"
114114
]
115115
},
116116
{
@@ -264,7 +264,7 @@
264264
" lambda_ = Constant(0.25)\n",
265265
" Id = Identity(mesh.geometric_dimension()) # 2x2 Identity tensor\n",
266266
" \n",
267-
" bc = DirichletBC(V, as_vector([0., 0.]), 1)\n",
267+
" bc = DirichletBC(V, Constant([0, 0]), 1)\n",
268268
" u = TrialFunction(V)\n",
269269
" v = TestFunction(V)\n",
270270
" a = inner(sigma(u), epsilon(v))*dx\n",
@@ -345,7 +345,7 @@
345345
"\n",
346346
" def sigma(u):\n",
347347
" return lambda_*div(u)*Id + 2*mu*epsilon(u) \n",
348-
" bc = DirichletBC(V, as_vector([0., 0.]), 1)\n",
348+
" bc = DirichletBC(V, Constant([0, 0]), 1)\n",
349349
" u = TrialFunction(V)\n",
350350
" v = TestFunction(V)\n",
351351
" a = inner(sigma(u), epsilon(v))*dx\n",
@@ -356,8 +356,8 @@
356356
" b0 = Function(V)\n",
357357
" b1 = Function(V)\n",
358358
" b2 = Function(V)\n",
359-
" b0.interpolate(as_vector([1., 0.]))\n",
360-
" b1.interpolate(as_vector([0., 1.]))\n",
359+
" b0.interpolate(Constant([1, 0]))\n",
360+
" b1.interpolate(Constant([0, 1]))\n",
361361
" b2.interpolate(as_vector([-y, x]))\n",
362362
" nullmodes = VectorSpaceBasis([b0, b1, b2])\n",
363363
" # Make sure they're orthonormal.\n",

05-mixed-poisson.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
"metadata": {},
116116
"outputs": [],
117117
"source": [
118-
"rt = FiniteElement(\"Raviart-Thomas\", triangle, 2, variant=\"integral\")\n",
118+
"rt = FiniteElement(\"Raviart-Thomas\", triangle, 2)\n",
119119
"Sigma = FunctionSpace(mesh, rt)\n",
120120
"V = FunctionSpace(mesh, \"DG\", 1)"
121121
]
@@ -206,7 +206,7 @@
206206
"\n",
207207
"tripcolor(uh, axes=axes[1])\n",
208208
"axes[1].set_aspect(\"equal\")\n",
209-
"axes[1].set_title(\"$u$\")\n",
209+
"axes[1].set_title(r\"$u$\")\n",
210210
"\n",
211211
"plt.tight_layout()"
212212
]

06-pde-constrained-optimisation.ipynb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
"metadata": {},
5353
"outputs": [],
5454
"source": [
55-
"from firedrake import *"
55+
"from firedrake import *\n",
56+
"from firedrake.__future__ import interpolate"
5657
]
5758
},
5859
{
@@ -116,7 +117,7 @@
116117
"\\end{split}\n",
117118
"$$\n",
118119
"\n",
119-
"Here, $u$ and $p$ are unknown velocity and pressure, $f$ is a prescribed inflow, $g$ is the control variable that we will optimise for and $\\alpha$ is a regularisation parameter. This corresponds physically to minimising the loss of energy as heat by controlling the in/outflow on $\\Gamma_\\text{circ}$. The regularisation parameter penalises too many non-zero control values."
120+
"Here, $u$ and $p$ are unknown velocity and pressure, $f$ is a prescribed inflow, $g$ is the control variable that we will optimise for and $\\alpha$ is a regularisation parameter. This corresponds physically to minimising the loss of energy as heat by controlling the in/outflow on $\\Gamma_\\text{circ}$. The regularisation parameter penalises large control values."
120121
]
121122
},
122123
{
@@ -132,10 +133,6 @@
132133
"metadata": {},
133134
"outputs": [],
134135
"source": [
135-
"import os\n",
136-
"if not os.path.isfile(\"stokes-control.msh\"):\n",
137-
" # If the mesh is not available locally, we download it.\n",
138-
" !curl -O https://raw.githubusercontent.com/firedrakeproject/notebooks/refs/heads/main/stokes-control.msh\n",
139136
"mesh = Mesh(\"stokes-control.msh\")"
140137
]
141138
},
@@ -440,6 +437,13 @@
440437
"\n",
441438
"How does it affect the solution before and after optimisation? "
442439
]
440+
},
441+
{
442+
"cell_type": "code",
443+
"execution_count": null,
444+
"metadata": {},
445+
"outputs": [],
446+
"source": []
443447
}
444448
],
445449
"metadata": {

07-geometric-multigrid.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@
427427
" 0)\n",
428428
"\n",
429429
" value = as_vector([gbar*(1 - (12*t)**2), 0])\n",
430-
" bcs = [DirichletBC(W.sub(0), value, (1, 2)),\n",
430+
" bcs = [DirichletBC(W.sub(0), interpolate(value, V), (1, 2)),\n",
431431
" DirichletBC(W.sub(0), zero(2), (3, 4))]\n",
432432
" \n",
433433
" a = (nu*inner(grad(u), grad(v)) - p*div(v) + q*div(u))*dx\n",

08-composable-solvers.ipynb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
"metadata": {},
143143
"outputs": [],
144144
"source": [
145-
"nullspace = MixedVectorSpaceBasis(W, [W.sub(0), VectorSpaceBasis(constant=True)])"
145+
"nullspace = MixedVectorSpaceBasis(W, [W.sub(0), VectorSpaceBasis(constant=True, comm=mesh.comm)])"
146146
]
147147
},
148148
{
@@ -669,6 +669,13 @@
669669
"outputs": [],
670670
"source": []
671671
},
672+
{
673+
"cell_type": "code",
674+
"execution_count": null,
675+
"metadata": {},
676+
"outputs": [],
677+
"source": []
678+
},
672679
{
673680
"cell_type": "code",
674681
"execution_count": null,

09-hybridisation.ipynb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,11 +961,32 @@
961961
"wh.assign(0.0)\n",
962962
"uD_solver_hybrid.solve()"
963963
]
964+
},
965+
{
966+
"cell_type": "code",
967+
"execution_count": null,
968+
"metadata": {},
969+
"outputs": [],
970+
"source": []
964971
}
965972
],
966973
"metadata": {
974+
"kernelspec": {
975+
"display_name": "Python 3 (ipykernel)",
976+
"language": "python",
977+
"name": "python3"
978+
},
967979
"language_info": {
968-
"name": "python"
980+
"codemirror_mode": {
981+
"name": "ipython",
982+
"version": 3
983+
},
984+
"file_extension": ".py",
985+
"mimetype": "text/x-python",
986+
"name": "python",
987+
"nbconvert_exporter": "python",
988+
"pygments_lexer": "ipython3",
989+
"version": "3.13.1"
969990
}
970991
},
971992
"nbformat": 4,

10-sum-factorisation.ipynb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,22 @@
406406
}
407407
],
408408
"metadata": {
409+
"kernelspec": {
410+
"display_name": "Python 3 (ipykernel)",
411+
"language": "python",
412+
"name": "python3"
413+
},
409414
"language_info": {
410-
"name": "python"
415+
"codemirror_mode": {
416+
"name": "ipython",
417+
"version": 3
418+
},
419+
"file_extension": ".py",
420+
"mimetype": "text/x-python",
421+
"name": "python",
422+
"nbconvert_exporter": "python",
423+
"pygments_lexer": "ipython3",
424+
"version": "3.13.1"
411425
}
412426
},
413427
"nbformat": 4,

0 commit comments

Comments
 (0)