xref: /petsc/src/ksp/pc/impls/fieldsplit/fieldsplit.c (revision aa6c7ce3b3ede9cc2f00f8ce4adba428e92e0fa2)
1dba47a55SKris Buschelman 
2b45d2f2cSJed Brown #include <petsc-private/pcimpl.h>     /*I "petscpc.h" I*/
31e25c274SJed Brown #include <petscdm.h>
40971522cSBarry Smith 
5443836d0SMatthew G Knepley /*
6443836d0SMatthew G Knepley   There is a nice discussion of block preconditioners in
7443836d0SMatthew G Knepley 
81997fe2eSSatish Balay [El08] A taxonomy and comparison of parallel block multi-level preconditioners for the incompressible Navier-Stokes equations
9443836d0SMatthew G Knepley        Howard Elman, V.E. Howle, John Shadid, Robert Shuttleworth, Ray Tuminaro, Journal of Computational Physics 227 (2008) 1790--1808
101b8e4c5fSJed Brown        http://chess.cs.umd.edu/~elman/papers/tax.pdf
11443836d0SMatthew G Knepley */
12443836d0SMatthew G Knepley 
132e71c61dSMatthew G. Knepley const char *const PCFieldSplitSchurPreTypes[] = {"SELF","SELFP","A11","USER","FULL","PCFieldSplitSchurPreType","PC_FIELDSPLIT_SCHUR_PRE_",0};
14c9c6ffaaSJed Brown const char *const PCFieldSplitSchurFactTypes[] = {"DIAG","LOWER","UPPER","FULL","PCFieldSplitSchurFactType","PC_FIELDSPLIT_SCHUR_FACT_",0};
15c5d2311dSJed Brown 
160971522cSBarry Smith typedef struct _PC_FieldSplitLink *PC_FieldSplitLink;
170971522cSBarry Smith struct _PC_FieldSplitLink {
1869a612a9SBarry Smith   KSP               ksp;
19443836d0SMatthew G Knepley   Vec               x,y,z;
20db4c96c1SJed Brown   char              *splitname;
210971522cSBarry Smith   PetscInt          nfields;
225d4c12cdSJungho Lee   PetscInt          *fields,*fields_col;
231b9fc7fcSBarry Smith   VecScatter        sctx;
245d4c12cdSJungho Lee   IS                is,is_col;
2551f519a2SBarry Smith   PC_FieldSplitLink next,previous;
260971522cSBarry Smith };
270971522cSBarry Smith 
280971522cSBarry Smith typedef struct {
2968dd23aaSBarry Smith   PCCompositeType type;
30ace3abfcSBarry Smith   PetscBool       defaultsplit;                    /* Flag for a system with a set of 'k' scalar fields with the same layout (and bs = k) */
31ace3abfcSBarry Smith   PetscBool       splitdefined;                    /* Flag is set after the splits have been defined, to prevent more splits from being added */
3230ad9308SMatthew Knepley   PetscInt        bs;                              /* Block size for IS and Mat structures */
3330ad9308SMatthew Knepley   PetscInt        nsplits;                         /* Number of field divisions defined */
3479416396SBarry Smith   Vec             *x,*y,w1,w2;
35519d70e2SJed Brown   Mat             *mat;                            /* The diagonal block for each split */
36519d70e2SJed Brown   Mat             *pmat;                           /* The preconditioning diagonal block for each split */
3730ad9308SMatthew Knepley   Mat             *Afield;                         /* The rows of the matrix associated with each split */
38ace3abfcSBarry Smith   PetscBool       issetup;
392fa5cd67SKarl Rupp 
4030ad9308SMatthew Knepley   /* Only used when Schur complement preconditioning is used */
4130ad9308SMatthew Knepley   Mat                       B;                     /* The (0,1) block */
4230ad9308SMatthew Knepley   Mat                       C;                     /* The (1,0) block */
43443836d0SMatthew G Knepley   Mat                       schur;                 /* The Schur complement S = A11 - A10 A00^{-1} A01, the KSP here, kspinner, is H_1 in [El08] */
44a7476a74SDmitry Karpeev   Mat                       schurp;                /* Assembled approximation to S built by MatSchurComplement to be used as a preconditioning matrix when solving with S */
45084e4875SJed Brown   Mat                       schur_user;            /* User-provided preconditioning matrix for the Schur complement */
46084e4875SJed Brown   PCFieldSplitSchurPreType  schurpre;              /* Determines which preconditioning matrix is used for the Schur complement */
47c9c6ffaaSJed Brown   PCFieldSplitSchurFactType schurfactorization;
4830ad9308SMatthew Knepley   KSP                       kspschur;              /* The solver for S */
49443836d0SMatthew G Knepley   KSP                       kspupper;              /* The solver for A in the upper diagonal part of the factorization (H_2 in [El08]) */
5097bbdb24SBarry Smith   PC_FieldSplitLink         head;
5163ec74ffSBarry Smith   PetscBool                 reset;                  /* indicates PCReset() has been last called on this object, hack */
52c1570756SJed Brown   PetscBool                 suboptionsset;          /* Indicates that the KSPSetFromOptions() has been called on the sub-KSPs */
534ab8060aSDmitry Karpeev   PetscBool                 dm_splits;              /* Whether to use DMCreateFieldDecomposition() whenever possible */
540971522cSBarry Smith } PC_FieldSplit;
550971522cSBarry Smith 
5616913363SBarry Smith /*
5716913363SBarry Smith     Notes: there is no particular reason that pmat, x, and y are stored as arrays in PC_FieldSplit instead of
5816913363SBarry Smith    inside PC_FieldSplitLink, just historical. If you want to be able to add new fields after already using the
5916913363SBarry Smith    PC you could change this.
6016913363SBarry Smith */
61084e4875SJed Brown 
62e6cab6aaSJed Brown /* This helper is so that setting a user-provided preconditioning matrix is orthogonal to choosing to use it.  This way the
63084e4875SJed Brown * application-provided FormJacobian can provide this matrix without interfering with the user's (command-line) choices. */
64084e4875SJed Brown static Mat FieldSplitSchurPre(PC_FieldSplit *jac)
65084e4875SJed Brown {
66084e4875SJed Brown   switch (jac->schurpre) {
67084e4875SJed Brown   case PC_FIELDSPLIT_SCHUR_PRE_SELF: return jac->schur;
68a7476a74SDmitry Karpeev   case PC_FIELDSPLIT_SCHUR_PRE_SELFP: return jac->schurp;
69e87fab1bSBarry Smith   case PC_FIELDSPLIT_SCHUR_PRE_A11: return jac->pmat[1];
70e74569cdSMatthew G. Knepley   case PC_FIELDSPLIT_SCHUR_PRE_FULL: /* We calculate this and store it in schur_user */
71084e4875SJed Brown   case PC_FIELDSPLIT_SCHUR_PRE_USER: /* Use a user-provided matrix if it is given, otherwise diagonal block */
72084e4875SJed Brown   default:
73084e4875SJed Brown     return jac->schur_user ? jac->schur_user : jac->pmat[1];
74084e4875SJed Brown   }
75084e4875SJed Brown }
76084e4875SJed Brown 
77084e4875SJed Brown 
789804daf3SBarry Smith #include <petscdraw.h>
790971522cSBarry Smith #undef __FUNCT__
800971522cSBarry Smith #define __FUNCT__ "PCView_FieldSplit"
810971522cSBarry Smith static PetscErrorCode PCView_FieldSplit(PC pc,PetscViewer viewer)
820971522cSBarry Smith {
830971522cSBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
840971522cSBarry Smith   PetscErrorCode    ierr;
85d9884438SBarry Smith   PetscBool         iascii,isdraw;
860971522cSBarry Smith   PetscInt          i,j;
875a9f2f41SSatish Balay   PC_FieldSplitLink ilink = jac->head;
880971522cSBarry Smith 
890971522cSBarry Smith   PetscFunctionBegin;
90251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
91d9884438SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
920971522cSBarry Smith   if (iascii) {
932c9966d7SBarry Smith     if (jac->bs > 0) {
9451f519a2SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  FieldSplit with %s composition: total splits = %D, blocksize = %D\n",PCCompositeTypes[jac->type],jac->nsplits,jac->bs);CHKERRQ(ierr);
952c9966d7SBarry Smith     } else {
962c9966d7SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  FieldSplit with %s composition: total splits = %D\n",PCCompositeTypes[jac->type],jac->nsplits);CHKERRQ(ierr);
972c9966d7SBarry Smith     }
98f5236f50SJed Brown     if (pc->useAmat) {
99f5236f50SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  using Amat (not Pmat) as operator for blocks\n");CHKERRQ(ierr);
100a3df900dSMatthew G Knepley     }
10169a612a9SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  Solver info for each split is in the following KSP objects:\n");CHKERRQ(ierr);
1020971522cSBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1030971522cSBarry Smith     for (i=0; i<jac->nsplits; i++) {
1041ab39975SBarry Smith       if (ilink->fields) {
1050971522cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Split number %D Fields ",i);CHKERRQ(ierr);
10679416396SBarry Smith         ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
1075a9f2f41SSatish Balay         for (j=0; j<ilink->nfields; j++) {
10879416396SBarry Smith           if (j > 0) {
10979416396SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer,",");CHKERRQ(ierr);
11079416396SBarry Smith           }
1115a9f2f41SSatish Balay           ierr = PetscViewerASCIIPrintf(viewer," %D",ilink->fields[j]);CHKERRQ(ierr);
1120971522cSBarry Smith         }
1130971522cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
11479416396SBarry Smith         ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
1151ab39975SBarry Smith       } else {
1161ab39975SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Split number %D Defined by IS\n",i);CHKERRQ(ierr);
1171ab39975SBarry Smith       }
1185a9f2f41SSatish Balay       ierr  = KSPView(ilink->ksp,viewer);CHKERRQ(ierr);
1195a9f2f41SSatish Balay       ilink = ilink->next;
1200971522cSBarry Smith     }
1210971522cSBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1222fa5cd67SKarl Rupp   }
1232fa5cd67SKarl Rupp 
1242fa5cd67SKarl Rupp  if (isdraw) {
125d9884438SBarry Smith     PetscDraw draw;
126d9884438SBarry Smith     PetscReal x,y,w,wd;
127d9884438SBarry Smith 
128d9884438SBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
129d9884438SBarry Smith     ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
130d9884438SBarry Smith     w    = 2*PetscMin(1.0 - x,x);
131d9884438SBarry Smith     wd   = w/(jac->nsplits + 1);
132d9884438SBarry Smith     x    = x - wd*(jac->nsplits-1)/2.0;
133d9884438SBarry Smith     for (i=0; i<jac->nsplits; i++) {
134d9884438SBarry Smith       ierr  = PetscDrawPushCurrentPoint(draw,x,y);CHKERRQ(ierr);
135d9884438SBarry Smith       ierr  = KSPView(ilink->ksp,viewer);CHKERRQ(ierr);
136d9884438SBarry Smith       ierr  = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
137d9884438SBarry Smith       x    += wd;
138d9884438SBarry Smith       ilink = ilink->next;
139d9884438SBarry Smith     }
1400971522cSBarry Smith   }
1410971522cSBarry Smith   PetscFunctionReturn(0);
1420971522cSBarry Smith }
1430971522cSBarry Smith 
1440971522cSBarry Smith #undef __FUNCT__
1453b224e63SBarry Smith #define __FUNCT__ "PCView_FieldSplit_Schur"
1463b224e63SBarry Smith static PetscErrorCode PCView_FieldSplit_Schur(PC pc,PetscViewer viewer)
1473b224e63SBarry Smith {
1483b224e63SBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1493b224e63SBarry Smith   PetscErrorCode    ierr;
1504996c5bdSBarry Smith   PetscBool         iascii,isdraw;
1513b224e63SBarry Smith   PetscInt          i,j;
1523b224e63SBarry Smith   PC_FieldSplitLink ilink = jac->head;
1533b224e63SBarry Smith 
1543b224e63SBarry Smith   PetscFunctionBegin;
155251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1564996c5bdSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1573b224e63SBarry Smith   if (iascii) {
1582c9966d7SBarry Smith     if (jac->bs > 0) {
159c9c6ffaaSJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  FieldSplit with Schur preconditioner, blocksize = %D, factorization %s\n",jac->bs,PCFieldSplitSchurFactTypes[jac->schurfactorization]);CHKERRQ(ierr);
1602c9966d7SBarry Smith     } else {
1612c9966d7SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  FieldSplit with Schur preconditioner, factorization %s\n",PCFieldSplitSchurFactTypes[jac->schurfactorization]);CHKERRQ(ierr);
1622c9966d7SBarry Smith     }
163f5236f50SJed Brown     if (pc->useAmat) {
164f5236f50SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  using Amat (not Pmat) as operator for blocks\n");CHKERRQ(ierr);
165a3df900dSMatthew G Knepley     }
1663e8b8b31SMatthew G Knepley     switch (jac->schurpre) {
1673e8b8b31SMatthew G Knepley     case PC_FIELDSPLIT_SCHUR_PRE_SELF:
1683e8b8b31SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioner for the Schur complement formed from S itself\n");CHKERRQ(ierr);break;
169a7476a74SDmitry Karpeev     case PC_FIELDSPLIT_SCHUR_PRE_SELFP:
170a7476a74SDmitry Karpeev       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioner for the Schur complement formed from Sp, an assembled approximation to S, which uses A00's diagonal's inverse\n");CHKERRQ(ierr);break;
171e87fab1bSBarry Smith     case PC_FIELDSPLIT_SCHUR_PRE_A11:
172e87fab1bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioner for the Schur complement formed from A11\n");CHKERRQ(ierr);break;
173e74569cdSMatthew G. Knepley     case PC_FIELDSPLIT_SCHUR_PRE_FULL:
174e74569cdSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioner for the Schur complement formed from the exact Schur complement\n");CHKERRQ(ierr);break;
1753e8b8b31SMatthew G Knepley     case PC_FIELDSPLIT_SCHUR_PRE_USER:
1763e8b8b31SMatthew G Knepley       if (jac->schur_user) {
1773e8b8b31SMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioner for the Schur complement formed from user provided matrix\n");CHKERRQ(ierr);
1783e8b8b31SMatthew G Knepley       } else {
179e87fab1bSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioner for the Schur complement formed from A11\n");CHKERRQ(ierr);
1803e8b8b31SMatthew G Knepley       }
1813e8b8b31SMatthew G Knepley       break;
1823e8b8b31SMatthew G Knepley     default:
18382f516ccSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_OUTOFRANGE, "Invalid Schur preconditioning type: %d", jac->schurpre);
1843e8b8b31SMatthew G Knepley     }
1853b224e63SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  Split info:\n");CHKERRQ(ierr);
1863b224e63SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1873b224e63SBarry Smith     for (i=0; i<jac->nsplits; i++) {
1883b224e63SBarry Smith       if (ilink->fields) {
1893b224e63SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Split number %D Fields ",i);CHKERRQ(ierr);
1903b224e63SBarry Smith         ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr);
1913b224e63SBarry Smith         for (j=0; j<ilink->nfields; j++) {
1923b224e63SBarry Smith           if (j > 0) {
1933b224e63SBarry Smith             ierr = PetscViewerASCIIPrintf(viewer,",");CHKERRQ(ierr);
1943b224e63SBarry Smith           }
1953b224e63SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," %D",ilink->fields[j]);CHKERRQ(ierr);
1963b224e63SBarry Smith         }
1973b224e63SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
1983b224e63SBarry Smith         ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr);
1993b224e63SBarry Smith       } else {
2003b224e63SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Split number %D Defined by IS\n",i);CHKERRQ(ierr);
2013b224e63SBarry Smith       }
2023b224e63SBarry Smith       ilink = ilink->next;
2033b224e63SBarry Smith     }
204435f959eSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"KSP solver for A00 block\n");CHKERRQ(ierr);
2053b224e63SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
20606de4afeSJed Brown     if (jac->head) {
207443836d0SMatthew G Knepley       ierr = KSPView(jac->head->ksp,viewer);CHKERRQ(ierr);
20806de4afeSJed Brown     } else  {ierr = PetscViewerASCIIPrintf(viewer,"  not yet available\n");CHKERRQ(ierr);}
2093b224e63SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
21006de4afeSJed Brown     if (jac->head && jac->kspupper != jac->head->ksp) {
211443836d0SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer,"KSP solver for upper A00 in upper triangular factor \n");CHKERRQ(ierr);
212443836d0SMatthew G Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
213b2750c55SJed Brown       if (jac->kspupper) {ierr = KSPView(jac->kspupper,viewer);CHKERRQ(ierr);}
214b2750c55SJed Brown       else {ierr = PetscViewerASCIIPrintf(viewer,"  not yet available\n");CHKERRQ(ierr);}
215443836d0SMatthew G Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
216443836d0SMatthew G Knepley     }
217435f959eSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"KSP solver for S = A11 - A10 inv(A00) A01 \n");CHKERRQ(ierr);
2183b224e63SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
21912cae6f2SJed Brown     if (jac->kspschur) {
2203b224e63SBarry Smith       ierr = KSPView(jac->kspschur,viewer);CHKERRQ(ierr);
22112cae6f2SJed Brown     } else {
22212cae6f2SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  not yet available\n");CHKERRQ(ierr);
22312cae6f2SJed Brown     }
2243b224e63SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2253b224e63SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
22606de4afeSJed Brown   } else if (isdraw && jac->head) {
2274996c5bdSBarry Smith     PetscDraw draw;
2284996c5bdSBarry Smith     PetscReal x,y,w,wd,h;
2294996c5bdSBarry Smith     PetscInt  cnt = 2;
2304996c5bdSBarry Smith     char      str[32];
2314996c5bdSBarry Smith 
2324996c5bdSBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
2334996c5bdSBarry Smith     ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
234c74581afSBarry Smith     if (jac->kspupper != jac->head->ksp) cnt++;
235c74581afSBarry Smith     w  = 2*PetscMin(1.0 - x,x);
236c74581afSBarry Smith     wd = w/(cnt + 1);
237c74581afSBarry Smith 
2384996c5bdSBarry Smith     ierr = PetscSNPrintf(str,32,"Schur fact. %s",PCFieldSplitSchurFactTypes[jac->schurfactorization]);CHKERRQ(ierr);
2390298fd71SBarry Smith     ierr = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_RED,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
2404996c5bdSBarry Smith     y   -= h;
2414996c5bdSBarry Smith     if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_USER &&  !jac->schur_user) {
242e87fab1bSBarry Smith       ierr = PetscSNPrintf(str,32,"Prec. for Schur from %s",PCFieldSplitSchurPreTypes[PC_FIELDSPLIT_SCHUR_PRE_A11]);CHKERRQ(ierr);
2433b224e63SBarry Smith     } else {
2444996c5bdSBarry Smith       ierr = PetscSNPrintf(str,32,"Prec. for Schur from %s",PCFieldSplitSchurPreTypes[jac->schurpre]);CHKERRQ(ierr);
2454996c5bdSBarry Smith     }
2460298fd71SBarry Smith     ierr = PetscDrawBoxedString(draw,x+wd*(cnt-1)/2.0,y,PETSC_DRAW_RED,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
2474996c5bdSBarry Smith     y   -= h;
2484996c5bdSBarry Smith     x    = x - wd*(cnt-1)/2.0;
2494996c5bdSBarry Smith 
2504996c5bdSBarry Smith     ierr = PetscDrawPushCurrentPoint(draw,x,y);CHKERRQ(ierr);
2514996c5bdSBarry Smith     ierr = KSPView(jac->head->ksp,viewer);CHKERRQ(ierr);
2524996c5bdSBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2534996c5bdSBarry Smith     if (jac->kspupper != jac->head->ksp) {
2544996c5bdSBarry Smith       x   += wd;
2554996c5bdSBarry Smith       ierr = PetscDrawPushCurrentPoint(draw,x,y);CHKERRQ(ierr);
2564996c5bdSBarry Smith       ierr = KSPView(jac->kspupper,viewer);CHKERRQ(ierr);
2574996c5bdSBarry Smith       ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2584996c5bdSBarry Smith     }
2594996c5bdSBarry Smith     x   += wd;
2604996c5bdSBarry Smith     ierr = PetscDrawPushCurrentPoint(draw,x,y);CHKERRQ(ierr);
2614996c5bdSBarry Smith     ierr = KSPView(jac->kspschur,viewer);CHKERRQ(ierr);
2624996c5bdSBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2633b224e63SBarry Smith   }
2643b224e63SBarry Smith   PetscFunctionReturn(0);
2653b224e63SBarry Smith }
2663b224e63SBarry Smith 
2673b224e63SBarry Smith #undef __FUNCT__
2686c924f48SJed Brown #define __FUNCT__ "PCFieldSplitSetRuntimeSplits_Private"
2696c924f48SJed Brown /* Precondition: jac->bs is set to a meaningful value */
2706c924f48SJed Brown static PetscErrorCode PCFieldSplitSetRuntimeSplits_Private(PC pc)
2716c924f48SJed Brown {
2726c924f48SJed Brown   PetscErrorCode ierr;
2736c924f48SJed Brown   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
2745d4c12cdSJungho Lee   PetscInt       i,nfields,*ifields,nfields_col,*ifields_col;
2755d4c12cdSJungho Lee   PetscBool      flg,flg_col;
2765d4c12cdSJungho Lee   char           optionname[128],splitname[8],optionname_col[128];
2776c924f48SJed Brown 
2786c924f48SJed Brown   PetscFunctionBegin;
279785e854fSJed Brown   ierr = PetscMalloc1(jac->bs,&ifields);CHKERRQ(ierr);
280785e854fSJed Brown   ierr = PetscMalloc1(jac->bs,&ifields_col);CHKERRQ(ierr);
2816c924f48SJed Brown   for (i=0,flg=PETSC_TRUE;; i++) {
2828caf3d72SBarry Smith     ierr        = PetscSNPrintf(splitname,sizeof(splitname),"%D",i);CHKERRQ(ierr);
2838caf3d72SBarry Smith     ierr        = PetscSNPrintf(optionname,sizeof(optionname),"-pc_fieldsplit_%D_fields",i);CHKERRQ(ierr);
2848caf3d72SBarry Smith     ierr        = PetscSNPrintf(optionname_col,sizeof(optionname_col),"-pc_fieldsplit_%D_fields_col",i);CHKERRQ(ierr);
2856c924f48SJed Brown     nfields     = jac->bs;
28629499fbbSJungho Lee     nfields_col = jac->bs;
2876c924f48SJed Brown     ierr        = PetscOptionsGetIntArray(((PetscObject)pc)->prefix,optionname,ifields,&nfields,&flg);CHKERRQ(ierr);
2885d4c12cdSJungho Lee     ierr        = PetscOptionsGetIntArray(((PetscObject)pc)->prefix,optionname_col,ifields_col,&nfields_col,&flg_col);CHKERRQ(ierr);
2896c924f48SJed Brown     if (!flg) break;
2905d4c12cdSJungho Lee     else if (flg && !flg_col) {
2915d4c12cdSJungho Lee       if (!nfields) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Cannot list zero fields");
2925d4c12cdSJungho Lee       ierr = PCFieldSplitSetFields(pc,splitname,nfields,ifields,ifields);CHKERRQ(ierr);
2932fa5cd67SKarl Rupp     } else {
2945d4c12cdSJungho Lee       if (!nfields || !nfields_col) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Cannot list zero fields");
2955d4c12cdSJungho Lee       if (nfields != nfields_col) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Number of row and column fields must match");
2965d4c12cdSJungho Lee       ierr = PCFieldSplitSetFields(pc,splitname,nfields,ifields,ifields_col);CHKERRQ(ierr);
2975d4c12cdSJungho Lee     }
2986c924f48SJed Brown   }
2996c924f48SJed Brown   if (i > 0) {
3006c924f48SJed Brown     /* Makes command-line setting of splits take precedence over setting them in code.
3016c924f48SJed Brown        Otherwise subsequent calls to PCFieldSplitSetIS() or PCFieldSplitSetFields() would
3026c924f48SJed Brown        create new splits, which would probably not be what the user wanted. */
3036c924f48SJed Brown     jac->splitdefined = PETSC_TRUE;
3046c924f48SJed Brown   }
3056c924f48SJed Brown   ierr = PetscFree(ifields);CHKERRQ(ierr);
3065d4c12cdSJungho Lee   ierr = PetscFree(ifields_col);CHKERRQ(ierr);
3076c924f48SJed Brown   PetscFunctionReturn(0);
3086c924f48SJed Brown }
3096c924f48SJed Brown 
3106c924f48SJed Brown #undef __FUNCT__
31169a612a9SBarry Smith #define __FUNCT__ "PCFieldSplitSetDefaults"
31269a612a9SBarry Smith static PetscErrorCode PCFieldSplitSetDefaults(PC pc)
3130971522cSBarry Smith {
3140971522cSBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
3150971522cSBarry Smith   PetscErrorCode    ierr;
3165a9f2f41SSatish Balay   PC_FieldSplitLink ilink              = jac->head;
3173a062f41SBarry Smith   PetscBool         fieldsplit_default = PETSC_FALSE,stokes = PETSC_FALSE,coupling = PETSC_FALSE;
3186c924f48SJed Brown   PetscInt          i;
3190971522cSBarry Smith 
3200971522cSBarry Smith   PetscFunctionBegin;
3217287d2a3SDmitry Karpeev   /*
3227287d2a3SDmitry Karpeev    Kinda messy, but at least this now uses DMCreateFieldDecomposition() even with jac->reset.
3237287d2a3SDmitry Karpeev    Should probably be rewritten.
3247287d2a3SDmitry Karpeev    */
3257287d2a3SDmitry Karpeev   if (!ilink || jac->reset) {
3260298fd71SBarry Smith     ierr = PetscOptionsGetBool(((PetscObject)pc)->prefix,"-pc_fieldsplit_detect_saddle_point",&stokes,NULL);CHKERRQ(ierr);
3273a062f41SBarry Smith     ierr = PetscOptionsGetBool(((PetscObject)pc)->prefix,"-pc_fieldsplit_detect_coupling",&coupling,NULL);CHKERRQ(ierr);
3283a062f41SBarry Smith     if (pc->dm && jac->dm_splits && !stokes && !coupling) {
329bafc1b83SMatthew G Knepley       PetscInt  numFields, f, i, j;
3300784a22cSJed Brown       char      **fieldNames;
3317b62db95SJungho Lee       IS        *fields;
332e7c4fc90SDmitry Karpeev       DM        *dms;
333bafc1b83SMatthew G Knepley       DM        subdm[128];
334bafc1b83SMatthew G Knepley       PetscBool flg;
335bafc1b83SMatthew G Knepley 
33616621825SDmitry Karpeev       ierr = DMCreateFieldDecomposition(pc->dm, &numFields, &fieldNames, &fields, &dms);CHKERRQ(ierr);
337bafc1b83SMatthew G Knepley       /* Allow the user to prescribe the splits */
338bafc1b83SMatthew G Knepley       for (i = 0, flg = PETSC_TRUE;; i++) {
339bafc1b83SMatthew G Knepley         PetscInt ifields[128];
340bafc1b83SMatthew G Knepley         IS       compField;
341bafc1b83SMatthew G Knepley         char     optionname[128], splitname[8];
342bafc1b83SMatthew G Knepley         PetscInt nfields = numFields;
343bafc1b83SMatthew G Knepley 
3448caf3d72SBarry Smith         ierr = PetscSNPrintf(optionname, sizeof(optionname), "-pc_fieldsplit_%D_fields", i);CHKERRQ(ierr);
345bafc1b83SMatthew G Knepley         ierr = PetscOptionsGetIntArray(((PetscObject) pc)->prefix, optionname, ifields, &nfields, &flg);CHKERRQ(ierr);
346bafc1b83SMatthew G Knepley         if (!flg) break;
34782f516ccSBarry Smith         if (numFields > 128) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Cannot currently support %d > 128 fields", numFields);
348bafc1b83SMatthew G Knepley         ierr = DMCreateSubDM(pc->dm, nfields, ifields, &compField, &subdm[i]);CHKERRQ(ierr);
349bafc1b83SMatthew G Knepley         if (nfields == 1) {
350bafc1b83SMatthew G Knepley           ierr = PCFieldSplitSetIS(pc, fieldNames[ifields[0]], compField);CHKERRQ(ierr);
35182f516ccSBarry Smith           /* ierr = PetscPrintf(PetscObjectComm((PetscObject)pc), "%s Field Indices:", fieldNames[ifields[0]]);CHKERRQ(ierr);
3520298fd71SBarry Smith              ierr = ISView(compField, NULL);CHKERRQ(ierr); */
353bafc1b83SMatthew G Knepley         } else {
3548caf3d72SBarry Smith           ierr = PetscSNPrintf(splitname, sizeof(splitname), "%D", i);CHKERRQ(ierr);
355bafc1b83SMatthew G Knepley           ierr = PCFieldSplitSetIS(pc, splitname, compField);CHKERRQ(ierr);
35682f516ccSBarry Smith           /* ierr = PetscPrintf(PetscObjectComm((PetscObject)pc), "%s Field Indices:", splitname);CHKERRQ(ierr);
3570298fd71SBarry Smith              ierr = ISView(compField, NULL);CHKERRQ(ierr); */
3587287d2a3SDmitry Karpeev         }
359bafc1b83SMatthew G Knepley         ierr = ISDestroy(&compField);CHKERRQ(ierr);
360bafc1b83SMatthew G Knepley         for (j = 0; j < nfields; ++j) {
361bafc1b83SMatthew G Knepley           f    = ifields[j];
3627b62db95SJungho Lee           ierr = PetscFree(fieldNames[f]);CHKERRQ(ierr);
3637b62db95SJungho Lee           ierr = ISDestroy(&fields[f]);CHKERRQ(ierr);
3647b62db95SJungho Lee         }
365bafc1b83SMatthew G Knepley       }
366bafc1b83SMatthew G Knepley       if (i == 0) {
367bafc1b83SMatthew G Knepley         for (f = 0; f < numFields; ++f) {
368bafc1b83SMatthew G Knepley           ierr = PCFieldSplitSetIS(pc, fieldNames[f], fields[f]);CHKERRQ(ierr);
369bafc1b83SMatthew G Knepley           ierr = PetscFree(fieldNames[f]);CHKERRQ(ierr);
370bafc1b83SMatthew G Knepley           ierr = ISDestroy(&fields[f]);CHKERRQ(ierr);
371bafc1b83SMatthew G Knepley         }
372bafc1b83SMatthew G Knepley       } else {
373d724dfffSBarry Smith         for (j=0; j<numFields; j++) {
374d724dfffSBarry Smith           ierr = DMDestroy(dms+j);CHKERRQ(ierr);
375d724dfffSBarry Smith         }
376d724dfffSBarry Smith         ierr = PetscFree(dms);CHKERRQ(ierr);
377785e854fSJed Brown         ierr = PetscMalloc1(i, &dms);CHKERRQ(ierr);
3782fa5cd67SKarl Rupp         for (j = 0; j < i; ++j) dms[j] = subdm[j];
379bafc1b83SMatthew G Knepley       }
3807b62db95SJungho Lee       ierr = PetscFree(fieldNames);CHKERRQ(ierr);
3817b62db95SJungho Lee       ierr = PetscFree(fields);CHKERRQ(ierr);
382e7c4fc90SDmitry Karpeev       if (dms) {
3838b8307b2SJed Brown         ierr = PetscInfo(pc, "Setting up physics based fieldsplit preconditioner using the embedded DM\n");CHKERRQ(ierr);
384bafc1b83SMatthew G Knepley         for (ilink = jac->head, i = 0; ilink; ilink = ilink->next, ++i) {
3857287d2a3SDmitry Karpeev           const char *prefix;
3867287d2a3SDmitry Karpeev           ierr = PetscObjectGetOptionsPrefix((PetscObject)(ilink->ksp),&prefix);CHKERRQ(ierr);
3877287d2a3SDmitry Karpeev           ierr = PetscObjectSetOptionsPrefix((PetscObject)(dms[i]), prefix);CHKERRQ(ierr);
3887b62db95SJungho Lee           ierr = KSPSetDM(ilink->ksp, dms[i]);CHKERRQ(ierr);
3897b62db95SJungho Lee           ierr = KSPSetDMActive(ilink->ksp, PETSC_FALSE);CHKERRQ(ierr);
3907287d2a3SDmitry Karpeev           ierr = PetscObjectIncrementTabLevel((PetscObject)dms[i],(PetscObject)ilink->ksp,0);CHKERRQ(ierr);
391e7c4fc90SDmitry Karpeev           ierr = DMDestroy(&dms[i]);CHKERRQ(ierr);
3922fa5ba8aSJed Brown         }
3937b62db95SJungho Lee         ierr = PetscFree(dms);CHKERRQ(ierr);
3948b8307b2SJed Brown       }
39566ffff09SJed Brown     } else {
396521d7252SBarry Smith       if (jac->bs <= 0) {
397704ba839SBarry Smith         if (pc->pmat) {
398521d7252SBarry Smith           ierr = MatGetBlockSize(pc->pmat,&jac->bs);CHKERRQ(ierr);
3992fa5cd67SKarl Rupp         } else jac->bs = 1;
400521d7252SBarry Smith       }
401d32f9abdSBarry Smith 
4026ce1633cSBarry Smith       if (stokes) {
4036ce1633cSBarry Smith         IS       zerodiags,rest;
4046ce1633cSBarry Smith         PetscInt nmin,nmax;
4056ce1633cSBarry Smith 
4066ce1633cSBarry Smith         ierr = MatGetOwnershipRange(pc->mat,&nmin,&nmax);CHKERRQ(ierr);
4076ce1633cSBarry Smith         ierr = MatFindZeroDiagonals(pc->mat,&zerodiags);CHKERRQ(ierr);
4086ce1633cSBarry Smith         ierr = ISComplement(zerodiags,nmin,nmax,&rest);CHKERRQ(ierr);
4097287d2a3SDmitry Karpeev         if (jac->reset) {
4107287d2a3SDmitry Karpeev           jac->head->is       = rest;
4117287d2a3SDmitry Karpeev           jac->head->next->is = zerodiags;
4122fa5cd67SKarl Rupp         } else {
4136ce1633cSBarry Smith           ierr = PCFieldSplitSetIS(pc,"0",rest);CHKERRQ(ierr);
4146ce1633cSBarry Smith           ierr = PCFieldSplitSetIS(pc,"1",zerodiags);CHKERRQ(ierr);
4157287d2a3SDmitry Karpeev         }
416fcfd50ebSBarry Smith         ierr = ISDestroy(&zerodiags);CHKERRQ(ierr);
417fcfd50ebSBarry Smith         ierr = ISDestroy(&rest);CHKERRQ(ierr);
4183a062f41SBarry Smith       } else if (coupling) {
4193a062f41SBarry Smith         IS       coupling,rest;
4203a062f41SBarry Smith         PetscInt nmin,nmax;
4213a062f41SBarry Smith 
4223a062f41SBarry Smith         ierr = MatGetOwnershipRange(pc->mat,&nmin,&nmax);CHKERRQ(ierr);
4233a062f41SBarry Smith         ierr = MatFindOffBlockDiagonalEntries(pc->mat,&coupling);CHKERRQ(ierr);
4246bea0878SBarry Smith         ierr = ISCreateStride(PetscObjectComm((PetscObject)pc->mat),nmax-nmin,nmin,1,&rest);CHKERRQ(ierr);
4253a062f41SBarry Smith         if (jac->reset) {
4263a062f41SBarry Smith           jac->head->is       = coupling;
4273a062f41SBarry Smith           jac->head->next->is = rest;
4283a062f41SBarry Smith         } else {
4293a062f41SBarry Smith           ierr = PCFieldSplitSetIS(pc,"0",coupling);CHKERRQ(ierr);
4303a062f41SBarry Smith           ierr = PCFieldSplitSetIS(pc,"1",rest);CHKERRQ(ierr);
4313a062f41SBarry Smith         }
4323a062f41SBarry Smith         ierr = ISDestroy(&coupling);CHKERRQ(ierr);
4333a062f41SBarry Smith         ierr = ISDestroy(&rest);CHKERRQ(ierr);
4346ce1633cSBarry Smith       } else {
435ce94432eSBarry Smith         if (jac->reset) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Cases not yet handled when PCReset() was used");
4369eeaaa73SJed Brown         ierr = PetscOptionsGetBool(((PetscObject)pc)->prefix,"-pc_fieldsplit_default",&fieldsplit_default,NULL);CHKERRQ(ierr);
4377287d2a3SDmitry Karpeev         if (!fieldsplit_default) {
438d32f9abdSBarry Smith           /* Allow user to set fields from command line,  if bs was known at the time of PCSetFromOptions_FieldSplit()
439d32f9abdSBarry Smith            then it is set there. This is not ideal because we should only have options set in XXSetFromOptions(). */
4406c924f48SJed Brown           ierr = PCFieldSplitSetRuntimeSplits_Private(pc);CHKERRQ(ierr);
4416c924f48SJed Brown           if (jac->splitdefined) {ierr = PetscInfo(pc,"Splits defined using the options database\n");CHKERRQ(ierr);}
442d32f9abdSBarry Smith         }
4437287d2a3SDmitry Karpeev         if (fieldsplit_default || !jac->splitdefined) {
444d32f9abdSBarry Smith           ierr = PetscInfo(pc,"Using default splitting of fields\n");CHKERRQ(ierr);
445db4c96c1SJed Brown           for (i=0; i<jac->bs; i++) {
4466c924f48SJed Brown             char splitname[8];
4478caf3d72SBarry Smith             ierr = PetscSNPrintf(splitname,sizeof(splitname),"%D",i);CHKERRQ(ierr);
4485d4c12cdSJungho Lee             ierr = PCFieldSplitSetFields(pc,splitname,1,&i,&i);CHKERRQ(ierr);
44979416396SBarry Smith           }
4505d4c12cdSJungho Lee           jac->defaultsplit = PETSC_TRUE;
451521d7252SBarry Smith         }
45266ffff09SJed Brown       }
4536ce1633cSBarry Smith     }
454edf189efSBarry Smith   } else if (jac->nsplits == 1) {
455edf189efSBarry Smith     if (ilink->is) {
456edf189efSBarry Smith       IS       is2;
457edf189efSBarry Smith       PetscInt nmin,nmax;
458edf189efSBarry Smith 
459edf189efSBarry Smith       ierr = MatGetOwnershipRange(pc->mat,&nmin,&nmax);CHKERRQ(ierr);
460edf189efSBarry Smith       ierr = ISComplement(ilink->is,nmin,nmax,&is2);CHKERRQ(ierr);
461db4c96c1SJed Brown       ierr = PCFieldSplitSetIS(pc,"1",is2);CHKERRQ(ierr);
462fcfd50ebSBarry Smith       ierr = ISDestroy(&is2);CHKERRQ(ierr);
46382f516ccSBarry Smith     } else SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Must provide at least two sets of fields to PCFieldSplit()");
464edf189efSBarry Smith   }
465d0af7cd3SBarry Smith 
466d0af7cd3SBarry Smith 
46782f516ccSBarry Smith   if (jac->nsplits < 2) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_PLIB,"Unhandled case, must have at least two fields, not %d", jac->nsplits);
46869a612a9SBarry Smith   PetscFunctionReturn(0);
46969a612a9SBarry Smith }
47069a612a9SBarry Smith 
4715a576424SJed Brown PETSC_EXTERN PetscErrorCode PetscOptionsFindPairPrefix_Private(const char pre[], const char name[], char *value[], PetscBool *flg);
472514bf10dSMatthew G Knepley 
47369a612a9SBarry Smith #undef __FUNCT__
47469a612a9SBarry Smith #define __FUNCT__ "PCSetUp_FieldSplit"
47569a612a9SBarry Smith static PetscErrorCode PCSetUp_FieldSplit(PC pc)
47669a612a9SBarry Smith {
47769a612a9SBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
47869a612a9SBarry Smith   PetscErrorCode    ierr;
4795a9f2f41SSatish Balay   PC_FieldSplitLink ilink;
4802c9966d7SBarry Smith   PetscInt          i,nsplit;
4815f4ab4e1SJungho Lee   PetscBool         sorted, sorted_col;
48269a612a9SBarry Smith 
48369a612a9SBarry Smith   PetscFunctionBegin;
48469a612a9SBarry Smith   ierr   = PCFieldSplitSetDefaults(pc);CHKERRQ(ierr);
48597bbdb24SBarry Smith   nsplit = jac->nsplits;
4865a9f2f41SSatish Balay   ilink  = jac->head;
48797bbdb24SBarry Smith 
48897bbdb24SBarry Smith   /* get the matrices for each split */
489704ba839SBarry Smith   if (!jac->issetup) {
4901b9fc7fcSBarry Smith     PetscInt rstart,rend,nslots,bs;
49197bbdb24SBarry Smith 
492704ba839SBarry Smith     jac->issetup = PETSC_TRUE;
493704ba839SBarry Smith 
4945d4c12cdSJungho Lee     /* This is done here instead of in PCFieldSplitSetFields() because may not have matrix at that point */
4952c9966d7SBarry Smith     if (jac->defaultsplit || !ilink->is) {
4962c9966d7SBarry Smith       if (jac->bs <= 0) jac->bs = nsplit;
4972c9966d7SBarry Smith     }
49851f519a2SBarry Smith     bs     = jac->bs;
49997bbdb24SBarry Smith     ierr   = MatGetOwnershipRange(pc->pmat,&rstart,&rend);CHKERRQ(ierr);
5001b9fc7fcSBarry Smith     nslots = (rend - rstart)/bs;
5011b9fc7fcSBarry Smith     for (i=0; i<nsplit; i++) {
5021b9fc7fcSBarry Smith       if (jac->defaultsplit) {
503ce94432eSBarry Smith         ierr = ISCreateStride(PetscObjectComm((PetscObject)pc),nslots,rstart+i,nsplit,&ilink->is);CHKERRQ(ierr);
5045f4ab4e1SJungho Lee         ierr = ISDuplicate(ilink->is,&ilink->is_col);CHKERRQ(ierr);
505704ba839SBarry Smith       } else if (!ilink->is) {
506ccb205f8SBarry Smith         if (ilink->nfields > 1) {
5075f4ab4e1SJungho Lee           PetscInt *ii,*jj,j,k,nfields = ilink->nfields,*fields = ilink->fields,*fields_col = ilink->fields_col;
508785e854fSJed Brown           ierr = PetscMalloc1(ilink->nfields*nslots,&ii);CHKERRQ(ierr);
509785e854fSJed Brown           ierr = PetscMalloc1(ilink->nfields*nslots,&jj);CHKERRQ(ierr);
5101b9fc7fcSBarry Smith           for (j=0; j<nslots; j++) {
5111b9fc7fcSBarry Smith             for (k=0; k<nfields; k++) {
5121b9fc7fcSBarry Smith               ii[nfields*j + k] = rstart + bs*j + fields[k];
5135f4ab4e1SJungho Lee               jj[nfields*j + k] = rstart + bs*j + fields_col[k];
51497bbdb24SBarry Smith             }
51597bbdb24SBarry Smith           }
516ce94432eSBarry Smith           ierr = ISCreateGeneral(PetscObjectComm((PetscObject)pc),nslots*nfields,ii,PETSC_OWN_POINTER,&ilink->is);CHKERRQ(ierr);
517ce94432eSBarry Smith           ierr = ISCreateGeneral(PetscObjectComm((PetscObject)pc),nslots*nfields,jj,PETSC_OWN_POINTER,&ilink->is_col);CHKERRQ(ierr);
518ccb205f8SBarry Smith         } else {
519ce94432eSBarry Smith           ierr = ISCreateStride(PetscObjectComm((PetscObject)pc),nslots,rstart+ilink->fields[0],bs,&ilink->is);CHKERRQ(ierr);
520ce94432eSBarry Smith           ierr = ISCreateStride(PetscObjectComm((PetscObject)pc),nslots,rstart+ilink->fields_col[0],bs,&ilink->is_col);CHKERRQ(ierr);
521ccb205f8SBarry Smith        }
5223e197d65SBarry Smith       }
523704ba839SBarry Smith       ierr = ISSorted(ilink->is,&sorted);CHKERRQ(ierr);
5245f4ab4e1SJungho Lee       if (ilink->is_col) { ierr = ISSorted(ilink->is_col,&sorted_col);CHKERRQ(ierr); }
5255f4ab4e1SJungho Lee       if (!sorted || !sorted_col) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Fields must be sorted when creating split");
526704ba839SBarry Smith       ilink = ilink->next;
5271b9fc7fcSBarry Smith     }
5281b9fc7fcSBarry Smith   }
5291b9fc7fcSBarry Smith 
530704ba839SBarry Smith   ilink = jac->head;
53197bbdb24SBarry Smith   if (!jac->pmat) {
532bdddcaaaSMatthew G Knepley     Vec xtmp;
533bdddcaaaSMatthew G Knepley 
5340298fd71SBarry Smith     ierr = MatGetVecs(pc->pmat,&xtmp,NULL);CHKERRQ(ierr);
535785e854fSJed Brown     ierr = PetscMalloc1(nsplit,&jac->pmat);CHKERRQ(ierr);
536dcca6d9dSJed Brown     ierr = PetscMalloc2(nsplit,&jac->x,nsplit,&jac->y);CHKERRQ(ierr);
537cf502942SBarry Smith     for (i=0; i<nsplit; i++) {
538bdddcaaaSMatthew G Knepley       MatNullSpace sp;
539bdddcaaaSMatthew G Knepley 
540a3df900dSMatthew G Knepley       /* Check for preconditioning matrix attached to IS */
541a3df900dSMatthew G Knepley       ierr = PetscObjectQuery((PetscObject) ilink->is, "pmat", (PetscObject*) &jac->pmat[i]);CHKERRQ(ierr);
542a3df900dSMatthew G Knepley       if (jac->pmat[i]) {
543a3df900dSMatthew G Knepley         ierr = PetscObjectReference((PetscObject) jac->pmat[i]);CHKERRQ(ierr);
544a3df900dSMatthew G Knepley         if (jac->type == PC_COMPOSITE_SCHUR) {
545a3df900dSMatthew G Knepley           jac->schur_user = jac->pmat[i];
5462fa5cd67SKarl Rupp 
547a3df900dSMatthew G Knepley           ierr = PetscObjectReference((PetscObject) jac->schur_user);CHKERRQ(ierr);
548a3df900dSMatthew G Knepley         }
549a3df900dSMatthew G Knepley       } else {
5503a062f41SBarry Smith         const char *prefix;
5515f4ab4e1SJungho Lee         ierr = MatGetSubMatrix(pc->pmat,ilink->is,ilink->is_col,MAT_INITIAL_MATRIX,&jac->pmat[i]);CHKERRQ(ierr);
5523a062f41SBarry Smith         ierr = KSPGetOptionsPrefix(ilink->ksp,&prefix);CHKERRQ(ierr);
5533a062f41SBarry Smith         ierr = MatSetOptionsPrefix(jac->pmat[i],prefix);CHKERRQ(ierr);
5543a062f41SBarry Smith         ierr = MatViewFromOptions(jac->pmat[i],NULL,"-mat_view");CHKERRQ(ierr);
555a3df900dSMatthew G Knepley       }
556bdddcaaaSMatthew G Knepley       /* create work vectors for each split */
557bdddcaaaSMatthew G Knepley       ierr     = MatGetVecs(jac->pmat[i],&jac->x[i],&jac->y[i]);CHKERRQ(ierr);
5580298fd71SBarry Smith       ilink->x = jac->x[i]; ilink->y = jac->y[i]; ilink->z = NULL;
559bdddcaaaSMatthew G Knepley       /* compute scatter contexts needed by multiplicative versions and non-default splits */
5600298fd71SBarry Smith       ierr = VecScatterCreate(xtmp,ilink->is,jac->x[i],NULL,&ilink->sctx);CHKERRQ(ierr);
561ed1f0337SMatthew G Knepley       /* Check for null space attached to IS */
562bafc1b83SMatthew G Knepley       ierr = PetscObjectQuery((PetscObject) ilink->is, "nullspace", (PetscObject*) &sp);CHKERRQ(ierr);
563bafc1b83SMatthew G Knepley       if (sp) {
564bafc1b83SMatthew G Knepley         ierr = MatSetNullSpace(jac->pmat[i], sp);CHKERRQ(ierr);
565bafc1b83SMatthew G Knepley       }
566ed1f0337SMatthew G Knepley       ierr = PetscObjectQuery((PetscObject) ilink->is, "nearnullspace", (PetscObject*) &sp);CHKERRQ(ierr);
567ed1f0337SMatthew G Knepley       if (sp) {
568ed1f0337SMatthew G Knepley         ierr = MatSetNearNullSpace(jac->pmat[i], sp);CHKERRQ(ierr);
569ed1f0337SMatthew G Knepley       }
570704ba839SBarry Smith       ilink = ilink->next;
571cf502942SBarry Smith     }
572bdddcaaaSMatthew G Knepley     ierr = VecDestroy(&xtmp);CHKERRQ(ierr);
57397bbdb24SBarry Smith   } else {
574cf502942SBarry Smith     for (i=0; i<nsplit; i++) {
575a3df900dSMatthew G Knepley       Mat pmat;
576a3df900dSMatthew G Knepley 
577a3df900dSMatthew G Knepley       /* Check for preconditioning matrix attached to IS */
578a3df900dSMatthew G Knepley       ierr = PetscObjectQuery((PetscObject) ilink->is, "pmat", (PetscObject*) &pmat);CHKERRQ(ierr);
579a3df900dSMatthew G Knepley       if (!pmat) {
5805f4ab4e1SJungho Lee         ierr = MatGetSubMatrix(pc->pmat,ilink->is,ilink->is_col,MAT_REUSE_MATRIX,&jac->pmat[i]);CHKERRQ(ierr);
581a3df900dSMatthew G Knepley       }
582704ba839SBarry Smith       ilink = ilink->next;
583cf502942SBarry Smith     }
58497bbdb24SBarry Smith   }
585f5236f50SJed Brown   if (pc->useAmat) {
586519d70e2SJed Brown     ilink = jac->head;
587519d70e2SJed Brown     if (!jac->mat) {
588785e854fSJed Brown       ierr = PetscMalloc1(nsplit,&jac->mat);CHKERRQ(ierr);
589519d70e2SJed Brown       for (i=0; i<nsplit; i++) {
5905f4ab4e1SJungho Lee         ierr  = MatGetSubMatrix(pc->mat,ilink->is,ilink->is_col,MAT_INITIAL_MATRIX,&jac->mat[i]);CHKERRQ(ierr);
591519d70e2SJed Brown         ilink = ilink->next;
592519d70e2SJed Brown       }
593519d70e2SJed Brown     } else {
594519d70e2SJed Brown       for (i=0; i<nsplit; i++) {
5955f4ab4e1SJungho Lee         if (jac->mat[i]) {ierr = MatGetSubMatrix(pc->mat,ilink->is,ilink->is_col,MAT_REUSE_MATRIX,&jac->mat[i]);CHKERRQ(ierr);}
596519d70e2SJed Brown         ilink = ilink->next;
597519d70e2SJed Brown       }
598519d70e2SJed Brown     }
599519d70e2SJed Brown   } else {
600519d70e2SJed Brown     jac->mat = jac->pmat;
601519d70e2SJed Brown   }
60297bbdb24SBarry Smith 
6036c8605c2SJed Brown   if (jac->type != PC_COMPOSITE_ADDITIVE  && jac->type != PC_COMPOSITE_SCHUR) {
60468dd23aaSBarry Smith     /* extract the rows of the matrix associated with each field: used for efficient computation of residual inside algorithm */
60568dd23aaSBarry Smith     ilink = jac->head;
60668dd23aaSBarry Smith     if (!jac->Afield) {
607785e854fSJed Brown       ierr = PetscMalloc1(nsplit,&jac->Afield);CHKERRQ(ierr);
60868dd23aaSBarry Smith       for (i=0; i<nsplit; i++) {
6090298fd71SBarry Smith         ierr  = MatGetSubMatrix(pc->mat,ilink->is,NULL,MAT_INITIAL_MATRIX,&jac->Afield[i]);CHKERRQ(ierr);
61068dd23aaSBarry Smith         ilink = ilink->next;
61168dd23aaSBarry Smith       }
61268dd23aaSBarry Smith     } else {
61368dd23aaSBarry Smith       for (i=0; i<nsplit; i++) {
6140298fd71SBarry Smith         ierr  = MatGetSubMatrix(pc->mat,ilink->is,NULL,MAT_REUSE_MATRIX,&jac->Afield[i]);CHKERRQ(ierr);
61568dd23aaSBarry Smith         ilink = ilink->next;
61668dd23aaSBarry Smith       }
61768dd23aaSBarry Smith     }
61868dd23aaSBarry Smith   }
61968dd23aaSBarry Smith 
6203b224e63SBarry Smith   if (jac->type == PC_COMPOSITE_SCHUR) {
6213b224e63SBarry Smith     IS          ccis;
6224aa3045dSJed Brown     PetscInt    rstart,rend;
623093c86ffSJed Brown     char        lscname[256];
624093c86ffSJed Brown     PetscObject LSC_L;
625ce94432eSBarry Smith 
626ce94432eSBarry Smith     if (nsplit != 2) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_INCOMP,"To use Schur complement preconditioner you must have exactly 2 fields");
62768dd23aaSBarry Smith 
628e6cab6aaSJed Brown     /* When extracting off-diagonal submatrices, we take complements from this range */
629e6cab6aaSJed Brown     ierr = MatGetOwnershipRangeColumn(pc->mat,&rstart,&rend);CHKERRQ(ierr);
630e6cab6aaSJed Brown 
6313b224e63SBarry Smith     /* need to handle case when one is resetting up the preconditioner */
6323b224e63SBarry Smith     if (jac->schur) {
6330298fd71SBarry Smith       KSP kspA = jac->head->ksp, kspInner = NULL, kspUpper = jac->kspupper;
634443836d0SMatthew G Knepley 
635fb3147dbSMatthew G Knepley       ierr  = MatSchurComplementGetKSP(jac->schur, &kspInner);CHKERRQ(ierr);
6363b224e63SBarry Smith       ilink = jac->head;
63749bb4cd7SJungho Lee       ierr  = ISComplement(ilink->is_col,rstart,rend,&ccis);CHKERRQ(ierr);
6384aa3045dSJed Brown       ierr  = MatGetSubMatrix(pc->mat,ilink->is,ccis,MAT_REUSE_MATRIX,&jac->B);CHKERRQ(ierr);
639fcfd50ebSBarry Smith       ierr  = ISDestroy(&ccis);CHKERRQ(ierr);
6403b224e63SBarry Smith       ilink = ilink->next;
64149bb4cd7SJungho Lee       ierr  = ISComplement(ilink->is_col,rstart,rend,&ccis);CHKERRQ(ierr);
6424aa3045dSJed Brown       ierr  = MatGetSubMatrix(pc->mat,ilink->is,ccis,MAT_REUSE_MATRIX,&jac->C);CHKERRQ(ierr);
643fcfd50ebSBarry Smith       ierr  = ISDestroy(&ccis);CHKERRQ(ierr);
644*aa6c7ce3SBarry Smith       ierr  = MatSchurComplementUpdateSubMatrices(jac->schur,jac->mat[0],jac->pmat[0],jac->B,jac->C,jac->mat[1]);CHKERRQ(ierr);
645a7476a74SDmitry Karpeev       if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELFP) {
646a7476a74SDmitry Karpeev 	ierr = MatDestroy(&jac->schurp);CHKERRQ(ierr);
647a7476a74SDmitry Karpeev 	ierr = MatSchurComplementGetPmat(jac->schur,MAT_INITIAL_MATRIX,&jac->schurp);CHKERRQ(ierr);
648a7476a74SDmitry Karpeev       }
649443836d0SMatthew G Knepley       if (kspA != kspInner) {
65023ee1639SBarry Smith         ierr = KSPSetOperators(kspA,jac->mat[0],jac->pmat[0]);CHKERRQ(ierr);
651443836d0SMatthew G Knepley       }
652443836d0SMatthew G Knepley       if (kspUpper != kspA) {
65323ee1639SBarry Smith         ierr = KSPSetOperators(kspUpper,jac->mat[0],jac->pmat[0]);CHKERRQ(ierr);
654443836d0SMatthew G Knepley       }
65523ee1639SBarry Smith       ierr = KSPSetOperators(jac->kspschur,jac->schur,FieldSplitSchurPre(jac));CHKERRQ(ierr);
6563b224e63SBarry Smith     } else {
657bafc1b83SMatthew G Knepley       const char   *Dprefix;
658470b340bSDmitry Karpeev       char         schurprefix[256], schurmatprefix[256];
659514bf10dSMatthew G Knepley       char         schurtestoption[256];
660bdddcaaaSMatthew G Knepley       MatNullSpace sp;
661514bf10dSMatthew G Knepley       PetscBool    flg;
6623b224e63SBarry Smith 
663a04f6461SBarry Smith       /* extract the A01 and A10 matrices */
6643b224e63SBarry Smith       ilink = jac->head;
66549bb4cd7SJungho Lee       ierr  = ISComplement(ilink->is_col,rstart,rend,&ccis);CHKERRQ(ierr);
6664aa3045dSJed Brown       ierr  = MatGetSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->B);CHKERRQ(ierr);
667fcfd50ebSBarry Smith       ierr  = ISDestroy(&ccis);CHKERRQ(ierr);
6683b224e63SBarry Smith       ilink = ilink->next;
66949bb4cd7SJungho Lee       ierr  = ISComplement(ilink->is_col,rstart,rend,&ccis);CHKERRQ(ierr);
6704aa3045dSJed Brown       ierr  = MatGetSubMatrix(pc->mat,ilink->is,ccis,MAT_INITIAL_MATRIX,&jac->C);CHKERRQ(ierr);
671fcfd50ebSBarry Smith       ierr  = ISDestroy(&ccis);CHKERRQ(ierr);
67220252d06SBarry Smith 
673f5236f50SJed Brown       /* Use mat[0] (diagonal block of Amat) preconditioned by pmat[0] to define Schur complement */
67420252d06SBarry Smith       ierr = MatCreate(((PetscObject)jac->mat[0])->comm,&jac->schur);CHKERRQ(ierr);
67520252d06SBarry Smith       ierr = MatSetType(jac->schur,MATSCHURCOMPLEMENT);CHKERRQ(ierr);
676bee83525SDmitry Karpeev       ierr = MatSchurComplementSetSubMatrices(jac->schur,jac->mat[0],jac->pmat[0],jac->B,jac->C,jac->mat[1]);CHKERRQ(ierr);
677470b340bSDmitry Karpeev       ierr = PetscSNPrintf(schurmatprefix, sizeof(schurmatprefix), "%sfieldsplit_%s_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname);CHKERRQ(ierr);
678470b340bSDmitry Karpeev       /* Note that the inner KSP is NOT going to inherit this prefix, and if it did, it would be reset just below.  Is that what we want? */
679470b340bSDmitry Karpeev       ierr = MatSetOptionsPrefix(jac->schur,schurmatprefix);CHKERRQ(ierr);
680470b340bSDmitry Karpeev       ierr = MatSetFromOptions(jac->schur);CHKERRQ(ierr);
681bdddcaaaSMatthew G Knepley       ierr = MatGetNullSpace(jac->pmat[1], &sp);CHKERRQ(ierr);
68220252d06SBarry Smith       if (sp) {
68320252d06SBarry Smith         ierr = MatSetNullSpace(jac->schur, sp);CHKERRQ(ierr);
68420252d06SBarry Smith       }
68520252d06SBarry Smith 
68620252d06SBarry Smith       ierr = PetscSNPrintf(schurtestoption, sizeof(schurtestoption), "-fieldsplit_%s_inner_", ilink->splitname);CHKERRQ(ierr);
6870298fd71SBarry Smith       ierr = PetscOptionsFindPairPrefix_Private(((PetscObject)pc)->prefix, schurtestoption, NULL, &flg);CHKERRQ(ierr);
688514bf10dSMatthew G Knepley       if (flg) {
689514bf10dSMatthew G Knepley         DM  dmInner;
69021635b76SJed Brown         KSP kspInner;
69121635b76SJed Brown 
69221635b76SJed Brown         ierr = MatSchurComplementGetKSP(jac->schur, &kspInner);CHKERRQ(ierr);
69321635b76SJed Brown         ierr = PetscSNPrintf(schurprefix, sizeof(schurprefix), "%sfieldsplit_%s_inner_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname);CHKERRQ(ierr);
69421635b76SJed Brown         /* Indent this deeper to emphasize the "inner" nature of this solver. */
69521635b76SJed Brown         ierr = PetscObjectIncrementTabLevel((PetscObject)kspInner, (PetscObject) pc, 2);CHKERRQ(ierr);
69621635b76SJed Brown         ierr = KSPSetOptionsPrefix(kspInner, schurprefix);CHKERRQ(ierr);
697514bf10dSMatthew G Knepley 
698514bf10dSMatthew G Knepley         /* Set DM for new solver */
699bafc1b83SMatthew G Knepley         ierr = KSPGetDM(jac->head->ksp, &dmInner);CHKERRQ(ierr);
70021635b76SJed Brown         ierr = KSPSetDM(kspInner, dmInner);CHKERRQ(ierr);
70121635b76SJed Brown         ierr = KSPSetDMActive(kspInner, PETSC_FALSE);CHKERRQ(ierr);
702514bf10dSMatthew G Knepley       } else {
70321635b76SJed Brown          /* Use the outer solver for the inner solve, but revert the KSPPREONLY from PCFieldSplitSetFields_FieldSplit or
70421635b76SJed Brown           * PCFieldSplitSetIS_FieldSplit. We don't want KSPPREONLY because it makes the Schur complement inexact,
70521635b76SJed Brown           * preventing Schur complement reduction to be an accurate solve. Usually when an iterative solver is used for
70621635b76SJed Brown           * S = D - C A_inner^{-1} B, we expect S to be defined using an accurate definition of A_inner^{-1}, so we make
70721635b76SJed Brown           * GMRES the default. Note that it is also common to use PREONLY for S, in which case S may not be used
70821635b76SJed Brown           * directly, and the user is responsible for setting an inexact method for fieldsplit's A^{-1}. */
70921635b76SJed Brown         ierr = KSPSetType(jac->head->ksp,KSPGMRES);CHKERRQ(ierr);
710514bf10dSMatthew G Knepley         ierr = MatSchurComplementSetKSP(jac->schur,jac->head->ksp);CHKERRQ(ierr);
711bafc1b83SMatthew G Knepley       }
71223ee1639SBarry Smith       ierr = KSPSetOperators(jac->head->ksp,jac->mat[0],jac->pmat[0]);CHKERRQ(ierr);
7135a9f2f41SSatish Balay       ierr = KSPSetFromOptions(jac->head->ksp);CHKERRQ(ierr);
71497bbdb24SBarry Smith       ierr = MatSetFromOptions(jac->schur);CHKERRQ(ierr);
71597bbdb24SBarry Smith 
716443836d0SMatthew G Knepley       ierr = PetscSNPrintf(schurtestoption, sizeof(schurtestoption), "-fieldsplit_%s_upper_", ilink->splitname);CHKERRQ(ierr);
7170298fd71SBarry Smith       ierr = PetscOptionsFindPairPrefix_Private(((PetscObject)pc)->prefix, schurtestoption, NULL, &flg);CHKERRQ(ierr);
718443836d0SMatthew G Knepley       if (flg) {
719443836d0SMatthew G Knepley         DM dmInner;
720443836d0SMatthew G Knepley 
721443836d0SMatthew G Knepley         ierr = PetscSNPrintf(schurprefix, sizeof(schurprefix), "%sfieldsplit_%s_upper_", ((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "", ilink->splitname);CHKERRQ(ierr);
72282f516ccSBarry Smith         ierr = KSPCreate(PetscObjectComm((PetscObject)pc), &jac->kspupper);CHKERRQ(ierr);
723443836d0SMatthew G Knepley         ierr = KSPSetOptionsPrefix(jac->kspupper, schurprefix);CHKERRQ(ierr);
724443836d0SMatthew G Knepley         ierr = KSPGetDM(jac->head->ksp, &dmInner);CHKERRQ(ierr);
725443836d0SMatthew G Knepley         ierr = KSPSetDM(jac->kspupper, dmInner);CHKERRQ(ierr);
726443836d0SMatthew G Knepley         ierr = KSPSetDMActive(jac->kspupper, PETSC_FALSE);CHKERRQ(ierr);
727443836d0SMatthew G Knepley         ierr = KSPSetFromOptions(jac->kspupper);CHKERRQ(ierr);
72823ee1639SBarry Smith         ierr = KSPSetOperators(jac->kspupper,jac->mat[0],jac->pmat[0]);CHKERRQ(ierr);
729443836d0SMatthew G Knepley         ierr = VecDuplicate(jac->head->x, &jac->head->z);CHKERRQ(ierr);
730443836d0SMatthew G Knepley       } else {
731443836d0SMatthew G Knepley         jac->kspupper = jac->head->ksp;
732443836d0SMatthew G Knepley         ierr          = PetscObjectReference((PetscObject) jac->head->ksp);CHKERRQ(ierr);
733443836d0SMatthew G Knepley       }
734443836d0SMatthew G Knepley 
735a7476a74SDmitry Karpeev       if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELFP) {
736a7476a74SDmitry Karpeev 	ierr = MatSchurComplementGetPmat(jac->schur,MAT_INITIAL_MATRIX,&jac->schurp);CHKERRQ(ierr);
737a7476a74SDmitry Karpeev       }
738ce94432eSBarry Smith       ierr = KSPCreate(PetscObjectComm((PetscObject)pc),&jac->kspschur);CHKERRQ(ierr);
73997bbdb24SBarry Smith       ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)jac->kspschur);CHKERRQ(ierr);
74020252d06SBarry Smith       ierr = PetscObjectIncrementTabLevel((PetscObject)jac->kspschur,(PetscObject)pc,1);CHKERRQ(ierr);
74197bbdb24SBarry Smith       if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_SELF) {
7427233a360SDmitry Karpeev         PC pcschur;
7437233a360SDmitry Karpeev         ierr = KSPGetPC(jac->kspschur,&pcschur);CHKERRQ(ierr);
7447233a360SDmitry Karpeev         ierr = PCSetType(pcschur,PCNONE);CHKERRQ(ierr);
74597bbdb24SBarry Smith         /* Note: This is bad if there exist preconditioners for MATSCHURCOMPLEMENT */
746e74569cdSMatthew G. Knepley       } else if (jac->schurpre == PC_FIELDSPLIT_SCHUR_PRE_FULL) {
747e74569cdSMatthew G. Knepley         ierr = MatSchurComplementComputeExplicitOperator(jac->schur, &jac->schur_user);CHKERRQ(ierr);
74897bbdb24SBarry Smith       }
74923ee1639SBarry Smith       ierr = KSPSetOperators(jac->kspschur,jac->schur,FieldSplitSchurPre(jac));CHKERRQ(ierr);
75097bbdb24SBarry Smith       ierr = KSPGetOptionsPrefix(jac->head->next->ksp, &Dprefix);CHKERRQ(ierr);
75197bbdb24SBarry Smith       ierr = KSPSetOptionsPrefix(jac->kspschur,         Dprefix);CHKERRQ(ierr);
752b20b4189SMatthew G. Knepley       /* propogate DM */
753b20b4189SMatthew G. Knepley       {
754b20b4189SMatthew G. Knepley         DM sdm;
755b20b4189SMatthew G. Knepley         ierr = KSPGetDM(jac->head->next->ksp, &sdm);CHKERRQ(ierr);
756b20b4189SMatthew G. Knepley         if (sdm) {
757b20b4189SMatthew G. Knepley           ierr = KSPSetDM(jac->kspschur, sdm);CHKERRQ(ierr);
758b20b4189SMatthew G. Knepley           ierr = KSPSetDMActive(jac->kspschur, PETSC_FALSE);CHKERRQ(ierr);
759b20b4189SMatthew G. Knepley         }
760b20b4189SMatthew G. Knepley       }
76197bbdb24SBarry Smith       /* really want setfromoptions called in PCSetFromOptions_FieldSplit(), but it is not ready yet */
76297bbdb24SBarry Smith       /* need to call this every time, since the jac->kspschur is freshly created, otherwise its options never get set */
76397bbdb24SBarry Smith       ierr = KSPSetFromOptions(jac->kspschur);CHKERRQ(ierr);
76497bbdb24SBarry Smith     }
76597bbdb24SBarry Smith 
7665a9f2f41SSatish Balay     /* HACK: special support to forward L and Lp matrices that might be used by PCLSC */
7678caf3d72SBarry Smith     ierr = PetscSNPrintf(lscname,sizeof(lscname),"%s_LSC_L",ilink->splitname);CHKERRQ(ierr);
768519d70e2SJed Brown     ierr = PetscObjectQuery((PetscObject)pc->mat,lscname,(PetscObject*)&LSC_L);CHKERRQ(ierr);
7693b224e63SBarry Smith     if (!LSC_L) {ierr = PetscObjectQuery((PetscObject)pc->pmat,lscname,(PetscObject*)&LSC_L);CHKERRQ(ierr);}
770c1570756SJed Brown     if (LSC_L) {ierr = PetscObjectCompose((PetscObject)jac->schur,"LSC_L",(PetscObject)LSC_L);CHKERRQ(ierr);}
7718caf3d72SBarry Smith     ierr = PetscSNPrintf(lscname,sizeof(lscname),"%s_LSC_Lp",ilink->splitname);CHKERRQ(ierr);
77297bbdb24SBarry Smith     ierr = PetscObjectQuery((PetscObject)pc->pmat,lscname,(PetscObject*)&LSC_L);CHKERRQ(ierr);
7735a9f2f41SSatish Balay     if (!LSC_L) {ierr = PetscObjectQuery((PetscObject)pc->mat,lscname,(PetscObject*)&LSC_L);CHKERRQ(ierr);}
7740971522cSBarry Smith     if (LSC_L) {ierr = PetscObjectCompose((PetscObject)jac->schur,"LSC_Lp",(PetscObject)LSC_L);CHKERRQ(ierr);}
77597bbdb24SBarry Smith   } else {
77668bd789dSDmitry Karpeev     /* set up the individual splits' PCs */
7770971522cSBarry Smith     i     = 0;
7780971522cSBarry Smith     ilink = jac->head;
7790971522cSBarry Smith     while (ilink) {
78023ee1639SBarry Smith       ierr = KSPSetOperators(ilink->ksp,jac->mat[i],jac->pmat[i]);CHKERRQ(ierr);
7810971522cSBarry Smith       /* really want setfromoptions called in PCSetFromOptions_FieldSplit(), but it is not ready yet */
7820971522cSBarry Smith       if (!jac->suboptionsset) {ierr = KSPSetFromOptions(ilink->ksp);CHKERRQ(ierr);}
7830971522cSBarry Smith       i++;
7840971522cSBarry Smith       ilink = ilink->next;
7850971522cSBarry Smith     }
7863b224e63SBarry Smith   }
7873b224e63SBarry Smith 
788c1570756SJed Brown   jac->suboptionsset = PETSC_TRUE;
7890971522cSBarry Smith   PetscFunctionReturn(0);
7900971522cSBarry Smith }
7910971522cSBarry Smith 
7925a9f2f41SSatish Balay #define FieldSplitSplitSolveAdd(ilink,xx,yy) \
793ca9f406cSSatish Balay   (VecScatterBegin(ilink->sctx,xx,ilink->x,INSERT_VALUES,SCATTER_FORWARD) || \
794ca9f406cSSatish Balay    VecScatterEnd(ilink->sctx,xx,ilink->x,INSERT_VALUES,SCATTER_FORWARD) || \
7955a9f2f41SSatish Balay    KSPSolve(ilink->ksp,ilink->x,ilink->y) || \
796ca9f406cSSatish Balay    VecScatterBegin(ilink->sctx,ilink->y,yy,ADD_VALUES,SCATTER_REVERSE) || \
797ca9f406cSSatish Balay    VecScatterEnd(ilink->sctx,ilink->y,yy,ADD_VALUES,SCATTER_REVERSE))
79879416396SBarry Smith 
7990971522cSBarry Smith #undef __FUNCT__
8003b224e63SBarry Smith #define __FUNCT__ "PCApply_FieldSplit_Schur"
8013b224e63SBarry Smith static PetscErrorCode PCApply_FieldSplit_Schur(PC pc,Vec x,Vec y)
8023b224e63SBarry Smith {
8033b224e63SBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
8043b224e63SBarry Smith   PetscErrorCode    ierr;
8053b224e63SBarry Smith   PC_FieldSplitLink ilinkA = jac->head, ilinkD = ilinkA->next;
806443836d0SMatthew G Knepley   KSP               kspA   = ilinkA->ksp, kspLower = kspA, kspUpper = jac->kspupper;
8073b224e63SBarry Smith 
8083b224e63SBarry Smith   PetscFunctionBegin;
809c5d2311dSJed Brown   switch (jac->schurfactorization) {
810c9c6ffaaSJed Brown   case PC_FIELDSPLIT_SCHUR_FACT_DIAG:
811a04f6461SBarry Smith     /* [A00 0; 0 -S], positive definite, suitable for MINRES */
812c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
813c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
814c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
815443836d0SMatthew G Knepley     ierr = KSPSolve(kspA,ilinkA->x,ilinkA->y);CHKERRQ(ierr);
816c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
817c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
818c5d2311dSJed Brown     ierr = KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);CHKERRQ(ierr);
819c5d2311dSJed Brown     ierr = VecScale(ilinkD->y,-1.);CHKERRQ(ierr);
820c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
821c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
822c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
823c5d2311dSJed Brown     break;
824c9c6ffaaSJed Brown   case PC_FIELDSPLIT_SCHUR_FACT_LOWER:
825a04f6461SBarry Smith     /* [A00 0; A10 S], suitable for left preconditioning */
826c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
827c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
828443836d0SMatthew G Knepley     ierr = KSPSolve(kspA,ilinkA->x,ilinkA->y);CHKERRQ(ierr);
829c5d2311dSJed Brown     ierr = MatMult(jac->C,ilinkA->y,ilinkD->x);CHKERRQ(ierr);
830c5d2311dSJed Brown     ierr = VecScale(ilinkD->x,-1.);CHKERRQ(ierr);
831c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
832c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
833c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
834c5d2311dSJed Brown     ierr = KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);CHKERRQ(ierr);
835c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
836c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
837c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
838c5d2311dSJed Brown     break;
839c9c6ffaaSJed Brown   case PC_FIELDSPLIT_SCHUR_FACT_UPPER:
840a04f6461SBarry Smith     /* [A00 A01; 0 S], suitable for right preconditioning */
841c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
842c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkD->sctx,x,ilinkD->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
843c5d2311dSJed Brown     ierr = KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);CHKERRQ(ierr);
844c5d2311dSJed Brown     ierr = MatMult(jac->B,ilinkD->y,ilinkA->x);CHKERRQ(ierr);
845c5d2311dSJed Brown     ierr = VecScale(ilinkA->x,-1.);CHKERRQ(ierr);
846c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkA->sctx,x,ilinkA->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
847c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
848c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkA->sctx,x,ilinkA->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
849443836d0SMatthew G Knepley     ierr = KSPSolve(kspA,ilinkA->x,ilinkA->y);CHKERRQ(ierr);
850c5d2311dSJed Brown     ierr = VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
851c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
852c5d2311dSJed Brown     ierr = VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
853c5d2311dSJed Brown     break;
854c9c6ffaaSJed Brown   case PC_FIELDSPLIT_SCHUR_FACT_FULL:
855a04f6461SBarry 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 */
8563b224e63SBarry Smith     ierr = VecScatterBegin(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
8573b224e63SBarry Smith     ierr = VecScatterEnd(ilinkA->sctx,x,ilinkA->x,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
858443836d0SMatthew G Knepley     ierr = KSPSolve(kspLower,ilinkA->x,ilinkA->y);CHKERRQ(ierr);
8593b224e63SBarry Smith     ierr = MatMult(jac->C,ilinkA->y,ilinkD->x);CHKERRQ(ierr);
8603b224e63SBarry Smith     ierr = VecScale(ilinkD->x,-1.0);CHKERRQ(ierr);
8613b224e63SBarry Smith     ierr = VecScatterBegin(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
8623b224e63SBarry Smith     ierr = VecScatterEnd(ilinkD->sctx,x,ilinkD->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
8633b224e63SBarry Smith 
8643b224e63SBarry Smith     ierr = KSPSolve(jac->kspschur,ilinkD->x,ilinkD->y);CHKERRQ(ierr);
8653b224e63SBarry Smith     ierr = VecScatterBegin(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
8663b224e63SBarry Smith     ierr = VecScatterEnd(ilinkD->sctx,ilinkD->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
8673b224e63SBarry Smith 
868443836d0SMatthew G Knepley     if (kspUpper == kspA) {
8693b224e63SBarry Smith       ierr = MatMult(jac->B,ilinkD->y,ilinkA->y);CHKERRQ(ierr);
8703b224e63SBarry Smith       ierr = VecAXPY(ilinkA->x,-1.0,ilinkA->y);CHKERRQ(ierr);
871443836d0SMatthew G Knepley       ierr = KSPSolve(kspA,ilinkA->x,ilinkA->y);CHKERRQ(ierr);
872443836d0SMatthew G Knepley     } else {
873443836d0SMatthew G Knepley       ierr = KSPSolve(kspA,ilinkA->x,ilinkA->y);CHKERRQ(ierr);
874443836d0SMatthew G Knepley       ierr = MatMult(jac->B,ilinkD->y,ilinkA->x);CHKERRQ(ierr);
875443836d0SMatthew G Knepley       ierr = KSPSolve(kspUpper,ilinkA->x,ilinkA->z);CHKERRQ(ierr);
876443836d0SMatthew G Knepley       ierr = VecAXPY(ilinkA->y,-1.0,ilinkA->z);CHKERRQ(ierr);
877443836d0SMatthew G Knepley     }
8783b224e63SBarry Smith     ierr = VecScatterBegin(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
8793b224e63SBarry Smith     ierr = VecScatterEnd(ilinkA->sctx,ilinkA->y,y,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
880c5d2311dSJed Brown   }
8813b224e63SBarry Smith   PetscFunctionReturn(0);
8823b224e63SBarry Smith }
8833b224e63SBarry Smith 
8843b224e63SBarry Smith #undef __FUNCT__
8850971522cSBarry Smith #define __FUNCT__ "PCApply_FieldSplit"
8860971522cSBarry Smith static PetscErrorCode PCApply_FieldSplit(PC pc,Vec x,Vec y)
8870971522cSBarry Smith {
8880971522cSBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
8890971522cSBarry Smith   PetscErrorCode    ierr;
8905a9f2f41SSatish Balay   PC_FieldSplitLink ilink = jac->head;
891939b8a20SBarry Smith   PetscInt          cnt,bs;
8920971522cSBarry Smith 
8930971522cSBarry Smith   PetscFunctionBegin;
89479416396SBarry Smith   if (jac->type == PC_COMPOSITE_ADDITIVE) {
8951b9fc7fcSBarry Smith     if (jac->defaultsplit) {
896939b8a20SBarry Smith       ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
897ce94432eSBarry 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);
898939b8a20SBarry Smith       ierr = VecGetBlockSize(y,&bs);CHKERRQ(ierr);
899ce94432eSBarry 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);
9000971522cSBarry Smith       ierr = VecStrideGatherAll(x,jac->x,INSERT_VALUES);CHKERRQ(ierr);
9015a9f2f41SSatish Balay       while (ilink) {
9025a9f2f41SSatish Balay         ierr  = KSPSolve(ilink->ksp,ilink->x,ilink->y);CHKERRQ(ierr);
9035a9f2f41SSatish Balay         ilink = ilink->next;
9040971522cSBarry Smith       }
9050971522cSBarry Smith       ierr = VecStrideScatterAll(jac->y,y,INSERT_VALUES);CHKERRQ(ierr);
9061b9fc7fcSBarry Smith     } else {
907efb30889SBarry Smith       ierr = VecSet(y,0.0);CHKERRQ(ierr);
9085a9f2f41SSatish Balay       while (ilink) {
9095a9f2f41SSatish Balay         ierr  = FieldSplitSplitSolveAdd(ilink,x,y);CHKERRQ(ierr);
9105a9f2f41SSatish Balay         ilink = ilink->next;
9111b9fc7fcSBarry Smith       }
9121b9fc7fcSBarry Smith     }
91316913363SBarry Smith   } else if (jac->type == PC_COMPOSITE_MULTIPLICATIVE || jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
91479416396SBarry Smith     if (!jac->w1) {
91579416396SBarry Smith       ierr = VecDuplicate(x,&jac->w1);CHKERRQ(ierr);
91679416396SBarry Smith       ierr = VecDuplicate(x,&jac->w2);CHKERRQ(ierr);
91779416396SBarry Smith     }
918efb30889SBarry Smith     ierr = VecSet(y,0.0);CHKERRQ(ierr);
9195a9f2f41SSatish Balay     ierr = FieldSplitSplitSolveAdd(ilink,x,y);CHKERRQ(ierr);
9203e197d65SBarry Smith     cnt  = 1;
9215a9f2f41SSatish Balay     while (ilink->next) {
9225a9f2f41SSatish Balay       ilink = ilink->next;
9233e197d65SBarry Smith       /* compute the residual only over the part of the vector needed */
9243e197d65SBarry Smith       ierr = MatMult(jac->Afield[cnt++],y,ilink->x);CHKERRQ(ierr);
9253e197d65SBarry Smith       ierr = VecScale(ilink->x,-1.0);CHKERRQ(ierr);
9263e197d65SBarry Smith       ierr = VecScatterBegin(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
9273e197d65SBarry Smith       ierr = VecScatterEnd(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
9283e197d65SBarry Smith       ierr = KSPSolve(ilink->ksp,ilink->x,ilink->y);CHKERRQ(ierr);
9293e197d65SBarry Smith       ierr = VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
9303e197d65SBarry Smith       ierr = VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
9313e197d65SBarry Smith     }
93251f519a2SBarry Smith     if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
93311755939SBarry Smith       cnt -= 2;
93451f519a2SBarry Smith       while (ilink->previous) {
93551f519a2SBarry Smith         ilink = ilink->previous;
93611755939SBarry Smith         /* compute the residual only over the part of the vector needed */
93711755939SBarry Smith         ierr = MatMult(jac->Afield[cnt--],y,ilink->x);CHKERRQ(ierr);
93811755939SBarry Smith         ierr = VecScale(ilink->x,-1.0);CHKERRQ(ierr);
93911755939SBarry Smith         ierr = VecScatterBegin(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
94011755939SBarry Smith         ierr = VecScatterEnd(ilink->sctx,x,ilink->x,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
94111755939SBarry Smith         ierr = KSPSolve(ilink->ksp,ilink->x,ilink->y);CHKERRQ(ierr);
94211755939SBarry Smith         ierr = VecScatterBegin(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
94311755939SBarry Smith         ierr = VecScatterEnd(ilink->sctx,ilink->y,y,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
94451f519a2SBarry Smith       }
94511755939SBarry Smith     }
946ce94432eSBarry Smith   } else SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Unsupported or unknown composition",(int) jac->type);
9470971522cSBarry Smith   PetscFunctionReturn(0);
9480971522cSBarry Smith }
9490971522cSBarry Smith 
950421e10b8SBarry Smith #define FieldSplitSplitSolveAddTranspose(ilink,xx,yy) \
951ca9f406cSSatish Balay   (VecScatterBegin(ilink->sctx,xx,ilink->y,INSERT_VALUES,SCATTER_FORWARD) || \
952ca9f406cSSatish Balay    VecScatterEnd(ilink->sctx,xx,ilink->y,INSERT_VALUES,SCATTER_FORWARD) || \
953421e10b8SBarry Smith    KSPSolveTranspose(ilink->ksp,ilink->y,ilink->x) || \
954ca9f406cSSatish Balay    VecScatterBegin(ilink->sctx,ilink->x,yy,ADD_VALUES,SCATTER_REVERSE) || \
955ca9f406cSSatish Balay    VecScatterEnd(ilink->sctx,ilink->x,yy,ADD_VALUES,SCATTER_REVERSE))
956421e10b8SBarry Smith 
957421e10b8SBarry Smith #undef __FUNCT__
9588c03b21aSDmitry Karpeev #define __FUNCT__ "PCApplyTranspose_FieldSplit"
959421e10b8SBarry Smith static PetscErrorCode PCApplyTranspose_FieldSplit(PC pc,Vec x,Vec y)
960421e10b8SBarry Smith {
961421e10b8SBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
962421e10b8SBarry Smith   PetscErrorCode    ierr;
963421e10b8SBarry Smith   PC_FieldSplitLink ilink = jac->head;
964939b8a20SBarry Smith   PetscInt          bs;
965421e10b8SBarry Smith 
966421e10b8SBarry Smith   PetscFunctionBegin;
967421e10b8SBarry Smith   if (jac->type == PC_COMPOSITE_ADDITIVE) {
968421e10b8SBarry Smith     if (jac->defaultsplit) {
969939b8a20SBarry Smith       ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
970ce94432eSBarry 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);
971939b8a20SBarry Smith       ierr = VecGetBlockSize(y,&bs);CHKERRQ(ierr);
972ce94432eSBarry 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);
973421e10b8SBarry Smith       ierr = VecStrideGatherAll(x,jac->x,INSERT_VALUES);CHKERRQ(ierr);
974421e10b8SBarry Smith       while (ilink) {
975421e10b8SBarry Smith         ierr  = KSPSolveTranspose(ilink->ksp,ilink->x,ilink->y);CHKERRQ(ierr);
976421e10b8SBarry Smith         ilink = ilink->next;
977421e10b8SBarry Smith       }
978421e10b8SBarry Smith       ierr = VecStrideScatterAll(jac->y,y,INSERT_VALUES);CHKERRQ(ierr);
979421e10b8SBarry Smith     } else {
980421e10b8SBarry Smith       ierr = VecSet(y,0.0);CHKERRQ(ierr);
981421e10b8SBarry Smith       while (ilink) {
982421e10b8SBarry Smith         ierr  = FieldSplitSplitSolveAddTranspose(ilink,x,y);CHKERRQ(ierr);
983421e10b8SBarry Smith         ilink = ilink->next;
984421e10b8SBarry Smith       }
985421e10b8SBarry Smith     }
986421e10b8SBarry Smith   } else {
987421e10b8SBarry Smith     if (!jac->w1) {
988421e10b8SBarry Smith       ierr = VecDuplicate(x,&jac->w1);CHKERRQ(ierr);
989421e10b8SBarry Smith       ierr = VecDuplicate(x,&jac->w2);CHKERRQ(ierr);
990421e10b8SBarry Smith     }
991421e10b8SBarry Smith     ierr = VecSet(y,0.0);CHKERRQ(ierr);
992421e10b8SBarry Smith     if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
993421e10b8SBarry Smith       ierr = FieldSplitSplitSolveAddTranspose(ilink,x,y);CHKERRQ(ierr);
994421e10b8SBarry Smith       while (ilink->next) {
995421e10b8SBarry Smith         ilink = ilink->next;
9969989ab13SBarry Smith         ierr  = MatMultTranspose(pc->mat,y,jac->w1);CHKERRQ(ierr);
997421e10b8SBarry Smith         ierr  = VecWAXPY(jac->w2,-1.0,jac->w1,x);CHKERRQ(ierr);
998421e10b8SBarry Smith         ierr  = FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);CHKERRQ(ierr);
999421e10b8SBarry Smith       }
1000421e10b8SBarry Smith       while (ilink->previous) {
1001421e10b8SBarry Smith         ilink = ilink->previous;
10029989ab13SBarry Smith         ierr  = MatMultTranspose(pc->mat,y,jac->w1);CHKERRQ(ierr);
1003421e10b8SBarry Smith         ierr  = VecWAXPY(jac->w2,-1.0,jac->w1,x);CHKERRQ(ierr);
1004421e10b8SBarry Smith         ierr  = FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);CHKERRQ(ierr);
1005421e10b8SBarry Smith       }
1006421e10b8SBarry Smith     } else {
1007421e10b8SBarry Smith       while (ilink->next) {   /* get to last entry in linked list */
1008421e10b8SBarry Smith         ilink = ilink->next;
1009421e10b8SBarry Smith       }
1010421e10b8SBarry Smith       ierr = FieldSplitSplitSolveAddTranspose(ilink,x,y);CHKERRQ(ierr);
1011421e10b8SBarry Smith       while (ilink->previous) {
1012421e10b8SBarry Smith         ilink = ilink->previous;
10139989ab13SBarry Smith         ierr  = MatMultTranspose(pc->mat,y,jac->w1);CHKERRQ(ierr);
1014421e10b8SBarry Smith         ierr  = VecWAXPY(jac->w2,-1.0,jac->w1,x);CHKERRQ(ierr);
1015421e10b8SBarry Smith         ierr  = FieldSplitSplitSolveAddTranspose(ilink,jac->w2,y);CHKERRQ(ierr);
1016421e10b8SBarry Smith       }
1017421e10b8SBarry Smith     }
1018421e10b8SBarry Smith   }
1019421e10b8SBarry Smith   PetscFunctionReturn(0);
1020421e10b8SBarry Smith }
1021421e10b8SBarry Smith 
10220971522cSBarry Smith #undef __FUNCT__
1023574deadeSBarry Smith #define __FUNCT__ "PCReset_FieldSplit"
1024574deadeSBarry Smith static PetscErrorCode PCReset_FieldSplit(PC pc)
10250971522cSBarry Smith {
10260971522cSBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
10270971522cSBarry Smith   PetscErrorCode    ierr;
10285a9f2f41SSatish Balay   PC_FieldSplitLink ilink = jac->head,next;
10290971522cSBarry Smith 
10300971522cSBarry Smith   PetscFunctionBegin;
10315a9f2f41SSatish Balay   while (ilink) {
1032574deadeSBarry Smith     ierr  = KSPReset(ilink->ksp);CHKERRQ(ierr);
1033fcfd50ebSBarry Smith     ierr  = VecDestroy(&ilink->x);CHKERRQ(ierr);
1034fcfd50ebSBarry Smith     ierr  = VecDestroy(&ilink->y);CHKERRQ(ierr);
1035443836d0SMatthew G Knepley     ierr  = VecDestroy(&ilink->z);CHKERRQ(ierr);
1036fcfd50ebSBarry Smith     ierr  = VecScatterDestroy(&ilink->sctx);CHKERRQ(ierr);
1037fcfd50ebSBarry Smith     ierr  = ISDestroy(&ilink->is);CHKERRQ(ierr);
1038b5787286SJed Brown     ierr  = ISDestroy(&ilink->is_col);CHKERRQ(ierr);
10395a9f2f41SSatish Balay     next  = ilink->next;
10405a9f2f41SSatish Balay     ilink = next;
10410971522cSBarry Smith   }
104205b42c5fSBarry Smith   ierr = PetscFree2(jac->x,jac->y);CHKERRQ(ierr);
1043574deadeSBarry Smith   if (jac->mat && jac->mat != jac->pmat) {
1044574deadeSBarry Smith     ierr = MatDestroyMatrices(jac->nsplits,&jac->mat);CHKERRQ(ierr);
1045574deadeSBarry Smith   } else if (jac->mat) {
10460298fd71SBarry Smith     jac->mat = NULL;
1047574deadeSBarry Smith   }
104897bbdb24SBarry Smith   if (jac->pmat) {ierr = MatDestroyMatrices(jac->nsplits,&jac->pmat);CHKERRQ(ierr);}
104968dd23aaSBarry Smith   if (jac->Afield) {ierr = MatDestroyMatrices(jac->nsplits,&jac->Afield);CHKERRQ(ierr);}
10506bf464f9SBarry Smith   ierr       = VecDestroy(&jac->w1);CHKERRQ(ierr);
10516bf464f9SBarry Smith   ierr       = VecDestroy(&jac->w2);CHKERRQ(ierr);
10526bf464f9SBarry Smith   ierr       = MatDestroy(&jac->schur);CHKERRQ(ierr);
1053a7476a74SDmitry Karpeev   ierr       = MatDestroy(&jac->schurp);CHKERRQ(ierr);
10546bf464f9SBarry Smith   ierr       = MatDestroy(&jac->schur_user);CHKERRQ(ierr);
10556bf464f9SBarry Smith   ierr       = KSPDestroy(&jac->kspschur);CHKERRQ(ierr);
1056d78dad28SBarry Smith   ierr       = KSPDestroy(&jac->kspupper);CHKERRQ(ierr);
10576bf464f9SBarry Smith   ierr       = MatDestroy(&jac->B);CHKERRQ(ierr);
10586bf464f9SBarry Smith   ierr       = MatDestroy(&jac->C);CHKERRQ(ierr);
105963ec74ffSBarry Smith   jac->reset = PETSC_TRUE;
1060574deadeSBarry Smith   PetscFunctionReturn(0);
1061574deadeSBarry Smith }
1062574deadeSBarry Smith 
1063574deadeSBarry Smith #undef __FUNCT__
1064574deadeSBarry Smith #define __FUNCT__ "PCDestroy_FieldSplit"
1065574deadeSBarry Smith static PetscErrorCode PCDestroy_FieldSplit(PC pc)
1066574deadeSBarry Smith {
1067574deadeSBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1068574deadeSBarry Smith   PetscErrorCode    ierr;
1069574deadeSBarry Smith   PC_FieldSplitLink ilink = jac->head,next;
1070574deadeSBarry Smith 
1071574deadeSBarry Smith   PetscFunctionBegin;
1072574deadeSBarry Smith   ierr = PCReset_FieldSplit(pc);CHKERRQ(ierr);
1073574deadeSBarry Smith   while (ilink) {
10746bf464f9SBarry Smith     ierr  = KSPDestroy(&ilink->ksp);CHKERRQ(ierr);
1075574deadeSBarry Smith     next  = ilink->next;
1076574deadeSBarry Smith     ierr  = PetscFree(ilink->splitname);CHKERRQ(ierr);
1077574deadeSBarry Smith     ierr  = PetscFree(ilink->fields);CHKERRQ(ierr);
10785d4c12cdSJungho Lee     ierr  = PetscFree(ilink->fields_col);CHKERRQ(ierr);
1079574deadeSBarry Smith     ierr  = PetscFree(ilink);CHKERRQ(ierr);
1080574deadeSBarry Smith     ilink = next;
1081574deadeSBarry Smith   }
1082574deadeSBarry Smith   ierr = PetscFree2(jac->x,jac->y);CHKERRQ(ierr);
1083c31cb41cSBarry Smith   ierr = PetscFree(pc->data);CHKERRQ(ierr);
1084bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",NULL);CHKERRQ(ierr);
1085bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetFields_C",NULL);CHKERRQ(ierr);
1086bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetIS_C",NULL);CHKERRQ(ierr);
1087bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetType_C",NULL);CHKERRQ(ierr);
1088bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetBlockSize_C",NULL);CHKERRQ(ierr);
1089bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSchurPrecondition_C",NULL);CHKERRQ(ierr);
1090bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C",NULL);CHKERRQ(ierr);
10910971522cSBarry Smith   PetscFunctionReturn(0);
10920971522cSBarry Smith }
10930971522cSBarry Smith 
10940971522cSBarry Smith #undef __FUNCT__
10950971522cSBarry Smith #define __FUNCT__ "PCSetFromOptions_FieldSplit"
10960971522cSBarry Smith static PetscErrorCode PCSetFromOptions_FieldSplit(PC pc)
10970971522cSBarry Smith {
10981b9fc7fcSBarry Smith   PetscErrorCode  ierr;
10996c924f48SJed Brown   PetscInt        bs;
1100bc59fbc5SBarry Smith   PetscBool       flg,stokes = PETSC_FALSE;
11019dcbbd2bSBarry Smith   PC_FieldSplit   *jac = (PC_FieldSplit*)pc->data;
11023b224e63SBarry Smith   PCCompositeType ctype;
11031b9fc7fcSBarry Smith 
11040971522cSBarry Smith   PetscFunctionBegin;
11051b9fc7fcSBarry Smith   ierr = PetscOptionsHead("FieldSplit options");CHKERRQ(ierr);
11064ab8060aSDmitry Karpeev   ierr = PetscOptionsBool("-pc_fieldsplit_dm_splits","Whether to use DMCreateFieldDecomposition() for splits","PCFieldSplitSetDMSplits",jac->dm_splits,&jac->dm_splits,NULL);CHKERRQ(ierr);
110751f519a2SBarry Smith   ierr = PetscOptionsInt("-pc_fieldsplit_block_size","Blocksize that defines number of fields","PCFieldSplitSetBlockSize",jac->bs,&bs,&flg);CHKERRQ(ierr);
110851f519a2SBarry Smith   if (flg) {
110951f519a2SBarry Smith     ierr = PCFieldSplitSetBlockSize(pc,bs);CHKERRQ(ierr);
111051f519a2SBarry Smith   }
1111704ba839SBarry Smith 
11120298fd71SBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)pc)->prefix,"-pc_fieldsplit_detect_saddle_point",&stokes,NULL);CHKERRQ(ierr);
1113c0adfefeSBarry Smith   if (stokes) {
1114c0adfefeSBarry Smith     ierr          = PCFieldSplitSetType(pc,PC_COMPOSITE_SCHUR);CHKERRQ(ierr);
1115c0adfefeSBarry Smith     jac->schurpre = PC_FIELDSPLIT_SCHUR_PRE_SELF;
1116c0adfefeSBarry Smith   }
1117c0adfefeSBarry Smith 
11183b224e63SBarry Smith   ierr = PetscOptionsEnum("-pc_fieldsplit_type","Type of composition","PCFieldSplitSetType",PCCompositeTypes,(PetscEnum)jac->type,(PetscEnum*)&ctype,&flg);CHKERRQ(ierr);
11193b224e63SBarry Smith   if (flg) {
11203b224e63SBarry Smith     ierr = PCFieldSplitSetType(pc,ctype);CHKERRQ(ierr);
11213b224e63SBarry Smith   }
1122c30613efSMatthew Knepley   /* Only setup fields once */
1123c30613efSMatthew Knepley   if ((jac->bs > 0) && (jac->nsplits == 0)) {
1124d32f9abdSBarry Smith     /* only allow user to set fields from command line if bs is already known.
1125d32f9abdSBarry Smith        otherwise user can set them in PCFieldSplitSetDefaults() */
11266c924f48SJed Brown     ierr = PCFieldSplitSetRuntimeSplits_Private(pc);CHKERRQ(ierr);
11276c924f48SJed Brown     if (jac->splitdefined) {ierr = PetscInfo(pc,"Splits defined using the options database\n");CHKERRQ(ierr);}
1128d32f9abdSBarry Smith   }
1129c5d2311dSJed Brown   if (jac->type == PC_COMPOSITE_SCHUR) {
1130c9c6ffaaSJed Brown     ierr = PetscOptionsGetEnum(((PetscObject)pc)->prefix,"-pc_fieldsplit_schur_factorization_type",PCFieldSplitSchurFactTypes,(PetscEnum*)&jac->schurfactorization,&flg);CHKERRQ(ierr);
1131c9c6ffaaSJed Brown     if (flg) {ierr = PetscInfo(pc,"Deprecated use of -pc_fieldsplit_schur_factorization_type\n");CHKERRQ(ierr);}
11320298fd71SBarry 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);
11330298fd71SBarry 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);
1134c5d2311dSJed Brown   }
11351b9fc7fcSBarry Smith   ierr = PetscOptionsTail();CHKERRQ(ierr);
11360971522cSBarry Smith   PetscFunctionReturn(0);
11370971522cSBarry Smith }
11380971522cSBarry Smith 
11390971522cSBarry Smith /*------------------------------------------------------------------------------------*/
11400971522cSBarry Smith 
11410971522cSBarry Smith #undef __FUNCT__
11420971522cSBarry Smith #define __FUNCT__ "PCFieldSplitSetFields_FieldSplit"
11431e6b0712SBarry Smith static PetscErrorCode  PCFieldSplitSetFields_FieldSplit(PC pc,const char splitname[],PetscInt n,const PetscInt *fields,const PetscInt *fields_col)
11440971522cSBarry Smith {
114597bbdb24SBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
11460971522cSBarry Smith   PetscErrorCode    ierr;
11475a9f2f41SSatish Balay   PC_FieldSplitLink ilink,next = jac->head;
114869a612a9SBarry Smith   char              prefix[128];
11495d4c12cdSJungho Lee   PetscInt          i;
11500971522cSBarry Smith 
11510971522cSBarry Smith   PetscFunctionBegin;
11526c924f48SJed Brown   if (jac->splitdefined) {
11536c924f48SJed Brown     ierr = PetscInfo1(pc,"Ignoring new split \"%s\" because the splits have already been defined\n",splitname);CHKERRQ(ierr);
11546c924f48SJed Brown     PetscFunctionReturn(0);
11556c924f48SJed Brown   }
115651f519a2SBarry Smith   for (i=0; i<n; i++) {
1157e32f2f54SBarry 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);
1158e32f2f54SBarry Smith     if (fields[i] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative field %D requested",fields[i]);
115951f519a2SBarry Smith   }
1160b00a9115SJed Brown   ierr = PetscNew(&ilink);CHKERRQ(ierr);
1161a04f6461SBarry Smith   if (splitname) {
1162db4c96c1SJed Brown     ierr = PetscStrallocpy(splitname,&ilink->splitname);CHKERRQ(ierr);
1163a04f6461SBarry Smith   } else {
1164785e854fSJed Brown     ierr = PetscMalloc1(3,&ilink->splitname);CHKERRQ(ierr);
1165a04f6461SBarry Smith     ierr = PetscSNPrintf(ilink->splitname,2,"%s",jac->nsplits);CHKERRQ(ierr);
1166a04f6461SBarry Smith   }
1167785e854fSJed Brown   ierr = PetscMalloc1(n,&ilink->fields);CHKERRQ(ierr);
11685a9f2f41SSatish Balay   ierr = PetscMemcpy(ilink->fields,fields,n*sizeof(PetscInt));CHKERRQ(ierr);
1169785e854fSJed Brown   ierr = PetscMalloc1(n,&ilink->fields_col);CHKERRQ(ierr);
11705d4c12cdSJungho Lee   ierr = PetscMemcpy(ilink->fields_col,fields_col,n*sizeof(PetscInt));CHKERRQ(ierr);
11712fa5cd67SKarl Rupp 
11725a9f2f41SSatish Balay   ilink->nfields = n;
11730298fd71SBarry Smith   ilink->next    = NULL;
1174ce94432eSBarry Smith   ierr           = KSPCreate(PetscObjectComm((PetscObject)pc),&ilink->ksp);CHKERRQ(ierr);
117520252d06SBarry Smith   ierr           = PetscObjectIncrementTabLevel((PetscObject)ilink->ksp,(PetscObject)pc,1);CHKERRQ(ierr);
11765a9f2f41SSatish Balay   ierr           = KSPSetType(ilink->ksp,KSPPREONLY);CHKERRQ(ierr);
11779005cf84SBarry Smith   ierr           = PetscLogObjectParent((PetscObject)pc,(PetscObject)ilink->ksp);CHKERRQ(ierr);
117869a612a9SBarry Smith 
11798caf3d72SBarry Smith   ierr = PetscSNPrintf(prefix,sizeof(prefix),"%sfieldsplit_%s_",((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "",ilink->splitname);CHKERRQ(ierr);
11805a9f2f41SSatish Balay   ierr = KSPSetOptionsPrefix(ilink->ksp,prefix);CHKERRQ(ierr);
11810971522cSBarry Smith 
11820971522cSBarry Smith   if (!next) {
11835a9f2f41SSatish Balay     jac->head       = ilink;
11840298fd71SBarry Smith     ilink->previous = NULL;
11850971522cSBarry Smith   } else {
11860971522cSBarry Smith     while (next->next) {
11870971522cSBarry Smith       next = next->next;
11880971522cSBarry Smith     }
11895a9f2f41SSatish Balay     next->next      = ilink;
119051f519a2SBarry Smith     ilink->previous = next;
11910971522cSBarry Smith   }
11920971522cSBarry Smith   jac->nsplits++;
11930971522cSBarry Smith   PetscFunctionReturn(0);
11940971522cSBarry Smith }
11950971522cSBarry Smith 
1196e69d4d44SBarry Smith #undef __FUNCT__
1197e69d4d44SBarry Smith #define __FUNCT__ "PCFieldSplitGetSubKSP_FieldSplit_Schur"
11981e6b0712SBarry Smith static PetscErrorCode  PCFieldSplitGetSubKSP_FieldSplit_Schur(PC pc,PetscInt *n,KSP **subksp)
1199e69d4d44SBarry Smith {
1200e69d4d44SBarry Smith   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
1201e69d4d44SBarry Smith   PetscErrorCode ierr;
1202e69d4d44SBarry Smith 
1203e69d4d44SBarry Smith   PetscFunctionBegin;
1204785e854fSJed Brown   ierr = PetscMalloc1(jac->nsplits,subksp);CHKERRQ(ierr);
1205e69d4d44SBarry Smith   ierr = MatSchurComplementGetKSP(jac->schur,*subksp);CHKERRQ(ierr);
12062fa5cd67SKarl Rupp 
1207e69d4d44SBarry Smith   (*subksp)[1] = jac->kspschur;
120813e0d083SBarry Smith   if (n) *n = jac->nsplits;
1209e69d4d44SBarry Smith   PetscFunctionReturn(0);
1210e69d4d44SBarry Smith }
12110971522cSBarry Smith 
12120971522cSBarry Smith #undef __FUNCT__
121369a612a9SBarry Smith #define __FUNCT__ "PCFieldSplitGetSubKSP_FieldSplit"
12141e6b0712SBarry Smith static PetscErrorCode  PCFieldSplitGetSubKSP_FieldSplit(PC pc,PetscInt *n,KSP **subksp)
12150971522cSBarry Smith {
12160971522cSBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
12170971522cSBarry Smith   PetscErrorCode    ierr;
12180971522cSBarry Smith   PetscInt          cnt   = 0;
12195a9f2f41SSatish Balay   PC_FieldSplitLink ilink = jac->head;
12200971522cSBarry Smith 
12210971522cSBarry Smith   PetscFunctionBegin;
1222785e854fSJed Brown   ierr = PetscMalloc1(jac->nsplits,subksp);CHKERRQ(ierr);
12235a9f2f41SSatish Balay   while (ilink) {
12245a9f2f41SSatish Balay     (*subksp)[cnt++] = ilink->ksp;
12255a9f2f41SSatish Balay     ilink            = ilink->next;
12260971522cSBarry Smith   }
12275d480477SMatthew 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);
122813e0d083SBarry Smith   if (n) *n = jac->nsplits;
12290971522cSBarry Smith   PetscFunctionReturn(0);
12300971522cSBarry Smith }
12310971522cSBarry Smith 
1232704ba839SBarry Smith #undef __FUNCT__
1233704ba839SBarry Smith #define __FUNCT__ "PCFieldSplitSetIS_FieldSplit"
12341e6b0712SBarry Smith static PetscErrorCode  PCFieldSplitSetIS_FieldSplit(PC pc,const char splitname[],IS is)
1235704ba839SBarry Smith {
1236704ba839SBarry Smith   PC_FieldSplit     *jac = (PC_FieldSplit*)pc->data;
1237704ba839SBarry Smith   PetscErrorCode    ierr;
1238704ba839SBarry Smith   PC_FieldSplitLink ilink, next = jac->head;
1239704ba839SBarry Smith   char              prefix[128];
1240704ba839SBarry Smith 
1241704ba839SBarry Smith   PetscFunctionBegin;
12426c924f48SJed Brown   if (jac->splitdefined) {
12436c924f48SJed Brown     ierr = PetscInfo1(pc,"Ignoring new split \"%s\" because the splits have already been defined\n",splitname);CHKERRQ(ierr);
12446c924f48SJed Brown     PetscFunctionReturn(0);
12456c924f48SJed Brown   }
1246b00a9115SJed Brown   ierr = PetscNew(&ilink);CHKERRQ(ierr);
1247a04f6461SBarry Smith   if (splitname) {
1248db4c96c1SJed Brown     ierr = PetscStrallocpy(splitname,&ilink->splitname);CHKERRQ(ierr);
1249a04f6461SBarry Smith   } else {
1250785e854fSJed Brown     ierr = PetscMalloc1(8,&ilink->splitname);CHKERRQ(ierr);
1251b5787286SJed Brown     ierr = PetscSNPrintf(ilink->splitname,7,"%D",jac->nsplits);CHKERRQ(ierr);
1252a04f6461SBarry Smith   }
12531ab39975SBarry Smith   ierr          = PetscObjectReference((PetscObject)is);CHKERRQ(ierr);
1254b5787286SJed Brown   ierr          = ISDestroy(&ilink->is);CHKERRQ(ierr);
1255b5787286SJed Brown   ilink->is     = is;
1256b5787286SJed Brown   ierr          = PetscObjectReference((PetscObject)is);CHKERRQ(ierr);
1257b5787286SJed Brown   ierr          = ISDestroy(&ilink->is_col);CHKERRQ(ierr);
1258b5787286SJed Brown   ilink->is_col = is;
12590298fd71SBarry Smith   ilink->next   = NULL;
1260ce94432eSBarry Smith   ierr          = KSPCreate(PetscObjectComm((PetscObject)pc),&ilink->ksp);CHKERRQ(ierr);
126120252d06SBarry Smith   ierr          = PetscObjectIncrementTabLevel((PetscObject)ilink->ksp,(PetscObject)pc,1);CHKERRQ(ierr);
1262704ba839SBarry Smith   ierr          = KSPSetType(ilink->ksp,KSPPREONLY);CHKERRQ(ierr);
12639005cf84SBarry Smith   ierr          = PetscLogObjectParent((PetscObject)pc,(PetscObject)ilink->ksp);CHKERRQ(ierr);
1264704ba839SBarry Smith 
12658caf3d72SBarry Smith   ierr = PetscSNPrintf(prefix,sizeof(prefix),"%sfieldsplit_%s_",((PetscObject)pc)->prefix ? ((PetscObject)pc)->prefix : "",ilink->splitname);CHKERRQ(ierr);
1266704ba839SBarry Smith   ierr = KSPSetOptionsPrefix(ilink->ksp,prefix);CHKERRQ(ierr);
1267704ba839SBarry Smith 
1268704ba839SBarry Smith   if (!next) {
1269704ba839SBarry Smith     jac->head       = ilink;
12700298fd71SBarry Smith     ilink->previous = NULL;
1271704ba839SBarry Smith   } else {
1272704ba839SBarry Smith     while (next->next) {
1273704ba839SBarry Smith       next = next->next;
1274704ba839SBarry Smith     }
1275704ba839SBarry Smith     next->next      = ilink;
1276704ba839SBarry Smith     ilink->previous = next;
1277704ba839SBarry Smith   }
1278704ba839SBarry Smith   jac->nsplits++;
1279704ba839SBarry Smith   PetscFunctionReturn(0);
1280704ba839SBarry Smith }
1281704ba839SBarry Smith 
12820971522cSBarry Smith #undef __FUNCT__
12830971522cSBarry Smith #define __FUNCT__ "PCFieldSplitSetFields"
12840971522cSBarry Smith /*@
12850971522cSBarry Smith     PCFieldSplitSetFields - Sets the fields for one particular split in the field split preconditioner
12860971522cSBarry Smith 
1287ad4df100SBarry Smith     Logically Collective on PC
12880971522cSBarry Smith 
12890971522cSBarry Smith     Input Parameters:
12900971522cSBarry Smith +   pc  - the preconditioner context
12910298fd71SBarry Smith .   splitname - name of this split, if NULL the number of the split is used
12920971522cSBarry Smith .   n - the number of fields in this split
1293db4c96c1SJed Brown -   fields - the fields in this split
12940971522cSBarry Smith 
12950971522cSBarry Smith     Level: intermediate
12960971522cSBarry Smith 
1297d32f9abdSBarry Smith     Notes: Use PCFieldSplitSetIS() to set a completely general set of indices as a field.
1298d32f9abdSBarry Smith 
12997287d2a3SDmitry Karpeev      The PCFieldSplitSetFields() is for defining fields as strided blocks. For example, if the block
1300d32f9abdSBarry 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
1301d32f9abdSBarry Smith      0xx3xx6xx9xx12 ... x1xx4xx7xx ... xx2xx5xx8xx.. 01x34x67x... 0x1x3x5x7.. x12x45x78x....
1302d32f9abdSBarry Smith      where the numbered entries indicate what is in the field.
1303d32f9abdSBarry Smith 
1304db4c96c1SJed Brown      This function is called once per split (it creates a new split each time).  Solve options
1305db4c96c1SJed Brown      for this split will be available under the prefix -fieldsplit_SPLITNAME_.
1306db4c96c1SJed Brown 
13075d4c12cdSJungho Lee      Developer Note: This routine does not actually create the IS representing the split, that is delayed
13085d4c12cdSJungho Lee      until PCSetUp_FieldSplit(), because information about the vector/matrix layouts may not be
13095d4c12cdSJungho Lee      available when this routine is called.
13105d4c12cdSJungho Lee 
1311d32f9abdSBarry Smith .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetBlockSize(), PCFieldSplitSetIS()
13120971522cSBarry Smith 
13130971522cSBarry Smith @*/
13145d4c12cdSJungho Lee PetscErrorCode  PCFieldSplitSetFields(PC pc,const char splitname[],PetscInt n,const PetscInt *fields,const PetscInt *fields_col)
13150971522cSBarry Smith {
13164ac538c5SBarry Smith   PetscErrorCode ierr;
13170971522cSBarry Smith 
13180971522cSBarry Smith   PetscFunctionBegin;
13190700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1320db4c96c1SJed Brown   PetscValidCharPointer(splitname,2);
1321ce94432eSBarry Smith   if (n < 1) SETERRQ2(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Provided number of fields %D in split \"%s\" not positive",n,splitname);
1322db4c96c1SJed Brown   PetscValidIntPointer(fields,3);
13235d4c12cdSJungho Lee   ierr = PetscTryMethod(pc,"PCFieldSplitSetFields_C",(PC,const char[],PetscInt,const PetscInt*,const PetscInt*),(pc,splitname,n,fields,fields_col));CHKERRQ(ierr);
13240971522cSBarry Smith   PetscFunctionReturn(0);
13250971522cSBarry Smith }
13260971522cSBarry Smith 
13270971522cSBarry Smith #undef __FUNCT__
1328704ba839SBarry Smith #define __FUNCT__ "PCFieldSplitSetIS"
1329704ba839SBarry Smith /*@
1330704ba839SBarry Smith     PCFieldSplitSetIS - Sets the exact elements for field
1331704ba839SBarry Smith 
1332ad4df100SBarry Smith     Logically Collective on PC
1333704ba839SBarry Smith 
1334704ba839SBarry Smith     Input Parameters:
1335704ba839SBarry Smith +   pc  - the preconditioner context
13360298fd71SBarry Smith .   splitname - name of this split, if NULL the number of the split is used
1337db4c96c1SJed Brown -   is - the index set that defines the vector elements in this field
1338704ba839SBarry Smith 
1339d32f9abdSBarry Smith 
1340a6ffb8dbSJed Brown     Notes:
1341a6ffb8dbSJed Brown     Use PCFieldSplitSetFields(), for fields defined by strided types.
1342a6ffb8dbSJed Brown 
1343db4c96c1SJed Brown     This function is called once per split (it creates a new split each time).  Solve options
1344db4c96c1SJed Brown     for this split will be available under the prefix -fieldsplit_SPLITNAME_.
1345d32f9abdSBarry Smith 
1346704ba839SBarry Smith     Level: intermediate
1347704ba839SBarry Smith 
1348704ba839SBarry Smith .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetBlockSize()
1349704ba839SBarry Smith 
1350704ba839SBarry Smith @*/
13517087cfbeSBarry Smith PetscErrorCode  PCFieldSplitSetIS(PC pc,const char splitname[],IS is)
1352704ba839SBarry Smith {
13534ac538c5SBarry Smith   PetscErrorCode ierr;
1354704ba839SBarry Smith 
1355704ba839SBarry Smith   PetscFunctionBegin;
13560700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
13577b62db95SJungho Lee   if (splitname) PetscValidCharPointer(splitname,2);
1358db4c96c1SJed Brown   PetscValidHeaderSpecific(is,IS_CLASSID,3);
13594ac538c5SBarry Smith   ierr = PetscTryMethod(pc,"PCFieldSplitSetIS_C",(PC,const char[],IS),(pc,splitname,is));CHKERRQ(ierr);
1360704ba839SBarry Smith   PetscFunctionReturn(0);
1361704ba839SBarry Smith }
1362704ba839SBarry Smith 
1363704ba839SBarry Smith #undef __FUNCT__
136457a9adfeSMatthew G Knepley #define __FUNCT__ "PCFieldSplitGetIS"
136557a9adfeSMatthew G Knepley /*@
136657a9adfeSMatthew G Knepley     PCFieldSplitGetIS - Retrieves the elements for a field as an IS
136757a9adfeSMatthew G Knepley 
136857a9adfeSMatthew G Knepley     Logically Collective on PC
136957a9adfeSMatthew G Knepley 
137057a9adfeSMatthew G Knepley     Input Parameters:
137157a9adfeSMatthew G Knepley +   pc  - the preconditioner context
137257a9adfeSMatthew G Knepley -   splitname - name of this split
137357a9adfeSMatthew G Knepley 
137457a9adfeSMatthew G Knepley     Output Parameter:
13750298fd71SBarry Smith -   is - the index set that defines the vector elements in this field, or NULL if the field is not found
137657a9adfeSMatthew G Knepley 
137757a9adfeSMatthew G Knepley     Level: intermediate
137857a9adfeSMatthew G Knepley 
137957a9adfeSMatthew G Knepley .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetIS()
138057a9adfeSMatthew G Knepley 
138157a9adfeSMatthew G Knepley @*/
138257a9adfeSMatthew G Knepley PetscErrorCode PCFieldSplitGetIS(PC pc,const char splitname[],IS *is)
138357a9adfeSMatthew G Knepley {
138457a9adfeSMatthew G Knepley   PetscErrorCode ierr;
138557a9adfeSMatthew G Knepley 
138657a9adfeSMatthew G Knepley   PetscFunctionBegin;
138757a9adfeSMatthew G Knepley   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
138857a9adfeSMatthew G Knepley   PetscValidCharPointer(splitname,2);
138957a9adfeSMatthew G Knepley   PetscValidPointer(is,3);
139057a9adfeSMatthew G Knepley   {
139157a9adfeSMatthew G Knepley     PC_FieldSplit     *jac  = (PC_FieldSplit*) pc->data;
139257a9adfeSMatthew G Knepley     PC_FieldSplitLink ilink = jac->head;
139357a9adfeSMatthew G Knepley     PetscBool         found;
139457a9adfeSMatthew G Knepley 
13950298fd71SBarry Smith     *is = NULL;
139657a9adfeSMatthew G Knepley     while (ilink) {
139757a9adfeSMatthew G Knepley       ierr = PetscStrcmp(ilink->splitname, splitname, &found);CHKERRQ(ierr);
139857a9adfeSMatthew G Knepley       if (found) {
139957a9adfeSMatthew G Knepley         *is = ilink->is;
140057a9adfeSMatthew G Knepley         break;
140157a9adfeSMatthew G Knepley       }
140257a9adfeSMatthew G Knepley       ilink = ilink->next;
140357a9adfeSMatthew G Knepley     }
140457a9adfeSMatthew G Knepley   }
140557a9adfeSMatthew G Knepley   PetscFunctionReturn(0);
140657a9adfeSMatthew G Knepley }
140757a9adfeSMatthew G Knepley 
140857a9adfeSMatthew G Knepley #undef __FUNCT__
140951f519a2SBarry Smith #define __FUNCT__ "PCFieldSplitSetBlockSize"
141051f519a2SBarry Smith /*@
141151f519a2SBarry Smith     PCFieldSplitSetBlockSize - Sets the block size for defining where fields start in the
141251f519a2SBarry Smith       fieldsplit preconditioner. If not set the matrix block size is used.
141351f519a2SBarry Smith 
1414ad4df100SBarry Smith     Logically Collective on PC
141551f519a2SBarry Smith 
141651f519a2SBarry Smith     Input Parameters:
141751f519a2SBarry Smith +   pc  - the preconditioner context
141851f519a2SBarry Smith -   bs - the block size
141951f519a2SBarry Smith 
142051f519a2SBarry Smith     Level: intermediate
142151f519a2SBarry Smith 
142251f519a2SBarry Smith .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields()
142351f519a2SBarry Smith 
142451f519a2SBarry Smith @*/
14257087cfbeSBarry Smith PetscErrorCode  PCFieldSplitSetBlockSize(PC pc,PetscInt bs)
142651f519a2SBarry Smith {
14274ac538c5SBarry Smith   PetscErrorCode ierr;
142851f519a2SBarry Smith 
142951f519a2SBarry Smith   PetscFunctionBegin;
14300700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1431c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(pc,bs,2);
14324ac538c5SBarry Smith   ierr = PetscTryMethod(pc,"PCFieldSplitSetBlockSize_C",(PC,PetscInt),(pc,bs));CHKERRQ(ierr);
143351f519a2SBarry Smith   PetscFunctionReturn(0);
143451f519a2SBarry Smith }
143551f519a2SBarry Smith 
143651f519a2SBarry Smith #undef __FUNCT__
143769a612a9SBarry Smith #define __FUNCT__ "PCFieldSplitGetSubKSP"
14380971522cSBarry Smith /*@C
143969a612a9SBarry Smith    PCFieldSplitGetSubKSP - Gets the KSP contexts for all splits
14400971522cSBarry Smith 
144169a612a9SBarry Smith    Collective on KSP
14420971522cSBarry Smith 
14430971522cSBarry Smith    Input Parameter:
14440971522cSBarry Smith .  pc - the preconditioner context
14450971522cSBarry Smith 
14460971522cSBarry Smith    Output Parameters:
144713e0d083SBarry Smith +  n - the number of splits
144869a612a9SBarry Smith -  pc - the array of KSP contexts
14490971522cSBarry Smith 
14500971522cSBarry Smith    Note:
1451d32f9abdSBarry Smith    After PCFieldSplitGetSubKSP() the array of KSPs IS to be freed by the user
1452d32f9abdSBarry Smith    (not the KSP just the array that contains them).
14530971522cSBarry Smith 
145469a612a9SBarry Smith    You must call KSPSetUp() before calling PCFieldSplitGetSubKSP().
14550971522cSBarry Smith 
1456196cc216SBarry Smith    Fortran Usage: You must pass in a KSP array that is large enough to contain all the local KSPs.
14570298fd71SBarry Smith       You can call PCFieldSplitGetSubKSP(pc,n,NULL_OBJECT,ierr) to determine how large the
1458196cc216SBarry Smith       KSP array must be.
1459196cc216SBarry Smith 
1460196cc216SBarry Smith 
14610971522cSBarry Smith    Level: advanced
14620971522cSBarry Smith 
14630971522cSBarry Smith .seealso: PCFIELDSPLIT
14640971522cSBarry Smith @*/
14657087cfbeSBarry Smith PetscErrorCode  PCFieldSplitGetSubKSP(PC pc,PetscInt *n,KSP *subksp[])
14660971522cSBarry Smith {
14674ac538c5SBarry Smith   PetscErrorCode ierr;
14680971522cSBarry Smith 
14690971522cSBarry Smith   PetscFunctionBegin;
14700700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
147113e0d083SBarry Smith   if (n) PetscValidIntPointer(n,2);
14724ac538c5SBarry Smith   ierr = PetscUseMethod(pc,"PCFieldSplitGetSubKSP_C",(PC,PetscInt*,KSP **),(pc,n,subksp));CHKERRQ(ierr);
14730971522cSBarry Smith   PetscFunctionReturn(0);
14740971522cSBarry Smith }
14750971522cSBarry Smith 
1476e69d4d44SBarry Smith #undef __FUNCT__
1477e69d4d44SBarry Smith #define __FUNCT__ "PCFieldSplitSchurPrecondition"
1478e69d4d44SBarry Smith /*@
1479e69d4d44SBarry Smith     PCFieldSplitSchurPrecondition -  Indicates if the Schur complement is preconditioned by a preconditioner constructed by the
1480a04f6461SBarry Smith       A11 matrix. Otherwise no preconditioner is used.
1481e69d4d44SBarry Smith 
1482e69d4d44SBarry Smith     Collective on PC
1483e69d4d44SBarry Smith 
1484e69d4d44SBarry Smith     Input Parameters:
1485e69d4d44SBarry Smith +   pc      - the preconditioner context
1486a7476a74SDmitry Karpeev .   ptype   - which matrix to use for preconditioning the Schur complement: PC_FIELDSPLIT_SCHUR_PRE_A11 (default), PC_FIELDSPLIT_SCHUR_PRE_SELF, PC_FIELDSPLIT_PRE_USER
14870298fd71SBarry Smith -   userpre - matrix to use for preconditioning, or NULL
1488084e4875SJed Brown 
1489e69d4d44SBarry Smith     Options Database:
14902e71c61dSMatthew G. Knepley .     -pc_fieldsplit_schur_precondition <self,selfp,user,a11,full> default is a11
1491e69d4d44SBarry Smith 
1492fd1303e9SJungho Lee     Notes:
1493fd1303e9SJungho Lee $    If ptype is
1494fd1303e9SJungho Lee $        user then the preconditioner for the Schur complement is generated by the provided matrix (pre argument
1495fd1303e9SJungho Lee $             to this function).
1496e87fab1bSBarry Smith $        a11 then the preconditioner for the Schur complement is generated by the block diagonal part of the original
1497fd1303e9SJungho Lee $             matrix associated with the Schur complement (i.e. A11)
1498e74569cdSMatthew G. Knepley $        full then the preconditioner uses the exact Schur complement (this is expensive)
1499fd1303e9SJungho Lee $        self the preconditioner for the Schur complement is generated from the Schur complement matrix itself:
1500fd1303e9SJungho Lee $             The only preconditioner that currently works directly with the Schur complement matrix object is the PCLSC
1501fd1303e9SJungho Lee $             preconditioner
1502a7476a74SDmitry Karpeev $        selfp then the preconditioning matrix is an explicitly-assembled approximation Sp = A11 - A10 inv(diag(A00)) A01
1503a7476a74SDmitry Karpeev $             This is only a good preconditioner when diag(A00) is a good preconditioner for A00.
1504fd1303e9SJungho Lee 
1505e87fab1bSBarry Smith      When solving a saddle point problem, where the A11 block is identically zero, using a11 as the ptype only makes sense
1506fd1303e9SJungho Lee     with the additional option -fieldsplit_1_pc_type none. Usually for saddle point problems one would use a ptype of self and
1507a7476a74SDmitry Karpeev     -fieldsplit_1_pc_type lsc which uses the least squares commutator to compute a preconditioner for the Schur complement.
1508fd1303e9SJungho Lee 
1509a7476a74SDmitry Karpeev     Developer Notes: This is a terrible name, which gives no good indication of what the function does.
1510a7476a74SDmitry Karpeev                      Among other things, it should have 'Set' in the name, since it sets the type of matrix to use.
1511fd1303e9SJungho Lee 
1512e69d4d44SBarry Smith     Level: intermediate
1513e69d4d44SBarry Smith 
1514fd1303e9SJungho Lee .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields(), PCFieldSplitSchurPreType, PCLSC
1515e69d4d44SBarry Smith 
1516e69d4d44SBarry Smith @*/
15177087cfbeSBarry Smith PetscErrorCode  PCFieldSplitSchurPrecondition(PC pc,PCFieldSplitSchurPreType ptype,Mat pre)
1518e69d4d44SBarry Smith {
15194ac538c5SBarry Smith   PetscErrorCode ierr;
1520e69d4d44SBarry Smith 
1521e69d4d44SBarry Smith   PetscFunctionBegin;
15220700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
15234ac538c5SBarry Smith   ierr = PetscTryMethod(pc,"PCFieldSplitSchurPrecondition_C",(PC,PCFieldSplitSchurPreType,Mat),(pc,ptype,pre));CHKERRQ(ierr);
1524e69d4d44SBarry Smith   PetscFunctionReturn(0);
1525e69d4d44SBarry Smith }
1526e69d4d44SBarry Smith 
1527e69d4d44SBarry Smith #undef __FUNCT__
1528470b340bSDmitry Karpeev #define __FUNCT__ "PCFieldSplitSchurGetS"
152945e7fc46SDmitry Karpeev /*@
1530470b340bSDmitry Karpeev     PCFieldSplitSchurGetS -  extract the MatSchurComplement object used by this PC in case it needs to be configured separately
153145e7fc46SDmitry Karpeev 
153245e7fc46SDmitry Karpeev     Not collective
153345e7fc46SDmitry Karpeev 
153445e7fc46SDmitry Karpeev     Input Parameter:
153545e7fc46SDmitry Karpeev .   pc      - the preconditioner context
153645e7fc46SDmitry Karpeev 
153745e7fc46SDmitry Karpeev     Output Parameter:
1538470b340bSDmitry Karpeev .   S       - the Schur complement matrix
153945e7fc46SDmitry Karpeev 
1540470b340bSDmitry Karpeev     Notes:
1541470b340bSDmitry Karpeev     This matrix should not be destroyed using MatDestroy(); rather, use PCFieldSplitSchurRestoreS().
154245e7fc46SDmitry Karpeev 
154345e7fc46SDmitry Karpeev     Level: advanced
154445e7fc46SDmitry Karpeev 
1545470b340bSDmitry Karpeev .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSchurPreType, PCFieldSplitSchurPrecondition(), MatSchurComplement, PCFieldSplitSchurRestoreS()
154645e7fc46SDmitry Karpeev 
154745e7fc46SDmitry Karpeev @*/
1548470b340bSDmitry Karpeev PetscErrorCode  PCFieldSplitSchurGetS(PC pc,Mat *S)
154945e7fc46SDmitry Karpeev {
155045e7fc46SDmitry Karpeev   PetscErrorCode ierr;
155145e7fc46SDmitry Karpeev   const char*    t;
155245e7fc46SDmitry Karpeev   PetscBool      isfs;
155345e7fc46SDmitry Karpeev   PC_FieldSplit  *jac;
155445e7fc46SDmitry Karpeev 
155545e7fc46SDmitry Karpeev   PetscFunctionBegin;
155645e7fc46SDmitry Karpeev   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
155745e7fc46SDmitry Karpeev   ierr = PetscObjectGetType((PetscObject)pc,&t);CHKERRQ(ierr);
155845e7fc46SDmitry Karpeev   ierr = PetscStrcmp(t,PCFIELDSPLIT,&isfs);CHKERRQ(ierr);
155945e7fc46SDmitry Karpeev   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PC of type PCFIELDSPLIT, got %s instead",t);
156045e7fc46SDmitry Karpeev   jac = (PC_FieldSplit*)pc->data;
1561470b340bSDmitry Karpeev   if (jac->type != PC_COMPOSITE_SCHUR) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PCFIELDSPLIT of type SCHUR, got %D instead",jac->type);
1562470b340bSDmitry Karpeev   if (S) *S = jac->schur;
156345e7fc46SDmitry Karpeev   PetscFunctionReturn(0);
156445e7fc46SDmitry Karpeev }
156545e7fc46SDmitry Karpeev 
156645e7fc46SDmitry Karpeev #undef __FUNCT__
1567470b340bSDmitry Karpeev #define __FUNCT__ "PCFieldSplitSchurRestoreS"
1568470b340bSDmitry Karpeev /*@
1569470b340bSDmitry Karpeev     PCFieldSplitSchurRestoreS -  restores the MatSchurComplement object used by this PC
1570470b340bSDmitry Karpeev 
1571470b340bSDmitry Karpeev     Not collective
1572470b340bSDmitry Karpeev 
1573470b340bSDmitry Karpeev     Input Parameters:
1574470b340bSDmitry Karpeev +   pc      - the preconditioner context
1575470b340bSDmitry Karpeev .   S       - the Schur complement matrix
1576470b340bSDmitry Karpeev 
1577470b340bSDmitry Karpeev     Level: advanced
1578470b340bSDmitry Karpeev 
1579470b340bSDmitry Karpeev .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSchurPreType, PCFieldSplitSchurPrecondition(), MatSchurComplement, PCFieldSplitSchurGetS()
1580470b340bSDmitry Karpeev 
1581470b340bSDmitry Karpeev @*/
1582bca69d2bSDmitry Karpeev PetscErrorCode  PCFieldSplitSchurRestoreS(PC pc,Mat *S)
1583470b340bSDmitry Karpeev {
1584470b340bSDmitry Karpeev   PetscErrorCode ierr;
1585470b340bSDmitry Karpeev   const char*    t;
1586470b340bSDmitry Karpeev   PetscBool      isfs;
1587470b340bSDmitry Karpeev   PC_FieldSplit  *jac;
1588470b340bSDmitry Karpeev 
1589470b340bSDmitry Karpeev   PetscFunctionBegin;
1590470b340bSDmitry Karpeev   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1591470b340bSDmitry Karpeev   ierr = PetscObjectGetType((PetscObject)pc,&t);CHKERRQ(ierr);
1592470b340bSDmitry Karpeev   ierr = PetscStrcmp(t,PCFIELDSPLIT,&isfs);CHKERRQ(ierr);
1593470b340bSDmitry Karpeev   if (!isfs) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PC of type PCFIELDSPLIT, got %s instead",t);
1594470b340bSDmitry Karpeev   jac = (PC_FieldSplit*)pc->data;
1595470b340bSDmitry Karpeev   if (jac->type != PC_COMPOSITE_SCHUR) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected PCFIELDSPLIT of type SCHUR, got %D instead",jac->type);
1596bca69d2bSDmitry Karpeev   if (!S || *S != jac->schur) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"MatSchurComplement restored is not the same as gotten");
1597470b340bSDmitry Karpeev   PetscFunctionReturn(0);
1598470b340bSDmitry Karpeev }
1599470b340bSDmitry Karpeev 
1600470b340bSDmitry Karpeev 
1601470b340bSDmitry Karpeev #undef __FUNCT__
1602e69d4d44SBarry Smith #define __FUNCT__ "PCFieldSplitSchurPrecondition_FieldSplit"
16031e6b0712SBarry Smith static PetscErrorCode  PCFieldSplitSchurPrecondition_FieldSplit(PC pc,PCFieldSplitSchurPreType ptype,Mat pre)
1604e69d4d44SBarry Smith {
1605e69d4d44SBarry Smith   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
1606084e4875SJed Brown   PetscErrorCode ierr;
1607e69d4d44SBarry Smith 
1608e69d4d44SBarry Smith   PetscFunctionBegin;
1609084e4875SJed Brown   jac->schurpre = ptype;
1610a7476a74SDmitry Karpeev   if (ptype == PC_FIELDSPLIT_SCHUR_PRE_USER && pre) {
16116bf464f9SBarry Smith     ierr            = MatDestroy(&jac->schur_user);CHKERRQ(ierr);
1612084e4875SJed Brown     jac->schur_user = pre;
1613084e4875SJed Brown     ierr            = PetscObjectReference((PetscObject)jac->schur_user);CHKERRQ(ierr);
1614084e4875SJed Brown   }
1615e69d4d44SBarry Smith   PetscFunctionReturn(0);
1616e69d4d44SBarry Smith }
1617e69d4d44SBarry Smith 
161830ad9308SMatthew Knepley #undef __FUNCT__
1619c9c6ffaaSJed Brown #define __FUNCT__ "PCFieldSplitSetSchurFactType"
1620ab1df9f5SJed Brown /*@
1621c9c6ffaaSJed Brown     PCFieldSplitSetSchurFactType -  sets which blocks of the approximate block factorization to retain
1622ab1df9f5SJed Brown 
1623ab1df9f5SJed Brown     Collective on PC
1624ab1df9f5SJed Brown 
1625ab1df9f5SJed Brown     Input Parameters:
1626ab1df9f5SJed Brown +   pc  - the preconditioner context
1627c9c6ffaaSJed Brown -   ftype - which blocks of factorization to retain, PC_FIELDSPLIT_SCHUR_FACT_FULL is default
1628ab1df9f5SJed Brown 
1629ab1df9f5SJed Brown     Options Database:
1630c9c6ffaaSJed Brown .     -pc_fieldsplit_schur_fact_type <diag,lower,upper,full> default is full
1631ab1df9f5SJed Brown 
1632ab1df9f5SJed Brown 
1633ab1df9f5SJed Brown     Level: intermediate
1634ab1df9f5SJed Brown 
1635ab1df9f5SJed Brown     Notes:
1636ab1df9f5SJed Brown     The FULL factorization is
1637ab1df9f5SJed Brown 
1638ab1df9f5SJed Brown $   (A   B)  = (1       0) (A   0) (1  Ainv*B)
1639ab1df9f5SJed Brown $   (C   D)    (C*Ainv  1) (0   S) (0     1  )
1640ab1df9f5SJed Brown 
16416be4592eSBarry 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,
16426be4592eSBarry 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).
1643ab1df9f5SJed Brown 
16446be4592eSBarry 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
16456be4592eSBarry 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
16466be4592eSBarry 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,
16476be4592eSBarry 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.
1648ab1df9f5SJed Brown 
16496be4592eSBarry 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
16506be4592eSBarry 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).
1651ab1df9f5SJed Brown 
1652ab1df9f5SJed Brown     References:
1653ab1df9f5SJed Brown     Murphy, Golub, and Wathen, A note on preconditioning indefinite linear systems, SIAM J. Sci. Comput., 21 (2000) pp. 1969-1972.
1654ab1df9f5SJed Brown 
1655ab1df9f5SJed Brown     Ipsen, A note on preconditioning nonsymmetric matrices, SIAM J. Sci. Comput., 23 (2001), pp. 1050-1051.
1656ab1df9f5SJed Brown 
1657ab1df9f5SJed Brown .seealso: PCFieldSplitGetSubKSP(), PCFIELDSPLIT, PCFieldSplitSetFields(), PCFieldSplitSchurPreType
1658ab1df9f5SJed Brown @*/
1659c9c6ffaaSJed Brown PetscErrorCode  PCFieldSplitSetSchurFactType(PC pc,PCFieldSplitSchurFactType ftype)
1660ab1df9f5SJed Brown {
1661ab1df9f5SJed Brown   PetscErrorCode ierr;
1662ab1df9f5SJed Brown 
1663ab1df9f5SJed Brown   PetscFunctionBegin;
1664ab1df9f5SJed Brown   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1665c9c6ffaaSJed Brown   ierr = PetscTryMethod(pc,"PCFieldSplitSetSchurFactType_C",(PC,PCFieldSplitSchurFactType),(pc,ftype));CHKERRQ(ierr);
1666ab1df9f5SJed Brown   PetscFunctionReturn(0);
1667ab1df9f5SJed Brown }
1668ab1df9f5SJed Brown 
1669ab1df9f5SJed Brown #undef __FUNCT__
1670c9c6ffaaSJed Brown #define __FUNCT__ "PCFieldSplitSetSchurFactType_FieldSplit"
16711e6b0712SBarry Smith static PetscErrorCode PCFieldSplitSetSchurFactType_FieldSplit(PC pc,PCFieldSplitSchurFactType ftype)
1672ab1df9f5SJed Brown {
1673ab1df9f5SJed Brown   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
1674ab1df9f5SJed Brown 
1675ab1df9f5SJed Brown   PetscFunctionBegin;
1676ab1df9f5SJed Brown   jac->schurfactorization = ftype;
1677ab1df9f5SJed Brown   PetscFunctionReturn(0);
1678ab1df9f5SJed Brown }
1679ab1df9f5SJed Brown 
1680ab1df9f5SJed Brown #undef __FUNCT__
168130ad9308SMatthew Knepley #define __FUNCT__ "PCFieldSplitGetSchurBlocks"
168230ad9308SMatthew Knepley /*@C
16838c03b21aSDmitry Karpeev    PCFieldSplitGetSchurBlocks - Gets all matrix blocks for the Schur complement
168430ad9308SMatthew Knepley 
168530ad9308SMatthew Knepley    Collective on KSP
168630ad9308SMatthew Knepley 
168730ad9308SMatthew Knepley    Input Parameter:
168830ad9308SMatthew Knepley .  pc - the preconditioner context
168930ad9308SMatthew Knepley 
169030ad9308SMatthew Knepley    Output Parameters:
1691a04f6461SBarry Smith +  A00 - the (0,0) block
1692a04f6461SBarry Smith .  A01 - the (0,1) block
1693a04f6461SBarry Smith .  A10 - the (1,0) block
1694a04f6461SBarry Smith -  A11 - the (1,1) block
169530ad9308SMatthew Knepley 
169630ad9308SMatthew Knepley    Level: advanced
169730ad9308SMatthew Knepley 
169830ad9308SMatthew Knepley .seealso: PCFIELDSPLIT
169930ad9308SMatthew Knepley @*/
1700a04f6461SBarry Smith PetscErrorCode  PCFieldSplitGetSchurBlocks(PC pc,Mat *A00,Mat *A01,Mat *A10, Mat *A11)
170130ad9308SMatthew Knepley {
170230ad9308SMatthew Knepley   PC_FieldSplit *jac = (PC_FieldSplit*) pc->data;
170330ad9308SMatthew Knepley 
170430ad9308SMatthew Knepley   PetscFunctionBegin;
17050700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1706ce94432eSBarry Smith   if (jac->type != PC_COMPOSITE_SCHUR) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONG, "FieldSplit is not using a Schur complement approach.");
1707a04f6461SBarry Smith   if (A00) *A00 = jac->pmat[0];
1708a04f6461SBarry Smith   if (A01) *A01 = jac->B;
1709a04f6461SBarry Smith   if (A10) *A10 = jac->C;
1710a04f6461SBarry Smith   if (A11) *A11 = jac->pmat[1];
171130ad9308SMatthew Knepley   PetscFunctionReturn(0);
171230ad9308SMatthew Knepley }
171330ad9308SMatthew Knepley 
171479416396SBarry Smith #undef __FUNCT__
171579416396SBarry Smith #define __FUNCT__ "PCFieldSplitSetType_FieldSplit"
17161e6b0712SBarry Smith static PetscErrorCode  PCFieldSplitSetType_FieldSplit(PC pc,PCCompositeType type)
171779416396SBarry Smith {
171879416396SBarry Smith   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
1719e69d4d44SBarry Smith   PetscErrorCode ierr;
172079416396SBarry Smith 
172179416396SBarry Smith   PetscFunctionBegin;
172279416396SBarry Smith   jac->type = type;
17233b224e63SBarry Smith   if (type == PC_COMPOSITE_SCHUR) {
17243b224e63SBarry Smith     pc->ops->apply = PCApply_FieldSplit_Schur;
17253b224e63SBarry Smith     pc->ops->view  = PCView_FieldSplit_Schur;
17262fa5cd67SKarl Rupp 
1727bdf89e91SBarry Smith     ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit_Schur);CHKERRQ(ierr);
1728bdf89e91SBarry Smith     ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSchurPrecondition_C",PCFieldSplitSchurPrecondition_FieldSplit);CHKERRQ(ierr);
1729bdf89e91SBarry Smith     ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C",PCFieldSplitSetSchurFactType_FieldSplit);CHKERRQ(ierr);
1730e69d4d44SBarry Smith 
17313b224e63SBarry Smith   } else {
17323b224e63SBarry Smith     pc->ops->apply = PCApply_FieldSplit;
17333b224e63SBarry Smith     pc->ops->view  = PCView_FieldSplit;
17342fa5cd67SKarl Rupp 
1735bdf89e91SBarry Smith     ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit);CHKERRQ(ierr);
1736bdf89e91SBarry Smith     ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSchurPrecondition_C",0);CHKERRQ(ierr);
1737bdf89e91SBarry Smith     ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetSchurFactType_C",0);CHKERRQ(ierr);
17383b224e63SBarry Smith   }
173979416396SBarry Smith   PetscFunctionReturn(0);
174079416396SBarry Smith }
174179416396SBarry Smith 
174251f519a2SBarry Smith #undef __FUNCT__
174351f519a2SBarry Smith #define __FUNCT__ "PCFieldSplitSetBlockSize_FieldSplit"
17441e6b0712SBarry Smith static PetscErrorCode  PCFieldSplitSetBlockSize_FieldSplit(PC pc,PetscInt bs)
174551f519a2SBarry Smith {
174651f519a2SBarry Smith   PC_FieldSplit *jac = (PC_FieldSplit*)pc->data;
174751f519a2SBarry Smith 
174851f519a2SBarry Smith   PetscFunctionBegin;
1749ce94432eSBarry Smith   if (bs < 1) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Blocksize must be positive, you gave %D",bs);
1750ce94432eSBarry 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);
175151f519a2SBarry Smith   jac->bs = bs;
175251f519a2SBarry Smith   PetscFunctionReturn(0);
175351f519a2SBarry Smith }
175451f519a2SBarry Smith 
175579416396SBarry Smith #undef __FUNCT__
175679416396SBarry Smith #define __FUNCT__ "PCFieldSplitSetType"
1757bc08b0f1SBarry Smith /*@
175879416396SBarry Smith    PCFieldSplitSetType - Sets the type of fieldsplit preconditioner.
175979416396SBarry Smith 
176079416396SBarry Smith    Collective on PC
176179416396SBarry Smith 
176279416396SBarry Smith    Input Parameter:
176379416396SBarry Smith .  pc - the preconditioner context
176481540f2fSBarry Smith .  type - PC_COMPOSITE_ADDITIVE, PC_COMPOSITE_MULTIPLICATIVE (default), PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE, PC_COMPOSITE_SPECIAL, PC_COMPOSITE_SCHUR
176579416396SBarry Smith 
176679416396SBarry Smith    Options Database Key:
1767a4efd8eaSMatthew Knepley .  -pc_fieldsplit_type <type: one of multiplicative, additive, symmetric_multiplicative, special, schur> - Sets fieldsplit preconditioner type
176879416396SBarry Smith 
1769b02e2d75SMatthew G Knepley    Level: Intermediate
177079416396SBarry Smith 
177179416396SBarry Smith .keywords: PC, set, type, composite preconditioner, additive, multiplicative
177279416396SBarry Smith 
177379416396SBarry Smith .seealso: PCCompositeSetType()
177479416396SBarry Smith 
177579416396SBarry Smith @*/
17767087cfbeSBarry Smith PetscErrorCode  PCFieldSplitSetType(PC pc,PCCompositeType type)
177779416396SBarry Smith {
17784ac538c5SBarry Smith   PetscErrorCode ierr;
177979416396SBarry Smith 
178079416396SBarry Smith   PetscFunctionBegin;
17810700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
17824ac538c5SBarry Smith   ierr = PetscTryMethod(pc,"PCFieldSplitSetType_C",(PC,PCCompositeType),(pc,type));CHKERRQ(ierr);
178379416396SBarry Smith   PetscFunctionReturn(0);
178479416396SBarry Smith }
178579416396SBarry Smith 
1786b02e2d75SMatthew G Knepley #undef __FUNCT__
1787b02e2d75SMatthew G Knepley #define __FUNCT__ "PCFieldSplitGetType"
1788b02e2d75SMatthew G Knepley /*@
1789b02e2d75SMatthew G Knepley   PCFieldSplitGetType - Gets the type of fieldsplit preconditioner.
1790b02e2d75SMatthew G Knepley 
1791b02e2d75SMatthew G Knepley   Not collective
1792b02e2d75SMatthew G Knepley 
1793b02e2d75SMatthew G Knepley   Input Parameter:
1794b02e2d75SMatthew G Knepley . pc - the preconditioner context
1795b02e2d75SMatthew G Knepley 
1796b02e2d75SMatthew G Knepley   Output Parameter:
1797b02e2d75SMatthew G Knepley . type - PC_COMPOSITE_ADDITIVE, PC_COMPOSITE_MULTIPLICATIVE (default), PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE, PC_COMPOSITE_SPECIAL, PC_COMPOSITE_SCHUR
1798b02e2d75SMatthew G Knepley 
1799b02e2d75SMatthew G Knepley   Level: Intermediate
1800b02e2d75SMatthew G Knepley 
1801b02e2d75SMatthew G Knepley .keywords: PC, set, type, composite preconditioner, additive, multiplicative
1802b02e2d75SMatthew G Knepley .seealso: PCCompositeSetType()
1803b02e2d75SMatthew G Knepley @*/
1804b02e2d75SMatthew G Knepley PetscErrorCode PCFieldSplitGetType(PC pc, PCCompositeType *type)
1805b02e2d75SMatthew G Knepley {
1806b02e2d75SMatthew G Knepley   PC_FieldSplit *jac = (PC_FieldSplit*) pc->data;
1807b02e2d75SMatthew G Knepley 
1808b02e2d75SMatthew G Knepley   PetscFunctionBegin;
1809b02e2d75SMatthew G Knepley   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1810b02e2d75SMatthew G Knepley   PetscValidIntPointer(type,2);
1811b02e2d75SMatthew G Knepley   *type = jac->type;
1812b02e2d75SMatthew G Knepley   PetscFunctionReturn(0);
1813b02e2d75SMatthew G Knepley }
1814b02e2d75SMatthew G Knepley 
18154ab8060aSDmitry Karpeev #undef __FUNCT__
18164ab8060aSDmitry Karpeev #define __FUNCT__ "PCFieldSplitSetDMSplits"
18174ab8060aSDmitry Karpeev /*@
18184ab8060aSDmitry Karpeev    PCFieldSplitSetDMSplits - Flags whether DMCreateFieldDecomposition() should be used to define the splits, whenever possible.
18194ab8060aSDmitry Karpeev 
18204ab8060aSDmitry Karpeev    Logically Collective
18214ab8060aSDmitry Karpeev 
18224ab8060aSDmitry Karpeev    Input Parameters:
18234ab8060aSDmitry Karpeev +  pc   - the preconditioner context
18244ab8060aSDmitry Karpeev -  flg  - boolean indicating whether to use field splits defined by the DM
18254ab8060aSDmitry Karpeev 
18264ab8060aSDmitry Karpeev    Options Database Key:
18274ab8060aSDmitry Karpeev .  -pc_fieldsplit_dm_splits
18284ab8060aSDmitry Karpeev 
18294ab8060aSDmitry Karpeev    Level: Intermediate
18304ab8060aSDmitry Karpeev 
18314ab8060aSDmitry Karpeev .keywords: PC, DM, composite preconditioner, additive, multiplicative
18324ab8060aSDmitry Karpeev 
18334ab8060aSDmitry Karpeev .seealso: PCFieldSplitGetDMSplits()
18344ab8060aSDmitry Karpeev 
18354ab8060aSDmitry Karpeev @*/
18364ab8060aSDmitry Karpeev PetscErrorCode  PCFieldSplitSetDMSplits(PC pc,PetscBool flg)
18374ab8060aSDmitry Karpeev {
18384ab8060aSDmitry Karpeev   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
18394ab8060aSDmitry Karpeev   PetscBool      isfs;
18404ab8060aSDmitry Karpeev   PetscErrorCode ierr;
18414ab8060aSDmitry Karpeev 
18424ab8060aSDmitry Karpeev   PetscFunctionBegin;
18434ab8060aSDmitry Karpeev   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
18444ab8060aSDmitry Karpeev   PetscValidLogicalCollectiveBool(pc,flg,2);
18454ab8060aSDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);CHKERRQ(ierr);
18464ab8060aSDmitry Karpeev   if (isfs) {
18474ab8060aSDmitry Karpeev     jac->dm_splits = flg;
18484ab8060aSDmitry Karpeev   }
18494ab8060aSDmitry Karpeev   PetscFunctionReturn(0);
18504ab8060aSDmitry Karpeev }
18514ab8060aSDmitry Karpeev 
18524ab8060aSDmitry Karpeev 
18534ab8060aSDmitry Karpeev #undef __FUNCT__
18544ab8060aSDmitry Karpeev #define __FUNCT__ "PCFieldSplitGetDMSplits"
18554ab8060aSDmitry Karpeev /*@
18564ab8060aSDmitry Karpeev    PCFieldSplitGetDMSplits - Returns flag indicating whether DMCreateFieldDecomposition() should be used to define the splits, whenever possible.
18574ab8060aSDmitry Karpeev 
18584ab8060aSDmitry Karpeev    Logically Collective
18594ab8060aSDmitry Karpeev 
18604ab8060aSDmitry Karpeev    Input Parameter:
18614ab8060aSDmitry Karpeev .  pc   - the preconditioner context
18624ab8060aSDmitry Karpeev 
18634ab8060aSDmitry Karpeev    Output Parameter:
18644ab8060aSDmitry Karpeev .  flg  - boolean indicating whether to use field splits defined by the DM
18654ab8060aSDmitry Karpeev 
18664ab8060aSDmitry Karpeev    Level: Intermediate
18674ab8060aSDmitry Karpeev 
18684ab8060aSDmitry Karpeev .keywords: PC, DM, composite preconditioner, additive, multiplicative
18694ab8060aSDmitry Karpeev 
18704ab8060aSDmitry Karpeev .seealso: PCFieldSplitSetDMSplits()
18714ab8060aSDmitry Karpeev 
18724ab8060aSDmitry Karpeev @*/
18734ab8060aSDmitry Karpeev PetscErrorCode  PCFieldSplitGetDMSplits(PC pc,PetscBool* flg)
18744ab8060aSDmitry Karpeev {
18754ab8060aSDmitry Karpeev   PC_FieldSplit  *jac = (PC_FieldSplit*)pc->data;
18764ab8060aSDmitry Karpeev   PetscBool      isfs;
18774ab8060aSDmitry Karpeev   PetscErrorCode ierr;
18784ab8060aSDmitry Karpeev 
18794ab8060aSDmitry Karpeev   PetscFunctionBegin;
18804ab8060aSDmitry Karpeev   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
18814ab8060aSDmitry Karpeev   PetscValidPointer(flg,2);
18824ab8060aSDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&isfs);CHKERRQ(ierr);
18834ab8060aSDmitry Karpeev   if (isfs) {
18844ab8060aSDmitry Karpeev     if(flg) *flg = jac->dm_splits;
18854ab8060aSDmitry Karpeev   }
18864ab8060aSDmitry Karpeev   PetscFunctionReturn(0);
18874ab8060aSDmitry Karpeev }
18884ab8060aSDmitry Karpeev 
18890971522cSBarry Smith /* -------------------------------------------------------------------------------------*/
18900971522cSBarry Smith /*MC
1891a8c7a070SBarry Smith    PCFIELDSPLIT - Preconditioner created by combining separate preconditioners for individual
1892a04f6461SBarry Smith                   fields or groups of fields. See the users manual section "Solving Block Matrices" for more details.
18930971522cSBarry Smith 
1894edf189efSBarry Smith      To set options on the solvers for each block append -fieldsplit_ to all the PC
1895edf189efSBarry Smith         options database keys. For example, -fieldsplit_pc_type ilu -fieldsplit_pc_factor_levels 1
18960971522cSBarry Smith 
1897a8c7a070SBarry Smith      To set the options on the solvers separate for each block call PCFieldSplitGetSubKSP()
189869a612a9SBarry Smith          and set the options directly on the resulting KSP object
18990971522cSBarry Smith 
19000971522cSBarry Smith    Level: intermediate
19010971522cSBarry Smith 
190279416396SBarry Smith    Options Database Keys:
190381540f2fSBarry Smith +   -pc_fieldsplit_%d_fields <a,b,..> - indicates the fields to be used in the %d'th split
190481540f2fSBarry Smith .   -pc_fieldsplit_default - automatically add any fields to additional splits that have not
190581540f2fSBarry Smith                               been supplied explicitly by -pc_fieldsplit_%d_fields
190681540f2fSBarry Smith .   -pc_fieldsplit_block_size <bs> - size of block that defines fields (i.e. there are bs fields)
19070f188ba9SJed Brown .   -pc_fieldsplit_type <additive,multiplicative,symmetric_multiplicative,schur> - type of relaxation or factorization splitting
19082e71c61dSMatthew G. Knepley .   -pc_fieldsplit_schur_precondition <self,selfp,user,a11,full> - default is a11
1909435f959eSBarry Smith .   -pc_fieldsplit_detect_saddle_point - automatically finds rows with zero or negative diagonal and uses Schur complement with no preconditioner as the solver
191079416396SBarry Smith 
19115d4c12cdSJungho Lee -    Options prefix for inner solvers when using Schur complement preconditioner are -fieldsplit_0_ and -fieldsplit_1_
19125d4c12cdSJungho Lee      for all other solvers they are -fieldsplit_%d_ for the dth field, use -fieldsplit_ for all fields
19135d4c12cdSJungho Lee 
1914c8a0d604SMatthew G Knepley    Notes:
1915c8a0d604SMatthew G Knepley     Use PCFieldSplitSetFields() to set fields defined by "strided" entries and PCFieldSplitSetIS()
1916d32f9abdSBarry Smith      to define a field by an arbitrary collection of entries.
1917d32f9abdSBarry Smith 
1918d32f9abdSBarry Smith       If no fields are set the default is used. The fields are defined by entries strided by bs,
1919d32f9abdSBarry Smith       beginning at 0 then 1, etc to bs-1. The block size can be set with PCFieldSplitSetBlockSize(),
1920d32f9abdSBarry Smith       if this is not called the block size defaults to the blocksize of the second matrix passed
1921d32f9abdSBarry Smith       to KSPSetOperators()/PCSetOperators().
1922d32f9abdSBarry Smith 
1923c8a0d604SMatthew G Knepley $     For the Schur complement preconditioner if J = ( A00 A01 )
1924c8a0d604SMatthew G Knepley $                                                    ( A10 A11 )
1925c8a0d604SMatthew G Knepley $     the preconditioner using full factorization is
192613b05affSJed Brown $              ( I   -ksp(A00) A01 ) ( inv(A00)     0  ) (     I          0  )
1927c8a0d604SMatthew G Knepley $              ( 0         I       ) (   0      ksp(S) ) ( -A10 ksp(A00)  I  )
192813b05affSJed Brown      where the action of inv(A00) is applied using the KSP solver with prefix -fieldsplit_0_.  S is the Schur complement
192913b05affSJed Brown $              S = A11 - A10 ksp(A00) A01
193013b05affSJed Brown      which is usually dense and not stored explicitly.  The action of ksp(S) is computed using the KSP solver with prefix -fieldsplit_splitname_ (where splitname was given
1931c8a0d604SMatthew G Knepley      in providing the SECOND split or 1 if not give). For PCFieldSplitGetKSP() when field number is 0,
1932c8a0d604SMatthew G Knepley      it returns the KSP associated with -fieldsplit_0_ while field number 1 gives -fieldsplit_1_ KSP. By default
1933a04f6461SBarry Smith      A11 is used to construct a preconditioner for S, use PCFieldSplitSchurPrecondition() to turn on or off this
1934a7476a74SDmitry Karpeev      option. You can use the preconditioner PCLSC to precondition the Schur complement with -fieldsplit_1_pc_type lsc.
1935a7476a74SDmitry Karpeev      When option -fieldsplit_schur_precondition selfp is given, an approximation to S is assembled --
1936a7476a74SDmitry Karpeev      Sp = A11 - A10 inv(diag(A00)) A01, which has type AIJ and can be used with a variety of preconditioners
1937a7476a74SDmitry Karpeev      (e.g., -fieldsplit_1_pc_type asm).
1938a7476a74SDmitry Karpeev      The factorization type is set using -pc_fieldsplit_schur_fact_type <diag, lower, upper, full>. The full is shown above,
19395668aaf4SBarry Smith      diag gives
1940c8a0d604SMatthew G Knepley $              ( inv(A00)     0   )
1941c8a0d604SMatthew G Knepley $              (   0      -ksp(S) )
19425668aaf4SBarry 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
1943c8a0d604SMatthew G Knepley $              (  A00   0 )
1944c8a0d604SMatthew G Knepley $              (  A10   S )
1945c8a0d604SMatthew G Knepley      where the inverses of A00 and S are applied using KSPs. The upper factorization is the inverse of
1946c8a0d604SMatthew G Knepley $              ( A00 A01 )
1947c8a0d604SMatthew G Knepley $              (  0   S  )
1948c8a0d604SMatthew G Knepley      where again the inverses of A00 and S are applied using KSPs.
1949e69d4d44SBarry Smith 
1950edf189efSBarry Smith      If only one set of indices (one IS) is provided with PCFieldSplitSetIS() then the complement of that IS
1951edf189efSBarry Smith      is used automatically for a second block.
1952edf189efSBarry Smith 
1953ff218e97SBarry Smith      The fieldsplit preconditioner cannot currently be used with the BAIJ or SBAIJ data formats if the blocksize is larger than 1.
1954ff218e97SBarry Smith      Generally it should be used with the AIJ format.
1955ff218e97SBarry Smith 
1956ff218e97SBarry Smith      The forms of these preconditioners are closely related if not identical to forms derived as "Distributive Iterations", see,
1957ff218e97SBarry Smith      for example, page 294 in "Principles of Computational Fluid Dynamics" by Pieter Wesseling. Note that one can also use PCFIELDSPLIT
1958ff218e97SBarry Smith      inside a smoother resulting in "Distributive Smoothers".
19590716a85fSBarry Smith 
1960a541d17aSBarry Smith    Concepts: physics based preconditioners, block preconditioners
19610971522cSBarry Smith 
19627e8cb189SBarry Smith .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC, Block_Preconditioners, PCLSC,
1963e69d4d44SBarry Smith            PCFieldSplitGetSubKSP(), PCFieldSplitSetFields(), PCFieldSplitSetType(), PCFieldSplitSetIS(), PCFieldSplitSchurPrecondition()
19640971522cSBarry Smith M*/
19650971522cSBarry Smith 
19660971522cSBarry Smith #undef __FUNCT__
19670971522cSBarry Smith #define __FUNCT__ "PCCreate_FieldSplit"
19688cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PCCreate_FieldSplit(PC pc)
19690971522cSBarry Smith {
19700971522cSBarry Smith   PetscErrorCode ierr;
19710971522cSBarry Smith   PC_FieldSplit  *jac;
19720971522cSBarry Smith 
19730971522cSBarry Smith   PetscFunctionBegin;
1974b00a9115SJed Brown   ierr = PetscNewLog(pc,&jac);CHKERRQ(ierr);
19752fa5cd67SKarl Rupp 
19760971522cSBarry Smith   jac->bs                 = -1;
19770971522cSBarry Smith   jac->nsplits            = 0;
19783e197d65SBarry Smith   jac->type               = PC_COMPOSITE_MULTIPLICATIVE;
1979e6cab6aaSJed Brown   jac->schurpre           = PC_FIELDSPLIT_SCHUR_PRE_USER; /* Try user preconditioner first, fall back on diagonal */
1980c9c6ffaaSJed Brown   jac->schurfactorization = PC_FIELDSPLIT_SCHUR_FACT_FULL;
1981fbe7908bSJed Brown   jac->dm_splits          = PETSC_TRUE;
198251f519a2SBarry Smith 
19830971522cSBarry Smith   pc->data = (void*)jac;
19840971522cSBarry Smith 
19850971522cSBarry Smith   pc->ops->apply           = PCApply_FieldSplit;
1986421e10b8SBarry Smith   pc->ops->applytranspose  = PCApplyTranspose_FieldSplit;
19870971522cSBarry Smith   pc->ops->setup           = PCSetUp_FieldSplit;
1988574deadeSBarry Smith   pc->ops->reset           = PCReset_FieldSplit;
19890971522cSBarry Smith   pc->ops->destroy         = PCDestroy_FieldSplit;
19900971522cSBarry Smith   pc->ops->setfromoptions  = PCSetFromOptions_FieldSplit;
19910971522cSBarry Smith   pc->ops->view            = PCView_FieldSplit;
19920971522cSBarry Smith   pc->ops->applyrichardson = 0;
19930971522cSBarry Smith 
1994bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitGetSubKSP_C",PCFieldSplitGetSubKSP_FieldSplit);CHKERRQ(ierr);
1995bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetFields_C",PCFieldSplitSetFields_FieldSplit);CHKERRQ(ierr);
1996bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetIS_C",PCFieldSplitSetIS_FieldSplit);CHKERRQ(ierr);
1997bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetType_C",PCFieldSplitSetType_FieldSplit);CHKERRQ(ierr);
1998bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCFieldSplitSetBlockSize_C",PCFieldSplitSetBlockSize_FieldSplit);CHKERRQ(ierr);
19990971522cSBarry Smith   PetscFunctionReturn(0);
20000971522cSBarry Smith }
20010971522cSBarry Smith 
20020971522cSBarry Smith 
2003a541d17aSBarry Smith 
2004