From b17c48c2109d1baa34925eb328a5078a22cecf98 Mon Sep 17 00:00:00 2001 From: Josh Hope-Collins Date: Thu, 30 Apr 2026 16:58:50 +0100 Subject: [PATCH] auxiliary pc with user provided Amat and optional Pmat --- petsctools/pc.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/petsctools/pc.py b/petsctools/pc.py index ea4e443..6ae1f70 100644 --- a/petsctools/pc.py +++ b/petsctools/pc.py @@ -1,5 +1,6 @@ import abc from .exceptions import PetscToolsException +from .options import _validate_prefix class PCBase(abc.ABC): @@ -153,3 +154,33 @@ def view(self, pc, viewer=None): pcname = f"{type(self).__module__}.{type(self).__name__}" viewer.printfASCII( f"Python type preconditioner {pcname}\n") + + +class AuxiliaryPC(PCBase): + prefix = "aux" + + def initialize(self, pc): + from petsc4py import PETSc + A, P = self.get_mats(pc) + ksp = PETSc.KSP().create(comm=pc.comm) + ksp.setOperators(A, P or A) + ksp.setOptionsPrefix( + _validate_prefix((pc.getOptionsPrefix() or "") + self.prefix) + ) + ksp.setFromOptions() + self.ksp = ksp + + def update(self, pc): + A, P = self.ksp.getOperators() + A.assemble() + if P is not A: + P.assemble() + + def apply(self, pc, x, y): + self.ksp.solve(x, y) + + def applyTranspose(self, pc, x, y): + self.ksp.solveTranspose(x, y) + + def mats(self, pc): + return pc.getOperators()