Actual source code: fieldsplit.c

petsc-master 2016-12-08
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:   PC_FieldSplitLink         head;
 48:   PetscBool                 isrestrict;             /* indicates PCFieldSplitRestrictIS() has been last called on this object, hack */
 49:   PetscBool                 suboptionsset;          /* Indicates that the KSPSetFromOptions() has been called on the sub-KSPs */
 50:   PetscBool                 dm_splits;              /* Whether to use DMCreateFieldDecomposition() whenever possible */
 51:   PetscBool                 diag_use_amat;          /* Whether to extract diagonal matrix blocks from Amat, rather than Pmat (weaker than -pc_use_amat) */
 52:   PetscBool                 offdiag_use_amat;       /* Whether to extract off-diagonal matrix blocks from Amat, rather than Pmat (weaker than -pc_use_amat) */
 53: } PC_FieldSplit;

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

 61: /* This helper is so that setting a user-provided preconditioning matrix is orthogonal to choosing to use it.  This way the
 62: * application-provided FormJacobian can provide this matrix without interfering with the user's (command-line) choices. */
 63: static Mat FieldSplitSchurPre(PC_FieldSplit *jac)
 64: {
 65:   switch (jac->schurpre) {
 66:   case PC_FIELDSPLIT_SCHUR_PRE_SELF: return jac->schur;
 67:   case PC_FIELDSPLIT_SCHUR_PRE_SELFP: return jac->schurp;
 68:   case PC_FIELDSPLIT_SCHUR_PRE_A11: return jac->pmat[1];
 69:   case PC_FIELDSPLIT_SCHUR_PRE_FULL: /* We calculate this and store it in schur_user */
 70:   case PC_FIELDSPLIT_SCHUR_PRE_USER: /* Use a user-provided matrix if it is given, otherwise diagonal block */
 71:   default:
 72:     return jac->schur_user ? jac->schur_user : jac->pmat[1];
 73:   }
 74: }


 77:  #include <petscdraw.h>
 80: static PetscErrorCode PCView_FieldSplit(PC pc,PetscViewer viewer)
 81: {
 82:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
 83:   PetscErrorCode    ierr;
 84:   PetscBool         iascii,isdraw;
 85:   PetscInt          i,j;
 86:   PC_FieldSplitLink ilink = jac->head;

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

129:  if (isdraw) {
130:     PetscDraw draw;
131:     PetscReal x,y,w,wd;

133:     PetscViewerDrawGetDraw(viewer,0,&draw);
134:     PetscDrawGetCurrentPoint(draw,&x,&y);
135:     w    = 2*PetscMin(1.0 - x,x);
136:     wd   = w/(jac->nsplits + 1);
137:     x    = x - wd*(jac->nsplits-1)/2.0;
138:     for (i=0; i<jac->nsplits; i++) {
139:       PetscDrawPushCurrentPoint(draw,x,y);
140:       KSPView(ilink->ksp,viewer);
141:       PetscDrawPopCurrentPoint(draw);
142:       x    += wd;
143:       ilink = ilink->next;
144:     }
145:   }
146:   return(0);
147: }

151: static PetscErrorCode PCView_FieldSplit_Schur(PC pc,PetscViewer viewer)
152: {
153:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
154:   PetscErrorCode    ierr;
155:   PetscBool         iascii,isdraw;
156:   PetscInt          i,j;
157:   PC_FieldSplitLink ilink = jac->head;

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

237:     PetscViewerDrawGetDraw(viewer,0,&draw);
238:     PetscDrawGetCurrentPoint(draw,&x,&y);
239:     if (jac->kspupper != jac->head->ksp) cnt++;
240:     w  = 2*PetscMin(1.0 - x,x);
241:     wd = w/(cnt + 1);

243:     PetscSNPrintf(str,32,"Schur fact. %s",PCFieldSplitSchurFactTypes[jac->schurfactorization]);
244:     PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_RED,PETSC_DRAW_BLACK,str,NULL,&h);
245:     y   -= h;
246:     if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_USER &&  !jac->schur_user) {
247:       PetscSNPrintf(str,32,"Prec. for Schur from %s",PCFieldSplitSchurPreTypes[PC_FIELDSPLIT_SCHUR_PRE_A11]);
248:     } else {
249:       PetscSNPrintf(str,32,"Prec. for Schur from %s",PCFieldSplitSchurPreTypes[jac->schurpre]);
250:     }
251:     PetscDrawStringBoxed(draw,x+wd*(cnt-1)/2.0,y,PETSC_DRAW_RED,PETSC_DRAW_BLACK,str,NULL,&h);
252:     y   -= h;
253:     x    = x - wd*(cnt-1)/2.0;

255:     PetscDrawPushCurrentPoint(draw,x,y);
256:     KSPView(jac->head->ksp,viewer);
257:     PetscDrawPopCurrentPoint(draw);
258:     if (jac->kspupper != jac->head->ksp) {
259:       x   += wd;
260:       PetscDrawPushCurrentPoint(draw,x,y);
261:       KSPView(jac->kspupper,viewer);
262:       PetscDrawPopCurrentPoint(draw);
263:     }
264:     x   += wd;
265:     PetscDrawPushCurrentPoint(draw,x,y);
266:     KSPView(jac->kspschur,viewer);
267:     PetscDrawPopCurrentPoint(draw);
268:   }
269:   return(0);
270: }

274: /* Precondition: jac->bs is set to a meaningful value */
275: static PetscErrorCode PCFieldSplitSetRuntimeSplits_Private(PC pc)
276: {
278:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
279:   PetscInt       i,nfields,*ifields,nfields_col,*ifields_col;
280:   PetscBool      flg,flg_col;
281:   char           optionname[128],splitname[8],optionname_col[128];

284:   PetscMalloc1(jac->bs,&ifields);
285:   PetscMalloc1(jac->bs,&ifields_col);
286:   for (i=0,flg=PETSC_TRUE;; i++) {
287:     PetscSNPrintf(splitname,sizeof(splitname),"%D",i);
288:     PetscSNPrintf(optionname,sizeof(optionname),"-pc_fieldsplit_%D_fields",i);
289:     PetscSNPrintf(optionname_col,sizeof(optionname_col),"-pc_fieldsplit_%D_fields_col",i);
290:     nfields     = jac->bs;
291:     nfields_col = jac->bs;
292:     PetscOptionsGetIntArray(((PetscObject)pc)->options,((PetscObject)pc)->prefix,optionname,ifields,&nfields,&flg);
293:     PetscOptionsGetIntArray(((PetscObject)pc)->options,((PetscObject)pc)->prefix,optionname_col,ifields_col,&nfields_col,&flg_col);
294:     if (!flg) break;
295:     else if (flg && !flg_col) {
296:       if (!nfields) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Cannot list zero fields");
297:       PCFieldSplitSetFields(pc,splitname,nfields,ifields,ifields);
298:     } else {
299:       if (!nfields || !nfields_col) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Cannot list zero fields");
300:       if (nfields != nfields_col) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Number of row and column fields must match");
301:       PCFieldSplitSetFields(pc,splitname,nfields,ifields,ifields_col);
302:     }
303:   }
304:   if (i > 0) {
305:     /* Makes command-line setting of splits take precedence over setting them in code.
306:        Otherwise subsequent calls to PCFieldSplitSetIS() or PCFieldSplitSetFields() would
307:        create new splits, which would probably not be what the user wanted. */
308:     jac->splitdefined = PETSC_TRUE;
309:   }
310:   PetscFree(ifields);
311:   PetscFree(ifields_col);
312:   return(0);
313: }

317: static PetscErrorCode PCFieldSplitSetDefaults(PC pc)
318: {
319:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
320:   PetscErrorCode    ierr;
321:   PC_FieldSplitLink ilink = jac->head;
322:   PetscBool         fieldsplit_default = PETSC_FALSE,stokes = PETSC_FALSE,coupling = PETSC_FALSE;
323:   PetscInt          i;

326:   /*
327:    Kinda messy, but at least this now uses DMCreateFieldDecomposition().
328:    Should probably be rewritten.
329:    */
330:   if (!ilink) {
331:     PetscOptionsGetBool(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_fieldsplit_detect_saddle_point",&stokes,NULL);
332:     PetscOptionsGetBool(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_fieldsplit_detect_coupling",&coupling,NULL);
333:     if (pc->dm && jac->dm_splits && !stokes && !coupling) {
334:       PetscInt  numFields, f, i, j;
335:       char      **fieldNames;
336:       IS        *fields;
337:       DM        *dms;
338:       DM        subdm[128];
339:       PetscBool flg;

341:       DMCreateFieldDecomposition(pc->dm, &numFields, &fieldNames, &fields, &dms);
342:       /* Allow the user to prescribe the splits */
343:       for (i = 0, flg = PETSC_TRUE;; i++) {
344:         PetscInt ifields[128];
345:         IS       compField;
346:         char     optionname[128], splitname[8];
347:         PetscInt nfields = numFields;

349:         PetscSNPrintf(optionname, sizeof(optionname), "-pc_fieldsplit_%D_fields", i);
350:         PetscOptionsGetIntArray(((PetscObject)pc)->options,((PetscObject)pc)->prefix, optionname, ifields, &nfields, &flg);
351:         if (!flg) break;
352:         if (numFields > 128) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Cannot currently support %d > 128 fields", numFields);
353:         DMCreateSubDM(pc->dm, nfields, ifields, &compField, &subdm[i]);
354:         if (nfields == 1) {
355:           PCFieldSplitSetIS(pc, fieldNames[ifields[0]], compField);
356:         } else {
357:           PetscSNPrintf(splitname, sizeof(splitname), "%D", i);
358:           PCFieldSplitSetIS(pc, splitname, compField);
359:         }
360:         ISDestroy(&compField);
361:         for (j = 0; j < nfields; ++j) {
362:           f    = ifields[j];
363:           PetscFree(fieldNames[f]);
364:           ISDestroy(&fields[f]);
365:         }
366:       }
367:       if (i == 0) {
368:         for (f = 0; f < numFields; ++f) {
369:           PCFieldSplitSetIS(pc, fieldNames[f], fields[f]);
370:           PetscFree(fieldNames[f]);
371:           ISDestroy(&fields[f]);
372:         }
373:       } else {
374:         for (j=0; j<numFields; j++) {
375:           DMDestroy(dms+j);
376:         }
377:         PetscFree(dms);
378:         PetscMalloc1(i, &dms);
379:         for (j = 0; j < i; ++j) dms[j] = subdm[j];
380:       }
381:       PetscFree(fieldNames);
382:       PetscFree(fields);
383:       if (dms) {
384:         PetscInfo(pc, "Setting up physics based fieldsplit preconditioner using the embedded DM\n");
385:         for (ilink = jac->head, i = 0; ilink; ilink = ilink->next, ++i) {
386:           const char *prefix;
387:           PetscObjectGetOptionsPrefix((PetscObject)(ilink->ksp),&prefix);
388:           PetscObjectSetOptionsPrefix((PetscObject)(dms[i]), prefix);
389:           KSPSetDM(ilink->ksp, dms[i]);
390:           KSPSetDMActive(ilink->ksp, PETSC_FALSE);
391:           PetscObjectIncrementTabLevel((PetscObject)dms[i],(PetscObject)ilink->ksp,0);
392:           DMDestroy(&dms[i]);
393:         }
394:         PetscFree(dms);
395:       }
396:     } else {
397:       if (jac->bs <= 0) {
398:         if (pc->pmat) {
399:           MatGetBlockSize(pc->pmat,&jac->bs);
400:         } else jac->bs = 1;
401:       }

403:       if (stokes) {
404:         IS       zerodiags,rest;
405:         PetscInt nmin,nmax;

407:         MatGetOwnershipRange(pc->mat,&nmin,&nmax);
408:         MatFindZeroDiagonals(pc->mat,&zerodiags);
409:         ISComplement(zerodiags,nmin,nmax,&rest);
410:         PCFieldSplitSetIS(pc,"0",rest);
411:         PCFieldSplitSetIS(pc,"1",zerodiags);
412:         ISDestroy(&zerodiags);
413:         ISDestroy(&rest);
414:       } else if (coupling) {
415:         IS       coupling,rest;
416:         PetscInt nmin,nmax;

418:         MatGetOwnershipRange(pc->mat,&nmin,&nmax);
419:         MatFindOffBlockDiagonalEntries(pc->mat,&coupling);
420:         ISCreateStride(PetscObjectComm((PetscObject)pc->mat),nmax-nmin,nmin,1,&rest);
421:         ISSetIdentity(rest);
422:         PCFieldSplitSetIS(pc,"0",rest);
423:         PCFieldSplitSetIS(pc,"1",coupling);
424:         ISDestroy(&coupling);
425:         ISDestroy(&rest);
426:       } else {
427:         PetscOptionsGetBool(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_fieldsplit_default",&fieldsplit_default,NULL);
428:         if (!fieldsplit_default) {
429:           /* Allow user to set fields from command line,  if bs was known at the time of PCSetFromOptions_FieldSplit()
430:            then it is set there. This is not ideal because we should only have options set in XXSetFromOptions(). */
431:           PCFieldSplitSetRuntimeSplits_Private(pc);
432:           if (jac->splitdefined) {PetscInfo(pc,"Splits defined using the options database\n");}
433:         }
434:         if ((fieldsplit_default || !jac->splitdefined) && !jac->isrestrict) {
435:           PetscInfo(pc,"Using default splitting of fields\n");
436:           for (i=0; i<jac->bs; i++) {
437:             char splitname[8];
438:             PetscSNPrintf(splitname,sizeof(splitname),"%D",i);
439:             PCFieldSplitSetFields(pc,splitname,1,&i,&i);
440:           }
441:           jac->defaultsplit = PETSC_TRUE;
442:         }
443:       }
444:     }
445:   } else if (jac->nsplits == 1) {
446:     if (ilink->is) {
447:       IS       is2;
448:       PetscInt nmin,nmax;

450:       MatGetOwnershipRange(pc->mat,&nmin,&nmax);
451:       ISComplement(ilink->is,nmin,nmax,&is2);
452:       PCFieldSplitSetIS(pc,"1",is2);
453:       ISDestroy(&is2);
454:     } else SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Must provide at least two sets of fields to PCFieldSplit()");
455:   }

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

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

465: static PetscErrorCode PCSetUp_FieldSplit(PC pc)
466: {
467:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
468:   PetscErrorCode    ierr;
469:   PC_FieldSplitLink ilink;
470:   PetscInt          i,nsplit;
471:   PetscBool         sorted, sorted_col;

474:   PCFieldSplitSetDefaults(pc);
475:   nsplit = jac->nsplits;
476:   ilink  = jac->head;

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

482:     jac->issetup = PETSC_TRUE;

484:     /* This is done here instead of in PCFieldSplitSetFields() because may not have matrix at that point */
485:     if (jac->defaultsplit || !ilink->is) {
486:       if (jac->bs <= 0) jac->bs = nsplit;
487:     }
488:     bs     = jac->bs;
489:     MatGetOwnershipRange(pc->pmat,&rstart,&rend);
490:     nslots = (rend - rstart)/bs;
491:     for (i=0; i<nsplit; i++) {
492:       if (jac->defaultsplit) {
493:         ISCreateStride(PetscObjectComm((PetscObject)pc),nslots,rstart+i,nsplit,&ilink->is);
494:         ISDuplicate(ilink->is,&ilink->is_col);
495:       } else if (!ilink->is) {
496:         if (ilink->nfields > 1) {
497:           PetscInt *ii,*jj,j,k,nfields = ilink->nfields,*fields = ilink->fields,*fields_col = ilink->fields_col;
498:           PetscMalloc1(ilink->nfields*nslots,&ii);
499:           PetscMalloc1(ilink->nfields*nslots,&jj);
500:           for (j=0; j<nslots; j++) {
501:             for (k=0; k<nfields; k++) {
502:               ii[nfields*j + k] = rstart + bs*j + fields[k];
503:               jj[nfields*j + k] = rstart + bs*j + fields_col[k];
504:             }
505:           }
506:           ISCreateGeneral(PetscObjectComm((PetscObject)pc),nslots*nfields,ii,PETSC_OWN_POINTER,&ilink->is);
507:           ISCreateGeneral(PetscObjectComm((PetscObject)pc),nslots*nfields,jj,PETSC_OWN_POINTER,&ilink->is_col);
508:           ISSetBlockSize(ilink->is, nfields);
509:           ISSetBlockSize(ilink->is_col, nfields);
510:         } else {
511:           ISCreateStride(PetscObjectComm((PetscObject)pc),nslots,rstart+ilink->fields[0],bs,&ilink->is);
512:           ISCreateStride(PetscObjectComm((PetscObject)pc),nslots,rstart+ilink->fields_col[0],bs,&ilink->is_col);
513:        }
514:       }
515:       ISSorted(ilink->is,&sorted);
516:       if (ilink->is_col) { ISSorted(ilink->is_col,&sorted_col); }
517:       if (!sorted || !sorted_col) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Fields must be sorted when creating split");
518:       ilink = ilink->next;
519:     }
520:   }

522:   ilink = jac->head;
523:   if (!jac->pmat) {
524:     Vec xtmp;

526:     MatCreateVecs(pc->pmat,&xtmp,NULL);
527:     PetscMalloc1(nsplit,&jac->pmat);
528:     PetscMalloc2(nsplit,&jac->x,nsplit,&jac->y);
529:     for (i=0; i<nsplit; i++) {
530:       MatNullSpace sp;

532:       /* Check for preconditioning matrix attached to IS */
533:       PetscObjectQuery((PetscObject) ilink->is, "pmat", (PetscObject*) &jac->pmat[i]);
534:       if (jac->pmat[i]) {
535:         PetscObjectReference((PetscObject) jac->pmat[i]);
536:         if (jac->type == PC_COMPOSITE_SCHUR) {
537:           jac->schur_user = jac->pmat[i];

539:           PetscObjectReference((PetscObject) jac->schur_user);
540:         }
541:       } else {
542:         const char *prefix;
543:         MatGetSubMatrix(pc->pmat,ilink->is,ilink->is_col,MAT_INITIAL_MATRIX,&jac->pmat[i]);
544:         KSPGetOptionsPrefix(ilink->ksp,&prefix);
545:         MatSetOptionsPrefix(jac->pmat[i],prefix);
546:         MatViewFromOptions(jac->pmat[i],NULL,"-mat_view");
547:       }
548:       /* create work vectors for each split */
549:       MatCreateVecs(jac->pmat[i],&jac->x[i],&jac->y[i]);
550:       ilink->x = jac->x[i]; ilink->y = jac->y[i]; ilink->z = NULL;
551:       /* compute scatter contexts needed by multiplicative versions and non-default splits */
552:       VecScatterCreate(xtmp,ilink->is,jac->x[i],NULL,&ilink->sctx);
553:       PetscObjectQuery((PetscObject) ilink->is, "nearnullspace", (PetscObject*) &sp);
554:       if (sp) {
555:         MatSetNearNullSpace(jac->pmat[i], sp);
556:       }
557:       ilink = ilink->next;
558:     }
559:     VecDestroy(&xtmp);
560:   } else {
561:     for (i=0; i<nsplit; i++) {
562:       Mat pmat;

564:       /* Check for preconditioning matrix attached to IS */
565:       PetscObjectQuery((PetscObject) ilink->is, "pmat", (PetscObject*) &pmat);
566:       if (!pmat) {
567:         MatGetSubMatrix(pc->pmat,ilink->is,ilink->is_col,MAT_REUSE_MATRIX,&jac->pmat[i]);
568:       }
569:       ilink = ilink->next;
570:     }
571:   }
572:   if (jac->diag_use_amat) {
573:     ilink = jac->head;
574:     if (!jac->mat) {
575:       PetscMalloc1(nsplit,&jac->mat);
576:       for (i=0; i<nsplit; i++) {
577:         MatGetSubMatrix(pc->mat,ilink->is,ilink->is_col,MAT_INITIAL_MATRIX,&jac->mat[i]);
578:         ilink = ilink->next;
579:       }
580:     } else {
581:       for (i=0; i<nsplit; i++) {
582:         if (jac->mat[i]) {MatGetSubMatrix(pc->mat,ilink->is,ilink->is_col,MAT_REUSE_MATRIX,&jac->mat[i]);}
583:         ilink = ilink->next;
584:       }
585:     }
586:   } else {
587:     jac->mat = jac->pmat;
588:   }

590:   /* Check for null space attached to IS */
591:   ilink = jac->head;
592:   for (i=0; i<nsplit; i++) {
593:     MatNullSpace sp;

595:     PetscObjectQuery((PetscObject) ilink->is, "nullspace", (PetscObject*) &sp);
596:     if (sp) {
597:       MatSetNullSpace(jac->mat[i], sp);
598:     }
599:     ilink = ilink->next;
600:   }

602:   if (jac->type != PC_COMPOSITE_ADDITIVE  && jac->type != PC_COMPOSITE_SCHUR) {
603:     /* extract the rows of the matrix associated with each field: used for efficient computation of residual inside algorithm */
604:     /* FIXME: Can/should we reuse jac->mat whenever (jac->diag_use_amat) is true? */
605:     ilink = jac->head;
606:     if (nsplit == 2 && jac->type == PC_COMPOSITE_MULTIPLICATIVE) {
607:       /* 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 */
608:       if (!jac->Afield) {
609:         PetscCalloc1(nsplit,&jac->Afield);
610:         if (jac->offdiag_use_amat) {
611:           MatGetSubMatrix(pc->mat,ilink->next->is,ilink->is,MAT_INITIAL_MATRIX,&jac->Afield[1]);
612:         } else {
613:           MatGetSubMatrix(pc->pmat,ilink->next->is,ilink->is,MAT_INITIAL_MATRIX,&jac->Afield[1]);
614:         }
615:       } else {
616:         if (jac->offdiag_use_amat) {
617:           MatGetSubMatrix(pc->mat,ilink->next->is,ilink->is,MAT_REUSE_MATRIX,&jac->Afield[1]);
618:         } else {
619:           MatGetSubMatrix(pc->pmat,ilink->next->is,ilink->is,MAT_REUSE_MATRIX,&jac->Afield[1]);
620:         }
621:       }
622:     } else {
623:       if (!jac->Afield) {
624:         PetscMalloc1(nsplit,&jac->Afield);
625:         for (i=0; i<nsplit; i++) {
626:           if (jac->offdiag_use_amat) {
627:             MatGetSubMatrix(pc->mat,ilink->is,NULL,MAT_INITIAL_MATRIX,&jac->Afield[i]);
628:           } else {
629:             MatGetSubMatrix(pc->pmat,ilink->is,NULL,MAT_INITIAL_MATRIX,&jac->Afield[i]);
630:           }
631:           ilink = ilink->next;
632:         }
633:       } else {
634:         for (i=0; i<nsplit; i++) {
635:           if (jac->offdiag_use_amat) {
636:             MatGetSubMatrix(pc->mat,ilink->is,NULL,MAT_REUSE_MATRIX,&jac->Afield[i]);
637:           } else {
638:             MatGetSubMatrix(pc->pmat,ilink->is,NULL,MAT_REUSE_MATRIX,&jac->Afield[i]);
639:           }
640:           ilink = ilink->next;
641:         }
642:       }
643:     }
644:   }

646:   if (jac->type == PC_COMPOSITE_SCHUR) {
647:     IS          ccis;
648:     PetscInt    rstart,rend;
649:     char        lscname[256];
650:     PetscObject LSC_L;

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

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

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

661:       MatSchurComplementGetKSP(jac->schur, &kspInner);
662:       ilink = jac->head;
663:       ISComplement(ilink->is_col,rstart,rend,&ccis);
664:       if (jac->offdiag_use_amat) {
665:         MatGetSubMatrix(pc->mat,ilink->is,ccis,MAT_REUSE_MATRIX,&jac->B);
666:       } else {
667:         MatGetSubMatrix(pc->pmat,ilink->is,ccis,MAT_REUSE_MATRIX,&jac->B);
668:       }
669:       ISDestroy(&ccis);
670:       ilink = ilink->next;
671:       ISComplement(ilink->is_col,rstart,rend,&ccis);
672:       if (jac->offdiag_use_amat) {
673:         MatGetSubMatrix(pc->mat,ilink->is,ccis,MAT_REUSE_MATRIX,&jac->C);
674:       } else {
675:         MatGetSubMatrix(pc->pmat,ilink->is,ccis,MAT_REUSE_MATRIX,&jac->C);
676:       }
677:       ISDestroy(&ccis);
678:       MatSchurComplementUpdateSubMatrices(jac->schur,jac->mat[0],jac->pmat[0],jac->B,jac->C,jac->mat[1]);
679:       if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELFP) {
680:         MatDestroy(&jac->schurp);
681:         MatSchurComplementGetPmat(jac->schur,MAT_INITIAL_MATRIX,&jac->schurp);
682:       }
683:       if (kspA != kspInner) {
684:         KSPSetOperators(kspA,jac->mat[0],jac->pmat[0]);
685:       }
686:       if (kspUpper != kspA) {
687:         KSPSetOperators(kspUpper,jac->mat[0],jac->pmat[0]);
688:       }
689:       KSPSetOperators(jac->kspschur,jac->schur,FieldSplitSchurPre(jac));
690:     } else {
691:       const char   *Dprefix;
692:       char         schurprefix[256], schurmatprefix[256];
693:       char         schurtestoption[256];
694:       MatNullSpace sp;
695:       PetscBool    flg;

697:       /* extract the A01 and A10 matrices */
698:       ilink = jac->head;
699:       ISComplement(ilink->is_col,rstart,rend,&ccis);
700:       if (jac->offdiag_use_amat) {
701:         MatGetSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->B);
702:       } else {
703:         MatGetSubMatrix(pc->pmat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->B);
704:       }
705:       ISDestroy(&ccis);
706:       ilink = ilink->next;
707:       ISComplement(ilink->is_col,rstart,rend,&ccis);
708:       if (jac->offdiag_use_amat) {
709:         MatGetSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->C);
710:       } else {
711:         MatGetSubMatrix(pc->pmat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->C);
712:       }
713:       ISDestroy(&ccis);

715:       /* Use mat[0] (diagonal block of Amat) preconditioned by pmat[0] to define Schur complement */
716:       MatCreate(((PetscObject)jac->mat[0])->comm,&jac->schur);
717:       MatSetType(jac->schur,MATSCHURCOMPLEMENT);
718:       MatSchurComplementSetSubMatrices(jac->schur,jac->mat[0],jac->pmat[0],jac->B,jac->C,jac->mat[1]);
719:       PetscSNPrintf(schurmatprefix, sizeof(schurmatprefix), "%sfieldsplit_%s_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname);
720:       /* Note that the inner KSP is NOT going to inherit this prefix, and if it did, it would be reset just below.  Is that what we want? */
721:       MatSetOptionsPrefix(jac->schur,schurmatprefix);
722:       MatSetFromOptions(jac->schur);
723:       MatGetNullSpace(jac->mat[1], &sp);
724:       if (sp) {
725:         MatSetNullSpace(jac->schur, sp);
726:       }

728:       PetscSNPrintf(schurtestoption, sizeof(schurtestoption), "-fieldsplit_%s_inner_", ilink->splitname);
729:       PetscOptionsFindPairPrefix_Private(((PetscObject)pc)->options,((PetscObject)pc)->prefix, schurtestoption, NULL, &flg);
730:       if (flg) {
731:         DM  dmInner;
732:         KSP kspInner;

734:         MatSchurComplementGetKSP(jac->schur, &kspInner);
735:         PetscSNPrintf(schurprefix, sizeof(schurprefix), "%sfieldsplit_%s_inner_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname);
736:         /* Indent this deeper to emphasize the "inner" nature of this solver. */
737:         PetscObjectIncrementTabLevel((PetscObject)kspInner, (PetscObject) pc, 2);
738:         KSPSetOptionsPrefix(kspInner, schurprefix);

740:         /* Set DM for new solver */
741:         KSPGetDM(jac->head->ksp, &dmInner);
742:         KSPSetDM(kspInner, dmInner);
743:         KSPSetDMActive(kspInner, PETSC_FALSE);
744:       } else {
745:          /* Use the outer solver for the inner solve, but revert the KSPPREONLY from PCFieldSplitSetFields_FieldSplit or
746:           * PCFieldSplitSetIS_FieldSplit. We don't want KSPPREONLY because it makes the Schur complement inexact,
747:           * preventing Schur complement reduction to be an accurate solve. Usually when an iterative solver is used for
748:           * S = D - C A_inner^{-1} B, we expect S to be defined using an accurate definition of A_inner^{-1}, so we make
749:           * GMRES the default. Note that it is also common to use PREONLY for S, in which case S may not be used
750:           * directly, and the user is responsible for setting an inexact method for fieldsplit's A^{-1}. */
751:         KSPSetType(jac->head->ksp,KSPGMRES);
752:         MatSchurComplementSetKSP(jac->schur,jac->head->ksp);
753:       }
754:       KSPSetOperators(jac->head->ksp,jac->mat[0],jac->pmat[0]);
755:       KSPSetFromOptions(jac->head->ksp);
756:       MatSetFromOptions(jac->schur);

758:       PetscSNPrintf(schurtestoption, sizeof(schurtestoption), "-fieldsplit_%s_upper_", ilink->splitname);
759:       PetscOptionsFindPairPrefix_Private(((PetscObject)pc)->options,((PetscObject)pc)->prefix, schurtestoption, NULL, &flg);
760:       if (flg) {
761:         DM dmInner;

763:         PetscSNPrintf(schurprefix, sizeof(schurprefix), "%sfieldsplit_%s_upper_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname);
764:         KSPCreate(PetscObjectComm((PetscObject)pc), &jac->kspupper);
765:         KSPSetErrorIfNotConverged(jac->kspupper,pc->erroriffailure);
766:         KSPSetOptionsPrefix(jac->kspupper, schurprefix);
767:         KSPGetDM(jac->head->ksp, &dmInner);
768:         KSPSetDM(jac->kspupper, dmInner);
769:         KSPSetDMActive(jac->kspupper, PETSC_FALSE);
770:         KSPSetFromOptions(jac->kspupper);
771:         KSPSetOperators(jac->kspupper,jac->mat[0],jac->pmat[0]);
772:         VecDuplicate(jac->head->x, &jac->head->z);
773:       } else {
774:         jac->kspupper = jac->head->ksp;
775:         PetscObjectReference((PetscObject) jac->head->ksp);
776:       }

778:       if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELFP) {
779:         MatSchurComplementGetPmat(jac->schur,MAT_INITIAL_MATRIX,&jac->schurp);
780:       }
781:       KSPCreate(PetscObjectComm((PetscObject)pc),&jac->kspschur);
782:       KSPSetErrorIfNotConverged(jac->kspschur,pc->erroriffailure);
783:       PetscLogObjectParent((PetscObject)pc,(PetscObject)jac->kspschur);
784:       PetscObjectIncrementTabLevel((PetscObject)jac->kspschur,(PetscObject)pc,1);
785:       if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELF) {
786:         PC pcschur;
787:         KSPGetPC(jac->kspschur,&pcschur);
788:         PCSetType(pcschur,PCNONE);
789:         /* Note: This is bad if there exist preconditioners for MATSCHURCOMPLEMENT */
790:       } else if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_FULL) {
791:         MatSchurComplementComputeExplicitOperator(jac->schur, &jac->schur_user);
792:       }
793:       KSPSetOperators(jac->kspschur,jac->schur,FieldSplitSchurPre(jac));
794:       KSPGetOptionsPrefix(jac->head->next->ksp, &Dprefix);
795:       KSPSetOptionsPrefix(jac->kspschur,         Dprefix);
796:       /* propogate DM */
797:       {
798:         DM sdm;
799:         KSPGetDM(jac->head->next->ksp, &sdm);
800:         if (sdm) {
801:           KSPSetDM(jac->kspschur, sdm);
802:           KSPSetDMActive(jac->kspschur, PETSC_FALSE);
803:         }
804:       }
805:       /* really want setfromoptions called in PCSetFromOptions_FieldSplit(), but it is not ready yet */
806:       /* need to call this every time, since the jac->kspschur is freshly created, otherwise its options never get set */
807:       KSPSetFromOptions(jac->kspschur);
808:     }

810:     /* HACK: special support to forward L and Lp matrices that might be used by PCLSC */
811:     PetscSNPrintf(lscname,sizeof(lscname),"%s_LSC_L",ilink->splitname);
812:     PetscObjectQuery((PetscObject)pc->mat,lscname,(PetscObject*)&LSC_L);
813:     if (!LSC_L) {PetscObjectQuery((PetscObject)pc->pmat,lscname,(PetscObject*)&LSC_L);}
814:     if (LSC_L) {PetscObjectCompose((PetscObject)jac->schur,"LSC_L",(PetscObject)LSC_L);}
815:     PetscSNPrintf(lscname,sizeof(lscname),"%s_LSC_Lp",ilink->splitname);
816:     PetscObjectQuery((PetscObject)pc->pmat,lscname,(PetscObject*)&LSC_L);
817:     if (!LSC_L) {PetscObjectQuery((PetscObject)pc->mat,lscname,(PetscObject*)&LSC_L);}
818:     if (LSC_L) {PetscObjectCompose((PetscObject)jac->schur,"LSC_Lp",(PetscObject)LSC_L);}
819:   } else {
820:     /* set up the individual splits' PCs */
821:     i     = 0;
822:     ilink = jac->head;
823:     while (ilink) {
824:       KSPSetOperators(ilink->ksp,jac->mat[i],jac->pmat[i]);
825:       /* really want setfromoptions called in PCSetFromOptions_FieldSplit(), but it is not ready yet */
826:       if (!jac->suboptionsset) {KSPSetFromOptions(ilink->ksp);}
827:       i++;
828:       ilink = ilink->next;
829:     }
830:   }

832:   jac->suboptionsset = PETSC_TRUE;
833:   return(0);
834: }

836: #define FieldSplitSplitSolveAdd(ilink,xx,yy) \
837:   (VecScatterBegin(ilink->sctx,xx,ilink->x,INSERT_VALUES,SCATTER_FORWARD) || \
838:    VecScatterEnd(ilink->sctx,xx,ilink->x,INSERT_VALUES,SCATTER_FORWARD) || \
839:    PetscLogEventBegin(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL) ||\
840:    KSPSolve(ilink->ksp,ilink->x,ilink->y) ||                               \
841:    PetscLogEventEnd(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL) ||\
842:    VecScatterBegin(ilink->sctx,ilink->y,yy,ADD_VALUES,SCATTER_REVERSE) ||  \
843:    VecScatterEnd(ilink->sctx,ilink->y,yy,ADD_VALUES,SCATTER_REVERSE))

847: static PetscErrorCode PCApply_FieldSplit_Schur(PC pc,Vec x,Vec y)
848: {
849:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
850:   PetscErrorCode    ierr;
851:   PC_FieldSplitLink ilinkA = jac->head, ilinkD = ilinkA->next;
852:   KSP               kspA   = ilinkA->ksp, kspLower = kspA, kspUpper = jac->kspupper;

855:   switch (jac->schurfactorization) {
856:   case PC_FIELDSPLIT_SCHUR_FACT_DIAG:
857:     /* [A00 0; 0 -S], positive definite, suitable for MINRES */
858:     VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
859:     VecScatterBegin(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
860:     VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
861:     PetscLogEventBegin(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
862:     KSPSolve(kspA,ilinkA->x,ilinkA->y);
863:     PetscLogEventEnd(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
864:     VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
865:     VecScatterEnd(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
866:     PetscLogEventBegin(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);
867:     KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);
868:     PetscLogEventEnd(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);
869:     VecScale(ilinkD->y,-1.);
870:     VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
871:     VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
872:     VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
873:     break;
874:   case PC_FIELDSPLIT_SCHUR_FACT_LOWER:
875:     /* [A00 0; A10 S], suitable for left preconditioning */
876:     VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
877:     VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
878:     PetscLogEventBegin(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
879:     KSPSolve(kspA,ilinkA->x,ilinkA->y);
880:     PetscLogEventEnd(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
881:     MatMult(jac->C,ilinkA->y,ilinkD->x);
882:     VecScale(ilinkD->x,-1.);
883:     VecScatterBegin(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);
884:     VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
885:     VecScatterEnd(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);
886:     PetscLogEventBegin(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);
887:     KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);
888:     PetscLogEventEnd(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);
889:     VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
890:     VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
891:     VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
892:     break;
893:   case PC_FIELDSPLIT_SCHUR_FACT_UPPER:
894:     /* [A00 A01; 0 S], suitable for right preconditioning */
895:     VecScatterBegin(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
896:     VecScatterEnd(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
897:     PetscLogEventBegin(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);
898:     KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);
899:     PetscLogEventEnd(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);    MatMult(jac->B,ilinkD->y,ilinkA->x);
900:     VecScale(ilinkA->x,-1.);
901:     VecScatterBegin(ilinkA->sctx,x,ilinkA->x,ADD_VALUES,SCATTER_FORWARD);
902:     VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
903:     VecScatterEnd(ilinkA->sctx,x,ilinkA->x,ADD_VALUES,SCATTER_FORWARD);
904:     PetscLogEventBegin(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
905:     KSPSolve(kspA,ilinkA->x,ilinkA->y);
906:     PetscLogEventEnd(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
907:     VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
908:     VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
909:     VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
910:     break;
911:   case PC_FIELDSPLIT_SCHUR_FACT_FULL:
912:     /* [1 0; A10 A00^{-1} 1] [A00 0; 0 S] [1 A00^{-1}A01; 0 1], an exact solve if applied exactly, needs one extra solve with A */
913:     VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
914:     VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
915:     PetscLogEventBegin(KSP_Solve_FS_L,kspLower,ilinkA->x,ilinkA->y,NULL);
916:     KSPSolve(kspLower,ilinkA->x,ilinkA->y);
917:     PetscLogEventEnd(KSP_Solve_FS_L,kspLower,ilinkA->x,ilinkA->y,NULL);
918:     MatMult(jac->C,ilinkA->y,ilinkD->x);
919:     VecScale(ilinkD->x,-1.0);
920:     VecScatterBegin(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);
921:     VecScatterEnd(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);

923:     PetscLogEventBegin(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);
924:     KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);
925:     PetscLogEventEnd(KSP_Solve_FS_S,jac->kspschur,ilinkD->x,ilinkD->y,NULL);
926:     VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
927:     VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);

929:     if (kspUpper == kspA) {
930:       MatMult(jac->B,ilinkD->y,ilinkA->y);
931:       VecAXPY(ilinkA->x,-1.0,ilinkA->y);
932:       PetscLogEventBegin(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
933:       KSPSolve(kspA,ilinkA->x,ilinkA->y);
934:       PetscLogEventEnd(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
935:     } else {
936:       PetscLogEventBegin(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
937:       KSPSolve(kspA,ilinkA->x,ilinkA->y);
938:       PetscLogEventEnd(ilinkA->event,kspA,ilinkA->x,ilinkA->y,NULL);
939:       MatMult(jac->B,ilinkD->y,ilinkA->x);
940:       PetscLogEventBegin(KSP_Solve_FS_U,kspUpper,ilinkA->x,ilinkA->z,NULL);
941:       KSPSolve(kspUpper,ilinkA->x,ilinkA->z);
942:       PetscLogEventEnd(KSP_Solve_FS_U,kspUpper,ilinkA->x,ilinkA->z,NULL);
943:       VecAXPY(ilinkA->y,-1.0,ilinkA->z);
944:     }
945:     VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
946:     VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
947:   }
948:   return(0);
949: }

953: static PetscErrorCode PCApply_FieldSplit(PC pc,Vec x,Vec y)
954: {
955:   PC_FieldSplit      *jac = (PC_FieldSplit*)pc->data;
956:   PetscErrorCode     ierr;
957:   PC_FieldSplitLink  ilink = jac->head;
958:   PetscInt           cnt,bs;
959:   KSPConvergedReason reason;

962:   if (jac->type == PC_COMPOSITE_ADDITIVE) {
963:     if (jac->defaultsplit) {
964:       VecGetBlockSize(x,&bs);
965:       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);
966:       VecGetBlockSize(y,&bs);
967:       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);
968:       VecStrideGatherAll(x,jac->x,INSERT_VALUES);
969:       while (ilink) {
970:         PetscLogEventBegin(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
971:         KSPSolve(ilink->ksp,ilink->x,ilink->y);
972:         PetscLogEventEnd(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
973:         KSPGetConvergedReason(ilink->ksp,&reason);
974:         if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
975:           pc->failedreason = PC_SUBPC_ERROR;
976:         }
977:         ilink = ilink->next;
978:       }
979:       VecStrideScatterAll(jac->y,y,INSERT_VALUES);
980:     } else {
981:       VecSet(y,0.0);
982:       while (ilink) {
983:         FieldSplitSplitSolveAdd(ilink,x,y);
984:         KSPGetConvergedReason(ilink->ksp,&reason);
985:         if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
986:           pc->failedreason = PC_SUBPC_ERROR;
987:         }
988:         ilink = ilink->next;
989:       }
990:     }
991:   } else if (jac->type == PC_COMPOSITE_MULTIPLICATIVE && jac->nsplits == 2) {
992:     VecSet(y,0.0);
993:     /* solve on first block for first block variables */
994:     VecScatterBegin(ilink->sctx,x,ilink->x,INSERT_VALUES,SCATTER_FORWARD);
995:     VecScatterEnd(ilink->sctx,x,ilink->x,INSERT_VALUES,SCATTER_FORWARD);
996:     PetscLogEventBegin(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
997:     KSPSolve(ilink->ksp,ilink->x,ilink->y);
998:     PetscLogEventEnd(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
999:     KSPGetConvergedReason(ilink->ksp,&reason);
1000:     if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1001:       pc->failedreason = PC_SUBPC_ERROR;
1002:     }
1003:     VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
1004:     VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);

1006:     /* compute the residual only onto second block variables using first block variables */
1007:     MatMult(jac->Afield[1],ilink->y,ilink->next->x);
1008:     ilink = ilink->next;
1009:     VecScale(ilink->x,-1.0);
1010:     VecScatterBegin(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
1011:     VecScatterEnd(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);

1013:     /* solve on second block variables */
1014:     PetscLogEventBegin(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1015:     KSPSolve(ilink->ksp,ilink->x,ilink->y);
1016:     PetscLogEventEnd(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1017:     KSPGetConvergedReason(ilink->ksp,&reason);
1018:     if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1019:       pc->failedreason = PC_SUBPC_ERROR;
1020:     }
1021:     VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
1022:     VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
1023:   } else if (jac->type == PC_COMPOSITE_MULTIPLICATIVE || jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
1024:     if (!jac->w1) {
1025:       VecDuplicate(x,&jac->w1);
1026:       VecDuplicate(x,&jac->w2);
1027:     }
1028:     VecSet(y,0.0);
1029:     FieldSplitSplitSolveAdd(ilink,x,y);
1030:     KSPGetConvergedReason(ilink->ksp,&reason);
1031:     if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1032:       pc->failedreason = PC_SUBPC_ERROR;
1033:     }
1034:     cnt  = 1;
1035:     while (ilink->next) {
1036:       ilink = ilink->next;
1037:       /* compute the residual only over the part of the vector needed */
1038:       MatMult(jac->Afield[cnt++],y,ilink->x);
1039:       VecScale(ilink->x,-1.0);
1040:       VecScatterBegin(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
1041:       VecScatterEnd(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
1042:       PetscLogEventBegin(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1043:       KSPSolve(ilink->ksp,ilink->x,ilink->y);
1044:       PetscLogEventEnd(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1045:       KSPGetConvergedReason(ilink->ksp,&reason);
1046:       if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1047:         pc->failedreason = PC_SUBPC_ERROR;
1048:       }
1049:       VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
1050:       VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
1051:     }
1052:     if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
1053:       cnt -= 2;
1054:       while (ilink->previous) {
1055:         ilink = ilink->previous;
1056:         /* compute the residual only over the part of the vector needed */
1057:         MatMult(jac->Afield[cnt--],y,ilink->x);
1058:         VecScale(ilink->x,-1.0);
1059:         VecScatterBegin(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
1060:         VecScatterEnd(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
1061:         PetscLogEventBegin(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1062:         KSPSolve(ilink->ksp,ilink->x,ilink->y);
1063:         PetscLogEventEnd(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1064:         KSPGetConvergedReason(ilink->ksp,&reason);
1065:         if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1066:           pc->failedreason = PC_SUBPC_ERROR;
1067:         }
1068:         VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
1069:         VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
1070:       }
1071:     }
1072:   } else SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Unsupported or unknown composition",(int) jac->type);
1073:   return(0);
1074: }

1076: #define FieldSplitSplitSolveAddTranspose(ilink,xx,yy) \
1077:   (VecScatterBegin(ilink->sctx,xx,ilink->y,INSERT_VALUES,SCATTER_FORWARD) || \
1078:    VecScatterEnd(ilink->sctx,xx,ilink->y,INSERT_VALUES,SCATTER_FORWARD) || \
1079:    PetscLogEventBegin(ilink->event,ilink->ksp,ilink->y,ilink->x,NULL) || \
1080:    KSPSolveTranspose(ilink->ksp,ilink->y,ilink->x) ||                  \
1081:    PetscLogEventBegin(ilink->event,ilink->ksp,ilink->y,ilink->x,NULL) || \
1082:    VecScatterBegin(ilink->sctx,ilink->x,yy,ADD_VALUES,SCATTER_REVERSE) || \
1083:    VecScatterEnd(ilink->sctx,ilink->x,yy,ADD_VALUES,SCATTER_REVERSE))

1087: static PetscErrorCode PCApplyTranspose_FieldSplit(PC pc,Vec x,Vec y)
1088: {
1089:   PC_FieldSplit      *jac = (PC_FieldSplit*)pc->data;
1090:   PetscErrorCode     ierr;
1091:   PC_FieldSplitLink  ilink = jac->head;
1092:   PetscInt           bs;
1093:   KSPConvergedReason reason;

1096:   if (jac->type == PC_COMPOSITE_ADDITIVE) {
1097:     if (jac->defaultsplit) {
1098:       VecGetBlockSize(x,&bs);
1099:       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);
1100:       VecGetBlockSize(y,&bs);
1101:       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);
1102:       VecStrideGatherAll(x,jac->x,INSERT_VALUES);
1103:       while (ilink) {
1104:         PetscLogEventBegin(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1105:         KSPSolveTranspose(ilink->ksp,ilink->x,ilink->y);
1106:         PetscLogEventEnd(ilink->event,ilink->ksp,ilink->x,ilink->y,NULL);
1107:         KSPGetConvergedReason(ilink->ksp,&reason);
1108:         if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1109:           pc->failedreason = PC_SUBPC_ERROR;
1110:         }
1111:         ilink = ilink->next;
1112:       }
1113:       VecStrideScatterAll(jac->y,y,INSERT_VALUES);
1114:     } else {
1115:       VecSet(y,0.0);
1116:       while (ilink) {
1117:         FieldSplitSplitSolveAddTranspose(ilink,x,y);
1118:         KSPGetConvergedReason(ilink->ksp,&reason);
1119:         if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1120:           pc->failedreason = PC_SUBPC_ERROR;
1121:         }
1122:         ilink = ilink->next;
1123:       }
1124:     }
1125:   } else {
1126:     if (!jac->w1) {
1127:       VecDuplicate(x,&jac->w1);
1128:       VecDuplicate(x,&jac->w2);
1129:     }
1130:     VecSet(y,0.0);
1131:     if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
1132:       FieldSplitSplitSolveAddTranspose(ilink,x,y);
1133:       KSPGetConvergedReason(ilink->ksp,&reason);
1134:       if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1135:         pc->failedreason = PC_SUBPC_ERROR;
1136:       }
1137:       while (ilink->next) {
1138:         ilink = ilink->next;
1139:         MatMultTranspose(pc->mat,y,jac->w1);
1140:         VecWAXPY(jac->w2,-1.0,jac->w1,x);
1141:         FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);
1142:       }
1143:       while (ilink->previous) {
1144:         ilink = ilink->previous;
1145:         MatMultTranspose(pc->mat,y,jac->w1);
1146:         VecWAXPY(jac->w2,-1.0,jac->w1,x);
1147:         FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);
1148:       }
1149:     } else {
1150:       while (ilink->next) {   /* get to last entry in linked list */
1151:         ilink = ilink->next;
1152:       }
1153:       FieldSplitSplitSolveAddTranspose(ilink,x,y);
1154:       KSPGetConvergedReason(ilink->ksp,&reason);
1155:       if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1156:         pc->failedreason = PC_SUBPC_ERROR;
1157:       }
1158:       while (ilink->previous) {
1159:         ilink = ilink->previous;
1160:         MatMultTranspose(pc->mat,y,jac->w1);
1161:         VecWAXPY(jac->w2,-1.0,jac->w1,x);
1162:         FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);
1163:       }
1164:     }
1165:   }
1166:   return(0);
1167: }

1171: static PetscErrorCode PCReset_FieldSplit(PC pc)
1172: {
1173:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1174:   PetscErrorCode    ierr;
1175:   PC_FieldSplitLink ilink = jac->head,next;

1178:   while (ilink) {
1179:     KSPDestroy(&ilink->ksp);
1180:     VecDestroy(&ilink->x);
1181:     VecDestroy(&ilink->y);
1182:     VecDestroy(&ilink->z);
1183:     VecScatterDestroy(&ilink->sctx);
1184:     ISDestroy(&ilink->is);
1185:     ISDestroy(&ilink->is_col);
1186:     PetscFree(ilink->splitname);
1187:     PetscFree(ilink->fields);
1188:     PetscFree(ilink->fields_col);
1189:     next  = ilink->next;
1190:     PetscFree(ilink);
1191:     ilink = next;
1192:   }
1193:   jac->head = NULL;
1194:   PetscFree2(jac->x,jac->y);
1195:   if (jac->mat && jac->mat != jac->pmat) {
1196:     MatDestroyMatrices(jac->nsplits,&jac->mat);
1197:   } else if (jac->mat) {
1198:     jac->mat = NULL;
1199:   }
1200:   if (jac->pmat) {MatDestroyMatrices(jac->nsplits,&jac->pmat);}
1201:   if (jac->Afield) {MatDestroyMatrices(jac->nsplits,&jac->Afield);}
1202:   jac->nsplits = 0;
1203:   VecDestroy(&jac->w1);
1204:   VecDestroy(&jac->w2);
1205:   MatDestroy(&jac->schur);
1206:   MatDestroy(&jac->schurp);
1207:   MatDestroy(&jac->schur_user);
1208:   KSPDestroy(&jac->kspschur);
1209:   KSPDestroy(&jac->kspupper);
1210:   MatDestroy(&jac->B);
1211:   MatDestroy(&jac->C);
1212:   jac->isrestrict = PETSC_FALSE;
1213:   return(0);
1214: }

1218: static PetscErrorCode PCDestroy_FieldSplit(PC pc)
1219: {
1220:   PetscErrorCode    ierr;

1223:   PCReset_FieldSplit(pc);
1224:   PetscFree(pc->data);
1225:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",NULL);
1226:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetFields_C",NULL);
1227:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetIS_C",NULL);
1228:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetType_C",NULL);
1229:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetBlockSize_C",NULL);
1230:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurPre_C",NULL);
1231:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSchurPre_C",NULL);
1232:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C",NULL);
1233:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitRestrictIS_C",NULL);
1234:   return(0);
1235: }

1239: static PetscErrorCode PCSetFromOptions_FieldSplit(PetscOptionItems *PetscOptionsObject,PC pc)
1240: {
1241:   PetscErrorCode  ierr;
1242:   PetscInt        bs;
1243:   PetscBool       flg,stokes = PETSC_FALSE;
1244:   PC_FieldSplit   *jac = (PC_FieldSplit*)pc->data;
1245:   PCCompositeType ctype;

1248:   PetscOptionsHead(PetscOptionsObject,"FieldSplit options");
1249:   PetscOptionsBool("-pc_fieldsplit_dm_splits","Whether to use DMCreateFieldDecomposition() for splits","PCFieldSplitSetDMSplits",jac->dm_splits,&jac->dm_splits,NULL);
1250:   PetscOptionsInt("-pc_fieldsplit_block_size","Blocksize that defines number of fields","PCFieldSplitSetBlockSize",jac->bs,&bs,&flg);
1251:   if (flg) {
1252:     PCFieldSplitSetBlockSize(pc,bs);
1253:   }
1254:   jac->diag_use_amat = pc->useAmat;
1255:   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);
1256:   jac->offdiag_use_amat = pc->useAmat;
1257:   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);
1258:   /* FIXME: No programmatic equivalent to the following. */
1259:   PetscOptionsGetBool(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_fieldsplit_detect_saddle_point",&stokes,NULL);
1260:   if (stokes) {
1261:     PCFieldSplitSetType(pc,PC_COMPOSITE_SCHUR);
1262:     jac->schurpre = PC_FIELDSPLIT_SCHUR_PRE_SELF;
1263:   }

1265:   PetscOptionsEnum("-pc_fieldsplit_type","Type of composition","PCFieldSplitSetType",PCCompositeTypes,(PetscEnum)jac->type,(PetscEnum*)&ctype,&flg);
1266:   if (flg) {
1267:     PCFieldSplitSetType(pc,ctype);
1268:   }
1269:   /* Only setup fields once */
1270:   if ((jac->bs > 0) && (jac->nsplits == 0)) {
1271:     /* only allow user to set fields from command line if bs is already known.
1272:        otherwise user can set them in PCFieldSplitSetDefaults() */
1273:     PCFieldSplitSetRuntimeSplits_Private(pc);
1274:     if (jac->splitdefined) {PetscInfo(pc,"Splits defined using the options database\n");}
1275:   }
1276:   if (jac->type == PC_COMPOSITE_SCHUR) {
1277:     PetscOptionsGetEnum(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_fieldsplit_schur_factorization_type",PCFieldSplitSchurFactTypes,(PetscEnum*)&jac->schurfactorization,&flg);
1278:     if (flg) {PetscInfo(pc,"Deprecated use of -pc_fieldsplit_schur_factorization_type\n");}
1279:     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);
1280:     PetscOptionsEnum("-pc_fieldsplit_schur_precondition","How to build preconditioner for Schur complement","PCFieldSplitSetSchurPre",PCFieldSplitSchurPreTypes,(PetscEnum)jac->schurpre,(PetscEnum*)&jac->schurpre,NULL);
1281:   }
1282:   PetscOptionsTail();
1283:   return(0);
1284: }

1286: /*------------------------------------------------------------------------------------*/

1290: static PetscErrorCode  PCFieldSplitSetFields_FieldSplit(PC pc,const char splitname[],PetscInt n,const PetscInt *fields,const PetscInt *fields_col)
1291: {
1292:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1293:   PetscErrorCode    ierr;
1294:   PC_FieldSplitLink ilink,next = jac->head;
1295:   char              prefix[128];
1296:   PetscInt          i;

1299:   if (jac->splitdefined) {
1300:     PetscInfo1(pc,"Ignoring new split \"%s\" because the splits have already been defined\n",splitname);
1301:     return(0);
1302:   }
1303:   for (i=0; i<n; i++) {
1304:     if (fields[i] >= jac->bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Field %D requested but only %D exist",fields[i],jac->bs);
1305:     if (fields[i] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative field %D requested",fields[i]);
1306:   }
1307:   PetscNew(&ilink);
1308:   if (splitname) {
1309:     PetscStrallocpy(splitname,&ilink->splitname);
1310:   } else {
1311:     PetscMalloc1(3,&ilink->splitname);
1312:     PetscSNPrintf(ilink->splitname,2,"%s",jac->nsplits);
1313:   }
1314:   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 */
1315:   PetscMalloc1(n,&ilink->fields);
1316:   PetscMemcpy(ilink->fields,fields,n*sizeof(PetscInt));
1317:   PetscMalloc1(n,&ilink->fields_col);
1318:   PetscMemcpy(ilink->fields_col,fields_col,n*sizeof(PetscInt));

1320:   ilink->nfields = n;
1321:   ilink->next    = NULL;
1322:   KSPCreate(PetscObjectComm((PetscObject)pc),&ilink->ksp);
1323:   KSPSetErrorIfNotConverged(ilink->ksp,pc->erroriffailure);
1324:   PetscObjectIncrementTabLevel((PetscObject)ilink->ksp,(PetscObject)pc,1);
1325:   KSPSetType(ilink->ksp,KSPPREONLY);
1326:   PetscLogObjectParent((PetscObject)pc,(PetscObject)ilink->ksp);

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

1331:   if (!next) {
1332:     jac->head       = ilink;
1333:     ilink->previous = NULL;
1334:   } else {
1335:     while (next->next) {
1336:       next = next->next;
1337:     }
1338:     next->next      = ilink;
1339:     ilink->previous = next;
1340:   }
1341:   jac->nsplits++;
1342:   return(0);
1343: }

1347: static PetscErrorCode  PCFieldSplitGetSubKSP_FieldSplit_Schur(PC pc,PetscInt *n,KSP **subksp)
1348: {
1349:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;

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

1357:   (*subksp)[1] = jac->kspschur;
1358:   if (n) *n = jac->nsplits;
1359:   return(0);
1360: }

1364: static PetscErrorCode  PCFieldSplitGetSubKSP_FieldSplit(PC pc,PetscInt *n,KSP **subksp)
1365: {
1366:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1367:   PetscErrorCode    ierr;
1368:   PetscInt          cnt   = 0;
1369:   PC_FieldSplitLink ilink = jac->head;

1372:   PetscMalloc1(jac->nsplits,subksp);
1373:   while (ilink) {
1374:     (*subksp)[cnt++] = ilink->ksp;
1375:     ilink            = ilink->next;
1376:   }
1377:   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);
1378:   if (n) *n = jac->nsplits;
1379:   return(0);
1380: }

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

1387:     Input Parameters:
1388: +   pc  - the preconditioner context
1389: +   is - the index set that defines the indices to which the fieldsplit is to be restricted

1391:     Level: advanced

1393: @*/
1394: PetscErrorCode  PCFieldSplitRestrictIS(PC pc,IS isy)
1395: {

1401:   PetscTryMethod(pc,"PCFieldSplitRestrictIS_C",(PC,IS),(pc,isy));
1402:   return(0);
1403: }


1408: static PetscErrorCode  PCFieldSplitRestrictIS_FieldSplit(PC pc, IS isy)
1409: {
1410:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1411:   PetscErrorCode    ierr;
1412:   PC_FieldSplitLink ilink = jac->head, next;
1413:   PetscInt          localsize,size,sizez,i;
1414:   const PetscInt    *ind, *indz;
1415:   PetscInt          *indc, *indcz;
1416:   PetscBool         flg;

1419:   ISGetLocalSize(isy,&localsize);
1420:   MPI_Scan(&localsize,&size,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)isy));
1421:   size -= localsize;
1422:   while(ilink) {
1423:     IS isrl,isr;
1424:     PC subpc;
1425:     ISEmbed(ilink->is, isy, PETSC_TRUE, &isrl);
1426:     ISGetLocalSize(isrl,&localsize);
1427:     PetscMalloc1(localsize,&indc);
1428:     ISGetIndices(isrl,&ind);
1429:     PetscMemcpy(indc,ind,localsize*sizeof(PetscInt));
1430:     ISRestoreIndices(isrl,&ind);
1431:     ISDestroy(&isrl);
1432:     for (i=0; i<localsize; i++) *(indc+i) += size;
1433:     ISCreateGeneral(PetscObjectComm((PetscObject)isy),localsize,indc,PETSC_OWN_POINTER,&isr);
1434:     PetscObjectReference((PetscObject)isr);
1435:     ISDestroy(&ilink->is);
1436:     ilink->is     = isr;
1437:     PetscObjectReference((PetscObject)isr);
1438:     ISDestroy(&ilink->is_col);
1439:     ilink->is_col = isr;
1440:     ISDestroy(&isr);
1441:     KSPGetPC(ilink->ksp, &subpc);
1442:     PetscObjectTypeCompare((PetscObject)subpc,PCFIELDSPLIT,&flg);
1443:     if(flg) {
1444:       IS iszl,isz;
1445:       MPI_Comm comm;
1446:       ISGetLocalSize(ilink->is,&localsize);
1447:       comm   = PetscObjectComm((PetscObject)ilink->is);
1448:       ISEmbed(isy, ilink->is, PETSC_TRUE, &iszl);
1449:       MPI_Scan(&localsize,&sizez,1,MPIU_INT,MPI_SUM,comm);
1450:       sizez -= localsize;
1451:       ISGetLocalSize(iszl,&localsize);
1452:       PetscMalloc1(localsize,&indcz);
1453:       ISGetIndices(iszl,&indz);
1454:       PetscMemcpy(indcz,indz,localsize*sizeof(PetscInt));
1455:       ISRestoreIndices(iszl,&indz);
1456:       ISDestroy(&iszl);
1457:       for (i=0; i<localsize; i++) *(indcz+i) += sizez;
1458:       ISCreateGeneral(comm,localsize,indcz,PETSC_OWN_POINTER,&isz);
1459:       PCFieldSplitRestrictIS(subpc,isz);
1460:       ISDestroy(&isz);
1461:     }
1462:     next = ilink->next;
1463:     ilink = next;
1464:   }
1465:   jac->isrestrict = PETSC_TRUE;
1466:   return(0);
1467: }

1471: static PetscErrorCode  PCFieldSplitSetIS_FieldSplit(PC pc,const char splitname[],IS is)
1472: {
1473:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1474:   PetscErrorCode    ierr;
1475:   PC_FieldSplitLink ilink, next = jac->head;
1476:   char              prefix[128];

1479:   if (jac->splitdefined) {
1480:     PetscInfo1(pc,"Ignoring new split \"%s\" because the splits have already been defined\n",splitname);
1481:     return(0);
1482:   }
1483:   PetscNew(&ilink);
1484:   if (splitname) {
1485:     PetscStrallocpy(splitname,&ilink->splitname);
1486:   } else {
1487:     PetscMalloc1(8,&ilink->splitname);
1488:     PetscSNPrintf(ilink->splitname,7,"%D",jac->nsplits);
1489:   }
1490:   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 */
1491:   PetscObjectReference((PetscObject)is);
1492:   ISDestroy(&ilink->is);
1493:   ilink->is     = is;
1494:   PetscObjectReference((PetscObject)is);
1495:   ISDestroy(&ilink->is_col);
1496:   ilink->is_col = is;
1497:   ilink->next   = NULL;
1498:   KSPCreate(PetscObjectComm((PetscObject)pc),&ilink->ksp);
1499:   KSPSetErrorIfNotConverged(ilink->ksp,pc->erroriffailure);
1500:   PetscObjectIncrementTabLevel((PetscObject)ilink->ksp,(PetscObject)pc,1);
1501:   KSPSetType(ilink->ksp,KSPPREONLY);
1502:   PetscLogObjectParent((PetscObject)pc,(PetscObject)ilink->ksp);

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

1507:   if (!next) {
1508:     jac->head       = ilink;
1509:     ilink->previous = NULL;
1510:   } else {
1511:     while (next->next) {
1512:       next = next->next;
1513:     }
1514:     next->next      = ilink;
1515:     ilink->previous = next;
1516:   }
1517:   jac->nsplits++;
1518:   return(0);
1519: }

1523: /*@
1524:     PCFieldSplitSetFields - Sets the fields for one particular split in the field split preconditioner

1526:     Logically Collective on PC

1528:     Input Parameters:
1529: +   pc  - the preconditioner context
1530: .   splitname - name of this split, if NULL the number of the split is used
1531: .   n - the number of fields in this split
1532: -   fields - the fields in this split

1534:     Level: intermediate

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

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

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

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

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

1552: @*/
1553: PetscErrorCode  PCFieldSplitSetFields(PC pc,const char splitname[],PetscInt n,const PetscInt *fields,const PetscInt *fields_col)
1554: {

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

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

1571:     Logically Collective on PC

1573:     Input Parameters:
1574: +   pc  - the preconditioner object
1575: -   flg - boolean flag indicating whether or not to use Amat to extract the diagonal blocks from

1577:     Options Database:
1578: .     -pc_fieldsplit_diag_use_amat

1580:     Level: intermediate

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

1584: @*/
1585: PetscErrorCode  PCFieldSplitSetDiagUseAmat(PC pc,PetscBool flg)
1586: {
1587:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
1588:   PetscBool      isfs;

1593:   PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
1594:   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"PC not of type %s",PCFIELDSPLIT);
1595:   jac->diag_use_amat = flg;
1596:   return(0);
1597: }

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

1604:     Logically Collective on PC

1606:     Input Parameters:
1607: .   pc  - the preconditioner object

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


1613:     Level: intermediate

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

1617: @*/
1618: PetscErrorCode  PCFieldSplitGetDiagUseAmat(PC pc,PetscBool *flg)
1619: {
1620:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
1621:   PetscBool      isfs;

1627:   PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
1628:   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"PC not of type %s",PCFIELDSPLIT);
1629:   *flg = jac->diag_use_amat;
1630:   return(0);
1631: }

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

