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 43*95452b02SPatrick Sanan Fortran Notes: 44*95452b02SPatrick 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 47be29d3c6SBarry Smith .keywords: PC, shell, get, context 48be29d3c6SBarry Smith 49c5ae4b9aSBarry Smith .seealso: PCShellSetContext() 50be29d3c6SBarry Smith @*/ 517087cfbeSBarry Smith PetscErrorCode PCShellGetContext(PC pc,void **ctx) 52be29d3c6SBarry Smith { 53be29d3c6SBarry Smith PetscErrorCode ierr; 54ace3abfcSBarry Smith PetscBool flg; 55be29d3c6SBarry Smith 56be29d3c6SBarry Smith PetscFunctionBegin; 570700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 58be29d3c6SBarry Smith PetscValidPointer(ctx,2); 59251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr); 60be29d3c6SBarry Smith if (!flg) *ctx = 0; 61be29d3c6SBarry Smith else *ctx = ((PC_Shell*)(pc->data))->ctx; 62be29d3c6SBarry Smith PetscFunctionReturn(0); 63be29d3c6SBarry Smith } 64be29d3c6SBarry Smith 659dd1005fSJed Brown /*@ 66be29d3c6SBarry Smith PCShellSetContext - sets the context for a shell PC 67be29d3c6SBarry Smith 683f9fe445SBarry Smith Logically Collective on PC 69be29d3c6SBarry Smith 70be29d3c6SBarry Smith Input Parameters: 71be29d3c6SBarry Smith + pc - the shell PC 72be29d3c6SBarry Smith - ctx - the context 73be29d3c6SBarry Smith 74be29d3c6SBarry Smith Level: advanced 75be29d3c6SBarry Smith 76*95452b02SPatrick Sanan Fortran Notes: 77*95452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 78daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 79daf670e6SBarry Smith 80be29d3c6SBarry Smith 816895c445SBarry Smith 82c5ae4b9aSBarry Smith .seealso: PCShellGetContext(), PCSHELL 83be29d3c6SBarry Smith @*/ 847087cfbeSBarry Smith PetscErrorCode PCShellSetContext(PC pc,void *ctx) 85be29d3c6SBarry Smith { 86c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 87be29d3c6SBarry Smith PetscErrorCode ierr; 88ace3abfcSBarry Smith PetscBool flg; 89be29d3c6SBarry Smith 90be29d3c6SBarry Smith PetscFunctionBegin; 910700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 92251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr); 932fa5cd67SKarl Rupp if (flg) shell->ctx = ctx; 94be29d3c6SBarry Smith PetscFunctionReturn(0); 95be29d3c6SBarry Smith } 96be29d3c6SBarry Smith 976849ba73SBarry Smith static PetscErrorCode PCSetUp_Shell(PC pc) 984b9ad928SBarry Smith { 99c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 100dfbe8321SBarry Smith PetscErrorCode ierr; 1014b9ad928SBarry Smith 1024b9ad928SBarry Smith PetscFunctionBegin; 103ce94432eSBarry Smith if (!shell->setup) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No setup() routine provided to Shell PC"); 104eb6b5d47SBarry Smith PetscStackCall("PCSHELL user function setup()",ierr = (*shell->setup)(pc);CHKERRQ(ierr)); 1054b9ad928SBarry Smith PetscFunctionReturn(0); 1064b9ad928SBarry Smith } 1074b9ad928SBarry Smith 1086849ba73SBarry Smith static PetscErrorCode PCApply_Shell(PC pc,Vec x,Vec y) 1094b9ad928SBarry Smith { 110c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 111dfbe8321SBarry Smith PetscErrorCode ierr; 112e3f487b0SBarry Smith PetscObjectState instate,outstate; 1134b9ad928SBarry Smith 1144b9ad928SBarry Smith PetscFunctionBegin; 115ce94432eSBarry Smith if (!shell->apply) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No apply() routine provided to Shell PC"); 116e3f487b0SBarry Smith ierr = PetscObjectStateGet((PetscObject)y, &instate);CHKERRQ(ierr); 117eb6b5d47SBarry Smith PetscStackCall("PCSHELL user function apply()",ierr = (*shell->apply)(pc,x,y);CHKERRQ(ierr)); 118e3f487b0SBarry Smith ierr = PetscObjectStateGet((PetscObject)y, &outstate);CHKERRQ(ierr); 119e3f487b0SBarry Smith if (instate == outstate) { 120e3f487b0SBarry Smith /* increase the state of the output vector since the user did not update its state themselve as should have been done */ 121e3f487b0SBarry Smith ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 122e3f487b0SBarry Smith } 1234b9ad928SBarry Smith PetscFunctionReturn(0); 1244b9ad928SBarry Smith } 1254b9ad928SBarry Smith 1261b581b66SBarry Smith static PetscErrorCode PCApplySymmetricLeft_Shell(PC pc,Vec x,Vec y) 1271b581b66SBarry Smith { 1281b581b66SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 1291b581b66SBarry Smith PetscErrorCode ierr; 1301b581b66SBarry Smith 1311b581b66SBarry Smith PetscFunctionBegin; 1321b581b66SBarry Smith if (!shell->applysymmetricleft) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No apply() routine provided to Shell PC"); 1331b581b66SBarry Smith PetscStackCall("PCSHELL user function apply()",ierr = (*shell->applysymmetricleft)(pc,x,y);CHKERRQ(ierr)); 1341b581b66SBarry Smith PetscFunctionReturn(0); 1351b581b66SBarry Smith } 1361b581b66SBarry Smith 1371b581b66SBarry Smith static PetscErrorCode PCApplySymmetricRight_Shell(PC pc,Vec x,Vec y) 1381b581b66SBarry Smith { 1391b581b66SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 1401b581b66SBarry Smith PetscErrorCode ierr; 1411b581b66SBarry Smith 1421b581b66SBarry Smith PetscFunctionBegin; 1431b581b66SBarry Smith if (!shell->applysymmetricright) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No apply() routine provided to Shell PC"); 1441b581b66SBarry Smith PetscStackCall("PCSHELL user function apply()",ierr = (*shell->applysymmetricright)(pc,x,y);CHKERRQ(ierr)); 1451b581b66SBarry Smith PetscFunctionReturn(0); 1461b581b66SBarry Smith } 1471b581b66SBarry Smith 1482bb17772SBarry Smith static PetscErrorCode PCApplyBA_Shell(PC pc,PCSide side,Vec x,Vec y,Vec w) 1492bb17772SBarry Smith { 150c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 1512bb17772SBarry Smith PetscErrorCode ierr; 152e3f487b0SBarry Smith PetscObjectState instate,outstate; 1532bb17772SBarry Smith 1542bb17772SBarry Smith PetscFunctionBegin; 155ce94432eSBarry Smith if (!shell->applyBA) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No applyBA() routine provided to Shell PC"); 156e3f487b0SBarry Smith ierr = PetscObjectStateGet((PetscObject)w, &instate);CHKERRQ(ierr); 157eb6b5d47SBarry Smith PetscStackCall("PCSHELL user function applyBA()",ierr = (*shell->applyBA)(pc,side,x,y,w);CHKERRQ(ierr)); 158e3f487b0SBarry Smith ierr = PetscObjectStateGet((PetscObject)w, &outstate);CHKERRQ(ierr); 159e3f487b0SBarry Smith if (instate == outstate) { 160e3f487b0SBarry Smith /* increase the state of the output vector since the user did not update its state themselve as should have been done */ 161e3f487b0SBarry Smith ierr = PetscObjectStateIncrease((PetscObject)w);CHKERRQ(ierr); 162e3f487b0SBarry Smith } 1632bb17772SBarry Smith PetscFunctionReturn(0); 1642bb17772SBarry Smith } 1652bb17772SBarry Smith 166a06fd7f2SStefano Zampini static PetscErrorCode PCPreSolveChangeRHS_Shell(PC pc,PetscBool* change) 167a06fd7f2SStefano Zampini { 168a06fd7f2SStefano Zampini PetscFunctionBegin; 169a06fd7f2SStefano Zampini *change = PETSC_TRUE; 170a06fd7f2SStefano Zampini PetscFunctionReturn(0); 171a06fd7f2SStefano Zampini } 172a06fd7f2SStefano Zampini 1737cdd61b2SBarry Smith static PetscErrorCode PCPreSolve_Shell(PC pc,KSP ksp,Vec b,Vec x) 1747cdd61b2SBarry Smith { 175c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 1767cdd61b2SBarry Smith PetscErrorCode ierr; 1777cdd61b2SBarry Smith 1787cdd61b2SBarry Smith PetscFunctionBegin; 179ce94432eSBarry Smith if (!shell->presolve) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No presolve() routine provided to Shell PC"); 180eb6b5d47SBarry Smith PetscStackCall("PCSHELL user function presolve()",ierr = (*shell->presolve)(pc,ksp,b,x);CHKERRQ(ierr)); 1817cdd61b2SBarry Smith PetscFunctionReturn(0); 1827cdd61b2SBarry Smith } 1837cdd61b2SBarry Smith 1847cdd61b2SBarry Smith static PetscErrorCode PCPostSolve_Shell(PC pc,KSP ksp,Vec b,Vec x) 1857cdd61b2SBarry Smith { 186c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 1877cdd61b2SBarry Smith PetscErrorCode ierr; 1887cdd61b2SBarry Smith 1897cdd61b2SBarry Smith PetscFunctionBegin; 190ce94432eSBarry Smith if (!shell->postsolve) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No postsolve() routine provided to Shell PC"); 191eb6b5d47SBarry Smith PetscStackCall("PCSHELL user function postsolve()",ierr = (*shell->postsolve)(pc,ksp,b,x);CHKERRQ(ierr)); 1927cdd61b2SBarry Smith PetscFunctionReturn(0); 1937cdd61b2SBarry Smith } 1947cdd61b2SBarry Smith 1956849ba73SBarry Smith static PetscErrorCode PCApplyTranspose_Shell(PC pc,Vec x,Vec y) 1964b9ad928SBarry Smith { 197c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 198dfbe8321SBarry Smith PetscErrorCode ierr; 199e3f487b0SBarry Smith PetscObjectState instate,outstate; 2004b9ad928SBarry Smith 2014b9ad928SBarry Smith PetscFunctionBegin; 202ce94432eSBarry Smith if (!shell->applytranspose) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No applytranspose() routine provided to Shell PC"); 203e3f487b0SBarry Smith ierr = PetscObjectStateGet((PetscObject)y, &instate);CHKERRQ(ierr); 204eb6b5d47SBarry Smith PetscStackCall("PCSHELL user function applytranspose()",ierr = (*shell->applytranspose)(pc,x,y);CHKERRQ(ierr)); 205e3f487b0SBarry Smith ierr = PetscObjectStateGet((PetscObject)y, &outstate);CHKERRQ(ierr); 206e3f487b0SBarry Smith if (instate == outstate) { 207e3f487b0SBarry Smith /* increase the state of the output vector since the user did not update its state themself as should have been done */ 208e3f487b0SBarry Smith ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 209e3f487b0SBarry Smith } 2104b9ad928SBarry Smith PetscFunctionReturn(0); 2114b9ad928SBarry Smith } 2124b9ad928SBarry Smith 213ace3abfcSBarry 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) 2144b9ad928SBarry Smith { 215dfbe8321SBarry Smith PetscErrorCode ierr; 216c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 217e3f487b0SBarry Smith PetscObjectState instate,outstate; 2184b9ad928SBarry Smith 2194b9ad928SBarry Smith PetscFunctionBegin; 220ce94432eSBarry Smith if (!shell->applyrich) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No applyrichardson() routine provided to Shell PC"); 221e3f487b0SBarry Smith ierr = PetscObjectStateGet((PetscObject)y, &instate);CHKERRQ(ierr); 222eb6b5d47SBarry Smith PetscStackCall("PCSHELL user function applyrichardson()",ierr = (*shell->applyrich)(pc,x,y,w,rtol,abstol,dtol,it,guesszero,outits,reason);CHKERRQ(ierr)); 223e3f487b0SBarry Smith ierr = PetscObjectStateGet((PetscObject)y, &outstate);CHKERRQ(ierr); 224e3f487b0SBarry Smith if (instate == outstate) { 225e3f487b0SBarry Smith /* increase the state of the output vector since the user did not update its state themself as should have been done */ 226e3f487b0SBarry Smith ierr = PetscObjectStateIncrease((PetscObject)y);CHKERRQ(ierr); 227e3f487b0SBarry Smith } 2284b9ad928SBarry Smith PetscFunctionReturn(0); 2294b9ad928SBarry Smith } 2304b9ad928SBarry Smith 2316849ba73SBarry Smith static PetscErrorCode PCDestroy_Shell(PC pc) 2324b9ad928SBarry Smith { 2334b9ad928SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 234dfbe8321SBarry Smith PetscErrorCode ierr; 2354b9ad928SBarry Smith 2364b9ad928SBarry Smith PetscFunctionBegin; 237503cfb0cSBarry Smith ierr = PetscFree(shell->name);CHKERRQ(ierr); 2382fa5cd67SKarl Rupp if (shell->destroy) PetscStackCall("PCSHELL user function destroy()",ierr = (*shell->destroy)(pc);CHKERRQ(ierr)); 239a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetDestroy_C",NULL);CHKERRQ(ierr); 240a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetSetUp_C",NULL);CHKERRQ(ierr); 241a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApply_C",NULL);CHKERRQ(ierr); 242a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplySymmetricLeft_C",NULL);CHKERRQ(ierr); 243a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplySymmetricRight_C",NULL);CHKERRQ(ierr); 244a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyBA_C",NULL);CHKERRQ(ierr); 245a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetPreSolve_C",NULL);CHKERRQ(ierr); 246a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetPostSolve_C",NULL);CHKERRQ(ierr); 247a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetView_C",NULL);CHKERRQ(ierr); 248a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyTranspose_C",NULL);CHKERRQ(ierr); 249a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetName_C",NULL);CHKERRQ(ierr); 250a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellGetName_C",NULL);CHKERRQ(ierr); 251a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyRichardson_C",NULL);CHKERRQ(ierr); 252a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCPreSolveChangeRHS_C",NULL);CHKERRQ(ierr); 253c31cb41cSBarry Smith ierr = PetscFree(pc->data);CHKERRQ(ierr); 2544b9ad928SBarry Smith PetscFunctionReturn(0); 2554b9ad928SBarry Smith } 2564b9ad928SBarry Smith 2576849ba73SBarry Smith static PetscErrorCode PCView_Shell(PC pc,PetscViewer viewer) 2584b9ad928SBarry Smith { 2594b9ad928SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 260dfbe8321SBarry Smith PetscErrorCode ierr; 261ace3abfcSBarry Smith PetscBool iascii; 2624b9ad928SBarry Smith 2634b9ad928SBarry Smith PetscFunctionBegin; 264251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 26532077d6dSBarry Smith if (iascii) { 2662fa5cd67SKarl Rupp if (shell->name) { 267efd4aadfSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %s\n",shell->name);CHKERRQ(ierr); 2682fa5cd67SKarl Rupp } else { 269efd4aadfSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," no name\n");CHKERRQ(ierr); 2702fa5cd67SKarl Rupp } 2714b9ad928SBarry Smith } 2724b9ad928SBarry Smith if (shell->view) { 2734b9ad928SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 2746891c3e4SJed Brown ierr = (*shell->view)(pc,viewer);CHKERRQ(ierr); 2754b9ad928SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2764b9ad928SBarry Smith } 2774b9ad928SBarry Smith PetscFunctionReturn(0); 2784b9ad928SBarry Smith } 2794b9ad928SBarry Smith 2804b9ad928SBarry Smith /* ------------------------------------------------------------------------------*/ 281f7a08781SBarry Smith static PetscErrorCode PCShellSetDestroy_Shell(PC pc, PetscErrorCode (*destroy)(PC)) 28218be62a5SSatish Balay { 283c5ae4b9aSBarry Smith PC_Shell *shell= (PC_Shell*)pc->data; 28418be62a5SSatish Balay 28518be62a5SSatish Balay PetscFunctionBegin; 28618be62a5SSatish Balay shell->destroy = destroy; 28718be62a5SSatish Balay PetscFunctionReturn(0); 28818be62a5SSatish Balay } 28918be62a5SSatish Balay 290f7a08781SBarry Smith static PetscErrorCode PCShellSetSetUp_Shell(PC pc, PetscErrorCode (*setup)(PC)) 2914b9ad928SBarry Smith { 292c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data;; 2934b9ad928SBarry Smith 2944b9ad928SBarry Smith PetscFunctionBegin; 2954b9ad928SBarry Smith shell->setup = setup; 296d01c8aa3SLisandro Dalcin if (setup) pc->ops->setup = PCSetUp_Shell; 297d01c8aa3SLisandro Dalcin else pc->ops->setup = 0; 2984b9ad928SBarry Smith PetscFunctionReturn(0); 2994b9ad928SBarry Smith } 3004b9ad928SBarry Smith 301f7a08781SBarry Smith static PetscErrorCode PCShellSetApply_Shell(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 3024b9ad928SBarry Smith { 303c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3044b9ad928SBarry Smith 3054b9ad928SBarry Smith PetscFunctionBegin; 3064b9ad928SBarry Smith shell->apply = apply; 3074b9ad928SBarry Smith PetscFunctionReturn(0); 3084b9ad928SBarry Smith } 3094b9ad928SBarry Smith 3101b581b66SBarry Smith static PetscErrorCode PCShellSetApplySymmetricLeft_Shell(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 3111b581b66SBarry Smith { 3121b581b66SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3131b581b66SBarry Smith 3141b581b66SBarry Smith PetscFunctionBegin; 3151b581b66SBarry Smith shell->applysymmetricleft = apply; 3161b581b66SBarry Smith PetscFunctionReturn(0); 3171b581b66SBarry Smith } 3181b581b66SBarry Smith 3191b581b66SBarry Smith static PetscErrorCode PCShellSetApplySymmetricRight_Shell(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 3201b581b66SBarry Smith { 3211b581b66SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3221b581b66SBarry Smith 3231b581b66SBarry Smith PetscFunctionBegin; 3241b581b66SBarry Smith shell->applysymmetricright = apply; 3251b581b66SBarry Smith PetscFunctionReturn(0); 3261b581b66SBarry Smith } 3271b581b66SBarry Smith 328f7a08781SBarry Smith static PetscErrorCode PCShellSetApplyBA_Shell(PC pc,PetscErrorCode (*applyBA)(PC,PCSide,Vec,Vec,Vec)) 3292bb17772SBarry Smith { 330c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3312bb17772SBarry Smith 3322bb17772SBarry Smith PetscFunctionBegin; 333d01c8aa3SLisandro Dalcin shell->applyBA = applyBA; 334d01c8aa3SLisandro Dalcin if (applyBA) pc->ops->applyBA = PCApplyBA_Shell; 335aef0136fSBarry Smith else pc->ops->applyBA = 0; 3362bb17772SBarry Smith PetscFunctionReturn(0); 3372bb17772SBarry Smith } 3382bb17772SBarry Smith 339f7a08781SBarry Smith static PetscErrorCode PCShellSetPreSolve_Shell(PC pc,PetscErrorCode (*presolve)(PC,KSP,Vec,Vec)) 3407cdd61b2SBarry Smith { 341c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 342a06fd7f2SStefano Zampini PetscErrorCode ierr; 3437cdd61b2SBarry Smith 3447cdd61b2SBarry Smith PetscFunctionBegin; 3457cdd61b2SBarry Smith shell->presolve = presolve; 346a06fd7f2SStefano Zampini if (presolve) { 347a06fd7f2SStefano Zampini pc->ops->presolve = PCPreSolve_Shell; 348a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCPreSolveChangeRHS_C",PCPreSolveChangeRHS_Shell);CHKERRQ(ierr); 349a06fd7f2SStefano Zampini } else { 350a06fd7f2SStefano Zampini pc->ops->presolve = 0; 351a06fd7f2SStefano Zampini ierr = PetscObjectComposeFunction((PetscObject)pc,"PCPreSolveChangeRHS_C",NULL);CHKERRQ(ierr); 352a06fd7f2SStefano Zampini } 3537cdd61b2SBarry Smith PetscFunctionReturn(0); 3547cdd61b2SBarry Smith } 3557cdd61b2SBarry Smith 356f7a08781SBarry Smith static PetscErrorCode PCShellSetPostSolve_Shell(PC pc,PetscErrorCode (*postsolve)(PC,KSP,Vec,Vec)) 3577cdd61b2SBarry Smith { 358c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3597cdd61b2SBarry Smith 3607cdd61b2SBarry Smith PetscFunctionBegin; 3617cdd61b2SBarry Smith shell->postsolve = postsolve; 362d01c8aa3SLisandro Dalcin if (postsolve) pc->ops->postsolve = PCPostSolve_Shell; 363d01c8aa3SLisandro Dalcin else pc->ops->postsolve = 0; 3647cdd61b2SBarry Smith PetscFunctionReturn(0); 3657cdd61b2SBarry Smith } 3667cdd61b2SBarry Smith 367f7a08781SBarry Smith static PetscErrorCode PCShellSetView_Shell(PC pc,PetscErrorCode (*view)(PC,PetscViewer)) 3684b9ad928SBarry Smith { 369c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3704b9ad928SBarry Smith 3714b9ad928SBarry Smith PetscFunctionBegin; 3724b9ad928SBarry Smith shell->view = view; 3734b9ad928SBarry Smith PetscFunctionReturn(0); 3744b9ad928SBarry Smith } 3754b9ad928SBarry Smith 376f7a08781SBarry Smith static PetscErrorCode PCShellSetApplyTranspose_Shell(PC pc,PetscErrorCode (*applytranspose)(PC,Vec,Vec)) 3774b9ad928SBarry Smith { 378c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3794b9ad928SBarry Smith 3804b9ad928SBarry Smith PetscFunctionBegin; 3814b9ad928SBarry Smith shell->applytranspose = applytranspose; 382d01c8aa3SLisandro Dalcin if (applytranspose) pc->ops->applytranspose = PCApplyTranspose_Shell; 383d01c8aa3SLisandro Dalcin else pc->ops->applytranspose = 0; 384d01c8aa3SLisandro Dalcin PetscFunctionReturn(0); 385d01c8aa3SLisandro Dalcin } 386d01c8aa3SLisandro Dalcin 387f7a08781SBarry Smith static PetscErrorCode PCShellSetApplyRichardson_Shell(PC pc,PetscErrorCode (*applyrich)(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool ,PetscInt*,PCRichardsonConvergedReason*)) 388d01c8aa3SLisandro Dalcin { 389c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 390d01c8aa3SLisandro Dalcin 391d01c8aa3SLisandro Dalcin PetscFunctionBegin; 392d01c8aa3SLisandro Dalcin shell->applyrich = applyrich; 393d01c8aa3SLisandro Dalcin if (applyrich) pc->ops->applyrichardson = PCApplyRichardson_Shell; 394d01c8aa3SLisandro Dalcin else pc->ops->applyrichardson = 0; 3954b9ad928SBarry Smith PetscFunctionReturn(0); 3964b9ad928SBarry Smith } 3974b9ad928SBarry Smith 398f7a08781SBarry Smith static PetscErrorCode PCShellSetName_Shell(PC pc,const char name[]) 3994b9ad928SBarry Smith { 400c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 401dfbe8321SBarry Smith PetscErrorCode ierr; 4024b9ad928SBarry Smith 4034b9ad928SBarry Smith PetscFunctionBegin; 404503cfb0cSBarry Smith ierr = PetscFree(shell->name);CHKERRQ(ierr); 4054b9ad928SBarry Smith ierr = PetscStrallocpy(name,&shell->name);CHKERRQ(ierr); 4064b9ad928SBarry Smith PetscFunctionReturn(0); 4074b9ad928SBarry Smith } 4084b9ad928SBarry Smith 409f7a08781SBarry Smith static PetscErrorCode PCShellGetName_Shell(PC pc,const char *name[]) 4104b9ad928SBarry Smith { 411c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 4124b9ad928SBarry Smith 4134b9ad928SBarry Smith PetscFunctionBegin; 4144b9ad928SBarry Smith *name = shell->name; 4154b9ad928SBarry Smith PetscFunctionReturn(0); 4164b9ad928SBarry Smith } 4174b9ad928SBarry Smith 4184b9ad928SBarry Smith /* -------------------------------------------------------------------------------*/ 4194b9ad928SBarry Smith 42018be62a5SSatish Balay /*@C 42118be62a5SSatish Balay PCShellSetDestroy - Sets routine to use to destroy the user-provided 42218be62a5SSatish Balay application context. 42318be62a5SSatish Balay 4243f9fe445SBarry Smith Logically Collective on PC 42518be62a5SSatish Balay 42618be62a5SSatish Balay Input Parameters: 42718be62a5SSatish Balay + pc - the preconditioner context 42818be62a5SSatish Balay . destroy - the application-provided destroy routine 42918be62a5SSatish Balay 43018be62a5SSatish Balay Calling sequence of destroy: 43118be62a5SSatish Balay .vb 4326891c3e4SJed Brown PetscErrorCode destroy (PC) 43318be62a5SSatish Balay .ve 43418be62a5SSatish Balay 43518be62a5SSatish Balay . ptr - the application context 43618be62a5SSatish Balay 437*95452b02SPatrick Sanan Notes: 438*95452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 4394aa34b0aSBarry Smith 44018be62a5SSatish Balay Level: developer 44118be62a5SSatish Balay 44218be62a5SSatish Balay .keywords: PC, shell, set, destroy, user-provided 44318be62a5SSatish Balay 44418be62a5SSatish Balay .seealso: PCShellSetApply(), PCShellSetContext() 44518be62a5SSatish Balay @*/ 4467087cfbeSBarry Smith PetscErrorCode PCShellSetDestroy(PC pc,PetscErrorCode (*destroy)(PC)) 44718be62a5SSatish Balay { 4484ac538c5SBarry Smith PetscErrorCode ierr; 44918be62a5SSatish Balay 45018be62a5SSatish Balay PetscFunctionBegin; 4510700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 4524ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetDestroy_C",(PC,PetscErrorCode (*)(PC)),(pc,destroy));CHKERRQ(ierr); 45318be62a5SSatish Balay PetscFunctionReturn(0); 45418be62a5SSatish Balay } 45518be62a5SSatish Balay 45618be62a5SSatish Balay 4574b9ad928SBarry Smith /*@C 4584b9ad928SBarry Smith PCShellSetSetUp - Sets routine to use to "setup" the preconditioner whenever the 4594b9ad928SBarry Smith matrix operator is changed. 4604b9ad928SBarry Smith 4613f9fe445SBarry Smith Logically Collective on PC 4624b9ad928SBarry Smith 4634b9ad928SBarry Smith Input Parameters: 4644b9ad928SBarry Smith + pc - the preconditioner context 4654b9ad928SBarry Smith . setup - the application-provided setup routine 4664b9ad928SBarry Smith 4674b9ad928SBarry Smith Calling sequence of setup: 4684b9ad928SBarry Smith .vb 4696891c3e4SJed Brown PetscErrorCode setup (PC pc) 4704b9ad928SBarry Smith .ve 4714b9ad928SBarry Smith 4726891c3e4SJed Brown . pc - the preconditioner, get the application context with PCShellGetContext() 4734b9ad928SBarry Smith 474*95452b02SPatrick Sanan Notes: 475*95452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 4764aa34b0aSBarry Smith 4774b9ad928SBarry Smith Level: developer 4784b9ad928SBarry Smith 4794b9ad928SBarry Smith .keywords: PC, shell, set, setup, user-provided 4804b9ad928SBarry Smith 481be29d3c6SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetApply(), PCShellSetContext() 4824b9ad928SBarry Smith @*/ 4837087cfbeSBarry Smith PetscErrorCode PCShellSetSetUp(PC pc,PetscErrorCode (*setup)(PC)) 4844b9ad928SBarry Smith { 4854ac538c5SBarry Smith PetscErrorCode ierr; 4864b9ad928SBarry Smith 4874b9ad928SBarry Smith PetscFunctionBegin; 4880700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 4894ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetSetUp_C",(PC,PetscErrorCode (*)(PC)),(pc,setup));CHKERRQ(ierr); 4904b9ad928SBarry Smith PetscFunctionReturn(0); 4914b9ad928SBarry Smith } 4924b9ad928SBarry Smith 4934b9ad928SBarry Smith 4944b9ad928SBarry Smith /*@C 4954b9ad928SBarry Smith PCShellSetView - Sets routine to use as viewer of shell preconditioner 4964b9ad928SBarry Smith 4973f9fe445SBarry Smith Logically Collective on PC 4984b9ad928SBarry Smith 4994b9ad928SBarry Smith Input Parameters: 5004b9ad928SBarry Smith + pc - the preconditioner context 5014b9ad928SBarry Smith - view - the application-provided view routine 5024b9ad928SBarry Smith 5034b9ad928SBarry Smith Calling sequence of apply: 5044b9ad928SBarry Smith .vb 5056891c3e4SJed Brown PetscErrorCode view(PC pc,PetscViewer v) 5064b9ad928SBarry Smith .ve 5074b9ad928SBarry Smith 5086891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 5094b9ad928SBarry Smith - v - viewer 5104b9ad928SBarry Smith 511*95452b02SPatrick Sanan Notes: 512*95452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 5134aa34b0aSBarry Smith 5144b9ad928SBarry Smith Level: developer 5154b9ad928SBarry Smith 5164b9ad928SBarry Smith .keywords: PC, shell, set, apply, user-provided 5174b9ad928SBarry Smith 5184b9ad928SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose() 5194b9ad928SBarry Smith @*/ 5207087cfbeSBarry Smith PetscErrorCode PCShellSetView(PC pc,PetscErrorCode (*view)(PC,PetscViewer)) 5214b9ad928SBarry Smith { 5224ac538c5SBarry Smith PetscErrorCode ierr; 5234b9ad928SBarry Smith 5244b9ad928SBarry Smith PetscFunctionBegin; 5250700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 5264ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetView_C",(PC,PetscErrorCode (*)(PC,PetscViewer)),(pc,view));CHKERRQ(ierr); 5274b9ad928SBarry Smith PetscFunctionReturn(0); 5284b9ad928SBarry Smith } 5294b9ad928SBarry Smith 5304b9ad928SBarry Smith /*@C 5314b9ad928SBarry Smith PCShellSetApply - Sets routine to use as preconditioner. 5324b9ad928SBarry Smith 5333f9fe445SBarry Smith Logically Collective on PC 5344b9ad928SBarry Smith 5354b9ad928SBarry Smith Input Parameters: 5364b9ad928SBarry Smith + pc - the preconditioner context 537be29d3c6SBarry Smith - apply - the application-provided preconditioning routine 5384b9ad928SBarry Smith 5394b9ad928SBarry Smith Calling sequence of apply: 5404b9ad928SBarry Smith .vb 5416891c3e4SJed Brown PetscErrorCode apply (PC pc,Vec xin,Vec xout) 5424b9ad928SBarry Smith .ve 5434b9ad928SBarry Smith 5446891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 5454b9ad928SBarry Smith . xin - input vector 5464b9ad928SBarry Smith - xout - output vector 5474b9ad928SBarry Smith 548*95452b02SPatrick Sanan Notes: 549*95452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 5504aa34b0aSBarry Smith 5514b9ad928SBarry Smith Level: developer 5524b9ad928SBarry Smith 5534b9ad928SBarry Smith .keywords: PC, shell, set, apply, user-provided 5544b9ad928SBarry Smith 555a4c07401SPatrick Sanan .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetContext(), PCShellSetApplyBA(), PCShellSetApplySymmetricRight(),PCShellSetApplySymmetricLeft() 5564b9ad928SBarry Smith @*/ 5577087cfbeSBarry Smith PetscErrorCode PCShellSetApply(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 5584b9ad928SBarry Smith { 5594ac538c5SBarry Smith PetscErrorCode ierr; 5604b9ad928SBarry Smith 5614b9ad928SBarry Smith PetscFunctionBegin; 5620700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 5634ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetApply_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,apply));CHKERRQ(ierr); 5644b9ad928SBarry Smith PetscFunctionReturn(0); 5654b9ad928SBarry Smith } 5664b9ad928SBarry Smith 5671b581b66SBarry Smith /*@C 5681b581b66SBarry Smith PCShellSetApplySymmetricLeft - Sets routine to use as left preconditioner (when the PC_SYMMETRIC is used). 5691b581b66SBarry Smith 5701b581b66SBarry Smith Logically Collective on PC 5711b581b66SBarry Smith 5721b581b66SBarry Smith Input Parameters: 5731b581b66SBarry Smith + pc - the preconditioner context 5741b581b66SBarry Smith - apply - the application-provided left preconditioning routine 5751b581b66SBarry Smith 5761b581b66SBarry Smith Calling sequence of apply: 5771b581b66SBarry Smith .vb 5781b581b66SBarry Smith PetscErrorCode apply (PC pc,Vec xin,Vec xout) 5791b581b66SBarry Smith .ve 5801b581b66SBarry Smith 5811b581b66SBarry Smith + pc - the preconditioner, get the application context with PCShellGetContext() 5821b581b66SBarry Smith . xin - input vector 5831b581b66SBarry Smith - xout - output vector 5841b581b66SBarry Smith 585*95452b02SPatrick Sanan Notes: 586*95452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 5871b581b66SBarry Smith 5881b581b66SBarry Smith Level: developer 5891b581b66SBarry Smith 5901b581b66SBarry Smith .keywords: PC, shell, set, apply, user-provided 5911b581b66SBarry Smith 5921b581b66SBarry Smith .seealso: PCShellSetApply(), PCShellSetApplySymmetricLeft(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetContext() 5931b581b66SBarry Smith @*/ 5941b581b66SBarry Smith PetscErrorCode PCShellSetApplySymmetricLeft(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 5951b581b66SBarry Smith { 5961b581b66SBarry Smith PetscErrorCode ierr; 5971b581b66SBarry Smith 5981b581b66SBarry Smith PetscFunctionBegin; 5991b581b66SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 6001b581b66SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetApplySymmetricLeft_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,apply));CHKERRQ(ierr); 6011b581b66SBarry Smith PetscFunctionReturn(0); 6021b581b66SBarry Smith } 6031b581b66SBarry Smith 6041b581b66SBarry Smith /*@C 605a4c07401SPatrick Sanan PCShellSetApplySymmetricRight - Sets routine to use as right preconditioner (when the PC_SYMMETRIC is used). 6061b581b66SBarry Smith 6071b581b66SBarry Smith Logically Collective on PC 6081b581b66SBarry Smith 6091b581b66SBarry Smith Input Parameters: 6101b581b66SBarry Smith + pc - the preconditioner context 6111b581b66SBarry Smith - apply - the application-provided right preconditioning routine 6121b581b66SBarry Smith 6131b581b66SBarry Smith Calling sequence of apply: 6141b581b66SBarry Smith .vb 6151b581b66SBarry Smith PetscErrorCode apply (PC pc,Vec xin,Vec xout) 6161b581b66SBarry Smith .ve 6171b581b66SBarry Smith 6181b581b66SBarry Smith + pc - the preconditioner, get the application context with PCShellGetContext() 6191b581b66SBarry Smith . xin - input vector 6201b581b66SBarry Smith - xout - output vector 6211b581b66SBarry Smith 622*95452b02SPatrick Sanan Notes: 623*95452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 6241b581b66SBarry Smith 6251b581b66SBarry Smith Level: developer 6261b581b66SBarry Smith 6271b581b66SBarry Smith .keywords: PC, shell, set, apply, user-provided 6281b581b66SBarry Smith 6291b581b66SBarry Smith .seealso: PCShellSetApply(), PCShellSetApplySymmetricLeft(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetContext() 6301b581b66SBarry Smith @*/ 6311b581b66SBarry Smith PetscErrorCode PCShellSetApplySymmetricRight(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 6321b581b66SBarry Smith { 6331b581b66SBarry Smith PetscErrorCode ierr; 6341b581b66SBarry Smith 6351b581b66SBarry Smith PetscFunctionBegin; 6361b581b66SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 6371b581b66SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetApplySymmetricRight_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,apply));CHKERRQ(ierr); 6381b581b66SBarry Smith PetscFunctionReturn(0); 6391b581b66SBarry Smith } 6401b581b66SBarry Smith 6412bb17772SBarry Smith /*@C 6422bb17772SBarry Smith PCShellSetApplyBA - Sets routine to use as preconditioner times operator. 6432bb17772SBarry Smith 6443f9fe445SBarry Smith Logically Collective on PC 6452bb17772SBarry Smith 6462bb17772SBarry Smith Input Parameters: 6472bb17772SBarry Smith + pc - the preconditioner context 6482bb17772SBarry Smith - applyBA - the application-provided BA routine 6492bb17772SBarry Smith 6502bb17772SBarry Smith Calling sequence of apply: 6512bb17772SBarry Smith .vb 6526891c3e4SJed Brown PetscErrorCode applyBA (PC pc,Vec xin,Vec xout) 6532bb17772SBarry Smith .ve 6542bb17772SBarry Smith 6556891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 6562bb17772SBarry Smith . xin - input vector 6572bb17772SBarry Smith - xout - output vector 6582bb17772SBarry Smith 659*95452b02SPatrick Sanan Notes: 660*95452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 6614aa34b0aSBarry Smith 6622bb17772SBarry Smith Level: developer 6632bb17772SBarry Smith 6642bb17772SBarry Smith .keywords: PC, shell, set, apply, user-provided 6652bb17772SBarry Smith 6662bb17772SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetContext(), PCShellSetApply() 6672bb17772SBarry Smith @*/ 6687087cfbeSBarry Smith PetscErrorCode PCShellSetApplyBA(PC pc,PetscErrorCode (*applyBA)(PC,PCSide,Vec,Vec,Vec)) 6692bb17772SBarry Smith { 6704ac538c5SBarry Smith PetscErrorCode ierr; 6712bb17772SBarry Smith 6722bb17772SBarry Smith PetscFunctionBegin; 6730700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 6744ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetApplyBA_C",(PC,PetscErrorCode (*)(PC,PCSide,Vec,Vec,Vec)),(pc,applyBA));CHKERRQ(ierr); 6752bb17772SBarry Smith PetscFunctionReturn(0); 6762bb17772SBarry Smith } 6772bb17772SBarry Smith 6784b9ad928SBarry Smith /*@C 6794b9ad928SBarry Smith PCShellSetApplyTranspose - Sets routine to use as preconditioner transpose. 6804b9ad928SBarry Smith 6813f9fe445SBarry Smith Logically Collective on PC 6824b9ad928SBarry Smith 6834b9ad928SBarry Smith Input Parameters: 6844b9ad928SBarry Smith + pc - the preconditioner context 6854b9ad928SBarry Smith - apply - the application-provided preconditioning transpose routine 6864b9ad928SBarry Smith 6874b9ad928SBarry Smith Calling sequence of apply: 6884b9ad928SBarry Smith .vb 6896891c3e4SJed Brown PetscErrorCode applytranspose (PC pc,Vec xin,Vec xout) 6904b9ad928SBarry Smith .ve 6914b9ad928SBarry Smith 6926891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 6934b9ad928SBarry Smith . xin - input vector 6944b9ad928SBarry Smith - xout - output vector 6954b9ad928SBarry Smith 696*95452b02SPatrick Sanan Notes: 697*95452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 6984aa34b0aSBarry Smith 6994b9ad928SBarry Smith Level: developer 7004b9ad928SBarry Smith 7014b9ad928SBarry Smith Notes: 7024b9ad928SBarry Smith Uses the same context variable as PCShellSetApply(). 7034b9ad928SBarry Smith 7044b9ad928SBarry Smith .keywords: PC, shell, set, apply, user-provided 7054b9ad928SBarry Smith 7062bb17772SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApply(), PCSetContext(), PCShellSetApplyBA() 7074b9ad928SBarry Smith @*/ 7087087cfbeSBarry Smith PetscErrorCode PCShellSetApplyTranspose(PC pc,PetscErrorCode (*applytranspose)(PC,Vec,Vec)) 7094b9ad928SBarry Smith { 7104ac538c5SBarry Smith PetscErrorCode ierr; 7114b9ad928SBarry Smith 7124b9ad928SBarry Smith PetscFunctionBegin; 7130700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 7144ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetApplyTranspose_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,applytranspose));CHKERRQ(ierr); 7154b9ad928SBarry Smith PetscFunctionReturn(0); 7164b9ad928SBarry Smith } 7174b9ad928SBarry Smith 7187cdd61b2SBarry Smith /*@C 7197cdd61b2SBarry Smith PCShellSetPreSolve - Sets routine to apply to the operators/vectors before a KSPSolve() is 7207cdd61b2SBarry Smith applied. This usually does something like scale the linear system in some application 7217cdd61b2SBarry Smith specific way. 7227cdd61b2SBarry Smith 7233f9fe445SBarry Smith Logically Collective on PC 7247cdd61b2SBarry Smith 7257cdd61b2SBarry Smith Input Parameters: 7267cdd61b2SBarry Smith + pc - the preconditioner context 7277cdd61b2SBarry Smith - presolve - the application-provided presolve routine 7287cdd61b2SBarry Smith 7297cdd61b2SBarry Smith Calling sequence of presolve: 7307cdd61b2SBarry Smith .vb 7316891c3e4SJed Brown PetscErrorCode presolve (PC,KSP ksp,Vec b,Vec x) 7327cdd61b2SBarry Smith .ve 7337cdd61b2SBarry Smith 7346891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 7357cdd61b2SBarry Smith . xin - input vector 7367cdd61b2SBarry Smith - xout - output vector 7377cdd61b2SBarry Smith 738*95452b02SPatrick Sanan Notes: 739*95452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 7404aa34b0aSBarry Smith 7417cdd61b2SBarry Smith Level: developer 7427cdd61b2SBarry Smith 7437cdd61b2SBarry Smith .keywords: PC, shell, set, apply, user-provided 7447cdd61b2SBarry Smith 745be29d3c6SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetPostSolve(), PCShellSetContext() 7467cdd61b2SBarry Smith @*/ 7477087cfbeSBarry Smith PetscErrorCode PCShellSetPreSolve(PC pc,PetscErrorCode (*presolve)(PC,KSP,Vec,Vec)) 7487cdd61b2SBarry Smith { 7494ac538c5SBarry Smith PetscErrorCode ierr; 7507cdd61b2SBarry Smith 7517cdd61b2SBarry Smith PetscFunctionBegin; 7520700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 7534ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetPreSolve_C",(PC,PetscErrorCode (*)(PC,KSP,Vec,Vec)),(pc,presolve));CHKERRQ(ierr); 7547cdd61b2SBarry Smith PetscFunctionReturn(0); 7557cdd61b2SBarry Smith } 7567cdd61b2SBarry Smith 7577cdd61b2SBarry Smith /*@C 7587cdd61b2SBarry Smith PCShellSetPostSolve - Sets routine to apply to the operators/vectors before a KSPSolve() is 7597cdd61b2SBarry Smith applied. This usually does something like scale the linear system in some application 7607cdd61b2SBarry Smith specific way. 7617cdd61b2SBarry Smith 7623f9fe445SBarry Smith Logically Collective on PC 7637cdd61b2SBarry Smith 7647cdd61b2SBarry Smith Input Parameters: 7657cdd61b2SBarry Smith + pc - the preconditioner context 7667cdd61b2SBarry Smith - postsolve - the application-provided presolve routine 7677cdd61b2SBarry Smith 7687cdd61b2SBarry Smith Calling sequence of postsolve: 7697cdd61b2SBarry Smith .vb 7706891c3e4SJed Brown PetscErrorCode postsolve(PC,KSP ksp,Vec b,Vec x) 7717cdd61b2SBarry Smith .ve 7727cdd61b2SBarry Smith 7736891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 7747cdd61b2SBarry Smith . xin - input vector 7757cdd61b2SBarry Smith - xout - output vector 7767cdd61b2SBarry Smith 777*95452b02SPatrick Sanan Notes: 778*95452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 7794aa34b0aSBarry Smith 7807cdd61b2SBarry Smith Level: developer 7817cdd61b2SBarry Smith 7827cdd61b2SBarry Smith .keywords: PC, shell, set, apply, user-provided 7837cdd61b2SBarry Smith 784be29d3c6SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetPreSolve(), PCShellSetContext() 7857cdd61b2SBarry Smith @*/ 7867087cfbeSBarry Smith PetscErrorCode PCShellSetPostSolve(PC pc,PetscErrorCode (*postsolve)(PC,KSP,Vec,Vec)) 7877cdd61b2SBarry Smith { 7884ac538c5SBarry Smith PetscErrorCode ierr; 7897cdd61b2SBarry Smith 7907cdd61b2SBarry Smith PetscFunctionBegin; 7910700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 7924ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetPostSolve_C",(PC,PetscErrorCode (*)(PC,KSP,Vec,Vec)),(pc,postsolve));CHKERRQ(ierr); 7937cdd61b2SBarry Smith PetscFunctionReturn(0); 7947cdd61b2SBarry Smith } 7957cdd61b2SBarry Smith 7964b9ad928SBarry Smith /*@C 7974b9ad928SBarry Smith PCShellSetName - Sets an optional name to associate with a shell 7984b9ad928SBarry Smith preconditioner. 7994b9ad928SBarry Smith 8004b9ad928SBarry Smith Not Collective 8014b9ad928SBarry Smith 8024b9ad928SBarry Smith Input Parameters: 8034b9ad928SBarry Smith + pc - the preconditioner context 8044b9ad928SBarry Smith - name - character string describing shell preconditioner 8054b9ad928SBarry Smith 8064b9ad928SBarry Smith Level: developer 8074b9ad928SBarry Smith 8084b9ad928SBarry Smith .keywords: PC, shell, set, name, user-provided 8094b9ad928SBarry Smith 8104b9ad928SBarry Smith .seealso: PCShellGetName() 8114b9ad928SBarry Smith @*/ 8127087cfbeSBarry Smith PetscErrorCode PCShellSetName(PC pc,const char name[]) 8134b9ad928SBarry Smith { 8144ac538c5SBarry Smith PetscErrorCode ierr; 8154b9ad928SBarry Smith 8164b9ad928SBarry Smith PetscFunctionBegin; 8170700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 8184ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetName_C",(PC,const char []),(pc,name));CHKERRQ(ierr); 8194b9ad928SBarry Smith PetscFunctionReturn(0); 8204b9ad928SBarry Smith } 8214b9ad928SBarry Smith 8224b9ad928SBarry Smith /*@C 8234b9ad928SBarry Smith PCShellGetName - Gets an optional name that the user has set for a shell 8244b9ad928SBarry Smith preconditioner. 8254b9ad928SBarry Smith 8264b9ad928SBarry Smith Not Collective 8274b9ad928SBarry Smith 8284b9ad928SBarry Smith Input Parameter: 8294b9ad928SBarry Smith . pc - the preconditioner context 8304b9ad928SBarry Smith 8314b9ad928SBarry Smith Output Parameter: 8324b9ad928SBarry Smith . name - character string describing shell preconditioner (you should not free this) 8334b9ad928SBarry Smith 8344b9ad928SBarry Smith Level: developer 8354b9ad928SBarry Smith 8364b9ad928SBarry Smith .keywords: PC, shell, get, name, user-provided 8374b9ad928SBarry Smith 8384b9ad928SBarry Smith .seealso: PCShellSetName() 8394b9ad928SBarry Smith @*/ 840ccaf0856SBarry Smith PetscErrorCode PCShellGetName(PC pc,const char *name[]) 8414b9ad928SBarry Smith { 8424ac538c5SBarry Smith PetscErrorCode ierr; 8434b9ad928SBarry Smith 8444b9ad928SBarry Smith PetscFunctionBegin; 8450700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 8464482741eSBarry Smith PetscValidPointer(name,2); 847ccaf0856SBarry Smith ierr = PetscUseMethod(pc,"PCShellGetName_C",(PC,const char*[]),(pc,name));CHKERRQ(ierr); 8484b9ad928SBarry Smith PetscFunctionReturn(0); 8494b9ad928SBarry Smith } 8504b9ad928SBarry Smith 8514b9ad928SBarry Smith /*@C 8524b9ad928SBarry Smith PCShellSetApplyRichardson - Sets routine to use as preconditioner 8534b9ad928SBarry Smith in Richardson iteration. 8544b9ad928SBarry Smith 8553f9fe445SBarry Smith Logically Collective on PC 8564b9ad928SBarry Smith 8574b9ad928SBarry Smith Input Parameters: 8584b9ad928SBarry Smith + pc - the preconditioner context 859be29d3c6SBarry Smith - apply - the application-provided preconditioning routine 8604b9ad928SBarry Smith 8614b9ad928SBarry Smith Calling sequence of apply: 8624b9ad928SBarry Smith .vb 8636891c3e4SJed Brown PetscErrorCode apply (PC pc,Vec b,Vec x,Vec r,PetscReal rtol,PetscReal abstol,PetscReal dtol,PetscInt maxits) 8644b9ad928SBarry Smith .ve 8654b9ad928SBarry Smith 8666891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 8674b9ad928SBarry Smith . b - right-hand-side 8684b9ad928SBarry Smith . x - current iterate 8694b9ad928SBarry Smith . r - work space 8704b9ad928SBarry Smith . rtol - relative tolerance of residual norm to stop at 87170441072SBarry Smith . abstol - absolute tolerance of residual norm to stop at 8724b9ad928SBarry Smith . dtol - if residual norm increases by this factor than return 8734b9ad928SBarry Smith - maxits - number of iterations to run 8744b9ad928SBarry Smith 875*95452b02SPatrick Sanan Notes: 876*95452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 8774aa34b0aSBarry Smith 8784b9ad928SBarry Smith Level: developer 8794b9ad928SBarry Smith 8804b9ad928SBarry Smith .keywords: PC, shell, set, apply, Richardson, user-provided 8814b9ad928SBarry Smith 882be29d3c6SBarry Smith .seealso: PCShellSetApply(), PCShellSetContext() 8834b9ad928SBarry Smith @*/ 8847087cfbeSBarry Smith PetscErrorCode PCShellSetApplyRichardson(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool,PetscInt*,PCRichardsonConvergedReason*)) 8854b9ad928SBarry Smith { 8864ac538c5SBarry Smith PetscErrorCode ierr; 8874b9ad928SBarry Smith 8884b9ad928SBarry Smith PetscFunctionBegin; 8890700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 8904ac538c5SBarry Smith ierr = PetscTryMethod(pc,"PCShellSetApplyRichardson_C",(PC,PetscErrorCode (*)(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool,PetscInt*,PCRichardsonConvergedReason*)),(pc,apply));CHKERRQ(ierr); 8914b9ad928SBarry Smith PetscFunctionReturn(0); 8924b9ad928SBarry Smith } 8934b9ad928SBarry Smith 8944b9ad928SBarry Smith /*MC 8954b9ad928SBarry Smith PCSHELL - Creates a new preconditioner class for use with your 8964b9ad928SBarry Smith own private data storage format. 8974b9ad928SBarry Smith 8984b9ad928SBarry Smith Level: advanced 89990198e61SBarry Smith > 9004b9ad928SBarry Smith Concepts: providing your own preconditioner 9014b9ad928SBarry Smith 9024b9ad928SBarry Smith Usage: 9036891c3e4SJed Brown $ extern PetscErrorCode apply(PC,Vec,Vec); 9046891c3e4SJed Brown $ extern PetscErrorCode applyba(PC,PCSide,Vec,Vec,Vec); 9056891c3e4SJed Brown $ extern PetscErrorCode applytranspose(PC,Vec,Vec); 9066891c3e4SJed Brown $ extern PetscErrorCode setup(PC); 9076891c3e4SJed Brown $ extern PetscErrorCode destroy(PC); 9086891c3e4SJed Brown $ 9094b9ad928SBarry Smith $ PCCreate(comm,&pc); 9104b9ad928SBarry Smith $ PCSetType(pc,PCSHELL); 911be29d3c6SBarry Smith $ PCShellSetContext(pc,ctx) 9126891c3e4SJed Brown $ PCShellSetApply(pc,apply); 9136891c3e4SJed Brown $ PCShellSetApplyBA(pc,applyba); (optional) 9146891c3e4SJed Brown $ PCShellSetApplyTranspose(pc,applytranspose); (optional) 9154b9ad928SBarry Smith $ PCShellSetSetUp(pc,setup); (optional) 916d01c8aa3SLisandro Dalcin $ PCShellSetDestroy(pc,destroy); (optional) 9174b9ad928SBarry Smith 9184b9ad928SBarry Smith .seealso: PCCreate(), PCSetType(), PCType (for list of available types), PC, 919fd2d0fe1Svictor MATSHELL, PCShellSetSetUp(), PCShellSetApply(), PCShellSetView(), 920fd2d0fe1Svictor PCShellSetApplyTranspose(), PCShellSetName(), PCShellSetApplyRichardson(), 9212bb17772SBarry Smith PCShellGetName(), PCShellSetContext(), PCShellGetContext(), PCShellSetApplyBA() 9224b9ad928SBarry Smith M*/ 9234b9ad928SBarry Smith 9248cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PCCreate_Shell(PC pc) 9254b9ad928SBarry Smith { 926dfbe8321SBarry Smith PetscErrorCode ierr; 9274b9ad928SBarry Smith PC_Shell *shell; 9284b9ad928SBarry Smith 9294b9ad928SBarry Smith PetscFunctionBegin; 930b00a9115SJed Brown ierr = PetscNewLog(pc,&shell);CHKERRQ(ierr); 9314b9ad928SBarry Smith pc->data = (void*)shell; 9324b9ad928SBarry Smith 933d01c8aa3SLisandro Dalcin pc->ops->destroy = PCDestroy_Shell; 9344b9ad928SBarry Smith pc->ops->view = PCView_Shell; 935d01c8aa3SLisandro Dalcin pc->ops->apply = PCApply_Shell; 9361b581b66SBarry Smith pc->ops->applysymmetricleft = PCApplySymmetricLeft_Shell; 9371b581b66SBarry Smith pc->ops->applysymmetricright = PCApplySymmetricRight_Shell; 938d01c8aa3SLisandro Dalcin pc->ops->applytranspose = 0; 9394b9ad928SBarry Smith pc->ops->applyrichardson = 0; 940d01c8aa3SLisandro Dalcin pc->ops->setup = 0; 9419bbb2c88SBarry Smith pc->ops->presolve = 0; 9429bbb2c88SBarry Smith pc->ops->postsolve = 0; 9434b9ad928SBarry Smith 9444b9ad928SBarry Smith shell->apply = 0; 9454b9ad928SBarry Smith shell->applytranspose = 0; 9464b9ad928SBarry Smith shell->name = 0; 9474b9ad928SBarry Smith shell->applyrich = 0; 9487cdd61b2SBarry Smith shell->presolve = 0; 9497cdd61b2SBarry Smith shell->postsolve = 0; 9504b9ad928SBarry Smith shell->ctx = 0; 9514b9ad928SBarry Smith shell->setup = 0; 9524b9ad928SBarry Smith shell->view = 0; 95318be62a5SSatish Balay shell->destroy = 0; 9541b581b66SBarry Smith shell->applysymmetricleft = 0; 9551b581b66SBarry Smith shell->applysymmetricright = 0; 9564b9ad928SBarry Smith 957bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetDestroy_C",PCShellSetDestroy_Shell);CHKERRQ(ierr); 958bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetSetUp_C",PCShellSetSetUp_Shell);CHKERRQ(ierr); 959bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApply_C",PCShellSetApply_Shell);CHKERRQ(ierr); 9601b581b66SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplySymmetricLeft_C",PCShellSetApplySymmetricLeft_Shell);CHKERRQ(ierr); 9611b581b66SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplySymmetricRight_C",PCShellSetApplySymmetricRight_Shell);CHKERRQ(ierr); 962bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyBA_C",PCShellSetApplyBA_Shell);CHKERRQ(ierr); 963bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetPreSolve_C",PCShellSetPreSolve_Shell);CHKERRQ(ierr); 964bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetPostSolve_C",PCShellSetPostSolve_Shell);CHKERRQ(ierr); 965bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetView_C",PCShellSetView_Shell);CHKERRQ(ierr); 966bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyTranspose_C",PCShellSetApplyTranspose_Shell);CHKERRQ(ierr); 967bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetName_C",PCShellSetName_Shell);CHKERRQ(ierr); 968bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellGetName_C",PCShellGetName_Shell);CHKERRQ(ierr); 969bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyRichardson_C",PCShellSetApplyRichardson_Shell);CHKERRQ(ierr); 9704b9ad928SBarry Smith PetscFunctionReturn(0); 9714b9ad928SBarry Smith } 9724b9ad928SBarry Smith 9734b9ad928SBarry Smith 9744b9ad928SBarry Smith 9754b9ad928SBarry Smith 9764b9ad928SBarry Smith 9774b9ad928SBarry Smith 978