Skip to content

Fix FiniteVolume2D boundary extrapolation on meshes with fewer than 3 nodes - #5839

Open
aabills wants to merge 3 commits into
mainfrom
fix/fv2d-coarse-mesh-boundary-value
Open

aabills wants to merge 3 commits into
mainfrom
fix/fv2d-coarse-mesh-boundary-value

Conversation

@aabills

@aabills aabills commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Description

FiniteVolume2D.boundary_value_or_flux read the second and second-last node spacings in both directions (d_nodes_lr[1], d_nodes_lr[-2], d_nodes_tb[1], d_nodes_tb[-2]) before branching on side and extrapolation order. Any 2D submesh with fewer than 3 nodes in either direction therefore raised IndexError: index 1 is out of bounds for axis 0 with size 1 for every boundary operator, including a plain BoundaryValue(var, "right") or BoundaryIntegral(var, "right") that only extrapolates in lr.

Reproduce on main: pybamm.lithium_ion.BasicDFN2D() with its z_2d var_pts set to 2, solved via pybamm.Simulation.

Spacings are now looked up lazily through a local node_spacing(direction, index) helper, called only by the branch that uses them. When the mesh has too few nodes for the requested extrapolation, it raises a pybamm.DiscretisationError naming the order, variable, direction, and required vs. available node count.

Meshes with at least 3 nodes in both directions use the same spacings as before, so their results are unchanged.

Not covered here: with z_2d set to 1, the model gets past boundary extrapolation and then fails in harmonic_mean, which is a separate bug.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Tests

  • New TestExtrapolationCoarseMesh2D in tests/unit/test_spatial_methods/test_finite_volume_2d/test_extrapolation.py: with 2 tb nodes, linear and quadratic left/right values and the right boundary integral are exact; six too-coarse combinations raise DiscretisationError. All 8 fail on main.
  • 2D / finite-volume unit subset: 468 passed. Pre-commit clean.

🤖 Generated with Claude Code

Boundary extrapolation read the second node spacing in both directions
unconditionally, so any 2D mesh with fewer than 3 nodes in either
direction raised IndexError. Spacings are now looked up lazily and a
DiscretisationError is raised when the mesh is too coarse for the
requested extrapolation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aabills
aabills requested a review from a team as a code owner October 2, 2026 22:41
aabills and others added 2 commits October 2, 2026 15:41
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

🐰 Bencher Report

ProjectPyBaMM
Branchfix/fv2d-coarse-mesh-boundary-value
Testbedbare-metal

⚠️ WARNING: Truncated view!

The full continuous benchmarking report exceeds the maximum length allowed on this platform.

🐰 View full continuous benchmarking report in Bencher

@codecov

codecov Bot commented Oct 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.09%. Comparing base (445ba33) to head (99fec58).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5839   +/-   ##
=======================================
  Coverage   98.09%   98.09%           
=======================================
  Files         346      346           
  Lines       35354    35355    +1     
=======================================
+ Hits        34680    34681    +1     
  Misses        674      674           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant