From a6af646dbcbb4db2ee2698811d66e7bb2c2e1935 Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Sat, 1 Feb 2025 12:33:33 +0100 Subject: [PATCH 1/5] Add python wrapper to basix_element --- python/dolfinx/fem/element.py | 6 +++--- python/test/unit/fem/test_function_space.py | 7 ++----- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/python/dolfinx/fem/element.py b/python/dolfinx/fem/element.py index 673d060714..c3ec57fe2c 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 @@ -189,14 +189,14 @@ 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. """ - 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/test/unit/fem/test_function_space.py b/python/test/unit/fem/test_function_space.py index 4255c1fd69..1fb98c043e 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 From 01fd7706fadad4824613c276446dae7181ab46db Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Thu, 13 Feb 2025 15:57:43 +0100 Subject: [PATCH 2/5] Fix throwaway return type --- python/test/unit/fem/test_function_space.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/test/unit/fem/test_function_space.py b/python/test/unit/fem/test_function_space.py index 1fb98c043e..69eb6f61fe 100644 --- a/python/test/unit/fem/test_function_space.py +++ b/python/test/unit/fem/test_function_space.py @@ -254,7 +254,7 @@ def test_basix_element(V, W, Q, V2): # Mixed spaces do not yet return a basix element with pytest.raises(RuntimeError): - Q.element.basix_element + _ = Q.element.basix_element @pytest.mark.skip_in_parallel From c44d8fbca9ae62ea5f43c763ee1753e861a81742 Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Thu, 13 Feb 2025 16:02:16 +0100 Subject: [PATCH 3/5] Add doc on cached property and possible lacking update --- python/dolfinx/fem/element.py | 3 +++ python/dolfinx/fem/function.py | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/python/dolfinx/fem/element.py b/python/dolfinx/fem/element.py index c3ec57fe2c..d5aae09411 100644 --- a/python/dolfinx/fem/element.py +++ b/python/dolfinx/fem/element.py @@ -195,6 +195,9 @@ def basix_element(self) -> basix.finite_element.FiniteElement: Raises: Runtime error if Basix element does not exist. + + Note: + Cached property: Wrapper constructed on initial call and not updated on subsequent calls. """ return basix.finite_element.FiniteElement(self._cpp_object.basix_element) diff --git a/python/dolfinx/fem/function.py b/python/dolfinx/fem/function.py index 8c8a3277ea..52b6f91dc8 100644 --- a/python/dolfinx/fem/function.py +++ b/python/dolfinx/fem/function.py @@ -717,7 +717,11 @@ 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 From d957615ac44134d4998effe523f5f647d7603487 Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Thu, 13 Feb 2025 16:03:56 +0100 Subject: [PATCH 4/5] Ruff --- python/dolfinx/fem/element.py | 2 +- python/dolfinx/fem/function.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/python/dolfinx/fem/element.py b/python/dolfinx/fem/element.py index d5aae09411..bcd68a6774 100644 --- a/python/dolfinx/fem/element.py +++ b/python/dolfinx/fem/element.py @@ -195,7 +195,7 @@ def basix_element(self) -> basix.finite_element.FiniteElement: Raises: Runtime error if Basix element does not exist. - + Note: Cached property: Wrapper constructed on initial call and not updated on subsequent calls. """ diff --git a/python/dolfinx/fem/function.py b/python/dolfinx/fem/function.py index 52b6f91dc8..15935cf79b 100644 --- a/python/dolfinx/fem/function.py +++ b/python/dolfinx/fem/function.py @@ -718,7 +718,7 @@ def ufl_function_space(self) -> ufl.FunctionSpace: @cached_property def element(self) -> FiniteElement: """Function space finite element. - + Note: Cached property: Wrapper constructed on initial call and not updated on subsequent calls. """ From a3a61da6b6a260287cd1949d8711a29e54a9407e Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Thu, 13 Feb 2025 16:06:13 +0100 Subject: [PATCH 5/5] Ruff2 --- python/dolfinx/fem/element.py | 3 ++- python/dolfinx/fem/function.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/python/dolfinx/fem/element.py b/python/dolfinx/fem/element.py index bcd68a6774..6dd437c919 100644 --- a/python/dolfinx/fem/element.py +++ b/python/dolfinx/fem/element.py @@ -197,7 +197,8 @@ def basix_element(self) -> basix.finite_element.FiniteElement: Runtime error if Basix element does not exist. Note: - Cached property: Wrapper constructed on initial call and not updated on subsequent calls. + Cached property: Wrapper constructed on initial call and not updated on subsequent + calls. """ return basix.finite_element.FiniteElement(self._cpp_object.basix_element) diff --git a/python/dolfinx/fem/function.py b/python/dolfinx/fem/function.py index 15935cf79b..ea5f1ba16b 100644 --- a/python/dolfinx/fem/function.py +++ b/python/dolfinx/fem/function.py @@ -720,7 +720,8 @@ def element(self) -> FiniteElement: """Function space finite element. Note: - Cached property: Wrapper constructed on initial call and not updated on subsequent calls. + Cached property: Wrapper constructed on initial call and not updated on subsequent + calls. """ return FiniteElement(self._cpp_object.element)