From 168c198a3e314f6d7ec71eb97bf08310a78bbc8b Mon Sep 17 00:00:00 2001
From: Thomas Fischer <thomas.fischer@ufz.de>
Date: Mon, 20 Dec 2021 10:28:48 +0100
Subject: [PATCH] [MaL/LA] PETScLinearSolver constr. using string
 petsc_options.

---
 MathLib/LinAlg/PETSc/PETScLinearSolver.cpp | 22 ++++++++++++++++++++++
 MathLib/LinAlg/PETSc/PETScLinearSolver.h   | 11 +++++++++++
 2 files changed, 33 insertions(+)

diff --git a/MathLib/LinAlg/PETSc/PETScLinearSolver.cpp b/MathLib/LinAlg/PETSc/PETScLinearSolver.cpp
index f259b47ae6e..065f7675427 100644
--- a/MathLib/LinAlg/PETSc/PETScLinearSolver.cpp
+++ b/MathLib/LinAlg/PETSc/PETScLinearSolver.cpp
@@ -73,6 +73,28 @@ PETScLinearSolver::PETScLinearSolver(const std::string /*prefix*/,
     KSPSetFromOptions(solver_);  // set run-time options
 }
 
+PETScLinearSolver::PETScLinearSolver(std::string const& prefix,
+                                     std::string const& petsc_options)
+{
+#if PETSC_VERSION_LT(3, 7, 0)
+    PetscOptionsInsertString(petsc_options.c_str());
+#else
+    PetscOptionsInsertString(nullptr, petsc_options.c_str());
+#endif
+
+    KSPCreate(PETSC_COMM_WORLD, &solver_);
+
+    KSPGetPC(solver_, &pc_);
+
+    if (!prefix.empty())
+    {
+        KSPSetOptionsPrefix(solver_, prefix.c_str());
+    }
+
+    KSPSetInitialGuessNonzero(solver_, PETSC_TRUE);
+    KSPSetFromOptions(solver_);  // set run-time options
+}
+
 bool PETScLinearSolver::solve(PETScMatrix& A, PETScVector& b, PETScVector& x)
 {
     BaseLib::RunTime wtimer;
diff --git a/MathLib/LinAlg/PETSc/PETScLinearSolver.h b/MathLib/LinAlg/PETSc/PETScLinearSolver.h
index f8ae00dbddf..1690c95a5dc 100644
--- a/MathLib/LinAlg/PETSc/PETScLinearSolver.h
+++ b/MathLib/LinAlg/PETSc/PETScLinearSolver.h
@@ -48,6 +48,17 @@ public:
     */
     PETScLinearSolver(const std::string prefix,
                       BaseLib::ConfigTree const* const option);
+    /*!
+        Constructor
+        \param prefix  Name used to distinguish the options in the command
+                       line for this solver. It can be the name of the PDE
+                       that owns an instance of this class.
+        \param petsc_options PETSc options string which is passed to PETSc lib
+        and inserted in the PETSc option database (see
+        https://petsc.org/release/docs/manualpages/Sys/PetscOptionsInsertString.html).
+    */
+    PETScLinearSolver(std::string const& prefix,
+                      std::string const& petsc_options);
 
     ~PETScLinearSolver() { KSPDestroy(&solver_); }
     // TODO check if some args in LinearSolver interface can be made const&.
-- 
GitLab