Actual source code: fieldsplit.c

petsc-master 2015-07-03
Report Typos and Errors
  2: #include <petsc/private/pcimpl.h>     /*I "petscpc.h" I*/
  3: #include <petscdm.h>


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

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

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

 33:   /* Only used when Schur complement preconditioning is used */
 34:   Mat                       B;                     /* The (0,1) block */
 35:   Mat                       C;                     /* The (1,0) block */
 36:   Mat                       schur;                 /* The Schur complement S = A11 - A10 A00^{-1} A01, the KSP here, kspinner, is H_1 in [El08] */
 37:   Mat                       schurp;                /* Assembled approximation to S built by MatSchurComplement to be used as a preconditioning matrix when solving with S */
 38:   Mat                       schur_user;            /* User-provided preconditioning matrix for the Schur complement */
 39:   PCFieldSplitSchurPreType  schurpre;              /* Determines which preconditioning matrix is used for the Schur complement */
 40:   PCFieldSplitSchurFactType schurfactorization;
 41:   KSP                       kspschur;              /* The solver for S */
 42:   KSP                       kspupper;              /* The solver for A in the upper diagonal part of the factorization (H_2 in [El08]) */
 43:   PC_FieldSplitLink         head;
 44:   PetscBool                 reset;                  /* indicates PCReset() has been last called on this object, hack */
 45:   PetscBool                 suboptionsset;          /* Indicates that the KSPSetFromOptions() has been called on the sub-KSPs */
 46:   PetscBool                 dm_splits;              /* Whether to use DMCreateFieldDecomposition() whenever possible */
 47:   PetscBool                 diag_use_amat;          /* Whether to extract diagonal matrix blocks from Amat, rather than Pmat (weaker than -pc_use_amat) */
 48:   PetscBool                 offdiag_use_amat;       /* Whether to extract off-diagonal matrix blocks from Amat, rather than Pmat (weaker than -pc_use_amat) */
 49: } PC_FieldSplit;

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

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


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

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

125:  if (isdraw) {
126:     PetscDraw draw;
127:     PetscReal x,y,w,wd;

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

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

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

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

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

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

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

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

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

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

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

345:         PetscSNPrintf(optionname, sizeof(optionname), "-pc_fieldsplit_%D_fields", i);
346:         PetscOptionsGetIntArray(((PetscObject) pc)->prefix, optionname, ifields, &nfields, &flg);
347:         if (!flg) break;
348:         if (numFields > 128) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Cannot currently support %d > 128 fields", numFields);
349:         DMCreateSubDM(pc->dm, nfields, ifields, &compField, &subdm[i]);
350:         if (nfields == 1) {
351:           PCFieldSplitSetIS(pc, fieldNames[ifields[0]], compField);
352:           /* PetscPrintf(PetscObjectComm((PetscObject)pc), "%s Field Indices:", fieldNames[ifields[0]]);
353:              ISView(compField, NULL); */
354:         } else {
355:           PetscSNPrintf(splitname, sizeof(splitname), "%D", i);
356:           PCFieldSplitSetIS(pc, splitname, compField);
357:           /* PetscPrintf(PetscObjectComm((PetscObject)pc), "%s Field Indices:", splitname);
358:              ISView(compField, NULL); */
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:         if (jac->reset) {
411:           jac->head->is       = rest;
412:           jac->head->next->is = zerodiags;
413:         } else {
414:           PCFieldSplitSetIS(pc,"0",rest);
415:           PCFieldSplitSetIS(pc,"1",zerodiags);
416:         }
417:         ISDestroy(&zerodiags);
418:         ISDestroy(&rest);
419:       } else if (coupling) {
420:         IS       coupling,rest;
421:         PetscInt nmin,nmax;

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

461:       MatGetOwnershipRange(pc->mat,&nmin,&nmax);
462:       ISComplement(ilink->is,nmin,nmax,&is2);
463:       PCFieldSplitSetIS(pc,"1",is2);
464:       ISDestroy(&is2);
465:     } else SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Must provide at least two sets of fields to PCFieldSplit()");
466:   }


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

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

477: static PetscErrorCode PCSetUp_FieldSplit(PC pc)
478: {
479:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
480:   PetscErrorCode    ierr;
481:   PC_FieldSplitLink ilink;
482:   PetscInt          i,nsplit;
483:   PetscBool         sorted, sorted_col;

486:   PCFieldSplitSetDefaults(pc);
487:   nsplit = jac->nsplits;
488:   ilink  = jac->head;

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

494:     jac->issetup = PETSC_TRUE;

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

534:   ilink = jac->head;
535:   if (!jac->pmat) {
536:     Vec xtmp;

538:     MatCreateVecs(pc->pmat,&xtmp,NULL);
539:     PetscMalloc1(nsplit,&jac->pmat);
540:     PetscMalloc2(nsplit,&jac->x,nsplit,&jac->y);
541:     for (i=0; i<nsplit; i++) {
542:       MatNullSpace sp;

544:       /* Check for preconditioning matrix attached to IS */
545:       PetscObjectQuery((PetscObject) ilink->is, "pmat", (PetscObject*) &jac->pmat[i]);
546:       if (jac->pmat[i]) {
547:         PetscObjectReference((PetscObject) jac->pmat[i]);
548:         if (jac->type == PC_COMPOSITE_SCHUR) {
549:           jac->schur_user = jac->pmat[i];

551:           PetscObjectReference((PetscObject) jac->schur_user);
552:         }
553:       } else {
554:         const char *prefix;
555:         MatGetSubMatrix(pc->pmat,ilink->is,ilink->is_col,MAT_INITIAL_MATRIX,&jac->pmat[i]);
556:         KSPGetOptionsPrefix(ilink->ksp,&prefix);
557:         MatSetOptionsPrefix(jac->pmat[i],prefix);
558:         MatViewFromOptions(jac->pmat[i],NULL,"-mat_view");
559:       }
560:       /* create work vectors for each split */
561:       MatCreateVecs(jac->pmat[i],&jac->x[i],&jac->y[i]);
562:       ilink->x = jac->x[i]; ilink->y = jac->y[i]; ilink->z = NULL;
563:       /* compute scatter contexts needed by multiplicative versions and non-default splits */
564:       VecScatterCreate(xtmp,ilink->is,jac->x[i],NULL,&ilink->sctx);
565:       /* Check for null space attached to IS */
566:       PetscObjectQuery((PetscObject) ilink->is, "nullspace", (PetscObject*) &sp);
567:       if (sp) {
568:         MatSetNullSpace(jac->pmat[i], sp);
569:       }
570:       PetscObjectQuery((PetscObject) ilink->is, "nearnullspace", (PetscObject*) &sp);
571:       if (sp) {
572:         MatSetNearNullSpace(jac->pmat[i], sp);
573:       }
574:       ilink = ilink->next;
575:     }
576:     VecDestroy(&xtmp);
577:   } else {
578:     for (i=0; i<nsplit; i++) {
579:       Mat pmat;

581:       /* Check for preconditioning matrix attached to IS */
582:       PetscObjectQuery((PetscObject) ilink->is, "pmat", (PetscObject*) &pmat);
583:       if (!pmat) {
584:         MatGetSubMatrix(pc->pmat,ilink->is,ilink->is_col,MAT_REUSE_MATRIX,&jac->pmat[i]);
585:       }
586:       ilink = ilink->next;
587:     }
588:   }
589:   if (jac->diag_use_amat) {
590:     ilink = jac->head;
591:     if (!jac->mat) {
592:       PetscMalloc1(nsplit,&jac->mat);
593:       for (i=0; i<nsplit; i++) {
594:         MatGetSubMatrix(pc->mat,ilink->is,ilink->is_col,MAT_INITIAL_MATRIX,&jac->mat[i]);
595:         ilink = ilink->next;
596:       }
597:     } else {
598:       for (i=0; i<nsplit; i++) {
599:         if (jac->mat[i]) {MatGetSubMatrix(pc->mat,ilink->is,ilink->is_col,MAT_REUSE_MATRIX,&jac->mat[i]);}
600:         ilink = ilink->next;
601:       }
602:     }
603:   } else {
604:     jac->mat = jac->pmat;
605:   }

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

635:   if (jac->type == PC_COMPOSITE_SCHUR) {
636:     IS          ccis;
637:     PetscInt    rstart,rend;
638:     char        lscname[256];
639:     PetscObject LSC_L;

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

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

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

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

686:       /* extract the A01 and A10 matrices */
687:       ilink = jac->head;
688:       ISComplement(ilink->is_col,rstart,rend,&ccis);
689:       if (jac->offdiag_use_amat) {
690:         MatGetSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->B);
691:       } else {
692:         MatGetSubMatrix(pc->pmat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->B);
693:       }
694:       ISDestroy(&ccis);
695:       ilink = ilink->next;
696:       ISComplement(ilink->is_col,rstart,rend,&ccis);
697:       if (jac->offdiag_use_amat) {
698:         MatGetSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->C);
699:       } else {
700:         MatGetSubMatrix(pc->pmat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->C);
701:       }
702:       ISDestroy(&ccis);

704:       /* Use mat[0] (diagonal block of Amat) preconditioned by pmat[0] to define Schur complement */
705:       MatCreate(((PetscObject)jac->mat[0])->comm,&jac->schur);
706:       MatSetType(jac->schur,MATSCHURCOMPLEMENT);
707:       MatSchurComplementSetSubMatrices(jac->schur,jac->mat[0],jac->pmat[0],jac->B,jac->C,jac->mat[1]);
708:       PetscSNPrintf(schurmatprefix, sizeof(schurmatprefix), "%sfieldsplit_%s_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname);
709:       /* 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? */
710:       MatSetOptionsPrefix(jac->schur,schurmatprefix);
711:       MatSetFromOptions(jac->schur);
712:       MatGetNullSpace(jac->pmat[1], &sp);
713:       if (sp) {
714:         MatSetNullSpace(jac->schur, sp);
715:       }

717:       PetscSNPrintf(schurtestoption, sizeof(schurtestoption), "-fieldsplit_%s_inner_", ilink->splitname);
718:       PetscOptionsFindPairPrefix_Private(((PetscObject)pc)->prefix, schurtestoption, NULL, &flg);
719:       if (flg) {
720:         DM  dmInner;
721:         KSP kspInner;

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

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

747:       PetscSNPrintf(schurtestoption, sizeof(schurtestoption), "-fieldsplit_%s_upper_", ilink->splitname);
748:       PetscOptionsFindPairPrefix_Private(((PetscObject)pc)->prefix, schurtestoption, NULL, &flg);
749:       if (flg) {
750:         DM dmInner;

752:         PetscSNPrintf(schurprefix, sizeof(schurprefix), "%sfieldsplit_%s_upper_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname);
753:         KSPCreate(PetscObjectComm((PetscObject)pc), &jac->kspupper);
754:         KSPSetErrorIfNotConverged(jac->kspupper,pc->erroriffailure);
755:         KSPSetOptionsPrefix(jac->kspupper, schurprefix);
756:         KSPGetDM(jac->head->ksp, &dmInner);
757:         KSPSetDM(jac->kspupper, dmInner);
758:         KSPSetDMActive(jac->kspupper, PETSC_FALSE);
759:         KSPSetFromOptions(jac->kspupper);
760:         KSPSetOperators(jac->kspupper,jac->mat[0],jac->pmat[0]);
761:         VecDuplicate(jac->head->x, &jac->head->z);
762:       } else {
763:         jac->kspupper = jac->head->ksp;
764:         PetscObjectReference((PetscObject) jac->head->ksp);
765:       }

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

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

821:   jac->suboptionsset = PETSC_TRUE;
822:   return(0);
823: }

825: #define FieldSplitSplitSolveAdd(ilink,xx,yy) \
826:   (VecScatterBegin(ilink->sctx,xx,ilink->x,INSERT_VALUES,SCATTER_FORWARD) || \
827:    VecScatterEnd(ilink->sctx,xx,ilink->x,INSERT_VALUES,SCATTER_FORWARD) || \
828:    KSPSolve(ilink->ksp,ilink->x,ilink->y) || \
829:    VecScatterBegin(ilink->sctx,ilink->y,yy,ADD_VALUES,SCATTER_REVERSE) || \
830:    VecScatterEnd(ilink->sctx,ilink->y,yy,ADD_VALUES,SCATTER_REVERSE))

834: static PetscErrorCode PCApply_FieldSplit_Schur(PC pc,Vec x,Vec y)
835: {
836:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
837:   PetscErrorCode    ierr;
838:   PC_FieldSplitLink ilinkA = jac->head, ilinkD = ilinkA->next;
839:   KSP               kspA   = ilinkA->ksp, kspLower = kspA, kspUpper = jac->kspupper;

842:   switch (jac->schurfactorization) {
843:   case PC_FIELDSPLIT_SCHUR_FACT_DIAG:
844:     /* [A00 0; 0 -S], positive definite, suitable for MINRES */
845:     VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
846:     VecScatterBegin(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
847:     VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
848:     KSPSolve(kspA,ilinkA->x,ilinkA->y);
849:     VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
850:     VecScatterEnd(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
851:     KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);
852:     VecScale(ilinkD->y,-1.);
853:     VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
854:     VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
855:     VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
856:     break;
857:   case PC_FIELDSPLIT_SCHUR_FACT_LOWER:
858:     /* [A00 0; A10 S], suitable for left preconditioning */
859:     VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
860:     VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
861:     KSPSolve(kspA,ilinkA->x,ilinkA->y);
862:     MatMult(jac->C,ilinkA->y,ilinkD->x);
863:     VecScale(ilinkD->x,-1.);
864:     VecScatterBegin(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);
865:     VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
866:     VecScatterEnd(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);
867:     KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);
868:     VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
869:     VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
870:     VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
871:     break;
872:   case PC_FIELDSPLIT_SCHUR_FACT_UPPER:
873:     /* [A00 A01; 0 S], suitable for right preconditioning */
874:     VecScatterBegin(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
875:     VecScatterEnd(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
876:     KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);
877:     MatMult(jac->B,ilinkD->y,ilinkA->x);
878:     VecScale(ilinkA->x,-1.);
879:     VecScatterBegin(ilinkA->sctx,x,ilinkA->x,ADD_VALUES,SCATTER_FORWARD);
880:     VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
881:     VecScatterEnd(ilinkA->sctx,x,ilinkA->x,ADD_VALUES,SCATTER_FORWARD);
882:     KSPSolve(kspA,ilinkA->x,ilinkA->y);
883:     VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
884:     VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
885:     VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
886:     break;
887:   case PC_FIELDSPLIT_SCHUR_FACT_FULL:
888:     /* [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 */
889:     VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
890:     VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
891:     KSPSolve(kspLower,ilinkA->x,ilinkA->y);
892:     MatMult(jac->C,ilinkA->y,ilinkD->x);
893:     VecScale(ilinkD->x,-1.0);
894:     VecScatterBegin(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);
895:     VecScatterEnd(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);

897:     KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);
898:     VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
899:     VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);

901:     if (kspUpper == kspA) {
902:       MatMult(jac->B,ilinkD->y,ilinkA->y);
903:       VecAXPY(ilinkA->x,-1.0,ilinkA->y);
904:       KSPSolve(kspA,ilinkA->x,ilinkA->y);
905:     } else {
906:       KSPSolve(kspA,ilinkA->x,ilinkA->y);
907:       MatMult(jac->B,ilinkD->y,ilinkA->x);
908:       KSPSolve(kspUpper,ilinkA->x,ilinkA->z);
909:       VecAXPY(ilinkA->y,-1.0,ilinkA->z);
910:     }
911:     VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
912:     VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
913:   }
914:   return(0);
915: }

919: static PetscErrorCode PCApply_FieldSplit(PC pc,Vec x,Vec y)
920: {
921:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
922:   PetscErrorCode    ierr;
923:   PC_FieldSplitLink ilink = jac->head;
924:   PetscInt          cnt,bs;

927:   if (jac->type == PC_COMPOSITE_ADDITIVE) {
928:     if (jac->defaultsplit) {
929:       VecGetBlockSize(x,&bs);
930:       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);
931:       VecGetBlockSize(y,&bs);
932:       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);
933:       VecStrideGatherAll(x,jac->x,INSERT_VALUES);
934:       while (ilink) {
935:         KSPSolve(ilink->ksp,ilink->x,ilink->y);
936:         ilink = ilink->next;
937:       }
938:       VecStrideScatterAll(jac->y,y,INSERT_VALUES);
939:     } else {
940:       VecSet(y,0.0);
941:       while (ilink) {
942:         FieldSplitSplitSolveAdd(ilink,x,y);
943:         ilink = ilink->next;
944:       }
945:     }
946:   } else if (jac->type == PC_COMPOSITE_MULTIPLICATIVE && jac->nsplits == 2) {
947:     VecSet(y,0.0);
948:     /* solve on first block for first block variables */
949:     VecScatterBegin(ilink->sctx,x,ilink->x,INSERT_VALUES,SCATTER_FORWARD);
950:     VecScatterEnd(ilink->sctx,x,ilink->x,INSERT_VALUES,SCATTER_FORWARD);
951:     KSPSolve(ilink->ksp,ilink->x,ilink->y);
952:     VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
953:     VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);

955:     /* compute the residual only onto second block variables using first block variables */
956:     MatMult(jac->Afield[1],ilink->y,ilink->next->x);
957:     ilink = ilink->next;
958:     VecScale(ilink->x,-1.0);
959:     VecScatterBegin(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
960:     VecScatterEnd(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);

962:     /* solve on second block variables */
963:     KSPSolve(ilink->ksp,ilink->x,ilink->y);
964:     VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
965:     VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
966:   } else if (jac->type == PC_COMPOSITE_MULTIPLICATIVE || jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
967:     if (!jac->w1) {
968:       VecDuplicate(x,&jac->w1);
969:       VecDuplicate(x,&jac->w2);
970:     }
971:     VecSet(y,0.0);
972:     FieldSplitSplitSolveAdd(ilink,x,y);
973:     cnt  = 1;
974:     while (ilink->next) {
975:       ilink = ilink->next;
976:       /* compute the residual only over the part of the vector needed */
977:       MatMult(jac->Afield[cnt++],y,ilink->x);
978:       VecScale(ilink->x,-1.0);
979:       VecScatterBegin(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
980:       VecScatterEnd(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
981:       KSPSolve(ilink->ksp,ilink->x,ilink->y);
982:       VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
983:       VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
984:     }
985:     if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
986:       cnt -= 2;
987:       while (ilink->previous) {
988:         ilink = ilink->previous;
989:         /* compute the residual only over the part of the vector needed */
990:         MatMult(jac->Afield[cnt--],y,ilink->x);
991:         VecScale(ilink->x,-1.0);
992:         VecScatterBegin(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
993:         VecScatterEnd(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
994:         KSPSolve(ilink->ksp,ilink->x,ilink->y);
995:         VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
996:         VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
997:       }
998:     }
999:   } else SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Unsupported or unknown composition",(int) jac->type);
1000:   return(0);
1001: }

1003: #define FieldSplitSplitSolveAddTranspose(ilink,xx,yy) \
1004:   (VecScatterBegin(ilink->sctx,xx,ilink->y,INSERT_VALUES,SCATTER_FORWARD) || \
1005:    VecScatterEnd(ilink->sctx,xx,ilink->y,INSERT_VALUES,SCATTER_FORWARD) || \
1006:    KSPSolveTranspose(ilink->ksp,ilink->y,ilink->x) || \
1007:    VecScatterBegin(ilink->sctx,ilink->x,yy,ADD_VALUES,SCATTER_REVERSE) || \
1008:    VecScatterEnd(ilink->sctx,ilink->x,yy,ADD_VALUES,SCATTER_REVERSE))

1012: static PetscErrorCode PCApplyTranspose_FieldSplit(PC pc,Vec x,Vec y)
1013: {
1014:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1015:   PetscErrorCode    ierr;
1016:   PC_FieldSplitLink ilink = jac->head;
1017:   PetscInt          bs;

1020:   if (jac->type == PC_COMPOSITE_ADDITIVE) {
1021:     if (jac->defaultsplit) {
1022:       VecGetBlockSize(x,&bs);
1023:       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);
1024:       VecGetBlockSize(y,&bs);
1025:       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);
1026:       VecStrideGatherAll(x,jac->x,INSERT_VALUES);
1027:       while (ilink) {
1028:         KSPSolveTranspose(ilink->ksp,ilink->x,ilink->y);
1029:         ilink = ilink->next;
1030:       }
1031:       VecStrideScatterAll(jac->y,y,INSERT_VALUES);
1032:     } else {
1033:       VecSet(y,0.0);
1034:       while (ilink) {
1035:         FieldSplitSplitSolveAddTranspose(ilink,x,y);
1036:         ilink = ilink->next;
1037:       }
1038:     }
1039:   } else {
1040:     if (!jac->w1) {
1041:       VecDuplicate(x,&jac->w1);
1042:       VecDuplicate(x,&jac->w2);
1043:     }
1044:     VecSet(y,0.0);
1045:     if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
1046:       FieldSplitSplitSolveAddTranspose(ilink,x,y);
1047:       while (ilink->next) {
1048:         ilink = ilink->next;
1049:         MatMultTranspose(pc->mat,y,jac->w1);
1050:         VecWAXPY(jac->w2,-1.0,jac->w1,x);
1051:         FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);
1052:       }
1053:       while (ilink->previous) {
1054:         ilink = ilink->previous;
1055:         MatMultTranspose(pc->mat,y,jac->w1);
1056:         VecWAXPY(jac->w2,-1.0,jac->w1,x);
1057:         FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);
1058:       }
1059:     } else {
1060:       while (ilink->next) {   /* get to last entry in linked list */
1061:         ilink = ilink->next;
1062:       }
1063:       FieldSplitSplitSolveAddTranspose(ilink,x,y);
1064:       while (ilink->previous) {
1065:         ilink = ilink->previous;
1066:         MatMultTranspose(pc->mat,y,jac->w1);
1067:         VecWAXPY(jac->w2,-1.0,jac->w1,x);
1068:         FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);
1069:       }
1070:     }
1071:   }
1072:   return(0);
1073: }

