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 43daf670e6SBarry Smith Fortran Notes: To use this from Fortran you must write a Fortran interface definition for this 44daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 45daf670e6SBarry Smith 46be29d3c6SBarry Smith .keywords: PC, shell, get, context 47be29d3c6SBarry Smith 48c5ae4b9aSBarry Smith .seealso: PCShellSetContext() 49be29d3c6SBarry Smith @*/ 507087cfbeSBarry Smith PetscErrorCode PCShellGetContext(PC pc,void **ctx) 51be29d3c6SBarry Smith { 52be29d3c6SBarry Smith PetscErrorCode ierr; 53ace3abfcSBarry Smith PetscBool flg; 54be29d3c6SBarry Smith 55be29d3c6SBarry Smith PetscFunctionBegin; 560700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 57be29d3c6SBarry Smith PetscValidPointer(ctx,2); 58251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr); 59be29d3c6SBarry Smith if (!flg) *ctx = 0; 60be29d3c6SBarry Smith else *ctx = ((PC_Shell*)(pc->data))->ctx; 61be29d3c6SBarry Smith PetscFunctionReturn(0); 62be29d3c6SBarry Smith } 63be29d3c6SBarry Smith 649dd1005fSJed Brown /*@ 65be29d3c6SBarry Smith PCShellSetContext - sets the context for a shell PC 66be29d3c6SBarry Smith 673f9fe445SBarry Smith Logically Collective on PC 68be29d3c6SBarry Smith 69be29d3c6SBarry Smith Input Parameters: 70be29d3c6SBarry Smith + pc - the shell PC 71be29d3c6SBarry Smith - ctx - the context 72be29d3c6SBarry Smith 73be29d3c6SBarry Smith Level: advanced 74be29d3c6SBarry Smith 75daf670e6SBarry Smith Fortran Notes: 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) { 265*efd4aadfSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %s\n",shell->name);CHKERRQ(ierr); 2662fa5cd67SKarl Rupp } else { 267*efd4aadfSBarry 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 { 290c5ae4b9aSBarry Smith 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 42618be62a5SSatish Balay . 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 4354aa34b0aSBarry Smith Notes: the function MUST return an error code of 0 on success and nonzero on failure. 4364aa34b0aSBarry Smith 43718be62a5SSatish Balay Level: developer 43818be62a5SSatish Balay 43918be62a5SSatish Balay .keywords: PC, shell, set, destroy, user-provided 44018be62a5SSatish Balay 44118be62a5SSatish Balay .seealso: PCShellSetApply(), PCShellSetContext() 44218be62a5SSatish Balay @*/ 4437087cfbeSBarry Smith PetscErrorCode PCShellSetDestroy(PC pc,PetscErrorCode (*destroy)(PC)) 44418be62a5SSatish Balay { 4454ac538c5SBarry Smith PetscErrorCode ierr; 44618be62a5SSatish Balay 44718be62a5SSatish Balay PetscFunctionBegin; 4480700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 4494ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetDestroy_C",(PC,PetscErrorCode (*)(PC)),(pc,destroy));CHKERRQ(ierr); 45018be62a5SSatish Balay PetscFunctionReturn(0); 45118be62a5SSatish Balay } 45218be62a5SSatish Balay 45318be62a5SSatish Balay 4544b9ad928SBarry Smith /*@C 4554b9ad928SBarry Smith PCShellSetSetUp - Sets routine to use to "setup" the preconditioner whenever the 4564b9ad928SBarry Smith matrix operator is changed. 4574b9ad928SBarry Smith 4583f9fe445SBarry Smith Logically Collective on PC 4594b9ad928SBarry Smith 4604b9ad928SBarry Smith Input Parameters: 4614b9ad928SBarry Smith + pc - the preconditioner context 4624b9ad928SBarry Smith . setup - the application-provided setup routine 4634b9ad928SBarry Smith 4644b9ad928SBarry Smith Calling sequence of setup: 4654b9ad928SBarry Smith .vb 4666891c3e4SJed Brown PetscErrorCode setup (PC pc) 4674b9ad928SBarry Smith .ve 4684b9ad928SBarry Smith 4696891c3e4SJed Brown . pc - the preconditioner, get the application context with PCShellGetContext() 4704b9ad928SBarry Smith 4714aa34b0aSBarry Smith Notes: the function MUST return an error code of 0 on success and nonzero on failure. 4724aa34b0aSBarry Smith 4734b9ad928SBarry Smith Level: developer 4744b9ad928SBarry Smith 4754b9ad928SBarry Smith .keywords: PC, shell, set, setup, user-provided 4764b9ad928SBarry Smith 477be29d3c6SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetApply(), PCShellSetContext() 4784b9ad928SBarry Smith @*/ 4797087cfbeSBarry Smith PetscErrorCode PCShellSetSetUp(PC pc,PetscErrorCode (*setup)(PC)) 4804b9ad928SBarry Smith { 4814ac538c5SBarry Smith PetscErrorCode ierr; 4824b9ad928SBarry Smith 4834b9ad928SBarry Smith PetscFunctionBegin; 4840700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 4854ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetSetUp_C",(PC,PetscErrorCode (*)(PC)),(pc,setup));CHKERRQ(ierr); 4864b9ad928SBarry Smith PetscFunctionReturn(0); 4874b9ad928SBarry Smith } 4884b9ad928SBarry Smith 4894b9ad928SBarry Smith 4904b9ad928SBarry Smith /*@C 4914b9ad928SBarry Smith PCShellSetView - Sets routine to use as viewer of shell preconditioner 4924b9ad928SBarry Smith 4933f9fe445SBarry Smith Logically Collective on PC 4944b9ad928SBarry Smith 4954b9ad928SBarry Smith Input Parameters: 4964b9ad928SBarry Smith + pc - the preconditioner context 4974b9ad928SBarry Smith - view - the application-provided view routine 4984b9ad928SBarry Smith 4994b9ad928SBarry Smith Calling sequence of apply: 5004b9ad928SBarry Smith .vb 5016891c3e4SJed Brown PetscErrorCode view(PC pc,PetscViewer v) 5024b9ad928SBarry Smith .ve 5034b9ad928SBarry Smith 5046891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 5054b9ad928SBarry Smith - v - viewer 5064b9ad928SBarry Smith 5074aa34b0aSBarry Smith Notes: the function MUST return an error code of 0 on success and nonzero on failure. 5084aa34b0aSBarry Smith 5094b9ad928SBarry Smith Level: developer 5104b9ad928SBarry Smith 5114b9ad928SBarry Smith .keywords: PC, shell, set, apply, user-provided 5124b9ad928SBarry Smith 5134b9ad928SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose() 5144b9ad928SBarry Smith @*/ 5157087cfbeSBarry Smith PetscErrorCode PCShellSetView(PC pc,PetscErrorCode (*view)(PC,PetscViewer)) 5164b9ad928SBarry Smith { 5174ac538c5SBarry Smith PetscErrorCode ierr; 5184b9ad928SBarry Smith 5194b9ad928SBarry Smith PetscFunctionBegin; 5200700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 5214ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetView_C",(PC,PetscErrorCode (*)(PC,PetscViewer)),(pc,view));CHKERRQ(ierr); 5224b9ad928SBarry Smith PetscFunctionReturn(0); 5234b9ad928SBarry Smith } 5244b9ad928SBarry Smith 5254b9ad928SBarry Smith /*@C 5264b9ad928SBarry Smith PCShellSetApply - Sets routine to use as preconditioner. 5274b9ad928SBarry Smith 5283f9fe445SBarry Smith Logically Collective on PC 5294b9ad928SBarry Smith 5304b9ad928SBarry Smith Input Parameters: 5314b9ad928SBarry Smith + pc - the preconditioner context 532be29d3c6SBarry Smith - apply - the application-provided preconditioning routine 5334b9ad928SBarry Smith 5344b9ad928SBarry Smith Calling sequence of apply: 5354b9ad928SBarry Smith .vb 5366891c3e4SJed Brown PetscErrorCode apply (PC pc,Vec xin,Vec xout) 5374b9ad928SBarry Smith .ve 5384b9ad928SBarry Smith 5396891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 5404b9ad928SBarry Smith . xin - input vector 5414b9ad928SBarry Smith - xout - output vector 5424b9ad928SBarry Smith 5434aa34b0aSBarry Smith Notes: the function MUST return an error code of 0 on success and nonzero on failure. 5444aa34b0aSBarry Smith 5454b9ad928SBarry Smith Level: developer 5464b9ad928SBarry Smith 5474b9ad928SBarry Smith .keywords: PC, shell, set, apply, user-provided 5484b9ad928SBarry Smith 549a4c07401SPatrick Sanan .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetContext(), PCShellSetApplyBA(), PCShellSetApplySymmetricRight(),PCShellSetApplySymmetricLeft() 5504b9ad928SBarry Smith @*/ 5517087cfbeSBarry Smith PetscErrorCode PCShellSetApply(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 5524b9ad928SBarry Smith { 5534ac538c5SBarry Smith PetscErrorCode ierr; 5544b9ad928SBarry Smith 5554b9ad928SBarry Smith PetscFunctionBegin; 5560700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 5574ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetApply_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,apply));CHKERRQ(ierr); 5584b9ad928SBarry Smith PetscFunctionReturn(0); 5594b9ad928SBarry Smith } 5604b9ad928SBarry Smith 5611b581b66SBarry Smith /*@C 5621b581b66SBarry Smith PCShellSetApplySymmetricLeft - Sets routine to use as left preconditioner (when the PC_SYMMETRIC is used). 5631b581b66SBarry Smith 5641b581b66SBarry Smith Logically Collective on PC 5651b581b66SBarry Smith 5661b581b66SBarry Smith Input Parameters: 5671b581b66SBarry Smith + pc - the preconditioner context 5681b581b66SBarry Smith - apply - the application-provided left preconditioning routine 5691b581b66SBarry Smith 5701b581b66SBarry Smith Calling sequence of apply: 5711b581b66SBarry Smith .vb 5721b581b66SBarry Smith PetscErrorCode apply (PC pc,Vec xin,Vec xout) 5731b581b66SBarry Smith .ve 5741b581b66SBarry Smith 5751b581b66SBarry Smith + pc - the preconditioner, get the application context with PCShellGetContext() 5761b581b66SBarry Smith . xin - input vector 5771b581b66SBarry Smith - xout - output vector 5781b581b66SBarry Smith 5791b581b66SBarry Smith Notes: the function MUST return an error code of 0 on success and nonzero on failure. 5801b581b66SBarry Smith 5811b581b66SBarry Smith Level: developer 5821b581b66SBarry Smith 5831b581b66SBarry Smith .keywords: PC, shell, set, apply, user-provided 5841b581b66SBarry Smith 5851b581b66SBarry Smith .seealso: PCShellSetApply(), PCShellSetApplySymmetricLeft(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetContext() 5861b581b66SBarry Smith @*/ 5871b581b66SBarry Smith PetscErrorCode PCShellSetApplySymmetricLeft(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 5881b581b66SBarry Smith { 5891b581b66SBarry Smith PetscErrorCode ierr; 5901b581b66SBarry Smith 5911b581b66SBarry Smith PetscFunctionBegin; 5921b581b66SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 5931b581b66SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetApplySymmetricLeft_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,apply));CHKERRQ(ierr); 5941b581b66SBarry Smith PetscFunctionReturn(0); 5951b581b66SBarry Smith } 5961b581b66SBarry Smith 5971b581b66SBarry Smith /*@C 598a4c07401SPatrick Sanan PCShellSetApplySymmetricRight - Sets routine to use as right preconditioner (when the PC_SYMMETRIC is used). 5991b581b66SBarry Smith 6001b581b66SBarry Smith Logically Collective on PC 6011b581b66SBarry Smith 6021b581b66SBarry Smith Input Parameters: 6031b581b66SBarry Smith + pc - the preconditioner context 6041b581b66SBarry Smith - apply - the application-provided right preconditioning routine 6051b581b66SBarry Smith 6061b581b66SBarry Smith Calling sequence of apply: 6071b581b66SBarry Smith .vb 6081b581b66SBarry Smith PetscErrorCode apply (PC pc,Vec xin,Vec xout) 6091b581b66SBarry Smith .ve 6101b581b66SBarry Smith 6111b581b66SBarry Smith + pc - the preconditioner, get the application context with PCShellGetContext() 6121b581b66SBarry Smith . xin - input vector 6131b581b66SBarry Smith - xout - output vector 6141b581b66SBarry Smith 6151b581b66SBarry Smith Notes: the function MUST return an error code of 0 on success and nonzero on failure. 6161b581b66SBarry Smith 6171b581b66SBarry Smith Level: developer 6181b581b66SBarry Smith 6191b581b66SBarry Smith .keywords: PC, shell, set, apply, user-provided 6201b581b66SBarry Smith 6211b581b66SBarry Smith .seealso: PCShellSetApply(), PCShellSetApplySymmetricLeft(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetContext() 6221b581b66SBarry Smith @*/ 6231b581b66SBarry Smith PetscErrorCode PCShellSetApplySymmetricRight(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 6241b581b66SBarry Smith { 6251b581b66SBarry Smith PetscErrorCode ierr; 6261b581b66SBarry Smith 6271b581b66SBarry Smith PetscFunctionBegin; 6281b581b66SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 6291b581b66SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetApplySymmetricRight_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,apply));CHKERRQ(ierr); 6301b581b66SBarry Smith PetscFunctionReturn(0); 6311b581b66SBarry Smith } 6321b581b66SBarry Smith 6332bb17772SBarry Smith /*@C 6342bb17772SBarry Smith PCShellSetApplyBA - Sets routine to use as preconditioner times operator. 6352bb17772SBarry Smith 6363f9fe445SBarry Smith Logically Collective on PC 6372bb17772SBarry Smith 6382bb17772SBarry Smith Input Parameters: 6392bb17772SBarry Smith + pc - the preconditioner context 6402bb17772SBarry Smith - applyBA - the application-provided BA routine 6412bb17772SBarry Smith 6422bb17772SBarry Smith Calling sequence of apply: 6432bb17772SBarry Smith .vb 6446891c3e4SJed Brown PetscErrorCode applyBA (PC pc,Vec xin,Vec xout) 6452bb17772SBarry Smith .ve 6462bb17772SBarry Smith 6476891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 6482bb17772SBarry Smith . xin - input vector 6492bb17772SBarry Smith - xout - output vector 6502bb17772SBarry Smith 6514aa34b0aSBarry Smith Notes: the function MUST return an error code of 0 on success and nonzero on failure. 6524aa34b0aSBarry Smith 6532bb17772SBarry Smith Level: developer 6542bb17772SBarry Smith 6552bb17772SBarry Smith .keywords: PC, shell, set, apply, user-provided 6562bb17772SBarry Smith 6572bb17772SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetContext(), PCShellSetApply() 6582bb17772SBarry Smith @*/ 6597087cfbeSBarry Smith PetscErrorCode PCShellSetApplyBA(PC pc,PetscErrorCode (*applyBA)(PC,PCSide,Vec,Vec,Vec)) 6602bb17772SBarry Smith { 6614ac538c5SBarry Smith PetscErrorCode ierr; 6622bb17772SBarry Smith 6632bb17772SBarry Smith PetscFunctionBegin; 6640700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 6654ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetApplyBA_C",(PC,PetscErrorCode (*)(PC,PCSide,Vec,Vec,Vec)),(pc,applyBA));CHKERRQ(ierr); 6662bb17772SBarry Smith PetscFunctionReturn(0); 6672bb17772SBarry Smith } 6682bb17772SBarry Smith 6694b9ad928SBarry Smith /*@C 6704b9ad928SBarry Smith PCShellSetApplyTranspose - Sets routine to use as preconditioner transpose. 6714b9ad928SBarry Smith 6723f9fe445SBarry Smith Logically Collective on PC 6734b9ad928SBarry Smith 6744b9ad928SBarry Smith Input Parameters: 6754b9ad928SBarry Smith + pc - the preconditioner context 6764b9ad928SBarry Smith - apply - the application-provided preconditioning transpose routine 6774b9ad928SBarry Smith 6784b9ad928SBarry Smith Calling sequence of apply: 6794b9ad928SBarry Smith .vb 6806891c3e4SJed Brown PetscErrorCode applytranspose (PC pc,Vec xin,Vec xout) 6814b9ad928SBarry Smith .ve 6824b9ad928SBarry Smith 6836891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 6844b9ad928SBarry Smith . xin - input vector 6854b9ad928SBarry Smith - xout - output vector 6864b9ad928SBarry Smith 6874aa34b0aSBarry Smith Notes: the function MUST return an error code of 0 on success and nonzero on failure. 6884aa34b0aSBarry Smith 6894b9ad928SBarry Smith Level: developer 6904b9ad928SBarry Smith 6914b9ad928SBarry Smith Notes: 6924b9ad928SBarry Smith Uses the same context variable as PCShellSetApply(). 6934b9ad928SBarry Smith 6944b9ad928SBarry Smith .keywords: PC, shell, set, apply, user-provided 6954b9ad928SBarry Smith 6962bb17772SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApply(), PCSetContext(), PCShellSetApplyBA() 6974b9ad928SBarry Smith @*/ 6987087cfbeSBarry Smith PetscErrorCode PCShellSetApplyTranspose(PC pc,PetscErrorCode (*applytranspose)(PC,Vec,Vec)) 6994b9ad928SBarry Smith { 7004ac538c5SBarry Smith PetscErrorCode ierr; 7014b9ad928SBarry Smith 7024b9ad928SBarry Smith PetscFunctionBegin; 7030700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 7044ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetApplyTranspose_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,applytranspose));CHKERRQ(ierr); 7054b9ad928SBarry Smith PetscFunctionReturn(0); 7064b9ad928SBarry Smith } 7074b9ad928SBarry Smith 7087cdd61b2SBarry Smith /*@C 7097cdd61b2SBarry Smith PCShellSetPreSolve - Sets routine to apply to the operators/vectors before a KSPSolve() is 7107cdd61b2SBarry Smith applied. This usually does something like scale the linear system in some application 7117cdd61b2SBarry Smith specific way. 7127cdd61b2SBarry Smith 7133f9fe445SBarry Smith Logically Collective on PC 7147cdd61b2SBarry Smith 7157cdd61b2SBarry Smith Input Parameters: 7167cdd61b2SBarry Smith + pc - the preconditioner context 7177cdd61b2SBarry Smith - presolve - the application-provided presolve routine 7187cdd61b2SBarry Smith 7197cdd61b2SBarry Smith Calling sequence of presolve: 7207cdd61b2SBarry Smith .vb 7216891c3e4SJed Brown PetscErrorCode presolve (PC,KSP ksp,Vec b,Vec x) 7227cdd61b2SBarry Smith .ve 7237cdd61b2SBarry Smith 7246891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 7257cdd61b2SBarry Smith . xin - input vector 7267cdd61b2SBarry Smith - xout - output vector 7277cdd61b2SBarry Smith 7284aa34b0aSBarry Smith Notes: the function MUST return an error code of 0 on success and nonzero on failure. 7294aa34b0aSBarry Smith 7307cdd61b2SBarry Smith Level: developer 7317cdd61b2SBarry Smith 7327cdd61b2SBarry Smith .keywords: PC, shell, set, apply, user-provided 7337cdd61b2SBarry Smith 734be29d3c6SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetPostSolve(), PCShellSetContext() 7357cdd61b2SBarry Smith @*/ 7367087cfbeSBarry Smith PetscErrorCode PCShellSetPreSolve(PC pc,PetscErrorCode (*presolve)(PC,KSP,Vec,Vec)) 7377cdd61b2SBarry Smith { 7384ac538c5SBarry Smith PetscErrorCode ierr; 7397cdd61b2SBarry Smith 7407cdd61b2SBarry Smith PetscFunctionBegin; 7410700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 7424ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetPreSolve_C",(PC,PetscErrorCode (*)(PC,KSP,Vec,Vec)),(pc,presolve));CHKERRQ(ierr); 7437cdd61b2SBarry Smith PetscFunctionReturn(0); 7447cdd61b2SBarry Smith } 7457cdd61b2SBarry Smith 7467cdd61b2SBarry Smith /*@C 7477cdd61b2SBarry Smith PCShellSetPostSolve - Sets routine to apply to the operators/vectors before a KSPSolve() is 7487cdd61b2SBarry Smith applied. This usually does something like scale the linear system in some application 7497cdd61b2SBarry Smith specific way. 7507cdd61b2SBarry Smith 7513f9fe445SBarry Smith Logically Collective on PC 7527cdd61b2SBarry Smith 7537cdd61b2SBarry Smith Input Parameters: 7547cdd61b2SBarry Smith + pc - the preconditioner context 7557cdd61b2SBarry Smith - postsolve - the application-provided presolve routine 7567cdd61b2SBarry Smith 7577cdd61b2SBarry Smith Calling sequence of postsolve: 7587cdd61b2SBarry Smith .vb 7596891c3e4SJed Brown PetscErrorCode postsolve(PC,KSP ksp,Vec b,Vec x) 7607cdd61b2SBarry Smith .ve 7617cdd61b2SBarry Smith 7626891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 7637cdd61b2SBarry Smith . xin - input vector 7647cdd61b2SBarry Smith - xout - output vector 7657cdd61b2SBarry Smith 7664aa34b0aSBarry Smith Notes: the function MUST return an error code of 0 on success and nonzero on failure. 7674aa34b0aSBarry Smith 7687cdd61b2SBarry Smith Level: developer 7697cdd61b2SBarry Smith 7707cdd61b2SBarry Smith .keywords: PC, shell, set, apply, user-provided 7717cdd61b2SBarry Smith 772be29d3c6SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetPreSolve(), PCShellSetContext() 7737cdd61b2SBarry Smith @*/ 7747087cfbeSBarry Smith PetscErrorCode PCShellSetPostSolve(PC pc,PetscErrorCode (*postsolve)(PC,KSP,Vec,Vec)) 7757cdd61b2SBarry Smith { 7764ac538c5SBarry Smith PetscErrorCode ierr; 7777cdd61b2SBarry Smith 7787cdd61b2SBarry Smith PetscFunctionBegin; 7790700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 7804ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetPostSolve_C",(PC,PetscErrorCode (*)(PC,KSP,Vec,Vec)),(pc,postsolve));CHKERRQ(ierr); 7817cdd61b2SBarry Smith PetscFunctionReturn(0); 7827cdd61b2SBarry Smith } 7837cdd61b2SBarry Smith 7844b9ad928SBarry Smith /*@C 7854b9ad928SBarry Smith PCShellSetName - Sets an optional name to associate with a shell 7864b9ad928SBarry Smith preconditioner. 7874b9ad928SBarry Smith 7884b9ad928SBarry Smith Not Collective 7894b9ad928SBarry Smith 7904b9ad928SBarry Smith Input Parameters: 7914b9ad928SBarry Smith + pc - the preconditioner context 7924b9ad928SBarry Smith - name - character string describing shell preconditioner 7934b9ad928SBarry Smith 7944b9ad928SBarry Smith Level: developer 7954b9ad928SBarry Smith 7964b9ad928SBarry Smith .keywords: PC, shell, set, name, user-provided 7974b9ad928SBarry Smith 7984b9ad928SBarry Smith .seealso: PCShellGetName() 7994b9ad928SBarry Smith @*/ 8007087cfbeSBarry Smith PetscErrorCode PCShellSetName(PC pc,const char name[]) 8014b9ad928SBarry Smith { 8024ac538c5SBarry Smith PetscErrorCode ierr; 8034b9ad928SBarry Smith 8044b9ad928SBarry Smith PetscFunctionBegin; 8050700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 8064ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetName_C",(PC,const char []),(pc,name));CHKERRQ(ierr); 8074b9ad928SBarry Smith PetscFunctionReturn(0); 8084b9ad928SBarry Smith } 8094b9ad928SBarry Smith 8104b9ad928SBarry Smith /*@C 8114b9ad928SBarry Smith PCShellGetName - Gets an optional name that the user has set for a shell 8124b9ad928SBarry Smith preconditioner. 8134b9ad928SBarry Smith 8144b9ad928SBarry Smith Not Collective 8154b9ad928SBarry Smith 8164b9ad928SBarry Smith Input Parameter: 8174b9ad928SBarry Smith . pc - the preconditioner context 8184b9ad928SBarry Smith 8194b9ad928SBarry Smith Output Parameter: 8204b9ad928SBarry Smith . name - character string describing shell preconditioner (you should not free this) 8214b9ad928SBarry Smith 8224b9ad928SBarry Smith Level: developer 8234b9ad928SBarry Smith 8244b9ad928SBarry Smith .keywords: PC, shell, get, name, user-provided 8254b9ad928SBarry Smith 8264b9ad928SBarry Smith .seealso: PCShellSetName() 8274b9ad928SBarry Smith @*/ 828ccaf0856SBarry Smith PetscErrorCode PCShellGetName(PC pc,const char *name[]) 8294b9ad928SBarry Smith { 8304ac538c5SBarry Smith PetscErrorCode ierr; 8314b9ad928SBarry Smith 8324b9ad928SBarry Smith PetscFunctionBegin; 8330700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 8344482741eSBarry Smith PetscValidPointer(name,2); 835ccaf0856SBarry Smith ierr = PetscUseMethod(pc,"PCShellGetName_C",(PC,const char*[]),(pc,name));CHKERRQ(ierr); 8364b9ad928SBarry Smith PetscFunctionReturn(0); 8374b9ad928SBarry Smith } 8384b9ad928SBarry Smith 8394b9ad928SBarry Smith /*@C 8404b9ad928SBarry Smith PCShellSetApplyRichardson - Sets routine to use as preconditioner 8414b9ad928SBarry Smith in Richardson iteration. 8424b9ad928SBarry Smith 8433f9fe445SBarry Smith Logically Collective on PC 8444b9ad928SBarry Smith 8454b9ad928SBarry Smith Input Parameters: 8464b9ad928SBarry Smith + pc - the preconditioner context 847be29d3c6SBarry Smith - apply - the application-provided preconditioning routine 8484b9ad928SBarry Smith 8494b9ad928SBarry Smith Calling sequence of apply: 8504b9ad928SBarry Smith .vb 8516891c3e4SJed Brown PetscErrorCode apply (PC pc,Vec b,Vec x,Vec r,PetscReal rtol,PetscReal abstol,PetscReal dtol,PetscInt maxits) 8524b9ad928SBarry Smith .ve 8534b9ad928SBarry Smith 8546891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 8554b9ad928SBarry Smith . b - right-hand-side 8564b9ad928SBarry Smith . x - current iterate 8574b9ad928SBarry Smith . r - work space 8584b9ad928SBarry Smith . rtol - relative tolerance of residual norm to stop at 85970441072SBarry Smith . abstol - absolute tolerance of residual norm to stop at 8604b9ad928SBarry Smith . dtol - if residual norm increases by this factor than return 8614b9ad928SBarry Smith - maxits - number of iterations to run 8624b9ad928SBarry Smith 8634aa34b0aSBarry Smith Notes: the function MUST return an error code of 0 on success and nonzero on failure. 8644aa34b0aSBarry Smith 8654b9ad928SBarry Smith Level: developer 8664b9ad928SBarry Smith 8674b9ad928SBarry Smith .keywords: PC, shell, set, apply, Richardson, user-provided 8684b9ad928SBarry Smith 869be29d3c6SBarry Smith .seealso: PCShellSetApply(), PCShellSetContext() 8704b9ad928SBarry Smith @*/ 8717087cfbeSBarry Smith PetscErrorCode PCShellSetApplyRichardson(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool,PetscInt*,PCRichardsonConvergedReason*)) 8724b9ad928SBarry Smith { 8734ac538c5SBarry Smith PetscErrorCode ierr; 8744b9ad928SBarry Smith 8754b9ad928SBarry Smith PetscFunctionBegin; 8760700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 8774ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetApplyRichardson_C",(PC,PetscErrorCode (*)(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool,PetscInt*,PCRichardsonConvergedReason*)),(pc,apply));CHKERRQ(ierr); 8784b9ad928SBarry Smith PetscFunctionReturn(0); 8794b9ad928SBarry Smith } 8804b9ad928SBarry Smith 8814b9ad928SBarry Smith /*MC 8824b9ad928SBarry Smith PCSHELL - Creates a new preconditioner class for use with your 8834b9ad928SBarry Smith own private data storage format. 8844b9ad928SBarry Smith 8854b9ad928SBarry Smith Level: advanced 88690198e61SBarry Smith > 8874b9ad928SBarry Smith Concepts: providing your own preconditioner 8884b9ad928SBarry Smith 8894b9ad928SBarry Smith Usage: 8906891c3e4SJed Brown $ extern PetscErrorCode apply(PC,Vec,Vec); 8916891c3e4SJed Brown $ extern PetscErrorCode applyba(PC,PCSide,Vec,Vec,Vec); 8926891c3e4SJed Brown $ extern PetscErrorCode applytranspose(PC,Vec,Vec); 8936891c3e4SJed Brown $ extern PetscErrorCode setup(PC); 8946891c3e4SJed Brown $ extern PetscErrorCode destroy(PC); 8956891c3e4SJed Brown $ 8964b9ad928SBarry Smith $ PCCreate(comm,&pc); 8974b9ad928SBarry Smith $ PCSetType(pc,PCSHELL); 898be29d3c6SBarry Smith $ PCShellSetContext(pc,ctx) 8996891c3e4SJed Brown $ PCShellSetApply(pc,apply); 9006891c3e4SJed Brown $ PCShellSetApplyBA(pc,applyba); (optional) 9016891c3e4SJed Brown $ PCShellSetApplyTranspose(pc,applytranspose); (optional) 9024b9ad928SBarry Smith $ PCShellSetSetUp(pc,setup); (optional) 903d01c8aa3SLisandro Dalcin $ PCShellSetDestroy(pc,destroy); (optional) 9044b9ad928SBarry Smith 9054b9ad928SBarry Smith .seealso: PCCreate(), PCSetType(), PCType (for list of available types), PC, 906fd2d0fe1Svictor MATSHELL, PCShellSetSetUp(), PCShellSetApply(), PCShellSetView(), 907fd2d0fe1Svictor PCShellSetApplyTranspose(), PCShellSetName(), PCShellSetApplyRichardson(), 9082bb17772SBarry Smith PCShellGetName(), PCShellSetContext(), PCShellGetContext(), PCShellSetApplyBA() 9094b9ad928SBarry Smith M*/ 9104b9ad928SBarry Smith 9118cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PCCreate_Shell(PC pc) 9124b9ad928SBarry Smith { 913dfbe8321SBarry Smith PetscErrorCode ierr; 9144b9ad928SBarry Smith PC_Shell *shell; 9154b9ad928SBarry Smith 9164b9ad928SBarry Smith PetscFunctionBegin; 917b00a9115SJed Brown ierr = PetscNewLog(pc,&shell);CHKERRQ(ierr); 9184b9ad928SBarry Smith pc->data = (void*)shell; 9194b9ad928SBarry Smith 920d01c8aa3SLisandro Dalcin pc->ops->destroy = PCDestroy_Shell; 9214b9ad928SBarry Smith pc->ops->view = PCView_Shell; 922d01c8aa3SLisandro Dalcin pc->ops->apply = PCApply_Shell; 9231b581b66SBarry Smith pc->ops->applysymmetricleft = PCApplySymmetricLeft_Shell; 9241b581b66SBarry Smith pc->ops->applysymmetricright = PCApplySymmetricRight_Shell; 925d01c8aa3SLisandro Dalcin pc->ops->applytranspose = 0; 9264b9ad928SBarry Smith pc->ops->applyrichardson = 0; 927d01c8aa3SLisandro Dalcin pc->ops->setup = 0; 9289bbb2c88SBarry Smith pc->ops->presolve = 0; 9299bbb2c88SBarry Smith pc->ops->postsolve = 0; 9304b9ad928SBarry Smith 9314b9ad928SBarry Smith shell->apply = 0; 9324b9ad928SBarry Smith shell->applytranspose = 0; 9334b9ad928SBarry Smith shell->name = 0; 9344b9ad928SBarry Smith shell->applyrich = 0; 9357cdd61b2SBarry Smith shell->presolve = 0; 9367cdd61b2SBarry Smith shell->postsolve = 0; 9374b9ad928SBarry Smith shell->ctx = 0; 9384b9ad928SBarry Smith shell->setup = 0; 9394b9ad928SBarry Smith shell->view = 0; 94018be62a5SSatish Balay shell->destroy = 0; 9411b581b66SBarry Smith shell->applysymmetricleft = 0; 9421b581b66SBarry Smith shell->applysymmetricright = 0; 9434b9ad928SBarry Smith 944bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetDestroy_C",PCShellSetDestroy_Shell);CHKERRQ(ierr); 945bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetSetUp_C",PCShellSetSetUp_Shell);CHKERRQ(ierr); 946bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApply_C",PCShellSetApply_Shell);CHKERRQ(ierr); 9471b581b66SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplySymmetricLeft_C",PCShellSetApplySymmetricLeft_Shell);CHKERRQ(ierr); 9481b581b66SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplySymmetricRight_C",PCShellSetApplySymmetricRight_Shell);CHKERRQ(ierr); 949bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyBA_C",PCShellSetApplyBA_Shell);CHKERRQ(ierr); 950bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetPreSolve_C",PCShellSetPreSolve_Shell);CHKERRQ(ierr); 951bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetPostSolve_C",PCShellSetPostSolve_Shell);CHKERRQ(ierr); 952bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetView_C",PCShellSetView_Shell);CHKERRQ(ierr); 953bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyTranspose_C",PCShellSetApplyTranspose_Shell);CHKERRQ(ierr); 954bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetName_C",PCShellSetName_Shell);CHKERRQ(ierr); 955bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellGetName_C",PCShellGetName_Shell);CHKERRQ(ierr); 956bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyRichardson_C",PCShellSetApplyRichardson_Shell);CHKERRQ(ierr); 9574b9ad928SBarry Smith PetscFunctionReturn(0); 9584b9ad928SBarry Smith } 9594b9ad928SBarry Smith 9604b9ad928SBarry Smith 9614b9ad928SBarry Smith 9624b9ad928SBarry Smith 9634b9ad928SBarry Smith 9644b9ad928SBarry Smith 965