xref: /petsc/doc/manual/ksp.md (revision 7f296bb328fcd4c99f2da7bfe8ba7ed8a4ebceee)
1*7f296bb3SBarry Smith(ch_ksp)=
2*7f296bb3SBarry Smith
3*7f296bb3SBarry Smith# KSP: Linear System Solvers
4*7f296bb3SBarry Smith
5*7f296bb3SBarry SmithThe `KSP` object is the heart of PETSc, because it provides uniform
6*7f296bb3SBarry Smithand efficient access to all of the package’s linear system solvers,
7*7f296bb3SBarry Smithincluding parallel and sequential, direct and iterative. `KSP` is
8*7f296bb3SBarry Smithintended for solving systems of the form
9*7f296bb3SBarry Smith
10*7f296bb3SBarry Smith$$
11*7f296bb3SBarry SmithA x = b,
12*7f296bb3SBarry Smith$$ (eq_axeqb)
13*7f296bb3SBarry Smith
14*7f296bb3SBarry Smithwhere $A$ denotes the matrix representation of a linear operator,
15*7f296bb3SBarry Smith$b$ is the right-hand-side vector, and $x$ is the solution
16*7f296bb3SBarry Smithvector. `KSP` uses the same calling sequence for both direct and
17*7f296bb3SBarry Smithiterative solution of a linear system. In addition, particular solution
18*7f296bb3SBarry Smithtechniques and their associated options can be selected at runtime.
19*7f296bb3SBarry Smith
20*7f296bb3SBarry SmithThe combination of a Krylov subspace method and a preconditioner is at
21*7f296bb3SBarry Smiththe center of most modern numerical codes for the iterative solution of
22*7f296bb3SBarry Smithlinear systems. Many textbooks (e.g. {cite}`fgn` {cite}`vandervorst2003`, or {cite}`saad2003`) provide an
23*7f296bb3SBarry Smithoverview of the theory of such methods.
24*7f296bb3SBarry SmithThe `KSP` package, discussed in
25*7f296bb3SBarry Smith{any}`sec_ksp`, provides many popular Krylov subspace
26*7f296bb3SBarry Smithiterative methods; the `PC` module, described in
27*7f296bb3SBarry Smith{any}`sec_pc`, includes a variety of preconditioners.
28*7f296bb3SBarry Smith
29*7f296bb3SBarry Smith(sec_usingksp)=
30*7f296bb3SBarry Smith
31*7f296bb3SBarry Smith## Using KSP
32*7f296bb3SBarry Smith
33*7f296bb3SBarry SmithTo solve a linear system with `KSP`, one must first create a solver
34*7f296bb3SBarry Smithcontext with the command
35*7f296bb3SBarry Smith
36*7f296bb3SBarry Smith```
37*7f296bb3SBarry SmithKSPCreate(MPI_Comm comm,KSP *ksp);
38*7f296bb3SBarry Smith```
39*7f296bb3SBarry Smith
40*7f296bb3SBarry SmithHere `comm` is the MPI communicator and `ksp` is the newly formed
41*7f296bb3SBarry Smithsolver context. Before actually solving a linear system with `KSP`,
42*7f296bb3SBarry Smiththe user must call the following routine to set the matrices associated
43*7f296bb3SBarry Smithwith the linear system:
44*7f296bb3SBarry Smith
45*7f296bb3SBarry Smith```
46*7f296bb3SBarry SmithKSPSetOperators(KSP ksp,Mat Amat,Mat Pmat);
47*7f296bb3SBarry Smith```
48*7f296bb3SBarry Smith
49*7f296bb3SBarry SmithThe argument `Amat`, representing the matrix that defines the linear
50*7f296bb3SBarry Smithsystem, is a symbolic placeholder for any kind of matrix or operator. In
51*7f296bb3SBarry Smithparticular, `KSP` *does* support matrix-free methods. The routine
52*7f296bb3SBarry Smith`MatCreateShell()` in {any}`sec_matrixfree`
53*7f296bb3SBarry Smithprovides further information regarding matrix-free methods. Typically,
54*7f296bb3SBarry Smiththe matrix from which the preconditioner is to be constructed, `Pmat`,
55*7f296bb3SBarry Smithis the same as the matrix that defines the linear system, `Amat`;
56*7f296bb3SBarry Smithhowever, occasionally these matrices differ (for instance, when a
57*7f296bb3SBarry Smithpreconditioning matrix is obtained from a lower order method than that
58*7f296bb3SBarry Smithemployed to form the linear system matrix).
59*7f296bb3SBarry Smith
60*7f296bb3SBarry SmithMuch of the power of `KSP` can be accessed through the single routine
61*7f296bb3SBarry Smith
62*7f296bb3SBarry Smith```
63*7f296bb3SBarry SmithKSPSetFromOptions(KSP ksp);
64*7f296bb3SBarry Smith```
65*7f296bb3SBarry Smith
66*7f296bb3SBarry SmithThis routine accepts the option `-help` as well as any of
67*7f296bb3SBarry Smiththe `KSP` and `PC` options discussed below. To solve a linear
68*7f296bb3SBarry Smithsystem, one sets the right hand size and solution vectors using the
69*7f296bb3SBarry Smithcommand
70*7f296bb3SBarry Smith
71*7f296bb3SBarry Smith```
72*7f296bb3SBarry SmithKSPSolve(KSP ksp,Vec b,Vec x);
73*7f296bb3SBarry Smith```
74*7f296bb3SBarry Smith
75*7f296bb3SBarry Smithwhere `b` and `x` respectively denote the right-hand side and
76*7f296bb3SBarry Smithsolution vectors. On return, the iteration number at which the iterative
77*7f296bb3SBarry Smithprocess stopped can be obtained using
78*7f296bb3SBarry Smith
79*7f296bb3SBarry Smith```
80*7f296bb3SBarry SmithKSPGetIterationNumber(KSP ksp, PetscInt *its);
81*7f296bb3SBarry Smith```
82*7f296bb3SBarry Smith
83*7f296bb3SBarry SmithNote that this does not state that the method converged at this
84*7f296bb3SBarry Smithiteration: it can also have reached the maximum number of iterations, or
85*7f296bb3SBarry Smithhave diverged.
86*7f296bb3SBarry Smith
87*7f296bb3SBarry Smith{any}`sec_convergencetests` gives more details
88*7f296bb3SBarry Smithregarding convergence testing. Note that multiple linear solves can be
89*7f296bb3SBarry Smithperformed by the same `KSP` context. Once the `KSP` context is no
90*7f296bb3SBarry Smithlonger needed, it should be destroyed with the command
91*7f296bb3SBarry Smith
92*7f296bb3SBarry Smith```
93*7f296bb3SBarry SmithKSPDestroy(KSP *ksp);
94*7f296bb3SBarry Smith```
95*7f296bb3SBarry Smith
96*7f296bb3SBarry SmithThe above procedure is sufficient for general use of the `KSP`
97*7f296bb3SBarry Smithpackage. One additional step is required for users who wish to customize
98*7f296bb3SBarry Smithcertain preconditioners (e.g., see {any}`sec_bjacobi`) or
99*7f296bb3SBarry Smithto log certain performance data using the PETSc profiling facilities (as
100*7f296bb3SBarry Smithdiscussed in {any}`ch_profiling`). In this case, the user can
101*7f296bb3SBarry Smithoptionally explicitly call
102*7f296bb3SBarry Smith
103*7f296bb3SBarry Smith```
104*7f296bb3SBarry SmithKSPSetUp(KSP ksp);
105*7f296bb3SBarry Smith```
106*7f296bb3SBarry Smith
107*7f296bb3SBarry Smithbefore calling `KSPSolve()` to perform any setup required for the
108*7f296bb3SBarry Smithlinear solvers. The explicit call of this routine enables the separate
109*7f296bb3SBarry Smithprofiling of any computations performed during the set up phase, such
110*7f296bb3SBarry Smithas incomplete factorization for the ILU preconditioner.
111*7f296bb3SBarry Smith
112*7f296bb3SBarry SmithThe default solver within `KSP` is restarted GMRES, `KSPGMRES`, preconditioned for
113*7f296bb3SBarry Smiththe uniprocess case with ILU(0), and for the multiprocess case with the
114*7f296bb3SBarry Smithblock Jacobi method (with one block per process, each of which is solved
115*7f296bb3SBarry Smithwith ILU(0)). A variety of other solvers and options are also available.
116*7f296bb3SBarry SmithTo allow application programmers to set any of the preconditioner or
117*7f296bb3SBarry SmithKrylov subspace options directly within the code, we provide routines
118*7f296bb3SBarry Smiththat extract the `PC` and `KSP` contexts,
119*7f296bb3SBarry Smith
120*7f296bb3SBarry Smith```
121*7f296bb3SBarry SmithKSPGetPC(KSP ksp,PC *pc);
122*7f296bb3SBarry Smith```
123*7f296bb3SBarry Smith
124*7f296bb3SBarry SmithThe application programmer can then directly call any of the `PC` or
125*7f296bb3SBarry Smith`KSP` routines to modify the corresponding default options.
126*7f296bb3SBarry Smith
127*7f296bb3SBarry SmithTo solve a linear system with a direct solver (supported by
128*7f296bb3SBarry SmithPETSc for sequential matrices, and by several external solvers through
129*7f296bb3SBarry SmithPETSc interfaces, see {any}`sec_externalsol`) one may use
130*7f296bb3SBarry Smiththe options `-ksp_type` `preonly` (or the equivalent `-ksp_type` `none`)
131*7f296bb3SBarry Smith`-pc_type` `lu` or `-pc_type` `cholesky` (see below).
132*7f296bb3SBarry Smith
133*7f296bb3SBarry SmithBy default, if a direct solver is used, the factorization is *not* done
134*7f296bb3SBarry Smithin-place. This approach prevents the user from the unexpected surprise
135*7f296bb3SBarry Smithof having a corrupted matrix after a linear solve. The routine
136*7f296bb3SBarry Smith`PCFactorSetUseInPlace()`, discussed below, causes factorization to be
137*7f296bb3SBarry Smithdone in-place.
138*7f296bb3SBarry Smith
139*7f296bb3SBarry Smith## Solving Successive Linear Systems
140*7f296bb3SBarry Smith
141*7f296bb3SBarry SmithWhen solving multiple linear systems of the same size with the same
142*7f296bb3SBarry Smithmethod, several options are available. To solve successive linear
143*7f296bb3SBarry Smithsystems having the *same* preconditioner matrix (i.e., the same data
144*7f296bb3SBarry Smithstructure with exactly the same matrix elements) but different
145*7f296bb3SBarry Smithright-hand-side vectors, the user should simply call `KSPSolve()`
146*7f296bb3SBarry Smithmultiple times. The preconditioner setup operations (e.g., factorization
147*7f296bb3SBarry Smithfor ILU) will be done during the first call to `KSPSolve()` only; such
148*7f296bb3SBarry Smithoperations will *not* be repeated for successive solves.
149*7f296bb3SBarry Smith
150*7f296bb3SBarry SmithTo solve successive linear systems that have *different* matrix values, because you
151*7f296bb3SBarry Smithhave changed the matrix values in the `Mat` objects you passed to `KSPSetOperators()`,
152*7f296bb3SBarry Smithstill simply call `KPSSolve()`. In this case the preconditioner will be recomputed
153*7f296bb3SBarry Smithautomatically. Use the option `-ksp_reuse_preconditioner true`, or call
154*7f296bb3SBarry Smith`KSPSetReusePreconditioner()`, to reuse the previously computed preconditioner.
155*7f296bb3SBarry SmithFor many problems, if the matrix changes values only slightly, reusing the
156*7f296bb3SBarry Smithold preconditioner can be more efficient.
157*7f296bb3SBarry Smith
158*7f296bb3SBarry SmithIf you wish to reuse the `KSP` with a different sized matrix and vectors, you must
159*7f296bb3SBarry Smithcall `KSPReset()` before calling `KSPSetOperators()` with the new matrix.
160*7f296bb3SBarry Smith
161*7f296bb3SBarry Smith(sec_ksp)=
162*7f296bb3SBarry Smith
163*7f296bb3SBarry Smith## Krylov Methods
164*7f296bb3SBarry Smith
165*7f296bb3SBarry SmithThe Krylov subspace methods accept a number of options, many of which
166*7f296bb3SBarry Smithare discussed below. First, to set the Krylov subspace method that is to
167*7f296bb3SBarry Smithbe used, one calls the command
168*7f296bb3SBarry Smith
169*7f296bb3SBarry Smith```
170*7f296bb3SBarry SmithKSPSetType(KSP ksp,KSPType method);
171*7f296bb3SBarry Smith```
172*7f296bb3SBarry Smith
173*7f296bb3SBarry SmithThe type can be one of `KSPRICHARDSON`, `KSPCHEBYSHEV`, `KSPCG`,
174*7f296bb3SBarry Smith`KSPGMRES`, `KSPTCQMR`, `KSPBCGS`, `KSPCGS`, `KSPTFQMR`,
175*7f296bb3SBarry Smith`KSPCR`, `KSPLSQR`, `KSPBICG`, `KSPPREONLY` (or the equivalent `KSPNONE`), or others; see
176*7f296bb3SBarry Smith{any}`tab-kspdefaults` or the `KSPType` man page for more.
177*7f296bb3SBarry SmithThe `KSP` method can also be set with the options database command
178*7f296bb3SBarry Smith`-ksp_type`, followed by one of the options `richardson`,
179*7f296bb3SBarry Smith`chebyshev`, `cg`, `gmres`, `tcqmr`, `bcgs`, `cgs`,
180*7f296bb3SBarry Smith`tfqmr`, `cr`, `lsqr`, `bicg`, `preonly` (or the equivalent `none`), or others (see
181*7f296bb3SBarry Smith{any}`tab-kspdefaults` or the `KSPType` man page). There are
182*7f296bb3SBarry Smithmethod-specific options. For instance, for the Richardson, Chebyshev, and
183*7f296bb3SBarry SmithGMRES methods:
184*7f296bb3SBarry Smith
185*7f296bb3SBarry Smith```
186*7f296bb3SBarry SmithKSPRichardsonSetScale(KSP ksp,PetscReal scale);
187*7f296bb3SBarry SmithKSPChebyshevSetEigenvalues(KSP ksp,PetscReal emax,PetscReal emin);
188*7f296bb3SBarry SmithKSPGMRESSetRestart(KSP ksp,PetscInt max_steps);
189*7f296bb3SBarry Smith```
190*7f296bb3SBarry Smith
191*7f296bb3SBarry SmithThe default parameter values are
192*7f296bb3SBarry Smith`scale=1.0, emax=0.01, emin=100.0`, and `max_steps=30`. The
193*7f296bb3SBarry SmithGMRES restart and Richardson damping factor can also be set with the
194*7f296bb3SBarry Smithoptions `-ksp_gmres_restart <n>` and
195*7f296bb3SBarry Smith`-ksp_richardson_scale <factor>`.
196*7f296bb3SBarry Smith
197*7f296bb3SBarry SmithThe default technique for orthogonalization of the Krylov vectors in
198*7f296bb3SBarry SmithGMRES is the unmodified (classical) Gram-Schmidt method, which can be
199*7f296bb3SBarry Smithset with
200*7f296bb3SBarry Smith
201*7f296bb3SBarry Smith```
202*7f296bb3SBarry SmithKSPGMRESSetOrthogonalization(KSP ksp,KSPGMRESClassicalGramSchmidtOrthogonalization);
203*7f296bb3SBarry Smith```
204*7f296bb3SBarry Smith
205*7f296bb3SBarry Smithor the options database command `-ksp_gmres_classicalgramschmidt`. By
206*7f296bb3SBarry Smithdefault this will *not* use iterative refinement to improve the
207*7f296bb3SBarry Smithstability of the orthogonalization. This can be changed with the option
208*7f296bb3SBarry Smith
209*7f296bb3SBarry Smith```
210*7f296bb3SBarry SmithKSPGMRESSetCGSRefinementType(KSP ksp,KSPGMRESCGSRefinementType type)
211*7f296bb3SBarry Smith```
212*7f296bb3SBarry Smith
213*7f296bb3SBarry Smithor via the options database with
214*7f296bb3SBarry Smith
215*7f296bb3SBarry Smith```
216*7f296bb3SBarry Smith-ksp_gmres_cgs_refinement_type <refine_never,refine_ifneeded,refine_always>
217*7f296bb3SBarry Smith```
218*7f296bb3SBarry Smith
219*7f296bb3SBarry SmithThe values for `KSPGMRESCGSRefinementType()` are
220*7f296bb3SBarry Smith`KSP_GMRES_CGS_REFINE_NEVER`, `KSP_GMRES_CGS_REFINE_IFNEEDED`
221*7f296bb3SBarry Smithand `KSP_GMRES_CGS_REFINE_ALWAYS`.
222*7f296bb3SBarry Smith
223*7f296bb3SBarry SmithOne can also use modified Gram-Schmidt, by using the orthogonalization
224*7f296bb3SBarry Smithroutine `KSPGMRESModifiedGramSchmidtOrthogonalization()` or by using
225*7f296bb3SBarry Smiththe command line option `-ksp_gmres_modifiedgramschmidt`.
226*7f296bb3SBarry Smith
227*7f296bb3SBarry SmithFor the conjugate gradient method with complex numbers, there are two
228*7f296bb3SBarry Smithslightly different algorithms depending on whether the matrix is
229*7f296bb3SBarry SmithHermitian symmetric or truly symmetric (the default is to assume that it
230*7f296bb3SBarry Smithis Hermitian symmetric). To indicate that it is symmetric, one uses the
231*7f296bb3SBarry Smithcommand
232*7f296bb3SBarry Smith
233*7f296bb3SBarry Smith```
234*7f296bb3SBarry SmithKSPCGSetType(ksp,KSP_CG_SYMMETRIC);
235*7f296bb3SBarry Smith```
236*7f296bb3SBarry Smith
237*7f296bb3SBarry SmithNote that this option is not valid for all matrices.
238*7f296bb3SBarry Smith
239*7f296bb3SBarry SmithSome `KSP` types do not support preconditioning. For instance,
240*7f296bb3SBarry Smiththe CGLS algorithm does not involve a preconditioner; any preconditioner
241*7f296bb3SBarry Smithset to work with the `KSP` object is ignored if `KSPCGLS` was
242*7f296bb3SBarry Smithselected.
243*7f296bb3SBarry Smith
244*7f296bb3SBarry SmithBy default, `KSP` assumes an initial guess of zero by zeroing the
245*7f296bb3SBarry Smithinitial value for the solution vector that is given; this zeroing is
246*7f296bb3SBarry Smithdone at the call to `KSPSolve()`. To use a nonzero initial guess, the
247*7f296bb3SBarry Smithuser *must* call
248*7f296bb3SBarry Smith
249*7f296bb3SBarry Smith```
250*7f296bb3SBarry SmithKSPSetInitialGuessNonzero(KSP ksp,PetscBool flg);
251*7f296bb3SBarry Smith```
252*7f296bb3SBarry Smith
253*7f296bb3SBarry Smith(sec_ksppc)=
254*7f296bb3SBarry Smith
255*7f296bb3SBarry Smith### Preconditioning within KSP
256*7f296bb3SBarry Smith
257*7f296bb3SBarry SmithSince the rate of convergence of Krylov projection methods for a
258*7f296bb3SBarry Smithparticular linear system is strongly dependent on its spectrum,
259*7f296bb3SBarry Smithpreconditioning is typically used to alter the spectrum and hence
260*7f296bb3SBarry Smithaccelerate the convergence rate of iterative techniques. Preconditioning
261*7f296bb3SBarry Smithcan be applied to the system {eq}`eq_axeqb` by
262*7f296bb3SBarry Smith
263*7f296bb3SBarry Smith$$
264*7f296bb3SBarry Smith(M_L^{-1} A M_R^{-1}) \, (M_R x) = M_L^{-1} b,
265*7f296bb3SBarry Smith$$ (eq_prec)
266*7f296bb3SBarry Smith
267*7f296bb3SBarry Smithwhere $M_L$ and $M_R$ indicate preconditioning matrices (or,
268*7f296bb3SBarry Smithmatrices from which the preconditioner is to be constructed). If
269*7f296bb3SBarry Smith$M_L = I$ in {eq}`eq_prec`, right preconditioning
270*7f296bb3SBarry Smithresults, and the residual of {eq}`eq_axeqb`,
271*7f296bb3SBarry Smith
272*7f296bb3SBarry Smith$$
273*7f296bb3SBarry Smithr \equiv b - Ax = b - A M_R^{-1} \, M_R x,
274*7f296bb3SBarry Smith$$
275*7f296bb3SBarry Smith
276*7f296bb3SBarry Smithis preserved. In contrast, the residual is altered for left
277*7f296bb3SBarry Smith($M_R = I$) and symmetric preconditioning, as given by
278*7f296bb3SBarry Smith
279*7f296bb3SBarry Smith$$
280*7f296bb3SBarry Smithr_L \equiv M_L^{-1} b - M_L^{-1} A x = M_L^{-1} r.
281*7f296bb3SBarry Smith$$
282*7f296bb3SBarry Smith
283*7f296bb3SBarry SmithBy default, most KSP implementations use left preconditioning. Some more
284*7f296bb3SBarry Smithnaturally use other options, though. For instance, `KSPQCG` defaults
285*7f296bb3SBarry Smithto use symmetric preconditioning and `KSPFGMRES` uses right
286*7f296bb3SBarry Smithpreconditioning by default. Right preconditioning can be activated for
287*7f296bb3SBarry Smithsome methods by using the options database command
288*7f296bb3SBarry Smith`-ksp_pc_side right` or calling the routine
289*7f296bb3SBarry Smith
290*7f296bb3SBarry Smith```
291*7f296bb3SBarry SmithKSPSetPCSide(ksp,PC_RIGHT);
292*7f296bb3SBarry Smith```
293*7f296bb3SBarry Smith
294*7f296bb3SBarry SmithAttempting to use right preconditioning for a method that does not
295*7f296bb3SBarry Smithcurrently support it results in an error message of the form
296*7f296bb3SBarry Smith
297*7f296bb3SBarry Smith```none
298*7f296bb3SBarry SmithKSPSetUp_Richardson:No right preconditioning for KSPRICHARDSON
299*7f296bb3SBarry Smith```
300*7f296bb3SBarry Smith
301*7f296bb3SBarry Smith```{eval-rst}
302*7f296bb3SBarry Smith.. list-table:: KSP Objects
303*7f296bb3SBarry Smith  :name: tab-kspdefaults
304*7f296bb3SBarry Smith  :header-rows: 1
305*7f296bb3SBarry Smith
306*7f296bb3SBarry Smith  * - Method
307*7f296bb3SBarry Smith    - KSPType
308*7f296bb3SBarry Smith    - Options Database
309*7f296bb3SBarry Smith  * - Richardson
310*7f296bb3SBarry Smith    - ``KSPRICHARDSON``
311*7f296bb3SBarry Smith    - ``richardson``
312*7f296bb3SBarry Smith  * - Chebyshev
313*7f296bb3SBarry Smith    - ``KSPCHEBYSHEV``
314*7f296bb3SBarry Smith    - ``chebyshev``
315*7f296bb3SBarry Smith  * - Conjugate Gradient :cite:`hs:52`
316*7f296bb3SBarry Smith    - ``KSPCG``
317*7f296bb3SBarry Smith    - ``cg``
318*7f296bb3SBarry Smith  * - Pipelined Conjugate Gradients :cite:`ghyselsvanroose2014`
319*7f296bb3SBarry Smith    - ``KSPPIPECG``
320*7f296bb3SBarry Smith    - ``pipecg``
321*7f296bb3SBarry Smith  * - Pipelined Conjugate Gradients (Gropp)
322*7f296bb3SBarry Smith    - ``KSPGROPPCG``
323*7f296bb3SBarry Smith    - ``groppcg``
324*7f296bb3SBarry Smith  * - Pipelined Conjugate Gradients with Residual Replacement
325*7f296bb3SBarry Smith    - ``KSPPIPECGRR``
326*7f296bb3SBarry Smith    - ``pipecgrr``
327*7f296bb3SBarry Smith  * - Conjugate Gradients for the Normal Equations
328*7f296bb3SBarry Smith    - ``KSPCGNE``
329*7f296bb3SBarry Smith    - ``cgne``
330*7f296bb3SBarry Smith  * - Flexible Conjugate Gradients :cite:`flexiblecg`
331*7f296bb3SBarry Smith    - ``KSPFCG``
332*7f296bb3SBarry Smith    - ``fcg``
333*7f296bb3SBarry Smith  * -  Pipelined, Flexible Conjugate Gradients :cite:`sananschneppmay2016`
334*7f296bb3SBarry Smith    - ``KSPPIPEFCG``
335*7f296bb3SBarry Smith    - ``pipefcg``
336*7f296bb3SBarry Smith  * - Conjugate Gradients for Least Squares
337*7f296bb3SBarry Smith    - ``KSPCGLS``
338*7f296bb3SBarry Smith    - ``cgls``
339*7f296bb3SBarry Smith  * - Conjugate Gradients with Constraint (1)
340*7f296bb3SBarry Smith    - ``KSPNASH``
341*7f296bb3SBarry Smith    - ``nash``
342*7f296bb3SBarry Smith  * - Conjugate Gradients with Constraint (2)
343*7f296bb3SBarry Smith    - ``KSPSTCG``
344*7f296bb3SBarry Smith    - ``stcg``
345*7f296bb3SBarry Smith  * - Conjugate Gradients with Constraint (3)
346*7f296bb3SBarry Smith    - ``KSPGLTR``
347*7f296bb3SBarry Smith    - ``gltr``
348*7f296bb3SBarry Smith  * - Conjugate Gradients with Constraint (4)
349*7f296bb3SBarry Smith    - ``KSPQCG``
350*7f296bb3SBarry Smith    - ``qcg``
351*7f296bb3SBarry Smith  * - BiConjugate Gradient
352*7f296bb3SBarry Smith    - ``KSPBICG``
353*7f296bb3SBarry Smith    - ``bicg``
354*7f296bb3SBarry Smith  * - BiCGSTAB :cite:`v:92`
355*7f296bb3SBarry Smith    - ``KSPBCGS``
356*7f296bb3SBarry Smith    - ``bcgs``
357*7f296bb3SBarry Smith  * - Improved BiCGSTAB
358*7f296bb3SBarry Smith    - ``KSPIBCGS``
359*7f296bb3SBarry Smith    - ``ibcgs``
360*7f296bb3SBarry Smith  * - QMRCGSTAB :cite:`chan1994qmrcgs`
361*7f296bb3SBarry Smith    - ``KSPQMRCGS``
362*7f296bb3SBarry Smith    - ``qmrcgs``
363*7f296bb3SBarry Smith  * - Flexible BiCGSTAB
364*7f296bb3SBarry Smith    - ``KSPFBCGS``
365*7f296bb3SBarry Smith    - ``fbcgs``
366*7f296bb3SBarry Smith  * - Flexible BiCGSTAB (variant)
367*7f296bb3SBarry Smith    - ``KSPFBCGSR``
368*7f296bb3SBarry Smith    - ``fbcgsr``
369*7f296bb3SBarry Smith  * - Enhanced BiCGSTAB(L)
370*7f296bb3SBarry Smith    - ``KSPBCGSL``
371*7f296bb3SBarry Smith    - ``bcgsl``
372*7f296bb3SBarry Smith  * - Minimal Residual Method :cite:`paige.saunders:solution`
373*7f296bb3SBarry Smith    - ``KSPMINRES``
374*7f296bb3SBarry Smith    - ``minres``
375*7f296bb3SBarry Smith  * - Generalized Minimal Residual :cite:`saad.schultz:gmres`
376*7f296bb3SBarry Smith    - ``KSPGMRES``
377*7f296bb3SBarry Smith    - ``gmres``
378*7f296bb3SBarry Smith  * - Flexible Generalized Minimal Residual :cite:`saad1993`
379*7f296bb3SBarry Smith    - ``KSPFGMRES``
380*7f296bb3SBarry Smith    - ``fgmres``
381*7f296bb3SBarry Smith  * - Deflated Generalized Minimal Residual
382*7f296bb3SBarry Smith    - ``KSPDGMRES``
383*7f296bb3SBarry Smith    - ``dgmres``
384*7f296bb3SBarry Smith  * - Pipelined Generalized Minimal Residual :cite:`ghyselsashbymeerbergenvanroose2013`
385*7f296bb3SBarry Smith    - ``KSPPGMRES``
386*7f296bb3SBarry Smith    - ``pgmres``
387*7f296bb3SBarry Smith  * - Pipelined, Flexible Generalized Minimal Residual :cite:`sananschneppmay2016`
388*7f296bb3SBarry Smith    - ``KSPPIPEFGMRES``
389*7f296bb3SBarry Smith    - ``pipefgmres``
390*7f296bb3SBarry Smith  * - Generalized Minimal Residual with Accelerated Restart
391*7f296bb3SBarry Smith    - ``KSPLGMRES``
392*7f296bb3SBarry Smith    - ``lgmres``
393*7f296bb3SBarry Smith  * - Conjugate Residual :cite:`eisenstat1983variational`
394*7f296bb3SBarry Smith    - ``KSPCR``
395*7f296bb3SBarry Smith    - ``cr``
396*7f296bb3SBarry Smith  * - Generalized Conjugate Residual
397*7f296bb3SBarry Smith    - ``KSPGCR``
398*7f296bb3SBarry Smith    - ``gcr``
399*7f296bb3SBarry Smith  * - Pipelined Conjugate Residual
400*7f296bb3SBarry Smith    - ``KSPPIPECR``
401*7f296bb3SBarry Smith    - ``pipecr``
402*7f296bb3SBarry Smith  * - Pipelined, Flexible Conjugate Residual :cite:`sananschneppmay2016`
403*7f296bb3SBarry Smith    - ``KSPPIPEGCR``
404*7f296bb3SBarry Smith    - ``pipegcr``
405*7f296bb3SBarry Smith  * - FETI-DP
406*7f296bb3SBarry Smith    - ``KSPFETIDP``
407*7f296bb3SBarry Smith    - ``fetidp``
408*7f296bb3SBarry Smith  * - Conjugate Gradient Squared :cite:`so:89`
409*7f296bb3SBarry Smith    - ``KSPCGS``
410*7f296bb3SBarry Smith    - ``cgs``
411*7f296bb3SBarry Smith  * - Transpose-Free Quasi-Minimal Residual (1) :cite:`f:93`
412*7f296bb3SBarry Smith    - ``KSPTFQMR``
413*7f296bb3SBarry Smith    - ``tfqmr``
414*7f296bb3SBarry Smith  * - Transpose-Free Quasi-Minimal Residual (2)
415*7f296bb3SBarry Smith    - ``KSPTCQMR``
416*7f296bb3SBarry Smith    - ``tcqmr``
417*7f296bb3SBarry Smith  * - Least Squares Method
418*7f296bb3SBarry Smith    - ``KSPLSQR``
419*7f296bb3SBarry Smith    - ``lsqr``
420*7f296bb3SBarry Smith  * - Symmetric LQ Method :cite:`paige.saunders:solution`
421*7f296bb3SBarry Smith    - ``KSPSYMMLQ``
422*7f296bb3SBarry Smith    - ``symmlq``
423*7f296bb3SBarry Smith  * - TSIRM
424*7f296bb3SBarry Smith    - ``KSPTSIRM``
425*7f296bb3SBarry Smith    - ``tsirm``
426*7f296bb3SBarry Smith  * - Python Shell
427*7f296bb3SBarry Smith    - ``KSPPYTHON``
428*7f296bb3SBarry Smith    - ``python``
429*7f296bb3SBarry Smith  * - Shell for no ``KSP`` method
430*7f296bb3SBarry Smith    - ``KSPNONE``
431*7f296bb3SBarry Smith    - ``none``
432*7f296bb3SBarry Smith
433*7f296bb3SBarry Smith```
434*7f296bb3SBarry Smith
435*7f296bb3SBarry SmithNote: the bi-conjugate gradient method requires application of both the
436*7f296bb3SBarry Smithmatrix and its transpose plus the preconditioner and its transpose.
437*7f296bb3SBarry SmithCurrently not all matrices and preconditioners provide this support and
438*7f296bb3SBarry Smiththus the `KSPBICG` cannot always be used.
439*7f296bb3SBarry Smith
440*7f296bb3SBarry SmithNote: PETSc implements the FETI-DP (Finite Element Tearing and
441*7f296bb3SBarry SmithInterconnecting Dual-Primal) method as an implementation of `KSP` since it recasts the
442*7f296bb3SBarry Smithoriginal problem into a constrained minimization one with Lagrange
443*7f296bb3SBarry Smithmultipliers. The only matrix type supported is `MATIS`. Support for
444*7f296bb3SBarry Smithsaddle point problems is provided. See the man page for `KSPFETIDP` for
445*7f296bb3SBarry Smithfurther details.
446*7f296bb3SBarry Smith
447*7f296bb3SBarry Smith(sec_convergencetests)=
448*7f296bb3SBarry Smith
449*7f296bb3SBarry Smith### Convergence Tests
450*7f296bb3SBarry Smith
451*7f296bb3SBarry SmithThe default convergence test, `KSPConvergedDefault()`, uses the \$ l_2 \$ norm of the preconditioned \$ B(b - A x) \$ or unconditioned residual \$ b - Ax\$, depending on the `KSPType` and the value of `KSPNormType` set with `KSPSetNormType`. For `KSPCG` and `KSPGMRES` the default is the norm of the preconditioned residual.
452*7f296bb3SBarry SmithThe preconditioned residual is used by default for
453*7f296bb3SBarry Smithconvergence testing of all left-preconditioned `KSP` methods. For the
454*7f296bb3SBarry Smithconjugate gradient, Richardson, and Chebyshev methods the true residual
455*7f296bb3SBarry Smithcan be used by the options database command
456*7f296bb3SBarry Smith`-ksp_norm_type unpreconditioned` or by calling the routine
457*7f296bb3SBarry Smith
458*7f296bb3SBarry Smith```
459*7f296bb3SBarry SmithKSPSetNormType(ksp, KSP_NORM_UNPRECONDITIONED);
460*7f296bb3SBarry Smith```
461*7f296bb3SBarry Smith
462*7f296bb3SBarry Smith`KSPCG` also supports using the natural norm induced by the symmetric positive-definite
463*7f296bb3SBarry Smithmatrix that defines the linear system with the options database command `-ksp_norm_type natural` or by calling the routine
464*7f296bb3SBarry Smith
465*7f296bb3SBarry Smith```
466*7f296bb3SBarry SmithKSPSetNormType(ksp, KSP_NORM_NATURAL);
467*7f296bb3SBarry Smith```
468*7f296bb3SBarry Smith
469*7f296bb3SBarry SmithConvergence (or divergence) is decided
470*7f296bb3SBarry Smithby three quantities: the decrease of the residual norm relative to the
471*7f296bb3SBarry Smithnorm of the right-hand side, `rtol`, the absolute size of the residual
472*7f296bb3SBarry Smithnorm, `atol`, and the relative increase in the residual, `dtol`.
473*7f296bb3SBarry SmithConvergence is detected at iteration $k$ if
474*7f296bb3SBarry Smith
475*7f296bb3SBarry Smith$$
476*7f296bb3SBarry Smith\| r_k \|_2 < {\rm max} ( \text{rtol} * \| b \|_2, \text{atol}),
477*7f296bb3SBarry Smith$$
478*7f296bb3SBarry Smith
479*7f296bb3SBarry Smithwhere $r_k = b - A x_k$. Divergence is detected if
480*7f296bb3SBarry Smith
481*7f296bb3SBarry Smith$$
482*7f296bb3SBarry Smith\| r_k \|_2 > \text{dtol} * \| b \|_2.
483*7f296bb3SBarry Smith$$
484*7f296bb3SBarry Smith
485*7f296bb3SBarry SmithThese parameters, as well as the maximum number of allowable iterations,
486*7f296bb3SBarry Smithcan be set with the routine
487*7f296bb3SBarry Smith
488*7f296bb3SBarry Smith```
489*7f296bb3SBarry SmithKSPSetTolerances(KSP ksp,PetscReal rtol,PetscReal atol,PetscReal dtol,PetscInt maxits);
490*7f296bb3SBarry Smith```
491*7f296bb3SBarry Smith
492*7f296bb3SBarry SmithThe user can retain the current value of any of these parameters by
493*7f296bb3SBarry Smithspecifying `PETSC_CURRENT` as the corresponding tolerance; the
494*7f296bb3SBarry Smithdefaults are `rtol=1e-5`, `atol=1e-50`, `dtol=1e5`, and
495*7f296bb3SBarry Smith`maxits=1e4`. Using `PETSC_DETERMINE` will set the parameters back to their
496*7f296bb3SBarry Smithinitial values when the object's type was set. These parameters can also be set from the options
497*7f296bb3SBarry Smithdatabase with the commands `-ksp_rtol` `<rtol>`, `-ksp_atol`
498*7f296bb3SBarry Smith`<atol>`, `-ksp_divtol` `<dtol>`, and `-ksp_max_it` `<its>`.
499*7f296bb3SBarry Smith
500*7f296bb3SBarry SmithIn addition to providing an interface to a simple convergence test,
501*7f296bb3SBarry Smith`KSP` allows the application programmer the flexibility to provide
502*7f296bb3SBarry Smithcustomized convergence-testing routines. The user can specify a
503*7f296bb3SBarry Smithcustomized routine with the command
504*7f296bb3SBarry Smith
505*7f296bb3SBarry Smith```
506*7f296bb3SBarry SmithKSPSetConvergenceTest(KSP ksp,PetscErrorCode (*test)(KSP ksp,PetscInt it,PetscReal rnorm, KSPConvergedReason *reason,void *ctx),void *ctx,PetscErrorCode (*destroy)(void *ctx));
507*7f296bb3SBarry Smith```
508*7f296bb3SBarry Smith
509*7f296bb3SBarry SmithThe final routine argument, `ctx`, is an optional context for private
510*7f296bb3SBarry Smithdata for the user-defined convergence routine, `test`. Other `test`
511*7f296bb3SBarry Smithroutine arguments are the iteration number, `it`, and the residual’s
512*7f296bb3SBarry Smithnorm, `rnorm`. The routine for detecting convergence,
513*7f296bb3SBarry Smith`test`, should set `reason` to positive for convergence, 0 for no
514*7f296bb3SBarry Smithconvergence, and negative for failure to converge. A full list of
515*7f296bb3SBarry Smithpossible values is given in the `KSPConvergedReason` manual page.
516*7f296bb3SBarry SmithYou can use `KSPGetConvergedReason()` after
517*7f296bb3SBarry Smith`KSPSolve()` to see why convergence/divergence was detected.
518*7f296bb3SBarry Smith
519*7f296bb3SBarry Smith(sec_kspmonitor)=
520*7f296bb3SBarry Smith
521*7f296bb3SBarry Smith### Convergence Monitoring
522*7f296bb3SBarry Smith
523*7f296bb3SBarry SmithBy default, the Krylov solvers, `KSPSolve()`, run silently without displaying
524*7f296bb3SBarry Smithinformation about the iterations. The user can indicate that the norms
525*7f296bb3SBarry Smithof the residuals should be displayed at each iteration by using `-ksp_monitor` with
526*7f296bb3SBarry Smiththe options database. To display the residual norms in a graphical
527*7f296bb3SBarry Smithwindow (running under X Windows), one should use
528*7f296bb3SBarry Smith`-ksp_monitor draw::draw_lg`. Application programmers can also
529*7f296bb3SBarry Smithprovide their own routines to perform the monitoring by using the
530*7f296bb3SBarry Smithcommand
531*7f296bb3SBarry Smith
532*7f296bb3SBarry Smith```
533*7f296bb3SBarry SmithKSPMonitorSet(KSP ksp, PetscErrorCode (*mon)(KSP ksp, PetscInt it, PetscReal rnorm, void *ctx), void *ctx, (PetscCtxDestroyFn *)mondestroy);
534*7f296bb3SBarry Smith```
535*7f296bb3SBarry Smith
536*7f296bb3SBarry SmithThe final routine argument, `ctx`, is an optional context for private
537*7f296bb3SBarry Smithdata for the user-defined monitoring routine, `mon`. Other `mon`
538*7f296bb3SBarry Smithroutine arguments are the iteration number (`it`) and the residual’s
539*7f296bb3SBarry Smithnorm (`rnorm`), as discussed above in {any}`sec_convergencetests`.
540*7f296bb3SBarry SmithA helpful routine within user-defined
541*7f296bb3SBarry Smithmonitors is `PetscObjectGetComm((PetscObject)ksp,MPI_Comm *comm)`,
542*7f296bb3SBarry Smithwhich returns in `comm` the MPI communicator for the `KSP` context.
543*7f296bb3SBarry SmithSee {any}`sec_writing` for more discussion of the use of
544*7f296bb3SBarry SmithMPI communicators within PETSc.
545*7f296bb3SBarry Smith
546*7f296bb3SBarry SmithMany monitoring routines are supplied with PETSc, including
547*7f296bb3SBarry Smith
548*7f296bb3SBarry Smith```
549*7f296bb3SBarry SmithKSPMonitorResidual(KSP,PetscInt,PetscReal, void *);
550*7f296bb3SBarry SmithKSPMonitorSingularValue(KSP,PetscInt,PetscReal,void *);
551*7f296bb3SBarry SmithKSPMonitorTrueResidual(KSP,PetscInt,PetscReal, void *);
552*7f296bb3SBarry Smith```
553*7f296bb3SBarry Smith
554*7f296bb3SBarry SmithThe default monitor simply prints an estimate of a norm of
555*7f296bb3SBarry Smiththe residual at each iteration. The routine
556*7f296bb3SBarry Smith`KSPMonitorSingularValue()` is appropriate only for use with the
557*7f296bb3SBarry Smithconjugate gradient method or GMRES, since it prints estimates of the
558*7f296bb3SBarry Smithextreme singular values of the preconditioned operator at each
559*7f296bb3SBarry Smithiteration computed via the Lanczos or Arnoldi algorithms.
560*7f296bb3SBarry Smith
561*7f296bb3SBarry SmithSince `KSPMonitorTrueResidual()` prints the true
562*7f296bb3SBarry Smithresidual at each iteration by actually computing the residual using the
563*7f296bb3SBarry Smithformula $r = b - Ax$, the routine is slow and should be used only
564*7f296bb3SBarry Smithfor testing or convergence studies, not for timing. These `KSPSolve()` monitors may
565*7f296bb3SBarry Smithbe accessed with the command line options `-ksp_monitor`,
566*7f296bb3SBarry Smith`-ksp_monitor_singular_value`, and `-ksp_monitor_true_residual`.
567*7f296bb3SBarry Smith
568*7f296bb3SBarry SmithTo employ the default graphical monitor, one should use the command
569*7f296bb3SBarry Smith`-ksp_monitor draw::draw_lg`.
570*7f296bb3SBarry Smith
571*7f296bb3SBarry SmithOne can cancel hardwired monitoring routines for KSP at runtime with
572*7f296bb3SBarry Smith`-ksp_monitor_cancel`.
573*7f296bb3SBarry Smith
574*7f296bb3SBarry Smith### Understanding the Operator’s Spectrum
575*7f296bb3SBarry Smith
576*7f296bb3SBarry SmithSince the convergence of Krylov subspace methods depends strongly on the
577*7f296bb3SBarry Smithspectrum (eigenvalues) of the preconditioned operator, PETSc has
578*7f296bb3SBarry Smithspecific routines for eigenvalue approximation via the Arnoldi or
579*7f296bb3SBarry SmithLanczos iteration. First, before the linear solve one must call
580*7f296bb3SBarry Smith
581*7f296bb3SBarry Smith```
582*7f296bb3SBarry SmithKSPSetComputeEigenvalues(ksp,PETSC_TRUE);
583*7f296bb3SBarry Smith```
584*7f296bb3SBarry Smith
585*7f296bb3SBarry SmithThen after the `KSP` solve one calls
586*7f296bb3SBarry Smith
587*7f296bb3SBarry Smith```
588*7f296bb3SBarry SmithKSPComputeEigenvalues(KSP ksp,PetscInt n,PetscReal *realpart,PetscReal *complexpart,PetscInt *neig);
589*7f296bb3SBarry Smith```
590*7f296bb3SBarry Smith
591*7f296bb3SBarry SmithHere, `n` is the size of the two arrays and the eigenvalues are
592*7f296bb3SBarry Smithinserted into those two arrays. `neig` is the number of eigenvalues
593*7f296bb3SBarry Smithcomputed; this number depends on the size of the Krylov space generated
594*7f296bb3SBarry Smithduring the linear system solution, for GMRES it is never larger than the
595*7f296bb3SBarry Smith`restart` parameter. There is an additional routine
596*7f296bb3SBarry Smith
597*7f296bb3SBarry Smith```
598*7f296bb3SBarry SmithKSPComputeEigenvaluesExplicitly(KSP ksp, PetscInt n,PetscReal *realpart,PetscReal *complexpart);
599*7f296bb3SBarry Smith```
600*7f296bb3SBarry Smith
601*7f296bb3SBarry Smiththat is useful only for very small problems. It explicitly computes the
602*7f296bb3SBarry Smithfull representation of the preconditioned operator and calls LAPACK to
603*7f296bb3SBarry Smithcompute its eigenvalues. It should be only used for matrices of size up
604*7f296bb3SBarry Smithto a couple hundred. The `PetscDrawSP*()` routines are very useful for
605*7f296bb3SBarry Smithdrawing scatter plots of the eigenvalues.
606*7f296bb3SBarry Smith
607*7f296bb3SBarry SmithThe eigenvalues may also be computed and displayed graphically with the
608*7f296bb3SBarry Smithoptions data base commands `-ksp_view_eigenvalues draw` and
609*7f296bb3SBarry Smith`-ksp_view_eigenvalues_explicit draw`. Or they can be dumped to the
610*7f296bb3SBarry Smithscreen in ASCII text via `-ksp_view_eigenvalues` and
611*7f296bb3SBarry Smith`-ksp_view_eigenvalues_explicit`.
612*7f296bb3SBarry Smith
613*7f296bb3SBarry Smith(sec_flexibleksp)=
614*7f296bb3SBarry Smith
615*7f296bb3SBarry Smith### Flexible Krylov Methods
616*7f296bb3SBarry Smith
617*7f296bb3SBarry SmithStandard Krylov methods require that the preconditioner be a linear operator, thus, for example, a standard `KSP` method
618*7f296bb3SBarry Smithcannot use a `KSP` in its preconditioner, as is common in the Block-Jacobi method `PCBJACOBI`, for example.
619*7f296bb3SBarry SmithFlexible Krylov methods are a subset of methods that allow (with modest additional requirements
620*7f296bb3SBarry Smithon memory) the preconditioner to be nonlinear. For example, they can be used with the `PCKSP` preconditioner.
621*7f296bb3SBarry SmithThe flexible `KSP` methods have the label "Flexible" in {any}`tab-kspdefaults`.
622*7f296bb3SBarry Smith
623*7f296bb3SBarry SmithOne can use `KSPMonitorDynamicTolerance()` to control the tolerances used by inner `KSP` solvers in `PCKSP`, `PCBJACOBI`, and `PCDEFLATION`.
624*7f296bb3SBarry Smith
625*7f296bb3SBarry SmithIn addition to supporting `PCKSP`, the flexible methods support `KSP*SetModifyPC()`, for example, `KSPFGMRESSetModifyPC()`, these functions
626*7f296bb3SBarry Smithallow the user to provide a callback function that changes the preconditioner at each Krylov iteration. Its calling sequence is as follows.
627*7f296bb3SBarry Smith
628*7f296bb3SBarry Smith```
629*7f296bb3SBarry SmithPetscErrorCode f(KSP ksp,PetscInt total_its,PetscInt its_since_restart,PetscReal res_norm,void *ctx);
630*7f296bb3SBarry Smith```
631*7f296bb3SBarry Smith
632*7f296bb3SBarry Smith(sec_pipelineksp)=
633*7f296bb3SBarry Smith
634*7f296bb3SBarry Smith### Pipelined Krylov Methods
635*7f296bb3SBarry Smith
636*7f296bb3SBarry SmithStandard Krylov methods have one or more global reductions resulting from the computations of inner products or norms in each iteration.
637*7f296bb3SBarry SmithThese reductions need to block until all MPI processes have received the results. For a large number of MPI processes (this number is machine dependent
638*7f296bb3SBarry Smithbut can be above 10,000 processes) this synchronization is very time consuming and can significantly slow the computation. Pipelined Krylov
639*7f296bb3SBarry Smithmethods overlap the reduction operations with local computations (generally the application of the matrix-vector products and precondtiioners)
640*7f296bb3SBarry Smiththus effectively "hiding" the time of the reductions. In addition, they may reduce the number of global synchronizations by rearranging the
641*7f296bb3SBarry Smithcomputations in a way that some of them can be collapsed, e.g., two or more calls to `MPI_Allreduce()` may be combined into one call.
642*7f296bb3SBarry SmithThe pipeline `KSP` methods have the label "Pipeline" in {any}`tab-kspdefaults`.
643*7f296bb3SBarry Smith
644*7f296bb3SBarry SmithSpecial configuration of MPI may be necessary for reductions to make asynchronous progress, which is important for
645*7f296bb3SBarry Smithperformance of pipelined methods. See {any}`doc_faq_pipelined` for details.
646*7f296bb3SBarry Smith
647*7f296bb3SBarry Smith### Other KSP Options
648*7f296bb3SBarry Smith
649*7f296bb3SBarry SmithTo obtain the solution vector and right-hand side from a `KSP`
650*7f296bb3SBarry Smithcontext, one uses
651*7f296bb3SBarry Smith
652*7f296bb3SBarry Smith```
653*7f296bb3SBarry SmithKSPGetSolution(KSP ksp,Vec *x);
654*7f296bb3SBarry SmithKSPGetRhs(KSP ksp,Vec *rhs);
655*7f296bb3SBarry Smith```
656*7f296bb3SBarry Smith
657*7f296bb3SBarry SmithDuring the iterative process the solution may not yet have been
658*7f296bb3SBarry Smithcalculated or it may be stored in a different location. To access the
659*7f296bb3SBarry Smithapproximate solution during the iterative process, one uses the command
660*7f296bb3SBarry Smith
661*7f296bb3SBarry Smith```
662*7f296bb3SBarry SmithKSPBuildSolution(KSP ksp,Vec w,Vec *v);
663*7f296bb3SBarry Smith```
664*7f296bb3SBarry Smith
665*7f296bb3SBarry Smithwhere the solution is returned in `v`. The user can optionally provide
666*7f296bb3SBarry Smitha vector in `w` as the location to store the vector; however, if `w`
667*7f296bb3SBarry Smithis `NULL`, space allocated by PETSc in the `KSP` context is used.
668*7f296bb3SBarry SmithOne should not destroy this vector. For certain `KSP` methods (e.g.,
669*7f296bb3SBarry SmithGMRES), the construction of the solution is expensive, while for many
670*7f296bb3SBarry Smithothers it doesn’t even require a vector copy.
671*7f296bb3SBarry Smith
672*7f296bb3SBarry SmithAccess to the residual is done in a similar way with the command
673*7f296bb3SBarry Smith
674*7f296bb3SBarry Smith```
675*7f296bb3SBarry SmithKSPBuildResidual(KSP ksp,Vec t,Vec w,Vec *v);
676*7f296bb3SBarry Smith```
677*7f296bb3SBarry Smith
678*7f296bb3SBarry SmithAgain, for GMRES and certain other methods this is an expensive
679*7f296bb3SBarry Smithoperation.
680*7f296bb3SBarry Smith
681*7f296bb3SBarry Smith(sec_pc)=
682*7f296bb3SBarry Smith
683*7f296bb3SBarry Smith## Preconditioners
684*7f296bb3SBarry Smith
685*7f296bb3SBarry SmithAs discussed in {any}`sec_ksppc`, Krylov subspace methods
686*7f296bb3SBarry Smithare typically used in conjunction with a preconditioner. To employ a
687*7f296bb3SBarry Smithparticular preconditioning method, the user can either select it from
688*7f296bb3SBarry Smiththe options database using input of the form `-pc_type <methodname>`
689*7f296bb3SBarry Smithor set the method with the command
690*7f296bb3SBarry Smith
691*7f296bb3SBarry Smith```
692*7f296bb3SBarry SmithPCSetType(PC pc,PCType method);
693*7f296bb3SBarry Smith```
694*7f296bb3SBarry Smith
695*7f296bb3SBarry SmithIn {any}`tab-pcdefaults` we summarize the basic
696*7f296bb3SBarry Smithpreconditioning methods supported in PETSc. See the `PCType` manual
697*7f296bb3SBarry Smithpage for a complete list.
698*7f296bb3SBarry Smith
699*7f296bb3SBarry SmithThe `PCSHELL` preconditioner allows users to provide their own
700*7f296bb3SBarry Smithspecific, application-provided custom preconditioner.
701*7f296bb3SBarry Smith
702*7f296bb3SBarry SmithThe direct
703*7f296bb3SBarry Smithpreconditioner, `PCLU` , is, in fact, a direct solver for the linear
704*7f296bb3SBarry Smithsystem that uses LU factorization. `PCLU` is included as a
705*7f296bb3SBarry Smithpreconditioner so that PETSc has a consistent interface among direct and
706*7f296bb3SBarry Smithiterative linear solvers.
707*7f296bb3SBarry Smith
708*7f296bb3SBarry SmithPETSc provides several domain decomposition methods/preconditioners including
709*7f296bb3SBarry Smith`PCASM`, `PCGASM`, `PCBDDC`, and `PCHPDDM`. In addition PETSc provides
710*7f296bb3SBarry Smithmultiple multigrid solvers/preconditioners including `PCMG`, `PCGAMG`, `PCHYPRE`,
711*7f296bb3SBarry Smithand `PCML`. See further discussion below.
712*7f296bb3SBarry Smith
713*7f296bb3SBarry Smith```{eval-rst}
714*7f296bb3SBarry Smith.. list-table:: PETSc Preconditioners (partial list)
715*7f296bb3SBarry Smith   :name: tab-pcdefaults
716*7f296bb3SBarry Smith   :header-rows: 1
717*7f296bb3SBarry Smith
718*7f296bb3SBarry Smith   * - Method
719*7f296bb3SBarry Smith     - PCType
720*7f296bb3SBarry Smith     - Options Database
721*7f296bb3SBarry Smith   * - Jacobi
722*7f296bb3SBarry Smith     - ``PCJACOBI``
723*7f296bb3SBarry Smith     - ``jacobi``
724*7f296bb3SBarry Smith   * - Block Jacobi
725*7f296bb3SBarry Smith     - ``PCBJACOBI``
726*7f296bb3SBarry Smith     - ``bjacobi``
727*7f296bb3SBarry Smith   * - SOR (and SSOR)
728*7f296bb3SBarry Smith     - ``PCSOR``
729*7f296bb3SBarry Smith     - ``sor``
730*7f296bb3SBarry Smith   * - SOR with Eisenstat trick
731*7f296bb3SBarry Smith     - ``PCEISENSTAT``
732*7f296bb3SBarry Smith     - ``eisenstat``
733*7f296bb3SBarry Smith   * - Incomplete Cholesky
734*7f296bb3SBarry Smith     - ``PCICC``
735*7f296bb3SBarry Smith     - ``icc``
736*7f296bb3SBarry Smith   * - Incomplete LU
737*7f296bb3SBarry Smith     - ``PCILU``
738*7f296bb3SBarry Smith     - ``ilu``
739*7f296bb3SBarry Smith   * - Additive Schwarz
740*7f296bb3SBarry Smith     - ``PCASM``
741*7f296bb3SBarry Smith     - ``asm``
742*7f296bb3SBarry Smith   * - Generalized Additive Schwarz
743*7f296bb3SBarry Smith     - ``PCGASM``
744*7f296bb3SBarry Smith     - ``gasm``
745*7f296bb3SBarry Smith   * - Algebraic Multigrid
746*7f296bb3SBarry Smith     - ``PCGAMG``
747*7f296bb3SBarry Smith     - ``gamg``
748*7f296bb3SBarry Smith   * - Balancing Domain Decomposition by Constraints
749*7f296bb3SBarry Smith     - ``PCBDDC``
750*7f296bb3SBarry Smith     - ``bddc``
751*7f296bb3SBarry Smith   * - Linear solver
752*7f296bb3SBarry Smith     - ``PCKSP``
753*7f296bb3SBarry Smith     - ``ksp``
754*7f296bb3SBarry Smith   * - Combination of preconditioners
755*7f296bb3SBarry Smith     - ``PCCOMPOSITE``
756*7f296bb3SBarry Smith     - ``composite``
757*7f296bb3SBarry Smith   * - LU
758*7f296bb3SBarry Smith     - ``PCLU``
759*7f296bb3SBarry Smith     - ``lu``
760*7f296bb3SBarry Smith   * - Cholesky
761*7f296bb3SBarry Smith     - ``PCCHOLESKY``
762*7f296bb3SBarry Smith     - ``cholesky``
763*7f296bb3SBarry Smith   * - No preconditioning
764*7f296bb3SBarry Smith     - ``PCNONE``
765*7f296bb3SBarry Smith     - ``none``
766*7f296bb3SBarry Smith   * - Shell for user-defined ``PC``
767*7f296bb3SBarry Smith     - ``PCSHELL``
768*7f296bb3SBarry Smith     - ``shell``
769*7f296bb3SBarry Smith```
770*7f296bb3SBarry Smith
771*7f296bb3SBarry SmithEach preconditioner may have associated with it a set of options, which
772*7f296bb3SBarry Smithcan be set with routines and options database commands provided for this
773*7f296bb3SBarry Smithpurpose. Such routine names and commands are all of the form
774*7f296bb3SBarry Smith`PC<TYPE><Option>` and `-pc_<type>_<option> [value]`. A complete
775*7f296bb3SBarry Smithlist can be found by consulting the `PCType` manual page; we discuss
776*7f296bb3SBarry Smithjust a few in the sections below.
777*7f296bb3SBarry Smith
778*7f296bb3SBarry Smith(sec_ilu_icc)=
779*7f296bb3SBarry Smith
780*7f296bb3SBarry Smith### ILU and ICC Preconditioners
781*7f296bb3SBarry Smith
782*7f296bb3SBarry SmithSome of the options for ILU preconditioner are
783*7f296bb3SBarry Smith
784*7f296bb3SBarry Smith```
785*7f296bb3SBarry SmithPCFactorSetLevels(PC pc,PetscInt levels);
786*7f296bb3SBarry SmithPCFactorSetReuseOrdering(PC pc,PetscBool flag);
787*7f296bb3SBarry SmithPCFactorSetDropTolerance(PC pc,PetscReal dt,PetscReal dtcol,PetscInt dtcount);
788*7f296bb3SBarry SmithPCFactorSetReuseFill(PC pc,PetscBool flag);
789*7f296bb3SBarry SmithPCFactorSetUseInPlace(PC pc,PetscBool flg);
790*7f296bb3SBarry SmithPCFactorSetAllowDiagonalFill(PC pc,PetscBool flg);
791*7f296bb3SBarry Smith```
792*7f296bb3SBarry Smith
793*7f296bb3SBarry SmithWhen repeatedly solving linear systems with the same `KSP` context,
794*7f296bb3SBarry Smithone can reuse some information computed during the first linear solve.
795*7f296bb3SBarry SmithIn particular, `PCFactorSetReuseOrdering()` causes the ordering (for
796*7f296bb3SBarry Smithexample, set with `-pc_factor_mat_ordering_type` `order`) computed
797*7f296bb3SBarry Smithin the first factorization to be reused for later factorizations.
798*7f296bb3SBarry Smith`PCFactorSetUseInPlace()` is often used with `PCASM` or
799*7f296bb3SBarry Smith`PCBJACOBI` when zero fill is used, since it reuses the matrix space
800*7f296bb3SBarry Smithto store the incomplete factorization it saves memory and copying time.
801*7f296bb3SBarry SmithNote that in-place factorization is not appropriate with any ordering
802*7f296bb3SBarry Smithbesides natural and cannot be used with the drop tolerance
803*7f296bb3SBarry Smithfactorization. These options may be set in the database with
804*7f296bb3SBarry Smith
805*7f296bb3SBarry Smith- `-pc_factor_levels <levels>`
806*7f296bb3SBarry Smith- `-pc_factor_reuse_ordering`
807*7f296bb3SBarry Smith- `-pc_factor_reuse_fill`
808*7f296bb3SBarry Smith- `-pc_factor_in_place`
809*7f296bb3SBarry Smith- `-pc_factor_nonzeros_along_diagonal`
810*7f296bb3SBarry Smith- `-pc_factor_diagonal_fill`
811*7f296bb3SBarry Smith
812*7f296bb3SBarry SmithSee {any}`sec_symbolfactor` for information on
813*7f296bb3SBarry Smithpreallocation of memory for anticipated fill during factorization. By
814*7f296bb3SBarry Smithalleviating the considerable overhead for dynamic memory allocation,
815*7f296bb3SBarry Smithsuch tuning can significantly enhance performance.
816*7f296bb3SBarry Smith
817*7f296bb3SBarry SmithPETSc supports incomplete factorization preconditioners
818*7f296bb3SBarry Smithfor several matrix types for sequential matrices (for example
819*7f296bb3SBarry Smith`MATSEQAIJ`, `MATSEQBAIJ`, and `MATSEQSBAIJ`).
820*7f296bb3SBarry Smith
821*7f296bb3SBarry Smith### SOR and SSOR Preconditioners
822*7f296bb3SBarry Smith
823*7f296bb3SBarry SmithPETSc provides only a sequential SOR preconditioner; it can only be
824*7f296bb3SBarry Smithused with sequential matrices or as the subblock preconditioner when
825*7f296bb3SBarry Smithusing block Jacobi or ASM preconditioning (see below).
826*7f296bb3SBarry Smith
827*7f296bb3SBarry SmithThe options for SOR preconditioning with `PCSOR` are
828*7f296bb3SBarry Smith
829*7f296bb3SBarry Smith```
830*7f296bb3SBarry SmithPCSORSetOmega(PC pc,PetscReal omega);
831*7f296bb3SBarry SmithPCSORSetIterations(PC pc,PetscInt its,PetscInt lits);
832*7f296bb3SBarry SmithPCSORSetSymmetric(PC pc,MatSORType type);
833*7f296bb3SBarry Smith```
834*7f296bb3SBarry Smith
835*7f296bb3SBarry SmithThe first of these commands sets the relaxation factor for successive
836*7f296bb3SBarry Smithover (under) relaxation. The second command sets the number of inner
837*7f296bb3SBarry Smithiterations `its` and local iterations `lits` (the number of
838*7f296bb3SBarry Smithsmoothing sweeps on a process before doing a ghost point update from the
839*7f296bb3SBarry Smithother processes) to use between steps of the Krylov space method. The
840*7f296bb3SBarry Smithtotal number of SOR sweeps is given by `its*lits`. The third command
841*7f296bb3SBarry Smithsets the kind of SOR sweep, where the argument `type` can be one of
842*7f296bb3SBarry Smith`SOR_FORWARD_SWEEP`, `SOR_BACKWARD_SWEEP` or
843*7f296bb3SBarry Smith`SOR_SYMMETRIC_SWEEP`, the default being `SOR_FORWARD_SWEEP`.
844*7f296bb3SBarry SmithSetting the type to be `SOR_SYMMETRIC_SWEEP` produces the SSOR method.
845*7f296bb3SBarry SmithIn addition, each process can locally and independently perform the
846*7f296bb3SBarry Smithspecified variant of SOR with the types `SOR_LOCAL_FORWARD_SWEEP`,
847*7f296bb3SBarry Smith`SOR_LOCAL_BACKWARD_SWEEP`, and `SOR_LOCAL_SYMMETRIC_SWEEP`. These
848*7f296bb3SBarry Smithvariants can also be set with the options `-pc_sor_omega <omega>`,
849*7f296bb3SBarry Smith`-pc_sor_its <its>`, `-pc_sor_lits <lits>`, `-pc_sor_backward`,
850*7f296bb3SBarry Smith`-pc_sor_symmetric`, `-pc_sor_local_forward`,
851*7f296bb3SBarry Smith`-pc_sor_local_backward`, and `-pc_sor_local_symmetric`.
852*7f296bb3SBarry Smith
853*7f296bb3SBarry SmithThe Eisenstat trick {cite}`eisenstat81` for SSOR
854*7f296bb3SBarry Smithpreconditioning can be employed with the method `PCEISENSTAT`
855*7f296bb3SBarry Smith(`-pc_type` `eisenstat`). By using both left and right
856*7f296bb3SBarry Smithpreconditioning of the linear system, this variant of SSOR requires
857*7f296bb3SBarry Smithabout half of the floating-point operations for conventional SSOR. The
858*7f296bb3SBarry Smithoption `-pc_eisenstat_no_diagonal_scaling` (or the routine
859*7f296bb3SBarry Smith`PCEisenstatSetNoDiagonalScaling()`) turns off diagonal scaling in
860*7f296bb3SBarry Smithconjunction with Eisenstat SSOR method, while the option
861*7f296bb3SBarry Smith`-pc_eisenstat_omega <omega>` (or the routine
862*7f296bb3SBarry Smith`PCEisenstatSetOmega(PC pc,PetscReal omega)`) sets the SSOR relaxation
863*7f296bb3SBarry Smithcoefficient, `omega`, as discussed above.
864*7f296bb3SBarry Smith
865*7f296bb3SBarry Smith(sec_factorization)=
866*7f296bb3SBarry Smith
867*7f296bb3SBarry Smith### LU Factorization
868*7f296bb3SBarry Smith
869*7f296bb3SBarry SmithThe LU preconditioner provides several options. The first, given by the
870*7f296bb3SBarry Smithcommand
871*7f296bb3SBarry Smith
872*7f296bb3SBarry Smith```
873*7f296bb3SBarry SmithPCFactorSetUseInPlace(PC pc,PetscBool flg);
874*7f296bb3SBarry Smith```
875*7f296bb3SBarry Smith
876*7f296bb3SBarry Smithcauses the factorization to be performed in-place and hence destroys the
877*7f296bb3SBarry Smithoriginal matrix. The options database variant of this command is
878*7f296bb3SBarry Smith`-pc_factor_in_place`. Another direct preconditioner option is
879*7f296bb3SBarry Smithselecting the ordering of equations with the command
880*7f296bb3SBarry Smith`-pc_factor_mat_ordering_type <ordering>`. The possible orderings are
881*7f296bb3SBarry Smith
882*7f296bb3SBarry Smith- `MATORDERINGNATURAL` - Natural
883*7f296bb3SBarry Smith- `MATORDERINGND` - Nested Dissection
884*7f296bb3SBarry Smith- `MATORDERING1WD` - One-way Dissection
885*7f296bb3SBarry Smith- `MATORDERINGRCM` - Reverse Cuthill-McKee
886*7f296bb3SBarry Smith- `MATORDERINGQMD` - Quotient Minimum Degree
887*7f296bb3SBarry Smith
888*7f296bb3SBarry SmithThese orderings can also be set through the options database by
889*7f296bb3SBarry Smithspecifying one of the following: `-pc_factor_mat_ordering_type`
890*7f296bb3SBarry Smith`natural`, or `nd`, or `1wd`, or `rcm`, or `qmd`. In addition,
891*7f296bb3SBarry Smithsee `MatGetOrdering()`, discussed in {any}`sec_matfactor`.
892*7f296bb3SBarry Smith
893*7f296bb3SBarry SmithThe sparse LU factorization provided in PETSc does not perform pivoting
894*7f296bb3SBarry Smithfor numerical stability (since they are designed to preserve nonzero
895*7f296bb3SBarry Smithstructure), and thus occasionally an LU factorization will fail with a
896*7f296bb3SBarry Smithzero pivot when, in fact, the matrix is non-singular. The option
897*7f296bb3SBarry Smith`-pc_factor_nonzeros_along_diagonal <tol>` will often help eliminate
898*7f296bb3SBarry Smiththe zero pivot, by preprocessing the column ordering to remove small
899*7f296bb3SBarry Smithvalues from the diagonal. Here, `tol` is an optional tolerance to
900*7f296bb3SBarry Smithdecide if a value is nonzero; by default it is `1.e-10`.
901*7f296bb3SBarry Smith
902*7f296bb3SBarry SmithIn addition, {any}`sec_symbolfactor` provides information
903*7f296bb3SBarry Smithon preallocation of memory for anticipated fill during factorization.
904*7f296bb3SBarry SmithSuch tuning can significantly enhance performance, since it eliminates
905*7f296bb3SBarry Smiththe considerable overhead for dynamic memory allocation.
906*7f296bb3SBarry Smith
907*7f296bb3SBarry Smith(sec_bjacobi)=
908*7f296bb3SBarry Smith
909*7f296bb3SBarry Smith### Block Jacobi and Overlapping Additive Schwarz Preconditioners
910*7f296bb3SBarry Smith
911*7f296bb3SBarry SmithThe block Jacobi and overlapping additive Schwarz (domain decomposition) methods in PETSc are
912*7f296bb3SBarry Smithsupported in parallel; however, only the uniprocess version of the block
913*7f296bb3SBarry SmithGauss-Seidel method is available. By default, the PETSc
914*7f296bb3SBarry Smithimplementations of these methods employ ILU(0) factorization on each
915*7f296bb3SBarry Smithindividual block (that is, the default solver on each subblock is
916*7f296bb3SBarry Smith`PCType=PCILU`, `KSPType=KSPPREONLY` (or equivalently `KSPType=KSPNONE`); the user can set alternative
917*7f296bb3SBarry Smithlinear solvers via the options `-sub_ksp_type` and `-sub_pc_type`.
918*7f296bb3SBarry SmithIn fact, all of the `KSP` and `PC` options can be applied to the
919*7f296bb3SBarry Smithsubproblems by inserting the prefix `-sub_` at the beginning of the
920*7f296bb3SBarry Smithoption name. These options database commands set the particular options
921*7f296bb3SBarry Smithfor *all* of the blocks within the global problem. In addition, the
922*7f296bb3SBarry Smithroutines
923*7f296bb3SBarry Smith
924*7f296bb3SBarry Smith```
925*7f296bb3SBarry SmithPCBJacobiGetSubKSP(PC pc,PetscInt *n_local,PetscInt *first_local,KSP **subksp);
926*7f296bb3SBarry SmithPCASMGetSubKSP(PC pc,PetscInt *n_local,PetscInt *first_local,KSP **subksp);
927*7f296bb3SBarry Smith```
928*7f296bb3SBarry Smith
929*7f296bb3SBarry Smithextract the `KSP` context for each local block. The argument
930*7f296bb3SBarry Smith`n_local` is the number of blocks on the calling process, and
931*7f296bb3SBarry Smith`first_local` indicates the global number of the first block on the
932*7f296bb3SBarry Smithprocess. The blocks are numbered successively by processes from zero
933*7f296bb3SBarry Smiththrough $b_g-1$, where $b_g$ is the number of global blocks.
934*7f296bb3SBarry SmithThe array of `KSP` contexts for the local blocks is given by
935*7f296bb3SBarry Smith`subksp`. This mechanism enables the user to set different solvers for
936*7f296bb3SBarry Smiththe various blocks. To set the appropriate data structures, the user
937*7f296bb3SBarry Smith*must* explicitly call `KSPSetUp()` before calling
938*7f296bb3SBarry Smith`PCBJacobiGetSubKSP()` or `PCASMGetSubKSP(`). For further details,
939*7f296bb3SBarry Smithsee
940*7f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex7.c.html">KSP Tutorial ex7</a>
941*7f296bb3SBarry Smithor
942*7f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex8.c.html">KSP Tutorial ex8</a>.
943*7f296bb3SBarry Smith
944*7f296bb3SBarry SmithThe block Jacobi, block Gauss-Seidel, and additive Schwarz
945*7f296bb3SBarry Smithpreconditioners allow the user to set the number of blocks into which
946*7f296bb3SBarry Smiththe problem is divided. The options database commands to set this value
947*7f296bb3SBarry Smithare `-pc_bjacobi_blocks` `n` and `-pc_bgs_blocks` `n`, and,
948*7f296bb3SBarry Smithwithin a program, the corresponding routines are
949*7f296bb3SBarry Smith
950*7f296bb3SBarry Smith```
951*7f296bb3SBarry SmithPCBJacobiSetTotalBlocks(PC pc,PetscInt blocks,PetscInt *size);
952*7f296bb3SBarry SmithPCASMSetTotalSubdomains(PC pc,PetscInt n,IS *is,IS *islocal);
953*7f296bb3SBarry SmithPCASMSetType(PC pc,PCASMType type);
954*7f296bb3SBarry Smith```
955*7f296bb3SBarry Smith
956*7f296bb3SBarry SmithThe optional argument `size` is an array indicating the size of each
957*7f296bb3SBarry Smithblock. Currently, for certain parallel matrix formats, only a single
958*7f296bb3SBarry Smithblock per process is supported. However, the `MATMPIAIJ` and
959*7f296bb3SBarry Smith`MATMPIBAIJ` formats support the use of general blocks as long as no
960*7f296bb3SBarry Smithblocks are shared among processes. The `is` argument contains the
961*7f296bb3SBarry Smithindex sets that define the subdomains.
962*7f296bb3SBarry Smith
963*7f296bb3SBarry SmithThe object `PCASMType` is one of `PC_ASM_BASIC`,
964*7f296bb3SBarry Smith`PC_ASM_INTERPOLATE`, `PC_ASM_RESTRICT`, or `PC_ASM_NONE` and may
965*7f296bb3SBarry Smithalso be set with the options database `-pc_asm_type` `[basic`,
966*7f296bb3SBarry Smith`interpolate`, `restrict`, `none]`. The type `PC_ASM_BASIC` (or
967*7f296bb3SBarry Smith`-pc_asm_type` `basic`) corresponds to the standard additive Schwarz
968*7f296bb3SBarry Smithmethod that uses the full restriction and interpolation operators. The
969*7f296bb3SBarry Smithtype `PC_ASM_RESTRICT` (or `-pc_asm_type` `restrict`) uses a full
970*7f296bb3SBarry Smithrestriction operator, but during the interpolation process ignores the
971*7f296bb3SBarry Smithoff-process values. Similarly, `PC_ASM_INTERPOLATE` (or
972*7f296bb3SBarry Smith`-pc_asm_type` `interpolate`) uses a limited restriction process in
973*7f296bb3SBarry Smithconjunction with a full interpolation, while `PC_ASM_NONE` (or
974*7f296bb3SBarry Smith`-pc_asm_type` `none`) ignores off-process values for both
975*7f296bb3SBarry Smithrestriction and interpolation. The ASM types with limited restriction or
976*7f296bb3SBarry Smithinterpolation were suggested by Xiao-Chuan Cai and Marcus Sarkis
977*7f296bb3SBarry Smith{cite}`cs99`. `PC_ASM_RESTRICT` is the PETSc default, as
978*7f296bb3SBarry Smithit saves substantial communication and for many problems has the added
979*7f296bb3SBarry Smithbenefit of requiring fewer iterations for convergence than the standard
980*7f296bb3SBarry Smithadditive Schwarz method.
981*7f296bb3SBarry Smith
982*7f296bb3SBarry SmithThe user can also set the number of blocks and sizes on a per-process
983*7f296bb3SBarry Smithbasis with the commands
984*7f296bb3SBarry Smith
985*7f296bb3SBarry Smith```
986*7f296bb3SBarry SmithPCBJacobiSetLocalBlocks(PC pc,PetscInt blocks,PetscInt *size);
987*7f296bb3SBarry SmithPCASMSetLocalSubdomains(PC pc,PetscInt N,IS *is,IS *islocal);
988*7f296bb3SBarry Smith```
989*7f296bb3SBarry Smith
990*7f296bb3SBarry SmithFor the ASM preconditioner one can use the following command to set the
991*7f296bb3SBarry Smithoverlap to compute in constructing the subdomains.
992*7f296bb3SBarry Smith
993*7f296bb3SBarry Smith```
994*7f296bb3SBarry SmithPCASMSetOverlap(PC pc,PetscInt overlap);
995*7f296bb3SBarry Smith```
996*7f296bb3SBarry Smith
997*7f296bb3SBarry SmithThe overlap defaults to 1, so if one desires that no additional overlap
998*7f296bb3SBarry Smithbe computed beyond what may have been set with a call to
999*7f296bb3SBarry Smith`PCASMSetTotalSubdomains()` or `PCASMSetLocalSubdomains()`, then
1000*7f296bb3SBarry Smith`overlap` must be set to be 0. In particular, if one does *not*
1001*7f296bb3SBarry Smithexplicitly set the subdomains in an application code, then all overlap
1002*7f296bb3SBarry Smithwould be computed internally by PETSc, and using an overlap of 0 would
1003*7f296bb3SBarry Smithresult in an ASM variant that is equivalent to the block Jacobi
1004*7f296bb3SBarry Smithpreconditioner. Note that one can define initial index sets `is` with
1005*7f296bb3SBarry Smith*any* overlap via `PCASMSetTotalSubdomains()` or
1006*7f296bb3SBarry Smith`PCASMSetLocalSubdomains()`; the routine `PCASMSetOverlap()` merely
1007*7f296bb3SBarry Smithallows PETSc to extend that overlap further if desired.
1008*7f296bb3SBarry Smith
1009*7f296bb3SBarry Smith`PCGASM` is a generalization of `PCASM` that allows
1010*7f296bb3SBarry Smiththe user to specify subdomains that span multiple MPI processes. This can be
1011*7f296bb3SBarry Smithuseful for problems where small subdomains result in poor convergence.
1012*7f296bb3SBarry SmithTo be effective, the multi-processor subproblems must be solved using a
1013*7f296bb3SBarry Smithsufficiently strong subsolver, such as `PCLU`, for which `SuperLU_DIST` or a
1014*7f296bb3SBarry Smithsimilar parallel direct solver could be used; other choices may include
1015*7f296bb3SBarry Smitha multigrid solver on the subdomains.
1016*7f296bb3SBarry Smith
1017*7f296bb3SBarry SmithThe interface for `PCGASM` is similar to that of `PCASM`. In
1018*7f296bb3SBarry Smithparticular, `PCGASMType` is one of `PC_GASM_BASIC`,
1019*7f296bb3SBarry Smith`PC_GASM_INTERPOLATE`, `PC_GASM_RESTRICT`, `PC_GASM_NONE`. These
1020*7f296bb3SBarry Smithoptions have the same meaning as with `PCASM` and may also be set with
1021*7f296bb3SBarry Smiththe options database `-pc_gasm_type` `[basic`, `interpolate`,
1022*7f296bb3SBarry Smith`restrict`, `none]`.
1023*7f296bb3SBarry Smith
1024*7f296bb3SBarry SmithUnlike `PCASM`, however, `PCGASM` allows the user to define
1025*7f296bb3SBarry Smithsubdomains that span multiple MPI processes. The simplest way to do this is
1026*7f296bb3SBarry Smithusing a call to `PCGASMSetTotalSubdomains(PC pc,PetscInt N)` with
1027*7f296bb3SBarry Smiththe total number of subdomains `N` that is smaller than the MPI
1028*7f296bb3SBarry Smithcommunicator `size`. In this case `PCGASM` will coalesce `size/N`
1029*7f296bb3SBarry Smithconsecutive single-rank subdomains into a single multi-rank subdomain.
1030*7f296bb3SBarry SmithThe single-rank subdomains contain the degrees of freedom corresponding
1031*7f296bb3SBarry Smithto the locally-owned rows of the `PCGASM` preconditioning matrix –
1032*7f296bb3SBarry Smiththese are the subdomains `PCASM` and `PCGASM` use by default.
1033*7f296bb3SBarry Smith
1034*7f296bb3SBarry SmithEach of the multirank subdomain subproblems is defined on the
1035*7f296bb3SBarry Smithsubcommunicator that contains the coalesced `PCGASM` processes. In general
1036*7f296bb3SBarry Smiththis might not result in a very good subproblem if the single-rank
1037*7f296bb3SBarry Smithproblems corresponding to the coalesced processes are not very strongly
1038*7f296bb3SBarry Smithconnected. In the future this will be addressed with a hierarchical
1039*7f296bb3SBarry Smithpartitioner that generates well-connected coarse subdomains first before
1040*7f296bb3SBarry Smithsubpartitioning them into the single-rank subdomains.
1041*7f296bb3SBarry Smith
1042*7f296bb3SBarry SmithIn the meantime the user can provide his or her own multi-rank
1043*7f296bb3SBarry Smithsubdomains by calling `PCGASMSetSubdomains(PC,IS[],IS[])` where each
1044*7f296bb3SBarry Smithof the `IS` objects on the list defines the inner (without the
1045*7f296bb3SBarry Smithoverlap) or the outer (including the overlap) subdomain on the
1046*7f296bb3SBarry Smithsubcommunicator of the `IS` object. A helper subroutine
1047*7f296bb3SBarry Smith`PCGASMCreateSubdomains2D()` is similar to PCASM’s but is capable of
1048*7f296bb3SBarry Smithconstructing multi-rank subdomains that can be then used with
1049*7f296bb3SBarry Smith`PCGASMSetSubdomains()`. An alternative way of creating multi-rank
1050*7f296bb3SBarry Smithsubdomains is by using the underlying `DM` object, if it is capable of
1051*7f296bb3SBarry Smithgenerating such decompositions via `DMCreateDomainDecomposition()`.
1052*7f296bb3SBarry SmithOrdinarily the decomposition specified by the user via
1053*7f296bb3SBarry Smith`PCGASMSetSubdomains()` takes precedence, unless
1054*7f296bb3SBarry Smith`PCGASMSetUseDMSubdomains()` instructs `PCGASM` to prefer
1055*7f296bb3SBarry Smith`DM`-created decompositions.
1056*7f296bb3SBarry Smith
1057*7f296bb3SBarry SmithCurrently there is no support for increasing the overlap of multi-rank
1058*7f296bb3SBarry Smithsubdomains via `PCGASMSetOverlap()` – this functionality works only
1059*7f296bb3SBarry Smithfor subdomains that fit within a single MPI process, exactly as in
1060*7f296bb3SBarry Smith`PCASM`.
1061*7f296bb3SBarry Smith
1062*7f296bb3SBarry SmithExamples of the described `PCGASM` usage can be found in
1063*7f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex62.c.html">KSP Tutorial ex62</a>.
1064*7f296bb3SBarry SmithIn particular, `runex62_superlu_dist` illustrates the use of
1065*7f296bb3SBarry Smith`SuperLU_DIST` as the subdomain solver on coalesced multi-rank
1066*7f296bb3SBarry Smithsubdomains. The `runex62_2D_*` examples illustrate the use of
1067*7f296bb3SBarry Smith`PCGASMCreateSubdomains2D()`.
1068*7f296bb3SBarry Smith
1069*7f296bb3SBarry Smith(sec_amg)=
1070*7f296bb3SBarry Smith
1071*7f296bb3SBarry Smith### Algebraic Multigrid (AMG) Preconditioners
1072*7f296bb3SBarry Smith
1073*7f296bb3SBarry SmithPETSc has a native algebraic multigrid preconditioner `PCGAMG` –
1074*7f296bb3SBarry Smith*gamg* – and interfaces to three external AMG packages: *hypre*, *ML*
1075*7f296bb3SBarry Smithand *AMGx* (CUDA platforms only) that can be downloaded in the
1076*7f296bb3SBarry Smithconfiguration phase (e.g., `--download-hypre` ) and used by
1077*7f296bb3SBarry Smithspecifying that command line parameter (e.g., `-pc_type hypre`).
1078*7f296bb3SBarry Smith*Hypre* is relatively monolithic in that a PETSc matrix is converted into a hypre
1079*7f296bb3SBarry Smithmatrix, and then *hypre* is called to solve the entire problem. *ML* is more
1080*7f296bb3SBarry Smithmodular because PETSc only has *ML* generate the coarse grid spaces
1081*7f296bb3SBarry Smith(columns of the prolongation operator), which is the core of an AMG method,
1082*7f296bb3SBarry Smithand then constructs a `PCMG` with Galerkin coarse grid operator
1083*7f296bb3SBarry Smithconstruction. `PCGAMG` is designed from the beginning to be modular, to
1084*7f296bb3SBarry Smithallow for new components to be added easily and also populates a
1085*7f296bb3SBarry Smithmultigrid preconditioner `PCMG` so generic multigrid parameters are
1086*7f296bb3SBarry Smithused (see {any}`sec_mg`). PETSc provides a fully supported (smoothed) aggregation AMG, but supports the addition of new methods
1087*7f296bb3SBarry Smith(`-pc_type gamg -pc_gamg_type agg` or `PCSetType(pc,PCGAMG)` and
1088*7f296bb3SBarry Smith`PCGAMGSetType(pc, PCGAMGAGG)`. Examples of extension are reference implementations of
1089*7f296bb3SBarry Smitha classical AMG method (`-pc_gamg_type classical`), a (2D) hybrid geometric
1090*7f296bb3SBarry SmithAMG method (`-pc_gamg_type geo`) that are not supported. A 2.5D AMG method DofColumns
1091*7f296bb3SBarry Smith{cite}`isaacstadlerghattas2015` supports 2D coarsenings extruded in the third dimension. `PCGAMG` does require the use
1092*7f296bb3SBarry Smithof `MATAIJ` matrices. For instance, `MATBAIJ` matrices are not supported. One
1093*7f296bb3SBarry Smithcan use `MATAIJ` instead of `MATBAIJ` without changing any code other than the
1094*7f296bb3SBarry Smithconstructor (or the `-mat_type` from the command line). For instance,
1095*7f296bb3SBarry Smith`MatSetValuesBlocked` works with `MATAIJ` matrices.
1096*7f296bb3SBarry Smith
1097*7f296bb3SBarry Smith**Important parameters for PCGAMGAGG**
1098*7f296bb3SBarry Smith
1099*7f296bb3SBarry Smith- Control the generation of the coarse grid
1100*7f296bb3SBarry Smith
1101*7f296bb3SBarry Smith  > - `-pc_gamg_aggressive_coarsening` \<n:int:1> Use aggressive coarsening on the finest n levels to construct the coarser mesh.
1102*7f296bb3SBarry Smith  >   See `PCGAMGAGGSetNSmooths()`. The larger value produces a faster preconditioner to create and solve, but the convergence may be slower.
1103*7f296bb3SBarry Smith  > - `-pc_gamg_low_memory_threshold_filter` \<bool:false> Filter small matrix entries before coarsening the mesh.
1104*7f296bb3SBarry Smith  >   See `PCGAMGSetLowMemoryFilter()`.
1105*7f296bb3SBarry Smith  > - `-pc_gamg_threshold` \<tol:real:0.0> The threshold of small values to drop when `-pc_gamg_low_memory_threshold_filter` is used. A
1106*7f296bb3SBarry Smith  >   negative value means keeping even the locations with 0.0. See `PCGAMGSetThreshold()`
1107*7f296bb3SBarry Smith  > - `-pc_gamg_threshold_scale` \<v>:real:1.0> Set a scale factor applied to each coarser level when `-pc_gamg_low_memory_threshold_filter` is used.
1108*7f296bb3SBarry Smith  >   See `PCGAMGSetThresholdScale()`.
1109*7f296bb3SBarry Smith  > - `-pc_gamg_mat_coarsen_type` \<mis|hem|misk:misk> Algorithm used to coarsen the matrix graph. See `MatCoarsenSetType()`.
1110*7f296bb3SBarry Smith  > - `-pc_gamg_mat_coarsen_max_it` \<it:int:4> Maximum HEM iterations to use. See `MatCoarsenSetMaximumIterations()`.
1111*7f296bb3SBarry Smith  > - `-pc_gamg_aggressive_mis_k` \<k:int:2> k distance in MIS coarsening (>2 is 'aggressive') to use in coarsening.
1112*7f296bb3SBarry Smith  >   See `PCGAMGMISkSetAggressive()`. The larger value produces a preconditioner that is faster to create and solve with but the convergence may be slower.
1113*7f296bb3SBarry Smith  >   This option and the previous option work to determine how aggressively the grids are coarsened.
1114*7f296bb3SBarry Smith  > - `-pc_gamg_mis_k_minimum_degree_ordering` \<bool:true> Use a minimum degree ordering in the greedy MIS algorithm used to coarsen.
1115*7f296bb3SBarry Smith  >   See `PCGAMGMISkSetMinDegreeOrdering()`
1116*7f296bb3SBarry Smith
1117*7f296bb3SBarry Smith- Control the generation of the prolongation for `PCGAMGAGG`
1118*7f296bb3SBarry Smith
1119*7f296bb3SBarry Smith  > - `-pc_gamg_agg_nsmooths` \<n:int:1> Number of smoothing steps to be used in constructing the prolongation. For symmetric problems,
1120*7f296bb3SBarry Smith  >   generally, one or more is best. For some strongly nonsymmetric problems, 0 may be best. See `PCGAMGSetNSmooths()`.
1121*7f296bb3SBarry Smith
1122*7f296bb3SBarry Smith- Control the amount of parallelism on the levels
1123*7f296bb3SBarry Smith
1124*7f296bb3SBarry Smith  > - `-pc_gamg_process_eq_limit` \<n:int:50> Sets the minimum number of equations allowed per process when coarsening (otherwise, fewer MPI processes
1125*7f296bb3SBarry Smith  >   are used for the coarser mesh). A larger value will cause the coarser problems to be run on fewer MPI processes, resulting
1126*7f296bb3SBarry Smith  >   in less communication and possibly a faster time to solution. See `PCGAMGSetProcEqLim()`.
1127*7f296bb3SBarry Smith  >
1128*7f296bb3SBarry Smith  > - `-pc_gamg_rank_reduction_factors` \<rn,rn-1,...,r1:int> Set a schedule for MPI rank reduction on coarse grids. `See PCGAMGSetRankReductionFactors()`
1129*7f296bb3SBarry Smith  >   This overrides the lessening of processes that would arise from `-pc_gamg_process_eq_limit`.
1130*7f296bb3SBarry Smith  >
1131*7f296bb3SBarry Smith  > - `-pc_gamg_repartition` \<bool:false> Run a partitioner on each coarser mesh generated rather than using the default partition arising from the
1132*7f296bb3SBarry Smith  >   finer mesh. See `PCGAMGSetRepartition()`. This increases the preconditioner setup time but will result in less time per
1133*7f296bb3SBarry Smith  >   iteration of the solver.
1134*7f296bb3SBarry Smith  >
1135*7f296bb3SBarry Smith  > - `-pc_gamg_parallel_coarse_grid_solver` \<bool:false> Allow the coarse grid solve to run in parallel, depending on the value of `-pc_gamg_coarse_eq_limit`.
1136*7f296bb3SBarry Smith  >   See `PCGAMGSetParallelCoarseGridSolve()`. If the coarse grid problem is large then this can
1137*7f296bb3SBarry Smith  >   improve the time to solution.
1138*7f296bb3SBarry Smith  >
1139*7f296bb3SBarry Smith  >   - `-pc_gamg_coarse_eq_limit` \<n:int:50> Sets the minimum number of equations allowed per process on the coarsest level when coarsening
1140*7f296bb3SBarry Smith  >     (otherwise fewer MPI processes will be used). A larger value will cause the coarse problems to be run on fewer MPI processes.
1141*7f296bb3SBarry Smith  >     This only applies if `-pc_gamg_parallel_coarse_grid_solver` is set to true. See `PCGAMGSetCoarseEqLim()`.
1142*7f296bb3SBarry Smith
1143*7f296bb3SBarry Smith- Control the smoothers
1144*7f296bb3SBarry Smith
1145*7f296bb3SBarry Smith  > - `-pc_mg_levels` \<n:int> Set the maximum number of levels to use.
1146*7f296bb3SBarry Smith  > - `-mg_levels_ksp_type` \<KSPType:chebyshev> If `KSPCHEBYSHEV` or `KSPRICHARDSON` is not used, then the Krylov
1147*7f296bb3SBarry Smith  >   method for the entire multigrid solve has to be a flexible method such as `KSPFGMRES`. Generally, the
1148*7f296bb3SBarry Smith  >   stronger the Krylov method the faster the convergence, but with more cost per iteration. See `KSPSetType()`.
1149*7f296bb3SBarry Smith  > - `-mg_levels_ksp_max_it` \<its:int:2> Sets the number of iterations to run the smoother on each level. Generally, the more iterations
1150*7f296bb3SBarry Smith  >   , the faster the convergence, but with more cost per multigrid iteration. See `PCMGSetNumberSmooth()`.
1151*7f296bb3SBarry Smith  > - `-mg_levels_ksp_xxx` Sets options for the `KSP` in the smoother on the levels.
1152*7f296bb3SBarry Smith  > - `-mg_levels_pc_type` \<PCType:jacobi> Sets the smoother to use on each level. See `PCSetType()`. Generally, the
1153*7f296bb3SBarry Smith  >   stronger the preconditioner the faster the convergence, but with more cost per iteration.
1154*7f296bb3SBarry Smith  > - `-mg_levels_pc_xxx` Sets options for the `PC` in the smoother on the levels.
1155*7f296bb3SBarry Smith  > - `-mg_coarse_ksp_type` \<KSPType:none> Sets the solver `KSPType` to use on the coarsest level.
1156*7f296bb3SBarry Smith  > - `-mg_coarse_pc_type` \<PCType:lu> Sets the solver `PCType` to use on the coarsest level.
1157*7f296bb3SBarry Smith  > - `-pc_gamg_asm_use_agg` \<bool:false> Use `PCASM` as the smoother on each level with the aggregates defined by the coarsening process are
1158*7f296bb3SBarry Smith  >   the subdomains. This option automatically switches the smoother on the levels to be `PCASM`.
1159*7f296bb3SBarry Smith  > - `-mg_levels_pc_asm_overlap` \<n:int:0> Use non-zero overlap with `-pc_gamg_asm_use_agg`. See `PCASMSetOverlap()`.
1160*7f296bb3SBarry Smith
1161*7f296bb3SBarry Smith- Control the multigrid algorithm
1162*7f296bb3SBarry Smith
1163*7f296bb3SBarry Smith  > - `-pc_mg_type` \<additive|multiplicative|full|kaskade:multiplicative> The type of multigrid to use. Usually, multiplicative is the fastest.
1164*7f296bb3SBarry Smith  > - `-pc_mg_cycle_type` \<v|w:v> Use V- or W-cycle with `-pc_mg_type` `multiplicative`
1165*7f296bb3SBarry Smith
1166*7f296bb3SBarry Smith`PCGAMG` provides unsmoothed aggregation (`-pc_gamg_agg_nsmooths 0`) and
1167*7f296bb3SBarry Smithsmoothed aggregation (`-pc_gamg_agg_nsmooths 1` or
1168*7f296bb3SBarry Smith`PCGAMGSetNSmooths(pc,1)`). Smoothed aggregation (SA), {cite}`vanek1996algebraic`, {cite}`vanek2001convergence`, is recommended
1169*7f296bb3SBarry Smithfor symmetric positive definite systems. Unsmoothed aggregation can be
1170*7f296bb3SBarry Smithuseful for asymmetric problems and problems where the highest eigenestimates are problematic. If poor convergence rates are observed using
1171*7f296bb3SBarry Smiththe smoothed version, one can test unsmoothed aggregation.
1172*7f296bb3SBarry Smith
1173*7f296bb3SBarry Smith**Eigenvalue estimates:** The parameters for the KSP eigen estimator,
1174*7f296bb3SBarry Smithused for SA, can be set with `-pc_gamg_esteig_ksp_max_it` and
1175*7f296bb3SBarry Smith`-pc_gamg_esteig_ksp_type`. For example, CG generally converges to the
1176*7f296bb3SBarry Smithhighest eigenvalue faster than GMRES (the default for KSP) if your problem
1177*7f296bb3SBarry Smithis symmetric positive definite. One can specify CG with
1178*7f296bb3SBarry Smith`-pc_gamg_esteig_ksp_type cg`. The default for
1179*7f296bb3SBarry Smith`-pc_gamg_esteig_ksp_max_it` is 10, which we have found is pretty safe
1180*7f296bb3SBarry Smithwith a (default) safety factor of 1.1. One can specify the range of real
1181*7f296bb3SBarry Smitheigenvalues in the same way as with Chebyshev KSP solvers
1182*7f296bb3SBarry Smith(smoothers), with `-pc_gamg_eigenvalues <emin,emax>`. GAMG sets the MG
1183*7f296bb3SBarry Smithsmoother type to chebyshev by default. By default, GAMG uses its eigen
1184*7f296bb3SBarry Smithestimate, if it has one, for Chebyshev smoothers if the smoother uses
1185*7f296bb3SBarry SmithJacobi preconditioning. This can be overridden with
1186*7f296bb3SBarry Smith`-pc_gamg_use_sa_esteig  <true,false>`.
1187*7f296bb3SBarry Smith
1188*7f296bb3SBarry SmithAMG methods require knowledge of the number of degrees of freedom per
1189*7f296bb3SBarry Smithvertex; the default is one (a scalar problem). Vector problems like
1190*7f296bb3SBarry Smithelasticity should set the block size of the matrix appropriately with
1191*7f296bb3SBarry Smith`-mat_block_size bs` or `MatSetBlockSize(mat,bs)`. Equations must be
1192*7f296bb3SBarry Smithordered in “vertex-major” ordering (e.g.,
1193*7f296bb3SBarry Smith$x_1,y_1,z_1,x_2,y_2,...$).
1194*7f296bb3SBarry Smith
1195*7f296bb3SBarry Smith**Near null space:** Smoothed aggregation requires an explicit
1196*7f296bb3SBarry Smithrepresentation of the (near) null space of the operator for optimal
1197*7f296bb3SBarry Smithperformance. One can provide an orthonormal set of null space vectors
1198*7f296bb3SBarry Smithwith `MatSetNearNullSpace()`. The vector of all ones is the default
1199*7f296bb3SBarry Smithfor each variable given by the block size (e.g., the translational rigid
1200*7f296bb3SBarry Smithbody modes). For elasticity, where rotational rigid body modes are
1201*7f296bb3SBarry Smithrequired to complete the near null-space you can use
1202*7f296bb3SBarry Smith`MatNullSpaceCreateRigidBody()` to create the null space vectors and
1203*7f296bb3SBarry Smiththen `MatSetNearNullSpace()`.
1204*7f296bb3SBarry Smith
1205*7f296bb3SBarry Smith**Coarse grid data model:** The GAMG framework provides for reducing the
1206*7f296bb3SBarry Smithnumber of active processes on coarse grids to reduce communication costs
1207*7f296bb3SBarry Smithwhen there is not enough parallelism to keep relative communication
1208*7f296bb3SBarry Smithcosts down. Most AMG solvers reduce to just one active process on the
1209*7f296bb3SBarry Smithcoarsest grid (the PETSc MG framework also supports redundantly solving
1210*7f296bb3SBarry Smiththe coarse grid on all processes to reduce communication
1211*7f296bb3SBarry Smithcosts potentially). However, this forcing to one process can be overridden if one
1212*7f296bb3SBarry Smithwishes to use a parallel coarse grid solver. GAMG generalizes this by
1213*7f296bb3SBarry Smithreducing the active number of processes on other coarse grids.
1214*7f296bb3SBarry SmithGAMG will select the number of active processors by fitting the desired
1215*7f296bb3SBarry Smithnumber of equations per process (set with
1216*7f296bb3SBarry Smith`-pc_gamg_process_eq_limit <50>,`) at each level given that size of
1217*7f296bb3SBarry Smitheach level. If $P_i < P$ processors are desired on a level
1218*7f296bb3SBarry Smith$i$, then the first $P_i$ processes are populated with the grid
1219*7f296bb3SBarry Smithand the remaining are empty on that grid. One can, and probably should,
1220*7f296bb3SBarry Smithrepartition the coarse grids with `-pc_gamg_repartition <true>`,
1221*7f296bb3SBarry Smithotherwise an integer process reduction factor ($q$) is selected
1222*7f296bb3SBarry Smithand the equations on the first $q$ processes are moved to process
1223*7f296bb3SBarry Smith0, and so on. As mentioned, multigrid generally coarsens the problem
1224*7f296bb3SBarry Smithuntil it is small enough to be solved with an exact solver (e.g., LU or
1225*7f296bb3SBarry SmithSVD) in a relatively short time. GAMG will stop coarsening when the
1226*7f296bb3SBarry Smithnumber of the equation on a grid falls below the threshold given by
1227*7f296bb3SBarry Smith`-pc_gamg_coarse_eq_limit <50>,`.
1228*7f296bb3SBarry Smith
1229*7f296bb3SBarry Smith**Coarse grid parameters:** There are several options to provide
1230*7f296bb3SBarry Smithparameters to the coarsening algorithm and parallel data layout. Run a
1231*7f296bb3SBarry Smithcode using `PCGAMG` with `-help` to get a full listing of GAMG
1232*7f296bb3SBarry Smithparameters with short descriptions. The rate of coarsening is
1233*7f296bb3SBarry Smithcritical in AMG performance – too slow coarsening will result in an
1234*7f296bb3SBarry Smithoverly expensive solver per iteration and too fast coarsening will
1235*7f296bb3SBarry Smithresult in decrease in the convergence rate. `-pc_gamg_threshold <-1>`
1236*7f296bb3SBarry Smithand `-pc_gamg_aggressive_coarsening <N>` are the primary parameters that
1237*7f296bb3SBarry Smithcontrol coarsening rates, which is very important for AMG performance. A
1238*7f296bb3SBarry Smithgreedy maximal independent set (MIS) algorithm is used in coarsening.
1239*7f296bb3SBarry SmithSquaring the graph implements MIS-2; the root vertex in an
1240*7f296bb3SBarry Smithaggregate is more than two edges away from another root vertex instead
1241*7f296bb3SBarry Smithof more than one in MIS. The threshold parameter sets a normalized
1242*7f296bb3SBarry Smiththreshold for which edges are removed from the MIS graph, thereby
1243*7f296bb3SBarry Smithcoarsening slower. Zero will keep all non-zero edges, a negative number
1244*7f296bb3SBarry Smithwill keep zero edges, and a positive number will drop small edges. Typical
1245*7f296bb3SBarry Smithfinite threshold values are in the range of $0.01 - 0.05$. There
1246*7f296bb3SBarry Smithare additional parameters for changing the weights on coarse grids.
1247*7f296bb3SBarry Smith
1248*7f296bb3SBarry SmithThe parallel MIS algorithms require symmetric weights/matrices. Thus `PCGAMG`
1249*7f296bb3SBarry Smithwill automatically make the graph symmetric if it is not symmetric. Since this
1250*7f296bb3SBarry Smithhas additional cost, users should indicate the symmetry of the matrices they
1251*7f296bb3SBarry Smithprovide by calling
1252*7f296bb3SBarry Smith
1253*7f296bb3SBarry Smith```
1254*7f296bb3SBarry SmithMatSetOption(mat,MAT_SYMMETRIC,PETSC_TRUE (or PETSC_FALSE))
1255*7f296bb3SBarry Smith```
1256*7f296bb3SBarry Smith
1257*7f296bb3SBarry Smithor
1258*7f296bb3SBarry Smith
1259*7f296bb3SBarry Smith```
1260*7f296bb3SBarry SmithMatSetOption(mat,MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE (or PETSC_FALSE)).
1261*7f296bb3SBarry Smith```
1262*7f296bb3SBarry Smith
1263*7f296bb3SBarry SmithIf they know that the matrix will always have symmetry despite future changes
1264*7f296bb3SBarry Smithto the matrix (with, for example, `MatSetValues()`) then they should also call
1265*7f296bb3SBarry Smith
1266*7f296bb3SBarry Smith```
1267*7f296bb3SBarry SmithMatSetOption(mat,MAT_SYMMETRY_ETERNAL,PETSC_TRUE (or PETSC_FALSE))
1268*7f296bb3SBarry Smith```
1269*7f296bb3SBarry Smith
1270*7f296bb3SBarry Smithor
1271*7f296bb3SBarry Smith
1272*7f296bb3SBarry Smith```
1273*7f296bb3SBarry SmithMatSetOption(mat,MAT_STRUCTURAL_SYMMETRY_ETERNAL,PETSC_TRUE (or PETSC_FALSE)).
1274*7f296bb3SBarry Smith```
1275*7f296bb3SBarry Smith
1276*7f296bb3SBarry SmithUsing this information allows the algorithm to skip unnecessary computations.
1277*7f296bb3SBarry Smith
1278*7f296bb3SBarry Smith**Troubleshooting algebraic multigrid methods:** If `PCGAMG`, *ML*, *AMGx* or
1279*7f296bb3SBarry Smith*hypre* does not perform well; the first thing to try is one of the other
1280*7f296bb3SBarry Smithmethods. Often, the default parameters or just the strengths of different
1281*7f296bb3SBarry Smithalgorithms can fix performance problems or provide useful information to
1282*7f296bb3SBarry Smithguide further debugging. There are several sources of poor performance
1283*7f296bb3SBarry Smithof AMG solvers and often special purpose methods must be developed to
1284*7f296bb3SBarry Smithachieve the full potential of multigrid. To name just a few sources of
1285*7f296bb3SBarry Smithperformance degradation that may not be fixed with parameters in PETSc
1286*7f296bb3SBarry Smithcurrently: non-elliptic operators, curl/curl operators, highly stretched
1287*7f296bb3SBarry Smithgrids or highly anisotropic problems, large jumps in material
1288*7f296bb3SBarry Smithcoefficients with complex geometry (AMG is particularly well suited to
1289*7f296bb3SBarry Smithjumps in coefficients, but it is not a perfect solution), highly
1290*7f296bb3SBarry Smithincompressible elasticity, not to mention ill-posed problems and many
1291*7f296bb3SBarry Smithothers. For Grad-Div and Curl-Curl operators, you may want to try the
1292*7f296bb3SBarry SmithAuxiliary-space Maxwell Solver (AMS,
1293*7f296bb3SBarry Smith`-pc_type hypre -pc_hypre_type ams`) or the Auxiliary-space Divergence
1294*7f296bb3SBarry SmithSolver (ADS, `-pc_type hypre -pc_hypre_type ads`) solvers. These
1295*7f296bb3SBarry Smithsolvers need some additional information on the underlying mesh;
1296*7f296bb3SBarry Smithspecifically, AMS needs the discrete gradient operator, which can be
1297*7f296bb3SBarry Smithspecified via `PCHYPRESetDiscreteGradient()`. In addition to the
1298*7f296bb3SBarry Smithdiscrete gradient, ADS also needs the specification of the discrete curl
1299*7f296bb3SBarry Smithoperator, which can be set using `PCHYPRESetDiscreteCurl()`.
1300*7f296bb3SBarry Smith
1301*7f296bb3SBarry Smith**I am converging slowly, what do I do?** AMG methods are sensitive to
1302*7f296bb3SBarry Smithcoarsening rates and methods; for GAMG use `-pc_gamg_threshold <x>`
1303*7f296bb3SBarry Smithor `PCGAMGSetThreshold()` to regulate coarsening rates; higher values decrease
1304*7f296bb3SBarry Smithcoarsening rate. Squaring the graph is the second mechanism for
1305*7f296bb3SBarry Smithincreasing the coarsening rate. Use `-pc_gamg_aggressive_coarsening <N>`, or
1306*7f296bb3SBarry Smith`PCGAMGSetAggressiveLevels(pc,N)`, to aggressive ly coarsen (MIS-2) the graph on the finest N
1307*7f296bb3SBarry Smithlevels. A high threshold (e.g., $x=0.08$) will result in an
1308*7f296bb3SBarry Smithexpensive but potentially powerful preconditioner, and a low threshold
1309*7f296bb3SBarry Smith(e.g., $x=0.0$) will result in faster coarsening, fewer levels,
1310*7f296bb3SBarry Smithcheaper solves, and generally worse convergence rates.
1311*7f296bb3SBarry Smith
1312*7f296bb3SBarry SmithOne can run with `-info :pc` and grep for `PCGAMG` to get statistics on
1313*7f296bb3SBarry Smitheach level, which can be used to see if you are coarsening at an
1314*7f296bb3SBarry Smithappropriate rate. With smoothed aggregation, you generally want to coarse
1315*7f296bb3SBarry Smithat about a rate of 3:1 in each dimension. Coarsening too slowly will
1316*7f296bb3SBarry Smithresult in large numbers of non-zeros per row on coarse grids (this is
1317*7f296bb3SBarry Smithreported). The number of non-zeros can go up very high, say about 300
1318*7f296bb3SBarry Smith(times the degrees of freedom per vertex) on a 3D hex mesh. One can also
1319*7f296bb3SBarry Smithlook at the grid complexity, which is also reported (the ratio of the
1320*7f296bb3SBarry Smithtotal number of matrix entries for all levels to the number of matrix
1321*7f296bb3SBarry Smithentries on the fine level). Grid complexity should be well under 2.0 and
1322*7f296bb3SBarry Smithpreferably around $1.3$ or lower. If convergence is poor and the
1323*7f296bb3SBarry SmithGalerkin coarse grid construction is much smaller than the time for each
1324*7f296bb3SBarry Smithsolve, one can safely decrease the coarsening rate.
1325*7f296bb3SBarry Smith`-pc_gamg_threshold` $-1.0$ is the simplest and most robust
1326*7f296bb3SBarry Smithoption and is recommended if poor convergence rates are observed, at
1327*7f296bb3SBarry Smithleast until the source of the problem is discovered. In conclusion, decreasing the coarsening rate (increasing the
1328*7f296bb3SBarry Smiththreshold) should be tried if convergence is slow.
1329*7f296bb3SBarry Smith
1330*7f296bb3SBarry Smith**A note on Chebyshev smoothers.** Chebyshev solvers are attractive as
1331*7f296bb3SBarry Smithmultigrid smoothers because they can target a specific interval of the
1332*7f296bb3SBarry Smithspectrum, which is the purpose of a smoother. The spectral bounds for
1333*7f296bb3SBarry SmithChebyshev solvers are simple to compute because they rely on the highest
1334*7f296bb3SBarry Smitheigenvalue of your (diagonally preconditioned) operator, which is
1335*7f296bb3SBarry Smithconceptually simple to compute. However, if this highest eigenvalue
1336*7f296bb3SBarry Smithestimate is not accurate (too low), the solvers can fail with an
1337*7f296bb3SBarry Smithindefinite preconditioner message. One can run with `-info` and grep
1338*7f296bb3SBarry Smithfor `PCGAMG` to get these estimates or use `-ksp_view`. These highest
1339*7f296bb3SBarry Smitheigenvalues are generally between 1.5-3.0. For symmetric positive
1340*7f296bb3SBarry Smithdefinite systems, CG is a better eigenvalue estimator
1341*7f296bb3SBarry Smith`-mg_levels_esteig_ksp_type cg`. Bad Eigen estimates often cause indefinite matrix messages. Explicitly damped Jacobi or Krylov
1342*7f296bb3SBarry Smithsmoothers can provide an alternative to Chebyshev, and *hypre* has
1343*7f296bb3SBarry Smithalternative smoothers.
1344*7f296bb3SBarry Smith
1345*7f296bb3SBarry Smith**Now, am I solving alright? Can I expect better?** If you find that you
1346*7f296bb3SBarry Smithare getting nearly one digit in reduction of the residual per iteration
1347*7f296bb3SBarry Smithand are using a modest number of point smoothing steps (e.g., 1-4
1348*7f296bb3SBarry Smithiterations of SOR), then you may be fairly close to textbook multigrid
1349*7f296bb3SBarry Smithefficiency. However, you also need to check the setup costs. This can be
1350*7f296bb3SBarry Smithdetermined by running with `-log_view` and check that the time for the
1351*7f296bb3SBarry SmithGalerkin coarse grid construction (`MatPtAP()`) is not (much) more than
1352*7f296bb3SBarry Smiththe time spent in each solve (`KSPSolve()`). If the `MatPtAP()` time is
1353*7f296bb3SBarry Smithtoo large, then one can increase the coarsening rate by decreasing the
1354*7f296bb3SBarry Smiththreshold and using aggressive coarsening
1355*7f296bb3SBarry Smith(`-pc_gamg_aggressive_coarsening <N>`, squares the graph on the finest N
1356*7f296bb3SBarry Smithlevels). Likewise, if your `MatPtAP()` time is short and your convergence
1357*7f296bb3SBarry SmithIf the rate is not ideal, you could decrease the coarsening rate.
1358*7f296bb3SBarry Smith
1359*7f296bb3SBarry SmithPETSc’s AMG solver is a framework for developers to
1360*7f296bb3SBarry Smitheasily add AMG capabilities, like new AMG methods or an AMG component
1361*7f296bb3SBarry Smithlike a matrix triple product. Contact us directly if you are interested
1362*7f296bb3SBarry Smithin contributing.
1363*7f296bb3SBarry Smith
1364*7f296bb3SBarry SmithUsing algebraic multigrid as a "standalone" solver is possible but not recommended, as it does not accelerate it with a Krylov method.
1365*7f296bb3SBarry SmithUse a `KSPType` of `KSPRICHARDSON`
1366*7f296bb3SBarry Smith(or equivalently `-ksp_type richardson`) to achieve this. Using `KSPPREONLY` will not work since it only applies a single multigrid cycle.
1367*7f296bb3SBarry Smith
1368*7f296bb3SBarry Smith#### Adaptive Interpolation
1369*7f296bb3SBarry Smith
1370*7f296bb3SBarry Smith**Interpolation** transfers a function from the coarse space to the fine space. We would like this process to be accurate for the functions resolved by the coarse grid, in particular the approximate solution computed there. By default, we create these matrices using local interpolation of the fine grid dual basis functions in the coarse basis. However, an adaptive procedure can optimize the coefficients of the interpolator to reproduce pairs of coarse/fine functions which should approximate the lowest modes of the generalized eigenproblem
1371*7f296bb3SBarry Smith
1372*7f296bb3SBarry Smith$$
1373*7f296bb3SBarry SmithA x = \lambda M x
1374*7f296bb3SBarry Smith$$
1375*7f296bb3SBarry Smith
1376*7f296bb3SBarry Smithwhere $A$ is the system matrix and $M$ is the smoother. Note that for defect-correction MG, the interpolated solution from the coarse space need not be as accurate as the fine solution, for the same reason that updates in iterative refinement can be less accurate. However, in FAS or in the final interpolation step for each level of Full Multigrid, we must have interpolation as accurate as the fine solution since we are moving the entire solution itself.
1377*7f296bb3SBarry Smith
1378*7f296bb3SBarry Smith**Injection** should accurately transfer the fine solution to the coarse grid. Accuracy here means that the action of a coarse dual function on either should produce approximately the same result. In the structured grid case, this means that we just use the same values on coarse points. This can result in aliasing.
1379*7f296bb3SBarry Smith
1380*7f296bb3SBarry Smith**Restriction** is intended to transfer the fine residual to the coarse space. Here we use averaging (often the transpose of the interpolation operation) to damp out the fine space contributions. Thus, it is less accurate than injection, but avoids aliasing of the high modes.
1381*7f296bb3SBarry Smith
1382*7f296bb3SBarry SmithFor a multigrid cycle, the interpolator $P$ is intended to accurately reproduce "smooth" functions from the coarse space in the fine space, keeping the energy of the interpolant about the same. For the Laplacian on a structured mesh, it is easy to determine what these low-frequency functions are. They are the Fourier modes. However an arbitrary operator $A$ will have different coarse modes that we want to resolve accurately on the fine grid, so that our coarse solve produces a good guess for the fine problem. How do we make sure that our interpolator $P$ can do this?
1383*7f296bb3SBarry Smith
1384*7f296bb3SBarry SmithWe first must decide what we mean by accurate interpolation of some functions. Suppose we know the continuum function $f$ that we care about, and we are only interested in a finite element description of discrete functions. Then the coarse function representing $f$ is given by
1385*7f296bb3SBarry Smith
1386*7f296bb3SBarry Smith$$
1387*7f296bb3SBarry Smithf^C = \sum_i f^C_i \phi^C_i,
1388*7f296bb3SBarry Smith$$
1389*7f296bb3SBarry Smith
1390*7f296bb3SBarry Smithand similarly the fine grid form is
1391*7f296bb3SBarry Smith
1392*7f296bb3SBarry Smith$$
1393*7f296bb3SBarry Smithf^F = \sum_i f^F_i \phi^F_i.
1394*7f296bb3SBarry Smith$$
1395*7f296bb3SBarry Smith
1396*7f296bb3SBarry SmithNow we would like the interpolant of the coarse representer to the fine grid to be as close as possible to the fine representer in a least squares sense, meaning we want to solve the minimization problem
1397*7f296bb3SBarry Smith
1398*7f296bb3SBarry Smith$$
1399*7f296bb3SBarry Smith\min_{P} \| f^F - P f^C \|_2
1400*7f296bb3SBarry Smith$$
1401*7f296bb3SBarry Smith
1402*7f296bb3SBarry SmithNow we can express $P$ as a matrix by looking at the matrix elements $P_{ij} = \phi^F_i P \phi^C_j$. Then we have
1403*7f296bb3SBarry Smith
1404*7f296bb3SBarry Smith$$
1405*7f296bb3SBarry Smith\begin{aligned}
1406*7f296bb3SBarry Smith  &\phi^F_i f^F - \phi^F_i P f^C \\
1407*7f296bb3SBarry Smith= &f^F_i - \sum_j P_{ij} f^C_j
1408*7f296bb3SBarry Smith\end{aligned}
1409*7f296bb3SBarry Smith$$
1410*7f296bb3SBarry Smith
1411*7f296bb3SBarry Smithso that our discrete optimization problem is
1412*7f296bb3SBarry Smith
1413*7f296bb3SBarry Smith$$
1414*7f296bb3SBarry Smith\min_{P_{ij}} \| f^F_i - \sum_j P_{ij} f^C_j \|_2
1415*7f296bb3SBarry Smith$$
1416*7f296bb3SBarry Smith
1417*7f296bb3SBarry Smithand we will treat each row of the interpolator as a separate optimization problem. We could allow an arbitrary sparsity pattern, or try to determine adaptively, as is done in sparse approximate inverse preconditioning. However, we know the supports of the basis functions in finite elements, and thus the naive sparsity pattern from local interpolation can be used.
1418*7f296bb3SBarry Smith
1419*7f296bb3SBarry SmithWe note here that the BAMG framework of Brannick et al. {cite}`brandtbrannickkahllivshits2011` does not use fine and coarse functions spaces, but rather a fine point/coarse point division which we will not employ here. Our general PETSc routine should work for both since the input would be the checking set (fine basis coefficients or fine space points) and the approximation set (coarse basis coefficients in the support or coarse points in the sparsity pattern).
1420*7f296bb3SBarry Smith
1421*7f296bb3SBarry SmithWe can easily solve the above problem using QR factorization. However, there are many smooth functions from the coarse space that we want interpolated accurately, and a single $f$ would not constrain the values $P_{ij}`$ well. Therefore, we will use several functions $\{f_k\}$ in our minimization,
1422*7f296bb3SBarry Smith
1423*7f296bb3SBarry Smith$$
1424*7f296bb3SBarry Smith\begin{aligned}
1425*7f296bb3SBarry Smith  &\min_{P_{ij}} \sum_k w_k \| f^{F,k}_i - \sum_j P_{ij} f^{C,k}_j \|_2 \\
1426*7f296bb3SBarry Smith= &\min_{P_{ij}} \sum_k \| \sqrt{w_k} f^{F,k}_i - \sqrt{w_k} \sum_j P_{ij} f^{C,k}_j \|_2 \\
1427*7f296bb3SBarry Smith= &\min_{P_{ij}} \| W^{1/2} \mathbf{f}^{F}_i - W^{1/2} \mathbf{f}^{C} p_i \|_2
1428*7f296bb3SBarry Smith\end{aligned}
1429*7f296bb3SBarry Smith$$
1430*7f296bb3SBarry Smith
1431*7f296bb3SBarry Smithwhere
1432*7f296bb3SBarry Smith
1433*7f296bb3SBarry Smith$$
1434*7f296bb3SBarry Smith\begin{aligned}
1435*7f296bb3SBarry SmithW         &= \begin{pmatrix} w_0 & & \\ & \ddots & \\ & & w_K \end{pmatrix} \\
1436*7f296bb3SBarry Smith\mathbf{f}^{F}_i &= \begin{pmatrix} f^{F,0}_i \\ \vdots \\ f^{F,K}_i \end{pmatrix} \\
1437*7f296bb3SBarry Smith\mathbf{f}^{C}   &= \begin{pmatrix} f^{C,0}_0 & \cdots & f^{C,0}_n \\ \vdots & \ddots &  \vdots \\ f^{C,K}_0 & \cdots & f^{C,K}_n \end{pmatrix} \\
1438*7f296bb3SBarry Smithp_i       &= \begin{pmatrix} P_{i0} \\ \vdots \\ P_{in} \end{pmatrix}
1439*7f296bb3SBarry Smith\end{aligned}
1440*7f296bb3SBarry Smith$$
1441*7f296bb3SBarry Smith
1442*7f296bb3SBarry Smithor alternatively
1443*7f296bb3SBarry Smith
1444*7f296bb3SBarry Smith$$
1445*7f296bb3SBarry Smith\begin{aligned}
1446*7f296bb3SBarry Smith[W]_{kk}     &= w_k \\
1447*7f296bb3SBarry Smith[f^{F}_i]_k  &= f^{F,k}_i \\
1448*7f296bb3SBarry Smith[f^{C}]_{kj} &= f^{C,k}_j \\
1449*7f296bb3SBarry Smith[p_i]_j      &= P_{ij}
1450*7f296bb3SBarry Smith\end{aligned}
1451*7f296bb3SBarry Smith$$
1452*7f296bb3SBarry Smith
1453*7f296bb3SBarry SmithWe thus have a standard least-squares problem
1454*7f296bb3SBarry Smith
1455*7f296bb3SBarry Smith$$
1456*7f296bb3SBarry Smith\min_{P_{ij}} \| b - A x \|_2
1457*7f296bb3SBarry Smith$$
1458*7f296bb3SBarry Smith
1459*7f296bb3SBarry Smithwhere
1460*7f296bb3SBarry Smith
1461*7f296bb3SBarry Smith$$
1462*7f296bb3SBarry Smith\begin{aligned}
1463*7f296bb3SBarry SmithA &= W^{1/2} f^{C} \\
1464*7f296bb3SBarry Smithb &= W^{1/2} f^{F}_i \\
1465*7f296bb3SBarry Smithx &= p_i
1466*7f296bb3SBarry Smith\end{aligned}
1467*7f296bb3SBarry Smith$$
1468*7f296bb3SBarry Smith
1469*7f296bb3SBarry Smithwhich can be solved using LAPACK.
1470*7f296bb3SBarry Smith
1471*7f296bb3SBarry SmithWe will typically perform this optimization on a multigrid level $l$ when the change in eigenvalue from level $l+1$ is relatively large, meaning
1472*7f296bb3SBarry Smith
1473*7f296bb3SBarry Smith$$
1474*7f296bb3SBarry Smith\frac{|\lambda_l - \lambda_{l+1}|}{|\lambda_l|}.
1475*7f296bb3SBarry Smith$$
1476*7f296bb3SBarry Smith
1477*7f296bb3SBarry SmithThis indicates that the generalized eigenvector associated with that eigenvalue was not adequately represented by $P^l_{l+1}`$, and the interpolator should be recomputed.
1478*7f296bb3SBarry Smith
1479*7f296bb3SBarry Smith```{raw} html
1480*7f296bb3SBarry Smith<hr>
1481*7f296bb3SBarry Smith```
1482*7f296bb3SBarry Smith
1483*7f296bb3SBarry Smith### Balancing Domain Decomposition by Constraints
1484*7f296bb3SBarry Smith
1485*7f296bb3SBarry SmithPETSc provides the Balancing Domain Decomposition by Constraints (`PCBDDC`)
1486*7f296bb3SBarry Smithmethod for preconditioning parallel finite element problems stored in
1487*7f296bb3SBarry Smithunassembled format (see `MATIS`). `PCBDDC` is a 2-level non-overlapping
1488*7f296bb3SBarry Smithdomain decomposition method which can be easily adapted to different
1489*7f296bb3SBarry Smithproblems and discretizations by means of few user customizations. The
1490*7f296bb3SBarry Smithapplication of the preconditioner to a vector consists in the static
1491*7f296bb3SBarry Smithcondensation of the residual at the interior of the subdomains by means
1492*7f296bb3SBarry Smithof local Dirichlet solves, followed by an additive combination of Neumann
1493*7f296bb3SBarry Smithlocal corrections and the solution of a global coupled coarse problem.
1494*7f296bb3SBarry SmithCommand line options for the underlying `KSP` objects are prefixed by
1495*7f296bb3SBarry Smith`-pc_bddc_dirichlet`, `-pc_bddc_neumann`, and `-pc_bddc_coarse`
1496*7f296bb3SBarry Smithrespectively.
1497*7f296bb3SBarry Smith
1498*7f296bb3SBarry SmithThe implementation supports any kind of linear system, and
1499*7f296bb3SBarry Smithassumes a one-to-one mapping between subdomains and MPI processes.
1500*7f296bb3SBarry SmithComplex numbers are supported as well. For non-symmetric problems, use
1501*7f296bb3SBarry Smiththe runtime option `-pc_bddc_symmetric 0`.
1502*7f296bb3SBarry Smith
1503*7f296bb3SBarry SmithUnlike conventional non-overlapping methods that iterates just on the
1504*7f296bb3SBarry Smithdegrees of freedom at the interface between subdomain, `PCBDDC`
1505*7f296bb3SBarry Smithiterates on the whole set of degrees of freedom, allowing the use of
1506*7f296bb3SBarry Smithapproximate subdomain solvers. When using approximate solvers, the
1507*7f296bb3SBarry Smithcommand line switches `-pc_bddc_dirichlet_approximate` and/or
1508*7f296bb3SBarry Smith`-pc_bddc_neumann_approximate` should be used to inform `PCBDDC`. If
1509*7f296bb3SBarry Smithany of the local problems is singular, the nullspace of the local
1510*7f296bb3SBarry Smithoperator should be attached to the local matrix via
1511*7f296bb3SBarry Smith`MatSetNullSpace()`.
1512*7f296bb3SBarry Smith
1513*7f296bb3SBarry SmithAt the basis of the method there’s the analysis of the connected
1514*7f296bb3SBarry Smithcomponents of the interface for the detection of vertices, edges and
1515*7f296bb3SBarry Smithfaces equivalence classes. Additional information on the degrees of
1516*7f296bb3SBarry Smithfreedom can be supplied to `PCBDDC` by using the following functions:
1517*7f296bb3SBarry Smith
1518*7f296bb3SBarry Smith- `PCBDDCSetDofsSplitting()`
1519*7f296bb3SBarry Smith- `PCBDDCSetLocalAdjacencyGraph()`
1520*7f296bb3SBarry Smith- `PCBDDCSetPrimalVerticesLocalIS()`
1521*7f296bb3SBarry Smith- `PCBDDCSetNeumannBoundaries()`
1522*7f296bb3SBarry Smith- `PCBDDCSetDirichletBoundaries()`
1523*7f296bb3SBarry Smith- `PCBDDCSetNeumannBoundariesLocal()`
1524*7f296bb3SBarry Smith- `PCBDDCSetDirichletBoundariesLocal()`
1525*7f296bb3SBarry Smith
1526*7f296bb3SBarry SmithCrucial for the convergence of the iterative process is the
1527*7f296bb3SBarry Smithspecification of the primal constraints to be imposed at the interface
1528*7f296bb3SBarry Smithbetween subdomains. `PCBDDC` uses by default vertex continuities and
1529*7f296bb3SBarry Smithedge arithmetic averages, which are enough for the three-dimensional
1530*7f296bb3SBarry SmithPoisson problem with constant coefficients. The user can switch on and
1531*7f296bb3SBarry Smithoff the usage of vertices, edges or face constraints by using the
1532*7f296bb3SBarry Smithcommand line switches `-pc_bddc_use_vertices`, `-pc_bddc_use_edges`,
1533*7f296bb3SBarry Smith`-pc_bddc_use_faces`. A customization of the constraints is available
1534*7f296bb3SBarry Smithby attaching a `MatNullSpace` object to the preconditioning matrix via
1535*7f296bb3SBarry Smith`MatSetNearNullSpace()`. The vectors of the `MatNullSpace` object
1536*7f296bb3SBarry Smithshould represent the constraints in the form of quadrature rules;
1537*7f296bb3SBarry Smithquadrature rules for different classes of the interface can be listed in
1538*7f296bb3SBarry Smiththe same vector. The number of vectors of the `MatNullSpace` object
1539*7f296bb3SBarry Smithcorresponds to the maximum number of constraints that can be imposed for
1540*7f296bb3SBarry Smitheach class. Once all the quadrature rules for a given interface class
1541*7f296bb3SBarry Smithhave been extracted, an SVD operation is performed to retain the
1542*7f296bb3SBarry Smithnon-singular modes. As an example, the rigid body modes represent an
1543*7f296bb3SBarry Smitheffective choice for elasticity, even in the almost incompressible case.
1544*7f296bb3SBarry SmithFor particular problems, e.g. edge-based discretization with Nedelec
1545*7f296bb3SBarry Smithelements, a user defined change of basis of the degrees of freedom can
1546*7f296bb3SBarry Smithbe beneficial for `PCBDDC`; use `PCBDDCSetChangeOfBasisMat()` to
1547*7f296bb3SBarry Smithcustomize the change of basis.
1548*7f296bb3SBarry Smith
1549*7f296bb3SBarry SmithThe `PCBDDC` method is usually robust with respect to jumps in the material
1550*7f296bb3SBarry Smithparameters aligned with the interface; for PDEs with more than one
1551*7f296bb3SBarry Smithmaterial parameter you may also consider to use the so-called deluxe
1552*7f296bb3SBarry Smithscaling, available via the command line switch
1553*7f296bb3SBarry Smith`-pc_bddc_use_deluxe_scaling`. Other scalings are available, see
1554*7f296bb3SBarry Smith`PCISSetSubdomainScalingFactor()`,
1555*7f296bb3SBarry Smith`PCISSetSubdomainDiagonalScaling()` or
1556*7f296bb3SBarry Smith`PCISSetUseStiffnessScaling()`. However, the convergence properties of
1557*7f296bb3SBarry Smiththe `PCBDDC` method degrades in presence of large jumps in the material
1558*7f296bb3SBarry Smithcoefficients not aligned with the interface; for such cases, PETSc has
1559*7f296bb3SBarry Smiththe capability of adaptively computing the primal constraints. Adaptive
1560*7f296bb3SBarry Smithselection of constraints could be requested by specifying a threshold
1561*7f296bb3SBarry Smithvalue at command line by using `-pc_bddc_adaptive_threshold x`. Valid
1562*7f296bb3SBarry Smithvalues for the threshold `x` ranges from 1 to infinity, with smaller
1563*7f296bb3SBarry Smithvalues corresponding to more robust preconditioners. For SPD problems in
1564*7f296bb3SBarry Smith2D, or in 3D with only face degrees of freedom (like in the case of
1565*7f296bb3SBarry SmithRaviart-Thomas or Brezzi-Douglas-Marini elements), such a threshold is a
1566*7f296bb3SBarry Smithvery accurate estimator of the condition number of the resulting
1567*7f296bb3SBarry Smithpreconditioned operator. Since the adaptive selection of constraints for
1568*7f296bb3SBarry Smith`PCBDDC` methods is still an active topic of research, its implementation is
1569*7f296bb3SBarry Smithcurrently limited to SPD problems; moreover, because the technique
1570*7f296bb3SBarry Smithrequires the explicit knowledge of the local Schur complements, it needs
1571*7f296bb3SBarry Smiththe external package MUMPS.
1572*7f296bb3SBarry Smith
1573*7f296bb3SBarry SmithWhen solving problems decomposed in thousands of subdomains or more, the
1574*7f296bb3SBarry Smithsolution of the `PCBDDC` coarse problem could become a bottleneck; in order
1575*7f296bb3SBarry Smithto overcome this issue, the user could either consider to solve the
1576*7f296bb3SBarry Smithparallel coarse problem on a subset of the communicator associated with
1577*7f296bb3SBarry Smith`PCBDDC` by using the command line switch
1578*7f296bb3SBarry Smith`-pc_bddc_coarse_redistribute`, or instead use a multilevel approach.
1579*7f296bb3SBarry SmithThe latter can be requested by specifying the number of requested level
1580*7f296bb3SBarry Smithat command line (`-pc_bddc_levels`) or by using `PCBDDCSetLevels()`.
1581*7f296bb3SBarry SmithAn additional parameter (see `PCBDDCSetCoarseningRatio()`) controls
1582*7f296bb3SBarry Smiththe number of subdomains that will be generated at the next level; the
1583*7f296bb3SBarry Smithlarger the coarsening ratio, the lower the number of coarser subdomains.
1584*7f296bb3SBarry Smith
1585*7f296bb3SBarry SmithFor further details, see the example
1586*7f296bb3SBarry Smith<a href="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex59.c">KSP Tutorial ex59</a>
1587*7f296bb3SBarry Smithand the online documentation for `PCBDDC`.
1588*7f296bb3SBarry Smith
1589*7f296bb3SBarry Smith### Shell Preconditioners
1590*7f296bb3SBarry Smith
1591*7f296bb3SBarry SmithThe shell preconditioner simply uses an application-provided routine to
1592*7f296bb3SBarry Smithimplement the preconditioner. That is, it allows users to write or wrap their
1593*7f296bb3SBarry Smithown custom preconditioners as a `PC` and use it with `KSP`, etc.
1594*7f296bb3SBarry Smith
1595*7f296bb3SBarry SmithTo provide a custom preconditioner application, use
1596*7f296bb3SBarry Smith
1597*7f296bb3SBarry Smith```
1598*7f296bb3SBarry SmithPCShellSetApply(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec));
1599*7f296bb3SBarry Smith```
1600*7f296bb3SBarry Smith
1601*7f296bb3SBarry SmithOften a preconditioner needs access to an application-provided data
1602*7f296bb3SBarry Smithstructured. For this, one should use
1603*7f296bb3SBarry Smith
1604*7f296bb3SBarry Smith```
1605*7f296bb3SBarry SmithPCShellSetContext(PC pc,void *ctx);
1606*7f296bb3SBarry Smith```
1607*7f296bb3SBarry Smith
1608*7f296bb3SBarry Smithto set this data structure and
1609*7f296bb3SBarry Smith
1610*7f296bb3SBarry Smith```
1611*7f296bb3SBarry SmithPCShellGetContext(PC pc,void *ctx);
1612*7f296bb3SBarry Smith```
1613*7f296bb3SBarry Smith
1614*7f296bb3SBarry Smithto retrieve it in `apply`. The three routine arguments of `apply()`
1615*7f296bb3SBarry Smithare the `PC`, the input vector, and the output vector, respectively.
1616*7f296bb3SBarry Smith
1617*7f296bb3SBarry SmithFor a preconditioner that requires some sort of “setup” before being
1618*7f296bb3SBarry Smithused, that requires a new setup every time the operator is changed, one
1619*7f296bb3SBarry Smithcan provide a routine that is called every time the operator is changed
1620*7f296bb3SBarry Smith(usually via `KSPSetOperators()`).
1621*7f296bb3SBarry Smith
1622*7f296bb3SBarry Smith```
1623*7f296bb3SBarry SmithPCShellSetSetUp(PC pc,PetscErrorCode (*setup)(PC));
1624*7f296bb3SBarry Smith```
1625*7f296bb3SBarry Smith
1626*7f296bb3SBarry SmithThe argument to the `setup` routine is the same `PC` object which
1627*7f296bb3SBarry Smithcan be used to obtain the operators with `PCGetOperators()` and the
1628*7f296bb3SBarry Smithapplication-provided data structure that was set with
1629*7f296bb3SBarry Smith`PCShellSetContext()`.
1630*7f296bb3SBarry Smith
1631*7f296bb3SBarry Smith(sec_combining_pcs)=
1632*7f296bb3SBarry Smith
1633*7f296bb3SBarry Smith### Combining Preconditioners
1634*7f296bb3SBarry Smith
1635*7f296bb3SBarry SmithThe `PC` type `PCCOMPOSITE` allows one to form new preconditioners
1636*7f296bb3SBarry Smithby combining already-defined preconditioners and solvers. Combining
1637*7f296bb3SBarry Smithpreconditioners usually requires some experimentation to find a
1638*7f296bb3SBarry Smithcombination of preconditioners that works better than any single method.
1639*7f296bb3SBarry SmithIt is a tricky business and is not recommended until your application
1640*7f296bb3SBarry Smithcode is complete and running and you are trying to improve performance.
1641*7f296bb3SBarry SmithIn many cases using a single preconditioner is better than a
1642*7f296bb3SBarry Smithcombination; an exception is the multigrid/multilevel preconditioners
1643*7f296bb3SBarry Smith(solvers) that are always combinations of some sort, see {any}`sec_mg`.
1644*7f296bb3SBarry Smith
1645*7f296bb3SBarry SmithLet $B_1$ and $B_2$ represent the application of two
1646*7f296bb3SBarry Smithpreconditioners of type `type1` and `type2`. The preconditioner
1647*7f296bb3SBarry Smith$B = B_1 + B_2$ can be obtained with
1648*7f296bb3SBarry Smith
1649*7f296bb3SBarry Smith```
1650*7f296bb3SBarry SmithPCSetType(pc,PCCOMPOSITE);
1651*7f296bb3SBarry SmithPCCompositeAddPCType(pc,type1);
1652*7f296bb3SBarry SmithPCCompositeAddPCType(pc,type2);
1653*7f296bb3SBarry Smith```
1654*7f296bb3SBarry Smith
1655*7f296bb3SBarry SmithAny number of preconditioners may added in this way.
1656*7f296bb3SBarry Smith
1657*7f296bb3SBarry SmithThis way of combining preconditioners is called additive, since the
1658*7f296bb3SBarry Smithactions of the preconditioners are added together. This is the default
1659*7f296bb3SBarry Smithbehavior. An alternative can be set with the option
1660*7f296bb3SBarry Smith
1661*7f296bb3SBarry Smith```
1662*7f296bb3SBarry SmithPCCompositeSetType(pc,PC_COMPOSITE_MULTIPLICATIVE);
1663*7f296bb3SBarry Smith```
1664*7f296bb3SBarry Smith
1665*7f296bb3SBarry SmithIn this form the new residual is updated after the application of each
1666*7f296bb3SBarry Smithpreconditioner and the next preconditioner applied to the next residual.
1667*7f296bb3SBarry SmithFor example, with two composed preconditioners: $B_1$ and
1668*7f296bb3SBarry Smith$B_2$; $y = B x$ is obtained from
1669*7f296bb3SBarry Smith
1670*7f296bb3SBarry Smith$$
1671*7f296bb3SBarry Smith\begin{aligned}
1672*7f296bb3SBarry Smithy    = B_1 x \\
1673*7f296bb3SBarry Smithw_1  = x - A y \\
1674*7f296bb3SBarry Smithy    = y + B_2 w_1\end{aligned}
1675*7f296bb3SBarry Smith$$
1676*7f296bb3SBarry Smith
1677*7f296bb3SBarry SmithLoosely, this corresponds to a Gauss-Seidel iteration, while additive
1678*7f296bb3SBarry Smithcorresponds to a Jacobi iteration.
1679*7f296bb3SBarry Smith
1680*7f296bb3SBarry SmithUnder most circumstances, the multiplicative form requires one-half the
1681*7f296bb3SBarry Smithnumber of iterations as the additive form; however, the multiplicative
1682*7f296bb3SBarry Smithform does require the application of $A$ inside the
1683*7f296bb3SBarry Smithpreconditioner.
1684*7f296bb3SBarry Smith
1685*7f296bb3SBarry SmithIn the multiplicative version, the calculation of the residual inside
1686*7f296bb3SBarry Smiththe preconditioner can be done in two ways: using the original linear
1687*7f296bb3SBarry Smithsystem matrix or using the matrix used to build the preconditioners
1688*7f296bb3SBarry Smith$B_1$, $B_2$, etc. By default it uses the “preconditioner
1689*7f296bb3SBarry Smithmatrix”, to use the `Amat` matrix use the option
1690*7f296bb3SBarry Smith
1691*7f296bb3SBarry Smith```
1692*7f296bb3SBarry SmithPCSetUseAmat(PC pc);
1693*7f296bb3SBarry Smith```
1694*7f296bb3SBarry Smith
1695*7f296bb3SBarry SmithThe individual preconditioners can be accessed (in order to set options)
1696*7f296bb3SBarry Smithvia
1697*7f296bb3SBarry Smith
1698*7f296bb3SBarry Smith```
1699*7f296bb3SBarry SmithPCCompositeGetPC(PC pc,PetscInt count,PC *subpc);
1700*7f296bb3SBarry Smith```
1701*7f296bb3SBarry Smith
1702*7f296bb3SBarry SmithFor example, to set the first sub preconditioners to use ILU(1)
1703*7f296bb3SBarry Smith
1704*7f296bb3SBarry Smith```
1705*7f296bb3SBarry SmithPC subpc;
1706*7f296bb3SBarry SmithPCCompositeGetPC(pc,0,&subpc);
1707*7f296bb3SBarry SmithPCFactorSetFill(subpc,1);
1708*7f296bb3SBarry Smith```
1709*7f296bb3SBarry Smith
1710*7f296bb3SBarry SmithOne can also change the operator that is used to construct a particular
1711*7f296bb3SBarry Smith`PC` in the composite `PC` calling `PCSetOperators()` on the obtained `PC`.
1712*7f296bb3SBarry Smith`PCFIELDSPLIT`, {any}`sec_block_matrices`, provides an alternative approach to defining composite preconditioners
1713*7f296bb3SBarry Smithwith a variety of pre-defined compositions.
1714*7f296bb3SBarry Smith
1715*7f296bb3SBarry SmithThese various options can also be set via the options database. For
1716*7f296bb3SBarry Smithexample, `-pc_type` `composite` `-pc_composite_pcs` `jacobi,ilu`
1717*7f296bb3SBarry Smithcauses the composite preconditioner to be used with two preconditioners:
1718*7f296bb3SBarry SmithJacobi and ILU. The option `-pc_composite_type` `multiplicative`
1719*7f296bb3SBarry Smithinitiates the multiplicative version of the algorithm, while
1720*7f296bb3SBarry Smith`-pc_composite_type` `additive` the additive version. Using the
1721*7f296bb3SBarry Smith`Amat` matrix is obtained with the option `-pc_use_amat`. One sets
1722*7f296bb3SBarry Smithoptions for the sub-preconditioners with the extra prefix `-sub_N_`
1723*7f296bb3SBarry Smithwhere `N` is the number of the sub-preconditioner. For example,
1724*7f296bb3SBarry Smith`-sub_0_pc_ifactor_fill` `0`.
1725*7f296bb3SBarry Smith
1726*7f296bb3SBarry SmithPETSc also allows a preconditioner to be a complete `KSPSolve()` linear solver. This
1727*7f296bb3SBarry Smithis achieved with the `PCKSP` type.
1728*7f296bb3SBarry Smith
1729*7f296bb3SBarry Smith```
1730*7f296bb3SBarry SmithPCSetType(PC pc,PCKSP);
1731*7f296bb3SBarry SmithPCKSPGetKSP(pc,&ksp);
1732*7f296bb3SBarry Smith /* set any KSP/PC options */
1733*7f296bb3SBarry Smith```
1734*7f296bb3SBarry Smith
1735*7f296bb3SBarry SmithFrom the command line one can use 5 iterations of biCG-stab with ILU(0)
1736*7f296bb3SBarry Smithpreconditioning as the preconditioner with
1737*7f296bb3SBarry Smith`-pc_type ksp -ksp_pc_type ilu -ksp_ksp_max_it 5 -ksp_ksp_type bcgs`.
1738*7f296bb3SBarry Smith
1739*7f296bb3SBarry SmithBy default the inner `KSP` solver uses the outer preconditioner
1740*7f296bb3SBarry Smithmatrix, `Pmat`, as the matrix to be solved in the linear system; to
1741*7f296bb3SBarry Smithuse the matrix that defines the linear system, `Amat` use the option
1742*7f296bb3SBarry Smith
1743*7f296bb3SBarry Smith```
1744*7f296bb3SBarry SmithPCSetUseAmat(PC pc);
1745*7f296bb3SBarry Smith```
1746*7f296bb3SBarry Smith
1747*7f296bb3SBarry Smithor at the command line with `-pc_use_amat`.
1748*7f296bb3SBarry Smith
1749*7f296bb3SBarry SmithNaturally, one can use a `PCKSP` preconditioner inside a composite
1750*7f296bb3SBarry Smithpreconditioner. For example,
1751*7f296bb3SBarry Smith`-pc_type composite -pc_composite_pcs ilu,ksp -sub_1_pc_type jacobi -sub_1_ksp_max_it 10`
1752*7f296bb3SBarry Smithuses two preconditioners: ILU(0) and 10 iterations of GMRES with Jacobi
1753*7f296bb3SBarry Smithpreconditioning. However, it is not clear whether one would ever wish to
1754*7f296bb3SBarry Smithdo such a thing.
1755*7f296bb3SBarry Smith
1756*7f296bb3SBarry Smith(sec_mg)=
1757*7f296bb3SBarry Smith
1758*7f296bb3SBarry Smith### Multigrid Preconditioners
1759*7f296bb3SBarry Smith
1760*7f296bb3SBarry SmithA large suite of routines is available for using geometric multigrid as
1761*7f296bb3SBarry Smitha preconditioner [^id3]. In the `PC` framework, the user is required to
1762*7f296bb3SBarry Smithprovide the coarse grid solver, smoothers, restriction and interpolation
1763*7f296bb3SBarry Smithoperators, and code to calculate residuals. The `PC` package allows
1764*7f296bb3SBarry Smiththese components to be encapsulated within a PETSc-compliant
1765*7f296bb3SBarry Smithpreconditioner. We fully support both matrix-free and matrix-based
1766*7f296bb3SBarry Smithmultigrid solvers.
1767*7f296bb3SBarry Smith
1768*7f296bb3SBarry SmithA multigrid preconditioner is created with the four commands
1769*7f296bb3SBarry Smith
1770*7f296bb3SBarry Smith```
1771*7f296bb3SBarry SmithKSPCreate(MPI_Comm comm,KSP *ksp);
1772*7f296bb3SBarry SmithKSPGetPC(KSP ksp,PC *pc);
1773*7f296bb3SBarry SmithPCSetType(PC pc,PCMG);
1774*7f296bb3SBarry SmithPCMGSetLevels(pc,PetscInt levels,MPI_Comm *comms);
1775*7f296bb3SBarry Smith```
1776*7f296bb3SBarry Smith
1777*7f296bb3SBarry SmithA large number of parameters affect the multigrid behavior. The command
1778*7f296bb3SBarry Smith
1779*7f296bb3SBarry Smith```
1780*7f296bb3SBarry SmithPCMGSetType(PC pc,PCMGType mode);
1781*7f296bb3SBarry Smith```
1782*7f296bb3SBarry Smith
1783*7f296bb3SBarry Smithindicates which form of multigrid to apply {cite}`1sbg`.
1784*7f296bb3SBarry Smith
1785*7f296bb3SBarry SmithFor standard V or W-cycle multigrids, one sets the `mode` to be
1786*7f296bb3SBarry Smith`PC_MG_MULTIPLICATIVE`; for the additive form (which in certain cases
1787*7f296bb3SBarry Smithreduces to the BPX method, or additive multilevel Schwarz, or multilevel
1788*7f296bb3SBarry Smithdiagonal scaling), one uses `PC_MG_ADDITIVE` as the `mode`. For a
1789*7f296bb3SBarry Smithvariant of full multigrid, one can use `PC_MG_FULL`, and for the
1790*7f296bb3SBarry SmithKaskade algorithm `PC_MG_KASKADE`. For the multiplicative and full
1791*7f296bb3SBarry Smithmultigrid options, one can use a W-cycle by calling
1792*7f296bb3SBarry Smith
1793*7f296bb3SBarry Smith```
1794*7f296bb3SBarry SmithPCMGSetCycleType(PC pc,PCMGCycleType ctype);
1795*7f296bb3SBarry Smith```
1796*7f296bb3SBarry Smith
1797*7f296bb3SBarry Smithwith a value of `PC_MG_CYCLE_W` for `ctype`. The commands above can
1798*7f296bb3SBarry Smithalso be set from the options database. The option names are
1799*7f296bb3SBarry Smith`-pc_mg_type [multiplicative, additive, full, kaskade]`, and
1800*7f296bb3SBarry Smith`-pc_mg_cycle_type` `<ctype>`.
1801*7f296bb3SBarry Smith
1802*7f296bb3SBarry SmithThe user can control the amount of smoothing by configuring the solvers
1803*7f296bb3SBarry Smithon the levels. By default, the up and down smoothers are identical. If
1804*7f296bb3SBarry Smithseparate configuration of up and down smooths is required, it can be
1805*7f296bb3SBarry Smithrequested with the option `-pc_mg_distinct_smoothup` or the routine
1806*7f296bb3SBarry Smith
1807*7f296bb3SBarry Smith```
1808*7f296bb3SBarry SmithPCMGSetDistinctSmoothUp(PC pc);
1809*7f296bb3SBarry Smith```
1810*7f296bb3SBarry Smith
1811*7f296bb3SBarry SmithThe multigrid routines, which determine the solvers and
1812*7f296bb3SBarry Smithinterpolation/restriction operators that are used, are mandatory. To set
1813*7f296bb3SBarry Smiththe coarse grid solver, one must call
1814*7f296bb3SBarry Smith
1815*7f296bb3SBarry Smith```
1816*7f296bb3SBarry SmithPCMGGetCoarseSolve(PC pc,KSP *ksp);
1817*7f296bb3SBarry Smith```
1818*7f296bb3SBarry Smith
1819*7f296bb3SBarry Smithand set the appropriate options in `ksp`. Similarly, the smoothers are
1820*7f296bb3SBarry Smithcontrolled by first calling
1821*7f296bb3SBarry Smith
1822*7f296bb3SBarry Smith```
1823*7f296bb3SBarry SmithPCMGGetSmoother(PC pc,PetscInt level,KSP *ksp);
1824*7f296bb3SBarry Smith```
1825*7f296bb3SBarry Smith
1826*7f296bb3SBarry Smithand then setting the various options in the `ksp.` For example,
1827*7f296bb3SBarry Smith
1828*7f296bb3SBarry Smith```
1829*7f296bb3SBarry SmithPCMGGetSmoother(pc,1,&ksp);
1830*7f296bb3SBarry SmithKSPSetOperators(ksp,A1,A1);
1831*7f296bb3SBarry Smith```
1832*7f296bb3SBarry Smith
1833*7f296bb3SBarry Smithsets the matrix that defines the smoother on level 1 of the multigrid.
1834*7f296bb3SBarry SmithWhile
1835*7f296bb3SBarry Smith
1836*7f296bb3SBarry Smith```
1837*7f296bb3SBarry SmithPCMGGetSmoother(pc,1,&ksp);
1838*7f296bb3SBarry SmithKSPGetPC(ksp,&pc);
1839*7f296bb3SBarry SmithPCSetType(pc,PCSOR);
1840*7f296bb3SBarry Smith```
1841*7f296bb3SBarry Smith
1842*7f296bb3SBarry Smithsets SOR as the smoother to use on level 1.
1843*7f296bb3SBarry Smith
1844*7f296bb3SBarry SmithTo use a different pre- or postsmoother, one should call the following
1845*7f296bb3SBarry Smithroutines instead.
1846*7f296bb3SBarry Smith
1847*7f296bb3SBarry Smith```
1848*7f296bb3SBarry SmithPCMGGetSmootherUp(PC pc,PetscInt level,KSP *upksp);
1849*7f296bb3SBarry SmithPCMGGetSmootherDown(PC pc,PetscInt level,KSP *downksp);
1850*7f296bb3SBarry Smith```
1851*7f296bb3SBarry Smith
1852*7f296bb3SBarry SmithUse
1853*7f296bb3SBarry Smith
1854*7f296bb3SBarry Smith```
1855*7f296bb3SBarry SmithPCMGSetInterpolation(PC pc,PetscInt level,Mat P);
1856*7f296bb3SBarry Smith```
1857*7f296bb3SBarry Smith
1858*7f296bb3SBarry Smithand
1859*7f296bb3SBarry Smith
1860*7f296bb3SBarry Smith```
1861*7f296bb3SBarry SmithPCMGSetRestriction(PC pc,PetscInt level,Mat R);
1862*7f296bb3SBarry Smith```
1863*7f296bb3SBarry Smith
1864*7f296bb3SBarry Smithto define the intergrid transfer operations. If only one of these is
1865*7f296bb3SBarry Smithset, its transpose will be used for the other.
1866*7f296bb3SBarry Smith
1867*7f296bb3SBarry SmithIt is possible for these interpolation operations to be matrix-free (see
1868*7f296bb3SBarry Smith{any}`sec_matrixfree`); One should then make
1869*7f296bb3SBarry Smithsure that these operations are defined for the (matrix-free) matrices
1870*7f296bb3SBarry Smithpassed in. Note that this system is arranged so that if the
1871*7f296bb3SBarry Smithinterpolation is the transpose of the restriction, you can pass the same
1872*7f296bb3SBarry Smith`mat` argument to both `PCMGSetRestriction()` and
1873*7f296bb3SBarry Smith`PCMGSetInterpolation()`.
1874*7f296bb3SBarry Smith
1875*7f296bb3SBarry SmithOn each level except the coarsest, one must also set the routine to
1876*7f296bb3SBarry Smithcompute the residual. The following command suffices:
1877*7f296bb3SBarry Smith
1878*7f296bb3SBarry Smith```
1879*7f296bb3SBarry SmithPCMGSetResidual(PC pc,PetscInt level,PetscErrorCode (*residual)(Mat,Vec,Vec,Vec),Mat mat);
1880*7f296bb3SBarry Smith```
1881*7f296bb3SBarry Smith
1882*7f296bb3SBarry SmithThe `residual()` function normally does not need to be set if one’s
1883*7f296bb3SBarry Smithoperator is stored in `Mat` format. In certain circumstances, where it
1884*7f296bb3SBarry Smithis much cheaper to calculate the residual directly, rather than through
1885*7f296bb3SBarry Smiththe usual formula $b - Ax$, the user may wish to provide an
1886*7f296bb3SBarry Smithalternative.
1887*7f296bb3SBarry Smith
1888*7f296bb3SBarry SmithFinally, the user may provide three work vectors for each level (except
1889*7f296bb3SBarry Smithon the finest, where only the residual work vector is required). The
1890*7f296bb3SBarry Smithwork vectors are set with the commands
1891*7f296bb3SBarry Smith
1892*7f296bb3SBarry Smith```
1893*7f296bb3SBarry SmithPCMGSetRhs(PC pc,PetscInt level,Vec b);
1894*7f296bb3SBarry SmithPCMGSetX(PC pc,PetscInt level,Vec x);
1895*7f296bb3SBarry SmithPCMGSetR(PC pc,PetscInt level,Vec r);
1896*7f296bb3SBarry Smith```
1897*7f296bb3SBarry Smith
1898*7f296bb3SBarry SmithThe `PC` references these vectors, so you should call `VecDestroy()`
1899*7f296bb3SBarry Smithwhen you are finished with them. If any of these vectors are not
1900*7f296bb3SBarry Smithprovided, the preconditioner will allocate them.
1901*7f296bb3SBarry Smith
1902*7f296bb3SBarry SmithOne can control the `KSP` and `PC` options used on the various
1903*7f296bb3SBarry Smithlevels (as well as the coarse grid) using the prefix `mg_levels_`
1904*7f296bb3SBarry Smith(`mg_coarse_` for the coarse grid). For example,
1905*7f296bb3SBarry Smith`-mg_levels_ksp_type cg` will cause the CG method to be used as the
1906*7f296bb3SBarry SmithKrylov method for each level. Or
1907*7f296bb3SBarry Smith`-mg_levels_pc_type ilu -mg_levels_pc_factor_levels 2` will cause the
1908*7f296bb3SBarry SmithILU preconditioner to be used on each level with two levels of fill in
1909*7f296bb3SBarry Smiththe incomplete factorization.
1910*7f296bb3SBarry Smith
1911*7f296bb3SBarry Smith(sec_block_matrices)=
1912*7f296bb3SBarry Smith
1913*7f296bb3SBarry Smith## Solving Block Matrices with PCFIELDSPLIT
1914*7f296bb3SBarry Smith
1915*7f296bb3SBarry SmithBlock matrices represent an important class of problems in numerical
1916*7f296bb3SBarry Smithlinear algebra and offer the possibility of far more efficient iterative
1917*7f296bb3SBarry Smithsolvers than just treating the entire matrix as a black box. In this
1918*7f296bb3SBarry Smithsection, we use the common linear algebra definition of block matrices, where matrices are divided into a small, problem-size independent (two,
1919*7f296bb3SBarry Smiththree, or so) number of very large blocks. These blocks arise naturally
1920*7f296bb3SBarry Smithfrom the underlying physics or discretization of the problem, such as the velocity and pressure. Under a certain numbering of
1921*7f296bb3SBarry Smithunknowns, the matrix can be written as
1922*7f296bb3SBarry Smith
1923*7f296bb3SBarry Smith$$
1924*7f296bb3SBarry Smith\left( \begin{array}{cccc}
1925*7f296bb3SBarry SmithA_{00}   & A_{01} & A_{02} & A_{03} \\
1926*7f296bb3SBarry SmithA_{10}   & A_{11} & A_{12} & A_{13} \\
1927*7f296bb3SBarry SmithA_{20}   & A_{21} & A_{22} & A_{23} \\
1928*7f296bb3SBarry SmithA_{30}   & A_{31} & A_{32} & A_{33} \\
1929*7f296bb3SBarry Smith\end{array} \right),
1930*7f296bb3SBarry Smith$$
1931*7f296bb3SBarry Smith
1932*7f296bb3SBarry Smithwhere each $A_{ij}$ is an entire block. The matrices on a parallel computer are not explicitly stored this way. Instead, each process will
1933*7f296bb3SBarry Smithown some rows of $A_{0*}$, $A_{1*}$ etc. On a
1934*7f296bb3SBarry Smithprocess, the blocks may be stored in one block followed by another
1935*7f296bb3SBarry Smith
1936*7f296bb3SBarry Smith$$
1937*7f296bb3SBarry Smith\left( \begin{array}{ccccccc}
1938*7f296bb3SBarry SmithA_{{00}_{00}}   & A_{{00}_{01}} & A_{{00}_{02}} & ... & A_{{01}_{00}} & A_{{01}_{01}} & ...  \\
1939*7f296bb3SBarry SmithA_{{00}_{10}}   & A_{{00}_{11}} & A_{{00}_{12}} & ... & A_{{01}_{10}} & A_{{01}_{11}} & ... \\
1940*7f296bb3SBarry SmithA_{{00}_{20}}   & A_{{00}_{21}} & A_{{00}_{22}} & ... & A_{{01}_{20}} & A_{{01}_{21}}  & ...\\
1941*7f296bb3SBarry Smith... \\
1942*7f296bb3SBarry SmithA_{{10}_{00}}   & A_{{10}_{01}} & A_{{10}_{02}} & ... & A_{{11}_{00}} & A_{{11}_{01}}  & ... \\
1943*7f296bb3SBarry SmithA_{{10}_{10}}   & A_{{10}_{11}} & A_{{10}_{12}} & ... & A_{{11}_{10}} & A_{{11}_{11}}  & ... \\
1944*7f296bb3SBarry Smith... \\
1945*7f296bb3SBarry Smith\end{array} \right)
1946*7f296bb3SBarry Smith$$
1947*7f296bb3SBarry Smith
1948*7f296bb3SBarry Smithor interlaced, for example, with four blocks
1949*7f296bb3SBarry Smith
1950*7f296bb3SBarry Smith$$
1951*7f296bb3SBarry Smith\left( \begin{array}{ccccc}
1952*7f296bb3SBarry SmithA_{{00}_{00}}   & A_{{01}_{00}} &  A_{{00}_{01}} & A_{{01}_{01}} &  ... \\
1953*7f296bb3SBarry SmithA_{{10}_{00}}   & A_{{11}_{00}} &  A_{{10}_{01}} & A_{{11}_{01}} &  ... \\
1954*7f296bb3SBarry SmithA_{{00}_{10}}   & A_{{01}_{10}} & A_{{00}_{11}} & A_{{01}_{11}} & ...\\
1955*7f296bb3SBarry SmithA_{{10}_{10}}   & A_{{11}_{10}} & A_{{10}_{11}} & A_{{11}_{11}} & ...\\
1956*7f296bb3SBarry Smith...
1957*7f296bb3SBarry Smith\end{array} \right).
1958*7f296bb3SBarry Smith$$
1959*7f296bb3SBarry Smith
1960*7f296bb3SBarry SmithNote that for interlaced storage, the number of rows/columns of each
1961*7f296bb3SBarry Smithblock must be the same size. Matrices obtained with `DMCreateMatrix()`
1962*7f296bb3SBarry Smithwhere the `DM` is a `DMDA` are always stored interlaced. Block
1963*7f296bb3SBarry Smithmatrices can also be stored using the `MATNEST` format, which holds
1964*7f296bb3SBarry Smithseparate assembled blocks. Each of these nested matrices is itself
1965*7f296bb3SBarry Smithdistributed in parallel. It is more efficient to use `MATNEST` with
1966*7f296bb3SBarry Smiththe methods described in this section because there are fewer copies and
1967*7f296bb3SBarry Smithbetter formats (e.g., `MATBAIJ` or `MATSBAIJ`) can be used for the
1968*7f296bb3SBarry Smithcomponents, but it is not possible to use many other methods with
1969*7f296bb3SBarry Smith`MATNEST`. See {any}`sec_matnest` for more on assembling
1970*7f296bb3SBarry Smithblock matrices without depending on a specific matrix format.
1971*7f296bb3SBarry Smith
1972*7f296bb3SBarry SmithThe PETSc `PCFIELDSPLIT` preconditioner implements the
1973*7f296bb3SBarry Smith“block” solvers in PETSc, {cite}`elman2008tcp`. There are three ways to provide the
1974*7f296bb3SBarry Smithinformation that defines the blocks. If the matrices are stored as
1975*7f296bb3SBarry Smithinterlaced then `PCFieldSplitSetFields()` can be called repeatedly to
1976*7f296bb3SBarry Smithindicate which fields belong to each block. More generally
1977*7f296bb3SBarry Smith`PCFieldSplitSetIS()` can be used to indicate exactly which
1978*7f296bb3SBarry Smithrows/columns of the matrix belong to a particular block (field). You can provide
1979*7f296bb3SBarry Smithnames for each block with these routines; if you do not, they are numbered from 0. With these two approaches, the blocks may
1980*7f296bb3SBarry Smithoverlap (though they generally will not overlap). If only one block is defined,
1981*7f296bb3SBarry Smiththen the complement of the matrices is used to define the other block.
1982*7f296bb3SBarry SmithFinally, the option `-pc_fieldsplit_detect_saddle_point` causes two
1983*7f296bb3SBarry Smithdiagonal blocks to be found, one associated with all rows/columns that
1984*7f296bb3SBarry Smithhave zeros on the diagonals and the rest.
1985*7f296bb3SBarry Smith
1986*7f296bb3SBarry Smith**Important parameters for PCFIELDSPLIT**
1987*7f296bb3SBarry Smith
1988*7f296bb3SBarry Smith- Control the fields used
1989*7f296bb3SBarry Smith
1990*7f296bb3SBarry Smith  - `-pc_fieldsplit_detect_saddle_point` \<bool:false> Generate two fields, the first consists of all rows with a nonzero on the diagonal, and the second will be all rows
1991*7f296bb3SBarry Smith    with zero on the diagonal. See `PCFieldSplitSetDetectSaddlePoint()`.
1992*7f296bb3SBarry Smith
1993*7f296bb3SBarry Smith  - `-pc_fieldsplit_dm_splits` \<bool:true> Use the `DM` attached to the preconditioner to determine the fields. See `PCFieldSplitSetDMSplits()` and
1994*7f296bb3SBarry Smith    `DMCreateFieldDecomposition()`.
1995*7f296bb3SBarry Smith
1996*7f296bb3SBarry Smith  - `-pc_fieldsplit_%d_fields` \<f1,f2,...:int> Use f1, f2, .. to define field `d`. The `fn` are in the range of 0, ..., bs-1 where bs is the block size
1997*7f296bb3SBarry Smith    of the matrix or set with `PCFieldSplitSetBlockSize()`. See `PCFieldSplitSetFields()`.
1998*7f296bb3SBarry Smith
1999*7f296bb3SBarry Smith    - `-pc_fieldsplit_default` \<bool:true> Automatically add any fields needed that have not been supplied explicitly by `-pc_fieldsplit_%d_fields`.
2000*7f296bb3SBarry Smith
2001*7f296bb3SBarry Smith  - `DMFieldsplitSetIS()` Provide the `IS` that defines a particular field.
2002*7f296bb3SBarry Smith
2003*7f296bb3SBarry Smith- Control the type of the block preconditioner
2004*7f296bb3SBarry Smith
2005*7f296bb3SBarry Smith  - `-pc_fieldsplit_type` \<additive|multiplicative|symmetric_multiplicative|schur|gkb:multiplicative> The order in which the field solves are applied.
2006*7f296bb3SBarry Smith    For symmetric problems where `KSPCG` is used `symmetric_multiplicative` must be used instead of `multiplicative`. `additive` is the least expensive
2007*7f296bb3SBarry Smith    to apply but provides the worst convergence. `schur` requires either a good preconditioner for the Schur complement or a naturally well-conditioned
2008*7f296bb3SBarry Smith    Schur complement, but when it works well can be extremely effective. See `PCFieldSplitSetType()`. `gkb` is for symmetric saddle-point problems (the lower-right
2009*7f296bb3SBarry Smith    the block is zero).
2010*7f296bb3SBarry Smith
2011*7f296bb3SBarry Smith  - `-pc_fieldsplit_diag_use_amat` \<bool:false> Use the first matrix that is passed to `KSPSetJacobian()` to construct the block-diagonal sub-matrices used in the algorithms,
2012*7f296bb3SBarry Smith    by default, the second matrix is used.
2013*7f296bb3SBarry Smith
2014*7f296bb3SBarry Smith  - Options for Schur preconditioner: `-pc_fieldsplit_type`
2015*7f296bb3SBarry Smith    `schur`
2016*7f296bb3SBarry Smith
2017*7f296bb3SBarry Smith    - `-pc_fieldsplit_schur_fact_type` \<diag|lower|upper|full:diag> See `PCFieldSplitSetSchurFactType()`. `full` reduces the iterations but each iteration requires additional
2018*7f296bb3SBarry Smith      field solves.
2019*7f296bb3SBarry Smith
2020*7f296bb3SBarry Smith    - `-pc_fieldsplit_schur_precondition` \<self|selfp|user|a11|full:user> How the Schur complement is preconditioned. See `PCFieldSplitSetSchurPre()`.
2021*7f296bb3SBarry Smith
2022*7f296bb3SBarry Smith      - `-fieldsplit_1_mat_schur_complement_ainv_type` \<diag|lump:diag> Use the lumped diagonal of $A_{00}$ when `-pc_fieldsplit_schur_precondition`
2023*7f296bb3SBarry Smith        `selfp` is used.
2024*7f296bb3SBarry Smith
2025*7f296bb3SBarry Smith    - `-pc_fieldsplit_schur_scale` \<scale:real:-1.0> Controls the sign flip of S for `-pc_fieldsplit_schur_fact_type` `diag`.
2026*7f296bb3SBarry Smith      See `PCFieldSplitSetSchurScale()`
2027*7f296bb3SBarry Smith
2028*7f296bb3SBarry Smith    - `fieldsplit_1_xxx` controls the solver for the Schur complement system.
2029*7f296bb3SBarry Smith      If a `DM` provided the fields, use the second field name set in the `DM` instead of 1.
2030*7f296bb3SBarry Smith
2031*7f296bb3SBarry Smith      - `-fieldsplit_1_pc_type` `lsc` `-fieldsplit_1_lsc_pc_xxx` use
2032*7f296bb3SBarry Smith        the least squares commutators {cite}`elmanhowleshadidshuttleworthtuminaro2006` {cite}`silvester2001efficient`
2033*7f296bb3SBarry Smith        preconditioner for the Schur complement with any preconditioner for the least-squares matrix, see `PCLSC`.
2034*7f296bb3SBarry Smith        If a `DM` provided the fields, use the second field name set in the `DM` instead of 1.
2035*7f296bb3SBarry Smith
2036*7f296bb3SBarry Smith    - `-fieldsplit_upper_xxx` Set options for the solver in the upper solver when `-pc_fieldsplit_schur_fact_type`
2037*7f296bb3SBarry Smith      `upper` or `full` is used. Defaults to
2038*7f296bb3SBarry Smith      using the solver as provided with `-fieldsplit_0_xxx`.
2039*7f296bb3SBarry Smith
2040*7f296bb3SBarry Smith    - `-fieldsplit_1_inner_xxx` Set the options for the solver inside the application of the Schur complement;
2041*7f296bb3SBarry Smith      defaults to using the solver as provided with `-fieldsplit_0_xxx`. If a `DM` provides the fields use the name of the second field name set in the `DM` instead of 1.
2042*7f296bb3SBarry Smith
2043*7f296bb3SBarry Smith  - Options for GKB preconditioner: `-pc_fieldsplit_type` gkb
2044*7f296bb3SBarry Smith
2045*7f296bb3SBarry Smith    - `-pc_fieldsplit_gkb_tol` \<tol:real:1e-5> See `PCFieldSplitSetGKBTol()`.
2046*7f296bb3SBarry Smith    - `-pc_fieldsplit_gkb_delay` \<delay:int:5> See `PCFieldSplitSetGKBDelay()`.
2047*7f296bb3SBarry Smith    - `-pc_fieldsplit_gkb_nu` \<nu:real:1.0> See `PCFieldSplitSetGKBNu()`.
2048*7f296bb3SBarry Smith    - `-pc_fieldsplit_gkb_maxit` \<maxit:int:100> See `PCFieldSplitSetGKBMaxit()`.
2049*7f296bb3SBarry Smith    - `-pc_fieldsplit_gkb_monitor` \<bool:false> Monitor the convergence of the inner solver.
2050*7f296bb3SBarry Smith
2051*7f296bb3SBarry Smith- Options for additive and multiplication field solvers:
2052*7f296bb3SBarry Smith
2053*7f296bb3SBarry Smith  > - `-fieldsplit_%d_xxx` Set options for the solver for field number `d`. For example, `-fieldsplit_0_pc_type`
2054*7f296bb3SBarry Smith  >   `jacobi`. When the fields are obtained from a `DM` use the
2055*7f296bb3SBarry Smith  >   field name instead of `d`.
2056*7f296bb3SBarry Smith
2057*7f296bb3SBarry SmithFor simplicity, we restrict our matrices to two-by-two blocks in the rest of the section. So the matrix is
2058*7f296bb3SBarry Smith
2059*7f296bb3SBarry Smith$$
2060*7f296bb3SBarry Smith\left( \begin{array}{cc}
2061*7f296bb3SBarry SmithA_{00}   & A_{01} \\
2062*7f296bb3SBarry SmithA_{10}   & A_{11} \\
2063*7f296bb3SBarry Smith\end{array} \right).
2064*7f296bb3SBarry Smith$$
2065*7f296bb3SBarry Smith
2066*7f296bb3SBarry SmithOn occasion, the user may provide another matrix that is used to
2067*7f296bb3SBarry Smithconstruct parts of the preconditioner
2068*7f296bb3SBarry Smith
2069*7f296bb3SBarry Smith$$
2070*7f296bb3SBarry Smith\left( \begin{array}{cc}
2071*7f296bb3SBarry SmithAp_{00}   & Ap_{01} \\
2072*7f296bb3SBarry SmithAp_{10}   & Ap_{11} \\
2073*7f296bb3SBarry Smith\end{array} \right).
2074*7f296bb3SBarry Smith$$
2075*7f296bb3SBarry Smith
2076*7f296bb3SBarry SmithFor notational simplicity define $\text{ksp}(A,Ap)$ to mean
2077*7f296bb3SBarry Smithapproximately solving a linear system using `KSP` with the operator
2078*7f296bb3SBarry Smith$A$ and preconditioner built from matrix $Ap$.
2079*7f296bb3SBarry Smith
2080*7f296bb3SBarry SmithFor matrices defined with any number of blocks, there are three “block”
2081*7f296bb3SBarry Smithalgorithms available: block Jacobi,
2082*7f296bb3SBarry Smith
2083*7f296bb3SBarry Smith$$
2084*7f296bb3SBarry Smith\left( \begin{array}{cc}
2085*7f296bb3SBarry Smith  \text{ksp}(A_{00},Ap_{00})   & 0 \\
2086*7f296bb3SBarry Smith  0   & \text{ksp}(A_{11},Ap_{11}) \\
2087*7f296bb3SBarry Smith\end{array} \right)
2088*7f296bb3SBarry Smith$$
2089*7f296bb3SBarry Smith
2090*7f296bb3SBarry Smithblock Gauss-Seidel,
2091*7f296bb3SBarry Smith
2092*7f296bb3SBarry Smith$$
2093*7f296bb3SBarry Smith\left( \begin{array}{cc}
2094*7f296bb3SBarry SmithI   & 0 \\
2095*7f296bb3SBarry Smith0 & A^{-1}_{11} \\
2096*7f296bb3SBarry Smith\end{array} \right)
2097*7f296bb3SBarry Smith\left( \begin{array}{cc}
2098*7f296bb3SBarry SmithI   & 0 \\
2099*7f296bb3SBarry Smith-A_{10} & I \\
2100*7f296bb3SBarry Smith\end{array} \right)
2101*7f296bb3SBarry Smith\left( \begin{array}{cc}
2102*7f296bb3SBarry SmithA^{-1}_{00}   & 0 \\
2103*7f296bb3SBarry Smith0 & I \\
2104*7f296bb3SBarry Smith\end{array} \right)
2105*7f296bb3SBarry Smith$$
2106*7f296bb3SBarry Smith
2107*7f296bb3SBarry Smithwhich is implemented [^id4] as
2108*7f296bb3SBarry Smith
2109*7f296bb3SBarry Smith$$
2110*7f296bb3SBarry Smith\left( \begin{array}{cc}
2111*7f296bb3SBarry SmithI   & 0 \\
2112*7f296bb3SBarry Smith  0 & \text{ksp}(A_{11},Ap_{11}) \\
2113*7f296bb3SBarry Smith\end{array} \right)
2114*7f296bb3SBarry Smith$$
2115*7f296bb3SBarry Smith
2116*7f296bb3SBarry Smith$$
2117*7f296bb3SBarry Smith\left[
2118*7f296bb3SBarry Smith\left( \begin{array}{cc}
2119*7f296bb3SBarry Smith0   & 0 \\
2120*7f296bb3SBarry Smith0 & I \\
2121*7f296bb3SBarry Smith\end{array} \right) +
2122*7f296bb3SBarry Smith\left( \begin{array}{cc}
2123*7f296bb3SBarry SmithI   & 0 \\
2124*7f296bb3SBarry Smith-A_{10} & -A_{11} \\
2125*7f296bb3SBarry Smith\end{array} \right)
2126*7f296bb3SBarry Smith\left( \begin{array}{cc}
2127*7f296bb3SBarry SmithI   & 0 \\
2128*7f296bb3SBarry Smith0 & 0 \\
2129*7f296bb3SBarry Smith\end{array} \right)
2130*7f296bb3SBarry Smith\right]
2131*7f296bb3SBarry Smith$$
2132*7f296bb3SBarry Smith
2133*7f296bb3SBarry Smith$$
2134*7f296bb3SBarry Smith\left( \begin{array}{cc}
2135*7f296bb3SBarry Smith  \text{ksp}(A_{00},Ap_{00})   & 0 \\
2136*7f296bb3SBarry Smith0 & I \\
2137*7f296bb3SBarry Smith\end{array} \right)
2138*7f296bb3SBarry Smith$$
2139*7f296bb3SBarry Smith
2140*7f296bb3SBarry Smithand symmetric block Gauss-Seidel
2141*7f296bb3SBarry Smith
2142*7f296bb3SBarry Smith$$
2143*7f296bb3SBarry Smith\left( \begin{array}{cc}
2144*7f296bb3SBarry SmithA_{00}^{-1}   & 0 \\
2145*7f296bb3SBarry Smith0 & I \\
2146*7f296bb3SBarry Smith\end{array} \right)
2147*7f296bb3SBarry Smith\left( \begin{array}{cc}
2148*7f296bb3SBarry SmithI   & -A_{01} \\
2149*7f296bb3SBarry Smith0 & I \\
2150*7f296bb3SBarry Smith\end{array} \right)
2151*7f296bb3SBarry Smith\left( \begin{array}{cc}
2152*7f296bb3SBarry SmithA_{00}   & 0 \\
2153*7f296bb3SBarry Smith0 & A_{11}^{-1} \\
2154*7f296bb3SBarry Smith\end{array} \right)
2155*7f296bb3SBarry Smith\left( \begin{array}{cc}
2156*7f296bb3SBarry SmithI   & 0 \\
2157*7f296bb3SBarry Smith-A_{10} & I \\
2158*7f296bb3SBarry Smith\end{array} \right)
2159*7f296bb3SBarry Smith\left( \begin{array}{cc}
2160*7f296bb3SBarry SmithA_{00}^{-1}   & 0 \\
2161*7f296bb3SBarry Smith0 & I \\
2162*7f296bb3SBarry Smith\end{array} \right).
2163*7f296bb3SBarry Smith$$
2164*7f296bb3SBarry Smith
2165*7f296bb3SBarry SmithThese can be accessed with
2166*7f296bb3SBarry Smith`-pc_fieldsplit_type<additive,multiplicative,``symmetric_multiplicative>`
2167*7f296bb3SBarry Smithor the function `PCFieldSplitSetType()`. The option prefixes for the
2168*7f296bb3SBarry Smithinternal KSPs are given by `-fieldsplit_name_`.
2169*7f296bb3SBarry Smith
2170*7f296bb3SBarry SmithBy default blocks $A_{00}, A_{01}$ and so on are extracted out of
2171*7f296bb3SBarry Smith`Pmat`, the matrix that the `KSP` uses to build the preconditioner,
2172*7f296bb3SBarry Smithand not out of `Amat` (i.e., $A$ itself). As discussed above, in
2173*7f296bb3SBarry Smith{any}`sec_combining_pcs`, however, it is
2174*7f296bb3SBarry Smithpossible to use `Amat` instead of `Pmat` by calling
2175*7f296bb3SBarry Smith`PCSetUseAmat(pc)` or using `-pc_use_amat` on the command line.
2176*7f296bb3SBarry SmithAlternatively, you can have `PCFIELDSPLIT` extract the diagonal blocks
2177*7f296bb3SBarry Smith$A_{00}, A_{11}$ etc. out of `Amat` by calling
2178*7f296bb3SBarry Smith`PCFieldSplitSetDiagUseAmat(pc,PETSC_TRUE)` or supplying command-line
2179*7f296bb3SBarry Smithargument `-pc_fieldsplit_diag_use_amat`. Similarly,
2180*7f296bb3SBarry Smith`PCFieldSplitSetOffDiagUseAmat(pc,{PETSC_TRUE`) or
2181*7f296bb3SBarry Smith`-pc_fieldsplit_off_diag_use_amat` will cause the off-diagonal blocks
2182*7f296bb3SBarry Smith$A_{01},A_{10}$ etc. to be extracted out of `Amat`.
2183*7f296bb3SBarry Smith
2184*7f296bb3SBarry SmithFor two-by-two blocks only, there is another family of solvers based on
2185*7f296bb3SBarry SmithSchur complements. The inverse of the Schur complement factorization is
2186*7f296bb3SBarry Smith
2187*7f296bb3SBarry Smith$$
2188*7f296bb3SBarry Smith\left[
2189*7f296bb3SBarry Smith\left( \begin{array}{cc}
2190*7f296bb3SBarry SmithI   & 0 \\
2191*7f296bb3SBarry SmithA_{10}A_{00}^{-1} & I \\
2192*7f296bb3SBarry Smith\end{array} \right)
2193*7f296bb3SBarry Smith\left( \begin{array}{cc}
2194*7f296bb3SBarry SmithA_{00}  & 0 \\
2195*7f296bb3SBarry Smith0 & S \\
2196*7f296bb3SBarry Smith\end{array} \right)
2197*7f296bb3SBarry Smith\left( \begin{array}{cc}
2198*7f296bb3SBarry SmithI   & A_{00}^{-1} A_{01} \\
2199*7f296bb3SBarry Smith0 & I \\
2200*7f296bb3SBarry Smith\end{array} \right)
2201*7f296bb3SBarry Smith\right]^{-1} =
2202*7f296bb3SBarry Smith$$
2203*7f296bb3SBarry Smith
2204*7f296bb3SBarry Smith$$
2205*7f296bb3SBarry Smith\left( \begin{array}{cc}
2206*7f296bb3SBarry SmithI   & A_{00}^{-1} A_{01} \\
2207*7f296bb3SBarry Smith0 & I \\
2208*7f296bb3SBarry Smith\end{array} \right)^{-1}
2209*7f296bb3SBarry Smith\left( \begin{array}{cc}
2210*7f296bb3SBarry SmithA_{00}^{-1}  & 0 \\
2211*7f296bb3SBarry Smith0 & S^{-1} \\
2212*7f296bb3SBarry Smith\end{array} \right)
2213*7f296bb3SBarry Smith\left( \begin{array}{cc}
2214*7f296bb3SBarry SmithI   & 0 \\
2215*7f296bb3SBarry SmithA_{10}A_{00}^{-1} & I \\
2216*7f296bb3SBarry Smith\end{array} \right)^{-1} =
2217*7f296bb3SBarry Smith$$
2218*7f296bb3SBarry Smith
2219*7f296bb3SBarry Smith$$
2220*7f296bb3SBarry Smith\left( \begin{array}{cc}
2221*7f296bb3SBarry SmithI   & -A_{00}^{-1} A_{01} \\
2222*7f296bb3SBarry Smith0 & I \\
2223*7f296bb3SBarry Smith\end{array} \right)
2224*7f296bb3SBarry Smith\left( \begin{array}{cc}
2225*7f296bb3SBarry SmithA_{00}^{-1}  & 0 \\
2226*7f296bb3SBarry Smith0 & S^{-1} \\
2227*7f296bb3SBarry Smith\end{array} \right)
2228*7f296bb3SBarry Smith\left( \begin{array}{cc}
2229*7f296bb3SBarry SmithI   & 0 \\
2230*7f296bb3SBarry Smith-A_{10}A_{00}^{-1} & I \\
2231*7f296bb3SBarry Smith\end{array} \right) =
2232*7f296bb3SBarry Smith$$
2233*7f296bb3SBarry Smith
2234*7f296bb3SBarry Smith$$
2235*7f296bb3SBarry Smith\left( \begin{array}{cc}
2236*7f296bb3SBarry SmithA_{00}^{-1}   & 0 \\
2237*7f296bb3SBarry Smith0 & I \\
2238*7f296bb3SBarry Smith\end{array} \right)
2239*7f296bb3SBarry Smith\left( \begin{array}{cc}
2240*7f296bb3SBarry SmithI   & -A_{01} \\
2241*7f296bb3SBarry Smith0 & I \\
2242*7f296bb3SBarry Smith\end{array} \right)
2243*7f296bb3SBarry Smith\left( \begin{array}{cc}
2244*7f296bb3SBarry SmithA_{00}^{-1}  & 0 \\
2245*7f296bb3SBarry Smith0 & S^{-1} \\
2246*7f296bb3SBarry Smith\end{array} \right)
2247*7f296bb3SBarry Smith\left( \begin{array}{cc}
2248*7f296bb3SBarry SmithI   & 0 \\
2249*7f296bb3SBarry Smith-A_{10} & I \\
2250*7f296bb3SBarry Smith\end{array} \right)
2251*7f296bb3SBarry Smith\left( \begin{array}{cc}
2252*7f296bb3SBarry SmithA_{00}^{-1}   & 0 \\
2253*7f296bb3SBarry Smith0 & I \\
2254*7f296bb3SBarry Smith\end{array} \right).
2255*7f296bb3SBarry Smith$$
2256*7f296bb3SBarry Smith
2257*7f296bb3SBarry SmithThe preconditioner is accessed with `-pc_fieldsplit_type` `schur` and is
2258*7f296bb3SBarry Smithimplemented as
2259*7f296bb3SBarry Smith
2260*7f296bb3SBarry Smith$$
2261*7f296bb3SBarry Smith\left( \begin{array}{cc}
2262*7f296bb3SBarry Smith  \text{ksp}(A_{00},Ap_{00})   & 0 \\
2263*7f296bb3SBarry Smith0 & I \\
2264*7f296bb3SBarry Smith\end{array} \right)
2265*7f296bb3SBarry Smith\left( \begin{array}{cc}
2266*7f296bb3SBarry SmithI   & -A_{01} \\
2267*7f296bb3SBarry Smith0 & I \\
2268*7f296bb3SBarry Smith\end{array} \right)
2269*7f296bb3SBarry Smith$$
2270*7f296bb3SBarry Smith
2271*7f296bb3SBarry Smith$$
2272*7f296bb3SBarry Smith\left( \begin{array}{cc}
2273*7f296bb3SBarry SmithI  & 0 \\
2274*7f296bb3SBarry Smith  0 & \text{ksp}(\hat{S},\hat{S}p) \\
2275*7f296bb3SBarry Smith\end{array} \right)
2276*7f296bb3SBarry Smith\left( \begin{array}{cc}
2277*7f296bb3SBarry SmithI   & 0 \\
2278*7f296bb3SBarry Smith  -A_{10} \text{ksp}(A_{00},Ap_{00}) & I \\
2279*7f296bb3SBarry Smith\end{array} \right).
2280*7f296bb3SBarry Smith$$
2281*7f296bb3SBarry Smith
2282*7f296bb3SBarry SmithWhere
2283*7f296bb3SBarry Smith$\hat{S} = A_{11} - A_{10} \text{ksp}(A_{00},Ap_{00}) A_{01}$ is
2284*7f296bb3SBarry Smiththe approximate Schur complement.
2285*7f296bb3SBarry Smith
2286*7f296bb3SBarry SmithThere are several variants of the Schur complement preconditioner
2287*7f296bb3SBarry Smithobtained by dropping some of the terms; these can be obtained with
2288*7f296bb3SBarry Smith`-pc_fieldsplit_schur_fact_type <diag,lower,upper,full>` or the
2289*7f296bb3SBarry Smithfunction `PCFieldSplitSetSchurFactType()`. Note that the `diag` form
2290*7f296bb3SBarry Smithuses the preconditioner
2291*7f296bb3SBarry Smith
2292*7f296bb3SBarry Smith$$
2293*7f296bb3SBarry Smith\left( \begin{array}{cc}
2294*7f296bb3SBarry Smith  \text{ksp}(A_{00},Ap_{00})   & 0 \\
2295*7f296bb3SBarry Smith  0 & -\text{ksp}(\hat{S},\hat{S}p) \\
2296*7f296bb3SBarry Smith\end{array} \right).
2297*7f296bb3SBarry Smith$$
2298*7f296bb3SBarry Smith
2299*7f296bb3SBarry SmithThis is done to ensure the preconditioner is positive definite for a
2300*7f296bb3SBarry Smitha common class of problems, saddle points with a positive definite
2301*7f296bb3SBarry Smith$A_{00}$: for these, the Schur complement is negative definite.
2302*7f296bb3SBarry Smith
2303*7f296bb3SBarry SmithThe effectiveness of the Schur complement preconditioner depends on the
2304*7f296bb3SBarry Smithavailability of a good preconditioner $\hat Sp$ for the Schur
2305*7f296bb3SBarry Smithcomplement matrix. In general, you are responsible for supplying
2306*7f296bb3SBarry Smith$\hat Sp$ via
2307*7f296bb3SBarry Smith`PCFieldSplitSetSchurPre(pc,PC_FIELDSPLIT_SCHUR_PRE_USER,Sp)`.
2308*7f296bb3SBarry SmithWithout a good problem-specific $\hat Sp$, you can use
2309*7f296bb3SBarry Smithsome built-in options.
2310*7f296bb3SBarry Smith
2311*7f296bb3SBarry SmithUsing `-pc_fieldsplit_schur_precondition user` on the command line
2312*7f296bb3SBarry Smithactivates the matrix supplied programmatically, as explained above.
2313*7f296bb3SBarry Smith
2314*7f296bb3SBarry SmithWith `-pc_fieldsplit_schur_precondition a11` (default)
2315*7f296bb3SBarry Smith$\hat Sp = A_{11}$ is used to build a preconditioner for
2316*7f296bb3SBarry Smith$\hat S$.
2317*7f296bb3SBarry Smith
2318*7f296bb3SBarry SmithOtherwise, `-pc_fieldsplit_schur_precondition self` will set
2319*7f296bb3SBarry Smith$\hat Sp = \hat S$ and use the Schur complement matrix itself to
2320*7f296bb3SBarry Smithbuild the preconditioner.
2321*7f296bb3SBarry Smith
2322*7f296bb3SBarry SmithThe problem with the last approach is that $\hat S$ is used in
2323*7f296bb3SBarry Smiththe unassembled, matrix-free form, and many preconditioners (e.g., ILU)
2324*7f296bb3SBarry Smithcannot be built out of such matrices. Instead, you can *assemble* an
2325*7f296bb3SBarry Smithapproximation to $\hat S$ by inverting $A_{00}$, but only
2326*7f296bb3SBarry Smithapproximately, to ensure the sparsity of $\hat Sp$ as much
2327*7f296bb3SBarry Smithas possible. Specifically, using
2328*7f296bb3SBarry Smith`-pc_fieldsplit_schur_precondition selfp` will assemble
2329*7f296bb3SBarry Smith$\hat Sp = A_{11} - A_{10} \text{inv}(A_{00}) A_{01}$.
2330*7f296bb3SBarry Smith
2331*7f296bb3SBarry SmithBy default $\text{inv}(A_{00})$ is the inverse of the diagonal of
2332*7f296bb3SBarry Smith$A_{00}$, but using
2333*7f296bb3SBarry Smith`-fieldsplit_1_mat_schur_complement_ainv_type lump` will lump
2334*7f296bb3SBarry Smith$A_{00}$ first. Using
2335*7f296bb3SBarry Smith`-fieldsplit_1_mat_schur_complement_ainv_type blockdiag` will use the
2336*7f296bb3SBarry Smithinverse of the block diagonal of $A_{00}$. Option
2337*7f296bb3SBarry Smith`-mat_schur_complement_ainv_type` applies to any matrix of
2338*7f296bb3SBarry Smith`MatSchurComplement` type and here it is used with the prefix
2339*7f296bb3SBarry Smith`-fieldsplit_1` of the linear system in the second split.
2340*7f296bb3SBarry Smith
2341*7f296bb3SBarry SmithFinally, you can use the `PCLSC` preconditioner for the Schur
2342*7f296bb3SBarry Smithcomplement with `-pc_fieldsplit_type schur -fieldsplit_1_pc_type lsc`.
2343*7f296bb3SBarry SmithThis uses for the preconditioner to $\hat{S}$ the operator
2344*7f296bb3SBarry Smith
2345*7f296bb3SBarry Smith$$
2346*7f296bb3SBarry Smith\text{ksp}(A_{10} A_{01},A_{10} A_{01}) A_{10} A_{00} A_{01} \text{ksp}(A_{10} A_{01},A_{10} A_{01})
2347*7f296bb3SBarry Smith$$
2348*7f296bb3SBarry Smith
2349*7f296bb3SBarry SmithWhich, of course, introduces two additional inner solves for each
2350*7f296bb3SBarry Smithapplication of the Schur complement. The options prefix for this inner
2351*7f296bb3SBarry Smith`KSP` is `-fieldsplit_1_lsc_`. Instead of constructing the matrix
2352*7f296bb3SBarry Smith$A_{10} A_{01}$, users can provide their own matrix. This is
2353*7f296bb3SBarry Smithdone by attaching the matrix/matrices to the $Sp$ matrix they
2354*7f296bb3SBarry Smithprovide with
2355*7f296bb3SBarry Smith
2356*7f296bb3SBarry Smith```
2357*7f296bb3SBarry SmithPetscObjectCompose((PetscObject)Sp,"LSC_L",(PetscObject)L);
2358*7f296bb3SBarry SmithPetscObjectCompose((PetscObject)Sp,"LSC_Lp",(PetscObject)Lp);
2359*7f296bb3SBarry Smith```
2360*7f296bb3SBarry Smith
2361*7f296bb3SBarry Smith(sec_singular)=
2362*7f296bb3SBarry Smith
2363*7f296bb3SBarry Smith## Solving Singular Systems
2364*7f296bb3SBarry Smith
2365*7f296bb3SBarry SmithSometimes one is required to solver singular linear systems. In this
2366*7f296bb3SBarry Smithcase, the system matrix has a nontrivial null space. For example, the
2367*7f296bb3SBarry Smithdiscretization of the Laplacian operator with Neumann boundary
2368*7f296bb3SBarry Smithconditions has a null space of the constant functions. PETSc has tools
2369*7f296bb3SBarry Smithto help solve these systems. This approach is only guaranteed to work for left preconditioning (see `KSPSetPCSide()`); for example it
2370*7f296bb3SBarry Smithmay not work in some situations with `KSPFGMRES`.
2371*7f296bb3SBarry Smith
2372*7f296bb3SBarry SmithFirst, one must know what the null space is and store it using an
2373*7f296bb3SBarry Smithorthonormal basis in an array of PETSc Vecs. The constant functions can
2374*7f296bb3SBarry Smithbe handled separately, since they are such a common case. Create a
2375*7f296bb3SBarry Smith`MatNullSpace` object with the command
2376*7f296bb3SBarry Smith
2377*7f296bb3SBarry Smith```
2378*7f296bb3SBarry SmithMatNullSpaceCreate(MPI_Comm,PetscBool hasconstants,PetscInt dim,Vec *basis,MatNullSpace *nsp);
2379*7f296bb3SBarry Smith```
2380*7f296bb3SBarry Smith
2381*7f296bb3SBarry SmithHere, `dim` is the number of vectors in `basis` and `hasconstants`
2382*7f296bb3SBarry Smithindicates if the null space contains the constant functions. If the null
2383*7f296bb3SBarry Smithspace contains the constant functions you do not need to include it in
2384*7f296bb3SBarry Smiththe `basis` vectors you provide, nor in the count `dim`.
2385*7f296bb3SBarry Smith
2386*7f296bb3SBarry SmithOne then tells the `KSP` object you are using what the null space is
2387*7f296bb3SBarry Smithwith the call
2388*7f296bb3SBarry Smith
2389*7f296bb3SBarry Smith```
2390*7f296bb3SBarry SmithMatSetNullSpace(Mat Amat,MatNullSpace nsp);
2391*7f296bb3SBarry Smith```
2392*7f296bb3SBarry Smith
2393*7f296bb3SBarry SmithThe `Amat` should be the *first* matrix argument used with
2394*7f296bb3SBarry Smith`KSPSetOperators()`, `SNESSetJacobian()`, or `TSSetIJacobian()`.
2395*7f296bb3SBarry SmithThe PETSc solvers will now
2396*7f296bb3SBarry Smithhandle the null space during the solution process.
2397*7f296bb3SBarry Smith
2398*7f296bb3SBarry SmithIf the right-hand side of linear system is not in the range of `Amat`, that is it is not
2399*7f296bb3SBarry Smithorthogonal to the null space of `Amat` transpose, then the residual
2400*7f296bb3SBarry Smithnorm of the Krylov iteration will not converge to zero; it will converge to a non-zero value while the
2401*7f296bb3SBarry Smithsolution is converging to the least squares solution of the linear system. One can, if one desires,
2402*7f296bb3SBarry Smithapply `MatNullSpaceRemove()` with the null space of `Amat` transpose to the right-hand side before calling
2403*7f296bb3SBarry Smith`KSPSolve()`. Then the residual norm will converge to zero.
2404*7f296bb3SBarry Smith
2405*7f296bb3SBarry SmithIf one chooses a direct solver (or an incomplete factorization) it may
2406*7f296bb3SBarry Smithstill detect a zero pivot. You can run with the additional options or
2407*7f296bb3SBarry Smith`-pc_factor_shift_type NONZERO`
2408*7f296bb3SBarry Smith`-pc_factor_shift_amount  <dampingfactor>` to prevent the zero pivot.
2409*7f296bb3SBarry SmithA good choice for the `dampingfactor` is 1.e-10.
2410*7f296bb3SBarry Smith
2411*7f296bb3SBarry SmithIf the matrix is non-symmetric and you wish to solve the transposed linear system
2412*7f296bb3SBarry Smithyou must provide the null space of the transposed matrix with `MatSetTransposeNullSpace()`.
2413*7f296bb3SBarry Smith
2414*7f296bb3SBarry Smith(sec_externalsol)=
2415*7f296bb3SBarry Smith
2416*7f296bb3SBarry Smith## Using External Linear Solvers
2417*7f296bb3SBarry Smith
2418*7f296bb3SBarry SmithPETSc interfaces to several external linear solvers (also see {any}`acknowledgements`).
2419*7f296bb3SBarry SmithTo use these solvers, one may:
2420*7f296bb3SBarry Smith
2421*7f296bb3SBarry Smith1. Run `configure` with the additional options
2422*7f296bb3SBarry Smith   `--download-packagename` e.g. `--download-superlu_dist`
2423*7f296bb3SBarry Smith   `--download-parmetis` (SuperLU_DIST needs ParMetis) or
2424*7f296bb3SBarry Smith   `--download-mumps` `--download-scalapack` (MUMPS requires
2425*7f296bb3SBarry Smith   ScaLAPACK).
2426*7f296bb3SBarry Smith2. Build the PETSc libraries.
2427*7f296bb3SBarry Smith3. Use the runtime option: `-ksp_type preonly` (or equivalently `-ksp_type none`) `-pc_type <pctype>`
2428*7f296bb3SBarry Smith   `-pc_factor_mat_solver_type <packagename>`. For eg:
2429*7f296bb3SBarry Smith   `-ksp_type preonly` `-pc_type lu`
2430*7f296bb3SBarry Smith   `-pc_factor_mat_solver_type superlu_dist`.
2431*7f296bb3SBarry Smith
2432*7f296bb3SBarry Smith```{eval-rst}
2433*7f296bb3SBarry Smith.. list-table:: Options for External Solvers
2434*7f296bb3SBarry Smith   :name: tab-externaloptions
2435*7f296bb3SBarry Smith   :header-rows: 1
2436*7f296bb3SBarry Smith
2437*7f296bb3SBarry Smith   * - MatType
2438*7f296bb3SBarry Smith     - PCType
2439*7f296bb3SBarry Smith     - MatSolverType
2440*7f296bb3SBarry Smith     - Package
2441*7f296bb3SBarry Smith   * - ``seqaij``
2442*7f296bb3SBarry Smith     - ``lu``
2443*7f296bb3SBarry Smith     - ``MATSOLVERESSL``
2444*7f296bb3SBarry Smith     - ``essl``
2445*7f296bb3SBarry Smith   * - ``seqaij``
2446*7f296bb3SBarry Smith     - ``lu``
2447*7f296bb3SBarry Smith     - ``MATSOLVERLUSOL``
2448*7f296bb3SBarry Smith     -  ``lusol``
2449*7f296bb3SBarry Smith   * - ``seqaij``
2450*7f296bb3SBarry Smith     - ``lu``
2451*7f296bb3SBarry Smith     - ``MATSOLVERMATLAB``
2452*7f296bb3SBarry Smith     - ``matlab``
2453*7f296bb3SBarry Smith   * - ``aij``
2454*7f296bb3SBarry Smith     - ``lu``
2455*7f296bb3SBarry Smith     - ``MATSOLVERMUMPS``
2456*7f296bb3SBarry Smith     - ``mumps``
2457*7f296bb3SBarry Smith   * - ``aij``
2458*7f296bb3SBarry Smith     - ``cholesky``
2459*7f296bb3SBarry Smith     - -
2460*7f296bb3SBarry Smith     - -
2461*7f296bb3SBarry Smith   * - ``sbaij``
2462*7f296bb3SBarry Smith     - ``cholesky``
2463*7f296bb3SBarry Smith     - -
2464*7f296bb3SBarry Smith     - -
2465*7f296bb3SBarry Smith   * - ``seqaij``
2466*7f296bb3SBarry Smith     - ``lu``
2467*7f296bb3SBarry Smith     - ``MATSOLVERSUPERLU``
2468*7f296bb3SBarry Smith     - ``superlu``
2469*7f296bb3SBarry Smith   * - ``aij``
2470*7f296bb3SBarry Smith     - ``lu``
2471*7f296bb3SBarry Smith     - ``MATSOLVERSUPERLU_DIST``
2472*7f296bb3SBarry Smith     - ``superlu_dist``
2473*7f296bb3SBarry Smith   * - ``seqaij``
2474*7f296bb3SBarry Smith     - ``lu``
2475*7f296bb3SBarry Smith     - ``MATSOLVERUMFPACK``
2476*7f296bb3SBarry Smith     - ``umfpack``
2477*7f296bb3SBarry Smith   * - ``seqaij``
2478*7f296bb3SBarry Smith     - ``cholesky``
2479*7f296bb3SBarry Smith     - ``MATSOLVERCHOLMOD``
2480*7f296bb3SBarry Smith     - ``cholmod``
2481*7f296bb3SBarry Smith   * - ``seqaij``
2482*7f296bb3SBarry Smith     - ``lu``
2483*7f296bb3SBarry Smith     - ``MATSOLVERKLU``
2484*7f296bb3SBarry Smith     -  ``klu``
2485*7f296bb3SBarry Smith   * - ``dense``
2486*7f296bb3SBarry Smith     - ``lu``
2487*7f296bb3SBarry Smith     - ``MATSOLVERELEMENTAL``
2488*7f296bb3SBarry Smith     -  ``elemental``
2489*7f296bb3SBarry Smith   * - ``dense``
2490*7f296bb3SBarry Smith     - ``cholesky``
2491*7f296bb3SBarry Smith     - -
2492*7f296bb3SBarry Smith     - -
2493*7f296bb3SBarry Smith   * - ``seqaij``
2494*7f296bb3SBarry Smith     - ``lu``
2495*7f296bb3SBarry Smith     - ``MATSOLVERMKL_PARDISO``
2496*7f296bb3SBarry Smith     - ``mkl_pardiso``
2497*7f296bb3SBarry Smith   * - ``aij``
2498*7f296bb3SBarry Smith     - ``lu``
2499*7f296bb3SBarry Smith     - ``MATSOLVERMKL_CPARDISO``
2500*7f296bb3SBarry Smith     - ``mkl_cpardiso``
2501*7f296bb3SBarry Smith   * - ``aij``
2502*7f296bb3SBarry Smith     - ``lu``
2503*7f296bb3SBarry Smith     - ``MATSOLVERPASTIX``
2504*7f296bb3SBarry Smith     -  ``pastix``
2505*7f296bb3SBarry Smith   * - ``aij``
2506*7f296bb3SBarry Smith     - ``cholesky``
2507*7f296bb3SBarry Smith     - ``MATSOLVERBAS``
2508*7f296bb3SBarry Smith     -  ``bas``
2509*7f296bb3SBarry Smith   * - ``aijcusparse``
2510*7f296bb3SBarry Smith     - ``lu``
2511*7f296bb3SBarry Smith     - ``MATSOLVERCUSPARSE``
2512*7f296bb3SBarry Smith     - ``cusparse``
2513*7f296bb3SBarry Smith   * - ``aijcusparse``
2514*7f296bb3SBarry Smith     - ``cholesky``
2515*7f296bb3SBarry Smith     -  -
2516*7f296bb3SBarry Smith     -  -
2517*7f296bb3SBarry Smith   * - ``aij``
2518*7f296bb3SBarry Smith     - ``lu``, ``cholesky``
2519*7f296bb3SBarry Smith     - ``MATSOLVERPETSC``
2520*7f296bb3SBarry Smith     - ``petsc``
2521*7f296bb3SBarry Smith   * - ``baij``
2522*7f296bb3SBarry Smith     - -
2523*7f296bb3SBarry Smith     - -
2524*7f296bb3SBarry Smith     - -
2525*7f296bb3SBarry Smith   * - ``aijcrl``
2526*7f296bb3SBarry Smith     - -
2527*7f296bb3SBarry Smith     - -
2528*7f296bb3SBarry Smith     - -
2529*7f296bb3SBarry Smith   * - ``aijperm``
2530*7f296bb3SBarry Smith     - -
2531*7f296bb3SBarry Smith     - -
2532*7f296bb3SBarry Smith     - -
2533*7f296bb3SBarry Smith   * - ``seqdense``
2534*7f296bb3SBarry Smith     - -
2535*7f296bb3SBarry Smith     - -
2536*7f296bb3SBarry Smith     - -
2537*7f296bb3SBarry Smith   * - ``aij``
2538*7f296bb3SBarry Smith     - -
2539*7f296bb3SBarry Smith     - -
2540*7f296bb3SBarry Smith     - -
2541*7f296bb3SBarry Smith   * - ``baij``
2542*7f296bb3SBarry Smith     - -
2543*7f296bb3SBarry Smith     - -
2544*7f296bb3SBarry Smith     - -
2545*7f296bb3SBarry Smith   * - ``aijcrl``
2546*7f296bb3SBarry Smith     - -
2547*7f296bb3SBarry Smith     - -
2548*7f296bb3SBarry Smith     - -
2549*7f296bb3SBarry Smith   * - ``aijperm``
2550*7f296bb3SBarry Smith     - -
2551*7f296bb3SBarry Smith     - -
2552*7f296bb3SBarry Smith     - -
2553*7f296bb3SBarry Smith   * - ``seqdense``
2554*7f296bb3SBarry Smith     - -
2555*7f296bb3SBarry Smith     - -
2556*7f296bb3SBarry Smith     - -
2557*7f296bb3SBarry Smith```
2558*7f296bb3SBarry Smith
2559*7f296bb3SBarry SmithThe default and available input options for each external software can
2560*7f296bb3SBarry Smithbe found by specifying `-help` at runtime.
2561*7f296bb3SBarry Smith
2562*7f296bb3SBarry SmithAs an alternative to using runtime flags to employ these external
2563*7f296bb3SBarry Smithpackages, procedural calls are provided for some packages. For example,
2564*7f296bb3SBarry Smiththe following procedural calls are equivalent to runtime options
2565*7f296bb3SBarry Smith`-ksp_type preonly` (or equivalently `-ksp_type none`) `-pc_type lu`
2566*7f296bb3SBarry Smith`-pc_factor_mat_solver_type mumps` `-mat_mumps_icntl_7 3`:
2567*7f296bb3SBarry Smith
2568*7f296bb3SBarry Smith```
2569*7f296bb3SBarry SmithKSPSetType(ksp,KSPPREONLY); (or equivalently KSPSetType(ksp,KSPNONE))
2570*7f296bb3SBarry SmithKSPGetPC(ksp,&pc);
2571*7f296bb3SBarry SmithPCSetType(pc,PCLU);
2572*7f296bb3SBarry SmithPCFactorSetMatSolverType(pc,MATSOLVERMUMPS);
2573*7f296bb3SBarry SmithPCFactorSetUpMatSolverType(pc);
2574*7f296bb3SBarry SmithPCFactorGetMatrix(pc,&F);
2575*7f296bb3SBarry Smithicntl=7; ival = 3;
2576*7f296bb3SBarry SmithMatMumpsSetIcntl(F,icntl,ival);
2577*7f296bb3SBarry Smith```
2578*7f296bb3SBarry Smith
2579*7f296bb3SBarry SmithOne can also create matrices with the appropriate capabilities by
2580*7f296bb3SBarry Smithcalling `MatCreate()` followed by `MatSetType()` specifying the
2581*7f296bb3SBarry Smithdesired matrix type from {any}`tab-externaloptions`. These
2582*7f296bb3SBarry Smithmatrix types inherit capabilities from their PETSc matrix parents:
2583*7f296bb3SBarry Smith`MATSEQAIJ`, `MATMPIAIJ`, etc. As a result, the preallocation routines
2584*7f296bb3SBarry Smith`MatSeqAIJSetPreallocation()`, `MatMPIAIJSetPreallocation()`, etc.
2585*7f296bb3SBarry Smithand any other type specific routines of the base class are supported.
2586*7f296bb3SBarry SmithOne can also call `MatConvert()` inplace to convert the matrix to and
2587*7f296bb3SBarry Smithfrom its base class without performing an expensive data copy.
2588*7f296bb3SBarry Smith`MatConvert()` cannot be called on matrices that have already been
2589*7f296bb3SBarry Smithfactored.
2590*7f296bb3SBarry Smith
2591*7f296bb3SBarry SmithIn {any}`tab-externaloptions`, the base class `aij` refers
2592*7f296bb3SBarry Smithto the fact that inheritance is based on `MATSEQAIJ` when constructed
2593*7f296bb3SBarry Smithwith a single process communicator, and from `MATMPIAIJ` otherwise.
2594*7f296bb3SBarry SmithThe same holds for `baij` and `sbaij`. For codes that are intended
2595*7f296bb3SBarry Smithto be run as both a single process or with multiple processes, depending
2596*7f296bb3SBarry Smithon the `mpiexec` command, it is recommended that both sets of
2597*7f296bb3SBarry Smithpreallocation routines are called for these communicator morphing types.
2598*7f296bb3SBarry SmithThe call for the incorrect type will simply be ignored without any harm
2599*7f296bb3SBarry Smithor message.
2600*7f296bb3SBarry Smith
2601*7f296bb3SBarry Smith(sec_pcmpi)=
2602*7f296bb3SBarry Smith
2603*7f296bb3SBarry Smith## Using PETSc's MPI parallel linear solvers from a non-MPI program
2604*7f296bb3SBarry Smith
2605*7f296bb3SBarry SmithUsing PETSc's MPI linear solver server it is possible to use multiple MPI processes to solve a
2606*7f296bb3SBarry Smitha linear system when the application code, including the matrix generation, is run on a single
2607*7f296bb3SBarry SmithMPI process (with or without OpenMP). The application code must be built with MPI and must call
2608*7f296bb3SBarry Smith`PetscInitialize()` at the very beginning of the program and end with `PetscFinalize()`. The
2609*7f296bb3SBarry Smithapplication code may utilize OpenMP.
2610*7f296bb3SBarry SmithThe code may create multiple matrices and `KSP` objects and call `KSPSolve()`, similarly the
2611*7f296bb3SBarry Smithcode may utilize the `SNES` nonlinear solvers, the `TS` ODE integrators, and the `Tao` optimization algorithms
2612*7f296bb3SBarry Smithwhich use `KSP`.
2613*7f296bb3SBarry Smith
2614*7f296bb3SBarry SmithThe program must then be launched using the standard approaches for launching MPI programs with the additional
2615*7f296bb3SBarry SmithPETSc option `-mpi_linear_solver_server`. The linear solves are controlled via the options database in the usual manner (using any options prefix
2616*7f296bb3SBarry Smithyou may have provided via `KSPSetOptionsPrefix()`, for example `-ksp_type cg -ksp_monitor -pc_type bjacobi -ksp_view`. The solver options cannot be set via
2617*7f296bb3SBarry Smiththe functional interface, for example `KSPSetType()` etc.
2618*7f296bb3SBarry Smith
2619*7f296bb3SBarry SmithThe option `-mpi_linear_solver_server_view` will print
2620*7f296bb3SBarry Smitha summary of all the systems solved by the MPI linear solver server when the program completes. By default the linear solver server
2621*7f296bb3SBarry Smithwill only parallelize the linear solve to the extent that it believes is appropriate to obtain speedup for the parallel solve, for example, if the
2622*7f296bb3SBarry Smithmatrix has 1,000 rows and columns the solution will not be parallelized by default. One can use the option `-mpi_linear_solver_server_minimum_count_per_rank 5000`
2623*7f296bb3SBarry Smithto cause the linear solver server to allow as few as 5,000 unknowns per MPI process in the parallel solve.
2624*7f296bb3SBarry Smith
2625*7f296bb3SBarry SmithSee `PCMPI`, `PCMPIServerBegin()`, and `PCMPIServerEnd()` for more details on the solvers.
2626*7f296bb3SBarry Smith
2627*7f296bb3SBarry SmithFor help when anything goes wrong with the MPI linear solver server see `PCMPIServerBegin()`.
2628*7f296bb3SBarry Smith
2629*7f296bb3SBarry SmithAmdahl's law makes clear that parallelizing only a portion of a numerical code can only provide a limited improvement
2630*7f296bb3SBarry Smithin the computation time; thus it is crucial to understand what phases of a computation must be parallelized (via MPI, OpenMP, or some other model)
2631*7f296bb3SBarry Smithto ensure a useful increase in performance. One of the crucial phases is likely the generation of the matrix entries; the
2632*7f296bb3SBarry Smithuse of `MatSetPreallocationCOO()` and `MatSetValuesCOO()` in an OpenMP code allows parallelizing the generation of the matrix.
2633*7f296bb3SBarry Smith
2634*7f296bb3SBarry SmithSee {any}`sec_pcmpi_study` for a study of the use of `PCMPI` on a specific PETSc application.
2635*7f296bb3SBarry Smith
2636*7f296bb3SBarry Smith```{rubric} Footnotes
2637*7f296bb3SBarry Smith```
2638*7f296bb3SBarry Smith
2639*7f296bb3SBarry Smith[^id3]: See {any}`sec_amg` for information on using algebraic multigrid.
2640*7f296bb3SBarry Smith
2641*7f296bb3SBarry Smith[^id4]: This may seem an odd way to implement since it involves the "extra"
2642*7f296bb3SBarry Smith    multiply by $-A_{11}$. The reason is this is implemented this
2643*7f296bb3SBarry Smith    way is that this approach works for any number of blocks that may
2644*7f296bb3SBarry Smith    overlap.
2645*7f296bb3SBarry Smith
2646*7f296bb3SBarry Smith```{rubric} References
2647*7f296bb3SBarry Smith```
2648*7f296bb3SBarry Smith
2649*7f296bb3SBarry Smith```{eval-rst}
2650*7f296bb3SBarry Smith.. bibliography:: /petsc.bib
2651*7f296bb3SBarry Smith   :filter: docname in docnames
2652*7f296bb3SBarry Smith```
2653