1077: static PetscErrorCode PCReset_FieldSplit(PC pc)
1078: {
1079:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1080:   PetscErrorCode    ierr;
1081:   PC_FieldSplitLink ilink = jac->head,next;

1084:   while (ilink) {
1085:     KSPReset(ilink->ksp);
1086:     VecDestroy(&ilink->x);
1087:     VecDestroy(&ilink->y);
1088:     VecDestroy(&ilink->z);
1089:     VecScatterDestroy(&ilink->sctx);
1090:     ISDestroy(&ilink->is);
1091:     ISDestroy(&ilink->is_col);
1092:     next  = ilink->next;
1093:     ilink = next;
1094:   }
1095:   PetscFree2(jac->x,jac->y);
1096:   if (jac->mat && jac->mat != jac->pmat) {
1097:     MatDestroyMatrices(jac->nsplits,&jac->mat);
1098:   } else if (jac->mat) {
1099:     jac->mat = NULL;
1100:   }
1101:   if (jac->pmat) {MatDestroyMatrices(jac->nsplits,&jac->pmat);}
1102:   if (jac->Afield) {MatDestroyMatrices(jac->nsplits,&jac->Afield);}
1103:   VecDestroy(&jac->w1);
1104:   VecDestroy(&jac->w2);
1105:   MatDestroy(&jac->schur);
1106:   MatDestroy(&jac->schurp);
1107:   MatDestroy(&jac->schur_user);
1108:   KSPDestroy(&jac->kspschur);
1109:   KSPDestroy(&jac->kspupper);
1110:   MatDestroy(&jac->B);
1111:   MatDestroy(&jac->C);
1112:   jac->reset = PETSC_TRUE;
1113:   return(0);
1114: }

1118: static PetscErrorCode PCDestroy_FieldSplit(PC pc)
1119: {
1120:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1121:   PetscErrorCode    ierr;
1122:   PC_FieldSplitLink ilink = jac->head,next;

1125:   PCReset_FieldSplit(pc);
1126:   while (ilink) {
1127:     KSPDestroy(&ilink->ksp);
1128:     next  = ilink->next;
1129:     PetscFree(ilink->splitname);
1130:     PetscFree(ilink->fields);
1131:     PetscFree(ilink->fields_col);
1132:     PetscFree(ilink);
1133:     ilink = next;
1134:   }
1135:   PetscFree2(jac->x,jac->y);
1136:   PetscFree(pc->data);
1137:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",NULL);
1138:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetFields_C",NULL);
1139:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetIS_C",NULL);
1140:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetType_C",NULL);
1141:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetBlockSize_C",NULL);
1142:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurPre_C",NULL);
1143:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSchurPre_C",NULL);
1144:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C",NULL);
1145:   return(0);
1146: }

1150: static PetscErrorCode PCSetFromOptions_FieldSplit(PetscOptions *PetscOptionsObject,PC pc)
1151: {
1152:   PetscErrorCode  ierr;
1153:   PetscInt        bs;
1154:   PetscBool       flg,stokes = PETSC_FALSE;
1155:   PC_FieldSplit   *jac = (PC_FieldSplit*)pc->data;
1156:   PCCompositeType ctype;

1159:   PetscOptionsHead(PetscOptionsObject,"FieldSplit options");
1160:   PetscOptionsBool("-pc_fieldsplit_dm_splits","Whether to use DMCreateFieldDecomposition() for splits","PCFieldSplitSetDMSplits",jac->dm_splits,&jac->dm_splits,NULL);
1161:   PetscOptionsInt("-pc_fieldsplit_block_size","Blocksize that defines number of fields","PCFieldSplitSetBlockSize",jac->bs,&bs,&flg);
1162:   if (flg) {
1163:     PCFieldSplitSetBlockSize(pc,bs);
1164:   }
1165:   jac->diag_use_amat = pc->useAmat;
1166:   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);
1167:   jac->offdiag_use_amat = pc->useAmat;
1168:   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);
1169:   /* FIXME: No programmatic equivalent to the following. */
1170:   PetscOptionsGetBool(((PetscObject)pc)->prefix,"-pc_fieldsplit_detect_saddle_point",&stokes,NULL);
1171:   if (stokes) {
1172:     PCFieldSplitSetType(pc,PC_COMPOSITE_SCHUR);
1173:     jac->schurpre = PC_FIELDSPLIT_SCHUR_PRE_SELF;
1174:   }

1176:   PetscOptionsEnum("-pc_fieldsplit_type","Type of composition","PCFieldSplitSetType",PCCompositeTypes,(PetscEnum)jac->type,(PetscEnum*)&ctype,&flg);
1177:   if (flg) {
1178:     PCFieldSplitSetType(pc,ctype);
1179:   }
1180:   /* Only setup fields once */
1181:   if ((jac->bs > 0) && (jac->nsplits == 0)) {
1182:     /* only allow user to set fields from command line if bs is already known.
1183:        otherwise user can set them in PCFieldSplitSetDefaults() */
1184:     PCFieldSplitSetRuntimeSplits_Private(pc);
1185:     if (jac->splitdefined) {PetscInfo(pc,"Splits defined using the options database\n");}
1186:   }
1187:   if (jac->type == PC_COMPOSITE_SCHUR) {
1188:     PetscOptionsGetEnum(((PetscObject)pc)->prefix,"-pc_fieldsplit_schur_factorization_type",PCFieldSplitSchurFactTypes,(PetscEnum*)&jac->schurfactorization,&flg);
1189:     if (flg) {PetscInfo(pc,"Deprecated use of -pc_fieldsplit_schur_factorization_type\n");}
1190:     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);
1191:     PetscOptionsEnum("-pc_fieldsplit_schur_precondition","How to build preconditioner for Schur complement","PCFieldSplitSetSchurPre",PCFieldSplitSchurPreTypes,(PetscEnum)jac->schurpre,(PetscEnum*)&jac->schurpre,NULL);
1192:   }
1193:   PetscOptionsTail();
1194:   return(0);
1195: }

1197: /*------------------------------------------------------------------------------------*/

