Actual source code: fieldsplit.c

  1: #include <petsc/private/pcimpl.h>
  2: #include <petsc/private/kspimpl.h>
  3: #include <petsc/private/matimpl.h>
  4: #include <petscdm.h>
  5: #include <petscdevice.h>
  6: #if PetscDefined(HAVE_CUDA)
  7: #include <petscdevice_cuda.h>
  8: #endif
  9: #if PetscDefined(HAVE_HIP)
 10: #include <petscdevice_hip.h>
 11: #endif

 13: const char *const PCFieldSplitSchurPreTypes[]  = {"SELF", "SELFP", "A11", "USER", "FULL", "PCFieldSplitSchurPreType", "PC_FIELDSPLIT_SCHUR_PRE_", NULL};
 14: const char *const PCFieldSplitSchurFactTypes[] = {"DIAG", "LOWER", "UPPER", "FULL", "PCFieldSplitSchurFactType", "PC_FIELDSPLIT_SCHUR_FACT_", NULL};

 16: PetscLogEvent KSP_Solve_FS_0, KSP_Solve_FS_1, KSP_Solve_FS_S, KSP_Solve_FS_U, KSP_Solve_FS_L, KSP_Solve_FS_2, KSP_Solve_FS_3, KSP_Solve_FS_4;

 18: typedef struct _PC_FieldSplitLink *PC_FieldSplitLink;
 19: struct _PC_FieldSplitLink {
 20:   KSP               ksp;
 21:   Vec               x, y, z;
 22:   Mat               X, Y, Z;
 23:   char             *splitname;
 24:   PetscInt          nfields;
 25:   PetscInt         *fields, *fields_col;
 26:   VecScatter        sctx;
 27:   IS                is, is_col;
 28:   PC_FieldSplitLink next, previous;
 29:   PetscLogEvent     event;

 31:   /* Used only when setting coordinates with PCSetCoordinates */
 32:   PetscInt   dim;
 33:   PetscInt   ndofs;
 34:   PetscReal *coords;
 35: };

 37: typedef struct {
 38:   PCCompositeType type;
 39:   PetscBool       defaultsplit; /* Flag for a system with a set of 'k' scalar fields with the same layout (and bs = k) */
 40:   PetscBool       splitdefined; /* Flag is set after the splits have been defined, to prevent more splits from being added */
 41:   PetscInt        bs;           /* Block size for IS and Mat structures */
 42:   PetscInt        nsplits;      /* Number of field divisions defined */
 43:   Vec            *x, *y, w1, w2;
 44:   Mat            *mat;    /* The diagonal block for each split */
 45:   Mat            *pmat;   /* The preconditioning diagonal block for each split */
 46:   Mat            *Afield; /* The rows of the matrix associated with each split */
 47:   PetscBool       issetup;

 49:   /* Only used when Schur complement preconditioning is used */
 50:   Mat                       B;          /* The (0,1) block */
 51:   Mat                       C;          /* The (1,0) block */
 52:   Mat                       schur;      /* The Schur complement S = A11 - A10 A00^{-1} A01, the KSP here, kspinner, is H_1 in [El08] */
 53:   Mat                       schurp;     /* Assembled approximation to S built by MatSchurComplement to be used as a matrix for constructing the preconditioner when solving with S */
 54:   Mat                       schur_user; /* User-provided matrix for constructing the preconditioner for the Schur complement */
 55:   PCFieldSplitSchurPreType  schurpre;   /* Determines which matrix is used for the Schur complement */
 56:   PCFieldSplitSchurFactType schurfactorization;
 57:   KSP                       kspschur;   /* The solver for S */
 58:   KSP                       kspupper;   /* The solver for A in the upper diagonal part of the factorization (H_2 in [El08]) */
 59:   PetscScalar               schurscale; /* Scaling factor for the Schur complement solution with DIAG factorization */

 61:   /* Only used when Golub-Kahan bidiagonalization preconditioning is used */
 62:   Mat          H;           /* The modified matrix H = A00 + nu*A01*A01'              */
 63:   PetscReal    gkbtol;      /* Stopping tolerance for lower bound estimate            */
 64:   PetscInt     gkbdelay;    /* The delay window for the stopping criterion            */
 65:   PetscReal    gkbnu;       /* Parameter for augmented Lagrangian H = A + nu*A01*A01' */
 66:   PetscInt     gkbmaxit;    /* Maximum number of iterations for outer loop            */
 67:   PetscBool    gkbmonitor;  /* Monitor for gkb iterations and the lower bound error   */
 68:   PetscViewer  gkbviewer;   /* Viewer context for gkbmonitor                          */
 69:   Vec          u, v, d, Hu; /* Work vectors for the GKB algorithm                     */
 70:   PetscScalar *vecz;        /* Contains intermediate values, eg for lower bound       */

 72:   PC_FieldSplitLink head;
 73:   PetscBool         isrestrict;       /* indicates PCFieldSplitRestrictIS() has been last called on this object, hack */
 74:   PetscBool         suboptionsset;    /* Indicates that the KSPSetFromOptions() has been called on the sub-KSPs */
 75:   PetscBool         dm_splits;        /* Whether to use DMCreateFieldDecomposition() whenever possible */
 76:   PetscBool         diag_use_amat;    /* Whether to extract diagonal matrix blocks from Amat, rather than Pmat (weaker than -pc_use_amat) */
 77:   PetscBool         offdiag_use_amat; /* Whether to extract off-diagonal matrix blocks from Amat, rather than Pmat (weaker than -pc_use_amat) */
 78:   PetscBool         detect;           /* Whether to form 2-way split by finding zero diagonal entries */
 79:   PetscBool         coordinates_set;  /* Whether PCSetCoordinates has been called */
 80: } PC_FieldSplit;

 82: /*
 83:     Note:
 84:     there is no particular reason that pmat, x, and y are stored as arrays in PC_FieldSplit instead of
 85:    inside PC_FieldSplitLink, just historical. If you want to be able to add new fields after already using the
 86:    PC you could change this.
 87: */

 89: /* This helper is so that setting a user-provided matrix is orthogonal to choosing to use it.  This way the
 90: * application-provided FormJacobian can provide this matrix without interfering with the user's (command-line) choices. */
 91: static Mat FieldSplitSchurPre(PC_FieldSplit *jac)
 92: {
 93:   switch (jac->schurpre) {
 94:   case PC_FIELDSPLIT_SCHUR_PRE_SELF:
 95:     return jac->schur;
 96:   case PC_FIELDSPLIT_SCHUR_PRE_SELFP:
 97:     return jac->schurp;
 98:   case PC_FIELDSPLIT_SCHUR_PRE_A11:
 99:     return jac->pmat[1];
100:   case PC_FIELDSPLIT_SCHUR_PRE_FULL: /* We calculate this and store it in schur_user */
101:   case PC_FIELDSPLIT_SCHUR_PRE_USER: /* Use a user-provided matrix if it is given, otherwise diagonal block */
102:   default:
103:     return jac->schur_user ? jac->schur_user : jac->pmat[1];
104:   }
105: }

107: #include <petscdraw.h>
108: static PetscErrorCode PCView_FieldSplit(PC pc, PetscViewer viewer)
109: {
110:   PC_FieldSplit    *jac = (PC_FieldSplit *)pc->data;
111:   PetscBool         isascii, isdraw;
112:   PetscInt          i, j;
113:   PC_FieldSplitLink ilink = jac->head;

115:   PetscFunctionBegin;
116:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
117:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
118:   if (isascii) {
119:     if (jac->bs > 0) {
120:       PetscCall(PetscViewerASCIIPrintf(viewer, "  FieldSplit with %s composition: total splits = %" PetscInt_FMT ", blocksize = %" PetscInt_FMT "\n", PCCompositeTypes[jac->type], jac->nsplits, jac->bs));
121:     } else {
122:       PetscCall(PetscViewerASCIIPrintf(viewer, "  FieldSplit with %s composition: total splits = %" PetscInt_FMT "\n", PCCompositeTypes[jac->type], jac->nsplits));
123:     }
124:     if (pc->useAmat) PetscCall(PetscViewerASCIIPrintf(viewer, "  using Amat (not Pmat) as operator for blocks\n"));
125:     if (jac->diag_use_amat) PetscCall(PetscViewerASCIIPrintf(viewer, "  using Amat (not Pmat) as operator for diagonal blocks\n"));
126:     if (jac->offdiag_use_amat) PetscCall(PetscViewerASCIIPrintf(viewer, "  using Amat (not Pmat) as operator for off-diagonal blocks\n"));
127:     PetscCall(PetscViewerASCIIPrintf(viewer, "  Solver info for each split is in the following KSP objects:\n"));
128:     for (i = 0; i < jac->nsplits; i++) {
129:       if (ilink->fields) {
130:         PetscCall(PetscViewerASCIIPrintf(viewer, "Split number %" PetscInt_FMT " Fields ", i));
131:         PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
132:         for (j = 0; j < ilink->nfields; j++) {
133:           if (j > 0) PetscCall(PetscViewerASCIIPrintf(viewer, ","));
134:           PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT, ilink->fields[j]));
135:         }
136:         PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
137:         PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
138:       } else {
139:         PetscCall(PetscViewerASCIIPrintf(viewer, "Split number %" PetscInt_FMT " Defined by IS\n", i));
140:       }
141:       PetscCall(KSPView(ilink->ksp, viewer));
142:       ilink = ilink->next;
143:     }
144:   }

146:   if (isdraw) {
147:     PetscDraw draw;
148:     PetscReal x, y, w, wd;

150:     PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
151:     PetscCall(PetscDrawGetCurrentPoint(draw, &x, &y));
152:     w  = 2 * PetscMin(1.0 - x, x);
153:     wd = w / (jac->nsplits + 1);
154:     x  = x - wd * (jac->nsplits - 1) / 2.0;
155:     for (i = 0; i < jac->nsplits; i++) {
156:       PetscCall(PetscDrawPushCurrentPoint(draw, x, y));
157:       PetscCall(KSPView(ilink->ksp, viewer));
158:       PetscCall(PetscDrawPopCurrentPoint(draw));
159:       x += wd;
160:       ilink = ilink->next;
161:     }
162:   }
163:   PetscFunctionReturn(PETSC_SUCCESS);
164: }

166: static PetscErrorCode PCView_FieldSplit_Schur(PC pc, PetscViewer viewer)
167: {
168:   PC_FieldSplit             *jac = (PC_FieldSplit *)pc->data;
169:   PetscBool                  isascii, isdraw;
170:   PetscInt                   i, j;
171:   PC_FieldSplitLink          ilink = jac->head;
172:   MatSchurComplementAinvType atype;

174:   PetscFunctionBegin;
175:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
176:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
177:   if (isascii) {
178:     if (jac->bs > 0) {
179:       PetscCall(PetscViewerASCIIPrintf(viewer, "  FieldSplit with Schur preconditioner, blocksize = %" PetscInt_FMT ", factorization %s\n", jac->bs, PCFieldSplitSchurFactTypes[jac->schurfactorization]));
180:     } else {
181:       PetscCall(PetscViewerASCIIPrintf(viewer, "  FieldSplit with Schur preconditioner, factorization %s\n", PCFieldSplitSchurFactTypes[jac->schurfactorization]));
182:     }
183:     if (pc->useAmat) PetscCall(PetscViewerASCIIPrintf(viewer, "  using Amat (not Pmat) as operator for blocks\n"));
184:     switch (jac->schurpre) {
185:     case PC_FIELDSPLIT_SCHUR_PRE_SELF:
186:       PetscCall(PetscViewerASCIIPrintf(viewer, "  Preconditioner for the Schur complement formed from S itself\n"));
187:       break;
188:     case PC_FIELDSPLIT_SCHUR_PRE_SELFP:
189:       if (jac->schur) {
190:         PetscCall(MatSchurComplementGetAinvType(jac->schur, &atype));
191:         PetscCall(PetscViewerASCIIPrintf(viewer, "  Preconditioner for the Schur complement formed from Sp, an assembled approximation to S, which uses A00's %sinverse\n", atype == MAT_SCHUR_COMPLEMENT_AINV_DIAG ? "diagonal's " : (atype == MAT_SCHUR_COMPLEMENT_AINV_BLOCK_DIAG ? "block diagonal's " : (atype == MAT_SCHUR_COMPLEMENT_AINV_FULL ? "full " : "lumped diagonal's "))));
192:       }
193:       break;
194:     case PC_FIELDSPLIT_SCHUR_PRE_A11:
195:       PetscCall(PetscViewerASCIIPrintf(viewer, "  Preconditioner for the Schur complement formed from A11\n"));
196:       break;
197:     case PC_FIELDSPLIT_SCHUR_PRE_FULL:
198:       PetscCall(PetscViewerASCIIPrintf(viewer, "  Preconditioner for the Schur complement formed from the exact Schur complement\n"));
199:       break;
200:     case PC_FIELDSPLIT_SCHUR_PRE_USER:
201:       if (jac->schur_user) {
202:         PetscCall(PetscViewerASCIIPrintf(viewer, "  Preconditioner for the Schur complement formed from user provided matrix\n"));
203:       } else {
204:         PetscCall(PetscViewerASCIIPrintf(viewer, "  Preconditioner for the Schur complement formed from A11\n"));
205:       }
206:       break;
207:     default:
208:       SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Invalid Schur preconditioning type: %d", jac->schurpre);
209:     }
210:     PetscCall(PetscViewerASCIIPrintf(viewer, "  Split info:\n"));
211:     PetscCall(PetscViewerASCIIPushTab(viewer));
212:     for (i = 0; i < jac->nsplits; i++) {
213:       if (ilink->fields) {
214:         PetscCall(PetscViewerASCIIPrintf(viewer, "Split number %" PetscInt_FMT " Fields ", i));
215:         PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
216:         for (j = 0; j < ilink->nfields; j++) {
217:           if (j > 0) PetscCall(PetscViewerASCIIPrintf(viewer, ","));
218:           PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT, ilink->fields[j]));
219:         }
220:         PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
221:         PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
222:       } else {
223:         PetscCall(PetscViewerASCIIPrintf(viewer, "Split number %" PetscInt_FMT " Defined by IS\n", i));
224:       }
225:       ilink = ilink->next;
226:     }
227:     PetscCall(PetscViewerASCIIPrintf(viewer, "KSP solver for A00 block\n"));
228:     PetscCall(PetscViewerASCIIPushTab(viewer));
229:     if (jac->head) PetscCall(KSPView(jac->head->ksp, viewer));
230:     else PetscCall(PetscViewerASCIIPrintf(viewer, "  not yet available\n"));
231:     PetscCall(PetscViewerASCIIPopTab(viewer));
232:     if (jac->head && jac->kspupper != jac->head->ksp) {
233:       PetscCall(PetscViewerASCIIPrintf(viewer, "KSP solver for upper A00 in upper triangular factor\n"));
234:       PetscCall(PetscViewerASCIIPushTab(viewer));
235:       if (jac->kspupper) PetscCall(KSPView(jac->kspupper, viewer));
236:       else PetscCall(PetscViewerASCIIPrintf(viewer, "  not yet available\n"));
237:       PetscCall(PetscViewerASCIIPopTab(viewer));
238:     }
239:     PetscCall(PetscViewerASCIIPrintf(viewer, "KSP solver for S = A11 - A10 inv(A00) A01\n"));
240:     PetscCall(PetscViewerASCIIPushTab(viewer));
241:     if (jac->kspschur) {
242:       PetscCall(KSPView(jac->kspschur, viewer));
243:     } else {
244:       PetscCall(PetscViewerASCIIPrintf(viewer, "  not yet available\n"));
245:     }
246:     PetscCall(PetscViewerASCIIPopTab(viewer));
247:     PetscCall(PetscViewerASCIIPopTab(viewer));
248:   } else if (isdraw && jac->head) {
249:     PetscDraw draw;
250:     PetscReal x, y, w, wd, h;
251:     PetscInt  cnt = 2;
252:     char      str[32];

254:     PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
255:     PetscCall(PetscDrawGetCurrentPoint(draw, &x, &y));
256:     if (jac->kspupper != jac->head->ksp) cnt++;
257:     w  = 2 * PetscMin(1.0 - x, x);
258:     wd = w / (cnt + 1);

260:     PetscCall(PetscSNPrintf(str, 32, "Schur fact. %s", PCFieldSplitSchurFactTypes[jac->schurfactorization]));
261:     PetscCall(PetscDrawStringBoxed(draw, x, y, PETSC_DRAW_RED, PETSC_DRAW_BLACK, str, NULL, &h));
262:     y -= h;
263:     if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_USER && !jac->schur_user) {
264:       PetscCall(PetscSNPrintf(str, 32, "Prec. for Schur from %s", PCFieldSplitSchurPreTypes[PC_FIELDSPLIT_SCHUR_PRE_A11]));
265:     } else {
266:       PetscCall(PetscSNPrintf(str, 32, "Prec. for Schur from %s", PCFieldSplitSchurPreTypes[jac->schurpre]));
267:     }
268:     PetscCall(PetscDrawStringBoxed(draw, x + wd * (cnt - 1) / 2.0, y, PETSC_DRAW_RED, PETSC_DRAW_BLACK, str, NULL, &h));
269:     y -= h;
270:     x = x - wd * (cnt - 1) / 2.0;

272:     PetscCall(PetscDrawPushCurrentPoint(draw, x, y));
273:     PetscCall(KSPView(jac->head->ksp, viewer));
274:     PetscCall(PetscDrawPopCurrentPoint(draw));
275:     if (jac->kspupper != jac->head->ksp) {
276:       x += wd;
277:       PetscCall(PetscDrawPushCurrentPoint(draw, x, y));
278:       PetscCall(KSPView(jac->kspupper, viewer));
279:       PetscCall(PetscDrawPopCurrentPoint(draw));
280:     }
281:     x += wd;
282:     PetscCall(PetscDrawPushCurrentPoint(draw, x, y));
283:     PetscCall(KSPView(jac->kspschur, viewer));
284:     PetscCall(PetscDrawPopCurrentPoint(draw));
285:   }
286:   PetscFunctionReturn(PETSC_SUCCESS);
287: }

289: static PetscErrorCode PCView_FieldSplit_GKB(PC pc, PetscViewer viewer)
290: {
291:   PC_FieldSplit    *jac = (PC_FieldSplit *)pc->data;
292:   PetscBool         isascii, isdraw;
293:   PetscInt          i, j;
294:   PC_FieldSplitLink ilink = jac->head;

296:   PetscFunctionBegin;
297:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
298:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
299:   if (isascii) {
300:     if (jac->bs > 0) {
301:       PetscCall(PetscViewerASCIIPrintf(viewer, "  FieldSplit with %s composition: total splits = %" PetscInt_FMT ", blocksize = %" PetscInt_FMT "\n", PCCompositeTypes[jac->type], jac->nsplits, jac->bs));
302:     } else {
303:       PetscCall(PetscViewerASCIIPrintf(viewer, "  FieldSplit with %s composition: total splits = %" PetscInt_FMT "\n", PCCompositeTypes[jac->type], jac->nsplits));
304:     }
305:     if (pc->useAmat) PetscCall(PetscViewerASCIIPrintf(viewer, "  using Amat (not Pmat) as operator for blocks\n"));
306:     if (jac->diag_use_amat) PetscCall(PetscViewerASCIIPrintf(viewer, "  using Amat (not Pmat) as operator for diagonal blocks\n"));
307:     if (jac->offdiag_use_amat) PetscCall(PetscViewerASCIIPrintf(viewer, "  using Amat (not Pmat) as operator for off-diagonal blocks\n"));

309:     PetscCall(PetscViewerASCIIPrintf(viewer, "  Stopping tolerance=%.1e, delay in error estimate=%" PetscInt_FMT ", maximum iterations=%" PetscInt_FMT "\n", (double)jac->gkbtol, jac->gkbdelay, jac->gkbmaxit));
310:     PetscCall(PetscViewerASCIIPrintf(viewer, "  Solver info for H = A00 + nu*A01*A01' matrix:\n"));
311:     PetscCall(PetscViewerASCIIPushTab(viewer));

313:     if (ilink->fields) {
314:       PetscCall(PetscViewerASCIIPrintf(viewer, "Split number 0 Fields "));
315:       PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
316:       for (j = 0; j < ilink->nfields; j++) {
317:         if (j > 0) PetscCall(PetscViewerASCIIPrintf(viewer, ","));
318:         PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT, ilink->fields[j]));
319:       }
320:       PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
321:       PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
322:     } else {
323:       PetscCall(PetscViewerASCIIPrintf(viewer, "Split number 0 Defined by IS\n"));
324:     }
325:     PetscCall(KSPView(ilink->ksp, viewer));

327:     PetscCall(PetscViewerASCIIPopTab(viewer));
328:   }

330:   if (isdraw) {
331:     PetscDraw draw;
332:     PetscReal x, y, w, wd;

334:     PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
335:     PetscCall(PetscDrawGetCurrentPoint(draw, &x, &y));
336:     w  = 2 * PetscMin(1.0 - x, x);
337:     wd = w / (jac->nsplits + 1);
338:     x  = x - wd * (jac->nsplits - 1) / 2.0;
339:     for (i = 0; i < jac->nsplits; i++) {
340:       PetscCall(PetscDrawPushCurrentPoint(draw, x, y));
341:       PetscCall(KSPView(ilink->ksp, viewer));
342:       PetscCall(PetscDrawPopCurrentPoint(draw));
343:       x += wd;
344:       ilink = ilink->next;
345:     }
346:   }
347:   PetscFunctionReturn(PETSC_SUCCESS);
348: }

350: /* Precondition: jac->bs is set to a meaningful value or MATNEST */
351: static PetscErrorCode PCFieldSplitSetRuntimeSplits_Private(PC pc)
352: {
353:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
354:   PetscInt       bs, i, nfields, *ifields, nfields_col, *ifields_col;
355:   PetscBool      flg, flg_col, mnest;
356:   char           optionname[128], splitname[8], optionname_col[128];

358:   PetscFunctionBegin;
359:   PetscCall(PetscObjectTypeCompare((PetscObject)pc->mat, MATNEST, &mnest));
360:   if (mnest) PetscCall(MatNestGetSize(pc->pmat, &bs, NULL));
361:   else bs = jac->bs;
362:   PetscCall(PetscMalloc2(bs, &ifields, bs, &ifields_col));
363:   for (i = 0, flg = PETSC_TRUE;; i++) {
364:     PetscCall(PetscSNPrintf(splitname, sizeof(splitname), "%" PetscInt_FMT, i));
365:     PetscCall(PetscSNPrintf(optionname, sizeof(optionname), "-pc_fieldsplit_%" PetscInt_FMT "_fields", i));
366:     PetscCall(PetscSNPrintf(optionname_col, sizeof(optionname_col), "-pc_fieldsplit_%" PetscInt_FMT "_fields_col", i));
367:     nfields     = bs;
368:     nfields_col = bs;
369:     PetscCall(PetscOptionsGetIntArray(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, optionname, ifields, &nfields, &flg));
370:     PetscCall(PetscOptionsGetIntArray(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, optionname_col, ifields_col, &nfields_col, &flg_col));
371:     if (!flg) break;
372:     else if (flg && !flg_col) {
373:       PetscCheck(nfields, PETSC_COMM_SELF, PETSC_ERR_USER, "Cannot list zero fields");
374:       PetscCall(PCFieldSplitSetFields(pc, splitname, nfields, ifields, ifields));
375:     } else {
376:       PetscCheck(nfields && nfields_col, PETSC_COMM_SELF, PETSC_ERR_USER, "Cannot list zero fields");
377:       PetscCheck(nfields == nfields_col, PETSC_COMM_SELF, PETSC_ERR_USER, "Number of row and column fields must match");
378:       PetscCall(PCFieldSplitSetFields(pc, splitname, nfields, ifields, ifields_col));
379:     }
380:   }
381:   if (i > 0) {
382:     /* Makes command-line setting of splits take precedence over setting them in code.
383:        Otherwise subsequent calls to PCFieldSplitSetIS() or PCFieldSplitSetFields() would
384:        create new splits, which would probably not be what the user wanted. */
385:     jac->splitdefined = PETSC_TRUE;
386:   }
387:   PetscCall(PetscFree2(ifields, ifields_col));
388:   PetscFunctionReturn(PETSC_SUCCESS);
389: }

