Parallel Finite Element - General geometry Ewald-like Method
Part of Continuum-particle Simulation Suite under MICCOM
stokes_solver.h
Go to the documentation of this file.
1 // Parallel Finite Element-General Geometry Ewald-like Method.
2 // Copyright (C) 2015-2016 Xujun Zhao, Jiyuan Li, Xikai Jiang
3 
4 // This code is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 
10 // This code 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 
16 // You should have received a copy of the GNU General Public
17 // License along with this code; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20 
21 //
22 // stokes_solver.h
23 //
24 //
25 // Created by Xujun Zhao on 2/17/16.
26 //
27 //
28 
29 #pragma once
30 
31 #include "libmesh/petsc_macro.h" // ahead of LIBMESH_HAVE_PETSC
32 #include "libmesh/libmesh_config.h"
33 
34 
35 // make sure libMesh has been complied with PETSc
36 #ifdef LIBMESH_HAVE_PETSC // 11111111111111111111111111111111
37 
38 
39 // C++ includes
40 #include <iostream>
41 //#include <memory>
42 
43 // Local includes libmesh headers
44 #include "libmesh/equation_systems.h"
45 #include "libmesh/parallel_object.h"
46 #include "libmesh/reference_counted_object.h"
47 
48 
49 // include PETSc KSP solver
50 EXTERN_C_FOR_PETSC_BEGIN
51 # include <petscksp.h>
52 EXTERN_C_FOR_PETSC_END
53 
54 
55 using libMesh::EquationSystems;
56 using libMesh::ReferenceCountedObject;
57 using libMesh::ParallelObject;
58 using libMesh::Real;
59 
60 
61 
62 /* this class performs schur complement reduction type solve
63  for the saddle point problems arised from mixed finite
64  element methods
65  */
66 
67 
68 
69 enum StokesSolverType
70 {
71  superLU_dist,
72  field_split,
73  user_define
74 };
75 
76 
77 
78 class StokesSolver : public ReferenceCountedObject<StokesSolver>,
79  public ParallelObject
80 //class SchurComplementSolver
81 {
82 public:
83  // constructor
84  StokesSolver(EquationSystems& es_stokes);
85 
86 
87  // constructor
88  StokesSolver(EquationSystems& es_stokes,
89  const StokesSolverType solver_type);
90 
91 
92  // Destructor
93  ~StokesSolver();
94 
95 
96  /*
97  * Init the KSP solver:
98  * The system matrix needs to be assembled before calling this init function!
99  */
100  void init_ksp_solver();
101 
102 
103  /*
104  * Return if the ksp solver is initialized or not.
105  */
106  const bool is_ksp_initialized() const { return _is_init; }
107 
108 
109  /*
110  * Set the solver type
111  */
112  void set_solver_type(const StokesSolverType solver_type);
113 
114 
115 
116  /*
117  * solve the equation system Ax = b
118  */
119  void solve();
120 
121 
122  /*
123  * Build IS for u/p for PC field split
124  * -called by: solve()
125  */
126  void build_is(IS *is_v,
127  IS *is_p);
128 
129 
130  /*
131  * Set up the PC for the schur complement reduction algorithm
132  * -called by solve()
133  */
134  void setup_schur_pc(KSP ksp,
135  IS is_v,
136  IS is_p,
137  Mat *pmat,
138  const bool userPC,
139  const bool userKSP);
140 
141 
142  /* ^
143  * Schur complement approximation S used as a PC for S*y1=y2, for example:
144  * S1 = A11 - A10 diag(A00)^(-1) A01;
145  * Sa = 1/v * Mp (pressure matrix, v is kinematic viscosity)
146  * -called by solve()
147  */
148  void setup_approx_schur_matrix(IS is_v,
149  IS is_p,
150  Mat *pmat);
151 
152 
153 
154  /*
155  * Return the number of linear iterations required to solve Ax=b
156  */
157  const unsigned int n_linear_iterations() const;
158 
159 
160  /*
161  * Return the final residual of the linear solve
162  */
163  const Real final_linear_residual() const;
164 
165 
166  /*
167  * Petsc View
168  */
169  void petsc_view_is(IS is_p) const;
170  void petsc_view_vector(Vec vector) const;
171  void petsc_view_matrix(Mat matix) const;
172 
173 
174 private:
175 
176  // EquationSystems* _es;
177  EquationSystems& _equation_systems;
178 
179  // the type of the solver used for Stokes
180  StokesSolverType _solver_type;
181 
182  // solver relative tolerance
183  PetscReal _rtol;
184 
185  // solver absolute tolerance
186  PetscReal _atol;
187 
188  // (iterative) solver maximum iteration
189  PetscInt _max_it;
190 
191  // KSP sover
192  KSP _ksp;
193 
194  // IS pointers for velocity and pressure, respectively
195  IS _is_v;
196  IS _is_p;
197 
198  // preconditioning matrix for Schur Complement
199  Mat _schur_pmat;
200 
201  // Label if the system is initialized.
202  // If not, the destructor cannot destroy PETSc objects.
203  bool _is_init;
204 }; // end of class SchurComplementSolver
205 #endif // #ifdef LIBMESH_HAVE_PETSC // 11111111111111111111111111111111
206 
207