Skip to content

Commit 6e2f077

Browse files
author
Firedrake
committed
Upstream updates
1 parent 1e7baf4 commit 6e2f077

12 files changed

+527
-233
lines changed

01-spd-helmholtz.ipynb

Lines changed: 6 additions & 28 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",
@@ -141,8 +142,7 @@
141142
"metadata": {},
142143
"outputs": [],
143144
"source": [
144-
"from firedrake import *\n",
145-
"from firedrake.__future__ import interpolate"
145+
"from firedrake import *"
146146
]
147147
},
148148
{
@@ -168,7 +168,7 @@
168168
"outputs": [],
169169
"source": [
170170
"# In the notebook, we can also use a special \"?\" magic to pop up a help box\n",
171-
"?UnitSquareMesh"
171+
"UnitSquareMesh?"
172172
]
173173
},
174174
{
@@ -303,7 +303,7 @@
303303
"cell_type": "markdown",
304304
"metadata": {},
305305
"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."
306+
"We now solve the variational problem. Since we don't specify any solver parameters, the default direct solver will be used."
307307
]
308308
},
309309
{
@@ -364,28 +364,6 @@
364364
"fig.colorbar(collection);"
365365
]
366366
},
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-
},
389367
{
390368
"cell_type": "code",
391369
"execution_count": null,
@@ -545,7 +523,7 @@
545523
"outputs": [],
546524
"source": [
547525
"uh = Function(V)\n",
548-
"solve(a == L, uh, solver_parameters={'ksp_type': 'cg', 'pc_type': 'none'})"
526+
"solve(a == L, uh)"
549527
]
550528
},
551529
{
@@ -579,7 +557,7 @@
579557
"name": "python",
580558
"nbconvert_exporter": "python",
581559
"pygments_lexer": "ipython3",
582-
"version": "3.13.1"
560+
"version": "3.13.3"
583561
}
584562
},
585563
"nbformat": 4,

02-poisson.ipynb

Lines changed: 5 additions & 3 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",
@@ -238,7 +240,7 @@
238240
"name": "python",
239241
"nbconvert_exporter": "python",
240242
"pygments_lexer": "ipython3",
241-
"version": "3.13.1"
243+
"version": "3.13.3"
242244
}
243245
},
244246
"nbformat": 4,

03-elasticity.ipynb

Lines changed: 12 additions & 13 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 \\operatorname{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",
@@ -75,7 +75,6 @@
7575
"outputs": [],
7676
"source": [
7777
"from firedrake import *\n",
78-
"from firedrake.__future__ import interpolate\n",
7978
"length = 1\n",
8079
"width = 0.2\n",
8180
"mesh = RectangleMesh(40, 20, length, width)"
@@ -110,7 +109,7 @@
110109
"metadata": {},
111110
"outputs": [],
112111
"source": [
113-
"bc = DirichletBC(V, as_vector([0., 0.]), 1)"
112+
"bc = DirichletBC(V, Constant([0, 0]), 1)"
114113
]
115114
},
116115
{
@@ -241,7 +240,7 @@
241240
"source": [
242241
"# Solving bigger problems\n",
243242
"\n",
244-
"Up to now, we've only really solved quite small problems, and therefore haven't really had to worry about tuning the solver. As we increase the size of the problem we're solving, the direct solver approach will no longer be good enough. Firedrake uses [PETSc](http://www.mcs.anl.gov/petsc) to provide solvers, and uses PETSc solver parameters to control them.\n",
243+
"Up to now, we've only really solved quite small problems, and therefore haven't really had to worry about tuning the solver. As we increase the size of the problem we're solving, the direct solver approach will no longer be good enough. Firedrake uses [PETSc](http://petsc.org) to provide solvers, and uses PETSc solver parameters to control them.\n",
245244
"\n",
246245
"Let's dive straight in. We'll write a function that solves the same elasticity problem, but takes parameters for the number of cells in the mesh, as well as a dictionary of solver options."
247246
]
@@ -264,7 +263,7 @@
264263
" lambda_ = Constant(0.25)\n",
265264
" Id = Identity(mesh.geometric_dimension()) # 2x2 Identity tensor\n",
266265
" \n",
267-
" bc = DirichletBC(V, as_vector([0., 0.]), 1)\n",
266+
" bc = DirichletBC(V, Constant([0, 0]), 1)\n",
268267
" u = TrialFunction(V)\n",
269268
" v = TestFunction(V)\n",
270269
" a = inner(sigma(u), epsilon(v))*dx\n",
@@ -345,7 +344,7 @@
345344
"\n",
346345
" def sigma(u):\n",
347346
" return lambda_*div(u)*Id + 2*mu*epsilon(u) \n",
348-
" bc = DirichletBC(V, as_vector([0., 0.]), 1)\n",
347+
" bc = DirichletBC(V, Constant([0, 0]), 1)\n",
349348
" u = TrialFunction(V)\n",
350349
" v = TestFunction(V)\n",
351350
" a = inner(sigma(u), epsilon(v))*dx\n",
@@ -356,8 +355,8 @@
356355
" b0 = Function(V)\n",
357356
" b1 = Function(V)\n",
358357
" b2 = Function(V)\n",
359-
" b0.interpolate(as_vector([1., 0.]))\n",
360-
" b1.interpolate(as_vector([0., 1.]))\n",
358+
" b0.interpolate(Constant([1, 0]))\n",
359+
" b1.interpolate(Constant([0, 1]))\n",
361360
" b2.interpolate(as_vector([-y, x]))\n",
362361
" nullmodes = VectorSpaceBasis([b0, b1, b2])\n",
363362
" # Make sure they're orthonormal.\n",
@@ -422,7 +421,7 @@
422421
"name": "python",
423422
"nbconvert_exporter": "python",
424423
"pygments_lexer": "ipython3",
425-
"version": "3.13.1"
424+
"version": "3.12.10"
426425
}
427426
},
428427
"nbformat": 4,

