Skip to content

feat(deps): support numpy >= 2.0 (#247)#301

Merged
lmoresi merged 2 commits into
developmentfrom
feature/numpy2-support
Jul 3, 2026
Merged

feat(deps): support numpy >= 2.0 (#247)#301
lmoresi merged 2 commits into
developmentfrom
feature/numpy2-support

Conversation

@lmoresi

@lmoresi lmoresi commented Jul 3, 2026

Copy link
Copy Markdown
Member

Resolves #247. Lifts the numpy = "<2" pin (clean break — numpy < 2 no longer supported) and replaces the numpy-1.x APIs removed/changed in numpy 2.0.

Source changes

  • np.core.numeric/multiarray.* → public np.* (cross/dot/concatenate/array_equal/allclose) — np.core is private in 2.0
  • np.row_stack removed — it aliased np.vstack (already handled); dropped the now-duplicate handler
  • __array__(self)__array__(self, dtype=None, copy=None) in the 4 array-view classes (numpy 2.0 passes dtype/copy)
  • 2-D np.cross → explicit z-component (numpy 2.0 removed the 2-D cross) in the triangle shape-quality path
  • np.doublenp.float64 (51 occurrences, 8 files; cosmetic — valid in 1.x too)

Env

  • pixi.toml: numpy = "<2"">=2"; pixi.lock re-solved cleanly to numpy 2.5.0.
  • PETSc is already 3.25 and the conda-petsc floor is >=3.21 (ships numpy-2 petsc4py builds) — no PETSc version bump needed. Answers the issue's min-PETSc question: 3.21.

Validation

  • Source edits verified under the current numpy 1.26 (build clean; imports; units/array/swarm tests green — 41 targeted + 8 offset).
  • The numpy-2 environment is validated by CI (conda-petsc dev, numpy-2 petsc4py). Matches the prototype that passed core + 4-rank MPI at numpy 2.4.6 / PETSc 3.25.
  • Local amr users (custom-built petsc4py) rebuild their bindings (./uw build / petsc-local-build) after pulling — the custom petsc4py was compiled against numpy 1.26.

Underworld development team with AI support from Claude Code

Replaces numpy 1.x APIs removed/changed in numpy 2.0 and lifts the
`numpy = "<2"` pixi pin (a clean break — numpy < 2 is no longer supported).

Source changes:
- np.core.numeric/multiarray.* -> public np.* (cross/dot/concatenate/
  array_equal/allclose) in unit_aware_array.py (np.core is private in 2.0)
- np.row_stack removed -> it aliased np.vstack, already handled; dropped the
  now-duplicate handler
- __array__(self) -> __array__(self, dtype=None, copy=None) in the 4 array-view
  classes (numpy 2.0 calls __array__ with dtype/copy keywords)
- 2-D np.cross -> explicit z-component (numpy 2.0 removed the 2-D cross) in the
  triangle shape-quality path
- np.double -> np.float64 (51 occurrences, 8 files; cosmetic, valid in 1.x too)

Env:
- pixi.toml numpy pin "<2" -> ">=2"; pixi.lock re-solved to numpy 2.5.0.
- PETSc is already 3.25 and the conda-petsc floor is >=3.21 (numpy-2 builds),
  so no PETSc version bump is required.

Source edits verified under numpy 1.26 (build clean, imports, units/array/swarm
tests green). numpy-2 environment validated by CI (conda-petsc, which ships
numpy-2 petsc4py builds); local amr custom-petsc4py users rebuild their bindings.

Underworld development team with AI support from Claude Code
Copilot AI review requested due to automatic review settings July 3, 2026 04:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request updates Underworld3 to support NumPy >= 2.0 by removing reliance on private NumPy internals, adapting to NumPy 2’s updated array protocol, and updating environment pins/locks accordingly.

Changes:

  • Replaced removed/private NumPy 1.x APIs (e.g. np.core.*, np.double, 2D np.cross) with NumPy 2–compatible equivalents.
  • Updated several array-view classes’ __array__ signatures to accept NumPy 2’s dtype/copy keywords.
  • Lifted the numpy <2 environment pin to numpy >=2 and re-solved pixi.lock (NumPy 2.5.0).

Reviewed changes

Copilot reviewed 14 out of 25 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/underworld3/visualisation/visualisation.py Switch array casting from np.double to np.float64.
src/underworld3/utilities/unit_aware_array.py Replace private np.core.* calls with public NumPy APIs; remove row_stack handler.
src/underworld3/swarm.py Update __array__ protocol and float dtype allocations for NumPy 2.
src/underworld3/meshing/smoothing.py Replace np.double with np.float64 in numeric allocations/casts.
src/underworld3/function/unit_conversion.py Update dtype documentation and conversions to np.float64.
src/underworld3/function/pure_sympy_evaluator.py Ensure evaluated coordinate arrays use np.float64.
src/underworld3/function/functions_unit_system.py Normalize coordinate arrays to np.float64 for evaluation paths.
src/underworld3/function/_function.pyx Update Cython-side float64 handling and dtype checks for NumPy 2.
src/underworld3/discretisation/discretisation_mesh.py Replace removed 2D np.cross usage with explicit z-component formula.
src/underworld3/discretisation/discretisation_mesh_variables.py Update float dtype allocations and __array__ protocol for mesh array views.
pixi.toml Lift NumPy pin to >=2.
pixi.lock Lockfile refresh to NumPy 2.5.0 and updated dependency set.
Expt_Grains/Particle_numbers.txt New data file added (appears to be a generated artifact).
docs/developer/design/FREESLIP_DYNAMIC_TOPOGRAPHY_FREESURFACE.md Add large free-surface design/method note.
docs/blog/free-surface-without-advecting-it.md Add draft blog post related to the free-surface method note.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 1443 to 1445
# Compute cross product using numpy's default behavior
result = np.core.numeric.cross(np.asarray(a), np.asarray(b), *args, **kwargs)
result = np.cross(np.asarray(a), np.asarray(b), *args, **kwargs)

Comment on lines +1 to +5
0 0
1 0
2 0
3 0
4 0
Comment on lines +1 to +7
# A free surface you never advect

*Draft blog post — Underworld development team. Status: held pending a possible
method paper (the scheme is plausibly novel; see the
[method note](../developer/design/FREESLIP_DYNAMIC_TOPOGRAPHY_FREESURFACE.md)
and its cited novelty assessment). Not wired into the published docs toctree
yet — release at the team's discretion.*
__array_wrap__ detected in-place operations via `result.base is self.base`.
A fresh ufunc result (e.g. `scalar * arr`) has base=None; when the array also
owns its data (base=None), that test is `None is None` == True, so under numpy
2.0's ufunc dispatch it returned `self` and DROPPED the operation — e.g.
`1e-4 * v_at_Vpts` returned `v_at_Vpts`. This silently broke swarm.advection
(dt factor lost -> displacement = v instead of v*dt) and CFL back-stepping
(test_0007, test_0814 regressed only under numpy 2; fine on 1.26).

Fix: require a NON-None shared base for the base-equality in-place branch, so a
fresh scalar*array result is correctly returned (scaled) rather than mistaken
for an in-place write. Verified under numpy 2.5: test_0007 + test_0814 pass,
plus units/swarm/array/evaluate suites (43 passed).

Underworld development team with AI support from Claude Code
@lmoresi

lmoresi commented Jul 3, 2026

Copy link
Copy Markdown
Member Author

Thanks @copilot-pull-request-reviewer — good catches. Follow-up in #305:

  1. 2-D unit-aware np.cross — confirmed real: np.cross on a 2-D UnitAwareArray raised under numpy 2. fix(units): 2-D np.cross on UnitAwareArray under numpy 2 (+ drop committed artifact) — #301 follow-up #305 emulates the z-component for (...,2) inputs (delegating to np.cross for 3-D), with a regression test.
  2. Expt_Grains/Particle_numbers.txt — correct, that generated artifact was swept in by an over-broad git add -A. fix(units): 2-D np.cross on UnitAwareArray under numpy 2 (+ drop committed artifact) — #301 follow-up #305 removes it and gitignores Expt_Grains/.
  3. The free-surface docs were also swept in by the same git add -A; they're legitimate content so I've left them rather than churn — but the scope point is fair.

Underworld development team with AI support from Claude Code

lmoresi added a commit that referenced this pull request Jul 3, 2026
…ted artifact (#305)

Follow-up to #301 (Copilot review):

1. numpy 2.0 removed the 2-D cross product. The UnitAwareArray np.cross handler
   delegated straight to np.cross, so `np.cross(a2d, b2d)` on unit-aware 2-D
   vectors raised ValueError under numpy 2. Emulate the 2-D z-component for
   (...,2) inputs (matching numpy 1.x), delegate to np.cross for 3-D. New
   tests/test_0758_unit_aware_cross.py (4 cases).

2. Expt_Grains/Particle_numbers.txt (a generated example output, written by
   docs/examples/porous_flow/advanced/Ex_Explicit_Flow_Grains.py) was swept into
   #301 by an over-broad `git add -A`. Remove it from version control and
   gitignore Expt_Grains/.

Underworld development team with AI support from Claude Code
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