MAST3
Multidisciplinary-design Adaptation and Sensitivity Toolkit (MAST)
linear_solver.hpp
Go to the documentation of this file.
1 /*
2  * MAST: Multidisciplinary-design Adaptation and Sensitivity Toolkit
3  * Copyright (C) 2013-2020 Manav Bhatia and MAST authors
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #ifndef __mast_petsc_linear_solver_h__
21 #define __mast_petsc_linear_solver_h__
22 
23 // MAST includes
25 #include <mast/base/exceptions.hpp>
26 
27 // PETSc includes
28 #include <petsc.h>
29 
30 
31 namespace MAST {
32 namespace Solvers {
33 namespace PETScWrapper {
34 
35 
36 class LinearSolver {
37 
38 public:
39 
40  LinearSolver(const MPI_Comm comm):
41  _comm (comm),
42  _A (nullptr) {
43 
44  }
45 
46 
47  virtual ~LinearSolver() {
48 
49  if (_ksp) {
50 
51  PetscErrorCode
52  ierr = KSPDestroy(&_ksp);
53  CHKERRABORT(_comm, ierr);
54  }
55  }
56 
63  inline void init(Mat A, const std::string* scope = nullptr) {
64 
65  Assert0(!_A, "solver already initialized");
66 
67  _A = &A;
68 
69  PC pc;
70 
71  // setup the KSP
72  PetscErrorCode
73  ierr = KSPCreate(_comm, &_ksp);
74  CHKERRABORT(_comm, ierr);
75 
76  if (scope) {
77 
78  std::string nm = *scope + "_";
79  ierr = KSPSetOptionsPrefix(_ksp, nm.c_str());
80  CHKERRABORT(_comm, ierr);
81  }
82 
83  ierr = KSPSetOperators(_ksp, *_A, *_A);
84  CHKERRABORT(_comm, ierr);
85 
86  ierr = KSPSetFromOptions(_ksp);
87  CHKERRABORT(_comm, ierr);
88 
89  // setup the PC
90  ierr = KSPGetPC(_ksp, &pc);
91  CHKERRABORT(_comm, ierr);
92 
93  ierr = PCSetFromOptions(pc);
94  CHKERRABORT(_comm, ierr);
95  }
96 
97 
102  inline void solve(Vec x, Vec b) {
103 
104  Assert0(_A, "solver not initialized");
105 
106  PetscErrorCode
107  ierr = 0;
108 
109  // now solve
110  ierr = KSPSolve(_ksp, b, x);
111  CHKERRABORT(_comm, ierr);
112  }
113 
114 
115  KSP ksp() {
116 
117  return _ksp;
118  }
119 
120 
121 private:
122 
123  const MPI_Comm _comm;
124 
125  KSP _ksp;
126  Mat *_A;
127 };
128 
129 }
130 }
131 }
132 
133 #endif // __mast_petsc_linear_solver_h__
void solve(Vec x, Vec b)
Solves , where is the system matrix.
#define Assert0(cond, msg)
Definition: exceptions.hpp:134
void init(Mat A, const std::string *scope=nullptr)
initialize the solver for operator matrix A.