Skip to content

Finsberg/fix extrema dtype float32 spaces - #269

Open
finsberg wants to merge 3 commits into
finsberg/fenicsx-compatfrom
finsberg/fix-extrema-dtype-float32-spaces
Open

finsberg wants to merge 3 commits into
finsberg/fenicsx-compatfrom
finsberg/fix-extrema-dtype-float32-spaces

Conversation

@finsberg

Copy link
Copy Markdown
Member

Summary

Three bugs that turned up while fixing the mypy errors on finsberg/fenicsx-compat, one commit each.

compute_extrema: kind was a default value, not an annotation

 def compute_extrema(
     u: ufl.core.expr.Expr,
-    kind=MinMaxFunc,
+    kind: MinMaxFunc,
     ...
-        raise ValueError("Unknown extrema {kind}, supports min and max only.")
+        raise ValueError(f"Unknown extrema {kind}, supports min and max only.")

Without kind, a call used to raise a misleading "Unknown extrema {kind}" error. Now kind is required, as it already is in find_cell_extrema. Every working call already passed it.

create_space_of_simple_functions: float32 meshes and int64 cell tags

+dtype = mesh.geometry.x.dtype
+_fe_constructor, _fem_constructor = FiniteElement_float{32,64}, FunctionSpace_float{32,64}  # by dtype
-el = basix.ufl.element("P", ..., discontinuous=True)
+el = basix.ufl.element("P", ..., discontinuous=True, dtype=dtype)
-adj_flattened = np.zeros_like(cell_tag.values)          # int64 tags -> int64 array
+adj_flattened = np.zeros(len(cell_tag.values), dtype=np.int32)
-dolfinx.cpp.fem.FunctionSpace_float64(mesh._cpp_object, ...)
+_fem_constructor(mesh._cpp_object, ...)

The float32/float64 choice follows create_geometry_function_space.

_extract_dtype (private): typed for what it is given

It was annotated as also taking a UFL expression, but a plain expression such as u * v raised AttributeError. Both callers (assemble_scalar and norm) only pass forms, so the annotation now says that. No change at runtime.

Evidence

  • Before:
    test_material_space_dtypes[int32-float32]  TypeError: __init__(): incompatible function arguments   (FunctionSpace_float64)
    test_material_space_dtypes[int64-float32]  TypeError: __init__(): incompatible function arguments   (AdjacencyList_int32)
    test_material_space_dtypes[int64-float64]  TypeError: __init__(): incompatible function arguments   (AdjacencyList_int32)
    test_extrema_requires_kind                 ValueError: Unknown extrema {kind}, supports min and max only.
    test_extrema_unknown_kind                  Regex pattern did not match
    
    After: all 6 new tests pass, and so does the full suite against DOLFINx 0.12.0.dev0:
    pytest                 879 passed, 1154 skipped, 11 xfailed
    mpirun -n 3 pytest     881 passed, 1152 skipped, 11 xfailed   (every rank)
    mypy src tests         Success: no issues found in 43 source files
    

Merge Danger

Door: two-way

Each fix is a small, separate commit and can be reverted on its own.

Blast Radius: callers

  • Calling compute_extrema without kind now raises TypeError instead of ValueError. Such a call never worked.
  • create_space_of_simple_functions now accepts float32 meshes and int64 cell tags, which used to raise. float64 meshes with int32 tags behave as before.

🤖 Generated with Claude Code

finsberg and others added 3 commits October 21, 2026 19:09
`kind=MinMaxFunc` gave `kind` the type alias as its default value rather
than as its annotation, so a call without `kind` reached the "Unknown
extrema" error instead of failing on the missing argument. That error
also lacked its f-prefix and printed `{kind}` literally.

`kind` is now annotated and required, as in find_cell_extrema. Every
working call already passed it, since without it the function raised.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…functions

The function hard-coded the float64 cpp element and function space, so
a float32 mesh failed when the function space was built. It now picks
the classes by the mesh's geometry dtype, as create_geometry_function_space
does, and builds both basix elements with that dtype.

The dofmap array was allocated with np.zeros_like(cell_tag.values), so
int64 cell tags gave an int64 array that AdjacencyList_int32 rejects.
It is now always int32, as it holds positions into `tags`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
It was annotated as also taking a UFL expression, but an expression
without a dtype, such as `u * v`, raised AttributeError on
`.coefficients()`, which only forms have. Its two callers,
assemble_scalar and norm, only pass forms, so the annotation now says
so. No change at runtime.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@finsberg
finsberg added this pull request to stack #270 October 21, 2026 19:38
@finsberg
finsberg requested a review from jorgensd October 21, 2026 19:38
@finsberg
finsberg removed this pull request from stack #270 October 21, 2026 19:40
@finsberg
finsberg changed the base branch from finsberg/fenicsx-compat to main October 21, 2026 19:40
@finsberg
finsberg changed the base branch from main to finsberg/fenicsx-compat October 21, 2026 19:40

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