Skip to content

Support -snes_adapt#5213

Open
pbrubeck wants to merge 8 commits into
mainfrom
pbrubeck/snes-adapt
Open

Support -snes_adapt#5213
pbrubeck wants to merge 8 commits into
mainfrom
pbrubeck/snes-adapt

Conversation

@pbrubeck

@pbrubeck pbrubeck commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Description

Codex-assisted.

Implements UFL-level refinement of a variational problem and PETSc-level refinement of the SNES solver. In this way, users will not require to write the adaptivity loop themselves.

from firedrake import *

mesh = UnitSquareMesh(10, 10)
hierarchy = AdaptiveMeshHierarchy(mesh)

V = FunctionSpace(mesh, "CG", 1)
u = Function(V)
v = TestFunction(v_space)
F = inner(grad(u), grad(v)) * dx - f * v * dx

def marking_callback(solver_context, solution):
    # Select cells based on some criteria (e.g., Dorfler marking)
    marker = Function(DG0)
    ...
    return marker

params = {
    "snes_adapt_sequence": 5,  # Number of adaptation iterations
}

problem = NonlinearVariationalProblem(F, u)
solver = NonlinearVariationalSolver(problem, solver_parameters=params, 
                                    marking_callback=marking_callback)
solver.solve()          
u_adapt = solver.get_adapted_solution()

Depends on https://gitlab.com/petsc/petsc/-/merge_requests/9407

Comment thread firedrake/variational_solver.py Outdated
Comment thread firedrake/variational_solver.py Outdated
Comment thread firedrake/variational_solver.py Outdated
Comment thread firedrake/dmhooks.py Outdated
Comment thread firedrake/dmhooks.py Outdated
Comment thread tests/firedrake/multigrid/test_snes_adapt.py Outdated
Comment thread firedrake/variational_solver.py
Comment thread firedrake/dmhooks.py Outdated
Comment thread firedrake/dmhooks.py Outdated
Comment thread firedrake/dmhooks.py Outdated
Comment thread firedrake/dmhooks.py Outdated
Comment thread firedrake/dmhooks.py Outdated
Comment thread firedrake/dmhooks.py Outdated
Comment thread firedrake/dmhooks.py Outdated
Comment thread firedrake/dmhooks.py Outdated
Comment thread firedrake/dmhooks.py Outdated
@pbrubeck pbrubeck force-pushed the pbrubeck/snes-adapt branch 2 times, most recently from b27150b to 4932ac2 Compare July 2, 2026 15:50
Comment thread firedrake/dmhooks.py
@pbrubeck pbrubeck force-pushed the pbrubeck/snes-adapt branch 2 times, most recently from 9bab35e to 8e73057 Compare July 2, 2026 17:27
@pbrubeck pbrubeck force-pushed the pbrubeck/snes-adapt branch 3 times, most recently from 230017a to f817644 Compare July 3, 2026 11:15
@pbrubeck pbrubeck force-pushed the pbrubeck/snes-adapt branch 2 times, most recently from 1c7406a to 5ff2783 Compare July 3, 2026 14:21
Comment thread tests/firedrake/multigrid/test_snes_adapt.py Outdated
Comment thread firedrake/variational_solver.py Outdated
Comment thread firedrake/variational_solver.py Outdated
Comment thread firedrake/variational_solver.py Outdated
Comment thread firedrake/variational_solver.py Outdated
Comment thread firedrake/variational_solver.py Outdated
Comment thread firedrake/variational_solver.py Outdated
pbrubeck added 3 commits July 6, 2026 11:49
…x a DM refcount bug in adaptive refinement

refine() no longer falls back to coarsen(): both now share a common
reconstruct() singledispatch for direction-agnostic handlers, and only
register their own special cases (Mesh, FunctionSpace, Function/Cofunction).

Also fix a segfault in -snes_adapt_sequence: DMAdaptorAdapt() unconditionally
destroys its input DM once a level is superseded, but AdaptiveMeshHierarchy
keeps that DM alive as a coarse grid, so take an extra reference before that
happens.
Comment thread firedrake/mg/ufl_utils.py Outdated
Comment thread firedrake/mg/ufl_utils.py Outdated
Comment thread firedrake/dmhooks.py Outdated
Comment on lines +454 to +456
# DMAdaptorAdapt() unconditionally destroys its input DM, but
# AdaptiveMeshHierarchy keeps this level's DM alive as a coarse grid.
dm.incRef()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we only keep alive the coarsest level?

Comment thread firedrake/mg/ufl_utils.py Outdated
Comment thread firedrake/mg/ufl_utils.py Outdated
Comment thread firedrake/mg/ufl_utils.py Outdated
@pbrubeck pbrubeck force-pushed the pbrubeck/snes-adapt branch from dc269c6 to aa43084 Compare July 9, 2026 16:07
@pbrubeck pbrubeck marked this pull request as ready for review July 9, 2026 16:07
Comment thread firedrake/mg/ufl_utils.py Outdated
Comment thread firedrake/mg/ufl_utils.py
name = f"{name}_level_{level}"

new = firedrake.Function(Vnew, name=name)
new.interpolate(expr)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should prolong instead, and that would be done at a later point

Suggested change
new.interpolate(expr)

Comment thread firedrake/mg/ufl_utils.py
if is_dual(c):
manager.restrict(c, cmapping[c])
if clevel > mlevel:
manager.restrict(c, mapped)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
manager.restrict(c, mapped)
manager.restrict(c, mapped)
else:
mapped.interpolate(c)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant