xref: /petsc/src/ksp/pc/impls/composite/composite.c (revision c60c7ad4eb3baa38c2a8deee7f23b741b9653612)
1dba47a55SKris Buschelman 
24b9ad928SBarry Smith /*
34b9ad928SBarry Smith       Defines a preconditioner that can consist of a collection of PCs
44b9ad928SBarry Smith */
507475bc1SBarry Smith #include <petsc-private/pcimpl.h>
6c6db04a5SJed Brown #include <petscksp.h>            /*I "petscksp.h" I*/
74b9ad928SBarry Smith 
84b9ad928SBarry Smith typedef struct _PC_CompositeLink *PC_CompositeLink;
94b9ad928SBarry Smith struct _PC_CompositeLink {
104b9ad928SBarry Smith   PC               pc;
114b9ad928SBarry Smith   PC_CompositeLink next;
12421e10b8SBarry Smith   PC_CompositeLink previous;
134b9ad928SBarry Smith };
144b9ad928SBarry Smith 
154b9ad928SBarry Smith typedef struct {
164b9ad928SBarry Smith   PC_CompositeLink head;
174b9ad928SBarry Smith   PCCompositeType  type;
184b9ad928SBarry Smith   Vec              work1;
194b9ad928SBarry Smith   Vec              work2;
204b9ad928SBarry Smith   PetscScalar      alpha;
214b9ad928SBarry Smith } PC_Composite;
224b9ad928SBarry Smith 
234b9ad928SBarry Smith #undef __FUNCT__
244b9ad928SBarry Smith #define __FUNCT__ "PCApply_Composite_Multiplicative"
256849ba73SBarry Smith static PetscErrorCode PCApply_Composite_Multiplicative(PC pc,Vec x,Vec y)
264b9ad928SBarry Smith {
27dfbe8321SBarry Smith   PetscErrorCode   ierr;
284b9ad928SBarry Smith   PC_Composite     *jac = (PC_Composite*)pc->data;
294b9ad928SBarry Smith   PC_CompositeLink next = jac->head;
304b9ad928SBarry Smith   Mat              mat  = pc->pmat;
314b9ad928SBarry Smith 
324b9ad928SBarry Smith   PetscFunctionBegin;
33ce94432eSBarry Smith   if (!next) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"No composite preconditioners supplied via PCCompositeAddPC() or -pc_composite_pcs");
344b9ad928SBarry Smith   if (next->next && !jac->work2) { /* allocate second work vector */
354b9ad928SBarry Smith     ierr = VecDuplicate(jac->work1,&jac->work2);CHKERRQ(ierr);
364b9ad928SBarry Smith   }
3749517cdeSBarry Smith   if (pc->useAmat) mat = pc->mat;
3809b21952SJed Brown   ierr = PCApply(next->pc,x,y);CHKERRQ(ierr);                      /* y <- B x */
394b9ad928SBarry Smith   while (next->next) {
404b9ad928SBarry Smith     next = next->next;
4109b21952SJed Brown     ierr = MatMult(mat,y,jac->work1);CHKERRQ(ierr);                /* work1 <- A y */
4209b21952SJed Brown     ierr = VecWAXPY(jac->work2,-1.0,jac->work1,x);CHKERRQ(ierr);   /* work2 <- x - work1 */
4351f519a2SBarry Smith     ierr = VecSet(jac->work1,0.0);CHKERRQ(ierr);  /* zero since some PC's may not set all entries in the result */
4409b21952SJed Brown     ierr = PCApply(next->pc,jac->work2,jac->work1);CHKERRQ(ierr);  /* work1 <- C work2 */
4509b21952SJed Brown     ierr = VecAXPY(y,1.0,jac->work1);CHKERRQ(ierr);                /* y <- y + work1 = B x + C (x - A B x) = (B + C (1 - A B)) x */
464b9ad928SBarry Smith   }
47421e10b8SBarry Smith   if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
48421e10b8SBarry Smith     while (next->previous) {
49421e10b8SBarry Smith       next = next->previous;
50421e10b8SBarry Smith       ierr = MatMult(mat,y,jac->work1);CHKERRQ(ierr);
51421e10b8SBarry Smith       ierr = VecWAXPY(jac->work2,-1.0,jac->work1,x);CHKERRQ(ierr);
52421e10b8SBarry Smith       ierr = VecSet(jac->work1,0.0);CHKERRQ(ierr);  /* zero since some PC's may not set all entries in the result */
53421e10b8SBarry Smith       ierr = PCApply(next->pc,jac->work2,jac->work1);CHKERRQ(ierr);
54421e10b8SBarry Smith       ierr = VecAXPY(y,1.0,jac->work1);CHKERRQ(ierr);
55421e10b8SBarry Smith     }
56421e10b8SBarry Smith   }
574b9ad928SBarry Smith   PetscFunctionReturn(0);
584b9ad928SBarry Smith }
594b9ad928SBarry Smith 
602533e041SBarry Smith #undef __FUNCT__
612533e041SBarry Smith #define __FUNCT__ "PCApplyTranspose_Composite_Multiplicative"
622533e041SBarry Smith static PetscErrorCode PCApplyTranspose_Composite_Multiplicative(PC pc,Vec x,Vec y)
632533e041SBarry Smith {
642533e041SBarry Smith   PetscErrorCode   ierr;
652533e041SBarry Smith   PC_Composite     *jac = (PC_Composite*)pc->data;
662533e041SBarry Smith   PC_CompositeLink next = jac->head;
672533e041SBarry Smith   Mat              mat  = pc->pmat;
682533e041SBarry Smith 
692533e041SBarry Smith   PetscFunctionBegin;
70ce94432eSBarry Smith   if (!next) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"No composite preconditioners supplied via PCCompositeAddPC() or -pc_composite_pcs");
712533e041SBarry Smith   if (next->next && !jac->work2) { /* allocate second work vector */
722533e041SBarry Smith     ierr = VecDuplicate(jac->work1,&jac->work2);CHKERRQ(ierr);
732533e041SBarry Smith   }
7449517cdeSBarry Smith   if (pc->useAmat) mat = pc->mat;
752533e041SBarry Smith   /* locate last PC */
762533e041SBarry Smith   while (next->next) {
772533e041SBarry Smith     next = next->next;
782533e041SBarry Smith   }
792533e041SBarry Smith   ierr = PCApplyTranspose(next->pc,x,y);CHKERRQ(ierr);
802533e041SBarry Smith   while (next->previous) {
812533e041SBarry Smith     next = next->previous;
822533e041SBarry Smith     ierr = MatMultTranspose(mat,y,jac->work1);CHKERRQ(ierr);
832533e041SBarry Smith     ierr = VecWAXPY(jac->work2,-1.0,jac->work1,x);CHKERRQ(ierr);
842533e041SBarry Smith     ierr = VecSet(jac->work1,0.0);CHKERRQ(ierr);  /* zero since some PC's may not set all entries in the result */
852533e041SBarry Smith     ierr = PCApplyTranspose(next->pc,jac->work2,jac->work1);CHKERRQ(ierr);
862533e041SBarry Smith     ierr = VecAXPY(y,1.0,jac->work1);CHKERRQ(ierr);
872533e041SBarry Smith   }
882533e041SBarry Smith   if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
892533e041SBarry Smith     next = jac->head;
902533e041SBarry Smith     while (next->next) {
912533e041SBarry Smith       next = next->next;
922533e041SBarry Smith       ierr = MatMultTranspose(mat,y,jac->work1);CHKERRQ(ierr);
932533e041SBarry Smith       ierr = VecWAXPY(jac->work2,-1.0,jac->work1,x);CHKERRQ(ierr);
942533e041SBarry Smith       ierr = VecSet(jac->work1,0.0);CHKERRQ(ierr);  /* zero since some PC's may not set all entries in the result */
952533e041SBarry Smith       ierr = PCApplyTranspose(next->pc,jac->work2,jac->work1);CHKERRQ(ierr);
962533e041SBarry Smith       ierr = VecAXPY(y,1.0,jac->work1);CHKERRQ(ierr);
972533e041SBarry Smith     }
982533e041SBarry Smith   }
992533e041SBarry Smith   PetscFunctionReturn(0);
1002533e041SBarry Smith }
1012533e041SBarry Smith 
1024b9ad928SBarry Smith /*
1034b9ad928SBarry Smith     This is very special for a matrix of the form alpha I + R + S
1044b9ad928SBarry Smith where first preconditioner is built from alpha I + S and second from
1054b9ad928SBarry Smith alpha I + R
1064b9ad928SBarry Smith */
1074b9ad928SBarry Smith #undef __FUNCT__
1084b9ad928SBarry Smith #define __FUNCT__ "PCApply_Composite_Special"
1096849ba73SBarry Smith static PetscErrorCode PCApply_Composite_Special(PC pc,Vec x,Vec y)
1104b9ad928SBarry Smith {
111dfbe8321SBarry Smith   PetscErrorCode   ierr;
1124b9ad928SBarry Smith   PC_Composite     *jac = (PC_Composite*)pc->data;
1134b9ad928SBarry Smith   PC_CompositeLink next = jac->head;
1144b9ad928SBarry Smith 
1154b9ad928SBarry Smith   PetscFunctionBegin;
116ce94432eSBarry Smith   if (!next) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"No composite preconditioners supplied via PCCompositeAddPC() or -pc_composite_pcs");
117ce94432eSBarry Smith   if (!next->next || next->next->next) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Special composite preconditioners requires exactly two PCs");
1184b9ad928SBarry Smith 
119d8fd42c4SBarry Smith   ierr = PCApply(next->pc,x,jac->work1);CHKERRQ(ierr);
120d8fd42c4SBarry Smith   ierr = PCApply(next->next->pc,jac->work1,y);CHKERRQ(ierr);
1214b9ad928SBarry Smith   PetscFunctionReturn(0);
1224b9ad928SBarry Smith }
1234b9ad928SBarry Smith 
1244b9ad928SBarry Smith #undef __FUNCT__
1254b9ad928SBarry Smith #define __FUNCT__ "PCApply_Composite_Additive"
1266849ba73SBarry Smith static PetscErrorCode PCApply_Composite_Additive(PC pc,Vec x,Vec y)
1274b9ad928SBarry Smith {
128dfbe8321SBarry Smith   PetscErrorCode   ierr;
1294b9ad928SBarry Smith   PC_Composite     *jac = (PC_Composite*)pc->data;
1304b9ad928SBarry Smith   PC_CompositeLink next = jac->head;
1314b9ad928SBarry Smith 
1324b9ad928SBarry Smith   PetscFunctionBegin;
133ce94432eSBarry Smith   if (!next) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"No composite preconditioners supplied via PCCompositeAddPC() or -pc_composite_pcs");
134d8fd42c4SBarry Smith   ierr = PCApply(next->pc,x,y);CHKERRQ(ierr);
1354b9ad928SBarry Smith   while (next->next) {
1364b9ad928SBarry Smith     next = next->next;
13751f519a2SBarry Smith     ierr = VecSet(jac->work1,0.0);CHKERRQ(ierr);  /* zero since some PC's may not set all entries in the result */
138d8fd42c4SBarry Smith     ierr = PCApply(next->pc,x,jac->work1);CHKERRQ(ierr);
139efb30889SBarry Smith     ierr = VecAXPY(y,1.0,jac->work1);CHKERRQ(ierr);
1404b9ad928SBarry Smith   }
1414b9ad928SBarry Smith   PetscFunctionReturn(0);
1424b9ad928SBarry Smith }
1434b9ad928SBarry Smith 
1444b9ad928SBarry Smith #undef __FUNCT__
1452533e041SBarry Smith #define __FUNCT__ "PCApplyTranspose_Composite_Additive"
1462533e041SBarry Smith static PetscErrorCode PCApplyTranspose_Composite_Additive(PC pc,Vec x,Vec y)
1472533e041SBarry Smith {
1482533e041SBarry Smith   PetscErrorCode   ierr;
1492533e041SBarry Smith   PC_Composite     *jac = (PC_Composite*)pc->data;
1502533e041SBarry Smith   PC_CompositeLink next = jac->head;
1512533e041SBarry Smith 
1522533e041SBarry Smith   PetscFunctionBegin;
153ce94432eSBarry Smith   if (!next) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"No composite preconditioners supplied via PCCompositeAddPC() or -pc_composite_pcs");
1542533e041SBarry Smith   ierr = PCApplyTranspose(next->pc,x,y);CHKERRQ(ierr);
1552533e041SBarry Smith   while (next->next) {
1562533e041SBarry Smith     next = next->next;
1572533e041SBarry Smith     ierr = VecSet(jac->work1,0.0);CHKERRQ(ierr);  /* zero since some PC's may not set all entries in the result */
1582533e041SBarry Smith     ierr = PCApplyTranspose(next->pc,x,jac->work1);CHKERRQ(ierr);
1592533e041SBarry Smith     ierr = VecAXPY(y,1.0,jac->work1);CHKERRQ(ierr);
1602533e041SBarry Smith   }
1612533e041SBarry Smith   PetscFunctionReturn(0);
1622533e041SBarry Smith }
1632533e041SBarry Smith 
1642533e041SBarry Smith #undef __FUNCT__
1654b9ad928SBarry Smith #define __FUNCT__ "PCSetUp_Composite"
1666849ba73SBarry Smith static PetscErrorCode PCSetUp_Composite(PC pc)
1674b9ad928SBarry Smith {
168dfbe8321SBarry Smith   PetscErrorCode   ierr;
1694b9ad928SBarry Smith   PC_Composite     *jac = (PC_Composite*)pc->data;
1704b9ad928SBarry Smith   PC_CompositeLink next = jac->head;
1714b9ad928SBarry Smith 
1724b9ad928SBarry Smith   PetscFunctionBegin;
1734b9ad928SBarry Smith   if (!jac->work1) {
1742a7a6963SBarry Smith     ierr = MatCreateVecs(pc->pmat,&jac->work1,0);CHKERRQ(ierr);
1754b9ad928SBarry Smith   }
1764b9ad928SBarry Smith   while (next) {
17723ee1639SBarry Smith     ierr = PCSetOperators(next->pc,pc->mat,pc->pmat);CHKERRQ(ierr);
1784b9ad928SBarry Smith     next = next->next;
1794b9ad928SBarry Smith   }
1804b9ad928SBarry Smith   PetscFunctionReturn(0);
1814b9ad928SBarry Smith }
1824b9ad928SBarry Smith 
1834b9ad928SBarry Smith #undef __FUNCT__
18469d2c0f9SBarry Smith #define __FUNCT__ "PCReset_Composite"
18569d2c0f9SBarry Smith static PetscErrorCode PCReset_Composite(PC pc)
18669d2c0f9SBarry Smith {
18769d2c0f9SBarry Smith   PC_Composite     *jac = (PC_Composite*)pc->data;
18869d2c0f9SBarry Smith   PetscErrorCode   ierr;
1895f48b12bSBarry Smith   PC_CompositeLink next = jac->head;
19069d2c0f9SBarry Smith 
19169d2c0f9SBarry Smith   PetscFunctionBegin;
19269d2c0f9SBarry Smith   while (next) {
19369d2c0f9SBarry Smith     ierr = PCReset(next->pc);CHKERRQ(ierr);
19469d2c0f9SBarry Smith     next = next->next;
19569d2c0f9SBarry Smith   }
1966bf464f9SBarry Smith   ierr = VecDestroy(&jac->work1);CHKERRQ(ierr);
1976bf464f9SBarry Smith   ierr = VecDestroy(&jac->work2);CHKERRQ(ierr);
19869d2c0f9SBarry Smith   PetscFunctionReturn(0);
19969d2c0f9SBarry Smith }
20069d2c0f9SBarry Smith 
20169d2c0f9SBarry Smith #undef __FUNCT__
2024b9ad928SBarry Smith #define __FUNCT__ "PCDestroy_Composite"
2036849ba73SBarry Smith static PetscErrorCode PCDestroy_Composite(PC pc)
2044b9ad928SBarry Smith {
2054b9ad928SBarry Smith   PC_Composite     *jac = (PC_Composite*)pc->data;
206dfbe8321SBarry Smith   PetscErrorCode   ierr;
207724c2c99SHong Zhang   PC_CompositeLink next = jac->head,next_tmp;
2084b9ad928SBarry Smith 
2094b9ad928SBarry Smith   PetscFunctionBegin;
21069d2c0f9SBarry Smith   ierr = PCReset_Composite(pc);CHKERRQ(ierr);
2114b9ad928SBarry Smith   while (next) {
2126bf464f9SBarry Smith     ierr     = PCDestroy(&next->pc);CHKERRQ(ierr);
213724c2c99SHong Zhang     next_tmp = next;
2144b9ad928SBarry Smith     next     = next->next;
215724c2c99SHong Zhang     ierr     = PetscFree(next_tmp);CHKERRQ(ierr);
2164b9ad928SBarry Smith   }
217c31cb41cSBarry Smith   ierr = PetscFree(pc->data);CHKERRQ(ierr);
2184b9ad928SBarry Smith   PetscFunctionReturn(0);
2194b9ad928SBarry Smith }
2204b9ad928SBarry Smith 
2214b9ad928SBarry Smith #undef __FUNCT__
2224b9ad928SBarry Smith #define __FUNCT__ "PCSetFromOptions_Composite"
2236849ba73SBarry Smith static PetscErrorCode PCSetFromOptions_Composite(PC pc)
2244b9ad928SBarry Smith {
2254b9ad928SBarry Smith   PC_Composite     *jac = (PC_Composite*)pc->data;
226dfbe8321SBarry Smith   PetscErrorCode   ierr;
2279dcbbd2bSBarry Smith   PetscInt         nmax = 8,i;
2284b9ad928SBarry Smith   PC_CompositeLink next;
229e5999256SBarry Smith   char             *pcs[8];
230ace3abfcSBarry Smith   PetscBool        flg;
2314b9ad928SBarry Smith 
2324b9ad928SBarry Smith   PetscFunctionBegin;
2334b9ad928SBarry Smith   ierr = PetscOptionsHead("Composite preconditioner options");CHKERRQ(ierr);
2349dcbbd2bSBarry Smith   ierr = PetscOptionsEnum("-pc_composite_type","Type of composition","PCCompositeSetType",PCCompositeTypes,(PetscEnum)jac->type,(PetscEnum*)&jac->type,&flg);CHKERRQ(ierr);
23551f519a2SBarry Smith   if (flg) {
23651f519a2SBarry Smith     ierr = PCCompositeSetType(pc,jac->type);CHKERRQ(ierr);
23751f519a2SBarry Smith   }
2384b9ad928SBarry Smith   ierr = PetscOptionsStringArray("-pc_composite_pcs","List of composite solvers","PCCompositeAddPC",pcs,&nmax,&flg);CHKERRQ(ierr);
2394b9ad928SBarry Smith   if (flg) {
2404b9ad928SBarry Smith     for (i=0; i<nmax; i++) {
2414b9ad928SBarry Smith       ierr = PCCompositeAddPC(pc,pcs[i]);CHKERRQ(ierr);
242724c2c99SHong Zhang       ierr = PetscFree(pcs[i]);CHKERRQ(ierr);   /* deallocate string pcs[i], which is allocated in PetscOptionsStringArray() */
2434b9ad928SBarry Smith     }
2444b9ad928SBarry Smith   }
2454b9ad928SBarry Smith   ierr = PetscOptionsTail();CHKERRQ(ierr);
2464b9ad928SBarry Smith 
2474b9ad928SBarry Smith   next = jac->head;
2484b9ad928SBarry Smith   while (next) {
2494b9ad928SBarry Smith     ierr = PCSetFromOptions(next->pc);CHKERRQ(ierr);
2504b9ad928SBarry Smith     next = next->next;
2514b9ad928SBarry Smith   }
2524b9ad928SBarry Smith   PetscFunctionReturn(0);
2534b9ad928SBarry Smith }
2544b9ad928SBarry Smith 
2554b9ad928SBarry Smith #undef __FUNCT__
2564b9ad928SBarry Smith #define __FUNCT__ "PCView_Composite"
2576849ba73SBarry Smith static PetscErrorCode PCView_Composite(PC pc,PetscViewer viewer)
2584b9ad928SBarry Smith {
2594b9ad928SBarry Smith   PC_Composite     *jac = (PC_Composite*)pc->data;
260dfbe8321SBarry Smith   PetscErrorCode   ierr;
2614b9ad928SBarry Smith   PC_CompositeLink next = jac->head;
262ace3abfcSBarry Smith   PetscBool        iascii;
2634b9ad928SBarry Smith 
2644b9ad928SBarry Smith   PetscFunctionBegin;
265251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
26632077d6dSBarry Smith   if (iascii) {
2679dcbbd2bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"Composite PC type - %s\n",PCCompositeTypes[jac->type]);CHKERRQ(ierr);
2684b9ad928SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"PCs on composite preconditioner follow\n");CHKERRQ(ierr);
2694b9ad928SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"---------------------------------\n");CHKERRQ(ierr);
2704b9ad928SBarry Smith   }
27132077d6dSBarry Smith   if (iascii) {
2724b9ad928SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2734b9ad928SBarry Smith   }
2744b9ad928SBarry Smith   while (next) {
2754b9ad928SBarry Smith     ierr = PCView(next->pc,viewer);CHKERRQ(ierr);
2764b9ad928SBarry Smith     next = next->next;
2774b9ad928SBarry Smith   }
27832077d6dSBarry Smith   if (iascii) {
2794b9ad928SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2804b9ad928SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"---------------------------------\n");CHKERRQ(ierr);
2814b9ad928SBarry Smith   }
2824b9ad928SBarry Smith   PetscFunctionReturn(0);
2834b9ad928SBarry Smith }
2844b9ad928SBarry Smith 
2854b9ad928SBarry Smith /* ------------------------------------------------------------------------------*/
2864b9ad928SBarry Smith 
2874b9ad928SBarry Smith #undef __FUNCT__
2884b9ad928SBarry Smith #define __FUNCT__ "PCCompositeSpecialSetAlpha_Composite"
2891e6b0712SBarry Smith static PetscErrorCode  PCCompositeSpecialSetAlpha_Composite(PC pc,PetscScalar alpha)
2904b9ad928SBarry Smith {
2914b9ad928SBarry Smith   PC_Composite *jac = (PC_Composite*)pc->data;
2925fd66863SKarl Rupp 
2934b9ad928SBarry Smith   PetscFunctionBegin;
2944b9ad928SBarry Smith   jac->alpha = alpha;
2954b9ad928SBarry Smith   PetscFunctionReturn(0);
2964b9ad928SBarry Smith }
2974b9ad928SBarry Smith 
2984b9ad928SBarry Smith #undef __FUNCT__
2994b9ad928SBarry Smith #define __FUNCT__ "PCCompositeSetType_Composite"
3001e6b0712SBarry Smith static PetscErrorCode  PCCompositeSetType_Composite(PC pc,PCCompositeType type)
3014b9ad928SBarry Smith {
302fad69fbaSJed Brown   PC_Composite *jac = (PC_Composite*)pc->data;
303fad69fbaSJed Brown 
3044b9ad928SBarry Smith   PetscFunctionBegin;
3054b9ad928SBarry Smith   if (type == PC_COMPOSITE_ADDITIVE) {
3064b9ad928SBarry Smith     pc->ops->apply          = PCApply_Composite_Additive;
3072533e041SBarry Smith     pc->ops->applytranspose = PCApplyTranspose_Composite_Additive;
308421e10b8SBarry Smith   } else if (type ==  PC_COMPOSITE_MULTIPLICATIVE || type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) {
3094b9ad928SBarry Smith     pc->ops->apply          = PCApply_Composite_Multiplicative;
3102533e041SBarry Smith     pc->ops->applytranspose = PCApplyTranspose_Composite_Multiplicative;
3114b9ad928SBarry Smith   } else if (type ==  PC_COMPOSITE_SPECIAL) {
3124b9ad928SBarry Smith     pc->ops->apply          = PCApply_Composite_Special;
3130298fd71SBarry Smith     pc->ops->applytranspose = NULL;
314ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONG,"Unkown composite preconditioner type");
315fad69fbaSJed Brown   jac->type = type;
3164b9ad928SBarry Smith   PetscFunctionReturn(0);
3174b9ad928SBarry Smith }
3184b9ad928SBarry Smith 
3194b9ad928SBarry Smith #undef __FUNCT__
320*c60c7ad4SBarry Smith #define __FUNCT__ "PCCompositeGetType_Composite"
321*c60c7ad4SBarry Smith static PetscErrorCode  PCCompositeGetType_Composite(PC pc,PCCompositeType *type)
322*c60c7ad4SBarry Smith {
323*c60c7ad4SBarry Smith   PC_Composite *jac = (PC_Composite*)pc->data;
324*c60c7ad4SBarry Smith 
325*c60c7ad4SBarry Smith   PetscFunctionBegin;
326*c60c7ad4SBarry Smith   *type = jac->type;
327*c60c7ad4SBarry Smith   PetscFunctionReturn(0);
328*c60c7ad4SBarry Smith }
329*c60c7ad4SBarry Smith 
330*c60c7ad4SBarry Smith #undef __FUNCT__
3314b9ad928SBarry Smith #define __FUNCT__ "PCCompositeAddPC_Composite"
3321e6b0712SBarry Smith static PetscErrorCode  PCCompositeAddPC_Composite(PC pc,PCType type)
3334b9ad928SBarry Smith {
3344b9ad928SBarry Smith   PC_Composite     *jac;
3355a9f2f41SSatish Balay   PC_CompositeLink next,ilink;
336dfbe8321SBarry Smith   PetscErrorCode   ierr;
33779416396SBarry Smith   PetscInt         cnt = 0;
3382dcb1b2aSMatthew Knepley   const char       *prefix;
3392dcb1b2aSMatthew Knepley   char             newprefix[8];
3404b9ad928SBarry Smith 
3414b9ad928SBarry Smith   PetscFunctionBegin;
342b00a9115SJed Brown   ierr        = PetscNewLog(pc,&ilink);CHKERRQ(ierr);
3435a9f2f41SSatish Balay   ilink->next = 0;
344ce94432eSBarry Smith   ierr        = PCCreate(PetscObjectComm((PetscObject)pc),&ilink->pc);CHKERRQ(ierr);
345ace3abfcSBarry Smith   ierr        = PetscLogObjectParent((PetscObject)pc,(PetscObject)ilink->pc);CHKERRQ(ierr);
3464b9ad928SBarry Smith 
3474b9ad928SBarry Smith   jac  = (PC_Composite*)pc->data;
3484b9ad928SBarry Smith   next = jac->head;
3494b9ad928SBarry Smith   if (!next) {
3505a9f2f41SSatish Balay     jac->head       = ilink;
3510298fd71SBarry Smith     ilink->previous = NULL;
3524b9ad928SBarry Smith   } else {
3534b9ad928SBarry Smith     cnt++;
3544b9ad928SBarry Smith     while (next->next) {
3554b9ad928SBarry Smith       next = next->next;
3564b9ad928SBarry Smith       cnt++;
3574b9ad928SBarry Smith     }
3585a9f2f41SSatish Balay     next->next      = ilink;
359421e10b8SBarry Smith     ilink->previous = next;
3604b9ad928SBarry Smith   }
3614b9ad928SBarry Smith   ierr = PCGetOptionsPrefix(pc,&prefix);CHKERRQ(ierr);
3625a9f2f41SSatish Balay   ierr = PCSetOptionsPrefix(ilink->pc,prefix);CHKERRQ(ierr);
36313f74950SBarry Smith   sprintf(newprefix,"sub_%d_",(int)cnt);
3645a9f2f41SSatish Balay   ierr = PCAppendOptionsPrefix(ilink->pc,newprefix);CHKERRQ(ierr);
3654b9ad928SBarry Smith   /* type is set after prefix, because some methods may modify prefix, e.g. pcksp */
3665a9f2f41SSatish Balay   ierr = PCSetType(ilink->pc,type);CHKERRQ(ierr);
3674b9ad928SBarry Smith   PetscFunctionReturn(0);
3684b9ad928SBarry Smith }
3694b9ad928SBarry Smith 
3704b9ad928SBarry Smith #undef __FUNCT__
3714b9ad928SBarry Smith #define __FUNCT__ "PCCompositeGetPC_Composite"
3721e6b0712SBarry Smith static PetscErrorCode  PCCompositeGetPC_Composite(PC pc,PetscInt n,PC *subpc)
3734b9ad928SBarry Smith {
3744b9ad928SBarry Smith   PC_Composite     *jac;
3754b9ad928SBarry Smith   PC_CompositeLink next;
37679416396SBarry Smith   PetscInt         i;
3774b9ad928SBarry Smith 
3784b9ad928SBarry Smith   PetscFunctionBegin;
3794b9ad928SBarry Smith   jac  = (PC_Composite*)pc->data;
3804b9ad928SBarry Smith   next = jac->head;
3814b9ad928SBarry Smith   for (i=0; i<n; i++) {
382ce94432eSBarry Smith     if (!next->next) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_INCOMP,"Not enough PCs in composite preconditioner");
3834b9ad928SBarry Smith     next = next->next;
3844b9ad928SBarry Smith   }
3854b9ad928SBarry Smith   *subpc = next->pc;
3864b9ad928SBarry Smith   PetscFunctionReturn(0);
3874b9ad928SBarry Smith }
3884b9ad928SBarry Smith 
3894b9ad928SBarry Smith /* -------------------------------------------------------------------------------- */
3904b9ad928SBarry Smith #undef __FUNCT__
3914b9ad928SBarry Smith #define __FUNCT__ "PCCompositeSetType"
392f39d8e23SSatish Balay /*@
3934b9ad928SBarry Smith    PCCompositeSetType - Sets the type of composite preconditioner.
3944b9ad928SBarry Smith 
395ad4df100SBarry Smith    Logically Collective on PC
3964b9ad928SBarry Smith 
397*c60c7ad4SBarry Smith    Input Parameters:
3982a6744ebSBarry Smith +  pc - the preconditioner context
3992a6744ebSBarry Smith -  type - PC_COMPOSITE_ADDITIVE (default), PC_COMPOSITE_MULTIPLICATIVE, PC_COMPOSITE_SPECIAL
4004b9ad928SBarry Smith 
4014b9ad928SBarry Smith    Options Database Key:
4024b9ad928SBarry Smith .  -pc_composite_type <type: one of multiplicative, additive, special> - Sets composite preconditioner type
4034b9ad928SBarry Smith 
4044b9ad928SBarry Smith    Level: Developer
4054b9ad928SBarry Smith 
4064b9ad928SBarry Smith .keywords: PC, set, type, composite preconditioner, additive, multiplicative
4074b9ad928SBarry Smith @*/
4087087cfbeSBarry Smith PetscErrorCode  PCCompositeSetType(PC pc,PCCompositeType type)
4094b9ad928SBarry Smith {
4107bb14e67SBarry Smith   PetscErrorCode ierr;
4114b9ad928SBarry Smith 
4124b9ad928SBarry Smith   PetscFunctionBegin;
4130700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
414c5eb9154SBarry Smith   PetscValidLogicalCollectiveEnum(pc,type,2);
4157bb14e67SBarry Smith   ierr = PetscTryMethod(pc,"PCCompositeSetType_C",(PC,PCCompositeType),(pc,type));CHKERRQ(ierr);
4164b9ad928SBarry Smith   PetscFunctionReturn(0);
4174b9ad928SBarry Smith }
4184b9ad928SBarry Smith 
4194b9ad928SBarry Smith #undef __FUNCT__
420*c60c7ad4SBarry Smith #define __FUNCT__ "PCCompositeGetType"
421*c60c7ad4SBarry Smith /*@
422*c60c7ad4SBarry Smith    PCCompositeSetType - Gets the type of composite preconditioner.
423*c60c7ad4SBarry Smith 
424*c60c7ad4SBarry Smith    Logically Collective on PC
425*c60c7ad4SBarry Smith 
426*c60c7ad4SBarry Smith    Input Parameter:
427*c60c7ad4SBarry Smith .  pc - the preconditioner context
428*c60c7ad4SBarry Smith 
429*c60c7ad4SBarry Smith    Output Parameter:
430*c60c7ad4SBarry Smith .  type - PC_COMPOSITE_ADDITIVE (default), PC_COMPOSITE_MULTIPLICATIVE, PC_COMPOSITE_SPECIAL
431*c60c7ad4SBarry Smith 
432*c60c7ad4SBarry Smith    Options Database Key:
433*c60c7ad4SBarry Smith .  -pc_composite_type <type: one of multiplicative, additive, special> - Sets composite preconditioner type
434*c60c7ad4SBarry Smith 
435*c60c7ad4SBarry Smith    Level: Developer
436*c60c7ad4SBarry Smith 
437*c60c7ad4SBarry Smith .keywords: PC, set, type, composite preconditioner, additive, multiplicative
438*c60c7ad4SBarry Smith @*/
439*c60c7ad4SBarry Smith PetscErrorCode  PCCompositeGetType(PC pc,PCCompositeType *type)
440*c60c7ad4SBarry Smith {
441*c60c7ad4SBarry Smith   PetscErrorCode ierr;
442*c60c7ad4SBarry Smith 
443*c60c7ad4SBarry Smith   PetscFunctionBegin;
444*c60c7ad4SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
445*c60c7ad4SBarry Smith   ierr = PetscUseMethod(pc,"PCCompositeGetType_C",(PC,PCCompositeType*),(pc,type));CHKERRQ(ierr);
446*c60c7ad4SBarry Smith   PetscFunctionReturn(0);
447*c60c7ad4SBarry Smith }
448*c60c7ad4SBarry Smith 
449*c60c7ad4SBarry Smith #undef __FUNCT__
4504b9ad928SBarry Smith #define __FUNCT__ "PCCompositeSpecialSetAlpha"
451f39d8e23SSatish Balay /*@
4524b9ad928SBarry Smith    PCCompositeSpecialSetAlpha - Sets alpha for the special composite preconditioner
4534b9ad928SBarry Smith      for alphaI + R + S
4544b9ad928SBarry Smith 
455ad4df100SBarry Smith    Logically Collective on PC
4564b9ad928SBarry Smith 
4574b9ad928SBarry Smith    Input Parameter:
4584b9ad928SBarry Smith +  pc - the preconditioner context
4594b9ad928SBarry Smith -  alpha - scale on identity
4604b9ad928SBarry Smith 
4614b9ad928SBarry Smith    Level: Developer
4624b9ad928SBarry Smith 
4634b9ad928SBarry Smith .keywords: PC, set, type, composite preconditioner, additive, multiplicative
4644b9ad928SBarry Smith @*/
4657087cfbeSBarry Smith PetscErrorCode  PCCompositeSpecialSetAlpha(PC pc,PetscScalar alpha)
4664b9ad928SBarry Smith {
4674ac538c5SBarry Smith   PetscErrorCode ierr;
4684b9ad928SBarry Smith 
4694b9ad928SBarry Smith   PetscFunctionBegin;
4700700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
471c5eb9154SBarry Smith   PetscValidLogicalCollectiveScalar(pc,alpha,2);
4724ac538c5SBarry Smith   ierr = PetscTryMethod(pc,"PCCompositeSpecialSetAlpha_C",(PC,PetscScalar),(pc,alpha));CHKERRQ(ierr);
4734b9ad928SBarry Smith   PetscFunctionReturn(0);
4744b9ad928SBarry Smith }
4754b9ad928SBarry Smith 
4764b9ad928SBarry Smith #undef __FUNCT__
4774b9ad928SBarry Smith #define __FUNCT__ "PCCompositeAddPC"
4784b9ad928SBarry Smith /*@C
4794b9ad928SBarry Smith    PCCompositeAddPC - Adds another PC to the composite PC.
4804b9ad928SBarry Smith 
4814b9ad928SBarry Smith    Collective on PC
4824b9ad928SBarry Smith 
4834b9ad928SBarry Smith    Input Parameters:
4842a6744ebSBarry Smith +  pc - the preconditioner context
4852a6744ebSBarry Smith -  type - the type of the new preconditioner
4864b9ad928SBarry Smith 
4874b9ad928SBarry Smith    Level: Developer
4884b9ad928SBarry Smith 
4894b9ad928SBarry Smith .keywords: PC, composite preconditioner, add
4904b9ad928SBarry Smith @*/
49119fd82e9SBarry Smith PetscErrorCode  PCCompositeAddPC(PC pc,PCType type)
4924b9ad928SBarry Smith {
4934ac538c5SBarry Smith   PetscErrorCode ierr;
4944b9ad928SBarry Smith 
4954b9ad928SBarry Smith   PetscFunctionBegin;
4960700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
49719fd82e9SBarry Smith   ierr = PetscTryMethod(pc,"PCCompositeAddPC_C",(PC,PCType),(pc,type));CHKERRQ(ierr);
4984b9ad928SBarry Smith   PetscFunctionReturn(0);
4994b9ad928SBarry Smith }
5004b9ad928SBarry Smith 
5014b9ad928SBarry Smith #undef __FUNCT__
5024b9ad928SBarry Smith #define __FUNCT__ "PCCompositeGetPC"
503f39d8e23SSatish Balay /*@
5044b9ad928SBarry Smith    PCCompositeGetPC - Gets one of the PC objects in the composite PC.
5054b9ad928SBarry Smith 
5064b9ad928SBarry Smith    Not Collective
5074b9ad928SBarry Smith 
5084b9ad928SBarry Smith    Input Parameter:
5092a6744ebSBarry Smith +  pc - the preconditioner context
5102a6744ebSBarry Smith -  n - the number of the pc requested
5114b9ad928SBarry Smith 
5124b9ad928SBarry Smith    Output Parameters:
5134b9ad928SBarry Smith .  subpc - the PC requested
5144b9ad928SBarry Smith 
5154b9ad928SBarry Smith    Level: Developer
5164b9ad928SBarry Smith 
5174b9ad928SBarry Smith .keywords: PC, get, composite preconditioner, sub preconditioner
5184b9ad928SBarry Smith 
5194b9ad928SBarry Smith .seealso: PCCompositeAddPC()
5204b9ad928SBarry Smith @*/
5217087cfbeSBarry Smith PetscErrorCode  PCCompositeGetPC(PC pc,PetscInt n,PC *subpc)
5224b9ad928SBarry Smith {
5234ac538c5SBarry Smith   PetscErrorCode ierr;
5244b9ad928SBarry Smith 
5254b9ad928SBarry Smith   PetscFunctionBegin;
5260700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
5274482741eSBarry Smith   PetscValidPointer(subpc,3);
5284ac538c5SBarry Smith   ierr = PetscUseMethod(pc,"PCCompositeGetPC_C",(PC,PetscInt,PC*),(pc,n,subpc));CHKERRQ(ierr);
5294b9ad928SBarry Smith   PetscFunctionReturn(0);
5304b9ad928SBarry Smith }
5314b9ad928SBarry Smith 
5324b9ad928SBarry Smith /* -------------------------------------------------------------------------------------------*/
5334b9ad928SBarry Smith 
5344b9ad928SBarry Smith /*MC
5354b9ad928SBarry Smith      PCCOMPOSITE - Build a preconditioner by composing together several preconditioners
5364b9ad928SBarry Smith 
5374b9ad928SBarry Smith    Options Database Keys:
5382eab2d5bSJungho Lee +  -pc_composite_type <type: one of multiplicative, additive, symmetric_multiplicative, special> - Sets composite preconditioner type
53949517cdeSBarry Smith .  -pc_use_amat - Activates PCSetUseAmat()
54051f519a2SBarry Smith -  -pc_composite_pcs - <pc0,pc1,...> list of PCs to compose
5414b9ad928SBarry Smith 
5424b9ad928SBarry Smith    Level: intermediate
5434b9ad928SBarry Smith 
5444b9ad928SBarry Smith    Concepts: composing solvers
5454b9ad928SBarry Smith 
5464b9ad928SBarry Smith    Notes: To use a Krylov method inside the composite preconditioner, set the PCType of one or more
5474b9ad928SBarry Smith           inner PCs to be PCKSP.
5484b9ad928SBarry Smith           Using a Krylov method inside another Krylov method can be dangerous (you get divergence or
549b3ef52cdSBarry Smith           the incorrect answer) unless you use KSPFGMRES as the outer Krylov method
5504b9ad928SBarry Smith 
5514b9ad928SBarry Smith 
5524b9ad928SBarry Smith .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC,
5534b9ad928SBarry Smith            PCSHELL, PCKSP, PCCompositeSetType(), PCCompositeSpecialSetAlpha(), PCCompositeAddPC(),
55449517cdeSBarry Smith            PCCompositeGetPC(), PCSetUseAmat()
5554b9ad928SBarry Smith 
5564b9ad928SBarry Smith M*/
5574b9ad928SBarry Smith 
5584b9ad928SBarry Smith #undef __FUNCT__
5594b9ad928SBarry Smith #define __FUNCT__ "PCCreate_Composite"
5608cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PCCreate_Composite(PC pc)
5614b9ad928SBarry Smith {
562dfbe8321SBarry Smith   PetscErrorCode ierr;
5634b9ad928SBarry Smith   PC_Composite   *jac;
5644b9ad928SBarry Smith 
5654b9ad928SBarry Smith   PetscFunctionBegin;
566b00a9115SJed Brown   ierr = PetscNewLog(pc,&jac);CHKERRQ(ierr);
5672fa5cd67SKarl Rupp 
5684b9ad928SBarry Smith   pc->ops->apply           = PCApply_Composite_Additive;
5692533e041SBarry Smith   pc->ops->applytranspose  = PCApplyTranspose_Composite_Additive;
5704b9ad928SBarry Smith   pc->ops->setup           = PCSetUp_Composite;
57169d2c0f9SBarry Smith   pc->ops->reset           = PCReset_Composite;
5724b9ad928SBarry Smith   pc->ops->destroy         = PCDestroy_Composite;
5734b9ad928SBarry Smith   pc->ops->setfromoptions  = PCSetFromOptions_Composite;
5744b9ad928SBarry Smith   pc->ops->view            = PCView_Composite;
5754b9ad928SBarry Smith   pc->ops->applyrichardson = 0;
5764b9ad928SBarry Smith 
5774b9ad928SBarry Smith   pc->data   = (void*)jac;
5784b9ad928SBarry Smith   jac->type  = PC_COMPOSITE_ADDITIVE;
5794b9ad928SBarry Smith   jac->work1 = 0;
5804b9ad928SBarry Smith   jac->work2 = 0;
5814b9ad928SBarry Smith   jac->head  = 0;
5824b9ad928SBarry Smith 
583bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCCompositeSetType_C",PCCompositeSetType_Composite);CHKERRQ(ierr);
584*c60c7ad4SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCCompositeGetType_C",PCCompositeGetType_Composite);CHKERRQ(ierr);
585bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCCompositeAddPC_C",PCCompositeAddPC_Composite);CHKERRQ(ierr);
586bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCCompositeGetPC_C",PCCompositeGetPC_Composite);CHKERRQ(ierr);
587bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCCompositeSpecialSetAlpha_C",PCCompositeSpecialSetAlpha_Composite);CHKERRQ(ierr);
5884b9ad928SBarry Smith   PetscFunctionReturn(0);
5894b9ad928SBarry Smith }
5904b9ad928SBarry Smith 
591