From 22f99dab186570ef81ca95c17cd83f650b60070a Mon Sep 17 00:00:00 2001 From: "Jack S. Hale" Date: Tue, 6 Aug 2024 16:23:55 +0200 Subject: [PATCH 01/14] Expose more optional feature booleans in public API. --- python/dolfinx/__init__.py | 7 ++++++- python/test/unit/fem/test_petsc_nonlinear_assembler.py | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/python/dolfinx/__init__.py b/python/dolfinx/__init__.py index dcdfa35aa3f..6beb4bf8c10 100644 --- a/python/dolfinx/__init__.py +++ b/python/dolfinx/__init__.py @@ -30,10 +30,15 @@ from dolfinx.common import ( TimingType, git_commit_hash, + has_adios2, + has_complex_ufcx_kernels, has_debug, has_kahip, - has_petsc, has_parmetis, + has_petsc, + has_ptscotch, + has_slepc, + ufcx_signature, list_timings, timing, ) diff --git a/python/test/unit/fem/test_petsc_nonlinear_assembler.py b/python/test/unit/fem/test_petsc_nonlinear_assembler.py index 5f7bb7b3e92..57674c71cf2 100644 --- a/python/test/unit/fem/test_petsc_nonlinear_assembler.py +++ b/python/test/unit/fem/test_petsc_nonlinear_assembler.py @@ -55,6 +55,7 @@ def F_mono(self, snes, x, F): from dolfinx.fem.petsc import apply_lifting, assemble_vector, set_bc + print("ghost_update") x.ghostUpdate(addv=PETSc.InsertMode.INSERT, mode=PETSc.ScatterMode.FORWARD) with x.localForm() as _x: self.soln_vars.x.array[:] = _x.array_r From 16f5d1671b09ad5c8af7537c69de65f2c9a85ec3 Mon Sep 17 00:00:00 2001 From: "Jack S. Hale" Date: Tue, 6 Aug 2024 16:33:58 +0200 Subject: [PATCH 02/14] Update python/test/unit/fem/test_petsc_nonlinear_assembler.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jørgen Schartum Dokken --- python/test/unit/fem/test_petsc_nonlinear_assembler.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/test/unit/fem/test_petsc_nonlinear_assembler.py b/python/test/unit/fem/test_petsc_nonlinear_assembler.py index 57674c71cf2..5f7bb7b3e92 100644 --- a/python/test/unit/fem/test_petsc_nonlinear_assembler.py +++ b/python/test/unit/fem/test_petsc_nonlinear_assembler.py @@ -55,7 +55,6 @@ def F_mono(self, snes, x, F): from dolfinx.fem.petsc import apply_lifting, assemble_vector, set_bc - print("ghost_update") x.ghostUpdate(addv=PETSc.InsertMode.INSERT, mode=PETSc.ScatterMode.FORWARD) with x.localForm() as _x: self.soln_vars.x.array[:] = _x.array_r From 96dde5ce27b2125513a66841e58b0e5caf2b7379 Mon Sep 17 00:00:00 2001 From: "Jack S. Hale" Date: Tue, 6 Aug 2024 16:47:51 +0200 Subject: [PATCH 03/14] Top level namespace very cluttered - propose all this to common. --- python/dolfinx/__init__.py | 18 ------------------ python/dolfinx/common.py | 10 +++++++++- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/python/dolfinx/__init__.py b/python/dolfinx/__init__.py index 6beb4bf8c10..9a582c2dbdd 100644 --- a/python/dolfinx/__init__.py +++ b/python/dolfinx/__init__.py @@ -28,19 +28,7 @@ # Initialise logging from dolfinx.common import ( - TimingType, git_commit_hash, - has_adios2, - has_complex_ufcx_kernels, - has_debug, - has_kahip, - has_parmetis, - has_petsc, - has_ptscotch, - has_slepc, - ufcx_signature, - list_timings, - timing, ) from dolfinx.cpp import __version__ @@ -73,11 +61,5 @@ def get_include(user=False): "nls", "plot", "utils", - "TimingType", "git_commit_hash", - "has_debug", - "has_kahip", - "has_parmetis", - "list_timings", - "timing", ] diff --git a/python/dolfinx/common.py b/python/dolfinx/common.py index be70ce239ab..ffff9124db6 100644 --- a/python/dolfinx/common.py +++ b/python/dolfinx/common.py @@ -13,10 +13,14 @@ IndexMap, git_commit_hash, has_adios2, + has_complex_ufcx_kernels, has_debug, has_kahip, has_parmetis, + has_ptscotch, has_petsc, + has_slepc, + ufcx_signature, ) __all__ = [ @@ -25,10 +29,14 @@ "timed", "git_commit_hash", "has_adios2", + "has_complex_ufcx_kernels", "has_debug", "has_kahip", + "has_parmetis" "has_petsc", - "has_parmetis", + "has_ptscotch", + "has_slepc", + "ufcx_signature", ] TimingType = _cpp.common.TimingType From 88e3f7909470a7c99f0531f00741e84de004d29e Mon Sep 17 00:00:00 2001 From: "Jack S. Hale" Date: Tue, 6 Aug 2024 16:53:54 +0200 Subject: [PATCH 04/14] Fix. --- python/dolfinx/common.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/dolfinx/common.py b/python/dolfinx/common.py index ffff9124db6..7fbecc46231 100644 --- a/python/dolfinx/common.py +++ b/python/dolfinx/common.py @@ -17,8 +17,8 @@ has_debug, has_kahip, has_parmetis, - has_ptscotch, has_petsc, + has_ptscotch, has_slepc, ufcx_signature, ) @@ -32,7 +32,7 @@ "has_complex_ufcx_kernels", "has_debug", "has_kahip", - "has_parmetis" + "has_parmetis", "has_petsc", "has_ptscotch", "has_slepc", From bcc44a9a4ee9cb78f1ab7adf1519205b963ce6a0 Mon Sep 17 00:00:00 2001 From: "Jack S. Hale" Date: Tue, 6 Aug 2024 17:16:32 +0200 Subject: [PATCH 05/14] Fix demos. --- python/demo/demo_axis.py | 2 +- python/demo/demo_biharmonic.py | 2 +- python/demo/demo_cahn-hilliard.py | 2 +- python/demo/demo_elasticity.py | 2 +- python/demo/demo_half_loaded_waveguide.py | 2 +- python/demo/demo_hdg.py | 2 +- python/demo/demo_helmholtz.py | 2 +- python/demo/demo_mixed-poisson.py | 2 +- python/demo/demo_navier-stokes.py | 16 ++-------------- python/demo/demo_pml.py | 2 +- python/demo/demo_poisson.py | 2 +- .../demo/demo_scattering_boundary_conditions.py | 2 +- python/demo/demo_static-condensation.py | 2 +- python/demo/demo_stokes.py | 2 +- python/demo/demo_tnt-elements.py | 2 +- 15 files changed, 16 insertions(+), 28 deletions(-) diff --git a/python/demo/demo_axis.py b/python/demo/demo_axis.py index f8115241f19..9c04ca89d2a 100644 --- a/python/demo/demo_axis.py +++ b/python/demo/demo_axis.py @@ -42,7 +42,7 @@ scalar_type = PETSc.ScalarType real_type = PETSc.RealType - if not dolfinx.has_petsc: + if not dolfinx.common.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) except ModuleNotFoundError: diff --git a/python/demo/demo_biharmonic.py b/python/demo/demo_biharmonic.py index bf8f0d9c9bc..5010190e4b0 100644 --- a/python/demo/demo_biharmonic.py +++ b/python/demo/demo_biharmonic.py @@ -115,7 +115,7 @@ if importlib.util.find_spec("petsc4py") is not None: import dolfinx - if not dolfinx.has_petsc: + if not dolfinx.common.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) from petsc4py.PETSc import ScalarType # type: ignore diff --git a/python/demo/demo_cahn-hilliard.py b/python/demo/demo_cahn-hilliard.py index 846218670f8..5f8c575b36f 100644 --- a/python/demo/demo_cahn-hilliard.py +++ b/python/demo/demo_cahn-hilliard.py @@ -126,7 +126,7 @@ import dolfinx - if not dolfinx.has_petsc: + if not dolfinx.common.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) except ModuleNotFoundError: diff --git a/python/demo/demo_elasticity.py b/python/demo/demo_elasticity.py index 79c4856a6ff..535cf31e658 100644 --- a/python/demo/demo_elasticity.py +++ b/python/demo/demo_elasticity.py @@ -29,7 +29,7 @@ import dolfinx - if not dolfinx.has_petsc: + if not dolfinx.common.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) except ModuleNotFoundError: diff --git a/python/demo/demo_half_loaded_waveguide.py b/python/demo/demo_half_loaded_waveguide.py index a332f3d33aa..b63a75cc6f2 100644 --- a/python/demo/demo_half_loaded_waveguide.py +++ b/python/demo/demo_half_loaded_waveguide.py @@ -48,7 +48,7 @@ import dolfinx - if not dolfinx.has_petsc: + if not dolfinx.common.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) if PETSc.IntType == np.int64 and MPI.COMM_WORLD.size > 1: diff --git a/python/demo/demo_hdg.py b/python/demo/demo_hdg.py index efee7e9ebae..a511bb72dde 100644 --- a/python/demo/demo_hdg.py +++ b/python/demo/demo_hdg.py @@ -21,7 +21,7 @@ if importlib.util.find_spec("petsc4py") is not None: import dolfinx - if not dolfinx.has_petsc: + if not dolfinx.common.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) from petsc4py import PETSc diff --git a/python/demo/demo_helmholtz.py b/python/demo/demo_helmholtz.py index fe99f88f9f0..75624145b6f 100644 --- a/python/demo/demo_helmholtz.py +++ b/python/demo/demo_helmholtz.py @@ -26,7 +26,7 @@ import dolfinx - if not dolfinx.has_petsc: + if not dolfinx.common.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) except ModuleNotFoundError: diff --git a/python/demo/demo_mixed-poisson.py b/python/demo/demo_mixed-poisson.py index cd5811fdc1a..7b16c5cba99 100644 --- a/python/demo/demo_mixed-poisson.py +++ b/python/demo/demo_mixed-poisson.py @@ -98,7 +98,7 @@ import dolfinx - if not dolfinx.has_petsc: + if not dolfinx.common.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) except ModuleNotFoundError: diff --git a/python/demo/demo_navier-stokes.py b/python/demo/demo_navier-stokes.py index ebe5d73be7b..22630054189 100644 --- a/python/demo/demo_navier-stokes.py +++ b/python/demo/demo_navier-stokes.py @@ -168,7 +168,7 @@ if importlib.util.find_spec("petsc4py") is not None: import dolfinx - if not dolfinx.has_petsc: + if not dolfinx.common.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) else: @@ -176,6 +176,7 @@ exit(0) from mpi4py import MPI +from petsc4py import PETSc # + import numpy as np @@ -200,19 +201,6 @@ outer, ) -try: - from petsc4py import PETSc - - import dolfinx - - if not dolfinx.has_petsc: - print("This demo requires DOLFINx to be compiled with PETSc enabled.") - exit(0) -except ModuleNotFoundError: - print("This demo requires petsc4py.") - exit(0) - - if np.issubdtype(PETSc.ScalarType, np.complexfloating): # type: ignore print("Demo should only be executed with DOLFINx real mode") exit(0) diff --git a/python/demo/demo_pml.py b/python/demo/demo_pml.py index 7cf298df95d..eeaf3ec332d 100644 --- a/python/demo/demo_pml.py +++ b/python/demo/demo_pml.py @@ -20,7 +20,7 @@ if importlib.util.find_spec("petsc4py") is not None: import dolfinx - if not dolfinx.has_petsc: + if not dolfinx.common.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) else: diff --git a/python/demo/demo_poisson.py b/python/demo/demo_poisson.py index 78f3aec3982..552d78cb914 100644 --- a/python/demo/demo_poisson.py +++ b/python/demo/demo_poisson.py @@ -69,7 +69,7 @@ if importlib.util.find_spec("petsc4py") is not None: import dolfinx - if not dolfinx.has_petsc: + if not dolfinx.common.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) from petsc4py.PETSc import ScalarType # type: ignore diff --git a/python/demo/demo_scattering_boundary_conditions.py b/python/demo/demo_scattering_boundary_conditions.py index 81fdfa474da..39b03fd58ba 100644 --- a/python/demo/demo_scattering_boundary_conditions.py +++ b/python/demo/demo_scattering_boundary_conditions.py @@ -40,7 +40,7 @@ if importlib.util.find_spec("petsc4py") is not None: import dolfinx - if not dolfinx.has_petsc: + if not dolfinx.common.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) diff --git a/python/demo/demo_static-condensation.py b/python/demo/demo_static-condensation.py index 93c7d29d690..1be48219eb7 100644 --- a/python/demo/demo_static-condensation.py +++ b/python/demo/demo_static-condensation.py @@ -27,7 +27,7 @@ import dolfinx - if not dolfinx.has_petsc: + if not dolfinx.common.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) except ModuleNotFoundError: diff --git a/python/demo/demo_stokes.py b/python/demo/demo_stokes.py index 65de072c569..f5bab5c90d5 100644 --- a/python/demo/demo_stokes.py +++ b/python/demo/demo_stokes.py @@ -92,7 +92,7 @@ import dolfinx - if not dolfinx.has_petsc: + if not dolfinx.common.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) except ModuleNotFoundError: diff --git a/python/demo/demo_tnt-elements.py b/python/demo/demo_tnt-elements.py index 06eadf560b6..79e1b0bd261 100644 --- a/python/demo/demo_tnt-elements.py +++ b/python/demo/demo_tnt-elements.py @@ -25,7 +25,7 @@ if importlib.util.find_spec("petsc4py") is not None: import dolfinx - if not dolfinx.has_petsc: + if not dolfinx.common.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) else: From 68504c580d6a8237ccae96159e48196317b9b2ee Mon Sep 17 00:00:00 2001 From: "Jack S. Hale" Date: Tue, 6 Aug 2024 17:21:11 +0200 Subject: [PATCH 06/14] Fix. --- python/test/unit/fem/test_custom_jit_kernels.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 From b7965542ade1449eb7294f6368943820d3f0133a Mon Sep 17 00:00:00 2001 From: "Jack S. Hale" Date: Wed, 7 Aug 2024 13:59:11 +0200 Subject: [PATCH 07/14] Add has_petsc4py. has_petsc now refers to the state of the C++ library. --- python/dolfinx/common.py | 2 ++ python/dolfinx/wrappers/common.cpp | 1 + 2 files changed, 3 insertions(+) diff --git a/python/dolfinx/common.py b/python/dolfinx/common.py index 7fbecc46231..5e146b24bc9 100644 --- a/python/dolfinx/common.py +++ b/python/dolfinx/common.py @@ -18,6 +18,7 @@ has_kahip, has_parmetis, has_petsc, + has_petsc4py, has_ptscotch, has_slepc, ufcx_signature, @@ -34,6 +35,7 @@ "has_kahip", "has_parmetis", "has_petsc", + "has_petsc4py", "has_ptscotch", "has_slepc", "ufcx_signature", diff --git a/python/dolfinx/wrappers/common.cpp b/python/dolfinx/wrappers/common.cpp index 4c237fe190e..6a8a9e680c4 100644 --- a/python/dolfinx/wrappers/common.cpp +++ b/python/dolfinx/wrappers/common.cpp @@ -47,6 +47,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(); From b4572714f932825534c256937edc26f4df218fb4 Mon Sep 17 00:00:00 2001 From: "Jack S. Hale" Date: Wed, 7 Aug 2024 15:41:59 +0200 Subject: [PATCH 08/14] Add has_petsc4py. --- python/dolfinx/wrappers/common.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/python/dolfinx/wrappers/common.cpp b/python/dolfinx/wrappers/common.cpp index 6a8a9e680c4..992cee3be0b 100644 --- a/python/dolfinx/wrappers/common.cpp +++ b/python/dolfinx/wrappers/common.cpp @@ -36,6 +36,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) { @@ -47,7 +58,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_petsc4py") = has_petsc4py(); m.attr("has_ptscotch") = dolfinx::has_ptscotch(); m.attr("has_slepc") = dolfinx::has_slepc(); m.attr("ufcx_signature") = dolfinx::ufcx_signature(); From 813d42d890b6ec7e08a804f166e76405d32a004d Mon Sep 17 00:00:00 2001 From: "Jack S. Hale" Date: Wed, 7 Aug 2024 16:12:06 +0200 Subject: [PATCH 09/14] If we cannot find petsc4py module, or DOLFINx was built without petsc4py support, then exit. --- python/demo/demo_hdg.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/python/demo/demo_hdg.py b/python/demo/demo_hdg.py index a511bb72dde..efae8f259e7 100644 --- a/python/demo/demo_hdg.py +++ b/python/demo/demo_hdg.py @@ -19,17 +19,17 @@ import importlib.util if importlib.util.find_spec("petsc4py") is not None: - import dolfinx - - if not dolfinx.common.has_petsc: - print("This demo requires DOLFINx to be compiled with PETSc enabled.") - exit(0) from petsc4py import PETSc +else: + print("Could not find petsc4py module.") + exit(0) - from dolfinx.fem.petsc import assemble_matrix_block, assemble_vector_block +import dolfinx.common +if dolfinx.common.has_petsc4py: + from petsc4py import PETSc else: - print("This demo requires petsc4py.") + print("DOLFINx Python was built without petsc4py support.") exit(0) import sys @@ -41,6 +41,7 @@ import ufl from dolfinx import fem, mesh from dolfinx.cpp.mesh import cell_num_entities +from dolfinx.fem.petsc import assemble_matrix_block, assemble_vector_block from ufl import div, dot, grad, inner From 83fca009bd8129535754c8e123f2e49818608336 Mon Sep 17 00:00:00 2001 From: "Jack S. Hale" Date: Wed, 7 Aug 2024 16:28:21 +0200 Subject: [PATCH 10/14] Check that DOLFINx Python was built with petsc4py. --- python/dolfinx/__init__.py | 3 +++ python/dolfinx/fem/petsc.py | 5 +++++ python/dolfinx/nls/petsc.py | 3 +++ 3 files changed, 11 insertions(+) diff --git a/python/dolfinx/__init__.py b/python/dolfinx/__init__.py index 9a582c2dbdd..3b87a15f161 100644 --- a/python/dolfinx/__init__.py +++ b/python/dolfinx/__init__.py @@ -13,6 +13,9 @@ 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 diff --git a/python/dolfinx/fem/petsc.py b/python/dolfinx/fem/petsc.py index 2e83bf2ba83..8ccbec006e7 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.common + +assert dolfinx.common.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..7405f3277fc 100644 --- a/python/dolfinx/nls/petsc.py +++ b/python/dolfinx/nls/petsc.py @@ -13,6 +13,9 @@ from mpi4py import MPI from petsc4py import PETSc + import dolfinx.common + assert dolfinx.common.has_petsc4py + from dolfinx.fem.problem import NonlinearProblem import types From 6ecebf7d4e16bd094e2222dbe88a2ad6822251cc Mon Sep 17 00:00:00 2001 From: "Jack S. Hale" Date: Wed, 7 Aug 2024 16:32:24 +0200 Subject: [PATCH 11/14] Fix logic. --- python/demo/demo_hdg.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/python/demo/demo_hdg.py b/python/demo/demo_hdg.py index efae8f259e7..2c5ee8c04e7 100644 --- a/python/demo/demo_hdg.py +++ b/python/demo/demo_hdg.py @@ -16,26 +16,24 @@ # - Solve Poisson's equation using an HDG scheme. # + -import importlib.util +from mpi4py import MPI -if importlib.util.find_spec("petsc4py") is not None: - from petsc4py import PETSc -else: - print("Could not find petsc4py module.") +import dolfinx.common + +if not dolfinx.common.has_petsc4py: + print("DOLFINx Python was built without petsc4py support.") exit(0) -import dolfinx.common +import importlib.util -if dolfinx.common.has_petsc4py: +if importlib.util.find_spec("petsc4py") is not None: from petsc4py import PETSc else: - print("DOLFINx Python was built without petsc4py support.") + print("Could not find petsc4py module.") exit(0) import sys -from mpi4py import MPI - import numpy as np import ufl From e5f396337894ebafb1a01b9555f87fd2bd19d444 Mon Sep 17 00:00:00 2001 From: "Jack S. Hale" Date: Wed, 7 Aug 2024 16:38:08 +0200 Subject: [PATCH 12/14] Ruff format. --- python/dolfinx/__init__.py | 4 +++- python/dolfinx/nls/petsc.py | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/python/dolfinx/__init__.py b/python/dolfinx/__init__.py index 3b87a15f161..32726894b94 100644 --- a/python/dolfinx/__init__.py +++ b/python/dolfinx/__init__.py @@ -13,9 +13,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 + + assert dolfinx.common.has_petsc4py default_scalar_type = _PETSc.ScalarType # type: ignore default_real_type = _PETSc.RealType # type: ignore diff --git a/python/dolfinx/nls/petsc.py b/python/dolfinx/nls/petsc.py index 7405f3277fc..1c0781f1e38 100644 --- a/python/dolfinx/nls/petsc.py +++ b/python/dolfinx/nls/petsc.py @@ -14,6 +14,7 @@ from petsc4py import PETSc import dolfinx.common + assert dolfinx.common.has_petsc4py from dolfinx.fem.problem import NonlinearProblem From 6119ef90d5f4ff0ef34538b50c667b758232576f Mon Sep 17 00:00:00 2001 From: "Jack S. Hale" Date: Wed, 11 Sep 2024 17:06:57 +0200 Subject: [PATCH 13/14] Revert moving feature to booleans to common. --- python/demo/demo_axis.py | 2 +- python/demo/demo_biharmonic.py | 2 +- python/demo/demo_cahn-hilliard.py | 2 +- python/demo/demo_elasticity.py | 2 +- python/demo/demo_half_loaded_waveguide.py | 2 +- python/demo/demo_hdg.py | 21 ++++++++++--------- python/demo/demo_helmholtz.py | 2 +- python/demo/demo_mixed-poisson.py | 2 +- python/demo/demo_navier-stokes.py | 16 ++++++++++++-- python/demo/demo_pml.py | 2 +- python/demo/demo_poisson.py | 2 +- .../demo_scattering_boundary_conditions.py | 2 +- python/demo/demo_static-condensation.py | 2 +- python/demo/demo_stokes.py | 2 +- python/demo/demo_tnt-elements.py | 2 +- python/dolfinx/__init__.py | 21 ++++++++++++++++++- python/dolfinx/fem/petsc.py | 4 ++-- python/dolfinx/nls/petsc.py | 4 ++-- 18 files changed, 62 insertions(+), 30 deletions(-) diff --git a/python/demo/demo_axis.py b/python/demo/demo_axis.py index 9c04ca89d2a..f8115241f19 100644 --- a/python/demo/demo_axis.py +++ b/python/demo/demo_axis.py @@ -42,7 +42,7 @@ scalar_type = PETSc.ScalarType real_type = PETSc.RealType - if not dolfinx.common.has_petsc: + if not dolfinx.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) except ModuleNotFoundError: diff --git a/python/demo/demo_biharmonic.py b/python/demo/demo_biharmonic.py index 5010190e4b0..bf8f0d9c9bc 100644 --- a/python/demo/demo_biharmonic.py +++ b/python/demo/demo_biharmonic.py @@ -115,7 +115,7 @@ if importlib.util.find_spec("petsc4py") is not None: import dolfinx - if not dolfinx.common.has_petsc: + if not dolfinx.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) from petsc4py.PETSc import ScalarType # type: ignore diff --git a/python/demo/demo_cahn-hilliard.py b/python/demo/demo_cahn-hilliard.py index 5f8c575b36f..846218670f8 100644 --- a/python/demo/demo_cahn-hilliard.py +++ b/python/demo/demo_cahn-hilliard.py @@ -126,7 +126,7 @@ import dolfinx - if not dolfinx.common.has_petsc: + if not dolfinx.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) except ModuleNotFoundError: diff --git a/python/demo/demo_elasticity.py b/python/demo/demo_elasticity.py index 535cf31e658..79c4856a6ff 100644 --- a/python/demo/demo_elasticity.py +++ b/python/demo/demo_elasticity.py @@ -29,7 +29,7 @@ import dolfinx - if not dolfinx.common.has_petsc: + if not dolfinx.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) except ModuleNotFoundError: diff --git a/python/demo/demo_half_loaded_waveguide.py b/python/demo/demo_half_loaded_waveguide.py index b63a75cc6f2..a332f3d33aa 100644 --- a/python/demo/demo_half_loaded_waveguide.py +++ b/python/demo/demo_half_loaded_waveguide.py @@ -48,7 +48,7 @@ import dolfinx - if not dolfinx.common.has_petsc: + if not dolfinx.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) if PETSc.IntType == np.int64 and MPI.COMM_WORLD.size > 1: diff --git a/python/demo/demo_hdg.py b/python/demo/demo_hdg.py index 2c5ee8c04e7..efee7e9ebae 100644 --- a/python/demo/demo_hdg.py +++ b/python/demo/demo_hdg.py @@ -16,30 +16,31 @@ # - Solve Poisson's equation using an HDG scheme. # + -from mpi4py import MPI - -import dolfinx.common - -if not dolfinx.common.has_petsc4py: - print("DOLFINx Python was built without petsc4py support.") - exit(0) - import importlib.util if importlib.util.find_spec("petsc4py") is not None: + import dolfinx + + if not dolfinx.has_petsc: + print("This demo requires DOLFINx to be compiled with PETSc enabled.") + exit(0) from petsc4py import PETSc + + from dolfinx.fem.petsc import assemble_matrix_block, assemble_vector_block + else: - print("Could not find petsc4py module.") + print("This demo requires petsc4py.") exit(0) import sys +from mpi4py import MPI + import numpy as np import ufl from dolfinx import fem, mesh from dolfinx.cpp.mesh import cell_num_entities -from dolfinx.fem.petsc import assemble_matrix_block, assemble_vector_block from ufl import div, dot, grad, inner diff --git a/python/demo/demo_helmholtz.py b/python/demo/demo_helmholtz.py index 75624145b6f..fe99f88f9f0 100644 --- a/python/demo/demo_helmholtz.py +++ b/python/demo/demo_helmholtz.py @@ -26,7 +26,7 @@ import dolfinx - if not dolfinx.common.has_petsc: + if not dolfinx.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) except ModuleNotFoundError: diff --git a/python/demo/demo_mixed-poisson.py b/python/demo/demo_mixed-poisson.py index 7b16c5cba99..cd5811fdc1a 100644 --- a/python/demo/demo_mixed-poisson.py +++ b/python/demo/demo_mixed-poisson.py @@ -98,7 +98,7 @@ import dolfinx - if not dolfinx.common.has_petsc: + if not dolfinx.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) except ModuleNotFoundError: diff --git a/python/demo/demo_navier-stokes.py b/python/demo/demo_navier-stokes.py index 22630054189..ebe5d73be7b 100644 --- a/python/demo/demo_navier-stokes.py +++ b/python/demo/demo_navier-stokes.py @@ -168,7 +168,7 @@ if importlib.util.find_spec("petsc4py") is not None: import dolfinx - if not dolfinx.common.has_petsc: + if not dolfinx.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) else: @@ -176,7 +176,6 @@ exit(0) from mpi4py import MPI -from petsc4py import PETSc # + import numpy as np @@ -201,6 +200,19 @@ outer, ) +try: + from petsc4py import PETSc + + import dolfinx + + if not dolfinx.has_petsc: + print("This demo requires DOLFINx to be compiled with PETSc enabled.") + exit(0) +except ModuleNotFoundError: + print("This demo requires petsc4py.") + exit(0) + + if np.issubdtype(PETSc.ScalarType, np.complexfloating): # type: ignore print("Demo should only be executed with DOLFINx real mode") exit(0) diff --git a/python/demo/demo_pml.py b/python/demo/demo_pml.py index eeaf3ec332d..7cf298df95d 100644 --- a/python/demo/demo_pml.py +++ b/python/demo/demo_pml.py @@ -20,7 +20,7 @@ if importlib.util.find_spec("petsc4py") is not None: import dolfinx - if not dolfinx.common.has_petsc: + if not dolfinx.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) else: diff --git a/python/demo/demo_poisson.py b/python/demo/demo_poisson.py index 552d78cb914..78f3aec3982 100644 --- a/python/demo/demo_poisson.py +++ b/python/demo/demo_poisson.py @@ -69,7 +69,7 @@ if importlib.util.find_spec("petsc4py") is not None: import dolfinx - if not dolfinx.common.has_petsc: + if not dolfinx.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) from petsc4py.PETSc import ScalarType # type: ignore diff --git a/python/demo/demo_scattering_boundary_conditions.py b/python/demo/demo_scattering_boundary_conditions.py index 39b03fd58ba..81fdfa474da 100644 --- a/python/demo/demo_scattering_boundary_conditions.py +++ b/python/demo/demo_scattering_boundary_conditions.py @@ -40,7 +40,7 @@ if importlib.util.find_spec("petsc4py") is not None: import dolfinx - if not dolfinx.common.has_petsc: + if not dolfinx.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) diff --git a/python/demo/demo_static-condensation.py b/python/demo/demo_static-condensation.py index 1be48219eb7..93c7d29d690 100644 --- a/python/demo/demo_static-condensation.py +++ b/python/demo/demo_static-condensation.py @@ -27,7 +27,7 @@ import dolfinx - if not dolfinx.common.has_petsc: + if not dolfinx.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) except ModuleNotFoundError: diff --git a/python/demo/demo_stokes.py b/python/demo/demo_stokes.py index f5bab5c90d5..65de072c569 100644 --- a/python/demo/demo_stokes.py +++ b/python/demo/demo_stokes.py @@ -92,7 +92,7 @@ import dolfinx - if not dolfinx.common.has_petsc: + if not dolfinx.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) except ModuleNotFoundError: diff --git a/python/demo/demo_tnt-elements.py b/python/demo/demo_tnt-elements.py index 79e1b0bd261..06eadf560b6 100644 --- a/python/demo/demo_tnt-elements.py +++ b/python/demo/demo_tnt-elements.py @@ -25,7 +25,7 @@ if importlib.util.find_spec("petsc4py") is not None: import dolfinx - if not dolfinx.common.has_petsc: + if not dolfinx.has_petsc: print("This demo requires DOLFINx to be compiled with PETSc enabled.") exit(0) else: diff --git a/python/dolfinx/__init__.py b/python/dolfinx/__init__.py index 32726894b94..cedb2d26516 100644 --- a/python/dolfinx/__init__.py +++ b/python/dolfinx/__init__.py @@ -31,9 +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 ( 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 ) from dolfinx.cpp import __version__ @@ -67,4 +76,14 @@ def get_include(user=False): "plot", "utils", "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" ] diff --git a/python/dolfinx/fem/petsc.py b/python/dolfinx/fem/petsc.py index 4064a922251..c5a01e4265a 100644 --- a/python/dolfinx/fem/petsc.py +++ b/python/dolfinx/fem/petsc.py @@ -20,9 +20,9 @@ from petsc4py import PETSc # ruff: noqa: E402 -import dolfinx.common +import dolfinx -assert dolfinx.common.has_petsc4py +assert dolfinx.has_petsc4py import numpy as np diff --git a/python/dolfinx/nls/petsc.py b/python/dolfinx/nls/petsc.py index 1c0781f1e38..33f1f22a7fa 100644 --- a/python/dolfinx/nls/petsc.py +++ b/python/dolfinx/nls/petsc.py @@ -13,9 +13,9 @@ from mpi4py import MPI from petsc4py import PETSc - import dolfinx.common + import dolfinx - assert dolfinx.common.has_petsc4py + assert dolfinx.has_petsc4py from dolfinx.fem.problem import NonlinearProblem From 3219e616eb9cc52f3aa365eccee5628f471e17e6 Mon Sep 17 00:00:00 2001 From: "Jack S. Hale" Date: Wed, 11 Sep 2024 17:08:39 +0200 Subject: [PATCH 14/14] Ruff format. --- python/dolfinx/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/dolfinx/__init__.py b/python/dolfinx/__init__.py index cedb2d26516..97755fe44f9 100644 --- a/python/dolfinx/__init__.py +++ b/python/dolfinx/__init__.py @@ -42,7 +42,7 @@ has_petsc4py, has_ptscotch, has_slepc, - ufcx_signature + ufcx_signature, ) from dolfinx.cpp import __version__ @@ -85,5 +85,5 @@ def get_include(user=False): "has_petsc4py", "has_ptscotch", "has_slepc", - "ufcx_signature" + "ufcx_signature", ]