04-burgers.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@
388388
"name": "python",
389389
"nbconvert_exporter": "python",
390390
"pygments_lexer": "ipython3",
391-
"version": "3.13.1"
391+
"version": "3.13.3"
392392
}
393393
},
394394
"nbformat": 4,

05-mixed-poisson.ipynb

Lines changed: 3 additions & 3 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
]
@@ -523,7 +523,7 @@
523523
"name": "python",
524524
"nbconvert_exporter": "python",
525525
"pygments_lexer": "ipython3",
526-
"version": "3.13.1"
526+
"version": "3.13.3"
527527
}
528528
},
529529
"nbformat": 4,

06-pde-constrained-optimisation.ipynb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
"\\end{split}\n",
117117
"$$\n",
118118
"\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."
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 large control values."
120120
]
121121
},
122122
{
@@ -258,10 +258,7 @@
258258
"outputs": [],
259259
"source": [
260260
"w = Function(W)\n",
261-
"solve(a == L, w, bcs=bcs, solver_parameters={\"mat_type\": \"aij\",\n",
262-
" \"ksp_type\": \"preonly\",\n",
263-
" \"pc_type\": \"lu\",\n",
264-
" \"pc_factor_shift_type\": \"inblocks\"})"
261+
"solve(a == L, w, bcs=bcs)"
265262
]
266263
},
267264
{
@@ -384,11 +381,7 @@
384381
"source": [
385382
"g.assign(g_opt)\n",
386383
"w_opt = Function(W)\n",
387-
"solve(a == L, w_opt, bcs=bcs, solver_parameters={\"mat_type\": \"aij\",\n",
388-
" \"ksp_type\": \"preonly\",\n",
389-
" \"pc_type\": \"lu\",\n",
390-
" \"pc_factor_shift_type\": \"inblocks\"},\n",
391-
" annotate=False)"
384+
"solve(a == L, w_opt, bcs=bcs, annotate=False)"
392385
]
393386
},
394387
{
@@ -440,6 +433,13 @@
440433
"\n",
441434
"How does it affect the solution before and after optimisation? "
442435
]
436+
},
437+
{
438+
"cell_type": "code",
439+
"execution_count": null,
440+
"metadata": {},
441+
"outputs": [],
442+
"source": []
443443
}
444444
],
445445
"metadata": {
@@ -458,7 +458,7 @@
458458
"name": "python",
459459
"nbconvert_exporter": "python",
460460
"pygments_lexer": "ipython3",
461-
"version": "3.13.1"
461+
"version": "3.12.10"
462462
}
463463
},
464464
"nbformat": 4,