391: static PetscErrorCode PCFieldSplitSetDefaults(PC pc)
392: {
393:   PC_FieldSplit    *jac                = (PC_FieldSplit *)pc->data;
394:   PC_FieldSplitLink ilink              = jac->head;
395:   PetscBool         fieldsplit_default = PETSC_FALSE, coupling = PETSC_FALSE;
396:   PetscInt          i;

398:   PetscFunctionBegin;
399:   /*
400:    Kinda messy, but at least this now uses DMCreateFieldDecomposition().
401:    Should probably be rewritten.
402:    */
403:   if (!ilink) {
404:     PetscCall(PetscOptionsGetBool(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, "-pc_fieldsplit_detect_coupling", &coupling, NULL));
405:     if (pc->dm && jac->dm_splits && !jac->detect && !coupling) {
406:       PetscInt  numFields, f, i, j;
407:       char    **fieldNames;
408:       IS       *fields;
409:       DM       *dms;
410:       DM        subdm[128];
411:       PetscBool flg;

413:       PetscCall(DMCreateFieldDecomposition(pc->dm, &numFields, &fieldNames, &fields, &dms));
414:       /* Allow the user to prescribe the splits */
415:       for (i = 0, flg = PETSC_TRUE;; i++) {
416:         PetscInt ifields[128];
417:         IS       compField;
418:         char     optionname[128], splitname[8];
419:         PetscInt nfields = numFields;

421:         PetscCall(PetscSNPrintf(optionname, sizeof(optionname), "-pc_fieldsplit_%" PetscInt_FMT "_fields", i));
422:         PetscCall(PetscOptionsGetIntArray(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, optionname, ifields, &nfields, &flg));
423:         if (!flg) break;
424:         PetscCheck(numFields <= 128, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Cannot currently support %" PetscInt_FMT " > 128 fields", numFields);
425:         PetscCall(DMCreateSubDM(pc->dm, nfields, ifields, &compField, &subdm[i]));
426:         if (nfields == 1) PetscCall(PCFieldSplitSetIS(pc, fieldNames[ifields[0]], compField));
427:         else {
428:           PetscCall(PetscSNPrintf(splitname, sizeof(splitname), "%" PetscInt_FMT, i));
429:           PetscCall(PCFieldSplitSetIS(pc, splitname, compField));
430:         }
431:         PetscCall(ISDestroy(&compField));
432:         for (j = 0; j < nfields; ++j) {
433:           f = ifields[j];
434:           PetscCall(PetscFree(fieldNames[f]));
435:           PetscCall(ISDestroy(&fields[f]));
436:         }
437:       }
438:       if (i == 0) {
439:         for (f = 0; f < numFields; ++f) {
440:           PetscCall(PCFieldSplitSetIS(pc, fieldNames[f], fields[f]));
441:           PetscCall(PetscFree(fieldNames[f]));
442:           PetscCall(ISDestroy(&fields[f]));
443:         }
444:       } else {
445:         for (j = 0; j < numFields; j++) PetscCall(DMDestroy(dms + j));
446:         PetscCall(PetscFree(dms));
447:         PetscCall(PetscMalloc1(i, &dms));
448:         for (j = 0; j < i; ++j) dms[j] = subdm[j];
449:       }
450:       PetscCall(PetscFree(fieldNames));
451:       PetscCall(PetscFree(fields));
452:       if (dms) {
453:         PetscCall(PetscInfo(pc, "Setting up physics based fieldsplit preconditioner using the embedded DM\n"));
454:         for (ilink = jac->head, i = 0; ilink; ilink = ilink->next, ++i) {
455:           const char *prefix;
456:           PetscCall(PetscObjectGetOptionsPrefix((PetscObject)ilink->ksp, &prefix));
457:           PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dms[i], prefix));
458:           PetscCall(KSPSetDM(ilink->ksp, dms[i]));
459:           PetscCall(KSPSetDMActive(ilink->ksp, KSP_DMACTIVE_ALL, PETSC_FALSE));
460:           PetscCall(PetscObjectIncrementTabLevel((PetscObject)dms[i], (PetscObject)ilink->ksp, 0));
461:           PetscCall(DMDestroy(&dms[i]));
462:         }
463:         PetscCall(PetscFree(dms));
464:       }
465:     } else {
466:       if (jac->bs <= 0) {
467:         if (pc->pmat) PetscCall(MatGetBlockSize(pc->pmat, &jac->bs));
468:         else jac->bs = 1;
469:       }

471:       if (jac->detect) {
472:         IS       zerodiags, rest;
473:         PetscInt nmin, nmax;

475:         PetscCall(MatGetOwnershipRange(pc->mat, &nmin, &nmax));
476:         if (jac->diag_use_amat) {
477:           PetscCall(MatFindZeroDiagonals(pc->mat, &zerodiags));
478:         } else {
479:           PetscCall(MatFindZeroDiagonals(pc->pmat, &zerodiags));
480:         }
481:         PetscCall(ISComplement(zerodiags, nmin, nmax, &rest));
482:         PetscCall(PCFieldSplitSetIS(pc, "0", rest));
483:         PetscCall(PCFieldSplitSetIS(pc, "1", zerodiags));
484:         PetscCall(ISDestroy(&zerodiags));
485:         PetscCall(ISDestroy(&rest));
486:       } else if (coupling) {
487:         IS       coupling, rest;
488:         PetscInt nmin, nmax;

490:         PetscCall(MatGetOwnershipRange(pc->mat, &nmin, &nmax));
491:         if (jac->offdiag_use_amat) {
492:           PetscCall(MatFindOffBlockDiagonalEntries(pc->mat, &coupling));
493:         } else {
494:           PetscCall(MatFindOffBlockDiagonalEntries(pc->pmat, &coupling));
495:         }
496:         PetscCall(ISCreateStride(PetscObjectComm((PetscObject)pc->mat), nmax - nmin, nmin, 1, &rest));
497:         PetscCall(ISSetIdentity(rest));
498:         PetscCall(PCFieldSplitSetIS(pc, "0", rest));
499:         PetscCall(PCFieldSplitSetIS(pc, "1", coupling));
500:         PetscCall(ISDestroy(&coupling));
501:         PetscCall(ISDestroy(&rest));
502:       } else {
503:         PetscCall(PetscOptionsGetBool(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, "-pc_fieldsplit_default", &fieldsplit_default, NULL));
504:         if (!fieldsplit_default) {
505:           /* Allow user to set fields from command line,  if bs was known at the time of PCSetFromOptions_FieldSplit()
506:            then it is set there. This is not ideal because we should only have options set in XXSetFromOptions(). */
507:           PetscCall(PCFieldSplitSetRuntimeSplits_Private(pc));
508:           if (jac->splitdefined) PetscCall(PetscInfo(pc, "Splits defined using the options database\n"));
509:         }
510:         if ((fieldsplit_default || !jac->splitdefined) && !jac->isrestrict) {
511:           Mat       M = pc->pmat;
512:           PetscBool isnest;
513:           PetscInt  nf;

515:           PetscCall(PetscInfo(pc, "Using default splitting of fields\n"));
516:           PetscCall(PetscObjectTypeCompare((PetscObject)pc->pmat, MATNEST, &isnest));
517:           if (!isnest) {
518:             M = pc->mat;
519:             PetscCall(PetscObjectTypeCompare((PetscObject)pc->mat, MATNEST, &isnest));
520:           }
521:           if (!isnest) nf = jac->bs;
522:           else PetscCall(MatNestGetSize(M, &nf, NULL));
523:           for (i = 0; i < nf; i++) {
524:             char splitname[8];

526:             PetscCall(PetscSNPrintf(splitname, sizeof(splitname), "%" PetscInt_FMT, i));
527:             PetscCall(PCFieldSplitSetFields(pc, splitname, 1, &i, &i));
528:           }
529:           jac->defaultsplit = PETSC_TRUE;
530:         }
531:       }
532:     }
533:   } else if (jac->nsplits == 1) {
534:     IS       is2;
535:     PetscInt nmin, nmax;

537:     PetscCheck(ilink->is, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Must provide at least two sets of fields to PCFieldSplit()");
538:     PetscCall(MatGetOwnershipRange(pc->mat, &nmin, &nmax));
539:     PetscCall(ISComplement(ilink->is, nmin, nmax, &is2));
540:     PetscCall(PCFieldSplitSetIS(pc, "1", is2));
541:     PetscCall(ISDestroy(&is2));
542:   }

544:   PetscCheck(jac->nsplits >= 2, PetscObjectComm((PetscObject)pc), PETSC_ERR_PLIB, "Unhandled case, must have at least two fields, not %" PetscInt_FMT, jac->nsplits);
545:   PetscFunctionReturn(PETSC_SUCCESS);
546: }

548: static PetscErrorCode MatGolubKahanComputeExplicitOperator(Mat A, Mat B, Mat C, Mat *H, PetscReal gkbnu)
549: {
550:   Mat       BT, T;
551:   PetscReal nrmT, nrmB;

553:   PetscFunctionBegin;
554:   PetscCall(MatHermitianTranspose(C, MAT_INITIAL_MATRIX, &T)); /* Test if augmented matrix is symmetric */
555:   PetscCall(MatAXPY(T, -1.0, B, DIFFERENT_NONZERO_PATTERN));
556:   PetscCall(MatNorm(T, NORM_1, &nrmT));
557:   PetscCall(MatNorm(B, NORM_1, &nrmB));
558:   PetscCheck(nrmB <= 0 || nrmT / nrmB < PETSC_SMALL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Matrix is not symmetric/Hermitian, GKB is not applicable.");

560:   /* Compute augmented Lagrangian matrix H = A00 + nu*A01*A01'. This corresponds to */
561:   /* setting N := 1/nu*I in [Ar13].                                                 */
562:   PetscCall(MatHermitianTranspose(B, MAT_INITIAL_MATRIX, &BT));
563:   PetscCall(MatMatMult(B, BT, MAT_INITIAL_MATRIX, PETSC_CURRENT, H)); /* H = A01*A01'          */
564:   PetscCall(MatAYPX(*H, gkbnu, A, DIFFERENT_NONZERO_PATTERN));        /* H = A00 + nu*A01*A01' */

566:   PetscCall(MatDestroy(&BT));
567:   PetscCall(MatDestroy(&T));
568:   PetscFunctionReturn(PETSC_SUCCESS);
569: }

571: PETSC_EXTERN PetscErrorCode PetscOptionsFindPairPrefix_Private(PetscOptions, const char pre[], const char name[], const char *option[], const char *value[], PetscBool *flg);

573: static PetscErrorCode PCSetUp_FieldSplit(PC pc)
574: {
575:   PC_FieldSplit    *jac = (PC_FieldSplit *)pc->data;
576:   PC_FieldSplitLink ilink;
577:   PetscInt          i, nsplit;
578:   PetscBool         matnest = PETSC_FALSE;

580:   PetscFunctionBegin;
581:   pc->failedreason = PC_NOERROR;
582:   PetscCall(PCFieldSplitSetDefaults(pc));
583:   nsplit = jac->nsplits;
584:   ilink  = jac->head;
585:   if (pc->pmat) PetscCall(PetscObjectTypeCompare((PetscObject)pc->pmat, MATNEST, &matnest));

587:   /* get the matrices for each split */
588:   if (!jac->issetup) {
589:     PetscInt rstart, rend, nslots, bs;

591:     jac->issetup = PETSC_TRUE;

593:     /* This is done here instead of in PCFieldSplitSetFields() because may not have matrix at that point */
594:     if (jac->defaultsplit || !ilink->is) {
595:       if (jac->bs <= 0) jac->bs = nsplit;
596:     }

598:     /*  MatCreateSubMatrix() for [S]BAIJ matrices can only work if the indices include entire blocks of the matrix */
599:     PetscCall(MatGetBlockSize(pc->pmat, &bs));
600:     if (bs > 1 && (jac->bs <= bs || jac->bs % bs)) {
601:       PetscBool blk;

603:       PetscCall(PetscObjectTypeCompareAny((PetscObject)pc->pmat, &blk, MATBAIJ, MATSBAIJ, MATSEQBAIJ, MATSEQSBAIJ, MATMPIBAIJ, MATMPISBAIJ, NULL));
604:       PetscCheck(!blk, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "Cannot use MATBAIJ with PCFIELDSPLIT and currently set matrix and PC blocksizes");
605:     }

607:     if (!matnest) { /* use the matrix blocksize and stride IS to determine the index sets that define the submatrices */
608:       bs = jac->bs;
609:       PetscCall(MatGetOwnershipRange(pc->pmat, &rstart, &rend));
610:       nslots = (rend - rstart) / bs;
611:       for (i = 0; i < nsplit; i++) {
612:         if (jac->defaultsplit) {
613:           PetscCall(ISCreateStride(PetscObjectComm((PetscObject)pc), nslots, rstart + i, nsplit, &ilink->is));
614:           PetscCall(PetscObjectReference((PetscObject)ilink->is));
615:           ilink->is_col = ilink->is;
616:         } else if (!ilink->is) {
617:           PetscBool same_fields = PETSC_TRUE;

619:           for (PetscInt k = 0; k < ilink->nfields; k++) {
620:             if (ilink->fields[k] != ilink->fields_col[k]) same_fields = PETSC_FALSE;
621:           }

623:           if (ilink->nfields > 1) {
624:             PetscInt *ii, *jj, j, k, nfields = ilink->nfields, *fields = ilink->fields, *fields_col = ilink->fields_col;

626:             PetscCall(PetscMalloc1(ilink->nfields * nslots, &ii));
627:             if (!same_fields) PetscCall(PetscMalloc1(ilink->nfields * nslots, &jj));
628:             for (j = 0; j < nslots; j++) {
629:               for (k = 0; k < nfields; k++) {
630:                 ii[nfields * j + k] = rstart + bs * j + fields[k];
631:                 if (!same_fields) jj[nfields * j + k] = rstart + bs * j + fields_col[k];
632:               }
633:             }
634:             PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)pc), nslots * nfields, ii, PETSC_OWN_POINTER, &ilink->is));
635:             if (!same_fields) PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)pc), nslots * nfields, jj, PETSC_OWN_POINTER, &ilink->is_col));
636:             else {
637:               PetscCall(PetscObjectReference((PetscObject)ilink->is));
638:               ilink->is_col = ilink->is;
639:             }
640:             PetscCall(ISSetBlockSize(ilink->is, nfields));
641:             PetscCall(ISSetBlockSize(ilink->is_col, nfields));
642:           } else {
643:             PetscCall(ISCreateStride(PetscObjectComm((PetscObject)pc), nslots, rstart + ilink->fields[0], bs, &ilink->is));
644:             if (!same_fields) PetscCall(ISCreateStride(PetscObjectComm((PetscObject)pc), nslots, rstart + ilink->fields_col[0], bs, &ilink->is_col));
645:             else {
646:               PetscCall(PetscObjectReference((PetscObject)ilink->is));
647:               ilink->is_col = ilink->is;
648:             }
649:           }
650:         }
651:         ilink = ilink->next;
652:       }
653:     } else { /* use the IS that define the MATNEST to determine the index sets that define the submatrices */
654:       IS      *rowis, *colis, *ises = NULL;
655:       PetscInt mis, nis;

657:       PetscCall(MatNestGetSize(pc->pmat, &mis, &nis));
658:       PetscCall(PetscMalloc2(mis, &rowis, nis, &colis));
659:       PetscCall(MatNestGetISs(pc->pmat, rowis, colis));
660:       if (!jac->defaultsplit) PetscCall(PetscMalloc1(mis, &ises));

662:       for (i = 0; i < nsplit; i++) {
663:         if (jac->defaultsplit) {
664:           PetscCall(ISDuplicate(rowis[i], &ilink->is));
665:           PetscCall(PetscObjectReference((PetscObject)ilink->is));
666:           ilink->is_col = ilink->is;
667:         } else if (!ilink->is) {
668:           if (ilink->nfields > 1) {
669:             for (PetscInt j = 0; j < ilink->nfields; j++) ises[j] = rowis[ilink->fields[j]];
670:             PetscCall(ISConcatenate(PetscObjectComm((PetscObject)pc), ilink->nfields, ises, &ilink->is));
671:           } else {
672:             PetscCall(ISDuplicate(rowis[ilink->fields[0]], &ilink->is));
673:           }
674:           PetscCall(PetscObjectReference((PetscObject)ilink->is));
675:           ilink->is_col = ilink->is;
676:         }
677:         ilink = ilink->next;
678:       }
679:       PetscCall(PetscFree2(rowis, colis));
680:       PetscCall(PetscFree(ises));
681:     }
682:   }

684:   ilink = jac->head;
685:   if (!jac->pmat) {
686:     Vec xtmp;

688:     PetscCall(MatCreateVecs(pc->pmat, &xtmp, NULL));
689:     PetscCall(PetscMalloc1(nsplit, &jac->pmat));
690:     PetscCall(PetscMalloc2(nsplit, &jac->x, nsplit, &jac->y));
691:     for (i = 0; i < nsplit; i++) {
692:       MatNullSpace sp;

694:       /* Check for matrix attached to IS */
695:       PetscCall(PetscObjectQuery((PetscObject)ilink->is, "pmat", (PetscObject *)&jac->pmat[i]));
696:       if (jac->pmat[i]) {
697:         PetscCall(PetscObjectReference((PetscObject)jac->pmat[i]));
698:         if (jac->type == PC_COMPOSITE_SCHUR) {
699:           jac->schur_user = jac->pmat[i];

701:           PetscCall(PetscObjectReference((PetscObject)jac->schur_user));
702:         }
703:       } else {
704:         const char *prefix;
705:         PetscCall(MatCreateSubMatrix(pc->pmat, ilink->is, ilink->is_col, MAT_INITIAL_MATRIX, &jac->pmat[i]));
706:         PetscCall(MatGetOptionsPrefix(jac->pmat[i], &prefix));
707:         if (!prefix) {
708:           PetscCall(KSPGetOptionsPrefix(ilink->ksp, &prefix));
709:           PetscCall(MatSetOptionsPrefix(jac->pmat[i], prefix));
710:         }
711:         PetscCall(MatSetFromOptions(jac->pmat[i]));
712:         PetscCall(MatViewFromOptions(jac->pmat[i], NULL, "-mat_view"));
713:       }
714:       /* create work vectors for each split */
715:       PetscCall(MatCreateVecs(jac->pmat[i], &jac->x[i], &jac->y[i]));
716:       ilink->x = jac->x[i];
717:       ilink->y = jac->y[i];
718:       ilink->z = NULL;
719:       /* compute scatter contexts needed by multiplicative versions and non-default splits */
720:       PetscCall(VecScatterCreate(xtmp, ilink->is, jac->x[i], NULL, &ilink->sctx));
721:       PetscCall(PetscObjectQuery((PetscObject)ilink->is, "nearnullspace", (PetscObject *)&sp));
722:       if (sp) PetscCall(MatSetNearNullSpace(jac->pmat[i], sp));
723:       ilink = ilink->next;
724:     }
725:     PetscCall(VecDestroy(&xtmp));
726:   } else {
727:     MatReuse      scall;
728:     MatNullSpace *nullsp = NULL;

730:     if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
731:       PetscCall(MatGetNullSpaces(nsplit, jac->pmat, &nullsp));
732:       for (i = 0; i < nsplit; i++) PetscCall(MatDestroy(&jac->pmat[i]));
733:       scall = MAT_INITIAL_MATRIX;
734:     } else scall = MAT_REUSE_MATRIX;

736:     for (i = 0; i < nsplit; i++) {
737:       Mat pmat;

739:       /* Check for matrix attached to IS */
740:       PetscCall(PetscObjectQuery((PetscObject)ilink->is, "pmat", (PetscObject *)&pmat));
741:       if (!pmat) PetscCall(MatCreateSubMatrix(pc->pmat, ilink->is, ilink->is_col, scall, &jac->pmat[i]));
742:       ilink = ilink->next;
743:     }
744:     if (nullsp) PetscCall(MatRestoreNullSpaces(nsplit, jac->pmat, &nullsp));
745:   }
746:   if (jac->diag_use_amat) {
747:     ilink = jac->head;
748:     if (!jac->mat) {
749:       PetscCall(PetscMalloc1(nsplit, &jac->mat));
750:       for (i = 0; i < nsplit; i++) {
751:         PetscCall(MatCreateSubMatrix(pc->mat, ilink->is, ilink->is_col, MAT_INITIAL_MATRIX, &jac->mat[i]));
752:         ilink = ilink->next;
753:       }
754:     } else {
755:       MatReuse      scall;
756:       MatNullSpace *nullsp = NULL;

758:       if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
759:         PetscCall(MatGetNullSpaces(nsplit, jac->mat, &nullsp));
760:         for (i = 0; i < nsplit; i++) PetscCall(MatDestroy(&jac->mat[i]));
761:         scall = MAT_INITIAL_MATRIX;
762:       } else scall = MAT_REUSE_MATRIX;

764:       for (i = 0; i < nsplit; i++) {
765:         PetscCall(MatCreateSubMatrix(pc->mat, ilink->is, ilink->is_col, scall, &jac->mat[i]));
766:         ilink = ilink->next;
767:       }
768:       if (nullsp) PetscCall(MatRestoreNullSpaces(nsplit, jac->mat, &nullsp));
769:     }
770:   } else {
771:     jac->mat = jac->pmat;
772:   }

774:   /* Check for null space attached to IS */
775:   ilink = jac->head;
776:   for (i = 0; i < nsplit; i++) {
777:     MatNullSpace sp;

779:     PetscCall(PetscObjectQuery((PetscObject)ilink->is, "nullspace", (PetscObject *)&sp));
780:     if (sp) PetscCall(MatSetNullSpace(jac->mat[i], sp));
781:     ilink = ilink->next;
782:   }

784:   if (jac->type != PC_COMPOSITE_ADDITIVE && jac->type != PC_COMPOSITE_SCHUR && jac->type != PC_COMPOSITE_GKB) {
785:     /* extract the rows of the matrix associated with each field: used for efficient computation of residual inside algorithm */
786:     /* FIXME: Can/should we reuse jac->mat whenever (jac->diag_use_amat) is true? */
787:     ilink = jac->head;
788:     if (nsplit == 2 && jac->type == PC_COMPOSITE_MULTIPLICATIVE) {
789:       /* special case need where Afield[0] is not needed and only certain columns of Afield[1] are needed since update is only on those rows of the solution */
790:       if (!jac->Afield) {
791:         PetscCall(PetscCalloc1(nsplit, &jac->Afield));
792:         if (jac->offdiag_use_amat) {
793:           PetscCall(MatCreateSubMatrix(pc->mat, ilink->next->is, ilink->is, MAT_INITIAL_MATRIX, &jac->Afield[1]));
794:         } else {
795:           PetscCall(MatCreateSubMatrix(pc->pmat, ilink->next->is, ilink->is, MAT_INITIAL_MATRIX, &jac->Afield[1]));
796:         }
797:       } else {
798:         MatReuse scall;

800:         if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
801:           PetscCall(MatDestroy(&jac->Afield[1]));
802:           scall = MAT_INITIAL_MATRIX;
803:         } else scall = MAT_REUSE_MATRIX;

805:         if (jac->offdiag_use_amat) {
806:           PetscCall(MatCreateSubMatrix(pc->mat, ilink->next->is, ilink->is, scall, &jac->Afield[1]));
807:         } else {
808:           PetscCall(MatCreateSubMatrix(pc->pmat, ilink->next->is, ilink->is, scall, &jac->Afield[1]));
809:         }
810:       }
811:     } else {
812:       if (!jac->Afield) {
813:         PetscCall(PetscMalloc1(nsplit, &jac->Afield));
814:         for (i = 0; i < nsplit; i++) {
815:           if (jac->offdiag_use_amat) {
816:             PetscCall(MatCreateSubMatrix(pc->mat, ilink->is, NULL, MAT_INITIAL_MATRIX, &jac->Afield[i]));
817:           } else {
818:             PetscCall(MatCreateSubMatrix(pc->pmat, ilink->is, NULL, MAT_INITIAL_MATRIX, &jac->Afield[i]));
819:           }
820:           ilink = ilink->next;
821:         }
822:       } else {
823:         MatReuse scall;
824:         if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
825:           for (i = 0; i < nsplit; i++) PetscCall(MatDestroy(&jac->Afield[i]));
826:           scall = MAT_INITIAL_MATRIX;
827:         } else scall = MAT_REUSE_MATRIX;

829:         for (i = 0; i < nsplit; i++) {
830:           if (jac->offdiag_use_amat) {
831:             PetscCall(MatCreateSubMatrix(pc->mat, ilink->is, NULL, scall, &jac->Afield[i]));
832:           } else {
833:             PetscCall(MatCreateSubMatrix(pc->pmat, ilink->is, NULL, scall, &jac->Afield[i]));
834:           }
835:           ilink = ilink->next;
836:         }
837:       }
838:     }
839:   }