1201: static PetscErrorCode  PCFieldSplitSetFields_FieldSplit(PC pc,const char splitname[],PetscInt n,const PetscInt *fields,const PetscInt *fields_col)
1202: {
1203:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1204:   PetscErrorCode    ierr;
1205:   PC_FieldSplitLink ilink,next = jac->head;
1206:   char              prefix[128];
1207:   PetscInt          i;

1210:   if (jac->splitdefined) {
1211:     PetscInfo1(pc,"Ignoring new split \"%s\" because the splits have already been defined\n",splitname);
1212:     return(0);
1213:   }
1214:   for (i=0; i<n; i++) {
1215:     if (fields[i] >= jac->bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Field %D requested but only %D exist",fields[i],jac->bs);
1216:     if (fields[i] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative field %D requested",fields[i]);
1217:   }
1218:   PetscNew(&ilink);
1219:   if (splitname) {
1220:     PetscStrallocpy(splitname,&ilink->splitname);
1221:   } else {
1222:     PetscMalloc1(3,&ilink->splitname);
1223:     PetscSNPrintf(ilink->splitname,2,"%s",jac->nsplits);
1224:   }
1225:   PetscMalloc1(n,&ilink->fields);
1226:   PetscMemcpy(ilink->fields,fields,n*sizeof(PetscInt));
1227:   PetscMalloc1(n,&ilink->fields_col);
1228:   PetscMemcpy(ilink->fields_col,fields_col,n*sizeof(PetscInt));

1230:   ilink->nfields = n;
1231:   ilink->next    = NULL;
1232:   KSPCreate(PetscObjectComm((PetscObject)pc),&ilink->ksp);
1233:   KSPSetErrorIfNotConverged(ilink->ksp,pc->erroriffailure);
1234:   PetscObjectIncrementTabLevel((PetscObject)ilink->ksp,(PetscObject)pc,1);
1235:   KSPSetType(ilink->ksp,KSPPREONLY);
1236:   PetscLogObjectParent((PetscObject)pc,(PetscObject)ilink->ksp);

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

1241:   if (!next) {
1242:     jac->head       = ilink;
1243:     ilink->previous = NULL;
1244:   } else {
1245:     while (next->next) {
1246:       next = next->next;
1247:     }
1248:     next->next      = ilink;
1249:     ilink->previous = next;
1250:   }
1251:   jac->nsplits++;
1252:   return(0);
1253: }

1257: static PetscErrorCode  PCFieldSplitGetSubKSP_FieldSplit_Schur(PC pc,PetscInt *n,KSP **subksp)
1258: {
1259:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;

1263:   PetscMalloc1(jac->nsplits,subksp);
1264:   MatSchurComplementGetKSP(jac->schur,*subksp);

1266:   (*subksp)[1] = jac->kspschur;
1267:   if (n) *n = jac->nsplits;
1268:   return(0);
1269: }

1273: static PetscErrorCode  PCFieldSplitGetSubKSP_FieldSplit(PC pc,PetscInt *n,KSP **subksp)
1274: {
1275:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1276:   PetscErrorCode    ierr;
1277:   PetscInt          cnt   = 0;
1278:   PC_FieldSplitLink ilink = jac->head;

1281:   PetscMalloc1(jac->nsplits,subksp);
1282:   while (ilink) {
1283:     (*subksp)[cnt++] = ilink->ksp;
1284:     ilink            = ilink->next;
1285:   }
1286:   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);
1287:   if (n) *n = jac->nsplits;
1288:   return(0);
1289: }

1293: static PetscErrorCode  PCFieldSplitSetIS_FieldSplit(PC pc,const char splitname[],IS is)
1294: {
1295:   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1296:   PetscErrorCode    ierr;
1297:   PC_FieldSplitLink ilink, next = jac->head;
1298:   char              prefix[128];

1301:   if (jac->splitdefined) {
1302:     PetscInfo1(pc,"Ignoring new split \"%s\" because the splits have already been defined\n",splitname);
1303:     return(0);
1304:   }
1305:   PetscNew(&ilink);
1306:   if (splitname) {
1307:     PetscStrallocpy(splitname,&ilink->splitname);
1308:   } else {
1309:     PetscMalloc1(8,&ilink->splitname);
1310:     PetscSNPrintf(ilink->splitname,7,"%D",jac->nsplits);
1311:   }
1312:   PetscObjectReference((PetscObject)is);
1313:   ISDestroy(&ilink->is);
1314:   ilink->is     = is;
1315:   PetscObjectReference((PetscObject)is);
1316:   ISDestroy(&ilink->is_col);
1317:   ilink->is_col = is;
1318:   ilink->next   = NULL;
1319:   KSPCreate(PetscObjectComm((PetscObject)pc),&ilink->ksp);
1320:   KSPSetErrorIfNotConverged(ilink->ksp,pc->erroriffailure);
1321:   PetscObjectIncrementTabLevel((PetscObject)ilink->ksp,(PetscObject)pc,1);
1322:   KSPSetType(ilink->ksp,KSPPREONLY);
1323:   PetscLogObjectParent((PetscObject)pc,(PetscObject)ilink->ksp);

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

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

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

1347:     Logically Collective on PC

1349:     Input Parameters:
1350: +   pc  - the preconditioner context
1351: .   splitname - name of this split, if NULL the number of the split is used
1352: .   n - the number of fields in this split
1353: -   fields - the fields in this split

1355:     Level: intermediate

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

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

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

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

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

1373: @*/
1374: PetscErrorCode  PCFieldSplitSetFields(PC pc,const char splitname[],PetscInt n,const PetscInt *fields,const PetscInt *fields_col)
1375: {

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

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

1392:     Logically Collective on PC

1394:     Input Parameters:
1395: +   pc  - the preconditioner object
1396: -   flg - boolean flag indicating whether or not to use Amat to extract the diagonal blocks from

1398:     Options Database:
1399: .     -pc_fieldsplit_diag_use_amat

1401:     Level: intermediate

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

1405: @*/
1406: PetscErrorCode  PCFieldSplitSetDiagUseAmat(PC pc,PetscBool flg)
1407: {
1408:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
1409:   PetscBool      isfs;

1414:   PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
1415:   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"PC not of type %s",PCFIELDSPLIT);
1416:   jac->diag_use_amat = flg;
1417:   return(0);
1418: }

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

1425:     Logically Collective on PC

1427:     Input Parameters:
1428: .   pc  - the preconditioner object

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


1434:     Level: intermediate

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

1438: @*/
1439: PetscErrorCode  PCFieldSplitGetDiagUseAmat(PC pc,PetscBool *flg)
1440: {
1441:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
1442:   PetscBool      isfs;

1448:   PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
1449:   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"PC not of type %s",PCFIELDSPLIT);
1450:   *flg = jac->diag_use_amat;
1451:   return(0);
1452: }

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

1459:     Logically Collective on PC

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

1465:     Options Database:
1466: .     -pc_fieldsplit_off_diag_use_amat

1468:     Level: intermediate

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

1472: @*/
1473: PetscErrorCode  PCFieldSplitSetOffDiagUseAmat(PC pc,PetscBool flg)
1474: {
1475:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
1476:   PetscBool      isfs;

1481:   PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
1482:   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"PC not of type %s",PCFIELDSPLIT);
1483:   jac->offdiag_use_amat = flg;
1484:   return(0);
1485: }

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

