From fc97ab83f240555db5fb1fffc2762223124c9ada Mon Sep 17 00:00:00 2001 From: Stefano Zampini Date: Tue, 17 Jan 2023 10:43:47 +0300 Subject: [PATCH] Use vmult and vmultT for PETSc shell preconditioning remove defaults --- include/deal.II/lac/petsc_precondition.h | 9 +++----- source/lac/petsc_precondition.cc | 28 ++++++++++++------------ tests/petsc/solver_03_mf.cc | 9 ++++++++ 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/include/deal.II/lac/petsc_precondition.h b/include/deal.II/lac/petsc_precondition.h index 321c250d48..a6ad69c86f 100644 --- a/include/deal.II/lac/petsc_precondition.h +++ b/include/deal.II/lac/petsc_precondition.h @@ -1099,18 +1099,15 @@ namespace PETScWrappers */ PreconditionShell(const MPI_Comm &communicator); - /** - * The callback for the application of the preconditioner. Defaults to - * a copy operation if not provided. + * The callback for the application of the preconditioner. */ - std::function apply; + std::function vmult; /** * The callback for the application of the transposed preconditioner. - * Defaults to the non-transpose operation if not provided. */ - std::function applyT; + std::function vmultT; protected: /** diff --git a/source/lac/petsc_precondition.cc b/source/lac/petsc_precondition.cc index 67fe466393..873c0139c1 100644 --- a/source/lac/petsc_precondition.cc +++ b/source/lac/petsc_precondition.cc @@ -68,7 +68,7 @@ namespace PETScWrappers { AssertThrow(pc != nullptr, StandardExceptions::ExcInvalidState()); - const PetscErrorCode ierr = PCApply(pc, src, dst); + PetscErrorCode ierr = PCApply(pc, src, dst); AssertThrow(ierr == 0, ExcPETScError(ierr)); } @@ -77,7 +77,7 @@ namespace PETScWrappers { AssertThrow(pc != nullptr, StandardExceptions::ExcInvalidState()); - const PetscErrorCode ierr = PCApplyTranspose(pc, src, dst); + PetscErrorCode ierr = PCApplyTranspose(pc, src, dst); AssertThrow(ierr == 0, ExcPETScError(ierr)); } @@ -1115,13 +1115,14 @@ namespace PETScWrappers PetscErrorCode ierr = PCShellGetContext(ppc, &ctx); AssertThrow(ierr == 0, ExcPETScError(ierr)); + auto user = static_cast(ctx); + AssertThrow(user->vmult, + StandardExceptions::ExcFunctionNotProvided( + "std::function vmult")); + VectorBase src(x); VectorBase dst(y); - auto user = static_cast(ctx); - if (user->apply) - user->apply(dst, src); - else - dst.sadd(0, src); + user->vmult(dst, src); PetscFunctionReturn(0); } @@ -1134,15 +1135,14 @@ namespace PETScWrappers PetscErrorCode ierr = PCShellGetContext(ppc, &ctx); AssertThrow(ierr == 0, ExcPETScError(ierr)); - auto user = static_cast(ctx); + auto user = static_cast(ctx); + AssertThrow(user->vmultT, + StandardExceptions::ExcFunctionNotProvided( + "std::function vmultT")); + VectorBase src(x); VectorBase dst(y); - if (user->applyT) - user->applyT(dst, src); - else if (user->apply) - user->apply(dst, src); - else - dst.sadd(0, src); + user->vmult(dst, src); PetscFunctionReturn(0); } diff --git a/tests/petsc/solver_03_mf.cc b/tests/petsc/solver_03_mf.cc index 31a65e9ed1..d9d90231af 100644 --- a/tests/petsc/solver_03_mf.cc +++ b/tests/petsc/solver_03_mf.cc @@ -65,6 +65,15 @@ main(int argc, char **argv) u = 0.; PETScWrappers::PreconditionShell preconditioner_user(A); + + // Identity preconditioner + preconditioner_user.vmult = + [](PETScWrappers::VectorBase & dst, + const PETScWrappers::VectorBase &src) -> int { + dst = src; + return 0; + }; + check_solver_within_range(solver.solve(A, u, f, preconditioner_user), control.last_step(), 42, -- 2.39.5