diff --git a/python/dolfinx/__init__.py b/python/dolfinx/__init__.py index dcdfa35aa3f..97755fe44f9 100644 --- a/python/dolfinx/__init__.py +++ b/python/dolfinx/__init__.py @@ -14,6 +14,11 @@ try: from petsc4py import PETSc as _PETSc + # Additional sanity check that DOLFINx was built with petsc4py support. + import dolfinx.common + + assert dolfinx.common.has_petsc4py + default_scalar_type = _PETSc.ScalarType # type: ignore default_real_type = _PETSc.RealType # type: ignore except ImportError: @@ -26,16 +31,18 @@ from dolfinx import cpp as _cpp from dolfinx import fem, geometry, graph, io, jit, la, log, mesh, nls, plot, utils -# Initialise logging from dolfinx.common import ( - TimingType, git_commit_hash, + has_adios2, + has_complex_ufcx_kernels, has_debug, has_kahip, - has_petsc, has_parmetis, - list_timings, - timing, + has_petsc, + has_petsc4py, + has_ptscotch, + has_slepc, + ufcx_signature, ) from dolfinx.cpp import __version__ @@ -68,11 +75,15 @@ def get_include(user=False): "nls", "plot", "utils", - "TimingType", "git_commit_hash", + "has_adios2", + "has_complex_ufcx_kernels", "has_debug", "has_kahip", "has_parmetis", - "list_timings", - "timing", + "has_petsc", + "has_petsc4py", + "has_ptscotch", + "has_slepc", + "ufcx_signature", ] diff --git a/python/dolfinx/common.py b/python/dolfinx/common.py index 3fd71f1e2fc..6172ffad2b5 100644 --- a/python/dolfinx/common.py +++ b/python/dolfinx/common.py @@ -13,10 +13,15 @@ IndexMap, git_commit_hash, has_adios2, + has_complex_ufcx_kernels, has_debug, has_kahip, has_parmetis, has_petsc, + has_petsc4py, + has_ptscotch, + has_slepc, + ufcx_signature, ) __all__ = [ @@ -25,10 +30,15 @@ "timed", "git_commit_hash", "has_adios2", + "has_complex_ufcx_kernels", "has_debug", "has_kahip", - "has_petsc", "has_parmetis", + "has_petsc", + "has_petsc4py", + "has_ptscotch", + "has_slepc", + "ufcx_signature", ] TimingType = _cpp.common.TimingType diff --git a/python/dolfinx/fem/petsc.py b/python/dolfinx/fem/petsc.py index cf9f630dcc5..c5a01e4265a 100644 --- a/python/dolfinx/fem/petsc.py +++ b/python/dolfinx/fem/petsc.py @@ -19,6 +19,11 @@ from petsc4py import PETSc +# ruff: noqa: E402 +import dolfinx + +assert dolfinx.has_petsc4py + import numpy as np import dolfinx.cpp as _cpp diff --git a/python/dolfinx/nls/petsc.py b/python/dolfinx/nls/petsc.py index 6400034d83d..33f1f22a7fa 100644 --- a/python/dolfinx/nls/petsc.py +++ b/python/dolfinx/nls/petsc.py @@ -13,6 +13,10 @@ from mpi4py import MPI from petsc4py import PETSc + import dolfinx + + assert dolfinx.has_petsc4py + from dolfinx.fem.problem import NonlinearProblem import types diff --git a/python/dolfinx/wrappers/common.cpp b/python/dolfinx/wrappers/common.cpp index 6844ad719cb..d50dc056905 100644 --- a/python/dolfinx/wrappers/common.cpp +++ b/python/dolfinx/wrappers/common.cpp @@ -37,6 +37,17 @@ namespace nb = nanobind; namespace dolfinx_wrappers { + +/// Return true if DOLFINx is compiled with petsc4py +consteval bool has_petsc4py() +{ +#ifdef HAS_PETSC4PY + return true; +#else + return false; +#endif +} + // Interface for dolfinx/common void common(nb::module_& m) { @@ -48,6 +59,7 @@ void common(nb::module_& m) m.attr("has_kahip") = dolfinx::has_kahip(); m.attr("has_parmetis") = dolfinx::has_parmetis(); m.attr("has_petsc") = dolfinx::has_petsc(); + m.attr("has_petsc4py") = has_petsc4py(); m.attr("has_ptscotch") = dolfinx::has_ptscotch(); m.attr("has_slepc") = dolfinx::has_slepc(); m.attr("ufcx_signature") = dolfinx::ufcx_signature(); diff --git a/python/test/unit/fem/test_custom_jit_kernels.py b/python/test/unit/fem/test_custom_jit_kernels.py index 3695afdc6d2..138512de47b 100644 --- a/python/test/unit/fem/test_custom_jit_kernels.py +++ b/python/test/unit/fem/test_custom_jit_kernels.py @@ -17,8 +17,9 @@ import dolfinx import dolfinx.utils import ffcx.codegeneration.utils -from dolfinx import TimingType, fem, la, list_timings from dolfinx import cpp as _cpp +from dolfinx import fem, la +from dolfinx.common import TimingType, list_timings from dolfinx.fem import Form, Function, IntegralType, form_cpp_class, functionspace from dolfinx.mesh import create_unit_square