Actual source code: fieldsplit.c

petsc-master 2019-10-23
Report Typos and Errors
  1:  #include <petsc/private/pcimpl.h>
  2: #include <petsc/private/kspimpl.h>    /*  This is needed to provide the appropriate PETSC_EXTERN for KSP_Solve_FS ....*/
  3:  #include <petscdm.h>

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

  8: 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;

 10: typedef struct _PC_FieldSplitLink *PC_FieldSplitLink;
 11: struct _PC_FieldSplitLink {
 12:   KSP               ksp;
 13:   Vec               x,y,z;
 14:   char              *splitname;
 15:   PetscInt          nfields;
 16:   PetscInt          *fields,*fields_col;
 17:   VecScatter        sctx;
 18:   IS                is,is_col;
 19:   PC_FieldSplitLink next,previous;
 20:   PetscLogEvent     event;
 21: };

 23: typedef struct {
 24:   PCCompositeType type;
 25:   PetscBool       defaultsplit;                    /* Flag for a system with a set of 'k' scalar fields with the same layout (and bs = k) */
 26:   PetscBool       splitdefined;                    /* Flag is set after the splits have been defined, to prevent more splits from being added */
 27:   PetscInt        bs;                              /* Block size for IS and Mat structures */
 28:   PetscInt        nsplits;                         /* Number of field divisions defined */
 29:   Vec             *x,*y,w1,w2;
 30:   Mat             *mat;                            /* The diagonal block for each split */
 31:   Mat             *pmat;                           /* The preconditioning diagonal block for each split */
 32:   Mat             *Afield;                         /* The rows of the matrix associated with each split */
 33:   PetscBool       issetup;

 35:   /* Only used when Schur complement preconditioning is used */
 36:   Mat                       B;                     /* The (0,1) block */
 37:   Mat                       C;                     /* The (1,0) block */
 38:   Mat                       schur;                 /* The Schur complement S = A11 - A10 A00^{-1} A01, the KSP here, kspinner, is H_1 in [El08] */
 39:   Mat                       schurp;                /* Assembled approximation to S built by MatSchurComplement to be used as a preconditioning matrix when solving with S */
 40:   Mat                       schur_user;            /* User-provided preconditioning matrix for the Schur complement */
 41:   PCFieldSplitSchurPreType  schurpre;              /* Determines which preconditioning matrix is used for the Schur complement */
 42:   PCFieldSplitSchurFactType schurfactorization;
 43:   KSP                       kspschur;              /* The solver for S */
 44:   KSP                       kspupper;              /* The solver for A in the upper diagonal part of the factorization (H_2 in [El08]) */
 45:   PetscScalar               schurscale;            /* Scaling factor for the Schur complement solution with DIAG factorization */

 47:   /* Only used when Golub-Kahan bidiagonalization preconditioning is used */
 48:   Mat                       H;                     /* The modified matrix H = A00 + nu*A01*A01'              */
 49:   PetscReal                 gkbtol;                /* Stopping tolerance for lower bound estimate            */
 50:   PetscInt                  gkbdelay;              /* The delay window for the stopping criterion            */
 51:   PetscReal                 gkbnu;                 /* Parameter for augmented Lagrangian H = A + nu*A01*A01' */
 52:   PetscInt                  gkbmaxit;              /* Maximum number of iterations for outer loop            */
 53:   PetscBool                 gkbmonitor;            /* Monitor for gkb iterations and the lower bound error   */
 54:   PetscViewer               gkbviewer;             /* Viewer context for gkbmonitor                          */
 55:   Vec                       u,v,d,Hu;              /* Work vectors for the GKB algorithm                     */
 56:   PetscScalar               *vecz;                 /* Contains intermediate values, eg for lower bound       */

 58:   PC_FieldSplitLink         head;
 59:   PetscBool                 isrestrict;             /* indicates PCFieldSplitRestrictIS() has been last called on this object, hack */
 60:   PetscBool                 suboptionsset;          /* Indicates that the KSPSetFromOptions() has been called on the sub-KSPs */
 61:   PetscBool                 dm_splits;              /* Whether to use DMCreateFieldDecomposition() whenever possible */
 62:   PetscBool                 diag_use_amat;          /* Whether to extract diagonal matrix blocks from Amat, rather than Pmat (weaker than -pc_use_amat) */
 63:   PetscBool                 offdiag_use_amat;       /* Whether to extract off-diagonal matrix blocks from Amat, rather than Pmat (weaker than -pc_use_amat) */
 64:   PetscBool                 detect;                 /* Whether to form 2-way split by finding zero diagonal entries */
 65: } PC_FieldSplit;

 67: /*
 68:     Notes:
 69:     there is no particular reason that pmat, x, and y are stored as arrays in PC_FieldSplit instead of
 70:    inside PC_FieldSplitLink, just historical. If you want to be able to add new fields after already using the
 71:    PC you could change this.
 72: */

 74: /* This helper is so that setting a user-provided preconditioning matrix is orthogonal to choosing to use it.  This way the
 75: * application-provided FormJacobian can provide this matrix without interfering with the user's (command-line) choices. */
 76: static Mat FieldSplitSchurPre(PC_FieldSplit *jac)
 77: {
 78:   switch (jac->schurpre) {
 79:   case PC_FIELDSPLIT_SCHUR_PRE_SELF: return jac->schur;
 80:   case PC_FIELDSPLIT_SCHUR_PRE_SELFP: return jac->schurp;
 81:   case PC_FIELDSPLIT_SCHUR_PRE_A11: return jac->pmat[1];
 82:   case PC_FIELDSPLIT_SCHUR_PRE_FULL: /* We calculate this and store it in schur_user */
 83:   case PC_FIELDSPLIT_SCHUR_PRE_USER: /* Use a user-provided matrix if it is given, otherwise diagonal block */
 84:   default:
 85:     return jac->schur_user ? jac->schur_user : jac->pmat[1];
 86:   }
 87: }


 90:  #include <petscdraw.h>
 91: static PetscErrorCode PCView_FieldSplit(PC pc,PetscViewer viewer)
 92: {
 93:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
 94:   PetscErrorCode    ierr;
 95:   PetscBool         iascii,isdraw;
 96:   PetscInt          i,j;
 97:   PC_FieldSplitLink ilink = jac->head;

100:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
101:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
102:   if (iascii) {
103:     if (jac->bs > 0) {
104:       PetscViewerASCIIPrintf(viewer,"  FieldSplit with %s composition: total splits = %D, blocksize = %D\n",PCCompositeTypes[jac->type],jac->nsplits,jac->bs);
105:     } else {
106:       PetscViewerASCIIPrintf(viewer,"  FieldSplit with %s composition: total splits = %D\n",PCCompositeTypes[jac->type],jac->nsplits);
107:     }
108:     if (pc->useAmat) {
109:       PetscViewerASCIIPrintf(viewer,"  using Amat (not Pmat) as operator for blocks\n");
110:     }
111:     if (jac->diag_use_amat) {
112:       PetscViewerASCIIPrintf(viewer,"  using Amat (not Pmat) as operator for diagonal blocks\n");
113:     }
114:     if (jac->offdiag_use_amat) {
115:       PetscViewerASCIIPrintf(viewer,"  using Amat (not Pmat) as operator for off-diagonal blocks\n");
116:     }
117:     PetscViewerASCIIPrintf(viewer,"  Solver info for each split is in the following KSP objects:\n");
118:     for (i=0; i<jac->nsplits; i++) {
119:       if (ilink->fields) {
120:         PetscViewerASCIIPrintf(viewer,"Split number %D Fields ",i);
121:         PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
122:         for (j=0; j<ilink->nfields; j++) {
123:           if (j > 0) {
124:             PetscViewerASCIIPrintf(viewer,",");
125:           }
126:           PetscViewerASCIIPrintf(viewer," %D",ilink->fields[j]);
127:         }
128:         PetscViewerASCIIPrintf(viewer,"\n");
129:         PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
130:       } else {
131:         PetscViewerASCIIPrintf(viewer,"Split number %D Defined by IS\n",i);
132:       }
133:       KSPView(ilink->ksp,viewer);
134:       ilink = ilink->next;
135:     }
136:   }

138:  if (isdraw) {
139:     PetscDraw draw;
140:     PetscReal x,y,w,wd;

142:     PetscViewerDrawGetDraw(viewer,0,&draw);
143:     PetscDrawGetCurrentPoint(draw,&x,&y);
144:     w    = 2*PetscMin(1.0 - x,x);
145:     wd   = w/(jac->nsplits + 1);
146:     x    = x - wd*(jac->nsplits-1)/2.0;
147:     for (i=0; i<jac->nsplits; i++) {
148:       PetscDrawPushCurrentPoint(draw,x,y);
149:       KSPView(ilink->ksp,viewer);
150:       PetscDrawPopCurrentPoint(draw);
151:       x    += wd;
152:       ilink = ilink->next;
153:     }
154:   }
155:   return(0);
156: }

158: static PetscErrorCode PCView_FieldSplit_Schur(PC pc,PetscViewer viewer)
159: {
160:   PC_FieldSplit              *jac = (PC_FieldSplit*)pc->data;
161:   PetscErrorCode             ierr;
162:   PetscBool                  iascii,isdraw;
163:   PetscInt                   i,j;
164:   PC_FieldSplitLink          ilink = jac->head;
165:   MatSchurComplementAinvType atype;

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

249:     PetscViewerDrawGetDraw(viewer,0,&draw);
250:     PetscDrawGetCurrentPoint(draw,&x,&y);
251:     if (jac->kspupper != jac->head->ksp) cnt++;
252:     w  = 2*PetscMin(1.0 - x,x);
253:     wd = w/(cnt + 1);

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

267:     PetscDrawPushCurrentPoint(draw,x,y);
268:     KSPView(jac->head->ksp,viewer);
269:     PetscDrawPopCurrentPoint(draw);
270:     if (jac->kspupper != jac->head->ksp) {
271:       x   += wd;
272:       PetscDrawPushCurrentPoint(draw,x,y);
273:       KSPView(jac->kspupper,viewer);
274:       PetscDrawPopCurrentPoint(draw);
275:     }
276:     x   += wd;
277:     PetscDrawPushCurrentPoint(draw,x,y);
278:     KSPView(jac->kspschur,viewer);
279:     PetscDrawPopCurrentPoint(draw);
280:   }
281:   return(0);
282: }

284: static PetscErrorCode PCView_FieldSplit_GKB(PC pc,PetscViewer viewer)
285: {
286:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
287:   PetscErrorCode    ierr;
288:   PetscBool         iascii,isdraw;
289:   PetscInt          i,j;
290:   PC_FieldSplitLink ilink = jac->head;

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

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

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

331:     PetscViewerASCIIPopTab(viewer);
332:   }

334:  if (isdraw) {
335:     PetscDraw draw;
336:     PetscReal x,y,w,wd;

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


355: /* Precondition: jac->bs is set to a meaningful value */
356: static PetscErrorCode PCFieldSplitSetRuntimeSplits_Private(PC pc)
357: {
359:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
360:   PetscInt       i,nfields,*ifields,nfields_col,*ifields_col;
361:   PetscBool      flg,flg_col;
362:   char           optionname[128],splitname[8],optionname_col[128];

365:   PetscMalloc1(jac->bs,&ifields);
366:   PetscMalloc1(jac->bs,&ifields_col);
367:   for (i=0,flg=PETSC_TRUE;; i++) {
368:     PetscSNPrintf(splitname,sizeof(splitname),"%D",i);
369:     PetscSNPrintf(optionname,sizeof(optionname),"-pc_fieldsplit_%D_fields",i);
370:     PetscSNPrintf(optionname_col,sizeof(optionname_col),"-pc_fieldsplit_%D_fields_col",i);
371:     nfields     = jac->bs;
372:     nfields_col = jac->bs;
373:     PetscOptionsGetIntArray(((PetscObject)pc)->options,((PetscObject)pc)->prefix,optionname,ifields,&nfields,&flg);
374:     PetscOptionsGetIntArray(((PetscObject)pc)->options,((PetscObject)pc)->prefix,optionname_col,ifields_col,&nfields_col,&flg_col);
375:     if (!flg) break;
376:     else if (flg && !flg_col) {
377:       if (!nfields) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Cannot list zero fields");
378:       PCFieldSplitSetFields(pc,splitname,nfields,ifields,ifields);
379:     } else {
380:       if (!nfields || !nfields_col) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Cannot list zero fields");
381:       if (nfields != nfields_col) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Number of row and column fields must match");
382:       PCFieldSplitSetFields(pc,splitname,nfields,ifields,ifields_col);
383:     }
384:   }
385:   if (i > 0) {
386:     /* Makes command-line setting of splits take precedence over setting them in code.
387:        Otherwise subsequent calls to PCFieldSplitSetIS() or PCFieldSplitSetFields() would
388:        create new splits, which would probably not be what the user wanted. */
389:     jac->splitdefined = PETSC_TRUE;
390:   }
391:   PetscFree(ifields);
392:   PetscFree(ifields_col);
393:   return(0);
394: }

396: static PetscErrorCode PCFieldSplitSetDefaults(PC pc)
397: {
398:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
399:   PetscErrorCode    ierr;
400:   PC_FieldSplitLink ilink = jac->head;
401:   PetscBool         fieldsplit_default = PETSC_FALSE,coupling = PETSC_FALSE;
402:   PetscInt          i;

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

419:       DMCreateFieldDecomposition(pc->dm, &numFields, &fieldNames, &fields, &dms);
420:       /* Allow the user to prescribe the splits */
421:       for (i = 0, flg = PETSC_TRUE;; i++) {
422:         PetscInt ifields[128];
423:         IS       compField;
424:         char     optionname[128], splitname[8];
425:         PetscInt nfields = numFields;

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

473:             DMKSPGetComputeOperators(pc->dm, &func, &ctx);
474:             DMKSPSetComputeOperators(dms[i],  func,  ctx);
475:           }
476:           PetscObjectIncrementTabLevel((PetscObject)dms[i],(PetscObject)ilink->ksp,0);
477:           DMDestroy(&dms[i]);
478:         }
479:         PetscFree(dms);
480:       }
481:     } else {
482:       if (jac->bs <= 0) {
483:         if (pc->pmat) {
484:           MatGetBlockSize(pc->pmat,&jac->bs);
485:         } else jac->bs = 1;
486:       }

488:       if (jac->detect) {
489:         IS       zerodiags,rest;
490:         PetscInt nmin,nmax;

492:         MatGetOwnershipRange(pc->mat,&nmin,&nmax);
493:         MatFindZeroDiagonals(pc->mat,&zerodiags);
494:         ISComplement(zerodiags,nmin,nmax,&rest);
495:         PCFieldSplitSetIS(pc,"0",rest);
496:         PCFieldSplitSetIS(pc,"1",zerodiags);
497:         ISDestroy(&zerodiags);
498:         ISDestroy(&rest);
499:       } else if (coupling) {
500:         IS       coupling,rest;
501:         PetscInt nmin,nmax;

503:         MatGetOwnershipRange(pc->mat,&nmin,&nmax);
504:         MatFindOffBlockDiagonalEntries(pc->mat,&coupling);
505:         ISCreateStride(PetscObjectComm((PetscObject)pc->mat),nmax-nmin,nmin,1,&rest);
506:         ISSetIdentity(rest);
507:         PCFieldSplitSetIS(pc,"0",rest);
508:         PCFieldSplitSetIS(pc,"1",coupling);
509:         ISDestroy(&coupling);
510:         ISDestroy(&rest);
511:       } else {
512:         PetscOptionsGetBool(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_fieldsplit_default",&fieldsplit_default,NULL);
513:         if (!fieldsplit_default) {
514:           /* Allow user to set fields from command line,  if bs was known at the time of PCSetFromOptions_FieldSplit()
515:            then it is set there. This is not ideal because we should only have options set in XXSetFromOptions(). */
516:           PCFieldSplitSetRuntimeSplits_Private(pc);
517:           if (jac->splitdefined) {PetscInfo(pc,"Splits defined using the options database\n");}
518:         }
519:         if ((fieldsplit_default || !jac->splitdefined) && !jac->isrestrict) {
520:           Mat       M = pc->pmat;
521:           PetscBool isnest;

523:           PetscInfo(pc,"Using default splitting of fields\n");
524:           PetscObjectTypeCompare((PetscObject)pc->pmat,MATNEST,&isnest);
525:           if (!isnest) {
526:             M    = pc->mat;
527:             PetscObjectTypeCompare((PetscObject)pc->mat,MATNEST,&isnest);
528:           }
529:           if (isnest) {
530:             IS       *fields;
531:             PetscInt nf;

533:             MatNestGetSize(M,&nf,NULL);
534:             PetscMalloc1(nf,&fields);
535:             MatNestGetISs(M,fields,NULL);
536:             for (i=0;i<nf;i++) {
537:               PCFieldSplitSetIS(pc,NULL,fields[i]);
538:             }
539:             PetscFree(fields);
540:           } else {
541:             for (i=0; i<jac->bs; i++) {
542:               char splitname[8];
543:               PetscSNPrintf(splitname,sizeof(splitname),"%D",i);
544:               PCFieldSplitSetFields(pc,splitname,1,&i,&i);
545:             }
546:             jac->defaultsplit = PETSC_TRUE;
547:           }
548:         }
549:       }
550:     }
551:   } else if (jac->nsplits == 1) {
552:     if (ilink->is) {
553:       IS       is2;
554:       PetscInt nmin,nmax;

556:       MatGetOwnershipRange(pc->mat,&nmin,&nmax);
557:       ISComplement(ilink->is,nmin,nmax,&is2);
558:       PCFieldSplitSetIS(pc,"1",is2);
559:       ISDestroy(&is2);
560:     } else SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Must provide at least two sets of fields to PCFieldSplit()");
561:   }

