1dba47a55SKris Buschelman 24b9ad928SBarry Smith /* 34b9ad928SBarry Smith Defines a preconditioner that can consist of a collection of PCs 44b9ad928SBarry Smith */ 5c6db04a5SJed Brown #include <private/pcimpl.h> /*I "petscpc.h" I*/ 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; 21ace3abfcSBarry Smith PetscBool use_true_matrix; 224b9ad928SBarry Smith } PC_Composite; 234b9ad928SBarry Smith 244b9ad928SBarry Smith #undef __FUNCT__ 254b9ad928SBarry Smith #define __FUNCT__ "PCApply_Composite_Multiplicative" 266849ba73SBarry Smith static PetscErrorCode PCApply_Composite_Multiplicative(PC pc,Vec x,Vec y) 274b9ad928SBarry Smith { 28dfbe8321SBarry Smith PetscErrorCode ierr; 294b9ad928SBarry Smith PC_Composite *jac = (PC_Composite*)pc->data; 304b9ad928SBarry Smith PC_CompositeLink next = jac->head; 314b9ad928SBarry Smith Mat mat = pc->pmat; 324b9ad928SBarry Smith 334b9ad928SBarry Smith PetscFunctionBegin; 34e7e72b3dSBarry Smith if (!next) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_WRONGSTATE,"No composite preconditioners supplied via PCCompositeAddPC() or -pc_composite_pcs"); 354b9ad928SBarry Smith if (next->next && !jac->work2) { /* allocate second work vector */ 364b9ad928SBarry Smith ierr = VecDuplicate(jac->work1,&jac->work2);CHKERRQ(ierr); 374b9ad928SBarry Smith } 384b9ad928SBarry Smith if (jac->use_true_matrix) mat = pc->mat; 392533e041SBarry Smith ierr = PCApply(next->pc,x,y);CHKERRQ(ierr); 404b9ad928SBarry Smith while (next->next) { 414b9ad928SBarry Smith next = next->next; 424b9ad928SBarry Smith ierr = MatMult(mat,y,jac->work1);CHKERRQ(ierr); 43efb30889SBarry Smith ierr = VecWAXPY(jac->work2,-1.0,jac->work1,x);CHKERRQ(ierr); 4451f519a2SBarry Smith ierr = VecSet(jac->work1,0.0);CHKERRQ(ierr); /* zero since some PC's may not set all entries in the result */ 45d8fd42c4SBarry Smith ierr = PCApply(next->pc,jac->work2,jac->work1);CHKERRQ(ierr); 46efb30889SBarry Smith ierr = VecAXPY(y,1.0,jac->work1);CHKERRQ(ierr); 474b9ad928SBarry Smith } 48421e10b8SBarry Smith if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) { 49421e10b8SBarry Smith while (next->previous) { 50421e10b8SBarry Smith next = next->previous; 51421e10b8SBarry Smith ierr = MatMult(mat,y,jac->work1);CHKERRQ(ierr); 52421e10b8SBarry Smith ierr = VecWAXPY(jac->work2,-1.0,jac->work1,x);CHKERRQ(ierr); 53421e10b8SBarry Smith ierr = VecSet(jac->work1,0.0);CHKERRQ(ierr); /* zero since some PC's may not set all entries in the result */ 54421e10b8SBarry Smith ierr = PCApply(next->pc,jac->work2,jac->work1);CHKERRQ(ierr); 55421e10b8SBarry Smith ierr = VecAXPY(y,1.0,jac->work1);CHKERRQ(ierr); 56421e10b8SBarry Smith } 57421e10b8SBarry Smith } 584b9ad928SBarry Smith PetscFunctionReturn(0); 594b9ad928SBarry Smith } 604b9ad928SBarry Smith 612533e041SBarry Smith #undef __FUNCT__ 622533e041SBarry Smith #define __FUNCT__ "PCApplyTranspose_Composite_Multiplicative" 632533e041SBarry Smith static PetscErrorCode PCApplyTranspose_Composite_Multiplicative(PC pc,Vec x,Vec y) 642533e041SBarry Smith { 652533e041SBarry Smith PetscErrorCode ierr; 662533e041SBarry Smith PC_Composite *jac = (PC_Composite*)pc->data; 672533e041SBarry Smith PC_CompositeLink next = jac->head; 682533e041SBarry Smith Mat mat = pc->pmat; 692533e041SBarry Smith 702533e041SBarry Smith PetscFunctionBegin; 712533e041SBarry Smith if (!next) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_WRONGSTATE,"No composite preconditioners supplied via PCCompositeAddPC() or -pc_composite_pcs"); 722533e041SBarry Smith if (next->next && !jac->work2) { /* allocate second work vector */ 732533e041SBarry Smith ierr = VecDuplicate(jac->work1,&jac->work2);CHKERRQ(ierr); 742533e041SBarry Smith } 752533e041SBarry Smith if (jac->use_true_matrix) mat = pc->mat; 762533e041SBarry Smith /* locate last PC */ 772533e041SBarry Smith while (next->next) { 782533e041SBarry Smith next = next->next; 792533e041SBarry Smith } 802533e041SBarry Smith ierr = PCApplyTranspose(next->pc,x,y);CHKERRQ(ierr); 812533e041SBarry Smith while (next->previous) { 822533e041SBarry Smith next = next->previous; 832533e041SBarry Smith ierr = MatMultTranspose(mat,y,jac->work1);CHKERRQ(ierr); 842533e041SBarry Smith ierr = VecWAXPY(jac->work2,-1.0,jac->work1,x);CHKERRQ(ierr); 852533e041SBarry Smith ierr = VecSet(jac->work1,0.0);CHKERRQ(ierr); /* zero since some PC's may not set all entries in the result */ 862533e041SBarry Smith ierr = PCApplyTranspose(next->pc,jac->work2,jac->work1);CHKERRQ(ierr); 872533e041SBarry Smith ierr = VecAXPY(y,1.0,jac->work1);CHKERRQ(ierr); 882533e041SBarry Smith } 892533e041SBarry Smith if (jac->type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) { 902533e041SBarry Smith next = jac->head; 912533e041SBarry Smith while (next->next) { 922533e041SBarry Smith next = next->next; 932533e041SBarry Smith ierr = MatMultTranspose(mat,y,jac->work1);CHKERRQ(ierr); 942533e041SBarry Smith ierr = VecWAXPY(jac->work2,-1.0,jac->work1,x);CHKERRQ(ierr); 952533e041SBarry Smith ierr = VecSet(jac->work1,0.0);CHKERRQ(ierr); /* zero since some PC's may not set all entries in the result */ 962533e041SBarry Smith ierr = PCApplyTranspose(next->pc,jac->work2,jac->work1);CHKERRQ(ierr); 972533e041SBarry Smith ierr = VecAXPY(y,1.0,jac->work1);CHKERRQ(ierr); 982533e041SBarry Smith } 992533e041SBarry Smith } 1002533e041SBarry Smith PetscFunctionReturn(0); 1012533e041SBarry Smith } 1022533e041SBarry Smith 1034b9ad928SBarry Smith /* 1044b9ad928SBarry Smith This is very special for a matrix of the form alpha I + R + S 1054b9ad928SBarry Smith where first preconditioner is built from alpha I + S and second from 1064b9ad928SBarry Smith alpha I + R 1074b9ad928SBarry Smith */ 1084b9ad928SBarry Smith #undef __FUNCT__ 1094b9ad928SBarry Smith #define __FUNCT__ "PCApply_Composite_Special" 1106849ba73SBarry Smith static PetscErrorCode PCApply_Composite_Special(PC pc,Vec x,Vec y) 1114b9ad928SBarry Smith { 112dfbe8321SBarry Smith PetscErrorCode ierr; 1134b9ad928SBarry Smith PC_Composite *jac = (PC_Composite*)pc->data; 1144b9ad928SBarry Smith PC_CompositeLink next = jac->head; 1154b9ad928SBarry Smith 1164b9ad928SBarry Smith PetscFunctionBegin; 117e7e72b3dSBarry Smith if (!next) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_WRONGSTATE,"No composite preconditioners supplied via PCCompositeAddPC() or -pc_composite_pcs"); 118e7e72b3dSBarry Smith if (!next->next || next->next->next) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_WRONGSTATE,"Special composite preconditioners requires exactly two PCs"); 1194b9ad928SBarry Smith 120d8fd42c4SBarry Smith ierr = PCApply(next->pc,x,jac->work1);CHKERRQ(ierr); 121d8fd42c4SBarry Smith ierr = PCApply(next->next->pc,jac->work1,y);CHKERRQ(ierr); 1224b9ad928SBarry Smith PetscFunctionReturn(0); 1234b9ad928SBarry Smith } 1244b9ad928SBarry Smith 1254b9ad928SBarry Smith #undef __FUNCT__ 1264b9ad928SBarry Smith #define __FUNCT__ "PCApply_Composite_Additive" 1276849ba73SBarry Smith static PetscErrorCode PCApply_Composite_Additive(PC pc,Vec x,Vec y) 1284b9ad928SBarry Smith { 129dfbe8321SBarry Smith PetscErrorCode ierr; 1304b9ad928SBarry Smith PC_Composite *jac = (PC_Composite*)pc->data; 1314b9ad928SBarry Smith PC_CompositeLink next = jac->head; 1324b9ad928SBarry Smith 1334b9ad928SBarry Smith PetscFunctionBegin; 134e7e72b3dSBarry Smith if (!next) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_WRONGSTATE,"No composite preconditioners supplied via PCCompositeAddPC() or -pc_composite_pcs"); 135d8fd42c4SBarry Smith ierr = PCApply(next->pc,x,y);CHKERRQ(ierr); 1364b9ad928SBarry Smith while (next->next) { 1374b9ad928SBarry Smith next = next->next; 13851f519a2SBarry Smith ierr = VecSet(jac->work1,0.0);CHKERRQ(ierr); /* zero since some PC's may not set all entries in the result */ 139d8fd42c4SBarry Smith ierr = PCApply(next->pc,x,jac->work1);CHKERRQ(ierr); 140efb30889SBarry Smith ierr = VecAXPY(y,1.0,jac->work1);CHKERRQ(ierr); 1414b9ad928SBarry Smith } 1424b9ad928SBarry Smith PetscFunctionReturn(0); 1434b9ad928SBarry Smith } 1444b9ad928SBarry Smith 1454b9ad928SBarry Smith #undef __FUNCT__ 1462533e041SBarry Smith #define __FUNCT__ "PCApplyTranspose_Composite_Additive" 1472533e041SBarry Smith static PetscErrorCode PCApplyTranspose_Composite_Additive(PC pc,Vec x,Vec y) 1482533e041SBarry Smith { 1492533e041SBarry Smith PetscErrorCode ierr; 1502533e041SBarry Smith PC_Composite *jac = (PC_Composite*)pc->data; 1512533e041SBarry Smith PC_CompositeLink next = jac->head; 1522533e041SBarry Smith 1532533e041SBarry Smith PetscFunctionBegin; 1542533e041SBarry Smith if (!next) SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_WRONGSTATE,"No composite preconditioners supplied via PCCompositeAddPC() or -pc_composite_pcs"); 1552533e041SBarry Smith ierr = PCApplyTranspose(next->pc,x,y);CHKERRQ(ierr); 1562533e041SBarry Smith while (next->next) { 1572533e041SBarry Smith next = next->next; 1582533e041SBarry Smith ierr = VecSet(jac->work1,0.0);CHKERRQ(ierr); /* zero since some PC's may not set all entries in the result */ 1592533e041SBarry Smith ierr = PCApplyTranspose(next->pc,x,jac->work1);CHKERRQ(ierr); 1602533e041SBarry Smith ierr = VecAXPY(y,1.0,jac->work1);CHKERRQ(ierr); 1612533e041SBarry Smith } 1622533e041SBarry Smith PetscFunctionReturn(0); 1632533e041SBarry Smith } 1642533e041SBarry Smith 1652533e041SBarry Smith #undef __FUNCT__ 1664b9ad928SBarry Smith #define __FUNCT__ "PCSetUp_Composite" 1676849ba73SBarry Smith static PetscErrorCode PCSetUp_Composite(PC pc) 1684b9ad928SBarry Smith { 169dfbe8321SBarry Smith PetscErrorCode ierr; 1704b9ad928SBarry Smith PC_Composite *jac = (PC_Composite*)pc->data; 1714b9ad928SBarry Smith PC_CompositeLink next = jac->head; 1724b9ad928SBarry Smith 1734b9ad928SBarry Smith PetscFunctionBegin; 1744b9ad928SBarry Smith if (!jac->work1) { 17523ce1328SBarry Smith ierr = MatGetVecs(pc->pmat,&jac->work1,0);CHKERRQ(ierr); 1764b9ad928SBarry Smith } 1774b9ad928SBarry Smith while (next) { 1784b9ad928SBarry Smith ierr = PCSetOperators(next->pc,pc->mat,pc->pmat,pc->flag);CHKERRQ(ierr); 1794b9ad928SBarry Smith next = next->next; 1804b9ad928SBarry Smith } 1814b9ad928SBarry Smith PetscFunctionReturn(0); 1824b9ad928SBarry Smith } 1834b9ad928SBarry Smith 1844b9ad928SBarry Smith #undef __FUNCT__ 18569d2c0f9SBarry Smith #define __FUNCT__ "PCReset_Composite" 18669d2c0f9SBarry Smith static PetscErrorCode PCReset_Composite(PC pc) 18769d2c0f9SBarry Smith { 18869d2c0f9SBarry Smith PC_Composite *jac = (PC_Composite*)pc->data; 18969d2c0f9SBarry Smith PetscErrorCode ierr; 1905f48b12bSBarry Smith PC_CompositeLink next = jac->head; 19169d2c0f9SBarry Smith 19269d2c0f9SBarry Smith PetscFunctionBegin; 19369d2c0f9SBarry Smith while (next) { 19469d2c0f9SBarry Smith ierr = PCReset(next->pc);CHKERRQ(ierr); 19569d2c0f9SBarry Smith next = next->next; 19669d2c0f9SBarry Smith } 1976bf464f9SBarry Smith ierr = VecDestroy(&jac->work1);CHKERRQ(ierr); 1986bf464f9SBarry Smith ierr = VecDestroy(&jac->work2);CHKERRQ(ierr); 19969d2c0f9SBarry Smith PetscFunctionReturn(0); 20069d2c0f9SBarry Smith } 20169d2c0f9SBarry Smith 20269d2c0f9SBarry Smith #undef __FUNCT__ 2034b9ad928SBarry Smith #define __FUNCT__ "PCDestroy_Composite" 2046849ba73SBarry Smith static PetscErrorCode PCDestroy_Composite(PC pc) 2054b9ad928SBarry Smith { 2064b9ad928SBarry Smith PC_Composite *jac = (PC_Composite*)pc->data; 207dfbe8321SBarry Smith PetscErrorCode ierr; 208724c2c99SHong Zhang PC_CompositeLink next = jac->head,next_tmp; 2094b9ad928SBarry Smith 2104b9ad928SBarry Smith PetscFunctionBegin; 21169d2c0f9SBarry Smith ierr = PCReset_Composite(pc);CHKERRQ(ierr); 2124b9ad928SBarry Smith while (next) { 2136bf464f9SBarry Smith ierr = PCDestroy(&next->pc);CHKERRQ(ierr); 214724c2c99SHong Zhang next_tmp = next; 2154b9ad928SBarry Smith next = next->next; 216724c2c99SHong Zhang ierr = PetscFree(next_tmp);CHKERRQ(ierr); 2174b9ad928SBarry Smith } 218c31cb41cSBarry Smith ierr = PetscFree(pc->data);CHKERRQ(ierr); 2194b9ad928SBarry Smith PetscFunctionReturn(0); 2204b9ad928SBarry Smith } 2214b9ad928SBarry Smith 2224b9ad928SBarry Smith #undef __FUNCT__ 2234b9ad928SBarry Smith #define __FUNCT__ "PCSetFromOptions_Composite" 2246849ba73SBarry Smith static PetscErrorCode PCSetFromOptions_Composite(PC pc) 2254b9ad928SBarry Smith { 2264b9ad928SBarry Smith PC_Composite *jac = (PC_Composite*)pc->data; 227dfbe8321SBarry Smith PetscErrorCode ierr; 2289dcbbd2bSBarry Smith PetscInt nmax = 8,i; 2294b9ad928SBarry Smith PC_CompositeLink next; 230e5999256SBarry Smith char *pcs[8]; 231ace3abfcSBarry Smith PetscBool flg; 2324b9ad928SBarry Smith 2334b9ad928SBarry Smith PetscFunctionBegin; 2344b9ad928SBarry Smith ierr = PetscOptionsHead("Composite preconditioner options");CHKERRQ(ierr); 2359dcbbd2bSBarry Smith ierr = PetscOptionsEnum("-pc_composite_type","Type of composition","PCCompositeSetType",PCCompositeTypes,(PetscEnum)jac->type,(PetscEnum*)&jac->type,&flg);CHKERRQ(ierr); 23651f519a2SBarry Smith if (flg) { 23751f519a2SBarry Smith ierr = PCCompositeSetType(pc,jac->type);CHKERRQ(ierr); 23851f519a2SBarry Smith } 23990d69ab7SBarry Smith flg = PETSC_FALSE; 240acfcf0e5SJed Brown ierr = PetscOptionsBool("-pc_composite_true","Use true matrix for inner solves","PCCompositeSetUseTrue",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 2414b9ad928SBarry Smith if (flg) { 2424b9ad928SBarry Smith ierr = PCCompositeSetUseTrue(pc);CHKERRQ(ierr); 2434b9ad928SBarry Smith } 2444b9ad928SBarry Smith ierr = PetscOptionsStringArray("-pc_composite_pcs","List of composite solvers","PCCompositeAddPC",pcs,&nmax,&flg);CHKERRQ(ierr); 2454b9ad928SBarry Smith if (flg) { 2464b9ad928SBarry Smith for (i=0; i<nmax; i++) { 2474b9ad928SBarry Smith ierr = PCCompositeAddPC(pc,pcs[i]);CHKERRQ(ierr); 248724c2c99SHong Zhang ierr = PetscFree(pcs[i]);CHKERRQ(ierr); /* deallocate string pcs[i], which is allocated in PetscOptionsStringArray() */ 2494b9ad928SBarry Smith } 2504b9ad928SBarry Smith } 2514b9ad928SBarry Smith ierr = PetscOptionsTail();CHKERRQ(ierr); 2524b9ad928SBarry Smith 2534b9ad928SBarry Smith next = jac->head; 2544b9ad928SBarry Smith while (next) { 2554b9ad928SBarry Smith ierr = PCSetFromOptions(next->pc);CHKERRQ(ierr); 2564b9ad928SBarry Smith next = next->next; 2574b9ad928SBarry Smith } 2584b9ad928SBarry Smith PetscFunctionReturn(0); 2594b9ad928SBarry Smith } 2604b9ad928SBarry Smith 2614b9ad928SBarry Smith #undef __FUNCT__ 2624b9ad928SBarry Smith #define __FUNCT__ "PCView_Composite" 2636849ba73SBarry Smith static PetscErrorCode PCView_Composite(PC pc,PetscViewer viewer) 2644b9ad928SBarry Smith { 2654b9ad928SBarry Smith PC_Composite *jac = (PC_Composite*)pc->data; 266dfbe8321SBarry Smith PetscErrorCode ierr; 2674b9ad928SBarry Smith PC_CompositeLink next = jac->head; 268ace3abfcSBarry Smith PetscBool iascii; 2694b9ad928SBarry Smith 2704b9ad928SBarry Smith PetscFunctionBegin; 2712692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 27232077d6dSBarry Smith if (iascii) { 2739dcbbd2bSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Composite PC type - %s\n",PCCompositeTypes[jac->type]);CHKERRQ(ierr); 2744b9ad928SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"PCs on composite preconditioner follow\n");CHKERRQ(ierr); 2754b9ad928SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"---------------------------------\n");CHKERRQ(ierr); 2764b9ad928SBarry Smith } else { 27765e19b50SBarry Smith SETERRQ1(((PetscObject)pc)->comm,PETSC_ERR_SUP,"Viewer type %s not supported for PCComposite",((PetscObject)viewer)->type_name); 2784b9ad928SBarry Smith } 27932077d6dSBarry Smith if (iascii) { 2804b9ad928SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 2814b9ad928SBarry Smith } 2824b9ad928SBarry Smith while (next) { 2834b9ad928SBarry Smith ierr = PCView(next->pc,viewer);CHKERRQ(ierr); 2844b9ad928SBarry Smith next = next->next; 2854b9ad928SBarry Smith } 28632077d6dSBarry Smith if (iascii) { 2874b9ad928SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2884b9ad928SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"---------------------------------\n");CHKERRQ(ierr); 2894b9ad928SBarry Smith } 2904b9ad928SBarry Smith PetscFunctionReturn(0); 2914b9ad928SBarry Smith } 2924b9ad928SBarry Smith 2934b9ad928SBarry Smith /* ------------------------------------------------------------------------------*/ 2944b9ad928SBarry Smith 2954b9ad928SBarry Smith EXTERN_C_BEGIN 2964b9ad928SBarry Smith #undef __FUNCT__ 2974b9ad928SBarry Smith #define __FUNCT__ "PCCompositeSpecialSetAlpha_Composite" 2987087cfbeSBarry Smith PetscErrorCode PCCompositeSpecialSetAlpha_Composite(PC pc,PetscScalar alpha) 2994b9ad928SBarry Smith { 3004b9ad928SBarry Smith PC_Composite *jac = (PC_Composite*)pc->data; 3014b9ad928SBarry Smith PetscFunctionBegin; 3024b9ad928SBarry Smith jac->alpha = alpha; 3034b9ad928SBarry Smith PetscFunctionReturn(0); 3044b9ad928SBarry Smith } 3054b9ad928SBarry Smith EXTERN_C_END 3064b9ad928SBarry Smith 3074b9ad928SBarry Smith EXTERN_C_BEGIN 3084b9ad928SBarry Smith #undef __FUNCT__ 3094b9ad928SBarry Smith #define __FUNCT__ "PCCompositeSetType_Composite" 3107087cfbeSBarry Smith PetscErrorCode PCCompositeSetType_Composite(PC pc,PCCompositeType type) 3114b9ad928SBarry Smith { 3124b9ad928SBarry Smith PetscFunctionBegin; 3134b9ad928SBarry Smith if (type == PC_COMPOSITE_ADDITIVE) { 3144b9ad928SBarry Smith pc->ops->apply = PCApply_Composite_Additive; 3152533e041SBarry Smith pc->ops->applytranspose = PCApplyTranspose_Composite_Additive; 316421e10b8SBarry Smith } else if (type == PC_COMPOSITE_MULTIPLICATIVE || type == PC_COMPOSITE_SYMMETRIC_MULTIPLICATIVE) { 3174b9ad928SBarry Smith pc->ops->apply = PCApply_Composite_Multiplicative; 3182533e041SBarry Smith pc->ops->applytranspose = PCApplyTranspose_Composite_Multiplicative; 3194b9ad928SBarry Smith } else if (type == PC_COMPOSITE_SPECIAL) { 3204b9ad928SBarry Smith pc->ops->apply = PCApply_Composite_Special; 3212533e041SBarry Smith pc->ops->applytranspose = PETSC_NULL; 322e7e72b3dSBarry Smith } else SETERRQ(((PetscObject)pc)->comm,PETSC_ERR_ARG_WRONG,"Unkown composite preconditioner type"); 3234b9ad928SBarry Smith PetscFunctionReturn(0); 3244b9ad928SBarry Smith } 3254b9ad928SBarry Smith EXTERN_C_END 3264b9ad928SBarry Smith 3274b9ad928SBarry Smith EXTERN_C_BEGIN 3284b9ad928SBarry Smith #undef __FUNCT__ 3294b9ad928SBarry Smith #define __FUNCT__ "PCCompositeAddPC_Composite" 3307087cfbeSBarry Smith PetscErrorCode PCCompositeAddPC_Composite(PC pc,PCType type) 3314b9ad928SBarry Smith { 3324b9ad928SBarry Smith PC_Composite *jac; 3335a9f2f41SSatish Balay PC_CompositeLink next,ilink; 334dfbe8321SBarry Smith PetscErrorCode ierr; 33579416396SBarry Smith PetscInt cnt = 0; 3362dcb1b2aSMatthew Knepley const char *prefix; 3372dcb1b2aSMatthew Knepley char newprefix[8]; 3384b9ad928SBarry Smith 3394b9ad928SBarry Smith PetscFunctionBegin; 34038f2d2fdSLisandro Dalcin ierr = PetscNewLog(pc,struct _PC_CompositeLink,&ilink);CHKERRQ(ierr); 3415a9f2f41SSatish Balay ilink->next = 0; 3427adad957SLisandro Dalcin ierr = PCCreate(((PetscObject)pc)->comm,&ilink->pc);CHKERRQ(ierr); 343ace3abfcSBarry Smith ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)ilink->pc);CHKERRQ(ierr); 3444b9ad928SBarry Smith 3454b9ad928SBarry Smith jac = (PC_Composite*)pc->data; 3464b9ad928SBarry Smith next = jac->head; 3474b9ad928SBarry Smith if (!next) { 3485a9f2f41SSatish Balay jac->head = ilink; 349421e10b8SBarry Smith ilink->previous = PETSC_NULL; 3504b9ad928SBarry Smith } else { 3514b9ad928SBarry Smith cnt++; 3524b9ad928SBarry Smith while (next->next) { 3534b9ad928SBarry Smith next = next->next; 3544b9ad928SBarry Smith cnt++; 3554b9ad928SBarry Smith } 3565a9f2f41SSatish Balay next->next = ilink; 357421e10b8SBarry Smith ilink->previous = next; 3584b9ad928SBarry Smith } 3594b9ad928SBarry Smith ierr = PCGetOptionsPrefix(pc,&prefix);CHKERRQ(ierr); 3605a9f2f41SSatish Balay ierr = PCSetOptionsPrefix(ilink->pc,prefix);CHKERRQ(ierr); 36113f74950SBarry Smith sprintf(newprefix,"sub_%d_",(int)cnt); 3625a9f2f41SSatish Balay ierr = PCAppendOptionsPrefix(ilink->pc,newprefix);CHKERRQ(ierr); 3634b9ad928SBarry Smith /* type is set after prefix, because some methods may modify prefix, e.g. pcksp */ 3645a9f2f41SSatish Balay ierr = PCSetType(ilink->pc,type);CHKERRQ(ierr); 3654b9ad928SBarry Smith PetscFunctionReturn(0); 3664b9ad928SBarry Smith } 3674b9ad928SBarry Smith EXTERN_C_END 3684b9ad928SBarry Smith 3694b9ad928SBarry Smith EXTERN_C_BEGIN 3704b9ad928SBarry Smith #undef __FUNCT__ 3714b9ad928SBarry Smith #define __FUNCT__ "PCCompositeGetPC_Composite" 3727087cfbeSBarry Smith 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++) { 382e7e72b3dSBarry Smith if (!next->next) SETERRQ(((PetscObject)pc)->comm,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 EXTERN_C_END 3894b9ad928SBarry Smith 3904b9ad928SBarry Smith EXTERN_C_BEGIN 3914b9ad928SBarry Smith #undef __FUNCT__ 3924b9ad928SBarry Smith #define __FUNCT__ "PCCompositeSetUseTrue_Composite" 3937087cfbeSBarry Smith PetscErrorCode PCCompositeSetUseTrue_Composite(PC pc) 3944b9ad928SBarry Smith { 3954b9ad928SBarry Smith PC_Composite *jac; 3964b9ad928SBarry Smith 3974b9ad928SBarry Smith PetscFunctionBegin; 3984b9ad928SBarry Smith jac = (PC_Composite*)pc->data; 3994b9ad928SBarry Smith jac->use_true_matrix = PETSC_TRUE; 4004b9ad928SBarry Smith PetscFunctionReturn(0); 4014b9ad928SBarry Smith } 4024b9ad928SBarry Smith EXTERN_C_END 4034b9ad928SBarry Smith 4044b9ad928SBarry Smith /* -------------------------------------------------------------------------------- */ 4054b9ad928SBarry Smith #undef __FUNCT__ 4064b9ad928SBarry Smith #define __FUNCT__ "PCCompositeSetType" 407f39d8e23SSatish Balay /*@ 4084b9ad928SBarry Smith PCCompositeSetType - Sets the type of composite preconditioner. 4094b9ad928SBarry Smith 410ad4df100SBarry Smith Logically Collective on PC 4114b9ad928SBarry Smith 4124b9ad928SBarry Smith Input Parameter: 4132a6744ebSBarry Smith + pc - the preconditioner context 4142a6744ebSBarry Smith - type - PC_COMPOSITE_ADDITIVE (default), PC_COMPOSITE_MULTIPLICATIVE, PC_COMPOSITE_SPECIAL 4154b9ad928SBarry Smith 4164b9ad928SBarry Smith Options Database Key: 4174b9ad928SBarry Smith . -pc_composite_type <type: one of multiplicative, additive, special> - Sets composite preconditioner type 4184b9ad928SBarry Smith 4194b9ad928SBarry Smith Level: Developer 4204b9ad928SBarry Smith 4214b9ad928SBarry Smith .keywords: PC, set, type, composite preconditioner, additive, multiplicative 4224b9ad928SBarry Smith @*/ 4237087cfbeSBarry Smith PetscErrorCode PCCompositeSetType(PC pc,PCCompositeType type) 4244b9ad928SBarry Smith { 4257bb14e67SBarry Smith PetscErrorCode ierr; 4264b9ad928SBarry Smith 4274b9ad928SBarry Smith PetscFunctionBegin; 4280700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 429c5eb9154SBarry Smith PetscValidLogicalCollectiveEnum(pc,type,2); 4307bb14e67SBarry Smith ierr = PetscTryMethod(pc,"PCCompositeSetType_C",(PC,PCCompositeType),(pc,type));CHKERRQ(ierr); 4314b9ad928SBarry Smith PetscFunctionReturn(0); 4324b9ad928SBarry Smith } 4334b9ad928SBarry Smith 4344b9ad928SBarry Smith #undef __FUNCT__ 4354b9ad928SBarry Smith #define __FUNCT__ "PCCompositeSpecialSetAlpha" 436f39d8e23SSatish Balay /*@ 4374b9ad928SBarry Smith PCCompositeSpecialSetAlpha - Sets alpha for the special composite preconditioner 4384b9ad928SBarry Smith for alphaI + R + S 4394b9ad928SBarry Smith 440ad4df100SBarry Smith Logically Collective on PC 4414b9ad928SBarry Smith 4424b9ad928SBarry Smith Input Parameter: 4434b9ad928SBarry Smith + pc - the preconditioner context 4444b9ad928SBarry Smith - alpha - scale on identity 4454b9ad928SBarry Smith 4464b9ad928SBarry Smith Level: Developer 4474b9ad928SBarry Smith 4484b9ad928SBarry Smith .keywords: PC, set, type, composite preconditioner, additive, multiplicative 4494b9ad928SBarry Smith @*/ 4507087cfbeSBarry Smith PetscErrorCode PCCompositeSpecialSetAlpha(PC pc,PetscScalar alpha) 4514b9ad928SBarry Smith { 4524ac538c5SBarry Smith PetscErrorCode ierr; 4534b9ad928SBarry Smith 4544b9ad928SBarry Smith PetscFunctionBegin; 4550700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 456c5eb9154SBarry Smith PetscValidLogicalCollectiveScalar(pc,alpha,2); 4574ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCCompositeSpecialSetAlpha_C",(PC,PetscScalar),(pc,alpha));CHKERRQ(ierr); 4584b9ad928SBarry Smith PetscFunctionReturn(0); 4594b9ad928SBarry Smith } 4604b9ad928SBarry Smith 4614b9ad928SBarry Smith #undef __FUNCT__ 4624b9ad928SBarry Smith #define __FUNCT__ "PCCompositeAddPC" 4634b9ad928SBarry Smith /*@C 4644b9ad928SBarry Smith PCCompositeAddPC - Adds another PC to the composite PC. 4654b9ad928SBarry Smith 4664b9ad928SBarry Smith Collective on PC 4674b9ad928SBarry Smith 4684b9ad928SBarry Smith Input Parameters: 4692a6744ebSBarry Smith + pc - the preconditioner context 4702a6744ebSBarry Smith - type - the type of the new preconditioner 4714b9ad928SBarry Smith 4724b9ad928SBarry Smith Level: Developer 4734b9ad928SBarry Smith 4744b9ad928SBarry Smith .keywords: PC, composite preconditioner, add 4754b9ad928SBarry Smith @*/ 4767087cfbeSBarry Smith PetscErrorCode PCCompositeAddPC(PC pc,PCType type) 4774b9ad928SBarry Smith { 4784ac538c5SBarry Smith PetscErrorCode ierr; 4794b9ad928SBarry Smith 4804b9ad928SBarry Smith PetscFunctionBegin; 4810700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 4824ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCCompositeAddPC_C",(PC,PCType),(pc,type));CHKERRQ(ierr); 4834b9ad928SBarry Smith PetscFunctionReturn(0); 4844b9ad928SBarry Smith } 4854b9ad928SBarry Smith 4864b9ad928SBarry Smith #undef __FUNCT__ 4874b9ad928SBarry Smith #define __FUNCT__ "PCCompositeGetPC" 488f39d8e23SSatish Balay /*@ 4894b9ad928SBarry Smith PCCompositeGetPC - Gets one of the PC objects in the composite PC. 4904b9ad928SBarry Smith 4914b9ad928SBarry Smith Not Collective 4924b9ad928SBarry Smith 4934b9ad928SBarry Smith Input Parameter: 4942a6744ebSBarry Smith + pc - the preconditioner context 4952a6744ebSBarry Smith - n - the number of the pc requested 4964b9ad928SBarry Smith 4974b9ad928SBarry Smith Output Parameters: 4984b9ad928SBarry Smith . subpc - the PC requested 4994b9ad928SBarry Smith 5004b9ad928SBarry Smith Level: Developer 5014b9ad928SBarry Smith 5024b9ad928SBarry Smith .keywords: PC, get, composite preconditioner, sub preconditioner 5034b9ad928SBarry Smith 5044b9ad928SBarry Smith .seealso: PCCompositeAddPC() 5054b9ad928SBarry Smith @*/ 5067087cfbeSBarry Smith PetscErrorCode PCCompositeGetPC(PC pc,PetscInt n,PC *subpc) 5074b9ad928SBarry Smith { 5084ac538c5SBarry Smith PetscErrorCode ierr; 5094b9ad928SBarry Smith 5104b9ad928SBarry Smith PetscFunctionBegin; 5110700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 5124482741eSBarry Smith PetscValidPointer(subpc,3); 5134ac538c5SBarry Smith ierr = PetscUseMethod(pc,"PCCompositeGetPC_C",(PC,PetscInt,PC *),(pc,n,subpc));CHKERRQ(ierr); 5144b9ad928SBarry Smith PetscFunctionReturn(0); 5154b9ad928SBarry Smith } 5164b9ad928SBarry Smith 5174b9ad928SBarry Smith #undef __FUNCT__ 5184b9ad928SBarry Smith #define __FUNCT__ "PCCompositeSetUseTrue" 5194b9ad928SBarry Smith /*@ 5204b9ad928SBarry Smith PCCompositeSetUseTrue - Sets a flag to indicate that the true matrix (rather than 5214b9ad928SBarry Smith the matrix used to define the preconditioner) is used to compute 5224b9ad928SBarry Smith the residual when the multiplicative scheme is used. 5234b9ad928SBarry Smith 524ad4df100SBarry Smith Logically Collective on PC 5254b9ad928SBarry Smith 5264b9ad928SBarry Smith Input Parameters: 5274b9ad928SBarry Smith . pc - the preconditioner context 5284b9ad928SBarry Smith 5294b9ad928SBarry Smith Options Database Key: 5304b9ad928SBarry Smith . -pc_composite_true - Activates PCCompositeSetUseTrue() 5314b9ad928SBarry Smith 5324b9ad928SBarry Smith Note: 5334b9ad928SBarry Smith For the common case in which the preconditioning and linear 5344b9ad928SBarry Smith system matrices are identical, this routine is unnecessary. 5354b9ad928SBarry Smith 5364b9ad928SBarry Smith Level: Developer 5374b9ad928SBarry Smith 5384b9ad928SBarry Smith .keywords: PC, composite preconditioner, set, true, flag 5394b9ad928SBarry Smith 5404b9ad928SBarry Smith .seealso: PCSetOperators(), PCBJacobiSetUseTrueLocal(), PCKSPSetUseTrue() 5414b9ad928SBarry Smith @*/ 5427087cfbeSBarry Smith PetscErrorCode PCCompositeSetUseTrue(PC pc) 5434b9ad928SBarry Smith { 5444ac538c5SBarry Smith PetscErrorCode ierr; 5454b9ad928SBarry Smith 5464b9ad928SBarry Smith PetscFunctionBegin; 5470700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 5484ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCCompositeSetUseTrue_C",(PC),(pc));CHKERRQ(ierr); 5494b9ad928SBarry Smith PetscFunctionReturn(0); 5504b9ad928SBarry Smith } 5514b9ad928SBarry Smith 5524b9ad928SBarry Smith /* -------------------------------------------------------------------------------------------*/ 5534b9ad928SBarry Smith 5544b9ad928SBarry Smith /*MC 5554b9ad928SBarry Smith PCCOMPOSITE - Build a preconditioner by composing together several preconditioners 5564b9ad928SBarry Smith 5574b9ad928SBarry Smith Options Database Keys: 5582eab2d5bSJungho Lee + -pc_composite_type <type: one of multiplicative, additive, symmetric_multiplicative, special> - Sets composite preconditioner type 5594b9ad928SBarry Smith . -pc_composite_true - Activates PCCompositeSetUseTrue() 56051f519a2SBarry Smith - -pc_composite_pcs - <pc0,pc1,...> list of PCs to compose 5614b9ad928SBarry Smith 5624b9ad928SBarry Smith Level: intermediate 5634b9ad928SBarry Smith 5644b9ad928SBarry Smith Concepts: composing solvers 5654b9ad928SBarry Smith 5664b9ad928SBarry Smith Notes: To use a Krylov method inside the composite preconditioner, set the PCType of one or more 5674b9ad928SBarry Smith inner PCs to be PCKSP. 5684b9ad928SBarry Smith Using a Krylov method inside another Krylov method can be dangerous (you get divergence or 569*b3ef52cdSBarry Smith the incorrect answer) unless you use KSPFGMRES as the outer Krylov method 5704b9ad928SBarry Smith 5714b9ad928SBarry Smith 5724b9ad928SBarry Smith .seealso: PCCreate(), PCSetType(), PCType (for list of available types), PC, 5734b9ad928SBarry Smith PCSHELL, PCKSP, PCCompositeSetType(), PCCompositeSpecialSetAlpha(), PCCompositeAddPC(), 5744b9ad928SBarry Smith PCCompositeGetPC(), PCCompositeSetUseTrue() 5754b9ad928SBarry Smith 5764b9ad928SBarry Smith M*/ 5774b9ad928SBarry Smith 5784b9ad928SBarry Smith EXTERN_C_BEGIN 5794b9ad928SBarry Smith #undef __FUNCT__ 5804b9ad928SBarry Smith #define __FUNCT__ "PCCreate_Composite" 5817087cfbeSBarry Smith PetscErrorCode PCCreate_Composite(PC pc) 5824b9ad928SBarry Smith { 583dfbe8321SBarry Smith PetscErrorCode ierr; 5844b9ad928SBarry Smith PC_Composite *jac; 5854b9ad928SBarry Smith 5864b9ad928SBarry Smith PetscFunctionBegin; 58738f2d2fdSLisandro Dalcin ierr = PetscNewLog(pc,PC_Composite,&jac);CHKERRQ(ierr); 5884b9ad928SBarry Smith pc->ops->apply = PCApply_Composite_Additive; 5892533e041SBarry Smith pc->ops->applytranspose = PCApplyTranspose_Composite_Additive; 5904b9ad928SBarry Smith pc->ops->setup = PCSetUp_Composite; 59169d2c0f9SBarry Smith pc->ops->reset = PCReset_Composite; 5924b9ad928SBarry Smith pc->ops->destroy = PCDestroy_Composite; 5934b9ad928SBarry Smith pc->ops->setfromoptions = PCSetFromOptions_Composite; 5944b9ad928SBarry Smith pc->ops->view = PCView_Composite; 5954b9ad928SBarry Smith pc->ops->applyrichardson = 0; 5964b9ad928SBarry Smith 5974b9ad928SBarry Smith pc->data = (void*)jac; 5984b9ad928SBarry Smith jac->type = PC_COMPOSITE_ADDITIVE; 5994b9ad928SBarry Smith jac->work1 = 0; 6004b9ad928SBarry Smith jac->work2 = 0; 6014b9ad928SBarry Smith jac->head = 0; 6024b9ad928SBarry Smith 6034b9ad928SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCCompositeSetType_C","PCCompositeSetType_Composite", 6044b9ad928SBarry Smith PCCompositeSetType_Composite);CHKERRQ(ierr); 6054b9ad928SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCCompositeAddPC_C","PCCompositeAddPC_Composite", 6064b9ad928SBarry Smith PCCompositeAddPC_Composite);CHKERRQ(ierr); 6074b9ad928SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCCompositeGetPC_C","PCCompositeGetPC_Composite", 6084b9ad928SBarry Smith PCCompositeGetPC_Composite);CHKERRQ(ierr); 6094b9ad928SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCCompositeSetUseTrue_C","PCCompositeSetUseTrue_Composite", 6104b9ad928SBarry Smith PCCompositeSetUseTrue_Composite);CHKERRQ(ierr); 6114b9ad928SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCCompositeSpecialSetAlpha_C","PCCompositeSpecialSetAlpha_Composite", 6124b9ad928SBarry Smith PCCompositeSpecialSetAlpha_Composite);CHKERRQ(ierr); 6134b9ad928SBarry Smith 6144b9ad928SBarry Smith PetscFunctionReturn(0); 6154b9ad928SBarry Smith } 6164b9ad928SBarry Smith EXTERN_C_END 6174b9ad928SBarry Smith 618