Actual source code: fieldsplit.c

petsc-master 2018-11-18
Report Typos and Errors


  3:  #include <petsc/private/pcimpl.h>
  4: #include <petsc/private/kspimpl.h>    /*  This is needed to provide the appropriate PETSC_EXTERN for KSP_Solve_FS ....*/
  5:  #include <petscdm.h>

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

333:     PetscViewerASCIIPopTab(viewer);
334:   }

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

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


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

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

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

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

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

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

483:       if (jac->detect) {
484:         IS       zerodiags,rest;
485:         PetscInt nmin,nmax;

487:         MatGetOwnershipRange(pc->mat,&nmin,&nmax);
488:         MatFindZeroDiagonals(pc->mat,&zerodiags);
489:         ISComplement(zerodiags,nmin,nmax,&rest);
490:         PCFieldSplitSetIS(pc,"0",rest);
491:         PCFieldSplitSetIS(pc,"1",zerodiags);
492:         ISDestroy(&zerodiags);
493:         ISDestroy(&rest);
494:       } else if (coupling) {
495:         IS       coupling,rest;
496:         PetscInt nmin,nmax;

498:         MatGetOwnershipRange(pc->mat,&nmin,&nmax);
499:         MatFindOffBlockDiagonalEntries(pc->mat,&coupling);
500:         ISCreateStride(PetscObjectComm((PetscObject)pc->mat),nmax-nmin,nmin,1,&rest);
501:         ISSetIdentity(rest);
502:         PCFieldSplitSetIS(pc,"0",rest);
503:         PCFieldSplitSetIS(pc,"1",coupling);
504:         ISDestroy(&coupling);
505:         ISDestroy(&rest);
506:       } else {
507:         PetscOptionsGetBool(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_fieldsplit_default",&fieldsplit_default,NULL);
508:         if (!fieldsplit_default) {
509:           /* Allow user to set fields from command line,  if bs was known at the time of PCSetFromOptions_FieldSplit()
510:            then it is set there. This is not ideal because we should only have options set in XXSetFromOptions(). */
511:           PCFieldSplitSetRuntimeSplits_Private(pc);
512:           if (jac->splitdefined) {PetscInfo(pc,"Splits defined using the options database\n");}
513:         }
514:         if ((fieldsplit_default || !jac->splitdefined) && !jac->isrestrict) {
515:           PetscInfo(pc,"Using default splitting of fields\n");
516:           for (i=0; i<jac->bs; i++) {
517:             char splitname[8];
518:             PetscSNPrintf(splitname,sizeof(splitname),"%D",i);
519:             PCFieldSplitSetFields(pc,splitname,1,&i,&i);
520:           }
521:           jac->defaultsplit = PETSC_TRUE;
522:         }
523:       }
524:     }
525:   } else if (jac->nsplits == 1) {
526:     if (ilink->is) {
527:       IS       is2;
528:       PetscInt nmin,nmax;

530:       MatGetOwnershipRange(pc->mat,&nmin,&nmax);
531:       ISComplement(ilink->is,nmin,nmax,&is2);
532:       PCFieldSplitSetIS(pc,"1",is2);
533:       ISDestroy(&is2);
534:     } else SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Must provide at least two sets of fields to PCFieldSplit()");
535:   }

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

541: static PetscErrorCode MatGolubKahanComputeExplicitOperator(Mat A,Mat B,Mat C,Mat *H,PetscReal gkbnu)
542: {
543:   PetscErrorCode    ierr;
544:   Mat               BT,T;
545:   PetscReal         nrmT,nrmB;

548:   MatHermitianTranspose(C,MAT_INITIAL_MATRIX,&T);            /* Test if augmented matrix is symmetric */
549:   MatAXPY(T,-1.0,B,DIFFERENT_NONZERO_PATTERN);
550:   MatNorm(T,NORM_1,&nrmT);
551:   MatNorm(B,NORM_1,&nrmB);
552:   if (nrmB > 0) {
553:     if (nrmT/nrmB >= PETSC_SMALL) {
554:       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Matrix is not symmetric/hermitian, GKB is not applicable.");
555:     }
556:   }
557:   /* Compute augmented Lagrangian matrix H = A00 + nu*A01*A01'. This corresponds to */
558:   /* setting N := 1/nu*I in [Ar13].                                                 */
559:   MatHermitianTranspose(B,MAT_INITIAL_MATRIX,&BT);
560:   MatMatMult(B,BT,MAT_INITIAL_MATRIX,PETSC_DEFAULT,H);       /* H = A01*A01'          */
561:   MatAYPX(*H,gkbnu,A,DIFFERENT_NONZERO_PATTERN);             /* H = A00 + nu*A01*A01' */

563:   MatDestroy(&BT);
564:   MatDestroy(&T);
565:   return(0);
566: }

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

570: static PetscErrorCode PCSetUp_FieldSplit(PC pc)
571: {
572:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
573:   PetscErrorCode    ierr;
574:   PC_FieldSplitLink ilink;
575:   PetscInt          i,nsplit;
576:   PetscBool         sorted, sorted_col;

579:   PCFieldSplitSetDefaults(pc);
580:   nsplit = jac->nsplits;
581:   ilink  = jac->head;

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

587:     jac->issetup = PETSC_TRUE;

589:     /* This is done here instead of in PCFieldSplitSetFields() because may not have matrix at that point */
590:     if (jac->defaultsplit || !ilink->is) {
591:       if (jac->bs <= 0) jac->bs = nsplit;
592:     }
593:     bs     = jac->bs;
594:     MatGetOwnershipRange(pc->pmat,&rstart,&rend);
595:     nslots = (rend - rstart)/bs;
596:     for (i=0; i<nsplit; i++) {
597:       if (jac->defaultsplit) {
598:         ISCreateStride(PetscObjectComm((PetscObject)pc),nslots,rstart+i,nsplit,&ilink->is);
599:         ISDuplicate(ilink->is,&ilink->is_col);
600:       } else if (!ilink->is) {
601:         if (ilink->nfields > 1) {
602:           PetscInt *ii,*jj,j,k,nfields = ilink->nfields,*fields = ilink->fields,*fields_col = ilink->fields_col;
603:           PetscMalloc1(ilink->nfields*nslots,&ii);
604:           PetscMalloc1(ilink->nfields*nslots,&jj);
605:           for (j=0; j<nslots; j++) {
606:             for (k=0; k<nfields; k++) {
607:               ii[nfields*j + k] = rstart + bs*j + fields[k];
608:               jj[nfields*j + k] = rstart + bs*j + fields_col[k];
609:             }
610:           }
611:           ISCreateGeneral(PetscObjectComm((PetscObject)pc),nslots*nfields,ii,PETSC_OWN_POINTER,&ilink->is);
612:           ISCreateGeneral(PetscObjectComm((PetscObject)pc),nslots*nfields,jj,PETSC_OWN_POINTER,&ilink->is_col);
613:           ISSetBlockSize(ilink->is, nfields);
614:           ISSetBlockSize(ilink->is_col, nfields);
615:         } else {
616:           ISCreateStride(PetscObjectComm((PetscObject)pc),nslots,rstart+ilink->fields[0],bs,&ilink->is);
617:           ISCreateStride(PetscObjectComm((PetscObject)pc),nslots,rstart+ilink->fields_col[0],bs,&ilink->is_col);
618:         }
619:       }
620:       ISSorted(ilink->is,&sorted);
621:       if (ilink->is_col) { ISSorted(ilink->is_col,&sorted_col); }
622:       if (!sorted || !sorted_col) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Fields must be sorted when creating split");
623:       ilink = ilink->next;
624:     }
625:   }

627:   ilink = jac->head;
628:   if (!jac->pmat) {
629:     Vec xtmp;

631:     MatCreateVecs(pc->pmat,&xtmp,NULL);
632:     PetscMalloc1(nsplit,&jac->pmat);
633:     PetscMalloc2(nsplit,&jac->x,nsplit,&jac->y);
634:     for (i=0; i<nsplit; i++) {
635:       MatNullSpace sp;

637:       /* Check for preconditioning matrix attached to IS */
638:       PetscObjectQuery((PetscObject) ilink->is, "pmat", (PetscObject*) &jac->pmat[i]);
639:       if (jac->pmat[i]) {
640:         PetscObjectReference((PetscObject) jac->pmat[i]);
641:         if (jac->type == PC_COMPOSITE_SCHUR) {
642:           jac->schur_user = jac->pmat[i];

644:           PetscObjectReference((PetscObject) jac->schur_user);
645:         }
646:       } else {
647:         const char *prefix;
648:         MatCreateSubMatrix(pc->pmat,ilink->is,ilink->is_col,MAT_INITIAL_MATRIX,&jac->pmat[i]);
649:         KSPGetOptionsPrefix(ilink->ksp,&prefix);
650:         MatSetOptionsPrefix(jac->pmat[i],prefix);
651:         MatViewFromOptions(jac->pmat[i],NULL,"-mat_view");
652:       }
653:       /* create work vectors for each split */
654:       MatCreateVecs(jac->pmat[i],&jac->x[i],&jac->y[i]);
655:       ilink->x = jac->x[i]; ilink->y = jac->y[i]; ilink->z = NULL;
656:       /* compute scatter contexts needed by multiplicative versions and non-default splits */
657:       VecScatterCreateWithData(xtmp,ilink->is,jac->x[i],NULL,&ilink->sctx);
658:       PetscObjectQuery((PetscObject) ilink->is, "nearnullspace", (PetscObject*) &sp);
659:       if (sp) {
660:         MatSetNearNullSpace(jac->pmat[i], sp);
661:       }
662:       ilink = ilink->next;
663:     }
664:     VecDestroy(&xtmp);
665:   } else {
666:     MatReuse scall;
667:     if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
668:       for (i=0; i<nsplit; i++) {
669:         MatDestroy(&jac->pmat[i]);
670:       }
671:       scall = MAT_INITIAL_MATRIX;
672:     } else scall = MAT_REUSE_MATRIX;