1638:     Logically Collective on PC

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

1644:     Options Database:
1645: .     -pc_fieldsplit_off_diag_use_amat

1647:     Level: intermediate

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

1651: @*/
1652: PetscErrorCode  PCFieldSplitSetOffDiagUseAmat(PC pc,PetscBool flg)
1653: {
1654:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
1655:   PetscBool      isfs;

1660:   PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
1661:   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"PC not of type %s",PCFIELDSPLIT);
1662:   jac->offdiag_use_amat = flg;
1663:   return(0);
1664: }

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

1671:     Logically Collective on PC

1673:     Input Parameters:
1674: .   pc  - the preconditioner object

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


1680:     Level: intermediate

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

1684: @*/
1685: PetscErrorCode  PCFieldSplitGetOffDiagUseAmat(PC pc,PetscBool *flg)
1686: {
1687:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
1688:   PetscBool      isfs;

1694:   PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
1695:   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"PC not of type %s",PCFIELDSPLIT);
1696:   *flg = jac->offdiag_use_amat;
1697:   return(0);
1698: }



1704: /*@C
1705:     PCFieldSplitSetIS - Sets the exact elements for field

1707:     Logically Collective on PC

1709:     Input Parameters:
1710: +   pc  - the preconditioner context
1711: .   splitname - name of this split, if NULL the number of the split is used
1712: -   is - the index set that defines the vector elements in this field


1715:     Notes:
1716:     Use PCFieldSplitSetFields(), for fields defined by strided types.

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

1721:     Level: intermediate

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

1725: @*/
1726: PetscErrorCode  PCFieldSplitSetIS(PC pc,const char splitname[],IS is)
1727: {

1734:   PetscTryMethod(pc,"PCFieldSplitSetIS_C",(PC,const char[],IS),(pc,splitname,is));
1735:   return(0);
1736: }

