Skip to content

Unstructured finite volume - #5397

Draft
aabills wants to merge 31 commits into
mainfrom
unstructured-finite-volume
Draft

Unstructured finite volume#5397
aabills wants to merge 31 commits into
mainfrom
unstructured-finite-volume

Conversation

@aabills

@aabills aabills commented Feb 26, 2026

Copy link
Copy Markdown
Contributor

Description

Adds an unstructured finite volume method for 2 and 3D, allowing for simulations on arbitrary domains with user-provided meshes.

Fixes # (issue)

Type of change

Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #)

Important checks:

Please confirm the following before marking the PR as ready for review:

  • No style issues: nox -s pre-commit
  • All tests pass: nox -s tests
  • The documentation builds: nox -s doctests
  • Code is commented for hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

@codecov

codecov Bot commented Feb 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.72855% with 492 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.85%. Comparing base (1e9fbce) to head (8c15609).

Files with missing lines Patch % Lines
packages/pybamm/src/pybamm/plotting/quick_plot.py 23.65% 142 Missing ⚠️
...es/pybamm/src/pybamm/solvers/processed_variable.py 30.45% 137 Missing ⚠️
...s/pybamm/src/pybamm/meshes/unstructured_submesh.py 80.41% 105 Missing ⚠️
...ybamm/src/pybamm/discretisations/discretisation.py 44.28% 39 Missing ⚠️
packages/pybamm/src/pybamm/meshes/meshes.py 66.25% 27 Missing ⚠️
packages/pybamm/src/pybamm/plotting/plot_vtk.py 98.04% 11 Missing ⚠️
...ry_models/lithium_ion/basic_dfn_3d_unstructured.py 93.04% 8 Missing ⚠️
...ackages/pybamm/src/pybamm/plotting/dynamic_plot.py 41.66% 7 Missing ⚠️
...ry_models/lithium_ion/basic_dfn_2d_unstructured.py 94.49% 6 Missing ⚠️
packages/pybamm/src/pybamm/solvers/solution.py 44.44% 5 Missing ⚠️
... and 2 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5397      +/-   ##
==========================================
- Coverage   98.12%   96.85%   -1.28%     
==========================================
  Files         339      344       +5     
  Lines       31996    34489    +2493     
==========================================
+ Hits        31396    33403    +2007     
- Misses        600     1086     +486     

☔ 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.

aabills and others added 20 commits February 27, 2026 10:36
Resolve conflicts:
- __init__.py: keep both VTKQuickPlot and nyquist_plot imports
- functions.py: take main's Max/Min _casadi_evaluate and _evaluate_for_shape
- convert_to_casadi.py: accept deletion from main's OOP refactor; port
  VectorField casadi conversion to _to_casadi method on the class
- vector_field.py: 85% -> 100%
- tensor_field.py: 90% -> 100%
- unstructured_submesh.py: 64% -> 87%

Added tests for quad/hex element types, boundary loops, contains_points_3d
on hex, optimize_ordering on single cell, generator error paths, and
_parse_lims string-variable handling. Added tensor/vector field getitem
edge cases, all-on-edges path, and VectorField._to_casadi coverage.
* lock before big change

* update

* Add TaggedSubMeshGenerator for per-region Gmsh extraction

One-instance-per-region MeshGenerator that pulls a single Gmsh
physical group out of a .msh file and wraps it as an
UnstructuredSubMesh. Simpler than UserSuppliedUnstructuredMesh when
the model already supplies one generator per pybamm domain (e.g.
multi-domain 3D thermal w/ body + tab regions).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
…inite-volume-check-858f6a

