xref: /petsc/src/ksp/pc/impls/hypre/hypre.c (revision f1a0fde416081eec0802fc3fe2b62de3a9ab880c) !
1 /*
2    Provides an interface to the LLNL package hypre
3 */
4 
5 #include <petscpkg_version.h>
6 #include <petsc/private/pcimpl.h> /*I "petscpc.h" I*/
7 /* this include is needed ONLY to allow access to the private data inside the Mat object specific to hypre */
8 #include <petsc/private/matimpl.h>
9 #include <petsc/private/vecimpl.h>
10 #include <../src/vec/vec/impls/hypre/vhyp.h>
11 #include <../src/mat/impls/hypre/mhypre.h>
12 #include <../src/dm/impls/da/hypre/mhyp.h>
13 #include <_hypre_parcsr_ls.h>
14 #include <petscmathypre.h>
15 
16 #if defined(PETSC_HAVE_HYPRE_DEVICE)
17   #include <petsc/private/deviceimpl.h>
18 #endif
19 
20 static PetscBool  cite            = PETSC_FALSE;
21 static const char hypreCitation[] = "@manual{hypre-web-page,\n  title  = {{\\sl hypre}: High Performance Preconditioners},\n  organization = {Lawrence Livermore National Laboratory},\n  note  = "
22                                     "{\\url{https://www.llnl.gov/casc/hypre}}\n}\n";
23 
24 /*
25    Private context (data structure) for the  preconditioner.
26 */
27 typedef struct {
28   HYPRE_Solver hsolver;
29   Mat          hpmat; /* MatHYPRE */
30 
31   HYPRE_Int (*destroy)(HYPRE_Solver);
32   HYPRE_Int (*solve)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
33   HYPRE_Int (*setup)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
34 
35   MPI_Comm comm_hypre;
36   char    *hypre_type;
37 
38   /* options for Pilut and BoomerAMG*/
39   PetscInt  maxiter;
40   PetscReal tol;
41 
42   /* options for Pilut */
43   PetscInt factorrowsize;
44 
45   /* options for ParaSails */
46   PetscInt  nlevels;
47   PetscReal threshold;
48   PetscReal filter;
49   PetscReal loadbal;
50   PetscInt  logging;
51   PetscInt  ruse;
52   PetscInt  symt;
53 
54   /* options for BoomerAMG */
55   PetscBool printstatistics;
56 
57   /* options for BoomerAMG */
58   PetscInt  cycletype;
59   PetscInt  maxlevels;
60   PetscReal strongthreshold;
61   PetscReal maxrowsum;
62   PetscInt  gridsweeps[3];
63   PetscInt  coarsentype;
64   PetscInt  measuretype;
65   PetscInt  smoothtype;
66   PetscInt  smoothnumlevels;
67   PetscInt  eu_level;         /* Number of levels for ILU(k) in Euclid */
68   PetscReal eu_droptolerance; /* Drop tolerance for ILU(k) in Euclid */
69   PetscInt  eu_bj;            /* Defines use of Block Jacobi ILU in Euclid */
70   PetscInt  relaxtype[3];
71   PetscReal relaxweight;
72   PetscReal outerrelaxweight;
73   PetscInt  relaxorder;
74   PetscReal truncfactor;
75   PetscBool applyrichardson;
76   PetscInt  pmax;
77   PetscInt  interptype;
78   PetscInt  maxc;
79   PetscInt  minc;
80 #if PETSC_PKG_HYPRE_VERSION_GE(2, 23, 0)
81   char *spgemm_type; // this is a global hypre parameter but is closely associated with BoomerAMG
82 #endif
83   /* GPU */
84   PetscBool keeptranspose;
85   PetscInt  rap2;
86   PetscInt  mod_rap2;
87 
88   /* AIR */
89   PetscInt  Rtype;
90   PetscReal Rstrongthreshold;
91   PetscReal Rfilterthreshold;
92   PetscInt  Adroptype;
93   PetscReal Adroptol;
94 
95   PetscInt  agg_nl;
96   PetscInt  agg_interptype;
97   PetscInt  agg_num_paths;
98   PetscBool nodal_relax;
99   PetscInt  nodal_relax_levels;
100 
101   PetscInt  nodal_coarsening;
102   PetscInt  nodal_coarsening_diag;
103   PetscInt  vec_interp_variant;
104   PetscInt  vec_interp_qmax;
105   PetscBool vec_interp_smooth;
106   PetscInt  interp_refine;
107 
108   /* NearNullSpace support */
109   VecHYPRE_IJVector *hmnull;
110   HYPRE_ParVector   *phmnull;
111   PetscInt           n_hmnull;
112   Vec                hmnull_constant;
113 
114   /* options for AS (Auxiliary Space preconditioners) */
115   PetscInt  as_print;
116   PetscInt  as_max_iter;
117   PetscReal as_tol;
118   PetscInt  as_relax_type;
119   PetscInt  as_relax_times;
120   PetscReal as_relax_weight;
121   PetscReal as_omega;
122   PetscInt  as_amg_alpha_opts[5]; /* AMG coarsen type, agg_levels, relax_type, interp_type, Pmax for vector Poisson (AMS) or Curl problem (ADS) */
123   PetscReal as_amg_alpha_theta;   /* AMG strength for vector Poisson (AMS) or Curl problem (ADS) */
124   PetscInt  as_amg_beta_opts[5];  /* AMG coarsen type, agg_levels, relax_type, interp_type, Pmax for scalar Poisson (AMS) or vector Poisson (ADS) */
125   PetscReal as_amg_beta_theta;    /* AMG strength for scalar Poisson (AMS) or vector Poisson (ADS)  */
126   PetscInt  ams_cycle_type;
127   PetscInt  ads_cycle_type;
128 
129   /* additional data */
130   Mat G;             /* MatHYPRE */
131   Mat C;             /* MatHYPRE */
132   Mat alpha_Poisson; /* MatHYPRE */
133   Mat beta_Poisson;  /* MatHYPRE */
134 
135   /* extra information for AMS */
136   PetscInt          dim; /* geometrical dimension */
137   VecHYPRE_IJVector coords[3];
138   VecHYPRE_IJVector constants[3];
139   VecHYPRE_IJVector interior;
140   Mat               RT_PiFull, RT_Pi[3];
141   Mat               ND_PiFull, ND_Pi[3];
142   PetscBool         ams_beta_is_zero;
143   PetscBool         ams_beta_is_zero_part;
144   PetscInt          ams_proj_freq;
145 } PC_HYPRE;
146 
147 PetscErrorCode PCHYPREGetSolver(PC pc, HYPRE_Solver *hsolver)
148 {
149   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
150 
151   PetscFunctionBegin;
152   *hsolver = jac->hsolver;
153   PetscFunctionReturn(PETSC_SUCCESS);
154 }
155 
156 /*
157   Matrices with AIJ format are created IN PLACE with using (I,J,data) from BoomerAMG. Since the data format in hypre_ParCSRMatrix
158   is different from that used in PETSc, the original hypre_ParCSRMatrix can not be used any more after call this routine.
159   It is used in PCHMG. Other users should avoid using this function.
160 */
161 static PetscErrorCode PCGetCoarseOperators_BoomerAMG(PC pc, PetscInt *nlevels, Mat *operators[])
162 {
163   PC_HYPRE            *jac  = (PC_HYPRE *)pc->data;
164   PetscBool            same = PETSC_FALSE;
165   PetscInt             num_levels, l;
166   Mat                 *mattmp;
167   hypre_ParCSRMatrix **A_array;
168 
169   PetscFunctionBegin;
170   PetscCall(PetscStrcmp(jac->hypre_type, "boomeramg", &same));
171   PetscCheck(same, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_NOTSAMETYPE, "Hypre type is not BoomerAMG ");
172   num_levels = hypre_ParAMGDataNumLevels((hypre_ParAMGData *)(jac->hsolver));
173   PetscCall(PetscMalloc1(num_levels, &mattmp));
174   A_array = hypre_ParAMGDataAArray((hypre_ParAMGData *)(jac->hsolver));
175   for (l = 1; l < num_levels; l++) {
176     PetscCall(MatCreateFromParCSR(A_array[l], MATAIJ, PETSC_OWN_POINTER, &(mattmp[num_levels - 1 - l])));
177     /* We want to own the data, and HYPRE can not touch this matrix any more */
178     A_array[l] = NULL;
179   }
180   *nlevels   = num_levels;
181   *operators = mattmp;
182   PetscFunctionReturn(PETSC_SUCCESS);
183 }
184 
185 /*
186   Matrices with AIJ format are created IN PLACE with using (I,J,data) from BoomerAMG. Since the data format in hypre_ParCSRMatrix
187   is different from that used in PETSc, the original hypre_ParCSRMatrix can not be used any more after call this routine.
188   It is used in PCHMG. Other users should avoid using this function.
189 */
190 static PetscErrorCode PCGetInterpolations_BoomerAMG(PC pc, PetscInt *nlevels, Mat *interpolations[])
191 {
192   PC_HYPRE            *jac  = (PC_HYPRE *)pc->data;
193   PetscBool            same = PETSC_FALSE;
194   PetscInt             num_levels, l;
195   Mat                 *mattmp;
196   hypre_ParCSRMatrix **P_array;
197 
198   PetscFunctionBegin;
199   PetscCall(PetscStrcmp(jac->hypre_type, "boomeramg", &same));
200   PetscCheck(same, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_NOTSAMETYPE, "Hypre type is not BoomerAMG ");
201   num_levels = hypre_ParAMGDataNumLevels((hypre_ParAMGData *)(jac->hsolver));
202   PetscCall(PetscMalloc1(num_levels, &mattmp));
203   P_array = hypre_ParAMGDataPArray((hypre_ParAMGData *)(jac->hsolver));
204   for (l = 1; l < num_levels; l++) {
205     PetscCall(MatCreateFromParCSR(P_array[num_levels - 1 - l], MATAIJ, PETSC_OWN_POINTER, &(mattmp[l - 1])));
206     /* We want to own the data, and HYPRE can not touch this matrix any more */
207     P_array[num_levels - 1 - l] = NULL;
208   }
209   *nlevels        = num_levels;
210   *interpolations = mattmp;
211   PetscFunctionReturn(PETSC_SUCCESS);
212 }
213 
214 /* Resets (frees) Hypre's representation of the near null space */
215 static PetscErrorCode PCHYPREResetNearNullSpace_Private(PC pc)
216 {
217   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
218   PetscInt  i;
219 
220   PetscFunctionBegin;
221   for (i = 0; i < jac->n_hmnull; i++) PetscCall(VecHYPRE_IJVectorDestroy(&jac->hmnull[i]));
222   PetscCall(PetscFree(jac->hmnull));
223   PetscCall(PetscFree(jac->phmnull));
224   PetscCall(VecDestroy(&jac->hmnull_constant));
225   jac->n_hmnull = 0;
226   PetscFunctionReturn(PETSC_SUCCESS);
227 }
228 
229 static PetscErrorCode PCSetUp_HYPRE(PC pc)
230 {
231   PC_HYPRE          *jac = (PC_HYPRE *)pc->data;
232   Mat_HYPRE         *hjac;
233   HYPRE_ParCSRMatrix hmat;
234   HYPRE_ParVector    bv, xv;
235   PetscBool          ishypre;
236 
237   PetscFunctionBegin;
238   if (!jac->hypre_type) PetscCall(PCHYPRESetType(pc, "boomeramg"));
239 
240   PetscCall(PetscObjectTypeCompare((PetscObject)pc->pmat, MATHYPRE, &ishypre));
241   if (!ishypre) {
242     PetscCall(MatDestroy(&jac->hpmat));
243     PetscCall(MatConvert(pc->pmat, MATHYPRE, MAT_INITIAL_MATRIX, &jac->hpmat));
244   } else {
245     PetscCall(PetscObjectReference((PetscObject)pc->pmat));
246     PetscCall(MatDestroy(&jac->hpmat));
247     jac->hpmat = pc->pmat;
248   }
249   /* allow debug */
250   PetscCall(MatViewFromOptions(jac->hpmat, NULL, "-pc_hypre_mat_view"));
251   hjac = (Mat_HYPRE *)(jac->hpmat->data);
252 
253   /* special case for BoomerAMG */
254   if (jac->setup == HYPRE_BoomerAMGSetup) {
255     MatNullSpace mnull;
256     PetscBool    has_const;
257     PetscInt     bs, nvec, i;
258     const Vec   *vecs;
259 
260     PetscCall(MatGetBlockSize(pc->pmat, &bs));
261     if (bs > 1) PetscCallExternal(HYPRE_BoomerAMGSetNumFunctions, jac->hsolver, bs);
262     PetscCall(MatGetNearNullSpace(pc->mat, &mnull));
263     if (mnull) {
264       PetscCall(PCHYPREResetNearNullSpace_Private(pc));
265       PetscCall(MatNullSpaceGetVecs(mnull, &has_const, &nvec, &vecs));
266       PetscCall(PetscMalloc1(nvec + 1, &jac->hmnull));
267       PetscCall(PetscMalloc1(nvec + 1, &jac->phmnull));
268       for (i = 0; i < nvec; i++) {
269         PetscCall(VecHYPRE_IJVectorCreate(vecs[i]->map, &jac->hmnull[i]));
270         PetscCall(VecHYPRE_IJVectorCopy(vecs[i], jac->hmnull[i]));
271         PetscCallExternal(HYPRE_IJVectorGetObject, jac->hmnull[i]->ij, (void **)&jac->phmnull[i]);
272       }
273       if (has_const) {
274         PetscCall(MatCreateVecs(pc->pmat, &jac->hmnull_constant, NULL));
275         PetscCall(VecSet(jac->hmnull_constant, 1));
276         PetscCall(VecNormalize(jac->hmnull_constant, NULL));
277         PetscCall(VecHYPRE_IJVectorCreate(jac->hmnull_constant->map, &jac->hmnull[nvec]));
278         PetscCall(VecHYPRE_IJVectorCopy(jac->hmnull_constant, jac->hmnull[nvec]));
279         PetscCallExternal(HYPRE_IJVectorGetObject, jac->hmnull[nvec]->ij, (void **)&jac->phmnull[nvec]);
280         nvec++;
281       }
282       PetscCallExternal(HYPRE_BoomerAMGSetInterpVectors, jac->hsolver, nvec, jac->phmnull);
283       jac->n_hmnull = nvec;
284     }
285   }
286 
287   /* special case for AMS */
288   if (jac->setup == HYPRE_AMSSetup) {
289     Mat_HYPRE         *hm;
290     HYPRE_ParCSRMatrix parcsr;
291     if (!jac->coords[0] && !jac->constants[0] && !(jac->ND_PiFull || (jac->ND_Pi[0] && jac->ND_Pi[1]))) {
292       SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "HYPRE AMS preconditioner needs either the coordinate vectors via PCSetCoordinates() or the edge constant vectors via PCHYPRESetEdgeConstantVectors() or the interpolation matrix via PCHYPRESetInterpolations()");
293     }
294     if (jac->dim) PetscCallExternal(HYPRE_AMSSetDimension, jac->hsolver, jac->dim);
295     if (jac->constants[0]) {
296       HYPRE_ParVector ozz, zoz, zzo = NULL;
297       PetscCallExternal(HYPRE_IJVectorGetObject, jac->constants[0]->ij, (void **)(&ozz));
298       PetscCallExternal(HYPRE_IJVectorGetObject, jac->constants[1]->ij, (void **)(&zoz));
299       if (jac->constants[2]) PetscCallExternal(HYPRE_IJVectorGetObject, jac->constants[2]->ij, (void **)(&zzo));
300       PetscCallExternal(HYPRE_AMSSetEdgeConstantVectors, jac->hsolver, ozz, zoz, zzo);
301     }
302     if (jac->coords[0]) {
303       HYPRE_ParVector coords[3];
304       coords[0] = NULL;
305       coords[1] = NULL;
306       coords[2] = NULL;
307       if (jac->coords[0]) PetscCallExternal(HYPRE_IJVectorGetObject, jac->coords[0]->ij, (void **)(&coords[0]));
308       if (jac->coords[1]) PetscCallExternal(HYPRE_IJVectorGetObject, jac->coords[1]->ij, (void **)(&coords[1]));
309       if (jac->coords[2]) PetscCallExternal(HYPRE_IJVectorGetObject, jac->coords[2]->ij, (void **)(&coords[2]));
310       PetscCallExternal(HYPRE_AMSSetCoordinateVectors, jac->hsolver, coords[0], coords[1], coords[2]);
311     }
312     PetscCheck(jac->G, PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "HYPRE AMS preconditioner needs the discrete gradient operator via PCHYPRESetDiscreteGradient");
313     hm = (Mat_HYPRE *)(jac->G->data);
314     PetscCallExternal(HYPRE_IJMatrixGetObject, hm->ij, (void **)(&parcsr));
315     PetscCallExternal(HYPRE_AMSSetDiscreteGradient, jac->hsolver, parcsr);
316     if (jac->alpha_Poisson) {
317       hm = (Mat_HYPRE *)(jac->alpha_Poisson->data);
318       PetscCallExternal(HYPRE_IJMatrixGetObject, hm->ij, (void **)(&parcsr));
319       PetscCallExternal(HYPRE_AMSSetAlphaPoissonMatrix, jac->hsolver, parcsr);
320     }
321     if (jac->ams_beta_is_zero) {
322       PetscCallExternal(HYPRE_AMSSetBetaPoissonMatrix, jac->hsolver, NULL);
323     } else if (jac->beta_Poisson) {
324       hm = (Mat_HYPRE *)(jac->beta_Poisson->data);
325       PetscCallExternal(HYPRE_IJMatrixGetObject, hm->ij, (void **)(&parcsr));
326       PetscCallExternal(HYPRE_AMSSetBetaPoissonMatrix, jac->hsolver, parcsr);
327     } else if (jac->ams_beta_is_zero_part) {
328       if (jac->interior) {
329         HYPRE_ParVector interior = NULL;
330         PetscCallExternal(HYPRE_IJVectorGetObject, jac->interior->ij, (void **)(&interior));
331         PetscCallExternal(HYPRE_AMSSetInteriorNodes, jac->hsolver, interior);
332       } else {
333         jac->ams_beta_is_zero_part = PETSC_FALSE;
334       }
335     }
336     if (jac->ND_PiFull || (jac->ND_Pi[0] && jac->ND_Pi[1])) {
337       PetscInt           i;
338       HYPRE_ParCSRMatrix nd_parcsrfull, nd_parcsr[3];
339       if (jac->ND_PiFull) {
340         hm = (Mat_HYPRE *)(jac->ND_PiFull->data);
341         PetscCallExternal(HYPRE_IJMatrixGetObject, hm->ij, (void **)(&nd_parcsrfull));
342       } else {
343         nd_parcsrfull = NULL;
344       }
345       for (i = 0; i < 3; ++i) {
346         if (jac->ND_Pi[i]) {
347           hm = (Mat_HYPRE *)(jac->ND_Pi[i]->data);
348           PetscCallExternal(HYPRE_IJMatrixGetObject, hm->ij, (void **)(&nd_parcsr[i]));
349         } else {
350           nd_parcsr[i] = NULL;
351         }
352       }
353       PetscCallExternal(HYPRE_AMSSetInterpolations, jac->hsolver, nd_parcsrfull, nd_parcsr[0], nd_parcsr[1], nd_parcsr[2]);
354     }
355   }
356   /* special case for ADS */
357   if (jac->setup == HYPRE_ADSSetup) {
358     Mat_HYPRE         *hm;
359     HYPRE_ParCSRMatrix parcsr;
360     if (!jac->coords[0] && !((jac->RT_PiFull || (jac->RT_Pi[0] && jac->RT_Pi[1])) && (jac->ND_PiFull || (jac->ND_Pi[0] && jac->ND_Pi[1])))) {
361       SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "HYPRE ADS preconditioner needs either the coordinate vectors via PCSetCoordinates() or the interpolation matrices via PCHYPRESetInterpolations");
362     } else PetscCheck(jac->coords[1] && jac->coords[2], PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "HYPRE ADS preconditioner has been designed for three dimensional problems! For two dimensional problems, use HYPRE AMS instead");
363     PetscCheck(jac->G, PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "HYPRE ADS preconditioner needs the discrete gradient operator via PCHYPRESetDiscreteGradient");
364     PetscCheck(jac->C, PetscObjectComm((PetscObject)pc), PETSC_ERR_USER, "HYPRE ADS preconditioner needs the discrete curl operator via PCHYPRESetDiscreteGradient");
365     if (jac->coords[0]) {
366       HYPRE_ParVector coords[3];
367       coords[0] = NULL;
368       coords[1] = NULL;
369       coords[2] = NULL;
370       if (jac->coords[0]) PetscCallExternal(HYPRE_IJVectorGetObject, jac->coords[0]->ij, (void **)(&coords[0]));
371       if (jac->coords[1]) PetscCallExternal(HYPRE_IJVectorGetObject, jac->coords[1]->ij, (void **)(&coords[1]));
372       if (jac->coords[2]) PetscCallExternal(HYPRE_IJVectorGetObject, jac->coords[2]->ij, (void **)(&coords[2]));
373       PetscCallExternal(HYPRE_ADSSetCoordinateVectors, jac->hsolver, coords[0], coords[1], coords[2]);
374     }
375     hm = (Mat_HYPRE *)(jac->G->data);
376     PetscCallExternal(HYPRE_IJMatrixGetObject, hm->ij, (void **)(&parcsr));
377     PetscCallExternal(HYPRE_ADSSetDiscreteGradient, jac->hsolver, parcsr);
378     hm = (Mat_HYPRE *)(jac->C->data);
379     PetscCallExternal(HYPRE_IJMatrixGetObject, hm->ij, (void **)(&parcsr));
380     PetscCallExternal(HYPRE_ADSSetDiscreteCurl, jac->hsolver, parcsr);
381     if ((jac->RT_PiFull || (jac->RT_Pi[0] && jac->RT_Pi[1])) && (jac->ND_PiFull || (jac->ND_Pi[0] && jac->ND_Pi[1]))) {
382       PetscInt           i;
383       HYPRE_ParCSRMatrix rt_parcsrfull, rt_parcsr[3];
384       HYPRE_ParCSRMatrix nd_parcsrfull, nd_parcsr[3];
385       if (jac->RT_PiFull) {
386         hm = (Mat_HYPRE *)(jac->RT_PiFull->data);
387         PetscCallExternal(HYPRE_IJMatrixGetObject, hm->ij, (void **)(&rt_parcsrfull));
388       } else {
389         rt_parcsrfull = NULL;
390       }
391       for (i = 0; i < 3; ++i) {
392         if (jac->RT_Pi[i]) {
393           hm = (Mat_HYPRE *)(jac->RT_Pi[i]->data);
394           PetscCallExternal(HYPRE_IJMatrixGetObject, hm->ij, (void **)(&rt_parcsr[i]));
395         } else {
396           rt_parcsr[i] = NULL;
397         }
398       }
399       if (jac->ND_PiFull) {
400         hm = (Mat_HYPRE *)(jac->ND_PiFull->data);
401         PetscCallExternal(HYPRE_IJMatrixGetObject, hm->ij, (void **)(&nd_parcsrfull));
402       } else {
403         nd_parcsrfull = NULL;
404       }
405       for (i = 0; i < 3; ++i) {
406         if (jac->ND_Pi[i]) {
407           hm = (Mat_HYPRE *)(jac->ND_Pi[i]->data);
408           PetscCallExternal(HYPRE_IJMatrixGetObject, hm->ij, (void **)(&nd_parcsr[i]));
409         } else {
410           nd_parcsr[i] = NULL;
411         }
412       }
413       PetscCallExternal(HYPRE_ADSSetInterpolations, jac->hsolver, rt_parcsrfull, rt_parcsr[0], rt_parcsr[1], rt_parcsr[2], nd_parcsrfull, nd_parcsr[0], nd_parcsr[1], nd_parcsr[2]);
414     }
415   }
416   PetscCallExternal(HYPRE_IJMatrixGetObject, hjac->ij, (void **)&hmat);
417   PetscCallExternal(HYPRE_IJVectorGetObject, hjac->b->ij, (void **)&bv);
418   PetscCallExternal(HYPRE_IJVectorGetObject, hjac->x->ij, (void **)&xv);
419   PetscCallExternal(jac->setup, jac->hsolver, hmat, bv, xv);
420   PetscFunctionReturn(PETSC_SUCCESS);
421 }
422 
423 static PetscErrorCode PCApply_HYPRE(PC pc, Vec b, Vec x)
424 {
425   PC_HYPRE          *jac  = (PC_HYPRE *)pc->data;
426   Mat_HYPRE         *hjac = (Mat_HYPRE *)(jac->hpmat->data);
427   HYPRE_ParCSRMatrix hmat;
428   HYPRE_ParVector    jbv, jxv;
429 
430   PetscFunctionBegin;
431   PetscCall(PetscCitationsRegister(hypreCitation, &cite));
432   if (!jac->applyrichardson) PetscCall(VecSet(x, 0.0));
433   PetscCall(VecHYPRE_IJVectorPushVecRead(hjac->b, b));
434   if (jac->applyrichardson) PetscCall(VecHYPRE_IJVectorPushVec(hjac->x, x));
435   else PetscCall(VecHYPRE_IJVectorPushVecWrite(hjac->x, x));
436   PetscCallExternal(HYPRE_IJMatrixGetObject, hjac->ij, (void **)&hmat);
437   PetscCallExternal(HYPRE_IJVectorGetObject, hjac->b->ij, (void **)&jbv);
438   PetscCallExternal(HYPRE_IJVectorGetObject, hjac->x->ij, (void **)&jxv);
439   PetscStackCallExternalVoid(
440     "Hypre solve", do {
441       HYPRE_Int hierr = (*jac->solve)(jac->hsolver, hmat, jbv, jxv);
442       if (hierr) {
443         PetscCheck(hierr == HYPRE_ERROR_CONV, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in HYPRE solver, error code %d", (int)hierr);
444         hypre__global_error = 0;
445       }
446     } while (0));
447 
448   if (jac->setup == HYPRE_AMSSetup && jac->ams_beta_is_zero_part) PetscCallExternal(HYPRE_AMSProjectOutGradients, jac->hsolver, jxv);
449   PetscCall(VecHYPRE_IJVectorPopVec(hjac->x));
450   PetscCall(VecHYPRE_IJVectorPopVec(hjac->b));
451   PetscFunctionReturn(PETSC_SUCCESS);
452 }
453 
454 static PetscErrorCode PCReset_HYPRE(PC pc)
455 {
456   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
457 
458   PetscFunctionBegin;
459   PetscCall(MatDestroy(&jac->hpmat));
460   PetscCall(MatDestroy(&jac->G));
461   PetscCall(MatDestroy(&jac->C));
462   PetscCall(MatDestroy(&jac->alpha_Poisson));
463   PetscCall(MatDestroy(&jac->beta_Poisson));
464   PetscCall(MatDestroy(&jac->RT_PiFull));
465   PetscCall(MatDestroy(&jac->RT_Pi[0]));
466   PetscCall(MatDestroy(&jac->RT_Pi[1]));
467   PetscCall(MatDestroy(&jac->RT_Pi[2]));
468   PetscCall(MatDestroy(&jac->ND_PiFull));
469   PetscCall(MatDestroy(&jac->ND_Pi[0]));
470   PetscCall(MatDestroy(&jac->ND_Pi[1]));
471   PetscCall(MatDestroy(&jac->ND_Pi[2]));
472   PetscCall(VecHYPRE_IJVectorDestroy(&jac->coords[0]));
473   PetscCall(VecHYPRE_IJVectorDestroy(&jac->coords[1]));
474   PetscCall(VecHYPRE_IJVectorDestroy(&jac->coords[2]));
475   PetscCall(VecHYPRE_IJVectorDestroy(&jac->constants[0]));
476   PetscCall(VecHYPRE_IJVectorDestroy(&jac->constants[1]));
477   PetscCall(VecHYPRE_IJVectorDestroy(&jac->constants[2]));
478   PetscCall(VecHYPRE_IJVectorDestroy(&jac->interior));
479   PetscCall(PCHYPREResetNearNullSpace_Private(pc));
480   jac->ams_beta_is_zero      = PETSC_FALSE;
481   jac->ams_beta_is_zero_part = PETSC_FALSE;
482   jac->dim                   = 0;
483   PetscFunctionReturn(PETSC_SUCCESS);
484 }
485 
486 static PetscErrorCode PCDestroy_HYPRE(PC pc)
487 {
488   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
489 
490   PetscFunctionBegin;
491   PetscCall(PCReset_HYPRE(pc));
492   if (jac->destroy) PetscCallExternal(jac->destroy, jac->hsolver);
493   PetscCall(PetscFree(jac->hypre_type));
494 #if PETSC_PKG_HYPRE_VERSION_GE(2, 23, 0)
495   PetscCall(PetscFree(jac->spgemm_type));
496 #endif
497   if (jac->comm_hypre != MPI_COMM_NULL) PetscCall(PetscCommRestoreComm(PetscObjectComm((PetscObject)pc), &jac->comm_hypre));
498   PetscCall(PetscFree(pc->data));
499 
500   PetscCall(PetscObjectChangeTypeName((PetscObject)pc, 0));
501   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetType_C", NULL));
502   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPREGetType_C", NULL));
503   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetDiscreteGradient_C", NULL));
504   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetDiscreteCurl_C", NULL));
505   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetInterpolations_C", NULL));
506   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetConstantEdgeVectors_C", NULL));
507   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetPoissonMatrix_C", NULL));
508   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetEdgeConstantVectors_C", NULL));
509   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPREAMSSetInteriorNodes_C", NULL));
510   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGetInterpolations_C", NULL));
511   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGetCoarseOperators_C", NULL));
512   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGalerkinSetMatProductAlgorithm_C", NULL));
513   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGalerkinGetMatProductAlgorithm_C", NULL));
514   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCSetCoordinates_C", NULL));
515   PetscFunctionReturn(PETSC_SUCCESS);
516 }
517 
518 static PetscErrorCode PCSetFromOptions_HYPRE_Pilut(PC pc, PetscOptionItems *PetscOptionsObject)
519 {
520   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
521   PetscBool flag;
522 
523   PetscFunctionBegin;
524   PetscOptionsHeadBegin(PetscOptionsObject, "HYPRE Pilut Options");
525   PetscCall(PetscOptionsInt("-pc_hypre_pilut_maxiter", "Number of iterations", "None", jac->maxiter, &jac->maxiter, &flag));
526   if (flag) PetscCallExternal(HYPRE_ParCSRPilutSetMaxIter, jac->hsolver, jac->maxiter);
527   PetscCall(PetscOptionsReal("-pc_hypre_pilut_tol", "Drop tolerance", "None", jac->tol, &jac->tol, &flag));
528   if (flag) PetscCallExternal(HYPRE_ParCSRPilutSetDropTolerance, jac->hsolver, jac->tol);
529   PetscCall(PetscOptionsInt("-pc_hypre_pilut_factorrowsize", "FactorRowSize", "None", jac->factorrowsize, &jac->factorrowsize, &flag));
530   if (flag) PetscCallExternal(HYPRE_ParCSRPilutSetFactorRowSize, jac->hsolver, jac->factorrowsize);
531   PetscOptionsHeadEnd();
532   PetscFunctionReturn(PETSC_SUCCESS);
533 }
534 
535 static PetscErrorCode PCView_HYPRE_Pilut(PC pc, PetscViewer viewer)
536 {
537   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
538   PetscBool iascii;
539 
540   PetscFunctionBegin;
541   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
542   if (iascii) {
543     PetscCall(PetscViewerASCIIPrintf(viewer, "  HYPRE Pilut preconditioning\n"));
544     if (jac->maxiter != PETSC_DEFAULT) {
545       PetscCall(PetscViewerASCIIPrintf(viewer, "    maximum number of iterations %" PetscInt_FMT "\n", jac->maxiter));
546     } else {
547       PetscCall(PetscViewerASCIIPrintf(viewer, "    default maximum number of iterations \n"));
548     }
549     if (jac->tol != PETSC_DEFAULT) {
550       PetscCall(PetscViewerASCIIPrintf(viewer, "    drop tolerance %g\n", (double)jac->tol));
551     } else {
552       PetscCall(PetscViewerASCIIPrintf(viewer, "    default drop tolerance \n"));
553     }
554     if (jac->factorrowsize != PETSC_DEFAULT) {
555       PetscCall(PetscViewerASCIIPrintf(viewer, "    factor row size %" PetscInt_FMT "\n", jac->factorrowsize));
556     } else {
557       PetscCall(PetscViewerASCIIPrintf(viewer, "    default factor row size \n"));
558     }
559   }
560   PetscFunctionReturn(PETSC_SUCCESS);
561 }
562 
563 static PetscErrorCode PCSetFromOptions_HYPRE_Euclid(PC pc, PetscOptionItems *PetscOptionsObject)
564 {
565   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
566   PetscBool flag, eu_bj = jac->eu_bj ? PETSC_TRUE : PETSC_FALSE;
567 
568   PetscFunctionBegin;
569   PetscOptionsHeadBegin(PetscOptionsObject, "HYPRE Euclid Options");
570   PetscCall(PetscOptionsInt("-pc_hypre_euclid_level", "Factorization levels", "None", jac->eu_level, &jac->eu_level, &flag));
571   if (flag) PetscCallExternal(HYPRE_EuclidSetLevel, jac->hsolver, jac->eu_level);
572 
573   PetscCall(PetscOptionsReal("-pc_hypre_euclid_droptolerance", "Drop tolerance for ILU(k) in Euclid", "None", jac->eu_droptolerance, &jac->eu_droptolerance, &flag));
574   if (flag) {
575     PetscMPIInt size;
576 
577     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pc), &size));
578     PetscCheck(size == 1, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "hypre's Euclid does not support a parallel drop tolerance");
579     PetscCallExternal(HYPRE_EuclidSetILUT, jac->hsolver, jac->eu_droptolerance);
580   }
581 
582   PetscCall(PetscOptionsBool("-pc_hypre_euclid_bj", "Use Block Jacobi for ILU in Euclid", "None", eu_bj, &eu_bj, &flag));
583   if (flag) {
584     jac->eu_bj = eu_bj ? 1 : 0;
585     PetscCallExternal(HYPRE_EuclidSetBJ, jac->hsolver, jac->eu_bj);
586   }
587   PetscOptionsHeadEnd();
588   PetscFunctionReturn(PETSC_SUCCESS);
589 }
590 
591 static PetscErrorCode PCView_HYPRE_Euclid(PC pc, PetscViewer viewer)
592 {
593   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
594   PetscBool iascii;
595 
596   PetscFunctionBegin;
597   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
598   if (iascii) {
599     PetscCall(PetscViewerASCIIPrintf(viewer, "  HYPRE Euclid preconditioning\n"));
600     if (jac->eu_level != PETSC_DEFAULT) {
601       PetscCall(PetscViewerASCIIPrintf(viewer, "    factorization levels %" PetscInt_FMT "\n", jac->eu_level));
602     } else {
603       PetscCall(PetscViewerASCIIPrintf(viewer, "    default factorization levels \n"));
604     }
605     PetscCall(PetscViewerASCIIPrintf(viewer, "    drop tolerance %g\n", (double)jac->eu_droptolerance));
606     PetscCall(PetscViewerASCIIPrintf(viewer, "    use Block-Jacobi? %" PetscInt_FMT "\n", jac->eu_bj));
607   }
608   PetscFunctionReturn(PETSC_SUCCESS);
609 }
610 
611 static PetscErrorCode PCApplyTranspose_HYPRE_BoomerAMG(PC pc, Vec b, Vec x)
612 {
613   PC_HYPRE          *jac  = (PC_HYPRE *)pc->data;
614   Mat_HYPRE         *hjac = (Mat_HYPRE *)(jac->hpmat->data);
615   HYPRE_ParCSRMatrix hmat;
616   HYPRE_ParVector    jbv, jxv;
617 
618   PetscFunctionBegin;
619   PetscCall(PetscCitationsRegister(hypreCitation, &cite));
620   PetscCall(VecSet(x, 0.0));
621   PetscCall(VecHYPRE_IJVectorPushVecRead(hjac->x, b));
622   PetscCall(VecHYPRE_IJVectorPushVecWrite(hjac->b, x));
623 
624   PetscCallExternal(HYPRE_IJMatrixGetObject, hjac->ij, (void **)&hmat);
625   PetscCallExternal(HYPRE_IJVectorGetObject, hjac->b->ij, (void **)&jbv);
626   PetscCallExternal(HYPRE_IJVectorGetObject, hjac->x->ij, (void **)&jxv);
627 
628   PetscStackCallExternalVoid(
629     "Hypre Transpose solve", do {
630       HYPRE_Int hierr = HYPRE_BoomerAMGSolveT(jac->hsolver, hmat, jbv, jxv);
631       if (hierr) {
632         /* error code of 1 in BoomerAMG merely means convergence not achieved */
633         PetscCheck(hierr == 1, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in HYPRE solver, error code %d", (int)hierr);
634         hypre__global_error = 0;
635       }
636     } while (0));
637 
638   PetscCall(VecHYPRE_IJVectorPopVec(hjac->x));
639   PetscCall(VecHYPRE_IJVectorPopVec(hjac->b));
640   PetscFunctionReturn(PETSC_SUCCESS);
641 }
642 
643 static PetscErrorCode PCMGGalerkinSetMatProductAlgorithm_HYPRE_BoomerAMG(PC pc, const char name[])
644 {
645   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
646   PetscBool flag;
647 
648 #if PETSC_PKG_HYPRE_VERSION_GE(2, 23, 0)
649   PetscFunctionBegin;
650   if (jac->spgemm_type) {
651     PetscCall(PetscStrcmp(jac->spgemm_type, name, &flag));
652     PetscCheck(flag, PetscObjectComm((PetscObject)pc), PETSC_ERR_ORDER, "Cannot reset the HYPRE SpGEMM (really we can)");
653     PetscFunctionReturn(PETSC_SUCCESS);
654   } else {
655     PetscCall(PetscStrallocpy(name, &jac->spgemm_type));
656   }
657   PetscCall(PetscStrcmp("cusparse", jac->spgemm_type, &flag));
658   if (flag) {
659     PetscCallExternal(HYPRE_SetSpGemmUseCusparse, 1);
660     PetscFunctionReturn(PETSC_SUCCESS);
661   }
662   PetscCall(PetscStrcmp("hypre", jac->spgemm_type, &flag));
663   if (flag) {
664     PetscCallExternal(HYPRE_SetSpGemmUseCusparse, 0);
665     PetscFunctionReturn(PETSC_SUCCESS);
666   }
667   jac->spgemm_type = NULL;
668   SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown HYPRE SpGEM type %s; Choices are cusparse, hypre", name);
669 #endif
670 }
671 
672 static PetscErrorCode PCMGGalerkinGetMatProductAlgorithm_HYPRE_BoomerAMG(PC pc, const char *spgemm[])
673 {
674   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
675 
676   PetscFunctionBegin;
677   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
678 #if PETSC_PKG_HYPRE_VERSION_GE(2, 23, 0)
679   *spgemm = jac->spgemm_type;
680 #endif
681   PetscFunctionReturn(PETSC_SUCCESS);
682 }
683 
684 static const char *HYPREBoomerAMGCycleType[]   = {"", "V", "W"};
685 static const char *HYPREBoomerAMGCoarsenType[] = {"CLJP", "Ruge-Stueben", "", "modifiedRuge-Stueben", "", "", "Falgout", "", "PMIS", "", "HMIS"};
686 static const char *HYPREBoomerAMGMeasureType[] = {"local", "global"};
687 /* The following corresponds to HYPRE_BoomerAMGSetRelaxType which has many missing numbers in the enum */
688 static const char *HYPREBoomerAMGSmoothType[] = {"Schwarz-smoothers", "Pilut", "ParaSails", "Euclid"};
689 static const char *HYPREBoomerAMGRelaxType[] = {"Jacobi", "sequential-Gauss-Seidel", "seqboundary-Gauss-Seidel", "SOR/Jacobi", "backward-SOR/Jacobi", "" /* [5] hybrid chaotic Gauss-Seidel (works only with OpenMP) */, "symmetric-SOR/Jacobi", "" /* 7 */, "l1scaled-SOR/Jacobi", "Gaussian-elimination", "" /* 10 */, "" /* 11 */, "" /* 12 */, "l1-Gauss-Seidel" /* nonsymmetric */, "backward-l1-Gauss-Seidel" /* nonsymmetric */, "CG" /* non-stationary */, "Chebyshev", "FCF-Jacobi", "l1scaled-Jacobi"};
690 static const char    *HYPREBoomerAMGInterpType[] = {"classical", "", "", "direct", "multipass", "multipass-wts", "ext+i", "ext+i-cc", "standard", "standard-wts", "block", "block-wtd", "FF", "FF1", "ext", "ad-wts", "ext-mm", "ext+i-mm", "ext+e-mm"};
691 static PetscErrorCode PCSetFromOptions_HYPRE_BoomerAMG(PC pc, PetscOptionItems *PetscOptionsObject)
692 {
693   PC_HYPRE   *jac = (PC_HYPRE *)pc->data;
694   PetscInt    bs, n, indx, level;
695   PetscBool   flg, tmp_truth;
696   double      tmpdbl, twodbl[2];
697   const char *symtlist[]           = {"nonsymmetric", "SPD", "nonsymmetric,SPD"};
698   const char *PCHYPRESpgemmTypes[] = {"cusparse", "hypre"};
699 
700   PetscFunctionBegin;
701   PetscOptionsHeadBegin(PetscOptionsObject, "HYPRE BoomerAMG Options");
702   PetscCall(PetscOptionsEList("-pc_hypre_boomeramg_cycle_type", "Cycle type", "None", HYPREBoomerAMGCycleType + 1, 2, HYPREBoomerAMGCycleType[jac->cycletype], &indx, &flg));
703   if (flg) {
704     jac->cycletype = indx + 1;
705     PetscCallExternal(HYPRE_BoomerAMGSetCycleType, jac->hsolver, jac->cycletype);
706   }
707   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_max_levels", "Number of levels (of grids) allowed", "None", jac->maxlevels, &jac->maxlevels, &flg));
708   if (flg) {
709     PetscCheck(jac->maxlevels >= 2, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Number of levels %" PetscInt_FMT " must be at least two", jac->maxlevels);
710     PetscCallExternal(HYPRE_BoomerAMGSetMaxLevels, jac->hsolver, jac->maxlevels);
711   }
712   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_max_iter", "Maximum iterations used PER hypre call", "None", jac->maxiter, &jac->maxiter, &flg));
713   if (flg) {
714     PetscCheck(jac->maxiter >= 1, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Number of iterations %" PetscInt_FMT " must be at least one", jac->maxiter);
715     PetscCallExternal(HYPRE_BoomerAMGSetMaxIter, jac->hsolver, jac->maxiter);
716   }
717   PetscCall(PetscOptionsReal("-pc_hypre_boomeramg_tol", "Convergence tolerance PER hypre call (0.0 = use a fixed number of iterations)", "None", jac->tol, &jac->tol, &flg));
718   if (flg) {
719     PetscCheck(jac->tol >= 0.0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Tolerance %g must be greater than or equal to zero", (double)jac->tol);
720     PetscCallExternal(HYPRE_BoomerAMGSetTol, jac->hsolver, jac->tol);
721   }
722   bs = 1;
723   if (pc->pmat) PetscCall(MatGetBlockSize(pc->pmat, &bs));
724   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_numfunctions", "Number of functions", "HYPRE_BoomerAMGSetNumFunctions", bs, &bs, &flg));
725   if (flg) PetscCallExternal(HYPRE_BoomerAMGSetNumFunctions, jac->hsolver, bs);
726 
727   PetscCall(PetscOptionsReal("-pc_hypre_boomeramg_truncfactor", "Truncation factor for interpolation (0=no truncation)", "None", jac->truncfactor, &jac->truncfactor, &flg));
728   if (flg) {
729     PetscCheck(jac->truncfactor >= 0.0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Truncation factor %g must be great than or equal zero", (double)jac->truncfactor);
730     PetscCallExternal(HYPRE_BoomerAMGSetTruncFactor, jac->hsolver, jac->truncfactor);
731   }
732 
733   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_P_max", "Max elements per row for interpolation operator (0=unlimited)", "None", jac->pmax, &jac->pmax, &flg));
734   if (flg) {
735     PetscCheck(jac->pmax >= 0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "P_max %" PetscInt_FMT " must be greater than or equal to zero", jac->pmax);
736     PetscCallExternal(HYPRE_BoomerAMGSetPMaxElmts, jac->hsolver, jac->pmax);
737   }
738 
739   PetscCall(PetscOptionsRangeInt("-pc_hypre_boomeramg_agg_nl", "Number of levels of aggressive coarsening", "None", jac->agg_nl, &jac->agg_nl, &flg, 0, jac->maxlevels));
740   if (flg) PetscCallExternal(HYPRE_BoomerAMGSetAggNumLevels, jac->hsolver, jac->agg_nl);
741 
742   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_agg_num_paths", "Number of paths for aggressive coarsening", "None", jac->agg_num_paths, &jac->agg_num_paths, &flg));
743   if (flg) {
744     PetscCheck(jac->agg_num_paths >= 1, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Number of paths %" PetscInt_FMT " must be greater than or equal to 1", jac->agg_num_paths);
745     PetscCallExternal(HYPRE_BoomerAMGSetNumPaths, jac->hsolver, jac->agg_num_paths);
746   }
747 
748   PetscCall(PetscOptionsReal("-pc_hypre_boomeramg_strong_threshold", "Threshold for being strongly connected", "None", jac->strongthreshold, &jac->strongthreshold, &flg));
749   if (flg) {
750     PetscCheck(jac->strongthreshold >= 0.0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Strong threshold %g must be great than or equal zero", (double)jac->strongthreshold);
751     PetscCallExternal(HYPRE_BoomerAMGSetStrongThreshold, jac->hsolver, jac->strongthreshold);
752   }
753   PetscCall(PetscOptionsReal("-pc_hypre_boomeramg_max_row_sum", "Maximum row sum", "None", jac->maxrowsum, &jac->maxrowsum, &flg));
754   if (flg) {
755     PetscCheck(jac->maxrowsum >= 0.0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Maximum row sum %g must be greater than zero", (double)jac->maxrowsum);
756     PetscCheck(jac->maxrowsum <= 1.0, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Maximum row sum %g must be less than or equal one", (double)jac->maxrowsum);
757     PetscCallExternal(HYPRE_BoomerAMGSetMaxRowSum, jac->hsolver, jac->maxrowsum);
758   }
759 
760   /* Grid sweeps */
761   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_grid_sweeps_all", "Number of sweeps for the up and down grid levels", "None", jac->gridsweeps[0], &indx, &flg));
762   if (flg) {
763     PetscCallExternal(HYPRE_BoomerAMGSetNumSweeps, jac->hsolver, indx);
764     /* modify the jac structure so we can view the updated options with PC_View */
765     jac->gridsweeps[0] = indx;
766     jac->gridsweeps[1] = indx;
767     /*defaults coarse to 1 */
768     jac->gridsweeps[2] = 1;
769   }
770   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_nodal_coarsen", "Use a nodal based coarsening 1-6", "HYPRE_BoomerAMGSetNodal", jac->nodal_coarsening, &jac->nodal_coarsening, &flg));
771   if (flg) PetscCallExternal(HYPRE_BoomerAMGSetNodal, jac->hsolver, jac->nodal_coarsening);
772   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_nodal_coarsen_diag", "Diagonal in strength matrix for nodal based coarsening 0-2", "HYPRE_BoomerAMGSetNodalDiag", jac->nodal_coarsening_diag, &jac->nodal_coarsening_diag, &flg));
773   if (flg) PetscCallExternal(HYPRE_BoomerAMGSetNodalDiag, jac->hsolver, jac->nodal_coarsening_diag);
774   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_vec_interp_variant", "Variant of algorithm 1-3", "HYPRE_BoomerAMGSetInterpVecVariant", jac->vec_interp_variant, &jac->vec_interp_variant, &flg));
775   if (flg) PetscCallExternal(HYPRE_BoomerAMGSetInterpVecVariant, jac->hsolver, jac->vec_interp_variant);
776   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_vec_interp_qmax", "Max elements per row for each Q", "HYPRE_BoomerAMGSetInterpVecQMax", jac->vec_interp_qmax, &jac->vec_interp_qmax, &flg));
777   if (flg) PetscCallExternal(HYPRE_BoomerAMGSetInterpVecQMax, jac->hsolver, jac->vec_interp_qmax);
778   PetscCall(PetscOptionsBool("-pc_hypre_boomeramg_vec_interp_smooth", "Whether to smooth the interpolation vectors", "HYPRE_BoomerAMGSetSmoothInterpVectors", jac->vec_interp_smooth, &jac->vec_interp_smooth, &flg));
779   if (flg) PetscCallExternal(HYPRE_BoomerAMGSetSmoothInterpVectors, jac->hsolver, jac->vec_interp_smooth);
780   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_interp_refine", "Preprocess the interpolation matrix through iterative weight refinement", "HYPRE_BoomerAMGSetInterpRefine", jac->interp_refine, &jac->interp_refine, &flg));
781   if (flg) PetscCallExternal(HYPRE_BoomerAMGSetInterpRefine, jac->hsolver, jac->interp_refine);
782   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_grid_sweeps_down", "Number of sweeps for the down cycles", "None", jac->gridsweeps[0], &indx, &flg));
783   if (flg) {
784     PetscCallExternal(HYPRE_BoomerAMGSetCycleNumSweeps, jac->hsolver, indx, 1);
785     jac->gridsweeps[0] = indx;
786   }
787   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_grid_sweeps_up", "Number of sweeps for the up cycles", "None", jac->gridsweeps[1], &indx, &flg));
788   if (flg) {
789     PetscCallExternal(HYPRE_BoomerAMGSetCycleNumSweeps, jac->hsolver, indx, 2);
790     jac->gridsweeps[1] = indx;
791   }
792   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_grid_sweeps_coarse", "Number of sweeps for the coarse level", "None", jac->gridsweeps[2], &indx, &flg));
793   if (flg) {
794     PetscCallExternal(HYPRE_BoomerAMGSetCycleNumSweeps, jac->hsolver, indx, 3);
795     jac->gridsweeps[2] = indx;
796   }
797 
798   /* Smooth type */
799   PetscCall(PetscOptionsEList("-pc_hypre_boomeramg_smooth_type", "Enable more complex smoothers", "None", HYPREBoomerAMGSmoothType, PETSC_STATIC_ARRAY_LENGTH(HYPREBoomerAMGSmoothType), HYPREBoomerAMGSmoothType[0], &indx, &flg));
800   if (flg) {
801     jac->smoothtype = indx;
802     PetscCallExternal(HYPRE_BoomerAMGSetSmoothType, jac->hsolver, indx + 6);
803     jac->smoothnumlevels = 25;
804     PetscCallExternal(HYPRE_BoomerAMGSetSmoothNumLevels, jac->hsolver, 25);
805   }
806 
807   /* Number of smoothing levels */
808   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_smooth_num_levels", "Number of levels on which more complex smoothers are used", "None", 25, &indx, &flg));
809   if (flg && (jac->smoothtype != -1)) {
810     jac->smoothnumlevels = indx;
811     PetscCallExternal(HYPRE_BoomerAMGSetSmoothNumLevels, jac->hsolver, indx);
812   }
813 
814   /* Number of levels for ILU(k) for Euclid */
815   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_eu_level", "Number of levels for ILU(k) in Euclid smoother", "None", 0, &indx, &flg));
816   if (flg && (jac->smoothtype == 3)) {
817     jac->eu_level = indx;
818     PetscCallExternal(HYPRE_BoomerAMGSetEuLevel, jac->hsolver, indx);
819   }
820 
821   /* Filter for ILU(k) for Euclid */
822   double droptolerance;
823   PetscCall(PetscOptionsReal("-pc_hypre_boomeramg_eu_droptolerance", "Drop tolerance for ILU(k) in Euclid smoother", "None", 0, &droptolerance, &flg));
824   if (flg && (jac->smoothtype == 3)) {
825     jac->eu_droptolerance = droptolerance;
826     PetscCallExternal(HYPRE_BoomerAMGSetEuLevel, jac->hsolver, droptolerance);
827   }
828 
829   /* Use Block Jacobi ILUT for Euclid */
830   PetscCall(PetscOptionsBool("-pc_hypre_boomeramg_eu_bj", "Use Block Jacobi for ILU in Euclid smoother?", "None", PETSC_FALSE, &tmp_truth, &flg));
831   if (flg && (jac->smoothtype == 3)) {
832     jac->eu_bj = tmp_truth;
833     PetscCallExternal(HYPRE_BoomerAMGSetEuBJ, jac->hsolver, jac->eu_bj);
834   }
835 
836   /* Relax type */
837   PetscCall(PetscOptionsEList("-pc_hypre_boomeramg_relax_type_all", "Relax type for the up and down cycles", "None", HYPREBoomerAMGRelaxType, PETSC_STATIC_ARRAY_LENGTH(HYPREBoomerAMGRelaxType), HYPREBoomerAMGRelaxType[6], &indx, &flg));
838   if (flg) {
839     jac->relaxtype[0] = jac->relaxtype[1] = indx;
840     PetscCallExternal(HYPRE_BoomerAMGSetRelaxType, jac->hsolver, indx);
841     /* by default, coarse type set to 9 */
842     jac->relaxtype[2] = 9;
843     PetscCallExternal(HYPRE_BoomerAMGSetCycleRelaxType, jac->hsolver, 9, 3);
844   }
845   PetscCall(PetscOptionsEList("-pc_hypre_boomeramg_relax_type_down", "Relax type for the down cycles", "None", HYPREBoomerAMGRelaxType, PETSC_STATIC_ARRAY_LENGTH(HYPREBoomerAMGRelaxType), HYPREBoomerAMGRelaxType[6], &indx, &flg));
846   if (flg) {
847     jac->relaxtype[0] = indx;
848     PetscCallExternal(HYPRE_BoomerAMGSetCycleRelaxType, jac->hsolver, indx, 1);
849   }
850   PetscCall(PetscOptionsEList("-pc_hypre_boomeramg_relax_type_up", "Relax type for the up cycles", "None", HYPREBoomerAMGRelaxType, PETSC_STATIC_ARRAY_LENGTH(HYPREBoomerAMGRelaxType), HYPREBoomerAMGRelaxType[6], &indx, &flg));
851   if (flg) {
852     jac->relaxtype[1] = indx;
853     PetscCallExternal(HYPRE_BoomerAMGSetCycleRelaxType, jac->hsolver, indx, 2);
854   }
855   PetscCall(PetscOptionsEList("-pc_hypre_boomeramg_relax_type_coarse", "Relax type on coarse grid", "None", HYPREBoomerAMGRelaxType, PETSC_STATIC_ARRAY_LENGTH(HYPREBoomerAMGRelaxType), HYPREBoomerAMGRelaxType[9], &indx, &flg));
856   if (flg) {
857     jac->relaxtype[2] = indx;
858     PetscCallExternal(HYPRE_BoomerAMGSetCycleRelaxType, jac->hsolver, indx, 3);
859   }
860 
861   /* Relaxation Weight */
862   PetscCall(PetscOptionsReal("-pc_hypre_boomeramg_relax_weight_all", "Relaxation weight for all levels (0 = hypre estimates, -k = determined with k CG steps)", "None", jac->relaxweight, &tmpdbl, &flg));
863   if (flg) {
864     PetscCallExternal(HYPRE_BoomerAMGSetRelaxWt, jac->hsolver, tmpdbl);
865     jac->relaxweight = tmpdbl;
866   }
867 
868   n         = 2;
869   twodbl[0] = twodbl[1] = 1.0;
870   PetscCall(PetscOptionsRealArray("-pc_hypre_boomeramg_relax_weight_level", "Set the relaxation weight for a particular level (weight,level)", "None", twodbl, &n, &flg));
871   if (flg) {
872     PetscCheck(n == 2, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Relax weight level: you must provide 2 values separated by a comma (and no space), you provided %" PetscInt_FMT, n);
873     indx = (int)PetscAbsReal(twodbl[1]);
874     PetscCallExternal(HYPRE_BoomerAMGSetLevelRelaxWt, jac->hsolver, twodbl[0], indx);
875   }
876 
877   /* Outer relaxation Weight */
878   PetscCall(PetscOptionsReal("-pc_hypre_boomeramg_outer_relax_weight_all", "Outer relaxation weight for all levels (-k = determined with k CG steps)", "None", jac->outerrelaxweight, &tmpdbl, &flg));
879   if (flg) {
880     PetscCallExternal(HYPRE_BoomerAMGSetOuterWt, jac->hsolver, tmpdbl);
881     jac->outerrelaxweight = tmpdbl;
882   }
883 
884   n         = 2;
885   twodbl[0] = twodbl[1] = 1.0;
886   PetscCall(PetscOptionsRealArray("-pc_hypre_boomeramg_outer_relax_weight_level", "Set the outer relaxation weight for a particular level (weight,level)", "None", twodbl, &n, &flg));
887   if (flg) {
888     PetscCheck(n == 2, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Relax weight outer level: You must provide 2 values separated by a comma (and no space), you provided %" PetscInt_FMT, n);
889     indx = (int)PetscAbsReal(twodbl[1]);
890     PetscCallExternal(HYPRE_BoomerAMGSetLevelOuterWt, jac->hsolver, twodbl[0], indx);
891   }
892 
893   /* the Relax Order */
894   PetscCall(PetscOptionsBool("-pc_hypre_boomeramg_no_CF", "Do not use CF-relaxation", "None", PETSC_FALSE, &tmp_truth, &flg));
895 
896   if (flg && tmp_truth) {
897     jac->relaxorder = 0;
898     PetscCallExternal(HYPRE_BoomerAMGSetRelaxOrder, jac->hsolver, jac->relaxorder);
899   }
900   PetscCall(PetscOptionsEList("-pc_hypre_boomeramg_measure_type", "Measure type", "None", HYPREBoomerAMGMeasureType, PETSC_STATIC_ARRAY_LENGTH(HYPREBoomerAMGMeasureType), HYPREBoomerAMGMeasureType[0], &indx, &flg));
901   if (flg) {
902     jac->measuretype = indx;
903     PetscCallExternal(HYPRE_BoomerAMGSetMeasureType, jac->hsolver, jac->measuretype);
904   }
905   /* update list length 3/07 */
906   PetscCall(PetscOptionsEList("-pc_hypre_boomeramg_coarsen_type", "Coarsen type", "None", HYPREBoomerAMGCoarsenType, PETSC_STATIC_ARRAY_LENGTH(HYPREBoomerAMGCoarsenType), HYPREBoomerAMGCoarsenType[6], &indx, &flg));
907   if (flg) {
908     jac->coarsentype = indx;
909     PetscCallExternal(HYPRE_BoomerAMGSetCoarsenType, jac->hsolver, jac->coarsentype);
910   }
911 
912   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_max_coarse_size", "Maximum size of coarsest grid", "None", jac->maxc, &jac->maxc, &flg));
913   if (flg) PetscCallExternal(HYPRE_BoomerAMGSetMaxCoarseSize, jac->hsolver, jac->maxc);
914   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_min_coarse_size", "Minimum size of coarsest grid", "None", jac->minc, &jac->minc, &flg));
915   if (flg) PetscCallExternal(HYPRE_BoomerAMGSetMinCoarseSize, jac->hsolver, jac->minc);
916 #if PETSC_PKG_HYPRE_VERSION_GE(2, 23, 0)
917   // global parameter but is closely associated with BoomerAMG
918   PetscCall(PetscOptionsEList("-pc_mg_galerkin_mat_product_algorithm", "Type of SpGEMM to use in hypre (only for now)", "PCMGGalerkinSetMatProductAlgorithm", PCHYPRESpgemmTypes, PETSC_STATIC_ARRAY_LENGTH(PCHYPRESpgemmTypes), PCHYPRESpgemmTypes[0], &indx, &flg));
919   if (!flg) indx = 0;
920   PetscCall(PCMGGalerkinSetMatProductAlgorithm_HYPRE_BoomerAMG(pc, PCHYPRESpgemmTypes[indx]));
921 #endif
922   /* AIR */
923 #if PETSC_PKG_HYPRE_VERSION_GE(2, 18, 0)
924   PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_restriction_type", "Type of AIR method (distance 1 or 2, 0 means no AIR)", "None", jac->Rtype, &jac->Rtype, NULL));
925   PetscCallExternal(HYPRE_BoomerAMGSetRestriction, jac->hsolver, jac->Rtype);
926   if (jac->Rtype) {
927     jac->interptype = 100; /* no way we can pass this with strings... Set it as default as in MFEM, then users can still customize it back to a different one */
928 
929     PetscCall(PetscOptionsReal("-pc_hypre_boomeramg_strongthresholdR", "Threshold for R", "None", jac->Rstrongthreshold, &jac->Rstrongthreshold, NULL));
930     PetscCallExternal(HYPRE_BoomerAMGSetStrongThresholdR, jac->hsolver, jac->Rstrongthreshold);
931 
932     PetscCall(PetscOptionsReal("-pc_hypre_boomeramg_filterthresholdR", "Filter threshold for R", "None", jac->Rfilterthreshold, &jac->Rfilterthreshold, NULL));
933     PetscCallExternal(HYPRE_BoomerAMGSetFilterThresholdR, jac->hsolver, jac->Rfilterthreshold);
934 
935     PetscCall(PetscOptionsReal("-pc_hypre_boomeramg_Adroptol", "Defines the drop tolerance for the A-matrices from the 2nd level of AMG", "None", jac->Adroptol, &jac->Adroptol, NULL));
936     PetscCallExternal(HYPRE_BoomerAMGSetADropTol, jac->hsolver, jac->Adroptol);
937 
938     PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_Adroptype", "Drops the entries that are not on the diagonal and smaller than its row norm: type 1: 1-norm, 2: 2-norm, -1: infinity norm", "None", jac->Adroptype, &jac->Adroptype, NULL));
939     PetscCallExternal(HYPRE_BoomerAMGSetADropType, jac->hsolver, jac->Adroptype);
940   }
941 #endif
942 
943 #if PETSC_PKG_HYPRE_VERSION_LE(9, 9, 9)
944   PetscCheck(!jac->Rtype || !jac->agg_nl, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "-pc_hypre_boomeramg_restriction_type (%" PetscInt_FMT ") and -pc_hypre_boomeramg_agg_nl (%" PetscInt_FMT ")", jac->Rtype, jac->agg_nl);
945 #endif
946 
947   /* new 3/07 */
948   PetscCall(PetscOptionsEList("-pc_hypre_boomeramg_interp_type", "Interpolation type", "None", HYPREBoomerAMGInterpType, PETSC_STATIC_ARRAY_LENGTH(HYPREBoomerAMGInterpType), HYPREBoomerAMGInterpType[0], &indx, &flg));
949   if (flg || jac->Rtype) {
950     if (flg) jac->interptype = indx;
951     PetscCallExternal(HYPRE_BoomerAMGSetInterpType, jac->hsolver, jac->interptype);
952   }
953 
954   PetscCall(PetscOptionsName("-pc_hypre_boomeramg_print_statistics", "Print statistics", "None", &flg));
955   if (flg) {
956     level = 3;
957     PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_print_statistics", "Print statistics", "None", level, &level, NULL));
958 
959     jac->printstatistics = PETSC_TRUE;
960     PetscCallExternal(HYPRE_BoomerAMGSetPrintLevel, jac->hsolver, level);
961   }
962 
963   PetscCall(PetscOptionsName("-pc_hypre_boomeramg_print_debug", "Print debug information", "None", &flg));
964   if (flg) {
965     level = 3;
966     PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_print_debug", "Print debug information", "None", level, &level, NULL));
967 
968     jac->printstatistics = PETSC_TRUE;
969     PetscCallExternal(HYPRE_BoomerAMGSetDebugFlag, jac->hsolver, level);
970   }
971 
972   PetscCall(PetscOptionsBool("-pc_hypre_boomeramg_nodal_relaxation", "Nodal relaxation via Schwarz", "None", PETSC_FALSE, &tmp_truth, &flg));
973   if (flg && tmp_truth) {
974     PetscInt tmp_int;
975     PetscCall(PetscOptionsInt("-pc_hypre_boomeramg_nodal_relaxation", "Nodal relaxation via Schwarz", "None", jac->nodal_relax_levels, &tmp_int, &flg));
976     if (flg) jac->nodal_relax_levels = tmp_int;
977     PetscCallExternal(HYPRE_BoomerAMGSetSmoothType, jac->hsolver, 6);
978     PetscCallExternal(HYPRE_BoomerAMGSetDomainType, jac->hsolver, 1);
979     PetscCallExternal(HYPRE_BoomerAMGSetOverlap, jac->hsolver, 0);
980     PetscCallExternal(HYPRE_BoomerAMGSetSmoothNumLevels, jac->hsolver, jac->nodal_relax_levels);
981   }
982 
983   PetscCall(PetscOptionsBool("-pc_hypre_boomeramg_keeptranspose", "Avoid transpose matvecs in preconditioner application", "None", jac->keeptranspose, &jac->keeptranspose, NULL));
984   PetscCallExternal(HYPRE_BoomerAMGSetKeepTranspose, jac->hsolver, jac->keeptranspose ? 1 : 0);
985 
986   /* options for ParaSails solvers */
987   PetscCall(PetscOptionsEList("-pc_hypre_boomeramg_parasails_sym", "Symmetry of matrix and preconditioner", "None", symtlist, PETSC_STATIC_ARRAY_LENGTH(symtlist), symtlist[0], &indx, &flg));
988   if (flg) {
989     jac->symt = indx;
990     PetscCallExternal(HYPRE_BoomerAMGSetSym, jac->hsolver, jac->symt);
991   }
992 
993   PetscOptionsHeadEnd();
994   PetscFunctionReturn(PETSC_SUCCESS);
995 }
996 
997 static PetscErrorCode PCApplyRichardson_HYPRE_BoomerAMG(PC pc, Vec b, Vec y, Vec w, PetscReal rtol, PetscReal abstol, PetscReal dtol, PetscInt its, PetscBool guesszero, PetscInt *outits, PCRichardsonConvergedReason *reason)
998 {
999   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1000   HYPRE_Int oits;
1001 
1002   PetscFunctionBegin;
1003   PetscCall(PetscCitationsRegister(hypreCitation, &cite));
1004   PetscCallExternal(HYPRE_BoomerAMGSetMaxIter, jac->hsolver, its * jac->maxiter);
1005   PetscCallExternal(HYPRE_BoomerAMGSetTol, jac->hsolver, rtol);
1006   jac->applyrichardson = PETSC_TRUE;
1007   PetscCall(PCApply_HYPRE(pc, b, y));
1008   jac->applyrichardson = PETSC_FALSE;
1009   PetscCallExternal(HYPRE_BoomerAMGGetNumIterations, jac->hsolver, &oits);
1010   *outits = oits;
1011   if (oits == its) *reason = PCRICHARDSON_CONVERGED_ITS;
1012   else *reason = PCRICHARDSON_CONVERGED_RTOL;
1013   PetscCallExternal(HYPRE_BoomerAMGSetTol, jac->hsolver, jac->tol);
1014   PetscCallExternal(HYPRE_BoomerAMGSetMaxIter, jac->hsolver, jac->maxiter);
1015   PetscFunctionReturn(PETSC_SUCCESS);
1016 }
1017 
1018 static PetscErrorCode PCView_HYPRE_BoomerAMG(PC pc, PetscViewer viewer)
1019 {
1020   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1021   PetscBool iascii;
1022 
1023   PetscFunctionBegin;
1024   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
1025   if (iascii) {
1026     PetscCall(PetscViewerASCIIPrintf(viewer, "  HYPRE BoomerAMG preconditioning\n"));
1027     PetscCall(PetscViewerASCIIPrintf(viewer, "    Cycle type %s\n", HYPREBoomerAMGCycleType[jac->cycletype]));
1028     PetscCall(PetscViewerASCIIPrintf(viewer, "    Maximum number of levels %" PetscInt_FMT "\n", jac->maxlevels));
1029     PetscCall(PetscViewerASCIIPrintf(viewer, "    Maximum number of iterations PER hypre call %" PetscInt_FMT "\n", jac->maxiter));
1030     PetscCall(PetscViewerASCIIPrintf(viewer, "    Convergence tolerance PER hypre call %g\n", (double)jac->tol));
1031     PetscCall(PetscViewerASCIIPrintf(viewer, "    Threshold for strong coupling %g\n", (double)jac->strongthreshold));
1032     PetscCall(PetscViewerASCIIPrintf(viewer, "    Interpolation truncation factor %g\n", (double)jac->truncfactor));
1033     PetscCall(PetscViewerASCIIPrintf(viewer, "    Interpolation: max elements per row %" PetscInt_FMT "\n", jac->pmax));
1034     if (jac->interp_refine) PetscCall(PetscViewerASCIIPrintf(viewer, "    Interpolation: number of steps of weighted refinement %" PetscInt_FMT "\n", jac->interp_refine));
1035     PetscCall(PetscViewerASCIIPrintf(viewer, "    Number of levels of aggressive coarsening %" PetscInt_FMT "\n", jac->agg_nl));
1036     PetscCall(PetscViewerASCIIPrintf(viewer, "    Number of paths for aggressive coarsening %" PetscInt_FMT "\n", jac->agg_num_paths));
1037 
1038     PetscCall(PetscViewerASCIIPrintf(viewer, "    Maximum row sums %g\n", (double)jac->maxrowsum));
1039 
1040     PetscCall(PetscViewerASCIIPrintf(viewer, "    Sweeps down         %" PetscInt_FMT "\n", jac->gridsweeps[0]));
1041     PetscCall(PetscViewerASCIIPrintf(viewer, "    Sweeps up           %" PetscInt_FMT "\n", jac->gridsweeps[1]));
1042     PetscCall(PetscViewerASCIIPrintf(viewer, "    Sweeps on coarse    %" PetscInt_FMT "\n", jac->gridsweeps[2]));
1043 
1044     PetscCall(PetscViewerASCIIPrintf(viewer, "    Relax down          %s\n", HYPREBoomerAMGRelaxType[jac->relaxtype[0]]));
1045     PetscCall(PetscViewerASCIIPrintf(viewer, "    Relax up            %s\n", HYPREBoomerAMGRelaxType[jac->relaxtype[1]]));
1046     PetscCall(PetscViewerASCIIPrintf(viewer, "    Relax on coarse     %s\n", HYPREBoomerAMGRelaxType[jac->relaxtype[2]]));
1047 
1048     PetscCall(PetscViewerASCIIPrintf(viewer, "    Relax weight  (all)      %g\n", (double)jac->relaxweight));
1049     PetscCall(PetscViewerASCIIPrintf(viewer, "    Outer relax weight (all) %g\n", (double)jac->outerrelaxweight));
1050 
1051     if (jac->relaxorder) {
1052       PetscCall(PetscViewerASCIIPrintf(viewer, "    Using CF-relaxation\n"));
1053     } else {
1054       PetscCall(PetscViewerASCIIPrintf(viewer, "    Not using CF-relaxation\n"));
1055     }
1056     if (jac->smoothtype != -1) {
1057       PetscCall(PetscViewerASCIIPrintf(viewer, "    Smooth type          %s\n", HYPREBoomerAMGSmoothType[jac->smoothtype]));
1058       PetscCall(PetscViewerASCIIPrintf(viewer, "    Smooth num levels    %" PetscInt_FMT "\n", jac->smoothnumlevels));
1059     } else {
1060       PetscCall(PetscViewerASCIIPrintf(viewer, "    Not using more complex smoothers.\n"));
1061     }
1062     if (jac->smoothtype == 3) {
1063       PetscCall(PetscViewerASCIIPrintf(viewer, "    Euclid ILU(k) levels %" PetscInt_FMT "\n", jac->eu_level));
1064       PetscCall(PetscViewerASCIIPrintf(viewer, "    Euclid ILU(k) drop tolerance %g\n", (double)jac->eu_droptolerance));
1065       PetscCall(PetscViewerASCIIPrintf(viewer, "    Euclid ILU use Block-Jacobi? %" PetscInt_FMT "\n", jac->eu_bj));
1066     }
1067     PetscCall(PetscViewerASCIIPrintf(viewer, "    Measure type        %s\n", HYPREBoomerAMGMeasureType[jac->measuretype]));
1068     PetscCall(PetscViewerASCIIPrintf(viewer, "    Coarsen type        %s\n", HYPREBoomerAMGCoarsenType[jac->coarsentype]));
1069     PetscCall(PetscViewerASCIIPrintf(viewer, "    Interpolation type  %s\n", jac->interptype != 100 ? HYPREBoomerAMGInterpType[jac->interptype] : "1pt"));
1070     if (jac->nodal_coarsening) PetscCall(PetscViewerASCIIPrintf(viewer, "    Using nodal coarsening with HYPRE_BOOMERAMGSetNodal() %" PetscInt_FMT "\n", jac->nodal_coarsening));
1071     if (jac->vec_interp_variant) {
1072       PetscCall(PetscViewerASCIIPrintf(viewer, "    HYPRE_BoomerAMGSetInterpVecVariant() %" PetscInt_FMT "\n", jac->vec_interp_variant));
1073       PetscCall(PetscViewerASCIIPrintf(viewer, "    HYPRE_BoomerAMGSetInterpVecQMax() %" PetscInt_FMT "\n", jac->vec_interp_qmax));
1074       PetscCall(PetscViewerASCIIPrintf(viewer, "    HYPRE_BoomerAMGSetSmoothInterpVectors() %d\n", jac->vec_interp_smooth));
1075     }
1076     if (jac->nodal_relax) PetscCall(PetscViewerASCIIPrintf(viewer, "    Using nodal relaxation via Schwarz smoothing on levels %" PetscInt_FMT "\n", jac->nodal_relax_levels));
1077 #if PETSC_PKG_HYPRE_VERSION_GE(2, 23, 0)
1078     PetscCall(PetscViewerASCIIPrintf(viewer, "    SpGEMM type         %s\n", jac->spgemm_type));
1079 #endif
1080     /* AIR */
1081     if (jac->Rtype) {
1082       PetscCall(PetscViewerASCIIPrintf(viewer, "    Using approximate ideal restriction type %" PetscInt_FMT "\n", jac->Rtype));
1083       PetscCall(PetscViewerASCIIPrintf(viewer, "      Threshold for R %g\n", (double)jac->Rstrongthreshold));
1084       PetscCall(PetscViewerASCIIPrintf(viewer, "      Filter for R %g\n", (double)jac->Rfilterthreshold));
1085       PetscCall(PetscViewerASCIIPrintf(viewer, "      A drop tolerance %g\n", (double)jac->Adroptol));
1086       PetscCall(PetscViewerASCIIPrintf(viewer, "      A drop type %" PetscInt_FMT "\n", jac->Adroptype));
1087     }
1088   }
1089   PetscFunctionReturn(PETSC_SUCCESS);
1090 }
1091 
1092 static PetscErrorCode PCSetFromOptions_HYPRE_ParaSails(PC pc, PetscOptionItems *PetscOptionsObject)
1093 {
1094   PC_HYPRE   *jac = (PC_HYPRE *)pc->data;
1095   PetscInt    indx;
1096   PetscBool   flag;
1097   const char *symtlist[] = {"nonsymmetric", "SPD", "nonsymmetric,SPD"};
1098 
1099   PetscFunctionBegin;
1100   PetscOptionsHeadBegin(PetscOptionsObject, "HYPRE ParaSails Options");
1101   PetscCall(PetscOptionsInt("-pc_hypre_parasails_nlevels", "Number of number of levels", "None", jac->nlevels, &jac->nlevels, 0));
1102   PetscCall(PetscOptionsReal("-pc_hypre_parasails_thresh", "Threshold", "None", jac->threshold, &jac->threshold, &flag));
1103   if (flag) PetscCallExternal(HYPRE_ParaSailsSetParams, jac->hsolver, jac->threshold, jac->nlevels);
1104 
1105   PetscCall(PetscOptionsReal("-pc_hypre_parasails_filter", "filter", "None", jac->filter, &jac->filter, &flag));
1106   if (flag) PetscCallExternal(HYPRE_ParaSailsSetFilter, jac->hsolver, jac->filter);
1107 
1108   PetscCall(PetscOptionsReal("-pc_hypre_parasails_loadbal", "Load balance", "None", jac->loadbal, &jac->loadbal, &flag));
1109   if (flag) PetscCallExternal(HYPRE_ParaSailsSetLoadbal, jac->hsolver, jac->loadbal);
1110 
1111   PetscCall(PetscOptionsBool("-pc_hypre_parasails_logging", "Print info to screen", "None", (PetscBool)jac->logging, (PetscBool *)&jac->logging, &flag));
1112   if (flag) PetscCallExternal(HYPRE_ParaSailsSetLogging, jac->hsolver, jac->logging);
1113 
1114   PetscCall(PetscOptionsBool("-pc_hypre_parasails_reuse", "Reuse nonzero pattern in preconditioner", "None", (PetscBool)jac->ruse, (PetscBool *)&jac->ruse, &flag));
1115   if (flag) PetscCallExternal(HYPRE_ParaSailsSetReuse, jac->hsolver, jac->ruse);
1116 
1117   PetscCall(PetscOptionsEList("-pc_hypre_parasails_sym", "Symmetry of matrix and preconditioner", "None", symtlist, PETSC_STATIC_ARRAY_LENGTH(symtlist), symtlist[0], &indx, &flag));
1118   if (flag) {
1119     jac->symt = indx;
1120     PetscCallExternal(HYPRE_ParaSailsSetSym, jac->hsolver, jac->symt);
1121   }
1122 
1123   PetscOptionsHeadEnd();
1124   PetscFunctionReturn(PETSC_SUCCESS);
1125 }
1126 
1127 static PetscErrorCode PCView_HYPRE_ParaSails(PC pc, PetscViewer viewer)
1128 {
1129   PC_HYPRE   *jac = (PC_HYPRE *)pc->data;
1130   PetscBool   iascii;
1131   const char *symt = 0;
1132 
1133   PetscFunctionBegin;
1134   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
1135   if (iascii) {
1136     PetscCall(PetscViewerASCIIPrintf(viewer, "  HYPRE ParaSails preconditioning\n"));
1137     PetscCall(PetscViewerASCIIPrintf(viewer, "    nlevels %" PetscInt_FMT "\n", jac->nlevels));
1138     PetscCall(PetscViewerASCIIPrintf(viewer, "    threshold %g\n", (double)jac->threshold));
1139     PetscCall(PetscViewerASCIIPrintf(viewer, "    filter %g\n", (double)jac->filter));
1140     PetscCall(PetscViewerASCIIPrintf(viewer, "    load balance %g\n", (double)jac->loadbal));
1141     PetscCall(PetscViewerASCIIPrintf(viewer, "    reuse nonzero structure %s\n", PetscBools[jac->ruse]));
1142     PetscCall(PetscViewerASCIIPrintf(viewer, "    print info to screen %s\n", PetscBools[jac->logging]));
1143     if (!jac->symt) symt = "nonsymmetric matrix and preconditioner";
1144     else if (jac->symt == 1) symt = "SPD matrix and preconditioner";
1145     else if (jac->symt == 2) symt = "nonsymmetric matrix but SPD preconditioner";
1146     else SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Unknown HYPRE ParaSails symmetric option %" PetscInt_FMT, jac->symt);
1147     PetscCall(PetscViewerASCIIPrintf(viewer, "    %s\n", symt));
1148   }
1149   PetscFunctionReturn(PETSC_SUCCESS);
1150 }
1151 
1152 static PetscErrorCode PCSetFromOptions_HYPRE_AMS(PC pc, PetscOptionItems *PetscOptionsObject)
1153 {
1154   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1155   PetscInt  n;
1156   PetscBool flag, flag2, flag3, flag4;
1157 
1158   PetscFunctionBegin;
1159   PetscOptionsHeadBegin(PetscOptionsObject, "HYPRE AMS Options");
1160   PetscCall(PetscOptionsInt("-pc_hypre_ams_print_level", "Debugging output level for AMS", "None", jac->as_print, &jac->as_print, &flag));
1161   if (flag) PetscCallExternal(HYPRE_AMSSetPrintLevel, jac->hsolver, jac->as_print);
1162   PetscCall(PetscOptionsInt("-pc_hypre_ams_max_iter", "Maximum number of AMS multigrid iterations within PCApply", "None", jac->as_max_iter, &jac->as_max_iter, &flag));
1163   if (flag) PetscCallExternal(HYPRE_AMSSetMaxIter, jac->hsolver, jac->as_max_iter);
1164   PetscCall(PetscOptionsInt("-pc_hypre_ams_cycle_type", "Cycle type for AMS multigrid", "None", jac->ams_cycle_type, &jac->ams_cycle_type, &flag));
1165   if (flag) PetscCallExternal(HYPRE_AMSSetCycleType, jac->hsolver, jac->ams_cycle_type);
1166   PetscCall(PetscOptionsReal("-pc_hypre_ams_tol", "Error tolerance for AMS multigrid", "None", jac->as_tol, &jac->as_tol, &flag));
1167   if (flag) PetscCallExternal(HYPRE_AMSSetTol, jac->hsolver, jac->as_tol);
1168   PetscCall(PetscOptionsInt("-pc_hypre_ams_relax_type", "Relaxation type for AMS smoother", "None", jac->as_relax_type, &jac->as_relax_type, &flag));
1169   PetscCall(PetscOptionsInt("-pc_hypre_ams_relax_times", "Number of relaxation steps for AMS smoother", "None", jac->as_relax_times, &jac->as_relax_times, &flag2));
1170   PetscCall(PetscOptionsReal("-pc_hypre_ams_relax_weight", "Relaxation weight for AMS smoother", "None", jac->as_relax_weight, &jac->as_relax_weight, &flag3));
1171   PetscCall(PetscOptionsReal("-pc_hypre_ams_omega", "SSOR coefficient for AMS smoother", "None", jac->as_omega, &jac->as_omega, &flag4));
1172   if (flag || flag2 || flag3 || flag4) PetscCallExternal(HYPRE_AMSSetSmoothingOptions, jac->hsolver, jac->as_relax_type, jac->as_relax_times, jac->as_relax_weight, jac->as_omega);
1173   PetscCall(PetscOptionsReal("-pc_hypre_ams_amg_alpha_theta", "Threshold for strong coupling of vector Poisson AMG solver", "None", jac->as_amg_alpha_theta, &jac->as_amg_alpha_theta, &flag));
1174   n = 5;
1175   PetscCall(PetscOptionsIntArray("-pc_hypre_ams_amg_alpha_options", "AMG options for vector Poisson", "None", jac->as_amg_alpha_opts, &n, &flag2));
1176   if (flag || flag2) {
1177     PetscCallExternal(HYPRE_AMSSetAlphaAMGOptions, jac->hsolver, jac->as_amg_alpha_opts[0], /* AMG coarsen type */
1178                       jac->as_amg_alpha_opts[1],                                            /* AMG agg_levels */
1179                       jac->as_amg_alpha_opts[2],                                            /* AMG relax_type */
1180                       jac->as_amg_alpha_theta, jac->as_amg_alpha_opts[3],                   /* AMG interp_type */
1181                       jac->as_amg_alpha_opts[4]);                                           /* AMG Pmax */
1182   }
1183   PetscCall(PetscOptionsReal("-pc_hypre_ams_amg_beta_theta", "Threshold for strong coupling of scalar Poisson AMG solver", "None", jac->as_amg_beta_theta, &jac->as_amg_beta_theta, &flag));
1184   n = 5;
1185   PetscCall(PetscOptionsIntArray("-pc_hypre_ams_amg_beta_options", "AMG options for scalar Poisson solver", "None", jac->as_amg_beta_opts, &n, &flag2));
1186   if (flag || flag2) {
1187     PetscCallExternal(HYPRE_AMSSetBetaAMGOptions, jac->hsolver, jac->as_amg_beta_opts[0], /* AMG coarsen type */
1188                       jac->as_amg_beta_opts[1],                                           /* AMG agg_levels */
1189                       jac->as_amg_beta_opts[2],                                           /* AMG relax_type */
1190                       jac->as_amg_beta_theta, jac->as_amg_beta_opts[3],                   /* AMG interp_type */
1191                       jac->as_amg_beta_opts[4]);                                          /* AMG Pmax */
1192   }
1193   PetscCall(PetscOptionsInt("-pc_hypre_ams_projection_frequency", "Frequency at which a projection onto the compatible subspace for problems with zero conductivity regions is performed", "None", jac->ams_proj_freq, &jac->ams_proj_freq, &flag));
1194   if (flag) { /* override HYPRE's default only if the options is used */
1195     PetscCallExternal(HYPRE_AMSSetProjectionFrequency, jac->hsolver, jac->ams_proj_freq);
1196   }
1197   PetscOptionsHeadEnd();
1198   PetscFunctionReturn(PETSC_SUCCESS);
1199 }
1200 
1201 static PetscErrorCode PCView_HYPRE_AMS(PC pc, PetscViewer viewer)
1202 {
1203   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1204   PetscBool iascii;
1205 
1206   PetscFunctionBegin;
1207   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
1208   if (iascii) {
1209     PetscCall(PetscViewerASCIIPrintf(viewer, "  HYPRE AMS preconditioning\n"));
1210     PetscCall(PetscViewerASCIIPrintf(viewer, "    subspace iterations per application %" PetscInt_FMT "\n", jac->as_max_iter));
1211     PetscCall(PetscViewerASCIIPrintf(viewer, "    subspace cycle type %" PetscInt_FMT "\n", jac->ams_cycle_type));
1212     PetscCall(PetscViewerASCIIPrintf(viewer, "    subspace iteration tolerance %g\n", (double)jac->as_tol));
1213     PetscCall(PetscViewerASCIIPrintf(viewer, "    smoother type %" PetscInt_FMT "\n", jac->as_relax_type));
1214     PetscCall(PetscViewerASCIIPrintf(viewer, "    number of smoothing steps %" PetscInt_FMT "\n", jac->as_relax_times));
1215     PetscCall(PetscViewerASCIIPrintf(viewer, "    smoother weight %g\n", (double)jac->as_relax_weight));
1216     PetscCall(PetscViewerASCIIPrintf(viewer, "    smoother omega %g\n", (double)jac->as_omega));
1217     if (jac->alpha_Poisson) {
1218       PetscCall(PetscViewerASCIIPrintf(viewer, "    vector Poisson solver (passed in by user)\n"));
1219     } else {
1220       PetscCall(PetscViewerASCIIPrintf(viewer, "    vector Poisson solver (computed) \n"));
1221     }
1222     PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG coarsening type %" PetscInt_FMT "\n", jac->as_amg_alpha_opts[0]));
1223     PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG levels of aggressive coarsening %" PetscInt_FMT "\n", jac->as_amg_alpha_opts[1]));
1224     PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG relaxation type %" PetscInt_FMT "\n", jac->as_amg_alpha_opts[2]));
1225     PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG interpolation type %" PetscInt_FMT "\n", jac->as_amg_alpha_opts[3]));
1226     PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG max nonzero elements in interpolation rows %" PetscInt_FMT "\n", jac->as_amg_alpha_opts[4]));
1227     PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG strength threshold %g\n", (double)jac->as_amg_alpha_theta));
1228     if (!jac->ams_beta_is_zero) {
1229       if (jac->beta_Poisson) {
1230         PetscCall(PetscViewerASCIIPrintf(viewer, "    scalar Poisson solver (passed in by user)\n"));
1231       } else {
1232         PetscCall(PetscViewerASCIIPrintf(viewer, "    scalar Poisson solver (computed) \n"));
1233       }
1234       PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG coarsening type %" PetscInt_FMT "\n", jac->as_amg_beta_opts[0]));
1235       PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG levels of aggressive coarsening %" PetscInt_FMT "\n", jac->as_amg_beta_opts[1]));
1236       PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG relaxation type %" PetscInt_FMT "\n", jac->as_amg_beta_opts[2]));
1237       PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG interpolation type %" PetscInt_FMT "\n", jac->as_amg_beta_opts[3]));
1238       PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG max nonzero elements in interpolation rows %" PetscInt_FMT "\n", jac->as_amg_beta_opts[4]));
1239       PetscCall(PetscViewerASCIIPrintf(viewer, "        boomerAMG strength threshold %g\n", (double)jac->as_amg_beta_theta));
1240       if (jac->ams_beta_is_zero_part) PetscCall(PetscViewerASCIIPrintf(viewer, "        compatible subspace projection frequency %" PetscInt_FMT " (-1 HYPRE uses default)\n", jac->ams_proj_freq));
1241     } else {
1242       PetscCall(PetscViewerASCIIPrintf(viewer, "    scalar Poisson solver not used (zero-conductivity everywhere) \n"));
1243     }
1244   }
1245   PetscFunctionReturn(PETSC_SUCCESS);
1246 }
1247 
1248 static PetscErrorCode PCSetFromOptions_HYPRE_ADS(PC pc, PetscOptionItems *PetscOptionsObject)
1249 {
1250   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1251   PetscInt  n;
1252   PetscBool flag, flag2, flag3, flag4;
1253 
1254   PetscFunctionBegin;
1255   PetscOptionsHeadBegin(PetscOptionsObject, "HYPRE ADS Options");
1256   PetscCall(PetscOptionsInt("-pc_hypre_ads_print_level", "Debugging output level for ADS", "None", jac->as_print, &jac->as_print, &flag));
1257   if (flag) PetscCallExternal(HYPRE_ADSSetPrintLevel, jac->hsolver, jac->as_print);
1258   PetscCall(PetscOptionsInt("-pc_hypre_ads_max_iter", "Maximum number of ADS multigrid iterations within PCApply", "None", jac->as_max_iter, &jac->as_max_iter, &flag));
1259   if (flag) PetscCallExternal(HYPRE_ADSSetMaxIter, jac->hsolver, jac->as_max_iter);
1260   PetscCall(PetscOptionsInt("-pc_hypre_ads_cycle_type", "Cycle type for ADS multigrid", "None", jac->ads_cycle_type, &jac->ads_cycle_type, &flag));
1261   if (flag) PetscCallExternal(HYPRE_ADSSetCycleType, jac->hsolver, jac->ads_cycle_type);
1262   PetscCall(PetscOptionsReal("-pc_hypre_ads_tol", "Error tolerance for ADS multigrid", "None", jac->as_tol, &jac->as_tol, &flag));
1263   if (flag) PetscCallExternal(HYPRE_ADSSetTol, jac->hsolver, jac->as_tol);
1264   PetscCall(PetscOptionsInt("-pc_hypre_ads_relax_type", "Relaxation type for ADS smoother", "None", jac->as_relax_type, &jac->as_relax_type, &flag));
1265   PetscCall(PetscOptionsInt("-pc_hypre_ads_relax_times", "Number of relaxation steps for ADS smoother", "None", jac->as_relax_times, &jac->as_relax_times, &flag2));
1266   PetscCall(PetscOptionsReal("-pc_hypre_ads_relax_weight", "Relaxation weight for ADS smoother", "None", jac->as_relax_weight, &jac->as_relax_weight, &flag3));
1267   PetscCall(PetscOptionsReal("-pc_hypre_ads_omega", "SSOR coefficient for ADS smoother", "None", jac->as_omega, &jac->as_omega, &flag4));
1268   if (flag || flag2 || flag3 || flag4) PetscCallExternal(HYPRE_ADSSetSmoothingOptions, jac->hsolver, jac->as_relax_type, jac->as_relax_times, jac->as_relax_weight, jac->as_omega);
1269   PetscCall(PetscOptionsReal("-pc_hypre_ads_ams_theta", "Threshold for strong coupling of AMS solver inside ADS", "None", jac->as_amg_alpha_theta, &jac->as_amg_alpha_theta, &flag));
1270   n = 5;
1271   PetscCall(PetscOptionsIntArray("-pc_hypre_ads_ams_options", "AMG options for AMS solver inside ADS", "None", jac->as_amg_alpha_opts, &n, &flag2));
1272   PetscCall(PetscOptionsInt("-pc_hypre_ads_ams_cycle_type", "Cycle type for AMS solver inside ADS", "None", jac->ams_cycle_type, &jac->ams_cycle_type, &flag3));
1273   if (flag || flag2 || flag3) {
1274     PetscCallExternal(HYPRE_ADSSetAMSOptions, jac->hsolver, jac->ams_cycle_type, /* AMS cycle type */
1275                       jac->as_amg_alpha_opts[0],                                 /* AMG coarsen type */
1276                       jac->as_amg_alpha_opts[1],                                 /* AMG agg_levels */
1277                       jac->as_amg_alpha_opts[2],                                 /* AMG relax_type */
1278                       jac->as_amg_alpha_theta, jac->as_amg_alpha_opts[3],        /* AMG interp_type */
1279                       jac->as_amg_alpha_opts[4]);                                /* AMG Pmax */
1280   }
1281   PetscCall(PetscOptionsReal("-pc_hypre_ads_amg_theta", "Threshold for strong coupling of vector AMG solver inside ADS", "None", jac->as_amg_beta_theta, &jac->as_amg_beta_theta, &flag));
1282   n = 5;
1283   PetscCall(PetscOptionsIntArray("-pc_hypre_ads_amg_options", "AMG options for vector AMG solver inside ADS", "None", jac->as_amg_beta_opts, &n, &flag2));
1284   if (flag || flag2) {
1285     PetscCallExternal(HYPRE_ADSSetAMGOptions, jac->hsolver, jac->as_amg_beta_opts[0], /* AMG coarsen type */
1286                       jac->as_amg_beta_opts[1],                                       /* AMG agg_levels */
1287                       jac->as_amg_beta_opts[2],                                       /* AMG relax_type */
1288                       jac->as_amg_beta_theta, jac->as_amg_beta_opts[3],               /* AMG interp_type */
1289                       jac->as_amg_beta_opts[4]);                                      /* AMG Pmax */
1290   }
1291   PetscOptionsHeadEnd();
1292   PetscFunctionReturn(PETSC_SUCCESS);
1293 }
1294 
1295 static PetscErrorCode PCView_HYPRE_ADS(PC pc, PetscViewer viewer)
1296 {
1297   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1298   PetscBool iascii;
1299 
1300   PetscFunctionBegin;
1301   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
1302   if (iascii) {
1303     PetscCall(PetscViewerASCIIPrintf(viewer, "  HYPRE ADS preconditioning\n"));
1304     PetscCall(PetscViewerASCIIPrintf(viewer, "    subspace iterations per application %" PetscInt_FMT "\n", jac->as_max_iter));
1305     PetscCall(PetscViewerASCIIPrintf(viewer, "    subspace cycle type %" PetscInt_FMT "\n", jac->ads_cycle_type));
1306     PetscCall(PetscViewerASCIIPrintf(viewer, "    subspace iteration tolerance %g\n", (double)jac->as_tol));
1307     PetscCall(PetscViewerASCIIPrintf(viewer, "    smoother type %" PetscInt_FMT "\n", jac->as_relax_type));
1308     PetscCall(PetscViewerASCIIPrintf(viewer, "    number of smoothing steps %" PetscInt_FMT "\n", jac->as_relax_times));
1309     PetscCall(PetscViewerASCIIPrintf(viewer, "    smoother weight %g\n", (double)jac->as_relax_weight));
1310     PetscCall(PetscViewerASCIIPrintf(viewer, "    smoother omega %g\n", (double)jac->as_omega));
1311     PetscCall(PetscViewerASCIIPrintf(viewer, "    AMS solver using boomerAMG\n"));
1312     PetscCall(PetscViewerASCIIPrintf(viewer, "        subspace cycle type %" PetscInt_FMT "\n", jac->ams_cycle_type));
1313     PetscCall(PetscViewerASCIIPrintf(viewer, "        coarsening type %" PetscInt_FMT "\n", jac->as_amg_alpha_opts[0]));
1314     PetscCall(PetscViewerASCIIPrintf(viewer, "        levels of aggressive coarsening %" PetscInt_FMT "\n", jac->as_amg_alpha_opts[1]));
1315     PetscCall(PetscViewerASCIIPrintf(viewer, "        relaxation type %" PetscInt_FMT "\n", jac->as_amg_alpha_opts[2]));
1316     PetscCall(PetscViewerASCIIPrintf(viewer, "        interpolation type %" PetscInt_FMT "\n", jac->as_amg_alpha_opts[3]));
1317     PetscCall(PetscViewerASCIIPrintf(viewer, "        max nonzero elements in interpolation rows %" PetscInt_FMT "\n", jac->as_amg_alpha_opts[4]));
1318     PetscCall(PetscViewerASCIIPrintf(viewer, "        strength threshold %g\n", (double)jac->as_amg_alpha_theta));
1319     PetscCall(PetscViewerASCIIPrintf(viewer, "    vector Poisson solver using boomerAMG\n"));
1320     PetscCall(PetscViewerASCIIPrintf(viewer, "        coarsening type %" PetscInt_FMT "\n", jac->as_amg_beta_opts[0]));
1321     PetscCall(PetscViewerASCIIPrintf(viewer, "        levels of aggressive coarsening %" PetscInt_FMT "\n", jac->as_amg_beta_opts[1]));
1322     PetscCall(PetscViewerASCIIPrintf(viewer, "        relaxation type %" PetscInt_FMT "\n", jac->as_amg_beta_opts[2]));
1323     PetscCall(PetscViewerASCIIPrintf(viewer, "        interpolation type %" PetscInt_FMT "\n", jac->as_amg_beta_opts[3]));
1324     PetscCall(PetscViewerASCIIPrintf(viewer, "        max nonzero elements in interpolation rows %" PetscInt_FMT "\n", jac->as_amg_beta_opts[4]));
1325     PetscCall(PetscViewerASCIIPrintf(viewer, "        strength threshold %g\n", (double)jac->as_amg_beta_theta));
1326   }
1327   PetscFunctionReturn(PETSC_SUCCESS);
1328 }
1329 
1330 static PetscErrorCode PCHYPRESetDiscreteGradient_HYPRE(PC pc, Mat G)
1331 {
1332   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1333   PetscBool ishypre;
1334 
1335   PetscFunctionBegin;
1336   PetscCall(PetscObjectTypeCompare((PetscObject)G, MATHYPRE, &ishypre));
1337   if (ishypre) {
1338     PetscCall(PetscObjectReference((PetscObject)G));
1339     PetscCall(MatDestroy(&jac->G));
1340     jac->G = G;
1341   } else {
1342     PetscCall(MatDestroy(&jac->G));
1343     PetscCall(MatConvert(G, MATHYPRE, MAT_INITIAL_MATRIX, &jac->G));
1344   }
1345   PetscFunctionReturn(PETSC_SUCCESS);
1346 }
1347 
1348 /*@
1349    PCHYPRESetDiscreteGradient - Set discrete gradient matrix for `PCHYPRE` type of ams or ads
1350 
1351    Collective
1352 
1353    Input Parameters:
1354 +  pc - the preconditioning context
1355 -  G - the discrete gradient
1356 
1357    Level: intermediate
1358 
1359    Notes:
1360     G should have as many rows as the number of edges and as many columns as the number of vertices in the mesh
1361 
1362     Each row of G has 2 nonzeros, with column indexes being the global indexes of edge's endpoints: matrix entries are +1 and -1 depending on edge orientation
1363 
1364    Developer Note:
1365    This automatically converts the matrix to `MATHYPRE` if it is not already of that type
1366 
1367 .seealso: `PCHYPRE`, `PCHYPRESetDiscreteCurl()`
1368 @*/
1369 PetscErrorCode PCHYPRESetDiscreteGradient(PC pc, Mat G)
1370 {
1371   PetscFunctionBegin;
1372   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1373   PetscValidHeaderSpecific(G, MAT_CLASSID, 2);
1374   PetscCheckSameComm(pc, 1, G, 2);
1375   PetscTryMethod(pc, "PCHYPRESetDiscreteGradient_C", (PC, Mat), (pc, G));
1376   PetscFunctionReturn(PETSC_SUCCESS);
1377 }
1378 
1379 static PetscErrorCode PCHYPRESetDiscreteCurl_HYPRE(PC pc, Mat C)
1380 {
1381   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1382   PetscBool ishypre;
1383 
1384   PetscFunctionBegin;
1385   PetscCall(PetscObjectTypeCompare((PetscObject)C, MATHYPRE, &ishypre));
1386   if (ishypre) {
1387     PetscCall(PetscObjectReference((PetscObject)C));
1388     PetscCall(MatDestroy(&jac->C));
1389     jac->C = C;
1390   } else {
1391     PetscCall(MatDestroy(&jac->C));
1392     PetscCall(MatConvert(C, MATHYPRE, MAT_INITIAL_MATRIX, &jac->C));
1393   }
1394   PetscFunctionReturn(PETSC_SUCCESS);
1395 }
1396 
1397 /*@
1398    PCHYPRESetDiscreteCurl - Set discrete curl matrx for `PCHYPRE` type of ads
1399 
1400    Collective
1401 
1402    Input Parameters:
1403 +  pc - the preconditioning context
1404 -  C - the discrete curl
1405 
1406    Level: intermediate
1407 
1408    Notes:
1409     C should have as many rows as the number of faces and as many columns as the number of edges in the mesh
1410 
1411     Each row of G has as many nonzeros as the number of edges of a face, with column indexes being the global indexes of the corresponding edge: matrix entries are +1 and -1 depending on edge orientation with respect to the face orientation
1412 
1413    Developer Note:
1414    This automatically converts the matrix to `MATHYPRE` if it is not already of that type
1415 
1416    If this is only for  `PCHYPRE` type of ads it should be called `PCHYPREADSSetDiscreteCurl()`
1417 
1418 .seealso: `PCHYPRE`, `PCHYPRESetDiscreteGradient()`
1419 @*/
1420 PetscErrorCode PCHYPRESetDiscreteCurl(PC pc, Mat C)
1421 {
1422   PetscFunctionBegin;
1423   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1424   PetscValidHeaderSpecific(C, MAT_CLASSID, 2);
1425   PetscCheckSameComm(pc, 1, C, 2);
1426   PetscTryMethod(pc, "PCHYPRESetDiscreteCurl_C", (PC, Mat), (pc, C));
1427   PetscFunctionReturn(PETSC_SUCCESS);
1428 }
1429 
1430 static PetscErrorCode PCHYPRESetInterpolations_HYPRE(PC pc, PetscInt dim, Mat RT_PiFull, Mat RT_Pi[], Mat ND_PiFull, Mat ND_Pi[])
1431 {
1432   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1433   PetscBool ishypre;
1434   PetscInt  i;
1435   PetscFunctionBegin;
1436 
1437   PetscCall(MatDestroy(&jac->RT_PiFull));
1438   PetscCall(MatDestroy(&jac->ND_PiFull));
1439   for (i = 0; i < 3; ++i) {
1440     PetscCall(MatDestroy(&jac->RT_Pi[i]));
1441     PetscCall(MatDestroy(&jac->ND_Pi[i]));
1442   }
1443 
1444   jac->dim = dim;
1445   if (RT_PiFull) {
1446     PetscCall(PetscObjectTypeCompare((PetscObject)RT_PiFull, MATHYPRE, &ishypre));
1447     if (ishypre) {
1448       PetscCall(PetscObjectReference((PetscObject)RT_PiFull));
1449       jac->RT_PiFull = RT_PiFull;
1450     } else {
1451       PetscCall(MatConvert(RT_PiFull, MATHYPRE, MAT_INITIAL_MATRIX, &jac->RT_PiFull));
1452     }
1453   }
1454   if (RT_Pi) {
1455     for (i = 0; i < dim; ++i) {
1456       if (RT_Pi[i]) {
1457         PetscCall(PetscObjectTypeCompare((PetscObject)RT_Pi[i], MATHYPRE, &ishypre));
1458         if (ishypre) {
1459           PetscCall(PetscObjectReference((PetscObject)RT_Pi[i]));
1460           jac->RT_Pi[i] = RT_Pi[i];
1461         } else {
1462           PetscCall(MatConvert(RT_Pi[i], MATHYPRE, MAT_INITIAL_MATRIX, &jac->RT_Pi[i]));
1463         }
1464       }
1465     }
1466   }
1467   if (ND_PiFull) {
1468     PetscCall(PetscObjectTypeCompare((PetscObject)ND_PiFull, MATHYPRE, &ishypre));
1469     if (ishypre) {
1470       PetscCall(PetscObjectReference((PetscObject)ND_PiFull));
1471       jac->ND_PiFull = ND_PiFull;
1472     } else {
1473       PetscCall(MatConvert(ND_PiFull, MATHYPRE, MAT_INITIAL_MATRIX, &jac->ND_PiFull));
1474     }
1475   }
1476   if (ND_Pi) {
1477     for (i = 0; i < dim; ++i) {
1478       if (ND_Pi[i]) {
1479         PetscCall(PetscObjectTypeCompare((PetscObject)ND_Pi[i], MATHYPRE, &ishypre));
1480         if (ishypre) {
1481           PetscCall(PetscObjectReference((PetscObject)ND_Pi[i]));
1482           jac->ND_Pi[i] = ND_Pi[i];
1483         } else {
1484           PetscCall(MatConvert(ND_Pi[i], MATHYPRE, MAT_INITIAL_MATRIX, &jac->ND_Pi[i]));
1485         }
1486       }
1487     }
1488   }
1489 
1490   PetscFunctionReturn(PETSC_SUCCESS);
1491 }
1492 
1493 /*@
1494    PCHYPRESetInterpolations - Set interpolation matrices for `PCHYPRE` type of ams or ads
1495 
1496    Collective
1497 
1498    Input Parameters:
1499 +  pc - the preconditioning context
1500 .  dim - the dimension of the problem, only used in AMS
1501 .  RT_PiFull - Raviart-Thomas interpolation matrix
1502 .  RT_Pi - x/y/z component of Raviart-Thomas interpolation matrix
1503 .  ND_PiFull - Nedelec interpolation matrix
1504 -  ND_Pi - x/y/z component of Nedelec interpolation matrix
1505 
1506    Level: intermediate
1507 
1508    Notes:
1509     For AMS, only Nedelec interpolation matrices are needed, the Raviart-Thomas interpolation matrices can be set to NULL.
1510 
1511     For ADS, both type of interpolation matrices are needed.
1512 
1513    Developer Note:
1514    This automatically converts the matrix to `MATHYPRE` if it is not already of that type
1515 
1516 .seealso: `PCHYPRE`
1517 @*/
1518 PetscErrorCode PCHYPRESetInterpolations(PC pc, PetscInt dim, Mat RT_PiFull, Mat RT_Pi[], Mat ND_PiFull, Mat ND_Pi[])
1519 {
1520   PetscInt i;
1521 
1522   PetscFunctionBegin;
1523   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1524   if (RT_PiFull) {
1525     PetscValidHeaderSpecific(RT_PiFull, MAT_CLASSID, 3);
1526     PetscCheckSameComm(pc, 1, RT_PiFull, 3);
1527   }
1528   if (RT_Pi) {
1529     PetscValidPointer(RT_Pi, 4);
1530     for (i = 0; i < dim; ++i) {
1531       if (RT_Pi[i]) {
1532         PetscValidHeaderSpecific(RT_Pi[i], MAT_CLASSID, 4);
1533         PetscCheckSameComm(pc, 1, RT_Pi[i], 4);
1534       }
1535     }
1536   }
1537   if (ND_PiFull) {
1538     PetscValidHeaderSpecific(ND_PiFull, MAT_CLASSID, 5);
1539     PetscCheckSameComm(pc, 1, ND_PiFull, 5);
1540   }
1541   if (ND_Pi) {
1542     PetscValidPointer(ND_Pi, 6);
1543     for (i = 0; i < dim; ++i) {
1544       if (ND_Pi[i]) {
1545         PetscValidHeaderSpecific(ND_Pi[i], MAT_CLASSID, 6);
1546         PetscCheckSameComm(pc, 1, ND_Pi[i], 6);
1547       }
1548     }
1549   }
1550   PetscTryMethod(pc, "PCHYPRESetInterpolations_C", (PC, PetscInt, Mat, Mat[], Mat, Mat[]), (pc, dim, RT_PiFull, RT_Pi, ND_PiFull, ND_Pi));
1551   PetscFunctionReturn(PETSC_SUCCESS);
1552 }
1553 
1554 static PetscErrorCode PCHYPRESetPoissonMatrix_HYPRE(PC pc, Mat A, PetscBool isalpha)
1555 {
1556   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1557   PetscBool ishypre;
1558 
1559   PetscFunctionBegin;
1560   PetscCall(PetscObjectTypeCompare((PetscObject)A, MATHYPRE, &ishypre));
1561   if (ishypre) {
1562     if (isalpha) {
1563       PetscCall(PetscObjectReference((PetscObject)A));
1564       PetscCall(MatDestroy(&jac->alpha_Poisson));
1565       jac->alpha_Poisson = A;
1566     } else {
1567       if (A) {
1568         PetscCall(PetscObjectReference((PetscObject)A));
1569       } else {
1570         jac->ams_beta_is_zero = PETSC_TRUE;
1571       }
1572       PetscCall(MatDestroy(&jac->beta_Poisson));
1573       jac->beta_Poisson = A;
1574     }
1575   } else {
1576     if (isalpha) {
1577       PetscCall(MatDestroy(&jac->alpha_Poisson));
1578       PetscCall(MatConvert(A, MATHYPRE, MAT_INITIAL_MATRIX, &jac->alpha_Poisson));
1579     } else {
1580       if (A) {
1581         PetscCall(MatDestroy(&jac->beta_Poisson));
1582         PetscCall(MatConvert(A, MATHYPRE, MAT_INITIAL_MATRIX, &jac->beta_Poisson));
1583       } else {
1584         PetscCall(MatDestroy(&jac->beta_Poisson));
1585         jac->ams_beta_is_zero = PETSC_TRUE;
1586       }
1587     }
1588   }
1589   PetscFunctionReturn(PETSC_SUCCESS);
1590 }
1591 
1592 /*@
1593    PCHYPRESetAlphaPoissonMatrix - Set vector Poisson matrix for `PCHYPRE` of type ams
1594 
1595    Collective
1596 
1597    Input Parameters:
1598 +  pc - the preconditioning context
1599 -  A - the matrix
1600 
1601    Level: intermediate
1602 
1603    Note:
1604     A should be obtained by discretizing the vector valued Poisson problem with linear finite elements
1605 
1606    Developer Note:
1607    This automatically converts the matrix to `MATHYPRE` if it is not already of that type
1608 
1609    If this is only for  `PCHYPRE` type of ams it should be called `PCHYPREAMSSetAlphaPoissonMatrix()`
1610 
1611 .seealso: `PCHYPRE`, `PCHYPRESetDiscreteGradient()`, `PCHYPRESetDiscreteCurl()`, `PCHYPRESetBetaPoissonMatrix()`
1612 @*/
1613 PetscErrorCode PCHYPRESetAlphaPoissonMatrix(PC pc, Mat A)
1614 {
1615   PetscFunctionBegin;
1616   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1617   PetscValidHeaderSpecific(A, MAT_CLASSID, 2);
1618   PetscCheckSameComm(pc, 1, A, 2);
1619   PetscTryMethod(pc, "PCHYPRESetPoissonMatrix_C", (PC, Mat, PetscBool), (pc, A, PETSC_TRUE));
1620   PetscFunctionReturn(PETSC_SUCCESS);
1621 }
1622 
1623 /*@
1624    PCHYPRESetBetaPoissonMatrix - Set Poisson matrix for `PCHYPRE` of type ams
1625 
1626    Collective
1627 
1628    Input Parameters:
1629 +  pc - the preconditioning context
1630 -  A - the matrix, or NULL to turn it off
1631 
1632    Level: intermediate
1633 
1634    Note:
1635    A should be obtained by discretizing the Poisson problem with linear finite elements.
1636 
1637    Developer Note:
1638    This automatically converts the matrix to `MATHYPRE` if it is not already of that type
1639 
1640    If this is only for  `PCHYPRE` type of ams it should be called `PCHYPREAMSPCHYPRESetBetaPoissonMatrix()`
1641 
1642 .seealso: `PCHYPRE`, `PCHYPRESetDiscreteGradient()`, `PCHYPRESetDiscreteCurl()`, `PCHYPRESetAlphaPoissonMatrix()`
1643 @*/
1644 PetscErrorCode PCHYPRESetBetaPoissonMatrix(PC pc, Mat A)
1645 {
1646   PetscFunctionBegin;
1647   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1648   if (A) {
1649     PetscValidHeaderSpecific(A, MAT_CLASSID, 2);
1650     PetscCheckSameComm(pc, 1, A, 2);
1651   }
1652   PetscTryMethod(pc, "PCHYPRESetPoissonMatrix_C", (PC, Mat, PetscBool), (pc, A, PETSC_FALSE));
1653   PetscFunctionReturn(PETSC_SUCCESS);
1654 }
1655 
1656 static PetscErrorCode PCHYPRESetEdgeConstantVectors_HYPRE(PC pc, Vec ozz, Vec zoz, Vec zzo)
1657 {
1658   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1659 
1660   PetscFunctionBegin;
1661   /* throw away any vector if already set */
1662   PetscCall(VecHYPRE_IJVectorDestroy(&jac->constants[0]));
1663   PetscCall(VecHYPRE_IJVectorDestroy(&jac->constants[1]));
1664   PetscCall(VecHYPRE_IJVectorDestroy(&jac->constants[2]));
1665   PetscCall(VecHYPRE_IJVectorCreate(ozz->map, &jac->constants[0]));
1666   PetscCall(VecHYPRE_IJVectorCopy(ozz, jac->constants[0]));
1667   PetscCall(VecHYPRE_IJVectorCreate(zoz->map, &jac->constants[1]));
1668   PetscCall(VecHYPRE_IJVectorCopy(zoz, jac->constants[1]));
1669   jac->dim = 2;
1670   if (zzo) {
1671     PetscCall(VecHYPRE_IJVectorCreate(zzo->map, &jac->constants[2]));
1672     PetscCall(VecHYPRE_IJVectorCopy(zzo, jac->constants[2]));
1673     jac->dim++;
1674   }
1675   PetscFunctionReturn(PETSC_SUCCESS);
1676 }
1677 
1678 /*@
1679    PCHYPRESetEdgeConstantVectors - Set the representation of the constant vector fields in the edge element basis for `PCHYPRE` of type ams
1680 
1681    Collective
1682 
1683    Input Parameters:
1684 +  pc - the preconditioning context
1685 .  ozz - vector representing (1,0,0) (or (1,0) in 2D)
1686 .  zoz - vector representing (0,1,0) (or (0,1) in 2D)
1687 -  zzo - vector representing (0,0,1) (use NULL in 2D)
1688 
1689    Level: intermediate
1690 
1691    Developer Note:
1692    If this is only for  `PCHYPRE` type of ams it should be called `PCHYPREAMSSetEdgeConstantVectors()`
1693 
1694 .seealso: `PCHYPRE`, `PCHYPRESetDiscreteGradient()`, `PCHYPRESetDiscreteCurl()`, `PCHYPRESetAlphaPoissonMatrix()`
1695 @*/
1696 PetscErrorCode PCHYPRESetEdgeConstantVectors(PC pc, Vec ozz, Vec zoz, Vec zzo)
1697 {
1698   PetscFunctionBegin;
1699   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1700   PetscValidHeaderSpecific(ozz, VEC_CLASSID, 2);
1701   PetscValidHeaderSpecific(zoz, VEC_CLASSID, 3);
1702   if (zzo) PetscValidHeaderSpecific(zzo, VEC_CLASSID, 4);
1703   PetscCheckSameComm(pc, 1, ozz, 2);
1704   PetscCheckSameComm(pc, 1, zoz, 3);
1705   if (zzo) PetscCheckSameComm(pc, 1, zzo, 4);
1706   PetscTryMethod(pc, "PCHYPRESetEdgeConstantVectors_C", (PC, Vec, Vec, Vec), (pc, ozz, zoz, zzo));
1707   PetscFunctionReturn(PETSC_SUCCESS);
1708 }
1709 
1710 static PetscErrorCode PCHYPREAMSSetInteriorNodes_HYPRE(PC pc, Vec interior)
1711 {
1712   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1713 
1714   PetscFunctionBegin;
1715   PetscCall(VecHYPRE_IJVectorDestroy(&jac->interior));
1716   PetscCall(VecHYPRE_IJVectorCreate(interior->map, &jac->interior));
1717   PetscCall(VecHYPRE_IJVectorCopy(interior, jac->interior));
1718   jac->ams_beta_is_zero_part = PETSC_TRUE;
1719   PetscFunctionReturn(PETSC_SUCCESS);
1720 }
1721 
1722 /*@
1723   PCHYPREAMSSetInteriorNodes - Set the list of interior nodes to a zero-conductivity region for `PCHYPRE` of type ams
1724 
1725    Collective
1726 
1727    Input Parameters:
1728 +  pc - the preconditioning context
1729 -  interior - vector. node is interior if its entry in the array is 1.0.
1730 
1731    Level: intermediate
1732 
1733    Note:
1734    This calls `HYPRE_AMSSetInteriorNodes()`
1735 
1736    Developer Note:
1737    If this is only for  `PCHYPRE` type of ams it should be called `PCHYPREAMSSetInteriorNodes()`
1738 
1739 .seealso: `PCHYPRE`, `PCHYPRESetDiscreteGradient()`, `PCHYPRESetDiscreteCurl()`, `PCHYPRESetAlphaPoissonMatrix()`
1740 @*/
1741 PetscErrorCode PCHYPREAMSSetInteriorNodes(PC pc, Vec interior)
1742 {
1743   PetscFunctionBegin;
1744   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
1745   PetscValidHeaderSpecific(interior, VEC_CLASSID, 2);
1746   PetscCheckSameComm(pc, 1, interior, 2);
1747   PetscTryMethod(pc, "PCHYPREAMSSetInteriorNodes_C", (PC, Vec), (pc, interior));
1748   PetscFunctionReturn(PETSC_SUCCESS);
1749 }
1750 
1751 static PetscErrorCode PCSetCoordinates_HYPRE(PC pc, PetscInt dim, PetscInt nloc, PetscReal *coords)
1752 {
1753   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1754   Vec       tv;
1755   PetscInt  i;
1756 
1757   PetscFunctionBegin;
1758   /* throw away any coordinate vector if already set */
1759   PetscCall(VecHYPRE_IJVectorDestroy(&jac->coords[0]));
1760   PetscCall(VecHYPRE_IJVectorDestroy(&jac->coords[1]));
1761   PetscCall(VecHYPRE_IJVectorDestroy(&jac->coords[2]));
1762   jac->dim = dim;
1763 
1764   /* compute IJ vector for coordinates */
1765   PetscCall(VecCreate(PetscObjectComm((PetscObject)pc), &tv));
1766   PetscCall(VecSetType(tv, VECSTANDARD));
1767   PetscCall(VecSetSizes(tv, nloc, PETSC_DECIDE));
1768   for (i = 0; i < dim; i++) {
1769     PetscScalar *array;
1770     PetscInt     j;
1771 
1772     PetscCall(VecHYPRE_IJVectorCreate(tv->map, &jac->coords[i]));
1773     PetscCall(VecGetArrayWrite(tv, &array));
1774     for (j = 0; j < nloc; j++) array[j] = coords[j * dim + i];
1775     PetscCall(VecRestoreArrayWrite(tv, &array));
1776     PetscCall(VecHYPRE_IJVectorCopy(tv, jac->coords[i]));
1777   }
1778   PetscCall(VecDestroy(&tv));
1779   PetscFunctionReturn(PETSC_SUCCESS);
1780 }
1781 
1782 static PetscErrorCode PCHYPREGetType_HYPRE(PC pc, const char *name[])
1783 {
1784   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1785 
1786   PetscFunctionBegin;
1787   *name = jac->hypre_type;
1788   PetscFunctionReturn(PETSC_SUCCESS);
1789 }
1790 
1791 static PetscErrorCode PCHYPRESetType_HYPRE(PC pc, const char name[])
1792 {
1793   PC_HYPRE *jac = (PC_HYPRE *)pc->data;
1794   PetscBool flag;
1795 
1796   PetscFunctionBegin;
1797   if (jac->hypre_type) {
1798     PetscCall(PetscStrcmp(jac->hypre_type, name, &flag));
1799     PetscCheck(flag, PetscObjectComm((PetscObject)pc), PETSC_ERR_ORDER, "Cannot reset the HYPRE preconditioner type once it has been set");
1800     PetscFunctionReturn(PETSC_SUCCESS);
1801   } else {
1802     PetscCall(PetscStrallocpy(name, &jac->hypre_type));
1803   }
1804 
1805   jac->maxiter         = PETSC_DEFAULT;
1806   jac->tol             = PETSC_DEFAULT;
1807   jac->printstatistics = PetscLogPrintInfo;
1808 
1809   PetscCall(PetscStrcmp("pilut", jac->hypre_type, &flag));
1810   if (flag) {
1811     PetscCall(PetscCommGetComm(PetscObjectComm((PetscObject)pc), &jac->comm_hypre));
1812     PetscCallExternal(HYPRE_ParCSRPilutCreate, jac->comm_hypre, &jac->hsolver);
1813     pc->ops->setfromoptions = PCSetFromOptions_HYPRE_Pilut;
1814     pc->ops->view           = PCView_HYPRE_Pilut;
1815     jac->destroy            = HYPRE_ParCSRPilutDestroy;
1816     jac->setup              = HYPRE_ParCSRPilutSetup;
1817     jac->solve              = HYPRE_ParCSRPilutSolve;
1818     jac->factorrowsize      = PETSC_DEFAULT;
1819     PetscFunctionReturn(PETSC_SUCCESS);
1820   }
1821   PetscCall(PetscStrcmp("euclid", jac->hypre_type, &flag));
1822   if (flag) {
1823 #if defined(PETSC_USE_64BIT_INDICES)
1824     SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Hypre Euclid does not support 64 bit indices");
1825 #endif
1826     PetscCall(PetscCommGetComm(PetscObjectComm((PetscObject)pc), &jac->comm_hypre));
1827     PetscCallExternal(HYPRE_EuclidCreate, jac->comm_hypre, &jac->hsolver);
1828     pc->ops->setfromoptions = PCSetFromOptions_HYPRE_Euclid;
1829     pc->ops->view           = PCView_HYPRE_Euclid;
1830     jac->destroy            = HYPRE_EuclidDestroy;
1831     jac->setup              = HYPRE_EuclidSetup;
1832     jac->solve              = HYPRE_EuclidSolve;
1833     jac->factorrowsize      = PETSC_DEFAULT;
1834     jac->eu_level           = PETSC_DEFAULT; /* default */
1835     PetscFunctionReturn(PETSC_SUCCESS);
1836   }
1837   PetscCall(PetscStrcmp("parasails", jac->hypre_type, &flag));
1838   if (flag) {
1839     PetscCall(PetscCommGetComm(PetscObjectComm((PetscObject)pc), &jac->comm_hypre));
1840     PetscCallExternal(HYPRE_ParaSailsCreate, jac->comm_hypre, &jac->hsolver);
1841     pc->ops->setfromoptions = PCSetFromOptions_HYPRE_ParaSails;
1842     pc->ops->view           = PCView_HYPRE_ParaSails;
1843     jac->destroy            = HYPRE_ParaSailsDestroy;
1844     jac->setup              = HYPRE_ParaSailsSetup;
1845     jac->solve              = HYPRE_ParaSailsSolve;
1846     /* initialize */
1847     jac->nlevels   = 1;
1848     jac->threshold = .1;
1849     jac->filter    = .1;
1850     jac->loadbal   = 0;
1851     if (PetscLogPrintInfo) jac->logging = (int)PETSC_TRUE;
1852     else jac->logging = (int)PETSC_FALSE;
1853 
1854     jac->ruse = (int)PETSC_FALSE;
1855     jac->symt = 0;
1856     PetscCallExternal(HYPRE_ParaSailsSetParams, jac->hsolver, jac->threshold, jac->nlevels);
1857     PetscCallExternal(HYPRE_ParaSailsSetFilter, jac->hsolver, jac->filter);
1858     PetscCallExternal(HYPRE_ParaSailsSetLoadbal, jac->hsolver, jac->loadbal);
1859     PetscCallExternal(HYPRE_ParaSailsSetLogging, jac->hsolver, jac->logging);
1860     PetscCallExternal(HYPRE_ParaSailsSetReuse, jac->hsolver, jac->ruse);
1861     PetscCallExternal(HYPRE_ParaSailsSetSym, jac->hsolver, jac->symt);
1862     PetscFunctionReturn(PETSC_SUCCESS);
1863   }
1864   PetscCall(PetscStrcmp("boomeramg", jac->hypre_type, &flag));
1865   if (flag) {
1866     PetscCallExternal(HYPRE_BoomerAMGCreate, &jac->hsolver);
1867     pc->ops->setfromoptions  = PCSetFromOptions_HYPRE_BoomerAMG;
1868     pc->ops->view            = PCView_HYPRE_BoomerAMG;
1869     pc->ops->applytranspose  = PCApplyTranspose_HYPRE_BoomerAMG;
1870     pc->ops->applyrichardson = PCApplyRichardson_HYPRE_BoomerAMG;
1871     PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGetInterpolations_C", PCGetInterpolations_BoomerAMG));
1872     PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGetCoarseOperators_C", PCGetCoarseOperators_BoomerAMG));
1873     jac->destroy         = HYPRE_BoomerAMGDestroy;
1874     jac->setup           = HYPRE_BoomerAMGSetup;
1875     jac->solve           = HYPRE_BoomerAMGSolve;
1876     jac->applyrichardson = PETSC_FALSE;
1877     /* these defaults match the hypre defaults */
1878     jac->cycletype       = 1;
1879     jac->maxlevels       = 25;
1880     jac->maxiter         = 1;
1881     jac->tol             = 0.0; /* tolerance of zero indicates use as preconditioner (suppresses convergence errors) */
1882     jac->truncfactor     = 0.0;
1883     jac->strongthreshold = .25;
1884     jac->maxrowsum       = .9;
1885     jac->coarsentype     = 6;
1886     jac->measuretype     = 0;
1887     jac->gridsweeps[0] = jac->gridsweeps[1] = jac->gridsweeps[2] = 1;
1888     jac->smoothtype                                              = -1; /* Not set by default */
1889     jac->smoothnumlevels                                         = 25;
1890     jac->eu_level                                                = 0;
1891     jac->eu_droptolerance                                        = 0;
1892     jac->eu_bj                                                   = 0;
1893     jac->relaxtype[0] = jac->relaxtype[1] = 6; /* Defaults to SYMMETRIC since in PETSc we are using a PC - most likely with CG */
1894     jac->relaxtype[2]                     = 9; /*G.E. */
1895     jac->relaxweight                      = 1.0;
1896     jac->outerrelaxweight                 = 1.0;
1897     jac->relaxorder                       = 1;
1898     jac->interptype                       = 0;
1899     jac->Rtype                            = 0;
1900     jac->Rstrongthreshold                 = 0.25;
1901     jac->Rfilterthreshold                 = 0.0;
1902     jac->Adroptype                        = -1;
1903     jac->Adroptol                         = 0.0;
1904     jac->agg_nl                           = 0;
1905     jac->agg_interptype                   = 4;
1906     jac->pmax                             = 0;
1907     jac->truncfactor                      = 0.0;
1908     jac->agg_num_paths                    = 1;
1909     jac->maxc                             = 9;
1910     jac->minc                             = 1;
1911     jac->nodal_coarsening                 = 0;
1912     jac->nodal_coarsening_diag            = 0;
1913     jac->vec_interp_variant               = 0;
1914     jac->vec_interp_qmax                  = 0;
1915     jac->vec_interp_smooth                = PETSC_FALSE;
1916     jac->interp_refine                    = 0;
1917     jac->nodal_relax                      = PETSC_FALSE;
1918     jac->nodal_relax_levels               = 1;
1919     jac->rap2                             = 0;
1920 
1921     /* GPU defaults
1922          from https://hypre.readthedocs.io/en/latest/solvers-boomeramg.html#gpu-supported-options
1923          and /src/parcsr_ls/par_amg.c */
1924 #if defined(PETSC_HAVE_HYPRE_DEVICE)
1925     jac->keeptranspose  = PETSC_TRUE;
1926     jac->mod_rap2       = 1;
1927     jac->coarsentype    = 8;
1928     jac->relaxorder     = 0;
1929     jac->interptype     = 6;
1930     jac->relaxtype[0]   = 18;
1931     jac->relaxtype[1]   = 18;
1932     jac->agg_interptype = 7;
1933 #else
1934     jac->keeptranspose = PETSC_FALSE;
1935     jac->mod_rap2      = 0;
1936 #endif
1937     PetscCallExternal(HYPRE_BoomerAMGSetCycleType, jac->hsolver, jac->cycletype);
1938     PetscCallExternal(HYPRE_BoomerAMGSetMaxLevels, jac->hsolver, jac->maxlevels);
1939     PetscCallExternal(HYPRE_BoomerAMGSetMaxIter, jac->hsolver, jac->maxiter);
1940     PetscCallExternal(HYPRE_BoomerAMGSetTol, jac->hsolver, jac->tol);
1941     PetscCallExternal(HYPRE_BoomerAMGSetTruncFactor, jac->hsolver, jac->truncfactor);
1942     PetscCallExternal(HYPRE_BoomerAMGSetStrongThreshold, jac->hsolver, jac->strongthreshold);
1943     PetscCallExternal(HYPRE_BoomerAMGSetMaxRowSum, jac->hsolver, jac->maxrowsum);
1944     PetscCallExternal(HYPRE_BoomerAMGSetCoarsenType, jac->hsolver, jac->coarsentype);
1945     PetscCallExternal(HYPRE_BoomerAMGSetMeasureType, jac->hsolver, jac->measuretype);
1946     PetscCallExternal(HYPRE_BoomerAMGSetRelaxOrder, jac->hsolver, jac->relaxorder);
1947     PetscCallExternal(HYPRE_BoomerAMGSetInterpType, jac->hsolver, jac->interptype);
1948     PetscCallExternal(HYPRE_BoomerAMGSetAggNumLevels, jac->hsolver, jac->agg_nl);
1949     PetscCallExternal(HYPRE_BoomerAMGSetAggInterpType, jac->hsolver, jac->agg_interptype);
1950     PetscCallExternal(HYPRE_BoomerAMGSetPMaxElmts, jac->hsolver, jac->pmax);
1951     PetscCallExternal(HYPRE_BoomerAMGSetNumPaths, jac->hsolver, jac->agg_num_paths);
1952     PetscCallExternal(HYPRE_BoomerAMGSetRelaxType, jac->hsolver, jac->relaxtype[0]);  /* defaults coarse to 9 */
1953     PetscCallExternal(HYPRE_BoomerAMGSetNumSweeps, jac->hsolver, jac->gridsweeps[0]); /* defaults coarse to 1 */
1954     PetscCallExternal(HYPRE_BoomerAMGSetMaxCoarseSize, jac->hsolver, jac->maxc);
1955     PetscCallExternal(HYPRE_BoomerAMGSetMinCoarseSize, jac->hsolver, jac->minc);
1956     /* GPU */
1957 #if PETSC_PKG_HYPRE_VERSION_GE(2, 18, 0)
1958     PetscCallExternal(HYPRE_BoomerAMGSetKeepTranspose, jac->hsolver, jac->keeptranspose ? 1 : 0);
1959     PetscCallExternal(HYPRE_BoomerAMGSetRAP2, jac->hsolver, jac->rap2);
1960     PetscCallExternal(HYPRE_BoomerAMGSetModuleRAP2, jac->hsolver, jac->mod_rap2);
1961 #endif
1962 
1963     /* AIR */
1964 #if PETSC_PKG_HYPRE_VERSION_GE(2, 18, 0)
1965     PetscCallExternal(HYPRE_BoomerAMGSetRestriction, jac->hsolver, jac->Rtype);
1966     PetscCallExternal(HYPRE_BoomerAMGSetStrongThresholdR, jac->hsolver, jac->Rstrongthreshold);
1967     PetscCallExternal(HYPRE_BoomerAMGSetFilterThresholdR, jac->hsolver, jac->Rfilterthreshold);
1968     PetscCallExternal(HYPRE_BoomerAMGSetADropTol, jac->hsolver, jac->Adroptol);
1969     PetscCallExternal(HYPRE_BoomerAMGSetADropType, jac->hsolver, jac->Adroptype);
1970 #endif
1971     PetscFunctionReturn(PETSC_SUCCESS);
1972   }
1973   PetscCall(PetscStrcmp("ams", jac->hypre_type, &flag));
1974   if (flag) {
1975     PetscCallExternal(HYPRE_AMSCreate, &jac->hsolver);
1976     pc->ops->setfromoptions = PCSetFromOptions_HYPRE_AMS;
1977     pc->ops->view           = PCView_HYPRE_AMS;
1978     jac->destroy            = HYPRE_AMSDestroy;
1979     jac->setup              = HYPRE_AMSSetup;
1980     jac->solve              = HYPRE_AMSSolve;
1981     jac->coords[0]          = NULL;
1982     jac->coords[1]          = NULL;
1983     jac->coords[2]          = NULL;
1984     jac->interior           = NULL;
1985     /* solver parameters: these are borrowed from mfem package, and they are not the default values from HYPRE AMS */
1986     jac->as_print       = 0;
1987     jac->as_max_iter    = 1;  /* used as a preconditioner */
1988     jac->as_tol         = 0.; /* used as a preconditioner */
1989     jac->ams_cycle_type = 13;
1990     /* Smoothing options */
1991     jac->as_relax_type   = 2;
1992     jac->as_relax_times  = 1;
1993     jac->as_relax_weight = 1.0;
1994     jac->as_omega        = 1.0;
1995     /* Vector valued Poisson AMG solver parameters: coarsen type, agg_levels, relax_type, interp_type, Pmax */
1996     jac->as_amg_alpha_opts[0] = 10;
1997     jac->as_amg_alpha_opts[1] = 1;
1998     jac->as_amg_alpha_opts[2] = 6;
1999     jac->as_amg_alpha_opts[3] = 6;
2000     jac->as_amg_alpha_opts[4] = 4;
2001     jac->as_amg_alpha_theta   = 0.25;
2002     /* Scalar Poisson AMG solver parameters: coarsen type, agg_levels, relax_type, interp_type, Pmax */
2003     jac->as_amg_beta_opts[0] = 10;
2004     jac->as_amg_beta_opts[1] = 1;
2005     jac->as_amg_beta_opts[2] = 6;
2006     jac->as_amg_beta_opts[3] = 6;
2007     jac->as_amg_beta_opts[4] = 4;
2008     jac->as_amg_beta_theta   = 0.25;
2009     PetscCallExternal(HYPRE_AMSSetPrintLevel, jac->hsolver, jac->as_print);
2010     PetscCallExternal(HYPRE_AMSSetMaxIter, jac->hsolver, jac->as_max_iter);
2011     PetscCallExternal(HYPRE_AMSSetCycleType, jac->hsolver, jac->ams_cycle_type);
2012     PetscCallExternal(HYPRE_AMSSetTol, jac->hsolver, jac->as_tol);
2013     PetscCallExternal(HYPRE_AMSSetSmoothingOptions, jac->hsolver, jac->as_relax_type, jac->as_relax_times, jac->as_relax_weight, jac->as_omega);
2014     PetscCallExternal(HYPRE_AMSSetAlphaAMGOptions, jac->hsolver, jac->as_amg_alpha_opts[0], /* AMG coarsen type */
2015                       jac->as_amg_alpha_opts[1],                                            /* AMG agg_levels */
2016                       jac->as_amg_alpha_opts[2],                                            /* AMG relax_type */
2017                       jac->as_amg_alpha_theta, jac->as_amg_alpha_opts[3],                   /* AMG interp_type */
2018                       jac->as_amg_alpha_opts[4]);                                           /* AMG Pmax */
2019     PetscCallExternal(HYPRE_AMSSetBetaAMGOptions, jac->hsolver, jac->as_amg_beta_opts[0],   /* AMG coarsen type */
2020                       jac->as_amg_beta_opts[1],                                             /* AMG agg_levels */
2021                       jac->as_amg_beta_opts[2],                                             /* AMG relax_type */
2022                       jac->as_amg_beta_theta, jac->as_amg_beta_opts[3],                     /* AMG interp_type */
2023                       jac->as_amg_beta_opts[4]);                                            /* AMG Pmax */
2024     /* Zero conductivity */
2025     jac->ams_beta_is_zero      = PETSC_FALSE;
2026     jac->ams_beta_is_zero_part = PETSC_FALSE;
2027     PetscFunctionReturn(PETSC_SUCCESS);
2028   }
2029   PetscCall(PetscStrcmp("ads", jac->hypre_type, &flag));
2030   if (flag) {
2031     PetscCallExternal(HYPRE_ADSCreate, &jac->hsolver);
2032     pc->ops->setfromoptions = PCSetFromOptions_HYPRE_ADS;
2033     pc->ops->view           = PCView_HYPRE_ADS;
2034     jac->destroy            = HYPRE_ADSDestroy;
2035     jac->setup              = HYPRE_ADSSetup;
2036     jac->solve              = HYPRE_ADSSolve;
2037     jac->coords[0]          = NULL;
2038     jac->coords[1]          = NULL;
2039     jac->coords[2]          = NULL;
2040     /* solver parameters: these are borrowed from mfem package, and they are not the default values from HYPRE ADS */
2041     jac->as_print       = 0;
2042     jac->as_max_iter    = 1;  /* used as a preconditioner */
2043     jac->as_tol         = 0.; /* used as a preconditioner */
2044     jac->ads_cycle_type = 13;
2045     /* Smoothing options */
2046     jac->as_relax_type   = 2;
2047     jac->as_relax_times  = 1;
2048     jac->as_relax_weight = 1.0;
2049     jac->as_omega        = 1.0;
2050     /* AMS solver parameters: cycle_type, coarsen type, agg_levels, relax_type, interp_type, Pmax */
2051     jac->ams_cycle_type       = 14;
2052     jac->as_amg_alpha_opts[0] = 10;
2053     jac->as_amg_alpha_opts[1] = 1;
2054     jac->as_amg_alpha_opts[2] = 6;
2055     jac->as_amg_alpha_opts[3] = 6;
2056     jac->as_amg_alpha_opts[4] = 4;
2057     jac->as_amg_alpha_theta   = 0.25;
2058     /* Vector Poisson AMG solver parameters: coarsen type, agg_levels, relax_type, interp_type, Pmax */
2059     jac->as_amg_beta_opts[0] = 10;
2060     jac->as_amg_beta_opts[1] = 1;
2061     jac->as_amg_beta_opts[2] = 6;
2062     jac->as_amg_beta_opts[3] = 6;
2063     jac->as_amg_beta_opts[4] = 4;
2064     jac->as_amg_beta_theta   = 0.25;
2065     PetscCallExternal(HYPRE_ADSSetPrintLevel, jac->hsolver, jac->as_print);
2066     PetscCallExternal(HYPRE_ADSSetMaxIter, jac->hsolver, jac->as_max_iter);
2067     PetscCallExternal(HYPRE_ADSSetCycleType, jac->hsolver, jac->ams_cycle_type);
2068     PetscCallExternal(HYPRE_ADSSetTol, jac->hsolver, jac->as_tol);
2069     PetscCallExternal(HYPRE_ADSSetSmoothingOptions, jac->hsolver, jac->as_relax_type, jac->as_relax_times, jac->as_relax_weight, jac->as_omega);
2070     PetscCallExternal(HYPRE_ADSSetAMSOptions, jac->hsolver, jac->ams_cycle_type,      /* AMG coarsen type */
2071                       jac->as_amg_alpha_opts[0],                                      /* AMG coarsen type */
2072                       jac->as_amg_alpha_opts[1],                                      /* AMG agg_levels */
2073                       jac->as_amg_alpha_opts[2],                                      /* AMG relax_type */
2074                       jac->as_amg_alpha_theta, jac->as_amg_alpha_opts[3],             /* AMG interp_type */
2075                       jac->as_amg_alpha_opts[4]);                                     /* AMG Pmax */
2076     PetscCallExternal(HYPRE_ADSSetAMGOptions, jac->hsolver, jac->as_amg_beta_opts[0], /* AMG coarsen type */
2077                       jac->as_amg_beta_opts[1],                                       /* AMG agg_levels */
2078                       jac->as_amg_beta_opts[2],                                       /* AMG relax_type */
2079                       jac->as_amg_beta_theta, jac->as_amg_beta_opts[3],               /* AMG interp_type */
2080                       jac->as_amg_beta_opts[4]);                                      /* AMG Pmax */
2081     PetscFunctionReturn(PETSC_SUCCESS);
2082   }
2083   PetscCall(PetscFree(jac->hypre_type));
2084 
2085   jac->hypre_type = NULL;
2086   SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown HYPRE preconditioner %s; Choices are euclid, pilut, parasails, boomeramg, ams", name);
2087 }
2088 
2089 /*
2090     It only gets here if the HYPRE type has not been set before the call to
2091    ...SetFromOptions() which actually is most of the time
2092 */
2093 PetscErrorCode PCSetFromOptions_HYPRE(PC pc, PetscOptionItems *PetscOptionsObject)
2094 {
2095   PetscInt    indx;
2096   const char *type[] = {"euclid", "pilut", "parasails", "boomeramg", "ams", "ads"};
2097   PetscBool   flg;
2098 
2099   PetscFunctionBegin;
2100   PetscOptionsHeadBegin(PetscOptionsObject, "HYPRE preconditioner options");
2101   PetscCall(PetscOptionsEList("-pc_hypre_type", "HYPRE preconditioner type", "PCHYPRESetType", type, PETSC_STATIC_ARRAY_LENGTH(type), "boomeramg", &indx, &flg));
2102   if (flg) {
2103     PetscCall(PCHYPRESetType_HYPRE(pc, type[indx]));
2104   } else {
2105     PetscCall(PCHYPRESetType_HYPRE(pc, "boomeramg"));
2106   }
2107   PetscTryTypeMethod(pc, setfromoptions, PetscOptionsObject);
2108   PetscOptionsHeadEnd();
2109   PetscFunctionReturn(PETSC_SUCCESS);
2110 }
2111 
2112 /*@C
2113      PCHYPRESetType - Sets which hypre preconditioner you wish to use
2114 
2115    Input Parameters:
2116 +     pc - the preconditioner context
2117 -     name - either  euclid, pilut, parasails, boomeramg, ams, ads
2118 
2119    Options Database Key:
2120    -pc_hypre_type - One of euclid, pilut, parasails, boomeramg, ams, ads
2121 
2122    Level: intermediate
2123 
2124 .seealso: `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `PCHYPRE`
2125 @*/
2126 PetscErrorCode PCHYPRESetType(PC pc, const char name[])
2127 {
2128   PetscFunctionBegin;
2129   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
2130   PetscValidCharPointer(name, 2);
2131   PetscTryMethod(pc, "PCHYPRESetType_C", (PC, const char[]), (pc, name));
2132   PetscFunctionReturn(PETSC_SUCCESS);
2133 }
2134 
2135 /*@C
2136      PCHYPREGetType - Gets which hypre preconditioner you are using
2137 
2138    Input Parameter:
2139 .     pc - the preconditioner context
2140 
2141    Output Parameter:
2142 .     name - either  euclid, pilut, parasails, boomeramg, ams, ads
2143 
2144    Level: intermediate
2145 
2146 .seealso: `PCCreate()`, `PCHYPRESetType()`, `PCType`, `PC`, `PCHYPRE`
2147 @*/
2148 PetscErrorCode PCHYPREGetType(PC pc, const char *name[])
2149 {
2150   PetscFunctionBegin;
2151   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
2152   PetscValidPointer(name, 2);
2153   PetscTryMethod(pc, "PCHYPREGetType_C", (PC, const char *[]), (pc, name));
2154   PetscFunctionReturn(PETSC_SUCCESS);
2155 }
2156 
2157 /*@C
2158    PCMGGalerkinSetMatProductAlgorithm - Set type of SpGEMM for hypre to use on GPUs
2159 
2160    Logically Collective
2161 
2162    Input Parameters:
2163 +  pc - the hypre context
2164 -  type - one of 'cusparse', 'hypre'
2165 
2166    Options Database Key:
2167 .  -pc_mg_galerkin_mat_product_algorithm <cusparse,hypre> - Type of SpGEMM to use in hypre
2168 
2169    Level: intermediate
2170 
2171    Developer Note:
2172    How the name starts with `PCMG`, should it not be `PCHYPREBoomerAMG`?
2173 
2174 .seealso: `PCHYPRE`, `PCMGGalerkinGetMatProductAlgorithm()`
2175 @*/
2176 PetscErrorCode PCMGGalerkinSetMatProductAlgorithm(PC pc, const char name[])
2177 {
2178   PetscFunctionBegin;
2179   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
2180   PetscTryMethod(pc, "PCMGGalerkinSetMatProductAlgorithm_C", (PC, const char[]), (pc, name));
2181   PetscFunctionReturn(PETSC_SUCCESS);
2182 }
2183 
2184 /*@C
2185    PCMGGalerkinGetMatProductAlgorithm - Get type of SpGEMM for hypre to use on GPUs
2186 
2187    Not Collective
2188 
2189    Input Parameter:
2190 .  pc - the multigrid context
2191 
2192    Output Parameter:
2193 .  name - one of 'cusparse', 'hypre'
2194 
2195    Level: intermediate
2196 
2197 .seealso: `PCHYPRE`, ``PCMGGalerkinSetMatProductAlgorithm()`
2198 @*/
2199 PetscErrorCode PCMGGalerkinGetMatProductAlgorithm(PC pc, const char *name[])
2200 {
2201   PetscFunctionBegin;
2202   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
2203   PetscTryMethod(pc, "PCMGGalerkinGetMatProductAlgorithm_C", (PC, const char *[]), (pc, name));
2204   PetscFunctionReturn(PETSC_SUCCESS);
2205 }
2206 
2207 /*MC
2208      PCHYPRE - Allows you to use the matrix element based preconditioners in the LLNL package hypre as PETSc `PC`
2209 
2210    Options Database Keys:
2211 +   -pc_hypre_type - One of euclid, pilut, parasails, boomeramg, ams, ads
2212 .   -pc_hypre_boomeramg_nodal_coarsen <n> - where n is from 1 to 6 (see `HYPRE_BOOMERAMGSetNodal()`)
2213 .   -pc_hypre_boomeramg_vec_interp_variant <v> - where v is from 1 to 3 (see `HYPRE_BoomerAMGSetInterpVecVariant()`)
2214 -   Many others, run with -pc_type hypre -pc_hypre_type XXX -help to see options for the XXX preconditioner
2215 
2216    Level: intermediate
2217 
2218    Notes:
2219     Apart from pc_hypre_type (for which there is `PCHYPRESetType()`),
2220           the many hypre options can ONLY be set via the options database (e.g. the command line
2221           or with `PetscOptionsSetValue()`, there are no functions to set them)
2222 
2223           The options -pc_hypre_boomeramg_max_iter and -pc_hypre_boomeramg_tol refer to the number of iterations
2224           (V-cycles) and tolerance that boomeramg does EACH time it is called. So for example, if
2225           -pc_hypre_boomeramg_max_iter is set to 2 then 2-V-cycles are being used to define the preconditioner
2226           (-pc_hypre_boomeramg_tol should be set to 0.0 - the default - to strictly use a fixed number of
2227           iterations per hypre call). -ksp_max_it and -ksp_rtol STILL determine the total number of iterations
2228           and tolerance for the Krylov solver. For example, if -pc_hypre_boomeramg_max_iter is 2 and -ksp_max_it is 10
2229           then AT MOST twenty V-cycles of boomeramg will be called.
2230 
2231            Note that the option -pc_hypre_boomeramg_relax_type_all defaults to symmetric relaxation
2232            (symmetric-SOR/Jacobi), which is required for Krylov solvers like CG that expect symmetry.
2233            Otherwise, you may want to use -pc_hypre_boomeramg_relax_type_all SOR/Jacobi.
2234           If you wish to use BoomerAMG WITHOUT a Krylov method use -ksp_type richardson NOT -ksp_type preonly
2235           and use -ksp_max_it to control the number of V-cycles.
2236           (see the PETSc FAQ.html at the PETSc website under the Documentation tab).
2237 
2238           `MatSetNearNullSpace()` - if you provide a near null space to your matrix it is ignored by hypre UNLESS you also use
2239           the following two options: ``-pc_hypre_boomeramg_nodal_coarsen <n> -pc_hypre_boomeramg_vec_interp_variant <v>``
2240 
2241           See `PCPFMG`, `PCSMG`, and `PCSYSPFMG` for access to hypre's other (nonalgebraic) multigrid solvers
2242 
2243           For `PCHYPRE` type of ams or ads auxiliary data must be provided to the preconditioner with `PCHYPRESetDiscreteGradient()`,
2244           `PCHYPRESetDiscreteCurl()`, `PCHYPRESetInterpolations()`, `PCHYPRESetAlphaPoissonMatrix()`, `PCHYPRESetBetaPoissonMatrix()`, `PCHYPRESetEdgeConstantVectors()`,
2245           `PCHYPREAMSSetInteriorNodes()`
2246 
2247    PETSc provides its own geometric and algebraic multigrid solvers `PCMG` and `PCGAMG`, also see `PCHMG` which is useful for certain multicomponent problems
2248 
2249    GPU Notes:
2250      To configure hypre BoomerAMG so that it can utilize NVIDIA GPUs run ./configure --download-hypre --with-cuda
2251      Then pass `VECCUDA` vectors and `MATAIJCUSPARSE` matrices to the solvers and PETSc will automatically utilize hypre's GPU solvers.
2252 
2253      To configure hypre BoomerAMG so that it can utilize AMD GPUs run ./configure --download-hypre --with-hip
2254      Then pass `VECHIP` vectors to the solvers and PETSc will automatically utilize hypre's GPU solvers.
2255 
2256 .seealso: `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `PCHYPRESetType()`, `PCPFMG`, `PCGAMG`, `PCSYSPFMG`, `PCSMG`, `PCHYPRESetDiscreteGradient()`,
2257           `PCHYPRESetDiscreteCurl()`, `PCHYPRESetInterpolations()`, `PCHYPRESetAlphaPoissonMatrix()`, `PCHYPRESetBetaPoissonMatrix()`, `PCHYPRESetEdgeConstantVectors()`,
2258           PCHYPREAMSSetInteriorNodes()
2259 M*/
2260 
2261 PETSC_EXTERN PetscErrorCode PCCreate_HYPRE(PC pc)
2262 {
2263   PC_HYPRE *jac;
2264 
2265   PetscFunctionBegin;
2266   PetscCall(PetscNew(&jac));
2267 
2268   pc->data                = jac;
2269   pc->ops->reset          = PCReset_HYPRE;
2270   pc->ops->destroy        = PCDestroy_HYPRE;
2271   pc->ops->setfromoptions = PCSetFromOptions_HYPRE;
2272   pc->ops->setup          = PCSetUp_HYPRE;
2273   pc->ops->apply          = PCApply_HYPRE;
2274   jac->comm_hypre         = MPI_COMM_NULL;
2275   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetType_C", PCHYPRESetType_HYPRE));
2276   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPREGetType_C", PCHYPREGetType_HYPRE));
2277   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCSetCoordinates_C", PCSetCoordinates_HYPRE));
2278   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetDiscreteGradient_C", PCHYPRESetDiscreteGradient_HYPRE));
2279   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetDiscreteCurl_C", PCHYPRESetDiscreteCurl_HYPRE));
2280   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetInterpolations_C", PCHYPRESetInterpolations_HYPRE));
2281   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetEdgeConstantVectors_C", PCHYPRESetEdgeConstantVectors_HYPRE));
2282   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPREAMSSetInteriorNodes_C", PCHYPREAMSSetInteriorNodes_HYPRE));
2283   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCHYPRESetPoissonMatrix_C", PCHYPRESetPoissonMatrix_HYPRE));
2284   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGalerkinSetMatProductAlgorithm_C", PCMGGalerkinSetMatProductAlgorithm_HYPRE_BoomerAMG));
2285   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGalerkinGetMatProductAlgorithm_C", PCMGGalerkinGetMatProductAlgorithm_HYPRE_BoomerAMG));
2286 #if defined(PETSC_HAVE_HYPRE_DEVICE)
2287   #if defined(HYPRE_USING_HIP)
2288   PetscCall(PetscDeviceInitialize(PETSC_DEVICE_HIP));
2289   #endif
2290   #if defined(HYPRE_USING_CUDA)
2291   PetscCall(PetscDeviceInitialize(PETSC_DEVICE_CUDA));
2292   #endif
2293 #endif
2294   PetscFunctionReturn(PETSC_SUCCESS);
2295 }
2296 
2297 typedef struct {
2298   MPI_Comm           hcomm; /* does not share comm with HYPRE_StructMatrix because need to create solver before getting matrix */
2299   HYPRE_StructSolver hsolver;
2300 
2301   /* keep copy of PFMG options used so may view them */
2302   PetscInt  its;
2303   double    tol;
2304   PetscInt  relax_type;
2305   PetscInt  rap_type;
2306   PetscInt  num_pre_relax, num_post_relax;
2307   PetscInt  max_levels;
2308   PetscInt  skip_relax;
2309   PetscBool print_statistics;
2310 } PC_PFMG;
2311 
2312 PetscErrorCode PCDestroy_PFMG(PC pc)
2313 {
2314   PC_PFMG *ex = (PC_PFMG *)pc->data;
2315 
2316   PetscFunctionBegin;
2317   if (ex->hsolver) PetscCallExternal(HYPRE_StructPFMGDestroy, ex->hsolver);
2318   PetscCall(PetscCommRestoreComm(PetscObjectComm((PetscObject)pc), &ex->hcomm));
2319   PetscCall(PetscFree(pc->data));
2320   PetscFunctionReturn(PETSC_SUCCESS);
2321 }
2322 
2323 static const char *PFMGRelaxType[] = {"Jacobi", "Weighted-Jacobi", "symmetric-Red/Black-Gauss-Seidel", "Red/Black-Gauss-Seidel"};
2324 static const char *PFMGRAPType[]   = {"Galerkin", "non-Galerkin"};
2325 
2326 PetscErrorCode PCView_PFMG(PC pc, PetscViewer viewer)
2327 {
2328   PetscBool iascii;
2329   PC_PFMG  *ex = (PC_PFMG *)pc->data;
2330 
2331   PetscFunctionBegin;
2332   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
2333   if (iascii) {
2334     PetscCall(PetscViewerASCIIPrintf(viewer, "  HYPRE PFMG preconditioning\n"));
2335     PetscCall(PetscViewerASCIIPrintf(viewer, "    max iterations %" PetscInt_FMT "\n", ex->its));
2336     PetscCall(PetscViewerASCIIPrintf(viewer, "    tolerance %g\n", ex->tol));
2337     PetscCall(PetscViewerASCIIPrintf(viewer, "    relax type %s\n", PFMGRelaxType[ex->relax_type]));
2338     PetscCall(PetscViewerASCIIPrintf(viewer, "    RAP type %s\n", PFMGRAPType[ex->rap_type]));
2339     PetscCall(PetscViewerASCIIPrintf(viewer, "    number pre-relax %" PetscInt_FMT " post-relax %" PetscInt_FMT "\n", ex->num_pre_relax, ex->num_post_relax));
2340     PetscCall(PetscViewerASCIIPrintf(viewer, "    max levels %" PetscInt_FMT "\n", ex->max_levels));
2341     PetscCall(PetscViewerASCIIPrintf(viewer, "    skip relax %" PetscInt_FMT "\n", ex->skip_relax));
2342   }
2343   PetscFunctionReturn(PETSC_SUCCESS);
2344 }
2345 
2346 PetscErrorCode PCSetFromOptions_PFMG(PC pc, PetscOptionItems *PetscOptionsObject)
2347 {
2348   PC_PFMG *ex = (PC_PFMG *)pc->data;
2349 
2350   PetscFunctionBegin;
2351   PetscOptionsHeadBegin(PetscOptionsObject, "PFMG options");
2352   PetscCall(PetscOptionsBool("-pc_pfmg_print_statistics", "Print statistics", "HYPRE_StructPFMGSetPrintLevel", ex->print_statistics, &ex->print_statistics, NULL));
2353   PetscCall(PetscOptionsInt("-pc_pfmg_its", "Number of iterations of PFMG to use as preconditioner", "HYPRE_StructPFMGSetMaxIter", ex->its, &ex->its, NULL));
2354   PetscCallExternal(HYPRE_StructPFMGSetMaxIter, ex->hsolver, ex->its);
2355   PetscCall(PetscOptionsInt("-pc_pfmg_num_pre_relax", "Number of smoothing steps before coarse grid", "HYPRE_StructPFMGSetNumPreRelax", ex->num_pre_relax, &ex->num_pre_relax, NULL));
2356   PetscCallExternal(HYPRE_StructPFMGSetNumPreRelax, ex->hsolver, ex->num_pre_relax);
2357   PetscCall(PetscOptionsInt("-pc_pfmg_num_post_relax", "Number of smoothing steps after coarse grid", "HYPRE_StructPFMGSetNumPostRelax", ex->num_post_relax, &ex->num_post_relax, NULL));
2358   PetscCallExternal(HYPRE_StructPFMGSetNumPostRelax, ex->hsolver, ex->num_post_relax);
2359 
2360   PetscCall(PetscOptionsInt("-pc_pfmg_max_levels", "Max Levels for MG hierarchy", "HYPRE_StructPFMGSetMaxLevels", ex->max_levels, &ex->max_levels, NULL));
2361   PetscCallExternal(HYPRE_StructPFMGSetMaxLevels, ex->hsolver, ex->max_levels);
2362 
2363   PetscCall(PetscOptionsReal("-pc_pfmg_tol", "Tolerance of PFMG", "HYPRE_StructPFMGSetTol", ex->tol, &ex->tol, NULL));
2364   PetscCallExternal(HYPRE_StructPFMGSetTol, ex->hsolver, ex->tol);
2365   PetscCall(PetscOptionsEList("-pc_pfmg_relax_type", "Relax type for the up and down cycles", "HYPRE_StructPFMGSetRelaxType", PFMGRelaxType, PETSC_STATIC_ARRAY_LENGTH(PFMGRelaxType), PFMGRelaxType[ex->relax_type], &ex->relax_type, NULL));
2366   PetscCallExternal(HYPRE_StructPFMGSetRelaxType, ex->hsolver, ex->relax_type);
2367   PetscCall(PetscOptionsEList("-pc_pfmg_rap_type", "RAP type", "HYPRE_StructPFMGSetRAPType", PFMGRAPType, PETSC_STATIC_ARRAY_LENGTH(PFMGRAPType), PFMGRAPType[ex->rap_type], &ex->rap_type, NULL));
2368   PetscCallExternal(HYPRE_StructPFMGSetRAPType, ex->hsolver, ex->rap_type);
2369   PetscCall(PetscOptionsInt("-pc_pfmg_skip_relax", "Skip relaxation on certain grids for isotropic problems. This can greatly improve efficiency by eliminating unnecessary relaxations when the underlying problem is isotropic", "HYPRE_StructPFMGSetSkipRelax", ex->skip_relax, &ex->skip_relax, NULL));
2370   PetscCallExternal(HYPRE_StructPFMGSetSkipRelax, ex->hsolver, ex->skip_relax);
2371   PetscOptionsHeadEnd();
2372   PetscFunctionReturn(PETSC_SUCCESS);
2373 }
2374 
2375 PetscErrorCode PCApply_PFMG(PC pc, Vec x, Vec y)
2376 {
2377   PC_PFMG           *ex = (PC_PFMG *)pc->data;
2378   PetscScalar       *yy;
2379   const PetscScalar *xx;
2380   PetscInt           ilower[3], iupper[3];
2381   HYPRE_Int          hlower[3], hupper[3];
2382   Mat_HYPREStruct   *mx = (Mat_HYPREStruct *)(pc->pmat->data);
2383 
2384   PetscFunctionBegin;
2385   PetscCall(PetscCitationsRegister(hypreCitation, &cite));
2386   PetscCall(DMDAGetCorners(mx->da, &ilower[0], &ilower[1], &ilower[2], &iupper[0], &iupper[1], &iupper[2]));
2387   /* when HYPRE_MIXEDINT is defined, sizeof(HYPRE_Int) == 32 */
2388   iupper[0] += ilower[0] - 1;
2389   iupper[1] += ilower[1] - 1;
2390   iupper[2] += ilower[2] - 1;
2391   hlower[0] = (HYPRE_Int)ilower[0];
2392   hlower[1] = (HYPRE_Int)ilower[1];
2393   hlower[2] = (HYPRE_Int)ilower[2];
2394   hupper[0] = (HYPRE_Int)iupper[0];
2395   hupper[1] = (HYPRE_Int)iupper[1];
2396   hupper[2] = (HYPRE_Int)iupper[2];
2397 
2398   /* copy x values over to hypre */
2399   PetscCallExternal(HYPRE_StructVectorSetConstantValues, mx->hb, 0.0);
2400   PetscCall(VecGetArrayRead(x, &xx));
2401   PetscCallExternal(HYPRE_StructVectorSetBoxValues, mx->hb, hlower, hupper, (HYPRE_Complex *)xx);
2402   PetscCall(VecRestoreArrayRead(x, &xx));
2403   PetscCallExternal(HYPRE_StructVectorAssemble, mx->hb);
2404   PetscCallExternal(HYPRE_StructPFMGSolve, ex->hsolver, mx->hmat, mx->hb, mx->hx);
2405 
2406   /* copy solution values back to PETSc */
2407   PetscCall(VecGetArray(y, &yy));
2408   PetscCallExternal(HYPRE_StructVectorGetBoxValues, mx->hx, hlower, hupper, (HYPRE_Complex *)yy);
2409   PetscCall(VecRestoreArray(y, &yy));
2410   PetscFunctionReturn(PETSC_SUCCESS);
2411 }
2412 
2413 static PetscErrorCode PCApplyRichardson_PFMG(PC pc, Vec b, Vec y, Vec w, PetscReal rtol, PetscReal abstol, PetscReal dtol, PetscInt its, PetscBool guesszero, PetscInt *outits, PCRichardsonConvergedReason *reason)
2414 {
2415   PC_PFMG  *jac = (PC_PFMG *)pc->data;
2416   HYPRE_Int oits;
2417 
2418   PetscFunctionBegin;
2419   PetscCall(PetscCitationsRegister(hypreCitation, &cite));
2420   PetscCallExternal(HYPRE_StructPFMGSetMaxIter, jac->hsolver, its * jac->its);
2421   PetscCallExternal(HYPRE_StructPFMGSetTol, jac->hsolver, rtol);
2422 
2423   PetscCall(PCApply_PFMG(pc, b, y));
2424   PetscCallExternal(HYPRE_StructPFMGGetNumIterations, jac->hsolver, &oits);
2425   *outits = oits;
2426   if (oits == its) *reason = PCRICHARDSON_CONVERGED_ITS;
2427   else *reason = PCRICHARDSON_CONVERGED_RTOL;
2428   PetscCallExternal(HYPRE_StructPFMGSetTol, jac->hsolver, jac->tol);
2429   PetscCallExternal(HYPRE_StructPFMGSetMaxIter, jac->hsolver, jac->its);
2430   PetscFunctionReturn(PETSC_SUCCESS);
2431 }
2432 
2433 PetscErrorCode PCSetUp_PFMG(PC pc)
2434 {
2435   PC_PFMG         *ex = (PC_PFMG *)pc->data;
2436   Mat_HYPREStruct *mx = (Mat_HYPREStruct *)(pc->pmat->data);
2437   PetscBool        flg;
2438 
2439   PetscFunctionBegin;
2440   PetscCall(PetscObjectTypeCompare((PetscObject)pc->pmat, MATHYPRESTRUCT, &flg));
2441   PetscCheck(flg, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "Must use MATHYPRESTRUCT with this preconditioner");
2442 
2443   /* create the hypre solver object and set its information */
2444   if (ex->hsolver) PetscCallExternal(HYPRE_StructPFMGDestroy, ex->hsolver);
2445   PetscCallExternal(HYPRE_StructPFMGCreate, ex->hcomm, &ex->hsolver);
2446 
2447   // Print Hypre statistics about the solve process
2448   if (ex->print_statistics) PetscCallExternal(HYPRE_StructPFMGSetPrintLevel, ex->hsolver, 3);
2449 
2450   // The hypre options must be repeated here because the StructPFMG was destroyed and recreated
2451   PetscCallExternal(HYPRE_StructPFMGSetMaxIter, ex->hsolver, ex->its);
2452   PetscCallExternal(HYPRE_StructPFMGSetNumPreRelax, ex->hsolver, ex->num_pre_relax);
2453   PetscCallExternal(HYPRE_StructPFMGSetNumPostRelax, ex->hsolver, ex->num_post_relax);
2454   PetscCallExternal(HYPRE_StructPFMGSetMaxLevels, ex->hsolver, ex->max_levels);
2455   PetscCallExternal(HYPRE_StructPFMGSetTol, ex->hsolver, ex->tol);
2456   PetscCallExternal(HYPRE_StructPFMGSetRelaxType, ex->hsolver, ex->relax_type);
2457   PetscCallExternal(HYPRE_StructPFMGSetRAPType, ex->hsolver, ex->rap_type);
2458 
2459   PetscCallExternal(HYPRE_StructPFMGSetup, ex->hsolver, mx->hmat, mx->hb, mx->hx);
2460   PetscCallExternal(HYPRE_StructPFMGSetZeroGuess, ex->hsolver);
2461   PetscFunctionReturn(PETSC_SUCCESS);
2462 }
2463 
2464 /*MC
2465      PCPFMG - the hypre PFMG multigrid solver
2466 
2467    Options Database Keys:
2468 + -pc_pfmg_its <its> - number of iterations of PFMG to use as preconditioner
2469 . -pc_pfmg_num_pre_relax <steps> - number of smoothing steps before coarse grid solve
2470 . -pc_pfmg_num_post_relax <steps> - number of smoothing steps after coarse grid solve
2471 . -pc_pfmg_tol <tol> - tolerance of PFMG
2472 . -pc_pfmg_relax_type - relaxation type for the up and down cycles, one of Jacobi,Weighted-Jacobi,symmetric-Red/Black-Gauss-Seidel,Red/Black-Gauss-Seidel
2473 . -pc_pfmg_rap_type - type of coarse matrix generation, one of Galerkin,non-Galerkin
2474 - -pc_pfmg_skip_relax - skip relaxation on certain grids for isotropic problems. This can greatly improve efficiency by eliminating unnecessary relaxations
2475                         when the underlying problem is isotropic, one of 0,1
2476 
2477    Level: advanced
2478 
2479    Notes:
2480    This is for CELL-centered descretizations
2481 
2482    See `PCSYSPFMG` for a version suitable for systems of PDEs, and `PCSMG`
2483 
2484    See `PCHYPRE` for hypre's BoomerAMG algebraic multigrid solver
2485 
2486    This must be used with the `MATHYPRESTRUCT` matrix type.
2487 
2488    This provides only some of the functionality of PFMG, it supports only one block per process defined by a PETSc `DMDA`.
2489 
2490 .seealso: `PCMG`, `MATHYPRESTRUCT`, `PCHYPRE`, `PCGAMG`, `PCSYSPFMG`, `PCSMG`
2491 M*/
2492 
2493 PETSC_EXTERN PetscErrorCode PCCreate_PFMG(PC pc)
2494 {
2495   PC_PFMG *ex;
2496 
2497   PetscFunctionBegin;
2498   PetscCall(PetscNew(&ex));
2499   pc->data = ex;
2500 
2501   ex->its              = 1;
2502   ex->tol              = 1.e-8;
2503   ex->relax_type       = 1;
2504   ex->rap_type         = 0;
2505   ex->num_pre_relax    = 1;
2506   ex->num_post_relax   = 1;
2507   ex->max_levels       = 0;
2508   ex->skip_relax       = 0;
2509   ex->print_statistics = PETSC_FALSE;
2510 
2511   pc->ops->setfromoptions  = PCSetFromOptions_PFMG;
2512   pc->ops->view            = PCView_PFMG;
2513   pc->ops->destroy         = PCDestroy_PFMG;
2514   pc->ops->apply           = PCApply_PFMG;
2515   pc->ops->applyrichardson = PCApplyRichardson_PFMG;
2516   pc->ops->setup           = PCSetUp_PFMG;
2517 
2518   PetscCall(PetscCommGetComm(PetscObjectComm((PetscObject)pc), &ex->hcomm));
2519   PetscCallExternal(HYPRE_StructPFMGCreate, ex->hcomm, &ex->hsolver);
2520   PetscFunctionReturn(PETSC_SUCCESS);
2521 }
2522 
2523 /* we know we are working with a HYPRE_SStructMatrix */
2524 typedef struct {
2525   MPI_Comm            hcomm; /* does not share comm with HYPRE_SStructMatrix because need to create solver before getting matrix */
2526   HYPRE_SStructSolver ss_solver;
2527 
2528   /* keep copy of SYSPFMG options used so may view them */
2529   PetscInt its;
2530   double   tol;
2531   PetscInt relax_type;
2532   PetscInt num_pre_relax, num_post_relax;
2533 } PC_SysPFMG;
2534 
2535 PetscErrorCode PCDestroy_SysPFMG(PC pc)
2536 {
2537   PC_SysPFMG *ex = (PC_SysPFMG *)pc->data;
2538 
2539   PetscFunctionBegin;
2540   if (ex->ss_solver) PetscCallExternal(HYPRE_SStructSysPFMGDestroy, ex->ss_solver);
2541   PetscCall(PetscCommRestoreComm(PetscObjectComm((PetscObject)pc), &ex->hcomm));
2542   PetscCall(PetscFree(pc->data));
2543   PetscFunctionReturn(PETSC_SUCCESS);
2544 }
2545 
2546 static const char *SysPFMGRelaxType[] = {"Weighted-Jacobi", "Red/Black-Gauss-Seidel"};
2547 
2548 PetscErrorCode PCView_SysPFMG(PC pc, PetscViewer viewer)
2549 {
2550   PetscBool   iascii;
2551   PC_SysPFMG *ex = (PC_SysPFMG *)pc->data;
2552 
2553   PetscFunctionBegin;
2554   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
2555   if (iascii) {
2556     PetscCall(PetscViewerASCIIPrintf(viewer, "  HYPRE SysPFMG preconditioning\n"));
2557     PetscCall(PetscViewerASCIIPrintf(viewer, "  max iterations %" PetscInt_FMT "\n", ex->its));
2558     PetscCall(PetscViewerASCIIPrintf(viewer, "  tolerance %g\n", ex->tol));
2559     PetscCall(PetscViewerASCIIPrintf(viewer, "  relax type %s\n", PFMGRelaxType[ex->relax_type]));
2560     PetscCall(PetscViewerASCIIPrintf(viewer, "  number pre-relax %" PetscInt_FMT " post-relax %" PetscInt_FMT "\n", ex->num_pre_relax, ex->num_post_relax));
2561   }
2562   PetscFunctionReturn(PETSC_SUCCESS);
2563 }
2564 
2565 PetscErrorCode PCSetFromOptions_SysPFMG(PC pc, PetscOptionItems *PetscOptionsObject)
2566 {
2567   PC_SysPFMG *ex  = (PC_SysPFMG *)pc->data;
2568   PetscBool   flg = PETSC_FALSE;
2569 
2570   PetscFunctionBegin;
2571   PetscOptionsHeadBegin(PetscOptionsObject, "SysPFMG options");
2572   PetscCall(PetscOptionsBool("-pc_syspfmg_print_statistics", "Print statistics", "HYPRE_SStructSysPFMGSetPrintLevel", flg, &flg, NULL));
2573   if (flg) PetscCallExternal(HYPRE_SStructSysPFMGSetPrintLevel, ex->ss_solver, 3);
2574   PetscCall(PetscOptionsInt("-pc_syspfmg_its", "Number of iterations of SysPFMG to use as preconditioner", "HYPRE_SStructSysPFMGSetMaxIter", ex->its, &ex->its, NULL));
2575   PetscCallExternal(HYPRE_SStructSysPFMGSetMaxIter, ex->ss_solver, ex->its);
2576   PetscCall(PetscOptionsInt("-pc_syspfmg_num_pre_relax", "Number of smoothing steps before coarse grid", "HYPRE_SStructSysPFMGSetNumPreRelax", ex->num_pre_relax, &ex->num_pre_relax, NULL));
2577   PetscCallExternal(HYPRE_SStructSysPFMGSetNumPreRelax, ex->ss_solver, ex->num_pre_relax);
2578   PetscCall(PetscOptionsInt("-pc_syspfmg_num_post_relax", "Number of smoothing steps after coarse grid", "HYPRE_SStructSysPFMGSetNumPostRelax", ex->num_post_relax, &ex->num_post_relax, NULL));
2579   PetscCallExternal(HYPRE_SStructSysPFMGSetNumPostRelax, ex->ss_solver, ex->num_post_relax);
2580 
2581   PetscCall(PetscOptionsReal("-pc_syspfmg_tol", "Tolerance of SysPFMG", "HYPRE_SStructSysPFMGSetTol", ex->tol, &ex->tol, NULL));
2582   PetscCallExternal(HYPRE_SStructSysPFMGSetTol, ex->ss_solver, ex->tol);
2583   PetscCall(PetscOptionsEList("-pc_syspfmg_relax_type", "Relax type for the up and down cycles", "HYPRE_SStructSysPFMGSetRelaxType", SysPFMGRelaxType, PETSC_STATIC_ARRAY_LENGTH(SysPFMGRelaxType), SysPFMGRelaxType[ex->relax_type], &ex->relax_type, NULL));
2584   PetscCallExternal(HYPRE_SStructSysPFMGSetRelaxType, ex->ss_solver, ex->relax_type);
2585   PetscOptionsHeadEnd();
2586   PetscFunctionReturn(PETSC_SUCCESS);
2587 }
2588 
2589 PetscErrorCode PCApply_SysPFMG(PC pc, Vec x, Vec y)
2590 {
2591   PC_SysPFMG        *ex = (PC_SysPFMG *)pc->data;
2592   PetscScalar       *yy;
2593   const PetscScalar *xx;
2594   PetscInt           ilower[3], iupper[3];
2595   HYPRE_Int          hlower[3], hupper[3];
2596   Mat_HYPRESStruct  *mx       = (Mat_HYPRESStruct *)(pc->pmat->data);
2597   PetscInt           ordering = mx->dofs_order;
2598   PetscInt           nvars    = mx->nvars;
2599   PetscInt           part     = 0;
2600   PetscInt           size;
2601   PetscInt           i;
2602 
2603   PetscFunctionBegin;
2604   PetscCall(PetscCitationsRegister(hypreCitation, &cite));
2605   PetscCall(DMDAGetCorners(mx->da, &ilower[0], &ilower[1], &ilower[2], &iupper[0], &iupper[1], &iupper[2]));
2606   /* when HYPRE_MIXEDINT is defined, sizeof(HYPRE_Int) == 32 */
2607   iupper[0] += ilower[0] - 1;
2608   iupper[1] += ilower[1] - 1;
2609   iupper[2] += ilower[2] - 1;
2610   hlower[0] = (HYPRE_Int)ilower[0];
2611   hlower[1] = (HYPRE_Int)ilower[1];
2612   hlower[2] = (HYPRE_Int)ilower[2];
2613   hupper[0] = (HYPRE_Int)iupper[0];
2614   hupper[1] = (HYPRE_Int)iupper[1];
2615   hupper[2] = (HYPRE_Int)iupper[2];
2616 
2617   size = 1;
2618   for (i = 0; i < 3; i++) size *= (iupper[i] - ilower[i] + 1);
2619 
2620   /* copy x values over to hypre for variable ordering */
2621   if (ordering) {
2622     PetscCallExternal(HYPRE_SStructVectorSetConstantValues, mx->ss_b, 0.0);
2623     PetscCall(VecGetArrayRead(x, &xx));
2624     for (i = 0; i < nvars; i++) PetscCallExternal(HYPRE_SStructVectorSetBoxValues, mx->ss_b, part, hlower, hupper, i, (HYPRE_Complex *)(xx + (size * i)));
2625     PetscCall(VecRestoreArrayRead(x, &xx));
2626     PetscCallExternal(HYPRE_SStructVectorAssemble, mx->ss_b);
2627     PetscCallExternal(HYPRE_SStructMatrixMatvec, 1.0, mx->ss_mat, mx->ss_b, 0.0, mx->ss_x);
2628     PetscCallExternal(HYPRE_SStructSysPFMGSolve, ex->ss_solver, mx->ss_mat, mx->ss_b, mx->ss_x);
2629 
2630     /* copy solution values back to PETSc */
2631     PetscCall(VecGetArray(y, &yy));
2632     for (i = 0; i < nvars; i++) PetscCallExternal(HYPRE_SStructVectorGetBoxValues, mx->ss_x, part, hlower, hupper, i, (HYPRE_Complex *)(yy + (size * i)));
2633     PetscCall(VecRestoreArray(y, &yy));
2634   } else { /* nodal ordering must be mapped to variable ordering for sys_pfmg */
2635     PetscScalar *z;
2636     PetscInt     j, k;
2637 
2638     PetscCall(PetscMalloc1(nvars * size, &z));
2639     PetscCallExternal(HYPRE_SStructVectorSetConstantValues, mx->ss_b, 0.0);
2640     PetscCall(VecGetArrayRead(x, &xx));
2641 
2642     /* transform nodal to hypre's variable ordering for sys_pfmg */
2643     for (i = 0; i < size; i++) {
2644       k = i * nvars;
2645       for (j = 0; j < nvars; j++) z[j * size + i] = xx[k + j];
2646     }
2647     for (i = 0; i < nvars; i++) PetscCallExternal(HYPRE_SStructVectorSetBoxValues, mx->ss_b, part, hlower, hupper, i, (HYPRE_Complex *)(z + (size * i)));
2648     PetscCall(VecRestoreArrayRead(x, &xx));
2649     PetscCallExternal(HYPRE_SStructVectorAssemble, mx->ss_b);
2650     PetscCallExternal(HYPRE_SStructSysPFMGSolve, ex->ss_solver, mx->ss_mat, mx->ss_b, mx->ss_x);
2651 
2652     /* copy solution values back to PETSc */
2653     PetscCall(VecGetArray(y, &yy));
2654     for (i = 0; i < nvars; i++) PetscCallExternal(HYPRE_SStructVectorGetBoxValues, mx->ss_x, part, hlower, hupper, i, (HYPRE_Complex *)(z + (size * i)));
2655     /* transform hypre's variable ordering for sys_pfmg to nodal ordering */
2656     for (i = 0; i < size; i++) {
2657       k = i * nvars;
2658       for (j = 0; j < nvars; j++) yy[k + j] = z[j * size + i];
2659     }
2660     PetscCall(VecRestoreArray(y, &yy));
2661     PetscCall(PetscFree(z));
2662   }
2663   PetscFunctionReturn(PETSC_SUCCESS);
2664 }
2665 
2666 static PetscErrorCode PCApplyRichardson_SysPFMG(PC pc, Vec b, Vec y, Vec w, PetscReal rtol, PetscReal abstol, PetscReal dtol, PetscInt its, PetscBool guesszero, PetscInt *outits, PCRichardsonConvergedReason *reason)
2667 {
2668   PC_SysPFMG *jac = (PC_SysPFMG *)pc->data;
2669   HYPRE_Int   oits;
2670 
2671   PetscFunctionBegin;
2672   PetscCall(PetscCitationsRegister(hypreCitation, &cite));
2673   PetscCallExternal(HYPRE_SStructSysPFMGSetMaxIter, jac->ss_solver, its * jac->its);
2674   PetscCallExternal(HYPRE_SStructSysPFMGSetTol, jac->ss_solver, rtol);
2675   PetscCall(PCApply_SysPFMG(pc, b, y));
2676   PetscCallExternal(HYPRE_SStructSysPFMGGetNumIterations, jac->ss_solver, &oits);
2677   *outits = oits;
2678   if (oits == its) *reason = PCRICHARDSON_CONVERGED_ITS;
2679   else *reason = PCRICHARDSON_CONVERGED_RTOL;
2680   PetscCallExternal(HYPRE_SStructSysPFMGSetTol, jac->ss_solver, jac->tol);
2681   PetscCallExternal(HYPRE_SStructSysPFMGSetMaxIter, jac->ss_solver, jac->its);
2682   PetscFunctionReturn(PETSC_SUCCESS);
2683 }
2684 
2685 PetscErrorCode PCSetUp_SysPFMG(PC pc)
2686 {
2687   PC_SysPFMG       *ex = (PC_SysPFMG *)pc->data;
2688   Mat_HYPRESStruct *mx = (Mat_HYPRESStruct *)(pc->pmat->data);
2689   PetscBool         flg;
2690 
2691   PetscFunctionBegin;
2692   PetscCall(PetscObjectTypeCompare((PetscObject)pc->pmat, MATHYPRESSTRUCT, &flg));
2693   PetscCheck(flg, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "Must use MATHYPRESSTRUCT with this preconditioner");
2694 
2695   /* create the hypre sstruct solver object and set its information */
2696   if (ex->ss_solver) PetscCallExternal(HYPRE_SStructSysPFMGDestroy, ex->ss_solver);
2697   PetscCallExternal(HYPRE_SStructSysPFMGCreate, ex->hcomm, &ex->ss_solver);
2698   PetscCallExternal(HYPRE_SStructSysPFMGSetZeroGuess, ex->ss_solver);
2699   PetscCallExternal(HYPRE_SStructSysPFMGSetup, ex->ss_solver, mx->ss_mat, mx->ss_b, mx->ss_x);
2700   PetscFunctionReturn(PETSC_SUCCESS);
2701 }
2702 
2703 /*MC
2704      PCSYSPFMG - the hypre SysPFMG multigrid solver
2705 
2706    Level: advanced
2707 
2708    Options Database Keys:
2709 + -pc_syspfmg_its <its> - number of iterations of SysPFMG to use as preconditioner
2710 . -pc_syspfmg_num_pre_relax <steps> - number of smoothing steps before coarse grid
2711 . -pc_syspfmg_num_post_relax <steps> - number of smoothing steps after coarse grid
2712 . -pc_syspfmg_tol <tol> - tolerance of SysPFMG
2713 - -pc_syspfmg_relax_type <Weighted-Jacobi,Red/Black-Gauss-Seidel> - relaxation type for the up and down cycles
2714 
2715    Notes:
2716    See `PCPFMG` for hypre's PFMG that works for a scalar PDE and `PCSMG`
2717 
2718    See `PCHYPRE` for hypre's BoomerAMG algebraic multigrid solver
2719 
2720    This is for CELL-centered descretizations
2721 
2722    This must be used with the `MATHYPRESSTRUCT` matrix type.
2723 
2724    This does not give access to all the functionality of hypres SysPFMG, it supports only one part, and one block per process defined by a PETSc `DMDA`.
2725 
2726 .seealso: `PCMG`, `MATHYPRESSTRUCT`, `PCPFMG`, `PCHYPRE`, `PCGAMG`, `PCSMG`
2727 M*/
2728 
2729 PETSC_EXTERN PetscErrorCode PCCreate_SysPFMG(PC pc)
2730 {
2731   PC_SysPFMG *ex;
2732 
2733   PetscFunctionBegin;
2734   PetscCall(PetscNew(&ex));
2735   pc->data = ex;
2736 
2737   ex->its            = 1;
2738   ex->tol            = 1.e-8;
2739   ex->relax_type     = 1;
2740   ex->num_pre_relax  = 1;
2741   ex->num_post_relax = 1;
2742 
2743   pc->ops->setfromoptions  = PCSetFromOptions_SysPFMG;
2744   pc->ops->view            = PCView_SysPFMG;
2745   pc->ops->destroy         = PCDestroy_SysPFMG;
2746   pc->ops->apply           = PCApply_SysPFMG;
2747   pc->ops->applyrichardson = PCApplyRichardson_SysPFMG;
2748   pc->ops->setup           = PCSetUp_SysPFMG;
2749 
2750   PetscCall(PetscCommGetComm(PetscObjectComm((PetscObject)pc), &ex->hcomm));
2751   PetscCallExternal(HYPRE_SStructSysPFMGCreate, ex->hcomm, &ex->ss_solver);
2752   PetscFunctionReturn(PETSC_SUCCESS);
2753 }
2754 
2755 /* PC SMG */
2756 typedef struct {
2757   MPI_Comm           hcomm; /* does not share comm with HYPRE_StructMatrix because need to create solver before getting matrix */
2758   HYPRE_StructSolver hsolver;
2759   PetscInt           its; /* keep copy of SMG options used so may view them */
2760   double             tol;
2761   PetscBool          print_statistics;
2762   PetscInt           num_pre_relax, num_post_relax;
2763 } PC_SMG;
2764 
2765 PetscErrorCode PCDestroy_SMG(PC pc)
2766 {
2767   PC_SMG *ex = (PC_SMG *)pc->data;
2768 
2769   PetscFunctionBegin;
2770   if (ex->hsolver) PetscCallExternal(HYPRE_StructSMGDestroy, ex->hsolver);
2771   PetscCall(PetscCommRestoreComm(PetscObjectComm((PetscObject)pc), &ex->hcomm));
2772   PetscCall(PetscFree(pc->data));
2773   PetscFunctionReturn(PETSC_SUCCESS);
2774 }
2775 
2776 PetscErrorCode PCView_SMG(PC pc, PetscViewer viewer)
2777 {
2778   PetscBool iascii;
2779   PC_SMG   *ex = (PC_SMG *)pc->data;
2780 
2781   PetscFunctionBegin;
2782   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
2783   if (iascii) {
2784     PetscCall(PetscViewerASCIIPrintf(viewer, "  HYPRE SMG preconditioning\n"));
2785     PetscCall(PetscViewerASCIIPrintf(viewer, "    max iterations %" PetscInt_FMT "\n", ex->its));
2786     PetscCall(PetscViewerASCIIPrintf(viewer, "    tolerance %g\n", ex->tol));
2787     PetscCall(PetscViewerASCIIPrintf(viewer, "    number pre-relax %" PetscInt_FMT " post-relax %" PetscInt_FMT "\n", ex->num_pre_relax, ex->num_post_relax));
2788   }
2789   PetscFunctionReturn(PETSC_SUCCESS);
2790 }
2791 
2792 PetscErrorCode PCSetFromOptions_SMG(PC pc, PetscOptionItems *PetscOptionsObject)
2793 {
2794   PC_SMG *ex = (PC_SMG *)pc->data;
2795 
2796   PetscFunctionBegin;
2797   PetscOptionsHeadBegin(PetscOptionsObject, "SMG options");
2798 
2799   PetscCall(PetscOptionsInt("-pc_smg_its", "Number of iterations of SMG to use as preconditioner", "HYPRE_StructSMGSetMaxIter", ex->its, &ex->its, NULL));
2800   PetscCall(PetscOptionsInt("-pc_smg_num_pre_relax", "Number of smoothing steps before coarse grid", "HYPRE_StructSMGSetNumPreRelax", ex->num_pre_relax, &ex->num_pre_relax, NULL));
2801   PetscCall(PetscOptionsInt("-pc_smg_num_post_relax", "Number of smoothing steps after coarse grid", "HYPRE_StructSMGSetNumPostRelax", ex->num_post_relax, &ex->num_post_relax, NULL));
2802   PetscCall(PetscOptionsReal("-pc_smg_tol", "Tolerance of SMG", "HYPRE_StructSMGSetTol", ex->tol, &ex->tol, NULL));
2803 
2804   PetscOptionsHeadEnd();
2805   PetscFunctionReturn(PETSC_SUCCESS);
2806 }
2807 
2808 PetscErrorCode PCApply_SMG(PC pc, Vec x, Vec y)
2809 {
2810   PC_SMG            *ex = (PC_SMG *)pc->data;
2811   PetscScalar       *yy;
2812   const PetscScalar *xx;
2813   PetscInt           ilower[3], iupper[3];
2814   HYPRE_Int          hlower[3], hupper[3];
2815   Mat_HYPREStruct   *mx = (Mat_HYPREStruct *)(pc->pmat->data);
2816 
2817   PetscFunctionBegin;
2818   PetscCall(PetscCitationsRegister(hypreCitation, &cite));
2819   PetscCall(DMDAGetCorners(mx->da, &ilower[0], &ilower[1], &ilower[2], &iupper[0], &iupper[1], &iupper[2]));
2820   /* when HYPRE_MIXEDINT is defined, sizeof(HYPRE_Int) == 32 */
2821   iupper[0] += ilower[0] - 1;
2822   iupper[1] += ilower[1] - 1;
2823   iupper[2] += ilower[2] - 1;
2824   hlower[0] = (HYPRE_Int)ilower[0];
2825   hlower[1] = (HYPRE_Int)ilower[1];
2826   hlower[2] = (HYPRE_Int)ilower[2];
2827   hupper[0] = (HYPRE_Int)iupper[0];
2828   hupper[1] = (HYPRE_Int)iupper[1];
2829   hupper[2] = (HYPRE_Int)iupper[2];
2830 
2831   /* copy x values over to hypre */
2832   PetscCallExternal(HYPRE_StructVectorSetConstantValues, mx->hb, 0.0);
2833   PetscCall(VecGetArrayRead(x, &xx));
2834   PetscCallExternal(HYPRE_StructVectorSetBoxValues, mx->hb, hlower, hupper, (HYPRE_Complex *)xx);
2835   PetscCall(VecRestoreArrayRead(x, &xx));
2836   PetscCallExternal(HYPRE_StructVectorAssemble, mx->hb);
2837   PetscCallExternal(HYPRE_StructSMGSolve, ex->hsolver, mx->hmat, mx->hb, mx->hx);
2838 
2839   /* copy solution values back to PETSc */
2840   PetscCall(VecGetArray(y, &yy));
2841   PetscCallExternal(HYPRE_StructVectorGetBoxValues, mx->hx, hlower, hupper, (HYPRE_Complex *)yy);
2842   PetscCall(VecRestoreArray(y, &yy));
2843   PetscFunctionReturn(PETSC_SUCCESS);
2844 }
2845 
2846 static PetscErrorCode PCApplyRichardson_SMG(PC pc, Vec b, Vec y, Vec w, PetscReal rtol, PetscReal abstol, PetscReal dtol, PetscInt its, PetscBool guesszero, PetscInt *outits, PCRichardsonConvergedReason *reason)
2847 {
2848   PC_SMG   *jac = (PC_SMG *)pc->data;
2849   HYPRE_Int oits;
2850 
2851   PetscFunctionBegin;
2852   PetscCall(PetscCitationsRegister(hypreCitation, &cite));
2853   PetscCallExternal(HYPRE_StructSMGSetMaxIter, jac->hsolver, its * jac->its);
2854   PetscCallExternal(HYPRE_StructSMGSetTol, jac->hsolver, rtol);
2855 
2856   PetscCall(PCApply_SMG(pc, b, y));
2857   PetscCallExternal(HYPRE_StructSMGGetNumIterations, jac->hsolver, &oits);
2858   *outits = oits;
2859   if (oits == its) *reason = PCRICHARDSON_CONVERGED_ITS;
2860   else *reason = PCRICHARDSON_CONVERGED_RTOL;
2861   PetscCallExternal(HYPRE_StructSMGSetTol, jac->hsolver, jac->tol);
2862   PetscCallExternal(HYPRE_StructSMGSetMaxIter, jac->hsolver, jac->its);
2863   PetscFunctionReturn(PETSC_SUCCESS);
2864 }
2865 
2866 PetscErrorCode PCSetUp_SMG(PC pc)
2867 {
2868   PetscInt         i, dim;
2869   PC_SMG          *ex = (PC_SMG *)pc->data;
2870   Mat_HYPREStruct *mx = (Mat_HYPREStruct *)(pc->pmat->data);
2871   PetscBool        flg;
2872   DMBoundaryType   p[3];
2873   PetscInt         M[3];
2874 
2875   PetscFunctionBegin;
2876   PetscCall(PetscObjectTypeCompare((PetscObject)pc->pmat, MATHYPRESTRUCT, &flg));
2877   PetscCheck(flg, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "Must use MATHYPRESTRUCT with this preconditioner");
2878 
2879   PetscCall(DMDAGetInfo(mx->da, &dim, &M[0], &M[1], &M[2], 0, 0, 0, 0, 0, &p[0], &p[1], &p[2], 0));
2880   // Check if power of 2 in periodic directions
2881   for (i = 0; i < dim; i++) {
2882     if (((M[i] & (M[i] - 1)) != 0) && (p[i] == DM_BOUNDARY_PERIODIC)) {
2883       SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "With SMG, the number of points in a periodic direction must be a power of 2, but is here %" PetscInt_FMT ".", M[i]);
2884     }
2885   }
2886 
2887   /* create the hypre solver object and set its information */
2888   if (ex->hsolver) PetscCallExternal(HYPRE_StructSMGDestroy, (ex->hsolver));
2889   PetscCallExternal(HYPRE_StructSMGCreate, ex->hcomm, &ex->hsolver);
2890   // The hypre options must be set here and not in SetFromOptions because it is created here!
2891   PetscCallExternal(HYPRE_StructSMGSetMaxIter, ex->hsolver, ex->its);
2892   PetscCallExternal(HYPRE_StructSMGSetNumPreRelax, ex->hsolver, ex->num_pre_relax);
2893   PetscCallExternal(HYPRE_StructSMGSetNumPostRelax, ex->hsolver, ex->num_post_relax);
2894   PetscCallExternal(HYPRE_StructSMGSetTol, ex->hsolver, ex->tol);
2895 
2896   PetscCallExternal(HYPRE_StructSMGSetup, ex->hsolver, mx->hmat, mx->hb, mx->hx);
2897   PetscCallExternal(HYPRE_StructSMGSetZeroGuess, ex->hsolver);
2898   PetscFunctionReturn(PETSC_SUCCESS);
2899 }
2900 
2901 /*MC
2902      PCSMG - the hypre (structured grid) SMG multigrid solver
2903 
2904    Level: advanced
2905 
2906    Options Database Keys:
2907 + -pc_smg_its <its> - number of iterations of SMG to use as preconditioner
2908 . -pc_smg_num_pre_relax <steps> - number of smoothing steps before coarse grid
2909 . -pc_smg_num_post_relax <steps> - number of smoothing steps after coarse grid
2910 - -pc_smg_tol <tol> - tolerance of SMG
2911 
2912    Notes:
2913    This is for CELL-centered descretizations
2914 
2915    This must be used with the `MATHYPRESTRUCT` `MatType`.
2916 
2917    This does not provide all the functionality of  hypre's SMG solver, it supports only one block per process defined by a PETSc `DMDA`.
2918 
2919    See `PCSYSPFMG`, `PCSMG`, `PCPFMG`, and `PCHYPRE` for access to hypre's other preconditioners
2920 
2921 .seealso:  `PCMG`, `MATHYPRESTRUCT`, `PCPFMG`, `PCSYSPFMG`, `PCHYPRE`, `PCGAMG`
2922 M*/
2923 
2924 PETSC_EXTERN PetscErrorCode PCCreate_SMG(PC pc)
2925 {
2926   PC_SMG *ex;
2927 
2928   PetscFunctionBegin;
2929   PetscCall(PetscNew(&ex));
2930   pc->data = ex;
2931 
2932   ex->its            = 1;
2933   ex->tol            = 1.e-8;
2934   ex->num_pre_relax  = 1;
2935   ex->num_post_relax = 1;
2936 
2937   pc->ops->setfromoptions  = PCSetFromOptions_SMG;
2938   pc->ops->view            = PCView_SMG;
2939   pc->ops->destroy         = PCDestroy_SMG;
2940   pc->ops->apply           = PCApply_SMG;
2941   pc->ops->applyrichardson = PCApplyRichardson_SMG;
2942   pc->ops->setup           = PCSetUp_SMG;
2943 
2944   PetscCall(PetscCommGetComm(PetscObjectComm((PetscObject)pc), &ex->hcomm));
2945   PetscCallExternal(HYPRE_StructSMGCreate, ex->hcomm, &ex->hsolver);
2946   PetscFunctionReturn(PETSC_SUCCESS);
2947 }
2948