674:     for (i=0; i<nsplit; i++) {
675:       Mat pmat;

677:       /* Check for preconditioning matrix attached to IS */
678:       PetscObjectQuery((PetscObject) ilink->is, "pmat", (PetscObject*) &pmat);
679:       if (!pmat) {
680:         MatCreateSubMatrix(pc->pmat,ilink->is,ilink->is_col,scall,&jac->pmat[i]);
681:       }
682:       ilink = ilink->next;
683:     }
684:   }
685:   if (jac->diag_use_amat) {
686:     ilink = jac->head;
687:     if (!jac->mat) {
688:       PetscMalloc1(nsplit,&jac->mat);
689:       for (i=0; i<nsplit; i++) {
690:         MatCreateSubMatrix(pc->mat,ilink->is,ilink->is_col,MAT_INITIAL_MATRIX,&jac->mat[i]);
691:         ilink = ilink->next;
692:       }
693:     } else {
694:       MatReuse scall;
695:       if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
696:         for (i=0; i<nsplit; i++) {
697:           MatDestroy(&jac->mat[i]);
698:         }
699:         scall = MAT_INITIAL_MATRIX;
700:       } else scall = MAT_REUSE_MATRIX;

702:       for (i=0; i<nsplit; i++) {
703:         if (jac->mat[i]) {MatCreateSubMatrix(pc->mat,ilink->is,ilink->is_col,scall,&jac->mat[i]);}
704:         ilink = ilink->next;
705:       }
706:     }
707:   } else {
708:     jac->mat = jac->pmat;
709:   }

711:   /* Check for null space attached to IS */
712:   ilink = jac->head;
713:   for (i=0; i<nsplit; i++) {
714:     MatNullSpace sp;

716:     PetscObjectQuery((PetscObject) ilink->is, "nullspace", (PetscObject*) &sp);
717:     if (sp) {
718:       MatSetNullSpace(jac->mat[i], sp);
719:     }
720:     ilink = ilink->next;
721:   }

723:   if (jac->type != PC_COMPOSITE_ADDITIVE  && jac->type != PC_COMPOSITE_SCHUR && jac->type != PC_COMPOSITE_GKB) {
724:     /* extract the rows of the matrix associated with each field: used for efficient computation of residual inside algorithm */
725:     /* FIXME: Can/should we reuse jac->mat whenever (jac->diag_use_amat) is true? */
726:     ilink = jac->head;
727:     if (nsplit == 2 && jac->type == PC_COMPOSITE_MULTIPLICATIVE) {
728:       /* 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 */
729:       if (!jac->Afield) {
730:         PetscCalloc1(nsplit,&jac->Afield);
731:         if (jac->offdiag_use_amat) {
732:           MatCreateSubMatrix(pc->mat,ilink->next->is,ilink->is,MAT_INITIAL_MATRIX,&jac->Afield[1]);
733:         } else {
734:           MatCreateSubMatrix(pc->pmat,ilink->next->is,ilink->is,MAT_INITIAL_MATRIX,&jac->Afield[1]);
735:         }
736:       } else {
737:         MatReuse scall;
738:         if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
739:           for (i=0; i<nsplit; i++) {
740:             MatDestroy(&jac->Afield[1]);
741:           }
742:           scall = MAT_INITIAL_MATRIX;
743:         } else scall = MAT_REUSE_MATRIX;

745:         if (jac->offdiag_use_amat) {
746:           MatCreateSubMatrix(pc->mat,ilink->next->is,ilink->is,scall,&jac->Afield[1]);
747:         } else {
748:           MatCreateSubMatrix(pc->pmat,ilink->next->is,ilink->is,scall,&jac->Afield[1]);
749:         }
750:       }
751:     } else {
752:       if (!jac->Afield) {
753:         PetscMalloc1(nsplit,&jac->Afield);
754:         for (i=0; i<nsplit; i++) {
755:           if (jac->offdiag_use_amat) {
756:             MatCreateSubMatrix(pc->mat,ilink->is,NULL,MAT_INITIAL_MATRIX,&jac->Afield[i]);
757:           } else {
758:             MatCreateSubMatrix(pc->pmat,ilink->is,NULL,MAT_INITIAL_MATRIX,&jac->Afield[i]);
759:           }
760:           ilink = ilink->next;
761:         }
762:       } else {
763:         MatReuse scall;
764:         if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
765:           for (i=0; i<nsplit; i++) {
766:             MatDestroy(&jac->Afield[i]);
767:           }
768:           scall = MAT_INITIAL_MATRIX;
769:         } else scall = MAT_REUSE_MATRIX;

771:         for (i=0; i<nsplit; i++) {
772:           if (jac->offdiag_use_amat) {
773:             MatCreateSubMatrix(pc->mat,ilink->is,NULL,scall,&jac->Afield[i]);
774:           } else {
775:             MatCreateSubMatrix(pc->pmat,ilink->is,NULL,scall,&jac->Afield[i]);
776:           }
777:           ilink = ilink->next;
778:         }
779:       }
780:     }
781:   }

783:   if (jac->type == PC_COMPOSITE_SCHUR) {
784:     IS          ccis;
785:     PetscBool   isspd;
786:     PetscInt    rstart,rend;
787:     char        lscname[256];
788:     PetscObject LSC_L;

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

792:     /* If pc->mat is SPD, don't scale by -1 the Schur complement */
793:     if (jac->schurscale == (PetscScalar)-1.0) {
794:       MatGetOption(pc->pmat,MAT_SPD,&isspd);
795:       jac->schurscale = (isspd == PETSC_TRUE) ? 1.0 : -1.0;
796:     }

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

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

805:       MatSchurComplementGetKSP(jac->schur, &kspInner);
806:       ilink = jac->head;
807:       ISComplement(ilink->is_col,rstart,rend,&ccis);
808:       if (jac->offdiag_use_amat) {
809:         MatCreateSubMatrix(pc->mat,ilink->is,ccis,MAT_REUSE_MATRIX,&jac->B);
810:       } else {
811:         MatCreateSubMatrix(pc->pmat,ilink->is,ccis,MAT_REUSE_MATRIX,&jac->B);
812:       }
813:       ISDestroy(&ccis);
814:       ilink = ilink->next;
815:       ISComplement(ilink->is_col,rstart,rend,&ccis);
816:       if (jac->offdiag_use_amat) {
817:         MatCreateSubMatrix(pc->mat,ilink->is,ccis,MAT_REUSE_MATRIX,&jac->C);
818:       } else {
819:         MatCreateSubMatrix(pc->pmat,ilink->is,ccis,MAT_REUSE_MATRIX,&jac->C);
820:       }
821:       ISDestroy(&ccis);
822:       MatSchurComplementUpdateSubMatrices(jac->schur,jac->mat[0],jac->pmat[0],jac->B,jac->C,jac->mat[1]);
823:       if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELFP) {
824:         MatDestroy(&jac->schurp);
825:         MatSchurComplementGetPmat(jac->schur,MAT_INITIAL_MATRIX,&jac->schurp);
826:       }
827:       if (kspA != kspInner) {
828:         KSPSetOperators(kspA,jac->mat[0],jac->pmat[0]);
829:       }
830:       if (kspUpper != kspA) {
831:         KSPSetOperators(kspUpper,jac->mat[0],jac->pmat[0]);
832:       }
833:       KSPSetOperators(jac->kspschur,jac->schur,FieldSplitSchurPre(jac));
834:     } else {
835:       const char   *Dprefix;
836:       char         schurprefix[256], schurmatprefix[256];
837:       char         schurtestoption[256];
838:       MatNullSpace sp;
839:       PetscBool    flg;
840:       KSP          kspt;

842:       /* extract the A01 and A10 matrices */
843:       ilink = jac->head;
844:       ISComplement(ilink->is_col,rstart,rend,&ccis);
845:       if (jac->offdiag_use_amat) {
846:         MatCreateSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->B);
847:       } else {
848:         MatCreateSubMatrix(pc->pmat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->B);
849:       }
850:       ISDestroy(&ccis);
851:       ilink = ilink->next;
852:       ISComplement(ilink->is_col,rstart,rend,&ccis);
853:       if (jac->offdiag_use_amat) {
854:         MatCreateSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->C);
855:       } else {
856:         MatCreateSubMatrix(pc->pmat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->C);
857:       }
858:       ISDestroy(&ccis);

