From f8b25081c42cf49dc893fae9a276dd7ce6fe7720 Mon Sep 17 00:00:00 2001 From: Connor Ward Date: Thu, 22 May 2025 11:04:56 -0400 Subject: [PATCH 1/2] Add abc for KSPMonitorFunction --- petsctools/monitor.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 petsctools/monitor.py diff --git a/petsctools/monitor.py b/petsctools/monitor.py new file mode 100644 index 0000000..e9b0a26 --- /dev/null +++ b/petsctools/monitor.py @@ -0,0 +1,16 @@ +import abc + +from petsc4py import PETSc + + +class AbstractKSPMonitorFunction(abc.ABC): + """Abstract base class for a KSP monitor function. + + In order to use it it must be attached to a KSP using the function + `PETSc.KSP.setMonitor`. + + """ + + @abc.abstractmethod + def __call__(self, ksp: PETSc.KSP, iteration: int, residual_norm: float): + pass From bb517155c3490dc08130350808e453fc32aa70ef Mon Sep 17 00:00:00 2001 From: Connor Ward Date: Thu, 22 May 2025 11:08:23 -0400 Subject: [PATCH 2/2] Add AbstractKSPMonitorFunction to __init__.py --- petsctools/__init__.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/petsctools/__init__.py b/petsctools/__init__.py index f4e7c58..718f90d 100644 --- a/petsctools/__init__.py +++ b/petsctools/__init__.py @@ -1,6 +1,4 @@ -import os - -from petsctools.exceptions import PetscToolsException # noqa: F401 +from .exceptions import PetscToolsException # noqa: F401 class MissingPetscException(PetscToolsException): @@ -8,6 +6,8 @@ class MissingPetscException(PetscToolsException): def get_config(): + import os + try: import petsc4py return petsc4py.get_config() @@ -34,6 +34,8 @@ def get_petsc_arch(): def get_petscvariables(): """Return PETSc's configuration information.""" + import os + path = os.path.join(get_petsc_dir(), get_petsc_arch(), "lib/petsc/conf/petscvariables") with open(path) as f: pairs = [line.split("=", maxsplit=1) for line in f.readlines()] @@ -43,13 +45,16 @@ def get_petscvariables(): try: import petsc4py # noqa: F401 petsc4py_found = True + del petsc4py except ImportError: petsc4py_found = False - if petsc4py_found: - from petsctools.init import ( # noqa: F401 + from .init import ( # noqa: F401 InvalidEnvironmentException, InvalidPetscVersionException, init, ) + from .monitor import AbstractKSPMonitorFunction # noqa: F401 + +del petsc4py_found