1492:     Logically Collective on PC

1494:     Input Parameters:
1495: .   pc  - the preconditioner object

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


1501:     Level: intermediate

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

1505: @*/
1506: PetscErrorCode  PCFieldSplitGetOffDiagUseAmat(PC pc,PetscBool *flg)
1507: {
1508:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
1509:   PetscBool      isfs;

1515:   PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
1516:   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"PC not of type %s",PCFIELDSPLIT);
1517:   *flg = jac->offdiag_use_amat;
1518:   return(0);
1519: }



1525: /*@C
1526:     PCFieldSplitSetIS - Sets the exact elements for field

1528:     Logically Collective on PC

1530:     Input Parameters:
1531: +   pc  - the preconditioner context
1532: .   splitname - name of this split, if NULL the number of the split is used
1533: -   is - the index set that defines the vector elements in this field


1536:     Notes:
1537:     Use PCFieldSplitSetFields(), for fields defined by strided types.

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

1542:     Level: intermediate

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

1546: @*/
1547: PetscErrorCode  PCFieldSplitSetIS(PC pc,const char splitname[],IS is)
1548: {

1555:   PetscTryMethod(pc,"PCFieldSplitSetIS_C",(PC,const char[],IS),(pc,splitname,is));
1556:   return(0);
1557: }

1561: /*@
1562:     PCFieldSplitGetIS - Retrieves the elements for a field as an IS

1564:     Logically Collective on PC

1566:     Input Parameters:
1567: +   pc  - the preconditioner context
1568: -   splitname - name of this split

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

1573:     Level: intermediate

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

1577: @*/
1578: PetscErrorCode PCFieldSplitGetIS(PC pc,const char splitname[],IS *is)
1579: {

1586:   {
1587:     PC_FieldSplit     *jac  = (PC_FieldSplit*) pc->data;
1588:     PC_FieldSplitLink ilink = jac->head;
1589:     PetscBool         found;

1591:     *is = NULL;
1592:     while (ilink) {
1593:       PetscStrcmp(ilink->splitname, splitname, &found);
1594:       if (found) {
1595:         *is = ilink->is;
1596:         break;
1597:       }
1598:       ilink = ilink->next;
1599:     }
1600:   }
1601:   return(0);
1602: }

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

1610:     Logically Collective on PC

1612:     Input Parameters:
1613: +   pc  - the preconditioner context
1614: -   bs - the block size

1616:     Level: intermediate

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

1620: @*/
1621: PetscErrorCode  PCFieldSplitSetBlockSize(PC pc,PetscInt bs)
1622: {

1628:   PetscTryMethod(pc,"PCFieldSplitSetBlockSize_C",(PC,PetscInt),(pc,bs));
1629:   return(0);
1630: }

1634: /*@C
1635:    PCFieldSplitGetSubKSP - Gets the KSP contexts for all splits

1637:    Collective on KSP

1639:    Input Parameter:
1640: .  pc - the preconditioner context

1642:    Output Parameters:
1643: +  n - the number of splits
1644: -  pc - the array of KSP contexts

1646:    Note:
1647:    After PCFieldSplitGetSubKSP() the array of KSPs IS to be freed by the user
1648:    (not the KSP just the array that contains them).

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

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


1657:    Level: advanced

1659: .seealso: PCFIELDSPLIT
1660: @*/
1661: PetscErrorCode  PCFieldSplitGetSubKSP(PC pc,PetscInt *n,KSP *subksp[])
1662: {

1668:   PetscUseMethod(pc,"PCFieldSplitGetSubKSP_C",(PC,PetscInt*,KSP **),(pc,n,subksp));
1669:   return(0);
1670: }

1674: /*@
1675:     PCFieldSplitSetSchurPre -  Indicates if the Schur complement is preconditioned by a preconditioner constructed by the
1676:       A11 matrix. Otherwise no preconditioner is used.

1678:     Collective on PC

1680:     Input Parameters:
1681: +   pc      - the preconditioner context
1682: .   ptype   - which matrix to use for preconditioning the Schur complement: PC_FIELDSPLIT_SCHUR_PRE_A11 (default), PC_FIELDSPLIT_SCHUR_PRE_SELF, PC_FIELDSPLIT_PRE_USER
1683: -   userpre - matrix to use for preconditioning, or NULL

1685:     Options Database:
1686: .     -pc_fieldsplit_schur_precondition <self,selfp,user,a11,full> default is a11

1688:     Notes:
1689: $    If ptype is
1690: $        user then the preconditioner for the Schur complement is generated by the provided matrix (pre argument
1691: $             to this function).
1692: $        a11 then the preconditioner for the Schur complement is generated by the block diagonal part of the preconditioner
1693: $             matrix associated with the Schur complement (i.e. A11), not he Schur complement matrix
1694: $        full then the preconditioner uses the exact Schur complement (this is expensive)
1695: $        self the preconditioner for the Schur complement is generated from the Schur complement matrix itself:
1696: $             The only preconditioner that currently works directly with the Schur complement matrix object is the PCLSC
1697: $             preconditioner
1698: $        selfp then the preconditioning matrix is an explicitly-assembled approximation Sp = A11 - A10 inv(diag(A00)) A01
1699: $             This is only a good preconditioner when diag(A00) is a good preconditioner for A00. Optionally, A00 can be
1700: $             lumped before extracting the diagonal: -fieldsplit_1_mat_schur_complement_ainv_type lump; diag is the default.

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

1706:     Level: intermediate

1708: .seealso: PCFieldSplitGetSchurPre(), PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields(), PCFieldSplitSchurPreType,
1709:           MatSchurComplementSetAinvType(), PCLSC

1711: @*/
1712: PetscErrorCode PCFieldSplitSetSchurPre(PC pc,PCFieldSplitSchurPreType ptype,Mat pre)
1713: {

1718:   PetscTryMethod(pc,"PCFieldSplitSetSchurPre_C",(PC,PCFieldSplitSchurPreType,Mat),(pc,ptype,pre));
1719:   return(0);
1720: }
1721: PetscErrorCode PCFieldSplitSchurPrecondition(PC pc,PCFieldSplitSchurPreType ptype,Mat pre) {return PCFieldSplitSetSchurPre(pc,ptype,pre);} /* Deprecated name */

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

1729:     Logically Collective on PC

1731:     Input Parameters:
1732: .   pc      - the preconditioner context

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

1738:     Level: intermediate

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

1742: @*/
1743: PetscErrorCode PCFieldSplitGetSchurPre(PC pc,PCFieldSplitSchurPreType *ptype,Mat *pre)
1744: {

1749:   PetscUseMethod(pc,"PCFieldSplitGetSchurPre_C",(PC,PCFieldSplitSchurPreType*,Mat*),(pc,ptype,pre));
1750:   return(0);
1751: }

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

1758:     Not collective

1760:     Input Parameter:
1761: .   pc      - the preconditioner context

1763:     Output Parameter:
1764: .   S       - the Schur complement matrix

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

1769:     Level: advanced

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