860:       /* Use mat[0] (diagonal block of Amat) preconditioned by pmat[0] to define Schur complement */
861:       MatCreate(((PetscObject)jac->mat[0])->comm,&jac->schur);
862:       MatSetType(jac->schur,MATSCHURCOMPLEMENT);
863:       MatSchurComplementSetSubMatrices(jac->schur,jac->mat[0],jac->pmat[0],jac->B,jac->C,jac->mat[1]);
864:       PetscSNPrintf(schurmatprefix, sizeof(schurmatprefix), "%sfieldsplit_%s_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname);
865:       MatSetOptionsPrefix(jac->schur,schurmatprefix);
866:       MatSchurComplementGetKSP(jac->schur,&kspt);
867:       KSPSetOptionsPrefix(kspt,schurmatprefix);

869:       /* Note: this is not true in general */
870:       MatGetNullSpace(jac->mat[1], &sp);
871:       if (sp) {
872:         MatSetNullSpace(jac->schur, sp);
873:       }

875:       PetscSNPrintf(schurtestoption, sizeof(schurtestoption), "-fieldsplit_%s_inner_", ilink->splitname);
876:       PetscOptionsFindPairPrefix_Private(((PetscObject)pc)->options,((PetscObject)pc)->prefix, schurtestoption, NULL, &flg);
877:       if (flg) {
878:         DM  dmInner;
879:         KSP kspInner;
880:         PC  pcInner;

882:         MatSchurComplementGetKSP(jac->schur, &kspInner);
883:         KSPReset(kspInner);
884:         KSPSetOperators(kspInner,jac->mat[0],jac->pmat[0]);
885:         PetscSNPrintf(schurprefix, sizeof(schurprefix), "%sfieldsplit_%s_inner_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname);
886:         /* Indent this deeper to emphasize the "inner" nature of this solver. */
887:         PetscObjectIncrementTabLevel((PetscObject)kspInner, (PetscObject) pc, 2);
888:         PetscObjectIncrementTabLevel((PetscObject)kspInner->pc, (PetscObject) pc, 2);
889:         KSPSetOptionsPrefix(kspInner, schurprefix);

891:         /* Set DM for new solver */
892:         KSPGetDM(jac->head->ksp, &dmInner);
893:         KSPSetDM(kspInner, dmInner);
894:         KSPSetDMActive(kspInner, PETSC_FALSE);

896:         /* Defaults to PCKSP as preconditioner */
897:         KSPGetPC(kspInner, &pcInner);
898:         PCSetType(pcInner, PCKSP);
899:         PCKSPSetKSP(pcInner, jac->head->ksp);
900:       } else {
901:          /* Use the outer solver for the inner solve, but revert the KSPPREONLY from PCFieldSplitSetFields_FieldSplit or
902:           * PCFieldSplitSetIS_FieldSplit. We don't want KSPPREONLY because it makes the Schur complement inexact,
903:           * preventing Schur complement reduction to be an accurate solve. Usually when an iterative solver is used for
904:           * S = D - C A_inner^{-1} B, we expect S to be defined using an accurate definition of A_inner^{-1}, so we make
905:           * GMRES the default. Note that it is also common to use PREONLY for S, in which case S may not be used
906:           * directly, and the user is responsible for setting an inexact method for fieldsplit's A^{-1}. */
907:         KSPSetType(jac->head->ksp,KSPGMRES);
908:         MatSchurComplementSetKSP(jac->schur,jac->head->ksp);
909:       }
910:       KSPSetOperators(jac->head->ksp,jac->mat[0],jac->pmat[0]);
911:       KSPSetFromOptions(jac->head->ksp);
912:       MatSetFromOptions(jac->schur);

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

919:         MatSchurComplementGetKSP(jac->schur, &kspInner);
920:         KSPGetPC(kspInner, &pcInner);
921:         PetscObjectTypeCompare((PetscObject)pcInner, PCKSP, &flg);
922:         if (flg) {
923:           KSP ksp;

925:           PCKSPGetKSP(pcInner, &ksp);
926:           if (ksp == jac->head->ksp) {
927:             PCSetUseAmat(pcInner, PETSC_TRUE);
928:           }
929:         }
930:       }
931:       PetscSNPrintf(schurtestoption, sizeof(schurtestoption), "-fieldsplit_%s_upper_", ilink->splitname);
932:       PetscOptionsFindPairPrefix_Private(((PetscObject)pc)->options,((PetscObject)pc)->prefix, schurtestoption, NULL, &flg);
933:       if (flg) {
934:         DM dmInner;

936:         PetscSNPrintf(schurprefix, sizeof(schurprefix), "%sfieldsplit_%s_upper_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname);
937:         KSPCreate(PetscObjectComm((PetscObject)pc), &jac->kspupper);
938:         KSPSetErrorIfNotConverged(jac->kspupper,pc->erroriffailure);
939:         KSPSetOptionsPrefix(jac->kspupper, schurprefix);
940:         PetscObjectIncrementTabLevel((PetscObject)jac->kspupper, (PetscObject) pc, 1);
941:         PetscObjectIncrementTabLevel((PetscObject)jac->kspupper->pc, (PetscObject) pc, 1);
942:         KSPGetDM(jac->head->ksp, &dmInner);
943:         KSPSetDM(jac->kspupper, dmInner);
944:         KSPSetDMActive(jac->kspupper, PETSC_FALSE);
945:         KSPSetFromOptions(jac->kspupper);
946:         KSPSetOperators(jac->kspupper,jac->mat[0],jac->pmat[0]);
947:         VecDuplicate(jac->head->x, &jac->head->z);
948:       } else {
949:         jac->kspupper = jac->head->ksp;
950:         PetscObjectReference((PetscObject) jac->head->ksp);
951:       }

953:       if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELFP) {
954:         MatSchurComplementGetPmat(jac->schur,MAT_INITIAL_MATRIX,&jac->schurp);
955:       }
956:       KSPCreate(PetscObjectComm((PetscObject)pc),&jac->kspschur);
957:       KSPSetErrorIfNotConverged(jac->kspschur,pc->erroriffailure);
958:       PetscLogObjectParent((PetscObject)pc,(PetscObject)jac->kspschur);
959:       PetscObjectIncrementTabLevel((PetscObject)jac->kspschur,(PetscObject)pc,1);
960:       if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELF) {
961:         PC pcschur;
962:         KSPGetPC(jac->kspschur,&pcschur);
963:         PCSetType(pcschur,PCNONE);
964:         /* Note: This is bad if there exist preconditioners for MATSCHURCOMPLEMENT */
965:       } else if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_FULL) {
966:         MatSchurComplementComputeExplicitOperator(jac->schur, &jac->schur_user);
967:       }
968:       KSPSetOperators(jac->kspschur,jac->schur,FieldSplitSchurPre(jac));
969:       KSPGetOptionsPrefix(jac->head->next->ksp, &Dprefix);
970:       KSPSetOptionsPrefix(jac->kspschur,         Dprefix);
971:       /* propagate DM */
972:       {
973:         DM sdm;
974:         KSPGetDM(jac->head->next->ksp, &sdm);
975:         if (sdm) {
976:           KSPSetDM(jac->kspschur, sdm);
977:           KSPSetDMActive(jac->kspschur, PETSC_FALSE);
978:         }
979:       }
980:       /* really want setfromoptions called in PCSetFromOptions_FieldSplit(), but it is not ready yet */
981:       /* need to call this every time, since the jac->kspschur is freshly created, otherwise its options never get set */
982:       KSPSetFromOptions(jac->kspschur);
983:     }
984:     MatAssemblyBegin(jac->schur,MAT_FINAL_ASSEMBLY);
985:     MatAssemblyEnd(jac->schur,MAT_FINAL_ASSEMBLY);

987:     /* HACK: special support to forward L and Lp matrices that might be used by PCLSC */
988:     PetscSNPrintf(lscname,sizeof(lscname),"%s_LSC_L",ilink->splitname);
989:     PetscObjectQuery((PetscObject)pc->mat,lscname,(PetscObject*)&LSC_L);
990:     if (!LSC_L) {PetscObjectQuery((PetscObject)pc->pmat,lscname,(PetscObject*)&LSC_L);}
991:     if (LSC_L) {PetscObjectCompose((PetscObject)jac->schur,"LSC_L",(PetscObject)LSC_L);}
992:     PetscSNPrintf(lscname,sizeof(lscname),"%s_LSC_Lp",ilink->splitname);
993:     PetscObjectQuery((PetscObject)pc->pmat,lscname,(PetscObject*)&LSC_L);
994:     if (!LSC_L) {PetscObjectQuery((PetscObject)pc->mat,lscname,(PetscObject*)&LSC_L);}
995:     if (LSC_L) {PetscObjectCompose((PetscObject)jac->schur,"LSC_Lp",(PetscObject)LSC_L);}
996:   } else if (jac->type == PC_COMPOSITE_GKB) {
997:     IS          ccis;
998:     PetscInt    rstart,rend;

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

1002:     ilink = jac->head;

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

1007:     ISComplement(ilink->is_col,rstart,rend,&ccis);
1008:     if (jac->offdiag_use_amat) {
1009:      MatCreateSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->B);
1010:     } else {
1011:       MatCreateSubMatrix(pc->pmat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->B);
1012:     }
1013:     ISDestroy(&ccis);
1014:     /* Create work vectors for GKB algorithm */
1015:     VecDuplicate(ilink->x,&jac->u);
1016:     VecDuplicate(ilink->x,&jac->Hu);
1017:     VecDuplicate(ilink->x,&jac->w2);
1018:     ilink = ilink->next;
1019:     ISComplement(ilink->is_col,rstart,rend,&ccis);
1020:     if (jac->offdiag_use_amat) {
1021:       MatCreateSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->C);
1022:     } else {
1023:             MatCreateSubMatrix(pc->pmat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->C);
1024:     }
1025:     ISDestroy(&ccis);
1026:     /* Create work vectors for GKB algorithm */
1027:     VecDuplicate(ilink->x,&jac->v);
1028:     VecDuplicate(ilink->x,&jac->d);
1029:     VecDuplicate(ilink->x,&jac->w1);
1030:     MatGolubKahanComputeExplicitOperator(jac->mat[0],jac->B,jac->C,&jac->H,jac->gkbnu);
1031:     PetscCalloc1(jac->gkbdelay,&jac->vecz);

1033:     ilink = jac->head;
1034:     KSPSetOperators(ilink->ksp,jac->H,jac->H);
1035:     if (!jac->suboptionsset) {KSPSetFromOptions(ilink->ksp);}
1036:     /* Create gkb_monitor context */
1037:     if (jac->gkbmonitor) {
1038:       PetscInt  tablevel;
1039:       PetscViewerCreate(PETSC_COMM_WORLD,&jac->gkbviewer);
1040:       PetscViewerSetType(jac->gkbviewer,PETSCVIEWERASCII);
1041:       PetscObjectGetTabLevel((PetscObject)ilink->ksp,&tablevel);
1042:       PetscViewerASCIISetTab(jac->gkbviewer,tablevel);
1043:       PetscObjectIncrementTabLevel((PetscObject)ilink->ksp,(PetscObject)ilink->ksp,1);
1044:     }
1045:   } else {
1046:     /* set up the individual splits' PCs */
1047:     i     = 0;
1048:     ilink = jac->head;
1049:     while (ilink) {
1050:       KSPSetOperators(ilink->ksp,jac->mat[i],jac->pmat[i]);
1051:       /* really want setfromoptions called in PCSetFromOptions_FieldSplit(), but it is not ready yet */
1052:       if (!jac->suboptionsset) {KSPSetFromOptions(ilink->ksp);}
1053:       i++;
1054:       ilink = ilink->next;
1055:     }
1056:   }

1058:   jac->suboptionsset = PETSC_TRUE;
1059:   return(0);
1060: }

1062: #define FieldSplitSplitSolveAdd(ilink,xx,yy) \
1063:   (VecScatterBegin(ilink->sctx,xx,ilink->x,INSERT_VALUES,SCATTER_FORWARD) || \
1064:    VecScatterEnd(ilink->sctx,xx,ilink->x,INSERT_VALUES,SCATTER_FORWARD) || \
1065:    PetscLogEventBegin(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL) ||\
1066:    KSPSolve(ilink->ksp,ilink->x,ilink->y) ||                               \
1067:    PetscLogEventEnd(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL) ||\
1068:    VecScatterBegin(ilink->sctx,ilink->y,yy,ADD_VALUES,SCATTER_REVERSE) ||  \
1069:    VecScatterEnd(ilink->sctx,ilink->y,yy,ADD_VALUES,SCATTER_REVERSE))