1740: /*@
1741:     PCFieldSplitGetIS - Retrieves the elements for a field as an IS

1743:     Logically Collective on PC

1745:     Input Parameters:
1746: +   pc  - the preconditioner context
1747: -   splitname - name of this split

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

1752:     Level: intermediate

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

1756: @*/
1757: PetscErrorCode PCFieldSplitGetIS(PC pc,const char splitname[],IS *is)
1758: {

1765:   {
1766:     PC_FieldSplit     *jac  = (PC_FieldSplit*) pc->data;
1767:     PC_FieldSplitLink ilink = jac->head;
1768:     PetscBool         found;

1770:     *is = NULL;
1771:     while (ilink) {
1772:       PetscStrcmp(ilink->splitname, splitname, &found);
1773:       if (found) {
1774:         *is = ilink->is;
1775:         break;
1776:       }
1777:       ilink = ilink->next;
1778:     }
1779:   }
1780:   return(0);
1781: }

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

1789:     Logically Collective on PC

1791:     Input Parameters:
1792: +   pc  - the preconditioner context
1793: -   bs - the block size

1795:     Level: intermediate

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

1799: @*/
1800: PetscErrorCode  PCFieldSplitSetBlockSize(PC pc,PetscInt bs)
1801: {

1807:   PetscTryMethod(pc,"PCFieldSplitSetBlockSize_C",(PC,PetscInt),(pc,bs));
1808:   return(0);
1809: }