1773: @*/
1774: PetscErrorCode  PCFieldSplitSchurGetS(PC pc,Mat *S)
1775: {
1777:   const char*    t;
1778:   PetscBool      isfs;
1779:   PC_FieldSplit  *jac;

1783:   PetscObjectGetType((PetscObject)pc,&t);
1784:   PetscStrcmp(t,PCFIELDSPLIT,&isfs);
1785:   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PC of type PCFIELDSPLIT, got %s instead",t);
1786:   jac = (PC_FieldSplit*)pc->data;
1787:   if (jac->type != PC_COMPOSITE_SCHUR) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PCFIELDSPLIT of type SCHUR, got %D instead",jac->type);
1788:   if (S) *S = jac->schur;
1789:   return(0);
1790: }

1794: /*@
1795:     PCFieldSplitSchurRestoreS -  restores the MatSchurComplement object used by this PC

1797:     Not collective

1799:     Input Parameters:
1800: +   pc      - the preconditioner context
1801: .   S       - the Schur complement matrix

1803:     Level: advanced

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

1807: @*/
1808: PetscErrorCode  PCFieldSplitSchurRestoreS(PC pc,Mat *S)
1809: {
1811:   const char*    t;
1812:   PetscBool      isfs;
1813:   PC_FieldSplit  *jac;

1817:   PetscObjectGetType((PetscObject)pc,&t);
1818:   PetscStrcmp(t,PCFIELDSPLIT,&isfs);
1819:   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PC of type PCFIELDSPLIT, got %s instead",t);
1820:   jac = (PC_FieldSplit*)pc->data;
1821:   if (jac->type != PC_COMPOSITE_SCHUR) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PCFIELDSPLIT of type SCHUR, got %D instead",jac->type);
1822:   if (!S || *S != jac->schur) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"MatSchurComplement restored is not the same as gotten");
1823:   return(0);
1824: }


1829: static PetscErrorCode  PCFieldSplitSetSchurPre_FieldSplit(PC pc,PCFieldSplitSchurPreType ptype,Mat pre)
1830: {
1831:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;

1835:   jac->schurpre = ptype;
1836:   if (ptype == PC_FIELDSPLIT_SCHUR_PRE_USER && pre) {
1837:     MatDestroy(&jac->schur_user);
1838:     jac->schur_user = pre;
1839:     PetscObjectReference((PetscObject)jac->schur_user);
1840:   }
1841:   return(0);
1842: }

1846: static PetscErrorCode  PCFieldSplitGetSchurPre_FieldSplit(PC pc,PCFieldSplitSchurPreType *ptype,Mat *pre)
1847: {
1848:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;

1851:   *ptype = jac->schurpre;
1852:   *pre   = jac->schur_user;
1853:   return(0);
1854: }

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

1861:     Collective on PC

1863:     Input Parameters:
1864: +   pc  - the preconditioner context
1865: -   ftype - which blocks of factorization to retain, PC_FIELDSPLIT_SCHUR_FACT_FULL is default

1867:     Options Database:
1868: .     -pc_fieldsplit_schur_fact_type <diag,lower,upper,full> default is full


1871:     Level: intermediate

1873:     Notes:
1874:     The FULL factorization is

1876: $   (A   B)  = (1       0) (A   0) (1  Ainv*B)
1877: $   (C   D)    (C*Ainv  1) (0   S) (0     1  )

1879:     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,
1880:     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).

1882:     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
1883:     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
1884:     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,
1885:     the preconditioned operator has three distinct nonzero eigenvalues and minimal polynomial of degree at most 4, so KSPGMRES converges in at most 4 iterations.

1887:     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
1888:     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).

1890:     References:
1891:     Murphy, Golub, and Wathen, A note on preconditioning indefinite linear systems, SIAM J. Sci. Comput., 21 (2000) pp. 1969-1972.

1893:     Ipsen, A note on preconditioning nonsymmetric matrices, SIAM J. Sci. Comput., 23 (2001), pp. 1050-1051.

1895: .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields(), PCFieldSplitSchurPreType
1896: @*/
1897: PetscErrorCode  PCFieldSplitSetSchurFactType(PC pc,PCFieldSplitSchurFactType ftype)
1898: {

1903:   PetscTryMethod(pc,"PCFieldSplitSetSchurFactType_C",(PC,PCFieldSplitSchurFactType),(pc,ftype));
1904:   return(0);
1905: }

1909: static PetscErrorCode PCFieldSplitSetSchurFactType_FieldSplit(PC pc,PCFieldSplitSchurFactType ftype)
1910: {
1911:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;

1914:   jac->schurfactorization = ftype;
1915:   return(0);
1916: }

1920: /*@C
1921:    PCFieldSplitGetSchurBlocks - Gets all matrix blocks for the Schur complement

1923:    Collective on KSP

1925:    Input Parameter:
1926: .  pc - the preconditioner context

1928:    Output Parameters:
1929: +  A00 - the (0,0) block
1930: .  A01 - the (0,1) block
1931: .  A10 - the (1,0) block
1932: -  A11 - the (1,1) block

1934:    Level: advanced

1936: .seealso: PCFIELDSPLIT
1937: @*/
1938: PetscErrorCode  PCFieldSplitGetSchurBlocks(PC pc,Mat *A00,Mat *A01,Mat *A10, Mat *A11)
1939: {
1940:   PC_FieldSplit *jac = (PC_FieldSplit*) pc->data;

1944:   if (jac->type != PC_COMPOSITE_SCHUR) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONG, "FieldSplit is not using a Schur complement approach.");
1945:   if (A00) *A00 = jac->pmat[0];
1946:   if (A01) *A01 = jac->B;
1947:   if (A10) *A10 = jac->C;
1948:   if (A11) *A11 = jac->pmat[1];
1949:   return(0);
1950: }

1954: static PetscErrorCode  PCFieldSplitSetType_FieldSplit(PC pc,PCCompositeType type)
1955: {
1956:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;

1960:   jac->type = type;
1961:   if (type == PC_COMPOSITE_SCHUR) {
1962:     pc->ops->apply = PCApply_FieldSplit_Schur;
1963:     pc->ops->view  = PCView_FieldSplit_Schur;

1965:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit_Schur);
1966:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurPre_C",PCFieldSplitSetSchurPre_FieldSplit);
1967:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSchurPre_C",PCFieldSplitGetSchurPre_FieldSplit);
1968:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C",PCFieldSplitSetSchurFactType_FieldSplit);

1970:   } else {
1971:     pc->ops->apply = PCApply_FieldSplit;
1972:     pc->ops->view  = PCView_FieldSplit;

1974:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit);
1975:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurPre_C",0);
1976:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSchurPre_C",0);
1977:     PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C",0);
1978:   }
1979:   return(0);
1980: }

1984: static PetscErrorCode  PCFieldSplitSetBlockSize_FieldSplit(PC pc,PetscInt bs)
1985: {
1986:   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;

1989:   if (bs < 1) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Blocksize must be positive, you gave %D",bs);
1990:   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);
1991:   jac->bs = bs;
1992:   return(0);
1993: }

