xref: /petsc/src/ksp/pc/impls/fieldsplit/fieldsplit.c (revision bca69d2b7e3f74db4c1b598a59ba6e2143e7045d)
1dba47a55SKris Buschelman 
2b45d2f2cSJed Brown #include <petsc-private/pcimpl.h>     /*I "petscpc.h" I*/
31e25c274SJed Brown #include <petscdm.h>
40971522cSBarry Smith 
5443836d0SMatthew G Knepley /*
6443836d0SMatthew G Knepley   There is a nice discussion of block preconditioners in
7443836d0SMatthew G Knepley 
81997fe2eSSatish Balay [El08] A taxonomy and comparison of parallel block multi-level preconditioners for the incompressible Navier-Stokes equations
9443836d0SMatthew G Knepley        Howard Elman, V.E. Howle, John Shadid, Robert Shuttleworth, Ray Tuminaro, Journal of Computational Physics 227 (2008) 1790--1808
101b8e4c5fSJed Brown        http://chess.cs.umd.edu/~elman/papers/tax.pdf
11443836d0SMatthew G Knepley */
12443836d0SMatthew G Knepley 
13a7476a74SDmitry Karpeev const char *const PCFieldSplitSchurPreTypes[] = {"SELF","SELFP","A11","USER","PCFieldSplitSchurPreType","PC_FIELDSPLIT_SCHUR_PRE_",0};
14c9c6ffaaSJed Brown const char *const PCFieldSplitSchurFactTypes[] = {"DIAG","LOWER","UPPER","FULL","PCFieldSplitSchurFactType","PC_FIELDSPLIT_SCHUR_FACT_",0};
15c5d2311dSJed Brown 
160971522cSBarry Smith typedef struct _PC_FieldSplitLink *PC_FieldSplitLink;
170971522cSBarry Smith struct _PC_FieldSplitLink {
1869a612a9SBarry Smith   KSP               ksp;
19443836d0SMatthew G Knepley   Vec               x,y,z;
20db4c96c1SJed Brown   char              *splitname;
210971522cSBarry Smith   PetscInt          nfields;
225d4c12cdSJungho Lee   PetscInt          *fields,*fields_col;
231b9fc7fcSBarry Smith   VecScatter        sctx;
245d4c12cdSJungho Lee   IS                is,is_col;
2551f519a2SBarry Smith   PC_FieldSplitLink next,previous;
260971522cSBarry Smith };
270971522cSBarry Smith 
280971522cSBarry Smith typedef struct {
2968dd23aaSBarry Smith   PCCompositeType type;
30ace3abfcSBarry Smith   PetscBool       defaultsplit;                    /* Flag for a system with a set of 'k' scalar fields with the same layout (and bs = k) */
31ace3abfcSBarry Smith   PetscBool       splitdefined;                    /* Flag is set after the splits have been defined, to prevent more splits from being added */
3230ad9308SMatthew Knepley   PetscInt        bs;                              /* Block size for IS and Mat structures */
3330ad9308SMatthew Knepley   PetscInt        nsplits;                         /* Number of field divisions defined */
3479416396SBarry Smith   Vec             *x,*y,w1,w2;
35519d70e2SJed Brown   Mat             *mat;                            /* The diagonal block for each split */
36519d70e2SJed Brown   Mat             *pmat;                           /* The preconditioning diagonal block for each split */
3730ad9308SMatthew Knepley   Mat             *Afield;                         /* The rows of the matrix associated with each split */
38ace3abfcSBarry Smith   PetscBool       issetup;
392fa5cd67SKarl Rupp 
4030ad9308SMatthew Knepley   /* Only used when Schur complement preconditioning is used */
4130ad9308SMatthew Knepley   Mat                       B;                     /* The (0,1) block */
4230ad9308SMatthew Knepley   Mat                       C;                     /* The (1,0) block */
43443836d0SMatthew G Knepley   Mat                       schur;                 /* The Schur complement S = A11 - A10 A00^{-1} A01, the KSP here, kspinner, is H_1 in [El08] */
44a7476a74SDmitry Karpeev   Mat                       schurp;                /* Assembled approximation to S built by MatSchurComplement to be used as a preconditioning matrix when solving with S */
45084e4875SJed Brown   Mat                       schur_user;            /* User-provided preconditioning matrix for the Schur complement */
46084e4875SJed Brown   PCFieldSplitSchurPreType  schurpre;              /* Determines which preconditioning matrix is used for the Schur complement */
47c9c6ffaaSJed Brown   PCFieldSplitSchurFactType schurfactorization;
4830ad9308SMatthew Knepley   KSP                       kspschur;              /* The solver for S */
49443836d0SMatthew G Knepley   KSP                       kspupper;              /* The solver for A in the upper diagonal part of the factorization (H_2 in [El08]) */
5097bbdb24SBarry Smith   PC_FieldSplitLink         head;
5163ec74ffSBarry Smith   PetscBool                 reset;                  /* indicates PCReset() has been last called on this object, hack */
52c1570756SJed Brown   PetscBool                 suboptionsset;          /* Indicates that the KSPSetFromOptions() has been called on the sub-KSPs */
534ab8060aSDmitry Karpeev   PetscBool                 dm_splits;              /* Whether to use DMCreateFieldDecomposition() whenever possible */
540971522cSBarry Smith } PC_FieldSplit;
550971522cSBarry Smith 
5616913363SBarry Smith /*
5716913363SBarry Smith     Notes: there is no particular reason that pmat, x, and y are stored as arrays in PC_FieldSplit instead of
5816913363SBarry Smith    inside PC_FieldSplitLink, just historical. If you want to be able to add new fields after already using the
5916913363SBarry Smith    PC you could change this.
6016913363SBarry Smith */
61084e4875SJed Brown 
62e6cab6aaSJed Brown /* This helper is so that setting a user-provided preconditioning matrix is orthogonal to choosing to use it.  This way the
63084e4875SJed Brown * application-provided FormJacobian can provide this matrix without interfering with the user's (command-line) choices. */
64084e4875SJed Brown static Mat FieldSplitSchurPre(PC_FieldSplit *jac)
65084e4875SJed Brown {
66084e4875SJed Brown   switch (jac->schurpre) {
67084e4875SJed Brown   case PC_FIELDSPLIT_SCHUR_PRE_SELF: return jac->schur;
68a7476a74SDmitry Karpeev   case PC_FIELDSPLIT_SCHUR_PRE_SELFP: return jac->schurp;
69e87fab1bSBarry Smith   case PC_FIELDSPLIT_SCHUR_PRE_A11: return jac->pmat[1];
70084e4875SJed Brown   case PC_FIELDSPLIT_SCHUR_PRE_USER:   /* Use a user-provided matrix if it is given, otherwise diagonal block */
71084e4875SJed Brown   default:
72084e4875SJed Brown     return jac->schur_user ? jac->schur_user : jac->pmat[1];
73084e4875SJed Brown   }
74084e4875SJed Brown }
75084e4875SJed Brown 
76084e4875SJed Brown 
779804daf3SBarry Smith #include <petscdraw.h>
780971522cSBarry Smith #undef __FUNCT__
790971522cSBarry Smith #define __FUNCT__ "PCView_FieldSplit"
800971522cSBarry Smith static PetscErrorCode PCView_FieldSplit(PC pc,PetscViewer viewer)
810971522cSBarry Smith {
820971522cSBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
830971522cSBarry Smith   PetscErrorCode    ierr;
84d9884438SBarry Smith   PetscBool         iascii,isdraw;
850971522cSBarry Smith   PetscInt          i,j;
865a9f2f41SSatish Balay   PC_FieldSplitLink ilink = jac->head;
870971522cSBarry Smith 
880971522cSBarry Smith   PetscFunctionBegin;
89251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
90d9884438SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
910971522cSBarry Smith   if (iascii) {
922c9966d7SBarry Smith     if (jac->bs > 0) {
9351f519a2SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  FieldSplit with %s composition: total splits = %D, blocksize = %D\n",PCCompositeTypes[jac->type],jac->nsplits,jac->bs);CHKERRQ(ierr);
942c9966d7SBarry Smith     } else {
952c9966d7SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  FieldSplit with %s composition: total splits = %D\n",PCCompositeTypes[jac->type],jac->nsplits);CHKERRQ(ierr);
962c9966d7SBarry Smith     }
97f5236f50SJed Brown     if (pc->useAmat) {
98f5236f50SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  using Amat (not Pmat) as operator for blocks\n");CHKERRQ(ierr);
99a3df900dSMatthew G Knepley     }
10069a612a9SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  Solver info for each split is in the following KSP objects:\n");CHKERRQ(ierr);
1010971522cSBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1020971522cSBarry Smith     for (i=0; i<jac->nsplits; i++) {
1031ab39975SBarry Smith       if (ilink->fields) {
1040971522cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Split number %D Fields ",i);CHKERRQ(ierr);
10579416396SBarry Smith         ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
1065a9f2f41SSatish Balay         for (j=0; j<ilink->nfields; j++) {
10779416396SBarry Smith           if (j > 0) {
10879416396SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer,",");CHKERRQ(ierr);
10979416396SBarry Smith           }
1105a9f2f41SSatish Balay           ierr = PetscViewerASCIIPrintf(viewer," %D",ilink->fields[j]);CHKERRQ(ierr);
1110971522cSBarry Smith         }
1120971522cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
11379416396SBarry Smith         ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
1141ab39975SBarry Smith       } else {
1151ab39975SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Split number %D Defined by IS\n",i);CHKERRQ(ierr);
1161ab39975SBarry Smith       }
1175a9f2f41SSatish Balay       ierr  = KSPView(ilink->ksp,viewer);CHKERRQ(ierr);
1185a9f2f41SSatish Balay       ilink = ilink->next;
1190971522cSBarry Smith     }
1200971522cSBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1212fa5cd67SKarl Rupp   }
1222fa5cd67SKarl Rupp 
1232fa5cd67SKarl Rupp  if (isdraw) {
124d9884438SBarry Smith     PetscDraw draw;
125d9884438SBarry Smith     PetscReal x,y,w,wd;
126d9884438SBarry Smith 
127d9884438SBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
128d9884438SBarry Smith     ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
129d9884438SBarry Smith     w    = 2*PetscMin(1.0 - x,x);
130d9884438SBarry Smith     wd   = w/(jac->nsplits + 1);
131d9884438SBarry Smith     x    = x - wd*(jac->nsplits-1)/2.0;
132d9884438SBarry Smith     for (i=0; i<jac->nsplits; i++) {
133d9884438SBarry Smith       ierr  = PetscDrawPushCurrentPoint(draw,x,y);CHKERRQ(ierr);
134d9884438SBarry Smith       ierr  = KSPView(ilink->ksp,viewer);CHKERRQ(ierr);
135d9884438SBarry Smith       ierr  = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
136d9884438SBarry Smith       x    += wd;
137d9884438SBarry Smith       ilink = ilink->next;
138d9884438SBarry Smith     }
1390971522cSBarry Smith   }
1400971522cSBarry Smith   PetscFunctionReturn(0);
1410971522cSBarry Smith }
1420971522cSBarry Smith 
1430971522cSBarry Smith #undef __FUNCT__
1443b224e63SBarry Smith #define __FUNCT__ "PCView_FieldSplit_Schur"
1453b224e63SBarry Smith static PetscErrorCode PCView_FieldSplit_Schur(PC pc,PetscViewer viewer)
1463b224e63SBarry Smith {
1473b224e63SBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1483b224e63SBarry Smith   PetscErrorCode    ierr;
1494996c5bdSBarry Smith   PetscBool         iascii,isdraw;
1503b224e63SBarry Smith   PetscInt          i,j;
1513b224e63SBarry Smith   PC_FieldSplitLink ilink = jac->head;
1523b224e63SBarry Smith 
1533b224e63SBarry Smith   PetscFunctionBegin;
154251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1554996c5bdSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1563b224e63SBarry Smith   if (iascii) {
1572c9966d7SBarry Smith     if (jac->bs > 0) {
158c9c6ffaaSJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  FieldSplit with Schur preconditioner, blocksize = %D, factorization %s\n",jac->bs,PCFieldSplitSchurFactTypes[jac->schurfactorization]);CHKERRQ(ierr);
1592c9966d7SBarry Smith     } else {
1602c9966d7SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  FieldSplit with Schur preconditioner, factorization %s\n",PCFieldSplitSchurFactTypes[jac->schurfactorization]);CHKERRQ(ierr);
1612c9966d7SBarry Smith     }
162f5236f50SJed Brown     if (pc->useAmat) {
163f5236f50SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  using Amat (not Pmat) as operator for blocks\n");CHKERRQ(ierr);
164a3df900dSMatthew G Knepley     }
1653e8b8b31SMatthew G Knepley     switch (jac->schurpre) {
1663e8b8b31SMatthew G Knepley     case PC_FIELDSPLIT_SCHUR_PRE_SELF:
1673e8b8b31SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioner for the Schur complement formed from S itself\n");CHKERRQ(ierr);break;
168a7476a74SDmitry Karpeev     case PC_FIELDSPLIT_SCHUR_PRE_SELFP:
169a7476a74SDmitry Karpeev       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioner for the Schur complement formed from Sp, an assembled approximation to S, which uses A00's diagonal's inverse\n");CHKERRQ(ierr);break;
170e87fab1bSBarry Smith     case PC_FIELDSPLIT_SCHUR_PRE_A11:
171e87fab1bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioner for the Schur complement formed from A11\n");CHKERRQ(ierr);break;
1723e8b8b31SMatthew G Knepley     case PC_FIELDSPLIT_SCHUR_PRE_USER:
1733e8b8b31SMatthew G Knepley       if (jac->schur_user) {
1743e8b8b31SMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioner for the Schur complement formed from user provided matrix\n");CHKERRQ(ierr);
1753e8b8b31SMatthew G Knepley       } else {
176e87fab1bSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioner for the Schur complement formed from A11\n");CHKERRQ(ierr);
1773e8b8b31SMatthew G Knepley       }
1783e8b8b31SMatthew G Knepley       break;
1793e8b8b31SMatthew G Knepley     default:
18082f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Invalid Schur preconditioning type: %d", jac->schurpre);
1813e8b8b31SMatthew G Knepley     }
1823b224e63SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  Split info:\n");CHKERRQ(ierr);
1833b224e63SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1843b224e63SBarry Smith     for (i=0; i<jac->nsplits; i++) {
1853b224e63SBarry Smith       if (ilink->fields) {
1863b224e63SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Split number %D Fields ",i);CHKERRQ(ierr);
1873b224e63SBarry Smith         ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
1883b224e63SBarry Smith         for (j=0; j<ilink->nfields; j++) {
1893b224e63SBarry Smith           if (j > 0) {
1903b224e63SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer,",");CHKERRQ(ierr);
1913b224e63SBarry Smith           }
1923b224e63SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %D",ilink->fields[j]);CHKERRQ(ierr);
1933b224e63SBarry Smith         }
1943b224e63SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
1953b224e63SBarry Smith         ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
1963b224e63SBarry Smith       } else {
1973b224e63SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Split number %D Defined by IS\n",i);CHKERRQ(ierr);
1983b224e63SBarry Smith       }
1993b224e63SBarry Smith       ilink = ilink->next;
2003b224e63SBarry Smith     }
201435f959eSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"KSP solver for A00 block \n");CHKERRQ(ierr);
2023b224e63SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
203443836d0SMatthew G Knepley     ierr = KSPView(jac->head->ksp,viewer);CHKERRQ(ierr);
2043b224e63SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
205443836d0SMatthew G Knepley     if (jac->kspupper != jac->head->ksp) {
206443836d0SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer,"KSP solver for upper A00 in upper triangular factor \n");CHKERRQ(ierr);
207443836d0SMatthew G Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
208b2750c55SJed Brown       if (jac->kspupper) {ierr = KSPView(jac->kspupper,viewer);CHKERRQ(ierr);}
209b2750c55SJed Brown       else {ierr = PetscViewerASCIIPrintf(viewer,"  not yet available\n");CHKERRQ(ierr);}
210443836d0SMatthew G Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
211443836d0SMatthew G Knepley     }
212435f959eSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"KSP solver for S = A11 - A10 inv(A00) A01 \n");CHKERRQ(ierr);
2133b224e63SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
21412cae6f2SJed Brown     if (jac->kspschur) {
2153b224e63SBarry Smith       ierr = KSPView(jac->kspschur,viewer);CHKERRQ(ierr);
21612cae6f2SJed Brown     } else {
21712cae6f2SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  not yet available\n");CHKERRQ(ierr);
21812cae6f2SJed Brown     }
2193b224e63SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2203b224e63SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2214996c5bdSBarry Smith   } else if (isdraw) {
2224996c5bdSBarry Smith     PetscDraw draw;
2234996c5bdSBarry Smith     PetscReal x,y,w,wd,h;
2244996c5bdSBarry Smith     PetscInt  cnt = 2;
2254996c5bdSBarry Smith     char      str[32];
2264996c5bdSBarry Smith 
2274996c5bdSBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
2284996c5bdSBarry Smith     ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
229c74581afSBarry Smith     if (jac->kspupper != jac->head->ksp) cnt++;
230c74581afSBarry Smith     w  = 2*PetscMin(1.0 - x,x);
231c74581afSBarry Smith     wd = w/(cnt + 1);
232c74581afSBarry Smith 
2334996c5bdSBarry Smith     ierr = PetscSNPrintf(str,32,"Schur fact. %s",PCFieldSplitSchurFactTypes[jac->schurfactorization]);CHKERRQ(ierr);
2340298fd71SBarry Smith     ierr = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_RED,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
2354996c5bdSBarry Smith     y   -= h;
2364996c5bdSBarry Smith     if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_USER &&  !jac->schur_user) {
237e87fab1bSBarry Smith       ierr = PetscSNPrintf(str,32,"Prec. for Schur from %s",PCFieldSplitSchurPreTypes[PC_FIELDSPLIT_SCHUR_PRE_A11]);CHKERRQ(ierr);
2383b224e63SBarry Smith     } else {
2394996c5bdSBarry Smith       ierr = PetscSNPrintf(str,32,"Prec. for Schur from %s",PCFieldSplitSchurPreTypes[jac->schurpre]);CHKERRQ(ierr);
2404996c5bdSBarry Smith     }
2410298fd71SBarry Smith     ierr = PetscDrawBoxedString(draw,x+wd*(cnt-1)/2.0,y,PETSC_DRAW_RED,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
2424996c5bdSBarry Smith     y   -= h;
2434996c5bdSBarry Smith     x    = x - wd*(cnt-1)/2.0;
2444996c5bdSBarry Smith 
2454996c5bdSBarry Smith     ierr = PetscDrawPushCurrentPoint(draw,x,y);CHKERRQ(ierr);
2464996c5bdSBarry Smith     ierr = KSPView(jac->head->ksp,viewer);CHKERRQ(ierr);
2474996c5bdSBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2484996c5bdSBarry Smith     if (jac->kspupper != jac->head->ksp) {
2494996c5bdSBarry Smith       x   += wd;
2504996c5bdSBarry Smith       ierr = PetscDrawPushCurrentPoint(draw,x,y);CHKERRQ(ierr);
2514996c5bdSBarry Smith       ierr = KSPView(jac->kspupper,viewer);CHKERRQ(ierr);
2524996c5bdSBarry Smith       ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2534996c5bdSBarry Smith     }
2544996c5bdSBarry Smith     x   += wd;
2554996c5bdSBarry Smith     ierr = PetscDrawPushCurrentPoint(draw,x,y);CHKERRQ(ierr);
2564996c5bdSBarry Smith     ierr = KSPView(jac->kspschur,viewer);CHKERRQ(ierr);
2574996c5bdSBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2583b224e63SBarry Smith   }
2593b224e63SBarry Smith   PetscFunctionReturn(0);
2603b224e63SBarry Smith }
2613b224e63SBarry Smith 
2623b224e63SBarry Smith #undef __FUNCT__
2636c924f48SJed Brown #define __FUNCT__ "PCFieldSplitSetRuntimeSplits_Private"
2646c924f48SJed Brown /* Precondition: jac->bs is set to a meaningful value */
2656c924f48SJed Brown static PetscErrorCode PCFieldSplitSetRuntimeSplits_Private(PC pc)
2666c924f48SJed Brown {
2676c924f48SJed Brown   PetscErrorCode ierr;
2686c924f48SJed Brown   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
2695d4c12cdSJungho Lee   PetscInt       i,nfields,*ifields,nfields_col,*ifields_col;
2705d4c12cdSJungho Lee   PetscBool      flg,flg_col;
2715d4c12cdSJungho Lee   char           optionname[128],splitname[8],optionname_col[128];
2726c924f48SJed Brown 
2736c924f48SJed Brown   PetscFunctionBegin;
274785e854fSJed Brown   ierr = PetscMalloc1(jac->bs,&ifields);CHKERRQ(ierr);
275785e854fSJed Brown   ierr = PetscMalloc1(jac->bs,&ifields_col);CHKERRQ(ierr);
2766c924f48SJed Brown   for (i=0,flg=PETSC_TRUE;; i++) {
2778caf3d72SBarry Smith     ierr        = PetscSNPrintf(splitname,sizeof(splitname),"%D",i);CHKERRQ(ierr);
2788caf3d72SBarry Smith     ierr        = PetscSNPrintf(optionname,sizeof(optionname),"-pc_fieldsplit_%D_fields",i);CHKERRQ(ierr);
2798caf3d72SBarry Smith     ierr        = PetscSNPrintf(optionname_col,sizeof(optionname_col),"-pc_fieldsplit_%D_fields_col",i);CHKERRQ(ierr);
2806c924f48SJed Brown     nfields     = jac->bs;
28129499fbbSJungho Lee     nfields_col = jac->bs;
2826c924f48SJed Brown     ierr        = PetscOptionsGetIntArray(((PetscObject)pc)->prefix,optionname,ifields,&nfields,&flg);CHKERRQ(ierr);
2835d4c12cdSJungho Lee     ierr        = PetscOptionsGetIntArray(((PetscObject)pc)->prefix,optionname_col,ifields_col,&nfields_col,&flg_col);CHKERRQ(ierr);
2846c924f48SJed Brown     if (!flg) break;
2855d4c12cdSJungho Lee     else if (flg && !flg_col) {
2865d4c12cdSJungho Lee       if (!nfields) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Cannot list zero fields");
2875d4c12cdSJungho Lee       ierr = PCFieldSplitSetFields(pc,splitname,nfields,ifields,ifields);CHKERRQ(ierr);
2882fa5cd67SKarl Rupp     } else {
2895d4c12cdSJungho Lee       if (!nfields || !nfields_col) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Cannot list zero fields");
2905d4c12cdSJungho Lee       if (nfields != nfields_col) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Number of row and column fields must match");
2915d4c12cdSJungho Lee       ierr = PCFieldSplitSetFields(pc,splitname,nfields,ifields,ifields_col);CHKERRQ(ierr);
2925d4c12cdSJungho Lee     }
2936c924f48SJed Brown   }
2946c924f48SJed Brown   if (i > 0) {
2956c924f48SJed Brown     /* Makes command-line setting of splits take precedence over setting them in code.
2966c924f48SJed Brown        Otherwise subsequent calls to PCFieldSplitSetIS() or PCFieldSplitSetFields() would
2976c924f48SJed Brown        create new splits, which would probably not be what the user wanted. */
2986c924f48SJed Brown     jac->splitdefined = PETSC_TRUE;
2996c924f48SJed Brown   }
3006c924f48SJed Brown   ierr = PetscFree(ifields);CHKERRQ(ierr);
3015d4c12cdSJungho Lee   ierr = PetscFree(ifields_col);CHKERRQ(ierr);
3026c924f48SJed Brown   PetscFunctionReturn(0);
3036c924f48SJed Brown }
3046c924f48SJed Brown 
3056c924f48SJed Brown #undef __FUNCT__
30669a612a9SBarry Smith #define __FUNCT__ "PCFieldSplitSetDefaults"
30769a612a9SBarry Smith static PetscErrorCode PCFieldSplitSetDefaults(PC pc)
3080971522cSBarry Smith {
3090971522cSBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
3100971522cSBarry Smith   PetscErrorCode    ierr;
3115a9f2f41SSatish Balay   PC_FieldSplitLink ilink              = jac->head;
3127287d2a3SDmitry Karpeev   PetscBool         fieldsplit_default = PETSC_FALSE,stokes = PETSC_FALSE;
3136c924f48SJed Brown   PetscInt          i;
3140971522cSBarry Smith 
3150971522cSBarry Smith   PetscFunctionBegin;
3167287d2a3SDmitry Karpeev   /*
3177287d2a3SDmitry Karpeev    Kinda messy, but at least this now uses DMCreateFieldDecomposition() even with jac->reset.
3187287d2a3SDmitry Karpeev    Should probably be rewritten.
3197287d2a3SDmitry Karpeev    */
3207287d2a3SDmitry Karpeev   if (!ilink || jac->reset) {
3210298fd71SBarry Smith     ierr = PetscOptionsGetBool(((PetscObject)pc)->prefix,"-pc_fieldsplit_detect_saddle_point",&stokes,NULL);CHKERRQ(ierr);
3224ab8060aSDmitry Karpeev     if (pc->dm && jac->dm_splits && !stokes) {
323bafc1b83SMatthew G Knepley       PetscInt  numFields, f, i, j;
3240784a22cSJed Brown       char      **fieldNames;
3257b62db95SJungho Lee       IS        *fields;
326e7c4fc90SDmitry Karpeev       DM        *dms;
327bafc1b83SMatthew G Knepley       DM        subdm[128];
328bafc1b83SMatthew G Knepley       PetscBool flg;
329bafc1b83SMatthew G Knepley 
33016621825SDmitry Karpeev       ierr = DMCreateFieldDecomposition(pc->dm, &numFields, &fieldNames, &fields, &dms);CHKERRQ(ierr);
331bafc1b83SMatthew G Knepley       /* Allow the user to prescribe the splits */
332bafc1b83SMatthew G Knepley       for (i = 0, flg = PETSC_TRUE;; i++) {
333bafc1b83SMatthew G Knepley         PetscInt ifields[128];
334bafc1b83SMatthew G Knepley         IS       compField;
335bafc1b83SMatthew G Knepley         char     optionname[128], splitname[8];
336bafc1b83SMatthew G Knepley         PetscInt nfields = numFields;
337bafc1b83SMatthew G Knepley 
3388caf3d72SBarry Smith         ierr = PetscSNPrintf(optionname, sizeof(optionname), "-pc_fieldsplit_%D_fields", i);CHKERRQ(ierr);
339bafc1b83SMatthew G Knepley         ierr = PetscOptionsGetIntArray(((PetscObject) pc)->prefix, optionname, ifields, &nfields, &flg);CHKERRQ(ierr);
340bafc1b83SMatthew G Knepley         if (!flg) break;
34182f516ccSBarry Smith         if (numFields > 128) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Cannot currently support %d > 128 fields", numFields);
342bafc1b83SMatthew G Knepley         ierr = DMCreateSubDM(pc->dm, nfields, ifields, &compField, &subdm[i]);CHKERRQ(ierr);
343bafc1b83SMatthew G Knepley         if (nfields == 1) {
344bafc1b83SMatthew G Knepley           ierr = PCFieldSplitSetIS(pc, fieldNames[ifields[0]], compField);CHKERRQ(ierr);
34582f516ccSBarry Smith           /* ierr = PetscPrintf(PetscObjectComm((PetscObject)pc), "%s Field Indices:", fieldNames[ifields[0]]);CHKERRQ(ierr);
3460298fd71SBarry Smith              ierr = ISView(compField, NULL);CHKERRQ(ierr); */
347bafc1b83SMatthew G Knepley         } else {
3488caf3d72SBarry Smith           ierr = PetscSNPrintf(splitname, sizeof(splitname), "%D", i);CHKERRQ(ierr);
349bafc1b83SMatthew G Knepley           ierr = PCFieldSplitSetIS(pc, splitname, compField);CHKERRQ(ierr);
35082f516ccSBarry Smith           /* ierr = PetscPrintf(PetscObjectComm((PetscObject)pc), "%s Field Indices:", splitname);CHKERRQ(ierr);
3510298fd71SBarry Smith              ierr = ISView(compField, NULL);CHKERRQ(ierr); */
3527287d2a3SDmitry Karpeev         }
353bafc1b83SMatthew G Knepley         ierr = ISDestroy(&compField);CHKERRQ(ierr);
354bafc1b83SMatthew G Knepley         for (j = 0; j < nfields; ++j) {
355bafc1b83SMatthew G Knepley           f    = ifields[j];
3567b62db95SJungho Lee           ierr = PetscFree(fieldNames[f]);CHKERRQ(ierr);
3577b62db95SJungho Lee           ierr = ISDestroy(&fields[f]);CHKERRQ(ierr);
3587b62db95SJungho Lee         }
359bafc1b83SMatthew G Knepley       }
360bafc1b83SMatthew G Knepley       if (i == 0) {
361bafc1b83SMatthew G Knepley         for (f = 0; f < numFields; ++f) {
362bafc1b83SMatthew G Knepley           ierr = PCFieldSplitSetIS(pc, fieldNames[f], fields[f]);CHKERRQ(ierr);
363bafc1b83SMatthew G Knepley           ierr = PetscFree(fieldNames[f]);CHKERRQ(ierr);
364bafc1b83SMatthew G Knepley           ierr = ISDestroy(&fields[f]);CHKERRQ(ierr);
365bafc1b83SMatthew G Knepley         }
366bafc1b83SMatthew G Knepley       } else {
367d724dfffSBarry Smith         for (j=0; j<numFields; j++) {
368d724dfffSBarry Smith           ierr = DMDestroy(dms+j);CHKERRQ(ierr);
369d724dfffSBarry Smith         }
370d724dfffSBarry Smith         ierr = PetscFree(dms);CHKERRQ(ierr);
371785e854fSJed Brown         ierr = PetscMalloc1(i, &dms);CHKERRQ(ierr);
3722fa5cd67SKarl Rupp         for (j = 0; j < i; ++j) dms[j] = subdm[j];
373bafc1b83SMatthew G Knepley       }
3747b62db95SJungho Lee       ierr = PetscFree(fieldNames);CHKERRQ(ierr);
3757b62db95SJungho Lee       ierr = PetscFree(fields);CHKERRQ(ierr);
376e7c4fc90SDmitry Karpeev       if (dms) {
3778b8307b2SJed Brown         ierr = PetscInfo(pc, "Setting up physics based fieldsplit preconditioner using the embedded DM\n");CHKERRQ(ierr);
378bafc1b83SMatthew G Knepley         for (ilink = jac->head, i = 0; ilink; ilink = ilink->next, ++i) {
3797287d2a3SDmitry Karpeev           const char *prefix;
3807287d2a3SDmitry Karpeev           ierr = PetscObjectGetOptionsPrefix((PetscObject)(ilink->ksp),&prefix);CHKERRQ(ierr);
3817287d2a3SDmitry Karpeev           ierr = PetscObjectSetOptionsPrefix((PetscObject)(dms[i]), prefix);CHKERRQ(ierr);
3827b62db95SJungho Lee           ierr = KSPSetDM(ilink->ksp, dms[i]);CHKERRQ(ierr);
3837b62db95SJungho Lee           ierr = KSPSetDMActive(ilink->ksp, PETSC_FALSE);CHKERRQ(ierr);
3847287d2a3SDmitry Karpeev           ierr = PetscObjectIncrementTabLevel((PetscObject)dms[i],(PetscObject)ilink->ksp,0);CHKERRQ(ierr);
385e7c4fc90SDmitry Karpeev           ierr = DMDestroy(&dms[i]);CHKERRQ(ierr);
3862fa5ba8aSJed Brown         }
3877b62db95SJungho Lee         ierr = PetscFree(dms);CHKERRQ(ierr);
3888b8307b2SJed Brown       }
38966ffff09SJed Brown     } else {
390521d7252SBarry Smith       if (jac->bs <= 0) {
391704ba839SBarry Smith         if (pc->pmat) {
392521d7252SBarry Smith           ierr = MatGetBlockSize(pc->pmat,&jac->bs);CHKERRQ(ierr);
3932fa5cd67SKarl Rupp         } else jac->bs = 1;
394521d7252SBarry Smith       }
395d32f9abdSBarry Smith 
3966ce1633cSBarry Smith       if (stokes) {
3976ce1633cSBarry Smith         IS       zerodiags,rest;
3986ce1633cSBarry Smith         PetscInt nmin,nmax;
3996ce1633cSBarry Smith 
4006ce1633cSBarry Smith         ierr = MatGetOwnershipRange(pc->mat,&nmin,&nmax);CHKERRQ(ierr);
4016ce1633cSBarry Smith         ierr = MatFindZeroDiagonals(pc->mat,&zerodiags);CHKERRQ(ierr);
4026ce1633cSBarry Smith         ierr = ISComplement(zerodiags,nmin,nmax,&rest);CHKERRQ(ierr);
4037287d2a3SDmitry Karpeev         if (jac->reset) {
4047287d2a3SDmitry Karpeev           jac->head->is       = rest;
4057287d2a3SDmitry Karpeev           jac->head->next->is = zerodiags;
4062fa5cd67SKarl Rupp         } else {
4076ce1633cSBarry Smith           ierr = PCFieldSplitSetIS(pc,"0",rest);CHKERRQ(ierr);
4086ce1633cSBarry Smith           ierr = PCFieldSplitSetIS(pc,"1",zerodiags);CHKERRQ(ierr);
4097287d2a3SDmitry Karpeev         }
410fcfd50ebSBarry Smith         ierr = ISDestroy(&zerodiags);CHKERRQ(ierr);
411fcfd50ebSBarry Smith         ierr = ISDestroy(&rest);CHKERRQ(ierr);
4126ce1633cSBarry Smith       } else {
413ce94432eSBarry Smith         if (jac->reset) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Cases not yet handled when PCReset() was used");
4149eeaaa73SJed Brown         ierr = PetscOptionsGetBool(((PetscObject)pc)->prefix,"-pc_fieldsplit_default",&fieldsplit_default,NULL);CHKERRQ(ierr);
4157287d2a3SDmitry Karpeev         if (!fieldsplit_default) {
416d32f9abdSBarry Smith           /* Allow user to set fields from command line,  if bs was known at the time of PCSetFromOptions_FieldSplit()
417d32f9abdSBarry Smith            then it is set there. This is not ideal because we should only have options set in XXSetFromOptions(). */
4186c924f48SJed Brown           ierr = PCFieldSplitSetRuntimeSplits_Private(pc);CHKERRQ(ierr);
4196c924f48SJed Brown           if (jac->splitdefined) {ierr = PetscInfo(pc,"Splits defined using the options database\n");CHKERRQ(ierr);}
420d32f9abdSBarry Smith         }
4217287d2a3SDmitry Karpeev         if (fieldsplit_default || !jac->splitdefined) {
422d32f9abdSBarry Smith           ierr = PetscInfo(pc,"Using default splitting of fields\n");CHKERRQ(ierr);
423db4c96c1SJed Brown           for (i=0; i<jac->bs; i++) {
4246c924f48SJed Brown             char splitname[8];
4258caf3d72SBarry Smith             ierr = PetscSNPrintf(splitname,sizeof(splitname),"%D",i);CHKERRQ(ierr);
4265d4c12cdSJungho Lee             ierr = PCFieldSplitSetFields(pc,splitname,1,&i,&i);CHKERRQ(ierr);
42779416396SBarry Smith           }
4285d4c12cdSJungho Lee           jac->defaultsplit = PETSC_TRUE;
429521d7252SBarry Smith         }
43066ffff09SJed Brown       }
4316ce1633cSBarry Smith     }
432edf189efSBarry Smith   } else if (jac->nsplits == 1) {
433edf189efSBarry Smith     if (ilink->is) {
434edf189efSBarry Smith       IS       is2;
435edf189efSBarry Smith       PetscInt nmin,nmax;
436edf189efSBarry Smith 
437edf189efSBarry Smith       ierr = MatGetOwnershipRange(pc->mat,&nmin,&nmax);CHKERRQ(ierr);
438edf189efSBarry Smith       ierr = ISComplement(ilink->is,nmin,nmax,&is2);CHKERRQ(ierr);
439db4c96c1SJed Brown       ierr = PCFieldSplitSetIS(pc,"1",is2);CHKERRQ(ierr);
440fcfd50ebSBarry Smith       ierr = ISDestroy(&is2);CHKERRQ(ierr);
44182f516ccSBarry Smith     } else SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Must provide at least two sets of fields to PCFieldSplit()");
442edf189efSBarry Smith   }
443d0af7cd3SBarry Smith 
444d0af7cd3SBarry Smith 
44582f516ccSBarry Smith   if (jac->nsplits < 2) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_PLIB,"Unhandled case, must have at least two fields, not %d", jac->nsplits);
44669a612a9SBarry Smith   PetscFunctionReturn(0);
44769a612a9SBarry Smith }
44869a612a9SBarry Smith 
4495a576424SJed Brown PETSC_EXTERN PetscErrorCode PetscOptionsFindPairPrefix_Private(const char pre[], const char name[], char *value[], PetscBool *flg);
450514bf10dSMatthew G Knepley 
45169a612a9SBarry Smith #undef __FUNCT__
45269a612a9SBarry Smith #define __FUNCT__ "PCSetUp_FieldSplit"
45369a612a9SBarry Smith static PetscErrorCode PCSetUp_FieldSplit(PC pc)
45469a612a9SBarry Smith {
45569a612a9SBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
45669a612a9SBarry Smith   PetscErrorCode    ierr;
4575a9f2f41SSatish Balay   PC_FieldSplitLink ilink;
4582c9966d7SBarry Smith   PetscInt          i,nsplit;
45969a612a9SBarry Smith   MatStructure      flag = pc->flag;
4605f4ab4e1SJungho Lee   PetscBool         sorted, sorted_col;
46169a612a9SBarry Smith 
46269a612a9SBarry Smith   PetscFunctionBegin;
46369a612a9SBarry Smith   ierr   = PCFieldSplitSetDefaults(pc);CHKERRQ(ierr);
46497bbdb24SBarry Smith   nsplit = jac->nsplits;
4655a9f2f41SSatish Balay   ilink  = jac->head;
46697bbdb24SBarry Smith 
46797bbdb24SBarry Smith   /* get the matrices for each split */
468704ba839SBarry Smith   if (!jac->issetup) {
4691b9fc7fcSBarry Smith     PetscInt rstart,rend,nslots,bs;
47097bbdb24SBarry Smith 
471704ba839SBarry Smith     jac->issetup = PETSC_TRUE;
472704ba839SBarry Smith 
4735d4c12cdSJungho Lee     /* This is done here instead of in PCFieldSplitSetFields() because may not have matrix at that point */
4742c9966d7SBarry Smith     if (jac->defaultsplit || !ilink->is) {
4752c9966d7SBarry Smith       if (jac->bs <= 0) jac->bs = nsplit;
4762c9966d7SBarry Smith     }
47751f519a2SBarry Smith     bs     = jac->bs;
47897bbdb24SBarry Smith     ierr   = MatGetOwnershipRange(pc->pmat,&rstart,&rend);CHKERRQ(ierr);
4791b9fc7fcSBarry Smith     nslots = (rend - rstart)/bs;
4801b9fc7fcSBarry Smith     for (i=0; i<nsplit; i++) {
4811b9fc7fcSBarry Smith       if (jac->defaultsplit) {
482ce94432eSBarry Smith         ierr = ISCreateStride(PetscObjectComm((PetscObject)pc),nslots,rstart+i,nsplit,&ilink->is);CHKERRQ(ierr);
4835f4ab4e1SJungho Lee         ierr = ISDuplicate(ilink->is,&ilink->is_col);CHKERRQ(ierr);
484704ba839SBarry Smith       } else if (!ilink->is) {
485ccb205f8SBarry Smith         if (ilink->nfields > 1) {
4865f4ab4e1SJungho Lee           PetscInt *ii,*jj,j,k,nfields = ilink->nfields,*fields = ilink->fields,*fields_col = ilink->fields_col;
487785e854fSJed Brown           ierr = PetscMalloc1(ilink->nfields*nslots,&ii);CHKERRQ(ierr);
488785e854fSJed Brown           ierr = PetscMalloc1(ilink->nfields*nslots,&jj);CHKERRQ(ierr);
4891b9fc7fcSBarry Smith           for (j=0; j<nslots; j++) {
4901b9fc7fcSBarry Smith             for (k=0; k<nfields; k++) {
4911b9fc7fcSBarry Smith               ii[nfields*j + k] = rstart + bs*j + fields[k];
4925f4ab4e1SJungho Lee               jj[nfields*j + k] = rstart + bs*j + fields_col[k];
49397bbdb24SBarry Smith             }
49497bbdb24SBarry Smith           }
495ce94432eSBarry Smith           ierr = ISCreateGeneral(PetscObjectComm((PetscObject)pc),nslots*nfields,ii,PETSC_OWN_POINTER,&ilink->is);CHKERRQ(ierr);
496ce94432eSBarry Smith           ierr = ISCreateGeneral(PetscObjectComm((PetscObject)pc),nslots*nfields,jj,PETSC_OWN_POINTER,&ilink->is_col);CHKERRQ(ierr);
497ccb205f8SBarry Smith         } else {
498ce94432eSBarry Smith           ierr = ISCreateStride(PetscObjectComm((PetscObject)pc),nslots,rstart+ilink->fields[0],bs,&ilink->is);CHKERRQ(ierr);
499ce94432eSBarry Smith           ierr = ISCreateStride(PetscObjectComm((PetscObject)pc),nslots,rstart+ilink->fields_col[0],bs,&ilink->is_col);CHKERRQ(ierr);
500ccb205f8SBarry Smith        }
5013e197d65SBarry Smith       }
502704ba839SBarry Smith       ierr = ISSorted(ilink->is,&sorted);CHKERRQ(ierr);
5035f4ab4e1SJungho Lee       if (ilink->is_col) { ierr = ISSorted(ilink->is_col,&sorted_col);CHKERRQ(ierr); }
5045f4ab4e1SJungho Lee       if (!sorted || !sorted_col) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Fields must be sorted when creating split");
505704ba839SBarry Smith       ilink = ilink->next;
5061b9fc7fcSBarry Smith     }
5071b9fc7fcSBarry Smith   }
5081b9fc7fcSBarry Smith 
509704ba839SBarry Smith   ilink = jac->head;
51097bbdb24SBarry Smith   if (!jac->pmat) {
511bdddcaaaSMatthew G Knepley     Vec xtmp;
512bdddcaaaSMatthew G Knepley 
5130298fd71SBarry Smith     ierr = MatGetVecs(pc->pmat,&xtmp,NULL);CHKERRQ(ierr);
514785e854fSJed Brown     ierr = PetscMalloc1(nsplit,&jac->pmat);CHKERRQ(ierr);
515dcca6d9dSJed Brown     ierr = PetscMalloc2(nsplit,&jac->x,nsplit,&jac->y);CHKERRQ(ierr);
516cf502942SBarry Smith     for (i=0; i<nsplit; i++) {
517bdddcaaaSMatthew G Knepley       MatNullSpace sp;
518bdddcaaaSMatthew G Knepley 
519a3df900dSMatthew G Knepley       /* Check for preconditioning matrix attached to IS */
520a3df900dSMatthew G Knepley       ierr = PetscObjectQuery((PetscObject) ilink->is, "pmat", (PetscObject*) &jac->pmat[i]);CHKERRQ(ierr);
521a3df900dSMatthew G Knepley       if (jac->pmat[i]) {
522a3df900dSMatthew G Knepley         ierr = PetscObjectReference((PetscObject) jac->pmat[i]);CHKERRQ(ierr);
523a3df900dSMatthew G Knepley         if (jac->type == PC_COMPOSITE_SCHUR) {
524a3df900dSMatthew G Knepley           jac->schur_user = jac->pmat[i];
5252fa5cd67SKarl Rupp 
526a3df900dSMatthew G Knepley           ierr = PetscObjectReference((PetscObject) jac->schur_user);CHKERRQ(ierr);
527a3df900dSMatthew G Knepley         }
528a3df900dSMatthew G Knepley       } else {
5295f4ab4e1SJungho Lee         ierr = MatGetSubMatrix(pc->pmat,ilink->is,ilink->is_col,MAT_INITIAL_MATRIX,&jac->pmat[i]);CHKERRQ(ierr);
530a3df900dSMatthew G Knepley       }
531bdddcaaaSMatthew G Knepley       /* create work vectors for each split */
532bdddcaaaSMatthew G Knepley       ierr     = MatGetVecs(jac->pmat[i],&jac->x[i],&jac->y[i]);CHKERRQ(ierr);
5330298fd71SBarry Smith       ilink->x = jac->x[i]; ilink->y = jac->y[i]; ilink->z = NULL;
534bdddcaaaSMatthew G Knepley       /* compute scatter contexts needed by multiplicative versions and non-default splits */
5350298fd71SBarry Smith       ierr = VecScatterCreate(xtmp,ilink->is,jac->x[i],NULL,&ilink->sctx);CHKERRQ(ierr);
536ed1f0337SMatthew G Knepley       /* Check for null space attached to IS */
537bafc1b83SMatthew G Knepley       ierr = PetscObjectQuery((PetscObject) ilink->is, "nullspace", (PetscObject*) &sp);CHKERRQ(ierr);
538bafc1b83SMatthew G Knepley       if (sp) {
539bafc1b83SMatthew G Knepley         ierr = MatSetNullSpace(jac->pmat[i], sp);CHKERRQ(ierr);
540bafc1b83SMatthew G Knepley       }
541ed1f0337SMatthew G Knepley       ierr = PetscObjectQuery((PetscObject) ilink->is, "nearnullspace", (PetscObject*) &sp);CHKERRQ(ierr);
542ed1f0337SMatthew G Knepley       if (sp) {
543ed1f0337SMatthew G Knepley         ierr = MatSetNearNullSpace(jac->pmat[i], sp);CHKERRQ(ierr);
544ed1f0337SMatthew G Knepley       }
545704ba839SBarry Smith       ilink = ilink->next;
546cf502942SBarry Smith     }
547bdddcaaaSMatthew G Knepley     ierr = VecDestroy(&xtmp);CHKERRQ(ierr);
54897bbdb24SBarry Smith   } else {
549cf502942SBarry Smith     for (i=0; i<nsplit; i++) {
550a3df900dSMatthew G Knepley       Mat pmat;
551a3df900dSMatthew G Knepley 
552a3df900dSMatthew G Knepley       /* Check for preconditioning matrix attached to IS */
553a3df900dSMatthew G Knepley       ierr = PetscObjectQuery((PetscObject) ilink->is, "pmat", (PetscObject*) &pmat);CHKERRQ(ierr);
554a3df900dSMatthew G Knepley       if (!pmat) {
5555f4ab4e1SJungho Lee         ierr = MatGetSubMatrix(pc->pmat,ilink->is,ilink->is_col,MAT_REUSE_MATRIX,&jac->pmat[i]);CHKERRQ(ierr);
556a3df900dSMatthew G Knepley       }
557704ba839SBarry Smith       ilink = ilink->next;
558cf502942SBarry Smith     }
55997bbdb24SBarry Smith   }
560f5236f50SJed Brown   if (pc->useAmat) {
561519d70e2SJed Brown     ilink = jac->head;
562519d70e2SJed Brown     if (!jac->mat) {
563785e854fSJed Brown       ierr = PetscMalloc1(nsplit,&jac->mat);CHKERRQ(ierr);
564519d70e2SJed Brown       for (i=0; i<nsplit; i++) {
5655f4ab4e1SJungho Lee         ierr  = MatGetSubMatrix(pc->mat,ilink->is,ilink->is_col,MAT_INITIAL_MATRIX,&jac->mat[i]);CHKERRQ(ierr);
566519d70e2SJed Brown         ilink = ilink->next;
567519d70e2SJed Brown       }
568519d70e2SJed Brown     } else {
569519d70e2SJed Brown       for (i=0; i<nsplit; i++) {
5705f4ab4e1SJungho Lee         if (jac->mat[i]) {ierr = MatGetSubMatrix(pc->mat,ilink->is,ilink->is_col,MAT_REUSE_MATRIX,&jac->mat[i]);CHKERRQ(ierr);}
571519d70e2SJed Brown         ilink = ilink->next;
572519d70e2SJed Brown       }
573519d70e2SJed Brown     }
574519d70e2SJed Brown   } else {
575519d70e2SJed Brown     jac->mat = jac->pmat;
576519d70e2SJed Brown   }
57797bbdb24SBarry Smith 
5786c8605c2SJed Brown   if (jac->type != PC_COMPOSITE_ADDITIVE  && jac->type != PC_COMPOSITE_SCHUR) {
57968dd23aaSBarry Smith     /* extract the rows of the matrix associated with each field: used for efficient computation of residual inside algorithm */
58068dd23aaSBarry Smith     ilink = jac->head;
58168dd23aaSBarry Smith     if (!jac->Afield) {
582785e854fSJed Brown       ierr = PetscMalloc1(nsplit,&jac->Afield);CHKERRQ(ierr);
58368dd23aaSBarry Smith       for (i=0; i<nsplit; i++) {
5840298fd71SBarry Smith         ierr  = MatGetSubMatrix(pc->mat,ilink->is,NULL,MAT_INITIAL_MATRIX,&jac->Afield[i]);CHKERRQ(ierr);
58568dd23aaSBarry Smith         ilink = ilink->next;
58668dd23aaSBarry Smith       }
58768dd23aaSBarry Smith     } else {
58868dd23aaSBarry Smith       for (i=0; i<nsplit; i++) {
5890298fd71SBarry Smith         ierr  = MatGetSubMatrix(pc->mat,ilink->is,NULL,MAT_REUSE_MATRIX,&jac->Afield[i]);CHKERRQ(ierr);
59068dd23aaSBarry Smith         ilink = ilink->next;
59168dd23aaSBarry Smith       }
59268dd23aaSBarry Smith     }
59368dd23aaSBarry Smith   }
59468dd23aaSBarry Smith 
5953b224e63SBarry Smith   if (jac->type == PC_COMPOSITE_SCHUR) {
5963b224e63SBarry Smith     IS          ccis;
5974aa3045dSJed Brown     PetscInt    rstart,rend;
598093c86ffSJed Brown     char        lscname[256];
599093c86ffSJed Brown     PetscObject LSC_L;
600ce94432eSBarry Smith 
601ce94432eSBarry Smith     if (nsplit != 2) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_INCOMP,"To use Schur complement preconditioner you must have exactly 2 fields");
60268dd23aaSBarry Smith 
603e6cab6aaSJed Brown     /* When extracting off-diagonal submatrices, we take complements from this range */
604e6cab6aaSJed Brown     ierr = MatGetOwnershipRangeColumn(pc->mat,&rstart,&rend);CHKERRQ(ierr);
605e6cab6aaSJed Brown 
6063b224e63SBarry Smith     /* need to handle case when one is resetting up the preconditioner */
6073b224e63SBarry Smith     if (jac->schur) {
6080298fd71SBarry Smith       KSP kspA = jac->head->ksp, kspInner = NULL, kspUpper = jac->kspupper;
609443836d0SMatthew G Knepley 
610fb3147dbSMatthew G Knepley       ierr  = MatSchurComplementGetKSP(jac->schur, &kspInner);CHKERRQ(ierr);
6113b224e63SBarry Smith       ilink = jac->head;
61249bb4cd7SJungho Lee       ierr  = ISComplement(ilink->is_col,rstart,rend,&ccis);CHKERRQ(ierr);
6134aa3045dSJed Brown       ierr  = MatGetSubMatrix(pc->mat,ilink->is,ccis,MAT_REUSE_MATRIX,&jac->B);CHKERRQ(ierr);
614fcfd50ebSBarry Smith       ierr  = ISDestroy(&ccis);CHKERRQ(ierr);
6153b224e63SBarry Smith       ilink = ilink->next;
61649bb4cd7SJungho Lee       ierr  = ISComplement(ilink->is_col,rstart,rend,&ccis);CHKERRQ(ierr);
6174aa3045dSJed Brown       ierr  = MatGetSubMatrix(pc->mat,ilink->is,ccis,MAT_REUSE_MATRIX,&jac->C);CHKERRQ(ierr);
618fcfd50ebSBarry Smith       ierr  = ISDestroy(&ccis);CHKERRQ(ierr);
619a3df900dSMatthew G Knepley       ierr  = MatSchurComplementUpdate(jac->schur,jac->mat[0],jac->pmat[0],jac->B,jac->C,jac->mat[1],pc->flag);CHKERRQ(ierr);
620a7476a74SDmitry Karpeev       if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELFP) {
621a7476a74SDmitry Karpeev 	ierr = MatDestroy(&jac->schurp);CHKERRQ(ierr);
622a7476a74SDmitry Karpeev 	ierr = MatSchurComplementGetPmat(jac->schur,MAT_INITIAL_MATRIX,&jac->schurp);CHKERRQ(ierr);
623a7476a74SDmitry Karpeev       }
624443836d0SMatthew G Knepley       if (kspA != kspInner) {
625443836d0SMatthew G Knepley         ierr = KSPSetOperators(kspA,jac->mat[0],jac->pmat[0],pc->flag);CHKERRQ(ierr);
626443836d0SMatthew G Knepley       }
627443836d0SMatthew G Knepley       if (kspUpper != kspA) {
628443836d0SMatthew G Knepley         ierr = KSPSetOperators(kspUpper,jac->mat[0],jac->pmat[0],pc->flag);CHKERRQ(ierr);
629443836d0SMatthew G Knepley       }
630084e4875SJed Brown       ierr = KSPSetOperators(jac->kspschur,jac->schur,FieldSplitSchurPre(jac),pc->flag);CHKERRQ(ierr);
6313b224e63SBarry Smith     } else {
632bafc1b83SMatthew G Knepley       const char   *Dprefix;
633470b340bSDmitry Karpeev       char         schurprefix[256], schurmatprefix[256];
634514bf10dSMatthew G Knepley       char         schurtestoption[256];
635bdddcaaaSMatthew G Knepley       MatNullSpace sp;
636514bf10dSMatthew G Knepley       PetscBool    flg;
6373b224e63SBarry Smith 
638a04f6461SBarry Smith       /* extract the A01 and A10 matrices */
6393b224e63SBarry Smith       ilink = jac->head;
64049bb4cd7SJungho Lee       ierr  = ISComplement(ilink->is_col,rstart,rend,&ccis);CHKERRQ(ierr);
6414aa3045dSJed Brown       ierr  = MatGetSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->B);CHKERRQ(ierr);
642fcfd50ebSBarry Smith       ierr  = ISDestroy(&ccis);CHKERRQ(ierr);
6433b224e63SBarry Smith       ilink = ilink->next;
64449bb4cd7SJungho Lee       ierr  = ISComplement(ilink->is_col,rstart,rend,&ccis);CHKERRQ(ierr);
6454aa3045dSJed Brown       ierr  = MatGetSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->C);CHKERRQ(ierr);
646fcfd50ebSBarry Smith       ierr  = ISDestroy(&ccis);CHKERRQ(ierr);
64720252d06SBarry Smith 
648f5236f50SJed Brown       /* Use mat[0] (diagonal block of Amat) preconditioned by pmat[0] to define Schur complement */
64920252d06SBarry Smith       ierr = MatCreate(((PetscObject)jac->mat[0])->comm,&jac->schur);CHKERRQ(ierr);
65020252d06SBarry Smith       ierr = MatSetType(jac->schur,MATSCHURCOMPLEMENT);CHKERRQ(ierr);
65120252d06SBarry Smith       ierr = MatSchurComplementSet(jac->schur,jac->mat[0],jac->pmat[0],jac->B,jac->C,jac->mat[1]);CHKERRQ(ierr);
652470b340bSDmitry Karpeev       ierr = PetscSNPrintf(schurmatprefix, sizeof(schurmatprefix), "%sfieldsplit_%s_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname);CHKERRQ(ierr);
653470b340bSDmitry Karpeev       /* 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? */
654470b340bSDmitry Karpeev       ierr = MatSetOptionsPrefix(jac->schur,schurmatprefix);CHKERRQ(ierr);
655470b340bSDmitry Karpeev       ierr = MatSetFromOptions(jac->schur);CHKERRQ(ierr);
656bdddcaaaSMatthew G Knepley       ierr = MatGetNullSpace(jac->pmat[1], &sp);CHKERRQ(ierr);
65720252d06SBarry Smith       if (sp) {
65820252d06SBarry Smith         ierr = MatSetNullSpace(jac->schur, sp);CHKERRQ(ierr);
65920252d06SBarry Smith       }
66020252d06SBarry Smith 
66120252d06SBarry Smith       ierr = PetscSNPrintf(schurtestoption, sizeof(schurtestoption), "-fieldsplit_%s_inner_", ilink->splitname);CHKERRQ(ierr);
6620298fd71SBarry Smith       ierr = PetscOptionsFindPairPrefix_Private(((PetscObject)pc)->prefix, schurtestoption, NULL, &flg);CHKERRQ(ierr);
663514bf10dSMatthew G Knepley       if (flg) {
664514bf10dSMatthew G Knepley         DM  dmInner;
66521635b76SJed Brown         KSP kspInner;
66621635b76SJed Brown 
66721635b76SJed Brown         ierr = MatSchurComplementGetKSP(jac->schur, &kspInner);CHKERRQ(ierr);
66821635b76SJed Brown         ierr = PetscSNPrintf(schurprefix, sizeof(schurprefix), "%sfieldsplit_%s_inner_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname);CHKERRQ(ierr);
66921635b76SJed Brown         /* Indent this deeper to emphasize the "inner" nature of this solver. */
67021635b76SJed Brown         ierr = PetscObjectIncrementTabLevel((PetscObject)kspInner, (PetscObject) pc, 2);CHKERRQ(ierr);
67121635b76SJed Brown         ierr = KSPSetOptionsPrefix(kspInner, schurprefix);CHKERRQ(ierr);
672514bf10dSMatthew G Knepley 
673514bf10dSMatthew G Knepley         /* Set DM for new solver */
674bafc1b83SMatthew G Knepley         ierr = KSPGetDM(jac->head->ksp, &dmInner);CHKERRQ(ierr);
67521635b76SJed Brown         ierr = KSPSetDM(kspInner, dmInner);CHKERRQ(ierr);
67621635b76SJed Brown         ierr = KSPSetDMActive(kspInner, PETSC_FALSE);CHKERRQ(ierr);
677514bf10dSMatthew G Knepley       } else {
67821635b76SJed Brown          /* Use the outer solver for the inner solve, but revert the KSPPREONLY from PCFieldSplitSetFields_FieldSplit or
67921635b76SJed Brown           * PCFieldSplitSetIS_FieldSplit. We don't want KSPPREONLY because it makes the Schur complement inexact,
68021635b76SJed Brown           * preventing Schur complement reduction to be an accurate solve. Usually when an iterative solver is used for
68121635b76SJed Brown           * S = D - C A_inner^{-1} B, we expect S to be defined using an accurate definition of A_inner^{-1}, so we make
68221635b76SJed Brown           * GMRES the default. Note that it is also common to use PREONLY for S, in which case S may not be used
68321635b76SJed Brown           * directly, and the user is responsible for setting an inexact method for fieldsplit's A^{-1}. */
68421635b76SJed Brown         ierr = KSPSetType(jac->head->ksp,KSPGMRES);CHKERRQ(ierr);
685514bf10dSMatthew G Knepley         ierr = MatSchurComplementSetKSP(jac->schur,jac->head->ksp);CHKERRQ(ierr);
686bafc1b83SMatthew G Knepley       }
6875a9f2f41SSatish Balay       ierr = KSPSetOperators(jac->head->ksp,jac->mat[0],jac->pmat[0],flag);CHKERRQ(ierr);
6885a9f2f41SSatish Balay       ierr = KSPSetFromOptions(jac->head->ksp);CHKERRQ(ierr);
68997bbdb24SBarry Smith       ierr = MatSetFromOptions(jac->schur);CHKERRQ(ierr);
69097bbdb24SBarry Smith 
691443836d0SMatthew G Knepley       ierr = PetscSNPrintf(schurtestoption, sizeof(schurtestoption), "-fieldsplit_%s_upper_", ilink->splitname);CHKERRQ(ierr);
6920298fd71SBarry Smith       ierr = PetscOptionsFindPairPrefix_Private(((PetscObject)pc)->prefix, schurtestoption, NULL, &flg);CHKERRQ(ierr);
693443836d0SMatthew G Knepley       if (flg) {
694443836d0SMatthew G Knepley         DM dmInner;
695443836d0SMatthew G Knepley 
696443836d0SMatthew G Knepley         ierr = PetscSNPrintf(schurprefix, sizeof(schurprefix), "%sfieldsplit_%s_upper_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname);CHKERRQ(ierr);
69782f516ccSBarry Smith         ierr = KSPCreate(PetscObjectComm((PetscObject)pc), &jac->kspupper);CHKERRQ(ierr);
698443836d0SMatthew G Knepley         ierr = KSPSetOptionsPrefix(jac->kspupper, schurprefix);CHKERRQ(ierr);
699443836d0SMatthew G Knepley         ierr = KSPGetDM(jac->head->ksp, &dmInner);CHKERRQ(ierr);
700443836d0SMatthew G Knepley         ierr = KSPSetDM(jac->kspupper, dmInner);CHKERRQ(ierr);
701443836d0SMatthew G Knepley         ierr = KSPSetDMActive(jac->kspupper, PETSC_FALSE);CHKERRQ(ierr);
702443836d0SMatthew G Knepley         ierr = KSPSetFromOptions(jac->kspupper);CHKERRQ(ierr);
703443836d0SMatthew G Knepley         ierr = KSPSetOperators(jac->kspupper,jac->mat[0],jac->pmat[0],flag);CHKERRQ(ierr);
704443836d0SMatthew G Knepley         ierr = VecDuplicate(jac->head->x, &jac->head->z);CHKERRQ(ierr);
705443836d0SMatthew G Knepley       } else {
706443836d0SMatthew G Knepley         jac->kspupper = jac->head->ksp;
707443836d0SMatthew G Knepley         ierr          = PetscObjectReference((PetscObject) jac->head->ksp);CHKERRQ(ierr);
708443836d0SMatthew G Knepley       }
709443836d0SMatthew G Knepley 
710a7476a74SDmitry Karpeev       if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELFP) {
711a7476a74SDmitry Karpeev 	ierr = MatSchurComplementGetPmat(jac->schur,MAT_INITIAL_MATRIX,&jac->schurp);CHKERRQ(ierr);
712a7476a74SDmitry Karpeev       }
713ce94432eSBarry Smith       ierr = KSPCreate(PetscObjectComm((PetscObject)pc),&jac->kspschur);CHKERRQ(ierr);
71497bbdb24SBarry Smith       ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)jac->kspschur);CHKERRQ(ierr);
71520252d06SBarry Smith       ierr = PetscObjectIncrementTabLevel((PetscObject)jac->kspschur,(PetscObject)pc,1);CHKERRQ(ierr);
71697bbdb24SBarry Smith       ierr = KSPSetOperators(jac->kspschur,jac->schur,FieldSplitSchurPre(jac),DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);
71797bbdb24SBarry Smith       if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELF) {
7187233a360SDmitry Karpeev         PC pcschur;
7197233a360SDmitry Karpeev         ierr = KSPGetPC(jac->kspschur,&pcschur);CHKERRQ(ierr);
7207233a360SDmitry Karpeev         ierr = PCSetType(pcschur,PCNONE);CHKERRQ(ierr);
72197bbdb24SBarry Smith         /* Note: This is bad if there exist preconditioners for MATSCHURCOMPLEMENT */
72297bbdb24SBarry Smith       }
72397bbdb24SBarry Smith       ierr = KSPGetOptionsPrefix(jac->head->next->ksp, &Dprefix);CHKERRQ(ierr);
72497bbdb24SBarry Smith       ierr = KSPSetOptionsPrefix(jac->kspschur,         Dprefix);CHKERRQ(ierr);
725b20b4189SMatthew G. Knepley       /* propogate DM */
726b20b4189SMatthew G. Knepley       {
727b20b4189SMatthew G. Knepley         DM sdm;
728b20b4189SMatthew G. Knepley         ierr = KSPGetDM(jac->head->next->ksp, &sdm);CHKERRQ(ierr);
729b20b4189SMatthew G. Knepley         if (sdm) {
730b20b4189SMatthew G. Knepley           ierr = KSPSetDM(jac->kspschur, sdm);CHKERRQ(ierr);
731b20b4189SMatthew G. Knepley           ierr = KSPSetDMActive(jac->kspschur, PETSC_FALSE);CHKERRQ(ierr);
732b20b4189SMatthew G. Knepley         }
733b20b4189SMatthew G. Knepley       }
73497bbdb24SBarry Smith       /* really want setfromoptions called in PCSetFromOptions_FieldSplit(), but it is not ready yet */
73597bbdb24SBarry Smith       /* need to call this every time, since the jac->kspschur is freshly created, otherwise its options never get set */
73697bbdb24SBarry Smith       ierr = KSPSetFromOptions(jac->kspschur);CHKERRQ(ierr);
73797bbdb24SBarry Smith     }
73897bbdb24SBarry Smith 
7395a9f2f41SSatish Balay     /* HACK: special support to forward L and Lp matrices that might be used by PCLSC */
7408caf3d72SBarry Smith     ierr = PetscSNPrintf(lscname,sizeof(lscname),"%s_LSC_L",ilink->splitname);CHKERRQ(ierr);
741519d70e2SJed Brown     ierr = PetscObjectQuery((PetscObject)pc->mat,lscname,(PetscObject*)&LSC_L);CHKERRQ(ierr);
7423b224e63SBarry Smith     if (!LSC_L) {ierr = PetscObjectQuery((PetscObject)pc->pmat,lscname,(PetscObject*)&LSC_L);CHKERRQ(ierr);}
743c1570756SJed Brown     if (LSC_L) {ierr = PetscObjectCompose((PetscObject)jac->schur,"LSC_L",(PetscObject)LSC_L);CHKERRQ(ierr);}
7448caf3d72SBarry Smith     ierr = PetscSNPrintf(lscname,sizeof(lscname),"%s_LSC_Lp",ilink->splitname);CHKERRQ(ierr);
74597bbdb24SBarry Smith     ierr = PetscObjectQuery((PetscObject)pc->pmat,lscname,(PetscObject*)&LSC_L);CHKERRQ(ierr);
7465a9f2f41SSatish Balay     if (!LSC_L) {ierr = PetscObjectQuery((PetscObject)pc->mat,lscname,(PetscObject*)&LSC_L);CHKERRQ(ierr);}
7470971522cSBarry Smith     if (LSC_L) {ierr = PetscObjectCompose((PetscObject)jac->schur,"LSC_Lp",(PetscObject)LSC_L);CHKERRQ(ierr);}
74897bbdb24SBarry Smith   } else {
74968bd789dSDmitry Karpeev     /* set up the individual splits' PCs */
7500971522cSBarry Smith     i     = 0;
7510971522cSBarry Smith     ilink = jac->head;
7520971522cSBarry Smith     while (ilink) {
7530971522cSBarry Smith       ierr = KSPSetOperators(ilink->ksp,jac->mat[i],jac->pmat[i],flag);CHKERRQ(ierr);
7540971522cSBarry Smith       /* really want setfromoptions called in PCSetFromOptions_FieldSplit(), but it is not ready yet */
7550971522cSBarry Smith       if (!jac->suboptionsset) {ierr = KSPSetFromOptions(ilink->ksp);CHKERRQ(ierr);}
7560971522cSBarry Smith       i++;
7570971522cSBarry Smith       ilink = ilink->next;
7580971522cSBarry Smith     }
7593b224e63SBarry Smith   }
7603b224e63SBarry Smith 
761c1570756SJed Brown   jac->suboptionsset = PETSC_TRUE;
7620971522cSBarry Smith   PetscFunctionReturn(0);
7630971522cSBarry Smith }
7640971522cSBarry Smith 
7655a9f2f41SSatish Balay #define FieldSplitSplitSolveAdd(ilink,xx,yy) \
766ca9f406cSSatish Balay   (VecScatterBegin(ilink->sctx,xx,ilink->x,INSERT_VALUES,SCATTER_FORWARD) || \
767ca9f406cSSatish Balay    VecScatterEnd(ilink->sctx,xx,ilink->x,INSERT_VALUES,SCATTER_FORWARD) || \
7685a9f2f41SSatish Balay    KSPSolve(ilink->ksp,ilink->x,ilink->y) || \
769ca9f406cSSatish Balay    VecScatterBegin(ilink->sctx,ilink->y,yy,ADD_VALUES,SCATTER_REVERSE) || \
770ca9f406cSSatish Balay    VecScatterEnd(ilink->sctx,ilink->y,yy,ADD_VALUES,SCATTER_REVERSE))
77179416396SBarry Smith 
7720971522cSBarry Smith #undef __FUNCT__
7733b224e63SBarry Smith #define __FUNCT__ "PCApply_FieldSplit_Schur"
7743b224e63SBarry Smith static PetscErrorCode PCApply_FieldSplit_Schur(PC pc,Vec x,Vec y)
7753b224e63SBarry Smith {
7763b224e63SBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
7773b224e63SBarry Smith   PetscErrorCode    ierr;
7783b224e63SBarry Smith   PC_FieldSplitLink ilinkA = jac->head, ilinkD = ilinkA->next;
779443836d0SMatthew G Knepley   KSP               kspA   = ilinkA->ksp, kspLower = kspA, kspUpper = jac->kspupper;
7803b224e63SBarry Smith 
7813b224e63SBarry Smith   PetscFunctionBegin;
782c5d2311dSJed Brown   switch (jac->schurfactorization) {
783c9c6ffaaSJed Brown   case PC_FIELDSPLIT_SCHUR_FACT_DIAG:
784a04f6461SBarry Smith     /* [A00 0; 0 -S], positive definite, suitable for MINRES */
785c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
786c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
787c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
788443836d0SMatthew G Knepley     ierr = KSPSolve(kspA,ilinkA->x,ilinkA->y);CHKERRQ(ierr);
789c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
790c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
791c5d2311dSJed Brown     ierr = KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);CHKERRQ(ierr);
792c5d2311dSJed Brown     ierr = VecScale(ilinkD->y,-1.);CHKERRQ(ierr);
793c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
794c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
795c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
796c5d2311dSJed Brown     break;
797c9c6ffaaSJed Brown   case PC_FIELDSPLIT_SCHUR_FACT_LOWER:
798a04f6461SBarry Smith     /* [A00 0; A10 S], suitable for left preconditioning */
799c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
800c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
801443836d0SMatthew G Knepley     ierr = KSPSolve(kspA,ilinkA->x,ilinkA->y);CHKERRQ(ierr);
802c5d2311dSJed Brown     ierr = MatMult(jac->C,ilinkA->y,ilinkD->x);CHKERRQ(ierr);
803c5d2311dSJed Brown     ierr = VecScale(ilinkD->x,-1.);CHKERRQ(ierr);
804c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
805c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
806c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
807c5d2311dSJed Brown     ierr = KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);CHKERRQ(ierr);
808c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
809c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
810c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
811c5d2311dSJed Brown     break;
812c9c6ffaaSJed Brown   case PC_FIELDSPLIT_SCHUR_FACT_UPPER:
813a04f6461SBarry Smith     /* [A00 A01; 0 S], suitable for right preconditioning */
814c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
815c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
816c5d2311dSJed Brown     ierr = KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);CHKERRQ(ierr);
817c5d2311dSJed Brown     ierr = MatMult(jac->B,ilinkD->y,ilinkA->x);CHKERRQ(ierr);
818c5d2311dSJed Brown     ierr = VecScale(ilinkA->x,-1.);CHKERRQ(ierr);
819c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkA->sctx,x,ilinkA->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
820c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
821c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkA->sctx,x,ilinkA->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
822443836d0SMatthew G Knepley     ierr = KSPSolve(kspA,ilinkA->x,ilinkA->y);CHKERRQ(ierr);
823c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
824c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
825c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
826c5d2311dSJed Brown     break;
827c9c6ffaaSJed Brown   case PC_FIELDSPLIT_SCHUR_FACT_FULL:
828a04f6461SBarry Smith     /* [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 */
8293b224e63SBarry Smith     ierr = VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
8303b224e63SBarry Smith     ierr = VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
831443836d0SMatthew G Knepley     ierr = KSPSolve(kspLower,ilinkA->x,ilinkA->y);CHKERRQ(ierr);
8323b224e63SBarry Smith     ierr = MatMult(jac->C,ilinkA->y,ilinkD->x);CHKERRQ(ierr);
8333b224e63SBarry Smith     ierr = VecScale(ilinkD->x,-1.0);CHKERRQ(ierr);
8343b224e63SBarry Smith     ierr = VecScatterBegin(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
8353b224e63SBarry Smith     ierr = VecScatterEnd(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
8363b224e63SBarry Smith 
8373b224e63SBarry Smith     ierr = KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);CHKERRQ(ierr);
8383b224e63SBarry Smith     ierr = VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
8393b224e63SBarry Smith     ierr = VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
8403b224e63SBarry Smith 
841443836d0SMatthew G Knepley     if (kspUpper == kspA) {
8423b224e63SBarry Smith       ierr = MatMult(jac->B,ilinkD->y,ilinkA->y);CHKERRQ(ierr);
8433b224e63SBarry Smith       ierr = VecAXPY(ilinkA->x,-1.0,ilinkA->y);CHKERRQ(ierr);
844443836d0SMatthew G Knepley       ierr = KSPSolve(kspA,ilinkA->x,ilinkA->y);CHKERRQ(ierr);
845443836d0SMatthew G Knepley     } else {
846443836d0SMatthew G Knepley       ierr = KSPSolve(kspA,ilinkA->x,ilinkA->y);CHKERRQ(ierr);
847443836d0SMatthew G Knepley       ierr = MatMult(jac->B,ilinkD->y,ilinkA->x);CHKERRQ(ierr);
848443836d0SMatthew G Knepley       ierr = KSPSolve(kspUpper,ilinkA->x,ilinkA->z);CHKERRQ(ierr);
849443836d0SMatthew G Knepley       ierr = VecAXPY(ilinkA->y,-1.0,ilinkA->z);CHKERRQ(ierr);
850443836d0SMatthew G Knepley     }
8513b224e63SBarry Smith     ierr = VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
8523b224e63SBarry Smith     ierr = VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
853c5d2311dSJed Brown   }
8543b224e63SBarry Smith   PetscFunctionReturn(0);
8553b224e63SBarry Smith }
8563b224e63SBarry Smith 
8573b224e63SBarry Smith #undef __FUNCT__
8580971522cSBarry Smith #define __FUNCT__ "PCApply_FieldSplit"
8590971522cSBarry Smith static PetscErrorCode PCApply_FieldSplit(PC pc,Vec x,Vec y)
8600971522cSBarry Smith {
8610971522cSBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
8620971522cSBarry Smith   PetscErrorCode    ierr;
8635a9f2f41SSatish Balay   PC_FieldSplitLink ilink = jac->head;
864939b8a20SBarry Smith   PetscInt          cnt,bs;
8650971522cSBarry Smith 
8660971522cSBarry Smith   PetscFunctionBegin;
86779416396SBarry Smith   if (jac->type == PC_COMPOSITE_ADDITIVE) {
8681b9fc7fcSBarry Smith     if (jac->defaultsplit) {
869939b8a20SBarry Smith       ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
870ce94432eSBarry Smith       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);
871939b8a20SBarry Smith       ierr = VecGetBlockSize(y,&bs);CHKERRQ(ierr);
872ce94432eSBarry Smith       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);
8730971522cSBarry Smith       ierr = VecStrideGatherAll(x,jac->x,INSERT_VALUES);CHKERRQ(ierr);
8745a9f2f41SSatish Balay       while (ilink) {
8755a9f2f41SSatish Balay         ierr  = KSPSolve(ilink->ksp,ilink->x,ilink->y);CHKERRQ(ierr);
8765a9f2f41SSatish Balay         ilink = ilink->next;
8770971522cSBarry Smith       }
8780971522cSBarry Smith       ierr = VecStrideScatterAll(jac->y,y,INSERT_VALUES);CHKERRQ(ierr);
8791b9fc7fcSBarry Smith     } else {
880efb30889SBarry Smith       ierr = VecSet(y,0.0);CHKERRQ(ierr);
8815a9f2f41SSatish Balay       while (ilink) {
8825a9f2f41SSatish Balay         ierr  = FieldSplitSplitSolveAdd(ilink,x,y);CHKERRQ(ierr);
8835a9f2f41SSatish Balay         ilink = ilink->next;
8841b9fc7fcSBarry Smith       }
8851b9fc7fcSBarry Smith     }
88616913363SBarry Smith   } else if (jac->type == PC_COMPOSITE_MULTIPLICATIVE || jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
88779416396SBarry Smith     if (!jac->w1) {
88879416396SBarry Smith       ierr = VecDuplicate(x,&jac->w1);CHKERRQ(ierr);
88979416396SBarry Smith       ierr = VecDuplicate(x,&jac->w2);CHKERRQ(ierr);
89079416396SBarry Smith     }
891efb30889SBarry Smith     ierr = VecSet(y,0.0);CHKERRQ(ierr);
8925a9f2f41SSatish Balay     ierr = FieldSplitSplitSolveAdd(ilink,x,y);CHKERRQ(ierr);
8933e197d65SBarry Smith     cnt  = 1;
8945a9f2f41SSatish Balay     while (ilink->next) {
8955a9f2f41SSatish Balay       ilink = ilink->next;
8963e197d65SBarry Smith       /* compute the residual only over the part of the vector needed */
8973e197d65SBarry Smith       ierr = MatMult(jac->Afield[cnt++],y,ilink->x);CHKERRQ(ierr);
8983e197d65SBarry Smith       ierr = VecScale(ilink->x,-1.0);CHKERRQ(ierr);
8993e197d65SBarry Smith       ierr = VecScatterBegin(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
9003e197d65SBarry Smith       ierr = VecScatterEnd(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
9013e197d65SBarry Smith       ierr = KSPSolve(ilink->ksp,ilink->x,ilink->y);CHKERRQ(ierr);
9023e197d65SBarry Smith       ierr = VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
9033e197d65SBarry Smith       ierr = VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
9043e197d65SBarry Smith     }
90551f519a2SBarry Smith     if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
90611755939SBarry Smith       cnt -= 2;
90751f519a2SBarry Smith       while (ilink->previous) {
90851f519a2SBarry Smith         ilink = ilink->previous;
90911755939SBarry Smith         /* compute the residual only over the part of the vector needed */
91011755939SBarry Smith         ierr = MatMult(jac->Afield[cnt--],y,ilink->x);CHKERRQ(ierr);
91111755939SBarry Smith         ierr = VecScale(ilink->x,-1.0);CHKERRQ(ierr);
91211755939SBarry Smith         ierr = VecScatterBegin(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
91311755939SBarry Smith         ierr = VecScatterEnd(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
91411755939SBarry Smith         ierr = KSPSolve(ilink->ksp,ilink->x,ilink->y);CHKERRQ(ierr);
91511755939SBarry Smith         ierr = VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
91611755939SBarry Smith         ierr = VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
91751f519a2SBarry Smith       }
91811755939SBarry Smith     }
919ce94432eSBarry Smith   } else SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Unsupported or unknown composition",(int) jac->type);
9200971522cSBarry Smith   PetscFunctionReturn(0);
9210971522cSBarry Smith }
9220971522cSBarry Smith 
923421e10b8SBarry Smith #define FieldSplitSplitSolveAddTranspose(ilink,xx,yy) \
924ca9f406cSSatish Balay   (VecScatterBegin(ilink->sctx,xx,ilink->y,INSERT_VALUES,SCATTER_FORWARD) || \
925ca9f406cSSatish Balay    VecScatterEnd(ilink->sctx,xx,ilink->y,INSERT_VALUES,SCATTER_FORWARD) || \
926421e10b8SBarry Smith    KSPSolveTranspose(ilink->ksp,ilink->y,ilink->x) || \
927ca9f406cSSatish Balay    VecScatterBegin(ilink->sctx,ilink->x,yy,ADD_VALUES,SCATTER_REVERSE) || \
928ca9f406cSSatish Balay    VecScatterEnd(ilink->sctx,ilink->x,yy,ADD_VALUES,SCATTER_REVERSE))
929421e10b8SBarry Smith 
930421e10b8SBarry Smith #undef __FUNCT__
9318c03b21aSDmitry Karpeev #define __FUNCT__ "PCApplyTranspose_FieldSplit"
932421e10b8SBarry Smith static PetscErrorCode PCApplyTranspose_FieldSplit(PC pc,Vec x,Vec y)
933421e10b8SBarry Smith {
934421e10b8SBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
935421e10b8SBarry Smith   PetscErrorCode    ierr;
936421e10b8SBarry Smith   PC_FieldSplitLink ilink = jac->head;
937939b8a20SBarry Smith   PetscInt          bs;
938421e10b8SBarry Smith 
939421e10b8SBarry Smith   PetscFunctionBegin;
940421e10b8SBarry Smith   if (jac->type == PC_COMPOSITE_ADDITIVE) {
941421e10b8SBarry Smith     if (jac->defaultsplit) {
942939b8a20SBarry Smith       ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
943ce94432eSBarry Smith       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);
944939b8a20SBarry Smith       ierr = VecGetBlockSize(y,&bs);CHKERRQ(ierr);
945ce94432eSBarry Smith       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);
946421e10b8SBarry Smith       ierr = VecStrideGatherAll(x,jac->x,INSERT_VALUES);CHKERRQ(ierr);
947421e10b8SBarry Smith       while (ilink) {
948421e10b8SBarry Smith         ierr  = KSPSolveTranspose(ilink->ksp,ilink->x,ilink->y);CHKERRQ(ierr);
949421e10b8SBarry Smith         ilink = ilink->next;
950421e10b8SBarry Smith       }
951421e10b8SBarry Smith       ierr = VecStrideScatterAll(jac->y,y,INSERT_VALUES);CHKERRQ(ierr);
952421e10b8SBarry Smith     } else {
953421e10b8SBarry Smith       ierr = VecSet(y,0.0);CHKERRQ(ierr);
954421e10b8SBarry Smith       while (ilink) {
955421e10b8SBarry Smith         ierr  = FieldSplitSplitSolveAddTranspose(ilink,x,y);CHKERRQ(ierr);
956421e10b8SBarry Smith         ilink = ilink->next;
957421e10b8SBarry Smith       }
958421e10b8SBarry Smith     }
959421e10b8SBarry Smith   } else {
960421e10b8SBarry Smith     if (!jac->w1) {
961421e10b8SBarry Smith       ierr = VecDuplicate(x,&jac->w1);CHKERRQ(ierr);
962421e10b8SBarry Smith       ierr = VecDuplicate(x,&jac->w2);CHKERRQ(ierr);
963421e10b8SBarry Smith     }
964421e10b8SBarry Smith     ierr = VecSet(y,0.0);CHKERRQ(ierr);
965421e10b8SBarry Smith     if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
966421e10b8SBarry Smith       ierr = FieldSplitSplitSolveAddTranspose(ilink,x,y);CHKERRQ(ierr);
967421e10b8SBarry Smith       while (ilink->next) {
968421e10b8SBarry Smith         ilink = ilink->next;
9699989ab13SBarry Smith         ierr  = MatMultTranspose(pc->mat,y,jac->w1);CHKERRQ(ierr);
970421e10b8SBarry Smith         ierr  = VecWAXPY(jac->w2,-1.0,jac->w1,x);CHKERRQ(ierr);
971421e10b8SBarry Smith         ierr  = FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);CHKERRQ(ierr);
972421e10b8SBarry Smith       }
973421e10b8SBarry Smith       while (ilink->previous) {
974421e10b8SBarry Smith         ilink = ilink->previous;
9759989ab13SBarry Smith         ierr  = MatMultTranspose(pc->mat,y,jac->w1);CHKERRQ(ierr);
976421e10b8SBarry Smith         ierr  = VecWAXPY(jac->w2,-1.0,jac->w1,x);CHKERRQ(ierr);
977421e10b8SBarry Smith         ierr  = FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);CHKERRQ(ierr);
978421e10b8SBarry Smith       }
979421e10b8SBarry Smith     } else {
980421e10b8SBarry Smith       while (ilink->next) {   /* get to last entry in linked list */
981421e10b8SBarry Smith         ilink = ilink->next;
982421e10b8SBarry Smith       }
983421e10b8SBarry Smith       ierr = FieldSplitSplitSolveAddTranspose(ilink,x,y);CHKERRQ(ierr);
984421e10b8SBarry Smith       while (ilink->previous) {
985421e10b8SBarry Smith         ilink = ilink->previous;
9869989ab13SBarry Smith         ierr  = MatMultTranspose(pc->mat,y,jac->w1);CHKERRQ(ierr);
987421e10b8SBarry Smith         ierr  = VecWAXPY(jac->w2,-1.0,jac->w1,x);CHKERRQ(ierr);
988421e10b8SBarry Smith         ierr  = FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);CHKERRQ(ierr);
989421e10b8SBarry Smith       }
990421e10b8SBarry Smith     }
991421e10b8SBarry Smith   }
992421e10b8SBarry Smith   PetscFunctionReturn(0);
993421e10b8SBarry Smith }
994421e10b8SBarry Smith 
9950971522cSBarry Smith #undef __FUNCT__
996574deadeSBarry Smith #define __FUNCT__ "PCReset_FieldSplit"
997574deadeSBarry Smith static PetscErrorCode PCReset_FieldSplit(PC pc)
9980971522cSBarry Smith {
9990971522cSBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
10000971522cSBarry Smith   PetscErrorCode    ierr;
10015a9f2f41SSatish Balay   PC_FieldSplitLink ilink = jac->head,next;
10020971522cSBarry Smith 
10030971522cSBarry Smith   PetscFunctionBegin;
10045a9f2f41SSatish Balay   while (ilink) {
1005574deadeSBarry Smith     ierr  = KSPReset(ilink->ksp);CHKERRQ(ierr);
1006fcfd50ebSBarry Smith     ierr  = VecDestroy(&ilink->x);CHKERRQ(ierr);
1007fcfd50ebSBarry Smith     ierr  = VecDestroy(&ilink->y);CHKERRQ(ierr);
1008443836d0SMatthew G Knepley     ierr  = VecDestroy(&ilink->z);CHKERRQ(ierr);
1009fcfd50ebSBarry Smith     ierr  = VecScatterDestroy(&ilink->sctx);CHKERRQ(ierr);
1010fcfd50ebSBarry Smith     ierr  = ISDestroy(&ilink->is);CHKERRQ(ierr);
1011b5787286SJed Brown     ierr  = ISDestroy(&ilink->is_col);CHKERRQ(ierr);
10125a9f2f41SSatish Balay     next  = ilink->next;
10135a9f2f41SSatish Balay     ilink = next;
10140971522cSBarry Smith   }
101505b42c5fSBarry Smith   ierr = PetscFree2(jac->x,jac->y);CHKERRQ(ierr);
1016574deadeSBarry Smith   if (jac->mat && jac->mat != jac->pmat) {
1017574deadeSBarry Smith     ierr = MatDestroyMatrices(jac->nsplits,&jac->mat);CHKERRQ(ierr);
1018574deadeSBarry Smith   } else if (jac->mat) {
10190298fd71SBarry Smith     jac->mat = NULL;
1020574deadeSBarry Smith   }
102197bbdb24SBarry Smith   if (jac->pmat) {ierr = MatDestroyMatrices(jac->nsplits,&jac->pmat);CHKERRQ(ierr);}
102268dd23aaSBarry Smith   if (jac->Afield) {ierr = MatDestroyMatrices(jac->nsplits,&jac->Afield);CHKERRQ(ierr);}
10236bf464f9SBarry Smith   ierr       = VecDestroy(&jac->w1);CHKERRQ(ierr);
10246bf464f9SBarry Smith   ierr       = VecDestroy(&jac->w2);CHKERRQ(ierr);
10256bf464f9SBarry Smith   ierr       = MatDestroy(&jac->schur);CHKERRQ(ierr);
1026a7476a74SDmitry Karpeev   ierr       = MatDestroy(&jac->schurp);CHKERRQ(ierr);
10276bf464f9SBarry Smith   ierr       = MatDestroy(&jac->schur_user);CHKERRQ(ierr);
10286bf464f9SBarry Smith   ierr       = KSPDestroy(&jac->kspschur);CHKERRQ(ierr);
1029d78dad28SBarry Smith   ierr       = KSPDestroy(&jac->kspupper);CHKERRQ(ierr);
10306bf464f9SBarry Smith   ierr       = MatDestroy(&jac->B);CHKERRQ(ierr);
10316bf464f9SBarry Smith   ierr       = MatDestroy(&jac->C);CHKERRQ(ierr);
103263ec74ffSBarry Smith   jac->reset = PETSC_TRUE;
1033574deadeSBarry Smith   PetscFunctionReturn(0);
1034574deadeSBarry Smith }
1035574deadeSBarry Smith 
1036574deadeSBarry Smith #undef __FUNCT__
1037574deadeSBarry Smith #define __FUNCT__ "PCDestroy_FieldSplit"
1038574deadeSBarry Smith static PetscErrorCode PCDestroy_FieldSplit(PC pc)
1039574deadeSBarry Smith {
1040574deadeSBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1041574deadeSBarry Smith   PetscErrorCode    ierr;
1042574deadeSBarry Smith   PC_FieldSplitLink ilink = jac->head,next;
1043574deadeSBarry Smith 
1044574deadeSBarry Smith   PetscFunctionBegin;
1045574deadeSBarry Smith   ierr = PCReset_FieldSplit(pc);CHKERRQ(ierr);
1046574deadeSBarry Smith   while (ilink) {
10476bf464f9SBarry Smith     ierr  = KSPDestroy(&ilink->ksp);CHKERRQ(ierr);
1048574deadeSBarry Smith     next  = ilink->next;
1049574deadeSBarry Smith     ierr  = PetscFree(ilink->splitname);CHKERRQ(ierr);
1050574deadeSBarry Smith     ierr  = PetscFree(ilink->fields);CHKERRQ(ierr);
10515d4c12cdSJungho Lee     ierr  = PetscFree(ilink->fields_col);CHKERRQ(ierr);
1052574deadeSBarry Smith     ierr  = PetscFree(ilink);CHKERRQ(ierr);
1053574deadeSBarry Smith     ilink = next;
1054574deadeSBarry Smith   }
1055574deadeSBarry Smith   ierr = PetscFree2(jac->x,jac->y);CHKERRQ(ierr);
1056c31cb41cSBarry Smith   ierr = PetscFree(pc->data);CHKERRQ(ierr);
1057bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",NULL);CHKERRQ(ierr);
1058bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetFields_C",NULL);CHKERRQ(ierr);
1059bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetIS_C",NULL);CHKERRQ(ierr);
1060bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetType_C",NULL);CHKERRQ(ierr);
1061bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetBlockSize_C",NULL);CHKERRQ(ierr);
1062bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSchurPrecondition_C",NULL);CHKERRQ(ierr);
1063bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C",NULL);CHKERRQ(ierr);
10640971522cSBarry Smith   PetscFunctionReturn(0);
10650971522cSBarry Smith }
10660971522cSBarry Smith 
10670971522cSBarry Smith #undef __FUNCT__
10680971522cSBarry Smith #define __FUNCT__ "PCSetFromOptions_FieldSplit"
10690971522cSBarry Smith static PetscErrorCode PCSetFromOptions_FieldSplit(PC pc)
10700971522cSBarry Smith {
10711b9fc7fcSBarry Smith   PetscErrorCode  ierr;
10726c924f48SJed Brown   PetscInt        bs;
1073bc59fbc5SBarry Smith   PetscBool       flg,stokes = PETSC_FALSE;
10749dcbbd2bSBarry Smith   PC_FieldSplit   *jac = (PC_FieldSplit*)pc->data;
10753b224e63SBarry Smith   PCCompositeType ctype;
10761b9fc7fcSBarry Smith 
10770971522cSBarry Smith   PetscFunctionBegin;
10781b9fc7fcSBarry Smith   ierr = PetscOptionsHead("FieldSplit options");CHKERRQ(ierr);
10794ab8060aSDmitry Karpeev   ierr = PetscOptionsBool("-pc_fieldsplit_dm_splits","Whether to use DMCreateFieldDecomposition() for splits","PCFieldSplitSetDMSplits",jac->dm_splits,&jac->dm_splits,NULL);CHKERRQ(ierr);
108051f519a2SBarry Smith   ierr = PetscOptionsInt("-pc_fieldsplit_block_size","Blocksize that defines number of fields","PCFieldSplitSetBlockSize",jac->bs,&bs,&flg);CHKERRQ(ierr);
108151f519a2SBarry Smith   if (flg) {
108251f519a2SBarry Smith     ierr = PCFieldSplitSetBlockSize(pc,bs);CHKERRQ(ierr);
108351f519a2SBarry Smith   }
1084704ba839SBarry Smith 
10850298fd71SBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)pc)->prefix,"-pc_fieldsplit_detect_saddle_point",&stokes,NULL);CHKERRQ(ierr);
1086c0adfefeSBarry Smith   if (stokes) {
1087c0adfefeSBarry Smith     ierr          = PCFieldSplitSetType(pc,PC_COMPOSITE_SCHUR);CHKERRQ(ierr);
1088c0adfefeSBarry Smith     jac->schurpre = PC_FIELDSPLIT_SCHUR_PRE_SELF;
1089c0adfefeSBarry Smith   }
1090c0adfefeSBarry Smith 
10913b224e63SBarry Smith   ierr = PetscOptionsEnum("-pc_fieldsplit_type","Type of composition","PCFieldSplitSetType",PCCompositeTypes,(PetscEnum)jac->type,(PetscEnum*)&ctype,&flg);CHKERRQ(ierr);
10923b224e63SBarry Smith   if (flg) {
10933b224e63SBarry Smith     ierr = PCFieldSplitSetType(pc,ctype);CHKERRQ(ierr);
10943b224e63SBarry Smith   }
1095c30613efSMatthew Knepley   /* Only setup fields once */
1096c30613efSMatthew Knepley   if ((jac->bs > 0) && (jac->nsplits == 0)) {
1097d32f9abdSBarry Smith     /* only allow user to set fields from command line if bs is already known.
1098d32f9abdSBarry Smith        otherwise user can set them in PCFieldSplitSetDefaults() */
10996c924f48SJed Brown     ierr = PCFieldSplitSetRuntimeSplits_Private(pc);CHKERRQ(ierr);
11006c924f48SJed Brown     if (jac->splitdefined) {ierr = PetscInfo(pc,"Splits defined using the options database\n");CHKERRQ(ierr);}
1101d32f9abdSBarry Smith   }
1102c5d2311dSJed Brown   if (jac->type == PC_COMPOSITE_SCHUR) {
1103c9c6ffaaSJed Brown     ierr = PetscOptionsGetEnum(((PetscObject)pc)->prefix,"-pc_fieldsplit_schur_factorization_type",PCFieldSplitSchurFactTypes,(PetscEnum*)&jac->schurfactorization,&flg);CHKERRQ(ierr);
1104c9c6ffaaSJed Brown     if (flg) {ierr = PetscInfo(pc,"Deprecated use of -pc_fieldsplit_schur_factorization_type\n");CHKERRQ(ierr);}
11050298fd71SBarry Smith     ierr = 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);CHKERRQ(ierr);
11060298fd71SBarry Smith     ierr = PetscOptionsEnum("-pc_fieldsplit_schur_precondition","How to build preconditioner for Schur complement","PCFieldSplitSchurPrecondition",PCFieldSplitSchurPreTypes,(PetscEnum)jac->schurpre,(PetscEnum*)&jac->schurpre,NULL);CHKERRQ(ierr);
1107c5d2311dSJed Brown   }
11081b9fc7fcSBarry Smith   ierr = PetscOptionsTail();CHKERRQ(ierr);
11090971522cSBarry Smith   PetscFunctionReturn(0);
11100971522cSBarry Smith }
11110971522cSBarry Smith 
11120971522cSBarry Smith /*------------------------------------------------------------------------------------*/
11130971522cSBarry Smith 
11140971522cSBarry Smith #undef __FUNCT__
11150971522cSBarry Smith #define __FUNCT__ "PCFieldSplitSetFields_FieldSplit"
11161e6b0712SBarry Smith static PetscErrorCode  PCFieldSplitSetFields_FieldSplit(PC pc,const char splitname[],PetscInt n,const PetscInt *fields,const PetscInt *fields_col)
11170971522cSBarry Smith {
111897bbdb24SBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
11190971522cSBarry Smith   PetscErrorCode    ierr;
11205a9f2f41SSatish Balay   PC_FieldSplitLink ilink,next = jac->head;
112169a612a9SBarry Smith   char              prefix[128];
11225d4c12cdSJungho Lee   PetscInt          i;
11230971522cSBarry Smith 
11240971522cSBarry Smith   PetscFunctionBegin;
11256c924f48SJed Brown   if (jac->splitdefined) {
11266c924f48SJed Brown     ierr = PetscInfo1(pc,"Ignoring new split \"%s\" because the splits have already been defined\n",splitname);CHKERRQ(ierr);
11276c924f48SJed Brown     PetscFunctionReturn(0);
11286c924f48SJed Brown   }
112951f519a2SBarry Smith   for (i=0; i<n; i++) {
1130e32f2f54SBarry Smith     if (fields[i] >= jac->bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Field %D requested but only %D exist",fields[i],jac->bs);
1131e32f2f54SBarry Smith     if (fields[i] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative field %D requested",fields[i]);
113251f519a2SBarry Smith   }
1133b00a9115SJed Brown   ierr = PetscNew(&ilink);CHKERRQ(ierr);
1134a04f6461SBarry Smith   if (splitname) {
1135db4c96c1SJed Brown     ierr = PetscStrallocpy(splitname,&ilink->splitname);CHKERRQ(ierr);
1136a04f6461SBarry Smith   } else {
1137785e854fSJed Brown     ierr = PetscMalloc1(3,&ilink->splitname);CHKERRQ(ierr);
1138a04f6461SBarry Smith     ierr = PetscSNPrintf(ilink->splitname,2,"%s",jac->nsplits);CHKERRQ(ierr);
1139a04f6461SBarry Smith   }
1140785e854fSJed Brown   ierr = PetscMalloc1(n,&ilink->fields);CHKERRQ(ierr);
11415a9f2f41SSatish Balay   ierr = PetscMemcpy(ilink->fields,fields,n*sizeof(PetscInt));CHKERRQ(ierr);
1142785e854fSJed Brown   ierr = PetscMalloc1(n,&ilink->fields_col);CHKERRQ(ierr);
11435d4c12cdSJungho Lee   ierr = PetscMemcpy(ilink->fields_col,fields_col,n*sizeof(PetscInt));CHKERRQ(ierr);
11442fa5cd67SKarl Rupp 
11455a9f2f41SSatish Balay   ilink->nfields = n;
11460298fd71SBarry Smith   ilink->next    = NULL;
1147ce94432eSBarry Smith   ierr           = KSPCreate(PetscObjectComm((PetscObject)pc),&ilink->ksp);CHKERRQ(ierr);
114820252d06SBarry Smith   ierr           = PetscObjectIncrementTabLevel((PetscObject)ilink->ksp,(PetscObject)pc,1);CHKERRQ(ierr);
11495a9f2f41SSatish Balay   ierr           = KSPSetType(ilink->ksp,KSPPREONLY);CHKERRQ(ierr);
11509005cf84SBarry Smith   ierr           = PetscLogObjectParent((PetscObject)pc,(PetscObject)ilink->ksp);CHKERRQ(ierr);
115169a612a9SBarry Smith 
11528caf3d72SBarry Smith   ierr = PetscSNPrintf(prefix,sizeof(prefix),"%sfieldsplit_%s_",((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "",ilink->splitname);CHKERRQ(ierr);
11535a9f2f41SSatish Balay   ierr = KSPSetOptionsPrefix(ilink->ksp,prefix);CHKERRQ(ierr);
11540971522cSBarry Smith 
11550971522cSBarry Smith   if (!next) {
11565a9f2f41SSatish Balay     jac->head       = ilink;
11570298fd71SBarry Smith     ilink->previous = NULL;
11580971522cSBarry Smith   } else {
11590971522cSBarry Smith     while (next->next) {
11600971522cSBarry Smith       next = next->next;
11610971522cSBarry Smith     }
11625a9f2f41SSatish Balay     next->next      = ilink;
116351f519a2SBarry Smith     ilink->previous = next;
11640971522cSBarry Smith   }
11650971522cSBarry Smith   jac->nsplits++;
11660971522cSBarry Smith   PetscFunctionReturn(0);
11670971522cSBarry Smith }
11680971522cSBarry Smith 
1169e69d4d44SBarry Smith #undef __FUNCT__
1170e69d4d44SBarry Smith #define __FUNCT__ "PCFieldSplitGetSubKSP_FieldSplit_Schur"
11711e6b0712SBarry Smith static PetscErrorCode  PCFieldSplitGetSubKSP_FieldSplit_Schur(PC pc,PetscInt *n,KSP **subksp)
1172e69d4d44SBarry Smith {
1173e69d4d44SBarry Smith   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
1174e69d4d44SBarry Smith   PetscErrorCode ierr;
1175e69d4d44SBarry Smith 
1176e69d4d44SBarry Smith   PetscFunctionBegin;
1177785e854fSJed Brown   ierr = PetscMalloc1(jac->nsplits,subksp);CHKERRQ(ierr);
1178e69d4d44SBarry Smith   ierr = MatSchurComplementGetKSP(jac->schur,*subksp);CHKERRQ(ierr);
11792fa5cd67SKarl Rupp 
1180e69d4d44SBarry Smith   (*subksp)[1] = jac->kspschur;
118113e0d083SBarry Smith   if (n) *n = jac->nsplits;
1182e69d4d44SBarry Smith   PetscFunctionReturn(0);
1183e69d4d44SBarry Smith }
11840971522cSBarry Smith 
11850971522cSBarry Smith #undef __FUNCT__
118669a612a9SBarry Smith #define __FUNCT__ "PCFieldSplitGetSubKSP_FieldSplit"
11871e6b0712SBarry Smith static PetscErrorCode  PCFieldSplitGetSubKSP_FieldSplit(PC pc,PetscInt *n,KSP **subksp)
11880971522cSBarry Smith {
11890971522cSBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
11900971522cSBarry Smith   PetscErrorCode    ierr;
11910971522cSBarry Smith   PetscInt          cnt   = 0;
11925a9f2f41SSatish Balay   PC_FieldSplitLink ilink = jac->head;
11930971522cSBarry Smith 
11940971522cSBarry Smith   PetscFunctionBegin;
1195785e854fSJed Brown   ierr = PetscMalloc1(jac->nsplits,subksp);CHKERRQ(ierr);
11965a9f2f41SSatish Balay   while (ilink) {
11975a9f2f41SSatish Balay     (*subksp)[cnt++] = ilink->ksp;
11985a9f2f41SSatish Balay     ilink            = ilink->next;
11990971522cSBarry Smith   }
12005d480477SMatthew G Knepley   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);
120113e0d083SBarry Smith   if (n) *n = jac->nsplits;
12020971522cSBarry Smith   PetscFunctionReturn(0);
12030971522cSBarry Smith }
12040971522cSBarry Smith 
1205704ba839SBarry Smith #undef __FUNCT__
1206704ba839SBarry Smith #define __FUNCT__ "PCFieldSplitSetIS_FieldSplit"
12071e6b0712SBarry Smith static PetscErrorCode  PCFieldSplitSetIS_FieldSplit(PC pc,const char splitname[],IS is)
1208704ba839SBarry Smith {
1209704ba839SBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1210704ba839SBarry Smith   PetscErrorCode    ierr;
1211704ba839SBarry Smith   PC_FieldSplitLink ilink, next = jac->head;
1212704ba839SBarry Smith   char              prefix[128];
1213704ba839SBarry Smith 
1214704ba839SBarry Smith   PetscFunctionBegin;
12156c924f48SJed Brown   if (jac->splitdefined) {
12166c924f48SJed Brown     ierr = PetscInfo1(pc,"Ignoring new split \"%s\" because the splits have already been defined\n",splitname);CHKERRQ(ierr);
12176c924f48SJed Brown     PetscFunctionReturn(0);
12186c924f48SJed Brown   }
1219b00a9115SJed Brown   ierr = PetscNew(&ilink);CHKERRQ(ierr);
1220a04f6461SBarry Smith   if (splitname) {
1221db4c96c1SJed Brown     ierr = PetscStrallocpy(splitname,&ilink->splitname);CHKERRQ(ierr);
1222a04f6461SBarry Smith   } else {
1223785e854fSJed Brown     ierr = PetscMalloc1(8,&ilink->splitname);CHKERRQ(ierr);
1224b5787286SJed Brown     ierr = PetscSNPrintf(ilink->splitname,7,"%D",jac->nsplits);CHKERRQ(ierr);
1225a04f6461SBarry Smith   }
12261ab39975SBarry Smith   ierr          = PetscObjectReference((PetscObject)is);CHKERRQ(ierr);
1227b5787286SJed Brown   ierr          = ISDestroy(&ilink->is);CHKERRQ(ierr);
1228b5787286SJed Brown   ilink->is     = is;
1229b5787286SJed Brown   ierr          = PetscObjectReference((PetscObject)is);CHKERRQ(ierr);
1230b5787286SJed Brown   ierr          = ISDestroy(&ilink->is_col);CHKERRQ(ierr);
1231b5787286SJed Brown   ilink->is_col = is;
12320298fd71SBarry Smith   ilink->next   = NULL;
1233ce94432eSBarry Smith   ierr          = KSPCreate(PetscObjectComm((PetscObject)pc),&ilink->ksp);CHKERRQ(ierr);
123420252d06SBarry Smith   ierr          = PetscObjectIncrementTabLevel((PetscObject)ilink->ksp,(PetscObject)pc,1);CHKERRQ(ierr);
1235704ba839SBarry Smith   ierr          = KSPSetType(ilink->ksp,KSPPREONLY);CHKERRQ(ierr);
12369005cf84SBarry Smith   ierr          = PetscLogObjectParent((PetscObject)pc,(PetscObject)ilink->ksp);CHKERRQ(ierr);
1237704ba839SBarry Smith 
12388caf3d72SBarry Smith   ierr = PetscSNPrintf(prefix,sizeof(prefix),"%sfieldsplit_%s_",((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "",ilink->splitname);CHKERRQ(ierr);
1239704ba839SBarry Smith   ierr = KSPSetOptionsPrefix(ilink->ksp,prefix);CHKERRQ(ierr);
1240704ba839SBarry Smith 
1241704ba839SBarry Smith   if (!next) {
1242704ba839SBarry Smith     jac->head       = ilink;
12430298fd71SBarry Smith     ilink->previous = NULL;
1244704ba839SBarry Smith   } else {
1245704ba839SBarry Smith     while (next->next) {
1246704ba839SBarry Smith       next = next->next;
1247704ba839SBarry Smith     }
1248704ba839SBarry Smith     next->next      = ilink;
1249704ba839SBarry Smith     ilink->previous = next;
1250704ba839SBarry Smith   }
1251704ba839SBarry Smith   jac->nsplits++;
1252704ba839SBarry Smith   PetscFunctionReturn(0);
1253704ba839SBarry Smith }
1254704ba839SBarry Smith 
12550971522cSBarry Smith #undef __FUNCT__
12560971522cSBarry Smith #define __FUNCT__ "PCFieldSplitSetFields"
12570971522cSBarry Smith /*@
12580971522cSBarry Smith     PCFieldSplitSetFields - Sets the fields for one particular split in the field split preconditioner
12590971522cSBarry Smith 
1260ad4df100SBarry Smith     Logically Collective on PC
12610971522cSBarry Smith 
12620971522cSBarry Smith     Input Parameters:
12630971522cSBarry Smith +   pc  - the preconditioner context
12640298fd71SBarry Smith .   splitname - name of this split, if NULL the number of the split is used
12650971522cSBarry Smith .   n - the number of fields in this split
1266db4c96c1SJed Brown -   fields - the fields in this split
12670971522cSBarry Smith 
12680971522cSBarry Smith     Level: intermediate
12690971522cSBarry Smith 
1270d32f9abdSBarry Smith     Notes: Use PCFieldSplitSetIS() to set a completely general set of indices as a field.
1271d32f9abdSBarry Smith 
12727287d2a3SDmitry Karpeev      The PCFieldSplitSetFields() is for defining fields as strided blocks. For example, if the block
1273d32f9abdSBarry Smith      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
1274d32f9abdSBarry Smith      0xx3xx6xx9xx12 ... x1xx4xx7xx ... xx2xx5xx8xx.. 01x34x67x... 0x1x3x5x7.. x12x45x78x....
1275d32f9abdSBarry Smith      where the numbered entries indicate what is in the field.
1276d32f9abdSBarry Smith 
1277db4c96c1SJed Brown      This function is called once per split (it creates a new split each time).  Solve options
1278db4c96c1SJed Brown      for this split will be available under the prefix -fieldsplit_SPLITNAME_.
1279db4c96c1SJed Brown 
12805d4c12cdSJungho Lee      Developer Note: This routine does not actually create the IS representing the split, that is delayed
12815d4c12cdSJungho Lee      until PCSetUp_FieldSplit(), because information about the vector/matrix layouts may not be
12825d4c12cdSJungho Lee      available when this routine is called.
12835d4c12cdSJungho Lee 
1284d32f9abdSBarry Smith .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetBlockSize(), PCFieldSplitSetIS()
12850971522cSBarry Smith 
12860971522cSBarry Smith @*/
12875d4c12cdSJungho Lee PetscErrorCode  PCFieldSplitSetFields(PC pc,const char splitname[],PetscInt n,const PetscInt *fields,const PetscInt *fields_col)
12880971522cSBarry Smith {
12894ac538c5SBarry Smith   PetscErrorCode ierr;
12900971522cSBarry Smith 
12910971522cSBarry Smith   PetscFunctionBegin;
12920700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1293db4c96c1SJed Brown   PetscValidCharPointer(splitname,2);
1294ce94432eSBarry Smith   if (n < 1) SETERRQ2(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Provided number of fields %D in split \"%s\" not positive",n,splitname);
1295db4c96c1SJed Brown   PetscValidIntPointer(fields,3);
12965d4c12cdSJungho Lee   ierr = PetscTryMethod(pc,"PCFieldSplitSetFields_C",(PC,const char[],PetscInt,const PetscInt*,const PetscInt*),(pc,splitname,n,fields,fields_col));CHKERRQ(ierr);
12970971522cSBarry Smith   PetscFunctionReturn(0);
12980971522cSBarry Smith }
12990971522cSBarry Smith 
13000971522cSBarry Smith #undef __FUNCT__
1301704ba839SBarry Smith #define __FUNCT__ "PCFieldSplitSetIS"
1302704ba839SBarry Smith /*@
1303704ba839SBarry Smith     PCFieldSplitSetIS - Sets the exact elements for field
1304704ba839SBarry Smith 
1305ad4df100SBarry Smith     Logically Collective on PC
1306704ba839SBarry Smith 
1307704ba839SBarry Smith     Input Parameters:
1308704ba839SBarry Smith +   pc  - the preconditioner context
13090298fd71SBarry Smith .   splitname - name of this split, if NULL the number of the split is used
1310db4c96c1SJed Brown -   is - the index set that defines the vector elements in this field
1311704ba839SBarry Smith 
1312d32f9abdSBarry Smith 
1313a6ffb8dbSJed Brown     Notes:
1314a6ffb8dbSJed Brown     Use PCFieldSplitSetFields(), for fields defined by strided types.
1315a6ffb8dbSJed Brown 
1316db4c96c1SJed Brown     This function is called once per split (it creates a new split each time).  Solve options
1317db4c96c1SJed Brown     for this split will be available under the prefix -fieldsplit_SPLITNAME_.
1318d32f9abdSBarry Smith 
1319704ba839SBarry Smith     Level: intermediate
1320704ba839SBarry Smith 
1321704ba839SBarry Smith .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetBlockSize()
1322704ba839SBarry Smith 
1323704ba839SBarry Smith @*/
13247087cfbeSBarry Smith PetscErrorCode  PCFieldSplitSetIS(PC pc,const char splitname[],IS is)
1325704ba839SBarry Smith {
13264ac538c5SBarry Smith   PetscErrorCode ierr;
1327704ba839SBarry Smith 
1328704ba839SBarry Smith   PetscFunctionBegin;
13290700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
13307b62db95SJungho Lee   if (splitname) PetscValidCharPointer(splitname,2);
1331db4c96c1SJed Brown   PetscValidHeaderSpecific(is,IS_CLASSID,3);
13324ac538c5SBarry Smith   ierr = PetscTryMethod(pc,"PCFieldSplitSetIS_C",(PC,const char[],IS),(pc,splitname,is));CHKERRQ(ierr);
1333704ba839SBarry Smith   PetscFunctionReturn(0);
1334704ba839SBarry Smith }
1335704ba839SBarry Smith 
1336704ba839SBarry Smith #undef __FUNCT__
133757a9adfeSMatthew G Knepley #define __FUNCT__ "PCFieldSplitGetIS"
133857a9adfeSMatthew G Knepley /*@
133957a9adfeSMatthew G Knepley     PCFieldSplitGetIS - Retrieves the elements for a field as an IS
134057a9adfeSMatthew G Knepley 
134157a9adfeSMatthew G Knepley     Logically Collective on PC
134257a9adfeSMatthew G Knepley 
134357a9adfeSMatthew G Knepley     Input Parameters:
134457a9adfeSMatthew G Knepley +   pc  - the preconditioner context
134557a9adfeSMatthew G Knepley -   splitname - name of this split
134657a9adfeSMatthew G Knepley 
134757a9adfeSMatthew G Knepley     Output Parameter:
13480298fd71SBarry Smith -   is - the index set that defines the vector elements in this field, or NULL if the field is not found
134957a9adfeSMatthew G Knepley 
135057a9adfeSMatthew G Knepley     Level: intermediate
135157a9adfeSMatthew G Knepley 
135257a9adfeSMatthew G Knepley .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetIS()
135357a9adfeSMatthew G Knepley 
135457a9adfeSMatthew G Knepley @*/
135557a9adfeSMatthew G Knepley PetscErrorCode PCFieldSplitGetIS(PC pc,const char splitname[],IS *is)
135657a9adfeSMatthew G Knepley {
135757a9adfeSMatthew G Knepley   PetscErrorCode ierr;
135857a9adfeSMatthew G Knepley 
135957a9adfeSMatthew G Knepley   PetscFunctionBegin;
136057a9adfeSMatthew G Knepley   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
136157a9adfeSMatthew G Knepley   PetscValidCharPointer(splitname,2);
136257a9adfeSMatthew G Knepley   PetscValidPointer(is,3);
136357a9adfeSMatthew G Knepley   {
136457a9adfeSMatthew G Knepley     PC_FieldSplit     *jac  = (PC_FieldSplit*) pc->data;
136557a9adfeSMatthew G Knepley     PC_FieldSplitLink ilink = jac->head;
136657a9adfeSMatthew G Knepley     PetscBool         found;
136757a9adfeSMatthew G Knepley 
13680298fd71SBarry Smith     *is = NULL;
136957a9adfeSMatthew G Knepley     while (ilink) {
137057a9adfeSMatthew G Knepley       ierr = PetscStrcmp(ilink->splitname, splitname, &found);CHKERRQ(ierr);
137157a9adfeSMatthew G Knepley       if (found) {
137257a9adfeSMatthew G Knepley         *is = ilink->is;
137357a9adfeSMatthew G Knepley         break;
137457a9adfeSMatthew G Knepley       }
137557a9adfeSMatthew G Knepley       ilink = ilink->next;
137657a9adfeSMatthew G Knepley     }
137757a9adfeSMatthew G Knepley   }
137857a9adfeSMatthew G Knepley   PetscFunctionReturn(0);
137957a9adfeSMatthew G Knepley }
138057a9adfeSMatthew G Knepley 
138157a9adfeSMatthew G Knepley #undef __FUNCT__
138251f519a2SBarry Smith #define __FUNCT__ "PCFieldSplitSetBlockSize"
138351f519a2SBarry Smith /*@
138451f519a2SBarry Smith     PCFieldSplitSetBlockSize - Sets the block size for defining where fields start in the
138551f519a2SBarry Smith       fieldsplit preconditioner. If not set the matrix block size is used.
138651f519a2SBarry Smith 
1387ad4df100SBarry Smith     Logically Collective on PC
138851f519a2SBarry Smith 
138951f519a2SBarry Smith     Input Parameters:
139051f519a2SBarry Smith +   pc  - the preconditioner context
139151f519a2SBarry Smith -   bs - the block size
139251f519a2SBarry Smith 
139351f519a2SBarry Smith     Level: intermediate
139451f519a2SBarry Smith 
139551f519a2SBarry Smith .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields()
139651f519a2SBarry Smith 
139751f519a2SBarry Smith @*/
13987087cfbeSBarry Smith PetscErrorCode  PCFieldSplitSetBlockSize(PC pc,PetscInt bs)
139951f519a2SBarry Smith {
14004ac538c5SBarry Smith   PetscErrorCode ierr;
140151f519a2SBarry Smith 
140251f519a2SBarry Smith   PetscFunctionBegin;
14030700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1404c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(pc,bs,2);
14054ac538c5SBarry Smith   ierr = PetscTryMethod(pc,"PCFieldSplitSetBlockSize_C",(PC,PetscInt),(pc,bs));CHKERRQ(ierr);
140651f519a2SBarry Smith   PetscFunctionReturn(0);
140751f519a2SBarry Smith }
140851f519a2SBarry Smith 
140951f519a2SBarry Smith #undef __FUNCT__
141069a612a9SBarry Smith #define __FUNCT__ "PCFieldSplitGetSubKSP"
14110971522cSBarry Smith /*@C
141269a612a9SBarry Smith    PCFieldSplitGetSubKSP - Gets the KSP contexts for all splits
14130971522cSBarry Smith 
141469a612a9SBarry Smith    Collective on KSP
14150971522cSBarry Smith 
14160971522cSBarry Smith    Input Parameter:
14170971522cSBarry Smith .  pc - the preconditioner context
14180971522cSBarry Smith 
14190971522cSBarry Smith    Output Parameters:
142013e0d083SBarry Smith +  n - the number of splits
142169a612a9SBarry Smith -  pc - the array of KSP contexts
14220971522cSBarry Smith 
14230971522cSBarry Smith    Note:
1424d32f9abdSBarry Smith    After PCFieldSplitGetSubKSP() the array of KSPs IS to be freed by the user
1425d32f9abdSBarry Smith    (not the KSP just the array that contains them).
14260971522cSBarry Smith 
142769a612a9SBarry Smith    You must call KSPSetUp() before calling PCFieldSplitGetSubKSP().
14280971522cSBarry Smith 
1429196cc216SBarry Smith    Fortran Usage: You must pass in a KSP array that is large enough to contain all the local KSPs.
14300298fd71SBarry Smith       You can call PCFieldSplitGetSubKSP(pc,n,NULL_OBJECT,ierr) to determine how large the
1431196cc216SBarry Smith       KSP array must be.
1432196cc216SBarry Smith 
1433196cc216SBarry Smith 
14340971522cSBarry Smith    Level: advanced
14350971522cSBarry Smith 
14360971522cSBarry Smith .seealso: PCFIELDSPLIT
14370971522cSBarry Smith @*/
14387087cfbeSBarry Smith PetscErrorCode  PCFieldSplitGetSubKSP(PC pc,PetscInt *n,KSP *subksp[])
14390971522cSBarry Smith {
14404ac538c5SBarry Smith   PetscErrorCode ierr;
14410971522cSBarry Smith 
14420971522cSBarry Smith   PetscFunctionBegin;
14430700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
144413e0d083SBarry Smith   if (n) PetscValidIntPointer(n,2);
14454ac538c5SBarry Smith   ierr = PetscUseMethod(pc,"PCFieldSplitGetSubKSP_C",(PC,PetscInt*,KSP **),(pc,n,subksp));CHKERRQ(ierr);
14460971522cSBarry Smith   PetscFunctionReturn(0);
14470971522cSBarry Smith }
14480971522cSBarry Smith 
1449e69d4d44SBarry Smith #undef __FUNCT__
1450e69d4d44SBarry Smith #define __FUNCT__ "PCFieldSplitSchurPrecondition"
1451e69d4d44SBarry Smith /*@
1452e69d4d44SBarry Smith     PCFieldSplitSchurPrecondition -  Indicates if the Schur complement is preconditioned by a preconditioner constructed by the
1453a04f6461SBarry Smith       A11 matrix. Otherwise no preconditioner is used.
1454e69d4d44SBarry Smith 
1455e69d4d44SBarry Smith     Collective on PC
1456e69d4d44SBarry Smith 
1457e69d4d44SBarry Smith     Input Parameters:
1458e69d4d44SBarry Smith +   pc      - the preconditioner context
1459a7476a74SDmitry Karpeev .   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
14600298fd71SBarry Smith -   userpre - matrix to use for preconditioning, or NULL
1461084e4875SJed Brown 
1462e69d4d44SBarry Smith     Options Database:
1463a7476a74SDmitry Karpeev .     -pc_fieldsplit_schur_precondition <self,selfp,user,a11> default is a11
1464e69d4d44SBarry Smith 
1465fd1303e9SJungho Lee     Notes:
1466fd1303e9SJungho Lee $    If ptype is
1467fd1303e9SJungho Lee $        user then the preconditioner for the Schur complement is generated by the provided matrix (pre argument
1468fd1303e9SJungho Lee $             to this function).
1469e87fab1bSBarry Smith $        a11 then the preconditioner for the Schur complement is generated by the block diagonal part of the original
1470fd1303e9SJungho Lee $             matrix associated with the Schur complement (i.e. A11)
1471fd1303e9SJungho Lee $        self the preconditioner for the Schur complement is generated from the Schur complement matrix itself:
1472fd1303e9SJungho Lee $             The only preconditioner that currently works directly with the Schur complement matrix object is the PCLSC
1473fd1303e9SJungho Lee $             preconditioner
1474a7476a74SDmitry Karpeev $        selfp then the preconditioning matrix is an explicitly-assembled approximation Sp = A11 - A10 inv(diag(A00)) A01
1475a7476a74SDmitry Karpeev $             This is only a good preconditioner when diag(A00) is a good preconditioner for A00.
1476fd1303e9SJungho Lee 
1477e87fab1bSBarry Smith      When solving a saddle point problem, where the A11 block is identically zero, using a11 as the ptype only makes sense
1478fd1303e9SJungho Lee     with the additional option -fieldsplit_1_pc_type none. Usually for saddle point problems one would use a ptype of self and
1479a7476a74SDmitry Karpeev     -fieldsplit_1_pc_type lsc which uses the least squares commutator to compute a preconditioner for the Schur complement.
1480fd1303e9SJungho Lee 
1481a7476a74SDmitry Karpeev     Developer Notes: This is a terrible name, which gives no good indication of what the function does.
1482a7476a74SDmitry Karpeev                      Among other things, it should have 'Set' in the name, since it sets the type of matrix to use.
1483fd1303e9SJungho Lee 
1484e69d4d44SBarry Smith     Level: intermediate
1485e69d4d44SBarry Smith 
1486fd1303e9SJungho Lee .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields(), PCFieldSplitSchurPreType, PCLSC
1487e69d4d44SBarry Smith 
1488e69d4d44SBarry Smith @*/
14897087cfbeSBarry Smith PetscErrorCode  PCFieldSplitSchurPrecondition(PC pc,PCFieldSplitSchurPreType ptype,Mat pre)
1490e69d4d44SBarry Smith {
14914ac538c5SBarry Smith   PetscErrorCode ierr;
1492e69d4d44SBarry Smith 
1493e69d4d44SBarry Smith   PetscFunctionBegin;
14940700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
14954ac538c5SBarry Smith   ierr = PetscTryMethod(pc,"PCFieldSplitSchurPrecondition_C",(PC,PCFieldSplitSchurPreType,Mat),(pc,ptype,pre));CHKERRQ(ierr);
1496e69d4d44SBarry Smith   PetscFunctionReturn(0);
1497e69d4d44SBarry Smith }
1498e69d4d44SBarry Smith 
1499e69d4d44SBarry Smith #undef __FUNCT__
1500470b340bSDmitry Karpeev #define __FUNCT__ "PCFieldSplitSchurGetS"
150145e7fc46SDmitry Karpeev /*@
1502470b340bSDmitry Karpeev     PCFieldSplitSchurGetS -  extract the MatSchurComplement object used by this PC in case it needs to be configured separately
150345e7fc46SDmitry Karpeev 
150445e7fc46SDmitry Karpeev     Not collective
150545e7fc46SDmitry Karpeev 
150645e7fc46SDmitry Karpeev     Input Parameter:
150745e7fc46SDmitry Karpeev .   pc      - the preconditioner context
150845e7fc46SDmitry Karpeev 
150945e7fc46SDmitry Karpeev     Output Parameter:
1510470b340bSDmitry Karpeev .   S       - the Schur complement matrix
151145e7fc46SDmitry Karpeev 
1512470b340bSDmitry Karpeev     Notes:
1513470b340bSDmitry Karpeev     This matrix should not be destroyed using MatDestroy(); rather, use PCFieldSplitSchurRestoreS().
151445e7fc46SDmitry Karpeev 
151545e7fc46SDmitry Karpeev     Level: advanced
151645e7fc46SDmitry Karpeev 
1517470b340bSDmitry Karpeev .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSchurPreType, PCFieldSplitSchurPrecondition(), MatSchurComplement, PCFieldSplitSchurRestoreS()
151845e7fc46SDmitry Karpeev 
151945e7fc46SDmitry Karpeev @*/
1520470b340bSDmitry Karpeev PetscErrorCode  PCFieldSplitSchurGetS(PC pc,Mat *S)
152145e7fc46SDmitry Karpeev {
152245e7fc46SDmitry Karpeev   PetscErrorCode ierr;
152345e7fc46SDmitry Karpeev   const char*    t;
152445e7fc46SDmitry Karpeev   PetscBool      isfs;
152545e7fc46SDmitry Karpeev   PC_FieldSplit  *jac;
152645e7fc46SDmitry Karpeev 
152745e7fc46SDmitry Karpeev   PetscFunctionBegin;
152845e7fc46SDmitry Karpeev   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
152945e7fc46SDmitry Karpeev   ierr = PetscObjectGetType((PetscObject)pc,&t);CHKERRQ(ierr);
153045e7fc46SDmitry Karpeev   ierr = PetscStrcmp(t,PCFIELDSPLIT,&isfs);CHKERRQ(ierr);
153145e7fc46SDmitry Karpeev   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PC of type PCFIELDSPLIT, got %s instead",t);
153245e7fc46SDmitry Karpeev   jac = (PC_FieldSplit*)pc->data;
1533470b340bSDmitry Karpeev   if (jac->type != PC_COMPOSITE_SCHUR) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PCFIELDSPLIT of type SCHUR, got %D instead",jac->type);
1534470b340bSDmitry Karpeev   if (S) *S = jac->schur;
153545e7fc46SDmitry Karpeev   PetscFunctionReturn(0);
153645e7fc46SDmitry Karpeev }
153745e7fc46SDmitry Karpeev 
153845e7fc46SDmitry Karpeev #undef __FUNCT__
1539470b340bSDmitry Karpeev #define __FUNCT__ "PCFieldSplitSchurRestoreS"
1540470b340bSDmitry Karpeev /*@
1541470b340bSDmitry Karpeev     PCFieldSplitSchurRestoreS -  restores the MatSchurComplement object used by this PC
1542470b340bSDmitry Karpeev 
1543470b340bSDmitry Karpeev     Not collective
1544470b340bSDmitry Karpeev 
1545470b340bSDmitry Karpeev     Input Parameters:
1546470b340bSDmitry Karpeev +   pc      - the preconditioner context
1547470b340bSDmitry Karpeev .   S       - the Schur complement matrix
1548470b340bSDmitry Karpeev 
1549470b340bSDmitry Karpeev     Level: advanced
1550470b340bSDmitry Karpeev 
1551470b340bSDmitry Karpeev .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSchurPreType, PCFieldSplitSchurPrecondition(), MatSchurComplement, PCFieldSplitSchurGetS()
1552470b340bSDmitry Karpeev 
1553470b340bSDmitry Karpeev @*/
1554*bca69d2bSDmitry Karpeev PetscErrorCode  PCFieldSplitSchurRestoreS(PC pc,Mat *S)
1555470b340bSDmitry Karpeev {
1556470b340bSDmitry Karpeev   PetscErrorCode ierr;
1557470b340bSDmitry Karpeev   const char*    t;
1558470b340bSDmitry Karpeev   PetscBool      isfs;
1559470b340bSDmitry Karpeev   PC_FieldSplit  *jac;
1560470b340bSDmitry Karpeev 
1561470b340bSDmitry Karpeev   PetscFunctionBegin;
1562470b340bSDmitry Karpeev   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1563470b340bSDmitry Karpeev   ierr = PetscObjectGetType((PetscObject)pc,&t);CHKERRQ(ierr);
1564470b340bSDmitry Karpeev   ierr = PetscStrcmp(t,PCFIELDSPLIT,&isfs);CHKERRQ(ierr);
1565470b340bSDmitry Karpeev   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PC of type PCFIELDSPLIT, got %s instead",t);
1566470b340bSDmitry Karpeev   jac = (PC_FieldSplit*)pc->data;
1567470b340bSDmitry Karpeev   if (jac->type != PC_COMPOSITE_SCHUR) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PCFIELDSPLIT of type SCHUR, got %D instead",jac->type);
1568*bca69d2bSDmitry Karpeev   if (!S || *S != jac->schur) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"MatSchurComplement restored is not the same as gotten");
1569470b340bSDmitry Karpeev   PetscFunctionReturn(0);
1570470b340bSDmitry Karpeev }
1571470b340bSDmitry Karpeev 
1572470b340bSDmitry Karpeev 
1573470b340bSDmitry Karpeev #undef __FUNCT__
1574e69d4d44SBarry Smith #define __FUNCT__ "PCFieldSplitSchurPrecondition_FieldSplit"
15751e6b0712SBarry Smith static PetscErrorCode  PCFieldSplitSchurPrecondition_FieldSplit(PC pc,PCFieldSplitSchurPreType ptype,Mat pre)
1576e69d4d44SBarry Smith {
1577e69d4d44SBarry Smith   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
1578084e4875SJed Brown   PetscErrorCode ierr;
1579e69d4d44SBarry Smith 
1580e69d4d44SBarry Smith   PetscFunctionBegin;
1581084e4875SJed Brown   jac->schurpre = ptype;
1582a7476a74SDmitry Karpeev   if (ptype == PC_FIELDSPLIT_SCHUR_PRE_USER && pre) {
15836bf464f9SBarry Smith     ierr            = MatDestroy(&jac->schur_user);CHKERRQ(ierr);
1584084e4875SJed Brown     jac->schur_user = pre;
1585084e4875SJed Brown     ierr            = PetscObjectReference((PetscObject)jac->schur_user);CHKERRQ(ierr);
1586084e4875SJed Brown   }
1587e69d4d44SBarry Smith   PetscFunctionReturn(0);
1588e69d4d44SBarry Smith }
1589e69d4d44SBarry Smith 
159030ad9308SMatthew Knepley #undef __FUNCT__
1591c9c6ffaaSJed Brown #define __FUNCT__ "PCFieldSplitSetSchurFactType"
1592ab1df9f5SJed Brown /*@
1593c9c6ffaaSJed Brown     PCFieldSplitSetSchurFactType -  sets which blocks of the approximate block factorization to retain
1594ab1df9f5SJed Brown 
1595ab1df9f5SJed Brown     Collective on PC
1596ab1df9f5SJed Brown 
1597ab1df9f5SJed Brown     Input Parameters:
1598ab1df9f5SJed Brown +   pc  - the preconditioner context
1599c9c6ffaaSJed Brown -   ftype - which blocks of factorization to retain, PC_FIELDSPLIT_SCHUR_FACT_FULL is default
1600ab1df9f5SJed Brown 
1601ab1df9f5SJed Brown     Options Database:
1602c9c6ffaaSJed Brown .     -pc_fieldsplit_schur_fact_type <diag,lower,upper,full> default is full
1603ab1df9f5SJed Brown 
1604ab1df9f5SJed Brown 
1605ab1df9f5SJed Brown     Level: intermediate
1606ab1df9f5SJed Brown 
1607ab1df9f5SJed Brown     Notes:
1608ab1df9f5SJed Brown     The FULL factorization is
1609ab1df9f5SJed Brown 
1610ab1df9f5SJed Brown $   (A   B)  = (1       0) (A   0) (1  Ainv*B)
1611ab1df9f5SJed Brown $   (C   D)    (C*Ainv  1) (0   S) (0     1  )
1612ab1df9f5SJed Brown 
16136be4592eSBarry Smith     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,
16146be4592eSBarry Smith     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).
1615ab1df9f5SJed Brown 
16166be4592eSBarry Smith     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
16176be4592eSBarry Smith     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
16186be4592eSBarry Smith     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,
16196be4592eSBarry Smith     the preconditioned operator has three distinct nonzero eigenvalues and minimal polynomial of degree at most 4, so KSPGMRES converges in at most 4 iterations.
1620ab1df9f5SJed Brown 
16216be4592eSBarry Smith     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
16226be4592eSBarry Smith     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).
1623ab1df9f5SJed Brown 
1624ab1df9f5SJed Brown     References:
1625ab1df9f5SJed Brown     Murphy, Golub, and Wathen, A note on preconditioning indefinite linear systems, SIAM J. Sci. Comput., 21 (2000) pp. 1969-1972.
1626ab1df9f5SJed Brown 
1627ab1df9f5SJed Brown     Ipsen, A note on preconditioning nonsymmetric matrices, SIAM J. Sci. Comput., 23 (2001), pp. 1050-1051.
1628ab1df9f5SJed Brown 
1629ab1df9f5SJed Brown .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields(), PCFieldSplitSchurPreType
1630ab1df9f5SJed Brown @*/
1631c9c6ffaaSJed Brown PetscErrorCode  PCFieldSplitSetSchurFactType(PC pc,PCFieldSplitSchurFactType ftype)
1632ab1df9f5SJed Brown {
1633ab1df9f5SJed Brown   PetscErrorCode ierr;
1634ab1df9f5SJed Brown 
1635ab1df9f5SJed Brown   PetscFunctionBegin;
1636ab1df9f5SJed Brown   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1637c9c6ffaaSJed Brown   ierr = PetscTryMethod(pc,"PCFieldSplitSetSchurFactType_C",(PC,PCFieldSplitSchurFactType),(pc,ftype));CHKERRQ(ierr);
1638ab1df9f5SJed Brown   PetscFunctionReturn(0);
1639ab1df9f5SJed Brown }
1640ab1df9f5SJed Brown 
1641ab1df9f5SJed Brown #undef __FUNCT__
1642c9c6ffaaSJed Brown #define __FUNCT__ "PCFieldSplitSetSchurFactType_FieldSplit"
16431e6b0712SBarry Smith static PetscErrorCode PCFieldSplitSetSchurFactType_FieldSplit(PC pc,PCFieldSplitSchurFactType ftype)
1644ab1df9f5SJed Brown {
1645ab1df9f5SJed Brown   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
1646ab1df9f5SJed Brown 
1647ab1df9f5SJed Brown   PetscFunctionBegin;
1648ab1df9f5SJed Brown   jac->schurfactorization = ftype;
1649ab1df9f5SJed Brown   PetscFunctionReturn(0);
1650ab1df9f5SJed Brown }
1651ab1df9f5SJed Brown 
1652ab1df9f5SJed Brown #undef __FUNCT__
165330ad9308SMatthew Knepley #define __FUNCT__ "PCFieldSplitGetSchurBlocks"
165430ad9308SMatthew Knepley /*@C
16558c03b21aSDmitry Karpeev    PCFieldSplitGetSchurBlocks - Gets all matrix blocks for the Schur complement
165630ad9308SMatthew Knepley 
165730ad9308SMatthew Knepley    Collective on KSP
165830ad9308SMatthew Knepley 
165930ad9308SMatthew Knepley    Input Parameter:
166030ad9308SMatthew Knepley .  pc - the preconditioner context
166130ad9308SMatthew Knepley 
166230ad9308SMatthew Knepley    Output Parameters:
1663a04f6461SBarry Smith +  A00 - the (0,0) block
1664a04f6461SBarry Smith .  A01 - the (0,1) block
1665a04f6461SBarry Smith .  A10 - the (1,0) block
1666a04f6461SBarry Smith -  A11 - the (1,1) block
166730ad9308SMatthew Knepley 
166830ad9308SMatthew Knepley    Level: advanced
166930ad9308SMatthew Knepley 
167030ad9308SMatthew Knepley .seealso: PCFIELDSPLIT
167130ad9308SMatthew Knepley @*/
1672a04f6461SBarry Smith PetscErrorCode  PCFieldSplitGetSchurBlocks(PC pc,Mat *A00,Mat *A01,Mat *A10, Mat *A11)
167330ad9308SMatthew Knepley {
167430ad9308SMatthew Knepley   PC_FieldSplit *jac = (PC_FieldSplit*) pc->data;
167530ad9308SMatthew Knepley 
167630ad9308SMatthew Knepley   PetscFunctionBegin;
16770700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1678ce94432eSBarry Smith   if (jac->type != PC_COMPOSITE_SCHUR) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONG, "FieldSplit is not using a Schur complement approach.");
1679a04f6461SBarry Smith   if (A00) *A00 = jac->pmat[0];
1680a04f6461SBarry Smith   if (A01) *A01 = jac->B;
1681a04f6461SBarry Smith   if (A10) *A10 = jac->C;
1682a04f6461SBarry Smith   if (A11) *A11 = jac->pmat[1];
168330ad9308SMatthew Knepley   PetscFunctionReturn(0);
168430ad9308SMatthew Knepley }
168530ad9308SMatthew Knepley 
168679416396SBarry Smith #undef __FUNCT__
168779416396SBarry Smith #define __FUNCT__ "PCFieldSplitSetType_FieldSplit"
16881e6b0712SBarry Smith static PetscErrorCode  PCFieldSplitSetType_FieldSplit(PC pc,PCCompositeType type)
168979416396SBarry Smith {
169079416396SBarry Smith   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
1691e69d4d44SBarry Smith   PetscErrorCode ierr;
169279416396SBarry Smith 
169379416396SBarry Smith   PetscFunctionBegin;
169479416396SBarry Smith   jac->type = type;
16953b224e63SBarry Smith   if (type == PC_COMPOSITE_SCHUR) {
16963b224e63SBarry Smith     pc->ops->apply = PCApply_FieldSplit_Schur;
16973b224e63SBarry Smith     pc->ops->view  = PCView_FieldSplit_Schur;
16982fa5cd67SKarl Rupp 
1699bdf89e91SBarry Smith     ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit_Schur);CHKERRQ(ierr);
1700bdf89e91SBarry Smith     ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSchurPrecondition_C",PCFieldSplitSchurPrecondition_FieldSplit);CHKERRQ(ierr);
1701bdf89e91SBarry Smith     ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C",PCFieldSplitSetSchurFactType_FieldSplit);CHKERRQ(ierr);
1702e69d4d44SBarry Smith 
17033b224e63SBarry Smith   } else {
17043b224e63SBarry Smith     pc->ops->apply = PCApply_FieldSplit;
17053b224e63SBarry Smith     pc->ops->view  = PCView_FieldSplit;
17062fa5cd67SKarl Rupp 
1707bdf89e91SBarry Smith     ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit);CHKERRQ(ierr);
1708bdf89e91SBarry Smith     ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSchurPrecondition_C",0);CHKERRQ(ierr);
1709bdf89e91SBarry Smith     ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C",0);CHKERRQ(ierr);
17103b224e63SBarry Smith   }
171179416396SBarry Smith   PetscFunctionReturn(0);
171279416396SBarry Smith }
171379416396SBarry Smith 
171451f519a2SBarry Smith #undef __FUNCT__
171551f519a2SBarry Smith #define __FUNCT__ "PCFieldSplitSetBlockSize_FieldSplit"
17161e6b0712SBarry Smith static PetscErrorCode  PCFieldSplitSetBlockSize_FieldSplit(PC pc,PetscInt bs)
171751f519a2SBarry Smith {
171851f519a2SBarry Smith   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
171951f519a2SBarry Smith 
172051f519a2SBarry Smith   PetscFunctionBegin;
1721ce94432eSBarry Smith   if (bs < 1) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Blocksize must be positive, you gave %D",bs);
1722ce94432eSBarry Smith   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);
172351f519a2SBarry Smith   jac->bs = bs;
172451f519a2SBarry Smith   PetscFunctionReturn(0);
172551f519a2SBarry Smith }
172651f519a2SBarry Smith 
172779416396SBarry Smith #undef __FUNCT__
172879416396SBarry Smith #define __FUNCT__ "PCFieldSplitSetType"
1729bc08b0f1SBarry Smith /*@
173079416396SBarry Smith    PCFieldSplitSetType - Sets the type of fieldsplit preconditioner.
173179416396SBarry Smith 
173279416396SBarry Smith    Collective on PC
173379416396SBarry Smith 
173479416396SBarry Smith    Input Parameter:
173579416396SBarry Smith .  pc - the preconditioner context
173681540f2fSBarry Smith .  type - PC_COMPOSITE_ADDITIVE, PC_COMPOSITE_MULTIPLICATIVE (default), PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE, PC_COMPOSITE_SPECIAL, PC_COMPOSITE_SCHUR
173779416396SBarry Smith 
173879416396SBarry Smith    Options Database Key:
1739a4efd8eaSMatthew Knepley .  -pc_fieldsplit_type <type: one of multiplicative, additive, symmetric_multiplicative, special, schur> - Sets fieldsplit preconditioner type
174079416396SBarry Smith 
1741b02e2d75SMatthew G Knepley    Level: Intermediate
174279416396SBarry Smith 
174379416396SBarry Smith .keywords: PC, set, type, composite preconditioner, additive, multiplicative
174479416396SBarry Smith 
174579416396SBarry Smith .seealso: PCCompositeSetType()
174679416396SBarry Smith 
174779416396SBarry Smith @*/
17487087cfbeSBarry Smith PetscErrorCode  PCFieldSplitSetType(PC pc,PCCompositeType type)
174979416396SBarry Smith {
17504ac538c5SBarry Smith   PetscErrorCode ierr;
175179416396SBarry Smith 
175279416396SBarry Smith   PetscFunctionBegin;
17530700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
17544ac538c5SBarry Smith   ierr = PetscTryMethod(pc,"PCFieldSplitSetType_C",(PC,PCCompositeType),(pc,type));CHKERRQ(ierr);
175579416396SBarry Smith   PetscFunctionReturn(0);
175679416396SBarry Smith }
175779416396SBarry Smith 
1758b02e2d75SMatthew G Knepley #undef __FUNCT__
1759b02e2d75SMatthew G Knepley #define __FUNCT__ "PCFieldSplitGetType"
1760b02e2d75SMatthew G Knepley /*@
1761b02e2d75SMatthew G Knepley   PCFieldSplitGetType - Gets the type of fieldsplit preconditioner.
1762b02e2d75SMatthew G Knepley 
1763b02e2d75SMatthew G Knepley   Not collective
1764b02e2d75SMatthew G Knepley 
1765b02e2d75SMatthew G Knepley   Input Parameter:
1766b02e2d75SMatthew G Knepley . pc - the preconditioner context
1767b02e2d75SMatthew G Knepley 
1768b02e2d75SMatthew G Knepley   Output Parameter:
1769b02e2d75SMatthew G Knepley . type - PC_COMPOSITE_ADDITIVE, PC_COMPOSITE_MULTIPLICATIVE (default), PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE, PC_COMPOSITE_SPECIAL, PC_COMPOSITE_SCHUR
1770b02e2d75SMatthew G Knepley 
1771b02e2d75SMatthew G Knepley   Level: Intermediate
1772b02e2d75SMatthew G Knepley 
1773b02e2d75SMatthew G Knepley .keywords: PC, set, type, composite preconditioner, additive, multiplicative
1774b02e2d75SMatthew G Knepley .seealso: PCCompositeSetType()
1775b02e2d75SMatthew G Knepley @*/
1776b02e2d75SMatthew G Knepley PetscErrorCode PCFieldSplitGetType(PC pc, PCCompositeType *type)
1777b02e2d75SMatthew G Knepley {
1778b02e2d75SMatthew G Knepley   PC_FieldSplit *jac = (PC_FieldSplit*) pc->data;
1779b02e2d75SMatthew G Knepley 
1780b02e2d75SMatthew G Knepley   PetscFunctionBegin;
1781b02e2d75SMatthew G Knepley   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1782b02e2d75SMatthew G Knepley   PetscValidIntPointer(type,2);
1783b02e2d75SMatthew G Knepley   *type = jac->type;
1784b02e2d75SMatthew G Knepley   PetscFunctionReturn(0);
1785b02e2d75SMatthew G Knepley }
1786b02e2d75SMatthew G Knepley 
17874ab8060aSDmitry Karpeev #undef __FUNCT__
17884ab8060aSDmitry Karpeev #define __FUNCT__ "PCFieldSplitSetDMSplits"
17894ab8060aSDmitry Karpeev /*@
17904ab8060aSDmitry Karpeev    PCFieldSplitSetDMSplits - Flags whether DMCreateFieldDecomposition() should be used to define the splits, whenever possible.
17914ab8060aSDmitry Karpeev 
17924ab8060aSDmitry Karpeev    Logically Collective
17934ab8060aSDmitry Karpeev 
17944ab8060aSDmitry Karpeev    Input Parameters:
17954ab8060aSDmitry Karpeev +  pc   - the preconditioner context
17964ab8060aSDmitry Karpeev -  flg  - boolean indicating whether to use field splits defined by the DM
17974ab8060aSDmitry Karpeev 
17984ab8060aSDmitry Karpeev    Options Database Key:
17994ab8060aSDmitry Karpeev .  -pc_fieldsplit_dm_splits
18004ab8060aSDmitry Karpeev 
18014ab8060aSDmitry Karpeev    Level: Intermediate
18024ab8060aSDmitry Karpeev 
18034ab8060aSDmitry Karpeev .keywords: PC, DM, composite preconditioner, additive, multiplicative
18044ab8060aSDmitry Karpeev 
18054ab8060aSDmitry Karpeev .seealso: PCFieldSplitGetDMSplits()
18064ab8060aSDmitry Karpeev 
18074ab8060aSDmitry Karpeev @*/
18084ab8060aSDmitry Karpeev PetscErrorCode  PCFieldSplitSetDMSplits(PC pc,PetscBool flg)
18094ab8060aSDmitry Karpeev {
18104ab8060aSDmitry Karpeev   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
18114ab8060aSDmitry Karpeev   PetscBool      isfs;
18124ab8060aSDmitry Karpeev   PetscErrorCode ierr;
18134ab8060aSDmitry Karpeev 
18144ab8060aSDmitry Karpeev   PetscFunctionBegin;
18154ab8060aSDmitry Karpeev   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
18164ab8060aSDmitry Karpeev   PetscValidLogicalCollectiveBool(pc,flg,2);
18174ab8060aSDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);CHKERRQ(ierr);
18184ab8060aSDmitry Karpeev   if (isfs) {
18194ab8060aSDmitry Karpeev     jac->dm_splits = flg;
18204ab8060aSDmitry Karpeev   }
18214ab8060aSDmitry Karpeev   PetscFunctionReturn(0);
18224ab8060aSDmitry Karpeev }
18234ab8060aSDmitry Karpeev 
18244ab8060aSDmitry Karpeev 
18254ab8060aSDmitry Karpeev #undef __FUNCT__
18264ab8060aSDmitry Karpeev #define __FUNCT__ "PCFieldSplitGetDMSplits"
18274ab8060aSDmitry Karpeev /*@
18284ab8060aSDmitry Karpeev    PCFieldSplitGetDMSplits - Returns flag indicating whether DMCreateFieldDecomposition() should be used to define the splits, whenever possible.
18294ab8060aSDmitry Karpeev 
18304ab8060aSDmitry Karpeev    Logically Collective
18314ab8060aSDmitry Karpeev 
18324ab8060aSDmitry Karpeev    Input Parameter:
18334ab8060aSDmitry Karpeev .  pc   - the preconditioner context
18344ab8060aSDmitry Karpeev 
18354ab8060aSDmitry Karpeev    Output Parameter:
18364ab8060aSDmitry Karpeev .  flg  - boolean indicating whether to use field splits defined by the DM
18374ab8060aSDmitry Karpeev 
18384ab8060aSDmitry Karpeev    Level: Intermediate
18394ab8060aSDmitry Karpeev 
18404ab8060aSDmitry Karpeev .keywords: PC, DM, composite preconditioner, additive, multiplicative
18414ab8060aSDmitry Karpeev 
18424ab8060aSDmitry Karpeev .seealso: PCFieldSplitSetDMSplits()
18434ab8060aSDmitry Karpeev 
18444ab8060aSDmitry Karpeev @*/
18454ab8060aSDmitry Karpeev PetscErrorCode  PCFieldSplitGetDMSplits(PC pc,PetscBool* flg)
18464ab8060aSDmitry Karpeev {
18474ab8060aSDmitry Karpeev   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
18484ab8060aSDmitry Karpeev   PetscBool      isfs;
18494ab8060aSDmitry Karpeev   PetscErrorCode ierr;
18504ab8060aSDmitry Karpeev 
18514ab8060aSDmitry Karpeev   PetscFunctionBegin;
18524ab8060aSDmitry Karpeev   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
18534ab8060aSDmitry Karpeev   PetscValidPointer(flg,2);
18544ab8060aSDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);CHKERRQ(ierr);
18554ab8060aSDmitry Karpeev   if (isfs) {
18564ab8060aSDmitry Karpeev     if(flg) *flg = jac->dm_splits;
18574ab8060aSDmitry Karpeev   }
18584ab8060aSDmitry Karpeev   PetscFunctionReturn(0);
18594ab8060aSDmitry Karpeev }
18604ab8060aSDmitry Karpeev 
18610971522cSBarry Smith /* -------------------------------------------------------------------------------------*/
18620971522cSBarry Smith /*MC
1863a8c7a070SBarry Smith    PCFIELDSPLIT - Preconditioner created by combining separate preconditioners for individual
1864a04f6461SBarry Smith                   fields or groups of fields. See the users manual section "Solving Block Matrices" for more details.
18650971522cSBarry Smith 
1866edf189efSBarry Smith      To set options on the solvers for each block append -fieldsplit_ to all the PC
1867edf189efSBarry Smith         options database keys. For example, -fieldsplit_pc_type ilu -fieldsplit_pc_factor_levels 1
18680971522cSBarry Smith 
1869a8c7a070SBarry Smith      To set the options on the solvers separate for each block call PCFieldSplitGetSubKSP()
187069a612a9SBarry Smith          and set the options directly on the resulting KSP object
18710971522cSBarry Smith 
18720971522cSBarry Smith    Level: intermediate
18730971522cSBarry Smith 
187479416396SBarry Smith    Options Database Keys:
187581540f2fSBarry Smith +   -pc_fieldsplit_%d_fields <a,b,..> - indicates the fields to be used in the %d'th split
187681540f2fSBarry Smith .   -pc_fieldsplit_default - automatically add any fields to additional splits that have not
187781540f2fSBarry Smith                               been supplied explicitly by -pc_fieldsplit_%d_fields
187881540f2fSBarry Smith .   -pc_fieldsplit_block_size <bs> - size of block that defines fields (i.e. there are bs fields)
18790f188ba9SJed Brown .   -pc_fieldsplit_type <additive,multiplicative,symmetric_multiplicative,schur> - type of relaxation or factorization splitting
1880a7476a74SDmitry Karpeev .   -pc_fieldsplit_schur_precondition <self,selfp,user,a11> - default is a11
1881435f959eSBarry Smith .   -pc_fieldsplit_detect_saddle_point - automatically finds rows with zero or negative diagonal and uses Schur complement with no preconditioner as the solver
188279416396SBarry Smith 
18835d4c12cdSJungho Lee -    Options prefix for inner solvers when using Schur complement preconditioner are -fieldsplit_0_ and -fieldsplit_1_
18845d4c12cdSJungho Lee      for all other solvers they are -fieldsplit_%d_ for the dth field, use -fieldsplit_ for all fields
18855d4c12cdSJungho Lee 
1886c8a0d604SMatthew G Knepley    Notes:
1887c8a0d604SMatthew G Knepley     Use PCFieldSplitSetFields() to set fields defined by "strided" entries and PCFieldSplitSetIS()
1888d32f9abdSBarry Smith      to define a field by an arbitrary collection of entries.
1889d32f9abdSBarry Smith 
1890d32f9abdSBarry Smith       If no fields are set the default is used. The fields are defined by entries strided by bs,
1891d32f9abdSBarry Smith       beginning at 0 then 1, etc to bs-1. The block size can be set with PCFieldSplitSetBlockSize(),
1892d32f9abdSBarry Smith       if this is not called the block size defaults to the blocksize of the second matrix passed
1893d32f9abdSBarry Smith       to KSPSetOperators()/PCSetOperators().
1894d32f9abdSBarry Smith 
1895c8a0d604SMatthew G Knepley $     For the Schur complement preconditioner if J = ( A00 A01 )
1896c8a0d604SMatthew G Knepley $                                                    ( A10 A11 )
1897c8a0d604SMatthew G Knepley $     the preconditioner using full factorization is
1898c8a0d604SMatthew G Knepley $              ( I   -A10 ksp(A00) ) ( inv(A00)     0  ) (     I          0  )
1899c8a0d604SMatthew G Knepley $              ( 0         I       ) (   0      ksp(S) ) ( -A10 ksp(A00)  I  )
1900a04f6461SBarry Smith      where the action of inv(A00) is applied using the KSP solver with prefix -fieldsplit_0_. The action of
1901c8a0d604SMatthew G Knepley      ksp(S) is computed using the KSP solver with prefix -fieldsplit_splitname_ (where splitname was given
1902a7476a74SDmitry Karpeev      in providing the SECOND split or 1 if not given). For PCFieldSplitGetKSP() when field number is 0,
1903c8a0d604SMatthew G Knepley      it returns the KSP associated with -fieldsplit_0_ while field number 1 gives -fieldsplit_1_ KSP. By default
1904a04f6461SBarry Smith      A11 is used to construct a preconditioner for S, use PCFieldSplitSchurPrecondition() to turn on or off this
1905a7476a74SDmitry Karpeev      option. You can use the preconditioner PCLSC to precondition the Schur complement with -fieldsplit_1_pc_type lsc.
1906a7476a74SDmitry Karpeev      When option -fieldsplit_schur_precondition selfp is given, an approximation to S is assembled --
1907a7476a74SDmitry Karpeev      Sp = A11 - A10 inv(diag(A00)) A01, which has type AIJ and can be used with a variety of preconditioners
1908a7476a74SDmitry Karpeev      (e.g., -fieldsplit_1_pc_type asm).
1909a7476a74SDmitry Karpeev      The factorization type is set using -pc_fieldsplit_schur_fact_type <diag, lower, upper, full>. The full is shown above,
19105668aaf4SBarry Smith      diag gives
1911c8a0d604SMatthew G Knepley $              ( inv(A00)     0   )
1912c8a0d604SMatthew G Knepley $              (   0      -ksp(S) )
19135668aaf4SBarry Smith      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
1914c8a0d604SMatthew G Knepley $              (  A00   0 )
1915c8a0d604SMatthew G Knepley $              (  A10   S )
1916c8a0d604SMatthew G Knepley      where the inverses of A00 and S are applied using KSPs. The upper factorization is the inverse of
1917c8a0d604SMatthew G Knepley $              ( A00 A01 )
1918c8a0d604SMatthew G Knepley $              (  0   S  )
1919c8a0d604SMatthew G Knepley      where again the inverses of A00 and S are applied using KSPs.
1920e69d4d44SBarry Smith 
1921edf189efSBarry Smith      If only one set of indices (one IS) is provided with PCFieldSplitSetIS() then the complement of that IS
1922edf189efSBarry Smith      is used automatically for a second block.
1923edf189efSBarry Smith 
1924ff218e97SBarry Smith      The fieldsplit preconditioner cannot currently be used with the BAIJ or SBAIJ data formats if the blocksize is larger than 1.
1925ff218e97SBarry Smith      Generally it should be used with the AIJ format.
1926ff218e97SBarry Smith 
1927ff218e97SBarry Smith      The forms of these preconditioners are closely related if not identical to forms derived as "Distributive Iterations", see,
1928ff218e97SBarry Smith      for example, page 294 in "Principles of Computational Fluid Dynamics" by Pieter Wesseling. Note that one can also use PCFIELDSPLIT
1929ff218e97SBarry Smith      inside a smoother resulting in "Distributive Smoothers".
19300716a85fSBarry Smith 
1931a541d17aSBarry Smith    Concepts: physics based preconditioners, block preconditioners
19320971522cSBarry Smith 
19337e8cb189SBarry Smith .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC, Block_Preconditioners, PCLSC,
1934e69d4d44SBarry Smith            PCFieldSplitGetSubKSP(), PCFieldSplitSetFields(), PCFieldSplitSetType(), PCFieldSplitSetIS(), PCFieldSplitSchurPrecondition()
19350971522cSBarry Smith M*/
19360971522cSBarry Smith 
19370971522cSBarry Smith #undef __FUNCT__
19380971522cSBarry Smith #define __FUNCT__ "PCCreate_FieldSplit"
19398cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PCCreate_FieldSplit(PC pc)
19400971522cSBarry Smith {
19410971522cSBarry Smith   PetscErrorCode ierr;
19420971522cSBarry Smith   PC_FieldSplit  *jac;
19430971522cSBarry Smith 
19440971522cSBarry Smith   PetscFunctionBegin;
1945b00a9115SJed Brown   ierr = PetscNewLog(pc,&jac);CHKERRQ(ierr);
19462fa5cd67SKarl Rupp 
19470971522cSBarry Smith   jac->bs                 = -1;
19480971522cSBarry Smith   jac->nsplits            = 0;
19493e197d65SBarry Smith   jac->type               = PC_COMPOSITE_MULTIPLICATIVE;
1950e6cab6aaSJed Brown   jac->schurpre           = PC_FIELDSPLIT_SCHUR_PRE_USER; /* Try user preconditioner first, fall back on diagonal */
1951c9c6ffaaSJed Brown   jac->schurfactorization = PC_FIELDSPLIT_SCHUR_FACT_FULL;
1952fbe7908bSJed Brown   jac->dm_splits          = PETSC_TRUE;
195351f519a2SBarry Smith 
19540971522cSBarry Smith   pc->data = (void*)jac;
19550971522cSBarry Smith 
19560971522cSBarry Smith   pc->ops->apply           = PCApply_FieldSplit;
1957421e10b8SBarry Smith   pc->ops->applytranspose  = PCApplyTranspose_FieldSplit;
19580971522cSBarry Smith   pc->ops->setup           = PCSetUp_FieldSplit;
1959574deadeSBarry Smith   pc->ops->reset           = PCReset_FieldSplit;
19600971522cSBarry Smith   pc->ops->destroy         = PCDestroy_FieldSplit;
19610971522cSBarry Smith   pc->ops->setfromoptions  = PCSetFromOptions_FieldSplit;
19620971522cSBarry Smith   pc->ops->view            = PCView_FieldSplit;
19630971522cSBarry Smith   pc->ops->applyrichardson = 0;
19640971522cSBarry Smith 
1965bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit);CHKERRQ(ierr);
1966bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetFields_C",PCFieldSplitSetFields_FieldSplit);CHKERRQ(ierr);
1967bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetIS_C",PCFieldSplitSetIS_FieldSplit);CHKERRQ(ierr);
1968bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetType_C",PCFieldSplitSetType_FieldSplit);CHKERRQ(ierr);
1969bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetBlockSize_C",PCFieldSplitSetBlockSize_FieldSplit);CHKERRQ(ierr);
19700971522cSBarry Smith   PetscFunctionReturn(0);
19710971522cSBarry Smith }
19720971522cSBarry Smith 
19730971522cSBarry Smith 
1974a541d17aSBarry Smith 
1975