Actual source code: fieldsplit.c
petsc-dev 2014-02-02
2: #include <petsc-private/pcimpl.h> /*I "petscpc.h" I*/
3: #include <petscdm.h>
5: /*
6: There is a nice discussion of block preconditioners in
8: [El08] A taxonomy and comparison of parallel block multi-level preconditioners for the incompressible Navier-Stokes equations
9: Howard Elman, V.E. Howle, John Shadid, Robert Shuttleworth, Ray Tuminaro, Journal of Computational Physics 227 (2008) 1790--1808
10: http://chess.cs.umd.edu/~elman/papers/tax.pdf
11: */
13: const char *const PCFieldSplitSchurPreTypes[] = {"SELF","A11","USER","PCFieldSplitSchurPreType","PC_FIELDSPLIT_SCHUR_PRE_",0};
14: const char *const PCFieldSplitSchurFactTypes[] = {"DIAG","LOWER","UPPER","FULL","PCFieldSplitSchurFactType","PC_FIELDSPLIT_SCHUR_FACT_",0};
16: typedef struct _PC_FieldSplitLink *PC_FieldSplitLink;
17: struct _PC_FieldSplitLink {
18: KSP ksp;
19: Vec x,y,z;
20: char *splitname;
21: PetscInt nfields;
22: PetscInt *fields,*fields_col;
23: VecScatter sctx;
24: IS is,is_col;
25: PC_FieldSplitLink next,previous;
26: };
28: typedef struct {
29: PCCompositeType type;
30: PetscBool defaultsplit; /* Flag for a system with a set of 'k' scalar fields with the same layout (and bs = k) */
31: PetscBool splitdefined; /* Flag is set after the splits have been defined, to prevent more splits from being added */
32: PetscInt bs; /* Block size for IS and Mat structures */
33: PetscInt nsplits; /* Number of field divisions defined */
34: Vec *x,*y,w1,w2;
35: Mat *mat; /* The diagonal block for each split */
36: Mat *pmat; /* The preconditioning diagonal block for each split */
37: Mat *Afield; /* The rows of the matrix associated with each split */
38: PetscBool issetup;
40: /* Only used when Schur complement preconditioning is used */
41: Mat B; /* The (0,1) block */
42: Mat C; /* The (1,0) block */
43: Mat schur; /* The Schur complement S = A11 - A10 A00^{-1} A01, the KSP here, kspinner, is H_1 in [El08] */
44: Mat schur_user; /* User-provided preconditioning matrix for the Schur complement */
45: PCFieldSplitSchurPreType schurpre; /* Determines which preconditioning matrix is used for the Schur complement */
46: PCFieldSplitSchurFactType schurfactorization;
47: KSP kspschur; /* The solver for S */
48: KSP kspupper; /* The solver for A in the upper diagonal part of the factorization (H_2 in [El08]) */
49: PC_FieldSplitLink head;
50: PetscBool reset; /* indicates PCReset() has been last called on this object, hack */
51: PetscBool suboptionsset; /* Indicates that the KSPSetFromOptions() has been called on the sub-KSPs */
52: PetscBool dm_splits; /* Whether to use DMCreateFieldDecomposition() whenever possible */
53: } PC_FieldSplit;
55: /*
56: Notes: there is no particular reason that pmat, x, and y are stored as arrays in PC_FieldSplit instead of
57: inside PC_FieldSplitLink, just historical. If you want to be able to add new fields after already using the
58: PC you could change this.
59: */
61: /* This helper is so that setting a user-provided preconditioning matrix is orthogonal to choosing to use it. This way the
62: * application-provided FormJacobian can provide this matrix without interfering with the user's (command-line) choices. */
63: static Mat FieldSplitSchurPre(PC_FieldSplit *jac)
64: {
65: switch (jac->schurpre) {
66: case PC_FIELDSPLIT_SCHUR_PRE_SELF: return jac->schur;
67: case PC_FIELDSPLIT_SCHUR_PRE_A11: return jac->pmat[1];
68: case PC_FIELDSPLIT_SCHUR_PRE_USER: /* Use a user-provided matrix if it is given, otherwise diagonal block */
69: default:
70: return jac->schur_user ? jac->schur_user : jac->pmat[1];
71: }
72: }
75: #include <petscdraw.h>
78: static PetscErrorCode PCView_FieldSplit(PC pc,PetscViewer viewer)
79: {
80: PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
81: PetscErrorCode ierr;
82: PetscBool iascii,isdraw;
83: PetscInt i,j;
84: PC_FieldSplitLink ilink = jac->head;
87: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
88: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
89: if (iascii) {
90: if (jac->bs > 0) {
91: PetscViewerASCIIPrintf(viewer," FieldSplit with %s composition: total splits = %D, blocksize = %D\n",PCCompositeTypes[jac->type],jac->nsplits,jac->bs);
92: } else {
93: PetscViewerASCIIPrintf(viewer," FieldSplit with %s composition: total splits = %D\n",PCCompositeTypes[jac->type],jac->nsplits);
94: }
95: if (pc->useAmat) {
96: PetscViewerASCIIPrintf(viewer," using Amat (not Pmat) as operator for blocks\n");
97: }
98: PetscViewerASCIIPrintf(viewer," Solver info for each split is in the following KSP objects:\n");
99: PetscViewerASCIIPushTab(viewer);
100: for (i=0; i<jac->nsplits; i++) {
101: if (ilink->fields) {
102: PetscViewerASCIIPrintf(viewer,"Split number %D Fields ",i);
103: PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
104: for (j=0; j<ilink->nfields; j++) {
105: if (j > 0) {
106: PetscViewerASCIIPrintf(viewer,",");
107: }
108: PetscViewerASCIIPrintf(viewer," %D",ilink->fields[j]);
109: }
110: PetscViewerASCIIPrintf(viewer,"\n");
111: PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
112: } else {
113: PetscViewerASCIIPrintf(viewer,"Split number %D Defined by IS\n",i);
114: }
115: KSPView(ilink->ksp,viewer);
116: ilink = ilink->next;
117: }
118: PetscViewerASCIIPopTab(viewer);
119: }
121: if (isdraw) {
122: PetscDraw draw;
123: PetscReal x,y,w,wd;
125: PetscViewerDrawGetDraw(viewer,0,&draw);
126: PetscDrawGetCurrentPoint(draw,&x,&y);
127: w = 2*PetscMin(1.0 - x,x);
128: wd = w/(jac->nsplits + 1);
129: x = x - wd*(jac->nsplits-1)/2.0;
130: for (i=0; i<jac->nsplits; i++) {
131: PetscDrawPushCurrentPoint(draw,x,y);
132: KSPView(ilink->ksp,viewer);
133: PetscDrawPopCurrentPoint(draw);
134: x += wd;
135: ilink = ilink->next;
136: }
137: }
138: return(0);
139: }
143: static PetscErrorCode PCView_FieldSplit_Schur(PC pc,PetscViewer viewer)
144: {
145: PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
146: PetscErrorCode ierr;
147: PetscBool iascii,isdraw;
148: PetscInt i,j;
149: PC_FieldSplitLink ilink = jac->head;
152: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
153: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
154: if (iascii) {
155: if (jac->bs > 0) {
156: PetscViewerASCIIPrintf(viewer," FieldSplit with Schur preconditioner, blocksize = %D, factorization %s\n",jac->bs,PCFieldSplitSchurFactTypes[jac->schurfactorization]);
157: } else {
158: PetscViewerASCIIPrintf(viewer," FieldSplit with Schur preconditioner, factorization %s\n",PCFieldSplitSchurFactTypes[jac->schurfactorization]);
159: }
160: if (pc->useAmat) {
161: PetscViewerASCIIPrintf(viewer," using Amat (not Pmat) as operator for blocks\n");
162: }
163: switch (jac->schurpre) {
164: case PC_FIELDSPLIT_SCHUR_PRE_SELF:
165: PetscViewerASCIIPrintf(viewer," Preconditioner for the Schur complement formed from S itself\n");break;
166: case PC_FIELDSPLIT_SCHUR_PRE_A11:
167: PetscViewerASCIIPrintf(viewer," Preconditioner for the Schur complement formed from A11\n");break;
168: case PC_FIELDSPLIT_SCHUR_PRE_USER:
169: if (jac->schur_user) {
170: PetscViewerASCIIPrintf(viewer," Preconditioner for the Schur complement formed from user provided matrix\n");
171: } else {
172: PetscViewerASCIIPrintf(viewer," Preconditioner for the Schur complement formed from A11\n");
173: }
174: break;
175: default:
176: SETERRQ1(PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Invalid Schur preconditioning type: %d", jac->schurpre);
177: }
178: PetscViewerASCIIPrintf(viewer," Split info:\n");
179: PetscViewerASCIIPushTab(viewer);
180: for (i=0; i<jac->nsplits; i++) {
181: if (ilink->fields) {
182: PetscViewerASCIIPrintf(viewer,"Split number %D Fields ",i);
183: PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
184: for (j=0; j<ilink->nfields; j++) {
185: if (j > 0) {
186: PetscViewerASCIIPrintf(viewer,",");
187: }
188: PetscViewerASCIIPrintf(viewer," %D",ilink->fields[j]);
189: }
190: PetscViewerASCIIPrintf(viewer,"\n");
191: PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
192: } else {
193: PetscViewerASCIIPrintf(viewer,"Split number %D Defined by IS\n",i);
194: }
195: ilink = ilink->next;
196: }
197: PetscViewerASCIIPrintf(viewer,"KSP solver for A00 block\n");
198: PetscViewerASCIIPushTab(viewer);
199: if (jac->head) {
200: KSPView(jac->head->ksp,viewer);
201: } else {PetscViewerASCIIPrintf(viewer," not yet available\n");}
202: PetscViewerASCIIPopTab(viewer);
203: if (jac->head && jac->kspupper != jac->head->ksp) {
204: PetscViewerASCIIPrintf(viewer,"KSP solver for upper A00 in upper triangular factor \n");
205: PetscViewerASCIIPushTab(viewer);
206: if (jac->kspupper) {KSPView(jac->kspupper,viewer);}
207: else {PetscViewerASCIIPrintf(viewer," not yet available\n");}
208: PetscViewerASCIIPopTab(viewer);
209: }
210: PetscViewerASCIIPrintf(viewer,"KSP solver for S = A11 - A10 inv(A00) A01 \n");
211: PetscViewerASCIIPushTab(viewer);
212: if (jac->kspschur) {
213: KSPView(jac->kspschur,viewer);
214: } else {
215: PetscViewerASCIIPrintf(viewer," not yet available\n");
216: }
217: PetscViewerASCIIPopTab(viewer);
218: PetscViewerASCIIPopTab(viewer);
219: } else if (isdraw && jac->head) {
220: PetscDraw draw;
221: PetscReal x,y,w,wd,h;
222: PetscInt cnt = 2;
223: char str[32];
225: PetscViewerDrawGetDraw(viewer,0,&draw);
226: PetscDrawGetCurrentPoint(draw,&x,&y);
227: if (jac->kspupper != jac->head->ksp) cnt++;
228: w = 2*PetscMin(1.0 - x,x);
229: wd = w/(cnt + 1);
231: PetscSNPrintf(str,32,"Schur fact. %s",PCFieldSplitSchurFactTypes[jac->schurfactorization]);
232: PetscDrawBoxedString(draw,x,y,PETSC_DRAW_RED,PETSC_DRAW_BLACK,str,NULL,&h);
233: y -= h;
234: if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_USER && !jac->schur_user) {
235: PetscSNPrintf(str,32,"Prec. for Schur from %s",PCFieldSplitSchurPreTypes[PC_FIELDSPLIT_SCHUR_PRE_A11]);
236: } else {
237: PetscSNPrintf(str,32,"Prec. for Schur from %s",PCFieldSplitSchurPreTypes[jac->schurpre]);
238: }
239: PetscDrawBoxedString(draw,x+wd*(cnt-1)/2.0,y,PETSC_DRAW_RED,PETSC_DRAW_BLACK,str,NULL,&h);
240: y -= h;
241: x = x - wd*(cnt-1)/2.0;
243: PetscDrawPushCurrentPoint(draw,x,y);
244: KSPView(jac->head->ksp,viewer);
245: PetscDrawPopCurrentPoint(draw);
246: if (jac->kspupper != jac->head->ksp) {
247: x += wd;
248: PetscDrawPushCurrentPoint(draw,x,y);
249: KSPView(jac->kspupper,viewer);
250: PetscDrawPopCurrentPoint(draw);
251: }
252: x += wd;
253: PetscDrawPushCurrentPoint(draw,x,y);
254: KSPView(jac->kspschur,viewer);
255: PetscDrawPopCurrentPoint(draw);
256: }
257: return(0);
258: }
262: /* Precondition: jac->bs is set to a meaningful value */
263: static PetscErrorCode PCFieldSplitSetRuntimeSplits_Private(PC pc)
264: {
266: PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
267: PetscInt i,nfields,*ifields,nfields_col,*ifields_col;
268: PetscBool flg,flg_col;
269: char optionname[128],splitname[8],optionname_col[128];
272: PetscMalloc1(jac->bs,&ifields);
273: PetscMalloc1(jac->bs,&ifields_col);
274: for (i=0,flg=PETSC_TRUE;; i++) {
275: PetscSNPrintf(splitname,sizeof(splitname),"%D",i);
276: PetscSNPrintf(optionname,sizeof(optionname),"-pc_fieldsplit_%D_fields",i);
277: PetscSNPrintf(optionname_col,sizeof(optionname_col),"-pc_fieldsplit_%D_fields_col",i);
278: nfields = jac->bs;
279: nfields_col = jac->bs;
280: PetscOptionsGetIntArray(((PetscObject)pc)->prefix,optionname,ifields,&nfields,&flg);
281: PetscOptionsGetIntArray(((PetscObject)pc)->prefix,optionname_col,ifields_col,&nfields_col,&flg_col);
282: if (!flg) break;
283: else if (flg && !flg_col) {
284: if (!nfields) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Cannot list zero fields");
285: PCFieldSplitSetFields(pc,splitname,nfields,ifields,ifields);
286: } else {
287: if (!nfields || !nfields_col) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Cannot list zero fields");
288: if (nfields != nfields_col) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Number of row and column fields must match");
289: PCFieldSplitSetFields(pc,splitname,nfields,ifields,ifields_col);
290: }
291: }
292: if (i > 0) {
293: /* Makes command-line setting of splits take precedence over setting them in code.
294: Otherwise subsequent calls to PCFieldSplitSetIS() or PCFieldSplitSetFields() would
295: create new splits, which would probably not be what the user wanted. */
296: jac->splitdefined = PETSC_TRUE;
297: }
298: PetscFree(ifields);
299: PetscFree(ifields_col);
300: return(0);
301: }
305: static PetscErrorCode PCFieldSplitSetDefaults(PC pc)
306: {
307: PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
308: PetscErrorCode ierr;
309: PC_FieldSplitLink ilink = jac->head;
310: PetscBool fieldsplit_default = PETSC_FALSE,stokes = PETSC_FALSE;
311: PetscInt i;
314: /*
315: Kinda messy, but at least this now uses DMCreateFieldDecomposition() even with jac->reset.
316: Should probably be rewritten.
317: */
318: if (!ilink || jac->reset) {
319: PetscOptionsGetBool(((PetscObject)pc)->prefix,"-pc_fieldsplit_detect_saddle_point",&stokes,NULL);
320: if (pc->dm && jac->dm_splits && !stokes) {
321: PetscInt numFields, f, i, j;
322: char **fieldNames;
323: IS *fields;
324: DM *dms;
325: DM subdm[128];
326: PetscBool flg;
328: DMCreateFieldDecomposition(pc->dm, &numFields, &fieldNames, &fields, &dms);
329: /* Allow the user to prescribe the splits */
330: for (i = 0, flg = PETSC_TRUE;; i++) {
331: PetscInt ifields[128];
332: IS compField;
333: char optionname[128], splitname[8];
334: PetscInt nfields = numFields;
336: PetscSNPrintf(optionname, sizeof(optionname), "-pc_fieldsplit_%D_fields", i);
337: PetscOptionsGetIntArray(((PetscObject) pc)->prefix, optionname, ifields, &nfields, &flg);
338: if (!flg) break;
339: if (numFields > 128) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Cannot currently support %d > 128 fields", numFields);
340: DMCreateSubDM(pc->dm, nfields, ifields, &compField, &subdm[i]);
341: if (nfields == 1) {
342: PCFieldSplitSetIS(pc, fieldNames[ifields[0]], compField);
343: /* PetscPrintf(PetscObjectComm((PetscObject)pc), "%s Field Indices:", fieldNames[ifields[0]]);
344: ISView(compField, NULL); */
345: } else {
346: PetscSNPrintf(splitname, sizeof(splitname), "%D", i);
347: PCFieldSplitSetIS(pc, splitname, compField);
348: /* PetscPrintf(PetscObjectComm((PetscObject)pc), "%s Field Indices:", splitname);
349: ISView(compField, NULL); */
350: }
351: ISDestroy(&compField);
352: for (j = 0; j < nfields; ++j) {
353: f = ifields[j];
354: PetscFree(fieldNames[f]);
355: ISDestroy(&fields[f]);
356: }
357: }
358: if (i == 0) {
359: for (f = 0; f < numFields; ++f) {
360: PCFieldSplitSetIS(pc, fieldNames[f], fields[f]);
361: PetscFree(fieldNames[f]);
362: ISDestroy(&fields[f]);
363: }
364: } else {
365: for (j=0; j<numFields; j++) {
366: DMDestroy(dms+j);
367: }
368: PetscFree(dms);
369: PetscMalloc1(i, &dms);
370: for (j = 0; j < i; ++j) dms[j] = subdm[j];
371: }
372: PetscFree(fieldNames);
373: PetscFree(fields);
374: if (dms) {
375: PetscInfo(pc, "Setting up physics based fieldsplit preconditioner using the embedded DM\n");
376: for (ilink = jac->head, i = 0; ilink; ilink = ilink->next, ++i) {
377: const char *prefix;
378: PetscObjectGetOptionsPrefix((PetscObject)(ilink->ksp),&prefix);
379: PetscObjectSetOptionsPrefix((PetscObject)(dms[i]), prefix);
380: KSPSetDM(ilink->ksp, dms[i]);
381: KSPSetDMActive(ilink->ksp, PETSC_FALSE);
382: PetscObjectIncrementTabLevel((PetscObject)dms[i],(PetscObject)ilink->ksp,0);
383: DMDestroy(&dms[i]);
384: }
385: PetscFree(dms);
386: }
387: } else {
388: if (jac->bs <= 0) {
389: if (pc->pmat) {
390: MatGetBlockSize(pc->pmat,&jac->bs);
391: } else jac->bs = 1;
392: }
394: if (stokes) {
395: IS zerodiags,rest;
396: PetscInt nmin,nmax;
398: MatGetOwnershipRange(pc->mat,&nmin,&nmax);
399: MatFindZeroDiagonals(pc->mat,&zerodiags);
400: ISComplement(zerodiags,nmin,nmax,&rest);
401: if (jac->reset) {
402: jac->head->is = rest;
403: jac->head->next->is = zerodiags;
404: } else {
405: PCFieldSplitSetIS(pc,"0",rest);
406: PCFieldSplitSetIS(pc,"1",zerodiags);
407: }
408: ISDestroy(&zerodiags);
409: ISDestroy(&rest);
410: } else {
411: if (jac->reset) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Cases not yet handled when PCReset() was used");
412: PetscOptionsGetBool(((PetscObject)pc)->prefix,"-pc_fieldsplit_default",&fieldsplit_default,NULL);
413: if (!fieldsplit_default) {
414: /* Allow user to set fields from command line, if bs was known at the time of PCSetFromOptions_FieldSplit()
415: then it is set there. This is not ideal because we should only have options set in XXSetFromOptions(). */
416: PCFieldSplitSetRuntimeSplits_Private(pc);
417: if (jac->splitdefined) {PetscInfo(pc,"Splits defined using the options database\n");}
418: }
419: if (fieldsplit_default || !jac->splitdefined) {
420: PetscInfo(pc,"Using default splitting of fields\n");
421: for (i=0; i<jac->bs; i++) {
422: char splitname[8];
423: PetscSNPrintf(splitname,sizeof(splitname),"%D",i);
424: PCFieldSplitSetFields(pc,splitname,1,&i,&i);
425: }
426: jac->defaultsplit = PETSC_TRUE;
427: }
428: }
429: }
430: } else if (jac->nsplits == 1) {
431: if (ilink->is) {
432: IS is2;
433: PetscInt nmin,nmax;
435: MatGetOwnershipRange(pc->mat,&nmin,&nmax);
436: ISComplement(ilink->is,nmin,nmax,&is2);
437: PCFieldSplitSetIS(pc,"1",is2);
438: ISDestroy(&is2);
439: } else SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Must provide at least two sets of fields to PCFieldSplit()");
440: }
443: if (jac->nsplits < 2) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_PLIB,"Unhandled case, must have at least two fields, not %d", jac->nsplits);
444: return(0);
445: }
447: PETSC_EXTERN PetscErrorCode PetscOptionsFindPairPrefix_Private(const char pre[], const char name[], char *value[], PetscBool *flg);
451: static PetscErrorCode PCSetUp_FieldSplit(PC pc)
452: {
453: PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
454: PetscErrorCode ierr;
455: PC_FieldSplitLink ilink;
456: PetscInt i,nsplit;
457: MatStructure flag = pc->flag;
458: PetscBool sorted, sorted_col;
461: PCFieldSplitSetDefaults(pc);
462: nsplit = jac->nsplits;
463: ilink = jac->head;
465: /* get the matrices for each split */
466: if (!jac->issetup) {
467: PetscInt rstart,rend,nslots,bs;
469: jac->issetup = PETSC_TRUE;
471: /* This is done here instead of in PCFieldSplitSetFields() because may not have matrix at that point */
472: if (jac->defaultsplit || !ilink->is) {
473: if (jac->bs <= 0) jac->bs = nsplit;
474: }
475: bs = jac->bs;
476: MatGetOwnershipRange(pc->pmat,&rstart,&rend);
477: nslots = (rend - rstart)/bs;
478: for (i=0; i<nsplit; i++) {
479: if (jac->defaultsplit) {
480: ISCreateStride(PetscObjectComm((PetscObject)pc),nslots,rstart+i,nsplit,&ilink->is);
481: ISDuplicate(ilink->is,&ilink->is_col);
482: } else if (!ilink->is) {
483: if (ilink->nfields > 1) {
484: PetscInt *ii,*jj,j,k,nfields = ilink->nfields,*fields = ilink->fields,*fields_col = ilink->fields_col;
485: PetscMalloc1(ilink->nfields*nslots,&ii);
486: PetscMalloc1(ilink->nfields*nslots,&jj);
487: for (j=0; j<nslots; j++) {
488: for (k=0; k<nfields; k++) {
489: ii[nfields*j + k] = rstart + bs*j + fields[k];
490: jj[nfields*j + k] = rstart + bs*j + fields_col[k];
491: }
492: }
493: ISCreateGeneral(PetscObjectComm((PetscObject)pc),nslots*nfields,ii,PETSC_OWN_POINTER,&ilink->is);
494: ISCreateGeneral(PetscObjectComm((PetscObject)pc),nslots*nfields,jj,PETSC_OWN_POINTER,&ilink->is_col);
495: } else {
496: ISCreateStride(PetscObjectComm((PetscObject)pc),nslots,rstart+ilink->fields[0],bs,&ilink->is);
497: ISCreateStride(PetscObjectComm((PetscObject)pc),nslots,rstart+ilink->fields_col[0],bs,&ilink->is_col);
498: }
499: }
500: ISSorted(ilink->is,&sorted);
501: if (ilink->is_col) { ISSorted(ilink->is_col,&sorted_col); }
502: if (!sorted || !sorted_col) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Fields must be sorted when creating split");
503: ilink = ilink->next;
504: }
505: }
507: ilink = jac->head;
508: if (!jac->pmat) {
509: Vec xtmp;
511: MatGetVecs(pc->pmat,&xtmp,NULL);
512: PetscMalloc1(nsplit,&jac->pmat);
513: PetscMalloc2(nsplit,&jac->x,nsplit,&jac->y);
514: for (i=0; i<nsplit; i++) {
515: MatNullSpace sp;
517: /* Check for preconditioning matrix attached to IS */
518: PetscObjectQuery((PetscObject) ilink->is, "pmat", (PetscObject*) &jac->pmat[i]);
519: if (jac->pmat[i]) {
520: PetscObjectReference((PetscObject) jac->pmat[i]);
521: if (jac->type == PC_COMPOSITE_SCHUR) {
522: jac->schur_user = jac->pmat[i];
524: PetscObjectReference((PetscObject) jac->schur_user);
525: }
526: } else {
527: MatGetSubMatrix(pc->pmat,ilink->is,ilink->is_col,MAT_INITIAL_MATRIX,&jac->pmat[i]);
528: }
529: /* create work vectors for each split */
530: MatGetVecs(jac->pmat[i],&jac->x[i],&jac->y[i]);
531: ilink->x = jac->x[i]; ilink->y = jac->y[i]; ilink->z = NULL;
532: /* compute scatter contexts needed by multiplicative versions and non-default splits */
533: VecScatterCreate(xtmp,ilink->is,jac->x[i],NULL,&ilink->sctx);
534: /* Check for null space attached to IS */
535: PetscObjectQuery((PetscObject) ilink->is, "nullspace", (PetscObject*) &sp);
536: if (sp) {
537: MatSetNullSpace(jac->pmat[i], sp);
538: }
539: PetscObjectQuery((PetscObject) ilink->is, "nearnullspace", (PetscObject*) &sp);
540: if (sp) {
541: MatSetNearNullSpace(jac->pmat[i], sp);
542: }
543: ilink = ilink->next;
544: }
545: VecDestroy(&xtmp);
546: } else {
547: for (i=0; i<nsplit; i++) {
548: Mat pmat;
550: /* Check for preconditioning matrix attached to IS */
551: PetscObjectQuery((PetscObject) ilink->is, "pmat", (PetscObject*) &pmat);
552: if (!pmat) {
553: MatGetSubMatrix(pc->pmat,ilink->is,ilink->is_col,MAT_REUSE_MATRIX,&jac->pmat[i]);
554: }
555: ilink = ilink->next;
556: }
557: }
558: if (pc->useAmat) {
559: ilink = jac->head;
560: if (!jac->mat) {
561: PetscMalloc1(nsplit,&jac->mat);
562: for (i=0; i<nsplit; i++) {
563: MatGetSubMatrix(pc->mat,ilink->is,ilink->is_col,MAT_INITIAL_MATRIX,&jac->mat[i]);
564: ilink = ilink->next;
565: }
566: } else {
567: for (i=0; i<nsplit; i++) {
568: if (jac->mat[i]) {MatGetSubMatrix(pc->mat,ilink->is,ilink->is_col,MAT_REUSE_MATRIX,&jac->mat[i]);}
569: ilink = ilink->next;
570: }
571: }
572: } else {
573: jac->mat = jac->pmat;
574: }
576: if (jac->type != PC_COMPOSITE_ADDITIVE && jac->type != PC_COMPOSITE_SCHUR) {
577: /* extract the rows of the matrix associated with each field: used for efficient computation of residual inside algorithm */
578: ilink = jac->head;
579: if (!jac->Afield) {
580: PetscMalloc1(nsplit,&jac->Afield);
581: for (i=0; i<nsplit; i++) {
582: MatGetSubMatrix(pc->mat,ilink->is,NULL,MAT_INITIAL_MATRIX,&jac->Afield[i]);
583: ilink = ilink->next;
584: }
585: } else {
586: for (i=0; i<nsplit; i++) {
587: MatGetSubMatrix(pc->mat,ilink->is,NULL,MAT_REUSE_MATRIX,&jac->Afield[i]);
588: ilink = ilink->next;
589: }
590: }
591: }
593: if (jac->type == PC_COMPOSITE_SCHUR) {
594: IS ccis;
595: PetscInt rstart,rend;
596: char lscname[256];
597: PetscObject LSC_L;
599: if (nsplit != 2) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_INCOMP,"To use Schur complement preconditioner you must have exactly 2 fields");
601: /* When extracting off-diagonal submatrices, we take complements from this range */
602: MatGetOwnershipRangeColumn(pc->mat,&rstart,&rend);
604: /* need to handle case when one is resetting up the preconditioner */
605: if (jac->schur) {
606: KSP kspA = jac->head->ksp, kspInner = NULL, kspUpper = jac->kspupper;
608: MatSchurComplementGetKSP(jac->schur, &kspInner);
609: ilink = jac->head;
610: ISComplement(ilink->is_col,rstart,rend,&ccis);
611: MatGetSubMatrix(pc->mat,ilink->is,ccis,MAT_REUSE_MATRIX,&jac->B);
612: ISDestroy(&ccis);
613: ilink = ilink->next;
614: ISComplement(ilink->is_col,rstart,rend,&ccis);
615: MatGetSubMatrix(pc->mat,ilink->is,ccis,MAT_REUSE_MATRIX,&jac->C);
616: ISDestroy(&ccis);
617: MatSchurComplementUpdate(jac->schur,jac->mat[0],jac->pmat[0],jac->B,jac->C,jac->mat[1],pc->flag);
618: if (kspA != kspInner) {
619: KSPSetOperators(kspA,jac->mat[0],jac->pmat[0],pc->flag);
620: }
621: if (kspUpper != kspA) {
622: KSPSetOperators(kspUpper,jac->mat[0],jac->pmat[0],pc->flag);
623: }
624: KSPSetOperators(jac->kspschur,jac->schur,FieldSplitSchurPre(jac),pc->flag);
625: } else {
626: const char *Dprefix;
627: char schurprefix[256];
628: char schurtestoption[256];
629: MatNullSpace sp;
630: PetscBool flg;
632: /* extract the A01 and A10 matrices */
633: ilink = jac->head;
634: ISComplement(ilink->is_col,rstart,rend,&ccis);
635: MatGetSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->B);
636: ISDestroy(&ccis);
637: ilink = ilink->next;
638: ISComplement(ilink->is_col,rstart,rend,&ccis);
639: MatGetSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->C);
640: ISDestroy(&ccis);
642: /* Use mat[0] (diagonal block of Amat) preconditioned by pmat[0] to define Schur complement */
643: MatCreate(((PetscObject)jac->mat[0])->comm,&jac->schur);
644: MatSetType(jac->schur,MATSCHURCOMPLEMENT);
645: MatSchurComplementSet(jac->schur,jac->mat[0],jac->pmat[0],jac->B,jac->C,jac->mat[1]);
647: MatGetNullSpace(jac->pmat[1], &sp);
648: if (sp) {
649: MatSetNullSpace(jac->schur, sp);
650: }
652: PetscSNPrintf(schurtestoption, sizeof(schurtestoption), "-fieldsplit_%s_inner_", ilink->splitname);
653: PetscOptionsFindPairPrefix_Private(((PetscObject)pc)->prefix, schurtestoption, NULL, &flg);
654: if (flg) {
655: DM dmInner;
656: KSP kspInner;
658: MatSchurComplementGetKSP(jac->schur, &kspInner);
659: PetscSNPrintf(schurprefix, sizeof(schurprefix), "%sfieldsplit_%s_inner_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname);
660: /* Indent this deeper to emphasize the "inner" nature of this solver. */
661: PetscObjectIncrementTabLevel((PetscObject)kspInner, (PetscObject) pc, 2);
662: KSPSetOptionsPrefix(kspInner, schurprefix);
664: /* Set DM for new solver */
665: KSPGetDM(jac->head->ksp, &dmInner);
666: KSPSetDM(kspInner, dmInner);
667: KSPSetDMActive(kspInner, PETSC_FALSE);
668: } else {
669: /* Use the outer solver for the inner solve, but revert the KSPPREONLY from PCFieldSplitSetFields_FieldSplit or
670: * PCFieldSplitSetIS_FieldSplit. We don't want KSPPREONLY because it makes the Schur complement inexact,
671: * preventing Schur complement reduction to be an accurate solve. Usually when an iterative solver is used for
672: * S = D - C A_inner^{-1} B, we expect S to be defined using an accurate definition of A_inner^{-1}, so we make
673: * GMRES the default. Note that it is also common to use PREONLY for S, in which case S may not be used
674: * directly, and the user is responsible for setting an inexact method for fieldsplit's A^{-1}. */
675: KSPSetType(jac->head->ksp,KSPGMRES);
676: MatSchurComplementSetKSP(jac->schur,jac->head->ksp);
677: }
678: KSPSetOperators(jac->head->ksp,jac->mat[0],jac->pmat[0],flag);
679: KSPSetFromOptions(jac->head->ksp);
680: MatSetFromOptions(jac->schur);
682: PetscSNPrintf(schurtestoption, sizeof(schurtestoption), "-fieldsplit_%s_upper_", ilink->splitname);
683: PetscOptionsFindPairPrefix_Private(((PetscObject)pc)->prefix, schurtestoption, NULL, &flg);
684: if (flg) {
685: DM dmInner;
687: PetscSNPrintf(schurprefix, sizeof(schurprefix), "%sfieldsplit_%s_upper_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname);
688: KSPCreate(PetscObjectComm((PetscObject)pc), &jac->kspupper);
689: KSPSetOptionsPrefix(jac->kspupper, schurprefix);
690: KSPGetDM(jac->head->ksp, &dmInner);
691: KSPSetDM(jac->kspupper, dmInner);
692: KSPSetDMActive(jac->kspupper, PETSC_FALSE);
693: KSPSetFromOptions(jac->kspupper);
694: KSPSetOperators(jac->kspupper,jac->mat[0],jac->pmat[0],flag);
695: VecDuplicate(jac->head->x, &jac->head->z);
696: } else {
697: jac->kspupper = jac->head->ksp;
698: PetscObjectReference((PetscObject) jac->head->ksp);
699: }
701: KSPCreate(PetscObjectComm((PetscObject)pc),&jac->kspschur);
702: PetscLogObjectParent((PetscObject)pc,(PetscObject)jac->kspschur);
703: PetscObjectIncrementTabLevel((PetscObject)jac->kspschur,(PetscObject)pc,1);
704: KSPSetOperators(jac->kspschur,jac->schur,FieldSplitSchurPre(jac),DIFFERENT_NONZERO_PATTERN);
705: if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELF) {
706: PC pcschur;
707: KSPGetPC(jac->kspschur,&pcschur);
708: PCSetType(pcschur,PCNONE);
709: /* Note: This is bad if there exist preconditioners for MATSCHURCOMPLEMENT */
710: }
711: KSPGetOptionsPrefix(jac->head->next->ksp, &Dprefix);
712: KSPSetOptionsPrefix(jac->kspschur, Dprefix);
713: /* propogate DM */
714: {
715: DM sdm;
716: KSPGetDM(jac->head->next->ksp, &sdm);
717: if (sdm) {
718: KSPSetDM(jac->kspschur, sdm);
719: KSPSetDMActive(jac->kspschur, PETSC_FALSE);
720: }
721: }
722: /* really want setfromoptions called in PCSetFromOptions_FieldSplit(), but it is not ready yet */
723: /* need to call this every time, since the jac->kspschur is freshly created, otherwise its options never get set */
724: KSPSetFromOptions(jac->kspschur);
725: }
727: /* HACK: special support to forward L and Lp matrices that might be used by PCLSC */
728: PetscSNPrintf(lscname,sizeof(lscname),"%s_LSC_L",ilink->splitname);
729: PetscObjectQuery((PetscObject)pc->mat,lscname,(PetscObject*)&LSC_L);
730: if (!LSC_L) {PetscObjectQuery((PetscObject)pc->pmat,lscname,(PetscObject*)&LSC_L);}
731: if (LSC_L) {PetscObjectCompose((PetscObject)jac->schur,"LSC_L",(PetscObject)LSC_L);}
732: PetscSNPrintf(lscname,sizeof(lscname),"%s_LSC_Lp",ilink->splitname);
733: PetscObjectQuery((PetscObject)pc->pmat,lscname,(PetscObject*)&LSC_L);
734: if (!LSC_L) {PetscObjectQuery((PetscObject)pc->mat,lscname,(PetscObject*)&LSC_L);}
735: if (LSC_L) {PetscObjectCompose((PetscObject)jac->schur,"LSC_Lp",(PetscObject)LSC_L);}
736: } else {
737: /* set up the individual splits' PCs */
738: i = 0;
739: ilink = jac->head;
740: while (ilink) {
741: KSPSetOperators(ilink->ksp,jac->mat[i],jac->pmat[i],flag);
742: /* really want setfromoptions called in PCSetFromOptions_FieldSplit(), but it is not ready yet */
743: if (!jac->suboptionsset) {KSPSetFromOptions(ilink->ksp);}
744: i++;
745: ilink = ilink->next;
746: }
747: }
749: jac->suboptionsset = PETSC_TRUE;
750: return(0);
751: }
753: #define FieldSplitSplitSolveAdd(ilink,xx,yy) \
754: (VecScatterBegin(ilink->sctx,xx,ilink->x,INSERT_VALUES,SCATTER_FORWARD) || \
755: VecScatterEnd(ilink->sctx,xx,ilink->x,INSERT_VALUES,SCATTER_FORWARD) || \
756: KSPSolve(ilink->ksp,ilink->x,ilink->y) || \
757: VecScatterBegin(ilink->sctx,ilink->y,yy,ADD_VALUES,SCATTER_REVERSE) || \
758: VecScatterEnd(ilink->sctx,ilink->y,yy,ADD_VALUES,SCATTER_REVERSE))
762: static PetscErrorCode PCApply_FieldSplit_Schur(PC pc,Vec x,Vec y)
763: {
764: PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
765: PetscErrorCode ierr;
766: PC_FieldSplitLink ilinkA = jac->head, ilinkD = ilinkA->next;
767: KSP kspA = ilinkA->ksp, kspLower = kspA, kspUpper = jac->kspupper;
770: switch (jac->schurfactorization) {
771: case PC_FIELDSPLIT_SCHUR_FACT_DIAG:
772: /* [A00 0; 0 -S], positive definite, suitable for MINRES */
773: VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
774: VecScatterBegin(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
775: VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
776: KSPSolve(kspA,ilinkA->x,ilinkA->y);
777: VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
778: VecScatterEnd(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
779: KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);
780: VecScale(ilinkD->y,-1.);
781: VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
782: VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
783: VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
784: break;
785: case PC_FIELDSPLIT_SCHUR_FACT_LOWER:
786: /* [A00 0; A10 S], suitable for left preconditioning */
787: VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
788: VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
789: KSPSolve(kspA,ilinkA->x,ilinkA->y);
790: MatMult(jac->C,ilinkA->y,ilinkD->x);
791: VecScale(ilinkD->x,-1.);
792: VecScatterBegin(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);
793: VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
794: VecScatterEnd(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);
795: KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);
796: VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
797: VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
798: VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
799: break;
800: case PC_FIELDSPLIT_SCHUR_FACT_UPPER:
801: /* [A00 A01; 0 S], suitable for right preconditioning */
802: VecScatterBegin(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
803: VecScatterEnd(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);
804: KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);
805: MatMult(jac->B,ilinkD->y,ilinkA->x);
806: VecScale(ilinkA->x,-1.);
807: VecScatterBegin(ilinkA->sctx,x,ilinkA->x,ADD_VALUES,SCATTER_FORWARD);
808: VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
809: VecScatterEnd(ilinkA->sctx,x,ilinkA->x,ADD_VALUES,SCATTER_FORWARD);
810: KSPSolve(kspA,ilinkA->x,ilinkA->y);
811: VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
812: VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
813: VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
814: break;
815: case PC_FIELDSPLIT_SCHUR_FACT_FULL:
816: /* [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 */
817: VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
818: VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);
819: KSPSolve(kspLower,ilinkA->x,ilinkA->y);
820: MatMult(jac->C,ilinkA->y,ilinkD->x);
821: VecScale(ilinkD->x,-1.0);
822: VecScatterBegin(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);
823: VecScatterEnd(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);
825: KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);
826: VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
827: VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);
829: if (kspUpper == kspA) {
830: MatMult(jac->B,ilinkD->y,ilinkA->y);
831: VecAXPY(ilinkA->x,-1.0,ilinkA->y);
832: KSPSolve(kspA,ilinkA->x,ilinkA->y);
833: } else {
834: KSPSolve(kspA,ilinkA->x,ilinkA->y);
835: MatMult(jac->B,ilinkD->y,ilinkA->x);
836: KSPSolve(kspUpper,ilinkA->x,ilinkA->z);
837: VecAXPY(ilinkA->y,-1.0,ilinkA->z);
838: }
839: VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
840: VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);
841: }
842: return(0);
843: }
847: static PetscErrorCode PCApply_FieldSplit(PC pc,Vec x,Vec y)
848: {
849: PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
850: PetscErrorCode ierr;
851: PC_FieldSplitLink ilink = jac->head;
852: PetscInt cnt,bs;
855: if (jac->type == PC_COMPOSITE_ADDITIVE) {
856: if (jac->defaultsplit) {
857: VecGetBlockSize(x,&bs);
858: 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);
859: VecGetBlockSize(y,&bs);
860: 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);
861: VecStrideGatherAll(x,jac->x,INSERT_VALUES);
862: while (ilink) {
863: KSPSolve(ilink->ksp,ilink->x,ilink->y);
864: ilink = ilink->next;
865: }
866: VecStrideScatterAll(jac->y,y,INSERT_VALUES);
867: } else {
868: VecSet(y,0.0);
869: while (ilink) {
870: FieldSplitSplitSolveAdd(ilink,x,y);
871: ilink = ilink->next;
872: }
873: }
874: } else if (jac->type == PC_COMPOSITE_MULTIPLICATIVE || jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
875: if (!jac->w1) {
876: VecDuplicate(x,&jac->w1);
877: VecDuplicate(x,&jac->w2);
878: }
879: VecSet(y,0.0);
880: FieldSplitSplitSolveAdd(ilink,x,y);
881: cnt = 1;
882: while (ilink->next) {
883: ilink = ilink->next;
884: /* compute the residual only over the part of the vector needed */
885: MatMult(jac->Afield[cnt++],y,ilink->x);
886: VecScale(ilink->x,-1.0);
887: VecScatterBegin(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
888: VecScatterEnd(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
889: KSPSolve(ilink->ksp,ilink->x,ilink->y);
890: VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
891: VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
892: }
893: if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
894: cnt -= 2;
895: while (ilink->previous) {
896: ilink = ilink->previous;
897: /* compute the residual only over the part of the vector needed */
898: MatMult(jac->Afield[cnt--],y,ilink->x);
899: VecScale(ilink->x,-1.0);
900: VecScatterBegin(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
901: VecScatterEnd(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);
902: KSPSolve(ilink->ksp,ilink->x,ilink->y);
903: VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
904: VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);
905: }
906: }
907: } else SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Unsupported or unknown composition",(int) jac->type);
908: return(0);
909: }
911: #define FieldSplitSplitSolveAddTranspose(ilink,xx,yy) \
912: (VecScatterBegin(ilink->sctx,xx,ilink->y,INSERT_VALUES,SCATTER_FORWARD) || \
913: VecScatterEnd(ilink->sctx,xx,ilink->y,INSERT_VALUES,SCATTER_FORWARD) || \
914: KSPSolveTranspose(ilink->ksp,ilink->y,ilink->x) || \
915: VecScatterBegin(ilink->sctx,ilink->x,yy,ADD_VALUES,SCATTER_REVERSE) || \
916: VecScatterEnd(ilink->sctx,ilink->x,yy,ADD_VALUES,SCATTER_REVERSE))
920: static PetscErrorCode PCApplyTranspose_FieldSplit(PC pc,Vec x,Vec y)
921: {
922: PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
923: PetscErrorCode ierr;
924: PC_FieldSplitLink ilink = jac->head;
925: PetscInt bs;
928: if (jac->type == PC_COMPOSITE_ADDITIVE) {
929: if (jac->defaultsplit) {
930: VecGetBlockSize(x,&bs);
931: 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);
932: VecGetBlockSize(y,&bs);
933: 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);
934: VecStrideGatherAll(x,jac->x,INSERT_VALUES);
935: while (ilink) {
936: KSPSolveTranspose(ilink->ksp,ilink->x,ilink->y);
937: ilink = ilink->next;
938: }
939: VecStrideScatterAll(jac->y,y,INSERT_VALUES);
940: } else {
941: VecSet(y,0.0);
942: while (ilink) {
943: FieldSplitSplitSolveAddTranspose(ilink,x,y);
944: ilink = ilink->next;
945: }
946: }
947: } else {
948: if (!jac->w1) {
949: VecDuplicate(x,&jac->w1);
950: VecDuplicate(x,&jac->w2);
951: }
952: VecSet(y,0.0);
953: if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
954: FieldSplitSplitSolveAddTranspose(ilink,x,y);
955: while (ilink->next) {
956: ilink = ilink->next;
957: MatMultTranspose(pc->mat,y,jac->w1);
958: VecWAXPY(jac->w2,-1.0,jac->w1,x);
959: FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);
960: }
961: while (ilink->previous) {
962: ilink = ilink->previous;
963: MatMultTranspose(pc->mat,y,jac->w1);
964: VecWAXPY(jac->w2,-1.0,jac->w1,x);
965: FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);
966: }
967: } else {
968: while (ilink->next) { /* get to last entry in linked list */
969: ilink = ilink->next;
970: }
971: FieldSplitSplitSolveAddTranspose(ilink,x,y);
972: while (ilink->previous) {
973: ilink = ilink->previous;
974: MatMultTranspose(pc->mat,y,jac->w1);
975: VecWAXPY(jac->w2,-1.0,jac->w1,x);
976: FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);
977: }
978: }
979: }
980: return(0);
981: }
985: static PetscErrorCode PCReset_FieldSplit(PC pc)
986: {
987: PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
988: PetscErrorCode ierr;
989: PC_FieldSplitLink ilink = jac->head,next;
992: while (ilink) {
993: KSPReset(ilink->ksp);
994: VecDestroy(&ilink->x);
995: VecDestroy(&ilink->y);
996: VecDestroy(&ilink->z);
997: VecScatterDestroy(&ilink->sctx);
998: ISDestroy(&ilink->is);
999: ISDestroy(&ilink->is_col);
1000: next = ilink->next;
1001: ilink = next;
1002: }
1003: PetscFree2(jac->x,jac->y);
1004: if (jac->mat && jac->mat != jac->pmat) {
1005: MatDestroyMatrices(jac->nsplits,&jac->mat);
1006: } else if (jac->mat) {
1007: jac->mat = NULL;
1008: }
1009: if (jac->pmat) {MatDestroyMatrices(jac->nsplits,&jac->pmat);}
1010: if (jac->Afield) {MatDestroyMatrices(jac->nsplits,&jac->Afield);}
1011: VecDestroy(&jac->w1);
1012: VecDestroy(&jac->w2);
1013: MatDestroy(&jac->schur);
1014: MatDestroy(&jac->schur_user);
1015: KSPDestroy(&jac->kspschur);
1016: KSPDestroy(&jac->kspupper);
1017: MatDestroy(&jac->B);
1018: MatDestroy(&jac->C);
1019: jac->reset = PETSC_TRUE;
1020: return(0);
1021: }
1025: static PetscErrorCode PCDestroy_FieldSplit(PC pc)
1026: {
1027: PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
1028: PetscErrorCode ierr;
1029: PC_FieldSplitLink ilink = jac->head,next;
1032: PCReset_FieldSplit(pc);
1033: while (ilink) {
1034: KSPDestroy(&ilink->ksp);
1035: next = ilink->next;
1036: PetscFree(ilink->splitname);
1037: PetscFree(ilink->fields);
1038: PetscFree(ilink->fields_col);
1039: PetscFree(ilink);
1040: ilink = next;
1041: }
1042: PetscFree2(jac->x,jac->y);
1043: PetscFree(pc->data);
1044: PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",NULL);
1045: PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetFields_C",NULL);
1046: PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetIS_C",NULL);
1047: PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetType_C",NULL);
1048: PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetBlockSize_C",NULL);
1049: PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSchurPrecondition_C",NULL);
1050: PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C",NULL);
1051: return(0);
1052: }
1056: static PetscErrorCode PCSetFromOptions_FieldSplit(PC pc)
1057: {
1058: PetscErrorCode ierr;
1059: PetscInt bs;
1060: PetscBool flg,stokes = PETSC_FALSE;
1061: PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
1062: PCCompositeType ctype;
1065: PetscOptionsHead("FieldSplit options");
1066: PetscOptionsBool("-pc_fieldsplit_dm_splits","Whether to use DMCreateFieldDecomposition() for splits","PCFieldSplitSetDMSplits",jac->dm_splits,&jac->dm_splits,NULL);
1067: PetscOptionsInt("-pc_fieldsplit_block_size","Blocksize that defines number of fields","PCFieldSplitSetBlockSize",jac->bs,&bs,&flg);
1068: if (flg) {
1069: PCFieldSplitSetBlockSize(pc,bs);
1070: }
1072: PetscOptionsGetBool(((PetscObject)pc)->prefix,"-pc_fieldsplit_detect_saddle_point",&stokes,NULL);
1073: if (stokes) {
1074: PCFieldSplitSetType(pc,PC_COMPOSITE_SCHUR);
1075: jac->schurpre = PC_FIELDSPLIT_SCHUR_PRE_SELF;
1076: }
1078: PetscOptionsEnum("-pc_fieldsplit_type","Type of composition","PCFieldSplitSetType",PCCompositeTypes,(PetscEnum)jac->type,(PetscEnum*)&ctype,&flg);
1079: if (flg) {
1080: PCFieldSplitSetType(pc,ctype);
1081: }
1082: /* Only setup fields once */
1083: if ((jac->bs > 0) && (jac->nsplits == 0)) {
1084: /* only allow user to set fields from command line if bs is already known.
1085: otherwise user can set them in PCFieldSplitSetDefaults() */
1086: PCFieldSplitSetRuntimeSplits_Private(pc);
1087: if (jac->splitdefined) {PetscInfo(pc,"Splits defined using the options database\n");}
1088: }
1089: if (jac->type == PC_COMPOSITE_SCHUR) {
1090: PetscOptionsGetEnum(((PetscObject)pc)->prefix,"-pc_fieldsplit_schur_factorization_type",PCFieldSplitSchurFactTypes,(PetscEnum*)&jac->schurfactorization,&flg);
1091: if (flg) {PetscInfo(pc,"Deprecated use of -pc_fieldsplit_schur_factorization_type\n");}
1092: 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);
1093: PetscOptionsEnum("-pc_fieldsplit_schur_precondition","How to build preconditioner for Schur complement","PCFieldSplitSchurPrecondition",PCFieldSplitSchurPreTypes,(PetscEnum)jac->schurpre,(PetscEnum*)&jac->schurpre,NULL);
1094: }
1095: PetscOptionsTail();
1096: return(0);
1097: }
1099: /*------------------------------------------------------------------------------------*/
1103: static PetscErrorCode PCFieldSplitSetFields_FieldSplit(PC pc,const char splitname[],PetscInt n,const PetscInt *fields,const PetscInt *fields_col)
1104: {
1105: PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
1106: PetscErrorCode ierr;
1107: PC_FieldSplitLink ilink,next = jac->head;
1108: char prefix[128];
1109: PetscInt i;
1112: if (jac->splitdefined) {
1113: PetscInfo1(pc,"Ignoring new split \"%s\" because the splits have already been defined\n",splitname);
1114: return(0);
1115: }
1116: for (i=0; i<n; i++) {
1117: if (fields[i] >= jac->bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Field %D requested but only %D exist",fields[i],jac->bs);
1118: if (fields[i] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative field %D requested",fields[i]);
1119: }
1120: PetscNew(&ilink);
1121: if (splitname) {
1122: PetscStrallocpy(splitname,&ilink->splitname);
1123: } else {
1124: PetscMalloc1(3,&ilink->splitname);
1125: PetscSNPrintf(ilink->splitname,2,"%s",jac->nsplits);
1126: }
1127: PetscMalloc1(n,&ilink->fields);
1128: PetscMemcpy(ilink->fields,fields,n*sizeof(PetscInt));
1129: PetscMalloc1(n,&ilink->fields_col);
1130: PetscMemcpy(ilink->fields_col,fields_col,n*sizeof(PetscInt));
1132: ilink->nfields = n;
1133: ilink->next = NULL;
1134: KSPCreate(PetscObjectComm((PetscObject)pc),&ilink->ksp);
1135: PetscObjectIncrementTabLevel((PetscObject)ilink->ksp,(PetscObject)pc,1);
1136: KSPSetType(ilink->ksp,KSPPREONLY);
1137: PetscLogObjectParent((PetscObject)pc,(PetscObject)ilink->ksp);
1139: PetscSNPrintf(prefix,sizeof(prefix),"%sfieldsplit_%s_",((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "",ilink->splitname);
1140: KSPSetOptionsPrefix(ilink->ksp,prefix);
1142: if (!next) {
1143: jac->head = ilink;
1144: ilink->previous = NULL;
1145: } else {
1146: while (next->next) {
1147: next = next->next;
1148: }
1149: next->next = ilink;
1150: ilink->previous = next;
1151: }
1152: jac->nsplits++;
1153: return(0);
1154: }
1158: static PetscErrorCode PCFieldSplitGetSubKSP_FieldSplit_Schur(PC pc,PetscInt *n,KSP **subksp)
1159: {
1160: PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
1164: PetscMalloc1(jac->nsplits,subksp);
1165: MatSchurComplementGetKSP(jac->schur,*subksp);
1167: (*subksp)[1] = jac->kspschur;
1168: if (n) *n = jac->nsplits;
1169: return(0);
1170: }
1174: static PetscErrorCode PCFieldSplitGetSubKSP_FieldSplit(PC pc,PetscInt *n,KSP **subksp)
1175: {
1176: PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
1177: PetscErrorCode ierr;
1178: PetscInt cnt = 0;
1179: PC_FieldSplitLink ilink = jac->head;
1182: PetscMalloc1(jac->nsplits,subksp);
1183: while (ilink) {
1184: (*subksp)[cnt++] = ilink->ksp;
1185: ilink = ilink->next;
1186: }
1187: 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);
1188: if (n) *n = jac->nsplits;
1189: return(0);
1190: }
1194: static PetscErrorCode PCFieldSplitSetIS_FieldSplit(PC pc,const char splitname[],IS is)
1195: {
1196: PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
1197: PetscErrorCode ierr;
1198: PC_FieldSplitLink ilink, next = jac->head;
1199: char prefix[128];
1202: if (jac->splitdefined) {
1203: PetscInfo1(pc,"Ignoring new split \"%s\" because the splits have already been defined\n",splitname);
1204: return(0);
1205: }
1206: PetscNew(&ilink);
1207: if (splitname) {
1208: PetscStrallocpy(splitname,&ilink->splitname);
1209: } else {
1210: PetscMalloc1(8,&ilink->splitname);
1211: PetscSNPrintf(ilink->splitname,7,"%D",jac->nsplits);
1212: }
1213: PetscObjectReference((PetscObject)is);
1214: ISDestroy(&ilink->is);
1215: ilink->is = is;
1216: PetscObjectReference((PetscObject)is);
1217: ISDestroy(&ilink->is_col);
1218: ilink->is_col = is;
1219: ilink->next = NULL;
1220: KSPCreate(PetscObjectComm((PetscObject)pc),&ilink->ksp);
1221: PetscObjectIncrementTabLevel((PetscObject)ilink->ksp,(PetscObject)pc,1);
1222: KSPSetType(ilink->ksp,KSPPREONLY);
1223: PetscLogObjectParent((PetscObject)pc,(PetscObject)ilink->ksp);
1225: PetscSNPrintf(prefix,sizeof(prefix),"%sfieldsplit_%s_",((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "",ilink->splitname);
1226: KSPSetOptionsPrefix(ilink->ksp,prefix);
1228: if (!next) {
1229: jac->head = ilink;
1230: ilink->previous = NULL;
1231: } else {
1232: while (next->next) {
1233: next = next->next;
1234: }
1235: next->next = ilink;
1236: ilink->previous = next;
1237: }
1238: jac->nsplits++;
1239: return(0);
1240: }
1244: /*@
1245: PCFieldSplitSetFields - Sets the fields for one particular split in the field split preconditioner
1247: Logically Collective on PC
1249: Input Parameters:
1250: + pc - the preconditioner context
1251: . splitname - name of this split, if NULL the number of the split is used
1252: . n - the number of fields in this split
1253: - fields - the fields in this split
1255: Level: intermediate
1257: Notes: Use PCFieldSplitSetIS() to set a completely general set of indices as a field.
1259: The PCFieldSplitSetFields() is for defining fields as strided blocks. For example, if the block
1260: 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
1261: 0xx3xx6xx9xx12 ... x1xx4xx7xx ... xx2xx5xx8xx.. 01x34x67x... 0x1x3x5x7.. x12x45x78x....
1262: where the numbered entries indicate what is in the field.
1264: This function is called once per split (it creates a new split each time). Solve options
1265: for this split will be available under the prefix -fieldsplit_SPLITNAME_.
1267: Developer Note: This routine does not actually create the IS representing the split, that is delayed
1268: until PCSetUp_FieldSplit(), because information about the vector/matrix layouts may not be
1269: available when this routine is called.
1271: .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetBlockSize(), PCFieldSplitSetIS()
1273: @*/
1274: PetscErrorCode PCFieldSplitSetFields(PC pc,const char splitname[],PetscInt n,const PetscInt *fields,const PetscInt *fields_col)
1275: {
1281: if (n < 1) SETERRQ2(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Provided number of fields %D in split \"%s\" not positive",n,splitname);
1283: PetscTryMethod(pc,"PCFieldSplitSetFields_C",(PC,const char[],PetscInt,const PetscInt*,const PetscInt*),(pc,splitname,n,fields,fields_col));
1284: return(0);
1285: }
1289: /*@
1290: PCFieldSplitSetIS - Sets the exact elements for field
1292: Logically Collective on PC
1294: Input Parameters:
1295: + pc - the preconditioner context
1296: . splitname - name of this split, if NULL the number of the split is used
1297: - is - the index set that defines the vector elements in this field
1300: Notes:
1301: Use PCFieldSplitSetFields(), for fields defined by strided types.
1303: This function is called once per split (it creates a new split each time). Solve options
1304: for this split will be available under the prefix -fieldsplit_SPLITNAME_.
1306: Level: intermediate
1308: .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetBlockSize()
1310: @*/
1311: PetscErrorCode PCFieldSplitSetIS(PC pc,const char splitname[],IS is)
1312: {
1319: PetscTryMethod(pc,"PCFieldSplitSetIS_C",(PC,const char[],IS),(pc,splitname,is));
1320: return(0);
1321: }
1325: /*@
1326: PCFieldSplitGetIS - Retrieves the elements for a field as an IS
1328: Logically Collective on PC
1330: Input Parameters:
1331: + pc - the preconditioner context
1332: - splitname - name of this split
1334: Output Parameter:
1335: - is - the index set that defines the vector elements in this field, or NULL if the field is not found
1337: Level: intermediate
1339: .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetIS()
1341: @*/
1342: PetscErrorCode PCFieldSplitGetIS(PC pc,const char splitname[],IS *is)
1343: {
1350: {
1351: PC_FieldSplit *jac = (PC_FieldSplit*) pc->data;
1352: PC_FieldSplitLink ilink = jac->head;
1353: PetscBool found;
1355: *is = NULL;
1356: while (ilink) {
1357: PetscStrcmp(ilink->splitname, splitname, &found);
1358: if (found) {
1359: *is = ilink->is;
1360: break;
1361: }
1362: ilink = ilink->next;
1363: }
1364: }
1365: return(0);
1366: }
1370: /*@
1371: PCFieldSplitSetBlockSize - Sets the block size for defining where fields start in the
1372: fieldsplit preconditioner. If not set the matrix block size is used.
1374: Logically Collective on PC
1376: Input Parameters:
1377: + pc - the preconditioner context
1378: - bs - the block size
1380: Level: intermediate
1382: .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields()
1384: @*/
1385: PetscErrorCode PCFieldSplitSetBlockSize(PC pc,PetscInt bs)
1386: {
1392: PetscTryMethod(pc,"PCFieldSplitSetBlockSize_C",(PC,PetscInt),(pc,bs));
1393: return(0);
1394: }
1398: /*@C
1399: PCFieldSplitGetSubKSP - Gets the KSP contexts for all splits
1401: Collective on KSP
1403: Input Parameter:
1404: . pc - the preconditioner context
1406: Output Parameters:
1407: + n - the number of splits
1408: - pc - the array of KSP contexts
1410: Note:
1411: After PCFieldSplitGetSubKSP() the array of KSPs IS to be freed by the user
1412: (not the KSP just the array that contains them).
1414: You must call KSPSetUp() before calling PCFieldSplitGetSubKSP().
1416: Fortran Usage: You must pass in a KSP array that is large enough to contain all the local KSPs.
1417: You can call PCFieldSplitGetSubKSP(pc,n,NULL_OBJECT,ierr) to determine how large the
1418: KSP array must be.
1421: Level: advanced
1423: .seealso: PCFIELDSPLIT
1424: @*/
1425: PetscErrorCode PCFieldSplitGetSubKSP(PC pc,PetscInt *n,KSP *subksp[])
1426: {
1432: PetscUseMethod(pc,"PCFieldSplitGetSubKSP_C",(PC,PetscInt*,KSP **),(pc,n,subksp));
1433: return(0);
1434: }
1438: /*@
1439: PCFieldSplitSchurPrecondition - Indicates if the Schur complement is preconditioned by a preconditioner constructed by the
1440: A11 matrix. Otherwise no preconditioner is used.
1442: Collective on PC
1444: Input Parameters:
1445: + pc - the preconditioner context
1446: . ptype - which matrix to use for preconditioning the Schur complement, PC_FIELDSPLIT_SCHUR_PRE_A11 (diag) is default
1447: - userpre - matrix to use for preconditioning, or NULL
1449: Options Database:
1450: . -pc_fieldsplit_schur_precondition <self,user,a11> default is a11
1452: Notes:
1453: $ If ptype is
1454: $ user then the preconditioner for the Schur complement is generated by the provided matrix (pre argument
1455: $ to this function).
1456: $ a11 then the preconditioner for the Schur complement is generated by the block diagonal part of the original
1457: $ matrix associated with the Schur complement (i.e. A11)
1458: $ self the preconditioner for the Schur complement is generated from the Schur complement matrix itself:
1459: $ The only preconditioner that currently works directly with the Schur complement matrix object is the PCLSC
1460: $ preconditioner
1462: When solving a saddle point problem, where the A11 block is identically zero, using a11 as the ptype only makes sense
1463: with the additional option -fieldsplit_1_pc_type none. Usually for saddle point problems one would use a ptype of self and
1464: -fieldsplit_1_pc_type lsc which uses the least squares commutator compute a preconditioner for the Schur complement.
1466: Developer Notes: This is a terrible name, gives no good indication of what the function does and should also have Set in
1467: the name since it sets a proceedure to use.
1469: Level: intermediate
1471: .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields(), PCFieldSplitSchurPreType, PCLSC
1473: @*/
1474: PetscErrorCode PCFieldSplitSchurPrecondition(PC pc,PCFieldSplitSchurPreType ptype,Mat pre)
1475: {
1480: PetscTryMethod(pc,"PCFieldSplitSchurPrecondition_C",(PC,PCFieldSplitSchurPreType,Mat),(pc,ptype,pre));
1481: return(0);
1482: }
1486: static PetscErrorCode PCFieldSplitSchurPrecondition_FieldSplit(PC pc,PCFieldSplitSchurPreType ptype,Mat pre)
1487: {
1488: PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
1492: jac->schurpre = ptype;
1493: if (pre) {
1494: MatDestroy(&jac->schur_user);
1495: jac->schur_user = pre;
1496: PetscObjectReference((PetscObject)jac->schur_user);
1497: }
1498: return(0);
1499: }
1503: /*@
1504: PCFieldSplitSetSchurFactType - sets which blocks of the approximate block factorization to retain
1506: Collective on PC
1508: Input Parameters:
1509: + pc - the preconditioner context
1510: - ftype - which blocks of factorization to retain, PC_FIELDSPLIT_SCHUR_FACT_FULL is default
1512: Options Database:
1513: . -pc_fieldsplit_schur_fact_type <diag,lower,upper,full> default is full
1516: Level: intermediate
1518: Notes:
1519: The FULL factorization is
1521: $ (A B) = (1 0) (A 0) (1 Ainv*B)
1522: $ (C D) (C*Ainv 1) (0 S) (0 1 )
1524: 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,
1525: 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).
1527: 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
1528: 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
1529: 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,
1530: the preconditioned operator has three distinct nonzero eigenvalues and minimal polynomial of degree at most 4, so KSPGMRES converges in at most 4 iterations.
1532: 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
1533: 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).
1535: References:
1536: Murphy, Golub, and Wathen, A note on preconditioning indefinite linear systems, SIAM J. Sci. Comput., 21 (2000) pp. 1969-1972.
1538: Ipsen, A note on preconditioning nonsymmetric matrices, SIAM J. Sci. Comput., 23 (2001), pp. 1050-1051.
1540: .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields(), PCFieldSplitSchurPreType
1541: @*/
1542: PetscErrorCode PCFieldSplitSetSchurFactType(PC pc,PCFieldSplitSchurFactType ftype)
1543: {
1548: PetscTryMethod(pc,"PCFieldSplitSetSchurFactType_C",(PC,PCFieldSplitSchurFactType),(pc,ftype));
1549: return(0);
1550: }
1554: static PetscErrorCode PCFieldSplitSetSchurFactType_FieldSplit(PC pc,PCFieldSplitSchurFactType ftype)
1555: {
1556: PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
1559: jac->schurfactorization = ftype;
1560: return(0);
1561: }
1565: /*@C
1566: PCFieldSplitGetSchurBlocks - Gets all matrix blocks for the Schur complement
1568: Collective on KSP
1570: Input Parameter:
1571: . pc - the preconditioner context
1573: Output Parameters:
1574: + A00 - the (0,0) block
1575: . A01 - the (0,1) block
1576: . A10 - the (1,0) block
1577: - A11 - the (1,1) block
1579: Level: advanced
1581: .seealso: PCFIELDSPLIT
1582: @*/
1583: PetscErrorCode PCFieldSplitGetSchurBlocks(PC pc,Mat *A00,Mat *A01,Mat *A10, Mat *A11)
1584: {
1585: PC_FieldSplit *jac = (PC_FieldSplit*) pc->data;
1589: if (jac->type != PC_COMPOSITE_SCHUR) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONG, "FieldSplit is not using a Schur complement approach.");
1590: if (A00) *A00 = jac->pmat[0];
1591: if (A01) *A01 = jac->B;
1592: if (A10) *A10 = jac->C;
1593: if (A11) *A11 = jac->pmat[1];
1594: return(0);
1595: }
1599: static PetscErrorCode PCFieldSplitSetType_FieldSplit(PC pc,PCCompositeType type)
1600: {
1601: PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
1605: jac->type = type;
1606: if (type == PC_COMPOSITE_SCHUR) {
1607: pc->ops->apply = PCApply_FieldSplit_Schur;
1608: pc->ops->view = PCView_FieldSplit_Schur;
1610: PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit_Schur);
1611: PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSchurPrecondition_C",PCFieldSplitSchurPrecondition_FieldSplit);
1612: PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C",PCFieldSplitSetSchurFactType_FieldSplit);
1614: } else {
1615: pc->ops->apply = PCApply_FieldSplit;
1616: pc->ops->view = PCView_FieldSplit;
1618: PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit);
1619: PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSchurPrecondition_C",0);
1620: PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C",0);
1621: }
1622: return(0);
1623: }
1627: static PetscErrorCode PCFieldSplitSetBlockSize_FieldSplit(PC pc,PetscInt bs)
1628: {
1629: PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
1632: if (bs < 1) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Blocksize must be positive, you gave %D",bs);
1633: 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);
1634: jac->bs = bs;
1635: return(0);
1636: }
1640: /*@
1641: PCFieldSplitSetType - Sets the type of fieldsplit preconditioner.
1643: Collective on PC
1645: Input Parameter:
1646: . pc - the preconditioner context
1647: . type - PC_COMPOSITE_ADDITIVE, PC_COMPOSITE_MULTIPLICATIVE (default), PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE, PC_COMPOSITE_SPECIAL, PC_COMPOSITE_SCHUR
1649: Options Database Key:
1650: . -pc_fieldsplit_type <type: one of multiplicative, additive, symmetric_multiplicative, special, schur> - Sets fieldsplit preconditioner type
1652: Level: Intermediate
1654: .keywords: PC, set, type, composite preconditioner, additive, multiplicative
1656: .seealso: PCCompositeSetType()
1658: @*/
1659: PetscErrorCode PCFieldSplitSetType(PC pc,PCCompositeType type)
1660: {
1665: PetscTryMethod(pc,"PCFieldSplitSetType_C",(PC,PCCompositeType),(pc,type));
1666: return(0);
1667: }
1671: /*@
1672: PCFieldSplitGetType - Gets the type of fieldsplit preconditioner.
1674: Not collective
1676: Input Parameter:
1677: . pc - the preconditioner context
1679: Output Parameter:
1680: . type - PC_COMPOSITE_ADDITIVE, PC_COMPOSITE_MULTIPLICATIVE (default), PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE, PC_COMPOSITE_SPECIAL, PC_COMPOSITE_SCHUR
1682: Level: Intermediate
1684: .keywords: PC, set, type, composite preconditioner, additive, multiplicative
1685: .seealso: PCCompositeSetType()
1686: @*/
1687: PetscErrorCode PCFieldSplitGetType(PC pc, PCCompositeType *type)
1688: {
1689: PC_FieldSplit *jac = (PC_FieldSplit*) pc->data;
1694: *type = jac->type;
1695: return(0);
1696: }
1700: /*@
1701: PCFieldSplitSetDMSplits - Flags whether DMCreateFieldDecomposition() should be used to define the splits, whenever possible.
1703: Logically Collective
1705: Input Parameters:
1706: + pc - the preconditioner context
1707: - flg - boolean indicating whether to use field splits defined by the DM
1709: Options Database Key:
1710: . -pc_fieldsplit_dm_splits
1712: Level: Intermediate
1714: .keywords: PC, DM, composite preconditioner, additive, multiplicative
1716: .seealso: PCFieldSplitGetDMSplits()
1718: @*/
1719: PetscErrorCode PCFieldSplitSetDMSplits(PC pc,PetscBool flg)
1720: {
1721: PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
1722: PetscBool isfs;
1728: PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
1729: if (isfs) {
1730: jac->dm_splits = flg;
1731: }
1732: return(0);
1733: }
1738: /*@
1739: PCFieldSplitGetDMSplits - Returns flag indicating whether DMCreateFieldDecomposition() should be used to define the splits, whenever possible.
1741: Logically Collective
1743: Input Parameter:
1744: . pc - the preconditioner context
1746: Output Parameter:
1747: . flg - boolean indicating whether to use field splits defined by the DM
1749: Level: Intermediate
1751: .keywords: PC, DM, composite preconditioner, additive, multiplicative
1753: .seealso: PCFieldSplitSetDMSplits()
1755: @*/
1756: PetscErrorCode PCFieldSplitGetDMSplits(PC pc,PetscBool* flg)
1757: {
1758: PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
1759: PetscBool isfs;
1765: PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);
1766: if (isfs) {
1767: if(flg) *flg = jac->dm_splits;
1768: }
1769: return(0);
1770: }
1772: /* -------------------------------------------------------------------------------------*/
1773: /*MC
1774: PCFIELDSPLIT - Preconditioner created by combining separate preconditioners for individual
1775: fields or groups of fields. See the users manual section "Solving Block Matrices" for more details.
1777: To set options on the solvers for each block append -fieldsplit_ to all the PC
1778: options database keys. For example, -fieldsplit_pc_type ilu -fieldsplit_pc_factor_levels 1
1780: To set the options on the solvers separate for each block call PCFieldSplitGetSubKSP()
1781: and set the options directly on the resulting KSP object
1783: Level: intermediate
1785: Options Database Keys:
1786: + -pc_fieldsplit_%d_fields <a,b,..> - indicates the fields to be used in the %d'th split
1787: . -pc_fieldsplit_default - automatically add any fields to additional splits that have not
1788: been supplied explicitly by -pc_fieldsplit_%d_fields
1789: . -pc_fieldsplit_block_size <bs> - size of block that defines fields (i.e. there are bs fields)
1790: . -pc_fieldsplit_type <additive,multiplicative,symmetric_multiplicative,schur> - type of relaxation or factorization splitting
1791: . -pc_fieldsplit_schur_precondition <self,user,a11> - default is a11
1792: . -pc_fieldsplit_detect_saddle_point - automatically finds rows with zero or negative diagonal and uses Schur complement with no preconditioner as the solver
1794: - Options prefix for inner solvers when using Schur complement preconditioner are -fieldsplit_0_ and -fieldsplit_1_
1795: for all other solvers they are -fieldsplit_%d_ for the dth field, use -fieldsplit_ for all fields
1797: Notes:
1798: Use PCFieldSplitSetFields() to set fields defined by "strided" entries and PCFieldSplitSetIS()
1799: to define a field by an arbitrary collection of entries.
1801: If no fields are set the default is used. The fields are defined by entries strided by bs,
1802: beginning at 0 then 1, etc to bs-1. The block size can be set with PCFieldSplitSetBlockSize(),
1803: if this is not called the block size defaults to the blocksize of the second matrix passed
1804: to KSPSetOperators()/PCSetOperators().
1806: $ For the Schur complement preconditioner if J = ( A00 A01 )
1807: $ ( A10 A11 )
1808: $ the preconditioner using full factorization is
1809: $ ( I -ksp(A00) A01 ) ( inv(A00) 0 ) ( I 0 )
1810: $ ( 0 I ) ( 0 ksp(S) ) ( -A10 ksp(A00) I )
1811: where the action of inv(A00) is applied using the KSP solver with prefix -fieldsplit_0_. S is the Schur complement
1812: $ S = A11 - A10 ksp(A00) A01
1813: 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
1814: in providing the SECOND split or 1 if not give). For PCFieldSplitGetKSP() when field number is 0,
1815: it returns the KSP associated with -fieldsplit_0_ while field number 1 gives -fieldsplit_1_ KSP. By default
1816: A11 is used to construct a preconditioner for S, use PCFieldSplitSchurPrecondition() to turn on or off this
1817: option. You can use the preconditioner PCLSC to precondition the Schur complement with -fieldsplit_1_pc_type lsc. The
1818: factorization type is set using -pc_fieldsplit_schur_fact_type <diag, lower, upper, full>. The full is shown above,
1819: diag gives
1820: $ ( inv(A00) 0 )
1821: $ ( 0 -ksp(S) )
1822: 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
1823: $ ( A00 0 )
1824: $ ( A10 S )
1825: where the inverses of A00 and S are applied using KSPs. The upper factorization is the inverse of
1826: $ ( A00 A01 )
1827: $ ( 0 S )
1828: where again the inverses of A00 and S are applied using KSPs.
1830: If only one set of indices (one IS) is provided with PCFieldSplitSetIS() then the complement of that IS
1831: is used automatically for a second block.
1833: The fieldsplit preconditioner cannot currently be used with the BAIJ or SBAIJ data formats if the blocksize is larger than 1.
1834: Generally it should be used with the AIJ format.
1836: The forms of these preconditioners are closely related if not identical to forms derived as "Distributive Iterations", see,
1837: for example, page 294 in "Principles of Computational Fluid Dynamics" by Pieter Wesseling. Note that one can also use PCFIELDSPLIT
1838: inside a smoother resulting in "Distributive Smoothers".
1840: Concepts: physics based preconditioners, block preconditioners
1842: .seealso: PCCreate(), PCSetType(), PCType (for list of available types), PC, Block_Preconditioners, PCLSC,
1843: PCFieldSplitGetSubKSP(), PCFieldSplitSetFields(), PCFieldSplitSetType(), PCFieldSplitSetIS(), PCFieldSplitSchurPrecondition()
1844: M*/
1848: PETSC_EXTERN PetscErrorCode PCCreate_FieldSplit(PC pc)
1849: {
1851: PC_FieldSplit *jac;
1854: PetscNewLog(pc,&jac);
1856: jac->bs = -1;
1857: jac->nsplits = 0;
1858: jac->type = PC_COMPOSITE_MULTIPLICATIVE;
1859: jac->schurpre = PC_FIELDSPLIT_SCHUR_PRE_USER; /* Try user preconditioner first, fall back on diagonal */
1860: jac->schurfactorization = PC_FIELDSPLIT_SCHUR_FACT_FULL;
1861: jac->dm_splits = PETSC_TRUE;
1863: pc->data = (void*)jac;
1865: pc->ops->apply = PCApply_FieldSplit;
1866: pc->ops->applytranspose = PCApplyTranspose_FieldSplit;
1867: pc->ops->setup = PCSetUp_FieldSplit;
1868: pc->ops->reset = PCReset_FieldSplit;
1869: pc->ops->destroy = PCDestroy_FieldSplit;
1870: pc->ops->setfromoptions = PCSetFromOptions_FieldSplit;
1871: pc->ops->view = PCView_FieldSplit;
1872: pc->ops->applyrichardson = 0;
1874: PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit);
1875: PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetFields_C",PCFieldSplitSetFields_FieldSplit);
1876: PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetIS_C",PCFieldSplitSetIS_FieldSplit);
1877: PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetType_C",PCFieldSplitSetType_FieldSplit);
1878: PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetBlockSize_C",PCFieldSplitSetBlockSize_FieldSplit);
1879: return(0);
1880: }