xref: /petsc/src/ksp/pc/impls/fieldsplit/fieldsplit.c (revision 8cc058d9cd56c1ccb3be12a47760ddfc446aaffc)
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 
8443836d0SMatthew G Knepley [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 
13e87fab1bSBarry Smith const char *const PCFieldSplitSchurPreTypes[] = {"SELF","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 */
32ace3abfcSBarry Smith   PetscBool       realdiagonal;                    /* Flag to use the diagonal blocks of mat preconditioned by pmat, instead of just pmat */
3330ad9308SMatthew Knepley   PetscInt        bs;                              /* Block size for IS and Mat structures */
3430ad9308SMatthew Knepley   PetscInt        nsplits;                         /* Number of field divisions defined */
3579416396SBarry Smith   Vec             *x,*y,w1,w2;
36519d70e2SJed Brown   Mat             *mat;                            /* The diagonal block for each split */
37519d70e2SJed Brown   Mat             *pmat;                           /* The preconditioning diagonal block for each split */
3830ad9308SMatthew Knepley   Mat             *Afield;                         /* The rows of the matrix associated with each split */
39ace3abfcSBarry Smith   PetscBool       issetup;
402fa5cd67SKarl Rupp 
4130ad9308SMatthew Knepley   /* Only used when Schur complement preconditioning is used */
4230ad9308SMatthew Knepley   Mat                       B;                     /* The (0,1) block */
4330ad9308SMatthew Knepley   Mat                       C;                     /* The (1,0) block */
44443836d0SMatthew G Knepley   Mat                       schur;                 /* The Schur complement S = A11 - A10 A00^{-1} A01, the KSP here, kspinner, is H_1 in [El08] */
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;
68e87fab1bSBarry Smith   case PC_FIELDSPLIT_SCHUR_PRE_A11: return jac->pmat[1];
69084e4875SJed Brown   case PC_FIELDSPLIT_SCHUR_PRE_USER:   /* Use a user-provided matrix if it is given, otherwise diagonal block */
70084e4875SJed Brown   default:
71084e4875SJed Brown     return jac->schur_user ? jac->schur_user : jac->pmat[1];
72084e4875SJed Brown   }
73084e4875SJed Brown }
74084e4875SJed Brown 
75084e4875SJed Brown 
769804daf3SBarry Smith #include <petscdraw.h>
770971522cSBarry Smith #undef __FUNCT__
780971522cSBarry Smith #define __FUNCT__ "PCView_FieldSplit"
790971522cSBarry Smith static PetscErrorCode PCView_FieldSplit(PC pc,PetscViewer viewer)
800971522cSBarry Smith {
810971522cSBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
820971522cSBarry Smith   PetscErrorCode    ierr;
83d9884438SBarry Smith   PetscBool         iascii,isdraw;
840971522cSBarry Smith   PetscInt          i,j;
855a9f2f41SSatish Balay   PC_FieldSplitLink ilink = jac->head;
860971522cSBarry Smith 
870971522cSBarry Smith   PetscFunctionBegin;
88251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
89d9884438SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
900971522cSBarry Smith   if (iascii) {
912c9966d7SBarry Smith     if (jac->bs > 0) {
9251f519a2SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  FieldSplit with %s composition: total splits = %D, blocksize = %D\n",PCCompositeTypes[jac->type],jac->nsplits,jac->bs);CHKERRQ(ierr);
932c9966d7SBarry Smith     } else {
942c9966d7SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  FieldSplit with %s composition: total splits = %D\n",PCCompositeTypes[jac->type],jac->nsplits);CHKERRQ(ierr);
952c9966d7SBarry Smith     }
96a3df900dSMatthew G Knepley     if (jac->realdiagonal) {
97a3df900dSMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer,"  using actual matrix for blocks rather than preconditioner matrix\n");CHKERRQ(ierr);
98a3df900dSMatthew G Knepley     }
9969a612a9SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  Solver info for each split is in the following KSP objects:\n");CHKERRQ(ierr);
1000971522cSBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1010971522cSBarry Smith     for (i=0; i<jac->nsplits; i++) {
1021ab39975SBarry Smith       if (ilink->fields) {
1030971522cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Split number %D Fields ",i);CHKERRQ(ierr);
10479416396SBarry Smith         ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
1055a9f2f41SSatish Balay         for (j=0; j<ilink->nfields; j++) {
10679416396SBarry Smith           if (j > 0) {
10779416396SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer,",");CHKERRQ(ierr);
10879416396SBarry Smith           }
1095a9f2f41SSatish Balay           ierr = PetscViewerASCIIPrintf(viewer," %D",ilink->fields[j]);CHKERRQ(ierr);
1100971522cSBarry Smith         }
1110971522cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
11279416396SBarry Smith         ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
1131ab39975SBarry Smith       } else {
1141ab39975SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Split number %D Defined by IS\n",i);CHKERRQ(ierr);
1151ab39975SBarry Smith       }
1165a9f2f41SSatish Balay       ierr  = KSPView(ilink->ksp,viewer);CHKERRQ(ierr);
1175a9f2f41SSatish Balay       ilink = ilink->next;
1180971522cSBarry Smith     }
1190971522cSBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1202fa5cd67SKarl Rupp   }
1212fa5cd67SKarl Rupp 
1222fa5cd67SKarl Rupp  if (isdraw) {
123d9884438SBarry Smith     PetscDraw draw;
124d9884438SBarry Smith     PetscReal x,y,w,wd;
125d9884438SBarry Smith 
126d9884438SBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
127d9884438SBarry Smith     ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
128d9884438SBarry Smith     w    = 2*PetscMin(1.0 - x,x);
129d9884438SBarry Smith     wd   = w/(jac->nsplits + 1);
130d9884438SBarry Smith     x    = x - wd*(jac->nsplits-1)/2.0;
131d9884438SBarry Smith     for (i=0; i<jac->nsplits; i++) {
132d9884438SBarry Smith       ierr  = PetscDrawPushCurrentPoint(draw,x,y);CHKERRQ(ierr);
133d9884438SBarry Smith       ierr  = KSPView(ilink->ksp,viewer);CHKERRQ(ierr);
134d9884438SBarry Smith       ierr  = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
135d9884438SBarry Smith       x    += wd;
136d9884438SBarry Smith       ilink = ilink->next;
137d9884438SBarry Smith     }
1380971522cSBarry Smith   }
1390971522cSBarry Smith   PetscFunctionReturn(0);
1400971522cSBarry Smith }
1410971522cSBarry Smith 
1420971522cSBarry Smith #undef __FUNCT__
1433b224e63SBarry Smith #define __FUNCT__ "PCView_FieldSplit_Schur"
1443b224e63SBarry Smith static PetscErrorCode PCView_FieldSplit_Schur(PC pc,PetscViewer viewer)
1453b224e63SBarry Smith {
1463b224e63SBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1473b224e63SBarry Smith   PetscErrorCode    ierr;
1484996c5bdSBarry Smith   PetscBool         iascii,isdraw;
1493b224e63SBarry Smith   PetscInt          i,j;
1503b224e63SBarry Smith   PC_FieldSplitLink ilink = jac->head;
1513b224e63SBarry Smith 
1523b224e63SBarry Smith   PetscFunctionBegin;
153251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1544996c5bdSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1553b224e63SBarry Smith   if (iascii) {
1562c9966d7SBarry Smith     if (jac->bs > 0) {
157c9c6ffaaSJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  FieldSplit with Schur preconditioner, blocksize = %D, factorization %s\n",jac->bs,PCFieldSplitSchurFactTypes[jac->schurfactorization]);CHKERRQ(ierr);
1582c9966d7SBarry Smith     } else {
1592c9966d7SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  FieldSplit with Schur preconditioner, factorization %s\n",PCFieldSplitSchurFactTypes[jac->schurfactorization]);CHKERRQ(ierr);
1602c9966d7SBarry Smith     }
161a3df900dSMatthew G Knepley     if (jac->realdiagonal) {
162a3df900dSMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer,"  using actual matrix for blocks rather than preconditioner matrix\n");CHKERRQ(ierr);
163a3df900dSMatthew G Knepley     }
1643e8b8b31SMatthew G Knepley     switch (jac->schurpre) {
1653e8b8b31SMatthew G Knepley     case PC_FIELDSPLIT_SCHUR_PRE_SELF:
1663e8b8b31SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioner for the Schur complement formed from S itself\n");CHKERRQ(ierr);break;
167e87fab1bSBarry Smith     case PC_FIELDSPLIT_SCHUR_PRE_A11:
168e87fab1bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioner for the Schur complement formed from A11\n");CHKERRQ(ierr);break;
1693e8b8b31SMatthew G Knepley     case PC_FIELDSPLIT_SCHUR_PRE_USER:
1703e8b8b31SMatthew G Knepley       if (jac->schur_user) {
1713e8b8b31SMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioner for the Schur complement formed from user provided matrix\n");CHKERRQ(ierr);
1723e8b8b31SMatthew G Knepley       } else {
173e87fab1bSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioner for the Schur complement formed from A11\n");CHKERRQ(ierr);
1743e8b8b31SMatthew G Knepley       }
1753e8b8b31SMatthew G Knepley       break;
1763e8b8b31SMatthew G Knepley     default:
17782f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Invalid Schur preconditioning type: %d", jac->schurpre);
1783e8b8b31SMatthew G Knepley     }
1793b224e63SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  Split info:\n");CHKERRQ(ierr);
1803b224e63SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1813b224e63SBarry Smith     for (i=0; i<jac->nsplits; i++) {
1823b224e63SBarry Smith       if (ilink->fields) {
1833b224e63SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Split number %D Fields ",i);CHKERRQ(ierr);
1843b224e63SBarry Smith         ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
1853b224e63SBarry Smith         for (j=0; j<ilink->nfields; j++) {
1863b224e63SBarry Smith           if (j > 0) {
1873b224e63SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer,",");CHKERRQ(ierr);
1883b224e63SBarry Smith           }
1893b224e63SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %D",ilink->fields[j]);CHKERRQ(ierr);
1903b224e63SBarry Smith         }
1913b224e63SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
1923b224e63SBarry Smith         ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
1933b224e63SBarry Smith       } else {
1943b224e63SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Split number %D Defined by IS\n",i);CHKERRQ(ierr);
1953b224e63SBarry Smith       }
1963b224e63SBarry Smith       ilink = ilink->next;
1973b224e63SBarry Smith     }
198435f959eSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"KSP solver for A00 block \n");CHKERRQ(ierr);
1993b224e63SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
200443836d0SMatthew G Knepley     ierr = KSPView(jac->head->ksp,viewer);CHKERRQ(ierr);
2013b224e63SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
202443836d0SMatthew G Knepley     if (jac->kspupper != jac->head->ksp) {
203443836d0SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer,"KSP solver for upper A00 in upper triangular factor \n");CHKERRQ(ierr);
204443836d0SMatthew G Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
205443836d0SMatthew G Knepley       ierr = KSPView(jac->kspupper,viewer);CHKERRQ(ierr);
206443836d0SMatthew G Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
207443836d0SMatthew G Knepley     }
208435f959eSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"KSP solver for S = A11 - A10 inv(A00) A01 \n");CHKERRQ(ierr);
2093b224e63SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
21012cae6f2SJed Brown     if (jac->kspschur) {
2113b224e63SBarry Smith       ierr = KSPView(jac->kspschur,viewer);CHKERRQ(ierr);
21212cae6f2SJed Brown     } else {
21312cae6f2SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  not yet available\n");CHKERRQ(ierr);
21412cae6f2SJed Brown     }
2153b224e63SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2163b224e63SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2174996c5bdSBarry Smith   } else if (isdraw) {
2184996c5bdSBarry Smith     PetscDraw draw;
2194996c5bdSBarry Smith     PetscReal x,y,w,wd,h;
2204996c5bdSBarry Smith     PetscInt  cnt = 2;
2214996c5bdSBarry Smith     char      str[32];
2224996c5bdSBarry Smith 
2234996c5bdSBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
2244996c5bdSBarry Smith     ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
225c74581afSBarry Smith     if (jac->kspupper != jac->head->ksp) cnt++;
226c74581afSBarry Smith     w  = 2*PetscMin(1.0 - x,x);
227c74581afSBarry Smith     wd = w/(cnt + 1);
228c74581afSBarry Smith 
2294996c5bdSBarry Smith     ierr = PetscSNPrintf(str,32,"Schur fact. %s",PCFieldSplitSchurFactTypes[jac->schurfactorization]);CHKERRQ(ierr);
2300298fd71SBarry Smith     ierr = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_RED,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
2314996c5bdSBarry Smith     y   -= h;
2324996c5bdSBarry Smith     if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_USER &&  !jac->schur_user) {
233e87fab1bSBarry Smith       ierr = PetscSNPrintf(str,32,"Prec. for Schur from %s",PCFieldSplitSchurPreTypes[PC_FIELDSPLIT_SCHUR_PRE_A11]);CHKERRQ(ierr);
2343b224e63SBarry Smith     } else {
2354996c5bdSBarry Smith       ierr = PetscSNPrintf(str,32,"Prec. for Schur from %s",PCFieldSplitSchurPreTypes[jac->schurpre]);CHKERRQ(ierr);
2364996c5bdSBarry Smith     }
2370298fd71SBarry Smith     ierr = PetscDrawBoxedString(draw,x+wd*(cnt-1)/2.0,y,PETSC_DRAW_RED,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
2384996c5bdSBarry Smith     y   -= h;
2394996c5bdSBarry Smith     x    = x - wd*(cnt-1)/2.0;
2404996c5bdSBarry Smith 
2414996c5bdSBarry Smith     ierr = PetscDrawPushCurrentPoint(draw,x,y);CHKERRQ(ierr);
2424996c5bdSBarry Smith     ierr = KSPView(jac->head->ksp,viewer);CHKERRQ(ierr);
2434996c5bdSBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2444996c5bdSBarry Smith     if (jac->kspupper != jac->head->ksp) {
2454996c5bdSBarry Smith       x   += wd;
2464996c5bdSBarry Smith       ierr = PetscDrawPushCurrentPoint(draw,x,y);CHKERRQ(ierr);
2474996c5bdSBarry Smith       ierr = KSPView(jac->kspupper,viewer);CHKERRQ(ierr);
2484996c5bdSBarry Smith       ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2494996c5bdSBarry Smith     }
2504996c5bdSBarry Smith     x   += wd;
2514996c5bdSBarry Smith     ierr = PetscDrawPushCurrentPoint(draw,x,y);CHKERRQ(ierr);
2524996c5bdSBarry Smith     ierr = KSPView(jac->kspschur,viewer);CHKERRQ(ierr);
2534996c5bdSBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2543b224e63SBarry Smith   }
2553b224e63SBarry Smith   PetscFunctionReturn(0);
2563b224e63SBarry Smith }
2573b224e63SBarry Smith 
2583b224e63SBarry Smith #undef __FUNCT__
2596c924f48SJed Brown #define __FUNCT__ "PCFieldSplitSetRuntimeSplits_Private"
2606c924f48SJed Brown /* Precondition: jac->bs is set to a meaningful value */
2616c924f48SJed Brown static PetscErrorCode PCFieldSplitSetRuntimeSplits_Private(PC pc)
2626c924f48SJed Brown {
2636c924f48SJed Brown   PetscErrorCode ierr;
2646c924f48SJed Brown   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
2655d4c12cdSJungho Lee   PetscInt       i,nfields,*ifields,nfields_col,*ifields_col;
2665d4c12cdSJungho Lee   PetscBool      flg,flg_col;
2675d4c12cdSJungho Lee   char           optionname[128],splitname[8],optionname_col[128];
2686c924f48SJed Brown 
2696c924f48SJed Brown   PetscFunctionBegin;
2706c924f48SJed Brown   ierr = PetscMalloc(jac->bs*sizeof(PetscInt),&ifields);CHKERRQ(ierr);
2715d4c12cdSJungho Lee   ierr = PetscMalloc(jac->bs*sizeof(PetscInt),&ifields_col);CHKERRQ(ierr);
2726c924f48SJed Brown   for (i=0,flg=PETSC_TRUE;; i++) {
2738caf3d72SBarry Smith     ierr        = PetscSNPrintf(splitname,sizeof(splitname),"%D",i);CHKERRQ(ierr);
2748caf3d72SBarry Smith     ierr        = PetscSNPrintf(optionname,sizeof(optionname),"-pc_fieldsplit_%D_fields",i);CHKERRQ(ierr);
2758caf3d72SBarry Smith     ierr        = PetscSNPrintf(optionname_col,sizeof(optionname_col),"-pc_fieldsplit_%D_fields_col",i);CHKERRQ(ierr);
2766c924f48SJed Brown     nfields     = jac->bs;
27729499fbbSJungho Lee     nfields_col = jac->bs;
2786c924f48SJed Brown     ierr        = PetscOptionsGetIntArray(((PetscObject)pc)->prefix,optionname,ifields,&nfields,&flg);CHKERRQ(ierr);
2795d4c12cdSJungho Lee     ierr        = PetscOptionsGetIntArray(((PetscObject)pc)->prefix,optionname_col,ifields_col,&nfields_col,&flg_col);CHKERRQ(ierr);
2806c924f48SJed Brown     if (!flg) break;
2815d4c12cdSJungho Lee     else if (flg && !flg_col) {
2825d4c12cdSJungho Lee       if (!nfields) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Cannot list zero fields");
2835d4c12cdSJungho Lee       ierr = PCFieldSplitSetFields(pc,splitname,nfields,ifields,ifields);CHKERRQ(ierr);
2842fa5cd67SKarl Rupp     } else {
2855d4c12cdSJungho Lee       if (!nfields || !nfields_col) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Cannot list zero fields");
2865d4c12cdSJungho Lee       if (nfields != nfields_col) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Number of row and column fields must match");
2875d4c12cdSJungho Lee       ierr = PCFieldSplitSetFields(pc,splitname,nfields,ifields,ifields_col);CHKERRQ(ierr);
2885d4c12cdSJungho Lee     }
2896c924f48SJed Brown   }
2906c924f48SJed Brown   if (i > 0) {
2916c924f48SJed Brown     /* Makes command-line setting of splits take precedence over setting them in code.
2926c924f48SJed Brown        Otherwise subsequent calls to PCFieldSplitSetIS() or PCFieldSplitSetFields() would
2936c924f48SJed Brown        create new splits, which would probably not be what the user wanted. */
2946c924f48SJed Brown     jac->splitdefined = PETSC_TRUE;
2956c924f48SJed Brown   }
2966c924f48SJed Brown   ierr = PetscFree(ifields);CHKERRQ(ierr);
2975d4c12cdSJungho Lee   ierr = PetscFree(ifields_col);CHKERRQ(ierr);
2986c924f48SJed Brown   PetscFunctionReturn(0);
2996c924f48SJed Brown }
3006c924f48SJed Brown 
3016c924f48SJed Brown #undef __FUNCT__
30269a612a9SBarry Smith #define __FUNCT__ "PCFieldSplitSetDefaults"
30369a612a9SBarry Smith static PetscErrorCode PCFieldSplitSetDefaults(PC pc)
3040971522cSBarry Smith {
3050971522cSBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
3060971522cSBarry Smith   PetscErrorCode    ierr;
3075a9f2f41SSatish Balay   PC_FieldSplitLink ilink              = jac->head;
3087287d2a3SDmitry Karpeev   PetscBool         fieldsplit_default = PETSC_FALSE,stokes = PETSC_FALSE;
3096c924f48SJed Brown   PetscInt          i;
3100971522cSBarry Smith 
3110971522cSBarry Smith   PetscFunctionBegin;
3127287d2a3SDmitry Karpeev   /*
3137287d2a3SDmitry Karpeev    Kinda messy, but at least this now uses DMCreateFieldDecomposition() even with jac->reset.
3147287d2a3SDmitry Karpeev    Should probably be rewritten.
3157287d2a3SDmitry Karpeev    */
3167287d2a3SDmitry Karpeev   if (!ilink || jac->reset) {
3170298fd71SBarry Smith     ierr = PetscOptionsGetBool(((PetscObject)pc)->prefix,"-pc_fieldsplit_detect_saddle_point",&stokes,NULL);CHKERRQ(ierr);
3184ab8060aSDmitry Karpeev     if (pc->dm && jac->dm_splits && !stokes) {
319bafc1b83SMatthew G Knepley       PetscInt  numFields, f, i, j;
3200784a22cSJed Brown       char      **fieldNames;
3217b62db95SJungho Lee       IS        *fields;
322e7c4fc90SDmitry Karpeev       DM        *dms;
323bafc1b83SMatthew G Knepley       DM        subdm[128];
324bafc1b83SMatthew G Knepley       PetscBool flg;
325bafc1b83SMatthew G Knepley 
32616621825SDmitry Karpeev       ierr = DMCreateFieldDecomposition(pc->dm, &numFields, &fieldNames, &fields, &dms);CHKERRQ(ierr);
327bafc1b83SMatthew G Knepley       /* Allow the user to prescribe the splits */
328bafc1b83SMatthew G Knepley       for (i = 0, flg = PETSC_TRUE;; i++) {
329bafc1b83SMatthew G Knepley         PetscInt ifields[128];
330bafc1b83SMatthew G Knepley         IS       compField;
331bafc1b83SMatthew G Knepley         char     optionname[128], splitname[8];
332bafc1b83SMatthew G Knepley         PetscInt nfields = numFields;
333bafc1b83SMatthew G Knepley 
3348caf3d72SBarry Smith         ierr = PetscSNPrintf(optionname, sizeof(optionname), "-pc_fieldsplit_%D_fields", i);CHKERRQ(ierr);
335bafc1b83SMatthew G Knepley         ierr = PetscOptionsGetIntArray(((PetscObject) pc)->prefix, optionname, ifields, &nfields, &flg);CHKERRQ(ierr);
336bafc1b83SMatthew G Knepley         if (!flg) break;
33782f516ccSBarry Smith         if (numFields > 128) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Cannot currently support %d > 128 fields", numFields);
338bafc1b83SMatthew G Knepley         ierr = DMCreateSubDM(pc->dm, nfields, ifields, &compField, &subdm[i]);CHKERRQ(ierr);
339bafc1b83SMatthew G Knepley         if (nfields == 1) {
340bafc1b83SMatthew G Knepley           ierr = PCFieldSplitSetIS(pc, fieldNames[ifields[0]], compField);CHKERRQ(ierr);
34182f516ccSBarry Smith           /* ierr = PetscPrintf(PetscObjectComm((PetscObject)pc), "%s Field Indices:", fieldNames[ifields[0]]);CHKERRQ(ierr);
3420298fd71SBarry Smith              ierr = ISView(compField, NULL);CHKERRQ(ierr); */
343bafc1b83SMatthew G Knepley         } else {
3448caf3d72SBarry Smith           ierr = PetscSNPrintf(splitname, sizeof(splitname), "%D", i);CHKERRQ(ierr);
345bafc1b83SMatthew G Knepley           ierr = PCFieldSplitSetIS(pc, splitname, compField);CHKERRQ(ierr);
34682f516ccSBarry Smith           /* ierr = PetscPrintf(PetscObjectComm((PetscObject)pc), "%s Field Indices:", splitname);CHKERRQ(ierr);
3470298fd71SBarry Smith              ierr = ISView(compField, NULL);CHKERRQ(ierr); */
3487287d2a3SDmitry Karpeev         }
349bafc1b83SMatthew G Knepley         ierr = ISDestroy(&compField);CHKERRQ(ierr);
350bafc1b83SMatthew G Knepley         for (j = 0; j < nfields; ++j) {
351bafc1b83SMatthew G Knepley           f    = ifields[j];
3527b62db95SJungho Lee           ierr = PetscFree(fieldNames[f]);CHKERRQ(ierr);
3537b62db95SJungho Lee           ierr = ISDestroy(&fields[f]);CHKERRQ(ierr);
3547b62db95SJungho Lee         }
355bafc1b83SMatthew G Knepley       }
356bafc1b83SMatthew G Knepley       if (i == 0) {
357bafc1b83SMatthew G Knepley         for (f = 0; f < numFields; ++f) {
358bafc1b83SMatthew G Knepley           ierr = PCFieldSplitSetIS(pc, fieldNames[f], fields[f]);CHKERRQ(ierr);
359bafc1b83SMatthew G Knepley           ierr = PetscFree(fieldNames[f]);CHKERRQ(ierr);
360bafc1b83SMatthew G Knepley           ierr = ISDestroy(&fields[f]);CHKERRQ(ierr);
361bafc1b83SMatthew G Knepley         }
362bafc1b83SMatthew G Knepley       } else {
363d724dfffSBarry Smith         for (j=0; j<numFields; j++) {
364d724dfffSBarry Smith           ierr = DMDestroy(dms+j);CHKERRQ(ierr);
365d724dfffSBarry Smith         }
366d724dfffSBarry Smith         ierr = PetscFree(dms);CHKERRQ(ierr);
367bafc1b83SMatthew G Knepley         ierr = PetscMalloc(i * sizeof(DM), &dms);CHKERRQ(ierr);
3682fa5cd67SKarl Rupp         for (j = 0; j < i; ++j) dms[j] = subdm[j];
369bafc1b83SMatthew G Knepley       }
3707b62db95SJungho Lee       ierr = PetscFree(fieldNames);CHKERRQ(ierr);
3717b62db95SJungho Lee       ierr = PetscFree(fields);CHKERRQ(ierr);
372e7c4fc90SDmitry Karpeev       if (dms) {
3738b8307b2SJed Brown         ierr = PetscInfo(pc, "Setting up physics based fieldsplit preconditioner using the embedded DM\n");CHKERRQ(ierr);
374bafc1b83SMatthew G Knepley         for (ilink = jac->head, i = 0; ilink; ilink = ilink->next, ++i) {
3757287d2a3SDmitry Karpeev           const char *prefix;
3767287d2a3SDmitry Karpeev           ierr = PetscObjectGetOptionsPrefix((PetscObject)(ilink->ksp),&prefix);CHKERRQ(ierr);
3777287d2a3SDmitry Karpeev           ierr = PetscObjectSetOptionsPrefix((PetscObject)(dms[i]), prefix);CHKERRQ(ierr);
3787b62db95SJungho Lee           ierr = KSPSetDM(ilink->ksp, dms[i]);CHKERRQ(ierr);
3797b62db95SJungho Lee           ierr = KSPSetDMActive(ilink->ksp, PETSC_FALSE);CHKERRQ(ierr);
3807287d2a3SDmitry Karpeev           ierr = PetscObjectIncrementTabLevel((PetscObject)dms[i],(PetscObject)ilink->ksp,0);CHKERRQ(ierr);
381e7c4fc90SDmitry Karpeev           ierr = DMDestroy(&dms[i]);CHKERRQ(ierr);
3822fa5ba8aSJed Brown         }
3837b62db95SJungho Lee         ierr = PetscFree(dms);CHKERRQ(ierr);
3848b8307b2SJed Brown       }
38566ffff09SJed Brown     } else {
386521d7252SBarry Smith       if (jac->bs <= 0) {
387704ba839SBarry Smith         if (pc->pmat) {
388521d7252SBarry Smith           ierr = MatGetBlockSize(pc->pmat,&jac->bs);CHKERRQ(ierr);
3892fa5cd67SKarl Rupp         } else jac->bs = 1;
390521d7252SBarry Smith       }
391d32f9abdSBarry Smith 
3926ce1633cSBarry Smith       if (stokes) {
3936ce1633cSBarry Smith         IS       zerodiags,rest;
3946ce1633cSBarry Smith         PetscInt nmin,nmax;
3956ce1633cSBarry Smith 
3966ce1633cSBarry Smith         ierr = MatGetOwnershipRange(pc->mat,&nmin,&nmax);CHKERRQ(ierr);
3976ce1633cSBarry Smith         ierr = MatFindZeroDiagonals(pc->mat,&zerodiags);CHKERRQ(ierr);
3986ce1633cSBarry Smith         ierr = ISComplement(zerodiags,nmin,nmax,&rest);CHKERRQ(ierr);
3997287d2a3SDmitry Karpeev         if (jac->reset) {
4007287d2a3SDmitry Karpeev           jac->head->is       = rest;
4017287d2a3SDmitry Karpeev           jac->head->next->is = zerodiags;
4022fa5cd67SKarl Rupp         } else {
4036ce1633cSBarry Smith           ierr = PCFieldSplitSetIS(pc,"0",rest);CHKERRQ(ierr);
4046ce1633cSBarry Smith           ierr = PCFieldSplitSetIS(pc,"1",zerodiags);CHKERRQ(ierr);
4057287d2a3SDmitry Karpeev         }
406fcfd50ebSBarry Smith         ierr = ISDestroy(&zerodiags);CHKERRQ(ierr);
407fcfd50ebSBarry Smith         ierr = ISDestroy(&rest);CHKERRQ(ierr);
4086ce1633cSBarry Smith       } else {
409ce94432eSBarry Smith         if (jac->reset) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Cases not yet handled when PCReset() was used");
4109eeaaa73SJed Brown         ierr = PetscOptionsGetBool(((PetscObject)pc)->prefix,"-pc_fieldsplit_default",&fieldsplit_default,NULL);CHKERRQ(ierr);
4117287d2a3SDmitry Karpeev         if (!fieldsplit_default) {
412d32f9abdSBarry Smith           /* Allow user to set fields from command line,  if bs was known at the time of PCSetFromOptions_FieldSplit()
413d32f9abdSBarry Smith            then it is set there. This is not ideal because we should only have options set in XXSetFromOptions(). */
4146c924f48SJed Brown           ierr = PCFieldSplitSetRuntimeSplits_Private(pc);CHKERRQ(ierr);
4156c924f48SJed Brown           if (jac->splitdefined) {ierr = PetscInfo(pc,"Splits defined using the options database\n");CHKERRQ(ierr);}
416d32f9abdSBarry Smith         }
4177287d2a3SDmitry Karpeev         if (fieldsplit_default || !jac->splitdefined) {
418d32f9abdSBarry Smith           ierr = PetscInfo(pc,"Using default splitting of fields\n");CHKERRQ(ierr);
419db4c96c1SJed Brown           for (i=0; i<jac->bs; i++) {
4206c924f48SJed Brown             char splitname[8];
4218caf3d72SBarry Smith             ierr = PetscSNPrintf(splitname,sizeof(splitname),"%D",i);CHKERRQ(ierr);
4225d4c12cdSJungho Lee             ierr = PCFieldSplitSetFields(pc,splitname,1,&i,&i);CHKERRQ(ierr);
42379416396SBarry Smith           }
4245d4c12cdSJungho Lee           jac->defaultsplit = PETSC_TRUE;
425521d7252SBarry Smith         }
42666ffff09SJed Brown       }
4276ce1633cSBarry Smith     }
428edf189efSBarry Smith   } else if (jac->nsplits == 1) {
429edf189efSBarry Smith     if (ilink->is) {
430edf189efSBarry Smith       IS       is2;
431edf189efSBarry Smith       PetscInt nmin,nmax;
432edf189efSBarry Smith 
433edf189efSBarry Smith       ierr = MatGetOwnershipRange(pc->mat,&nmin,&nmax);CHKERRQ(ierr);
434edf189efSBarry Smith       ierr = ISComplement(ilink->is,nmin,nmax,&is2);CHKERRQ(ierr);
435db4c96c1SJed Brown       ierr = PCFieldSplitSetIS(pc,"1",is2);CHKERRQ(ierr);
436fcfd50ebSBarry Smith       ierr = ISDestroy(&is2);CHKERRQ(ierr);
43782f516ccSBarry Smith     } else SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Must provide at least two sets of fields to PCFieldSplit()");
438edf189efSBarry Smith   }
439d0af7cd3SBarry Smith 
440d0af7cd3SBarry Smith 
44182f516ccSBarry Smith   if (jac->nsplits < 2) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_PLIB,"Unhandled case, must have at least two fields, not %d", jac->nsplits);
44269a612a9SBarry Smith   PetscFunctionReturn(0);
44369a612a9SBarry Smith }
44469a612a9SBarry Smith 
4455a576424SJed Brown PETSC_EXTERN PetscErrorCode PetscOptionsFindPairPrefix_Private(const char pre[], const char name[], char *value[], PetscBool *flg);
446514bf10dSMatthew G Knepley 
44769a612a9SBarry Smith #undef __FUNCT__
44869a612a9SBarry Smith #define __FUNCT__ "PCSetUp_FieldSplit"
44969a612a9SBarry Smith static PetscErrorCode PCSetUp_FieldSplit(PC pc)
45069a612a9SBarry Smith {
45169a612a9SBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
45269a612a9SBarry Smith   PetscErrorCode    ierr;
4535a9f2f41SSatish Balay   PC_FieldSplitLink ilink;
4542c9966d7SBarry Smith   PetscInt          i,nsplit;
45569a612a9SBarry Smith   MatStructure      flag = pc->flag;
4565f4ab4e1SJungho Lee   PetscBool         sorted, sorted_col;
45769a612a9SBarry Smith 
45869a612a9SBarry Smith   PetscFunctionBegin;
45969a612a9SBarry Smith   ierr   = PCFieldSplitSetDefaults(pc);CHKERRQ(ierr);
46097bbdb24SBarry Smith   nsplit = jac->nsplits;
4615a9f2f41SSatish Balay   ilink  = jac->head;
46297bbdb24SBarry Smith 
46397bbdb24SBarry Smith   /* get the matrices for each split */
464704ba839SBarry Smith   if (!jac->issetup) {
4651b9fc7fcSBarry Smith     PetscInt rstart,rend,nslots,bs;
46697bbdb24SBarry Smith 
467704ba839SBarry Smith     jac->issetup = PETSC_TRUE;
468704ba839SBarry Smith 
4695d4c12cdSJungho Lee     /* This is done here instead of in PCFieldSplitSetFields() because may not have matrix at that point */
4702c9966d7SBarry Smith     if (jac->defaultsplit || !ilink->is) {
4712c9966d7SBarry Smith       if (jac->bs <= 0) jac->bs = nsplit;
4722c9966d7SBarry Smith     }
47351f519a2SBarry Smith     bs     = jac->bs;
47497bbdb24SBarry Smith     ierr   = MatGetOwnershipRange(pc->pmat,&rstart,&rend);CHKERRQ(ierr);
4751b9fc7fcSBarry Smith     nslots = (rend - rstart)/bs;
4761b9fc7fcSBarry Smith     for (i=0; i<nsplit; i++) {
4771b9fc7fcSBarry Smith       if (jac->defaultsplit) {
478ce94432eSBarry Smith         ierr = ISCreateStride(PetscObjectComm((PetscObject)pc),nslots,rstart+i,nsplit,&ilink->is);CHKERRQ(ierr);
4795f4ab4e1SJungho Lee         ierr = ISDuplicate(ilink->is,&ilink->is_col);CHKERRQ(ierr);
480704ba839SBarry Smith       } else if (!ilink->is) {
481ccb205f8SBarry Smith         if (ilink->nfields > 1) {
4825f4ab4e1SJungho Lee           PetscInt *ii,*jj,j,k,nfields = ilink->nfields,*fields = ilink->fields,*fields_col = ilink->fields_col;
4835a9f2f41SSatish Balay           ierr = PetscMalloc(ilink->nfields*nslots*sizeof(PetscInt),&ii);CHKERRQ(ierr);
4845f4ab4e1SJungho Lee           ierr = PetscMalloc(ilink->nfields*nslots*sizeof(PetscInt),&jj);CHKERRQ(ierr);
4851b9fc7fcSBarry Smith           for (j=0; j<nslots; j++) {
4861b9fc7fcSBarry Smith             for (k=0; k<nfields; k++) {
4871b9fc7fcSBarry Smith               ii[nfields*j + k] = rstart + bs*j + fields[k];
4885f4ab4e1SJungho Lee               jj[nfields*j + k] = rstart + bs*j + fields_col[k];
48997bbdb24SBarry Smith             }
49097bbdb24SBarry Smith           }
491ce94432eSBarry Smith           ierr = ISCreateGeneral(PetscObjectComm((PetscObject)pc),nslots*nfields,ii,PETSC_OWN_POINTER,&ilink->is);CHKERRQ(ierr);
492ce94432eSBarry Smith           ierr = ISCreateGeneral(PetscObjectComm((PetscObject)pc),nslots*nfields,jj,PETSC_OWN_POINTER,&ilink->is_col);CHKERRQ(ierr);
493ccb205f8SBarry Smith         } else {
494ce94432eSBarry Smith           ierr = ISCreateStride(PetscObjectComm((PetscObject)pc),nslots,rstart+ilink->fields[0],bs,&ilink->is);CHKERRQ(ierr);
495ce94432eSBarry Smith           ierr = ISCreateStride(PetscObjectComm((PetscObject)pc),nslots,rstart+ilink->fields_col[0],bs,&ilink->is_col);CHKERRQ(ierr);
496ccb205f8SBarry Smith        }
4973e197d65SBarry Smith       }
498704ba839SBarry Smith       ierr = ISSorted(ilink->is,&sorted);CHKERRQ(ierr);
4995f4ab4e1SJungho Lee       if (ilink->is_col) { ierr = ISSorted(ilink->is_col,&sorted_col);CHKERRQ(ierr); }
5005f4ab4e1SJungho Lee       if (!sorted || !sorted_col) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Fields must be sorted when creating split");
501704ba839SBarry Smith       ilink = ilink->next;
5021b9fc7fcSBarry Smith     }
5031b9fc7fcSBarry Smith   }
5041b9fc7fcSBarry Smith 
505704ba839SBarry Smith   ilink = jac->head;
50697bbdb24SBarry Smith   if (!jac->pmat) {
507bdddcaaaSMatthew G Knepley     Vec xtmp;
508bdddcaaaSMatthew G Knepley 
5090298fd71SBarry Smith     ierr = MatGetVecs(pc->pmat,&xtmp,NULL);CHKERRQ(ierr);
510cf502942SBarry Smith     ierr = PetscMalloc(nsplit*sizeof(Mat),&jac->pmat);CHKERRQ(ierr);
511bdddcaaaSMatthew G Knepley     ierr = PetscMalloc2(nsplit,Vec,&jac->x,nsplit,Vec,&jac->y);CHKERRQ(ierr);
512cf502942SBarry Smith     for (i=0; i<nsplit; i++) {
513bdddcaaaSMatthew G Knepley       MatNullSpace sp;
514bdddcaaaSMatthew G Knepley 
515a3df900dSMatthew G Knepley       /* Check for preconditioning matrix attached to IS */
516a3df900dSMatthew G Knepley       ierr = PetscObjectQuery((PetscObject) ilink->is, "pmat", (PetscObject*) &jac->pmat[i]);CHKERRQ(ierr);
517a3df900dSMatthew G Knepley       if (jac->pmat[i]) {
518a3df900dSMatthew G Knepley         ierr = PetscObjectReference((PetscObject) jac->pmat[i]);CHKERRQ(ierr);
519a3df900dSMatthew G Knepley         if (jac->type == PC_COMPOSITE_SCHUR) {
520a3df900dSMatthew G Knepley           jac->schur_user = jac->pmat[i];
5212fa5cd67SKarl Rupp 
522a3df900dSMatthew G Knepley           ierr = PetscObjectReference((PetscObject) jac->schur_user);CHKERRQ(ierr);
523a3df900dSMatthew G Knepley         }
524a3df900dSMatthew G Knepley       } else {
5255f4ab4e1SJungho Lee         ierr = MatGetSubMatrix(pc->pmat,ilink->is,ilink->is_col,MAT_INITIAL_MATRIX,&jac->pmat[i]);CHKERRQ(ierr);
526a3df900dSMatthew G Knepley       }
527bdddcaaaSMatthew G Knepley       /* create work vectors for each split */
528bdddcaaaSMatthew G Knepley       ierr     = MatGetVecs(jac->pmat[i],&jac->x[i],&jac->y[i]);CHKERRQ(ierr);
5290298fd71SBarry Smith       ilink->x = jac->x[i]; ilink->y = jac->y[i]; ilink->z = NULL;
530bdddcaaaSMatthew G Knepley       /* compute scatter contexts needed by multiplicative versions and non-default splits */
5310298fd71SBarry Smith       ierr = VecScatterCreate(xtmp,ilink->is,jac->x[i],NULL,&ilink->sctx);CHKERRQ(ierr);
532ed1f0337SMatthew G Knepley       /* Check for null space attached to IS */
533bafc1b83SMatthew G Knepley       ierr = PetscObjectQuery((PetscObject) ilink->is, "nullspace", (PetscObject*) &sp);CHKERRQ(ierr);
534bafc1b83SMatthew G Knepley       if (sp) {
535bafc1b83SMatthew G Knepley         ierr = MatSetNullSpace(jac->pmat[i], sp);CHKERRQ(ierr);
536bafc1b83SMatthew G Knepley       }
537ed1f0337SMatthew G Knepley       ierr = PetscObjectQuery((PetscObject) ilink->is, "nearnullspace", (PetscObject*) &sp);CHKERRQ(ierr);
538ed1f0337SMatthew G Knepley       if (sp) {
539ed1f0337SMatthew G Knepley         ierr = MatSetNearNullSpace(jac->pmat[i], sp);CHKERRQ(ierr);
540ed1f0337SMatthew G Knepley       }
541704ba839SBarry Smith       ilink = ilink->next;
542cf502942SBarry Smith     }
543bdddcaaaSMatthew G Knepley     ierr = VecDestroy(&xtmp);CHKERRQ(ierr);
54497bbdb24SBarry Smith   } else {
545cf502942SBarry Smith     for (i=0; i<nsplit; i++) {
546a3df900dSMatthew G Knepley       Mat pmat;
547a3df900dSMatthew G Knepley 
548a3df900dSMatthew G Knepley       /* Check for preconditioning matrix attached to IS */
549a3df900dSMatthew G Knepley       ierr = PetscObjectQuery((PetscObject) ilink->is, "pmat", (PetscObject*) &pmat);CHKERRQ(ierr);
550a3df900dSMatthew G Knepley       if (!pmat) {
5515f4ab4e1SJungho Lee         ierr = MatGetSubMatrix(pc->pmat,ilink->is,ilink->is_col,MAT_REUSE_MATRIX,&jac->pmat[i]);CHKERRQ(ierr);
552a3df900dSMatthew G Knepley       }
553704ba839SBarry Smith       ilink = ilink->next;
554cf502942SBarry Smith     }
55597bbdb24SBarry Smith   }
556519d70e2SJed Brown   if (jac->realdiagonal) {
557519d70e2SJed Brown     ilink = jac->head;
558519d70e2SJed Brown     if (!jac->mat) {
559519d70e2SJed Brown       ierr = PetscMalloc(nsplit*sizeof(Mat),&jac->mat);CHKERRQ(ierr);
560519d70e2SJed Brown       for (i=0; i<nsplit; i++) {
5615f4ab4e1SJungho Lee         ierr  = MatGetSubMatrix(pc->mat,ilink->is,ilink->is_col,MAT_INITIAL_MATRIX,&jac->mat[i]);CHKERRQ(ierr);
562519d70e2SJed Brown         ilink = ilink->next;
563519d70e2SJed Brown       }
564519d70e2SJed Brown     } else {
565519d70e2SJed Brown       for (i=0; i<nsplit; i++) {
5665f4ab4e1SJungho Lee         if (jac->mat[i]) {ierr = MatGetSubMatrix(pc->mat,ilink->is,ilink->is_col,MAT_REUSE_MATRIX,&jac->mat[i]);CHKERRQ(ierr);}
567519d70e2SJed Brown         ilink = ilink->next;
568519d70e2SJed Brown       }
569519d70e2SJed Brown     }
570519d70e2SJed Brown   } else {
571519d70e2SJed Brown     jac->mat = jac->pmat;
572519d70e2SJed Brown   }
57397bbdb24SBarry Smith 
5746c8605c2SJed Brown   if (jac->type != PC_COMPOSITE_ADDITIVE  && jac->type != PC_COMPOSITE_SCHUR) {
57568dd23aaSBarry Smith     /* extract the rows of the matrix associated with each field: used for efficient computation of residual inside algorithm */
57668dd23aaSBarry Smith     ilink = jac->head;
57768dd23aaSBarry Smith     if (!jac->Afield) {
57868dd23aaSBarry Smith       ierr = PetscMalloc(nsplit*sizeof(Mat),&jac->Afield);CHKERRQ(ierr);
57968dd23aaSBarry Smith       for (i=0; i<nsplit; i++) {
5800298fd71SBarry Smith         ierr  = MatGetSubMatrix(pc->mat,ilink->is,NULL,MAT_INITIAL_MATRIX,&jac->Afield[i]);CHKERRQ(ierr);
58168dd23aaSBarry Smith         ilink = ilink->next;
58268dd23aaSBarry Smith       }
58368dd23aaSBarry Smith     } else {
58468dd23aaSBarry Smith       for (i=0; i<nsplit; i++) {
5850298fd71SBarry Smith         ierr  = MatGetSubMatrix(pc->mat,ilink->is,NULL,MAT_REUSE_MATRIX,&jac->Afield[i]);CHKERRQ(ierr);
58668dd23aaSBarry Smith         ilink = ilink->next;
58768dd23aaSBarry Smith       }
58868dd23aaSBarry Smith     }
58968dd23aaSBarry Smith   }
59068dd23aaSBarry Smith 
5913b224e63SBarry Smith   if (jac->type == PC_COMPOSITE_SCHUR) {
5923b224e63SBarry Smith     IS          ccis;
5934aa3045dSJed Brown     PetscInt    rstart,rend;
594093c86ffSJed Brown     char        lscname[256];
595093c86ffSJed Brown     PetscObject LSC_L;
596ce94432eSBarry Smith 
597ce94432eSBarry Smith     if (nsplit != 2) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_INCOMP,"To use Schur complement preconditioner you must have exactly 2 fields");
59868dd23aaSBarry Smith 
599e6cab6aaSJed Brown     /* When extracting off-diagonal submatrices, we take complements from this range */
600e6cab6aaSJed Brown     ierr = MatGetOwnershipRangeColumn(pc->mat,&rstart,&rend);CHKERRQ(ierr);
601e6cab6aaSJed Brown 
6023b224e63SBarry Smith     /* need to handle case when one is resetting up the preconditioner */
6033b224e63SBarry Smith     if (jac->schur) {
6040298fd71SBarry Smith       KSP kspA = jac->head->ksp, kspInner = NULL, kspUpper = jac->kspupper;
605443836d0SMatthew G Knepley 
606fb3147dbSMatthew G Knepley       ierr  = MatSchurComplementGetKSP(jac->schur, &kspInner);CHKERRQ(ierr);
6073b224e63SBarry Smith       ilink = jac->head;
60849bb4cd7SJungho Lee       ierr  = ISComplement(ilink->is_col,rstart,rend,&ccis);CHKERRQ(ierr);
6094aa3045dSJed Brown       ierr  = MatGetSubMatrix(pc->mat,ilink->is,ccis,MAT_REUSE_MATRIX,&jac->B);CHKERRQ(ierr);
610fcfd50ebSBarry Smith       ierr  = ISDestroy(&ccis);CHKERRQ(ierr);
6113b224e63SBarry Smith       ilink = ilink->next;
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->C);CHKERRQ(ierr);
614fcfd50ebSBarry Smith       ierr  = ISDestroy(&ccis);CHKERRQ(ierr);
615a3df900dSMatthew G Knepley       ierr  = MatSchurComplementUpdate(jac->schur,jac->mat[0],jac->pmat[0],jac->B,jac->C,jac->mat[1],pc->flag);CHKERRQ(ierr);
616443836d0SMatthew G Knepley       if (kspA != kspInner) {
617443836d0SMatthew G Knepley         ierr = KSPSetOperators(kspA,jac->mat[0],jac->pmat[0],pc->flag);CHKERRQ(ierr);
618443836d0SMatthew G Knepley       }
619443836d0SMatthew G Knepley       if (kspUpper != kspA) {
620443836d0SMatthew G Knepley         ierr = KSPSetOperators(kspUpper,jac->mat[0],jac->pmat[0],pc->flag);CHKERRQ(ierr);
621443836d0SMatthew G Knepley       }
622084e4875SJed Brown       ierr = KSPSetOperators(jac->kspschur,jac->schur,FieldSplitSchurPre(jac),pc->flag);CHKERRQ(ierr);
6233b224e63SBarry Smith     } else {
6241cee3971SBarry Smith       KSP          ksp;
625bafc1b83SMatthew G Knepley       const char   *Dprefix;
626bafc1b83SMatthew G Knepley       char         schurprefix[256];
627514bf10dSMatthew G Knepley       char         schurtestoption[256];
628bdddcaaaSMatthew G Knepley       MatNullSpace sp;
629514bf10dSMatthew G Knepley       PetscBool    flg;
6303b224e63SBarry Smith 
631a04f6461SBarry Smith       /* extract the A01 and A10 matrices */
6323b224e63SBarry Smith       ilink = jac->head;
63349bb4cd7SJungho Lee       ierr  = ISComplement(ilink->is_col,rstart,rend,&ccis);CHKERRQ(ierr);
6344aa3045dSJed Brown       ierr  = MatGetSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->B);CHKERRQ(ierr);
635fcfd50ebSBarry Smith       ierr  = ISDestroy(&ccis);CHKERRQ(ierr);
6363b224e63SBarry Smith       ilink = ilink->next;
63749bb4cd7SJungho Lee       ierr  = ISComplement(ilink->is_col,rstart,rend,&ccis);CHKERRQ(ierr);
6384aa3045dSJed Brown       ierr  = MatGetSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->C);CHKERRQ(ierr);
639fcfd50ebSBarry Smith       ierr  = ISDestroy(&ccis);CHKERRQ(ierr);
64020252d06SBarry Smith 
64120252d06SBarry Smith       /* Use mat[0] (diagonal block of the real matrix) preconditioned by pmat[0] to define Schur complement */
64220252d06SBarry Smith       ierr = MatCreate(((PetscObject)jac->mat[0])->comm,&jac->schur);CHKERRQ(ierr);
64320252d06SBarry Smith       ierr = MatSetType(jac->schur,MATSCHURCOMPLEMENT);CHKERRQ(ierr);
64420252d06SBarry Smith       ierr = MatSchurComplementGetKSP(jac->schur, &ksp);CHKERRQ(ierr);
64520252d06SBarry Smith       ierr = PetscSNPrintf(schurprefix, sizeof(schurprefix), "%sfieldsplit_%s_inner_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname);CHKERRQ(ierr);
64620252d06SBarry Smith       /* Indent this deeper to emphasize the "inner" nature of this solver. */
64720252d06SBarry Smith       ierr = PetscObjectIncrementTabLevel((PetscObject)ksp, (PetscObject) pc, 2);CHKERRQ(ierr);
64820252d06SBarry Smith       ierr = KSPSetOptionsPrefix(ksp, schurprefix);CHKERRQ(ierr);
64920252d06SBarry Smith       ierr = MatSchurComplementSet(jac->schur,jac->mat[0],jac->pmat[0],jac->B,jac->C,jac->mat[1]);CHKERRQ(ierr);
65020252d06SBarry Smith 
651bdddcaaaSMatthew G Knepley       ierr = MatGetNullSpace(jac->pmat[1], &sp);CHKERRQ(ierr);
65220252d06SBarry Smith       if (sp) {
65320252d06SBarry Smith         ierr = MatSetNullSpace(jac->schur, sp);CHKERRQ(ierr);
65420252d06SBarry Smith       }
65520252d06SBarry Smith 
65620252d06SBarry Smith       ierr = PetscSNPrintf(schurtestoption, sizeof(schurtestoption), "-fieldsplit_%s_inner_", ilink->splitname);CHKERRQ(ierr);
6570298fd71SBarry Smith       ierr = PetscOptionsFindPairPrefix_Private(((PetscObject)pc)->prefix, schurtestoption, NULL, &flg);CHKERRQ(ierr);
658514bf10dSMatthew G Knepley       if (flg) {
659514bf10dSMatthew G Knepley         DM dmInner;
660514bf10dSMatthew G Knepley 
661514bf10dSMatthew G Knepley         /* Set DM for new solver */
662bafc1b83SMatthew G Knepley         ierr = KSPGetDM(jac->head->ksp, &dmInner);CHKERRQ(ierr);
663bafc1b83SMatthew G Knepley         ierr = KSPSetDM(ksp, dmInner);CHKERRQ(ierr);
6647287d2a3SDmitry Karpeev         ierr = KSPSetDMActive(ksp, PETSC_FALSE);CHKERRQ(ierr);
665443836d0SMatthew G Knepley         ierr = KSPSetOperators(jac->head->ksp,jac->mat[0],jac->pmat[0],flag);CHKERRQ(ierr);
666443836d0SMatthew G Knepley         ierr = KSPSetFromOptions(jac->head->ksp);CHKERRQ(ierr);
667514bf10dSMatthew G Knepley       } else {
668514bf10dSMatthew G Knepley         ierr = MatSchurComplementSetKSP(jac->schur, jac->head->ksp);CHKERRQ(ierr);
669514bf10dSMatthew G Knepley         ierr = PetscObjectReference((PetscObject) jac->head->ksp);CHKERRQ(ierr);
670bafc1b83SMatthew G Knepley       }
67120b26d62SBarry Smith       ierr = MatSetFromOptions(jac->schur);CHKERRQ(ierr);
6723b224e63SBarry Smith 
673443836d0SMatthew G Knepley       ierr = PetscSNPrintf(schurtestoption, sizeof(schurtestoption), "-fieldsplit_%s_upper_", ilink->splitname);CHKERRQ(ierr);
6740298fd71SBarry Smith       ierr = PetscOptionsFindPairPrefix_Private(((PetscObject)pc)->prefix, schurtestoption, NULL, &flg);CHKERRQ(ierr);
675443836d0SMatthew G Knepley       if (flg) {
676443836d0SMatthew G Knepley         DM dmInner;
677443836d0SMatthew G Knepley 
678443836d0SMatthew G Knepley         ierr = PetscSNPrintf(schurprefix, sizeof(schurprefix), "%sfieldsplit_%s_upper_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname);CHKERRQ(ierr);
67982f516ccSBarry Smith         ierr = KSPCreate(PetscObjectComm((PetscObject)pc), &jac->kspupper);CHKERRQ(ierr);
680443836d0SMatthew G Knepley         ierr = KSPSetOptionsPrefix(jac->kspupper, schurprefix);CHKERRQ(ierr);
681443836d0SMatthew G Knepley         ierr = KSPGetDM(jac->head->ksp, &dmInner);CHKERRQ(ierr);
682443836d0SMatthew G Knepley         ierr = KSPSetDM(jac->kspupper, dmInner);CHKERRQ(ierr);
683443836d0SMatthew G Knepley         ierr = KSPSetDMActive(jac->kspupper, PETSC_FALSE);CHKERRQ(ierr);
684443836d0SMatthew G Knepley         ierr = KSPSetFromOptions(jac->kspupper);CHKERRQ(ierr);
685443836d0SMatthew G Knepley         ierr = KSPSetOperators(jac->kspupper,jac->mat[0],jac->pmat[0],flag);CHKERRQ(ierr);
686443836d0SMatthew G Knepley         ierr = VecDuplicate(jac->head->x, &jac->head->z);CHKERRQ(ierr);
687443836d0SMatthew G Knepley       } else {
688443836d0SMatthew G Knepley         jac->kspupper = jac->head->ksp;
689443836d0SMatthew G Knepley         ierr          = PetscObjectReference((PetscObject) jac->head->ksp);CHKERRQ(ierr);
690443836d0SMatthew G Knepley       }
691443836d0SMatthew G Knepley 
692ce94432eSBarry Smith       ierr = KSPCreate(PetscObjectComm((PetscObject)pc),&jac->kspschur);CHKERRQ(ierr);
6939005cf84SBarry Smith       ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)jac->kspschur);CHKERRQ(ierr);
69420252d06SBarry Smith       ierr = PetscObjectIncrementTabLevel((PetscObject)jac->kspschur,(PetscObject)pc,1);CHKERRQ(ierr);
695084e4875SJed Brown       ierr = KSPSetOperators(jac->kspschur,jac->schur,FieldSplitSchurPre(jac),DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);
696084e4875SJed Brown       if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELF) {
6977233a360SDmitry Karpeev         PC pcschur;
6987233a360SDmitry Karpeev         ierr = KSPGetPC(jac->kspschur,&pcschur);CHKERRQ(ierr);
6997233a360SDmitry Karpeev         ierr = PCSetType(pcschur,PCNONE);CHKERRQ(ierr);
700084e4875SJed Brown         /* Note: This is bad if there exist preconditioners for MATSCHURCOMPLEMENT */
701e69d4d44SBarry Smith       }
7027287d2a3SDmitry Karpeev       ierr = KSPGetOptionsPrefix(jac->head->next->ksp, &Dprefix);CHKERRQ(ierr);
7037287d2a3SDmitry Karpeev       ierr = KSPSetOptionsPrefix(jac->kspschur,         Dprefix);CHKERRQ(ierr);
7043b224e63SBarry Smith       /* really want setfromoptions called in PCSetFromOptions_FieldSplit(), but it is not ready yet */
70520b26d62SBarry Smith       /* need to call this every time, since the jac->kspschur is freshly created, otherwise its options never get set */
70620b26d62SBarry Smith       ierr = KSPSetFromOptions(jac->kspschur);CHKERRQ(ierr);
7073b224e63SBarry Smith     }
708093c86ffSJed Brown 
709093c86ffSJed Brown     /* HACK: special support to forward L and Lp matrices that might be used by PCLSC */
7108caf3d72SBarry Smith     ierr = PetscSNPrintf(lscname,sizeof(lscname),"%s_LSC_L",ilink->splitname);CHKERRQ(ierr);
711093c86ffSJed Brown     ierr = PetscObjectQuery((PetscObject)pc->mat,lscname,(PetscObject*)&LSC_L);CHKERRQ(ierr);
712093c86ffSJed Brown     if (!LSC_L) {ierr = PetscObjectQuery((PetscObject)pc->pmat,lscname,(PetscObject*)&LSC_L);CHKERRQ(ierr);}
713093c86ffSJed Brown     if (LSC_L) {ierr = PetscObjectCompose((PetscObject)jac->schur,"LSC_L",(PetscObject)LSC_L);CHKERRQ(ierr);}
7148caf3d72SBarry Smith     ierr = PetscSNPrintf(lscname,sizeof(lscname),"%s_LSC_Lp",ilink->splitname);CHKERRQ(ierr);
715093c86ffSJed Brown     ierr = PetscObjectQuery((PetscObject)pc->pmat,lscname,(PetscObject*)&LSC_L);CHKERRQ(ierr);
716093c86ffSJed Brown     if (!LSC_L) {ierr = PetscObjectQuery((PetscObject)pc->mat,lscname,(PetscObject*)&LSC_L);CHKERRQ(ierr);}
717093c86ffSJed Brown     if (LSC_L) {ierr = PetscObjectCompose((PetscObject)jac->schur,"LSC_Lp",(PetscObject)LSC_L);CHKERRQ(ierr);}
7183b224e63SBarry Smith   } else {
71968bd789dSDmitry Karpeev     /* set up the individual splits' PCs */
72097bbdb24SBarry Smith     i     = 0;
7215a9f2f41SSatish Balay     ilink = jac->head;
7225a9f2f41SSatish Balay     while (ilink) {
723519d70e2SJed Brown       ierr = KSPSetOperators(ilink->ksp,jac->mat[i],jac->pmat[i],flag);CHKERRQ(ierr);
7243b224e63SBarry Smith       /* really want setfromoptions called in PCSetFromOptions_FieldSplit(), but it is not ready yet */
725c1570756SJed Brown       if (!jac->suboptionsset) {ierr = KSPSetFromOptions(ilink->ksp);CHKERRQ(ierr);}
72697bbdb24SBarry Smith       i++;
7275a9f2f41SSatish Balay       ilink = ilink->next;
7280971522cSBarry Smith     }
7293b224e63SBarry Smith   }
7303b224e63SBarry Smith 
731c1570756SJed Brown   jac->suboptionsset = PETSC_TRUE;
7320971522cSBarry Smith   PetscFunctionReturn(0);
7330971522cSBarry Smith }
7340971522cSBarry Smith 
7355a9f2f41SSatish Balay #define FieldSplitSplitSolveAdd(ilink,xx,yy) \
736ca9f406cSSatish Balay   (VecScatterBegin(ilink->sctx,xx,ilink->x,INSERT_VALUES,SCATTER_FORWARD) || \
737ca9f406cSSatish Balay    VecScatterEnd(ilink->sctx,xx,ilink->x,INSERT_VALUES,SCATTER_FORWARD) || \
7385a9f2f41SSatish Balay    KSPSolve(ilink->ksp,ilink->x,ilink->y) || \
739ca9f406cSSatish Balay    VecScatterBegin(ilink->sctx,ilink->y,yy,ADD_VALUES,SCATTER_REVERSE) || \
740ca9f406cSSatish Balay    VecScatterEnd(ilink->sctx,ilink->y,yy,ADD_VALUES,SCATTER_REVERSE))
74179416396SBarry Smith 
7420971522cSBarry Smith #undef __FUNCT__
7433b224e63SBarry Smith #define __FUNCT__ "PCApply_FieldSplit_Schur"
7443b224e63SBarry Smith static PetscErrorCode PCApply_FieldSplit_Schur(PC pc,Vec x,Vec y)
7453b224e63SBarry Smith {
7463b224e63SBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
7473b224e63SBarry Smith   PetscErrorCode    ierr;
7483b224e63SBarry Smith   PC_FieldSplitLink ilinkA = jac->head, ilinkD = ilinkA->next;
749443836d0SMatthew G Knepley   KSP               kspA   = ilinkA->ksp, kspLower = kspA, kspUpper = jac->kspupper;
7503b224e63SBarry Smith 
7513b224e63SBarry Smith   PetscFunctionBegin;
752c5d2311dSJed Brown   switch (jac->schurfactorization) {
753c9c6ffaaSJed Brown   case PC_FIELDSPLIT_SCHUR_FACT_DIAG:
754a04f6461SBarry Smith     /* [A00 0; 0 -S], positive definite, suitable for MINRES */
755c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
756c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
757c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
758443836d0SMatthew G Knepley     ierr = KSPSolve(kspA,ilinkA->x,ilinkA->y);CHKERRQ(ierr);
759c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
760c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
761c5d2311dSJed Brown     ierr = KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);CHKERRQ(ierr);
762c5d2311dSJed Brown     ierr = VecScale(ilinkD->y,-1.);CHKERRQ(ierr);
763c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
764c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
765c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
766c5d2311dSJed Brown     break;
767c9c6ffaaSJed Brown   case PC_FIELDSPLIT_SCHUR_FACT_LOWER:
768a04f6461SBarry Smith     /* [A00 0; A10 S], suitable for left preconditioning */
769c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
770c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
771443836d0SMatthew G Knepley     ierr = KSPSolve(kspA,ilinkA->x,ilinkA->y);CHKERRQ(ierr);
772c5d2311dSJed Brown     ierr = MatMult(jac->C,ilinkA->y,ilinkD->x);CHKERRQ(ierr);
773c5d2311dSJed Brown     ierr = VecScale(ilinkD->x,-1.);CHKERRQ(ierr);
774c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
775c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
776c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
777c5d2311dSJed Brown     ierr = KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);CHKERRQ(ierr);
778c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
779c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
780c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
781c5d2311dSJed Brown     break;
782c9c6ffaaSJed Brown   case PC_FIELDSPLIT_SCHUR_FACT_UPPER:
783a04f6461SBarry Smith     /* [A00 A01; 0 S], suitable for right preconditioning */
784c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
785c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
786c5d2311dSJed Brown     ierr = KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);CHKERRQ(ierr);
787c5d2311dSJed Brown     ierr = MatMult(jac->B,ilinkD->y,ilinkA->x);CHKERRQ(ierr);
788c5d2311dSJed Brown     ierr = VecScale(ilinkA->x,-1.);CHKERRQ(ierr);
789c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkA->sctx,x,ilinkA->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
790c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
791c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkA->sctx,x,ilinkA->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
792443836d0SMatthew G Knepley     ierr = KSPSolve(kspA,ilinkA->x,ilinkA->y);CHKERRQ(ierr);
793c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
794c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
795c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
796c5d2311dSJed Brown     break;
797c9c6ffaaSJed Brown   case PC_FIELDSPLIT_SCHUR_FACT_FULL:
798a04f6461SBarry 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 */
7993b224e63SBarry Smith     ierr = VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
8003b224e63SBarry Smith     ierr = VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
801443836d0SMatthew G Knepley     ierr = KSPSolve(kspLower,ilinkA->x,ilinkA->y);CHKERRQ(ierr);
8023b224e63SBarry Smith     ierr = MatMult(jac->C,ilinkA->y,ilinkD->x);CHKERRQ(ierr);
8033b224e63SBarry Smith     ierr = VecScale(ilinkD->x,-1.0);CHKERRQ(ierr);
8043b224e63SBarry Smith     ierr = VecScatterBegin(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
8053b224e63SBarry Smith     ierr = VecScatterEnd(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
8063b224e63SBarry Smith 
8073b224e63SBarry Smith     ierr = KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);CHKERRQ(ierr);
8083b224e63SBarry Smith     ierr = VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
8093b224e63SBarry Smith     ierr = VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
8103b224e63SBarry Smith 
811443836d0SMatthew G Knepley     if (kspUpper == kspA) {
8123b224e63SBarry Smith       ierr = MatMult(jac->B,ilinkD->y,ilinkA->y);CHKERRQ(ierr);
8133b224e63SBarry Smith       ierr = VecAXPY(ilinkA->x,-1.0,ilinkA->y);CHKERRQ(ierr);
814443836d0SMatthew G Knepley       ierr = KSPSolve(kspA,ilinkA->x,ilinkA->y);CHKERRQ(ierr);
815443836d0SMatthew G Knepley     } else {
816443836d0SMatthew G Knepley       ierr = KSPSolve(kspA,ilinkA->x,ilinkA->y);CHKERRQ(ierr);
817443836d0SMatthew G Knepley       ierr = MatMult(jac->B,ilinkD->y,ilinkA->x);CHKERRQ(ierr);
818443836d0SMatthew G Knepley       ierr = KSPSolve(kspUpper,ilinkA->x,ilinkA->z);CHKERRQ(ierr);
819443836d0SMatthew G Knepley       ierr = VecAXPY(ilinkA->y,-1.0,ilinkA->z);CHKERRQ(ierr);
820443836d0SMatthew G Knepley     }
8213b224e63SBarry Smith     ierr = VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
8223b224e63SBarry Smith     ierr = VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
823c5d2311dSJed Brown   }
8243b224e63SBarry Smith   PetscFunctionReturn(0);
8253b224e63SBarry Smith }
8263b224e63SBarry Smith 
8273b224e63SBarry Smith #undef __FUNCT__
8280971522cSBarry Smith #define __FUNCT__ "PCApply_FieldSplit"
8290971522cSBarry Smith static PetscErrorCode PCApply_FieldSplit(PC pc,Vec x,Vec y)
8300971522cSBarry Smith {
8310971522cSBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
8320971522cSBarry Smith   PetscErrorCode    ierr;
8335a9f2f41SSatish Balay   PC_FieldSplitLink ilink = jac->head;
834939b8a20SBarry Smith   PetscInt          cnt,bs;
8350971522cSBarry Smith 
8360971522cSBarry Smith   PetscFunctionBegin;
83751f519a2SBarry Smith   CHKMEMQ;
83879416396SBarry Smith   if (jac->type == PC_COMPOSITE_ADDITIVE) {
8391b9fc7fcSBarry Smith     if (jac->defaultsplit) {
840939b8a20SBarry Smith       ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
841ce94432eSBarry 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);
842939b8a20SBarry Smith       ierr = VecGetBlockSize(y,&bs);CHKERRQ(ierr);
843ce94432eSBarry 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);
8440971522cSBarry Smith       ierr = VecStrideGatherAll(x,jac->x,INSERT_VALUES);CHKERRQ(ierr);
8455a9f2f41SSatish Balay       while (ilink) {
8465a9f2f41SSatish Balay         ierr  = KSPSolve(ilink->ksp,ilink->x,ilink->y);CHKERRQ(ierr);
8475a9f2f41SSatish Balay         ilink = ilink->next;
8480971522cSBarry Smith       }
8490971522cSBarry Smith       ierr = VecStrideScatterAll(jac->y,y,INSERT_VALUES);CHKERRQ(ierr);
8501b9fc7fcSBarry Smith     } else {
851efb30889SBarry Smith       ierr = VecSet(y,0.0);CHKERRQ(ierr);
8525a9f2f41SSatish Balay       while (ilink) {
8535a9f2f41SSatish Balay         ierr  = FieldSplitSplitSolveAdd(ilink,x,y);CHKERRQ(ierr);
8545a9f2f41SSatish Balay         ilink = ilink->next;
8551b9fc7fcSBarry Smith       }
8561b9fc7fcSBarry Smith     }
85716913363SBarry Smith   } else if (jac->type == PC_COMPOSITE_MULTIPLICATIVE || jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
85879416396SBarry Smith     if (!jac->w1) {
85979416396SBarry Smith       ierr = VecDuplicate(x,&jac->w1);CHKERRQ(ierr);
86079416396SBarry Smith       ierr = VecDuplicate(x,&jac->w2);CHKERRQ(ierr);
86179416396SBarry Smith     }
862efb30889SBarry Smith     ierr = VecSet(y,0.0);CHKERRQ(ierr);
8635a9f2f41SSatish Balay     ierr = FieldSplitSplitSolveAdd(ilink,x,y);CHKERRQ(ierr);
8643e197d65SBarry Smith     cnt  = 1;
8655a9f2f41SSatish Balay     while (ilink->next) {
8665a9f2f41SSatish Balay       ilink = ilink->next;
8673e197d65SBarry Smith       /* compute the residual only over the part of the vector needed */
8683e197d65SBarry Smith       ierr = MatMult(jac->Afield[cnt++],y,ilink->x);CHKERRQ(ierr);
8693e197d65SBarry Smith       ierr = VecScale(ilink->x,-1.0);CHKERRQ(ierr);
8703e197d65SBarry Smith       ierr = VecScatterBegin(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
8713e197d65SBarry Smith       ierr = VecScatterEnd(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
8723e197d65SBarry Smith       ierr = KSPSolve(ilink->ksp,ilink->x,ilink->y);CHKERRQ(ierr);
8733e197d65SBarry Smith       ierr = VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
8743e197d65SBarry Smith       ierr = VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
8753e197d65SBarry Smith     }
87651f519a2SBarry Smith     if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
87711755939SBarry Smith       cnt -= 2;
87851f519a2SBarry Smith       while (ilink->previous) {
87951f519a2SBarry Smith         ilink = ilink->previous;
88011755939SBarry Smith         /* compute the residual only over the part of the vector needed */
88111755939SBarry Smith         ierr = MatMult(jac->Afield[cnt--],y,ilink->x);CHKERRQ(ierr);
88211755939SBarry Smith         ierr = VecScale(ilink->x,-1.0);CHKERRQ(ierr);
88311755939SBarry Smith         ierr = VecScatterBegin(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
88411755939SBarry Smith         ierr = VecScatterEnd(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
88511755939SBarry Smith         ierr = KSPSolve(ilink->ksp,ilink->x,ilink->y);CHKERRQ(ierr);
88611755939SBarry Smith         ierr = VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
88711755939SBarry Smith         ierr = VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
88851f519a2SBarry Smith       }
88911755939SBarry Smith     }
890ce94432eSBarry Smith   } else SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Unsupported or unknown composition",(int) jac->type);
89151f519a2SBarry Smith   CHKMEMQ;
8920971522cSBarry Smith   PetscFunctionReturn(0);
8930971522cSBarry Smith }
8940971522cSBarry Smith 
895421e10b8SBarry Smith #define FieldSplitSplitSolveAddTranspose(ilink,xx,yy) \
896ca9f406cSSatish Balay   (VecScatterBegin(ilink->sctx,xx,ilink->y,INSERT_VALUES,SCATTER_FORWARD) || \
897ca9f406cSSatish Balay    VecScatterEnd(ilink->sctx,xx,ilink->y,INSERT_VALUES,SCATTER_FORWARD) || \
898421e10b8SBarry Smith    KSPSolveTranspose(ilink->ksp,ilink->y,ilink->x) || \
899ca9f406cSSatish Balay    VecScatterBegin(ilink->sctx,ilink->x,yy,ADD_VALUES,SCATTER_REVERSE) || \
900ca9f406cSSatish Balay    VecScatterEnd(ilink->sctx,ilink->x,yy,ADD_VALUES,SCATTER_REVERSE))
901421e10b8SBarry Smith 
902421e10b8SBarry Smith #undef __FUNCT__
9038c03b21aSDmitry Karpeev #define __FUNCT__ "PCApplyTranspose_FieldSplit"
904421e10b8SBarry Smith static PetscErrorCode PCApplyTranspose_FieldSplit(PC pc,Vec x,Vec y)
905421e10b8SBarry Smith {
906421e10b8SBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
907421e10b8SBarry Smith   PetscErrorCode    ierr;
908421e10b8SBarry Smith   PC_FieldSplitLink ilink = jac->head;
909939b8a20SBarry Smith   PetscInt          bs;
910421e10b8SBarry Smith 
911421e10b8SBarry Smith   PetscFunctionBegin;
912421e10b8SBarry Smith   CHKMEMQ;
913421e10b8SBarry Smith   if (jac->type == PC_COMPOSITE_ADDITIVE) {
914421e10b8SBarry Smith     if (jac->defaultsplit) {
915939b8a20SBarry Smith       ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
916ce94432eSBarry 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);
917939b8a20SBarry Smith       ierr = VecGetBlockSize(y,&bs);CHKERRQ(ierr);
918ce94432eSBarry 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);
919421e10b8SBarry Smith       ierr = VecStrideGatherAll(x,jac->x,INSERT_VALUES);CHKERRQ(ierr);
920421e10b8SBarry Smith       while (ilink) {
921421e10b8SBarry Smith         ierr  = KSPSolveTranspose(ilink->ksp,ilink->x,ilink->y);CHKERRQ(ierr);
922421e10b8SBarry Smith         ilink = ilink->next;
923421e10b8SBarry Smith       }
924421e10b8SBarry Smith       ierr = VecStrideScatterAll(jac->y,y,INSERT_VALUES);CHKERRQ(ierr);
925421e10b8SBarry Smith     } else {
926421e10b8SBarry Smith       ierr = VecSet(y,0.0);CHKERRQ(ierr);
927421e10b8SBarry Smith       while (ilink) {
928421e10b8SBarry Smith         ierr  = FieldSplitSplitSolveAddTranspose(ilink,x,y);CHKERRQ(ierr);
929421e10b8SBarry Smith         ilink = ilink->next;
930421e10b8SBarry Smith       }
931421e10b8SBarry Smith     }
932421e10b8SBarry Smith   } else {
933421e10b8SBarry Smith     if (!jac->w1) {
934421e10b8SBarry Smith       ierr = VecDuplicate(x,&jac->w1);CHKERRQ(ierr);
935421e10b8SBarry Smith       ierr = VecDuplicate(x,&jac->w2);CHKERRQ(ierr);
936421e10b8SBarry Smith     }
937421e10b8SBarry Smith     ierr = VecSet(y,0.0);CHKERRQ(ierr);
938421e10b8SBarry Smith     if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
939421e10b8SBarry Smith       ierr = FieldSplitSplitSolveAddTranspose(ilink,x,y);CHKERRQ(ierr);
940421e10b8SBarry Smith       while (ilink->next) {
941421e10b8SBarry Smith         ilink = ilink->next;
9429989ab13SBarry Smith         ierr  = MatMultTranspose(pc->mat,y,jac->w1);CHKERRQ(ierr);
943421e10b8SBarry Smith         ierr  = VecWAXPY(jac->w2,-1.0,jac->w1,x);CHKERRQ(ierr);
944421e10b8SBarry Smith         ierr  = FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);CHKERRQ(ierr);
945421e10b8SBarry Smith       }
946421e10b8SBarry Smith       while (ilink->previous) {
947421e10b8SBarry Smith         ilink = ilink->previous;
9489989ab13SBarry Smith         ierr  = MatMultTranspose(pc->mat,y,jac->w1);CHKERRQ(ierr);
949421e10b8SBarry Smith         ierr  = VecWAXPY(jac->w2,-1.0,jac->w1,x);CHKERRQ(ierr);
950421e10b8SBarry Smith         ierr  = FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);CHKERRQ(ierr);
951421e10b8SBarry Smith       }
952421e10b8SBarry Smith     } else {
953421e10b8SBarry Smith       while (ilink->next) {   /* get to last entry in linked list */
954421e10b8SBarry Smith         ilink = ilink->next;
955421e10b8SBarry Smith       }
956421e10b8SBarry Smith       ierr = FieldSplitSplitSolveAddTranspose(ilink,x,y);CHKERRQ(ierr);
957421e10b8SBarry Smith       while (ilink->previous) {
958421e10b8SBarry Smith         ilink = ilink->previous;
9599989ab13SBarry Smith         ierr  = MatMultTranspose(pc->mat,y,jac->w1);CHKERRQ(ierr);
960421e10b8SBarry Smith         ierr  = VecWAXPY(jac->w2,-1.0,jac->w1,x);CHKERRQ(ierr);
961421e10b8SBarry Smith         ierr  = FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);CHKERRQ(ierr);
962421e10b8SBarry Smith       }
963421e10b8SBarry Smith     }
964421e10b8SBarry Smith   }
965421e10b8SBarry Smith   CHKMEMQ;
966421e10b8SBarry Smith   PetscFunctionReturn(0);
967421e10b8SBarry Smith }
968421e10b8SBarry Smith 
9690971522cSBarry Smith #undef __FUNCT__
970574deadeSBarry Smith #define __FUNCT__ "PCReset_FieldSplit"
971574deadeSBarry Smith static PetscErrorCode PCReset_FieldSplit(PC pc)
9720971522cSBarry Smith {
9730971522cSBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
9740971522cSBarry Smith   PetscErrorCode    ierr;
9755a9f2f41SSatish Balay   PC_FieldSplitLink ilink = jac->head,next;
9760971522cSBarry Smith 
9770971522cSBarry Smith   PetscFunctionBegin;
9785a9f2f41SSatish Balay   while (ilink) {
979574deadeSBarry Smith     ierr  = KSPReset(ilink->ksp);CHKERRQ(ierr);
980fcfd50ebSBarry Smith     ierr  = VecDestroy(&ilink->x);CHKERRQ(ierr);
981fcfd50ebSBarry Smith     ierr  = VecDestroy(&ilink->y);CHKERRQ(ierr);
982443836d0SMatthew G Knepley     ierr  = VecDestroy(&ilink->z);CHKERRQ(ierr);
983fcfd50ebSBarry Smith     ierr  = VecScatterDestroy(&ilink->sctx);CHKERRQ(ierr);
984fcfd50ebSBarry Smith     ierr  = ISDestroy(&ilink->is);CHKERRQ(ierr);
985b5787286SJed Brown     ierr  = ISDestroy(&ilink->is_col);CHKERRQ(ierr);
9865a9f2f41SSatish Balay     next  = ilink->next;
9875a9f2f41SSatish Balay     ilink = next;
9880971522cSBarry Smith   }
98905b42c5fSBarry Smith   ierr = PetscFree2(jac->x,jac->y);CHKERRQ(ierr);
990574deadeSBarry Smith   if (jac->mat && jac->mat != jac->pmat) {
991574deadeSBarry Smith     ierr = MatDestroyMatrices(jac->nsplits,&jac->mat);CHKERRQ(ierr);
992574deadeSBarry Smith   } else if (jac->mat) {
9930298fd71SBarry Smith     jac->mat = NULL;
994574deadeSBarry Smith   }
99597bbdb24SBarry Smith   if (jac->pmat) {ierr = MatDestroyMatrices(jac->nsplits,&jac->pmat);CHKERRQ(ierr);}
99668dd23aaSBarry Smith   if (jac->Afield) {ierr = MatDestroyMatrices(jac->nsplits,&jac->Afield);CHKERRQ(ierr);}
9976bf464f9SBarry Smith   ierr       = VecDestroy(&jac->w1);CHKERRQ(ierr);
9986bf464f9SBarry Smith   ierr       = VecDestroy(&jac->w2);CHKERRQ(ierr);
9996bf464f9SBarry Smith   ierr       = MatDestroy(&jac->schur);CHKERRQ(ierr);
10006bf464f9SBarry Smith   ierr       = MatDestroy(&jac->schur_user);CHKERRQ(ierr);
10016bf464f9SBarry Smith   ierr       = KSPDestroy(&jac->kspschur);CHKERRQ(ierr);
1002d78dad28SBarry Smith   ierr       = KSPDestroy(&jac->kspupper);CHKERRQ(ierr);
10036bf464f9SBarry Smith   ierr       = MatDestroy(&jac->B);CHKERRQ(ierr);
10046bf464f9SBarry Smith   ierr       = MatDestroy(&jac->C);CHKERRQ(ierr);
100563ec74ffSBarry Smith   jac->reset = PETSC_TRUE;
1006574deadeSBarry Smith   PetscFunctionReturn(0);
1007574deadeSBarry Smith }
1008574deadeSBarry Smith 
1009574deadeSBarry Smith #undef __FUNCT__
1010574deadeSBarry Smith #define __FUNCT__ "PCDestroy_FieldSplit"
1011574deadeSBarry Smith static PetscErrorCode PCDestroy_FieldSplit(PC pc)
1012574deadeSBarry Smith {
1013574deadeSBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1014574deadeSBarry Smith   PetscErrorCode    ierr;
1015574deadeSBarry Smith   PC_FieldSplitLink ilink = jac->head,next;
1016574deadeSBarry Smith 
1017574deadeSBarry Smith   PetscFunctionBegin;
1018574deadeSBarry Smith   ierr = PCReset_FieldSplit(pc);CHKERRQ(ierr);
1019574deadeSBarry Smith   while (ilink) {
10206bf464f9SBarry Smith     ierr  = KSPDestroy(&ilink->ksp);CHKERRQ(ierr);
1021574deadeSBarry Smith     next  = ilink->next;
1022574deadeSBarry Smith     ierr  = PetscFree(ilink->splitname);CHKERRQ(ierr);
1023574deadeSBarry Smith     ierr  = PetscFree(ilink->fields);CHKERRQ(ierr);
10245d4c12cdSJungho Lee     ierr  = PetscFree(ilink->fields_col);CHKERRQ(ierr);
1025574deadeSBarry Smith     ierr  = PetscFree(ilink);CHKERRQ(ierr);
1026574deadeSBarry Smith     ilink = next;
1027574deadeSBarry Smith   }
1028574deadeSBarry Smith   ierr = PetscFree2(jac->x,jac->y);CHKERRQ(ierr);
1029c31cb41cSBarry Smith   ierr = PetscFree(pc->data);CHKERRQ(ierr);
103000de8ff0SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C","",NULL);CHKERRQ(ierr);
103100de8ff0SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetFields_C","",NULL);CHKERRQ(ierr);
103200de8ff0SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetIS_C","",NULL);CHKERRQ(ierr);
103300de8ff0SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetType_C","",NULL);CHKERRQ(ierr);
103400de8ff0SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetBlockSize_C","",NULL);CHKERRQ(ierr);
103500de8ff0SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSchurPrecondition_C","",NULL);CHKERRQ(ierr);
103600de8ff0SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C","",NULL);CHKERRQ(ierr);
10370971522cSBarry Smith   PetscFunctionReturn(0);
10380971522cSBarry Smith }
10390971522cSBarry Smith 
10400971522cSBarry Smith #undef __FUNCT__
10410971522cSBarry Smith #define __FUNCT__ "PCSetFromOptions_FieldSplit"
10420971522cSBarry Smith static PetscErrorCode PCSetFromOptions_FieldSplit(PC pc)
10430971522cSBarry Smith {
10441b9fc7fcSBarry Smith   PetscErrorCode  ierr;
10456c924f48SJed Brown   PetscInt        bs;
1046bc59fbc5SBarry Smith   PetscBool       flg,stokes = PETSC_FALSE;
10479dcbbd2bSBarry Smith   PC_FieldSplit   *jac = (PC_FieldSplit*)pc->data;
10483b224e63SBarry Smith   PCCompositeType ctype;
10491b9fc7fcSBarry Smith 
10500971522cSBarry Smith   PetscFunctionBegin;
10511b9fc7fcSBarry Smith   ierr = PetscOptionsHead("FieldSplit options");CHKERRQ(ierr);
10520298fd71SBarry Smith   ierr = PetscOptionsBool("-pc_fieldsplit_real_diagonal","Use diagonal blocks of the operator","PCFieldSplitSetRealDiagonal",jac->realdiagonal,&jac->realdiagonal,NULL);CHKERRQ(ierr);
10534ab8060aSDmitry Karpeev   ierr = PetscOptionsBool("-pc_fieldsplit_dm_splits","Whether to use DMCreateFieldDecomposition() for splits","PCFieldSplitSetDMSplits",jac->dm_splits,&jac->dm_splits,NULL);CHKERRQ(ierr);
105451f519a2SBarry Smith   ierr = PetscOptionsInt("-pc_fieldsplit_block_size","Blocksize that defines number of fields","PCFieldSplitSetBlockSize",jac->bs,&bs,&flg);CHKERRQ(ierr);
105551f519a2SBarry Smith   if (flg) {
105651f519a2SBarry Smith     ierr = PCFieldSplitSetBlockSize(pc,bs);CHKERRQ(ierr);
105751f519a2SBarry Smith   }
1058704ba839SBarry Smith 
10590298fd71SBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)pc)->prefix,"-pc_fieldsplit_detect_saddle_point",&stokes,NULL);CHKERRQ(ierr);
1060c0adfefeSBarry Smith   if (stokes) {
1061c0adfefeSBarry Smith     ierr          = PCFieldSplitSetType(pc,PC_COMPOSITE_SCHUR);CHKERRQ(ierr);
1062c0adfefeSBarry Smith     jac->schurpre = PC_FIELDSPLIT_SCHUR_PRE_SELF;
1063c0adfefeSBarry Smith   }
1064c0adfefeSBarry Smith 
10653b224e63SBarry Smith   ierr = PetscOptionsEnum("-pc_fieldsplit_type","Type of composition","PCFieldSplitSetType",PCCompositeTypes,(PetscEnum)jac->type,(PetscEnum*)&ctype,&flg);CHKERRQ(ierr);
10663b224e63SBarry Smith   if (flg) {
10673b224e63SBarry Smith     ierr = PCFieldSplitSetType(pc,ctype);CHKERRQ(ierr);
10683b224e63SBarry Smith   }
1069c30613efSMatthew Knepley   /* Only setup fields once */
1070c30613efSMatthew Knepley   if ((jac->bs > 0) && (jac->nsplits == 0)) {
1071d32f9abdSBarry Smith     /* only allow user to set fields from command line if bs is already known.
1072d32f9abdSBarry Smith        otherwise user can set them in PCFieldSplitSetDefaults() */
10736c924f48SJed Brown     ierr = PCFieldSplitSetRuntimeSplits_Private(pc);CHKERRQ(ierr);
10746c924f48SJed Brown     if (jac->splitdefined) {ierr = PetscInfo(pc,"Splits defined using the options database\n");CHKERRQ(ierr);}
1075d32f9abdSBarry Smith   }
1076c5d2311dSJed Brown   if (jac->type == PC_COMPOSITE_SCHUR) {
1077c9c6ffaaSJed Brown     ierr = PetscOptionsGetEnum(((PetscObject)pc)->prefix,"-pc_fieldsplit_schur_factorization_type",PCFieldSplitSchurFactTypes,(PetscEnum*)&jac->schurfactorization,&flg);CHKERRQ(ierr);
1078c9c6ffaaSJed Brown     if (flg) {ierr = PetscInfo(pc,"Deprecated use of -pc_fieldsplit_schur_factorization_type\n");CHKERRQ(ierr);}
10790298fd71SBarry 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);
10800298fd71SBarry 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);
1081c5d2311dSJed Brown   }
10821b9fc7fcSBarry Smith   ierr = PetscOptionsTail();CHKERRQ(ierr);
10830971522cSBarry Smith   PetscFunctionReturn(0);
10840971522cSBarry Smith }
10850971522cSBarry Smith 
10860971522cSBarry Smith /*------------------------------------------------------------------------------------*/
10870971522cSBarry Smith 
10880971522cSBarry Smith #undef __FUNCT__
10890971522cSBarry Smith #define __FUNCT__ "PCFieldSplitSetFields_FieldSplit"
10901e6b0712SBarry Smith static PetscErrorCode  PCFieldSplitSetFields_FieldSplit(PC pc,const char splitname[],PetscInt n,const PetscInt *fields,const PetscInt *fields_col)
10910971522cSBarry Smith {
109297bbdb24SBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
10930971522cSBarry Smith   PetscErrorCode    ierr;
10945a9f2f41SSatish Balay   PC_FieldSplitLink ilink,next = jac->head;
109569a612a9SBarry Smith   char              prefix[128];
10965d4c12cdSJungho Lee   PetscInt          i;
10970971522cSBarry Smith 
10980971522cSBarry Smith   PetscFunctionBegin;
10996c924f48SJed Brown   if (jac->splitdefined) {
11006c924f48SJed Brown     ierr = PetscInfo1(pc,"Ignoring new split \"%s\" because the splits have already been defined\n",splitname);CHKERRQ(ierr);
11016c924f48SJed Brown     PetscFunctionReturn(0);
11026c924f48SJed Brown   }
110351f519a2SBarry Smith   for (i=0; i<n; i++) {
1104e32f2f54SBarry 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);
1105e32f2f54SBarry Smith     if (fields[i] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative field %D requested",fields[i]);
110651f519a2SBarry Smith   }
1107704ba839SBarry Smith   ierr = PetscNew(struct _PC_FieldSplitLink,&ilink);CHKERRQ(ierr);
1108a04f6461SBarry Smith   if (splitname) {
1109db4c96c1SJed Brown     ierr = PetscStrallocpy(splitname,&ilink->splitname);CHKERRQ(ierr);
1110a04f6461SBarry Smith   } else {
1111a04f6461SBarry Smith     ierr = PetscMalloc(3*sizeof(char),&ilink->splitname);CHKERRQ(ierr);
1112a04f6461SBarry Smith     ierr = PetscSNPrintf(ilink->splitname,2,"%s",jac->nsplits);CHKERRQ(ierr);
1113a04f6461SBarry Smith   }
1114704ba839SBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&ilink->fields);CHKERRQ(ierr);
11155a9f2f41SSatish Balay   ierr = PetscMemcpy(ilink->fields,fields,n*sizeof(PetscInt));CHKERRQ(ierr);
11165d4c12cdSJungho Lee   ierr = PetscMalloc(n*sizeof(PetscInt),&ilink->fields_col);CHKERRQ(ierr);
11175d4c12cdSJungho Lee   ierr = PetscMemcpy(ilink->fields_col,fields_col,n*sizeof(PetscInt));CHKERRQ(ierr);
11182fa5cd67SKarl Rupp 
11195a9f2f41SSatish Balay   ilink->nfields = n;
11200298fd71SBarry Smith   ilink->next    = NULL;
1121ce94432eSBarry Smith   ierr           = KSPCreate(PetscObjectComm((PetscObject)pc),&ilink->ksp);CHKERRQ(ierr);
112220252d06SBarry Smith   ierr           = PetscObjectIncrementTabLevel((PetscObject)ilink->ksp,(PetscObject)pc,1);CHKERRQ(ierr);
11235a9f2f41SSatish Balay   ierr           = KSPSetType(ilink->ksp,KSPPREONLY);CHKERRQ(ierr);
11249005cf84SBarry Smith   ierr           = PetscLogObjectParent((PetscObject)pc,(PetscObject)ilink->ksp);CHKERRQ(ierr);
112569a612a9SBarry Smith 
11268caf3d72SBarry Smith   ierr = PetscSNPrintf(prefix,sizeof(prefix),"%sfieldsplit_%s_",((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "",ilink->splitname);CHKERRQ(ierr);
11275a9f2f41SSatish Balay   ierr = KSPSetOptionsPrefix(ilink->ksp,prefix);CHKERRQ(ierr);
11280971522cSBarry Smith 
11290971522cSBarry Smith   if (!next) {
11305a9f2f41SSatish Balay     jac->head       = ilink;
11310298fd71SBarry Smith     ilink->previous = NULL;
11320971522cSBarry Smith   } else {
11330971522cSBarry Smith     while (next->next) {
11340971522cSBarry Smith       next = next->next;
11350971522cSBarry Smith     }
11365a9f2f41SSatish Balay     next->next      = ilink;
113751f519a2SBarry Smith     ilink->previous = next;
11380971522cSBarry Smith   }
11390971522cSBarry Smith   jac->nsplits++;
11400971522cSBarry Smith   PetscFunctionReturn(0);
11410971522cSBarry Smith }
11420971522cSBarry Smith 
1143e69d4d44SBarry Smith #undef __FUNCT__
1144e69d4d44SBarry Smith #define __FUNCT__ "PCFieldSplitGetSubKSP_FieldSplit_Schur"
11451e6b0712SBarry Smith static PetscErrorCode  PCFieldSplitGetSubKSP_FieldSplit_Schur(PC pc,PetscInt *n,KSP **subksp)
1146e69d4d44SBarry Smith {
1147e69d4d44SBarry Smith   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
1148e69d4d44SBarry Smith   PetscErrorCode ierr;
1149e69d4d44SBarry Smith 
1150e69d4d44SBarry Smith   PetscFunctionBegin;
1151519d70e2SJed Brown   ierr = PetscMalloc(jac->nsplits*sizeof(KSP),subksp);CHKERRQ(ierr);
1152e69d4d44SBarry Smith   ierr = MatSchurComplementGetKSP(jac->schur,*subksp);CHKERRQ(ierr);
11532fa5cd67SKarl Rupp 
1154e69d4d44SBarry Smith   (*subksp)[1] = jac->kspschur;
115513e0d083SBarry Smith   if (n) *n = jac->nsplits;
1156e69d4d44SBarry Smith   PetscFunctionReturn(0);
1157e69d4d44SBarry Smith }
11580971522cSBarry Smith 
11590971522cSBarry Smith #undef __FUNCT__
116069a612a9SBarry Smith #define __FUNCT__ "PCFieldSplitGetSubKSP_FieldSplit"
11611e6b0712SBarry Smith static PetscErrorCode  PCFieldSplitGetSubKSP_FieldSplit(PC pc,PetscInt *n,KSP **subksp)
11620971522cSBarry Smith {
11630971522cSBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
11640971522cSBarry Smith   PetscErrorCode    ierr;
11650971522cSBarry Smith   PetscInt          cnt   = 0;
11665a9f2f41SSatish Balay   PC_FieldSplitLink ilink = jac->head;
11670971522cSBarry Smith 
11680971522cSBarry Smith   PetscFunctionBegin;
11695d480477SMatthew G Knepley   ierr = PetscMalloc(jac->nsplits*sizeof(KSP),subksp);CHKERRQ(ierr);
11705a9f2f41SSatish Balay   while (ilink) {
11715a9f2f41SSatish Balay     (*subksp)[cnt++] = ilink->ksp;
11725a9f2f41SSatish Balay     ilink            = ilink->next;
11730971522cSBarry Smith   }
11745d480477SMatthew 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);
117513e0d083SBarry Smith   if (n) *n = jac->nsplits;
11760971522cSBarry Smith   PetscFunctionReturn(0);
11770971522cSBarry Smith }
11780971522cSBarry Smith 
1179704ba839SBarry Smith #undef __FUNCT__
1180704ba839SBarry Smith #define __FUNCT__ "PCFieldSplitSetIS_FieldSplit"
11811e6b0712SBarry Smith static PetscErrorCode  PCFieldSplitSetIS_FieldSplit(PC pc,const char splitname[],IS is)
1182704ba839SBarry Smith {
1183704ba839SBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1184704ba839SBarry Smith   PetscErrorCode    ierr;
1185704ba839SBarry Smith   PC_FieldSplitLink ilink, next = jac->head;
1186704ba839SBarry Smith   char              prefix[128];
1187704ba839SBarry Smith 
1188704ba839SBarry Smith   PetscFunctionBegin;
11896c924f48SJed Brown   if (jac->splitdefined) {
11906c924f48SJed Brown     ierr = PetscInfo1(pc,"Ignoring new split \"%s\" because the splits have already been defined\n",splitname);CHKERRQ(ierr);
11916c924f48SJed Brown     PetscFunctionReturn(0);
11926c924f48SJed Brown   }
119316913363SBarry Smith   ierr = PetscNew(struct _PC_FieldSplitLink,&ilink);CHKERRQ(ierr);
1194a04f6461SBarry Smith   if (splitname) {
1195db4c96c1SJed Brown     ierr = PetscStrallocpy(splitname,&ilink->splitname);CHKERRQ(ierr);
1196a04f6461SBarry Smith   } else {
1197b5787286SJed Brown     ierr = PetscMalloc(8*sizeof(char),&ilink->splitname);CHKERRQ(ierr);
1198b5787286SJed Brown     ierr = PetscSNPrintf(ilink->splitname,7,"%D",jac->nsplits);CHKERRQ(ierr);
1199a04f6461SBarry Smith   }
12001ab39975SBarry Smith   ierr          = PetscObjectReference((PetscObject)is);CHKERRQ(ierr);
1201b5787286SJed Brown   ierr          = ISDestroy(&ilink->is);CHKERRQ(ierr);
1202b5787286SJed Brown   ilink->is     = is;
1203b5787286SJed Brown   ierr          = PetscObjectReference((PetscObject)is);CHKERRQ(ierr);
1204b5787286SJed Brown   ierr          = ISDestroy(&ilink->is_col);CHKERRQ(ierr);
1205b5787286SJed Brown   ilink->is_col = is;
12060298fd71SBarry Smith   ilink->next   = NULL;
1207ce94432eSBarry Smith   ierr          = KSPCreate(PetscObjectComm((PetscObject)pc),&ilink->ksp);CHKERRQ(ierr);
120820252d06SBarry Smith   ierr          = PetscObjectIncrementTabLevel((PetscObject)ilink->ksp,(PetscObject)pc,1);CHKERRQ(ierr);
1209704ba839SBarry Smith   ierr          = KSPSetType(ilink->ksp,KSPPREONLY);CHKERRQ(ierr);
12109005cf84SBarry Smith   ierr          = PetscLogObjectParent((PetscObject)pc,(PetscObject)ilink->ksp);CHKERRQ(ierr);
1211704ba839SBarry Smith 
12128caf3d72SBarry Smith   ierr = PetscSNPrintf(prefix,sizeof(prefix),"%sfieldsplit_%s_",((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "",ilink->splitname);CHKERRQ(ierr);
1213704ba839SBarry Smith   ierr = KSPSetOptionsPrefix(ilink->ksp,prefix);CHKERRQ(ierr);
1214704ba839SBarry Smith 
1215704ba839SBarry Smith   if (!next) {
1216704ba839SBarry Smith     jac->head       = ilink;
12170298fd71SBarry Smith     ilink->previous = NULL;
1218704ba839SBarry Smith   } else {
1219704ba839SBarry Smith     while (next->next) {
1220704ba839SBarry Smith       next = next->next;
1221704ba839SBarry Smith     }
1222704ba839SBarry Smith     next->next      = ilink;
1223704ba839SBarry Smith     ilink->previous = next;
1224704ba839SBarry Smith   }
1225704ba839SBarry Smith   jac->nsplits++;
1226704ba839SBarry Smith   PetscFunctionReturn(0);
1227704ba839SBarry Smith }
1228704ba839SBarry Smith 
12290971522cSBarry Smith #undef __FUNCT__
12300971522cSBarry Smith #define __FUNCT__ "PCFieldSplitSetFields"
12310971522cSBarry Smith /*@
12320971522cSBarry Smith     PCFieldSplitSetFields - Sets the fields for one particular split in the field split preconditioner
12330971522cSBarry Smith 
1234ad4df100SBarry Smith     Logically Collective on PC
12350971522cSBarry Smith 
12360971522cSBarry Smith     Input Parameters:
12370971522cSBarry Smith +   pc  - the preconditioner context
12380298fd71SBarry Smith .   splitname - name of this split, if NULL the number of the split is used
12390971522cSBarry Smith .   n - the number of fields in this split
1240db4c96c1SJed Brown -   fields - the fields in this split
12410971522cSBarry Smith 
12420971522cSBarry Smith     Level: intermediate
12430971522cSBarry Smith 
1244d32f9abdSBarry Smith     Notes: Use PCFieldSplitSetIS() to set a completely general set of indices as a field.
1245d32f9abdSBarry Smith 
12467287d2a3SDmitry Karpeev      The PCFieldSplitSetFields() is for defining fields as strided blocks. For example, if the block
1247d32f9abdSBarry 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
1248d32f9abdSBarry Smith      0xx3xx6xx9xx12 ... x1xx4xx7xx ... xx2xx5xx8xx.. 01x34x67x... 0x1x3x5x7.. x12x45x78x....
1249d32f9abdSBarry Smith      where the numbered entries indicate what is in the field.
1250d32f9abdSBarry Smith 
1251db4c96c1SJed Brown      This function is called once per split (it creates a new split each time).  Solve options
1252db4c96c1SJed Brown      for this split will be available under the prefix -fieldsplit_SPLITNAME_.
1253db4c96c1SJed Brown 
12545d4c12cdSJungho Lee      Developer Note: This routine does not actually create the IS representing the split, that is delayed
12555d4c12cdSJungho Lee      until PCSetUp_FieldSplit(), because information about the vector/matrix layouts may not be
12565d4c12cdSJungho Lee      available when this routine is called.
12575d4c12cdSJungho Lee 
1258d32f9abdSBarry Smith .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetBlockSize(), PCFieldSplitSetIS()
12590971522cSBarry Smith 
12600971522cSBarry Smith @*/
12615d4c12cdSJungho Lee PetscErrorCode  PCFieldSplitSetFields(PC pc,const char splitname[],PetscInt n,const PetscInt *fields,const PetscInt *fields_col)
12620971522cSBarry Smith {
12634ac538c5SBarry Smith   PetscErrorCode ierr;
12640971522cSBarry Smith 
12650971522cSBarry Smith   PetscFunctionBegin;
12660700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1267db4c96c1SJed Brown   PetscValidCharPointer(splitname,2);
1268ce94432eSBarry Smith   if (n < 1) SETERRQ2(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Provided number of fields %D in split \"%s\" not positive",n,splitname);
1269db4c96c1SJed Brown   PetscValidIntPointer(fields,3);
12705d4c12cdSJungho Lee   ierr = PetscTryMethod(pc,"PCFieldSplitSetFields_C",(PC,const char[],PetscInt,const PetscInt*,const PetscInt*),(pc,splitname,n,fields,fields_col));CHKERRQ(ierr);
12710971522cSBarry Smith   PetscFunctionReturn(0);
12720971522cSBarry Smith }
12730971522cSBarry Smith 
12740971522cSBarry Smith #undef __FUNCT__
1275704ba839SBarry Smith #define __FUNCT__ "PCFieldSplitSetIS"
1276704ba839SBarry Smith /*@
1277704ba839SBarry Smith     PCFieldSplitSetIS - Sets the exact elements for field
1278704ba839SBarry Smith 
1279ad4df100SBarry Smith     Logically Collective on PC
1280704ba839SBarry Smith 
1281704ba839SBarry Smith     Input Parameters:
1282704ba839SBarry Smith +   pc  - the preconditioner context
12830298fd71SBarry Smith .   splitname - name of this split, if NULL the number of the split is used
1284db4c96c1SJed Brown -   is - the index set that defines the vector elements in this field
1285704ba839SBarry Smith 
1286d32f9abdSBarry Smith 
1287a6ffb8dbSJed Brown     Notes:
1288a6ffb8dbSJed Brown     Use PCFieldSplitSetFields(), for fields defined by strided types.
1289a6ffb8dbSJed Brown 
1290db4c96c1SJed Brown     This function is called once per split (it creates a new split each time).  Solve options
1291db4c96c1SJed Brown     for this split will be available under the prefix -fieldsplit_SPLITNAME_.
1292d32f9abdSBarry Smith 
1293704ba839SBarry Smith     Level: intermediate
1294704ba839SBarry Smith 
1295704ba839SBarry Smith .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetBlockSize()
1296704ba839SBarry Smith 
1297704ba839SBarry Smith @*/
12987087cfbeSBarry Smith PetscErrorCode  PCFieldSplitSetIS(PC pc,const char splitname[],IS is)
1299704ba839SBarry Smith {
13004ac538c5SBarry Smith   PetscErrorCode ierr;
1301704ba839SBarry Smith 
1302704ba839SBarry Smith   PetscFunctionBegin;
13030700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
13047b62db95SJungho Lee   if (splitname) PetscValidCharPointer(splitname,2);
1305db4c96c1SJed Brown   PetscValidHeaderSpecific(is,IS_CLASSID,3);
13064ac538c5SBarry Smith   ierr = PetscTryMethod(pc,"PCFieldSplitSetIS_C",(PC,const char[],IS),(pc,splitname,is));CHKERRQ(ierr);
1307704ba839SBarry Smith   PetscFunctionReturn(0);
1308704ba839SBarry Smith }
1309704ba839SBarry Smith 
1310704ba839SBarry Smith #undef __FUNCT__
131157a9adfeSMatthew G Knepley #define __FUNCT__ "PCFieldSplitGetIS"
131257a9adfeSMatthew G Knepley /*@
131357a9adfeSMatthew G Knepley     PCFieldSplitGetIS - Retrieves the elements for a field as an IS
131457a9adfeSMatthew G Knepley 
131557a9adfeSMatthew G Knepley     Logically Collective on PC
131657a9adfeSMatthew G Knepley 
131757a9adfeSMatthew G Knepley     Input Parameters:
131857a9adfeSMatthew G Knepley +   pc  - the preconditioner context
131957a9adfeSMatthew G Knepley -   splitname - name of this split
132057a9adfeSMatthew G Knepley 
132157a9adfeSMatthew G Knepley     Output Parameter:
13220298fd71SBarry Smith -   is - the index set that defines the vector elements in this field, or NULL if the field is not found
132357a9adfeSMatthew G Knepley 
132457a9adfeSMatthew G Knepley     Level: intermediate
132557a9adfeSMatthew G Knepley 
132657a9adfeSMatthew G Knepley .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetIS()
132757a9adfeSMatthew G Knepley 
132857a9adfeSMatthew G Knepley @*/
132957a9adfeSMatthew G Knepley PetscErrorCode PCFieldSplitGetIS(PC pc,const char splitname[],IS *is)
133057a9adfeSMatthew G Knepley {
133157a9adfeSMatthew G Knepley   PetscErrorCode ierr;
133257a9adfeSMatthew G Knepley 
133357a9adfeSMatthew G Knepley   PetscFunctionBegin;
133457a9adfeSMatthew G Knepley   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
133557a9adfeSMatthew G Knepley   PetscValidCharPointer(splitname,2);
133657a9adfeSMatthew G Knepley   PetscValidPointer(is,3);
133757a9adfeSMatthew G Knepley   {
133857a9adfeSMatthew G Knepley     PC_FieldSplit     *jac  = (PC_FieldSplit*) pc->data;
133957a9adfeSMatthew G Knepley     PC_FieldSplitLink ilink = jac->head;
134057a9adfeSMatthew G Knepley     PetscBool         found;
134157a9adfeSMatthew G Knepley 
13420298fd71SBarry Smith     *is = NULL;
134357a9adfeSMatthew G Knepley     while (ilink) {
134457a9adfeSMatthew G Knepley       ierr = PetscStrcmp(ilink->splitname, splitname, &found);CHKERRQ(ierr);
134557a9adfeSMatthew G Knepley       if (found) {
134657a9adfeSMatthew G Knepley         *is = ilink->is;
134757a9adfeSMatthew G Knepley         break;
134857a9adfeSMatthew G Knepley       }
134957a9adfeSMatthew G Knepley       ilink = ilink->next;
135057a9adfeSMatthew G Knepley     }
135157a9adfeSMatthew G Knepley   }
135257a9adfeSMatthew G Knepley   PetscFunctionReturn(0);
135357a9adfeSMatthew G Knepley }
135457a9adfeSMatthew G Knepley 
135557a9adfeSMatthew G Knepley #undef __FUNCT__
135651f519a2SBarry Smith #define __FUNCT__ "PCFieldSplitSetBlockSize"
135751f519a2SBarry Smith /*@
135851f519a2SBarry Smith     PCFieldSplitSetBlockSize - Sets the block size for defining where fields start in the
135951f519a2SBarry Smith       fieldsplit preconditioner. If not set the matrix block size is used.
136051f519a2SBarry Smith 
1361ad4df100SBarry Smith     Logically Collective on PC
136251f519a2SBarry Smith 
136351f519a2SBarry Smith     Input Parameters:
136451f519a2SBarry Smith +   pc  - the preconditioner context
136551f519a2SBarry Smith -   bs - the block size
136651f519a2SBarry Smith 
136751f519a2SBarry Smith     Level: intermediate
136851f519a2SBarry Smith 
136951f519a2SBarry Smith .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields()
137051f519a2SBarry Smith 
137151f519a2SBarry Smith @*/
13727087cfbeSBarry Smith PetscErrorCode  PCFieldSplitSetBlockSize(PC pc,PetscInt bs)
137351f519a2SBarry Smith {
13744ac538c5SBarry Smith   PetscErrorCode ierr;
137551f519a2SBarry Smith 
137651f519a2SBarry Smith   PetscFunctionBegin;
13770700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1378c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(pc,bs,2);
13794ac538c5SBarry Smith   ierr = PetscTryMethod(pc,"PCFieldSplitSetBlockSize_C",(PC,PetscInt),(pc,bs));CHKERRQ(ierr);
138051f519a2SBarry Smith   PetscFunctionReturn(0);
138151f519a2SBarry Smith }
138251f519a2SBarry Smith 
138351f519a2SBarry Smith #undef __FUNCT__
138469a612a9SBarry Smith #define __FUNCT__ "PCFieldSplitGetSubKSP"
13850971522cSBarry Smith /*@C
138669a612a9SBarry Smith    PCFieldSplitGetSubKSP - Gets the KSP contexts for all splits
13870971522cSBarry Smith 
138869a612a9SBarry Smith    Collective on KSP
13890971522cSBarry Smith 
13900971522cSBarry Smith    Input Parameter:
13910971522cSBarry Smith .  pc - the preconditioner context
13920971522cSBarry Smith 
13930971522cSBarry Smith    Output Parameters:
139413e0d083SBarry Smith +  n - the number of splits
139569a612a9SBarry Smith -  pc - the array of KSP contexts
13960971522cSBarry Smith 
13970971522cSBarry Smith    Note:
1398d32f9abdSBarry Smith    After PCFieldSplitGetSubKSP() the array of KSPs IS to be freed by the user
1399d32f9abdSBarry Smith    (not the KSP just the array that contains them).
14000971522cSBarry Smith 
140169a612a9SBarry Smith    You must call KSPSetUp() before calling PCFieldSplitGetSubKSP().
14020971522cSBarry Smith 
1403196cc216SBarry Smith    Fortran Usage: You must pass in a KSP array that is large enough to contain all the local KSPs.
14040298fd71SBarry Smith       You can call PCFieldSplitGetSubKSP(pc,n,NULL_OBJECT,ierr) to determine how large the
1405196cc216SBarry Smith       KSP array must be.
1406196cc216SBarry Smith 
1407196cc216SBarry Smith 
14080971522cSBarry Smith    Level: advanced
14090971522cSBarry Smith 
14100971522cSBarry Smith .seealso: PCFIELDSPLIT
14110971522cSBarry Smith @*/
14127087cfbeSBarry Smith PetscErrorCode  PCFieldSplitGetSubKSP(PC pc,PetscInt *n,KSP *subksp[])
14130971522cSBarry Smith {
14144ac538c5SBarry Smith   PetscErrorCode ierr;
14150971522cSBarry Smith 
14160971522cSBarry Smith   PetscFunctionBegin;
14170700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
141813e0d083SBarry Smith   if (n) PetscValidIntPointer(n,2);
14194ac538c5SBarry Smith   ierr = PetscUseMethod(pc,"PCFieldSplitGetSubKSP_C",(PC,PetscInt*,KSP **),(pc,n,subksp));CHKERRQ(ierr);
14200971522cSBarry Smith   PetscFunctionReturn(0);
14210971522cSBarry Smith }
14220971522cSBarry Smith 
1423e69d4d44SBarry Smith #undef __FUNCT__
1424e69d4d44SBarry Smith #define __FUNCT__ "PCFieldSplitSchurPrecondition"
1425e69d4d44SBarry Smith /*@
1426e69d4d44SBarry Smith     PCFieldSplitSchurPrecondition -  Indicates if the Schur complement is preconditioned by a preconditioner constructed by the
1427a04f6461SBarry Smith       A11 matrix. Otherwise no preconditioner is used.
1428e69d4d44SBarry Smith 
1429e69d4d44SBarry Smith     Collective on PC
1430e69d4d44SBarry Smith 
1431e69d4d44SBarry Smith     Input Parameters:
1432e69d4d44SBarry Smith +   pc  - the preconditioner context
1433e87fab1bSBarry Smith .   ptype - which matrix to use for preconditioning the Schur complement, PC_FIELDSPLIT_SCHUR_PRE_A11 (diag) is default
14340298fd71SBarry Smith -   userpre - matrix to use for preconditioning, or NULL
1435084e4875SJed Brown 
1436e69d4d44SBarry Smith     Options Database:
1437e87fab1bSBarry Smith .     -pc_fieldsplit_schur_precondition <self,user,a11> default is a11
1438e69d4d44SBarry Smith 
1439fd1303e9SJungho Lee     Notes:
1440fd1303e9SJungho Lee $    If ptype is
1441fd1303e9SJungho Lee $        user then the preconditioner for the Schur complement is generated by the provided matrix (pre argument
1442fd1303e9SJungho Lee $             to this function).
1443e87fab1bSBarry Smith $        a11 then the preconditioner for the Schur complement is generated by the block diagonal part of the original
1444fd1303e9SJungho Lee $             matrix associated with the Schur complement (i.e. A11)
1445fd1303e9SJungho Lee $        self the preconditioner for the Schur complement is generated from the Schur complement matrix itself:
1446fd1303e9SJungho Lee $             The only preconditioner that currently works directly with the Schur complement matrix object is the PCLSC
1447fd1303e9SJungho Lee $             preconditioner
1448fd1303e9SJungho Lee 
1449e87fab1bSBarry Smith      When solving a saddle point problem, where the A11 block is identically zero, using a11 as the ptype only makes sense
1450fd1303e9SJungho Lee     with the additional option -fieldsplit_1_pc_type none. Usually for saddle point problems one would use a ptype of self and
1451fd1303e9SJungho Lee     -fieldsplit_1_pc_type lsc which uses the least squares commutator compute a preconditioner for the Schur complement.
1452fd1303e9SJungho Lee 
1453fd1303e9SJungho Lee     Developer Notes: This is a terrible name, gives no good indication of what the function does and should also have Set in
1454fd1303e9SJungho Lee      the name since it sets a proceedure to use.
1455fd1303e9SJungho Lee 
1456e69d4d44SBarry Smith     Level: intermediate
1457e69d4d44SBarry Smith 
1458fd1303e9SJungho Lee .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields(), PCFieldSplitSchurPreType, PCLSC
1459e69d4d44SBarry Smith 
1460e69d4d44SBarry Smith @*/
14617087cfbeSBarry Smith PetscErrorCode  PCFieldSplitSchurPrecondition(PC pc,PCFieldSplitSchurPreType ptype,Mat pre)
1462e69d4d44SBarry Smith {
14634ac538c5SBarry Smith   PetscErrorCode ierr;
1464e69d4d44SBarry Smith 
1465e69d4d44SBarry Smith   PetscFunctionBegin;
14660700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
14674ac538c5SBarry Smith   ierr = PetscTryMethod(pc,"PCFieldSplitSchurPrecondition_C",(PC,PCFieldSplitSchurPreType,Mat),(pc,ptype,pre));CHKERRQ(ierr);
1468e69d4d44SBarry Smith   PetscFunctionReturn(0);
1469e69d4d44SBarry Smith }
1470e69d4d44SBarry Smith 
1471e69d4d44SBarry Smith #undef __FUNCT__
1472e69d4d44SBarry Smith #define __FUNCT__ "PCFieldSplitSchurPrecondition_FieldSplit"
14731e6b0712SBarry Smith static PetscErrorCode  PCFieldSplitSchurPrecondition_FieldSplit(PC pc,PCFieldSplitSchurPreType ptype,Mat pre)
1474e69d4d44SBarry Smith {
1475e69d4d44SBarry Smith   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
1476084e4875SJed Brown   PetscErrorCode ierr;
1477e69d4d44SBarry Smith 
1478e69d4d44SBarry Smith   PetscFunctionBegin;
1479084e4875SJed Brown   jac->schurpre = ptype;
1480084e4875SJed Brown   if (pre) {
14816bf464f9SBarry Smith     ierr            = MatDestroy(&jac->schur_user);CHKERRQ(ierr);
1482084e4875SJed Brown     jac->schur_user = pre;
1483084e4875SJed Brown     ierr            = PetscObjectReference((PetscObject)jac->schur_user);CHKERRQ(ierr);
1484084e4875SJed Brown   }
1485e69d4d44SBarry Smith   PetscFunctionReturn(0);
1486e69d4d44SBarry Smith }
1487e69d4d44SBarry Smith 
148830ad9308SMatthew Knepley #undef __FUNCT__
1489c9c6ffaaSJed Brown #define __FUNCT__ "PCFieldSplitSetSchurFactType"
1490ab1df9f5SJed Brown /*@
1491c9c6ffaaSJed Brown     PCFieldSplitSetSchurFactType -  sets which blocks of the approximate block factorization to retain
1492ab1df9f5SJed Brown 
1493ab1df9f5SJed Brown     Collective on PC
1494ab1df9f5SJed Brown 
1495ab1df9f5SJed Brown     Input Parameters:
1496ab1df9f5SJed Brown +   pc  - the preconditioner context
1497c9c6ffaaSJed Brown -   ftype - which blocks of factorization to retain, PC_FIELDSPLIT_SCHUR_FACT_FULL is default
1498ab1df9f5SJed Brown 
1499ab1df9f5SJed Brown     Options Database:
1500c9c6ffaaSJed Brown .     -pc_fieldsplit_schur_fact_type <diag,lower,upper,full> default is full
1501ab1df9f5SJed Brown 
1502ab1df9f5SJed Brown 
1503ab1df9f5SJed Brown     Level: intermediate
1504ab1df9f5SJed Brown 
1505ab1df9f5SJed Brown     Notes:
1506ab1df9f5SJed Brown     The FULL factorization is
1507ab1df9f5SJed Brown 
1508ab1df9f5SJed Brown $   (A   B)  = (1       0) (A   0) (1  Ainv*B)
1509ab1df9f5SJed Brown $   (C   D)    (C*Ainv  1) (0   S) (0     1  )
1510ab1df9f5SJed Brown 
15116be4592eSBarry 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,
15126be4592eSBarry 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).
1513ab1df9f5SJed Brown 
15146be4592eSBarry 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
15156be4592eSBarry 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
15166be4592eSBarry 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,
15176be4592eSBarry 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.
1518ab1df9f5SJed Brown 
15196be4592eSBarry 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
15206be4592eSBarry 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).
1521ab1df9f5SJed Brown 
1522ab1df9f5SJed Brown     References:
1523ab1df9f5SJed Brown     Murphy, Golub, and Wathen, A note on preconditioning indefinite linear systems, SIAM J. Sci. Comput., 21 (2000) pp. 1969-1972.
1524ab1df9f5SJed Brown 
1525ab1df9f5SJed Brown     Ipsen, A note on preconditioning nonsymmetric matrices, SIAM J. Sci. Comput., 23 (2001), pp. 1050-1051.
1526ab1df9f5SJed Brown 
1527ab1df9f5SJed Brown .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields(), PCFieldSplitSchurPreType
1528ab1df9f5SJed Brown @*/
1529c9c6ffaaSJed Brown PetscErrorCode  PCFieldSplitSetSchurFactType(PC pc,PCFieldSplitSchurFactType ftype)
1530ab1df9f5SJed Brown {
1531ab1df9f5SJed Brown   PetscErrorCode ierr;
1532ab1df9f5SJed Brown 
1533ab1df9f5SJed Brown   PetscFunctionBegin;
1534ab1df9f5SJed Brown   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1535c9c6ffaaSJed Brown   ierr = PetscTryMethod(pc,"PCFieldSplitSetSchurFactType_C",(PC,PCFieldSplitSchurFactType),(pc,ftype));CHKERRQ(ierr);
1536ab1df9f5SJed Brown   PetscFunctionReturn(0);
1537ab1df9f5SJed Brown }
1538ab1df9f5SJed Brown 
1539ab1df9f5SJed Brown #undef __FUNCT__
1540c9c6ffaaSJed Brown #define __FUNCT__ "PCFieldSplitSetSchurFactType_FieldSplit"
15411e6b0712SBarry Smith static PetscErrorCode PCFieldSplitSetSchurFactType_FieldSplit(PC pc,PCFieldSplitSchurFactType ftype)
1542ab1df9f5SJed Brown {
1543ab1df9f5SJed Brown   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
1544ab1df9f5SJed Brown 
1545ab1df9f5SJed Brown   PetscFunctionBegin;
1546ab1df9f5SJed Brown   jac->schurfactorization = ftype;
1547ab1df9f5SJed Brown   PetscFunctionReturn(0);
1548ab1df9f5SJed Brown }
1549ab1df9f5SJed Brown 
1550ab1df9f5SJed Brown #undef __FUNCT__
155130ad9308SMatthew Knepley #define __FUNCT__ "PCFieldSplitGetSchurBlocks"
155230ad9308SMatthew Knepley /*@C
15538c03b21aSDmitry Karpeev    PCFieldSplitGetSchurBlocks - Gets all matrix blocks for the Schur complement
155430ad9308SMatthew Knepley 
155530ad9308SMatthew Knepley    Collective on KSP
155630ad9308SMatthew Knepley 
155730ad9308SMatthew Knepley    Input Parameter:
155830ad9308SMatthew Knepley .  pc - the preconditioner context
155930ad9308SMatthew Knepley 
156030ad9308SMatthew Knepley    Output Parameters:
1561a04f6461SBarry Smith +  A00 - the (0,0) block
1562a04f6461SBarry Smith .  A01 - the (0,1) block
1563a04f6461SBarry Smith .  A10 - the (1,0) block
1564a04f6461SBarry Smith -  A11 - the (1,1) block
156530ad9308SMatthew Knepley 
156630ad9308SMatthew Knepley    Level: advanced
156730ad9308SMatthew Knepley 
156830ad9308SMatthew Knepley .seealso: PCFIELDSPLIT
156930ad9308SMatthew Knepley @*/
1570a04f6461SBarry Smith PetscErrorCode  PCFieldSplitGetSchurBlocks(PC pc,Mat *A00,Mat *A01,Mat *A10, Mat *A11)
157130ad9308SMatthew Knepley {
157230ad9308SMatthew Knepley   PC_FieldSplit *jac = (PC_FieldSplit*) pc->data;
157330ad9308SMatthew Knepley 
157430ad9308SMatthew Knepley   PetscFunctionBegin;
15750700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1576ce94432eSBarry Smith   if (jac->type != PC_COMPOSITE_SCHUR) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONG, "FieldSplit is not using a Schur complement approach.");
1577a04f6461SBarry Smith   if (A00) *A00 = jac->pmat[0];
1578a04f6461SBarry Smith   if (A01) *A01 = jac->B;
1579a04f6461SBarry Smith   if (A10) *A10 = jac->C;
1580a04f6461SBarry Smith   if (A11) *A11 = jac->pmat[1];
158130ad9308SMatthew Knepley   PetscFunctionReturn(0);
158230ad9308SMatthew Knepley }
158330ad9308SMatthew Knepley 
158479416396SBarry Smith #undef __FUNCT__
158579416396SBarry Smith #define __FUNCT__ "PCFieldSplitSetType_FieldSplit"
15861e6b0712SBarry Smith static PetscErrorCode  PCFieldSplitSetType_FieldSplit(PC pc,PCCompositeType type)
158779416396SBarry Smith {
158879416396SBarry Smith   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
1589e69d4d44SBarry Smith   PetscErrorCode ierr;
159079416396SBarry Smith 
159179416396SBarry Smith   PetscFunctionBegin;
159279416396SBarry Smith   jac->type = type;
15933b224e63SBarry Smith   if (type == PC_COMPOSITE_SCHUR) {
15943b224e63SBarry Smith     pc->ops->apply = PCApply_FieldSplit_Schur;
15953b224e63SBarry Smith     pc->ops->view  = PCView_FieldSplit_Schur;
15962fa5cd67SKarl Rupp 
159700de8ff0SBarry Smith     ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C","PCFieldSplitGetSubKSP_FieldSplit_Schur",PCFieldSplitGetSubKSP_FieldSplit_Schur);CHKERRQ(ierr);
159800de8ff0SBarry Smith     ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSchurPrecondition_C","PCFieldSplitSchurPrecondition_FieldSplit",PCFieldSplitSchurPrecondition_FieldSplit);CHKERRQ(ierr);
159900de8ff0SBarry Smith     ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C","PCFieldSplitSetSchurFactType_FieldSplit",PCFieldSplitSetSchurFactType_FieldSplit);CHKERRQ(ierr);
1600e69d4d44SBarry Smith 
16013b224e63SBarry Smith   } else {
16023b224e63SBarry Smith     pc->ops->apply = PCApply_FieldSplit;
16033b224e63SBarry Smith     pc->ops->view  = PCView_FieldSplit;
16042fa5cd67SKarl Rupp 
160500de8ff0SBarry Smith     ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C","PCFieldSplitGetSubKSP_FieldSplit",PCFieldSplitGetSubKSP_FieldSplit);CHKERRQ(ierr);
160600de8ff0SBarry Smith     ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSchurPrecondition_C","",0);CHKERRQ(ierr);
160700de8ff0SBarry Smith     ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C","",0);CHKERRQ(ierr);
16083b224e63SBarry Smith   }
160979416396SBarry Smith   PetscFunctionReturn(0);
161079416396SBarry Smith }
161179416396SBarry Smith 
161251f519a2SBarry Smith #undef __FUNCT__
161351f519a2SBarry Smith #define __FUNCT__ "PCFieldSplitSetBlockSize_FieldSplit"
16141e6b0712SBarry Smith static PetscErrorCode  PCFieldSplitSetBlockSize_FieldSplit(PC pc,PetscInt bs)
161551f519a2SBarry Smith {
161651f519a2SBarry Smith   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
161751f519a2SBarry Smith 
161851f519a2SBarry Smith   PetscFunctionBegin;
1619ce94432eSBarry Smith   if (bs < 1) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Blocksize must be positive, you gave %D",bs);
1620ce94432eSBarry 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);
162151f519a2SBarry Smith   jac->bs = bs;
162251f519a2SBarry Smith   PetscFunctionReturn(0);
162351f519a2SBarry Smith }
162451f519a2SBarry Smith 
162579416396SBarry Smith #undef __FUNCT__
162679416396SBarry Smith #define __FUNCT__ "PCFieldSplitSetType"
1627bc08b0f1SBarry Smith /*@
162879416396SBarry Smith    PCFieldSplitSetType - Sets the type of fieldsplit preconditioner.
162979416396SBarry Smith 
163079416396SBarry Smith    Collective on PC
163179416396SBarry Smith 
163279416396SBarry Smith    Input Parameter:
163379416396SBarry Smith .  pc - the preconditioner context
163481540f2fSBarry Smith .  type - PC_COMPOSITE_ADDITIVE, PC_COMPOSITE_MULTIPLICATIVE (default), PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE, PC_COMPOSITE_SPECIAL, PC_COMPOSITE_SCHUR
163579416396SBarry Smith 
163679416396SBarry Smith    Options Database Key:
1637a4efd8eaSMatthew Knepley .  -pc_fieldsplit_type <type: one of multiplicative, additive, symmetric_multiplicative, special, schur> - Sets fieldsplit preconditioner type
163879416396SBarry Smith 
1639b02e2d75SMatthew G Knepley    Level: Intermediate
164079416396SBarry Smith 
164179416396SBarry Smith .keywords: PC, set, type, composite preconditioner, additive, multiplicative
164279416396SBarry Smith 
164379416396SBarry Smith .seealso: PCCompositeSetType()
164479416396SBarry Smith 
164579416396SBarry Smith @*/
16467087cfbeSBarry Smith PetscErrorCode  PCFieldSplitSetType(PC pc,PCCompositeType type)
164779416396SBarry Smith {
16484ac538c5SBarry Smith   PetscErrorCode ierr;
164979416396SBarry Smith 
165079416396SBarry Smith   PetscFunctionBegin;
16510700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
16524ac538c5SBarry Smith   ierr = PetscTryMethod(pc,"PCFieldSplitSetType_C",(PC,PCCompositeType),(pc,type));CHKERRQ(ierr);
165379416396SBarry Smith   PetscFunctionReturn(0);
165479416396SBarry Smith }
165579416396SBarry Smith 
1656b02e2d75SMatthew G Knepley #undef __FUNCT__
1657b02e2d75SMatthew G Knepley #define __FUNCT__ "PCFieldSplitGetType"
1658b02e2d75SMatthew G Knepley /*@
1659b02e2d75SMatthew G Knepley   PCFieldSplitGetType - Gets the type of fieldsplit preconditioner.
1660b02e2d75SMatthew G Knepley 
1661b02e2d75SMatthew G Knepley   Not collective
1662b02e2d75SMatthew G Knepley 
1663b02e2d75SMatthew G Knepley   Input Parameter:
1664b02e2d75SMatthew G Knepley . pc - the preconditioner context
1665b02e2d75SMatthew G Knepley 
1666b02e2d75SMatthew G Knepley   Output Parameter:
1667b02e2d75SMatthew G Knepley . type - PC_COMPOSITE_ADDITIVE, PC_COMPOSITE_MULTIPLICATIVE (default), PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE, PC_COMPOSITE_SPECIAL, PC_COMPOSITE_SCHUR
1668b02e2d75SMatthew G Knepley 
1669b02e2d75SMatthew G Knepley   Level: Intermediate
1670b02e2d75SMatthew G Knepley 
1671b02e2d75SMatthew G Knepley .keywords: PC, set, type, composite preconditioner, additive, multiplicative
1672b02e2d75SMatthew G Knepley .seealso: PCCompositeSetType()
1673b02e2d75SMatthew G Knepley @*/
1674b02e2d75SMatthew G Knepley PetscErrorCode PCFieldSplitGetType(PC pc, PCCompositeType *type)
1675b02e2d75SMatthew G Knepley {
1676b02e2d75SMatthew G Knepley   PC_FieldSplit *jac = (PC_FieldSplit*) pc->data;
1677b02e2d75SMatthew G Knepley 
1678b02e2d75SMatthew G Knepley   PetscFunctionBegin;
1679b02e2d75SMatthew G Knepley   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1680b02e2d75SMatthew G Knepley   PetscValidIntPointer(type,2);
1681b02e2d75SMatthew G Knepley   *type = jac->type;
1682b02e2d75SMatthew G Knepley   PetscFunctionReturn(0);
1683b02e2d75SMatthew G Knepley }
1684b02e2d75SMatthew G Knepley 
16854ab8060aSDmitry Karpeev #undef __FUNCT__
16864ab8060aSDmitry Karpeev #define __FUNCT__ "PCFieldSplitSetDMSplits"
16874ab8060aSDmitry Karpeev /*@
16884ab8060aSDmitry Karpeev    PCFieldSplitSetDMSplits - Flags whether DMCreateFieldDecomposition() should be used to define the splits, whenever possible.
16894ab8060aSDmitry Karpeev 
16904ab8060aSDmitry Karpeev    Logically Collective
16914ab8060aSDmitry Karpeev 
16924ab8060aSDmitry Karpeev    Input Parameters:
16934ab8060aSDmitry Karpeev +  pc   - the preconditioner context
16944ab8060aSDmitry Karpeev -  flg  - boolean indicating whether to use field splits defined by the DM
16954ab8060aSDmitry Karpeev 
16964ab8060aSDmitry Karpeev    Options Database Key:
16974ab8060aSDmitry Karpeev .  -pc_fieldsplit_dm_splits
16984ab8060aSDmitry Karpeev 
16994ab8060aSDmitry Karpeev    Level: Intermediate
17004ab8060aSDmitry Karpeev 
17014ab8060aSDmitry Karpeev .keywords: PC, DM, composite preconditioner, additive, multiplicative
17024ab8060aSDmitry Karpeev 
17034ab8060aSDmitry Karpeev .seealso: PCFieldSplitGetDMSplits()
17044ab8060aSDmitry Karpeev 
17054ab8060aSDmitry Karpeev @*/
17064ab8060aSDmitry Karpeev PetscErrorCode  PCFieldSplitSetDMSplits(PC pc,PetscBool flg)
17074ab8060aSDmitry Karpeev {
17084ab8060aSDmitry Karpeev   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
17094ab8060aSDmitry Karpeev   PetscBool      isfs;
17104ab8060aSDmitry Karpeev   PetscErrorCode ierr;
17114ab8060aSDmitry Karpeev 
17124ab8060aSDmitry Karpeev   PetscFunctionBegin;
17134ab8060aSDmitry Karpeev   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
17144ab8060aSDmitry Karpeev   PetscValidLogicalCollectiveBool(pc,flg,2);
17154ab8060aSDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);CHKERRQ(ierr);
17164ab8060aSDmitry Karpeev   if (isfs) {
17174ab8060aSDmitry Karpeev     jac->dm_splits = flg;
17184ab8060aSDmitry Karpeev   }
17194ab8060aSDmitry Karpeev   PetscFunctionReturn(0);
17204ab8060aSDmitry Karpeev }
17214ab8060aSDmitry Karpeev 
17224ab8060aSDmitry Karpeev 
17234ab8060aSDmitry Karpeev #undef __FUNCT__
17244ab8060aSDmitry Karpeev #define __FUNCT__ "PCFieldSplitGetDMSplits"
17254ab8060aSDmitry Karpeev /*@
17264ab8060aSDmitry Karpeev    PCFieldSplitGetDMSplits - Returns flag indicating whether DMCreateFieldDecomposition() should be used to define the splits, whenever possible.
17274ab8060aSDmitry Karpeev 
17284ab8060aSDmitry Karpeev    Logically Collective
17294ab8060aSDmitry Karpeev 
17304ab8060aSDmitry Karpeev    Input Parameter:
17314ab8060aSDmitry Karpeev .  pc   - the preconditioner context
17324ab8060aSDmitry Karpeev 
17334ab8060aSDmitry Karpeev    Output Parameter:
17344ab8060aSDmitry Karpeev .  flg  - boolean indicating whether to use field splits defined by the DM
17354ab8060aSDmitry Karpeev 
17364ab8060aSDmitry Karpeev    Level: Intermediate
17374ab8060aSDmitry Karpeev 
17384ab8060aSDmitry Karpeev .keywords: PC, DM, composite preconditioner, additive, multiplicative
17394ab8060aSDmitry Karpeev 
17404ab8060aSDmitry Karpeev .seealso: PCFieldSplitSetDMSplits()
17414ab8060aSDmitry Karpeev 
17424ab8060aSDmitry Karpeev @*/
17434ab8060aSDmitry Karpeev PetscErrorCode  PCFieldSplitGetDMSplits(PC pc,PetscBool* flg)
17444ab8060aSDmitry Karpeev {
17454ab8060aSDmitry Karpeev   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
17464ab8060aSDmitry Karpeev   PetscBool      isfs;
17474ab8060aSDmitry Karpeev   PetscErrorCode ierr;
17484ab8060aSDmitry Karpeev 
17494ab8060aSDmitry Karpeev   PetscFunctionBegin;
17504ab8060aSDmitry Karpeev   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
17514ab8060aSDmitry Karpeev   PetscValidPointer(flg,2);
17524ab8060aSDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);CHKERRQ(ierr);
17534ab8060aSDmitry Karpeev   if (isfs) {
17544ab8060aSDmitry Karpeev     if(flg) *flg = jac->dm_splits;
17554ab8060aSDmitry Karpeev   }
17564ab8060aSDmitry Karpeev   PetscFunctionReturn(0);
17574ab8060aSDmitry Karpeev }
17584ab8060aSDmitry Karpeev 
17590971522cSBarry Smith /* -------------------------------------------------------------------------------------*/
17600971522cSBarry Smith /*MC
1761a8c7a070SBarry Smith    PCFIELDSPLIT - Preconditioner created by combining separate preconditioners for individual
1762a04f6461SBarry Smith                   fields or groups of fields. See the users manual section "Solving Block Matrices" for more details.
17630971522cSBarry Smith 
1764edf189efSBarry Smith      To set options on the solvers for each block append -fieldsplit_ to all the PC
1765edf189efSBarry Smith         options database keys. For example, -fieldsplit_pc_type ilu -fieldsplit_pc_factor_levels 1
17660971522cSBarry Smith 
1767a8c7a070SBarry Smith      To set the options on the solvers separate for each block call PCFieldSplitGetSubKSP()
176869a612a9SBarry Smith          and set the options directly on the resulting KSP object
17690971522cSBarry Smith 
17700971522cSBarry Smith    Level: intermediate
17710971522cSBarry Smith 
177279416396SBarry Smith    Options Database Keys:
177381540f2fSBarry Smith +   -pc_fieldsplit_%d_fields <a,b,..> - indicates the fields to be used in the %d'th split
177481540f2fSBarry Smith .   -pc_fieldsplit_default - automatically add any fields to additional splits that have not
177581540f2fSBarry Smith                               been supplied explicitly by -pc_fieldsplit_%d_fields
177681540f2fSBarry Smith .   -pc_fieldsplit_block_size <bs> - size of block that defines fields (i.e. there are bs fields)
17770f188ba9SJed Brown .   -pc_fieldsplit_type <additive,multiplicative,symmetric_multiplicative,schur> - type of relaxation or factorization splitting
1778e87fab1bSBarry Smith .   -pc_fieldsplit_schur_precondition <self,user,a11> - default is a11
1779435f959eSBarry Smith .   -pc_fieldsplit_detect_saddle_point - automatically finds rows with zero or negative diagonal and uses Schur complement with no preconditioner as the solver
178079416396SBarry Smith 
17815d4c12cdSJungho Lee -    Options prefix for inner solvers when using Schur complement preconditioner are -fieldsplit_0_ and -fieldsplit_1_
17825d4c12cdSJungho Lee      for all other solvers they are -fieldsplit_%d_ for the dth field, use -fieldsplit_ for all fields
17835d4c12cdSJungho Lee 
1784c8a0d604SMatthew G Knepley    Notes:
1785c8a0d604SMatthew G Knepley     Use PCFieldSplitSetFields() to set fields defined by "strided" entries and PCFieldSplitSetIS()
1786d32f9abdSBarry Smith      to define a field by an arbitrary collection of entries.
1787d32f9abdSBarry Smith 
1788d32f9abdSBarry Smith       If no fields are set the default is used. The fields are defined by entries strided by bs,
1789d32f9abdSBarry Smith       beginning at 0 then 1, etc to bs-1. The block size can be set with PCFieldSplitSetBlockSize(),
1790d32f9abdSBarry Smith       if this is not called the block size defaults to the blocksize of the second matrix passed
1791d32f9abdSBarry Smith       to KSPSetOperators()/PCSetOperators().
1792d32f9abdSBarry Smith 
1793c8a0d604SMatthew G Knepley $     For the Schur complement preconditioner if J = ( A00 A01 )
1794c8a0d604SMatthew G Knepley $                                                    ( A10 A11 )
1795c8a0d604SMatthew G Knepley $     the preconditioner using full factorization is
1796c8a0d604SMatthew G Knepley $              ( I   -A10 ksp(A00) ) ( inv(A00)     0  ) (     I          0  )
1797c8a0d604SMatthew G Knepley $              ( 0         I       ) (   0      ksp(S) ) ( -A10 ksp(A00)  I  )
1798a04f6461SBarry Smith      where the action of inv(A00) is applied using the KSP solver with prefix -fieldsplit_0_. The action of
1799c8a0d604SMatthew G Knepley      ksp(S) is computed using the KSP solver with prefix -fieldsplit_splitname_ (where splitname was given
1800c8a0d604SMatthew G Knepley      in providing the SECOND split or 1 if not give). For PCFieldSplitGetKSP() when field number is 0,
1801c8a0d604SMatthew G Knepley      it returns the KSP associated with -fieldsplit_0_ while field number 1 gives -fieldsplit_1_ KSP. By default
1802a04f6461SBarry Smith      A11 is used to construct a preconditioner for S, use PCFieldSplitSchurPrecondition() to turn on or off this
1803c8a0d604SMatthew G Knepley      option. You can use the preconditioner PCLSC to precondition the Schur complement with -fieldsplit_1_pc_type lsc. The
1804c9c6ffaaSJed Brown      factorization type is set using -pc_fieldsplit_schur_fact_type <diag, lower, upper, full>. The full is shown above,
18055668aaf4SBarry Smith      diag gives
1806c8a0d604SMatthew G Knepley $              ( inv(A00)     0   )
1807c8a0d604SMatthew G Knepley $              (   0      -ksp(S) )
18085668aaf4SBarry 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
1809c8a0d604SMatthew G Knepley $              (  A00   0 )
1810c8a0d604SMatthew G Knepley $              (  A10   S )
1811c8a0d604SMatthew G Knepley      where the inverses of A00 and S are applied using KSPs. The upper factorization is the inverse of
1812c8a0d604SMatthew G Knepley $              ( A00 A01 )
1813c8a0d604SMatthew G Knepley $              (  0   S  )
1814c8a0d604SMatthew G Knepley      where again the inverses of A00 and S are applied using KSPs.
1815e69d4d44SBarry Smith 
1816edf189efSBarry Smith      If only one set of indices (one IS) is provided with PCFieldSplitSetIS() then the complement of that IS
1817edf189efSBarry Smith      is used automatically for a second block.
1818edf189efSBarry Smith 
1819ff218e97SBarry Smith      The fieldsplit preconditioner cannot currently be used with the BAIJ or SBAIJ data formats if the blocksize is larger than 1.
1820ff218e97SBarry Smith      Generally it should be used with the AIJ format.
1821ff218e97SBarry Smith 
1822ff218e97SBarry Smith      The forms of these preconditioners are closely related if not identical to forms derived as "Distributive Iterations", see,
1823ff218e97SBarry Smith      for example, page 294 in "Principles of Computational Fluid Dynamics" by Pieter Wesseling. Note that one can also use PCFIELDSPLIT
1824ff218e97SBarry Smith      inside a smoother resulting in "Distributive Smoothers".
18250716a85fSBarry Smith 
1826a541d17aSBarry Smith    Concepts: physics based preconditioners, block preconditioners
18270971522cSBarry Smith 
18287e8cb189SBarry Smith .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC, Block_Preconditioners, PCLSC,
1829e69d4d44SBarry Smith            PCFieldSplitGetSubKSP(), PCFieldSplitSetFields(), PCFieldSplitSetType(), PCFieldSplitSetIS(), PCFieldSplitSchurPrecondition()
18300971522cSBarry Smith M*/
18310971522cSBarry Smith 
18320971522cSBarry Smith #undef __FUNCT__
18330971522cSBarry Smith #define __FUNCT__ "PCCreate_FieldSplit"
1834*8cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PCCreate_FieldSplit(PC pc)
18350971522cSBarry Smith {
18360971522cSBarry Smith   PetscErrorCode ierr;
18370971522cSBarry Smith   PC_FieldSplit  *jac;
18380971522cSBarry Smith 
18390971522cSBarry Smith   PetscFunctionBegin;
184038f2d2fdSLisandro Dalcin   ierr = PetscNewLog(pc,PC_FieldSplit,&jac);CHKERRQ(ierr);
18412fa5cd67SKarl Rupp 
18420971522cSBarry Smith   jac->bs                 = -1;
18430971522cSBarry Smith   jac->nsplits            = 0;
18443e197d65SBarry Smith   jac->type               = PC_COMPOSITE_MULTIPLICATIVE;
1845e6cab6aaSJed Brown   jac->schurpre           = PC_FIELDSPLIT_SCHUR_PRE_USER; /* Try user preconditioner first, fall back on diagonal */
1846c9c6ffaaSJed Brown   jac->schurfactorization = PC_FIELDSPLIT_SCHUR_FACT_FULL;
1847fbe7908bSJed Brown   jac->dm_splits          = PETSC_TRUE;
184851f519a2SBarry Smith 
18490971522cSBarry Smith   pc->data = (void*)jac;
18500971522cSBarry Smith 
18510971522cSBarry Smith   pc->ops->apply           = PCApply_FieldSplit;
1852421e10b8SBarry Smith   pc->ops->applytranspose  = PCApplyTranspose_FieldSplit;
18530971522cSBarry Smith   pc->ops->setup           = PCSetUp_FieldSplit;
1854574deadeSBarry Smith   pc->ops->reset           = PCReset_FieldSplit;
18550971522cSBarry Smith   pc->ops->destroy         = PCDestroy_FieldSplit;
18560971522cSBarry Smith   pc->ops->setfromoptions  = PCSetFromOptions_FieldSplit;
18570971522cSBarry Smith   pc->ops->view            = PCView_FieldSplit;
18580971522cSBarry Smith   pc->ops->applyrichardson = 0;
18590971522cSBarry Smith 
186000de8ff0SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C","PCFieldSplitGetSubKSP_FieldSplit",PCFieldSplitGetSubKSP_FieldSplit);CHKERRQ(ierr);
186100de8ff0SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetFields_C","PCFieldSplitSetFields_FieldSplit",PCFieldSplitSetFields_FieldSplit);CHKERRQ(ierr);
186200de8ff0SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetIS_C","PCFieldSplitSetIS_FieldSplit",PCFieldSplitSetIS_FieldSplit);CHKERRQ(ierr);
186300de8ff0SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetType_C","PCFieldSplitSetType_FieldSplit",PCFieldSplitSetType_FieldSplit);CHKERRQ(ierr);
186400de8ff0SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetBlockSize_C","PCFieldSplitSetBlockSize_FieldSplit",PCFieldSplitSetBlockSize_FieldSplit);CHKERRQ(ierr);
18650971522cSBarry Smith   PetscFunctionReturn(0);
18660971522cSBarry Smith }
18670971522cSBarry Smith 
18680971522cSBarry Smith 
1869a541d17aSBarry Smith 
1870