1997: /*@
1998:    PCFieldSplitSetType - Sets the type of fieldsplit preconditioner.

2000:    Collective on PC

2002:    Input Parameter:
2003: .  pc - the preconditioner context
2004: .  type - PC_COMPOSITE_ADDITIVE, PC_COMPOSITE_MULTIPLICATIVE (default), PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE, PC_COMPOSITE_SPECIAL, PC_COMPOSITE_SCHUR

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

2009:    Level: Intermediate

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

2013: .seealso: PCCompositeSetType()

2015: @*/
2016: PetscErrorCode  PCFieldSplitSetType(PC pc,PCCompositeType type)
2017: {

2022:   PetscTryMethod(pc,"PCFieldSplitSetType_C",(PC,PCCompositeType),(pc,type));
2023:   return(0);
2024: }

2028: /*@
2029:   PCFieldSplitGetType - Gets the type of fieldsplit preconditioner.

2031:   Not collective

2033:   Input Parameter:
2034: . pc - the preconditioner context

2036:   Output Parameter:
2037: . type - PC_COMPOSITE_ADDITIVE, PC_COMPOSITE_MULTIPLICATIVE (default), PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE, PC_COMPOSITE_SPECIAL, PC_COMPOSITE_SCHUR

2039:   Level: Intermediate

2041: .keywords: PC, set, type, composite preconditioner, additive, multiplicative
2042: .seealso: PCCompositeSetType()
2043: @*/
2044: PetscErrorCode PCFieldSplitGetType(PC pc, PCCompositeType *type)
2045: {
2046:   PC_FieldSplit *jac = (PC_FieldSplit*) pc->data;

2051:   *type = jac->type;
2052:   return(0);
2053: }

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

2060:    Logically Collective

2062:    Input Parameters:
2063: +  pc   - the preconditioner context
2064: -  flg  - boolean indicating whether to use field splits defined by the DM

2066:    Options Database Key:
2067: .  -pc_fieldsplit_dm_splits

2069:    Level: Intermediate

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

2073: .seealso: PCFieldSplitGetDMSplits()

2075: @*/
2076: PetscErrorCode  PCFieldSplitSetDMSplits(PC pc,PetscBool flg)
2077: {
2078:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
2079:   PetscBool      isfs;

2085:   PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
2086:   if (isfs) {
2087:     jac->dm_splits = flg;
2088:   }
2089:   return(0);
2090: }


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

2098:    Logically Collective

2100:    Input Parameter:
2101: .  pc   - the preconditioner context

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

2106:    Level: Intermediate

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

2110: .seealso: PCFieldSplitSetDMSplits()

2112: @*/
2113: PetscErrorCode  PCFieldSplitGetDMSplits(PC pc,PetscBool* flg)
2114: {
2115:   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
2116:   PetscBool      isfs;

2122:   PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
2123:   if (isfs) {
2124:     if(flg) *flg = jac->dm_splits;
2125:   }
2126:   return(0);
2127: }

2129: /* -------------------------------------------------------------------------------------*/
2130: /*MC
2131:    PCFIELDSPLIT - Preconditioner created by combining separate preconditioners for individual
2132:                   fields or groups of fields. See the users manual section "Solving Block Matrices" for more details.

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

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

2140:    Level: intermediate

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

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

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

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

2163: $     For the Schur complement preconditioner if J = ( A00 A01 )
2164: $                                                    ( A10 A11 )
2165: $     the preconditioner using full factorization is
2166: $              ( I   -ksp(A00) A01 ) ( inv(A00)     0  ) (     I          0  )
2167: $              ( 0         I       ) (   0      ksp(S) ) ( -A10 ksp(A00)  I  )
2168:      where the action of inv(A00) is applied using the KSP solver with prefix -fieldsplit_0_.  S is the Schur complement
2169: $              S = A11 - A10 ksp(A00) A01
2170:      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
2171:      in providing the SECOND split or 1 if not give). For PCFieldSplitGetKSP() when field number is 0,
2172:      it returns the KSP associated with -fieldsplit_0_ while field number 1 gives -fieldsplit_1_ KSP. By default
2173:      A11 is used to construct a preconditioner for S, use PCFieldSplitSetSchurPre() to turn on or off this
2174:      option. You can use the preconditioner PCLSC to precondition the Schur complement with -fieldsplit_1_pc_type lsc.
2175:      When option -fieldsplit_schur_precondition selfp is given, an approximation to S is assembled --
2176:      Sp = A11 - A10 inv(diag(A00)) A01, which has type AIJ and can be used with a variety of preconditioners
2177:      (e.g., -fieldsplit_1_pc_type asm). Optionally, A00 can be lumped before extracting the diagonal:
2178:      -fieldsplit_1_mat_schur_complement_ainv_type lump; diag is the default.

2180:      The factorization type is set using -pc_fieldsplit_schur_fact_type <diag, lower, upper, full>. The full is shown above,
2181:      diag gives
2182: $              ( inv(A00)     0   )
2183: $              (   0      -ksp(S) )
2184:      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
2185: $              (  A00   0 )
2186: $              (  A10   S )
2187:      where the inverses of A00 and S are applied using KSPs. The upper factorization is the inverse of
2188: $              ( A00 A01 )
2189: $              (  0   S  )
2190:      where again the inverses of A00 and S are applied using KSPs.

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

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

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

2202:    Concepts: physics based preconditioners, block preconditioners

2204:    There is a nice discussion of block preconditioners in

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

2210: .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC, Block_Preconditioners, PCLSC,
2211:            PCFieldSplitGetSubKSP(), PCFieldSplitSetFields(), PCFieldSplitSetType(), PCFieldSplitSetIS(), PCFieldSplitSetSchurPre(),
2212:            MatSchurComplementSetAinvType()
2213: M*/

2217: PETSC_EXTERN PetscErrorCode PCCreate_FieldSplit(PC pc)
2218: {
2220:   PC_FieldSplit  *jac;

2223:   PetscNewLog(pc,&jac);

2225:   jac->bs                 = -1;
2226:   jac->nsplits            = 0;
2227:   jac->type               = PC_COMPOSITE_MULTIPLICATIVE;
2228:   jac->schurpre           = PC_FIELDSPLIT_SCHUR_PRE_USER; /* Try user preconditioner first, fall back on diagonal */
2229:   jac->schurfactorization = PC_FIELDSPLIT_SCHUR_FACT_FULL;
2230:   jac->dm_splits          = PETSC_TRUE;

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

2234:   pc->ops->apply           = PCApply_FieldSplit;
2235:   pc->ops->applytranspose  = PCApplyTranspose_FieldSplit;
2236:   pc->ops->setup           = PCSetUp_FieldSplit;
2237:   pc->ops->reset           = PCReset_FieldSplit;
2238:   pc->ops->destroy         = PCDestroy_FieldSplit;
2239:   pc->ops->setfromoptions  = PCSetFromOptions_FieldSplit;
2240:   pc->ops->view            = PCView_FieldSplit;
2241:   pc->ops->applyrichardson = 0;

2243:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit);
2244:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetFields_C",PCFieldSplitSetFields_FieldSplit);
2245:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetIS_C",PCFieldSplitSetIS_FieldSplit);
2246:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetType_C",PCFieldSplitSetType_FieldSplit);
2247:   PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetBlockSize_C",PCFieldSplitSetBlockSize_FieldSplit);
2248:   return(0);
2249: }