563:   if (jac->nsplits < 2) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_PLIB,"Unhandled case, must have at least two fields, not %d", jac->nsplits);
564:   return(0);
565: }

567: static PetscErrorCode MatGolubKahanComputeExplicitOperator(Mat A,Mat B,Mat C,Mat *H,PetscReal gkbnu)
568: {
569:   PetscErrorCode    ierr;
570:   Mat               BT,T;
571:   PetscReal         nrmT,nrmB;

574:   MatHermitianTranspose(C,MAT_INITIAL_MATRIX,&T);            /* Test if augmented matrix is symmetric */
575:   MatAXPY(T,-1.0,B,DIFFERENT_NONZERO_PATTERN);
576:   MatNorm(T,NORM_1,&nrmT);
577:   MatNorm(B,NORM_1,&nrmB);
578:   if (nrmB > 0) {
579:     if (nrmT/nrmB >= PETSC_SMALL) {
580:       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Matrix is not symmetric/hermitian, GKB is not applicable.");
581:     }
582:   }
583:   /* Compute augmented Lagrangian matrix H = A00 + nu*A01*A01'. This corresponds to */
584:   /* setting N := 1/nu*I in [Ar13].                                                 */
585:   MatHermitianTranspose(B,MAT_INITIAL_MATRIX,&BT);
586:   MatMatMult(B,BT,MAT_INITIAL_MATRIX,PETSC_DEFAULT,H);       /* H = A01*A01'          */
587:   MatAYPX(*H,gkbnu,A,DIFFERENT_NONZERO_PATTERN);             /* H = A00 + nu*A01*A01' */

589:   MatDestroy(&BT);
590:   MatDestroy(&T);
591:   return(0);
592: }

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

596: static PetscErrorCode PCSetUp_FieldSplit(PC pc)
597: {
598:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
599:   PetscErrorCode    ierr;
600:   PC_FieldSplitLink ilink;
601:   PetscInt          i,nsplit;
602:   PetscBool         sorted, sorted_col;

605:   pc->failedreason = PC_NOERROR;
606:   PCFieldSplitSetDefaults(pc);
607:   nsplit = jac->nsplits;
608:   ilink  = jac->head;

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

614:     jac->issetup = PETSC_TRUE;

616:     /* This is done here instead of in PCFieldSplitSetFields() because may not have matrix at that point */
617:     if (jac->defaultsplit || !ilink->is) {
618:       if (jac->bs <= 0) jac->bs = nsplit;
619:     }
620:     bs     = jac->bs;
621:     MatGetOwnershipRange(pc->pmat,&rstart,&rend);
622:     nslots = (rend - rstart)/bs;
623:     for (i=0; i<nsplit; i++) {
624:       if (jac->defaultsplit) {
625:         ISCreateStride(PetscObjectComm((PetscObject)pc),nslots,rstart+i,nsplit,&ilink->is);
626:         ISDuplicate(ilink->is,&ilink->is_col);
627:       } else if (!ilink->is) {
628:         if (ilink->nfields > 1) {
629:           PetscInt *ii,*jj,j,k,nfields = ilink->nfields,*fields = ilink->fields,*fields_col = ilink->fields_col;
630:           PetscMalloc1(ilink->nfields*nslots,&ii);
631:           PetscMalloc1(ilink->nfields*nslots,&jj);
632:           for (j=0; j<nslots; j++) {
633:             for (k=0; k<nfields; k++) {
634:               ii[nfields*j + k] = rstart + bs*j + fields[k];
635:               jj[nfields*j + k] = rstart + bs*j + fields_col[k];
636:             }
637:           }
638:           ISCreateGeneral(PetscObjectComm((PetscObject)pc),nslots*nfields,ii,PETSC_OWN_POINTER,&ilink->is);
639:           ISCreateGeneral(PetscObjectComm((PetscObject)pc),nslots*nfields,jj,PETSC_OWN_POINTER,&ilink->is_col);
640:           ISSetBlockSize(ilink->is, nfields);
641:           ISSetBlockSize(ilink->is_col, nfields);
642:         } else {
643:           ISCreateStride(PetscObjectComm((PetscObject)pc),nslots,rstart+ilink->fields[0],bs,&ilink->is);
644:           ISCreateStride(PetscObjectComm((PetscObject)pc),nslots,rstart+ilink->fields_col[0],bs,&ilink->is_col);
645:         }
646:       }
647:       ISSorted(ilink->is,&sorted);
648:       if (ilink->is_col) { ISSorted(ilink->is_col,&sorted_col); }
649:       if (!sorted || !sorted_col) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Fields must be sorted when creating split");
650:       ilink = ilink->next;
651:     }
652:   }

654:   ilink = jac->head;
655:   if (!jac->pmat) {
656:     Vec xtmp;

658:     MatCreateVecs(pc->pmat,&xtmp,NULL);
659:     PetscMalloc1(nsplit,&jac->pmat);
660:     PetscMalloc2(nsplit,&jac->x,nsplit,&jac->y);
661:     for (i=0; i<nsplit; i++) {
662:       MatNullSpace sp;

664:       /* Check for preconditioning matrix attached to IS */
665:       PetscObjectQuery((PetscObject) ilink->is, "pmat", (PetscObject*) &jac->pmat[i]);
666:       if (jac->pmat[i]) {
667:         PetscObjectReference((PetscObject) jac->pmat[i]);
668:         if (jac->type == PC_COMPOSITE_SCHUR) {
669:           jac->schur_user = jac->pmat[i];

671:           PetscObjectReference((PetscObject) jac->schur_user);
672:         }
673:       } else {
674:         const char *prefix;
675:         MatCreateSubMatrix(pc->pmat,ilink->is,ilink->is_col,MAT_INITIAL_MATRIX,&jac->pmat[i]);
676:         KSPGetOptionsPrefix(ilink->ksp,&prefix);
677:         MatSetOptionsPrefix(jac->pmat[i],prefix);
678:         MatViewFromOptions(jac->pmat[i],NULL,"-mat_view");
679:       }
680:       /* create work vectors for each split */
681:       MatCreateVecs(jac->pmat[i],&jac->x[i],&jac->y[i]);
682:       ilink->x = jac->x[i]; ilink->y = jac->y[i]; ilink->z = NULL;
683:       /* compute scatter contexts needed by multiplicative versions and non-default splits */
684:       VecScatterCreate(xtmp,ilink->is,jac->x[i],NULL,&ilink->sctx);
685:       PetscObjectQuery((PetscObject) ilink->is, "nearnullspace", (PetscObject*) &sp);
686:       if (sp) {
687:         MatSetNearNullSpace(jac->pmat[i], sp);
688:       }
689:       ilink = ilink->next;
690:     }
691:     VecDestroy(&xtmp);
692:   } else {
693:     MatReuse scall;
694:     if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
695:       for (i=0; i<nsplit; i++) {
696:         MatDestroy(&jac->pmat[i]);
697:       }
698:       scall = MAT_INITIAL_MATRIX;
699:     } else scall = MAT_REUSE_MATRIX;

701:     for (i=0; i<nsplit; i++) {
702:       Mat pmat;

704:       /* Check for preconditioning matrix attached to IS */
705:       PetscObjectQuery((PetscObject) ilink->is, "pmat", (PetscObject*) &pmat);
706:       if (!pmat) {
707:         MatCreateSubMatrix(pc->pmat,ilink->is,ilink->is_col,scall,&jac->pmat[i]);
708:       }
709:       ilink = ilink->next;
710:     }
711:   }
712:   if (jac->diag_use_amat) {
713:     ilink = jac->head;
714:     if (!jac->mat) {
715:       PetscMalloc1(nsplit,&jac->mat);
716:       for (i=0; i<nsplit; i++) {
717:         MatCreateSubMatrix(pc->mat,ilink->is,ilink->is_col,MAT_INITIAL_MATRIX,&jac->mat[i]);
718:         ilink = ilink->next;
719:       }
720:     } else {
721:       MatReuse scall;
722:       if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
723:         for (i=0; i<nsplit; i++) {
724:           MatDestroy(&jac->mat[i]);
725:         }
726:         scall = MAT_INITIAL_MATRIX;
727:       } else scall = MAT_REUSE_MATRIX;

729:       for (i=0; i<nsplit; i++) {
730:         if (jac->mat[i]) {MatCreateSubMatrix(pc->mat,ilink->is,ilink->is_col,scall,&jac->mat[i]);}
731:         ilink = ilink->next;
732:       }
733:     }
734:   } else {
735:     jac->mat = jac->pmat;
736:   }

738:   /* Check for null space attached to IS */
739:   ilink = jac->head;
740:   for (i=0; i<nsplit; i++) {
741:     MatNullSpace sp;

743:     PetscObjectQuery((PetscObject) ilink->is, "nullspace", (PetscObject*) &sp);
744:     if (sp) {
745:       MatSetNullSpace(jac->mat[i], sp);
746:     }
747:     ilink = ilink->next;
748:   }

750:   if (jac->type != PC_COMPOSITE_ADDITIVE  && jac->type != PC_COMPOSITE_SCHUR && jac->type != PC_COMPOSITE_GKB) {
751:     /* extract the rows of the matrix associated with each field: used for efficient computation of residual inside algorithm */
752:     /* FIXME: Can/should we reuse jac->mat whenever (jac->diag_use_amat) is true? */
753:     ilink = jac->head;
754:     if (nsplit == 2 && jac->type == PC_COMPOSITE_MULTIPLICATIVE) {
755:       /* 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 */
756:       if (!jac->Afield) {
757:         PetscCalloc1(nsplit,&jac->Afield);
758:         if (jac->offdiag_use_amat) {
759:           MatCreateSubMatrix(pc->mat,ilink->next->is,ilink->is,MAT_INITIAL_MATRIX,&jac->Afield[1]);
760:         } else {
761:           MatCreateSubMatrix(pc->pmat,ilink->next->is,ilink->is,MAT_INITIAL_MATRIX,&jac->Afield[1]);
762:         }
763:       } else {
764:         MatReuse scall;
765:         if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
766:           for (i=0; i<nsplit; i++) {
767:             MatDestroy(&jac->Afield[1]);
768:           }
769:           scall = MAT_INITIAL_MATRIX;
770:         } else scall = MAT_REUSE_MATRIX;

772:         if (jac->offdiag_use_amat) {
773:           MatCreateSubMatrix(pc->mat,ilink->next->is,ilink->is,scall,&jac->Afield[1]);
774:         } else {
775:           MatCreateSubMatrix(pc->pmat,ilink->next->is,ilink->is,scall,&jac->Afield[1]);
776:         }
777:       }
778:     } else {
779:       if (!jac->Afield) {
780:         PetscMalloc1(nsplit,&jac->Afield);
781:         for (i=0; i<nsplit; i++) {
782:           if (jac->offdiag_use_amat) {
783:             MatCreateSubMatrix(pc->mat,ilink->is,NULL,MAT_INITIAL_MATRIX,&jac->Afield[i]);
784:           } else {
785:             MatCreateSubMatrix(pc->pmat,ilink->is,NULL,MAT_INITIAL_MATRIX,&jac->Afield[i]);
786:           }
787:           ilink = ilink->next;
788:         }
789:       } else {
790:         MatReuse scall;
791:         if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
792:           for (i=0; i<nsplit; i++) {
793:             MatDestroy(&jac->Afield[i]);
794:           }
795:           scall = MAT_INITIAL_MATRIX;
796:         } else scall = MAT_REUSE_MATRIX;

798:         for (i=0; i<nsplit; i++) {
799:           if (jac->offdiag_use_amat) {
800:             MatCreateSubMatrix(pc->mat,ilink->is,NULL,scall,&jac->Afield[i]);
801:           } else {
802:             MatCreateSubMatrix(pc->pmat,ilink->is,NULL,scall,&jac->Afield[i]);
803:           }
804:           ilink = ilink->next;
805:         }
806:       }
807:     }
808:   }

810:   if (jac->type == PC_COMPOSITE_SCHUR) {
811:     IS          ccis;
812:     PetscBool   isspd;
813:     PetscInt    rstart,rend;
814:     char        lscname[256];
815:     PetscObject LSC_L;

817:     if (nsplit != 2) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_INCOMP,"To use Schur complement preconditioner you must have exactly 2 fields");

819:     /* If pc->mat is SPD, don't scale by -1 the Schur complement */
820:     if (jac->schurscale == (PetscScalar)-1.0) {
821:       MatGetOption(pc->pmat,MAT_SPD,&isspd);
822:       jac->schurscale = (isspd == PETSC_TRUE) ? 1.0 : -1.0;
823:     }

825:     /* When extracting off-diagonal submatrices, we take complements from this range */
826:     MatGetOwnershipRangeColumn(pc->mat,&rstart,&rend);

828:     /* need to handle case when one is resetting up the preconditioner */
829:     if (jac->schur) {
830:       KSP kspA = jac->head->ksp, kspInner = NULL, kspUpper = jac->kspupper;

832:       MatSchurComplementGetKSP(jac->schur, &kspInner);
833:       ilink = jac->head;
834:       ISComplement(ilink->is_col,rstart,rend,&ccis);
835:       if (jac->offdiag_use_amat) {
836:         MatCreateSubMatrix(pc->mat,ilink->is,ccis,MAT_REUSE_MATRIX,&jac->B);
837:       } else {
838:         MatCreateSubMatrix(pc->pmat,ilink->is,ccis,MAT_REUSE_MATRIX,&jac->B);
839:       }
840:       ISDestroy(&ccis);
841:       ilink = ilink->next;
842:       ISComplement(ilink->is_col,rstart,rend,&ccis);
843:       if (jac->offdiag_use_amat) {
844:         MatCreateSubMatrix(pc->mat,ilink->is,ccis,MAT_REUSE_MATRIX,&jac->C);
845:       } else {
846:         MatCreateSubMatrix(pc->pmat,ilink->is,ccis,MAT_REUSE_MATRIX,&jac->C);
847:       }
848:       ISDestroy(&ccis);
849:       MatSchurComplementUpdateSubMatrices(jac->schur,jac->mat[0],jac->pmat[0],jac->B,jac->C,jac->mat[1]);
850:       if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELFP) {
851:         MatDestroy(&jac->schurp);
852:         MatSchurComplementGetPmat(jac->schur,MAT_INITIAL_MATRIX,&jac->schurp);
853:       }
854:       if (kspA != kspInner) {
855:         KSPSetOperators(kspA,jac->mat[0],jac->pmat[0]);
856:       }
857:       if (kspUpper != kspA) {
858:         KSPSetOperators(kspUpper,jac->mat[0],jac->pmat[0]);
859:       }
860:       KSPSetOperators(jac->kspschur,jac->schur,FieldSplitSchurPre(jac));
861:     } else {
862:       const char   *Dprefix;
863:       char         schurprefix[256], schurmatprefix[256];
864:       char         schurtestoption[256];
865:       MatNullSpace sp;
866:       PetscBool    flg;
867:       KSP          kspt;

869:       /* extract the A01 and A10 matrices */
870:       ilink = jac->head;
871:       ISComplement(ilink->is_col,rstart,rend,&ccis);
872:       if (jac->offdiag_use_amat) {
873:         MatCreateSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->B);
874:       } else {
875:         MatCreateSubMatrix(pc->pmat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->B);
876:       }
877:       ISDestroy(&ccis);
878:       ilink = ilink->next;
879:       ISComplement(ilink->is_col,rstart,rend,&ccis);
880:       if (jac->offdiag_use_amat) {
881:         MatCreateSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->C);
882:       } else {
883:         MatCreateSubMatrix(pc->pmat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->C);
884:       }
885:       ISDestroy(&ccis);

