14d0a8057SBarry Smith 24b9ad928SBarry Smith /* 34b9ad928SBarry Smith This provides a simple shell for Fortran (and C programmers) to 44b9ad928SBarry Smith create their own preconditioner without writing much interface code. 54b9ad928SBarry Smith */ 64b9ad928SBarry Smith 7af0996ceSBarry Smith #include <petsc/private/pcimpl.h> /*I "petscpc.h" I*/ 84b9ad928SBarry Smith 94b9ad928SBarry Smith typedef struct { 10be29d3c6SBarry Smith void *ctx; /* user provided contexts for preconditioner */ 112fa5cd67SKarl Rupp 126891c3e4SJed Brown PetscErrorCode (*destroy)(PC); 136891c3e4SJed Brown PetscErrorCode (*setup)(PC); 146891c3e4SJed Brown PetscErrorCode (*apply)(PC,Vec,Vec); 151b581b66SBarry Smith PetscErrorCode (*applysymmetricleft)(PC,Vec,Vec); 161b581b66SBarry Smith PetscErrorCode (*applysymmetricright)(PC,Vec,Vec); 176891c3e4SJed Brown PetscErrorCode (*applyBA)(PC,PCSide,Vec,Vec,Vec); 186891c3e4SJed Brown PetscErrorCode (*presolve)(PC,KSP,Vec,Vec); 196891c3e4SJed Brown PetscErrorCode (*postsolve)(PC,KSP,Vec,Vec); 206891c3e4SJed Brown PetscErrorCode (*view)(PC,PetscViewer); 216891c3e4SJed Brown PetscErrorCode (*applytranspose)(PC,Vec,Vec); 22ace3abfcSBarry Smith PetscErrorCode (*applyrich)(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool,PetscInt*,PCRichardsonConvergedReason*); 232fa5cd67SKarl Rupp 244b9ad928SBarry Smith char *name; 254b9ad928SBarry Smith } PC_Shell; 264b9ad928SBarry Smith 27b29801fcSSatish Balay /*@C 28be29d3c6SBarry Smith PCShellGetContext - Returns the user-provided context associated with a shell PC 29be29d3c6SBarry Smith 30be29d3c6SBarry Smith Not Collective 31be29d3c6SBarry Smith 32be29d3c6SBarry Smith Input Parameter: 33c5ae4b9aSBarry Smith . pc - should have been created with PCSetType(pc,shell) 34be29d3c6SBarry Smith 35be29d3c6SBarry Smith Output Parameter: 36be29d3c6SBarry Smith . ctx - the user provided context 37be29d3c6SBarry Smith 38be29d3c6SBarry Smith Level: advanced 39be29d3c6SBarry Smith 40be29d3c6SBarry Smith Notes: 41be29d3c6SBarry Smith This routine is intended for use within various shell routines 42be29d3c6SBarry Smith 4395452b02SPatrick Sanan Fortran Notes: 4495452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 45daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 46daf670e6SBarry Smith 47c5ae4b9aSBarry Smith .seealso: PCShellSetContext() 48be29d3c6SBarry Smith @*/ 497087cfbeSBarry Smith PetscErrorCode PCShellGetContext(PC pc,void **ctx) 50be29d3c6SBarry Smith { 51be29d3c6SBarry Smith PetscErrorCode ierr; 52ace3abfcSBarry Smith PetscBool flg; 53be29d3c6SBarry Smith 54be29d3c6SBarry Smith PetscFunctionBegin; 550700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 56be29d3c6SBarry Smith PetscValidPointer(ctx,2); 57251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr); 58be29d3c6SBarry Smith if (!flg) *ctx = 0; 59be29d3c6SBarry Smith else *ctx = ((PC_Shell*)(pc->data))->ctx; 60be29d3c6SBarry Smith PetscFunctionReturn(0); 61be29d3c6SBarry Smith } 62be29d3c6SBarry Smith 639dd1005fSJed Brown /*@ 64be29d3c6SBarry Smith PCShellSetContext - sets the context for a shell PC 65be29d3c6SBarry Smith 663f9fe445SBarry Smith Logically Collective on PC 67be29d3c6SBarry Smith 68be29d3c6SBarry Smith Input Parameters: 69be29d3c6SBarry Smith + pc - the shell PC 70be29d3c6SBarry Smith - ctx - the context 71be29d3c6SBarry Smith 72be29d3c6SBarry Smith Level: advanced 73be29d3c6SBarry Smith 7495452b02SPatrick Sanan Fortran Notes: 7595452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 76daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 77daf670e6SBarry Smith 78be29d3c6SBarry Smith 796895c445SBarry Smith 80c5ae4b9aSBarry Smith .seealso: PCShellGetContext(), PCSHELL 81be29d3c6SBarry Smith @*/ 827087cfbeSBarry Smith PetscErrorCode PCShellSetContext(PC pc,void *ctx) 83be29d3c6SBarry Smith { 84c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 85be29d3c6SBarry Smith PetscErrorCode ierr; 86ace3abfcSBarry Smith PetscBool flg; 87be29d3c6SBarry Smith 88be29d3c6SBarry Smith PetscFunctionBegin; 890700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 90251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr); 912fa5cd67SKarl Rupp if (flg) shell->ctx = ctx; 92be29d3c6SBarry Smith PetscFunctionReturn(0); 93be29d3c6SBarry Smith } 94be29d3c6SBarry Smith 956849ba73SBarry Smith static PetscErrorCode PCSetUp_Shell(PC pc) 964b9ad928SBarry Smith { 97c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 98dfbe8321SBarry Smith PetscErrorCode ierr; 994b9ad928SBarry Smith 1004b9ad928SBarry Smith PetscFunctionBegin; 101ce94432eSBarry Smith if (!shell->setup) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No setup() routine provided to Shell PC"); 102eb6b5d47SBarry Smith PetscStackCall("PCSHELL user function setup()",ierr = (*shell->setup)(pc);CHKERRQ(ierr)); 1034b9ad928SBarry Smith PetscFunctionReturn(0); 1044b9ad928SBarry Smith } 1054b9ad928SBarry Smith 1066849ba73SBarry Smith static PetscErrorCode PCApply_Shell(PC pc,Vec x,Vec y) 1074b9ad928SBarry Smith { 108c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 109dfbe8321SBarry Smith PetscErrorCode ierr; 110e3f487b0SBarry Smith PetscObjectState instate,outstate; 1114b9ad928SBarry Smith 1124b9ad928SBarry Smith PetscFunctionBegin; 113ce94432eSBarry Smith if (!shell->apply) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No apply() routine provided to Shell PC"); 114e3f487b0SBarry Smith ierr = PetscObjectStateGet((PetscObject)y, &instate);CHKERRQ(ierr); 115eb6b5d47SBarry Smith PetscStackCall("PCSHELL user function apply()",ierr = (*shell->apply)(pc,x,y);CHKERRQ(ierr)); 116e3f487b0SBarry Smith ierr = PetscObjectStateGet((PetscObject)y, &outstate);CHKERRQ(ierr); 117e3f487b0SBarry Smith if (instate == outstate) { 118e3f487b0SBarry Smith /* increase the state of the output vector since the user did not update its state themselve as should have been done */ 119e3f487b0SBarry Smith ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 120e3f487b0SBarry Smith } 1214b9ad928SBarry Smith PetscFunctionReturn(0); 1224b9ad928SBarry Smith } 1234b9ad928SBarry Smith 1241b581b66SBarry Smith static PetscErrorCode PCApplySymmetricLeft_Shell(PC pc,Vec x,Vec y) 1251b581b66SBarry Smith { 1261b581b66SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 1271b581b66SBarry Smith PetscErrorCode ierr; 1281b581b66SBarry Smith 1291b581b66SBarry Smith PetscFunctionBegin; 1301b581b66SBarry Smith if (!shell->applysymmetricleft) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No apply() routine provided to Shell PC"); 1311b581b66SBarry Smith PetscStackCall("PCSHELL user function apply()",ierr = (*shell->applysymmetricleft)(pc,x,y);CHKERRQ(ierr)); 1321b581b66SBarry Smith PetscFunctionReturn(0); 1331b581b66SBarry Smith } 1341b581b66SBarry Smith 1351b581b66SBarry Smith static PetscErrorCode PCApplySymmetricRight_Shell(PC pc,Vec x,Vec y) 1361b581b66SBarry Smith { 1371b581b66SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 1381b581b66SBarry Smith PetscErrorCode ierr; 1391b581b66SBarry Smith 1401b581b66SBarry Smith PetscFunctionBegin; 1411b581b66SBarry Smith if (!shell->applysymmetricright) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No apply() routine provided to Shell PC"); 1421b581b66SBarry Smith PetscStackCall("PCSHELL user function apply()",ierr = (*shell->applysymmetricright)(pc,x,y);CHKERRQ(ierr)); 1431b581b66SBarry Smith PetscFunctionReturn(0); 1441b581b66SBarry Smith } 1451b581b66SBarry Smith 1462bb17772SBarry Smith static PetscErrorCode PCApplyBA_Shell(PC pc,PCSide side,Vec x,Vec y,Vec w) 1472bb17772SBarry Smith { 148c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 1492bb17772SBarry Smith PetscErrorCode ierr; 150e3f487b0SBarry Smith PetscObjectState instate,outstate; 1512bb17772SBarry Smith 1522bb17772SBarry Smith PetscFunctionBegin; 153ce94432eSBarry Smith if (!shell->applyBA) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No applyBA() routine provided to Shell PC"); 154e3f487b0SBarry Smith ierr = PetscObjectStateGet((PetscObject)w, &instate);CHKERRQ(ierr); 155eb6b5d47SBarry Smith PetscStackCall("PCSHELL user function applyBA()",ierr = (*shell->applyBA)(pc,side,x,y,w);CHKERRQ(ierr)); 156e3f487b0SBarry Smith ierr = PetscObjectStateGet((PetscObject)w, &outstate);CHKERRQ(ierr); 157e3f487b0SBarry Smith if (instate == outstate) { 158e3f487b0SBarry Smith /* increase the state of the output vector since the user did not update its state themselve as should have been done */ 159e3f487b0SBarry Smith ierr = PetscObjectStateIncrease((PetscObject)w);CHKERRQ(ierr); 160e3f487b0SBarry Smith } 1612bb17772SBarry Smith PetscFunctionReturn(0); 1622bb17772SBarry Smith } 1632bb17772SBarry Smith 164a06fd7f2SStefano Zampini static PetscErrorCode PCPreSolveChangeRHS_Shell(PC pc,PetscBool* change) 165a06fd7f2SStefano Zampini { 166a06fd7f2SStefano Zampini PetscFunctionBegin; 167a06fd7f2SStefano Zampini *change = PETSC_TRUE; 168a06fd7f2SStefano Zampini PetscFunctionReturn(0); 169a06fd7f2SStefano Zampini } 170a06fd7f2SStefano Zampini 1717cdd61b2SBarry Smith static PetscErrorCode PCPreSolve_Shell(PC pc,KSP ksp,Vec b,Vec x) 1727cdd61b2SBarry Smith { 173c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 1747cdd61b2SBarry Smith PetscErrorCode ierr; 1757cdd61b2SBarry Smith 1767cdd61b2SBarry Smith PetscFunctionBegin; 177ce94432eSBarry Smith if (!shell->presolve) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No presolve() routine provided to Shell PC"); 178eb6b5d47SBarry Smith PetscStackCall("PCSHELL user function presolve()",ierr = (*shell->presolve)(pc,ksp,b,x);CHKERRQ(ierr)); 1797cdd61b2SBarry Smith PetscFunctionReturn(0); 1807cdd61b2SBarry Smith } 1817cdd61b2SBarry Smith 1827cdd61b2SBarry Smith static PetscErrorCode PCPostSolve_Shell(PC pc,KSP ksp,Vec b,Vec x) 1837cdd61b2SBarry Smith { 184c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 1857cdd61b2SBarry Smith PetscErrorCode ierr; 1867cdd61b2SBarry Smith 1877cdd61b2SBarry Smith PetscFunctionBegin; 188ce94432eSBarry Smith if (!shell->postsolve) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No postsolve() routine provided to Shell PC"); 189eb6b5d47SBarry Smith PetscStackCall("PCSHELL user function postsolve()",ierr = (*shell->postsolve)(pc,ksp,b,x);CHKERRQ(ierr)); 1907cdd61b2SBarry Smith PetscFunctionReturn(0); 1917cdd61b2SBarry Smith } 1927cdd61b2SBarry Smith 1936849ba73SBarry Smith static PetscErrorCode PCApplyTranspose_Shell(PC pc,Vec x,Vec y) 1944b9ad928SBarry Smith { 195c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 196dfbe8321SBarry Smith PetscErrorCode ierr; 197e3f487b0SBarry Smith PetscObjectState instate,outstate; 1984b9ad928SBarry Smith 1994b9ad928SBarry Smith PetscFunctionBegin; 200ce94432eSBarry Smith if (!shell->applytranspose) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No applytranspose() routine provided to Shell PC"); 201e3f487b0SBarry Smith ierr = PetscObjectStateGet((PetscObject)y, &instate);CHKERRQ(ierr); 202eb6b5d47SBarry Smith PetscStackCall("PCSHELL user function applytranspose()",ierr = (*shell->applytranspose)(pc,x,y);CHKERRQ(ierr)); 203e3f487b0SBarry Smith ierr = PetscObjectStateGet((PetscObject)y, &outstate);CHKERRQ(ierr); 204e3f487b0SBarry Smith if (instate == outstate) { 205e3f487b0SBarry Smith /* increase the state of the output vector since the user did not update its state themself as should have been done */ 206e3f487b0SBarry Smith ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 207e3f487b0SBarry Smith } 2084b9ad928SBarry Smith PetscFunctionReturn(0); 2094b9ad928SBarry Smith } 2104b9ad928SBarry Smith 211ace3abfcSBarry Smith static PetscErrorCode PCApplyRichardson_Shell(PC pc,Vec x,Vec y,Vec w,PetscReal rtol,PetscReal abstol, PetscReal dtol,PetscInt it,PetscBool guesszero,PetscInt *outits,PCRichardsonConvergedReason *reason) 2124b9ad928SBarry Smith { 213dfbe8321SBarry Smith PetscErrorCode ierr; 214c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 215e3f487b0SBarry Smith PetscObjectState instate,outstate; 2164b9ad928SBarry Smith 2174b9ad928SBarry Smith PetscFunctionBegin; 218ce94432eSBarry Smith if (!shell->applyrich) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No applyrichardson() routine provided to Shell PC"); 219e3f487b0SBarry Smith ierr = PetscObjectStateGet((PetscObject)y, &instate);CHKERRQ(ierr); 220eb6b5d47SBarry Smith PetscStackCall("PCSHELL user function applyrichardson()",ierr = (*shell->applyrich)(pc,x,y,w,rtol,abstol,dtol,it,guesszero,outits,reason);CHKERRQ(ierr)); 221e3f487b0SBarry Smith ierr = PetscObjectStateGet((PetscObject)y, &outstate);CHKERRQ(ierr); 222e3f487b0SBarry Smith if (instate == outstate) { 223e3f487b0SBarry Smith /* increase the state of the output vector since the user did not update its state themself as should have been done */ 224e3f487b0SBarry Smith ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 225e3f487b0SBarry Smith } 2264b9ad928SBarry Smith PetscFunctionReturn(0); 2274b9ad928SBarry Smith } 2284b9ad928SBarry Smith 2296849ba73SBarry Smith static PetscErrorCode PCDestroy_Shell(PC pc) 2304b9ad928SBarry Smith { 2314b9ad928SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 232dfbe8321SBarry Smith PetscErrorCode ierr; 2334b9ad928SBarry Smith 2344b9ad928SBarry Smith PetscFunctionBegin; 235503cfb0cSBarry Smith ierr = PetscFree(shell->name);CHKERRQ(ierr); 2362fa5cd67SKarl Rupp if (shell->destroy) PetscStackCall("PCSHELL user function destroy()",ierr = (*shell->destroy)(pc);CHKERRQ(ierr)); 237a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetDestroy_C",NULL);CHKERRQ(ierr); 238a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetSetUp_C",NULL);CHKERRQ(ierr); 239a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApply_C",NULL);CHKERRQ(ierr); 240a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplySymmetricLeft_C",NULL);CHKERRQ(ierr); 241a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplySymmetricRight_C",NULL);CHKERRQ(ierr); 242a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyBA_C",NULL);CHKERRQ(ierr); 243a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetPreSolve_C",NULL);CHKERRQ(ierr); 244a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetPostSolve_C",NULL);CHKERRQ(ierr); 245a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetView_C",NULL);CHKERRQ(ierr); 246a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyTranspose_C",NULL);CHKERRQ(ierr); 247a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetName_C",NULL);CHKERRQ(ierr); 248a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellGetName_C",NULL);CHKERRQ(ierr); 249a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyRichardson_C",NULL);CHKERRQ(ierr); 250a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCPreSolveChangeRHS_C",NULL);CHKERRQ(ierr); 251c31cb41cSBarry Smith ierr = PetscFree(pc->data);CHKERRQ(ierr); 2524b9ad928SBarry Smith PetscFunctionReturn(0); 2534b9ad928SBarry Smith } 2544b9ad928SBarry Smith 2556849ba73SBarry Smith static PetscErrorCode PCView_Shell(PC pc,PetscViewer viewer) 2564b9ad928SBarry Smith { 2574b9ad928SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 258dfbe8321SBarry Smith PetscErrorCode ierr; 259ace3abfcSBarry Smith PetscBool iascii; 2604b9ad928SBarry Smith 2614b9ad928SBarry Smith PetscFunctionBegin; 262251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 26332077d6dSBarry Smith if (iascii) { 2642fa5cd67SKarl Rupp if (shell->name) { 265efd4aadfSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %s\n",shell->name);CHKERRQ(ierr); 2662fa5cd67SKarl Rupp } else { 267efd4aadfSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," no name\n");CHKERRQ(ierr); 2682fa5cd67SKarl Rupp } 2694b9ad928SBarry Smith } 2704b9ad928SBarry Smith if (shell->view) { 2714b9ad928SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 2726891c3e4SJed Brown ierr = (*shell->view)(pc,viewer);CHKERRQ(ierr); 2734b9ad928SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2744b9ad928SBarry Smith } 2754b9ad928SBarry Smith PetscFunctionReturn(0); 2764b9ad928SBarry Smith } 2774b9ad928SBarry Smith 2784b9ad928SBarry Smith /* ------------------------------------------------------------------------------*/ 279f7a08781SBarry Smith static PetscErrorCode PCShellSetDestroy_Shell(PC pc, PetscErrorCode (*destroy)(PC)) 28018be62a5SSatish Balay { 281c5ae4b9aSBarry Smith PC_Shell *shell= (PC_Shell*)pc->data; 28218be62a5SSatish Balay 28318be62a5SSatish Balay PetscFunctionBegin; 28418be62a5SSatish Balay shell->destroy = destroy; 28518be62a5SSatish Balay PetscFunctionReturn(0); 28618be62a5SSatish Balay } 28718be62a5SSatish Balay 288f7a08781SBarry Smith static PetscErrorCode PCShellSetSetUp_Shell(PC pc, PetscErrorCode (*setup)(PC)) 2894b9ad928SBarry Smith { 290*feb237baSPierre Jolivet PC_Shell *shell = (PC_Shell*)pc->data; 2914b9ad928SBarry Smith 2924b9ad928SBarry Smith PetscFunctionBegin; 2934b9ad928SBarry Smith shell->setup = setup; 294d01c8aa3SLisandro Dalcin if (setup) pc->ops->setup = PCSetUp_Shell; 295d01c8aa3SLisandro Dalcin else pc->ops->setup = 0; 2964b9ad928SBarry Smith PetscFunctionReturn(0); 2974b9ad928SBarry Smith } 2984b9ad928SBarry Smith 299f7a08781SBarry Smith static PetscErrorCode PCShellSetApply_Shell(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 3004b9ad928SBarry Smith { 301c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3024b9ad928SBarry Smith 3034b9ad928SBarry Smith PetscFunctionBegin; 3044b9ad928SBarry Smith shell->apply = apply; 3054b9ad928SBarry Smith PetscFunctionReturn(0); 3064b9ad928SBarry Smith } 3074b9ad928SBarry Smith 3081b581b66SBarry Smith static PetscErrorCode PCShellSetApplySymmetricLeft_Shell(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 3091b581b66SBarry Smith { 3101b581b66SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3111b581b66SBarry Smith 3121b581b66SBarry Smith PetscFunctionBegin; 3131b581b66SBarry Smith shell->applysymmetricleft = apply; 3141b581b66SBarry Smith PetscFunctionReturn(0); 3151b581b66SBarry Smith } 3161b581b66SBarry Smith 3171b581b66SBarry Smith static PetscErrorCode PCShellSetApplySymmetricRight_Shell(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 3181b581b66SBarry Smith { 3191b581b66SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3201b581b66SBarry Smith 3211b581b66SBarry Smith PetscFunctionBegin; 3221b581b66SBarry Smith shell->applysymmetricright = apply; 3231b581b66SBarry Smith PetscFunctionReturn(0); 3241b581b66SBarry Smith } 3251b581b66SBarry Smith 326f7a08781SBarry Smith static PetscErrorCode PCShellSetApplyBA_Shell(PC pc,PetscErrorCode (*applyBA)(PC,PCSide,Vec,Vec,Vec)) 3272bb17772SBarry Smith { 328c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3292bb17772SBarry Smith 3302bb17772SBarry Smith PetscFunctionBegin; 331d01c8aa3SLisandro Dalcin shell->applyBA = applyBA; 332d01c8aa3SLisandro Dalcin if (applyBA) pc->ops->applyBA = PCApplyBA_Shell; 333aef0136fSBarry Smith else pc->ops->applyBA = 0; 3342bb17772SBarry Smith PetscFunctionReturn(0); 3352bb17772SBarry Smith } 3362bb17772SBarry Smith 337f7a08781SBarry Smith static PetscErrorCode PCShellSetPreSolve_Shell(PC pc,PetscErrorCode (*presolve)(PC,KSP,Vec,Vec)) 3387cdd61b2SBarry Smith { 339c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 340a06fd7f2SStefano Zampini PetscErrorCode ierr; 3417cdd61b2SBarry Smith 3427cdd61b2SBarry Smith PetscFunctionBegin; 3437cdd61b2SBarry Smith shell->presolve = presolve; 344a06fd7f2SStefano Zampini if (presolve) { 345a06fd7f2SStefano Zampini pc->ops->presolve = PCPreSolve_Shell; 346a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCPreSolveChangeRHS_C",PCPreSolveChangeRHS_Shell);CHKERRQ(ierr); 347a06fd7f2SStefano Zampini } else { 348a06fd7f2SStefano Zampini pc->ops->presolve = 0; 349a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCPreSolveChangeRHS_C",NULL);CHKERRQ(ierr); 350a06fd7f2SStefano Zampini } 3517cdd61b2SBarry Smith PetscFunctionReturn(0); 3527cdd61b2SBarry Smith } 3537cdd61b2SBarry Smith 354f7a08781SBarry Smith static PetscErrorCode PCShellSetPostSolve_Shell(PC pc,PetscErrorCode (*postsolve)(PC,KSP,Vec,Vec)) 3557cdd61b2SBarry Smith { 356c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3577cdd61b2SBarry Smith 3587cdd61b2SBarry Smith PetscFunctionBegin; 3597cdd61b2SBarry Smith shell->postsolve = postsolve; 360d01c8aa3SLisandro Dalcin if (postsolve) pc->ops->postsolve = PCPostSolve_Shell; 361d01c8aa3SLisandro Dalcin else pc->ops->postsolve = 0; 3627cdd61b2SBarry Smith PetscFunctionReturn(0); 3637cdd61b2SBarry Smith } 3647cdd61b2SBarry Smith 365f7a08781SBarry Smith static PetscErrorCode PCShellSetView_Shell(PC pc,PetscErrorCode (*view)(PC,PetscViewer)) 3664b9ad928SBarry Smith { 367c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3684b9ad928SBarry Smith 3694b9ad928SBarry Smith PetscFunctionBegin; 3704b9ad928SBarry Smith shell->view = view; 3714b9ad928SBarry Smith PetscFunctionReturn(0); 3724b9ad928SBarry Smith } 3734b9ad928SBarry Smith 374f7a08781SBarry Smith static PetscErrorCode PCShellSetApplyTranspose_Shell(PC pc,PetscErrorCode (*applytranspose)(PC,Vec,Vec)) 3754b9ad928SBarry Smith { 376c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3774b9ad928SBarry Smith 3784b9ad928SBarry Smith PetscFunctionBegin; 3794b9ad928SBarry Smith shell->applytranspose = applytranspose; 380d01c8aa3SLisandro Dalcin if (applytranspose) pc->ops->applytranspose = PCApplyTranspose_Shell; 381d01c8aa3SLisandro Dalcin else pc->ops->applytranspose = 0; 382d01c8aa3SLisandro Dalcin PetscFunctionReturn(0); 383d01c8aa3SLisandro Dalcin } 384d01c8aa3SLisandro Dalcin 385f7a08781SBarry Smith static PetscErrorCode PCShellSetApplyRichardson_Shell(PC pc,PetscErrorCode (*applyrich)(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool ,PetscInt*,PCRichardsonConvergedReason*)) 386d01c8aa3SLisandro Dalcin { 387c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 388d01c8aa3SLisandro Dalcin 389d01c8aa3SLisandro Dalcin PetscFunctionBegin; 390d01c8aa3SLisandro Dalcin shell->applyrich = applyrich; 391d01c8aa3SLisandro Dalcin if (applyrich) pc->ops->applyrichardson = PCApplyRichardson_Shell; 392d01c8aa3SLisandro Dalcin else pc->ops->applyrichardson = 0; 3934b9ad928SBarry Smith PetscFunctionReturn(0); 3944b9ad928SBarry Smith } 3954b9ad928SBarry Smith 396f7a08781SBarry Smith static PetscErrorCode PCShellSetName_Shell(PC pc,const char name[]) 3974b9ad928SBarry Smith { 398c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 399dfbe8321SBarry Smith PetscErrorCode ierr; 4004b9ad928SBarry Smith 4014b9ad928SBarry Smith PetscFunctionBegin; 402503cfb0cSBarry Smith ierr = PetscFree(shell->name);CHKERRQ(ierr); 4034b9ad928SBarry Smith ierr = PetscStrallocpy(name,&shell->name);CHKERRQ(ierr); 4044b9ad928SBarry Smith PetscFunctionReturn(0); 4054b9ad928SBarry Smith } 4064b9ad928SBarry Smith 407f7a08781SBarry Smith static PetscErrorCode PCShellGetName_Shell(PC pc,const char *name[]) 4084b9ad928SBarry Smith { 409c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 4104b9ad928SBarry Smith 4114b9ad928SBarry Smith PetscFunctionBegin; 4124b9ad928SBarry Smith *name = shell->name; 4134b9ad928SBarry Smith PetscFunctionReturn(0); 4144b9ad928SBarry Smith } 4154b9ad928SBarry Smith 4164b9ad928SBarry Smith /* -------------------------------------------------------------------------------*/ 4174b9ad928SBarry Smith 41818be62a5SSatish Balay /*@C 41918be62a5SSatish Balay PCShellSetDestroy - Sets routine to use to destroy the user-provided 42018be62a5SSatish Balay application context. 42118be62a5SSatish Balay 4223f9fe445SBarry Smith Logically Collective on PC 42318be62a5SSatish Balay 42418be62a5SSatish Balay Input Parameters: 42518be62a5SSatish Balay + pc - the preconditioner context 426a2b725a8SWilliam Gropp - destroy - the application-provided destroy routine 42718be62a5SSatish Balay 42818be62a5SSatish Balay Calling sequence of destroy: 42918be62a5SSatish Balay .vb 4306891c3e4SJed Brown PetscErrorCode destroy (PC) 43118be62a5SSatish Balay .ve 43218be62a5SSatish Balay 43318be62a5SSatish Balay . ptr - the application context 43418be62a5SSatish Balay 43595452b02SPatrick Sanan Notes: 43695452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 4374aa34b0aSBarry Smith 43818be62a5SSatish Balay Level: developer 43918be62a5SSatish Balay 44018be62a5SSatish Balay .seealso: PCShellSetApply(), PCShellSetContext() 44118be62a5SSatish Balay @*/ 4427087cfbeSBarry Smith PetscErrorCode PCShellSetDestroy(PC pc,PetscErrorCode (*destroy)(PC)) 44318be62a5SSatish Balay { 4444ac538c5SBarry Smith PetscErrorCode ierr; 44518be62a5SSatish Balay 44618be62a5SSatish Balay PetscFunctionBegin; 4470700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 4484ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetDestroy_C",(PC,PetscErrorCode (*)(PC)),(pc,destroy));CHKERRQ(ierr); 44918be62a5SSatish Balay PetscFunctionReturn(0); 45018be62a5SSatish Balay } 45118be62a5SSatish Balay 45218be62a5SSatish Balay 4534b9ad928SBarry Smith /*@C 4544b9ad928SBarry Smith PCShellSetSetUp - Sets routine to use to "setup" the preconditioner whenever the 4554b9ad928SBarry Smith matrix operator is changed. 4564b9ad928SBarry Smith 4573f9fe445SBarry Smith Logically Collective on PC 4584b9ad928SBarry Smith 4594b9ad928SBarry Smith Input Parameters: 4604b9ad928SBarry Smith + pc - the preconditioner context 461a2b725a8SWilliam Gropp - setup - the application-provided setup routine 4624b9ad928SBarry Smith 4634b9ad928SBarry Smith Calling sequence of setup: 4644b9ad928SBarry Smith .vb 4656891c3e4SJed Brown PetscErrorCode setup (PC pc) 4664b9ad928SBarry Smith .ve 4674b9ad928SBarry Smith 4686891c3e4SJed Brown . pc - the preconditioner, get the application context with PCShellGetContext() 4694b9ad928SBarry Smith 47095452b02SPatrick Sanan Notes: 47195452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 4724aa34b0aSBarry Smith 4734b9ad928SBarry Smith Level: developer 4744b9ad928SBarry Smith 475be29d3c6SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetApply(), PCShellSetContext() 4764b9ad928SBarry Smith @*/ 4777087cfbeSBarry Smith PetscErrorCode PCShellSetSetUp(PC pc,PetscErrorCode (*setup)(PC)) 4784b9ad928SBarry Smith { 4794ac538c5SBarry Smith PetscErrorCode ierr; 4804b9ad928SBarry Smith 4814b9ad928SBarry Smith PetscFunctionBegin; 4820700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 4834ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetSetUp_C",(PC,PetscErrorCode (*)(PC)),(pc,setup));CHKERRQ(ierr); 4844b9ad928SBarry Smith PetscFunctionReturn(0); 4854b9ad928SBarry Smith } 4864b9ad928SBarry Smith 4874b9ad928SBarry Smith 4884b9ad928SBarry Smith /*@C 4894b9ad928SBarry Smith PCShellSetView - Sets routine to use as viewer of shell preconditioner 4904b9ad928SBarry Smith 4913f9fe445SBarry Smith Logically Collective on PC 4924b9ad928SBarry Smith 4934b9ad928SBarry Smith Input Parameters: 4944b9ad928SBarry Smith + pc - the preconditioner context 4954b9ad928SBarry Smith - view - the application-provided view routine 4964b9ad928SBarry Smith 49753a7a830SPatrick Sanan Calling sequence of view: 4984b9ad928SBarry Smith .vb 4996891c3e4SJed Brown PetscErrorCode view(PC pc,PetscViewer v) 5004b9ad928SBarry Smith .ve 5014b9ad928SBarry Smith 5026891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 5034b9ad928SBarry Smith - v - viewer 5044b9ad928SBarry Smith 50595452b02SPatrick Sanan Notes: 50695452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 5074aa34b0aSBarry Smith 5084b9ad928SBarry Smith Level: developer 5094b9ad928SBarry Smith 5104b9ad928SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose() 5114b9ad928SBarry Smith @*/ 5127087cfbeSBarry Smith PetscErrorCode PCShellSetView(PC pc,PetscErrorCode (*view)(PC,PetscViewer)) 5134b9ad928SBarry Smith { 5144ac538c5SBarry Smith PetscErrorCode ierr; 5154b9ad928SBarry Smith 5164b9ad928SBarry Smith PetscFunctionBegin; 5170700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 5184ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetView_C",(PC,PetscErrorCode (*)(PC,PetscViewer)),(pc,view));CHKERRQ(ierr); 5194b9ad928SBarry Smith PetscFunctionReturn(0); 5204b9ad928SBarry Smith } 5214b9ad928SBarry Smith 5224b9ad928SBarry Smith /*@C 5234b9ad928SBarry Smith PCShellSetApply - Sets routine to use as preconditioner. 5244b9ad928SBarry Smith 5253f9fe445SBarry Smith Logically Collective on PC 5264b9ad928SBarry Smith 5274b9ad928SBarry Smith Input Parameters: 5284b9ad928SBarry Smith + pc - the preconditioner context 529be29d3c6SBarry Smith - apply - the application-provided preconditioning routine 5304b9ad928SBarry Smith 5314b9ad928SBarry Smith Calling sequence of apply: 5324b9ad928SBarry Smith .vb 5336891c3e4SJed Brown PetscErrorCode apply (PC pc,Vec xin,Vec xout) 5344b9ad928SBarry Smith .ve 5354b9ad928SBarry Smith 5366891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 5374b9ad928SBarry Smith . xin - input vector 5384b9ad928SBarry Smith - xout - output vector 5394b9ad928SBarry Smith 54095452b02SPatrick Sanan Notes: 54195452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 5424aa34b0aSBarry Smith 5434b9ad928SBarry Smith Level: developer 5444b9ad928SBarry Smith 545a4c07401SPatrick Sanan .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetContext(), PCShellSetApplyBA(), PCShellSetApplySymmetricRight(),PCShellSetApplySymmetricLeft() 5464b9ad928SBarry Smith @*/ 5477087cfbeSBarry Smith PetscErrorCode PCShellSetApply(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 5484b9ad928SBarry Smith { 5494ac538c5SBarry Smith PetscErrorCode ierr; 5504b9ad928SBarry Smith 5514b9ad928SBarry Smith PetscFunctionBegin; 5520700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 5534ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetApply_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,apply));CHKERRQ(ierr); 5544b9ad928SBarry Smith PetscFunctionReturn(0); 5554b9ad928SBarry Smith } 5564b9ad928SBarry Smith 5571b581b66SBarry Smith /*@C 5581b581b66SBarry Smith PCShellSetApplySymmetricLeft - Sets routine to use as left preconditioner (when the PC_SYMMETRIC is used). 5591b581b66SBarry Smith 5601b581b66SBarry Smith Logically Collective on PC 5611b581b66SBarry Smith 5621b581b66SBarry Smith Input Parameters: 5631b581b66SBarry Smith + pc - the preconditioner context 5641b581b66SBarry Smith - apply - the application-provided left preconditioning routine 5651b581b66SBarry Smith 5661b581b66SBarry Smith Calling sequence of apply: 5671b581b66SBarry Smith .vb 5681b581b66SBarry Smith PetscErrorCode apply (PC pc,Vec xin,Vec xout) 5691b581b66SBarry Smith .ve 5701b581b66SBarry Smith 5711b581b66SBarry Smith + pc - the preconditioner, get the application context with PCShellGetContext() 5721b581b66SBarry Smith . xin - input vector 5731b581b66SBarry Smith - xout - output vector 5741b581b66SBarry Smith 57595452b02SPatrick Sanan Notes: 57695452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 5771b581b66SBarry Smith 5781b581b66SBarry Smith Level: developer 5791b581b66SBarry Smith 5801b581b66SBarry Smith .seealso: PCShellSetApply(), PCShellSetApplySymmetricLeft(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetContext() 5811b581b66SBarry Smith @*/ 5821b581b66SBarry Smith PetscErrorCode PCShellSetApplySymmetricLeft(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 5831b581b66SBarry Smith { 5841b581b66SBarry Smith PetscErrorCode ierr; 5851b581b66SBarry Smith 5861b581b66SBarry Smith PetscFunctionBegin; 5871b581b66SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 5881b581b66SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetApplySymmetricLeft_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,apply));CHKERRQ(ierr); 5891b581b66SBarry Smith PetscFunctionReturn(0); 5901b581b66SBarry Smith } 5911b581b66SBarry Smith 5921b581b66SBarry Smith /*@C 593a4c07401SPatrick Sanan PCShellSetApplySymmetricRight - Sets routine to use as right preconditioner (when the PC_SYMMETRIC is used). 5941b581b66SBarry Smith 5951b581b66SBarry Smith Logically Collective on PC 5961b581b66SBarry Smith 5971b581b66SBarry Smith Input Parameters: 5981b581b66SBarry Smith + pc - the preconditioner context 5991b581b66SBarry Smith - apply - the application-provided right preconditioning routine 6001b581b66SBarry Smith 6011b581b66SBarry Smith Calling sequence of apply: 6021b581b66SBarry Smith .vb 6031b581b66SBarry Smith PetscErrorCode apply (PC pc,Vec xin,Vec xout) 6041b581b66SBarry Smith .ve 6051b581b66SBarry Smith 6061b581b66SBarry Smith + pc - the preconditioner, get the application context with PCShellGetContext() 6071b581b66SBarry Smith . xin - input vector 6081b581b66SBarry Smith - xout - output vector 6091b581b66SBarry Smith 61095452b02SPatrick Sanan Notes: 61195452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 6121b581b66SBarry Smith 6131b581b66SBarry Smith Level: developer 6141b581b66SBarry Smith 6151b581b66SBarry Smith .seealso: PCShellSetApply(), PCShellSetApplySymmetricLeft(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetContext() 6161b581b66SBarry Smith @*/ 6171b581b66SBarry Smith PetscErrorCode PCShellSetApplySymmetricRight(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 6181b581b66SBarry Smith { 6191b581b66SBarry Smith PetscErrorCode ierr; 6201b581b66SBarry Smith 6211b581b66SBarry Smith PetscFunctionBegin; 6221b581b66SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 6231b581b66SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetApplySymmetricRight_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,apply));CHKERRQ(ierr); 6241b581b66SBarry Smith PetscFunctionReturn(0); 6251b581b66SBarry Smith } 6261b581b66SBarry Smith 6272bb17772SBarry Smith /*@C 6282bb17772SBarry Smith PCShellSetApplyBA - Sets routine to use as preconditioner times operator. 6292bb17772SBarry Smith 6303f9fe445SBarry Smith Logically Collective on PC 6312bb17772SBarry Smith 6322bb17772SBarry Smith Input Parameters: 6332bb17772SBarry Smith + pc - the preconditioner context 6342bb17772SBarry Smith - applyBA - the application-provided BA routine 6352bb17772SBarry Smith 63653a7a830SPatrick Sanan Calling sequence of applyBA: 6372bb17772SBarry Smith .vb 6386891c3e4SJed Brown PetscErrorCode applyBA (PC pc,Vec xin,Vec xout) 6392bb17772SBarry Smith .ve 6402bb17772SBarry Smith 6416891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 6422bb17772SBarry Smith . xin - input vector 6432bb17772SBarry Smith - xout - output vector 6442bb17772SBarry Smith 64595452b02SPatrick Sanan Notes: 64695452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 6474aa34b0aSBarry Smith 6482bb17772SBarry Smith Level: developer 6492bb17772SBarry Smith 6502bb17772SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetContext(), PCShellSetApply() 6512bb17772SBarry Smith @*/ 6527087cfbeSBarry Smith PetscErrorCode PCShellSetApplyBA(PC pc,PetscErrorCode (*applyBA)(PC,PCSide,Vec,Vec,Vec)) 6532bb17772SBarry Smith { 6544ac538c5SBarry Smith PetscErrorCode ierr; 6552bb17772SBarry Smith 6562bb17772SBarry Smith PetscFunctionBegin; 6570700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 6584ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetApplyBA_C",(PC,PetscErrorCode (*)(PC,PCSide,Vec,Vec,Vec)),(pc,applyBA));CHKERRQ(ierr); 6592bb17772SBarry Smith PetscFunctionReturn(0); 6602bb17772SBarry Smith } 6612bb17772SBarry Smith 6624b9ad928SBarry Smith /*@C 6634b9ad928SBarry Smith PCShellSetApplyTranspose - Sets routine to use as preconditioner transpose. 6644b9ad928SBarry Smith 6653f9fe445SBarry Smith Logically Collective on PC 6664b9ad928SBarry Smith 6674b9ad928SBarry Smith Input Parameters: 6684b9ad928SBarry Smith + pc - the preconditioner context 6694b9ad928SBarry Smith - apply - the application-provided preconditioning transpose routine 6704b9ad928SBarry Smith 6714b9ad928SBarry Smith Calling sequence of apply: 6724b9ad928SBarry Smith .vb 6736891c3e4SJed Brown PetscErrorCode applytranspose (PC pc,Vec xin,Vec xout) 6744b9ad928SBarry Smith .ve 6754b9ad928SBarry Smith 6766891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 6774b9ad928SBarry Smith . xin - input vector 6784b9ad928SBarry Smith - xout - output vector 6794b9ad928SBarry Smith 68095452b02SPatrick Sanan Notes: 68195452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 6824aa34b0aSBarry Smith 6834b9ad928SBarry Smith Level: developer 6844b9ad928SBarry Smith 6854b9ad928SBarry Smith Notes: 6864b9ad928SBarry Smith Uses the same context variable as PCShellSetApply(). 6874b9ad928SBarry Smith 6882bb17772SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApply(), PCSetContext(), PCShellSetApplyBA() 6894b9ad928SBarry Smith @*/ 6907087cfbeSBarry Smith PetscErrorCode PCShellSetApplyTranspose(PC pc,PetscErrorCode (*applytranspose)(PC,Vec,Vec)) 6914b9ad928SBarry Smith { 6924ac538c5SBarry Smith PetscErrorCode ierr; 6934b9ad928SBarry Smith 6944b9ad928SBarry Smith PetscFunctionBegin; 6950700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 6964ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetApplyTranspose_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,applytranspose));CHKERRQ(ierr); 6974b9ad928SBarry Smith PetscFunctionReturn(0); 6984b9ad928SBarry Smith } 6994b9ad928SBarry Smith 7007cdd61b2SBarry Smith /*@C 7017cdd61b2SBarry Smith PCShellSetPreSolve - Sets routine to apply to the operators/vectors before a KSPSolve() is 7027cdd61b2SBarry Smith applied. This usually does something like scale the linear system in some application 7037cdd61b2SBarry Smith specific way. 7047cdd61b2SBarry Smith 7053f9fe445SBarry Smith Logically Collective on PC 7067cdd61b2SBarry Smith 7077cdd61b2SBarry Smith Input Parameters: 7087cdd61b2SBarry Smith + pc - the preconditioner context 7097cdd61b2SBarry Smith - presolve - the application-provided presolve routine 7107cdd61b2SBarry Smith 7117cdd61b2SBarry Smith Calling sequence of presolve: 7127cdd61b2SBarry Smith .vb 7136891c3e4SJed Brown PetscErrorCode presolve (PC,KSP ksp,Vec b,Vec x) 7147cdd61b2SBarry Smith .ve 7157cdd61b2SBarry Smith 7166891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 7177cdd61b2SBarry Smith . xin - input vector 7187cdd61b2SBarry Smith - xout - output vector 7197cdd61b2SBarry Smith 72095452b02SPatrick Sanan Notes: 72195452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 7224aa34b0aSBarry Smith 7237cdd61b2SBarry Smith Level: developer 7247cdd61b2SBarry Smith 725be29d3c6SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetPostSolve(), PCShellSetContext() 7267cdd61b2SBarry Smith @*/ 7277087cfbeSBarry Smith PetscErrorCode PCShellSetPreSolve(PC pc,PetscErrorCode (*presolve)(PC,KSP,Vec,Vec)) 7287cdd61b2SBarry Smith { 7294ac538c5SBarry Smith PetscErrorCode ierr; 7307cdd61b2SBarry Smith 7317cdd61b2SBarry Smith PetscFunctionBegin; 7320700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 7334ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetPreSolve_C",(PC,PetscErrorCode (*)(PC,KSP,Vec,Vec)),(pc,presolve));CHKERRQ(ierr); 7347cdd61b2SBarry Smith PetscFunctionReturn(0); 7357cdd61b2SBarry Smith } 7367cdd61b2SBarry Smith 7377cdd61b2SBarry Smith /*@C 7387cdd61b2SBarry Smith PCShellSetPostSolve - Sets routine to apply to the operators/vectors before a KSPSolve() is 7397cdd61b2SBarry Smith applied. This usually does something like scale the linear system in some application 7407cdd61b2SBarry Smith specific way. 7417cdd61b2SBarry Smith 7423f9fe445SBarry Smith Logically Collective on PC 7437cdd61b2SBarry Smith 7447cdd61b2SBarry Smith Input Parameters: 7457cdd61b2SBarry Smith + pc - the preconditioner context 7467cdd61b2SBarry Smith - postsolve - the application-provided presolve routine 7477cdd61b2SBarry Smith 7487cdd61b2SBarry Smith Calling sequence of postsolve: 7497cdd61b2SBarry Smith .vb 7506891c3e4SJed Brown PetscErrorCode postsolve(PC,KSP ksp,Vec b,Vec x) 7517cdd61b2SBarry Smith .ve 7527cdd61b2SBarry Smith 7536891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 7547cdd61b2SBarry Smith . xin - input vector 7557cdd61b2SBarry Smith - xout - output vector 7567cdd61b2SBarry Smith 75795452b02SPatrick Sanan Notes: 75895452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 7594aa34b0aSBarry Smith 7607cdd61b2SBarry Smith Level: developer 7617cdd61b2SBarry Smith 762be29d3c6SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetPreSolve(), PCShellSetContext() 7637cdd61b2SBarry Smith @*/ 7647087cfbeSBarry Smith PetscErrorCode PCShellSetPostSolve(PC pc,PetscErrorCode (*postsolve)(PC,KSP,Vec,Vec)) 7657cdd61b2SBarry Smith { 7664ac538c5SBarry Smith PetscErrorCode ierr; 7677cdd61b2SBarry Smith 7687cdd61b2SBarry Smith PetscFunctionBegin; 7690700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 7704ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetPostSolve_C",(PC,PetscErrorCode (*)(PC,KSP,Vec,Vec)),(pc,postsolve));CHKERRQ(ierr); 7717cdd61b2SBarry Smith PetscFunctionReturn(0); 7727cdd61b2SBarry Smith } 7737cdd61b2SBarry Smith 7744b9ad928SBarry Smith /*@C 7754b9ad928SBarry Smith PCShellSetName - Sets an optional name to associate with a shell 7764b9ad928SBarry Smith preconditioner. 7774b9ad928SBarry Smith 7784b9ad928SBarry Smith Not Collective 7794b9ad928SBarry Smith 7804b9ad928SBarry Smith Input Parameters: 7814b9ad928SBarry Smith + pc - the preconditioner context 7824b9ad928SBarry Smith - name - character string describing shell preconditioner 7834b9ad928SBarry Smith 7844b9ad928SBarry Smith Level: developer 7854b9ad928SBarry Smith 7864b9ad928SBarry Smith .seealso: PCShellGetName() 7874b9ad928SBarry Smith @*/ 7887087cfbeSBarry Smith PetscErrorCode PCShellSetName(PC pc,const char name[]) 7894b9ad928SBarry Smith { 7904ac538c5SBarry Smith PetscErrorCode ierr; 7914b9ad928SBarry Smith 7924b9ad928SBarry Smith PetscFunctionBegin; 7930700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 7944ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetName_C",(PC,const char []),(pc,name));CHKERRQ(ierr); 7954b9ad928SBarry Smith PetscFunctionReturn(0); 7964b9ad928SBarry Smith } 7974b9ad928SBarry Smith 7984b9ad928SBarry Smith /*@C 7994b9ad928SBarry Smith PCShellGetName - Gets an optional name that the user has set for a shell 8004b9ad928SBarry Smith preconditioner. 8014b9ad928SBarry Smith 8024b9ad928SBarry Smith Not Collective 8034b9ad928SBarry Smith 8044b9ad928SBarry Smith Input Parameter: 8054b9ad928SBarry Smith . pc - the preconditioner context 8064b9ad928SBarry Smith 8074b9ad928SBarry Smith Output Parameter: 8084b9ad928SBarry Smith . name - character string describing shell preconditioner (you should not free this) 8094b9ad928SBarry Smith 8104b9ad928SBarry Smith Level: developer 8114b9ad928SBarry Smith 8124b9ad928SBarry Smith .seealso: PCShellSetName() 8134b9ad928SBarry Smith @*/ 814ccaf0856SBarry Smith PetscErrorCode PCShellGetName(PC pc,const char *name[]) 8154b9ad928SBarry Smith { 8164ac538c5SBarry Smith PetscErrorCode ierr; 8174b9ad928SBarry Smith 8184b9ad928SBarry Smith PetscFunctionBegin; 8190700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 8204482741eSBarry Smith PetscValidPointer(name,2); 821ccaf0856SBarry Smith ierr = PetscUseMethod(pc,"PCShellGetName_C",(PC,const char*[]),(pc,name));CHKERRQ(ierr); 8224b9ad928SBarry Smith PetscFunctionReturn(0); 8234b9ad928SBarry Smith } 8244b9ad928SBarry Smith 8254b9ad928SBarry Smith /*@C 8264b9ad928SBarry Smith PCShellSetApplyRichardson - Sets routine to use as preconditioner 8274b9ad928SBarry Smith in Richardson iteration. 8284b9ad928SBarry Smith 8293f9fe445SBarry Smith Logically Collective on PC 8304b9ad928SBarry Smith 8314b9ad928SBarry Smith Input Parameters: 8324b9ad928SBarry Smith + pc - the preconditioner context 833be29d3c6SBarry Smith - apply - the application-provided preconditioning routine 8344b9ad928SBarry Smith 8354b9ad928SBarry Smith Calling sequence of apply: 8364b9ad928SBarry Smith .vb 8376891c3e4SJed Brown PetscErrorCode apply (PC pc,Vec b,Vec x,Vec r,PetscReal rtol,PetscReal abstol,PetscReal dtol,PetscInt maxits) 8384b9ad928SBarry Smith .ve 8394b9ad928SBarry Smith 8406891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 8414b9ad928SBarry Smith . b - right-hand-side 8424b9ad928SBarry Smith . x - current iterate 8434b9ad928SBarry Smith . r - work space 8444b9ad928SBarry Smith . rtol - relative tolerance of residual norm to stop at 84570441072SBarry Smith . abstol - absolute tolerance of residual norm to stop at 8464b9ad928SBarry Smith . dtol - if residual norm increases by this factor than return 8474b9ad928SBarry Smith - maxits - number of iterations to run 8484b9ad928SBarry Smith 84995452b02SPatrick Sanan Notes: 85095452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 8514aa34b0aSBarry Smith 8524b9ad928SBarry Smith Level: developer 8534b9ad928SBarry Smith 854be29d3c6SBarry Smith .seealso: PCShellSetApply(), PCShellSetContext() 8554b9ad928SBarry Smith @*/ 8567087cfbeSBarry Smith PetscErrorCode PCShellSetApplyRichardson(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool,PetscInt*,PCRichardsonConvergedReason*)) 8574b9ad928SBarry Smith { 8584ac538c5SBarry Smith PetscErrorCode ierr; 8594b9ad928SBarry Smith 8604b9ad928SBarry Smith PetscFunctionBegin; 8610700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 8624ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetApplyRichardson_C",(PC,PetscErrorCode (*)(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool,PetscInt*,PCRichardsonConvergedReason*)),(pc,apply));CHKERRQ(ierr); 8634b9ad928SBarry Smith PetscFunctionReturn(0); 8644b9ad928SBarry Smith } 8654b9ad928SBarry Smith 8664b9ad928SBarry Smith /*MC 8674b9ad928SBarry Smith PCSHELL - Creates a new preconditioner class for use with your 8684b9ad928SBarry Smith own private data storage format. 8694b9ad928SBarry Smith 8704b9ad928SBarry Smith Level: advanced 87190198e61SBarry Smith > 8724b9ad928SBarry Smith Usage: 8736891c3e4SJed Brown $ extern PetscErrorCode apply(PC,Vec,Vec); 8746891c3e4SJed Brown $ extern PetscErrorCode applyba(PC,PCSide,Vec,Vec,Vec); 8756891c3e4SJed Brown $ extern PetscErrorCode applytranspose(PC,Vec,Vec); 8766891c3e4SJed Brown $ extern PetscErrorCode setup(PC); 8776891c3e4SJed Brown $ extern PetscErrorCode destroy(PC); 8786891c3e4SJed Brown $ 8794b9ad928SBarry Smith $ PCCreate(comm,&pc); 8804b9ad928SBarry Smith $ PCSetType(pc,PCSHELL); 881be29d3c6SBarry Smith $ PCShellSetContext(pc,ctx) 8826891c3e4SJed Brown $ PCShellSetApply(pc,apply); 8836891c3e4SJed Brown $ PCShellSetApplyBA(pc,applyba); (optional) 8846891c3e4SJed Brown $ PCShellSetApplyTranspose(pc,applytranspose); (optional) 8854b9ad928SBarry Smith $ PCShellSetSetUp(pc,setup); (optional) 886d01c8aa3SLisandro Dalcin $ PCShellSetDestroy(pc,destroy); (optional) 8874b9ad928SBarry Smith 8884b9ad928SBarry Smith .seealso: PCCreate(), PCSetType(), PCType (for list of available types), PC, 889fd2d0fe1Svictor MATSHELL, PCShellSetSetUp(), PCShellSetApply(), PCShellSetView(), 890fd2d0fe1Svictor PCShellSetApplyTranspose(), PCShellSetName(), PCShellSetApplyRichardson(), 8912bb17772SBarry Smith PCShellGetName(), PCShellSetContext(), PCShellGetContext(), PCShellSetApplyBA() 8924b9ad928SBarry Smith M*/ 8934b9ad928SBarry Smith 8948cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PCCreate_Shell(PC pc) 8954b9ad928SBarry Smith { 896dfbe8321SBarry Smith PetscErrorCode ierr; 8974b9ad928SBarry Smith PC_Shell *shell; 8984b9ad928SBarry Smith 8994b9ad928SBarry Smith PetscFunctionBegin; 900b00a9115SJed Brown ierr = PetscNewLog(pc,&shell);CHKERRQ(ierr); 9014b9ad928SBarry Smith pc->data = (void*)shell; 9024b9ad928SBarry Smith 903d01c8aa3SLisandro Dalcin pc->ops->destroy = PCDestroy_Shell; 9044b9ad928SBarry Smith pc->ops->view = PCView_Shell; 905d01c8aa3SLisandro Dalcin pc->ops->apply = PCApply_Shell; 9061b581b66SBarry Smith pc->ops->applysymmetricleft = PCApplySymmetricLeft_Shell; 9071b581b66SBarry Smith pc->ops->applysymmetricright = PCApplySymmetricRight_Shell; 908d01c8aa3SLisandro Dalcin pc->ops->applytranspose = 0; 9094b9ad928SBarry Smith pc->ops->applyrichardson = 0; 910d01c8aa3SLisandro Dalcin pc->ops->setup = 0; 9119bbb2c88SBarry Smith pc->ops->presolve = 0; 9129bbb2c88SBarry Smith pc->ops->postsolve = 0; 9134b9ad928SBarry Smith 9144b9ad928SBarry Smith shell->apply = 0; 9154b9ad928SBarry Smith shell->applytranspose = 0; 9164b9ad928SBarry Smith shell->name = 0; 9174b9ad928SBarry Smith shell->applyrich = 0; 9187cdd61b2SBarry Smith shell->presolve = 0; 9197cdd61b2SBarry Smith shell->postsolve = 0; 9204b9ad928SBarry Smith shell->ctx = 0; 9214b9ad928SBarry Smith shell->setup = 0; 9224b9ad928SBarry Smith shell->view = 0; 92318be62a5SSatish Balay shell->destroy = 0; 9241b581b66SBarry Smith shell->applysymmetricleft = 0; 9251b581b66SBarry Smith shell->applysymmetricright = 0; 9264b9ad928SBarry Smith 927bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetDestroy_C",PCShellSetDestroy_Shell);CHKERRQ(ierr); 928bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetSetUp_C",PCShellSetSetUp_Shell);CHKERRQ(ierr); 929bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApply_C",PCShellSetApply_Shell);CHKERRQ(ierr); 9301b581b66SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplySymmetricLeft_C",PCShellSetApplySymmetricLeft_Shell);CHKERRQ(ierr); 9311b581b66SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplySymmetricRight_C",PCShellSetApplySymmetricRight_Shell);CHKERRQ(ierr); 932bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyBA_C",PCShellSetApplyBA_Shell);CHKERRQ(ierr); 933bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetPreSolve_C",PCShellSetPreSolve_Shell);CHKERRQ(ierr); 934bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetPostSolve_C",PCShellSetPostSolve_Shell);CHKERRQ(ierr); 935bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetView_C",PCShellSetView_Shell);CHKERRQ(ierr); 936bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyTranspose_C",PCShellSetApplyTranspose_Shell);CHKERRQ(ierr); 937bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetName_C",PCShellSetName_Shell);CHKERRQ(ierr); 938bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellGetName_C",PCShellGetName_Shell);CHKERRQ(ierr); 939bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyRichardson_C",PCShellSetApplyRichardson_Shell);CHKERRQ(ierr); 9404b9ad928SBarry Smith PetscFunctionReturn(0); 9414b9ad928SBarry Smith } 9424b9ad928SBarry Smith 9434b9ad928SBarry Smith 9444b9ad928SBarry Smith 9454b9ad928SBarry Smith 9464b9ad928SBarry Smith 9474b9ad928SBarry Smith 948