Skip to content

Commit ed997a9

Browse files
authored
Compatibility fix for Firedrake 2024-12 Docker image (#24)
* Remove import of firedrake_configuration package - Monkey-patch u_restrict property required by newer PETSc TS - Package is no longer available due to pip install migration * Fix unit test failures - Migrate adjoint.py to newer interpolate API - Set max SNES failures to make TS retry steps * Migrate examples to newer VTKFile output API - Silences deprecation warning emitted from Firedrake * Adapt DAEProblem class to use to restricted solution - Duck typing compatible with VariationalProblem class - Use the new member everywhere * Remove unused & forgotten LOC --- This PR removes the (actually unused) import of `firedrake_configuration` package that is no longer present in recent (2 weeks or so) versions of [Firedrake Docker Hub image](https://hub.docker.com/r/firedrakeproject/firedrake/tags). Root cause of the issue is the migration of Firedrake to be `pip`-installable. There's details about the topic [here](firedrakeproject/firedrake#3877). Specifically, the problem started with [the PR that migrates _PyOP2_ into the main _Firedrake_ repo](firedrakeproject/firedrake#3817). --- It seems like the obvious fix, i.e. just remove the import, worked. The `firedrake_configuration` module wasn't used for anything directly within the `firedrake_ts` package. Besides this, the PR contains a few small fixes to make the unit tests and examples work. It seems like PETSc 3.22 upgrage that's bundled in the Firedrake 2024-12 image doesn't set the [`ts_max_snes_failures`](https://petsc.org/release/manualpages/TS/TSSetMaxSNESFailures/) option anymore. Somehow the default value causes the inner SNES solver never to retry any linear solves. So the TS solver has no other option but to fail whenever it happens instead of retrying. I also migrated the example applications to use the newer interpolate and VTK output Firedrake API.
1 parent 1186966 commit ed997a9

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

examples/adjoint.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
x = ufl.SpatialCoordinate(mesh)
1616
expr = ufl.as_vector([ufl.sin(2 * ufl.pi * x[0]), ufl.cos(2 * ufl.pi * x[1])])
17-
u = fd.interpolate(expr, V)
17+
u = fd.Function(V)
18+
u.interpolate(expr)
1819

1920
u_dot = fd.Function(V)
2021
v = fd.TestFunction(V)
@@ -35,7 +36,7 @@
3536
end = 0.1
3637
tspan = (t, end)
3738

38-
state_out = fd.File("result/state.pvd")
39+
state_out = fd.output.VTKFile("result/state.pvd")
3940

4041

4142
def ts_monitor(ts, steps, time, X):
@@ -81,5 +82,5 @@ def ts_monitor(ts, steps, time, X):
8182
print(f"Norm of dJdu after the adjoint solve: {dJdu_vec.norm()=} {fd.norm(fdJdu)=}")
8283

8384

84-
adj_out = fd.File("result/adj.pvd")
85+
adj_out = fd.output.VTKFile("result/adj.pvd")
8586
adj_out.write(fdJdu, time=0)

examples/burgers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
inner(u_t, v) + inner(dot(u, nabla_grad(u)), v) + nu * inner(grad(u), grad(v))
2525
) * dx
2626

27-
outfile = File("result/burgers.pvd")
27+
outfile = output.VTKFile("result/burgers.pvd")
2828
outfile.write(project(u, V_out, name="Velocity"), time=0.0)
2929

3030

firedrake_ts/solving_utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import numpy
44

55
from pyop2 import op2
6-
from firedrake_configuration import get_config
76
from firedrake import function, cofunction, dmhooks
87
from firedrake.exceptions import ConvergenceError
98
from firedrake.petsc import PETSc

firedrake_ts/ts_solver.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,16 @@ def __init__(
8181
is_form_consistent(self.is_linear, self.bcs)
8282
self.Jp_eq_J = Jp is None
8383

84-
self.u = u
84+
self.u_restrict = u
8585
self.udot = udot
8686
self.tspan = tspan
8787
self.F = F
8888
self.G = G
8989
self.Jp = Jp
90-
if not isinstance(self.u, function.Function):
90+
91+
if not isinstance(self.u_restrict, function.Function):
9192
raise TypeError(
92-
"Provided solution is a '%s', not a Function" % type(self.u).__name__
93+
"Provided solution is a '%s', not a Function" % type(self.u_restrict).__name__
9394
)
9495
if not isinstance(self.udot, function.Function):
9596
raise TypeError(
@@ -123,7 +124,7 @@ def dirichlet_bcs(self):
123124

124125
@utils.cached_property
125126
def dm(self):
126-
return self.u.function_space().dm
127+
return self.u_restrict.function_space().dm
127128

128129

129130
class DAESolver(OptionsManager):
@@ -227,14 +228,15 @@ def update_diffusivity(current_solution, current_time_derivative):
227228
# Mixed problem, use jacobi pc if user has not supplied
228229
# one.
229230
self.set_default_parameter("pc_type", "jacobi")
231+
self.set_default_parameter("ts_max_snes_failures", -1)
230232

231233
self.ts = PETSc.TS().create(comm=problem.dm.comm)
232234
self.snes = self.ts.getSNES()
233235

234236
self._problem = problem
235237

236238
self._ctx = ctx
237-
self._work = problem.u.dof_dset.layout_vec.duplicate()
239+
self._work = problem.u_restrict.dof_dset.layout_vec.duplicate()
238240

239241
self.ts.setDM(problem.dm)
240242
self.ts.setMonitor(monitor_callback)
@@ -338,15 +340,15 @@ def solve(self, bounds=None):
338340
# Make sure appcontext is attached to the DM before we solve.
339341
dm = self.ts.getDM()
340342
for dbc in self._problem.dirichlet_bcs():
341-
dbc.apply(self._problem.u)
343+
dbc.apply(self._problem.u_restrict)
342344

343345
if bounds is not None:
344346
lower, upper = bounds
345347
with lower.dat.vec_ro as lb, upper.dat.vec_ro as ub:
346348
self.snes.setVariableBounds(lb, ub)
347349

348350
work = self._work
349-
with self._problem.u.dat.vec as u:
351+
with self._problem.u_restrict.dat.vec as u:
350352
u.copy(work)
351353
with ExitStack() as stack:
352354
# Ensure options database has full set of options (so monitors

0 commit comments

Comments
 (0)