07-geometric-multigrid.ipynb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,7 @@
229229
"metadata": {},
230230
"outputs": [],
231231
"source": [
232-
"solver = create_solver({\"ksp_type\": \"preonly\",\n",
233-
" \"pc_type\": \"lu\",\n",
234-
" \"pc_factor_shift_type\": \"inblocks\",\n",
235-
" \"ksp_monitor\": None,\n",
236-
" \"pmat_type\": \"aij\"})\n",
232+
"solver = create_solver({\"ksp_monitor\": None})\n",
237233
"solver.solve()"
238234
]
239235
},
@@ -426,7 +422,7 @@
426422
" 1, \n",
427423
" 0)\n",
428424
"\n",
429-
" value = as_vector([gbar*(1 - (12*t)**2), 0])\n",
425+
" value = assemble(interpolate(as_vector([gbar*(1 - (12*t)**2), 0]), V))\n",
430426
" bcs = [DirichletBC(W.sub(0), value, (1, 2)),\n",
431427
" DirichletBC(W.sub(0), zero(2), (3, 4))]\n",
432428
" \n",
@@ -488,7 +484,7 @@
488484
"name": "python",
489485
"nbconvert_exporter": "python",
490486
"pygments_lexer": "ipython3",
491-
"version": "3.13.1"
487+
"version": "3.12.10"
492488
}
493489
},
494490
"nbformat": 4,

08-composable-solvers.ipynb

Lines changed: 4 additions & 11 deletions
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
{
@@ -303,7 +303,7 @@
303303
"source": [
304304
"### Block preconditioning\n",
305305
"\n",
306-
"Firedrake hooks up all the necessary machinery to access PETSc's [`PCFIELDSPLIT`](https://www.mcs.anl.gov/petsc/petsc-current/manualpages/PC/PCFIELDSPLIT.html#PCFIELDSPLIT) preconditioner. This provides mechanisms for building preconditioners based on block factorisations. The Stokes problem \n",
306+
"Firedrake hooks up all the necessary machinery to access PETSc's [`PCFIELDSPLIT`](https://petsc.org/release/manualpages/PC/PCFIELDSPLIT/) preconditioner. This provides mechanisms for building preconditioners based on block factorisations. The Stokes problem \n",
307307
"$$\n",
308308
"\\begin{align}\n",
309309
" \\nu\\int_\\Omega \\color{#800020}{\\nabla u : \\nabla v}\\,\\mathrm{d}x - \\int_\\Omega\n",
@@ -538,7 +538,7 @@
538538
"\n",
539539
"So far, we've only used direct solvers for the blocks. We can also use iterative methods. Here we'll use geometric multigrid to solve\n",
540540
"\n",
541-
"In the same way that Firedrake hooks up solvers such that [`PCFIELDSPLIT`](https://www.mcs.anl.gov/petsc/petsc-current/manualpages/PC/PCFIELDSPLIT.html#PCFIELDSPLIT) is enabled, if a problem was defined on a mesh from a `MeshHierarchy`, [`PCMG`](https://www.mcs.anl.gov/petsc/petsc-current/manualpages/PC/PCMG.html) and [`SNESFAS`](https://www.mcs.anl.gov/petsc/petsc-current/manualpages/SNES/SNESFASType.html) are also available."
541+
"In the same way that Firedrake hooks up solvers such that [`PCFIELDSPLIT`](https://petsc.org/release/manualpages/PC/PCFIELDSPLIT/) is enabled, if a problem was defined on a mesh from a `MeshHierarchy`, [`PCMG`](https://petsc.org/release/manualpages/PC/PCMG/) and [`SNESFAS`](https://petsc.org/release/manualpages/SNESFAS/SNESFAS/) are also available."
542542
]
543543
},
544544
{
@@ -662,13 +662,6 @@
662662
"#convergence(solver)"
663663
]
664664
},
665-
{
666-
"cell_type": "code",
667-
"execution_count": null,
668-
"metadata": {},
669-
"outputs": [],
670-
"source": []
671-
},
672665
{
673666
"cell_type": "code",
674667
"execution_count": null,
@@ -693,7 +686,7 @@
693686
"name": "python",
694687
"nbconvert_exporter": "python",
695688
"pygments_lexer": "ipython3",
696-
"version": "3.13.1"
689+
"version": "3.12.10"
697690
}
698691
},
699692
"nbformat": 4,

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.3"
969990
}
970991
},
971992
"nbformat": 4,

0 commit comments

Comments
 (0)