Skip to content

Commit

Permalink
Expose more optional feature booleans, remove Timer from top level `d…
Browse files Browse the repository at this point in the history
…olfinx` (#3335)

* Expose more optional feature booleans in public API.

* Update python/test/unit/fem/test_petsc_nonlinear_assembler.py

Co-authored-by: Jørgen Schartum Dokken <dokken92@gmail.com>

* Top level namespace very cluttered - propose all this to common.

* Fix.

* Fix demos.

* Fix.

* Add has_petsc4py. has_petsc now refers to the state of the C++ library.

* Add has_petsc4py.

* If we cannot find petsc4py module, or DOLFINx was built without petsc4py
support, then exit.

* Check that DOLFINx Python was built with petsc4py.

* Fix logic.

* Ruff format.

* Revert moving feature to booleans to common.

* Ruff format.

---------

Co-authored-by: Jørgen Schartum Dokken <dokken92@gmail.com>
  • Loading branch information
jhale and jorgensd committed Sep 11, 2024
1 parent 11afc1d commit 4abcfb8
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 10 deletions.
27 changes: 19 additions & 8 deletions python/dolfinx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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__

Expand Down Expand Up @@ -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",
]
12 changes: 11 additions & 1 deletion python/dolfinx/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__ = [
Expand 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
Expand Down
5 changes: 5 additions & 0 deletions python/dolfinx/fem/petsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions python/dolfinx/nls/petsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions python/dolfinx/wrappers/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion python/test/unit/fem/test_custom_jit_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 4abcfb8

Please sign in to comment.