1071: static PetscErrorCode PCApply_FieldSplit_Schur(PC pc,Vec x,Vec y)
1072: {
1073:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1074:   PetscErrorCode    ierr;
1075:   PC_FieldSplitLink ilinkA = jac->head, ilinkD = ilinkA->next;
1076:   KSP               kspA   = ilinkA->ksp, kspLower = kspA, kspUpper = jac->kspupper;

1079:   switch (jac->schurfactorization) {
1080:   case PC_FIELDSPLIT_SCHUR_FACT_DIAG:
1081:     /* [A00 0; 0 -S], positive definite, suitable for MINRES */
1082:     VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
1083:     VecScatterBegin(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
1084:     VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
1085:     PetscLogEventBegin(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
1086:     KSPSolve(kspA,ilinkA->x,ilinkA->y);
1087:     PetscLogEventEnd(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
1088:     VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
1089:     VecScatterEnd(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
1090:     PetscLogEventBegin(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);
1091:     KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);
1092:     PetscLogEventEnd(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);
1093:     VecScale(ilinkD->y,jac->schurscale);
1094:     VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
1095:     VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
1096:     VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
1097:     break;
1098:   case PC_FIELDSPLIT_SCHUR_FACT_LOWER:
1099:     /* [A00 0; A10 S], suitable for left preconditioning */
1100:     VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
1101:     VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
1102:     PetscLogEventBegin(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
1103:     KSPSolve(kspA,ilinkA->x,ilinkA->y);
1104:     PetscLogEventEnd(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
1105:     MatMult(jac->C,ilinkA->y,ilinkD->x);
1106:     VecScale(ilinkD->x,-1.);
1107:     VecScatterBegin(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);
1108:     VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
1109:     VecScatterEnd(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);
1110:     PetscLogEventBegin(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);
1111:     KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);
1112:     PetscLogEventEnd(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);
1113:     VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
1114:     VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
1115:     VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
1116:     break;
1117:   case PC_FIELDSPLIT_SCHUR_FACT_UPPER:
1118:     /* [A00 A01; 0 S], suitable for right preconditioning */
1119:     VecScatterBegin(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
1120:     VecScatterEnd(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
1121:     PetscLogEventBegin(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);
1122:     KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);
1123:     PetscLogEventEnd(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);    MatMult(jac->B,ilinkD->y,ilinkA->x);
1124:     VecScale(ilinkA->x,-1.);
1125:     VecScatterBegin(ilinkA->sctx,x,ilinkA->x,ADD_VALUES,SCATTER_FORWARD);
1126:     VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
1127:     VecScatterEnd(ilinkA->sctx,x,ilinkA->x,ADD_VALUES,SCATTER_FORWARD);
1128:     PetscLogEventBegin(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
1129:     KSPSolve(kspA,ilinkA->x,ilinkA->y);
1130:     PetscLogEventEnd(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
1131:     VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
1132:     VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
1133:     VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
1134:     break;
1135:   case PC_FIELDSPLIT_SCHUR_FACT_FULL:
1136:     /* [1 0; A10 A00^{-1} 1] [A00 0; 0 S] [1 A00^{-1}A01; 0 1] */
1137:     VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
1138:     VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
1139:     PetscLogEventBegin(KSP_Solve_FS_L,kspLower,ilinkA->x,ilinkA->y,NULL);
1140:     KSPSolve(kspLower,ilinkA->x,ilinkA->y);
1141:     PetscLogEventEnd(KSP_Solve_FS_L,kspLower,ilinkA->x,ilinkA->y,NULL);
1142:     MatMult(jac->C,ilinkA->y,ilinkD->x);
1143:     VecScale(ilinkD->x,-1.0);
1144:     VecScatterBegin(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);
1145:     VecScatterEnd(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);

1147:     PetscLogEventBegin(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);
1148:     KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);
1149:     PetscLogEventEnd(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);
1150:     VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);

1152:     if (kspUpper == kspA) {
1153:       MatMult(jac->B,ilinkD->y,ilinkA->y);
1154:       VecAXPY(ilinkA->x,-1.0,ilinkA->y);
1155:       PetscLogEventBegin(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
1156:       KSPSolve(kspA,ilinkA->x,ilinkA->y);
1157:       PetscLogEventEnd(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
1158:     } else {
1159:       PetscLogEventBegin(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
1160:       KSPSolve(kspA,ilinkA->x,ilinkA->y);
1161:       PetscLogEventEnd(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
1162:       MatMult(jac->B,ilinkD->y,ilinkA->x);
1163:       PetscLogEventBegin(KSP_Solve_FS_U,kspUpper,ilinkA->x,ilinkA->z,NULL);
1164:       KSPSolve(kspUpper,ilinkA->x,ilinkA->z);
1165:       PetscLogEventEnd(KSP_Solve_FS_U,kspUpper,ilinkA->x,ilinkA->z,NULL);
1166:       VecAXPY(ilinkA->y,-1.0,ilinkA->z);
1167:     }
1168:     VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
1169:     VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
1170:     VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
1171:   }
1172:   return(0);
1173: }

1175: static PetscErrorCode PCApply_FieldSplit(PC pc,Vec x,Vec y)
1176: {
1177:   PC_FieldSplit      *jac = (PC_FieldSplit*)pc->data;
1178:   PetscErrorCode     ierr;
1179:   PC_FieldSplitLink  ilink = jac->head;
1180:   PetscInt           cnt,bs;
1181:   KSPConvergedReason reason;

1184:   if (jac->type == PC_COMPOSITE_ADDITIVE) {
1185:     if (jac->defaultsplit) {
1186:       VecGetBlockSize(x,&bs);
1187:       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);
1188:       VecGetBlockSize(y,&bs);
1189:       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);
1190:       VecStrideGatherAll(x,jac->x,INSERT_VALUES);
1191:       while (ilink) {
1192:         PetscLogEventBegin(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1193:         KSPSolve(ilink->ksp,ilink->x,ilink->y);
1194:         PetscLogEventEnd(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1195:         KSPGetConvergedReason(ilink->ksp,&reason);
1196:         if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1197:           pc->failedreason = PC_SUBPC_ERROR;
1198:         }
1199:         ilink = ilink->next;
1200:       }
1201:       VecStrideScatterAll(jac->y,y,INSERT_VALUES);
1202:     } else {
1203:       VecSet(y,0.0);
1204:       while (ilink) {
1205:         FieldSplitSplitSolveAdd(ilink,x,y);
1206:         KSPGetConvergedReason(ilink->ksp,&reason);
1207:         if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1208:           pc->failedreason = PC_SUBPC_ERROR;
1209:         }
1210:         ilink = ilink->next;
1211:       }
1212:     }
1213:   } else if (jac->type == PC_COMPOSITE_MULTIPLICATIVE && jac->nsplits == 2) {
1214:     VecSet(y,0.0);
1215:     /* solve on first block for first block variables */
1216:     VecScatterBegin(ilink->sctx,x,ilink->x,INSERT_VALUES,SCATTER_FORWARD);
1217:     VecScatterEnd(ilink->sctx,x,ilink->x,INSERT_VALUES,SCATTER_FORWARD);
1218:     PetscLogEventBegin(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1219:     KSPSolve(ilink->ksp,ilink->x,ilink->y);
1220:     PetscLogEventEnd(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1221:     KSPGetConvergedReason(ilink->ksp,&reason);
1222:     if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1223:       pc->failedreason = PC_SUBPC_ERROR;
1224:     }
1225:     VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
1226:     VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);

1228:     /* compute the residual only onto second block variables using first block variables */
1229:     MatMult(jac->Afield[1],ilink->y,ilink->next->x);
1230:     ilink = ilink->next;
1231:     VecScale(ilink->x,-1.0);
1232:     VecScatterBegin(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
1233:     VecScatterEnd(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);

1235:     /* solve on second block variables */
1236:     PetscLogEventBegin(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1237:     KSPSolve(ilink->ksp,ilink->x,ilink->y);
1238:     PetscLogEventEnd(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1239:     KSPGetConvergedReason(ilink->ksp,&reason);
1240:     if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1241:       pc->failedreason = PC_SUBPC_ERROR;
1242:     }
1243:     VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
1244:     VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
1245:   } else if (jac->type == PC_COMPOSITE_MULTIPLICATIVE || jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
1246:     if (!jac->w1) {
1247:       VecDuplicate(x,&jac->w1);
1248:       VecDuplicate(x,&jac->w2);
1249:     }
1250:     VecSet(y,0.0);
1251:     FieldSplitSplitSolveAdd(ilink,x,y);
1252:     KSPGetConvergedReason(ilink->ksp,&reason);
1253:     if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1254:       pc->failedreason = PC_SUBPC_ERROR;
1255:     }
1256:     cnt  = 1;
1257:     while (ilink->next) {
1258:       ilink = ilink->next;
1259:       /* compute the residual only over the part of the vector needed */
1260:       MatMult(jac->Afield[cnt++],y,ilink->x);
1261:       VecScale(ilink->x,-1.0);
1262:       VecScatterBegin(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
1263:       VecScatterEnd(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
1264:       PetscLogEventBegin(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1265:       KSPSolve(ilink->ksp,ilink->x,ilink->y);
1266:       PetscLogEventEnd(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1267:       KSPGetConvergedReason(ilink->ksp,&reason);
1268:       if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1269:         pc->failedreason = PC_SUBPC_ERROR;
1270:       }
1271:       VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
1272:       VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
1273:     }
1274:     if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
1275:       cnt -= 2;
1276:       while (ilink->previous) {
1277:         ilink = ilink->previous;
1278:         /* compute the residual only over the part of the vector needed */
1279:         MatMult(jac->Afield[cnt--],y,ilink->x);
1280:         VecScale(ilink->x,-1.0);
1281:         VecScatterBegin(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
1282:         VecScatterEnd(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
1283:         PetscLogEventBegin(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1284:         KSPSolve(ilink->ksp,ilink->x,ilink->y);
1285:         PetscLogEventEnd(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1286:         KSPGetConvergedReason(ilink->ksp,&reason);
1287:         if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1288:           pc->failedreason = PC_SUBPC_ERROR;
1289:         }
1290:         VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
1291:         VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
1292:       }
1293:     }
1294:   } else SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Unsupported or unknown composition",(int) jac->type);
1295:   return(0);
1296: }


1299: static PetscErrorCode PCApply_FieldSplit_GKB(PC pc,Vec x,Vec y)
1300: {
1301:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1302:   PetscErrorCode    ierr;
1303:   PC_FieldSplitLink ilinkA = jac->head,ilinkD = ilinkA->next;
1304:   KSP               ksp = ilinkA->ksp;
1305:   Vec               u,v,Hu,d,work1,work2;
1306:   PetscScalar       alpha,z,nrmz2,*vecz;
1307:   PetscReal         lowbnd,nu,beta;
1308:   PetscInt          j,iterGKB;

1311:   VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
1312:   VecScatterBegin(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
1313:   VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
1314:   VecScatterEnd(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);

1316:   u     = jac->u;
1317:   v     = jac->v;
1318:   Hu    = jac->Hu;
1319:   d     = jac->d;
1320:   work1 = jac->w1;
1321:   work2 = jac->w2;
1322:   vecz  = jac->vecz;

1324:   /* Change RHS to comply with matrix regularization H = A + nu*B*B' */
1325:   /* Add q = q + nu*B*b */
1326:   if (jac->gkbnu) {
1327:     nu = jac->gkbnu;
1328:     VecScale(ilinkD->x,jac->gkbnu);
1329:     MatMultAdd(jac->B,ilinkD->x,ilinkA->x,ilinkA->x);            /* q = q + nu*B*b */
1330:   } else {
1331:     /* Situation when no augmented Lagrangian is used. Then we set inner  */
1332:     /* matrix N = I in [Ar13], and thus nu = 1.                           */
1333:     nu = 1;
1334:   }

1336:   /* Transform rhs from [q,tilde{b}] to [0,b] */
1337:   PetscLogEventBegin(ilinkA->event,ksp,ilinkA->x,ilinkA->y,NULL);
1338:   KSPSolve(ksp,ilinkA->x,ilinkA->y);
1339:   PetscLogEventEnd(ilinkA->event,ksp,ilinkA->x,ilinkA->y,NULL);
1340:   MatMultHermitianTranspose(jac->B,ilinkA->y,work1);
1341:   VecAXPBY(work1,1.0/nu,-1.0,ilinkD->x);            /* c = b - B'*x        */

1343:   /* First step of algorithm */
1344:   VecNorm(work1,NORM_2,&beta);                   /* beta = sqrt(nu*c'*c)*/
1345:   KSPCheckDot(ksp,beta);
1346:   beta  = PetscSqrtScalar(nu)*beta;
1347:   VecAXPBY(v,nu/beta,0.0,work1);                   /* v = nu/beta *c      */
1348:   MatMult(jac->B,v,work2);                       /* u = H^{-1}*B*v      */
1349:   PetscLogEventBegin(ilinkA->event,ksp,work2,u,NULL);
1350:   KSPSolve(ksp,work2,u);
1351:   PetscLogEventEnd(ilinkA->event,ksp,work2,u,NULL);
1352:   MatMult(jac->H,u,Hu);                          /* alpha = u'*H*u      */
1353:   VecDot(Hu,u,&alpha);
1354:   KSPCheckDot(ksp,alpha);
1355:   if (PetscRealPart(alpha) <= 0.0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_NOT_CONVERGED,"GKB preconditioner diverged, H is not positive definite");
1356:   alpha = PetscSqrtScalar(PetscAbsScalar(alpha));
1357:   VecScale(u,1.0/alpha);
1358:   VecAXPBY(d,1.0/alpha,0.0,v);                       /* v = nu/beta *c      */

1360:   z = beta/alpha;
1361:   vecz[1] = z;

1363:   /* Computation of first iterate x(1) and p(1) */
1364:   VecAXPY(ilinkA->y,z,u);
1365:   VecCopy(d,ilinkD->y);
1366:   VecScale(ilinkD->y,-z);

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

1373:   while (iterGKB < jac->gkbmaxit && lowbnd > jac->gkbtol) {
1374:     iterGKB += 1;
1375:     MatMultHermitianTranspose(jac->B,u,work1); /* v <- nu*(B'*u-alpha/nu*v) */
1376:     VecAXPBY(v,nu,-alpha,work1);
1377:     VecNorm(v,NORM_2,&beta);                   /* beta = sqrt(nu)*v'*v      */
1378:     beta  = beta/PetscSqrtScalar(nu);
1379:     VecScale(v,1.0/beta);
1380:     MatMult(jac->B,v,work2);                  /* u <- H^{-1}*(B*v-beta*H*u) */
1381:     MatMult(jac->H,u,Hu);
1382:     VecAXPY(work2,-beta,Hu);
1383:     PetscLogEventBegin(ilinkA->event,ksp,work2,u,NULL);
1384:     KSPSolve(ksp,work2,u);
1385:     PetscLogEventEnd(ilinkA->event,ksp,work2,u,NULL);
1386:     MatMult(jac->H,u,Hu);                      /* alpha = u'*H*u            */
1387:     VecDot(Hu,u,&alpha);
1388:     KSPCheckDot(ksp,alpha);
1389:     if (PetscRealPart(alpha) <= 0.0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_NOT_CONVERGED,"GKB preconditioner diverged, H is not positive definite");
1390:     alpha = PetscSqrtScalar(PetscAbsScalar(alpha));
1391:     VecScale(u,1.0/alpha);

1393:     z = -beta/alpha*z;                                            /* z <- beta/alpha*z     */
1394:     vecz[0] = z;

1396:     /* Computation of new iterate x(i+1) and p(i+1) */
1397:     VecAXPBY(d,1.0/alpha,-beta/alpha,v);       /* d = (v-beta*d)/alpha */
1398:     VecAXPY(ilinkA->y,z,u);                  /* r = r + z*u          */
1399:     VecAXPY(ilinkD->y,-z,d);                 /* p = p - z*d          */
1400:     MatMult(jac->H,ilinkA->y,Hu);            /* ||u||_H = u'*H*u     */
1401:     VecDot(Hu,ilinkA->y,&nrmz2);

1403:     /* Compute Lower Bound estimate */
1404:     if (iterGKB > jac->gkbdelay) {
1405:       lowbnd = 0.0;
1406:       for (j=0; j<jac->gkbdelay; j++) {
1407:         lowbnd += PetscAbsScalar(vecz[j]*vecz[j]);
1408:       }
1409:       lowbnd = PetscSqrtScalar(lowbnd/PetscAbsScalar(nrmz2));
1410:     }

1412:     for (j=0; j<jac->gkbdelay-1; j++) {
1413:       vecz[jac->gkbdelay-j-1] = vecz[jac->gkbdelay-j-2];
1414:     }
1415:     if (jac->gkbmonitor) {
1416:       PetscViewerASCIIPrintf(jac->gkbviewer,"%3D GKB Lower bound estimate %14.12e\n",iterGKB,lowbnd);
1417:     }
1418:   }

1420:   VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
1421:   VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
1422:   VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
1423:   VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);

1425:   return(0);
1426: }


1429: #define FieldSplitSplitSolveAddTranspose(ilink,xx,yy) \
1430:   (VecScatterBegin(ilink->sctx,xx,ilink->y,INSERT_VALUES,SCATTER_FORWARD) || \
1431:    VecScatterEnd(ilink->sctx,xx,ilink->y,INSERT_VALUES,SCATTER_FORWARD) || \
1432:    PetscLogEventBegin(ilink->event,ilink->ksp,ilink->y,ilink->x,NULL) || \
1433:    KSPSolveTranspose(ilink->ksp,ilink->y,ilink->x) ||                  \
1434:    PetscLogEventBegin(ilink->event,ilink->ksp,ilink->y,ilink->x,NULL) || \
1435:    VecScatterBegin(ilink->sctx,ilink->x,yy,ADD_VALUES,SCATTER_REVERSE) || \
1436:    VecScatterEnd(ilink->sctx,ilink->x,yy,ADD_VALUES,SCATTER_REVERSE))

1438: static PetscErrorCode PCApplyTranspose_FieldSplit(PC pc,Vec x,Vec y)
1439: {
1440:   PC_FieldSplit      *jac = (PC_FieldSplit*)pc->data;
1441:   PetscErrorCode     ierr;
1442:   PC_FieldSplitLink  ilink = jac->head;
1443:   PetscInt           bs;
1444:   KSPConvergedReason reason;

1447:   if (jac->type == PC_COMPOSITE_ADDITIVE) {
1448:     if (jac->defaultsplit) {
1449:       VecGetBlockSize(x,&bs);
1450:       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);
1451:       VecGetBlockSize(y,&bs);
1452:       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);
1453:       VecStrideGatherAll(x,jac->x,INSERT_VALUES);
1454:       while (ilink) {
1455:         PetscLogEventBegin(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1456:         KSPSolveTranspose(ilink->ksp,ilink->x,ilink->y);
1457:         PetscLogEventEnd(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1458:         KSPGetConvergedReason(ilink->ksp,&reason);
1459:         if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1460:           pc->failedreason = PC_SUBPC_ERROR;
1461:         }
1462:         ilink = ilink->next;
1463:       }
1464:       VecStrideScatterAll(jac->y,y,INSERT_VALUES);
1465:     } else {
1466:       VecSet(y,0.0);
1467:       while (ilink) {
1468:         FieldSplitSplitSolveAddTranspose(ilink,x,y);
1469:         KSPGetConvergedReason(ilink->ksp,&reason);
1470:         if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1471:           pc->failedreason = PC_SUBPC_ERROR;
1472:         }
1473:         ilink = ilink->next;
1474:       }
1475:     }
1476:   } else {
1477:     if (!jac->w1) {
1478:       VecDuplicate(x,&jac->w1);
1479:       VecDuplicate(x,&jac->w2);
1480:     }
1481:     VecSet(y,0.0);
1482:     if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
1483:       FieldSplitSplitSolveAddTranspose(ilink,x,y);
1484:       KSPGetConvergedReason(ilink->ksp,&reason);
1485:       if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1486:         pc->failedreason = PC_SUBPC_ERROR;
1487:       }
1488:       while (ilink->next) {
1489:         ilink = ilink->next;
1490:         MatMultTranspose(pc->mat,y,jac->w1);
1491:         VecWAXPY(jac->w2,-1.0,jac->w1,x);
1492:         FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);
1493:       }
1494:       while (ilink->previous) {
1495:         ilink = ilink->previous;
1496:         MatMultTranspose(pc->mat,y,jac->w1);
1497:         VecWAXPY(jac->w2,-1.0,jac->w1,x);
1498:         FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);
1499:       }
1500:     } else {
1501:       while (ilink->next) {   /* get to last entry in linked list */
1502:         ilink = ilink->next;
1503:       }
1504:       FieldSplitSplitSolveAddTranspose(ilink,x,y);
1505:       KSPGetConvergedReason(ilink->ksp,&reason);
1506:       if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1507:         pc->failedreason = PC_SUBPC_ERROR;
1508:       }
1509:       while (ilink->previous) {
1510:         ilink = ilink->previous;
1511:         MatMultTranspose(pc->mat,y,jac->w1);
1512:         VecWAXPY(jac->w2,-1.0,jac->w1,x);
1513:         FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);
1514:       }
1515:     }
1516:   }
1517:   return(0);
1518: }

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

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

1572: static PetscErrorCode PCDestroy_FieldSplit(PC pc)
1573: {
1574:   PetscErrorCode    ierr;

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

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

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

1642: /*------------------------------------------------------------------------------------*/

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

1653:   if (jac->splitdefined) {
1654:     PetscInfo1(pc,"Ignoring new split \"%s\" because the splits have already been defined\n",splitname);
1655:     return(0);
1656:   }
1657:   for (i=0; i<n; i++) {
1658:     if (fields[i] >= jac->bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Field %D requested but only %D exist",fields[i],jac->bs);
1659:     if (fields[i] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative field %D requested",fields[i]);
1660:   }
1661:   PetscNew(&ilink);
1662:   if (splitname) {
1663:     PetscStrallocpy(splitname,&ilink->splitname);
1664:   } else {
1665:     PetscMalloc1(3,&ilink->splitname);
1666:     PetscSNPrintf(ilink->splitname,2,"%s",jac->nsplits);
1667:   }
1668:   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 */
1669:   PetscMalloc1(n,&ilink->fields);
1670:   PetscMemcpy(ilink->fields,fields,n*sizeof(PetscInt));
1671:   PetscMalloc1(n,&ilink->fields_col);
1672:   PetscMemcpy(ilink->fields_col,fields_col,n*sizeof(PetscInt));

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

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

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

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

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

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

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

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

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

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

1745:   PetscMalloc1(jac->nsplits,subksp);
1746:   while (ilink) {
1747:     (*subksp)[cnt++] = ilink->ksp;
1748:     ilink            = ilink->next;
1749:   }
1750:   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);
1751:   if (n) *n = jac->nsplits;
1752:   return(0);
1753: }

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

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

1762:     Level: advanced

1764: @*/
1765: PetscErrorCode  PCFieldSplitRestrictIS(PC pc,IS isy)
1766: {

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


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

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

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

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

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

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

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

1891:     Logically Collective on PC

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

1899:     Level: intermediate

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

1904:      The PCFieldSplitSetFields() is for defining fields as strided blocks. For example, if the block
1905:      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
1906:      0xx3xx6xx9xx12 ... x1xx4xx7xx ... xx2xx5xx8xx.. 01x34x67x... 0x1x3x5x7.. x12x45x78x....
1907:      where the numbered entries indicate what is in the field.

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

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

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

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

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

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

1935:     Logically Collective on PC

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

1941:     Options Database:
1942: .     -pc_fieldsplit_diag_use_amat

1944:     Level: intermediate

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

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

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

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

1966:     Logically Collective on PC

1968:     Input Parameters:
1969: .   pc  - the preconditioner object

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


1975:     Level: intermediate

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

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

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

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

1998:     Logically Collective on PC

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

2004:     Options Database:
2005: .     -pc_fieldsplit_off_diag_use_amat

2007:     Level: intermediate

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

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

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

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

2029:     Logically Collective on PC

2031:     Input Parameters:
2032: .   pc  - the preconditioner object

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


2038:     Level: intermediate

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

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

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



2060: /*@C
2061:     PCFieldSplitSetIS - Sets the exact elements for field

2063:     Logically Collective on PC

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


2071:     Notes:
2072:     Use PCFieldSplitSetFields(), for fields defined by strided types.

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

2077:     Level: intermediate

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

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

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

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

2097:     Logically Collective on PC

2099:     Input Parameters:
2100: +   pc  - the preconditioner context
2101: -   splitname - name of this split

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

2106:     Level: intermediate

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

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

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

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

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

2141:     Logically Collective on PC

2143:     Input Parameters:
2144: +   pc  - the preconditioner context
2145: -   bs - the block size

2147:     Level: intermediate

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

2151: @*/
2152: PetscErrorCode  PCFieldSplitSetBlockSize(PC pc,PetscInt bs)
2153: {

2159:   PetscTryMethod(pc,"PCFieldSplitSetBlockSize_C",(PC,PetscInt),(pc,bs));
2160:   return(0);
2161: }

2163: /*@C
2164:    PCFieldSplitGetSubKSP - Gets the KSP contexts for all splits

2166:    Collective on KSP

2168:    Input Parameter:
2169: .  pc - the preconditioner context

2171:    Output Parameters:
2172: +  n - the number of splits
2173: -  subksp - the array of KSP contexts

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

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

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

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

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


2193:    Level: advanced

2195: .seealso: PCFIELDSPLIT
2196: @*/
2197: PetscErrorCode  PCFieldSplitGetSubKSP(PC pc,PetscInt *n,KSP *subksp[])
2198: {

2204:   PetscUseMethod(pc,"PCFieldSplitGetSubKSP_C",(PC,PetscInt*,KSP **),(pc,n,subksp));
2205:   return(0);
2206: }

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

2211:    Collective on KSP

2213:    Input Parameter:
2214: .  pc - the preconditioner context

2216:    Output Parameters:
2217: +  n - the number of splits
2218: -  subksp - the array of KSP contexts

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

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

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

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

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

2237:    Level: advanced

2239: .seealso: PCFIELDSPLIT
2240: @*/
2241: PetscErrorCode  PCFieldSplitSchurGetSubKSP(PC pc,PetscInt *n,KSP *subksp[])
2242: {

2248:   PetscUseMethod(pc,"PCFieldSplitSchurGetSubKSP_C",(PC,PetscInt*,KSP **),(pc,n,subksp));
2249:   return(0);
2250: }

2252: /*@
2253:     PCFieldSplitSetSchurPre -  Indicates what operator is used to construct the preconditioner for the Schur complement.
2254:       A11 matrix. Otherwise no preconditioner is used.

2256:     Collective on PC

2258:     Input Parameters:
2259: +   pc      - the preconditioner context
2260: .   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
2261:               PC_FIELDSPLIT_SCHUR_PRE_SELFP, and PC_FIELDSPLIT_SCHUR_PRE_FULL
2262: -   userpre - matrix to use for preconditioning, or NULL

2264:     Options Database:
2265: .     -pc_fieldsplit_schur_precondition <self,selfp,user,a11,full> - default is a11. See notes for meaning of various arguments

2267:     Notes:
2268: $    If ptype is
2269: $        a11 then the preconditioner for the Schur complement is generated from the block diagonal part of the preconditioner
2270: $             matrix associated with the Schur complement (i.e. A11), not the Schur complement matrix
2271: $        self the preconditioner for the Schur complement is generated from the symbolic representation of the Schur complement matrix:
2272: $             The only preconditioner that currently works with this symbolic respresentation matrix object is the PCLSC
2273: $             preconditioner
2274: $        user then the preconditioner for the Schur complement is generated from the user provided matrix (pre argument
2275: $             to this function).
2276: $        selfp then the preconditioning for the Schur complement is generated from an explicitly-assembled approximation Sp = A11 - A10 inv(diag(A00)) A01
2277: $             This is only a good preconditioner when diag(A00) is a good preconditioner for A00. Optionally, A00 can be
2278: $             lumped before extracting the diagonal using the additional option -fieldsplit_1_mat_schur_complement_ainv_type lump
2279: $        full then the preconditioner for the Schur complement is generated from the exact Schur complement matrix representation computed internally by PCFIELDSPLIT (this is expensive)
2280: $             useful mostly as a test that the Schur complement approach can work for your problem

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

2286:     Level: intermediate

2288: .seealso: PCFieldSplitGetSchurPre(), PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields(), PCFieldSplitSchurPreType,
2289:           MatSchurComplementSetAinvType(), PCLSC

2291: @*/
2292: PetscErrorCode PCFieldSplitSetSchurPre(PC pc,PCFieldSplitSchurPreType ptype,Mat pre)
2293: {

2298:   PetscTryMethod(pc,"PCFieldSplitSetSchurPre_C",(PC,PCFieldSplitSchurPreType,Mat),(pc,ptype,pre));
2299:   return(0);
2300: }

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

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

2308:     Logically Collective on PC

2310:     Input Parameters:
2311: .   pc      - the preconditioner context

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

2317:     Level: intermediate

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

2321: @*/
2322: PetscErrorCode PCFieldSplitGetSchurPre(PC pc,PCFieldSplitSchurPreType *ptype,Mat *pre)
2323: {

2328:   PetscUseMethod(pc,"PCFieldSplitGetSchurPre_C",(PC,PCFieldSplitSchurPreType*,Mat*),(pc,ptype,pre));
2329:   return(0);
2330: }

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

2335:     Not collective

2337:     Input Parameter:
2338: .   pc      - the preconditioner context

2340:     Output Parameter:
2341: .   S       - the Schur complement matrix

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

2346:     Level: advanced

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

2350: @*/
2351: PetscErrorCode  PCFieldSplitSchurGetS(PC pc,Mat *S)
2352: {
2354:   const char*    t;
2355:   PetscBool      isfs;
2356:   PC_FieldSplit  *jac;

2360:   PetscObjectGetType((PetscObject)pc,&t);
2361:   PetscStrcmp(t,PCFIELDSPLIT,&isfs);
2362:   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PC of type PCFIELDSPLIT, got %s instead",t);
2363:   jac = (PC_FieldSplit*)pc->data;
2364:   if (jac->type != PC_COMPOSITE_SCHUR) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PCFIELDSPLIT of type SCHUR, got %D instead",jac->type);
2365:   if (S) *S = jac->schur;
2366:   return(0);
2367: }

2369: /*@
2370:     PCFieldSplitSchurRestoreS -  restores the MatSchurComplement object used by this PC

2372:     Not collective

2374:     Input Parameters:
2375: +   pc      - the preconditioner context
2376: .   S       - the Schur complement matrix

2378:     Level: advanced

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

2382: @*/
2383: PetscErrorCode  PCFieldSplitSchurRestoreS(PC pc,Mat *S)
2384: {
2386:   const char*    t;
2387:   PetscBool      isfs;
2388:   PC_FieldSplit  *jac;

2392:   PetscObjectGetType((PetscObject)pc,&t);
2393:   PetscStrcmp(t,PCFIELDSPLIT,&isfs);
2394:   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PC of type PCFIELDSPLIT, got %s instead",t);
2395:   jac = (PC_FieldSplit*)pc->data;
2396:   if (jac->type != PC_COMPOSITE_SCHUR) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PCFIELDSPLIT of type SCHUR, got %D instead",jac->type);
2397:   if (!S || *S != jac->schur) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"MatSchurComplement restored is not the same as gotten");
2398:   return(0);
2399: }


2402: static PetscErrorCode  PCFieldSplitSetSchurPre_FieldSplit(PC pc,PCFieldSplitSchurPreType ptype,Mat pre)
2403: {
2404:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;

2408:   jac->schurpre = ptype;
2409:   if (ptype == PC_FIELDSPLIT_SCHUR_PRE_USER && pre) {
2410:     MatDestroy(&jac->schur_user);
2411:     jac->schur_user = pre;
2412:     PetscObjectReference((PetscObject)jac->schur_user);
2413:   }
2414:   return(0);
2415: }

2417: static PetscErrorCode  PCFieldSplitGetSchurPre_FieldSplit(PC pc,PCFieldSplitSchurPreType *ptype,Mat *pre)
2418: {
2419:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;

2422:   *ptype = jac->schurpre;
2423:   *pre   = jac->schur_user;
2424:   return(0);
2425: }

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

2430:     Collective on PC

2432:     Input Parameters:
2433: +   pc  - the preconditioner context
2434: -   ftype - which blocks of factorization to retain, PC_FIELDSPLIT_SCHUR_FACT_FULL is default

2436:     Options Database:
2437: .     -pc_fieldsplit_schur_fact_type <diag,lower,upper,full> default is full


2440:     Level: intermediate

2442:     Notes:
2443:     The FULL factorization is

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

2448:     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,
2449:     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().

2451: $    If A and S are solved exactly
2452: $      *) FULL factorization is a direct solver.
2453: $      *) 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.
2454: $      *) 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.

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

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

2461:     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).

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

2467: .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields(), PCFieldSplitSchurPreType, PCFieldSplitSetSchurScale()
2468: @*/
2469: PetscErrorCode  PCFieldSplitSetSchurFactType(PC pc,PCFieldSplitSchurFactType ftype)
2470: {

2475:   PetscTryMethod(pc,"PCFieldSplitSetSchurFactType_C",(PC,PCFieldSplitSchurFactType),(pc,ftype));
2476:   return(0);
2477: }

2479: static PetscErrorCode PCFieldSplitSetSchurFactType_FieldSplit(PC pc,PCFieldSplitSchurFactType ftype)
2480: {
2481:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;

2484:   jac->schurfactorization = ftype;
2485:   return(0);
2486: }

2488: /*@
2489:     PCFieldSplitSetSchurScale -  Controls the sign flip of S for PC_FIELDSPLIT_SCHUR_FACT_DIAG.

2491:     Collective on PC

2493:     Input Parameters:
2494: +   pc    - the preconditioner context
2495: -   scale - scaling factor for the Schur complement

2497:     Options Database:
2498: .     -pc_fieldsplit_schur_scale - default is -1.0

2500:     Level: intermediate

2502: .seealso: PCFIELDSPLIT, PCFieldSplitSetFields(), PCFieldSplitSchurFactType, PCFieldSplitSetSchurScale()
2503: @*/
2504: PetscErrorCode PCFieldSplitSetSchurScale(PC pc,PetscScalar scale)
2505: {

2511:   PetscTryMethod(pc,"PCFieldSplitSetSchurScale_C",(PC,PetscScalar),(pc,scale));
2512:   return(0);
2513: }

2515: static PetscErrorCode PCFieldSplitSetSchurScale_FieldSplit(PC pc,PetscScalar scale)
2516: {
2517:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;

2520:   jac->schurscale = scale;
2521:   return(0);
2522: }

2524: /*@C
2525:    PCFieldSplitGetSchurBlocks - Gets all matrix blocks for the Schur complement

2527:    Collective on KSP

2529:    Input Parameter:
2530: .  pc - the preconditioner context

2532:    Output Parameters:
2533: +  A00 - the (0,0) block
2534: .  A01 - the (0,1) block
2535: .  A10 - the (1,0) block
2536: -  A11 - the (1,1) block

2538:    Level: advanced

2540: .seealso: PCFIELDSPLIT
2541: @*/
2542: PetscErrorCode  PCFieldSplitGetSchurBlocks(PC pc,Mat *A00,Mat *A01,Mat *A10, Mat *A11)
2543: {
2544:   PC_FieldSplit *jac = (PC_FieldSplit*) pc->data;

2548:   if (jac->type != PC_COMPOSITE_SCHUR) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONG, "FieldSplit is not using a Schur complement approach.");
2549:   if (A00) *A00 = jac->pmat[0];
2550:   if (A01) *A01 = jac->B;
2551:   if (A10) *A10 = jac->C;
2552:   if (A11) *A11 = jac->pmat[1];
2553:   return(0);
2554: }

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

2559:     Collective on PC

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

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

2568:     Input Parameters:
2569: +   pc        - the preconditioner context
2570: -   tolerance - the solver tolerance

2572:     Options Database:
2573: .     -pc_fieldsplit_gkb_tol - default is 1e-5

2575:     Level: intermediate

2577: .seealso: PCFIELDSPLIT, PCFieldSplitSetGKBDelay(), PCFieldSplitSetGKBNu(), PCFieldSplitSetGKBMaxit()
2578: @*/
2579: PetscErrorCode PCFieldSplitSetGKBTol(PC pc,PetscReal tolerance)
2580: {

2586:   PetscTryMethod(pc,"PCFieldSplitSetGKBTol_C",(PC,PetscReal),(pc,tolerance));
2587:   return(0);
2588: }

2590: static PetscErrorCode PCFieldSplitSetGKBTol_FieldSplit(PC pc,PetscReal tolerance)
2591: {
2592:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;

2595:   jac->gkbtol = tolerance;
2596:   return(0);
2597: }


2600: /*@
2601:     PCFieldSplitSetGKBMaxit -  Sets the maximum number of iterations for the generalized Golub-Kahan bidiagonalization
2602:     preconditioner.

2604:     Collective on PC

2606:     Input Parameters:
2607: +   pc     - the preconditioner context
2608: -   maxit  - the maximum number of iterations

2610:     Options Database:
2611: .     -pc_fieldsplit_gkb_maxit - default is 100

2613:     Level: intermediate

2615: .seealso: PCFIELDSPLIT, PCFieldSplitSetGKBDelay(), PCFieldSplitSetGKBTol(), PCFieldSplitSetGKBNu()
2616: @*/
2617: PetscErrorCode PCFieldSplitSetGKBMaxit(PC pc,PetscInt maxit)
2618: {

2624:   PetscTryMethod(pc,"PCFieldSplitSetGKBMaxit_C",(PC,PetscInt),(pc,maxit));
2625:   return(0);
2626: }

2628: static PetscErrorCode PCFieldSplitSetGKBMaxit_FieldSplit(PC pc,PetscInt maxit)
2629: {
2630:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;

2633:   jac->gkbmaxit = maxit;
2634:   return(0);
2635: }

2637: /*@
2638:     PCFieldSplitSetGKBDelay -  Sets the delay in the lower bound error estimate in the generalized Golub-Kahan bidiagonalization
2639:     preconditioner.

2641:     Collective on PC

2643:     Notes:
2644:     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
2645:     is expressed as a truncated sum. The error at iteration k can only be measured at iteration (k + delay), and thus the algorithm needs
2646:     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

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

2650:     Input Parameters:
2651: +   pc     - the preconditioner context
2652: -   delay  - the delay window in the lower bound estimate

2654:     Options Database:
2655: .     -pc_fieldsplit_gkb_delay - default is 5

2657:     Level: intermediate

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

2668:   PetscTryMethod(pc,"PCFieldSplitSetGKBDelay_C",(PC,PetscInt),(pc,delay));
2669:   return(0);
2670: }

2672: static PetscErrorCode PCFieldSplitSetGKBDelay_FieldSplit(PC pc,PetscInt delay)
2673: {
2674:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;

2677:   jac->gkbdelay = delay;
2678:   return(0);
2679: }

2681: /*@
2682:     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.

2684:     Collective on PC

2686:     Notes:
2687:     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,
2688:     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
2689:     necessary to find a good balance in between the convergence of the inner and outer loop.

2691:     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.

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

2695:     Input Parameters:
2696: +   pc     - the preconditioner context
2697: -   nu     - the shift parameter

2699:     Options Database:
2700: .     -pc_fieldsplit_gkb_nu - default is 1

2702:     Level: intermediate

2704: .seealso: PCFIELDSPLIT, PCFieldSplitSetGKBDelay(), PCFieldSplitSetGKBTol(), PCFieldSplitSetGKBMaxit()
2705: @*/
2706: PetscErrorCode PCFieldSplitSetGKBNu(PC pc,PetscReal nu)
2707: {

2713:   PetscTryMethod(pc,"PCFieldSplitSetGKBNu_C",(PC,PetscReal),(pc,nu));
2714:   return(0);
2715: }

2717: static PetscErrorCode PCFieldSplitSetGKBNu_FieldSplit(PC pc,PetscReal nu)
2718: {
2719:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;

2722:   jac->gkbnu = nu;
2723:   return(0);
2724: }


2727: static PetscErrorCode  PCFieldSplitSetType_FieldSplit(PC pc,PCCompositeType type)
2728: {
2729:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;

2733:   jac->type = type;

2735:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",0);
2736:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurPre_C",0);
2737:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSchurPre_C",0);
2738:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C",0);
2739:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurScale_C",0);
2740:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetGKBTol_C",0);
2741:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetGKBMaxit_C",0);
2742:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetGKBNu_C",0);
2743:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetGKBDelay_C",0);

2745:   if (type == PC_COMPOSITE_SCHUR) {
2746:     pc->ops->apply = PCApply_FieldSplit_Schur;
2747:     pc->ops->view  = PCView_FieldSplit_Schur;

2749:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit_Schur);
2750:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurPre_C",PCFieldSplitSetSchurPre_FieldSplit);
2751:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSchurPre_C",PCFieldSplitGetSchurPre_FieldSplit);
2752:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C",PCFieldSplitSetSchurFactType_FieldSplit);
2753:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurScale_C",PCFieldSplitSetSchurScale_FieldSplit);
2754:   } else if (type == PC_COMPOSITE_GKB){
2755:     pc->ops->apply = PCApply_FieldSplit_GKB;
2756:     pc->ops->view  = PCView_FieldSplit_GKB;

2758:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit);
2759:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetGKBTol_C",PCFieldSplitSetGKBTol_FieldSplit);
2760:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetGKBMaxit_C",PCFieldSplitSetGKBMaxit_FieldSplit);
2761:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetGKBNu_C",PCFieldSplitSetGKBNu_FieldSplit);
2762:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetGKBDelay_C",PCFieldSplitSetGKBDelay_FieldSplit);
2763:   } else {
2764:     pc->ops->apply = PCApply_FieldSplit;
2765:     pc->ops->view  = PCView_FieldSplit;

2767:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit);
2768:   }
2769:   return(0);
2770: }