887:       /* Use mat[0] (diagonal block of Amat) preconditioned by pmat[0] to define Schur complement */
888:       MatCreate(((PetscObject)jac->mat[0])->comm,&jac->schur);
889:       MatSetType(jac->schur,MATSCHURCOMPLEMENT);
890:       MatSchurComplementSetSubMatrices(jac->schur,jac->mat[0],jac->pmat[0],jac->B,jac->C,jac->mat[1]);
891:       PetscSNPrintf(schurmatprefix, sizeof(schurmatprefix), "%sfieldsplit_%s_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname);
892:       MatSetOptionsPrefix(jac->schur,schurmatprefix);
893:       MatSchurComplementGetKSP(jac->schur,&kspt);
894:       KSPSetOptionsPrefix(kspt,schurmatprefix);

896:       /* Note: this is not true in general */
897:       MatGetNullSpace(jac->mat[1], &sp);
898:       if (sp) {
899:         MatSetNullSpace(jac->schur, sp);
900:       }

902:       PetscSNPrintf(schurtestoption, sizeof(schurtestoption), "-fieldsplit_%s_inner_", ilink->splitname);
903:       PetscOptionsFindPairPrefix_Private(((PetscObject)pc)->options,((PetscObject)pc)->prefix, schurtestoption, NULL, &flg);
904:       if (flg) {
905:         DM  dmInner;
906:         KSP kspInner;
907:         PC  pcInner;

909:         MatSchurComplementGetKSP(jac->schur, &kspInner);
910:         KSPReset(kspInner);
911:         KSPSetOperators(kspInner,jac->mat[0],jac->pmat[0]);
912:         PetscSNPrintf(schurprefix, sizeof(schurprefix), "%sfieldsplit_%s_inner_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname);
913:         /* Indent this deeper to emphasize the "inner" nature of this solver. */
914:         PetscObjectIncrementTabLevel((PetscObject)kspInner, (PetscObject) pc, 2);
915:         PetscObjectIncrementTabLevel((PetscObject)kspInner->pc, (PetscObject) pc, 2);
916:         KSPSetOptionsPrefix(kspInner, schurprefix);

918:         /* Set DM for new solver */
919:         KSPGetDM(jac->head->ksp, &dmInner);
920:         KSPSetDM(kspInner, dmInner);
921:         KSPSetDMActive(kspInner, PETSC_FALSE);

923:         /* Defaults to PCKSP as preconditioner */
924:         KSPGetPC(kspInner, &pcInner);
925:         PCSetType(pcInner, PCKSP);
926:         PCKSPSetKSP(pcInner, jac->head->ksp);
927:       } else {
928:          /* Use the outer solver for the inner solve, but revert the KSPPREONLY from PCFieldSplitSetFields_FieldSplit or
929:           * PCFieldSplitSetIS_FieldSplit. We don't want KSPPREONLY because it makes the Schur complement inexact,
930:           * preventing Schur complement reduction to be an accurate solve. Usually when an iterative solver is used for
931:           * S = D - C A_inner^{-1} B, we expect S to be defined using an accurate definition of A_inner^{-1}, so we make
932:           * GMRES the default. Note that it is also common to use PREONLY for S, in which case S may not be used
933:           * directly, and the user is responsible for setting an inexact method for fieldsplit's A^{-1}. */
934:         KSPSetType(jac->head->ksp,KSPGMRES);
935:         MatSchurComplementSetKSP(jac->schur,jac->head->ksp);
936:       }
937:       KSPSetOperators(jac->head->ksp,jac->mat[0],jac->pmat[0]);
938:       KSPSetFromOptions(jac->head->ksp);
939:       MatSetFromOptions(jac->schur);

941:       PetscObjectTypeCompare((PetscObject)jac->schur, MATSCHURCOMPLEMENT, &flg);
942:       if (flg) { /* Need to do this otherwise PCSetUp_KSP will overwrite the amat of jac->head->ksp */
943:         KSP kspInner;
944:         PC  pcInner;

946:         MatSchurComplementGetKSP(jac->schur, &kspInner);
947:         KSPGetPC(kspInner, &pcInner);
948:         PetscObjectTypeCompare((PetscObject)pcInner, PCKSP, &flg);
949:         if (flg) {
950:           KSP ksp;

952:           PCKSPGetKSP(pcInner, &ksp);
953:           if (ksp == jac->head->ksp) {
954:             PCSetUseAmat(pcInner, PETSC_TRUE);
955:           }
956:         }
957:       }
958:       PetscSNPrintf(schurtestoption, sizeof(schurtestoption), "-fieldsplit_%s_upper_", ilink->splitname);
959:       PetscOptionsFindPairPrefix_Private(((PetscObject)pc)->options,((PetscObject)pc)->prefix, schurtestoption, NULL, &flg);
960:       if (flg) {
961:         DM dmInner;

963:         PetscSNPrintf(schurprefix, sizeof(schurprefix), "%sfieldsplit_%s_upper_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname);
964:         KSPCreate(PetscObjectComm((PetscObject)pc), &jac->kspupper);
965:         KSPSetErrorIfNotConverged(jac->kspupper,pc->erroriffailure);
966:         KSPSetOptionsPrefix(jac->kspupper, schurprefix);
967:         PetscObjectIncrementTabLevel((PetscObject)jac->kspupper, (PetscObject) pc, 1);
968:         PetscObjectIncrementTabLevel((PetscObject)jac->kspupper->pc, (PetscObject) pc, 1);
969:         KSPGetDM(jac->head->ksp, &dmInner);
970:         KSPSetDM(jac->kspupper, dmInner);
971:         KSPSetDMActive(jac->kspupper, PETSC_FALSE);
972:         KSPSetFromOptions(jac->kspupper);
973:         KSPSetOperators(jac->kspupper,jac->mat[0],jac->pmat[0]);
974:         VecDuplicate(jac->head->x, &jac->head->z);
975:       } else {
976:         jac->kspupper = jac->head->ksp;
977:         PetscObjectReference((PetscObject) jac->head->ksp);
978:       }

980:       if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELFP) {
981:         MatSchurComplementGetPmat(jac->schur,MAT_INITIAL_MATRIX,&jac->schurp);
982:       }
983:       KSPCreate(PetscObjectComm((PetscObject)pc),&jac->kspschur);
984:       KSPSetErrorIfNotConverged(jac->kspschur,pc->erroriffailure);
985:       PetscLogObjectParent((PetscObject)pc,(PetscObject)jac->kspschur);
986:       PetscObjectIncrementTabLevel((PetscObject)jac->kspschur,(PetscObject)pc,1);
987:       if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELF) {
988:         PC pcschur;
989:         KSPGetPC(jac->kspschur,&pcschur);
990:         PCSetType(pcschur,PCNONE);
991:         /* Note: This is bad if there exist preconditioners for MATSCHURCOMPLEMENT */
992:       } else if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_FULL) {
993:         MatSchurComplementComputeExplicitOperator(jac->schur, &jac->schur_user);
994:       }
995:       KSPSetOperators(jac->kspschur,jac->schur,FieldSplitSchurPre(jac));
996:       KSPGetOptionsPrefix(jac->head->next->ksp, &Dprefix);
997:       KSPSetOptionsPrefix(jac->kspschur,         Dprefix);
998:       /* propagate DM */
999:       {
1000:         DM sdm;
1001:         KSPGetDM(jac->head->next->ksp, &sdm);
1002:         if (sdm) {
1003:           KSPSetDM(jac->kspschur, sdm);
1004:           KSPSetDMActive(jac->kspschur, PETSC_FALSE);
1005:         }
1006:       }
1007:       /* really want setfromoptions called in PCSetFromOptions_FieldSplit(), but it is not ready yet */
1008:       /* need to call this every time, since the jac->kspschur is freshly created, otherwise its options never get set */
1009:       KSPSetFromOptions(jac->kspschur);
1010:     }
1011:     MatAssemblyBegin(jac->schur,MAT_FINAL_ASSEMBLY);
1012:     MatAssemblyEnd(jac->schur,MAT_FINAL_ASSEMBLY);

1014:     /* HACK: special support to forward L and Lp matrices that might be used by PCLSC */
1015:     PetscSNPrintf(lscname,sizeof(lscname),"%s_LSC_L",ilink->splitname);
1016:     PetscObjectQuery((PetscObject)pc->mat,lscname,(PetscObject*)&LSC_L);
1017:     if (!LSC_L) {PetscObjectQuery((PetscObject)pc->pmat,lscname,(PetscObject*)&LSC_L);}
1018:     if (LSC_L) {PetscObjectCompose((PetscObject)jac->schur,"LSC_L",(PetscObject)LSC_L);}
1019:     PetscSNPrintf(lscname,sizeof(lscname),"%s_LSC_Lp",ilink->splitname);
1020:     PetscObjectQuery((PetscObject)pc->pmat,lscname,(PetscObject*)&LSC_L);
1021:     if (!LSC_L) {PetscObjectQuery((PetscObject)pc->mat,lscname,(PetscObject*)&LSC_L);}
1022:     if (LSC_L) {PetscObjectCompose((PetscObject)jac->schur,"LSC_Lp",(PetscObject)LSC_L);}
1023:   } else if (jac->type == PC_COMPOSITE_GKB) {
1024:     IS          ccis;
1025:     PetscInt    rstart,rend;

1027:     if (nsplit != 2) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_INCOMP,"To use GKB preconditioner you must have exactly 2 fields");

1029:     ilink = jac->head;

1031:     /* When extracting off-diagonal submatrices, we take complements from this range */
1032:     MatGetOwnershipRangeColumn(pc->mat,&rstart,&rend);

1034:     ISComplement(ilink->is_col,rstart,rend,&ccis);
1035:     if (jac->offdiag_use_amat) {
1036:      MatCreateSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->B);
1037:     } else {
1038:       MatCreateSubMatrix(pc->pmat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->B);
1039:     }
1040:     ISDestroy(&ccis);
1041:     /* Create work vectors for GKB algorithm */
1042:     VecDuplicate(ilink->x,&jac->u);
1043:     VecDuplicate(ilink->x,&jac->Hu);
1044:     VecDuplicate(ilink->x,&jac->w2);
1045:     ilink = ilink->next;
1046:     ISComplement(ilink->is_col,rstart,rend,&ccis);
1047:     if (jac->offdiag_use_amat) {
1048:       MatCreateSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->C);
1049:     } else {
1050:       MatCreateSubMatrix(pc->pmat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->C);
1051:     }
1052:     ISDestroy(&ccis);
1053:     /* Create work vectors for GKB algorithm */
1054:     VecDuplicate(ilink->x,&jac->v);
1055:     VecDuplicate(ilink->x,&jac->d);
1056:     VecDuplicate(ilink->x,&jac->w1);
1057:     MatGolubKahanComputeExplicitOperator(jac->mat[0],jac->B,jac->C,&jac->H,jac->gkbnu);
1058:     PetscCalloc1(jac->gkbdelay,&jac->vecz);

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

1085:   jac->suboptionsset = PETSC_TRUE;
1086:   return(0);
1087: }

1089: #define FieldSplitSplitSolveAdd(ilink,xx,yy) \
1090:   (VecScatterBegin(ilink->sctx,xx,ilink->x,INSERT_VALUES,SCATTER_FORWARD) || \
1091:    VecScatterEnd(ilink->sctx,xx,ilink->x,INSERT_VALUES,SCATTER_FORWARD) || \
1092:    PetscLogEventBegin(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL) ||\
1093:    KSPSolve(ilink->ksp,ilink->x,ilink->y) ||                               \
1094:    KSPCheckSolve(ilink->ksp,pc,ilink->y)  || \
1095:    PetscLogEventEnd(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL) ||\
1096:    VecScatterBegin(ilink->sctx,ilink->y,yy,ADD_VALUES,SCATTER_REVERSE) ||  \
1097:    VecScatterEnd(ilink->sctx,ilink->y,yy,ADD_VALUES,SCATTER_REVERSE))

