Skip to content

Commit daf58a1

Browse files
committed
support reusing the solver with different guesses
1 parent c16ad9d commit daf58a1

4 files changed

Lines changed: 31 additions & 8 deletions

File tree

firedrake/adaptive_variational_solver.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from firedrake import dmhooks
12
from firedrake.petsc import PETSc
23
from firedrake.function import Function
34
from firedrake.cofunction import Cofunction
@@ -91,7 +92,19 @@ def __init__(self, problem: NonlinearVariationalProblem, goal_functional, tolera
9192
self.degree = self.element.degree()
9293
self.nullspace = nullspace
9394
self.atm = AdaptiveTransferManager()
94-
self.amh = AdaptiveMeshHierarchy(V.mesh())
95+
96+
mesh = V.mesh().unique()
97+
amh, level = utils.get_level(mesh)
98+
if amh is None:
99+
amh = AdaptiveMeshHierarchy(mesh)
100+
if not isinstance(amh, AdaptiveMeshHierarchy):
101+
raise ValueError("Problem needs to be defined on an AdaptiveMeshHierarchy")
102+
103+
# FIXME we should construct a subset of the original hierarchy instead
104+
amh, level = utils.get_level(mesh)
105+
while len(amh) > level+1:
106+
amh.pop_mesh()
107+
self.amh = amh
95108

96109
# Data storage and writing
97110
self.output_dir = Path(self.options.output_dir)

firedrake/mg/adaptive_hierarchy.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class AdaptiveMeshHierarchy(HierarchyBase):
2121
"""
2222
def __init__(self, base_mesh: MeshGeometry, nested: bool = True):
2323
self.meshes = []
24-
self._meshes = []
24+
self._meshes = self.meshes
2525
self.nested = nested
2626
self.add_mesh(base_mesh)
2727

@@ -35,10 +35,20 @@ def add_mesh(self, mesh: MeshGeometry):
3535
The mesh to be added to the finest level.
3636
"""
3737
level = len(self.meshes)
38-
self._meshes.append(mesh)
3938
self.meshes.append(mesh)
4039
set_level(mesh, self, level)
4140

41+
def pop_mesh(self):
42+
"""
43+
Adds a mesh into the hierarchy.
44+
45+
Parameters
46+
----------
47+
mesh
48+
The mesh to be added to the finest level.
49+
"""
50+
self.meshes.pop()
51+
4252
def adapt(self, eta: Function | Cofunction, theta: float):
4353
"""
4454
Adds a new mesh to the hierarchy by locally refining the finest mesh

firedrake/mg/netgen.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,6 @@ def NetgenHierarchy(mesh, levs, flags, distribution_parameters=None):
246246
logger.info(f"\tOrder of the hierarchy: {order}")
247247
if isinstance(order, int):
248248
order = [order]*(levs+1)
249-
permutation_tol = flags.get("permutation_tol", 1e-8)
250-
location_tol = flags.get("location_tol", 1e-8)
251249
refType = flags.get("refinement_type", "uniform")
252250
logger.info(f"\tRefinement type: {refType}")
253251
optMoves = flags.get("optimisation_moves", False)
@@ -270,7 +268,7 @@ def NetgenHierarchy(mesh, levs, flags, distribution_parameters=None):
270268
temp_flags = dict(flags)
271269
temp_flags['degree'] = order[0]
272270
temp = fd.Mesh(mesh.netgen_mesh, distribution_parameters=parameters,
273-
netgen_flags=temp_flags, comm=comm)
271+
netgen_flags=temp_flags, comm=comm)
274272
mesh = temp
275273
# Make a plex (cdm) without overlap.
276274
dm_cell_type, = mesh.dm_cell_types
@@ -321,6 +319,7 @@ def NetgenHierarchy(mesh, levs, flags, distribution_parameters=None):
321319
netgen_flags=temp_flags,
322320
comm=comm)
323321
elif snap == "coarse":
322+
ho_field = meshes[0].coordinates
324323
mesh = snapToCoarse(ho_field, mesh, order[l+1], snap_smoothing, cg)
325324
toc = time.time()
326325
logger.info(f"\t\t\tMeshed curved. Time taken: {toc-tic}")

firedrake/mg/ufl_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ def coarsen_equation_bc(ebc, self, coefficient_mapping=None):
145145

146146
@coarsen.register(firedrake.functionspaceimpl.WithGeometryBase)
147147
def coarsen_function_space(V, self, coefficient_mapping=None):
148-
if hasattr(V, "_coarse") and self == coarsen:
149-
return V._coarse
148+
# FIXME support multiple hierarchies
149+
# if hasattr(V, "_coarse") and self == coarsen:
150+
# return V._coarse
150151
V_fine = V
151152
# Handle MixedFunctionSpace : V_fine.reconstruct requires MeshSequence.
152153
fine_mesh = V_fine.mesh() if V_fine.index is None else V_fine.parent.mesh()

0 commit comments

Comments
 (0)