841:   if (jac->type == PC_COMPOSITE_SCHUR) {
842:     PetscBool   isset, isspd = PETSC_FALSE, issym = PETSC_FALSE, flg;
843:     char        lscname[256];
844:     PetscObject LSC_L;

846:     PetscCheck(nsplit == 2, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "To use Schur complement preconditioner you must have exactly 2 fields");

848:     /* If pc->mat is SPD, don't scale by -1 the Schur complement */
849:     PetscCall(MatIsSPDKnown(pc->pmat, &isset, &isspd));
850:     if (jac->schurscale == (PetscScalar)-1.0) jac->schurscale = (isset && isspd) ? 1.0 : -1.0;
851:     PetscCall(MatIsSymmetricKnown(pc->pmat, &isset, &issym));

853:     PetscCall(PetscObjectTypeCompareAny(jac->offdiag_use_amat ? (PetscObject)pc->mat : (PetscObject)pc->pmat, &flg, MATSEQSBAIJ, MATMPISBAIJ, ""));

855:     if (jac->schur) {
856:       KSP      kspA = jac->head->ksp, kspInner = NULL, kspUpper = jac->kspupper;
857:       MatReuse scall;

859:       if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
860:         scall = MAT_INITIAL_MATRIX;
861:         PetscCall(MatDestroy(&jac->B));
862:         PetscCall(MatDestroy(&jac->C));
863:       } else scall = MAT_REUSE_MATRIX;

865:       PetscCall(MatSchurComplementGetKSP(jac->schur, &kspInner));
866:       ilink = jac->head;
867:       PetscCall(MatCreateSubMatrix(jac->offdiag_use_amat ? pc->mat : pc->pmat, ilink->is, ilink->next->is, scall, &jac->B));
868:       if (!flg) PetscCall(MatCreateSubMatrix(jac->offdiag_use_amat ? pc->mat : pc->pmat, ilink->next->is, ilink->is, scall, &jac->C));
869:       else {
870:         PetscCall(MatIsHermitianKnown(jac->offdiag_use_amat ? pc->mat : pc->pmat, &isset, &flg));
871:         if (isset && flg) PetscCall(MatCreateHermitianTranspose(jac->B, &jac->C));
872:         else PetscCall(MatCreateTranspose(jac->B, &jac->C));
873:       }
874:       ilink = ilink->next;
875:       PetscCall(MatSchurComplementUpdateSubMatrices(jac->schur, jac->mat[0], jac->pmat[0], jac->B, jac->C, jac->mat[1]));
876:       if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELFP) {
877:         PetscCall(MatDestroy(&jac->schurp));
878:         PetscCall(MatSchurComplementGetPmat(jac->schur, MAT_INITIAL_MATRIX, &jac->schurp));
879:       } else if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_FULL && jac->kspupper != jac->head->ksp) {
880:         PetscCall(MatDestroy(&jac->schur_user));
881:         PetscCall(MatSchurComplementComputeExplicitOperator(jac->schur, &jac->schur_user));
882:       }
883:       if (kspA != kspInner) PetscCall(KSPSetOperators(kspA, jac->mat[0], jac->pmat[0]));
884:       if (kspUpper != kspA) PetscCall(KSPSetOperators(kspUpper, jac->mat[0], jac->pmat[0]));
885:       PetscCall(KSPSetOperators(jac->kspschur, jac->schur, FieldSplitSchurPre(jac)));
886:     } else {
887:       const char  *Dprefix;
888:       char         schurprefix[256], schurmatprefix[256];
889:       char         schurtestoption[256];
890:       MatNullSpace sp;
891:       KSP          kspt;

893:       /* extract the A01 and A10 matrices */
894:       ilink = jac->head;
895:       PetscCall(MatCreateSubMatrix(jac->offdiag_use_amat ? pc->mat : pc->pmat, ilink->is, ilink->next->is, MAT_INITIAL_MATRIX, &jac->B));
896:       if (!flg) PetscCall(MatCreateSubMatrix(jac->offdiag_use_amat ? pc->mat : pc->pmat, ilink->next->is, ilink->is, MAT_INITIAL_MATRIX, &jac->C));
897:       else {
898:         PetscCall(MatIsHermitianKnown(jac->offdiag_use_amat ? pc->mat : pc->pmat, &isset, &flg));
899:         if (isset && flg) PetscCall(MatCreateHermitianTranspose(jac->B, &jac->C));
900:         else PetscCall(MatCreateTranspose(jac->B, &jac->C));
901:       }
902:       ilink = ilink->next;
903:       /* Use mat[0] (diagonal block of Amat) preconditioned by pmat[0] to define Schur complement */
904:       PetscCall(MatCreate(((PetscObject)jac->mat[0])->comm, &jac->schur));
905:       PetscCall(MatSetType(jac->schur, MATSCHURCOMPLEMENT));
906:       PetscCall(MatSchurComplementSetSubMatrices(jac->schur, jac->mat[0], jac->pmat[0], jac->B, jac->C, jac->mat[1]));
907:       PetscCall(PetscSNPrintf(schurmatprefix, sizeof(schurmatprefix), "%sfieldsplit_%s_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname));
908:       PetscCall(MatSetOptionsPrefix(jac->schur, schurmatprefix));
909:       PetscCall(MatSchurComplementGetKSP(jac->schur, &kspt));
910:       PetscCall(KSPSetOptionsPrefix(kspt, schurmatprefix));

912:       /* Note: this is not true in general */
913:       PetscCall(MatGetNullSpace(jac->mat[1], &sp));
914:       if (sp) PetscCall(MatSetNullSpace(jac->schur, sp));

916:       PetscCall(PetscSNPrintf(schurtestoption, sizeof(schurtestoption), "-fieldsplit_%s_inner_", ilink->splitname));
917:       PetscCall(PetscOptionsFindPairPrefix_Private(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, schurtestoption, NULL, NULL, &flg));
918:       if (flg) {
919:         DM  dmInner;
920:         KSP kspInner;
921:         PC  pcInner;

923:         PetscCall(MatSchurComplementGetKSP(jac->schur, &kspInner));
924:         PetscCall(KSPReset(kspInner));
925:         PetscCall(KSPSetOperators(kspInner, jac->mat[0], jac->pmat[0]));
926:         PetscCall(PetscSNPrintf(schurprefix, sizeof(schurprefix), "%sfieldsplit_%s_inner_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname));
927:         /* Indent this deeper to emphasize the "inner" nature of this solver. */
928:         PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspInner, (PetscObject)pc, 2));
929:         PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspInner->pc, (PetscObject)pc, 2));
930:         PetscCall(KSPSetOptionsPrefix(kspInner, schurprefix));

932:         /* Set DM for new solver */
933:         PetscCall(KSPGetDM(jac->head->ksp, &dmInner));
934:         PetscCall(KSPSetDM(kspInner, dmInner));
935:         PetscCall(KSPSetDMActive(kspInner, KSP_DMACTIVE_ALL, PETSC_FALSE));

937:         /* Defaults to PCKSP as preconditioner */
938:         PetscCall(KSPGetPC(kspInner, &pcInner));
939:         PetscCall(PCSetType(pcInner, PCKSP));
940:         PetscCall(PCKSPSetKSP(pcInner, jac->head->ksp));
941:       } else {
942:         /* Use the outer solver for the inner solve, but revert the KSPPREONLY from PCFieldSplitSetFields_FieldSplit or
943:           * PCFieldSplitSetIS_FieldSplit. We don't want KSPPREONLY because it makes the Schur complement inexact,
944:           * preventing Schur complement reduction to be an accurate solve. Usually when an iterative solver is used for
945:           * S = D - C A_inner^{-1} B, we expect S to be defined using an accurate definition of A_inner^{-1}, so we make
946:           * GMRES the default. Note that it is also common to use PREONLY for S, in which case S may not be used
947:           * directly, and the user is responsible for setting an inexact method for fieldsplit's A^{-1}. */
948:         PetscCall(KSPSetType(jac->head->ksp, KSPGMRES));
949:         PetscCall(MatSchurComplementSetKSP(jac->schur, jac->head->ksp));
950:       }
951:       PetscCall(KSPSetOperators(jac->head->ksp, jac->mat[0], jac->pmat[0]));
952:       PetscCall(KSPSetFromOptions(jac->head->ksp));
953:       PetscCall(MatSetFromOptions(jac->schur));

955:       PetscCall(PetscObjectTypeCompare((PetscObject)jac->schur, MATSCHURCOMPLEMENT, &flg));
956:       if (flg) { /* Need to do this otherwise PCSetUp_KSP will overwrite the amat of jac->head->ksp */
957:         KSP kspInner;
958:         PC  pcInner;

960:         PetscCall(MatSchurComplementGetKSP(jac->schur, &kspInner));
961:         PetscCall(KSPGetPC(kspInner, &pcInner));
962:         PetscCall(PetscObjectTypeCompare((PetscObject)pcInner, PCKSP, &flg));
963:         if (flg) {
964:           KSP ksp;

966:           PetscCall(PCKSPGetKSP(pcInner, &ksp));
967:           if (ksp == jac->head->ksp) PetscCall(PCSetUseAmat(pcInner, PETSC_TRUE));
968:         }
969:       }
970:       PetscCall(PetscSNPrintf(schurtestoption, sizeof(schurtestoption), "-fieldsplit_%s_upper_", ilink->splitname));
971:       PetscCall(PetscOptionsFindPairPrefix_Private(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, schurtestoption, NULL, NULL, &flg));
972:       if (flg) {
973:         DM dmInner;

975:         PetscCall(PetscSNPrintf(schurprefix, sizeof(schurprefix), "%sfieldsplit_%s_upper_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname));
976:         PetscCall(KSPCreate(PetscObjectComm((PetscObject)pc), &jac->kspupper));
977:         PetscCall(KSPSetNestLevel(jac->kspupper, pc->kspnestlevel));
978:         PetscCall(KSPSetErrorIfNotConverged(jac->kspupper, pc->erroriffailure));
979:         PetscCall(KSPSetOptionsPrefix(jac->kspupper, schurprefix));
980:         PetscCall(PetscObjectIncrementTabLevel((PetscObject)jac->kspupper, (PetscObject)pc, 1));
981:         PetscCall(PetscObjectIncrementTabLevel((PetscObject)jac->kspupper->pc, (PetscObject)pc, 1));
982:         PetscCall(KSPGetDM(jac->head->ksp, &dmInner));
983:         PetscCall(KSPSetDM(jac->kspupper, dmInner));
984:         PetscCall(KSPSetDMActive(jac->kspupper, KSP_DMACTIVE_ALL, PETSC_FALSE));
985:         PetscCall(KSPSetFromOptions(jac->kspupper));
986:         PetscCall(KSPSetOperators(jac->kspupper, jac->mat[0], jac->pmat[0]));
987:         PetscCall(VecDuplicate(jac->head->x, &jac->head->z));
988:       } else {
989:         jac->kspupper = jac->head->ksp;
990:         PetscCall(PetscObjectReference((PetscObject)jac->head->ksp));
991:       }

993:       if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELFP) PetscCall(MatSchurComplementGetPmat(jac->schur, MAT_INITIAL_MATRIX, &jac->schurp));
994:       PetscCall(KSPCreate(PetscObjectComm((PetscObject)pc), &jac->kspschur));
995:       PetscCall(KSPSetNestLevel(jac->kspschur, pc->kspnestlevel));
996:       PetscCall(KSPSetErrorIfNotConverged(jac->kspschur, pc->erroriffailure));
997:       PetscCall(PetscObjectIncrementTabLevel((PetscObject)jac->kspschur, (PetscObject)pc, 1));
998:       if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELF) {
999:         PC pcschur;
1000:         PetscCall(KSPGetPC(jac->kspschur, &pcschur));
1001:         PetscCall(PCSetType(pcschur, PCNONE));
1002:         /* Note: This is bad if there exist preconditioners for MATSCHURCOMPLEMENT */
1003:       } else if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_FULL) {
1004:         if (jac->schurfactorization != PC_FIELDSPLIT_SCHUR_FACT_FULL || jac->kspupper != jac->head->ksp) PetscCall(MatSchurComplementComputeExplicitOperator(jac->schur, &jac->schur_user));
1005:       }
1006:       PetscCall(KSPSetOperators(jac->kspschur, jac->schur, FieldSplitSchurPre(jac)));
1007:       PetscCall(KSPGetOptionsPrefix(jac->head->next->ksp, &Dprefix));
1008:       PetscCall(KSPSetOptionsPrefix(jac->kspschur, Dprefix));
1009:       /* propagate DM */
1010:       {
1011:         DM sdm;
1012:         PetscCall(KSPGetDM(jac->head->next->ksp, &sdm));
1013:         if (sdm) {
1014:           PetscCall(KSPSetDM(jac->kspschur, sdm));
1015:           PetscCall(KSPSetDMActive(jac->kspschur, KSP_DMACTIVE_ALL, PETSC_FALSE));
1016:         }
1017:       }
1018:       /* really want setfromoptions called in PCSetFromOptions_FieldSplit(), but it is not ready yet */
1019:       /* need to call this every time, since the jac->kspschur is freshly created, otherwise its options never get set */
1020:       PetscCall(KSPSetFromOptions(jac->kspschur));
1021:     }
1022:     PetscCall(MatAssemblyBegin(jac->schur, MAT_FINAL_ASSEMBLY));
1023:     PetscCall(MatAssemblyEnd(jac->schur, MAT_FINAL_ASSEMBLY));
1024:     if (issym) PetscCall(MatSetOption(jac->schur, MAT_SYMMETRIC, PETSC_TRUE));
1025:     if (isspd) PetscCall(MatSetOption(jac->schur, MAT_SPD, PETSC_TRUE));

1027:     /* HACK: special support to forward L and Lp matrices that might be used by PCLSC */
1028:     PetscCall(PetscSNPrintf(lscname, sizeof(lscname), "%s_LSC_L", ilink->splitname));
1029:     PetscCall(PetscObjectQuery((PetscObject)pc->mat, lscname, &LSC_L));
1030:     if (!LSC_L) PetscCall(PetscObjectQuery((PetscObject)pc->pmat, lscname, &LSC_L));
1031:     if (LSC_L) PetscCall(PetscObjectCompose((PetscObject)jac->schur, "LSC_L", LSC_L));
1032:     PetscCall(PetscSNPrintf(lscname, sizeof(lscname), "%s_LSC_Lp", ilink->splitname));
1033:     PetscCall(PetscObjectQuery((PetscObject)pc->pmat, lscname, &LSC_L));
1034:     if (!LSC_L) PetscCall(PetscObjectQuery((PetscObject)pc->mat, lscname, &LSC_L));
1035:     if (LSC_L) PetscCall(PetscObjectCompose((PetscObject)jac->schur, "LSC_Lp", LSC_L));
1036:   } else if (jac->type == PC_COMPOSITE_GKB) {
1037:     PetscCheck(nsplit == 2, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "To use GKB preconditioner you must have exactly 2 fields");
1038:     ilink = jac->head;
1039:     PetscCall(MatCreateSubMatrix(jac->offdiag_use_amat ? pc->mat : pc->pmat, ilink->is, ilink->next->is, MAT_INITIAL_MATRIX, &jac->B));
1040:     /* Create work vectors for GKB algorithm */
1041:     PetscCall(VecDuplicate(ilink->x, &jac->u));
1042:     PetscCall(VecDuplicate(ilink->x, &jac->Hu));
1043:     PetscCall(VecDuplicate(ilink->x, &jac->w2));
1044:     PetscCall(MatCreateSubMatrix(jac->offdiag_use_amat ? pc->mat : pc->pmat, ilink->next->is, ilink->is, MAT_INITIAL_MATRIX, &jac->C));
1045:     ilink = ilink->next;
1046:     /* Create work vectors for GKB algorithm */
1047:     PetscCall(VecDuplicate(ilink->x, &jac->v));
1048:     PetscCall(VecDuplicate(ilink->x, &jac->d));
1049:     PetscCall(VecDuplicate(ilink->x, &jac->w1));
1050:     PetscCall(MatGolubKahanComputeExplicitOperator(jac->mat[0], jac->B, jac->C, &jac->H, jac->gkbnu));
1051:     PetscCall(PetscCalloc1(jac->gkbdelay, &jac->vecz));

1053:     ilink = jac->head;
1054:     PetscCall(KSPSetOperators(ilink->ksp, jac->H, jac->H));
1055:     if (!jac->suboptionsset) PetscCall(KSPSetFromOptions(ilink->ksp));
1056:     /* Create gkb_monitor context */
1057:     if (jac->gkbmonitor) {
1058:       PetscInt tablevel;
1059:       PetscCall(PetscViewerCreate(PETSC_COMM_WORLD, &jac->gkbviewer));
1060:       PetscCall(PetscViewerSetType(jac->gkbviewer, PETSCVIEWERASCII));
1061:       PetscCall(PetscObjectGetTabLevel((PetscObject)ilink->ksp, &tablevel));
1062:       PetscCall(PetscViewerASCIISetTab(jac->gkbviewer, tablevel));
1063:       PetscCall(PetscObjectIncrementTabLevel((PetscObject)ilink->ksp, (PetscObject)ilink->ksp, 1));
1064:     }
1065:   } else {
1066:     /* set up the individual splits' PCs */
1067:     i     = 0;
1068:     ilink = jac->head;
1069:     while (ilink) {
1070:       PetscCall(KSPSetOperators(ilink->ksp, jac->mat[i], jac->pmat[i]));
1071:       /* really want setfromoptions called in PCSetFromOptions_FieldSplit(), but it is not ready yet */
1072:       if (!jac->suboptionsset) PetscCall(KSPSetFromOptions(ilink->ksp));
1073:       i++;
1074:       ilink = ilink->next;
1075:     }
1076:   }

1078:   /* Set coordinates to the sub PC objects whenever these are set */
1079:   if (jac->coordinates_set) {
1080:     PC pc_coords;
1081:     if (jac->type == PC_COMPOSITE_SCHUR) {
1082:       // Head is first block.
1083:       PetscCall(KSPGetPC(jac->head->ksp, &pc_coords));
1084:       PetscCall(PCSetCoordinates(pc_coords, jac->head->dim, jac->head->ndofs, jac->head->coords));
1085:       // Second one is Schur block, but its KSP object is in kspschur.
1086:       PetscCall(KSPGetPC(jac->kspschur, &pc_coords));
1087:       PetscCall(PCSetCoordinates(pc_coords, jac->head->next->dim, jac->head->next->ndofs, jac->head->next->coords));
1088:     } else if (jac->type == PC_COMPOSITE_GKB) {
1089:       PetscCall(PetscInfo(pc, "Warning: Setting coordinates does nothing for the GKB Fieldpslit preconditioner\n"));
1090:     } else {
1091:       ilink = jac->head;
1092:       while (ilink) {
1093:         PetscCall(KSPGetPC(ilink->ksp, &pc_coords));
1094:         PetscCall(PCSetCoordinates(pc_coords, ilink->dim, ilink->ndofs, ilink->coords));
1095:         ilink = ilink->next;
1096:       }
1097:     }
1098:   }

1100:   jac->suboptionsset = PETSC_TRUE;
1101:   PetscFunctionReturn(PETSC_SUCCESS);
1102: }

1104: static PetscErrorCode PCSetUpOnBlocks_FieldSplit_Schur(PC pc)
1105: {
1106:   PC_FieldSplit    *jac    = (PC_FieldSplit *)pc->data;
1107:   PC_FieldSplitLink ilinkA = jac->head;
1108:   KSP               kspA = ilinkA->ksp, kspUpper = jac->kspupper;

1110:   PetscFunctionBegin;
1111:   if (jac->schurfactorization == PC_FIELDSPLIT_SCHUR_FACT_FULL && kspUpper != kspA) {
1112:     PetscCall(KSPSetUp(kspUpper));
1113:     PetscCall(KSPSetUpOnBlocks(kspUpper));
1114:   }
1115:   PetscCall(KSPSetUp(kspA));
1116:   PetscCall(KSPSetUpOnBlocks(kspA));
1117:   if (jac->schurpre != PC_FIELDSPLIT_SCHUR_PRE_FULL) {
1118:     PetscCall(KSPSetUp(jac->kspschur));
1119:     PetscCall(KSPSetUpOnBlocks(jac->kspschur));
1120:   } else if (kspUpper == kspA && jac->schurfactorization == PC_FIELDSPLIT_SCHUR_FACT_FULL) {
1121:     Mat          A;
1122:     PetscInt     m, M, N;
1123:     VecType      vtype;
1124:     PetscMemType mtype;
1125:     PetscScalar *array;

1127:     PetscCall(MatGetSize(jac->B, &M, &N));
1128:     PetscCall(MatGetLocalSize(jac->B, &m, NULL));
1129:     PetscCall(MatGetVecType(jac->B, &vtype));
1130:     PetscCall(VecGetArrayAndMemType(ilinkA->x, &array, &mtype));
1131:     PetscCall(VecRestoreArrayAndMemType(ilinkA->x, &array));
1132:     PetscCall(PetscObjectQuery((PetscObject)jac->schur, "AinvB", (PetscObject *)&A));
1133:     if (A) {
1134:       PetscInt P;

1136:       PetscCall(MatGetSize(A, NULL, &P));
1137:       if (P < N + 1) { // need to recreate AinvB, otherwise, the Schur complement won't be updated
1138:         PetscCall(PetscObjectCompose((PetscObject)jac->schur, "AinvB", NULL));
1139:         A = NULL;
1140:       }
1141:     }
1142:     if (!A) {
1143:       if (PetscMemTypeHost(mtype) || (!PetscDefined(HAVE_CUDA) && !PetscDefined(HAVE_HIP))) PetscCall(PetscMalloc1(m * (N + 1), &array));
1144: #if PetscDefined(HAVE_CUDA)
1145:       else if (PetscMemTypeCUDA(mtype)) PetscCallCUDA(cudaMalloc((void **)&array, sizeof(PetscScalar) * m * (N + 1)));
1146: #endif
1147: #if PetscDefined(HAVE_HIP)
1148:       else if (PetscMemTypeHIP(mtype)) PetscCallHIP(hipMalloc((void **)&array, sizeof(PetscScalar) * m * (N + 1)));
1149: #endif
1150:       PetscCall(MatCreateDenseFromVecType(PetscObjectComm((PetscObject)jac->schur), vtype, m, PETSC_DECIDE, M, N + 1, PETSC_DECIDE, array, &A)); // number of columns of the Schur complement plus one
1151:       PetscCall(PetscObjectCompose((PetscObject)jac->schur, "AinvB", (PetscObject)A));
1152:       PetscCall(MatDestroy(&A));
1153:     }
1154:   }
1155:   PetscFunctionReturn(PETSC_SUCCESS);
1156: }

1158: static PetscErrorCode PCSetUpOnBlocks_FieldSplit(PC pc)
1159: {
1160:   PC_FieldSplit    *jac   = (PC_FieldSplit *)pc->data;
1161:   PC_FieldSplitLink ilink = jac->head;

1163:   PetscFunctionBegin;
1164:   while (ilink) {
1165:     PetscCall(KSPSetUp(ilink->ksp));
1166:     PetscCall(KSPSetUpOnBlocks(ilink->ksp));
1167:     ilink = ilink->next;
1168:   }
1169:   PetscFunctionReturn(PETSC_SUCCESS);
1170: }

1172: static PetscErrorCode PCSetUpOnBlocks_FieldSplit_GKB(PC pc)
1173: {
1174:   PC_FieldSplit    *jac    = (PC_FieldSplit *)pc->data;
1175:   PC_FieldSplitLink ilinkA = jac->head;
1176:   KSP               ksp    = ilinkA->ksp;

1178:   PetscFunctionBegin;
1179:   PetscCall(KSPSetUp(ksp));
1180:   PetscCall(KSPSetUpOnBlocks(ksp));
1181:   PetscFunctionReturn(PETSC_SUCCESS);
1182: }