1813: /*@C
1814:    PCFieldSplitGetSubKSP - Gets the KSP contexts for all splits

1816:    Collective on KSP

1818:    Input Parameter:
1819: .  pc - the preconditioner context

1821:    Output Parameters:
1822: +  n - the number of splits
1823: -  subksp - the array of KSP contexts

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

1829:    You must call KSPSetUp() before calling PCFieldSplitGetSubKSP().

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


1836:    Level: advanced

1838: .seealso: PCFIELDSPLIT
1839: @*/
1840: PetscErrorCode  PCFieldSplitGetSubKSP(PC pc,PetscInt *n,KSP *subksp[])
1841: {

1847:   PetscUseMethod(pc,"PCFieldSplitGetSubKSP_C",(PC,PetscInt*,KSP **),(pc,n,subksp));
1848:   return(0);
1849: }

1853: /*@
1854:     PCFieldSplitSetSchurPre -  Indicates what operator is used to construct the preconditioner for the Schur complement.
1855:       A11 matrix. Otherwise no preconditioner is used.

1857:     Collective on PC

1859:     Input Parameters:
1860: +   pc      - the preconditioner context
1861: .   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 
1862:               PC_FIELDSPLIT_SCHUR_PRE_SELFP, and PC_FIELDSPLIT_SCHUR_PRE_FULL
1863: -   userpre - matrix to use for preconditioning, or NULL

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

1868:     Notes:
1869: $    If ptype is
1870: $        a11 then the preconditioner for the Schur complement is generated from the block diagonal part of the preconditioner
1871: $             matrix associated with the Schur complement (i.e. A11), not the Schur complement matrix
1872: $        self the preconditioner for the Schur complement is generated from the symbolic representation of the Schur complement matrix:
1873: $             The only preconditioner that currently works with this symbolic respresentation matrix object is the PCLSC
1874: $             preconditioner
1875: $        user then the preconditioner for the Schur complement is generated from the user provided matrix (pre argument
1876: $             to this function).
1877: $        selfp then the preconditioning for the Schur complement is generated from an explicitly-assembled approximation Sp = A11 - A10 inv(diag(A00)) A01
1878: $             This is only a good preconditioner when diag(A00) is a good preconditioner for A00. Optionally, A00 can be
1879: $             lumped before extracting the diagonal using the additional option -fieldsplit_1_mat_schur_complement_ainv_type lump
1880: $        full then the preconditioner for the Schur complement is generated from the exact Schur complement matrix representation computed internally by PFIELDSPLIT (this is expensive)
1881: $             useful mostly as a test that the Schur complement approach can work for your problem

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

1887:     Level: intermediate

1889: .seealso: PCFieldSplitGetSchurPre(), PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields(), PCFieldSplitSchurPreType,
1890:           MatSchurComplementSetAinvType(), PCLSC

1892: @*/
1893: PetscErrorCode PCFieldSplitSetSchurPre(PC pc,PCFieldSplitSchurPreType ptype,Mat pre)
1894: {

1899:   PetscTryMethod(pc,"PCFieldSplitSetSchurPre_C",(PC,PCFieldSplitSchurPreType,Mat),(pc,ptype,pre));
1900:   return(0);
1901: }
1902: PetscErrorCode PCFieldSplitSchurPrecondition(PC pc,PCFieldSplitSchurPreType ptype,Mat pre) {return PCFieldSplitSetSchurPre(pc,ptype,pre);} /* Deprecated name */

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