2772: static PetscErrorCode  PCFieldSplitSetBlockSize_FieldSplit(PC pc,PetscInt bs)
2773: {
2774:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;

2777:   if (bs < 1) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Blocksize must be positive, you gave %D",bs);
2778:   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);
2779:   jac->bs = bs;
2780:   return(0);
2781: }

2783: /*@
2784:    PCFieldSplitSetType - Sets the type of fieldsplit preconditioner.

2786:    Collective on PC

2788:    Input Parameter:
2789: .  pc - the preconditioner context
2790: .  type - PC_COMPOSITE_ADDITIVE, PC_COMPOSITE_MULTIPLICATIVE (default), PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE, PC_COMPOSITE_SPECIAL, PC_COMPOSITE_SCHUR

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

2795:    Level: Intermediate

2797: .keywords: PC, set, type, composite preconditioner, additive, multiplicative

2799: .seealso: PCCompositeSetType()

2801: @*/
2802: PetscErrorCode  PCFieldSplitSetType(PC pc,PCCompositeType type)
2803: {

2808:   PetscTryMethod(pc,"PCFieldSplitSetType_C",(PC,PCCompositeType),(pc,type));
2809:   return(0);
2810: }

2812: /*@
2813:   PCFieldSplitGetType - Gets the type of fieldsplit preconditioner.

2815:   Not collective

2817:   Input Parameter:
2818: . pc - the preconditioner context

2820:   Output Parameter:
2821: . type - PC_COMPOSITE_ADDITIVE, PC_COMPOSITE_MULTIPLICATIVE (default), PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE, PC_COMPOSITE_SPECIAL, PC_COMPOSITE_SCHUR

2823:   Level: Intermediate

2825: .keywords: PC, set, type, composite preconditioner, additive, multiplicative
2826: .seealso: PCCompositeSetType()
2827: @*/
2828: PetscErrorCode PCFieldSplitGetType(PC pc, PCCompositeType *type)
2829: {
2830:   PC_FieldSplit *jac = (PC_FieldSplit*) pc->data;

2835:   *type = jac->type;
2836:   return(0);
2837: }

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

