Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions petsctools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import os

from petsctools.exceptions import PetscToolsException # noqa: F401
from .exceptions import PetscToolsException # noqa: F401


class MissingPetscException(PetscToolsException):
pass


def get_config():
import os

try:
import petsc4py
return petsc4py.get_config()
Expand All @@ -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()]
Expand All @@ -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
16 changes: 16 additions & 0 deletions petsctools/monitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import abc

from petsc4py import PETSc


class AbstractKSPMonitorFunction(abc.ABC):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good idea, but there's nothing that makes python monitors in particular special. If we're doing ABCs we should do as much of the suite of possible python types as possible, e.g. KSP, PC, SNES, Monitor, Linesearch etc. (see for example the KSP and PC examples in the petsc4py docs)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except I know how to do this for monitors but not anything else! I agree we should look to add more things. Have you implemented any of these before?

Copy link
Member

@JHopeCollins JHopeCollins May 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the protocols are here: https://petsc.org/main/petsc4py/petsc_python_types.html
I think there's some discussion about what should be an abstract method (i.e. throw an error if a child class doesn't implement it), and what are just blank methods to show what you can customise.

The main one missing from that link is SNES, but I know some of the methods and we can find the others from the petsc4py sauce.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I really want to have in petsctools are implementations of useful monitor functions and such. I'm less bothered about abstract classes and since petsc4py advertise most of the interfaces anyway it might be unnecessary here.

"""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