1910:     Logically Collective on PC

1912:     Input Parameters:
1913: .   pc      - the preconditioner context

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

1919:     Level: intermediate

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

1923: @*/
1924: PetscErrorCode PCFieldSplitGetSchurPre(PC pc,PCFieldSplitSchurPreType *ptype,Mat *pre)
1925: {

1930:   PetscUseMethod(pc,"PCFieldSplitGetSchurPre_C",(PC,PCFieldSplitSchurPreType*,Mat*),(pc,ptype,pre));
1931:   return(0);
1932: }

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

1939:     Not collective

1941:     Input Parameter:
1942: .   pc      - the preconditioner context

1944:     Output Parameter:
1945: .   S       - the Schur complement matrix

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

1950:     Level: advanced

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

1954: @*/
1955: PetscErrorCode  PCFieldSplitSchurGetS(PC pc,Mat *S)
1956: {
1958:   const char*    t;
1959:   PetscBool      isfs;
1960:   PC_FieldSplit  *jac;

1964:   PetscObjectGetType((PetscObject)pc,&t);
1965:   PetscStrcmp(t,PCFIELDSPLIT,&isfs);
1966:   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PC of type PCFIELDSPLIT, got %s instead",t);
1967:   jac = (PC_FieldSplit*)pc->data;
1968:   if (jac->type != PC_COMPOSITE_SCHUR) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PCFIELDSPLIT of type SCHUR, got %D instead",jac->type);
1969:   if (S) *S = jac->schur;
1970:   return(0);
1971: }

1975: /*@
1976:     PCFieldSplitSchurRestoreS -  restores the MatSchurComplement object used by this PC

1978:     Not collective

1980:     Input Parameters:
1981: +   pc      - the preconditioner context
1982: .   S       - the Schur complement matrix

1984:     Level: advanced

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

1988: @*/
1989: PetscErrorCode  PCFieldSplitSchurRestoreS(PC pc,Mat *S)
1990: {
1992:   const char*    t;
1993:   PetscBool      isfs;
1994:   PC_FieldSplit  *jac;

1998:   PetscObjectGetType((PetscObject)pc,&t);
1999:   PetscStrcmp(t,PCFIELDSPLIT,&isfs);
2000:   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PC of type PCFIELDSPLIT, got %s instead",t);
2001:   jac = (PC_FieldSplit*)pc->data;
2002:   if (jac->type != PC_COMPOSITE_SCHUR) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PCFIELDSPLIT of type SCHUR, got %D instead",jac->type);
2003:   if (!S || *S != jac->schur) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"MatSchurComplement restored is not the same as gotten");
2004:   return(0);
2005: }


2010: static PetscErrorCode  PCFieldSplitSetSchurPre_FieldSplit(PC pc,PCFieldSplitSchurPreType ptype,Mat pre)
2011: {
2012:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;

2016:   jac->schurpre = ptype;
2017:   if (ptype == PC_FIELDSPLIT_SCHUR_PRE_USER && pre) {
2018:     MatDestroy(&jac->schur_user);
2019:     jac->schur_user = pre;
2020:     PetscObjectReference((PetscObject)jac->schur_user);
2021:   }
2022:   return(0);
2023: }

2027: static PetscErrorCode  PCFieldSplitGetSchurPre_FieldSplit(PC pc,PCFieldSplitSchurPreType *ptype,Mat *pre)
2028: {
2029:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;

2032:   *ptype = jac->schurpre;
2033:   *pre   = jac->schur_user;
2034:   return(0);
2035: }

2039: /*@
2040:     PCFieldSplitSetSchurFactType -  sets which blocks of the approximate block factorization to retain

2042:     Collective on PC

2044:     Input Parameters:
2045: +   pc  - the preconditioner context
2046: -   ftype - which blocks of factorization to retain, PC_FIELDSPLIT_SCHUR_FACT_FULL is default

2048:     Options Database:
2049: .     -pc_fieldsplit_schur_fact_type <diag,lower,upper,full> default is full


2052:     Level: intermediate

2054:     Notes:
2055:     The FULL factorization is

2057: $   (A   B)  = (1       0) (A   0) (1  Ainv*B)
2058: $   (C   D)    (C*Ainv  1) (0   S) (0     1  )

2060:     where S = D - 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,
2061:     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).

2063:     If applied exactly, FULL factorization is a direct solver. The preconditioned operator with LOWER or UPPER has all eigenvalues equal to 1 and minimal polynomial
2064:     of degree 2, so KSPGMRES converges in 2 iterations. If the iteration count is very low, consider using KSPFGMRES or KSPGCR which can use one less preconditioner
2065:     application in this case. Note that the preconditioned operator may be highly non-normal, so such fast convergence may not be observed in practice. With DIAG,
2066:     the preconditioned operator has three distinct nonzero eigenvalues and minimal polynomial of degree at most 4, so KSPGMRES converges in at most 4 iterations.

2068:     For symmetric problems in which A is positive definite and S is negative definite, DIAG can be used with KSPMINRES. Note that a flexible method like KSPFGMRES
2069:     or KSPGCR must be used if the fieldsplit preconditioner is nonlinear (e.g. a few iterations of a Krylov method is used inside a split).

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

2075: .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields(), PCFieldSplitSchurPreType
2076: @*/
2077: PetscErrorCode  PCFieldSplitSetSchurFactType(PC pc,PCFieldSplitSchurFactType ftype)
2078: {

2083:   PetscTryMethod(pc,"PCFieldSplitSetSchurFactType_C",(PC,PCFieldSplitSchurFactType),(pc,ftype));
2084:   return(0);
2085: }

