]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Use vmult and vmultT for PETSc shell preconditioning
authorStefano Zampini <stefano.zampini@gmail.com>
Tue, 17 Jan 2023 07:43:47 +0000 (10:43 +0300)
committerStefano Zampini <stefano.zampini@gmail.com>
Sun, 22 Jan 2023 14:07:09 +0000 (17:07 +0300)
remove defaults

include/deal.II/lac/petsc_precondition.h
source/lac/petsc_precondition.cc
tests/petsc/solver_03_mf.cc

index 321c250d482a6e929a6237b05712f9bab3448f3f..a6ad69c86fe3bb13ff5f27788fb57ec8499ed276 100644 (file)
@@ -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<int(VectorBase &dst, const VectorBase &src)> apply;
+    std::function<int(VectorBase &dst, const VectorBase &src)> vmult;
 
     /**
      * The callback for the application of the transposed preconditioner.
-     * Defaults to the non-transpose operation if not provided.
      */
-    std::function<int(VectorBase &dst, const VectorBase &src)> applyT;
+    std::function<int(VectorBase &dst, const VectorBase &src)> vmultT;
 
   protected:
     /**
index 67fe46639377c3aaf6d2f9150d197c59b94c0b98..873c0139c1bf12e82c4e1f929753b2275b782f8a 100644 (file)
@@ -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<PreconditionShell *>(ctx);
+    AssertThrow(user->vmult,
+                StandardExceptions::ExcFunctionNotProvided(
+                  "std::function vmult"));
+
     VectorBase src(x);
     VectorBase dst(y);
-    auto       user = static_cast<PreconditionShell *>(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<PreconditionShell *>(ctx);
+    auto user = static_cast<PreconditionShell *>(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);
   }
 
index 31a65e9ed1f0a138460285f48f296c98614dc362..d9d90231aff33f9f6ec76436892a2d706ba3237c 100644 (file)
@@ -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,

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.