# Conflicts:
#	packages/pybamm/src/pybamm/meshes/unstructured_submesh.py
#	packages/pybamm/src/pybamm/models/full_battery_models/base_battery_model.py
#	packages/pybamm/src/pybamm/models/full_battery_models/lithium_ion/basic_dfn_2d_unstructured.py
#	packages/pybamm/src/pybamm/models/full_battery_models/lithium_ion/basic_dfn_3d_unstructured.py
#	packages/pybamm/src/pybamm/parameters/parameter_substitutor.py
#	packages/pybamm/src/pybamm/plotting/plot_vtk.py
#	packages/pybamm/src/pybamm/spatial_methods/finite_volume_unstructured.py
#	packages/pybamm/tests/unit/test_meshes/test_unstructured_submesh.py
#	packages/pybamm/tests/unit/test_spatial_methods/test_finite_volume_unstructured.py
#	pyproject.toml
Resolve lint on the merged tree: replace .keys() iteration and dict()
literals, merge isinstance calls, and drop the blanket except around
interface auto-discovery in favour of an explicit dimension check in
_interface_face_match, which already reports non-adjacency via its
matched flag.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
- sigma() gained an sto parameter on main; pass sto=None in the
  unstructured DFN models, matching base_thermal and li_metal.
- Restore main's strict 3D cell-geometry validation, which the branch
  had commented out; test_spm covers the arbitrary-geometry rejection.
- Reduction._from_json clears domains again after restore, since
  SpecificFunction._from_json bypasses __init__ and Max/Min would
  otherwise come back carrying the child's domain.
- Component serialises its index via to_json/_from_json, mirroring
  Magnitude, and gains a hypothesis strategy alongside Norm.
- UnstructuredSubMesh joins _SUBMESH_EXEMPT (no _from_json), and
  Reduction joins _NOT_ROUND_TRIPPABLE as an abstract base.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Add API pages for UnstructuredSubMesh and its generators, the
FiniteVolumeUnstructured spatial method, VTKQuickPlot, and the two
unstructured DFN models, and record the feature in the changelog.

Drop the root-level TODO.md: its main item (arbitrary domain names via
an explicit tag) is implemented as TaggedSubMeshGenerator, and the
workaround script it points at no longer exists.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
aabills and others added 2 commits July 31, 2026 14:14
Use OSMesa only on Linux and install the software GL backends in CI so
save_gif no longer segfaults on headless runners.

Co-authored-by: Cursor <cursoragent@cursor.com>
Headless Windows runners lack a software OpenGL backend, so save_gif
segfaults; install mesa-dist-win OSMesa via the PyVista setup action.

Co-authored-by: Cursor <cursoragent@cursor.com>
@aabills

aabills commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

Split into stacked prior PRs (plan B) so this can be reviewed in smaller pieces. Original branch unstructured-finite-volume is unchanged, and tip 8c1560943 is also preserved on backup/unstructured-finite-volume-full.

  1. Generalise VectorField to N components #5686 — VectorField N-comp
  2. Add unstructured mesh infrastructure #5687 — Meshing
  3. Add unstructured finite volume spatial method #5688 — Spatial method + ProcessedVariable
  4. Add VTK plotting for unstructured meshes #5689 — VTK plotting
  5. Add unstructured 2D/3D DFN battery models #5690 — Unstructured DFN models

Once that stack lands, this PR can be closed as superseded.

@aabills

aabills commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

Added a 6th stacked PR: #TBD deprecates pybamm.Magnitude in favour of Component.

Updating once the PR number is known…

@aabills

aabills commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

Also opened #5691: deprecate pybamm.Magnitudepybamm.Component (6th in the stack).

Full stack:

  1. Generalise VectorField to N components #5686 VectorField
  2. Add unstructured mesh infrastructure #5687 Meshing
  3. Add unstructured finite volume spatial method #5688 Spatial + ProcessedVariable
  4. Add VTK plotting for unstructured meshes #5689 VTK plotting
  5. Add unstructured 2D/3D DFN battery models #5690 Unstructured DFN models
  6. Deprecate pybamm.Magnitude in favour of Component #5691 Deprecate Magnitude

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.

2 participants