diff --git a/python/dolfinx/fem/element.py b/python/dolfinx/fem/element.py index 178d19f925b..81165925665 100644 --- a/python/dolfinx/fem/element.py +++ b/python/dolfinx/fem/element.py @@ -6,7 +6,7 @@ """Finite elements.""" import typing -from functools import singledispatch +from functools import cached_property, singledispatch import numpy as np import numpy.typing as npt @@ -193,14 +193,18 @@ def dtype(self) -> np.dtype: """Geometry type of the Mesh that the FunctionSpace is defined on.""" return self._cpp_object.dtype - @property + @cached_property def basix_element(self) -> basix.finite_element.FiniteElement: """Return underlying Basix C++ element (if it exists). Raises: Runtime error if Basix element does not exist. + + Note: + Cached property: Wrapper constructed on initial call and not updated on subsequent + calls. """ - return self._cpp_object.basix_element + return basix.finite_element.FiniteElement(self._cpp_object.basix_element) @property def num_sub_elements(self) -> int: diff --git a/python/dolfinx/fem/function.py b/python/dolfinx/fem/function.py index 0374a6fa24d..ef226414a6a 100644 --- a/python/dolfinx/fem/function.py +++ b/python/dolfinx/fem/function.py @@ -733,7 +733,12 @@ def ufl_function_space(self) -> ufl.FunctionSpace: @cached_property def element(self) -> FiniteElement: - """Function space finite element.""" + """Function space finite element. + + Note: + Cached property: Wrapper constructed on initial call and not updated on subsequent + calls. + """ return FiniteElement(self._cpp_object.element) @property diff --git a/python/test/unit/fem/test_function_space.py b/python/test/unit/fem/test_function_space.py index 4255c1fd69c..69eb6f61feb 100644 --- a/python/test/unit/fem/test_function_space.py +++ b/python/test/unit/fem/test_function_space.py @@ -250,14 +250,11 @@ def test_cell_mismatch(mesh): @pytest.mark.skipif(default_real_type != np.float64, reason="float32 not supported yet") def test_basix_element(V, W, Q, V2): for V_ in (V, W, V2): - e = V_.element.basix_element - assert isinstance( - e, (basix._basixcpp.FiniteElement_float64, basix._basixcpp.FiniteElement_float32) - ) + assert isinstance(V_.element.basix_element, basix.finite_element.FiniteElement) # Mixed spaces do not yet return a basix element with pytest.raises(RuntimeError): - e = Q.element.basix_element + _ = Q.element.basix_element @pytest.mark.skip_in_parallel