2842:    Logically Collective

2844:    Input Parameters:
2845: +  pc   - the preconditioner context
2846: -  flg  - boolean indicating whether to use field splits defined by the DM

2848:    Options Database Key:
2849: .  -pc_fieldsplit_dm_splits

2851:    Level: Intermediate

2853: .keywords: PC, DM, composite preconditioner, additive, multiplicative

2855: .seealso: PCFieldSplitGetDMSplits()

2857: @*/
2858: PetscErrorCode  PCFieldSplitSetDMSplits(PC pc,PetscBool flg)
2859: {
2860:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
2861:   PetscBool      isfs;

2867:   PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
2868:   if (isfs) {
2869:     jac->dm_splits = flg;
2870:   }
2871:   return(0);
2872: }


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

2878:    Logically Collective

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

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

2886:    Level: Intermediate

2888: .keywords: PC, DM, composite preconditioner, additive, multiplicative

2890: .seealso: PCFieldSplitSetDMSplits()

2892: @*/
2893: PetscErrorCode  PCFieldSplitGetDMSplits(PC pc,PetscBool* flg)
2894: {
2895:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
2896:   PetscBool      isfs;

2902:   PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
2903:   if (isfs) {
2904:     if(flg) *flg = jac->dm_splits;
2905:   }
2906:   return(0);
2907: }

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

