-
Notifications
You must be signed in to change notification settings - Fork 198
Mesh Adaptivity via DMPlexTransform #5215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 108 commits
Commits
Show all changes
113 commits
Select commit
Hold shift + click to select a range
5ff2783
Support -snes_adapt
pbrubeck 1c7406a
Support -snes_adapt
pbrubeck b4153b7
Native/redistributed transfer for AdaptiveMeshHierarcy
pbrubeck fc1e25e
cleanup
pbrubeck c999b13
Merge branch 'pbrubeck/snes-adapt' into pbrubeck/mg-redist
pbrubeck d588985
Works in serial
pbrubeck 46e2514
WIP: RedistMesh
pbrubeck 885a7eb
Deprecate AdaptiveTransferManager
pbrubeck c8b3473
WIP struggled a lot with injection
pbrubeck 2e0ef75
remove anti-patterns
pbrubeck a60293f
workaround
pbrubeck e24125e
fix parallel
pbrubeck 6c37126
add failing redistribution test
pbrubeck c9fba85
cleanup
pbrubeck f978cf9
Redistributed MeshHierarchy
pbrubeck 4e4bbeb
refactoring, and some FIXMEs
pbrubeck a810d2f
Fix RedistMesh + AdaptiveMeshHierarchy
pbrubeck 3df8ca5
refactoring
pbrubeck 1523df1
reconstruct coefficient in either direction
pbrubeck dbd64f3
Merge branch 'pbrubeck/snes-adapt' into pbrubeck/mg-redist
pbrubeck 631889c
cleanup
pbrubeck 2c01147
move code around
pbrubeck fc16d28
reuse MeshTopology._set_partitioner
pbrubeck 0d3aaef
expose the distribution_parameters
pbrubeck 6ad926e
test second level
pbrubeck ba9bfb4
Fix compositions of MeshHierarchy and AdaptiveMeshHierarchy
pbrubeck 2986a7b
DMPlex.adaptLabel
pbrubeck d3eab9b
Use DMPlex.adaptLabel()
pbrubeck 9bd0dfb
restore redistribution
pbrubeck 08957f9
Refactoring
pbrubeck 7a2fa1a
Enable 3D (tetrahedron) support for refine_sbr adaptive refinement
pbrubeck 4b96edc
Drop adapt label
pbrubeck 1aa43b5
cleanup
pbrubeck c6d1474
cleanup tests
pbrubeck f3236b5
Fix adaptive injection
pbrubeck b80cb8f
Apply suggestions from code review
pbrubeck b42e28d
cleanup
pbrubeck 8f83483
cleanup
pbrubeck 651804e
basic test
pbrubeck edc9b7b
Unify coarsen/refine dispatch under a shared reconstruct base, and fi…
pbrubeck b9c5a21
Merge remote-tracking branch 'origin/main' into pbrubeck/snes-adapt
pbrubeck aa43084
refactoring
pbrubeck 4035788
Apply suggestion from @pbrubeck
pbrubeck 8c612ac
Merge branch 'pbrubeck/snes-adapt' into pbrubeck/mg-redist
pbrubeck 0b4a188
Apply review suggestions: defer coefficient prolongation to transfer …
pbrubeck de7a0d0
Merge branch 'pbrubeck/snes-adapt' into pbrubeck/mg-redist
pbrubeck 7ace418
Apply review suggestions: split MeshHierarchy into three stages
pbrubeck 6cc4489
Apply follow-up review suggestions to MeshHierarchy
pbrubeck 4564981
Apply review suggestions to interface.py, adapt.py, and redist.py
pbrubeck 3fbf003
Apply further review suggestions to mesh.py, adapt.py, netgen.py
pbrubeck 098ca74
Apply suggestions from code review
pbrubeck 3e21650
Apply suggestion from @pbrubeck
pbrubeck 5a0306a
Apply suggestion from @pbrubeck
pbrubeck f3d5e67
Apply suggestion from @pbrubeck
pbrubeck 27a5260
Apply suggestions from code review
pbrubeck 8339a52
fixes
pbrubeck b25d720
Apply suggestions from code review
pbrubeck 0c87d21
Apply suggestion from @pbrubeck
pbrubeck 9be2fbe
Apply suggestions from code review
pbrubeck 60aa61b
Apply suggestion from @pbrubeck
pbrubeck 7b4f5d8
fix inject
pbrubeck 5e9b4cb
Deterministic tests
pbrubeck 2fe1c33
API improvements
pbrubeck 96e24f9
cythonize, remove balancing-based redistribution
pbrubeck 04bf785
Merge branch 'pbrubeck/snes-adapt' into pbrubeck/mg-redist
pbrubeck 82af367
Apply suggestions from code review
pbrubeck 5761136
Merge branch 'main' into pbrubeck/snes-adapt
pbrubeck 55dd08b
review comments
pbrubeck 633448f
Merge branch 'pbrubeck/snes-adapt' into pbrubeck/mg-redist
pbrubeck c1b2ef1
Apply suggestion from @pbrubeck
pbrubeck fc7f092
Apply suggestions from code review
pbrubeck 676ef79
set adaptive criterion when we detect a marking_callback
pbrubeck 231ed2c
Merge branch 'pbrubeck/snes-adapt' into pbrubeck/mg-redist
pbrubeck c8a7ed4
Split mesh redistribution into a separate branch
pbrubeck 459a2a0
Apply suggestions from code review
pbrubeck 9001da6
Apply suggestions from code review
pbrubeck 0aac44f
review suggestions
pbrubeck 466e756
tidy
pbrubeck 1ce6051
get_relative ensures that hierarchy is the same
pbrubeck 8a04d52
Merge branch 'pbrubeck/snes-adapt' into pbrubeck/mg-redist
pbrubeck f825d59
Merge branch 'main' into pbrubeck/snes-adapt
pbrubeck 2709cea
Fix
pbrubeck b355866
Fix a workaround for Real space
pbrubeck 8b1dcd3
Make code less pythonic
pbrubeck 3324471
review suggestions
pbrubeck dd2303e
Apply suggestion from @pbrubeck
pbrubeck c571474
reverse kwarg
pbrubeck 73a8567
Merge branch 'pbrubeck/snes-adapt' into pbrubeck/mg-redist
pbrubeck 115ac32
purge AdaptiveTransferManager
pbrubeck 1d99930
Merge branch 'main' into pbrubeck/snes-adapt
pbrubeck 1cf9f3c
Merge branch 'pbrubeck/snes-adapt' into pbrubeck/mg-redist
pbrubeck 9bbdb59
Merge branch 'pbrubeck/mg-redist' of github.com:firedrakeproject/fire…
pbrubeck a4cd3c7
merge conflict
pbrubeck 3582bb0
drop redist
pbrubeck 1e4ef15
petsctools.inserted_options
pbrubeck e35d3b7
docstrings
pbrubeck 3821989
drop redist
pbrubeck 6b68257
Cython memory view, fixes for petsctools.inserted_options
pbrubeck 51ae39b
more comments
pbrubeck c5fc2f9
Apply suggestions from code review
pbrubeck ef5aed1
comments
pbrubeck b74153e
make code more readable
pbrubeck 21f4ab7
petsctools main
pbrubeck abdc3a1
Fix test that broke after splitting redistribution
pbrubeck 2813f24
remove netgen fix
pbrubeck 1dae5ad
Merge branch 'main' into pbrubeck/mg-redist
pbrubeck 2c55fb1
Fuse AdaptiveMeshHierarchy into MeshHierarchy
pbrubeck ca39efa
Merge remote-tracking branch 'origin/main' into pbrubeck/mg-redist
pbrubeck 8090999
review suggestions
pbrubeck e0e8ea1
Merge branch 'main' into pbrubeck/mg-redist
pbrubeck 5761390
Apply suggestion from @pbrubeck
pbrubeck f00303e
Clarify why _transfer_high_order_coordinates clones the plex
pbrubeck 739a3be
remove clone
pbrubeck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| """Adaptive mesh refinement helpers.""" | ||
| import numpy as np | ||
| import petsctools | ||
|
|
||
| from firedrake.cython import dmcommon | ||
| from firedrake.cython import mgimpl as impl | ||
| from firedrake.utils import IntType | ||
| from firedrake.function import Function | ||
| from firedrake.functionspace import FunctionSpace | ||
| from firedrake.mesh import Mesh, DISTRIBUTION_PARAMETERS_NOOP | ||
| from firedrake.netgen import _transfer_high_order_coordinates | ||
|
|
||
|
|
||
| # PETSc's DMAdaptFlag value requesting refinement, for the adapt label. | ||
| DM_ADAPT_REFINE = 1 | ||
|
|
||
| # Label holding, on every cell, the number of the cell of the original mesh it | ||
| # descends from. The refinement transform propagates labels from a cell to its | ||
| # children, so this stays relative to the original mesh however many times we | ||
| # refine. | ||
| PARENT_LABEL = "_adaptive_dmplex_parent" | ||
|
|
||
| ADAPT_LABEL = "_adaptive_dmplex_adapt" | ||
|
|
||
|
|
||
| def _adapt_marked_cells(mesh, cell_marker): | ||
| """Refine the cells of ``mesh`` marked by ``cell_marker`` and return the refined DMPlex.""" | ||
| dm = mesh.topology_dm | ||
| ncoarse = mesh.cell_set.size | ||
|
|
||
| dm.createLabel(ADAPT_LABEL) | ||
| adapt_label = dm.getLabel(ADAPT_LABEL) | ||
| adapt_indicator = np.zeros(cell_marker.dat.data_ro_with_halos.shape, dtype=IntType) | ||
| adapt_indicator[:ncoarse] = cell_marker.dat.data_ro.real > 0 | ||
| dmcommon.mark_points_with_function_array( | ||
| dm, cell_marker.function_space().dm.getSection(), 0, | ||
| adapt_indicator, adapt_label, DM_ADAPT_REFINE, | ||
| ) | ||
|
|
||
| parameters = {"dm_plex_transform_type": "refine_sbr"} | ||
| try: | ||
| # options_prefix="" is essential | ||
| with petsctools.inserted_options(parameters=parameters, options_prefix=""): | ||
| new_dm = dm.adaptLabel(ADAPT_LABEL) | ||
| finally: | ||
|
connorjward marked this conversation as resolved.
|
||
| dm.removeLabel(ADAPT_LABEL) | ||
|
|
||
| # The transform propagates every label, including the temporary adapt | ||
| # label and the coarse mesh's stale pyop2_core/owned/ghost point | ||
| # classification. Mesh() skips recomputing that classification if it's | ||
| # already present, so it must be dropped here to force a fresh one for | ||
| # the new mesh's own point count and distribution. | ||
| for label in ("pyop2_core", "pyop2_owned", "pyop2_ghost", ADAPT_LABEL): | ||
| if new_dm.hasLabel(label): | ||
| new_dm.removeLabel(label) | ||
|
|
||
| return new_dm | ||
|
|
||
|
|
||
| def _copy_adaptive_refinement_metadata(source_mesh, target_mesh): | ||
| """Copy mesh-construction metadata from a mesh onto its adaptively-derived successor.""" | ||
| target_mesh._distribution_parameters = dict(source_mesh._distribution_parameters) | ||
| target_mesh._did_reordering = source_mesh._did_reordering | ||
| target_mesh._tolerance = source_mesh.tolerance | ||
| if hasattr(source_mesh, "netgen_mesh") and not hasattr(target_mesh, "netgen_mesh"): | ||
| target_mesh.netgen_mesh = source_mesh.netgen_mesh | ||
| if hasattr(source_mesh, "netgen_flags") and not hasattr(target_mesh, "netgen_flags"): | ||
| target_mesh.netgen_flags = source_mesh.netgen_flags | ||
|
|
||
|
|
||
| def refine_marked_elements(mesh, cell_marker): | ||
| """Adaptively refine a mesh using a DG0 marking function. | ||
|
|
||
| Positive integer marker values request repeated refinement of the | ||
| corresponding cells. Curved Netgen meshes are re-curved to the | ||
| original coordinate degree after refinement. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| mesh | ||
| The mesh to refine. | ||
| cell_marker | ||
| A DG0 `~firedrake.function.Function` on ``mesh``: cells with a | ||
| positive value ``n`` are refined ``n`` times. | ||
|
|
||
| Returns | ||
| ------- | ||
| MeshGeometry | ||
| The adaptively refined mesh, with ``adaptive_parent`` set to | ||
| ``mesh`` and ``adaptive_cell_maps`` set to the | ||
| ``(coarse_to_fine, fine_to_coarse)`` cell maps relative to it. | ||
|
|
||
| """ | ||
| with cell_marker.dat.vec_ro as v: | ||
| _, num_refinements = v.max() | ||
| # Always run at least one adaptation pass, even when no cell is marked, | ||
| # so that a fresh mesh (with its own cell maps) is produced uniformly. | ||
| num_refinements = max(int(np.rint(num_refinements)), 1) | ||
|
|
||
| ncoarse = mesh.cell_set.size | ||
| coarse_dm = mesh.topology_dm | ||
| if coarse_dm.hasLabel(PARENT_LABEL): | ||
| coarse_dm.removeLabel(PARENT_LABEL) | ||
| coarse_dm.createLabel(PARENT_LABEL) | ||
| impl.set_adaptive_parent_label(coarse_dm, mesh._cell_numbering, ncoarse, PARENT_LABEL) | ||
|
|
||
| current_mesh = mesh | ||
| current_mark = cell_marker | ||
| try: | ||
| for ref in range(num_refinements): | ||
| new_dm = _adapt_marked_cells(current_mesh, current_mark) | ||
| current_mesh = Mesh( | ||
| new_dm, | ||
| dim=mesh.geometric_dimension, | ||
| reorder=False, | ||
| distribution_parameters=DISTRIBUTION_PARAMETERS_NOOP, | ||
| comm=mesh.comm, | ||
| tolerance=mesh.tolerance, | ||
| ) | ||
| coarse_to_fine, fine_to_coarse = impl.adaptive_parent_child_cell_maps( | ||
| new_dm, current_mesh._cell_numbering, ncoarse, | ||
| current_mesh.cell_set.size, PARENT_LABEL, | ||
| ) | ||
| if ref < num_refinements - 1: | ||
| # A cell asking for n refinements stays marked until n rounds | ||
| # have happened, so its descendants inherit n minus the number | ||
| # of rounds so far. | ||
| ancestor = fine_to_coarse[:, 0] | ||
| refined = ancestor >= 0 | ||
| current_mark = Function(FunctionSpace(current_mesh, "DG", 0)) | ||
| current_mark.dat.data_wo[refined] = \ | ||
| cell_marker.dat.data_ro[ancestor[refined]] - (ref + 1) | ||
| finally: | ||
| coarse_dm.removeLabel(PARENT_LABEL) | ||
|
|
||
| final_mesh = current_mesh | ||
| if hasattr(mesh, "netgen_mesh"): | ||
| order = mesh.coordinates.function_space().ufl_element().degree() | ||
| if order > 1: | ||
| final_mesh = _transfer_high_order_coordinates(mesh, final_mesh, order) | ||
|
|
||
| final_mesh.topology_dm.removeLabel(PARENT_LABEL) | ||
| final_mesh.adaptive_parent = mesh | ||
| final_mesh.adaptive_cell_maps = (coarse_to_fine, fine_to_coarse) | ||
| _copy_adaptive_refinement_metadata(mesh, final_mesh) | ||
|
connorjward marked this conversation as resolved.
|
||
| return final_mesh | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.