1184: static PetscErrorCode PCApply_FieldSplit_Schur(PC pc, Vec x, Vec y)
1185: {
1186:   PC_FieldSplit    *jac    = (PC_FieldSplit *)pc->data;
1187:   PC_FieldSplitLink ilinkA = jac->head, ilinkD = ilinkA->next;
1188:   KSP               kspA = ilinkA->ksp, kspLower = kspA, kspUpper = jac->kspupper;
1189:   Mat               AinvB = NULL;
1190:   PetscInt          N, P;

1192:   PetscFunctionBegin;
1193:   switch (jac->schurfactorization) {
1194:   case PC_FIELDSPLIT_SCHUR_FACT_DIAG:
1195:     /* [A00 0; 0 -S], positive definite, suitable for MINRES */
1196:     PetscCall(VecScatterBegin(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1197:     PetscCall(VecScatterBegin(ilinkD->sctx, x, ilinkD->x, INSERT_VALUES, SCATTER_FORWARD));
1198:     PetscCall(VecScatterEnd(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1199:     PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1200:     PetscCall(KSPSolve(kspA, ilinkA->x, ilinkA->y));
1201:     PetscCall(KSPCheckSolve(kspA, pc, ilinkA->y));
1202:     PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1203:     PetscCall(VecScatterBegin(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1204:     PetscCall(VecScatterEnd(ilinkD->sctx, x, ilinkD->x, INSERT_VALUES, SCATTER_FORWARD));
1205:     PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1206:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1207:     PetscCall(KSPSolve(jac->kspschur, ilinkD->x, ilinkD->y));
1208:     PetscCall(KSPCheckSolve(jac->kspschur, pc, ilinkD->y));
1209:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1210:     PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1211:     PetscCall(VecScale(ilinkD->y, jac->schurscale));
1212:     PetscCall(VecScatterEnd(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1213:     PetscCall(VecScatterBegin(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1214:     PetscCall(VecScatterEnd(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1215:     break;
1216:   case PC_FIELDSPLIT_SCHUR_FACT_LOWER:
1217:     /* [A00 0; A10 S], suitable for left preconditioning */
1218:     PetscCall(VecScatterBegin(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1219:     PetscCall(VecScatterEnd(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1220:     PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1221:     PetscCall(KSPSolve(kspA, ilinkA->x, ilinkA->y));
1222:     PetscCall(KSPCheckSolve(kspA, pc, ilinkA->y));
1223:     PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1224:     PetscCall(MatMult(jac->C, ilinkA->y, ilinkD->x));
1225:     PetscCall(VecScale(ilinkD->x, -1.));
1226:     PetscCall(VecScatterBegin(ilinkD->sctx, x, ilinkD->x, ADD_VALUES, SCATTER_FORWARD));
1227:     PetscCall(VecScatterBegin(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1228:     PetscCall(VecScatterEnd(ilinkD->sctx, x, ilinkD->x, ADD_VALUES, SCATTER_FORWARD));
1229:     PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1230:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1231:     PetscCall(KSPSolve(jac->kspschur, ilinkD->x, ilinkD->y));
1232:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1233:     PetscCall(KSPCheckSolve(jac->kspschur, pc, ilinkD->y));
1234:     PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1235:     PetscCall(VecScatterEnd(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1236:     PetscCall(VecScatterBegin(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1237:     PetscCall(VecScatterEnd(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1238:     break;
1239:   case PC_FIELDSPLIT_SCHUR_FACT_UPPER:
1240:     /* [A00 A01; 0 S], suitable for right preconditioning */
1241:     PetscCall(VecScatterBegin(ilinkD->sctx, x, ilinkD->x, INSERT_VALUES, SCATTER_FORWARD));
1242:     PetscCall(VecScatterEnd(ilinkD->sctx, x, ilinkD->x, INSERT_VALUES, SCATTER_FORWARD));
1243:     PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1244:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1245:     PetscCall(KSPSolve(jac->kspschur, ilinkD->x, ilinkD->y));
1246:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1247:     PetscCall(KSPCheckSolve(jac->kspschur, pc, ilinkD->y));
1248:     PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1249:     PetscCall(MatMult(jac->B, ilinkD->y, ilinkA->x));
1250:     PetscCall(VecScale(ilinkA->x, -1.));
1251:     PetscCall(VecScatterBegin(ilinkA->sctx, x, ilinkA->x, ADD_VALUES, SCATTER_FORWARD));
1252:     PetscCall(VecScatterBegin(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1253:     PetscCall(VecScatterEnd(ilinkA->sctx, x, ilinkA->x, ADD_VALUES, SCATTER_FORWARD));
1254:     PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1255:     PetscCall(KSPSolve(kspA, ilinkA->x, ilinkA->y));
1256:     PetscCall(KSPCheckSolve(kspA, pc, ilinkA->y));
1257:     PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1258:     PetscCall(VecScatterEnd(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1259:     PetscCall(VecScatterBegin(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1260:     PetscCall(VecScatterEnd(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1261:     break;
1262:   case PC_FIELDSPLIT_SCHUR_FACT_FULL:
1263:     /* [1 0; A10 A00^{-1} 1] [A00 0; 0 S] [1 A00^{-1}A01; 0 1] */
1264:     PetscCall(MatGetSize(jac->B, NULL, &P));
1265:     N = P;
1266:     PetscCall(VecScatterBegin(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1267:     PetscCall(VecScatterEnd(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1268:     PetscCall(PetscLogEventBegin(KSP_Solve_FS_L, kspLower, ilinkA->x, ilinkA->y, NULL));
1269:     if (kspUpper == kspA) {
1270:       PetscCall(PetscObjectQuery((PetscObject)jac->schur, "AinvB", (PetscObject *)&AinvB));
1271:       if (AinvB) {
1272:         PetscCall(MatGetSize(AinvB, NULL, &N));
1273:         if (N > P) { // first time PCApply_FieldSplit_Schur() is called
1274:           PetscMemType mtype;
1275:           Vec          c = NULL;
1276:           PetscScalar *array;
1277:           PetscInt     m, M;

1279:           PetscCall(MatGetSize(jac->B, &M, NULL));
1280:           PetscCall(MatGetLocalSize(jac->B, &m, NULL));
1281:           PetscCall(MatDenseGetArrayAndMemType(AinvB, &array, &mtype));
1282:           PetscCall(VecCreateMPIWithArrayAndMemType(PetscObjectComm((PetscObject)jac->schur), mtype, 1, m, M, array + m * P, &c));
1283:           PetscCall(MatDenseRestoreArrayAndMemType(AinvB, &array));
1284:           PetscCall(VecCopy(ilinkA->x, c));
1285:           PetscCall(MatSchurComplementComputeExplicitOperator(jac->schur, &jac->schur_user));
1286:           PetscCall(KSPSetOperators(jac->kspschur, jac->schur, jac->schur_user));
1287:           PetscCall(VecCopy(c, ilinkA->y)); // retrieve the solution as the last column of the composed Mat
1288:           PetscCall(VecDestroy(&c));
1289:         }
1290:       }
1291:     }
1292:     if (N == P) PetscCall(KSPSolve(kspLower, ilinkA->x, ilinkA->y));
1293:     PetscCall(KSPCheckSolve(kspLower, pc, ilinkA->y));
1294:     PetscCall(PetscLogEventEnd(KSP_Solve_FS_L, kspLower, ilinkA->x, ilinkA->y, NULL));
1295:     PetscCall(MatMult(jac->C, ilinkA->y, ilinkD->x));
1296:     PetscCall(VecScale(ilinkD->x, -1.0));
1297:     PetscCall(VecScatterBegin(ilinkD->sctx, x, ilinkD->x, ADD_VALUES, SCATTER_FORWARD));
1298:     PetscCall(VecScatterEnd(ilinkD->sctx, x, ilinkD->x, ADD_VALUES, SCATTER_FORWARD));

1300:     PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1301:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1302:     PetscCall(KSPSolve(jac->kspschur, ilinkD->x, ilinkD->y));
1303:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1304:     PetscCall(KSPCheckSolve(jac->kspschur, pc, ilinkD->y));
1305:     PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1306:     PetscCall(VecScatterBegin(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));

1308:     if (kspUpper == kspA) {
1309:       if (!AinvB) {
1310:         PetscCall(MatMult(jac->B, ilinkD->y, ilinkA->y));
1311:         PetscCall(VecAXPY(ilinkA->x, -1.0, ilinkA->y));
1312:         PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1313:         PetscCall(KSPSolve(kspA, ilinkA->x, ilinkA->y));
1314:         PetscCall(KSPCheckSolve(kspA, pc, ilinkA->y));
1315:         PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1316:       } else PetscCall(MatMultAdd(AinvB, ilinkD->y, ilinkA->y, ilinkA->y));
1317:     } else {
1318:       PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1319:       PetscCall(KSPSolve(kspA, ilinkA->x, ilinkA->y));
1320:       PetscCall(KSPCheckSolve(kspA, pc, ilinkA->y));
1321:       PetscCall(MatMult(jac->B, ilinkD->y, ilinkA->x));
1322:       PetscCall(PetscLogEventBegin(KSP_Solve_FS_U, kspUpper, ilinkA->x, ilinkA->z, NULL));
1323:       PetscCall(KSPSolve(kspUpper, ilinkA->x, ilinkA->z));
1324:       PetscCall(KSPCheckSolve(kspUpper, pc, ilinkA->z));
1325:       PetscCall(PetscLogEventEnd(KSP_Solve_FS_U, kspUpper, ilinkA->x, ilinkA->z, NULL));
1326:       PetscCall(VecAXPY(ilinkA->y, -1.0, ilinkA->z));
1327:     }
1328:     PetscCall(VecScatterEnd(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1329:     PetscCall(VecScatterBegin(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1330:     PetscCall(VecScatterEnd(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1331:   }
1332:   PetscFunctionReturn(PETSC_SUCCESS);
1333: }

1335: /*
1336:   PCFieldSplitCreateWorkMats_Private - Allocate per-field dense work matrices for multi-RHS

1338:   Input Parameters:
1339: + pc - the PC context
1340: - X  - matrix to copy column-layout from

1342:   Notes:
1343:   If matrices already exist with correct column count, they are reused.
1344:   If column count changed, old matrices are destroyed and new ones created.
1345: */
1346: static PetscErrorCode PCFieldSplitCreateWorkMats_Private(PC pc, Mat X)
1347: {
1348:   PC_FieldSplit    *jac   = (PC_FieldSplit *)pc->data;
1349:   PC_FieldSplitLink ilink = jac->head;
1350:   PetscInt          mx, Mx, my, My, N;

1352:   PetscFunctionBegin;
1353:   while (ilink) {
1354:     /* check if reallocation needed (previous allocation with wrong column count) */
1355:     if (ilink->X) {
1356:       PetscCall(MatGetSize(ilink->X, NULL, &N));
1357:       if (N != X->cmap->N) {
1358:         PetscCall(MatDestroy(&ilink->X));
1359:         PetscCall(MatDestroy(&ilink->Y));
1360:         PetscCall(MatDestroy(&ilink->Z));
1361:       }
1362:     }
1363:     /* create if needed */
1364:     if (!ilink->X) {
1365:       VecType xtype, ytype;

1367:       PetscCall(VecGetType(ilink->x, &xtype));
1368:       PetscCall(VecGetType(ilink->y, &ytype));
1369:       PetscCall(VecGetLocalSize(ilink->x, &mx));
1370:       PetscCall(VecGetSize(ilink->x, &Mx));
1371:       PetscCall(VecGetLocalSize(ilink->y, &my));
1372:       PetscCall(VecGetSize(ilink->y, &My));
1373:       /* use default lda */
1374:       PetscCall(MatCreateDenseFromVecType(PetscObjectComm((PetscObject)pc), xtype, mx, X->cmap->n, Mx, X->cmap->N, PETSC_DECIDE, NULL, &ilink->X));
1375:       PetscCall(MatCreateDenseFromVecType(PetscObjectComm((PetscObject)pc), ytype, my, X->cmap->n, My, X->cmap->N, PETSC_DECIDE, NULL, &ilink->Y));
1376:     }
1377:     ilink = ilink->next;
1378:   }
1379:   PetscFunctionReturn(PETSC_SUCCESS);
1380: }

1382: static PetscErrorCode PCMatApply_FieldSplit_Schur(PC pc, Mat X, Mat Y)
1383: {
1384:   PC_FieldSplit    *jac    = (PC_FieldSplit *)pc->data;
1385:   PC_FieldSplitLink ilinkA = jac->head, ilinkD = ilinkA->next;
1386:   KSP               kspA = ilinkA->ksp, kspLower = kspA, kspUpper = jac->kspupper;
1387:   Mat               AinvB = NULL;
1388:   PetscInt          N, P;

1390:   PetscFunctionBegin;
1391:   /* create working matrices with the correct number of columns */
1392:   PetscCall(PCFieldSplitCreateWorkMats_Private(pc, X));
1393:   switch (jac->schurfactorization) {
1394:   case PC_FIELDSPLIT_SCHUR_FACT_DIAG:
1395:     /* [A00 0; 0 -S], positive definite, suitable for MINRES */
1396:     PetscCall(MatDenseScatter_Private(ilinkA->sctx, X, ilinkA->X, INSERT_VALUES, SCATTER_FORWARD));
1397:     PetscCall(MatDenseScatter_Private(ilinkD->sctx, X, ilinkD->X, INSERT_VALUES, SCATTER_FORWARD));
1398:     PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->X, ilinkA->Y, NULL));
1399:     PetscCall(KSPMatSolve(kspA, ilinkA->X, ilinkA->Y));
1400:     PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->X, ilinkA->Y, NULL));
1401:     PetscCall(MatDenseScatter_Private(ilinkA->sctx, ilinkA->Y, Y, INSERT_VALUES, SCATTER_REVERSE));
1402:     PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->X, ilinkD->Y, NULL));
1403:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1404:     PetscCall(KSPMatSolve(jac->kspschur, ilinkD->X, ilinkD->Y));
1405:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1406:     PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->X, ilinkD->Y, NULL));
1407:     PetscCall(MatScale(ilinkD->Y, jac->schurscale));
1408:     PetscCall(MatDenseScatter_Private(ilinkD->sctx, ilinkD->Y, Y, INSERT_VALUES, SCATTER_REVERSE));
1409:     break;
1410:   case PC_FIELDSPLIT_SCHUR_FACT_LOWER:
1411:     /* [A00 0; A10 S], suitable for left preconditioning */
1412:     PetscCall(MatDenseScatter_Private(ilinkA->sctx, X, ilinkA->X, INSERT_VALUES, SCATTER_FORWARD));
1413:     PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->X, ilinkA->Y, NULL));
1414:     PetscCall(KSPMatSolve(kspA, ilinkA->X, ilinkA->Y));
1415:     PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->X, ilinkA->Y, NULL));
1416:     PetscCall(MatMatMult(jac->C, ilinkA->Y, MAT_REUSE_MATRIX, PETSC_DETERMINE, &ilinkD->X));
1417:     PetscCall(MatScale(ilinkD->X, -1.0));
1418:     PetscCall(MatDenseScatter_Private(ilinkD->sctx, X, ilinkD->X, ADD_VALUES, SCATTER_FORWARD));
1419:     PetscCall(MatDenseScatter_Private(ilinkA->sctx, ilinkA->Y, Y, INSERT_VALUES, SCATTER_REVERSE));
1420:     PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->X, ilinkD->Y, NULL));
1421:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1422:     PetscCall(KSPMatSolve(jac->kspschur, ilinkD->X, ilinkD->Y));
1423:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1424:     PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->X, ilinkD->Y, NULL));
1425:     PetscCall(MatDenseScatter_Private(ilinkD->sctx, ilinkD->Y, Y, INSERT_VALUES, SCATTER_REVERSE));
1426:     break;
1427:   case PC_FIELDSPLIT_SCHUR_FACT_UPPER:
1428:     /* [A00 A01; 0 S], suitable for right preconditioning */
1429:     PetscCall(MatDenseScatter_Private(ilinkD->sctx, X, ilinkD->X, INSERT_VALUES, SCATTER_FORWARD));
1430:     PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->X, ilinkD->Y, NULL));
1431:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1432:     PetscCall(KSPMatSolve(jac->kspschur, ilinkD->X, ilinkD->Y));
1433:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1434:     PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->X, ilinkD->Y, NULL));
1435:     PetscCall(MatMatMult(jac->B, ilinkD->Y, MAT_REUSE_MATRIX, PETSC_DETERMINE, &ilinkA->X));
1436:     PetscCall(MatScale(ilinkA->X, -1.0));
1437:     PetscCall(MatDenseScatter_Private(ilinkA->sctx, X, ilinkA->X, ADD_VALUES, SCATTER_FORWARD));
1438:     PetscCall(MatDenseScatter_Private(ilinkD->sctx, ilinkD->Y, Y, INSERT_VALUES, SCATTER_REVERSE));
1439:     PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->X, ilinkA->Y, NULL));
1440:     PetscCall(KSPMatSolve(kspA, ilinkA->X, ilinkA->Y));
1441:     PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->X, ilinkA->Y, NULL));
1442:     PetscCall(MatDenseScatter_Private(ilinkA->sctx, ilinkA->Y, Y, INSERT_VALUES, SCATTER_REVERSE));
1443:     break;
1444:   case PC_FIELDSPLIT_SCHUR_FACT_FULL:
1445:     /* [1 0; A10 A00^{-1} 1] [A00 0; 0 S] [1 A00^{-1}A01; 0 1] */
1446:     PetscCall(MatGetSize(jac->B, NULL, &P));
1447:     N = P;
1448:     PetscCall(MatDenseScatter_Private(ilinkA->sctx, X, ilinkA->X, INSERT_VALUES, SCATTER_FORWARD));
1449:     PetscCall(PetscLogEventBegin(KSP_Solve_FS_L, kspLower, ilinkA->X, ilinkA->Y, NULL));
1450:     if (kspUpper == kspA) {
1451:       PetscCall(PetscObjectQuery((PetscObject)jac->schur, "AinvB", (PetscObject *)&AinvB));
1452:       if (AinvB) {
1453:         PetscCall(MatGetSize(AinvB, NULL, &N));
1454:         if (N > P) { // first time PCApply_FieldSplit_Schur() is called
1455:           PetscMemType mtype;
1456:           Mat          C = NULL;
1457:           PetscScalar *array;
1458:           PetscInt     m, M, q, Q, p;

1460:           PetscCall(MatGetSize(jac->B, &M, NULL));
1461:           PetscCall(MatGetLocalSize(jac->B, &m, NULL));
1462:           PetscCall(MatGetSize(X, NULL, &Q));
1463:           PetscCall(MatGetLocalSize(X, NULL, &q));
1464:           PetscCall(MatDenseGetArrayAndMemType(AinvB, &array, &mtype));
1465:           if (N != P + Q) {
1466:             Mat replace;

1468:             PetscCall(MatGetLocalSize(jac->B, NULL, &p));
1469:             if (PetscMemTypeCUDA(mtype)) {
1470: #if PetscDefined(HAVE_CUDA)
1471:               PetscCallCUDA(cudaFree(array));
1472:               PetscCallCUDA(cudaMalloc((void **)&array, sizeof(PetscScalar) * m * (P + Q)));
1473: #endif
1474:             } else if (PetscMemTypeHIP(mtype)) {
1475: #if PetscDefined(HAVE_HIP)
1476:               PetscCallHIP(hipFree(array));
1477:               PetscCallHIP(hipMalloc((void **)&array, sizeof(PetscScalar) * m * (P + Q)));
1478: #endif
1479:             } else {
1480:               PetscCheck(PetscMemTypeHost(mtype), PetscObjectComm((PetscObject)jac->schur), PETSC_ERR_SUP, "PetscMemType should be either PETSC_MEMTYPE_HOST, PETSC_MEMTYPE_CUDA, or PETSC_MEMTYPE_HIP");
1481:               PetscCall(PetscFree(array));
1482:               PetscCall(PetscMalloc1(m * (P + Q), &array));
1483:             }
1484:             PetscCall(MatCreateDenseWithMemType(PetscObjectComm((PetscObject)jac->schur), mtype, m, PETSC_DECIDE, M, P + Q, PETSC_DECIDE, array, &replace));
1485:             PetscCall(MatHeaderReplace(AinvB, &replace));
1486:           }
1487:           PetscCall(MatCreateDenseWithMemType(PetscObjectComm((PetscObject)jac->schur), mtype, m, q, M, Q, PETSC_DECIDE, array + m * P, &C));
1488:           PetscCall(MatDenseRestoreArrayAndMemType(AinvB, &array));
1489:           PetscCall(MatCopy(ilinkA->X, C, SAME_NONZERO_PATTERN));
1490:           PetscCall(MatSchurComplementComputeExplicitOperator(jac->schur, &jac->schur_user));
1491:           PetscCall(KSPSetOperators(jac->kspschur, jac->schur, jac->schur_user));
1492:           PetscCall(MatCopy(C, ilinkA->Y, SAME_NONZERO_PATTERN)); // retrieve solutions as last columns of the composed Mat
1493:           PetscCall(MatDestroy(&C));
1494:         }
1495:       }
1496:     }
1497:     if (N == P) PetscCall(KSPMatSolve(kspLower, ilinkA->X, ilinkA->Y));
1498:     PetscCall(PetscLogEventEnd(KSP_Solve_FS_L, kspLower, ilinkA->X, ilinkA->Y, NULL));
1499:     PetscCall(MatMatMult(jac->C, ilinkA->Y, MAT_REUSE_MATRIX, PETSC_DETERMINE, &ilinkD->X));
1500:     PetscCall(MatScale(ilinkD->X, -1.0));
1501:     PetscCall(MatDenseScatter_Private(ilinkD->sctx, X, ilinkD->X, ADD_VALUES, SCATTER_FORWARD));

1503:     PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->X, ilinkD->Y, NULL));
1504:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1505:     PetscCall(KSPMatSolve(jac->kspschur, ilinkD->X, ilinkD->Y));
1506:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1507:     PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->X, ilinkD->Y, NULL));
1508:     PetscCall(MatDenseScatter_Private(ilinkD->sctx, ilinkD->Y, Y, INSERT_VALUES, SCATTER_REVERSE));

1510:     if (kspUpper == kspA) {
1511:       if (!AinvB) {
1512:         PetscCall(MatMatMult(jac->B, ilinkD->Y, MAT_REUSE_MATRIX, PETSC_DETERMINE, &ilinkA->Y));
1513:         PetscCall(MatAXPY(ilinkA->X, -1.0, ilinkA->Y, SAME_NONZERO_PATTERN));
1514:         PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->X, ilinkA->Y, NULL));
1515:         PetscCall(KSPMatSolve(kspA, ilinkA->X, ilinkA->Y));
1516:         PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->X, ilinkA->Y, NULL));
1517:       } else {
1518:         PetscCall(MatMatMult(AinvB, ilinkD->Y, MAT_REUSE_MATRIX, PETSC_DETERMINE, &ilinkA->X));
1519:         PetscCall(MatAXPY(ilinkA->Y, 1.0, ilinkA->X, SAME_NONZERO_PATTERN));
1520:       }
1521:     } else {
1522:       PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->X, ilinkA->Y, NULL));
1523:       PetscCall(KSPMatSolve(kspA, ilinkA->X, ilinkA->Y));
1524:       PetscCall(MatMatMult(jac->B, ilinkD->Y, MAT_REUSE_MATRIX, PETSC_DETERMINE, &ilinkA->X));
1525:       if (!ilinkA->Z) PetscCall(MatDuplicate(ilinkA->X, MAT_DO_NOT_COPY_VALUES, &ilinkA->Z));
1526:       PetscCall(PetscLogEventBegin(KSP_Solve_FS_U, kspUpper, ilinkA->X, ilinkA->Z, NULL));
1527:       PetscCall(KSPMatSolve(kspUpper, ilinkA->X, ilinkA->Z));
1528:       PetscCall(PetscLogEventEnd(KSP_Solve_FS_U, kspUpper, ilinkA->X, ilinkA->Z, NULL));
1529:       PetscCall(MatAXPY(ilinkA->Y, -1.0, ilinkA->Z, SAME_NONZERO_PATTERN));
1530:     }
1531:     PetscCall(MatDenseScatter_Private(ilinkA->sctx, ilinkA->Y, Y, INSERT_VALUES, SCATTER_REVERSE));
1532:   }
1533:   PetscFunctionReturn(PETSC_SUCCESS);
1534: }

1536: static PetscErrorCode PCApplyTranspose_FieldSplit_Schur(PC pc, Vec x, Vec y)
1537: {
1538:   PC_FieldSplit    *jac    = (PC_FieldSplit *)pc->data;
1539:   PC_FieldSplitLink ilinkA = jac->head, ilinkD = ilinkA->next;
1540:   KSP               kspA = ilinkA->ksp, kspLower = kspA, kspUpper = jac->kspupper;

1542:   PetscFunctionBegin;
1543:   switch (jac->schurfactorization) {
1544:   case PC_FIELDSPLIT_SCHUR_FACT_DIAG:
1545:     /* [A00 0; 0 -S], positive definite, suitable for MINRES */
1546:     PetscCall(VecScatterBegin(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1547:     PetscCall(VecScatterBegin(ilinkD->sctx, x, ilinkD->x, INSERT_VALUES, SCATTER_FORWARD));
1548:     PetscCall(VecScatterEnd(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1549:     PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1550:     PetscCall(KSPSolveTranspose(kspA, ilinkA->x, ilinkA->y));
1551:     PetscCall(KSPCheckSolve(kspA, pc, ilinkA->y));
1552:     PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1553:     PetscCall(VecScatterBegin(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1554:     PetscCall(VecScatterEnd(ilinkD->sctx, x, ilinkD->x, INSERT_VALUES, SCATTER_FORWARD));
1555:     PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1556:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1557:     PetscCall(KSPSolveTranspose(jac->kspschur, ilinkD->x, ilinkD->y));
1558:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1559:     PetscCall(KSPCheckSolve(jac->kspschur, pc, ilinkD->y));
1560:     PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1561:     PetscCall(VecScale(ilinkD->y, jac->schurscale));
1562:     PetscCall(VecScatterEnd(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1563:     PetscCall(VecScatterBegin(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1564:     PetscCall(VecScatterEnd(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1565:     break;
1566:   case PC_FIELDSPLIT_SCHUR_FACT_UPPER:
1567:     PetscCall(VecScatterBegin(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1568:     PetscCall(VecScatterEnd(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1569:     PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1570:     PetscCall(KSPSolveTranspose(kspA, ilinkA->x, ilinkA->y));
1571:     PetscCall(KSPCheckSolve(kspA, pc, ilinkA->y));
1572:     PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1573:     PetscCall(MatMultTranspose(jac->B, ilinkA->y, ilinkD->x));
1574:     PetscCall(VecScale(ilinkD->x, -1.));
1575:     PetscCall(VecScatterBegin(ilinkD->sctx, x, ilinkD->x, ADD_VALUES, SCATTER_FORWARD));
1576:     PetscCall(VecScatterBegin(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1577:     PetscCall(VecScatterEnd(ilinkD->sctx, x, ilinkD->x, ADD_VALUES, SCATTER_FORWARD));
1578:     PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1579:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1580:     PetscCall(KSPSolveTranspose(jac->kspschur, ilinkD->x, ilinkD->y));
1581:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1582:     PetscCall(KSPCheckSolve(jac->kspschur, pc, ilinkD->y));
1583:     PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1584:     PetscCall(VecScatterEnd(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1585:     PetscCall(VecScatterBegin(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1586:     PetscCall(VecScatterEnd(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1587:     break;
1588:   case PC_FIELDSPLIT_SCHUR_FACT_LOWER:
1589:     PetscCall(VecScatterBegin(ilinkD->sctx, x, ilinkD->x, INSERT_VALUES, SCATTER_FORWARD));
1590:     PetscCall(VecScatterEnd(ilinkD->sctx, x, ilinkD->x, INSERT_VALUES, SCATTER_FORWARD));
1591:     PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1592:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1593:     PetscCall(KSPSolveTranspose(jac->kspschur, ilinkD->x, ilinkD->y));
1594:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1595:     PetscCall(KSPCheckSolve(jac->kspschur, pc, ilinkD->y));
1596:     PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1597:     PetscCall(MatMultTranspose(jac->C, ilinkD->y, ilinkA->x));
1598:     PetscCall(VecScale(ilinkA->x, -1.));
1599:     PetscCall(VecScatterBegin(ilinkA->sctx, x, ilinkA->x, ADD_VALUES, SCATTER_FORWARD));
1600:     PetscCall(VecScatterBegin(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1601:     PetscCall(VecScatterEnd(ilinkA->sctx, x, ilinkA->x, ADD_VALUES, SCATTER_FORWARD));
1602:     PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1603:     PetscCall(KSPSolveTranspose(kspA, ilinkA->x, ilinkA->y));
1604:     PetscCall(KSPCheckSolve(kspA, pc, ilinkA->y));
1605:     PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1606:     PetscCall(VecScatterEnd(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1607:     PetscCall(VecScatterBegin(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1608:     PetscCall(VecScatterEnd(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1609:     break;
1610:   case PC_FIELDSPLIT_SCHUR_FACT_FULL:
1611:     PetscCall(VecScatterBegin(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1612:     PetscCall(VecScatterEnd(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1613:     PetscCall(PetscLogEventBegin(KSP_Solve_FS_U, kspUpper, ilinkA->x, ilinkA->y, NULL));
1614:     PetscCall(KSPSolveTranspose(kspUpper, ilinkA->x, ilinkA->y));
1615:     PetscCall(KSPCheckSolve(kspUpper, pc, ilinkA->y));
1616:     PetscCall(PetscLogEventEnd(KSP_Solve_FS_U, kspUpper, ilinkA->x, ilinkA->y, NULL));
1617:     PetscCall(MatMultTranspose(jac->B, ilinkA->y, ilinkD->x));
1618:     PetscCall(VecScale(ilinkD->x, -1.0));
1619:     PetscCall(VecScatterBegin(ilinkD->sctx, x, ilinkD->x, ADD_VALUES, SCATTER_FORWARD));
1620:     PetscCall(VecScatterEnd(ilinkD->sctx, x, ilinkD->x, ADD_VALUES, SCATTER_FORWARD));

1622:     PetscCall(PetscLogEventBegin(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1623:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, 1));
1624:     PetscCall(KSPSolveTranspose(jac->kspschur, ilinkD->x, ilinkD->y));
1625:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)kspA, (PetscObject)kspA, -1));
1626:     PetscCall(KSPCheckSolve(jac->kspschur, pc, ilinkD->y));
1627:     PetscCall(PetscLogEventEnd(KSP_Solve_FS_S, jac->kspschur, ilinkD->x, ilinkD->y, NULL));
1628:     PetscCall(VecScatterBegin(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));

1630:     if (kspLower == kspA) {
1631:       PetscCall(MatMultTranspose(jac->C, ilinkD->y, ilinkA->y));
1632:       PetscCall(VecAXPY(ilinkA->x, -1.0, ilinkA->y));
1633:       PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1634:       PetscCall(KSPSolveTranspose(kspA, ilinkA->x, ilinkA->y));
1635:       PetscCall(KSPCheckSolve(kspA, pc, ilinkA->y));
1636:       PetscCall(PetscLogEventEnd(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1637:     } else {
1638:       PetscCall(PetscLogEventBegin(ilinkA->event, kspA, ilinkA->x, ilinkA->y, NULL));
1639:       PetscCall(KSPSolveTranspose(kspA, ilinkA->x, ilinkA->y));
1640:       PetscCall(KSPCheckSolve(kspA, pc, ilinkA->y));
1641:       PetscCall(MatMultTranspose(jac->C, ilinkD->y, ilinkA->x));
1642:       PetscCall(PetscLogEventBegin(KSP_Solve_FS_L, kspLower, ilinkA->x, ilinkA->z, NULL));
1643:       PetscCall(KSPSolveTranspose(kspLower, ilinkA->x, ilinkA->z));
1644:       PetscCall(KSPCheckSolve(kspLower, pc, ilinkA->z));
1645:       PetscCall(PetscLogEventEnd(KSP_Solve_FS_L, kspLower, ilinkA->x, ilinkA->z, NULL));
1646:       PetscCall(VecAXPY(ilinkA->y, -1.0, ilinkA->z));
1647:     }
1648:     PetscCall(VecScatterEnd(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1649:     PetscCall(VecScatterBegin(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1650:     PetscCall(VecScatterEnd(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1651:   }
1652:   PetscFunctionReturn(PETSC_SUCCESS);
1653: }

1655: #define FieldSplitSplitSolveAdd(ilink, xx, yy) \
1656:   ((PetscErrorCode)(VecScatterBegin(ilink->sctx, xx, ilink->x, INSERT_VALUES, SCATTER_FORWARD) || VecScatterEnd(ilink->sctx, xx, ilink->x, INSERT_VALUES, SCATTER_FORWARD) || PetscLogEventBegin(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL) || \
1657:                     KSPSolve(ilink->ksp, ilink->x, ilink->y) || KSPCheckSolve(ilink->ksp, pc, ilink->y) || PetscLogEventEnd(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL) || VecScatterBegin(ilink->sctx, ilink->y, yy, ADD_VALUES, SCATTER_REVERSE) || \
1658:                     VecScatterEnd(ilink->sctx, ilink->y, yy, ADD_VALUES, SCATTER_REVERSE)))

1660: static PetscErrorCode PCApply_FieldSplit(PC pc, Vec x, Vec y)
1661: {
1662:   PC_FieldSplit    *jac   = (PC_FieldSplit *)pc->data;
1663:   PC_FieldSplitLink ilink = jac->head;
1664:   PetscInt          cnt, bs;

1666:   PetscFunctionBegin;
1667:   if (jac->type == PC_COMPOSITE_ADDITIVE) {
1668:     PetscBool matnest;

1670:     PetscCall(PetscObjectTypeCompare((PetscObject)pc->pmat, MATNEST, &matnest));
1671:     if (jac->defaultsplit && !matnest) {
1672:       PetscCall(VecGetBlockSize(x, &bs));
1673:       PetscCheck(jac->bs <= 0 || bs == jac->bs, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Blocksize of x vector %" PetscInt_FMT " does not match fieldsplit blocksize %" PetscInt_FMT, bs, jac->bs);
1674:       PetscCall(VecGetBlockSize(y, &bs));
1675:       PetscCheck(jac->bs <= 0 || bs == jac->bs, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Blocksize of y vector %" PetscInt_FMT " does not match fieldsplit blocksize %" PetscInt_FMT, bs, jac->bs);
1676:       PetscCall(VecStrideGatherAll(x, jac->x, INSERT_VALUES));
1677:       while (ilink) {
1678:         PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1679:         PetscCall(KSPSolve(ilink->ksp, ilink->x, ilink->y));
1680:         PetscCall(KSPCheckSolve(ilink->ksp, pc, ilink->y));
1681:         PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1682:         ilink = ilink->next;
1683:       }
1684:       PetscCall(VecStrideScatterAll(jac->y, y, INSERT_VALUES));
1685:     } else {
1686:       PetscCall(VecSet(y, 0.0));
1687:       while (ilink) {
1688:         PetscCall(FieldSplitSplitSolveAdd(ilink, x, y));
1689:         ilink = ilink->next;
1690:       }
1691:     }
1692:   } else if (jac->type == PC_COMPOSITE_MULTIPLICATIVE && jac->nsplits == 2) {
1693:     PetscCall(VecSet(y, 0.0));
1694:     /* solve on first block for first block variables */
1695:     PetscCall(VecScatterBegin(ilink->sctx, x, ilink->x, INSERT_VALUES, SCATTER_FORWARD));
1696:     PetscCall(VecScatterEnd(ilink->sctx, x, ilink->x, INSERT_VALUES, SCATTER_FORWARD));
1697:     PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1698:     PetscCall(KSPSolve(ilink->ksp, ilink->x, ilink->y));
1699:     PetscCall(KSPCheckSolve(ilink->ksp, pc, ilink->y));
1700:     PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1701:     PetscCall(VecScatterBegin(ilink->sctx, ilink->y, y, ADD_VALUES, SCATTER_REVERSE));
1702:     PetscCall(VecScatterEnd(ilink->sctx, ilink->y, y, ADD_VALUES, SCATTER_REVERSE));

1704:     /* compute the residual only onto second block variables using first block variables */
1705:     PetscCall(MatMult(jac->Afield[1], ilink->y, ilink->next->x));
1706:     ilink = ilink->next;
1707:     PetscCall(VecScale(ilink->x, -1.0));
1708:     PetscCall(VecScatterBegin(ilink->sctx, x, ilink->x, ADD_VALUES, SCATTER_FORWARD));
1709:     PetscCall(VecScatterEnd(ilink->sctx, x, ilink->x, ADD_VALUES, SCATTER_FORWARD));

1711:     /* solve on second block variables */
1712:     PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1713:     PetscCall(KSPSolve(ilink->ksp, ilink->x, ilink->y));
1714:     PetscCall(KSPCheckSolve(ilink->ksp, pc, ilink->y));
1715:     PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1716:     PetscCall(VecScatterBegin(ilink->sctx, ilink->y, y, ADD_VALUES, SCATTER_REVERSE));
1717:     PetscCall(VecScatterEnd(ilink->sctx, ilink->y, y, ADD_VALUES, SCATTER_REVERSE));
1718:   } else if (jac->type == PC_COMPOSITE_MULTIPLICATIVE || jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
1719:     if (!jac->w1) {
1720:       PetscCall(VecDuplicate(x, &jac->w1));
1721:       PetscCall(VecDuplicate(x, &jac->w2));
1722:     }
1723:     PetscCall(VecSet(y, 0.0));
1724:     PetscCall(FieldSplitSplitSolveAdd(ilink, x, y));
1725:     cnt = 1;
1726:     while (ilink->next) {
1727:       ilink = ilink->next;
1728:       /* compute the residual only over the part of the vector needed */
1729:       PetscCall(MatMult(jac->Afield[cnt++], y, ilink->x));
1730:       PetscCall(VecScale(ilink->x, -1.0));
1731:       PetscCall(VecScatterBegin(ilink->sctx, x, ilink->x, ADD_VALUES, SCATTER_FORWARD));
1732:       PetscCall(VecScatterEnd(ilink->sctx, x, ilink->x, ADD_VALUES, SCATTER_FORWARD));
1733:       PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1734:       PetscCall(KSPSolve(ilink->ksp, ilink->x, ilink->y));
1735:       PetscCall(KSPCheckSolve(ilink->ksp, pc, ilink->y));
1736:       PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1737:       PetscCall(VecScatterBegin(ilink->sctx, ilink->y, y, ADD_VALUES, SCATTER_REVERSE));
1738:       PetscCall(VecScatterEnd(ilink->sctx, ilink->y, y, ADD_VALUES, SCATTER_REVERSE));
1739:     }
1740:     if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
1741:       cnt -= 2;
1742:       while (ilink->previous) {
1743:         ilink = ilink->previous;
1744:         /* compute the residual only over the part of the vector needed */
1745:         PetscCall(MatMult(jac->Afield[cnt--], y, ilink->x));
1746:         PetscCall(VecScale(ilink->x, -1.0));
1747:         PetscCall(VecScatterBegin(ilink->sctx, x, ilink->x, ADD_VALUES, SCATTER_FORWARD));
1748:         PetscCall(VecScatterEnd(ilink->sctx, x, ilink->x, ADD_VALUES, SCATTER_FORWARD));
1749:         PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1750:         PetscCall(KSPSolve(ilink->ksp, ilink->x, ilink->y));
1751:         PetscCall(KSPCheckSolve(ilink->ksp, pc, ilink->y));
1752:         PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1753:         PetscCall(VecScatterBegin(ilink->sctx, ilink->y, y, ADD_VALUES, SCATTER_REVERSE));
1754:         PetscCall(VecScatterEnd(ilink->sctx, ilink->y, y, ADD_VALUES, SCATTER_REVERSE));
1755:       }
1756:     }
1757:   } else SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Unsupported or unknown composition %d", (int)jac->type);
1758:   PetscFunctionReturn(PETSC_SUCCESS);
1759: }

1761: static PetscErrorCode PCMatApply_FieldSplit(PC pc, Mat X, Mat Y)
1762: {
1763:   PC_FieldSplit    *jac   = (PC_FieldSplit *)pc->data;
1764:   PC_FieldSplitLink ilink = jac->head;
1765:   PetscInt          cnt;

1767:   PetscFunctionBegin;
1768:   /* create working matrices with the correct number of columns */
1769:   PetscCall(PCFieldSplitCreateWorkMats_Private(pc, X));
1770:   if (jac->type == PC_COMPOSITE_ADDITIVE) {
1771:     PetscCall(MatZeroEntries(Y));
1772:     while (ilink) {
1773:       PetscCall(MatDenseScatter_Private(ilink->sctx, X, ilink->X, INSERT_VALUES, SCATTER_FORWARD));
1774:       PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1775:       PetscCall(KSPMatSolve(ilink->ksp, ilink->X, ilink->Y));
1776:       PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1777:       PetscCall(MatDenseScatter_Private(ilink->sctx, ilink->Y, Y, ADD_VALUES, SCATTER_REVERSE));
1778:       ilink = ilink->next;
1779:     }
1780:   } else if (jac->type == PC_COMPOSITE_MULTIPLICATIVE && jac->nsplits == 2) {
1781:     PetscCall(MatZeroEntries(Y));
1782:     PetscCall(MatDenseScatter_Private(ilink->sctx, X, ilink->X, INSERT_VALUES, SCATTER_FORWARD));
1783:     PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1784:     PetscCall(KSPMatSolve(ilink->ksp, ilink->X, ilink->Y));
1785:     PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1786:     PetscCall(MatDenseScatter_Private(ilink->sctx, ilink->Y, Y, ADD_VALUES, SCATTER_REVERSE));

1788:     /* compute the residual only onto second block variables using first block variables */
1789:     PetscCall(MatMatMult(jac->Afield[1], ilink->Y, MAT_REUSE_MATRIX, PETSC_DETERMINE, &ilink->next->X));
1790:     ilink = ilink->next;
1791:     PetscCall(MatScale(ilink->X, -1.0));
1792:     PetscCall(MatDenseScatter_Private(ilink->sctx, X, ilink->X, ADD_VALUES, SCATTER_FORWARD));

1794:     /* solve on second block variables */
1795:     PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1796:     PetscCall(KSPMatSolve(ilink->ksp, ilink->X, ilink->Y));
1797:     PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1798:     PetscCall(MatDenseScatter_Private(ilink->sctx, ilink->Y, Y, ADD_VALUES, SCATTER_REVERSE));
1799:   } else if (jac->type == PC_COMPOSITE_MULTIPLICATIVE || jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
1800:     /* general multiplicative with any number of splits */
1801:     PetscCall(MatZeroEntries(Y));
1802:     /* first split */
1803:     PetscCall(MatDenseScatter_Private(ilink->sctx, X, ilink->X, INSERT_VALUES, SCATTER_FORWARD));
1804:     PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1805:     PetscCall(KSPMatSolve(ilink->ksp, ilink->X, ilink->Y));
1806:     PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1807:     PetscCall(MatDenseScatter_Private(ilink->sctx, ilink->Y, Y, ADD_VALUES, SCATTER_REVERSE));
1808:     cnt = 1;
1809:     /* forward sweep */
1810:     while (ilink->next) {
1811:       ilink = ilink->next;
1812:       /* compute the residual only over the part of the vector needed */
1813:       PetscCall(MatMatMult(jac->Afield[cnt++], Y, MAT_REUSE_MATRIX, PETSC_DETERMINE, &ilink->X));
1814:       PetscCall(MatScale(ilink->X, -1.0));
1815:       PetscCall(MatDenseScatter_Private(ilink->sctx, X, ilink->X, ADD_VALUES, SCATTER_FORWARD));
1816:       PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1817:       PetscCall(KSPMatSolve(ilink->ksp, ilink->X, ilink->Y));
1818:       PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1819:       PetscCall(MatDenseScatter_Private(ilink->sctx, ilink->Y, Y, ADD_VALUES, SCATTER_REVERSE));
1820:     }
1821:     /* backward sweep for symmetric multiplicative */
1822:     if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
1823:       cnt -= 2;
1824:       while (ilink->previous) {
1825:         ilink = ilink->previous;
1826:         /* compute the residual only over the part of the vector needed */
1827:         PetscCall(MatMatMult(jac->Afield[cnt--], Y, MAT_REUSE_MATRIX, PETSC_DETERMINE, &ilink->X));
1828:         PetscCall(MatScale(ilink->X, -1.0));
1829:         PetscCall(MatDenseScatter_Private(ilink->sctx, X, ilink->X, ADD_VALUES, SCATTER_FORWARD));
1830:         PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1831:         PetscCall(KSPMatSolve(ilink->ksp, ilink->X, ilink->Y));
1832:         PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->X, ilink->Y, NULL));
1833:         PetscCall(MatDenseScatter_Private(ilink->sctx, ilink->Y, Y, ADD_VALUES, SCATTER_REVERSE));
1834:       }
1835:     }
1836:   } else SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "PCMatApply() not implemented for this fieldsplit type");
1837:   PetscFunctionReturn(PETSC_SUCCESS);
1838: }

1840: static PetscErrorCode PCApply_FieldSplit_GKB(PC pc, Vec x, Vec y)
1841: {
1842:   PC_FieldSplit    *jac    = (PC_FieldSplit *)pc->data;
1843:   PC_FieldSplitLink ilinkA = jac->head, ilinkD = ilinkA->next;
1844:   KSP               ksp = ilinkA->ksp;
1845:   Vec               u, v, Hu, d, work1, work2;
1846:   PetscScalar       alpha, z, nrmz2, *vecz;
1847:   PetscReal         lowbnd, nu, beta;
1848:   PetscInt          iterGKB;

1850:   PetscFunctionBegin;
1851:   PetscCall(VecScatterBegin(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1852:   PetscCall(VecScatterBegin(ilinkD->sctx, x, ilinkD->x, INSERT_VALUES, SCATTER_FORWARD));
1853:   PetscCall(VecScatterEnd(ilinkA->sctx, x, ilinkA->x, INSERT_VALUES, SCATTER_FORWARD));
1854:   PetscCall(VecScatterEnd(ilinkD->sctx, x, ilinkD->x, INSERT_VALUES, SCATTER_FORWARD));

1856:   u     = jac->u;
1857:   v     = jac->v;
1858:   Hu    = jac->Hu;
1859:   d     = jac->d;
1860:   work1 = jac->w1;
1861:   work2 = jac->w2;
1862:   vecz  = jac->vecz;

1864:   /* Change RHS to comply with matrix regularization H = A + nu*B*B' */
1865:   /* Add q = q + nu*B*b */
1866:   if (jac->gkbnu) {
1867:     nu = jac->gkbnu;
1868:     PetscCall(VecScale(ilinkD->x, jac->gkbnu));
1869:     PetscCall(MatMultAdd(jac->B, ilinkD->x, ilinkA->x, ilinkA->x)); /* q = q + nu*B*b */
1870:   } else {
1871:     /* Situation when no augmented Lagrangian is used. Then we set inner  */
1872:     /* matrix N = I in [Ar13], and thus nu = 1.                           */
1873:     nu = 1;
1874:   }

1876:   /* Transform rhs from [q,tilde{b}] to [0,b] */
1877:   PetscCall(PetscLogEventBegin(ilinkA->event, ksp, ilinkA->x, ilinkA->y, NULL));
1878:   PetscCall(KSPSolve(ksp, ilinkA->x, ilinkA->y));
1879:   PetscCall(KSPCheckSolve(ksp, pc, ilinkA->y));
1880:   PetscCall(PetscLogEventEnd(ilinkA->event, ksp, ilinkA->x, ilinkA->y, NULL));
1881:   PetscCall(MatMultHermitianTranspose(jac->B, ilinkA->y, work1));
1882:   PetscCall(VecAXPBY(work1, 1.0 / nu, -1.0, ilinkD->x)); /* c = b - B'*x        */

1884:   /* First step of algorithm */
1885:   PetscCall(VecNorm(work1, NORM_2, &beta)); /* beta = sqrt(nu*c'*c)*/
1886:   KSPCheckDot(ksp, beta);
1887:   beta = PetscSqrtReal(nu) * beta;
1888:   PetscCall(VecAXPBY(v, nu / beta, 0.0, work1)); /* v = nu/beta *c      */
1889:   PetscCall(MatMult(jac->B, v, work2));          /* u = H^{-1}*B*v      */
1890:   PetscCall(PetscLogEventBegin(ilinkA->event, ksp, work2, u, NULL));
1891:   PetscCall(KSPSolve(ksp, work2, u));
1892:   PetscCall(KSPCheckSolve(ksp, pc, u));
1893:   PetscCall(PetscLogEventEnd(ilinkA->event, ksp, work2, u, NULL));
1894:   PetscCall(MatMult(jac->H, u, Hu)); /* alpha = u'*H*u      */
1895:   PetscCall(VecDot(Hu, u, &alpha));
1896:   KSPCheckDot(ksp, alpha);
1897:   PetscCheck(PetscRealPart(alpha) > 0.0, PETSC_COMM_SELF, PETSC_ERR_NOT_CONVERGED, "GKB preconditioner diverged, H is not positive definite");
1898:   alpha = PetscSqrtReal(PetscAbsScalar(alpha));
1899:   PetscCall(VecScale(u, 1.0 / alpha));
1900:   PetscCall(VecAXPBY(d, 1.0 / alpha, 0.0, v)); /* v = nu/beta *c      */

1902:   z       = beta / alpha;
1903:   vecz[1] = z;

1905:   /* Computation of first iterate x(1) and p(1) */
1906:   PetscCall(VecAXPY(ilinkA->y, z, u));
1907:   PetscCall(VecCopy(d, ilinkD->y));
1908:   PetscCall(VecScale(ilinkD->y, -z));

1910:   iterGKB = 1;
1911:   lowbnd  = 2 * jac->gkbtol;
1912:   if (jac->gkbmonitor) PetscCall(PetscViewerASCIIPrintf(jac->gkbviewer, "%3" PetscInt_FMT " GKB Lower bound estimate %14.12e\n", iterGKB, (double)lowbnd));

1914:   while (iterGKB < jac->gkbmaxit && lowbnd > jac->gkbtol) {
1915:     iterGKB += 1;
1916:     PetscCall(MatMultHermitianTranspose(jac->B, u, work1)); /* v <- nu*(B'*u-alpha/nu*v) */
1917:     PetscCall(VecAXPBY(v, nu, -alpha, work1));
1918:     PetscCall(VecNorm(v, NORM_2, &beta)); /* beta = sqrt(nu)*v'*v      */
1919:     beta = beta / PetscSqrtReal(nu);
1920:     PetscCall(VecScale(v, 1.0 / beta));
1921:     PetscCall(MatMult(jac->B, v, work2)); /* u <- H^{-1}*(B*v-beta*H*u) */
1922:     PetscCall(MatMult(jac->H, u, Hu));
1923:     PetscCall(VecAXPY(work2, -beta, Hu));
1924:     PetscCall(PetscLogEventBegin(ilinkA->event, ksp, work2, u, NULL));
1925:     PetscCall(KSPSolve(ksp, work2, u));
1926:     PetscCall(KSPCheckSolve(ksp, pc, u));
1927:     PetscCall(PetscLogEventEnd(ilinkA->event, ksp, work2, u, NULL));
1928:     PetscCall(MatMult(jac->H, u, Hu)); /* alpha = u'*H*u            */
1929:     PetscCall(VecDot(Hu, u, &alpha));
1930:     KSPCheckDot(ksp, alpha);
1931:     PetscCheck(PetscRealPart(alpha) > 0.0, PETSC_COMM_SELF, PETSC_ERR_NOT_CONVERGED, "GKB preconditioner diverged, H is not positive definite");
1932:     alpha = PetscSqrtReal(PetscAbsScalar(alpha));
1933:     PetscCall(VecScale(u, 1.0 / alpha));

1935:     z       = -beta / alpha * z; /* z <- beta/alpha*z     */
1936:     vecz[0] = z;

1938:     /* Computation of new iterate x(i+1) and p(i+1) */
1939:     PetscCall(VecAXPBY(d, 1.0 / alpha, -beta / alpha, v)); /* d = (v-beta*d)/alpha */
1940:     PetscCall(VecAXPY(ilinkA->y, z, u));                   /* r = r + z*u          */
1941:     PetscCall(VecAXPY(ilinkD->y, -z, d));                  /* p = p - z*d          */
1942:     PetscCall(MatMult(jac->H, ilinkA->y, Hu));             /* ||u||_H = u'*H*u     */
1943:     PetscCall(VecDot(Hu, ilinkA->y, &nrmz2));

1945:     /* Compute Lower Bound estimate */
1946:     if (iterGKB > jac->gkbdelay) {
1947:       lowbnd = 0.0;
1948:       for (PetscInt j = 0; j < jac->gkbdelay; j++) lowbnd += PetscAbsScalar(vecz[j] * vecz[j]);
1949:       lowbnd = PetscSqrtReal(lowbnd / PetscAbsScalar(nrmz2));
1950:     }

1952:     for (PetscInt j = 0; j < jac->gkbdelay - 1; j++) vecz[jac->gkbdelay - j - 1] = vecz[jac->gkbdelay - j - 2];
1953:     if (jac->gkbmonitor) PetscCall(PetscViewerASCIIPrintf(jac->gkbviewer, "%3" PetscInt_FMT " GKB Lower bound estimate %14.12e\n", iterGKB, (double)lowbnd));
1954:   }

1956:   PetscCall(VecScatterBegin(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1957:   PetscCall(VecScatterEnd(ilinkA->sctx, ilinkA->y, y, INSERT_VALUES, SCATTER_REVERSE));
1958:   PetscCall(VecScatterBegin(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1959:   PetscCall(VecScatterEnd(ilinkD->sctx, ilinkD->y, y, INSERT_VALUES, SCATTER_REVERSE));
1960:   PetscFunctionReturn(PETSC_SUCCESS);
1961: }

1963: #define FieldSplitSplitSolveAddTranspose(ilink, xx, yy) \
1964:   ((PetscErrorCode)(VecScatterBegin(ilink->sctx, xx, ilink->y, INSERT_VALUES, SCATTER_FORWARD) || VecScatterEnd(ilink->sctx, xx, ilink->y, INSERT_VALUES, SCATTER_FORWARD) || PetscLogEventBegin(ilink->event, ilink->ksp, ilink->y, ilink->x, NULL) || \
1965:                     KSPSolveTranspose(ilink->ksp, ilink->y, ilink->x) || KSPCheckSolve(ilink->ksp, pc, ilink->x) || PetscLogEventEnd(ilink->event, ilink->ksp, ilink->y, ilink->x, NULL) || VecScatterBegin(ilink->sctx, ilink->x, yy, ADD_VALUES, SCATTER_REVERSE) || \
1966:                     VecScatterEnd(ilink->sctx, ilink->x, yy, ADD_VALUES, SCATTER_REVERSE)))

1968: static PetscErrorCode PCApplyTranspose_FieldSplit(PC pc, Vec x, Vec y)
1969: {
1970:   PC_FieldSplit    *jac   = (PC_FieldSplit *)pc->data;
1971:   PC_FieldSplitLink ilink = jac->head;
1972:   PetscInt          bs;

1974:   PetscFunctionBegin;
1975:   if (jac->type == PC_COMPOSITE_ADDITIVE) {
1976:     PetscBool matnest;

1978:     PetscCall(PetscObjectTypeCompare((PetscObject)pc->pmat, MATNEST, &matnest));
1979:     if (jac->defaultsplit && !matnest) {
1980:       PetscCall(VecGetBlockSize(x, &bs));
1981:       PetscCheck(jac->bs <= 0 || bs == jac->bs, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Blocksize of x vector %" PetscInt_FMT " does not match fieldsplit blocksize %" PetscInt_FMT, bs, jac->bs);
1982:       PetscCall(VecGetBlockSize(y, &bs));
1983:       PetscCheck(jac->bs <= 0 || bs == jac->bs, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Blocksize of y vector %" PetscInt_FMT " does not match fieldsplit blocksize %" PetscInt_FMT, bs, jac->bs);
1984:       PetscCall(VecStrideGatherAll(x, jac->x, INSERT_VALUES));
1985:       while (ilink) {
1986:         PetscCall(PetscLogEventBegin(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1987:         PetscCall(KSPSolveTranspose(ilink->ksp, ilink->x, ilink->y));
1988:         PetscCall(KSPCheckSolve(ilink->ksp, pc, ilink->y));
1989:         PetscCall(PetscLogEventEnd(ilink->event, ilink->ksp, ilink->x, ilink->y, NULL));
1990:         ilink = ilink->next;
1991:       }
1992:       PetscCall(VecStrideScatterAll(jac->y, y, INSERT_VALUES));
1993:     } else {
1994:       PetscCall(VecSet(y, 0.0));
1995:       while (ilink) {
1996:         PetscCall(FieldSplitSplitSolveAddTranspose(ilink, x, y));
1997:         ilink = ilink->next;
1998:       }
1999:     }
2000:   } else {
2001:     if (!jac->w1) {
2002:       PetscCall(VecDuplicate(x, &jac->w1));
2003:       PetscCall(VecDuplicate(x, &jac->w2));
2004:     }
2005:     PetscCall(VecSet(y, 0.0));
2006:     if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
2007:       PetscCall(FieldSplitSplitSolveAddTranspose(ilink, x, y));
2008:       while (ilink->next) {
2009:         ilink = ilink->next;
2010:         PetscCall(MatMultTranspose(pc->mat, y, jac->w1));
2011:         PetscCall(VecWAXPY(jac->w2, -1.0, jac->w1, x));
2012:         PetscCall(FieldSplitSplitSolveAddTranspose(ilink, jac->w2, y));
2013:       }
2014:       while (ilink->previous) {
2015:         ilink = ilink->previous;
2016:         PetscCall(MatMultTranspose(pc->mat, y, jac->w1));
2017:         PetscCall(VecWAXPY(jac->w2, -1.0, jac->w1, x));
2018:         PetscCall(FieldSplitSplitSolveAddTranspose(ilink, jac->w2, y));
2019:       }
2020:     } else {
2021:       while (ilink->next) { /* get to last entry in linked list */
2022:         ilink = ilink->next;
2023:       }
2024:       PetscCall(FieldSplitSplitSolveAddTranspose(ilink, x, y));
2025:       while (ilink->previous) {
2026:         ilink = ilink->previous;
2027:         PetscCall(MatMultTranspose(pc->mat, y, jac->w1));
2028:         PetscCall(VecWAXPY(jac->w2, -1.0, jac->w1, x));
2029:         PetscCall(FieldSplitSplitSolveAddTranspose(ilink, jac->w2, y));
2030:       }
2031:     }
2032:   }
2033:   PetscFunctionReturn(PETSC_SUCCESS);
2034: }

2036: static PetscErrorCode PCReset_FieldSplit(PC pc)
2037: {
2038:   PC_FieldSplit    *jac   = (PC_FieldSplit *)pc->data;
2039:   PC_FieldSplitLink ilink = jac->head, next;

2041:   PetscFunctionBegin;
2042:   while (ilink) {
2043:     PetscCall(KSPDestroy(&ilink->ksp));
2044:     PetscCall(VecDestroy(&ilink->x));
2045:     PetscCall(VecDestroy(&ilink->y));
2046:     PetscCall(VecDestroy(&ilink->z));
2047:     PetscCall(MatDestroy(&ilink->X));
2048:     PetscCall(MatDestroy(&ilink->Y));
2049:     PetscCall(MatDestroy(&ilink->Z));
2050:     PetscCall(VecScatterDestroy(&ilink->sctx));
2051:     PetscCall(ISDestroy(&ilink->is));
2052:     PetscCall(ISDestroy(&ilink->is_col));
2053:     PetscCall(PetscFree(ilink->splitname));
2054:     PetscCall(PetscFree(ilink->fields));
2055:     PetscCall(PetscFree(ilink->fields_col));
2056:     next = ilink->next;
2057:     PetscCall(PetscFree(ilink));
2058:     ilink = next;
2059:   }
2060:   jac->head = NULL;
2061:   PetscCall(PetscFree2(jac->x, jac->y));
2062:   if (jac->mat && jac->mat != jac->pmat) {
2063:     PetscCall(MatDestroyMatrices(jac->nsplits, &jac->mat));
2064:   } else if (jac->mat) {
2065:     jac->mat = NULL;
2066:   }
2067:   if (jac->pmat) PetscCall(MatDestroyMatrices(jac->nsplits, &jac->pmat));
2068:   if (jac->Afield) PetscCall(MatDestroyMatrices(jac->nsplits, &jac->Afield));
2069:   jac->nsplits = 0;
2070:   PetscCall(VecDestroy(&jac->w1));
2071:   PetscCall(VecDestroy(&jac->w2));
2072:   if (jac->schur) PetscCall(PetscObjectCompose((PetscObject)jac->schur, "AinvB", NULL));
2073:   PetscCall(MatDestroy(&jac->schur));
2074:   PetscCall(MatDestroy(&jac->schurp));
2075:   PetscCall(MatDestroy(&jac->schur_user));
2076:   PetscCall(KSPDestroy(&jac->kspschur));
2077:   PetscCall(KSPDestroy(&jac->kspupper));
2078:   PetscCall(MatDestroy(&jac->B));
2079:   PetscCall(MatDestroy(&jac->C));
2080:   PetscCall(MatDestroy(&jac->H));
2081:   PetscCall(VecDestroy(&jac->u));
2082:   PetscCall(VecDestroy(&jac->v));
2083:   PetscCall(VecDestroy(&jac->Hu));
2084:   PetscCall(VecDestroy(&jac->d));
2085:   PetscCall(PetscFree(jac->vecz));
2086:   PetscCall(PetscViewerDestroy(&jac->gkbviewer));
2087:   jac->isrestrict = PETSC_FALSE;
2088:   PetscFunctionReturn(PETSC_SUCCESS);
2089: }

2091: static PetscErrorCode PCDestroy_FieldSplit(PC pc)
2092: {
2093:   PetscFunctionBegin;
2094:   PetscCall(PCReset_FieldSplit(pc));
2095:   PetscCall(PetscFree(pc->data));
2096:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCSetCoordinates_C", NULL));
2097:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetFields_C", NULL));
2098:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetIS_C", NULL));
2099:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetType_C", NULL));
2100:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetBlockSize_C", NULL));
2101:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitRestrictIS_C", NULL));
2102:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSchurGetSubKSP_C", NULL));
2103:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitGetSubKSP_C", NULL));
2104:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBTol_C", NULL));
2105:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBMaxit_C", NULL));
2106:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBNu_C", NULL));
2107:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBDelay_C", NULL));
2108:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetSchurPre_C", NULL));
2109:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitGetSchurPre_C", NULL));
2110:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetSchurFactType_C", NULL));
2111:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetSchurScale_C", NULL));
2112:   PetscFunctionReturn(PETSC_SUCCESS);
2113: }

2115: static PetscErrorCode PCSetFromOptions_FieldSplit(PC pc, PetscOptionItems PetscOptionsObject)
2116: {
2117:   PetscInt        bs;
2118:   PetscBool       flg;
2119:   PC_FieldSplit  *jac = (PC_FieldSplit *)pc->data;
2120:   PCCompositeType ctype;

2122:   PetscFunctionBegin;
2123:   PetscOptionsHeadBegin(PetscOptionsObject, "FieldSplit options");
2124:   PetscCall(PetscOptionsBool("-pc_fieldsplit_dm_splits", "Whether to use DMCreateFieldDecomposition() for splits", "PCFieldSplitSetDMSplits", jac->dm_splits, &jac->dm_splits, NULL));
2125:   PetscCall(PetscOptionsInt("-pc_fieldsplit_block_size", "Blocksize that defines number of fields", "PCFieldSplitSetBlockSize", jac->bs, &bs, &flg));
2126:   if (flg) PetscCall(PCFieldSplitSetBlockSize(pc, bs));
2127:   jac->diag_use_amat = pc->useAmat;
2128:   PetscCall(PetscOptionsBool("-pc_fieldsplit_diag_use_amat", "Use Amat (not Pmat) to extract diagonal fieldsplit blocks", "PCFieldSplitSetDiagUseAmat", jac->diag_use_amat, &jac->diag_use_amat, NULL));
2129:   jac->offdiag_use_amat = pc->useAmat;
2130:   PetscCall(PetscOptionsBool("-pc_fieldsplit_off_diag_use_amat", "Use Amat (not Pmat) to extract off-diagonal fieldsplit blocks", "PCFieldSplitSetOffDiagUseAmat", jac->offdiag_use_amat, &jac->offdiag_use_amat, NULL));
2131:   PetscCall(PetscOptionsBool("-pc_fieldsplit_detect_saddle_point", "Form 2-way split by detecting zero diagonal entries", "PCFieldSplitSetDetectSaddlePoint", jac->detect, &jac->detect, NULL));
2132:   PetscCall(PCFieldSplitSetDetectSaddlePoint(pc, jac->detect)); /* Sets split type and Schur PC type */
2133:   PetscCall(PetscOptionsEnum("-pc_fieldsplit_type", "Type of composition", "PCFieldSplitSetType", PCCompositeTypes, (PetscEnum)jac->type, (PetscEnum *)&ctype, &flg));
2134:   if (flg) PetscCall(PCFieldSplitSetType(pc, ctype));
2135:   /* Only setup fields once */
2136:   if (jac->bs > 0 && jac->nsplits == 0) {
2137:     /* only allow user to set fields from command line.
2138:        otherwise user can set them in PCFieldSplitSetDefaults() */
2139:     PetscCall(PCFieldSplitSetRuntimeSplits_Private(pc));
2140:     if (jac->splitdefined) PetscCall(PetscInfo(pc, "Splits defined using the options database\n"));
2141:   }
2142:   if (jac->type == PC_COMPOSITE_SCHUR) {
2143:     PetscCall(PetscOptionsGetEnum(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, "-pc_fieldsplit_schur_factorization_type", PCFieldSplitSchurFactTypes, (PetscEnum *)&jac->schurfactorization, &flg));
2144:     if (flg) PetscCall(PetscInfo(pc, "Deprecated use of -pc_fieldsplit_schur_factorization_type\n"));
2145:     PetscCall(PetscOptionsEnum("-pc_fieldsplit_schur_fact_type", "Which off-diagonal parts of the block factorization to use", "PCFieldSplitSetSchurFactType", PCFieldSplitSchurFactTypes, (PetscEnum)jac->schurfactorization, (PetscEnum *)&jac->schurfactorization, NULL));
2146:     PetscCall(PetscOptionsEnum("-pc_fieldsplit_schur_precondition", "How to build preconditioner for Schur complement", "PCFieldSplitSetSchurPre", PCFieldSplitSchurPreTypes, (PetscEnum)jac->schurpre, (PetscEnum *)&jac->schurpre, NULL));
2147:     PetscCall(PetscOptionsScalar("-pc_fieldsplit_schur_scale", "Scale Schur complement", "PCFieldSplitSetSchurScale", jac->schurscale, &jac->schurscale, NULL));
2148:   } else if (jac->type == PC_COMPOSITE_GKB) {
2149:     PetscCall(PetscOptionsReal("-pc_fieldsplit_gkb_tol", "The tolerance for the lower bound stopping criterion", "PCFieldSplitSetGKBTol", jac->gkbtol, &jac->gkbtol, NULL));
2150:     PetscCall(PetscOptionsInt("-pc_fieldsplit_gkb_delay", "The delay value for lower bound criterion", "PCFieldSplitSetGKBDelay", jac->gkbdelay, &jac->gkbdelay, NULL));
2151:     PetscCall(PetscOptionsBoundedReal("-pc_fieldsplit_gkb_nu", "Parameter in augmented Lagrangian approach", "PCFieldSplitSetGKBNu", jac->gkbnu, &jac->gkbnu, NULL, 0.0));
2152:     PetscCall(PetscOptionsInt("-pc_fieldsplit_gkb_maxit", "Maximum allowed number of iterations", "PCFieldSplitSetGKBMaxit", jac->gkbmaxit, &jac->gkbmaxit, NULL));
2153:     PetscCall(PetscOptionsBool("-pc_fieldsplit_gkb_monitor", "Prints number of GKB iterations and error", "PCFieldSplitGKB", jac->gkbmonitor, &jac->gkbmonitor, NULL));
2154:   }
2155:   /*
2156:     In the initial call to this routine the sub-solver data structures do not exist so we cannot call KSPSetFromOptions() on them yet.
2157:     But after the initial setup of ALL the layers of sub-solvers is completed we do want to call KSPSetFromOptions() on the sub-solvers every time it
2158:     is called on the outer solver in case changes were made in the options database

2160:     But even after PCSetUp_FieldSplit() is called all the options inside the inner levels of sub-solvers may still not have been set thus we only call the KSPSetFromOptions()
2161:     if we know that the entire stack of sub-solvers below this have been complete instantiated, we check this by seeing if any solver iterations are complete.
2162:     Without this extra check test p2p1fetidp_olof_full and others fail with incorrect matrix types.

2164:     There could be a negative side effect of calling the KSPSetFromOptions() below.

2166:     If one captured the PetscObjectState of the options database one could skip these calls if the database has not changed from the previous call
2167:   */
2168:   if (jac->issetup) {
2169:     PC_FieldSplitLink ilink = jac->head;
2170:     if (jac->type == PC_COMPOSITE_SCHUR) {
2171:       if (jac->kspupper && jac->kspupper->totalits > 0) PetscCall(KSPSetFromOptions(jac->kspupper));
2172:       if (jac->kspschur && jac->kspschur->totalits > 0) PetscCall(KSPSetFromOptions(jac->kspschur));
2173:     }
2174:     while (ilink) {
2175:       if (ilink->ksp->totalits > 0) PetscCall(KSPSetFromOptions(ilink->ksp));
2176:       ilink = ilink->next;
2177:     }
2178:   }
2179:   PetscOptionsHeadEnd();
2180:   PetscFunctionReturn(PETSC_SUCCESS);
2181: }

2183: static PetscErrorCode PCFieldSplitSetFields_FieldSplit(PC pc, const char splitname[], PetscInt n, const PetscInt *fields, const PetscInt *fields_col)
2184: {
2185:   PC_FieldSplit    *jac = (PC_FieldSplit *)pc->data;
2186:   PC_FieldSplitLink ilink, next = jac->head;
2187:   char              prefix[128];
2188:   PetscInt          i;
2189:   PetscLogEvent     nse;

2191:   PetscFunctionBegin;
2192:   if (jac->splitdefined) {
2193:     PetscCall(PetscInfo(pc, "Ignoring new split \"%s\" because the splits have already been defined\n", splitname));
2194:     PetscFunctionReturn(PETSC_SUCCESS);
2195:   }
2196:   for (i = 0; i < n; i++) PetscCheck(fields[i] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative field %" PetscInt_FMT " requested", fields[i]);
2197:   PetscCall(PetscNew(&ilink));
2198:   if (splitname) {
2199:     PetscCall(PetscStrallocpy(splitname, &ilink->splitname));
2200:   } else {
2201:     PetscCall(PetscMalloc1(3, &ilink->splitname));
2202:     PetscCall(PetscSNPrintf(ilink->splitname, 2, "%" PetscInt_FMT, jac->nsplits));
2203:   }
2204:   PetscCall(PetscMPIIntCast(jac->nsplits, &nse));
2205:   ilink->event = jac->nsplits < 5 ? KSP_Solve_FS_0 + nse : KSP_Solve_FS_0 + 4; /* Splits greater than 4 logged in 4th split */
2206:   PetscCall(PetscMalloc1(n, &ilink->fields));
2207:   PetscCall(PetscArraycpy(ilink->fields, fields, n));
2208:   PetscCall(PetscMalloc1(n, &ilink->fields_col));
2209:   PetscCall(PetscArraycpy(ilink->fields_col, fields_col, n));

2211:   ilink->nfields = n;
2212:   ilink->next    = NULL;
2213:   PetscCall(KSPCreate(PetscObjectComm((PetscObject)pc), &ilink->ksp));
2214:   PetscCall(KSPSetNestLevel(ilink->ksp, pc->kspnestlevel));
2215:   PetscCall(KSPSetErrorIfNotConverged(ilink->ksp, pc->erroriffailure));
2216:   PetscCall(PetscObjectIncrementTabLevel((PetscObject)ilink->ksp, (PetscObject)pc, 1));
2217:   PetscCall(KSPSetType(ilink->ksp, KSPPREONLY));

2219:   PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%sfieldsplit_%s_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname));
2220:   PetscCall(KSPSetOptionsPrefix(ilink->ksp, prefix));

2222:   if (!next) {
2223:     jac->head       = ilink;
2224:     ilink->previous = NULL;
2225:   } else {
2226:     while (next->next) next = next->next;
2227:     next->next      = ilink;
2228:     ilink->previous = next;
2229:   }
2230:   jac->nsplits++;
2231:   PetscFunctionReturn(PETSC_SUCCESS);
2232: }

2234: static PetscErrorCode PCFieldSplitSchurGetSubKSP_FieldSplit(PC pc, PetscInt *n, KSP **subksp)
2235: {
2236:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;

2238:   PetscFunctionBegin;
2239:   *subksp = NULL;
2240:   if (n) *n = 0;
2241:   if (jac->type == PC_COMPOSITE_SCHUR) {
2242:     PetscInt nn;

2244:     PetscCheck(jac->schur, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Must call KSPSetUp() or PCSetUp() before calling PCFieldSplitSchurGetSubKSP()");
2245:     PetscCheck(jac->nsplits == 2, PetscObjectComm((PetscObject)pc), PETSC_ERR_PLIB, "Unexpected number of splits %" PetscInt_FMT " != 2", jac->nsplits);
2246:     nn = jac->nsplits + (jac->kspupper != jac->head->ksp ? 1 : 0);
2247:     PetscCall(PetscMalloc1(nn, subksp));
2248:     (*subksp)[0] = jac->head->ksp;
2249:     (*subksp)[1] = jac->kspschur;
2250:     if (jac->kspupper != jac->head->ksp) (*subksp)[2] = jac->kspupper;
2251:     if (n) *n = nn;
2252:   }
2253:   PetscFunctionReturn(PETSC_SUCCESS);
2254: }

2256: static PetscErrorCode PCFieldSplitGetSubKSP_FieldSplit_Schur(PC pc, PetscInt *n, KSP **subksp)
2257: {
2258:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;

2260:   PetscFunctionBegin;
2261:   PetscCheck(jac->schur, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Must call KSPSetUp() or PCSetUp() before calling PCFieldSplitGetSubKSP()");
2262:   PetscCall(PetscMalloc1(jac->nsplits, subksp));
2263:   PetscCall(MatSchurComplementGetKSP(jac->schur, *subksp));

2265:   (*subksp)[1] = jac->kspschur;
2266:   if (n) *n = jac->nsplits;
2267:   PetscFunctionReturn(PETSC_SUCCESS);
2268: }

2270: static PetscErrorCode PCFieldSplitGetSubKSP_FieldSplit(PC pc, PetscInt *n, KSP **subksp)
2271: {
2272:   PC_FieldSplit    *jac   = (PC_FieldSplit *)pc->data;
2273:   PetscInt          cnt   = 0;
2274:   PC_FieldSplitLink ilink = jac->head;

2276:   PetscFunctionBegin;
2277:   PetscCall(PetscMalloc1(jac->nsplits, subksp));
2278:   while (ilink) {
2279:     (*subksp)[cnt++] = ilink->ksp;
2280:     ilink            = ilink->next;
2281:   }
2282:   PetscCheck(cnt == jac->nsplits, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Corrupt PCFIELDSPLIT object: number of splits in linked list %" PetscInt_FMT " does not match number in object %" PetscInt_FMT, cnt, jac->nsplits);
2283:   if (n) *n = jac->nsplits;
2284:   PetscFunctionReturn(PETSC_SUCCESS);
2285: }

2287: /*@
2288:   PCFieldSplitRestrictIS - Restricts the fieldsplit `IS`s to be within a given `IS`.

2290:   Input Parameters:
2291: + pc  - the preconditioner context
2292: - isy - the index set that defines the indices to which the fieldsplit is to be restricted

2294:   Level: advanced

2296:   Developer Notes:
2297:   It seems the resulting `IS`s will not cover the entire space, so
2298:   how can they define a convergent preconditioner? Needs explaining.

2300: .seealso: [](sec_block_matrices), `PCFIELDSPLIT`, `PCFieldSplitSetFields()`, `PCFieldSplitSetIS()`
2301: @*/
2302: PetscErrorCode PCFieldSplitRestrictIS(PC pc, IS isy)
2303: {
2304:   PetscFunctionBegin;
2307:   PetscTryMethod(pc, "PCFieldSplitRestrictIS_C", (PC, IS), (pc, isy));
2308:   PetscFunctionReturn(PETSC_SUCCESS);
2309: }

2311: static PetscErrorCode PCFieldSplitRestrictIS_FieldSplit(PC pc, IS isy)
2312: {
2313:   PC_FieldSplit    *jac   = (PC_FieldSplit *)pc->data;
2314:   PC_FieldSplitLink ilink = jac->head, next;
2315:   PetscInt          localsize, size, sizez, i;
2316:   const PetscInt   *ind, *indz;
2317:   PetscInt         *indc, *indcz;
2318:   PetscBool         flg;

2320:   PetscFunctionBegin;
2321:   PetscCall(ISGetLocalSize(isy, &localsize));
2322:   PetscCallMPI(MPI_Scan(&localsize, &size, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)isy)));
2323:   size -= localsize;
2324:   while (ilink) {
2325:     IS isrl, isr;
2326:     PC subpc;
2327:     PetscCall(ISEmbed(ilink->is, isy, PETSC_TRUE, &isrl));
2328:     PetscCall(ISGetLocalSize(isrl, &localsize));
2329:     PetscCall(PetscMalloc1(localsize, &indc));
2330:     PetscCall(ISGetIndices(isrl, &ind));
2331:     PetscCall(PetscArraycpy(indc, ind, localsize));
2332:     PetscCall(ISRestoreIndices(isrl, &ind));
2333:     PetscCall(ISDestroy(&isrl));
2334:     for (i = 0; i < localsize; i++) *(indc + i) += size;
2335:     PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)isy), localsize, indc, PETSC_OWN_POINTER, &isr));
2336:     PetscCall(PetscObjectReference((PetscObject)isr));
2337:     PetscCall(ISDestroy(&ilink->is));
2338:     ilink->is = isr;
2339:     PetscCall(PetscObjectReference((PetscObject)isr));
2340:     PetscCall(ISDestroy(&ilink->is_col));
2341:     ilink->is_col = isr;
2342:     PetscCall(ISDestroy(&isr));
2343:     PetscCall(KSPGetPC(ilink->ksp, &subpc));
2344:     PetscCall(PetscObjectTypeCompare((PetscObject)subpc, PCFIELDSPLIT, &flg));
2345:     if (flg) {
2346:       IS       iszl, isz;
2347:       MPI_Comm comm;
2348:       PetscCall(ISGetLocalSize(ilink->is, &localsize));
2349:       comm = PetscObjectComm((PetscObject)ilink->is);
2350:       PetscCall(ISEmbed(isy, ilink->is, PETSC_TRUE, &iszl));
2351:       PetscCallMPI(MPI_Scan(&localsize, &sizez, 1, MPIU_INT, MPI_SUM, comm));
2352:       sizez -= localsize;
2353:       PetscCall(ISGetLocalSize(iszl, &localsize));
2354:       PetscCall(PetscMalloc1(localsize, &indcz));
2355:       PetscCall(ISGetIndices(iszl, &indz));
2356:       PetscCall(PetscArraycpy(indcz, indz, localsize));
2357:       PetscCall(ISRestoreIndices(iszl, &indz));
2358:       PetscCall(ISDestroy(&iszl));
2359:       for (i = 0; i < localsize; i++) *(indcz + i) += sizez;
2360:       PetscCall(ISCreateGeneral(comm, localsize, indcz, PETSC_OWN_POINTER, &isz));
2361:       PetscCall(PCFieldSplitRestrictIS(subpc, isz));
2362:       PetscCall(ISDestroy(&isz));
2363:     }
2364:     next  = ilink->next;
2365:     ilink = next;
2366:   }
2367:   jac->isrestrict = PETSC_TRUE;
2368:   PetscFunctionReturn(PETSC_SUCCESS);
2369: }

2371: static PetscErrorCode PCFieldSplitSetIS_FieldSplit(PC pc, const char splitname[], IS is)
2372: {
2373:   PC_FieldSplit    *jac = (PC_FieldSplit *)pc->data;
2374:   PC_FieldSplitLink ilink, next = jac->head;
2375:   char              prefix[128];
2376:   PetscLogEvent     nse;

2378:   PetscFunctionBegin;
2379:   if (jac->splitdefined) {
2380:     PetscCall(PetscInfo(pc, "Ignoring new split \"%s\" because the splits have already been defined\n", splitname));
2381:     PetscFunctionReturn(PETSC_SUCCESS);
2382:   }
2383:   PetscCall(PetscNew(&ilink));
2384:   if (splitname) {
2385:     PetscCall(PetscStrallocpy(splitname, &ilink->splitname));
2386:   } else {
2387:     PetscCall(PetscMalloc1(8, &ilink->splitname));
2388:     PetscCall(PetscSNPrintf(ilink->splitname, 7, "%" PetscInt_FMT, jac->nsplits));
2389:   }
2390:   PetscCall(PetscMPIIntCast(jac->nsplits, &nse));
2391:   ilink->event = jac->nsplits < 5 ? KSP_Solve_FS_0 + nse : KSP_Solve_FS_0 + 4; /* Splits greater than 4 logged in 4th split */
2392:   PetscCall(PetscObjectReference((PetscObject)is));
2393:   PetscCall(ISDestroy(&ilink->is));
2394:   ilink->is = is;
2395:   PetscCall(PetscObjectReference((PetscObject)is));
2396:   PetscCall(ISDestroy(&ilink->is_col));
2397:   ilink->is_col = is;
2398:   ilink->next   = NULL;
2399:   PetscCall(KSPCreate(PetscObjectComm((PetscObject)pc), &ilink->ksp));
2400:   PetscCall(KSPSetNestLevel(ilink->ksp, pc->kspnestlevel));
2401:   PetscCall(KSPSetErrorIfNotConverged(ilink->ksp, pc->erroriffailure));
2402:   PetscCall(PetscObjectIncrementTabLevel((PetscObject)ilink->ksp, (PetscObject)pc, 1));
2403:   PetscCall(KSPSetType(ilink->ksp, KSPPREONLY));

2405:   PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "%sfieldsplit_%s_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname));
2406:   PetscCall(KSPSetOptionsPrefix(ilink->ksp, prefix));

2408:   if (!next) {
2409:     jac->head       = ilink;
2410:     ilink->previous = NULL;
2411:   } else {
2412:     while (next->next) next = next->next;
2413:     next->next      = ilink;
2414:     ilink->previous = next;
2415:   }
2416:   jac->nsplits++;
2417:   PetscFunctionReturn(PETSC_SUCCESS);
2418: }

2420: /*@
2421:   PCFieldSplitSetFields - Sets the fields that define one particular split in `PCFIELDSPLIT`

2423:   Logically Collective

2425:   Input Parameters:
2426: + pc         - the preconditioner context
2427: . splitname  - name of this split, if `NULL` the number of the split is used
2428: . n          - the number of fields in this split
2429: . fields     - the fields in this split
2430: - fields_col - generally the same as `fields`, if it does not match `fields` then the submatrix that is solved for this set of fields comes from an off-diagonal block
2431:                of the matrix and `fields_col` provides the column indices for that block

2433:   Options Database Key:
2434: . -pc_fieldsplit_%d_fields a,b,... - indicates the fields to be used in the `%d`'th split

2436:   Level: intermediate

2438:   Notes:
2439:   Use `PCFieldSplitSetIS()` to set a  general set of indices as a split.

2441:   If the matrix used to construct the preconditioner is `MATNEST` then field i refers to the `is_row[i]` `IS` passed to `MatCreateNest()`.

2443:   If the matrix used to construct the preconditioner is not `MATNEST` then
2444:   `PCFieldSplitSetFields()` is for defining fields as strided blocks (based on the block size provided to the matrix with `MatSetBlockSize()` or
2445:   to the `PC` with `PCFieldSplitSetBlockSize()`). For example, if the block
2446:   size is three then one can define a split as 0, or 1 or 2 or 0,1 or 0,2 or 1,2 which mean
2447:   0xx3xx6xx9xx12 ... x1xx4xx7xx ... xx2xx5xx8xx.. 01x34x67x... 0x23x56x8.. x12x45x78x....
2448:   where the numbered entries indicate what is in the split.

2450:   This function is called once per split (it creates a new split each time).  Solve options
2451:   for this split will be available under the prefix `-fieldsplit_SPLITNAME_`.

2453:   `PCFieldSplitSetIS()` does not support having a `fields_col` different from `fields`

2455:   Developer Notes:
2456:   This routine does not actually create the `IS` representing the split, that is delayed
2457:   until `PCSetUp_FieldSplit()`, because information about the vector/matrix layouts may not be
2458:   available when this routine is called.

2460: .seealso: [](sec_block_matrices), `PC`, `PCFieldSplitGetSubKSP()`, `PCFIELDSPLIT`, `PCFieldSplitSetBlockSize()`, `PCFieldSplitSetIS()`, `PCFieldSplitRestrictIS()`,
2461:           `MatSetBlockSize()`, `MatCreateNest()`
2462: @*/
2463: PetscErrorCode PCFieldSplitSetFields(PC pc, const char splitname[], PetscInt n, const PetscInt fields[], const PetscInt fields_col[])
2464: {
2465:   PetscFunctionBegin;
2467:   PetscAssertPointer(splitname, 2);
2468:   PetscCheck(n >= 1, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Provided number of fields %" PetscInt_FMT " in split \"%s\" not positive", n, splitname);
2469:   PetscAssertPointer(fields, 4);
2470:   PetscTryMethod(pc, "PCFieldSplitSetFields_C", (PC, const char[], PetscInt, const PetscInt *, const PetscInt *), (pc, splitname, n, fields, fields_col));
2471:   PetscFunctionReturn(PETSC_SUCCESS);
2472: }

2474: /*@
2475:   PCFieldSplitSetDiagUseAmat - set flag indicating whether to extract diagonal blocks from Amat (rather than Pmat) to build
2476:   the sub-matrices associated with each split. Where `KSPSetOperators`(ksp,Amat,Pmat) was used to supply the operators.

2478:   Logically Collective

2480:   Input Parameters:
2481: + pc  - the preconditioner object
2482: - flg - boolean flag indicating whether or not to use Amat to extract the diagonal blocks from

2484:   Options Database Key:
2485: . -pc_fieldsplit_diag_use_amat - use the Amat to provide the diagonal blocks

2487:   Level: intermediate

2489: .seealso: [](sec_block_matrices), `PC`, `PCSetOperators()`, `KSPSetOperators()`, `PCFieldSplitGetDiagUseAmat()`, `PCFieldSplitSetOffDiagUseAmat()`, `PCFIELDSPLIT`
2490: @*/
2491: PetscErrorCode PCFieldSplitSetDiagUseAmat(PC pc, PetscBool flg)
2492: {
2493:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
2494:   PetscBool      isfs;

2496:   PetscFunctionBegin;
2498:   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCFIELDSPLIT, &isfs));
2499:   PetscCheck(isfs, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "PC not of type %s", PCFIELDSPLIT);
2500:   jac->diag_use_amat = flg;
2501:   PetscFunctionReturn(PETSC_SUCCESS);
2502: }

2504: /*@
2505:   PCFieldSplitGetDiagUseAmat - get the flag indicating whether to extract diagonal blocks from Amat (rather than Pmat) to build
2506:   the sub-matrices associated with each split.  Where `KSPSetOperators`(ksp,Amat,Pmat) was used to supply the operators.

2508:   Logically Collective

2510:   Input Parameter:
2511: . pc - the preconditioner object

2513:   Output Parameter:
2514: . flg - boolean flag indicating whether or not to use Amat to extract the diagonal blocks from

2516:   Level: intermediate

2518: .seealso: [](sec_block_matrices), `PC`, `PCSetOperators()`, `KSPSetOperators()`, `PCFieldSplitSetDiagUseAmat()`, `PCFieldSplitGetOffDiagUseAmat()`, `PCFIELDSPLIT`
2519: @*/
2520: PetscErrorCode PCFieldSplitGetDiagUseAmat(PC pc, PetscBool *flg)
2521: {
2522:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
2523:   PetscBool      isfs;

2525:   PetscFunctionBegin;
2527:   PetscAssertPointer(flg, 2);
2528:   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCFIELDSPLIT, &isfs));
2529:   PetscCheck(isfs, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "PC not of type %s", PCFIELDSPLIT);
2530:   *flg = jac->diag_use_amat;
2531:   PetscFunctionReturn(PETSC_SUCCESS);
2532: }

2534: /*@
2535:   PCFieldSplitSetOffDiagUseAmat - set flag indicating whether to extract off-diagonal blocks from Amat (rather than Pmat) to build
2536:   the sub-matrices associated with each split.  Where `KSPSetOperators`(ksp,Amat,Pmat) was used to supply the operators.

2538:   Logically Collective

2540:   Input Parameters:
2541: + pc  - the preconditioner object
2542: - flg - boolean flag indicating whether or not to use Amat to extract the off-diagonal blocks from

2544:   Options Database Key:
2545: . -pc_fieldsplit_off_diag_use_amat (true|false) - use the Amat to extract the off-diagonal blocks

2547:   Level: intermediate

2549: .seealso: [](sec_block_matrices), `PC`, `PCSetOperators()`, `KSPSetOperators()`, `PCFieldSplitGetOffDiagUseAmat()`, `PCFieldSplitSetDiagUseAmat()`, `PCFIELDSPLIT`
2550: @*/
2551: PetscErrorCode PCFieldSplitSetOffDiagUseAmat(PC pc, PetscBool flg)
2552: {
2553:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
2554:   PetscBool      isfs;

2556:   PetscFunctionBegin;
2558:   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCFIELDSPLIT, &isfs));
2559:   PetscCheck(isfs, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "PC not of type %s", PCFIELDSPLIT);
2560:   jac->offdiag_use_amat = flg;
2561:   PetscFunctionReturn(PETSC_SUCCESS);
2562: }

2564: /*@
2565:   PCFieldSplitGetOffDiagUseAmat - get the flag indicating whether to extract off-diagonal blocks from Amat (rather than Pmat) to build
2566:   the sub-matrices associated with each split.  Where `KSPSetOperators`(ksp,Amat,Pmat) was used to supply the operators.

2568:   Logically Collective

2570:   Input Parameter:
2571: . pc - the preconditioner object

2573:   Output Parameter:
2574: . flg - boolean flag indicating whether or not to use Amat to extract the off-diagonal blocks from

2576:   Level: intermediate

2578: .seealso: [](sec_block_matrices), `PC`, `PCSetOperators()`, `KSPSetOperators()`, `PCFieldSplitSetOffDiagUseAmat()`, `PCFieldSplitGetDiagUseAmat()`, `PCFIELDSPLIT`
2579: @*/
2580: PetscErrorCode PCFieldSplitGetOffDiagUseAmat(PC pc, PetscBool *flg)
2581: {
2582:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
2583:   PetscBool      isfs;

2585:   PetscFunctionBegin;
2587:   PetscAssertPointer(flg, 2);
2588:   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCFIELDSPLIT, &isfs));
2589:   PetscCheck(isfs, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "PC not of type %s", PCFIELDSPLIT);
2590:   *flg = jac->offdiag_use_amat;
2591:   PetscFunctionReturn(PETSC_SUCCESS);
2592: }

2594: /*@
2595:   PCFieldSplitSetIS - Sets the exact elements for a split in a `PCFIELDSPLIT`

2597:   Logically Collective

2599:   Input Parameters:
2600: + pc        - the preconditioner context
2601: . splitname - name of this split, if `NULL` the number of the split is used
2602: - is        - the index set that defines the elements in this split

2604:   Level: intermediate

2606:   Notes:
2607:   Use `PCFieldSplitSetFields()`, for splits defined by strided `IS` based on the matrix block size or the `is_rows[]` passed into `MATNEST`

2609:   This function is called once per split (it creates a new split each time).  Solve options
2610:   for this split will be available under the prefix -fieldsplit_SPLITNAME_.

2612: .seealso: [](sec_block_matrices), `PC`, `PCFieldSplitGetSubKSP()`, `PCFIELDSPLIT`, `PCFieldSplitSetBlockSize()`, `PCFieldSplitSetFields()`
2613: @*/
2614: PetscErrorCode PCFieldSplitSetIS(PC pc, const char splitname[], IS is)
2615: {
2616:   PetscFunctionBegin;
2618:   if (splitname) PetscAssertPointer(splitname, 2);
2620:   PetscTryMethod(pc, "PCFieldSplitSetIS_C", (PC, const char[], IS), (pc, splitname, is));
2621:   PetscFunctionReturn(PETSC_SUCCESS);
2622: }

2624: /*@
2625:   PCFieldSplitGetIS - Retrieves the elements for a split as an `IS`

2627:   Logically Collective

2629:   Input Parameters:
2630: + pc        - the preconditioner context
2631: - splitname - name of this split

2633:   Output Parameter:
2634: . is - the index set that defines the elements in this split, or `NULL` if the split is not found

2636:   Level: intermediate

2638: .seealso: [](sec_block_matrices), `PC`, `PCFieldSplitGetSubKSP()`, `PCFIELDSPLIT`, `PCFieldSplitSetIS()`, `PCFieldSplitGetISByIndex()`
2639: @*/
2640: PetscErrorCode PCFieldSplitGetIS(PC pc, const char splitname[], IS *is)
2641: {
2642:   PetscFunctionBegin;
2644:   PetscAssertPointer(splitname, 2);
2645:   PetscAssertPointer(is, 3);
2646:   {
2647:     PC_FieldSplit    *jac   = (PC_FieldSplit *)pc->data;
2648:     PC_FieldSplitLink ilink = jac->head;
2649:     PetscBool         found;

2651:     *is = NULL;
2652:     while (ilink) {
2653:       PetscCall(PetscStrcmp(ilink->splitname, splitname, &found));
2654:       if (found) {
2655:         *is = ilink->is;
2656:         break;
2657:       }
2658:       ilink = ilink->next;
2659:     }
2660:   }
2661:   PetscFunctionReturn(PETSC_SUCCESS);
2662: }

2664: /*@
2665:   PCFieldSplitGetISByIndex - Retrieves the elements for a given split as an `IS`

2667:   Logically Collective

2669:   Input Parameters:
2670: + pc    - the preconditioner context
2671: - index - index of this split

2673:   Output Parameter:
2674: . is - the index set that defines the elements in this split

2676:   Level: intermediate

2678: .seealso: [](sec_block_matrices), `PC`, `PCFieldSplitGetSubKSP()`, `PCFIELDSPLIT`, `PCFieldSplitGetIS()`, `PCFieldSplitSetIS()`
2679: @*/
2680: PetscErrorCode PCFieldSplitGetISByIndex(PC pc, PetscInt index, IS *is)
2681: {
2682:   PetscFunctionBegin;
2683:   PetscCheck(index >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative field %" PetscInt_FMT " requested", index);
2685:   PetscAssertPointer(is, 3);
2686:   {
2687:     PC_FieldSplit    *jac   = (PC_FieldSplit *)pc->data;
2688:     PC_FieldSplitLink ilink = jac->head;
2689:     PetscInt          i     = 0;
2690:     PetscCheck(index < jac->nsplits, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field %" PetscInt_FMT " requested but only %" PetscInt_FMT " exist", index, jac->nsplits);

2692:     while (i < index) {
2693:       ilink = ilink->next;
2694:       ++i;
2695:     }
2696:     PetscCall(PCFieldSplitGetIS(pc, ilink->splitname, is));
2697:   }
2698:   PetscFunctionReturn(PETSC_SUCCESS);
2699: }

2701: /*@
2702:   PCFieldSplitSetBlockSize - Sets the block size for defining where fields start in the
2703:   fieldsplit preconditioner when calling `PCFieldSplitSetFields()`. If not set the matrix block size is used.

2705:   Logically Collective

2707:   Input Parameters:
2708: + pc - the preconditioner context
2709: - bs - the block size

2711:   Level: intermediate

2713:   Note:
2714:   If the matrix is a `MATNEST` then the `is_rows[]` passed to `MatCreateNest()` determines the fields.

2716: .seealso: [](sec_block_matrices), `PC`, `PCFieldSplitGetSubKSP()`, `PCFIELDSPLIT`, `PCFieldSplitSetFields()`, `PCFieldSplitSetIS()`
2717: @*/
2718: PetscErrorCode PCFieldSplitSetBlockSize(PC pc, PetscInt bs)
2719: {
2720:   PetscFunctionBegin;
2723:   PetscTryMethod(pc, "PCFieldSplitSetBlockSize_C", (PC, PetscInt), (pc, bs));
2724:   PetscFunctionReturn(PETSC_SUCCESS);
2725: }

2727: /*@
2728:   PCFieldSplitGetSubKSP - Gets the `KSP` contexts for all splits

2730:   Collective

2732:   Input Parameter:
2733: . pc - the preconditioner context

2735:   Output Parameters:
2736: + n      - the number of splits
2737: - subksp - the array of `KSP` contexts

2739:   Level: advanced

2741:   Notes:
2742:   After `PCFieldSplitGetSubKSP()` the array of `KSP`s is to be freed by the user with `PetscFree()`
2743:   (not the `KSP`, just the array that contains them).

2745:   You must call `PCSetUp()` before calling `PCFieldSplitGetSubKSP()`.

2747:   If the fieldsplit is of type `PC_COMPOSITE_SCHUR`, it returns the `KSP` object used inside the
2748:   Schur complement and the `KSP` object used to iterate over the Schur complement.
2749:   To access all the `KSP` objects used in `PC_COMPOSITE_SCHUR`, use `PCFieldSplitSchurGetSubKSP()`.

2751:   If the fieldsplit is of type `PC_COMPOSITE_GKB`, it returns the `KSP` object used to solve the
2752:   inner linear system defined by the matrix H in each loop.

2754:   Fortran Note:
2755:   Call `PCFieldSplitRestoreSubKSP()` when the array of `KSP` is no longer needed

2757:   Developer Notes:
2758:   There should be a `PCFieldSplitRestoreSubKSP()` instead of requiring the user to call `PetscFree()`

2760: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitSetFields()`, `PCFieldSplitSetIS()`, `PCFieldSplitSchurGetSubKSP()`
2761: @*/
2762: PetscErrorCode PCFieldSplitGetSubKSP(PC pc, PetscInt *n, KSP *subksp[])
2763: {
2764:   PetscFunctionBegin;
2766:   if (n) PetscAssertPointer(n, 2);
2767:   PetscUseMethod(pc, "PCFieldSplitGetSubKSP_C", (PC, PetscInt *, KSP **), (pc, n, subksp));
2768:   PetscFunctionReturn(PETSC_SUCCESS);
2769: }

2771: /*@
2772:   PCFieldSplitSchurGetSubKSP - Gets the `KSP` contexts used inside the Schur complement based `PCFIELDSPLIT`

2774:   Collective

2776:   Input Parameter:
2777: . pc - the preconditioner context

2779:   Output Parameters:
2780: + n      - the number of splits
2781: - subksp - the array of `KSP` contexts

2783:   Level: advanced

2785:   Notes:
2786:   After `PCFieldSplitSchurGetSubKSP()` the array of `KSP`s is to be freed by the user with `PetscFree()`
2787:   (not the `KSP` just the array that contains them).

2789:   You must call `PCSetUp()` before calling `PCFieldSplitSchurGetSubKSP()`.

2791:   If the fieldsplit type is of type `PC_COMPOSITE_SCHUR`, it returns (in order)
2792: +  1  - the `KSP` used for the (1,1) block
2793: .  2  - the `KSP` used for the Schur complement (not the one used for the interior Schur solver)
2794: -  3  - the `KSP` used for the (1,1) block in the upper triangular factor (if different from that of the (1,1) block).

2796:   It returns a null array if the fieldsplit is not of type `PC_COMPOSITE_SCHUR`; in this case, you should use `PCFieldSplitGetSubKSP()`.

2798:   Fortran Note:
2799:   Call `PCFieldSplitSchurRestoreSubKSP()` when the array of `KSP` is no longer needed

2801:   Developer Notes:
2802:   There should be a `PCFieldSplitRestoreSubKSP()` instead of requiring the user to call `PetscFree()`

2804:   Should the functionality of `PCFieldSplitSchurGetSubKSP()` and `PCFieldSplitGetSubKSP()` be merged?

2806: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitSetFields()`, `PCFieldSplitSetIS()`, `PCFieldSplitGetSubKSP()`
2807: @*/
2808: PetscErrorCode PCFieldSplitSchurGetSubKSP(PC pc, PetscInt *n, KSP *subksp[])
2809: {
2810:   PetscFunctionBegin;
2812:   if (n) PetscAssertPointer(n, 2);
2813:   PetscUseMethod(pc, "PCFieldSplitSchurGetSubKSP_C", (PC, PetscInt *, KSP **), (pc, n, subksp));
2814:   PetscFunctionReturn(PETSC_SUCCESS);
2815: }

2817: /*@
2818:   PCFieldSplitSetSchurPre -  Indicates from what operator the preconditioner is constructed for the Schur complement.
2819:   The default is the A11 matrix.

2821:   Collective

2823:   Input Parameters:
2824: + pc    - the preconditioner context
2825: . ptype - which matrix to use for preconditioning the Schur complement: `PC_FIELDSPLIT_SCHUR_PRE_A11` (default),
2826:               `PC_FIELDSPLIT_SCHUR_PRE_SELF`, `PC_FIELDSPLIT_SCHUR_PRE_USER`,
2827:               `PC_FIELDSPLIT_SCHUR_PRE_SELFP`, and `PC_FIELDSPLIT_SCHUR_PRE_FULL`
2828: - pre   - matrix to use for preconditioning, or `NULL`

2830:   Options Database Keys:
2831: + -pc_fieldsplit_schur_precondition (self|selfp|user|a11|full) - default is `a11`. See notes for meaning of various arguments
2832: - -fieldsplit_1_pc_type pctype                                 - the preconditioner algorithm that is used to construct the preconditioner from the operator

2834:   Level: intermediate

2836:   Notes:
2837:   If ptype is
2838: +     a11 - the preconditioner for the Schur complement is generated from the block diagonal part of the preconditioner
2839:   matrix associated with the Schur complement (i.e. A11), not the Schur complement matrix
2840: .     self - the preconditioner for the Schur complement is generated from the symbolic representation of the Schur complement matrix:
2841:   The only preconditioners that currently work with this symbolic representation matrix object are `PCLSC` and `PCHPDDM`
2842: .     user - the preconditioner for the Schur complement is generated from the user provided matrix (pre argument
2843:   to this function).
2844: .     selfp - the preconditioning for the Schur complement is generated from an explicitly-assembled approximation $ Sp = A11 - A10 inv(diag(A00)) A01 $
2845:   This is only a good preconditioner when diag(A00) is a good preconditioner for A00. Optionally, A00 can be
2846:   lumped before extracting the diagonal using the additional option `-fieldsplit_1_mat_schur_complement_ainv_type lump`
2847: -     full - the preconditioner for the Schur complement is generated from the exact Schur complement matrix representation
2848:   computed internally by `PCFIELDSPLIT` (this is expensive)
2849:   useful mostly as a test that the Schur complement approach can work for your problem

2851:   When solving a saddle point problem, where the A11 block is identically zero, using `a11` as the ptype only makes sense
2852:   with the additional option `-fieldsplit_1_pc_type none`. Usually for saddle point problems one would use a `ptype` of `self` and
2853:   `-fieldsplit_1_pc_type lsc` which uses the least squares commutator to compute a preconditioner for the Schur complement.

2855:   Developer Note:
2856:   The name of this function and the option `-pc_fieldsplit_schur_precondition` are inconsistent; precondition should be used everywhere.

2858: .seealso: [](sec_block_matrices), `PC`, `PCFieldSplitGetSchurPre()`, `PCFieldSplitGetSubKSP()`, `PCFIELDSPLIT`, `PCFieldSplitSetFields()`, `PCFieldSplitSchurPreType`,
2859:           `MatSchurComplementSetAinvType()`, `PCLSC`, `PCFieldSplitSetSchurFactType()`
2860: @*/
2861: PetscErrorCode PCFieldSplitSetSchurPre(PC pc, PCFieldSplitSchurPreType ptype, Mat pre)
2862: {
2863:   PetscFunctionBegin;
2865:   PetscTryMethod(pc, "PCFieldSplitSetSchurPre_C", (PC, PCFieldSplitSchurPreType, Mat), (pc, ptype, pre));
2866:   PetscFunctionReturn(PETSC_SUCCESS);
2867: }

2869: PetscErrorCode PCFieldSplitSchurPrecondition(PC pc, PCFieldSplitSchurPreType ptype, Mat pre)
2870: {
2871:   return PCFieldSplitSetSchurPre(pc, ptype, pre);
2872: } /* Deprecated name */

2874: /*@
2875:   PCFieldSplitGetSchurPre - For Schur complement fieldsplit, determine how the Schur complement will be
2876:   preconditioned.  See `PCFieldSplitSetSchurPre()` for details.

2878:   Logically Collective

2880:   Input Parameter:
2881: . pc - the preconditioner context

2883:   Output Parameters:
2884: + ptype - which matrix to use for preconditioning the Schur complement: `PC_FIELDSPLIT_SCHUR_PRE_A11`, `PC_FIELDSPLIT_SCHUR_PRE_SELF`, `PC_FIELDSPLIT_SCHUR_PRE_USER`
2885: - pre   - matrix to use for preconditioning (with `PC_FIELDSPLIT_SCHUR_PRE_USER`), or `NULL`

2887:   Level: intermediate

2889: .seealso: [](sec_block_matrices), `PC`, `PCFieldSplitSetSchurPre()`, `PCFieldSplitGetSubKSP()`, `PCFIELDSPLIT`, `PCFieldSplitSetFields()`, `PCFieldSplitSchurPreType`, `PCLSC`
2890: @*/
2891: PetscErrorCode PCFieldSplitGetSchurPre(PC pc, PCFieldSplitSchurPreType *ptype, Mat *pre)
2892: {
2893:   PetscFunctionBegin;
2895:   PetscUseMethod(pc, "PCFieldSplitGetSchurPre_C", (PC, PCFieldSplitSchurPreType *, Mat *), (pc, ptype, pre));
2896:   PetscFunctionReturn(PETSC_SUCCESS);
2897: }

2899: /*@
2900:   PCFieldSplitSchurGetS -  extract the `MATSCHURCOMPLEMENT` object used by this `PCFIELDSPLIT` in case it needs to be configured separately

2902:   Not Collective

2904:   Input Parameter:
2905: . pc - the preconditioner context

2907:   Output Parameter:
2908: . S - the Schur complement matrix

2910:   Level: advanced

2912:   Note:
2913:   This matrix should not be destroyed using `MatDestroy()`; rather, use `PCFieldSplitSchurRestoreS()`.

2915: .seealso: [](sec_block_matrices), `PC`, `PCFieldSplitGetSubKSP()`, `PCFIELDSPLIT`, `PCFieldSplitSchurPreType`, `PCFieldSplitSetSchurPre()`, `MATSCHURCOMPLEMENT`, `PCFieldSplitSchurRestoreS()`,
2916:           `MatCreateSchurComplement()`, `MatSchurComplementGetKSP()`, `MatSchurComplementComputeExplicitOperator()`, `MatGetSchurComplement()`
2917: @*/
2918: PetscErrorCode PCFieldSplitSchurGetS(PC pc, Mat *S)
2919: {
2920:   const char    *t;
2921:   PetscBool      isfs;
2922:   PC_FieldSplit *jac;

2924:   PetscFunctionBegin;
2926:   PetscCall(PetscObjectGetType((PetscObject)pc, &t));
2927:   PetscCall(PetscStrcmp(t, PCFIELDSPLIT, &isfs));
2928:   PetscCheck(isfs, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Expected PC of type PCFIELDSPLIT, got %s instead", t);
2929:   jac = (PC_FieldSplit *)pc->data;
2930:   PetscCheck(jac->type == PC_COMPOSITE_SCHUR, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Expected PCFIELDSPLIT of type SCHUR, got %d instead", jac->type);
2931:   if (S) *S = jac->schur;
2932:   PetscFunctionReturn(PETSC_SUCCESS);
2933: }

2935: /*@
2936:   PCFieldSplitSchurRestoreS -  returns the `MATSCHURCOMPLEMENT` matrix used by this `PC`

2938:   Not Collective

2940:   Input Parameters:
2941: + pc - the preconditioner context
2942: - S  - the Schur complement matrix

2944:   Level: advanced

2946: .seealso: [](sec_block_matrices), `PC`, `PCFieldSplitGetSubKSP()`, `PCFIELDSPLIT`, `PCFieldSplitSchurPreType`, `PCFieldSplitSetSchurPre()`, `MatSchurComplement`, `PCFieldSplitSchurGetS()`
2947: @*/
2948: PetscErrorCode PCFieldSplitSchurRestoreS(PC pc, Mat *S)
2949: {
2950:   const char    *t;
2951:   PetscBool      isfs;
2952:   PC_FieldSplit *jac;

2954:   PetscFunctionBegin;
2956:   PetscCall(PetscObjectGetType((PetscObject)pc, &t));
2957:   PetscCall(PetscStrcmp(t, PCFIELDSPLIT, &isfs));
2958:   PetscCheck(isfs, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Expected PC of type PCFIELDSPLIT, got %s instead", t);
2959:   jac = (PC_FieldSplit *)pc->data;
2960:   PetscCheck(jac->type == PC_COMPOSITE_SCHUR, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Expected PCFIELDSPLIT of type SCHUR, got %d instead", jac->type);
2961:   PetscCheck(S && (*S == jac->schur), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "MatSchurComplement restored is not the same as gotten");
2962:   PetscFunctionReturn(PETSC_SUCCESS);
2963: }

2965: static PetscErrorCode PCFieldSplitSetSchurPre_FieldSplit(PC pc, PCFieldSplitSchurPreType ptype, Mat pre)
2966: {
2967:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;

2969:   PetscFunctionBegin;
2970:   jac->schurpre = ptype;
2971:   if (ptype == PC_FIELDSPLIT_SCHUR_PRE_USER && pre) {
2972:     PetscCall(MatDestroy(&jac->schur_user));
2973:     jac->schur_user = pre;
2974:     PetscCall(PetscObjectReference((PetscObject)jac->schur_user));
2975:   }
2976:   PetscFunctionReturn(PETSC_SUCCESS);
2977: }

2979: static PetscErrorCode PCFieldSplitGetSchurPre_FieldSplit(PC pc, PCFieldSplitSchurPreType *ptype, Mat *pre)
2980: {
2981:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;

2983:   PetscFunctionBegin;
2984:   if (ptype) *ptype = jac->schurpre;
2985:   if (pre) *pre = jac->schur_user;
2986:   PetscFunctionReturn(PETSC_SUCCESS);
2987: }

2989: /*@
2990:   PCFieldSplitSetSchurFactType -  sets which blocks of the approximate block factorization to retain in the preconditioner {cite}`murphy2000note` and {cite}`ipsen2001note`

2992:   Collective

2994:   Input Parameters:
2995: + pc    - the preconditioner context
2996: - ftype - which blocks of factorization to retain, `PC_FIELDSPLIT_SCHUR_FACT_FULL` is default

2998:   Options Database Key:
2999: . -pc_fieldsplit_schur_fact_type (diag|lower|upper|full) - default is `full`

3001:   Level: intermediate

3003:   Notes:
3004:   The `full` factorization is

3006:   ```{math}
3007:   \left(\begin{array}{cc} A & B \\
3008:   C & E \\
3009:   \end{array}\right) =
3010:   \left(\begin{array}{cc} I & 0 \\
3011:   C A^{-1} & I \\
3012:   \end{array}\right)
3013:   \left(\begin{array}{cc} A & 0 \\
3014:   0 & S \\
3015:   \end{array}\right)
3016:   \left(\begin{array}{cc} I & A^{-1}B \\
3017:   0 & I \\
3018:   \end{array}\right) = L D U,
3019:   ```

3021:   where $ S = E - C A^{-1} B $. In practice, the full factorization is applied via block triangular solves with the grouping $L(DU)$. `upper` uses $DU$, `lower` uses $LD$,
3022:   and `diag` is the diagonal part with the sign of $S$ flipped (because this makes the preconditioner positive definite for many formulations,
3023:   thus allowing the use of `KSPMINRES)`. Sign flipping of $S$ can be turned off with `PCFieldSplitSetSchurScale()`.

3025:   If $A$ and $S$ are solved exactly
3026: +  1 - `full` factorization is a direct solver.
3027: .  2 - The preconditioned operator with `lower` or `upper` has all eigenvalues equal to 1 and minimal polynomial of degree 2, so `KSPGMRES` converges in 2 iterations.
3028: -  3 - With `diag`, the preconditioned operator has three distinct nonzero eigenvalues and minimal polynomial of degree at most 4, so `KSPGMRES` converges in at most 4 iterations.

3030:   If the iteration count is very low, consider using `KSPFGMRES` or `KSPGCR` which can use one less preconditioner
3031:   application in this case. Note that the preconditioned operator may be highly non-normal, so such fast convergence may not be observed in practice.

3033:   For symmetric problems in which $A$ is positive definite and $S$ is negative definite, `diag` can be used with `KSPMINRES`.

3035:   A flexible method like `KSPFGMRES` or `KSPGCR`, [](sec_flexibleksp), must be used if the fieldsplit preconditioner is nonlinear (e.g., a few iterations of a Krylov method is used to solve with $A$ or $S$).

3037: .seealso: [](sec_block_matrices), `PC`, `PCFieldSplitGetSubKSP()`, `PCFIELDSPLIT`, `PCFieldSplitSetFields()`, `PCFieldSplitSchurPreType`, `PCFieldSplitSetSchurScale()`,
3038:           [](sec_flexibleksp), `PCFieldSplitSetSchurPre()`
3039: @*/
3040: PetscErrorCode PCFieldSplitSetSchurFactType(PC pc, PCFieldSplitSchurFactType ftype)
3041: {
3042:   PetscFunctionBegin;
3044:   PetscTryMethod(pc, "PCFieldSplitSetSchurFactType_C", (PC, PCFieldSplitSchurFactType), (pc, ftype));
3045:   PetscFunctionReturn(PETSC_SUCCESS);
3046: }

3048: static PetscErrorCode PCFieldSplitSetSchurFactType_FieldSplit(PC pc, PCFieldSplitSchurFactType ftype)
3049: {
3050:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;

3052:   PetscFunctionBegin;
3053:   jac->schurfactorization = ftype;
3054:   PetscFunctionReturn(PETSC_SUCCESS);
3055: }

3057: /*@
3058:   PCFieldSplitSetSchurScale -  Controls the sign flip of S for `PC_FIELDSPLIT_SCHUR_FACT_DIAG`.

3060:   Collective

3062:   Input Parameters:
3063: + pc    - the preconditioner context
3064: - scale - scaling factor for the Schur complement

3066:   Options Database Key:
3067: . -pc_fieldsplit_schur_scale scale - default is -1.0

3069:   Level: intermediate

3071: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitSetFields()`, `PCFieldSplitSchurFactType`, `PCFieldSplitSetSchurFactType()`
3072: @*/
3073: PetscErrorCode PCFieldSplitSetSchurScale(PC pc, PetscScalar scale)
3074: {
3075:   PetscFunctionBegin;
3078:   PetscTryMethod(pc, "PCFieldSplitSetSchurScale_C", (PC, PetscScalar), (pc, scale));
3079:   PetscFunctionReturn(PETSC_SUCCESS);
3080: }

3082: static PetscErrorCode PCFieldSplitSetSchurScale_FieldSplit(PC pc, PetscScalar scale)
3083: {
3084:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;

3086:   PetscFunctionBegin;
3087:   jac->schurscale = scale;
3088:   PetscFunctionReturn(PETSC_SUCCESS);
3089: }

3091: /*@
3092:   PCFieldSplitGetSchurBlocks - Gets all matrix blocks for the Schur complement

3094:   Collective

3096:   Input Parameter:
3097: . pc - the preconditioner context

3099:   Output Parameters:
3100: + A00 - the (0,0) block
3101: . A01 - the (0,1) block
3102: . A10 - the (1,0) block
3103: - A11 - the (1,1) block

3105:   Level: advanced

3107:   Note:
3108:   Use `NULL` for any unneeded output arguments

3110: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `MatSchurComplementGetSubMatrices()`, `MatSchurComplementSetSubMatrices()`
3111: @*/
3112: PetscErrorCode PCFieldSplitGetSchurBlocks(PC pc, Mat *A00, Mat *A01, Mat *A10, Mat *A11)
3113: {
3114:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;

3116:   PetscFunctionBegin;
3118:   PetscCheck(jac->type == PC_COMPOSITE_SCHUR, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONG, "FieldSplit is not using a Schur complement approach.");
3119:   if (A00) *A00 = jac->pmat[0];
3120:   if (A01) *A01 = jac->B;
3121:   if (A10) *A10 = jac->C;
3122:   if (A11) *A11 = jac->pmat[1];
3123:   PetscFunctionReturn(PETSC_SUCCESS);
3124: }

3126: /*@
3127:   PCFieldSplitSetGKBTol -  Sets the solver tolerance for the generalized Golub-Kahan bidiagonalization preconditioner {cite}`arioli2013` in `PCFIELDSPLIT`

3129:   Collective

3131:   Input Parameters:
3132: + pc        - the preconditioner context
3133: - tolerance - the solver tolerance

3135:   Options Database Key:
3136: . -pc_fieldsplit_gkb_tol tolerance - default is 1e-5

3138:   Level: intermediate

3140:   Note:
3141:   The generalized GKB algorithm {cite}`arioli2013` uses a lower bound estimate of the error in energy norm as stopping criterion.
3142:   It stops once the lower bound estimate undershoots the required solver tolerance. Although the actual error might be bigger than
3143:   this estimate, the stopping criterion is satisfactory in practical cases.

3145: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitSetGKBDelay()`, `PCFieldSplitSetGKBNu()`, `PCFieldSplitSetGKBMaxit()`
3146: @*/
3147: PetscErrorCode PCFieldSplitSetGKBTol(PC pc, PetscReal tolerance)
3148: {
3149:   PetscFunctionBegin;
3152:   PetscTryMethod(pc, "PCFieldSplitSetGKBTol_C", (PC, PetscReal), (pc, tolerance));
3153:   PetscFunctionReturn(PETSC_SUCCESS);
3154: }

3156: static PetscErrorCode PCFieldSplitSetGKBTol_FieldSplit(PC pc, PetscReal tolerance)
3157: {
3158:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;

3160:   PetscFunctionBegin;
3161:   jac->gkbtol = tolerance;
3162:   PetscFunctionReturn(PETSC_SUCCESS);
3163: }

3165: /*@
3166:   PCFieldSplitSetGKBMaxit -  Sets the maximum number of iterations for the generalized Golub-Kahan bidiagonalization preconditioner {cite}`arioli2013` in `PCFIELDSPLIT`

3168:   Collective

3170:   Input Parameters:
3171: + pc    - the preconditioner context
3172: - maxit - the maximum number of iterations

3174:   Options Database Key:
3175: . -pc_fieldsplit_gkb_maxit maxit - default is 100

3177:   Level: intermediate

3179: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitSetGKBDelay()`, `PCFieldSplitSetGKBTol()`, `PCFieldSplitSetGKBNu()`
3180: @*/
3181: PetscErrorCode PCFieldSplitSetGKBMaxit(PC pc, PetscInt maxit)
3182: {
3183:   PetscFunctionBegin;
3186:   PetscTryMethod(pc, "PCFieldSplitSetGKBMaxit_C", (PC, PetscInt), (pc, maxit));
3187:   PetscFunctionReturn(PETSC_SUCCESS);
3188: }

3190: static PetscErrorCode PCFieldSplitSetGKBMaxit_FieldSplit(PC pc, PetscInt maxit)
3191: {
3192:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;

3194:   PetscFunctionBegin;
3195:   jac->gkbmaxit = maxit;
3196:   PetscFunctionReturn(PETSC_SUCCESS);
3197: }

3199: /*@
3200:   PCFieldSplitSetGKBDelay -  Sets the delay in the lower bound error estimate in the generalized Golub-Kahan bidiagonalization {cite}`arioli2013` in `PCFIELDSPLIT`
3201:   preconditioner.

3203:   Collective

3205:   Input Parameters:
3206: + pc    - the preconditioner context
3207: - delay - the delay window in the lower bound estimate

3209:   Options Database Key:
3210: . -pc_fieldsplit_gkb_delay delay - default is 5

3212:   Level: intermediate

3214:   Notes:
3215:   The algorithm uses a lower bound estimate of the error in energy norm as stopping criterion. The lower bound of the error $ ||u-u^k||_H $
3216:   is expressed as a truncated sum. The error at iteration k can only be measured at iteration (k + `delay`), and thus the algorithm needs
3217:   at least (`delay` + 1) iterations to stop.

3219:   For more details on the generalized Golub-Kahan bidiagonalization method and its lower bound stopping criterion, please refer to {cite}`arioli2013`

3221: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitSetGKBNu()`, `PCFieldSplitSetGKBTol()`, `PCFieldSplitSetGKBMaxit()`
3222: @*/
3223: PetscErrorCode PCFieldSplitSetGKBDelay(PC pc, PetscInt delay)
3224: {
3225:   PetscFunctionBegin;
3228:   PetscTryMethod(pc, "PCFieldSplitSetGKBDelay_C", (PC, PetscInt), (pc, delay));
3229:   PetscFunctionReturn(PETSC_SUCCESS);
3230: }

3232: static PetscErrorCode PCFieldSplitSetGKBDelay_FieldSplit(PC pc, PetscInt delay)
3233: {
3234:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;

3236:   PetscFunctionBegin;
3237:   jac->gkbdelay = delay;
3238:   PetscFunctionReturn(PETSC_SUCCESS);
3239: }

3241: /*@
3242:   PCFieldSplitSetGKBNu -  Sets the scalar value nu >= 0 in the transformation H = A00 + nu*A01*A01' of the (1,1) block in the
3243:   Golub-Kahan bidiagonalization preconditioner {cite}`arioli2013` in `PCFIELDSPLIT`

3245:   Collective

3247:   Input Parameters:
3248: + pc - the preconditioner context
3249: - nu - the shift parameter

3251:   Options Database Key:
3252: . -pc_fieldsplit_gkb_nu nu - default is 1

3254:   Level: intermediate

3256:   Notes:
3257:   This shift is in general done to obtain better convergence properties for the outer loop of the algorithm. This is often achieved by choosing `nu` sufficiently large. However,
3258:   if `nu` is chosen too large, the matrix H might be badly conditioned and the solution of the linear system $Hx = b$ in the inner loop becomes difficult. It is therefore
3259:   necessary to find a good balance in between the convergence of the inner and outer loop.

3261:   For `nu` = 0, no shift is done. In this case A00 has to be positive definite. The matrix N in {cite}`arioli2013` is then chosen as identity.

3263: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitSetGKBDelay()`, `PCFieldSplitSetGKBTol()`, `PCFieldSplitSetGKBMaxit()`
3264: @*/
3265: PetscErrorCode PCFieldSplitSetGKBNu(PC pc, PetscReal nu)
3266: {
3267:   PetscFunctionBegin;
3270:   PetscTryMethod(pc, "PCFieldSplitSetGKBNu_C", (PC, PetscReal), (pc, nu));
3271:   PetscFunctionReturn(PETSC_SUCCESS);
3272: }

3274: static PetscErrorCode PCFieldSplitSetGKBNu_FieldSplit(PC pc, PetscReal nu)
3275: {
3276:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;

3278:   PetscFunctionBegin;
3279:   jac->gkbnu = nu;
3280:   PetscFunctionReturn(PETSC_SUCCESS);
3281: }

3283: static PetscErrorCode PCFieldSplitSetType_FieldSplit(PC pc, PCCompositeType type)
3284: {
3285:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;

3287:   PetscFunctionBegin;
3288:   jac->type = type;
3289:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitGetSubKSP_C", NULL));
3290:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetSchurPre_C", NULL));
3291:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitGetSchurPre_C", NULL));
3292:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetSchurFactType_C", NULL));
3293:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetSchurScale_C", NULL));
3294:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBTol_C", NULL));
3295:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBMaxit_C", NULL));
3296:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBNu_C", NULL));
3297:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBDelay_C", NULL));

3299:   if (type == PC_COMPOSITE_SCHUR) {
3300:     pc->ops->apply          = PCApply_FieldSplit_Schur;
3301:     pc->ops->applytranspose = PCApplyTranspose_FieldSplit_Schur;
3302:     pc->ops->matapply       = PCMatApply_FieldSplit_Schur;
3303:     pc->ops->view           = PCView_FieldSplit_Schur;
3304:     pc->ops->setuponblocks  = PCSetUpOnBlocks_FieldSplit_Schur;

3306:     PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitGetSubKSP_C", PCFieldSplitGetSubKSP_FieldSplit_Schur));
3307:     PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetSchurPre_C", PCFieldSplitSetSchurPre_FieldSplit));
3308:     PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitGetSchurPre_C", PCFieldSplitGetSchurPre_FieldSplit));
3309:     PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetSchurFactType_C", PCFieldSplitSetSchurFactType_FieldSplit));
3310:     PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetSchurScale_C", PCFieldSplitSetSchurScale_FieldSplit));
3311:   } else if (type == PC_COMPOSITE_GKB) {
3312:     pc->ops->apply          = PCApply_FieldSplit_GKB;
3313:     pc->ops->applytranspose = NULL;
3314:     pc->ops->matapply       = NULL;
3315:     pc->ops->view           = PCView_FieldSplit_GKB;
3316:     pc->ops->setuponblocks  = PCSetUpOnBlocks_FieldSplit_GKB;

3318:     PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitGetSubKSP_C", PCFieldSplitGetSubKSP_FieldSplit));
3319:     PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBTol_C", PCFieldSplitSetGKBTol_FieldSplit));
3320:     PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBMaxit_C", PCFieldSplitSetGKBMaxit_FieldSplit));
3321:     PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBNu_C", PCFieldSplitSetGKBNu_FieldSplit));
3322:     PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetGKBDelay_C", PCFieldSplitSetGKBDelay_FieldSplit));
3323:   } else {
3324:     pc->ops->apply          = PCApply_FieldSplit;
3325:     pc->ops->applytranspose = PCApplyTranspose_FieldSplit;
3326:     pc->ops->matapply       = PCMatApply_FieldSplit;
3327:     pc->ops->view           = PCView_FieldSplit;
3328:     pc->ops->setuponblocks  = PCSetUpOnBlocks_FieldSplit;

3330:     PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitGetSubKSP_C", PCFieldSplitGetSubKSP_FieldSplit));
3331:   }
3332:   PetscFunctionReturn(PETSC_SUCCESS);
3333: }

3335: static PetscErrorCode PCFieldSplitSetBlockSize_FieldSplit(PC pc, PetscInt bs)
3336: {
3337:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;

3339:   PetscFunctionBegin;
3340:   PetscCheck(bs >= 1, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Blocksize must be positive, you gave %" PetscInt_FMT, bs);
3341:   PetscCheck(jac->bs <= 0 || jac->bs == bs, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Cannot change fieldsplit blocksize from %" PetscInt_FMT " to %" PetscInt_FMT " after it has been set", jac->bs, bs);
3342:   jac->bs = bs;
3343:   PetscFunctionReturn(PETSC_SUCCESS);
3344: }

3346: static PetscErrorCode PCSetCoordinates_FieldSplit(PC pc, PetscInt dim, PetscInt nloc, PetscReal coords[])
3347: {
3348:   PC_FieldSplit    *jac           = (PC_FieldSplit *)pc->data;
3349:   PC_FieldSplitLink ilink_current = jac->head;
3350:   IS                is_owned;

3352:   PetscFunctionBegin;
3353:   jac->coordinates_set = PETSC_TRUE; // Internal flag
3354:   PetscCall(MatGetOwnershipIS(pc->mat, &is_owned, NULL));

3356:   while (ilink_current) {
3357:     // For each IS, embed it to get local coords indces
3358:     IS              is_coords;
3359:     PetscInt        ndofs_block;
3360:     const PetscInt *block_dofs_enumeration; // Numbering of the dofs relevant to the current block

3362:     // Setting drop to true for safety. It should make no difference.
3363:     PetscCall(ISEmbed(ilink_current->is, is_owned, PETSC_TRUE, &is_coords));
3364:     PetscCall(ISGetLocalSize(is_coords, &ndofs_block));
3365:     PetscCall(ISGetIndices(is_coords, &block_dofs_enumeration));

3367:     // Allocate coordinates vector and set it directly
3368:     PetscCall(PetscMalloc1(ndofs_block * dim, &ilink_current->coords));
3369:     for (PetscInt dof = 0; dof < ndofs_block; ++dof) {
3370:       for (PetscInt d = 0; d < dim; ++d) (ilink_current->coords)[dim * dof + d] = coords[dim * block_dofs_enumeration[dof] + d];
3371:     }
3372:     ilink_current->dim   = dim;
3373:     ilink_current->ndofs = ndofs_block;
3374:     PetscCall(ISRestoreIndices(is_coords, &block_dofs_enumeration));
3375:     PetscCall(ISDestroy(&is_coords));
3376:     ilink_current = ilink_current->next;
3377:   }
3378:   PetscCall(ISDestroy(&is_owned));
3379:   PetscFunctionReturn(PETSC_SUCCESS);
3380: }

3382: /*@
3383:   PCFieldSplitSetType - Sets the type, `PCCompositeType`, of a `PCFIELDSPLIT`

3385:   Collective

3387:   Input Parameters:
3388: + pc   - the preconditioner context
3389: - type - `PC_COMPOSITE_ADDITIVE`, `PC_COMPOSITE_MULTIPLICATIVE` (default), `PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE`, `PC_COMPOSITE_SPECIAL`, `PC_COMPOSITE_SCHUR`,
3390:          `PC_COMPOSITE_GKB`

3392:   Options Database Key:
3393: . -pc_fieldsplit_type (multiplicative|additive|symmetric_multiplicative|special|schur) - Sets fieldsplit preconditioner type

3395:   Level: intermediate

3397: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCCompositeType`, `PCCompositeGetType()`, `PC_COMPOSITE_ADDITIVE`, `PC_COMPOSITE_MULTIPLICATIVE`,
3398:           `PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE`, `PC_COMPOSITE_SPECIAL`, `PC_COMPOSITE_SCHUR`, `PCFieldSplitSetSchurFactType()`
3399: @*/
3400: PetscErrorCode PCFieldSplitSetType(PC pc, PCCompositeType type)
3401: {
3402:   PetscFunctionBegin;
3404:   PetscTryMethod(pc, "PCFieldSplitSetType_C", (PC, PCCompositeType), (pc, type));
3405:   PetscFunctionReturn(PETSC_SUCCESS);
3406: }

3408: /*@
3409:   PCFieldSplitGetType - Gets the type, `PCCompositeType`, of a `PCFIELDSPLIT`

3411:   Not collective

3413:   Input Parameter:
3414: . pc - the preconditioner context

3416:   Output Parameter:
3417: . type - `PC_COMPOSITE_ADDITIVE`, `PC_COMPOSITE_MULTIPLICATIVE` (default), `PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE`, `PC_COMPOSITE_SPECIAL`, `PC_COMPOSITE_SCHUR`

3419:   Level: intermediate

3421: .seealso: [](sec_block_matrices), `PC`, `PCCompositeSetType()`, `PCFIELDSPLIT`, `PCCompositeType`, `PC_COMPOSITE_ADDITIVE`, `PC_COMPOSITE_MULTIPLICATIVE`,
3422:           `PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE`, `PC_COMPOSITE_SPECIAL`, `PC_COMPOSITE_SCHUR`
3423: @*/
3424: PetscErrorCode PCFieldSplitGetType(PC pc, PCCompositeType *type)
3425: {
3426:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;

3428:   PetscFunctionBegin;
3430:   PetscAssertPointer(type, 2);
3431:   *type = jac->type;
3432:   PetscFunctionReturn(PETSC_SUCCESS);
3433: }

3435: /*@
3436:   PCFieldSplitSetDMSplits - Flags whether `DMCreateFieldDecomposition()` should be used to define the splits in a `PCFIELDSPLIT`, whenever possible.

3438:   Logically Collective

3440:   Input Parameters:
3441: + pc  - the preconditioner context
3442: - flg - boolean indicating whether to use field splits defined by the `DM`

3444:   Options Database Key:
3445: . -pc_fieldsplit_dm_splits (true|false) - use the field splits defined by the `DM`

3447:   Level: intermediate

3449:   Developer Note:
3450:   The name should be `PCFieldSplitSetUseDMSplits()`, similar change to options database

3452: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitGetDMSplits()`, `DMCreateFieldDecomposition()`, `PCFieldSplitSetFields()`, `PCFieldSplitSetIS()`
3453: @*/
3454: PetscErrorCode PCFieldSplitSetDMSplits(PC pc, PetscBool flg)
3455: {
3456:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
3457:   PetscBool      isfs;

3459:   PetscFunctionBegin;
3462:   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCFIELDSPLIT, &isfs));
3463:   if (isfs) jac->dm_splits = flg;
3464:   PetscFunctionReturn(PETSC_SUCCESS);
3465: }

3467: /*@
3468:   PCFieldSplitGetDMSplits - Returns flag indicating whether `DMCreateFieldDecomposition()` should be used to define the splits in a `PCFIELDSPLIT`, whenever possible.

3470:   Logically Collective

3472:   Input Parameter:
3473: . pc - the preconditioner context

3475:   Output Parameter:
3476: . flg - boolean indicating whether to use field splits defined by the `DM`

3478:   Level: intermediate

3480:   Developer Note:
3481:   The name should be `PCFieldSplitGetUseDMSplits()`

3483: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitSetDMSplits()`, `DMCreateFieldDecomposition()`, `PCFieldSplitSetFields()`, `PCFieldSplitSetIS()`
3484: @*/
3485: PetscErrorCode PCFieldSplitGetDMSplits(PC pc, PetscBool *flg)
3486: {
3487:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;
3488:   PetscBool      isfs;

3490:   PetscFunctionBegin;
3492:   PetscAssertPointer(flg, 2);
3493:   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCFIELDSPLIT, &isfs));
3494:   if (isfs) {
3495:     if (flg) *flg = jac->dm_splits;
3496:   }
3497:   PetscFunctionReturn(PETSC_SUCCESS);
3498: }

3500: /*@
3501:   PCFieldSplitGetDetectSaddlePoint - Returns flag indicating whether `PCFIELDSPLIT` will attempt to automatically determine fields based on zero diagonal entries.

3503:   Logically Collective

3505:   Input Parameter:
3506: . pc - the preconditioner context

3508:   Output Parameter:
3509: . flg - boolean indicating whether to detect fields or not

3511:   Level: intermediate

3513: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitSetDetectSaddlePoint()`
3514: @*/
3515: PetscErrorCode PCFieldSplitGetDetectSaddlePoint(PC pc, PetscBool *flg)
3516: {
3517:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;

3519:   PetscFunctionBegin;
3520:   *flg = jac->detect;
3521:   PetscFunctionReturn(PETSC_SUCCESS);
3522: }

3524: /*@
3525:   PCFieldSplitSetDetectSaddlePoint - Sets flag indicating whether `PCFIELDSPLIT` will attempt to automatically determine fields based on zero diagonal entries.

3527:   Logically Collective

3529:   Input Parameter:
3530: . pc - the preconditioner context

3532:   Output Parameter:
3533: . flg - boolean indicating whether to detect fields or not

3535:   Options Database Key:
3536: . -pc_fieldsplit_detect_saddle_point (true|false) - detect and use the saddle point

3538:   Level: intermediate

3540:   Note:
3541:   Also sets the split type to `PC_COMPOSITE_SCHUR` (see `PCFieldSplitSetType()`) and the Schur preconditioner type to `PC_FIELDSPLIT_SCHUR_PRE_SELF` (see `PCFieldSplitSetSchurPre()`).

3543: .seealso: [](sec_block_matrices), `PC`, `PCFIELDSPLIT`, `PCFieldSplitGetDetectSaddlePoint()`, `PCFieldSplitSetType()`, `PCFieldSplitSetSchurPre()`, `PC_FIELDSPLIT_SCHUR_PRE_SELF`
3544: @*/
3545: PetscErrorCode PCFieldSplitSetDetectSaddlePoint(PC pc, PetscBool flg)
3546: {
3547:   PC_FieldSplit *jac = (PC_FieldSplit *)pc->data;

3549:   PetscFunctionBegin;
3550:   jac->detect = flg;
3551:   if (jac->detect) {
3552:     PetscCall(PCFieldSplitSetType(pc, PC_COMPOSITE_SCHUR));
3553:     PetscCall(PCFieldSplitSetSchurPre(pc, PC_FIELDSPLIT_SCHUR_PRE_SELF, NULL));
3554:   }
3555:   PetscFunctionReturn(PETSC_SUCCESS);
3556: }

3558: /*MC
3559:   PCFIELDSPLIT - Preconditioner created by combining separate preconditioners for individual
3560:   collections of variables (that may overlap) called fields or splits. Each field often represents a different continuum variable
3561:   represented on a grid, such as velocity, pressure, or temperature.
3562:   In the literature these are sometimes called block preconditioners; but should not be confused with `PCBJACOBI`.
3563:   See [the users manual section on "Solving Block Matrices"](sec_block_matrices) for more details.

3565:   Options Database Keys:
3566: +   -pc_fieldsplit_%d_fields a,b,...                                                 - indicates the fields to be used in the `%d`'th split
3567: .   -pc_fieldsplit_default                                                           - automatically add any fields to additional splits that have not
3568:                                                                                        been supplied explicitly by `-pc_fieldsplit_%d_fields`
3569: .   -pc_fieldsplit_block_size bs                                                     - size of block that defines fields (i.e. there are bs fields)
3570:                                                                                        when the matrix is not of `MatType` `MATNEST`
3571: .   -pc_fieldsplit_type (additive|multiplicative|symmetric_multiplicative|schur|gkb) - type of relaxation or factorization splitting
3572: .   -pc_fieldsplit_schur_precondition (self|selfp|user|a11|full)                     - default is `a11`; see `PCFieldSplitSetSchurPre()`
3573: .   -pc_fieldsplit_schur_fact_type (diag|lower|upper|full)                           - set factorization type when using `-pc_fieldsplit_type schur`;
3574:                                                                                        see `PCFieldSplitSetSchurFactType()`
3575: .   -pc_fieldsplit_dm_splits (true|false) (default is true)                          - Whether to use `DMCreateFieldDecomposition()` for splits
3576: -   -pc_fieldsplit_detect_saddle_point (true|false)                                  - automatically finds rows with zero diagonal and uses Schur complement with no preconditioner as the solver

3578:   Options prefixes for inner solvers when using the Schur complement preconditioner are `-fieldsplit_0_` and `-fieldsplit_1_` .
3579:   The options prefix for the inner solver when using the Golub-Kahan biadiagonalization preconditioner is `-fieldsplit_0_`
3580:   For all other solvers they are `-fieldsplit_%d_` for the `%d`'th field; use `-fieldsplit_` for all fields.

3582:   To set options on the solvers for all blocks, prepend `-fieldsplit_` to all the `PC`
3583:   options database keys. For example, `-fieldsplit_pc_type ilu` `-fieldsplit_pc_factor_levels 1`.

3585:   To set the options on the solvers separate for each block call `PCFieldSplitGetSubKSP()`
3586:   and set the options directly on the resulting `KSP` object

3588:   Level: intermediate

3590:   Notes:
3591:   Use `PCFieldSplitSetFields()` to set splits defined by "strided" entries or with a `MATNEST` and `PCFieldSplitSetIS()`
3592:   to define a split by an arbitrary collection of entries.

3594:   If no splits are set, the default is used. If a `DM` is associated with the `PC` and it supports
3595:   `DMCreateFieldDecomposition()`, then that is used for the default. Otherwise if the matrix is not `MATNEST`, the splits are defined by entries strided by bs,
3596:   beginning at 0 then 1, etc to bs-1. The block size can be set with `PCFieldSplitSetBlockSize()`,
3597:   if this is not called the block size defaults to the blocksize of the second matrix passed
3598:   to `KSPSetOperators()`/`PCSetOperators()`.

3600:   For the Schur complement preconditioner if
3601:   ```{math}
3602:     J = \left[\begin{array}{cc} A_{00} & A_{01} \\ A_{10} & A_{11} \end{array}\right]
3603:   ```

3605:   the preconditioner using `full` factorization is logically
3606:   ```{math}
3607:     \left[\begin{array}{cc} I & -\text{ksp}(A_{00}) A_{01} \\ 0 & I \end{array}\right] \left[\begin{array}{cc} \text{ksp}(A_{00}) & 0 \\ 0 & \text{ksp}(S) \end{array}\right] \left[\begin{array}{cc} I & 0 \\ -A_{10} \text{ksp}(A_{00}) & I \end{array}\right]
3608:       ```
3609:   where the action of $\text{ksp}(A_{00})$ is applied using the `KSP` solver with prefix `-fieldsplit_0_`.  $S$ is the Schur complement
3610:   ```{math}
3611:      S = A_{11} - A_{10} \text{ksp}(A_{00}) A_{01}
3612:   ```
3613:   which is usually dense and not stored explicitly.  The action of $\text{ksp}(S)$ is computed using the `KSP` solver with prefix `-fieldsplit_splitname_` (where `splitname`
3614:   was given in providing the SECOND split or 1 if not given). Accordingly, if using `PCFieldSplitGetSubKSP()`, the array of sub-`KSP` contexts will hold two `KSP`s: at its
3615:   0th index, the `KSP` associated with `-fieldsplit_0_`, and at its 1st index, the `KSP` corresponding to `-fieldsplit_1_`.
3616:   By default, $A_{11}$ is used to construct a preconditioner for $S$, use `PCFieldSplitSetSchurPre()` for all the possible ways to construct the preconditioner for $S$.

3618:   The factorization type is set using `-pc_fieldsplit_schur_fact_type <diag, lower, upper, full>`. `full` is shown above,
3619:   `diag` gives
3620:   ```{math}
3621:     \left[\begin{array}{cc} \text{ksp}(A_{00}) & 0 \\  0 & -\text{ksp}(S) \end{array}\right]
3622:   ```
3623:   Note that, slightly counter intuitively, there is a negative in front of the $\text{ksp}(S)$  so that the preconditioner is positive definite. For SPD matrices $J$, the sign flip
3624:   can be turned off with `PCFieldSplitSetSchurScale()` or by command line `-pc_fieldsplit_schur_scale 1.0`. The `lower` factorization is the inverse of
3625:   ```{math}
3626:     \left[\begin{array}{cc} A_{00} & 0 \\  A_{10} & S \end{array}\right]
3627:   ```
3628:   where the inverses of $A_{00}$ and $S$ are applied using `KSP`s. The upper factorization is the inverse of
3629:   ```{math}
3630:     \left[\begin{array}{cc} A_{00} & A_{01} \\  0 & S \end{array}\right]
3631:   ```
3632:   where again the inverses of $A_{00}$ and $S$ are applied using `KSP`s.

3634:   If only one set of indices (one `IS`) is provided with `PCFieldSplitSetIS()` then the complement of that `IS`
3635:   is used automatically for a second submatrix.

3637:   The fieldsplit preconditioner cannot currently be used with the `MATBAIJ` or `MATSBAIJ` data formats if the blocksize is larger than 1.
3638:   Generally it should be used with the `MATAIJ` or `MATNEST` `MatType`

3640:   The forms of these preconditioners are closely related, if not identical, to forms derived as "Distributive Iterations", see,
3641:   for example, page 294 in "Principles of Computational Fluid Dynamics" by Pieter Wesseling {cite}`wesseling2009`.
3642:   One can also use `PCFIELDSPLIT` inside a smoother resulting in "Distributive Smoothers".

3644:   See "A taxonomy and comparison of parallel block multi-level preconditioners for the incompressible Navier-Stokes equations" {cite}`elman2008tcp`.

3646:   The Constrained Pressure Preconditioner (CPR) can be implemented using `PCCOMPOSITE` with `PCGALERKIN`. CPR first solves an $R A P$ subsystem, updates the
3647:   residual on all variables (`PCCompositeSetType(pc,PC_COMPOSITE_MULTIPLICATIVE)`), and then applies a simple ILU like preconditioner on all the variables.

3649:   The generalized Golub-Kahan bidiagonalization preconditioner (GKB) can be applied to symmetric $2 \times 2$ block matrices of the shape
3650:   ```{math}
3651:     \left[\begin{array}{cc} A_{00} & A_{01} \\ A_{01}' & 0 \end{array}\right]
3652:   ```
3653:   with $A_{00}$ positive semi-definite. The implementation follows {cite}`arioli2013`. Therein, we choose $N := 1/\nu * I$ and the $(1,1)$-block of the matrix is modified to $H = _{A00} + \nu*A_{01}*A_{01}'$.
3654:   A linear system $Hx = b$ has to be solved in each iteration of the GKB algorithm. This solver is chosen with the option prefix `-fieldsplit_0_`.

3656:   Some `PCFIELDSPLIT` variants are called physics-based preconditioners, since the preconditioner takes into account the underlying physics of the
3657:   problem. But this nomenclature is not well-defined.

3659:   Developer Note:
3660:   The Schur complement functionality of `PCFIELDSPLIT` should likely be factored into its own `PC` thus simplifying the implementation of the preconditioners and their
3661:   user API.

3663: .seealso: [](sec_block_matrices), `PC`, `PCCreate()`, `PCSetType()`, `PCType`, `PCLSC`,
3664:           `PCFieldSplitGetSubKSP()`, `PCFieldSplitSchurGetSubKSP()`, `PCFieldSplitSetFields()`,
3665:           `PCFieldSplitSetType()`, `PCFieldSplitSetIS()`, `PCFieldSplitSetSchurPre()`, `PCFieldSplitSetSchurFactType()`,
3666:           `MatSchurComplementSetAinvType()`, `PCFieldSplitSetSchurScale()`, `PCFieldSplitSetDetectSaddlePoint()`
3667: M*/

3669: PETSC_EXTERN PetscErrorCode PCCreate_FieldSplit(PC pc)
3670: {
3671:   PC_FieldSplit *jac;

3673:   PetscFunctionBegin;
3674:   PetscCall(PetscNew(&jac));

3676:   jac->bs                 = -1;
3677:   jac->type               = PC_COMPOSITE_MULTIPLICATIVE;
3678:   jac->schurpre           = PC_FIELDSPLIT_SCHUR_PRE_USER; /* Try user preconditioner first, fall back on diagonal */
3679:   jac->schurfactorization = PC_FIELDSPLIT_SCHUR_FACT_FULL;
3680:   jac->schurscale         = -1.0;
3681:   jac->dm_splits          = PETSC_TRUE;
3682:   jac->gkbtol             = 1e-5;
3683:   jac->gkbdelay           = 5;
3684:   jac->gkbnu              = 1;
3685:   jac->gkbmaxit           = 100;

3687:   pc->data = (void *)jac;

3689:   pc->ops->setup           = PCSetUp_FieldSplit;
3690:   pc->ops->reset           = PCReset_FieldSplit;
3691:   pc->ops->destroy         = PCDestroy_FieldSplit;
3692:   pc->ops->setfromoptions  = PCSetFromOptions_FieldSplit;
3693:   pc->ops->applyrichardson = NULL;

3695:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSchurGetSubKSP_C", PCFieldSplitSchurGetSubKSP_FieldSplit));
3696:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetFields_C", PCFieldSplitSetFields_FieldSplit));
3697:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetIS_C", PCFieldSplitSetIS_FieldSplit));
3698:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetType_C", PCFieldSplitSetType_FieldSplit));
3699:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitSetBlockSize_C", PCFieldSplitSetBlockSize_FieldSplit));
3700:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCFieldSplitRestrictIS_C", PCFieldSplitRestrictIS_FieldSplit));
3701:   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCSetCoordinates_C", PCSetCoordinates_FieldSplit));

3703:   /* Initialize function pointers */
3704:   PetscCall(PCFieldSplitSetType(pc, jac->type));
3705:   PetscFunctionReturn(PETSC_SUCCESS);
3706: }