2912:    Logically Collective

2914:    Input Parameter:
2915: .  pc   - the preconditioner context

2917:    Output Parameter:
2918: .  flg  - boolean indicating whether to detect fields or not

2920:    Level: Intermediate

2922: .seealso: PCFIELDSPLIT, PCFieldSplitSetDetectSaddlePoint()

2924: @*/
2925: PetscErrorCode PCFieldSplitGetDetectSaddlePoint(PC pc,PetscBool *flg)
2926: {
2927:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;

2930:   *flg = jac->detect;
2931:   return(0);
2932: }

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

2937:    Logically Collective

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

2942:    Input Parameter:
2943: .  pc   - the preconditioner context

2945:    Output Parameter:
2946: .  flg  - boolean indicating whether to detect fields or not

2948:    Options Database Key:
2949: .  -pc_fieldsplit_detect_saddle_point

2951:    Level: Intermediate

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

2955: @*/
2956: PetscErrorCode PCFieldSplitSetDetectSaddlePoint(PC pc,PetscBool flg)
2957: {
2958:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;

2962:   jac->detect = flg;
2963:   if (jac->detect) {
2964:     PCFieldSplitSetType(pc,PC_COMPOSITE_SCHUR);
2965:     PCFieldSplitSetSchurPre(pc,PC_FIELDSPLIT_SCHUR_PRE_SELF,NULL);
2966:   }
2967:   return(0);
2968: }