2089: static PetscErrorCode PCFieldSplitSetSchurFactType_FieldSplit(PC pc,PCFieldSplitSchurFactType ftype)
2090: {
2091:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;

2094:   jac->schurfactorization = ftype;
2095:   return(0);
2096: }

2100: /*@C
2101:    PCFieldSplitGetSchurBlocks - Gets all matrix blocks for the Schur complement

2103:    Collective on KSP

2105:    Input Parameter:
2106: .  pc - the preconditioner context

2108:    Output Parameters:
2109: +  A00 - the (0,0) block
2110: .  A01 - the (0,1) block
2111: .  A10 - the (1,0) block
2112: -  A11 - the (1,1) block

2114:    Level: advanced

2116: .seealso: PCFIELDSPLIT
2117: @*/
2118: PetscErrorCode  PCFieldSplitGetSchurBlocks(PC pc,Mat *A00,Mat *A01,Mat *A10, Mat *A11)
2119: {
2120:   PC_FieldSplit *jac = (PC_FieldSplit*) pc->data;

2124:   if (jac->type != PC_COMPOSITE_SCHUR) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONG, "FieldSplit is not using a Schur complement approach.");
2125:   if (A00) *A00 = jac->pmat[0];
2126:   if (A01) *A01 = jac->B;
2127:   if (A10) *A10 = jac->C;
2128:   if (A11) *A11 = jac->pmat[1];
2129:   return(0);
2130: }

2134: static PetscErrorCode  PCFieldSplitSetType_FieldSplit(PC pc,PCCompositeType type)
2135: {
2136:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;

2140:   jac->type = type;
2141:   if (type == PC_COMPOSITE_SCHUR) {
2142:     pc->ops->apply = PCApply_FieldSplit_Schur;
2143:     pc->ops->view  = PCView_FieldSplit_Schur;

2145:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit_Schur);
2146:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurPre_C",PCFieldSplitSetSchurPre_FieldSplit);
2147:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSchurPre_C",PCFieldSplitGetSchurPre_FieldSplit);
2148:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C",PCFieldSplitSetSchurFactType_FieldSplit);

2150:   } else {
2151:     pc->ops->apply = PCApply_FieldSplit;
2152:     pc->ops->view  = PCView_FieldSplit;

2154:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit);
2155:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurPre_C",0);
2156:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSchurPre_C",0);
2157:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C",0);
2158:   }
2159:   return(0);
2160: }

2164: static PetscErrorCode  PCFieldSplitSetBlockSize_FieldSplit(PC pc,PetscInt bs)
2165: {
2166:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;

2169:   if (bs < 1) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Blocksize must be positive, you gave %D",bs);
2170:   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);
2171:   jac->bs = bs;
2172:   return(0);
2173: }

2177: /*@
2178:    PCFieldSplitSetType - Sets the type of fieldsplit preconditioner.

2180:    Collective on PC

2182:    Input Parameter:
2183: .  pc - the preconditioner context
2184: .  type - PC_COMPOSITE_ADDITIVE, PC_COMPOSITE_MULTIPLICATIVE (default), PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE, PC_COMPOSITE_SPECIAL, PC_COMPOSITE_SCHUR

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

2189:    Level: Intermediate

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

2193: .seealso: PCCompositeSetType()

2195: @*/
2196: PetscErrorCode  PCFieldSplitSetType(PC pc,PCCompositeType type)
2197: {

2202:   PetscTryMethod(pc,"PCFieldSplitSetType_C",(PC,PCCompositeType),(pc,type));
2203:   return(0);
2204: }

2208: /*@
2209:   PCFieldSplitGetType - Gets the type of fieldsplit preconditioner.

2211:   Not collective

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

2216:   Output Parameter:
2217: . type - PC_COMPOSITE_ADDITIVE, PC_COMPOSITE_MULTIPLICATIVE (default), PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE, PC_COMPOSITE_SPECIAL, PC_COMPOSITE_SCHUR

2219:   Level: Intermediate

2221: .keywords: PC, set, type, composite preconditioner, additive, multiplicative
2222: .seealso: PCCompositeSetType()
2223: @*/
2224: PetscErrorCode PCFieldSplitGetType(PC pc, PCCompositeType *type)
2225: {
2226:   PC_FieldSplit *jac = (PC_FieldSplit*) pc->data;

2231:   *type = jac->type;
2232:   return(0);
2233: }

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

2240:    Logically Collective

2242:    Input Parameters:
2243: +  pc   - the preconditioner context
2244: -  flg  - boolean indicating whether to use field splits defined by the DM

2246:    Options Database Key:
2247: .  -pc_fieldsplit_dm_splits

2249:    Level: Intermediate

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

2253: .seealso: PCFieldSplitGetDMSplits()

2255: @*/
2256: PetscErrorCode  PCFieldSplitSetDMSplits(PC pc,PetscBool flg)
2257: {
2258:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
2259:   PetscBool      isfs;

2265:   PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
2266:   if (isfs) {
2267:     jac->dm_splits = flg;
2268:   }
2269:   return(0);
2270: }


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

2278:    Logically Collective

2280:    Input Parameter:
2281: .  pc   - the preconditioner context

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

2286:    Level: Intermediate

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

2290: .seealso: PCFieldSplitSetDMSplits()

2292: @*/
2293: PetscErrorCode  PCFieldSplitGetDMSplits(PC pc,PetscBool* flg)
2294: {
2295:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
2296:   PetscBool      isfs;

2302:   PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
2303:   if (isfs) {
2304:     if(flg) *flg = jac->dm_splits;
2305:   }
2306:   return(0);
2307: }

2309: /* -------------------------------------------------------------------------------------*/
2310: /*MC
2311:    PCFIELDSPLIT - Preconditioner created by combining separate preconditioners for individual
2312:                   fields or groups of fields. See the users manual section "Solving Block Matrices" for more details.

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

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

2320:    Level: intermediate

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

2331: -    Options prefix for inner solvers when using Schur complement preconditioner are -fieldsplit_0_ and -fieldsplit_1_
2332:      for all other solvers they are -fieldsplit_%d_ for the dth field, use -fieldsplit_ for all fields

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

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

2343: $     For the Schur complement preconditioner if J = ( A00 A01 )
2344: $                                                    ( A10 A11 )
2345: $     the preconditioner using full factorization is
2346: $              ( I   -ksp(A00) A01 ) ( inv(A00)     0  ) (     I          0  )
2347: $              ( 0         I       ) (   0      ksp(S) ) ( -A10 ksp(A00)  I  )
2348:      where the action of inv(A00) is applied using the KSP solver with prefix -fieldsplit_0_.  S is the Schur complement
2349: $              S = A11 - A10 ksp(A00) A01
2350:      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
2351:      in providing the SECOND split or 1 if not give). For PCFieldSplitGetKSP() when field number is 0,
2352:      it returns the KSP associated with -fieldsplit_0_ while field number 1 gives -fieldsplit_1_ KSP. By default
2353:      A11 is used to construct a preconditioner for S, use PCFieldSplitSetSchurPre() for all the possible ways to construct the preconditioner for S.

2355:      The factorization type is set using -pc_fieldsplit_schur_fact_type <diag, lower, upper, full>. The full is shown above,
2356:      diag gives
2357: $              ( inv(A00)     0   )
2358: $              (   0      -ksp(S) )
2359:      note that slightly counter intuitively there is a negative in front of the ksp(S) so that the preconditioner is positive definite. The lower factorization is the inverse of
2360: $              (  A00   0 )
2361: $              (  A10   S )
2362:      where the inverses of A00 and S are applied using KSPs. The upper factorization is the inverse of
2363: $              ( A00 A01 )
2364: $              (  0   S  )
2365:      where again the inverses of A00 and S are applied using KSPs.

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

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

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

2377:    Concepts: physics based preconditioners, block preconditioners

2379:    There is a nice discussion of block preconditioners in

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

2385:    The Constrained Pressure Preconditioner (CPR) does not appear to be currently implementable directly with PCFIELDSPLIT. CPR solves first the Schur complemented pressure equation, updates the
2386:    residual on all variables and then applies a simple ILU like preconditioner on all the variables. So it is very much like the full Schur complement with selfp representing the Schur complement but instead
2387:    of backsolving for the saturations in the last step it solves a full coupled (ILU) system for updates to all the variables.

2389: .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC, Block_Preconditioners, PCLSC,
2390:            PCFieldSplitGetSubKSP(), PCFieldSplitSetFields(), PCFieldSplitSetType(), PCFieldSplitSetIS(), PCFieldSplitSetSchurPre(),
2391:            MatSchurComplementSetAinvType()
2392: M*/

2396: PETSC_EXTERN PetscErrorCode PCCreate_FieldSplit(PC pc)
2397: {
2399:   PC_FieldSplit  *jac;

2402:   PetscNewLog(pc,&jac);

2404:   jac->bs                 = -1;
2405:   jac->nsplits            = 0;
2406:   jac->type               = PC_COMPOSITE_MULTIPLICATIVE;
2407:   jac->schurpre           = PC_FIELDSPLIT_SCHUR_PRE_USER; /* Try user preconditioner first, fall back on diagonal */
2408:   jac->schurfactorization = PC_FIELDSPLIT_SCHUR_FACT_FULL;
2409:   jac->dm_splits          = PETSC_TRUE;

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

2413:   pc->ops->apply           = PCApply_FieldSplit;
2414:   pc->ops->applytranspose  = PCApplyTranspose_FieldSplit;
2415:   pc->ops->setup           = PCSetUp_FieldSplit;
2416:   pc->ops->reset           = PCReset_FieldSplit;
2417:   pc->ops->destroy         = PCDestroy_FieldSplit;
2418:   pc->ops->setfromoptions  = PCSetFromOptions_FieldSplit;
2419:   pc->ops->view            = PCView_FieldSplit;
2420:   pc->ops->applyrichardson = 0;

2422:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit);
2423:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetFields_C",PCFieldSplitSetFields_FieldSplit);
2424:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetIS_C",PCFieldSplitSetIS_FieldSplit);
2425:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetType_C",PCFieldSplitSetType_FieldSplit);
2426:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetBlockSize_C",PCFieldSplitSetBlockSize_FieldSplit);
2427:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitRestrictIS_C",PCFieldSplitRestrictIS_FieldSplit);
2428:   return(0);
2429: }