1099: static PetscErrorCode PCApply_FieldSplit_Schur(PC pc,Vec x,Vec y)
1100: {
1101:   PC_FieldSplit      *jac = (PC_FieldSplit*)pc->data;
1102:   PetscErrorCode     ierr;
1103:   PC_FieldSplitLink  ilinkA = jac->head, ilinkD = ilinkA->next;
1104:   KSP                kspA   = ilinkA->ksp, kspLower = kspA, kspUpper = jac->kspupper;

1107:   switch (jac->schurfactorization) {
1108:   case PC_FIELDSPLIT_SCHUR_FACT_DIAG:
1109:     /* [A00 0; 0 -S], positive definite, suitable for MINRES */
1110:     VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
1111:     VecScatterBegin(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
1112:     VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
1113:     PetscLogEventBegin(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
1114:     KSPSolve(kspA,ilinkA->x,ilinkA->y);
1115:     KSPCheckSolve(kspA,pc,ilinkA->y);
1116:     PetscLogEventEnd(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
1117:     VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
1118:     VecScatterEnd(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
1119:     PetscLogEventBegin(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);
1120:     KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);
1121:     KSPCheckSolve(jac->kspschur,pc,ilinkD->y);
1122:     PetscLogEventEnd(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);
1123:     VecScale(ilinkD->y,jac->schurscale);
1124:     VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
1125:     VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
1126:     VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
1127:     break;
1128:   case PC_FIELDSPLIT_SCHUR_FACT_LOWER:
1129:     /* [A00 0; A10 S], suitable for left preconditioning */
1130:     VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
1131:     VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
1132:     PetscLogEventBegin(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
1133:     KSPSolve(kspA,ilinkA->x,ilinkA->y);
1134:     KSPCheckSolve(kspA,pc,ilinkA->y);
1135:     PetscLogEventEnd(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
1136:     MatMult(jac->C,ilinkA->y,ilinkD->x);
1137:     VecScale(ilinkD->x,-1.);
1138:     VecScatterBegin(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);
1139:     VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
1140:     VecScatterEnd(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);
1141:     PetscLogEventBegin(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);
1142:     KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);
1143:     KSPCheckSolve(jac->kspschur,pc,ilinkD->y);
1144:     PetscLogEventEnd(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);
1145:     VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
1146:     VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
1147:     VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
1148:     break;
1149:   case PC_FIELDSPLIT_SCHUR_FACT_UPPER:
1150:     /* [A00 A01; 0 S], suitable for right preconditioning */
1151:     VecScatterBegin(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
1152:     VecScatterEnd(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
1153:     PetscLogEventBegin(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);
1154:     KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);
1155:     KSPCheckSolve(jac->kspschur,pc,ilinkD->y);
1156:     PetscLogEventEnd(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);    MatMult(jac->B,ilinkD->y,ilinkA->x);
1157:     VecScale(ilinkA->x,-1.);
1158:     VecScatterBegin(ilinkA->sctx,x,ilinkA->x,ADD_VALUES,SCATTER_FORWARD);
1159:     VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
1160:     VecScatterEnd(ilinkA->sctx,x,ilinkA->x,ADD_VALUES,SCATTER_FORWARD);
1161:     PetscLogEventBegin(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
1162:     KSPSolve(kspA,ilinkA->x,ilinkA->y);
1163:     KSPCheckSolve(kspA,pc,ilinkA->y);
1164:     PetscLogEventEnd(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
1165:     VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
1166:     VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
1167:     VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
1168:     break;
1169:   case PC_FIELDSPLIT_SCHUR_FACT_FULL:
1170:     /* [1 0; A10 A00^{-1} 1] [A00 0; 0 S] [1 A00^{-1}A01; 0 1] */
1171:     VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
1172:     VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
1173:     PetscLogEventBegin(KSP_Solve_FS_L,kspLower,ilinkA->x,ilinkA->y,NULL);
1174:     KSPSolve(kspLower,ilinkA->x,ilinkA->y);
1175:     KSPCheckSolve(kspLower,pc,ilinkA->y);
1176:     PetscLogEventEnd(KSP_Solve_FS_L,kspLower,ilinkA->x,ilinkA->y,NULL);
1177:     MatMult(jac->C,ilinkA->y,ilinkD->x);
1178:     VecScale(ilinkD->x,-1.0);
1179:     VecScatterBegin(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);
1180:     VecScatterEnd(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);

1182:     PetscLogEventBegin(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);
1183:     KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);
1184:     KSPCheckSolve(jac->kspschur,pc,ilinkD->y);
1185:     PetscLogEventEnd(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);
1186:     VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);

1188:     if (kspUpper == kspA) {
1189:       MatMult(jac->B,ilinkD->y,ilinkA->y);
1190:       VecAXPY(ilinkA->x,-1.0,ilinkA->y);
1191:       PetscLogEventBegin(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
1192:       KSPSolve(kspA,ilinkA->x,ilinkA->y);
1193:       KSPCheckSolve(kspA,pc,ilinkA->y);
1194:       PetscLogEventEnd(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
1195:     } else {
1196:       PetscLogEventBegin(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
1197:       KSPSolve(kspA,ilinkA->x,ilinkA->y);
1198:       KSPCheckSolve(kspA,pc,ilinkA->y);
1199:       MatMult(jac->B,ilinkD->y,ilinkA->x);
1200:       PetscLogEventBegin(KSP_Solve_FS_U,kspUpper,ilinkA->x,ilinkA->z,NULL);
1201:       KSPSolve(kspUpper,ilinkA->x,ilinkA->z);
1202:       KSPCheckSolve(kspUpper,pc,ilinkA->z);
1203:       PetscLogEventEnd(KSP_Solve_FS_U,kspUpper,ilinkA->x,ilinkA->z,NULL);
1204:       VecAXPY(ilinkA->y,-1.0,ilinkA->z);
1205:     }
1206:     VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
1207:     VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
1208:     VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
1209:   }
1210:   return(0);
1211: }

1213: static PetscErrorCode PCApply_FieldSplit(PC pc,Vec x,Vec y)
1214: {
1215:   PC_FieldSplit      *jac = (PC_FieldSplit*)pc->data;
1216:   PetscErrorCode     ierr;
1217:   PC_FieldSplitLink  ilink = jac->head;
1218:   PetscInt           cnt,bs;

1221:   if (jac->type == PC_COMPOSITE_ADDITIVE) {
1222:     if (jac->defaultsplit) {
1223:       VecGetBlockSize(x,&bs);
1224:       if (jac->bs > 0 && bs != jac->bs) SETERRQ2(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Blocksize of x vector %D does not match fieldsplit blocksize %D",bs,jac->bs);
1225:       VecGetBlockSize(y,&bs);
1226:       if (jac->bs > 0 && bs != jac->bs) SETERRQ2(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Blocksize of y vector %D does not match fieldsplit blocksize %D",bs,jac->bs);
1227:       VecStrideGatherAll(x,jac->x,INSERT_VALUES);
1228:       while (ilink) {
1229:         PetscLogEventBegin(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1230:         KSPSolve(ilink->ksp,ilink->x,ilink->y);
1231:         KSPCheckSolve(ilink->ksp,pc,ilink->y);
1232:         PetscLogEventEnd(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1233:         ilink = ilink->next;
1234:       }
1235:       VecStrideScatterAll(jac->y,y,INSERT_VALUES);
1236:     } else {
1237:       VecSet(y,0.0);
1238:       while (ilink) {
1239:         FieldSplitSplitSolveAdd(ilink,x,y);
1240:         ilink = ilink->next;
1241:       }
1242:     }
1243:   } else if (jac->type == PC_COMPOSITE_MULTIPLICATIVE && jac->nsplits == 2) {
1244:     VecSet(y,0.0);
1245:     /* solve on first block for first block variables */
1246:     VecScatterBegin(ilink->sctx,x,ilink->x,INSERT_VALUES,SCATTER_FORWARD);
1247:     VecScatterEnd(ilink->sctx,x,ilink->x,INSERT_VALUES,SCATTER_FORWARD);
1248:     PetscLogEventBegin(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1249:     KSPSolve(ilink->ksp,ilink->x,ilink->y);
1250:     KSPCheckSolve(ilink->ksp,pc,ilink->y);
1251:     PetscLogEventEnd(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1252:     VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
1253:     VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);

1255:     /* compute the residual only onto second block variables using first block variables */
1256:     MatMult(jac->Afield[1],ilink->y,ilink->next->x);
1257:     ilink = ilink->next;
1258:     VecScale(ilink->x,-1.0);
1259:     VecScatterBegin(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
1260:     VecScatterEnd(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);

1262:     /* solve on second block variables */
1263:     PetscLogEventBegin(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1264:     KSPSolve(ilink->ksp,ilink->x,ilink->y);
1265:     KSPCheckSolve(ilink->ksp,pc,ilink->y);
1266:     PetscLogEventEnd(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1267:     VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
1268:     VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
1269:   } else if (jac->type == PC_COMPOSITE_MULTIPLICATIVE || jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
1270:     if (!jac->w1) {
1271:       VecDuplicate(x,&jac->w1);
1272:       VecDuplicate(x,&jac->w2);
1273:     }
1274:     VecSet(y,0.0);
1275:     FieldSplitSplitSolveAdd(ilink,x,y);
1276:     cnt  = 1;
1277:     while (ilink->next) {
1278:       ilink = ilink->next;
1279:       /* compute the residual only over the part of the vector needed */
1280:       MatMult(jac->Afield[cnt++],y,ilink->x);
1281:       VecScale(ilink->x,-1.0);
1282:       VecScatterBegin(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
1283:       VecScatterEnd(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
1284:       PetscLogEventBegin(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1285:       KSPSolve(ilink->ksp,ilink->x,ilink->y);
1286:       KSPCheckSolve(ilink->ksp,pc,ilink->y);
1287:       PetscLogEventEnd(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1288:       VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
1289:       VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
1290:     }
1291:     if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
1292:       cnt -= 2;
1293:       while (ilink->previous) {
1294:         ilink = ilink->previous;
1295:         /* compute the residual only over the part of the vector needed */
1296:         MatMult(jac->Afield[cnt--],y,ilink->x);
1297:         VecScale(ilink->x,-1.0);
1298:         VecScatterBegin(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
1299:         VecScatterEnd(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
1300:         PetscLogEventBegin(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1301:         KSPSolve(ilink->ksp,ilink->x,ilink->y);
1302:         KSPCheckSolve(ilink->ksp,pc,ilink->y);
1303:         PetscLogEventEnd(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1304:         VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
1305:         VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
1306:       }
1307:     }
1308:   } else SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Unsupported or unknown composition",(int) jac->type);
1309:   return(0);
1310: }


1313: static PetscErrorCode PCApply_FieldSplit_GKB(PC pc,Vec x,Vec y)
1314: {
1315:   PC_FieldSplit      *jac = (PC_FieldSplit*)pc->data;
1316:   PetscErrorCode     ierr;
1317:   PC_FieldSplitLink  ilinkA = jac->head,ilinkD = ilinkA->next;
1318:   KSP                ksp = ilinkA->ksp;
1319:   Vec                u,v,Hu,d,work1,work2;
1320:   PetscScalar        alpha,z,nrmz2,*vecz;
1321:   PetscReal          lowbnd,nu,beta;
1322:   PetscInt           j,iterGKB;

1325:   VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
1326:   VecScatterBegin(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
1327:   VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
1328:   VecScatterEnd(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);

1330:   u     = jac->u;
1331:   v     = jac->v;
1332:   Hu    = jac->Hu;
1333:   d     = jac->d;
1334:   work1 = jac->w1;
1335:   work2 = jac->w2;
1336:   vecz  = jac->vecz;

1338:   /* Change RHS to comply with matrix regularization H = A + nu*B*B' */
1339:   /* Add q = q + nu*B*b */
1340:   if (jac->gkbnu) {
1341:     nu = jac->gkbnu;
1342:     VecScale(ilinkD->x,jac->gkbnu);
1343:     MatMultAdd(jac->B,ilinkD->x,ilinkA->x,ilinkA->x);            /* q = q + nu*B*b */
1344:   } else {
1345:     /* Situation when no augmented Lagrangian is used. Then we set inner  */
1346:     /* matrix N = I in [Ar13], and thus nu = 1.                           */
1347:     nu = 1;
1348:   }

1350:   /* Transform rhs from [q,tilde{b}] to [0,b] */
1351:   PetscLogEventBegin(ilinkA->event,ksp,ilinkA->x,ilinkA->y,NULL);
1352:   KSPSolve(ksp,ilinkA->x,ilinkA->y);
1353:   KSPCheckSolve(ksp,pc,ilinkA->y);
1354:   PetscLogEventEnd(ilinkA->event,ksp,ilinkA->x,ilinkA->y,NULL);
1355:   MatMultHermitianTranspose(jac->B,ilinkA->y,work1);
1356:   VecAXPBY(work1,1.0/nu,-1.0,ilinkD->x);            /* c = b - B'*x        */

1358:   /* First step of algorithm */
1359:   VecNorm(work1,NORM_2,&beta);                   /* beta = sqrt(nu*c'*c)*/
1360:   KSPCheckDot(ksp,beta);
1361:   beta  = PetscSqrtScalar(nu)*beta;
1362:   VecAXPBY(v,nu/beta,0.0,work1);                   /* v = nu/beta *c      */
1363:   MatMult(jac->B,v,work2);                       /* u = H^{-1}*B*v      */
1364:   PetscLogEventBegin(ilinkA->event,ksp,work2,u,NULL);
1365:   KSPSolve(ksp,work2,u);
1366:   KSPCheckSolve(ksp,pc,u);
1367:   PetscLogEventEnd(ilinkA->event,ksp,work2,u,NULL);
1368:   MatMult(jac->H,u,Hu);                          /* alpha = u'*H*u      */
1369:   VecDot(Hu,u,&alpha);
1370:   KSPCheckDot(ksp,alpha);
1371:   if (PetscRealPart(alpha) <= 0.0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_NOT_CONVERGED,"GKB preconditioner diverged, H is not positive definite");
1372:   alpha = PetscSqrtScalar(PetscAbsScalar(alpha));
1373:   VecScale(u,1.0/alpha);
1374:   VecAXPBY(d,1.0/alpha,0.0,v);                       /* v = nu/beta *c      */

1376:   z = beta/alpha;
1377:   vecz[1] = z;

1379:   /* Computation of first iterate x(1) and p(1) */
1380:   VecAXPY(ilinkA->y,z,u);
1381:   VecCopy(d,ilinkD->y);
1382:   VecScale(ilinkD->y,-z);

1384:   iterGKB = 1; lowbnd = 2*jac->gkbtol;
1385:   if (jac->gkbmonitor) {
1386:       PetscViewerASCIIPrintf(jac->gkbviewer,"%3D GKB Lower bound estimate %14.12e\n",iterGKB,lowbnd);
1387:   }

1389:   while (iterGKB < jac->gkbmaxit && lowbnd > jac->gkbtol) {
1390:     iterGKB += 1;
1391:     MatMultHermitianTranspose(jac->B,u,work1); /* v <- nu*(B'*u-alpha/nu*v) */
1392:     VecAXPBY(v,nu,-alpha,work1);
1393:     VecNorm(v,NORM_2,&beta);                   /* beta = sqrt(nu)*v'*v      */
1394:     beta  = beta/PetscSqrtScalar(nu);
1395:     VecScale(v,1.0/beta);
1396:     MatMult(jac->B,v,work2);                  /* u <- H^{-1}*(B*v-beta*H*u) */
1397:     MatMult(jac->H,u,Hu);
1398:     VecAXPY(work2,-beta,Hu);
1399:     PetscLogEventBegin(ilinkA->event,ksp,work2,u,NULL);
1400:     KSPSolve(ksp,work2,u);
1401:     KSPCheckSolve(ksp,pc,u);
1402:     PetscLogEventEnd(ilinkA->event,ksp,work2,u,NULL);
1403:     MatMult(jac->H,u,Hu);                      /* alpha = u'*H*u            */
1404:     VecDot(Hu,u,&alpha);
1405:     KSPCheckDot(ksp,alpha);
1406:     if (PetscRealPart(alpha) <= 0.0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_NOT_CONVERGED,"GKB preconditioner diverged, H is not positive definite");
1407:     alpha = PetscSqrtScalar(PetscAbsScalar(alpha));
1408:     VecScale(u,1.0/alpha);

1410:     z = -beta/alpha*z;                                            /* z <- beta/alpha*z     */
1411:     vecz[0] = z;

1413:     /* Computation of new iterate x(i+1) and p(i+1) */
1414:     VecAXPBY(d,1.0/alpha,-beta/alpha,v);       /* d = (v-beta*d)/alpha */
1415:     VecAXPY(ilinkA->y,z,u);                  /* r = r + z*u          */
1416:     VecAXPY(ilinkD->y,-z,d);                 /* p = p - z*d          */
1417:     MatMult(jac->H,ilinkA->y,Hu);            /* ||u||_H = u'*H*u     */
1418:     VecDot(Hu,ilinkA->y,&nrmz2);

1420:     /* Compute Lower Bound estimate */
1421:     if (iterGKB > jac->gkbdelay) {
1422:       lowbnd = 0.0;
1423:       for (j=0; j<jac->gkbdelay; j++) {
1424:         lowbnd += PetscAbsScalar(vecz[j]*vecz[j]);
1425:       }
1426:       lowbnd = PetscSqrtScalar(lowbnd/PetscAbsScalar(nrmz2));
1427:     }

1429:     for (j=0; j<jac->gkbdelay-1; j++) {
1430:       vecz[jac->gkbdelay-j-1] = vecz[jac->gkbdelay-j-2];
1431:     }
1432:     if (jac->gkbmonitor) {
1433:       PetscViewerASCIIPrintf(jac->gkbviewer,"%3D GKB Lower bound estimate %14.12e\n",iterGKB,lowbnd);
1434:     }
1435:   }

1437:   VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
1438:   VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
1439:   VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
1440:   VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);

1442:   return(0);
1443: }


1446: #define FieldSplitSplitSolveAddTranspose(ilink,xx,yy) \
1447:   (VecScatterBegin(ilink->sctx,xx,ilink->y,INSERT_VALUES,SCATTER_FORWARD) || \
1448:    VecScatterEnd(ilink->sctx,xx,ilink->y,INSERT_VALUES,SCATTER_FORWARD) || \
1449:    PetscLogEventBegin(ilink->event,ilink->ksp,ilink->y,ilink->x,NULL) || \
1450:    KSPSolveTranspose(ilink->ksp,ilink->y,ilink->x) ||                  \
1451:    KSPCheckSolve(ilink->ksp,pc,ilink->x) || \
1452:    PetscLogEventEnd(ilink->event,ilink->ksp,ilink->y,ilink->x,NULL) ||   \
1453:    VecScatterBegin(ilink->sctx,ilink->x,yy,ADD_VALUES,SCATTER_REVERSE) || \
1454:    VecScatterEnd(ilink->sctx,ilink->x,yy,ADD_VALUES,SCATTER_REVERSE))

1456: static PetscErrorCode PCApplyTranspose_FieldSplit(PC pc,Vec x,Vec y)
1457: {
1458:   PC_FieldSplit      *jac = (PC_FieldSplit*)pc->data;
1459:   PetscErrorCode     ierr;
1460:   PC_FieldSplitLink  ilink = jac->head;
1461:   PetscInt           bs;

1464:   if (jac->type == PC_COMPOSITE_ADDITIVE) {
1465:     if (jac->defaultsplit) {
1466:       VecGetBlockSize(x,&bs);
1467:       if (jac->bs > 0 && bs != jac->bs) SETERRQ2(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Blocksize of x vector %D does not match fieldsplit blocksize %D",bs,jac->bs);
1468:       VecGetBlockSize(y,&bs);
1469:       if (jac->bs > 0 && bs != jac->bs) SETERRQ2(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Blocksize of y vector %D does not match fieldsplit blocksize %D",bs,jac->bs);
1470:       VecStrideGatherAll(x,jac->x,INSERT_VALUES);
1471:       while (ilink) {
1472:         PetscLogEventBegin(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1473:         KSPSolveTranspose(ilink->ksp,ilink->x,ilink->y);
1474:         KSPCheckSolve(ilink->ksp,pc,ilink->y);
1475:         PetscLogEventEnd(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1476:         ilink = ilink->next;
1477:       }
1478:       VecStrideScatterAll(jac->y,y,INSERT_VALUES);
1479:     } else {
1480:       VecSet(y,0.0);
1481:       while (ilink) {
1482:         FieldSplitSplitSolveAddTranspose(ilink,x,y);
1483:         ilink = ilink->next;
1484:       }
1485:     }
1486:   } else {
1487:     if (!jac->w1) {
1488:       VecDuplicate(x,&jac->w1);
1489:       VecDuplicate(x,&jac->w2);
1490:     }
1491:     VecSet(y,0.0);
1492:     if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
1493:       FieldSplitSplitSolveAddTranspose(ilink,x,y);
1494:       while (ilink->next) {
1495:         ilink = ilink->next;
1496:         MatMultTranspose(pc->mat,y,jac->w1);
1497:         VecWAXPY(jac->w2,-1.0,jac->w1,x);
1498:         FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);
1499:       }
1500:       while (ilink->previous) {
1501:         ilink = ilink->previous;
1502:         MatMultTranspose(pc->mat,y,jac->w1);
1503:         VecWAXPY(jac->w2,-1.0,jac->w1,x);
1504:         FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);
1505:       }
1506:     } else {
1507:       while (ilink->next) {   /* get to last entry in linked list */
1508:         ilink = ilink->next;
1509:       }
1510:       FieldSplitSplitSolveAddTranspose(ilink,x,y);
1511:       while (ilink->previous) {
1512:         ilink = ilink->previous;
1513:         MatMultTranspose(pc->mat,y,jac->w1);
1514:         VecWAXPY(jac->w2,-1.0,jac->w1,x);
1515:         FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);
1516:       }
1517:     }
1518:   }
1519:   return(0);
1520: }

1522: static PetscErrorCode PCReset_FieldSplit(PC pc)
1523: {
1524:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1525:   PetscErrorCode    ierr;
1526:   PC_FieldSplitLink ilink = jac->head,next;

1529:   while (ilink) {
1530:     KSPDestroy(&ilink->ksp);
1531:     VecDestroy(&ilink->x);
1532:     VecDestroy(&ilink->y);
1533:     VecDestroy(&ilink->z);
1534:     VecScatterDestroy(&ilink->sctx);
1535:     ISDestroy(&ilink->is);
1536:     ISDestroy(&ilink->is_col);
1537:     PetscFree(ilink->splitname);
1538:     PetscFree(ilink->fields);
1539:     PetscFree(ilink->fields_col);
1540:     next  = ilink->next;
1541:     PetscFree(ilink);
1542:     ilink = next;
1543:   }
1544:   jac->head = NULL;
1545:   PetscFree2(jac->x,jac->y);
1546:   if (jac->mat && jac->mat != jac->pmat) {
1547:     MatDestroyMatrices(jac->nsplits,&jac->mat);
1548:   } else if (jac->mat) {
1549:     jac->mat = NULL;
1550:   }
1551:   if (jac->pmat) {MatDestroyMatrices(jac->nsplits,&jac->pmat);}
1552:   if (jac->Afield) {MatDestroyMatrices(jac->nsplits,&jac->Afield);}
1553:   jac->nsplits = 0;
1554:   VecDestroy(&jac->w1);
1555:   VecDestroy(&jac->w2);
1556:   MatDestroy(&jac->schur);
1557:   MatDestroy(&jac->schurp);
1558:   MatDestroy(&jac->schur_user);
1559:   KSPDestroy(&jac->kspschur);
1560:   KSPDestroy(&jac->kspupper);
1561:   MatDestroy(&jac->B);
1562:   MatDestroy(&jac->C);
1563:   MatDestroy(&jac->H);
1564:   VecDestroy(&jac->u);
1565:   VecDestroy(&jac->v);
1566:   VecDestroy(&jac->Hu);
1567:   VecDestroy(&jac->d);
1568:   PetscFree(jac->vecz);
1569:   PetscViewerDestroy(&jac->gkbviewer);
1570:   jac->isrestrict = PETSC_FALSE;
1571:   return(0);
1572: }

1574: static PetscErrorCode PCDestroy_FieldSplit(PC pc)
1575: {
1576:   PetscErrorCode    ierr;

1579:   PCReset_FieldSplit(pc);
1580:   PetscFree(pc->data);
1581:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSchurGetSubKSP_C",NULL);
1582:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",NULL);
1583:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetFields_C",NULL);
1584:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetIS_C",NULL);
1585:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetType_C",NULL);
1586:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetBlockSize_C",NULL);
1587:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurPre_C",NULL);
1588:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSchurPre_C",NULL);
1589:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C",NULL);
1590:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitRestrictIS_C",NULL);
1591:   return(0);
1592: }

1594: static PetscErrorCode PCSetFromOptions_FieldSplit(PetscOptionItems *PetscOptionsObject,PC pc)
1595: {
1596:   PetscErrorCode  ierr;
1597:   PetscInt        bs;
1598:   PetscBool       flg;
1599:   PC_FieldSplit   *jac = (PC_FieldSplit*)pc->data;
1600:   PCCompositeType ctype;

1603:   PetscOptionsHead(PetscOptionsObject,"FieldSplit options");
1604:   PetscOptionsBool("-pc_fieldsplit_dm_splits","Whether to use DMCreateFieldDecomposition() for splits","PCFieldSplitSetDMSplits",jac->dm_splits,&jac->dm_splits,NULL);
1605:   PetscOptionsInt("-pc_fieldsplit_block_size","Blocksize that defines number of fields","PCFieldSplitSetBlockSize",jac->bs,&bs,&flg);
1606:   if (flg) {
1607:     PCFieldSplitSetBlockSize(pc,bs);
1608:   }
1609:   jac->diag_use_amat = pc->useAmat;
1610:   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);
1611:   jac->offdiag_use_amat = pc->useAmat;
1612:   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);
1613:   PetscOptionsBool("-pc_fieldsplit_detect_saddle_point","Form 2-way split by detecting zero diagonal entries", "PCFieldSplitSetDetectSaddlePoint",jac->detect,&jac->detect,NULL);
1614:   PCFieldSplitSetDetectSaddlePoint(pc,jac->detect); /* Sets split type and Schur PC type */
1615:   PetscOptionsEnum("-pc_fieldsplit_type","Type of composition","PCFieldSplitSetType",PCCompositeTypes,(PetscEnum)jac->type,(PetscEnum*)&ctype,&flg);
1616:   if (flg) {
1617:     PCFieldSplitSetType(pc,ctype);
1618:   }
1619:   /* Only setup fields once */
1620:   if ((jac->bs > 0) && (jac->nsplits == 0)) {
1621:     /* only allow user to set fields from command line if bs is already known.
1622:        otherwise user can set them in PCFieldSplitSetDefaults() */
1623:     PCFieldSplitSetRuntimeSplits_Private(pc);
1624:     if (jac->splitdefined) {PetscInfo(pc,"Splits defined using the options database\n");}
1625:   }
1626:   if (jac->type == PC_COMPOSITE_SCHUR) {
1627:     PetscOptionsGetEnum(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_fieldsplit_schur_factorization_type",PCFieldSplitSchurFactTypes,(PetscEnum*)&jac->schurfactorization,&flg);
1628:     if (flg) {PetscInfo(pc,"Deprecated use of -pc_fieldsplit_schur_factorization_type\n");}
1629:     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);
1630:     PetscOptionsEnum("-pc_fieldsplit_schur_precondition","How to build preconditioner for Schur complement","PCFieldSplitSetSchurPre",PCFieldSplitSchurPreTypes,(PetscEnum)jac->schurpre,(PetscEnum*)&jac->schurpre,NULL);
1631:     PetscOptionsScalar("-pc_fieldsplit_schur_scale","Scale Schur complement","PCFieldSplitSetSchurScale",jac->schurscale,&jac->schurscale,NULL);
1632:   } else if (jac->type == PC_COMPOSITE_GKB) {
1633:     PetscOptionsReal("-pc_fieldsplit_gkb_tol","The tolerance for the lower bound stopping criterion","PCFieldSplitGKBTol",jac->gkbtol,&jac->gkbtol,NULL);
1634:     PetscOptionsInt("-pc_fieldsplit_gkb_delay","The delay value for lower bound criterion","PCFieldSplitGKBDelay",jac->gkbdelay,&jac->gkbdelay,NULL);
1635:     PetscOptionsReal("-pc_fieldsplit_gkb_nu","Parameter in augmented Lagrangian approach","PCFieldSplitGKBNu",jac->gkbnu,&jac->gkbnu,NULL);
1636:     if (jac->gkbnu < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nu cannot be less than 0: value %f",jac->gkbnu);
1637:     PetscOptionsInt("-pc_fieldsplit_gkb_maxit","Maximum allowed number of iterations","PCFieldSplitGKBMaxit",jac->gkbmaxit,&jac->gkbmaxit,NULL);
1638:     PetscOptionsBool("-pc_fieldsplit_gkb_monitor","Prints number of GKB iterations and error","PCFieldSplitGKB",jac->gkbmonitor,&jac->gkbmonitor,NULL);
1639:   }
1640:   PetscOptionsTail();
1641:   return(0);
1642: }

1644: /*------------------------------------------------------------------------------------*/

1646: static PetscErrorCode  PCFieldSplitSetFields_FieldSplit(PC pc,const char splitname[],PetscInt n,const PetscInt *fields,const PetscInt *fields_col)
1647: {
1648:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1649:   PetscErrorCode    ierr;
1650:   PC_FieldSplitLink ilink,next = jac->head;
1651:   char              prefix[128];
1652:   PetscInt          i;

1655:   if (jac->splitdefined) {
1656:     PetscInfo1(pc,"Ignoring new split \"%s\" because the splits have already been defined\n",splitname);
1657:     return(0);
1658:   }
1659:   for (i=0; i<n; i++) {
1660:     if (fields[i] >= jac->bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Field %D requested but only %D exist",fields[i],jac->bs);
1661:     if (fields[i] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative field %D requested",fields[i]);
1662:   }
1663:   PetscNew(&ilink);
1664:   if (splitname) {
1665:     PetscStrallocpy(splitname,&ilink->splitname);
1666:   } else {
1667:     PetscMalloc1(3,&ilink->splitname);
1668:     PetscSNPrintf(ilink->splitname,2,"%s",jac->nsplits);
1669:   }
1670:   ilink->event = jac->nsplits < 5 ? KSP_Solve_FS_0 + jac->nsplits : KSP_Solve_FS_0 + 4; /* Any split great than 4 gets logged in the 4th split */
1671:   PetscMalloc1(n,&ilink->fields);
1672:   PetscArraycpy(ilink->fields,fields,n);
1673:   PetscMalloc1(n,&ilink->fields_col);
1674:   PetscArraycpy(ilink->fields_col,fields_col,n);

1676:   ilink->nfields = n;
1677:   ilink->next    = NULL;
1678:   KSPCreate(PetscObjectComm((PetscObject)pc),&ilink->ksp);
1679:   KSPSetErrorIfNotConverged(ilink->ksp,pc->erroriffailure);
1680:   PetscObjectIncrementTabLevel((PetscObject)ilink->ksp,(PetscObject)pc,1);
1681:   KSPSetType(ilink->ksp,KSPPREONLY);
1682:   PetscLogObjectParent((PetscObject)pc,(PetscObject)ilink->ksp);

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

1687:   if (!next) {
1688:     jac->head       = ilink;
1689:     ilink->previous = NULL;
1690:   } else {
1691:     while (next->next) {
1692:       next = next->next;
1693:     }
1694:     next->next      = ilink;
1695:     ilink->previous = next;
1696:   }
1697:   jac->nsplits++;
1698:   return(0);
1699: }

1701: static PetscErrorCode  PCFieldSplitSchurGetSubKSP_FieldSplit(PC pc,PetscInt *n,KSP **subksp)
1702: {
1703:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;

1707:   *subksp = NULL;
1708:   if (n) *n = 0;
1709:   if (jac->type == PC_COMPOSITE_SCHUR) {
1710:     PetscInt nn;

1712:     if (!jac->schur) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must call KSPSetUp() or PCSetUp() before calling PCFieldSplitSchurGetSubKSP()");
1713:     if (jac->nsplits != 2) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_PLIB,"Unexpected number of splits %D != 2",jac->nsplits);
1714:     nn   = jac->nsplits + (jac->kspupper != jac->head->ksp ? 1 : 0);
1715:     PetscMalloc1(nn,subksp);
1716:     (*subksp)[0] = jac->head->ksp;
1717:     (*subksp)[1] = jac->kspschur;
1718:     if (jac->kspupper != jac->head->ksp) (*subksp)[2] = jac->kspupper;
1719:     if (n) *n = nn;
1720:   }
1721:   return(0);
1722: }

1724: static PetscErrorCode  PCFieldSplitGetSubKSP_FieldSplit_Schur(PC pc,PetscInt *n,KSP **subksp)
1725: {
1726:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;

1730:   if (!jac->schur) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must call KSPSetUp() or PCSetUp() before calling PCFieldSplitGetSubKSP()");
1731:   PetscMalloc1(jac->nsplits,subksp);
1732:   MatSchurComplementGetKSP(jac->schur,*subksp);

1734:   (*subksp)[1] = jac->kspschur;
1735:   if (n) *n = jac->nsplits;
1736:   return(0);
1737: }

1739: static PetscErrorCode  PCFieldSplitGetSubKSP_FieldSplit(PC pc,PetscInt *n,KSP **subksp)
1740: {
1741:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1742:   PetscErrorCode    ierr;
1743:   PetscInt          cnt   = 0;
1744:   PC_FieldSplitLink ilink = jac->head;

1747:   PetscMalloc1(jac->nsplits,subksp);
1748:   while (ilink) {
1749:     (*subksp)[cnt++] = ilink->ksp;
1750:     ilink            = ilink->next;
1751:   }
1752:   if (cnt != jac->nsplits) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupt PCFIELDSPLIT object: number of splits in linked list %D does not match number in object %D",cnt,jac->nsplits);
1753:   if (n) *n = jac->nsplits;
1754:   return(0);
1755: }

1757: /*@C
1758:     PCFieldSplitRestrictIS - Restricts the fieldsplit ISs to be within a given IS.

1760:     Input Parameters:
1761: +   pc  - the preconditioner context
1762: -   is - the index set that defines the indices to which the fieldsplit is to be restricted

1764:     Level: advanced

1766: @*/
1767: PetscErrorCode  PCFieldSplitRestrictIS(PC pc,IS isy)
1768: {

1774:   PetscTryMethod(pc,"PCFieldSplitRestrictIS_C",(PC,IS),(pc,isy));
1775:   return(0);
1776: }


1779: static PetscErrorCode  PCFieldSplitRestrictIS_FieldSplit(PC pc, IS isy)
1780: {
1781:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1782:   PetscErrorCode    ierr;
1783:   PC_FieldSplitLink ilink = jac->head, next;
1784:   PetscInt          localsize,size,sizez,i;
1785:   const PetscInt    *ind, *indz;
1786:   PetscInt          *indc, *indcz;
1787:   PetscBool         flg;

1790:   ISGetLocalSize(isy,&localsize);
1791:   MPI_Scan(&localsize,&size,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)isy));
1792:   size -= localsize;
1793:   while(ilink) {
1794:     IS isrl,isr;
1795:     PC subpc;
1796:     ISEmbed(ilink->is, isy, PETSC_TRUE, &isrl);
1797:     ISGetLocalSize(isrl,&localsize);
1798:     PetscMalloc1(localsize,&indc);
1799:     ISGetIndices(isrl,&ind);
1800:     PetscArraycpy(indc,ind,localsize);
1801:     ISRestoreIndices(isrl,&ind);
1802:     ISDestroy(&isrl);
1803:     for (i=0; i<localsize; i++) *(indc+i) += size;
1804:     ISCreateGeneral(PetscObjectComm((PetscObject)isy),localsize,indc,PETSC_OWN_POINTER,&isr);
1805:     PetscObjectReference((PetscObject)isr);
1806:     ISDestroy(&ilink->is);
1807:     ilink->is     = isr;
1808:     PetscObjectReference((PetscObject)isr);
1809:     ISDestroy(&ilink->is_col);
1810:     ilink->is_col = isr;
1811:     ISDestroy(&isr);
1812:     KSPGetPC(ilink->ksp, &subpc);
1813:     PetscObjectTypeCompare((PetscObject)subpc,PCFIELDSPLIT,&flg);
1814:     if(flg) {
1815:       IS iszl,isz;
1816:       MPI_Comm comm;
1817:       ISGetLocalSize(ilink->is,&localsize);
1818:       comm   = PetscObjectComm((PetscObject)ilink->is);
1819:       ISEmbed(isy, ilink->is, PETSC_TRUE, &iszl);
1820:       MPI_Scan(&localsize,&sizez,1,MPIU_INT,MPI_SUM,comm);
1821:       sizez -= localsize;
1822:       ISGetLocalSize(iszl,&localsize);
1823:       PetscMalloc1(localsize,&indcz);
1824:       ISGetIndices(iszl,&indz);
1825:       PetscArraycpy(indcz,indz,localsize);
1826:       ISRestoreIndices(iszl,&indz);
1827:       ISDestroy(&iszl);
1828:       for (i=0; i<localsize; i++) *(indcz+i) += sizez;
1829:       ISCreateGeneral(comm,localsize,indcz,PETSC_OWN_POINTER,&isz);
1830:       PCFieldSplitRestrictIS(subpc,isz);
1831:       ISDestroy(&isz);
1832:     }
1833:     next = ilink->next;
1834:     ilink = next;
1835:   }
1836:   jac->isrestrict = PETSC_TRUE;
1837:   return(0);
1838: }

1840: static PetscErrorCode  PCFieldSplitSetIS_FieldSplit(PC pc,const char splitname[],IS is)
1841: {
1842:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1843:   PetscErrorCode    ierr;
1844:   PC_FieldSplitLink ilink, next = jac->head;
1845:   char              prefix[128];

1848:   if (jac->splitdefined) {
1849:     PetscInfo1(pc,"Ignoring new split \"%s\" because the splits have already been defined\n",splitname);
1850:     return(0);
1851:   }
1852:   PetscNew(&ilink);
1853:   if (splitname) {
1854:     PetscStrallocpy(splitname,&ilink->splitname);
1855:   } else {
1856:     PetscMalloc1(8,&ilink->splitname);
1857:     PetscSNPrintf(ilink->splitname,7,"%D",jac->nsplits);
1858:   }
1859:   ilink->event  = jac->nsplits < 5 ? KSP_Solve_FS_0 + jac->nsplits : KSP_Solve_FS_0 + 4; /* Any split great than 4 gets logged in the 4th split */
1860:   PetscObjectReference((PetscObject)is);
1861:   ISDestroy(&ilink->is);
1862:   ilink->is     = is;
1863:   PetscObjectReference((PetscObject)is);
1864:   ISDestroy(&ilink->is_col);
1865:   ilink->is_col = is;
1866:   ilink->next   = NULL;
1867:   KSPCreate(PetscObjectComm((PetscObject)pc),&ilink->ksp);
1868:   KSPSetErrorIfNotConverged(ilink->ksp,pc->erroriffailure);
1869:   PetscObjectIncrementTabLevel((PetscObject)ilink->ksp,(PetscObject)pc,1);
1870:   KSPSetType(ilink->ksp,KSPPREONLY);
1871:   PetscLogObjectParent((PetscObject)pc,(PetscObject)ilink->ksp);

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

1876:   if (!next) {
1877:     jac->head       = ilink;
1878:     ilink->previous = NULL;
1879:   } else {
1880:     while (next->next) {
1881:       next = next->next;
1882:     }
1883:     next->next      = ilink;
1884:     ilink->previous = next;
1885:   }
1886:   jac->nsplits++;
1887:   return(0);
1888: }

1890: /*@C
1891:     PCFieldSplitSetFields - Sets the fields for one particular split in the field split preconditioner

1893:     Logically Collective on PC

1895:     Input Parameters:
1896: +   pc  - the preconditioner context
1897: .   splitname - name of this split, if NULL the number of the split is used
1898: .   n - the number of fields in this split
1899: -   fields - the fields in this split

1901:     Level: intermediate

1903:     Notes:
1904:     Use PCFieldSplitSetIS() to set a completely general set of indices as a field.

1906:      The PCFieldSplitSetFields() is for defining fields as strided blocks. For example, if the block
1907:      size is three then one can define a field as 0, or 1 or 2 or 0,1 or 0,2 or 1,2 which mean
1908:      0xx3xx6xx9xx12 ... x1xx4xx7xx ... xx2xx5xx8xx.. 01x34x67x... 0x1x3x5x7.. x12x45x78x....
1909:      where the numbered entries indicate what is in the field.

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

1914:      Developer Note: This routine does not actually create the IS representing the split, that is delayed
1915:      until PCSetUp_FieldSplit(), because information about the vector/matrix layouts may not be
1916:      available when this routine is called.

1918: .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetBlockSize(), PCFieldSplitSetIS()

1920: @*/
1921: PetscErrorCode  PCFieldSplitSetFields(PC pc,const char splitname[],PetscInt n,const PetscInt *fields,const PetscInt *fields_col)
1922: {

1928:   if (n < 1) SETERRQ2(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Provided number of fields %D in split \"%s\" not positive",n,splitname);
1930:   PetscTryMethod(pc,"PCFieldSplitSetFields_C",(PC,const char[],PetscInt,const PetscInt*,const PetscInt*),(pc,splitname,n,fields,fields_col));
1931:   return(0);
1932: }

1934: /*@
1935:     PCFieldSplitSetDiagUseAmat - set flag indicating whether to extract diagonal blocks from Amat (rather than Pmat)

1937:     Logically Collective on PC

1939:     Input Parameters:
1940: +   pc  - the preconditioner object
1941: -   flg - boolean flag indicating whether or not to use Amat to extract the diagonal blocks from

1943:     Options Database:
1944: .     -pc_fieldsplit_diag_use_amat

1946:     Level: intermediate

1948: .seealso: PCFieldSplitGetDiagUseAmat(), PCFieldSplitSetOffDiagUseAmat(), PCFIELDSPLIT

1950: @*/
1951: PetscErrorCode  PCFieldSplitSetDiagUseAmat(PC pc,PetscBool flg)
1952: {
1953:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
1954:   PetscBool      isfs;

1959:   PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
1960:   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"PC not of type %s",PCFIELDSPLIT);
1961:   jac->diag_use_amat = flg;
1962:   return(0);
1963: }

1965: /*@
1966:     PCFieldSplitGetDiagUseAmat - get the flag indicating whether to extract diagonal blocks from Amat (rather than Pmat)

1968:     Logically Collective on PC

1970:     Input Parameters:
1971: .   pc  - the preconditioner object

1973:     Output Parameters:
1974: .   flg - boolean flag indicating whether or not to use Amat to extract the diagonal blocks from


1977:     Level: intermediate

1979: .seealso: PCFieldSplitSetDiagUseAmat(), PCFieldSplitGetOffDiagUseAmat(), PCFIELDSPLIT

1981: @*/
1982: PetscErrorCode  PCFieldSplitGetDiagUseAmat(PC pc,PetscBool *flg)
1983: {
1984:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
1985:   PetscBool      isfs;

1991:   PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
1992:   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"PC not of type %s",PCFIELDSPLIT);
1993:   *flg = jac->diag_use_amat;
1994:   return(0);
1995: }

1997: /*@
1998:     PCFieldSplitSetOffDiagUseAmat - set flag indicating whether to extract off-diagonal blocks from Amat (rather than Pmat)

2000:     Logically Collective on PC

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

2006:     Options Database:
2007: .     -pc_fieldsplit_off_diag_use_amat

2009:     Level: intermediate

2011: .seealso: PCFieldSplitGetOffDiagUseAmat(), PCFieldSplitSetDiagUseAmat(), PCFIELDSPLIT

2013: @*/
2014: PetscErrorCode  PCFieldSplitSetOffDiagUseAmat(PC pc,PetscBool flg)
2015: {
2016:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
2017:   PetscBool      isfs;

2022:   PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
2023:   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"PC not of type %s",PCFIELDSPLIT);
2024:   jac->offdiag_use_amat = flg;
2025:   return(0);
2026: }

2028: /*@
2029:     PCFieldSplitGetOffDiagUseAmat - get the flag indicating whether to extract off-diagonal blocks from Amat (rather than Pmat)

2031:     Logically Collective on PC

2033:     Input Parameters:
2034: .   pc  - the preconditioner object

2036:     Output Parameters:
2037: .   flg - boolean flag indicating whether or not to use Amat to extract the off-diagonal blocks from


2040:     Level: intermediate

2042: .seealso: PCFieldSplitSetOffDiagUseAmat(), PCFieldSplitGetDiagUseAmat(), PCFIELDSPLIT

2044: @*/
2045: PetscErrorCode  PCFieldSplitGetOffDiagUseAmat(PC pc,PetscBool *flg)
2046: {
2047:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
2048:   PetscBool      isfs;

2054:   PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
2055:   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"PC not of type %s",PCFIELDSPLIT);
2056:   *flg = jac->offdiag_use_amat;
2057:   return(0);
2058: }



2062: /*@C
2063:     PCFieldSplitSetIS - Sets the exact elements for field

2065:     Logically Collective on PC

2067:     Input Parameters:
2068: +   pc  - the preconditioner context
2069: .   splitname - name of this split, if NULL the number of the split is used
2070: -   is - the index set that defines the vector elements in this field


2073:     Notes:
2074:     Use PCFieldSplitSetFields(), for fields defined by strided types.

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

2079:     Level: intermediate

2081: .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetBlockSize()

2083: @*/
2084: PetscErrorCode  PCFieldSplitSetIS(PC pc,const char splitname[],IS is)
2085: {

2092:   PetscTryMethod(pc,"PCFieldSplitSetIS_C",(PC,const char[],IS),(pc,splitname,is));
2093:   return(0);
2094: }

2096: /*@C
2097:     PCFieldSplitGetIS - Retrieves the elements for a field as an IS

2099:     Logically Collective on PC

2101:     Input Parameters:
2102: +   pc  - the preconditioner context
2103: -   splitname - name of this split

2105:     Output Parameter:
2106: -   is - the index set that defines the vector elements in this field, or NULL if the field is not found

2108:     Level: intermediate

2110: .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetIS()

2112: @*/
2113: PetscErrorCode PCFieldSplitGetIS(PC pc,const char splitname[],IS *is)
2114: {

2121:   {
2122:     PC_FieldSplit     *jac  = (PC_FieldSplit*) pc->data;
2123:     PC_FieldSplitLink ilink = jac->head;
2124:     PetscBool         found;

2126:     *is = NULL;
2127:     while (ilink) {
2128:       PetscStrcmp(ilink->splitname, splitname, &found);
2129:       if (found) {
2130:         *is = ilink->is;
2131:         break;
2132:       }
2133:       ilink = ilink->next;
2134:     }
2135:   }
2136:   return(0);
2137: }

2139: /*@C
2140:     PCFieldSplitGetISByIndex - Retrieves the elements for a given index field as an IS

2142:     Logically Collective on PC

2144:     Input Parameters:
2145: +   pc  - the preconditioner context
2146: -   index - index of this split

2148:     Output Parameter:
2149: -   is - the index set that defines the vector elements in this field

2151:     Level: intermediate

2153: .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitGetIS(), PCFieldSplitSetIS()

2155: @*/
2156: PetscErrorCode PCFieldSplitGetISByIndex(PC pc,PetscInt index,IS *is)
2157: {

2161:   if (index < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative field %D requested",index);
2164:   {
2165:     PC_FieldSplit     *jac  = (PC_FieldSplit*) pc->data;
2166:     PC_FieldSplitLink ilink = jac->head;
2167:     PetscInt          i     = 0;
2168:     if (index >= jac->nsplits) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Field %D requested but only %D exist",index,jac->nsplits);

2170:     while (i < index) {
2171:       ilink = ilink->next;
2172:       ++i;
2173:     }
2174:     PCFieldSplitGetIS(pc,ilink->splitname,is);
2175:   }
2176:   return(0);
2177: }

2179: /*@
2180:     PCFieldSplitSetBlockSize - Sets the block size for defining where fields start in the
2181:       fieldsplit preconditioner. If not set the matrix block size is used.

2183:     Logically Collective on PC

2185:     Input Parameters:
2186: +   pc  - the preconditioner context
2187: -   bs - the block size

2189:     Level: intermediate

2191: .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields()

2193: @*/
2194: PetscErrorCode  PCFieldSplitSetBlockSize(PC pc,PetscInt bs)
2195: {

2201:   PetscTryMethod(pc,"PCFieldSplitSetBlockSize_C",(PC,PetscInt),(pc,bs));
2202:   return(0);
2203: }

2205: /*@C
2206:    PCFieldSplitGetSubKSP - Gets the KSP contexts for all splits

2208:    Collective on KSP

2210:    Input Parameter:
2211: .  pc - the preconditioner context

2213:    Output Parameters:
2214: +  n - the number of splits
2215: -  subksp - the array of KSP contexts

2217:    Note:
2218:    After PCFieldSplitGetSubKSP() the array of KSPs is to be freed by the user with PetscFree()
2219:    (not the KSP just the array that contains them).

2221:    You must call PCSetUp() before calling PCFieldSplitGetSubKSP().

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

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

2230:    Fortran Usage: You must pass in a KSP array that is large enough to contain all the local KSPs.
2231:       You can call PCFieldSplitGetSubKSP(pc,n,PETSC_NULL_KSP,ierr) to determine how large the
2232:       KSP array must be.


2235:    Level: advanced

2237: .seealso: PCFIELDSPLIT
2238: @*/
2239: PetscErrorCode  PCFieldSplitGetSubKSP(PC pc,PetscInt *n,KSP *subksp[])
2240: {

2246:   PetscUseMethod(pc,"PCFieldSplitGetSubKSP_C",(PC,PetscInt*,KSP **),(pc,n,subksp));
2247:   return(0);
2248: }

2250: /*@C
2251:    PCFieldSplitSchurGetSubKSP - Gets the KSP contexts used inside the Schur complement based PCFIELDSPLIT

2253:    Collective on KSP

2255:    Input Parameter:
2256: .  pc - the preconditioner context

2258:    Output Parameters:
2259: +  n - the number of splits
2260: -  subksp - the array of KSP contexts

2262:    Note:
2263:    After PCFieldSplitSchurGetSubKSP() the array of KSPs is to be freed by the user with PetscFree()
2264:    (not the KSP just the array that contains them).

2266:    You must call PCSetUp() before calling PCFieldSplitSchurGetSubKSP().

2268:    If the fieldsplit type is of type PC_COMPOSITE_SCHUR, it returns (in order)
2269:    - the KSP used for the (1,1) block
2270:    - the KSP used for the Schur complement (not the one used for the interior Schur solver)
2271:    - the KSP used for the (1,1) block in the upper triangular factor (if different from that of the (1,1) block).

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

2275:    Fortran Usage: You must pass in a KSP array that is large enough to contain all the local KSPs.
2276:       You can call PCFieldSplitSchurGetSubKSP(pc,n,PETSC_NULL_KSP,ierr) to determine how large the
2277:       KSP array must be.

2279:    Level: advanced

2281: .seealso: PCFIELDSPLIT
2282: @*/
2283: PetscErrorCode  PCFieldSplitSchurGetSubKSP(PC pc,PetscInt *n,KSP *subksp[])
2284: {

2290:   PetscUseMethod(pc,"PCFieldSplitSchurGetSubKSP_C",(PC,PetscInt*,KSP **),(pc,n,subksp));
2291:   return(0);
2292: }

2294: /*@
2295:     PCFieldSplitSetSchurPre -  Indicates from what operator the preconditioner is constructucted for the Schur complement.
2296:       The default is the A11 matrix. 

2298:     Collective on PC

2300:     Input Parameters:
2301: +   pc      - the preconditioner context
2302: .   ptype   - which matrix to use for preconditioning the Schur complement: PC_FIELDSPLIT_SCHUR_PRE_A11 (default), PC_FIELDSPLIT_SCHUR_PRE_SELF, PC_FIELDSPLIT_SCHUR_PRE_USER
2303:               PC_FIELDSPLIT_SCHUR_PRE_SELFP, and PC_FIELDSPLIT_SCHUR_PRE_FULL
2304: -   userpre - matrix to use for preconditioning, or NULL

2306:     Options Database:
2307: +    -pc_fieldsplit_schur_precondition <self,selfp,user,a11,full> - default is a11. See notes for meaning of various arguments
2308: -    -fieldsplit_1_pc_type <pctype> - the preconditioner algorithm that is used to construct the preconditioner from the operator

2310:     Notes:
2311: $    If ptype is
2312: $        a11 - the preconditioner for the Schur complement is generated from the block diagonal part of the preconditioner
2313: $        matrix associated with the Schur complement (i.e. A11), not the Schur complement matrix
2314: $        self - the preconditioner for the Schur complement is generated from the symbolic representation of the Schur complement matrix:
2315: $             The only preconditioner that currently works with this symbolic respresentation matrix object is the PCLSC
2316: $             preconditioner
2317: $        user - the preconditioner for the Schur complement is generated from the user provided matrix (pre argument
2318: $             to this function).
2319: $        selfp - the preconditioning for the Schur complement is generated from an explicitly-assembled approximation Sp = A11 - A10 inv(diag(A00)) A01
2320: $             This is only a good preconditioner when diag(A00) is a good preconditioner for A00. Optionally, A00 can be
2321: $             lumped before extracting the diagonal using the additional option -fieldsplit_1_mat_schur_complement_ainv_type lump
2322: $        full - the preconditioner for the Schur complement is generated from the exact Schur complement matrix representation computed internally by PCFIELDSPLIT (this is expensive)
2323: $             useful mostly as a test that the Schur complement approach can work for your problem

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

2329:     Level: intermediate

2331: .seealso: PCFieldSplitGetSchurPre(), PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields(), PCFieldSplitSchurPreType,
2332:           MatSchurComplementSetAinvType(), PCLSC

2334: @*/
2335: PetscErrorCode PCFieldSplitSetSchurPre(PC pc,PCFieldSplitSchurPreType ptype,Mat pre)
2336: {

2341:   PetscTryMethod(pc,"PCFieldSplitSetSchurPre_C",(PC,PCFieldSplitSchurPreType,Mat),(pc,ptype,pre));
2342:   return(0);
2343: }

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

2347: /*@
2348:     PCFieldSplitGetSchurPre - For Schur complement fieldsplit, determine how the Schur complement will be
2349:     preconditioned.  See PCFieldSplitSetSchurPre() for details.

2351:     Logically Collective on PC

2353:     Input Parameters:
2354: .   pc      - the preconditioner context

2356:     Output Parameters:
2357: +   ptype   - which matrix to use for preconditioning the Schur complement: PC_FIELDSPLIT_SCHUR_PRE_A11, PC_FIELDSPLIT_SCHUR_PRE_SELF, PC_FIELDSPLIT_PRE_USER
2358: -   userpre - matrix to use for preconditioning (with PC_FIELDSPLIT_PRE_USER), or NULL

2360:     Level: intermediate

2362: .seealso: PCFieldSplitSetSchurPre(), PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields(), PCFieldSplitSchurPreType, PCLSC

2364: @*/
2365: PetscErrorCode PCFieldSplitGetSchurPre(PC pc,PCFieldSplitSchurPreType *ptype,Mat *pre)
2366: {

2371:   PetscUseMethod(pc,"PCFieldSplitGetSchurPre_C",(PC,PCFieldSplitSchurPreType*,Mat*),(pc,ptype,pre));
2372:   return(0);
2373: }

2375: /*@
2376:     PCFieldSplitSchurGetS -  extract the MatSchurComplement object used by this PC in case it needs to be configured separately

2378:     Not collective

2380:     Input Parameter:
2381: .   pc      - the preconditioner context

2383:     Output Parameter:
2384: .   S       - the Schur complement matrix

2386:     Notes:
2387:     This matrix should not be destroyed using MatDestroy(); rather, use PCFieldSplitSchurRestoreS().

2389:     Level: advanced

2391: .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSchurPreType, PCFieldSplitSetSchurPre(), MatSchurComplement, PCFieldSplitSchurRestoreS()

2393: @*/
2394: PetscErrorCode  PCFieldSplitSchurGetS(PC pc,Mat *S)
2395: {
2397:   const char*    t;
2398:   PetscBool      isfs;
2399:   PC_FieldSplit  *jac;

2403:   PetscObjectGetType((PetscObject)pc,&t);
2404:   PetscStrcmp(t,PCFIELDSPLIT,&isfs);
2405:   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PC of type PCFIELDSPLIT, got %s instead",t);
2406:   jac = (PC_FieldSplit*)pc->data;
2407:   if (jac->type != PC_COMPOSITE_SCHUR) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PCFIELDSPLIT of type SCHUR, got %D instead",jac->type);
2408:   if (S) *S = jac->schur;
2409:   return(0);
2410: }

2412: /*@
2413:     PCFieldSplitSchurRestoreS -  restores the MatSchurComplement object used by this PC

2415:     Not collective

2417:     Input Parameters:
2418: +   pc      - the preconditioner context
2419: -   S       - the Schur complement matrix

2421:     Level: advanced

2423: .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSchurPreType, PCFieldSplitSetSchurPre(), MatSchurComplement, PCFieldSplitSchurGetS()

2425: @*/
2426: PetscErrorCode  PCFieldSplitSchurRestoreS(PC pc,Mat *S)
2427: {
2429:   const char*    t;
2430:   PetscBool      isfs;
2431:   PC_FieldSplit  *jac;

2435:   PetscObjectGetType((PetscObject)pc,&t);
2436:   PetscStrcmp(t,PCFIELDSPLIT,&isfs);
2437:   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PC of type PCFIELDSPLIT, got %s instead",t);
2438:   jac = (PC_FieldSplit*)pc->data;
2439:   if (jac->type != PC_COMPOSITE_SCHUR) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PCFIELDSPLIT of type SCHUR, got %D instead",jac->type);
2440:   if (!S || *S != jac->schur) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"MatSchurComplement restored is not the same as gotten");
2441:   return(0);
2442: }


2445: static PetscErrorCode  PCFieldSplitSetSchurPre_FieldSplit(PC pc,PCFieldSplitSchurPreType ptype,Mat pre)
2446: {
2447:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;

2451:   jac->schurpre = ptype;
2452:   if (ptype == PC_FIELDSPLIT_SCHUR_PRE_USER && pre) {
2453:     MatDestroy(&jac->schur_user);
2454:     jac->schur_user = pre;
2455:     PetscObjectReference((PetscObject)jac->schur_user);
2456:   }
2457:   return(0);
2458: }

2460: static PetscErrorCode  PCFieldSplitGetSchurPre_FieldSplit(PC pc,PCFieldSplitSchurPreType *ptype,Mat *pre)
2461: {
2462:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;

2465:   *ptype = jac->schurpre;
2466:   *pre   = jac->schur_user;
2467:   return(0);
2468: }

2470: /*@
2471:     PCFieldSplitSetSchurFactType -  sets which blocks of the approximate block factorization to retain in the preconditioner

2473:     Collective on PC

2475:     Input Parameters:
2476: +   pc  - the preconditioner context
2477: -   ftype - which blocks of factorization to retain, PC_FIELDSPLIT_SCHUR_FACT_FULL is default

2479:     Options Database:
2480: .     -pc_fieldsplit_schur_fact_type <diag,lower,upper,full> default is full


2483:     Level: intermediate

2485:     Notes:
2486:     The FULL factorization is

2488: $   (A   B)  = (1       0) (A   0) (1  Ainv*B)  = L D U
2489: $   (C   E)    (C*Ainv  1) (0   S) (0     1  )

2491:     where S = E - C*Ainv*B. In practice, the full factorization is applied via block triangular solves with the grouping L*(D*U). UPPER uses D*U, LOWER uses L*D,
2492:     and DIAG is the diagonal part with the sign of S flipped (because this makes the preconditioner positive definite for many formulations, thus allowing the use of KSPMINRES). Sign flipping of S can be turned off with PCFieldSplitSetSchurScale().

2494: $    If A and S are solved exactly
2495: $      *) FULL factorization is a direct solver.
2496: $      *) 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.
2497: $      *) 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.

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

2502:     For symmetric problems in which A is positive definite and S is negative definite, DIAG can be used with KSPMINRES.

2504:     Note that a flexible method like KSPFGMRES or KSPGCR 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).

2506:     References:
2507: +   1. - Murphy, Golub, and Wathen, A note on preconditioning indefinite linear systems, SIAM J. Sci. Comput., 21 (2000).
2508: -   2. - Ipsen, A note on preconditioning nonsymmetric matrices, SIAM J. Sci. Comput., 23 (2001).

2510: .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields(), PCFieldSplitSchurPreType, PCFieldSplitSetSchurScale()
2511: @*/
2512: PetscErrorCode  PCFieldSplitSetSchurFactType(PC pc,PCFieldSplitSchurFactType ftype)
2513: {

2518:   PetscTryMethod(pc,"PCFieldSplitSetSchurFactType_C",(PC,PCFieldSplitSchurFactType),(pc,ftype));
2519:   return(0);
2520: }

2522: static PetscErrorCode PCFieldSplitSetSchurFactType_FieldSplit(PC pc,PCFieldSplitSchurFactType ftype)
2523: {
2524:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;

2527:   jac->schurfactorization = ftype;
2528:   return(0);
2529: }

2531: /*@
2532:     PCFieldSplitSetSchurScale -  Controls the sign flip of S for PC_FIELDSPLIT_SCHUR_FACT_DIAG.

2534:     Collective on PC

2536:     Input Parameters:
2537: +   pc    - the preconditioner context
2538: -   scale - scaling factor for the Schur complement

2540:     Options Database:
2541: .     -pc_fieldsplit_schur_scale - default is -1.0

2543:     Level: intermediate

2545: .seealso: PCFIELDSPLIT, PCFieldSplitSetFields(), PCFieldSplitSchurFactType, PCFieldSplitSetSchurScale()
2546: @*/
2547: PetscErrorCode PCFieldSplitSetSchurScale(PC pc,PetscScalar scale)
2548: {

2554:   PetscTryMethod(pc,"PCFieldSplitSetSchurScale_C",(PC,PetscScalar),(pc,scale));
2555:   return(0);
2556: }

2558: static PetscErrorCode PCFieldSplitSetSchurScale_FieldSplit(PC pc,PetscScalar scale)
2559: {
2560:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;

2563:   jac->schurscale = scale;
2564:   return(0);
2565: }

2567: /*@C
2568:    PCFieldSplitGetSchurBlocks - Gets all matrix blocks for the Schur complement

2570:    Collective on KSP

2572:    Input Parameter:
2573: .  pc - the preconditioner context

2575:    Output Parameters:
2576: +  A00 - the (0,0) block
2577: .  A01 - the (0,1) block
2578: .  A10 - the (1,0) block
2579: -  A11 - the (1,1) block

2581:    Level: advanced

2583: .seealso: PCFIELDSPLIT
2584: @*/
2585: PetscErrorCode  PCFieldSplitGetSchurBlocks(PC pc,Mat *A00,Mat *A01,Mat *A10, Mat *A11)
2586: {
2587:   PC_FieldSplit *jac = (PC_FieldSplit*) pc->data;

2591:   if (jac->type != PC_COMPOSITE_SCHUR) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONG, "FieldSplit is not using a Schur complement approach.");
2592:   if (A00) *A00 = jac->pmat[0];
2593:   if (A01) *A01 = jac->B;
2594:   if (A10) *A10 = jac->C;
2595:   if (A11) *A11 = jac->pmat[1];
2596:   return(0);
2597: }

2599: /*@
2600:     PCFieldSplitSetGKBTol -  Sets the solver tolerance for the generalized Golub-Kahan bidiagonalization preconditioner.

2602:     Collective on PC

2604:     Notes:
2605:     The generalized GKB algorithm uses a lower bound estimate of the error in energy norm as stopping criterion.
2606:     It stops once the lower bound estimate undershoots the required solver tolerance. Although the actual error might be bigger than
2607:     this estimate, the stopping criterion is satisfactory in practical cases [A13].

2609: [Ar13] Generalized Golub-Kahan bidiagonalization and stopping criteria, SIAM J. Matrix Anal. Appl., Vol. 34, No. 2, pp. 571-592, 2013.

2611:     Input Parameters:
2612: +   pc        - the preconditioner context
2613: -   tolerance - the solver tolerance

2615:     Options Database:
2616: .     -pc_fieldsplit_gkb_tol - default is 1e-5

2618:     Level: intermediate

2620: .seealso: PCFIELDSPLIT, PCFieldSplitSetGKBDelay(), PCFieldSplitSetGKBNu(), PCFieldSplitSetGKBMaxit()
2621: @*/
2622: PetscErrorCode PCFieldSplitSetGKBTol(PC pc,PetscReal tolerance)
2623: {

2629:   PetscTryMethod(pc,"PCFieldSplitSetGKBTol_C",(PC,PetscReal),(pc,tolerance));
2630:   return(0);
2631: }

2633: static PetscErrorCode PCFieldSplitSetGKBTol_FieldSplit(PC pc,PetscReal tolerance)
2634: {
2635:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;

2638:   jac->gkbtol = tolerance;
2639:   return(0);
2640: }


2643: /*@
2644:     PCFieldSplitSetGKBMaxit -  Sets the maximum number of iterations for the generalized Golub-Kahan bidiagonalization
2645:     preconditioner.

2647:     Collective on PC

2649:     Input Parameters:
2650: +   pc     - the preconditioner context
2651: -   maxit  - the maximum number of iterations

2653:     Options Database:
2654: .     -pc_fieldsplit_gkb_maxit - default is 100

2656:     Level: intermediate

2658: .seealso: PCFIELDSPLIT, PCFieldSplitSetGKBDelay(), PCFieldSplitSetGKBTol(), PCFieldSplitSetGKBNu()
2659: @*/
2660: PetscErrorCode PCFieldSplitSetGKBMaxit(PC pc,PetscInt maxit)
2661: {

2667:   PetscTryMethod(pc,"PCFieldSplitSetGKBMaxit_C",(PC,PetscInt),(pc,maxit));
2668:   return(0);
2669: }

2671: static PetscErrorCode PCFieldSplitSetGKBMaxit_FieldSplit(PC pc,PetscInt maxit)
2672: {
2673:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;

2676:   jac->gkbmaxit = maxit;
2677:   return(0);
2678: }

2680: /*@
2681:     PCFieldSplitSetGKBDelay -  Sets the delay in the lower bound error estimate in the generalized Golub-Kahan bidiagonalization
2682:     preconditioner.

2684:     Collective on PC

2686:     Notes:
2687:     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
2688:     is expressed as a truncated sum. The error at iteration k can only be measured at iteration (k + delay), and thus the algorithm needs
2689:     at least (delay + 1) iterations to stop. For more details on the generalized Golub-Kahan bidiagonalization method and its lower bound stopping criterion, please refer to

2691: [Ar13] Generalized Golub-Kahan bidiagonalization and stopping criteria, SIAM J. Matrix Anal. Appl., Vol. 34, No. 2, pp. 571-592, 2013.

2693:     Input Parameters:
2694: +   pc     - the preconditioner context
2695: -   delay  - the delay window in the lower bound estimate

2697:     Options Database:
2698: .     -pc_fieldsplit_gkb_delay - default is 5

2700:     Level: intermediate

2702: .seealso: PCFIELDSPLIT, PCFieldSplitSetGKBNu(), PCFieldSplitSetGKBTol(), PCFieldSplitSetGKBMaxit()
2703: @*/
2704: PetscErrorCode PCFieldSplitSetGKBDelay(PC pc,PetscInt delay)
2705: {

2711:   PetscTryMethod(pc,"PCFieldSplitSetGKBDelay_C",(PC,PetscInt),(pc,delay));
2712:   return(0);
2713: }

2715: static PetscErrorCode PCFieldSplitSetGKBDelay_FieldSplit(PC pc,PetscInt delay)
2716: {
2717:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;

2720:   jac->gkbdelay = delay;
2721:   return(0);
2722: }

2724: /*@
2725:     PCFieldSplitSetGKBNu -  Sets the scalar value nu >= 0 in the transformation H = A00 + nu*A01*A01' of the (1,1) block in the Golub-Kahan bidiagonalization preconditioner.

2727:     Collective on PC

2729:     Notes:
2730:     This shift is in general done to obtain better convergence properties for the outer loop of the algorithm. This is often achieved by chosing nu sufficiently big. However,
2731:     if nu is chosen too big, the matrix H might be badly conditioned and the solution of the linear system Hx = b in the inner loop gets difficult. It is therefore
2732:     necessary to find a good balance in between the convergence of the inner and outer loop.

2734:     For nu = 0, no shift is done. In this case A00 has to be positive definite. The matrix N in [Ar13] is then chosen as identity.

2736: [Ar13] Generalized Golub-Kahan bidiagonalization and stopping criteria, SIAM J. Matrix Anal. Appl., Vol. 34, No. 2, pp. 571-592, 2013.

2738:     Input Parameters:
2739: +   pc     - the preconditioner context
2740: -   nu     - the shift parameter

2742:     Options Database:
2743: .     -pc_fieldsplit_gkb_nu - default is 1

2745:     Level: intermediate

2747: .seealso: PCFIELDSPLIT, PCFieldSplitSetGKBDelay(), PCFieldSplitSetGKBTol(), PCFieldSplitSetGKBMaxit()
2748: @*/
2749: PetscErrorCode PCFieldSplitSetGKBNu(PC pc,PetscReal nu)
2750: {

2756:   PetscTryMethod(pc,"PCFieldSplitSetGKBNu_C",(PC,PetscReal),(pc,nu));
2757:   return(0);
2758: }

2760: static PetscErrorCode PCFieldSplitSetGKBNu_FieldSplit(PC pc,PetscReal nu)
2761: {
2762:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;

2765:   jac->gkbnu = nu;
2766:   return(0);
2767: }


2770: static PetscErrorCode  PCFieldSplitSetType_FieldSplit(PC pc,PCCompositeType type)
2771: {
2772:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;

2776:   jac->type = type;

2778:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",0);
2779:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurPre_C",0);
2780:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSchurPre_C",0);
2781:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C",0);
2782:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurScale_C",0);
2783:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetGKBTol_C",0);
2784:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetGKBMaxit_C",0);
2785:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetGKBNu_C",0);
2786:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetGKBDelay_C",0);

2788:   if (type == PC_COMPOSITE_SCHUR) {
2789:     pc->ops->apply = PCApply_FieldSplit_Schur;
2790:     pc->ops->view  = PCView_FieldSplit_Schur;

2792:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit_Schur);
2793:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurPre_C",PCFieldSplitSetSchurPre_FieldSplit);
2794:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSchurPre_C",PCFieldSplitGetSchurPre_FieldSplit);
2795:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C",PCFieldSplitSetSchurFactType_FieldSplit);
2796:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurScale_C",PCFieldSplitSetSchurScale_FieldSplit);
2797:   } else if (type == PC_COMPOSITE_GKB){
2798:     pc->ops->apply = PCApply_FieldSplit_GKB;
2799:     pc->ops->view  = PCView_FieldSplit_GKB;

2801:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit);
2802:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetGKBTol_C",PCFieldSplitSetGKBTol_FieldSplit);
2803:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetGKBMaxit_C",PCFieldSplitSetGKBMaxit_FieldSplit);
2804:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetGKBNu_C",PCFieldSplitSetGKBNu_FieldSplit);
2805:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetGKBDelay_C",PCFieldSplitSetGKBDelay_FieldSplit);
2806:   } else {
2807:     pc->ops->apply = PCApply_FieldSplit;
2808:     pc->ops->view  = PCView_FieldSplit;

2810:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit);
2811:   }
2812:   return(0);
2813: }

2815: static PetscErrorCode  PCFieldSplitSetBlockSize_FieldSplit(PC pc,PetscInt bs)
2816: {
2817:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;

2820:   if (bs < 1) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Blocksize must be positive, you gave %D",bs);
2821:   if (jac->bs > 0 && jac->bs != bs) SETERRQ2(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Cannot change fieldsplit blocksize from %D to %D after it has been set",jac->bs,bs);
2822:   jac->bs = bs;
2823:   return(0);
2824: }

2826: /*@
2827:    PCFieldSplitSetType - Sets the type of fieldsplit preconditioner.

2829:    Collective on PC

2831:    Input Parameter:
2832: +  pc - the preconditioner context
2833: -  type - PC_COMPOSITE_ADDITIVE, PC_COMPOSITE_MULTIPLICATIVE (default), PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE, PC_COMPOSITE_SPECIAL, PC_COMPOSITE_SCHUR

2835:    Options Database Key:
2836: .  -pc_fieldsplit_type <type: one of multiplicative, additive, symmetric_multiplicative, special, schur> - Sets fieldsplit preconditioner type

2838:    Level: Intermediate

2840: .seealso: PCCompositeSetType()

2842: @*/
2843: PetscErrorCode  PCFieldSplitSetType(PC pc,PCCompositeType type)
2844: {

2849:   PetscTryMethod(pc,"PCFieldSplitSetType_C",(PC,PCCompositeType),(pc,type));
2850:   return(0);
2851: }

2853: /*@
2854:   PCFieldSplitGetType - Gets the type of fieldsplit preconditioner.

2856:   Not collective

2858:   Input Parameter:
2859: . pc - the preconditioner context

2861:   Output Parameter:
2862: . type - PC_COMPOSITE_ADDITIVE, PC_COMPOSITE_MULTIPLICATIVE (default), PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE, PC_COMPOSITE_SPECIAL, PC_COMPOSITE_SCHUR

2864:   Level: Intermediate

2866: .seealso: PCCompositeSetType()
2867: @*/
2868: PetscErrorCode PCFieldSplitGetType(PC pc, PCCompositeType *type)
2869: {
2870:   PC_FieldSplit *jac = (PC_FieldSplit*) pc->data;

2875:   *type = jac->type;
2876:   return(0);
2877: }

2879: /*@
2880:    PCFieldSplitSetDMSplits - Flags whether DMCreateFieldDecomposition() should be used to define the splits, whenever possible.

2882:    Logically Collective

2884:    Input Parameters:
2885: +  pc   - the preconditioner context
2886: -  flg  - boolean indicating whether to use field splits defined by the DM

2888:    Options Database Key:
2889: .  -pc_fieldsplit_dm_splits

2891:    Level: Intermediate

2893: .seealso: PCFieldSplitGetDMSplits()

2895: @*/
2896: PetscErrorCode  PCFieldSplitSetDMSplits(PC pc,PetscBool flg)
2897: {
2898:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
2899:   PetscBool      isfs;

2905:   PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
2906:   if (isfs) {
2907:     jac->dm_splits = flg;
2908:   }
2909:   return(0);
2910: }


2913: /*@
2914:    PCFieldSplitGetDMSplits - Returns flag indicating whether DMCreateFieldDecomposition() should be used to define the splits, whenever possible.

2916:    Logically Collective

2918:    Input Parameter:
2919: .  pc   - the preconditioner context

2921:    Output Parameter:
2922: .  flg  - boolean indicating whether to use field splits defined by the DM

2924:    Level: Intermediate

2926: .seealso: PCFieldSplitSetDMSplits()

2928: @*/
2929: PetscErrorCode  PCFieldSplitGetDMSplits(PC pc,PetscBool* flg)
2930: {
2931:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
2932:   PetscBool      isfs;

2938:   PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
2939:   if (isfs) {
2940:     if(flg) *flg = jac->dm_splits;
2941:   }
2942:   return(0);
2943: }

2945: /*@
2946:    PCFieldSplitGetDetectSaddlePoint - Returns flag indicating whether PCFieldSplit will attempt to automatically determine fields based on zero diagonal entries.

2948:    Logically Collective

2950:    Input Parameter:
2951: .  pc   - the preconditioner context

2953:    Output Parameter:
2954: .  flg  - boolean indicating whether to detect fields or not

2956:    Level: Intermediate

2958: .seealso: PCFIELDSPLIT, PCFieldSplitSetDetectSaddlePoint()

2960: @*/
2961: PetscErrorCode PCFieldSplitGetDetectSaddlePoint(PC pc,PetscBool *flg)
2962: {
2963:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;

2966:   *flg = jac->detect;
2967:   return(0);
2968: }

2970: /*@
2971:    PCFieldSplitSetDetectSaddlePoint - Sets flag indicating whether PCFieldSplit will attempt to automatically determine fields based on zero diagonal entries.

2973:    Logically Collective

2975:    Notes:
2976:    Also sets the split type to PC_COMPOSITE_SCHUR (see PCFieldSplitSetType()) and the Schur preconditioner type to PC_FIELDSPLIT_SCHUR_PRE_SELF (see PCFieldSplitSetSchurPre()).

2978:    Input Parameter:
2979: .  pc   - the preconditioner context

2981:    Output Parameter:
2982: .  flg  - boolean indicating whether to detect fields or not

2984:    Options Database Key:
2985: .  -pc_fieldsplit_detect_saddle_point

2987:    Level: Intermediate

2989: .seealso: PCFIELDSPLIT, PCFieldSplitSetDetectSaddlePoint(), PCFieldSplitSetType(), PCFieldSplitSetSchurPre()

2991: @*/
2992: PetscErrorCode PCFieldSplitSetDetectSaddlePoint(PC pc,PetscBool flg)
2993: {
2994:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;

2998:   jac->detect = flg;
2999:   if (jac->detect) {
3000:     PCFieldSplitSetType(pc,PC_COMPOSITE_SCHUR);
3001:     PCFieldSplitSetSchurPre(pc,PC_FIELDSPLIT_SCHUR_PRE_SELF,NULL);
3002:   }
3003:   return(0);
3004: }

3006: /* -------------------------------------------------------------------------------------*/
3007: /*MC
3008:    PCFIELDSPLIT - Preconditioner created by combining separate preconditioners for individual
3009:                   fields or groups of fields. See the users manual section "Solving Block Matrices" for more details.

3011:      To set options on the solvers for each block append -fieldsplit_ to all the PC
3012:         options database keys. For example, -fieldsplit_pc_type ilu -fieldsplit_pc_factor_levels 1

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

3017:    Level: intermediate

3019:    Options Database Keys:
3020: +   -pc_fieldsplit_%d_fields <a,b,..> - indicates the fields to be used in the %d'th split
3021: .   -pc_fieldsplit_default - automatically add any fields to additional splits that have not
3022:                               been supplied explicitly by -pc_fieldsplit_%d_fields
3023: .   -pc_fieldsplit_block_size <bs> - size of block that defines fields (i.e. there are bs fields)
3024: .   -pc_fieldsplit_type <additive,multiplicative,symmetric_multiplicative,schur,gkb> - type of relaxation or factorization splitting
3025: .   -pc_fieldsplit_schur_precondition <self,selfp,user,a11,full> - default is a11; see PCFieldSplitSetSchurPre()
3026: .   -pc_fieldsplit_detect_saddle_point - automatically finds rows with zero diagonal and uses Schur complement with no preconditioner as the solver

3028: .    Options prefix for inner solvers when using Schur complement preconditioner are -fieldsplit_0_ and -fieldsplit_1_
3029:      for all other solvers they are -fieldsplit_%d_ for the dth field, use -fieldsplit_ for all fields
3030: -    Options prefix for inner solver when using Golub Kahan biadiagonalization preconditioner is -fieldsplit_0_

3032:    Notes:
3033:     Use PCFieldSplitSetFields() to set fields defined by "strided" entries and PCFieldSplitSetIS()
3034:      to define a field by an arbitrary collection of entries.

3036:       If no fields are set the default is used. The fields are defined by entries strided by bs,
3037:       beginning at 0 then 1, etc to bs-1. The block size can be set with PCFieldSplitSetBlockSize(),
3038:       if this is not called the block size defaults to the blocksize of the second matrix passed
3039:       to KSPSetOperators()/PCSetOperators().

3041: $     For the Schur complement preconditioner if J = ( A00 A01 )
3042: $                                                    ( A10 A11 )
3043: $     the preconditioner using full factorization is
3044: $              ( I   -ksp(A00) A01 ) ( inv(A00)     0  ) (     I          0  )
3045: $              ( 0         I       ) (   0      ksp(S) ) ( -A10 ksp(A00)  I  )
3046:      where the action of inv(A00) is applied using the KSP solver with prefix -fieldsplit_0_.  S is the Schur complement
3047: $              S = A11 - A10 ksp(A00) A01
3048:      which is usually dense and not stored explicitly.  The action of ksp(S) is computed using the KSP solver with prefix -fieldsplit_splitname_ (where splitname was given
3049:      in providing the SECOND split or 1 if not give). For PCFieldSplitGetSubKSP() when field number is 0,
3050:      it returns the KSP associated with -fieldsplit_0_ while field number 1 gives -fieldsplit_1_ KSP. By default
3051:      A11 is used to construct a preconditioner for S, use PCFieldSplitSetSchurPre() for all the possible ways to construct the preconditioner for S.

3053:      The factorization type is set using -pc_fieldsplit_schur_fact_type <diag, lower, upper, full>. The full is shown above,
3054:      diag gives
3055: $              ( inv(A00)     0   )
3056: $              (   0      -ksp(S) )
3057:      note that slightly counter intuitively there is a negative in front of the ksp(S) so that the preconditioner is positive definite. For SPD matrices J, the sign flip
3058:      can be turned off with PCFieldSplitSetSchurScale() or by command line -pc_fieldsplit_schur_scale 1.0. The lower factorization is the inverse of
3059: $              (  A00   0 )
3060: $              (  A10   S )
3061:      where the inverses of A00 and S are applied using KSPs. The upper factorization is the inverse of
3062: $              ( A00 A01 )
3063: $              (  0   S  )
3064:      where again the inverses of A00 and S are applied using KSPs.

3066:      If only one set of indices (one IS) is provided with PCFieldSplitSetIS() then the complement of that IS
3067:      is used automatically for a second block.

3069:      The fieldsplit preconditioner cannot currently be used with the BAIJ or SBAIJ data formats if the blocksize is larger than 1.
3070:      Generally it should be used with the AIJ format.

3072:      The forms of these preconditioners are closely related if not identical to forms derived as "Distributive Iterations", see,
3073:      for example, page 294 in "Principles of Computational Fluid Dynamics" by Pieter Wesseling. Note that one can also use PCFIELDSPLIT
3074:      inside a smoother resulting in "Distributive Smoothers".

3076:    There is a nice discussion of block preconditioners in

3078: [El08] A taxonomy and comparison of parallel block multi-level preconditioners for the incompressible Navier-Stokes equations
3079:        Howard Elman, V.E. Howle, John Shadid, Robert Shuttleworth, Ray Tuminaro, Journal of Computational Physics 227 (2008) 1790--1808
3080:        http://chess.cs.umd.edu/~elman/papers/tax.pdf

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

3085:    The generalized Golub-Kahan bidiagonalization preconditioner (gkb) can be applied to symmetric 2x2 block matrices of the shape
3086: $        ( A00  A01 )
3087: $        ( A01' 0   )
3088:    with A00 positive semi-definite. The implementation follows [Ar13]. Therein, we choose N := 1/nu * I and the (1,1)-block of the matrix is modified to H = A00 + nu*A01*A01'.
3089:    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_.

3091: [Ar13] Generalized Golub-Kahan bidiagonalization and stopping criteria, SIAM J. Matrix Anal. Appl., Vol. 34, No. 2, pp. 571-592, 2013.

3093: .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC, Block_Preconditioners, PCLSC,
3094:            PCFieldSplitGetSubKSP(), PCFieldSplitSchurGetSubKSP(), PCFieldSplitSetFields(), PCFieldSplitSetType(), PCFieldSplitSetIS(), PCFieldSplitSetSchurPre(),
3095:           MatSchurComplementSetAinvType(), PCFieldSplitSetSchurScale(),
3096:           PCFieldSplitSetDetectSaddlePoint()
3097: M*/

3099: PETSC_EXTERN PetscErrorCode PCCreate_FieldSplit(PC pc)
3100: {
3102:   PC_FieldSplit  *jac;

3105:   PetscNewLog(pc,&jac);

3107:   jac->bs                 = -1;
3108:   jac->nsplits            = 0;
3109:   jac->type               = PC_COMPOSITE_MULTIPLICATIVE;
3110:   jac->schurpre           = PC_FIELDSPLIT_SCHUR_PRE_USER; /* Try user preconditioner first, fall back on diagonal */
3111:   jac->schurfactorization = PC_FIELDSPLIT_SCHUR_FACT_FULL;
3112:   jac->schurscale         = -1.0;
3113:   jac->dm_splits          = PETSC_TRUE;
3114:   jac->detect             = PETSC_FALSE;
3115:   jac->gkbtol             = 1e-5;
3116:   jac->gkbdelay           = 5;
3117:   jac->gkbnu              = 1;
3118:   jac->gkbmaxit           = 100;
3119:   jac->gkbmonitor         = PETSC_FALSE;

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

3123:   pc->ops->apply           = PCApply_FieldSplit;
3124:   pc->ops->applytranspose  = PCApplyTranspose_FieldSplit;
3125:   pc->ops->setup           = PCSetUp_FieldSplit;
3126:   pc->ops->reset           = PCReset_FieldSplit;
3127:   pc->ops->destroy         = PCDestroy_FieldSplit;
3128:   pc->ops->setfromoptions  = PCSetFromOptions_FieldSplit;
3129:   pc->ops->view            = PCView_FieldSplit;
3130:   pc->ops->applyrichardson = 0;

3132:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSchurGetSubKSP_C",PCFieldSplitSchurGetSubKSP_FieldSplit);
3133:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit);
3134:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetFields_C",PCFieldSplitSetFields_FieldSplit);
3135:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetIS_C",PCFieldSplitSetIS_FieldSplit);
3136:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetType_C",PCFieldSplitSetType_FieldSplit);
3137:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetBlockSize_C",PCFieldSplitSetBlockSize_FieldSplit);
3138:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitRestrictIS_C",PCFieldSplitRestrictIS_FieldSplit);
3139:   return(0);
3140: }