2970: /* -------------------------------------------------------------------------------------*/
2971: /*MC
2972:    PCFIELDSPLIT - Preconditioner created by combining separate preconditioners for individual
2973:                   fields or groups of fields. See the users manual section "Solving Block Matrices" for more details.

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

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

2981:    Level: intermediate

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

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

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

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

3005: $     For the Schur complement preconditioner if J = ( A00 A01 )
3006: $                                                    ( A10 A11 )
3007: $     the preconditioner using full factorization is
3008: $              ( I   -ksp(A00) A01 ) ( inv(A00)     0  ) (     I          0  )
3009: $              ( 0         I       ) (   0      ksp(S) ) ( -A10 ksp(A00)  I  )
3010:      where the action of inv(A00) is applied using the KSP solver with prefix -fieldsplit_0_.  S is the Schur complement
3011: $              S = A11 - A10 ksp(A00) A01
3012:      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
3013:      in providing the SECOND split or 1 if not give). For PCFieldSplitGetSubKSP() when field number is 0,
3014:      it returns the KSP associated with -fieldsplit_0_ while field number 1 gives -fieldsplit_1_ KSP. By default
3015:      A11 is used to construct a preconditioner for S, use PCFieldSplitSetSchurPre() for all the possible ways to construct the preconditioner for S.

3017:      The factorization type is set using -pc_fieldsplit_schur_fact_type <diag, lower, upper, full>. The full is shown above,
3018:      diag gives
3019: $              ( inv(A00)     0   )
3020: $              (   0      -ksp(S) )
3021:      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
3022:      can be turned off with PCFieldSplitSetSchurScale() or by command line -pc_fieldsplit_schur_scale 1.0. The lower factorization is the inverse of
3023: $              (  A00   0 )
3024: $              (  A10   S )
3025:      where the inverses of A00 and S are applied using KSPs. The upper factorization is the inverse of
3026: $              ( A00 A01 )
3027: $              (  0   S  )
3028:      where again the inverses of A00 and S are applied using KSPs.

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

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

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

3040:    Concepts: physics based preconditioners, block preconditioners

3042:    There is a nice discussion of block preconditioners in

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

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

3051:    The generalized Golub-Kahan bidiagonalization preconditioner (gkb) can be applied to symmetric 2x2 block matrices of the shape
3052: $        ( A00  A01 )
3053: $        ( A01' 0   )
3054:    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'.
3055:    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_.

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

3059: .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC, Block_Preconditioners, PCLSC,
3060:            PCFieldSplitGetSubKSP(), PCFieldSplitSchurGetSubKSP(), PCFieldSplitSetFields(), PCFieldSplitSetType(), PCFieldSplitSetIS(), PCFieldSplitSetSchurPre(),
3061:           MatSchurComplementSetAinvType(), PCFieldSplitSetSchurScale(),
3062:           PCFieldSplitSetDetectSaddlePoint()
3063: M*/

3065: PETSC_EXTERN PetscErrorCode PCCreate_FieldSplit(PC pc)
3066: {
3068:   PC_FieldSplit  *jac;

3071:   PetscNewLog(pc,&jac);

3073:   jac->bs                 = -1;
3074:   jac->nsplits            = 0;
3075:   jac->type               = PC_COMPOSITE_MULTIPLICATIVE;
3076:   jac->schurpre           = PC_FIELDSPLIT_SCHUR_PRE_USER; /* Try user preconditioner first, fall back on diagonal */
3077:   jac->schurfactorization = PC_FIELDSPLIT_SCHUR_FACT_FULL;
3078:   jac->schurscale         = -1.0;
3079:   jac->dm_splits          = PETSC_TRUE;
3080:   jac->detect             = PETSC_FALSE;
3081:   jac->gkbtol             = 1e-5;
3082:   jac->gkbdelay           = 5;
3083:   jac->gkbnu              = 1;
3084:   jac->gkbmaxit           = 100;
3085:   jac->gkbmonitor         = PETSC_FALSE;

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

3089:   pc->ops->apply           = PCApply_FieldSplit;
3090:   pc->ops->applytranspose  = PCApplyTranspose_FieldSplit;
3091:   pc->ops->setup           = PCSetUp_FieldSplit;
3092:   pc->ops->reset           = PCReset_FieldSplit;
3093:   pc->ops->destroy         = PCDestroy_FieldSplit;
3094:   pc->ops->setfromoptions  = PCSetFromOptions_FieldSplit;
3095:   pc->ops->view            = PCView_FieldSplit;
3096:   pc->ops->applyrichardson = 0;

3098:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSchurGetSubKSP_C",PCFieldSplitSchurGetSubKSP_FieldSplit);
3099:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit);
3100:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetFields_C",PCFieldSplitSetFields_FieldSplit);
3101:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetIS_C",PCFieldSplitSetIS_FieldSplit);
3102:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetType_C",PCFieldSplitSetType_FieldSplit);
3103:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetBlockSize_C",PCFieldSplitSetBlockSize_FieldSplit);
3104:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitRestrictIS_C",PCFieldSplitRestrictIS_FieldSplit);
3105:   return(0);
3106: }