Skip to content

Do not store an explicit point location tolerance on the mesh - #5758

Draft
pbrubeck wants to merge 1 commit into
releasefrom
pbrubeck/fix/locate-cell-tolerance
Draft

pbrubeck wants to merge 1 commit into
releasefrom
pbrubeck/fix/locate-cell-tolerance

Conversation

@pbrubeck

@pbrubeck pbrubeck commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Summary

An explicit tolerance argument for point location was stored as the mesh tolerance, so it changed every later call that did not give a tolerance:

mesh = UnitSquareMesh(1, 1)            # mesh.tolerance == 0.5
mesh.locate_cell(point, tolerance=0.01)
mesh.locate_cell(point)                # used 0.01, not 0.5

locate_cell, locate_reference_coordinate, locate_cell_and_reference_coordinate, locate_cells_ref_coords_and_dists, Function._at, PointEvaluator and VertexOnlyMesh all set mesh.tolerance = tolerance. Each of them now passes the tolerance down to the point locator and leaves mesh.tolerance unchanged.

What changes for users:

  • A tolerance argument now applies only to the call that receives it.
  • VertexOnlyMesh(mesh, points, tolerance=t) and PointEvaluator(mesh, points, tolerance=t) no longer change mesh.tolerance. Code that relied on that side effect must set mesh.tolerance itself.

Implementation details

 MeshGeometry
-  rtree                  # cached until (coordinates, mesh.tolerance) change
+  _rtrees                # {tolerance: rtree}, cached until the coordinates change
+  _rtree(tolerance)      # build or look up the rtree for one tolerance
+  rtree                  # _rtree(self.tolerance)
   locate_cells_ref_coords_and_dists(xs, tolerance)
-    self.tolerance = tolerance
-    run_c(self.coordinates._ctypes, ...)
+    run_c(self.coordinates._ctypes(tolerance), ...)
  • Function._ctypes is now a method that takes the tolerance, so that the C struct holds the rtree for that tolerance.
  • PointEvaluator stores its own tolerance and passes it to VertexOnlyMesh.
  • clear_rtree empties the map of rtrees.
  • The docstrings that said "Changing this from default will cause the rtree to be rebuilt" now say that the mesh tolerance is not changed.
  • test_point_eval_api.py::test_tolerance, test_point_eval_api.py::test_point_evaluator_tolerance and test_vertex_only_mesh_generation.py::test_point_tolerance asserted that the mesh tolerance changes. They now assert that it does not.

Evidence

New test in tests/firedrake/regression/test_locate_cell.py:

mesh.locate_cell(point, tolerance=0.01)
assert mesh.locate_cell(point) == new_mesh.locate_cell(point)

No tests have been run yet: the local environment can only import Firedrake main. This draft relies on CI for the first run on release. It still has to be confirmed that the new test fails without the fix.

Limitations

main has the same bug in the same places. On main, _box_ratio_heuristic and partition_rtree are also cached on self.tolerance, so the merge of release into main must pass the tolerance to them too.

Merge Danger

Door: two-way

Blast Radius: point evaluation

Code that set the mesh tolerance through a tolerance= argument of VertexOnlyMesh, PointEvaluator or locate_cell now keeps the old mesh tolerance in later calls.


AI assistance: Claude Code (Claude Opus 5.5) wrote the change, the tests and this description.

🤖 Generated with Claude Code

locate_cell, Function._at, PointEvaluator and VertexOnlyMesh stored an
explicit tolerance argument as the mesh tolerance, so the argument
changed every later call that did not give a tolerance. Pass the
tolerance down to the C locator instead, and cache one rtree for each
tolerance until the mesh coordinates change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pbrubeck pbrubeck added the LLM used An LLM was used in the production of this PR label Dec 1, 2026
@pbrubeck
pbrubeck requested review from leo-collins and removed request for leo-collins December 3, 2026 11:03

This branch has not been deployed

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

Labels

LLM used An LLM was used in the production of this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant