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); 157b6e2003SPierre Jolivet PetscErrorCode (*matapply)(PC,Mat,Mat); 161b581b66SBarry Smith PetscErrorCode (*applysymmetricleft)(PC,Vec,Vec); 171b581b66SBarry Smith PetscErrorCode (*applysymmetricright)(PC,Vec,Vec); 186891c3e4SJed Brown PetscErrorCode (*applyBA)(PC,PCSide,Vec,Vec,Vec); 196891c3e4SJed Brown PetscErrorCode (*presolve)(PC,KSP,Vec,Vec); 206891c3e4SJed Brown PetscErrorCode (*postsolve)(PC,KSP,Vec,Vec); 216891c3e4SJed Brown PetscErrorCode (*view)(PC,PetscViewer); 226891c3e4SJed Brown PetscErrorCode (*applytranspose)(PC,Vec,Vec); 23ace3abfcSBarry Smith PetscErrorCode (*applyrich)(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool,PetscInt*,PCRichardsonConvergedReason*); 242fa5cd67SKarl Rupp 254b9ad928SBarry Smith char *name; 264b9ad928SBarry Smith } PC_Shell; 274b9ad928SBarry Smith 28b29801fcSSatish Balay /*@C 29be29d3c6SBarry Smith PCShellGetContext - Returns the user-provided context associated with a shell PC 30be29d3c6SBarry Smith 31be29d3c6SBarry Smith Not Collective 32be29d3c6SBarry Smith 33be29d3c6SBarry Smith Input Parameter: 34c5ae4b9aSBarry Smith . pc - should have been created with PCSetType(pc,shell) 35be29d3c6SBarry Smith 36be29d3c6SBarry Smith Output Parameter: 37be29d3c6SBarry Smith . ctx - the user provided context 38be29d3c6SBarry Smith 39be29d3c6SBarry Smith Level: advanced 40be29d3c6SBarry Smith 41be29d3c6SBarry Smith Notes: 42be29d3c6SBarry Smith This routine is intended for use within various shell routines 43be29d3c6SBarry Smith 4495452b02SPatrick Sanan Fortran Notes: 4595452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 46daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 47daf670e6SBarry Smith 48db781477SPatrick Sanan .seealso: `PCShellSetContext()` 49be29d3c6SBarry Smith @*/ 503ec1f749SStefano Zampini PetscErrorCode PCShellGetContext(PC pc,void *ctx) 51be29d3c6SBarry Smith { 52ace3abfcSBarry Smith PetscBool flg; 53be29d3c6SBarry Smith 54be29d3c6SBarry Smith PetscFunctionBegin; 550700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 56be29d3c6SBarry Smith PetscValidPointer(ctx,2); 579566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)pc,PCSHELL,&flg)); 583ec1f749SStefano Zampini if (!flg) *(void**)ctx = NULL; 593ec1f749SStefano Zampini else *(void**)ctx = ((PC_Shell*)(pc->data))->ctx; 60be29d3c6SBarry Smith PetscFunctionReturn(0); 61be29d3c6SBarry Smith } 62be29d3c6SBarry Smith 639dd1005fSJed Brown /*@ 64be29d3c6SBarry Smith PCShellSetContext - sets the context for a shell PC 65be29d3c6SBarry Smith 663f9fe445SBarry Smith Logically Collective on PC 67be29d3c6SBarry Smith 68be29d3c6SBarry Smith Input Parameters: 69be29d3c6SBarry Smith + pc - the shell PC 70be29d3c6SBarry Smith - ctx - the context 71be29d3c6SBarry Smith 72be29d3c6SBarry Smith Level: advanced 73be29d3c6SBarry Smith 7495452b02SPatrick Sanan Fortran Notes: 7595452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 76daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 77daf670e6SBarry Smith 78db781477SPatrick Sanan .seealso: `PCShellGetContext()`, `PCSHELL` 79be29d3c6SBarry Smith @*/ 807087cfbeSBarry Smith PetscErrorCode PCShellSetContext(PC pc,void *ctx) 81be29d3c6SBarry Smith { 82c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 83ace3abfcSBarry Smith PetscBool flg; 84be29d3c6SBarry Smith 85be29d3c6SBarry Smith PetscFunctionBegin; 860700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 879566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)pc,PCSHELL,&flg)); 882fa5cd67SKarl Rupp if (flg) shell->ctx = ctx; 89be29d3c6SBarry Smith PetscFunctionReturn(0); 90be29d3c6SBarry Smith } 91be29d3c6SBarry Smith 926849ba73SBarry Smith static PetscErrorCode PCSetUp_Shell(PC pc) 934b9ad928SBarry Smith { 94c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 954b9ad928SBarry Smith 964b9ad928SBarry Smith PetscFunctionBegin; 9728b400f6SJacob Faibussowitsch PetscCheck(shell->setup,PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No setup() routine provided to Shell PC"); 9825e27a38SBarry Smith PetscCallBack("PCSHELL callback setup",(*shell->setup)(pc)); 994b9ad928SBarry Smith PetscFunctionReturn(0); 1004b9ad928SBarry Smith } 1014b9ad928SBarry Smith 1026849ba73SBarry Smith static PetscErrorCode PCApply_Shell(PC pc,Vec x,Vec y) 1034b9ad928SBarry Smith { 104c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 105e3f487b0SBarry Smith PetscObjectState instate,outstate; 1064b9ad928SBarry Smith 1074b9ad928SBarry Smith PetscFunctionBegin; 10828b400f6SJacob Faibussowitsch PetscCheck(shell->apply,PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No apply() routine provided to Shell PC"); 1099566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)y, &instate)); 11025e27a38SBarry Smith PetscCallBack("PCSHELL callback apply",(*shell->apply)(pc,x,y)); 1119566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)y, &outstate)); 112*1e66621cSBarry Smith /* increase the state of the output vector if the user did not update its state themself as should have been done */ 113*1e66621cSBarry Smith if (instate == outstate) PetscCall(PetscObjectStateIncrease((PetscObject)y)); 1144b9ad928SBarry Smith PetscFunctionReturn(0); 1154b9ad928SBarry Smith } 1164b9ad928SBarry Smith 1177b6e2003SPierre Jolivet static PetscErrorCode PCMatApply_Shell(PC pc,Mat X,Mat Y) 1187b6e2003SPierre Jolivet { 1197b6e2003SPierre Jolivet PC_Shell *shell = (PC_Shell*)pc->data; 1207b6e2003SPierre Jolivet PetscObjectState instate,outstate; 1217b6e2003SPierre Jolivet 1227b6e2003SPierre Jolivet PetscFunctionBegin; 12328b400f6SJacob Faibussowitsch PetscCheck(shell->matapply,PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No apply() routine provided to Shell PC"); 1249566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)Y, &instate)); 12525e27a38SBarry Smith PetscCallBack("PCSHELL callback apply",(*shell->matapply)(pc,X,Y)); 1269566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)Y, &outstate)); 127*1e66621cSBarry Smith /* increase the state of the output vector if the user did not update its state themself as should have been done */ 128*1e66621cSBarry Smith if (instate == outstate) PetscCall(PetscObjectStateIncrease((PetscObject)Y)); 1297b6e2003SPierre Jolivet PetscFunctionReturn(0); 1307b6e2003SPierre Jolivet } 1317b6e2003SPierre Jolivet 1321b581b66SBarry Smith static PetscErrorCode PCApplySymmetricLeft_Shell(PC pc,Vec x,Vec y) 1331b581b66SBarry Smith { 1341b581b66SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 1351b581b66SBarry Smith 1361b581b66SBarry Smith PetscFunctionBegin; 13728b400f6SJacob Faibussowitsch PetscCheck(shell->applysymmetricleft,PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No apply() routine provided to Shell PC"); 13825e27a38SBarry Smith PetscCallBack("PCSHELL callback apply symmetric left",(*shell->applysymmetricleft)(pc,x,y)); 1391b581b66SBarry Smith PetscFunctionReturn(0); 1401b581b66SBarry Smith } 1411b581b66SBarry Smith 1421b581b66SBarry Smith static PetscErrorCode PCApplySymmetricRight_Shell(PC pc,Vec x,Vec y) 1431b581b66SBarry Smith { 1441b581b66SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 1451b581b66SBarry Smith 1461b581b66SBarry Smith PetscFunctionBegin; 14728b400f6SJacob Faibussowitsch PetscCheck(shell->applysymmetricright,PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No apply() routine provided to Shell PC"); 14825e27a38SBarry Smith PetscCallBack("PCSHELL callback apply symmetric right",(*shell->applysymmetricright)(pc,x,y)); 1491b581b66SBarry Smith PetscFunctionReturn(0); 1501b581b66SBarry Smith } 1511b581b66SBarry Smith 1522bb17772SBarry Smith static PetscErrorCode PCApplyBA_Shell(PC pc,PCSide side,Vec x,Vec y,Vec w) 1532bb17772SBarry Smith { 154c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 155e3f487b0SBarry Smith PetscObjectState instate,outstate; 1562bb17772SBarry Smith 1572bb17772SBarry Smith PetscFunctionBegin; 15828b400f6SJacob Faibussowitsch PetscCheck(shell->applyBA,PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No applyBA() routine provided to Shell PC"); 1599566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)w, &instate)); 16025e27a38SBarry Smith PetscCallBack("PCSHELL callback applyBA",(*shell->applyBA)(pc,side,x,y,w)); 1619566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)w, &outstate)); 162*1e66621cSBarry Smith /* increase the state of the output vector if the user did not update its state themself as should have been done */ 163*1e66621cSBarry Smith if (instate == outstate) PetscCall(PetscObjectStateIncrease((PetscObject)w)); 1642bb17772SBarry Smith PetscFunctionReturn(0); 1652bb17772SBarry Smith } 1662bb17772SBarry Smith 167a06fd7f2SStefano Zampini static PetscErrorCode PCPreSolveChangeRHS_Shell(PC pc,PetscBool* change) 168a06fd7f2SStefano Zampini { 169a06fd7f2SStefano Zampini PetscFunctionBegin; 170a06fd7f2SStefano Zampini *change = PETSC_TRUE; 171a06fd7f2SStefano Zampini PetscFunctionReturn(0); 172a06fd7f2SStefano Zampini } 173a06fd7f2SStefano Zampini 1747cdd61b2SBarry Smith static PetscErrorCode PCPreSolve_Shell(PC pc,KSP ksp,Vec b,Vec x) 1757cdd61b2SBarry Smith { 176c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 1777cdd61b2SBarry Smith 1787cdd61b2SBarry Smith PetscFunctionBegin; 17928b400f6SJacob Faibussowitsch PetscCheck(shell->presolve,PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No presolve() routine provided to Shell PC"); 18025e27a38SBarry Smith PetscCallBack("PCSHELL callback presolve",(*shell->presolve)(pc,ksp,b,x)); 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 1887cdd61b2SBarry Smith PetscFunctionBegin; 18928b400f6SJacob Faibussowitsch PetscCheck(shell->postsolve,PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No postsolve() routine provided to Shell PC"); 19025e27a38SBarry Smith PetscCallBack("PCSHELL callback postsolve()",(*shell->postsolve)(pc,ksp,b,x)); 1917cdd61b2SBarry Smith PetscFunctionReturn(0); 1927cdd61b2SBarry Smith } 1937cdd61b2SBarry Smith 1946849ba73SBarry Smith static PetscErrorCode PCApplyTranspose_Shell(PC pc,Vec x,Vec y) 1954b9ad928SBarry Smith { 196c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 197e3f487b0SBarry Smith PetscObjectState instate,outstate; 1984b9ad928SBarry Smith 1994b9ad928SBarry Smith PetscFunctionBegin; 20028b400f6SJacob Faibussowitsch PetscCheck(shell->applytranspose,PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No applytranspose() routine provided to Shell PC"); 2019566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)y, &instate)); 20225e27a38SBarry Smith PetscCallBack("PCSHELL callback applytranspose",(*shell->applytranspose)(pc,x,y)); 2039566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)y, &outstate)); 204*1e66621cSBarry Smith /* increase the state of the output vector if the user did not update its state themself as should have been done */ 205*1e66621cSBarry Smith if (instate == outstate) PetscCall(PetscObjectStateIncrease((PetscObject)y)); 2064b9ad928SBarry Smith PetscFunctionReturn(0); 2074b9ad928SBarry Smith } 2084b9ad928SBarry Smith 209ace3abfcSBarry 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) 2104b9ad928SBarry Smith { 211c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 212e3f487b0SBarry Smith PetscObjectState instate,outstate; 2134b9ad928SBarry Smith 2144b9ad928SBarry Smith PetscFunctionBegin; 21528b400f6SJacob Faibussowitsch PetscCheck(shell->applyrich,PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No applyrichardson() routine provided to Shell PC"); 2169566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)y, &instate)); 21725e27a38SBarry Smith PetscCallBack("PCSHELL callback applyrichardson",(*shell->applyrich)(pc,x,y,w,rtol,abstol,dtol,it,guesszero,outits,reason)); 2189566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)y, &outstate)); 219e3f487b0SBarry Smith /* increase the state of the output vector since the user did not update its state themself as should have been done */ 220*1e66621cSBarry Smith if (instate == outstate) PetscCall(PetscObjectStateIncrease((PetscObject)y)); 2214b9ad928SBarry Smith PetscFunctionReturn(0); 2224b9ad928SBarry Smith } 2234b9ad928SBarry Smith 2246849ba73SBarry Smith static PetscErrorCode PCDestroy_Shell(PC pc) 2254b9ad928SBarry Smith { 2264b9ad928SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 2274b9ad928SBarry Smith 2284b9ad928SBarry Smith PetscFunctionBegin; 2299566063dSJacob Faibussowitsch PetscCall(PetscFree(shell->name)); 23025e27a38SBarry Smith if (shell->destroy) PetscCallBack("PCSHELL callback destroy",(*shell->destroy)(pc)); 2319566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetDestroy_C",NULL)); 2329566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetSetUp_C",NULL)); 2339566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApply_C",NULL)); 2349566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetMatApply_C",NULL)); 2359566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplySymmetricLeft_C",NULL)); 2369566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplySymmetricRight_C",NULL)); 2379566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyBA_C",NULL)); 2389566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetPreSolve_C",NULL)); 2399566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetPostSolve_C",NULL)); 2409566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetView_C",NULL)); 2419566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyTranspose_C",NULL)); 2429566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetName_C",NULL)); 2439566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellGetName_C",NULL)); 2449566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyRichardson_C",NULL)); 2459566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCPreSolveChangeRHS_C",NULL)); 2469566063dSJacob Faibussowitsch PetscCall(PetscFree(pc->data)); 2474b9ad928SBarry Smith PetscFunctionReturn(0); 2484b9ad928SBarry Smith } 2494b9ad928SBarry Smith 2506849ba73SBarry Smith static PetscErrorCode PCView_Shell(PC pc,PetscViewer viewer) 2514b9ad928SBarry Smith { 2524b9ad928SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 253ace3abfcSBarry Smith PetscBool iascii; 2544b9ad928SBarry Smith 2554b9ad928SBarry Smith PetscFunctionBegin; 2569566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii)); 25732077d6dSBarry Smith if (iascii) { 258*1e66621cSBarry Smith if (shell->name) PetscCall(PetscViewerASCIIPrintf(viewer," %s\n",shell->name)); 259*1e66621cSBarry Smith else PetscCall(PetscViewerASCIIPrintf(viewer," no name\n")); 2604b9ad928SBarry Smith } 2614b9ad928SBarry Smith if (shell->view) { 2629566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 2639566063dSJacob Faibussowitsch PetscCall((*shell->view)(pc,viewer)); 2649566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 2654b9ad928SBarry Smith } 2664b9ad928SBarry Smith PetscFunctionReturn(0); 2674b9ad928SBarry Smith } 2684b9ad928SBarry Smith 2694b9ad928SBarry Smith /* ------------------------------------------------------------------------------*/ 270f7a08781SBarry Smith static PetscErrorCode PCShellSetDestroy_Shell(PC pc, PetscErrorCode (*destroy)(PC)) 27118be62a5SSatish Balay { 272c5ae4b9aSBarry Smith PC_Shell *shell= (PC_Shell*)pc->data; 27318be62a5SSatish Balay 27418be62a5SSatish Balay PetscFunctionBegin; 27518be62a5SSatish Balay shell->destroy = destroy; 27618be62a5SSatish Balay PetscFunctionReturn(0); 27718be62a5SSatish Balay } 27818be62a5SSatish Balay 279f7a08781SBarry Smith static PetscErrorCode PCShellSetSetUp_Shell(PC pc, PetscErrorCode (*setup)(PC)) 2804b9ad928SBarry Smith { 281feb237baSPierre Jolivet PC_Shell *shell = (PC_Shell*)pc->data; 2824b9ad928SBarry Smith 2834b9ad928SBarry Smith PetscFunctionBegin; 2844b9ad928SBarry Smith shell->setup = setup; 285d01c8aa3SLisandro Dalcin if (setup) pc->ops->setup = PCSetUp_Shell; 2860a545947SLisandro Dalcin else pc->ops->setup = NULL; 2874b9ad928SBarry Smith PetscFunctionReturn(0); 2884b9ad928SBarry Smith } 2894b9ad928SBarry Smith 290f7a08781SBarry Smith static PetscErrorCode PCShellSetApply_Shell(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 2914b9ad928SBarry Smith { 292c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 2934b9ad928SBarry Smith 2944b9ad928SBarry Smith PetscFunctionBegin; 2954b9ad928SBarry Smith shell->apply = apply; 2964b9ad928SBarry Smith PetscFunctionReturn(0); 2974b9ad928SBarry Smith } 2984b9ad928SBarry Smith 2997b6e2003SPierre Jolivet static PetscErrorCode PCShellSetMatApply_Shell(PC pc,PetscErrorCode (*matapply)(PC,Mat,Mat)) 3007b6e2003SPierre Jolivet { 3017b6e2003SPierre Jolivet PC_Shell *shell = (PC_Shell*)pc->data; 3027b6e2003SPierre Jolivet 3037b6e2003SPierre Jolivet PetscFunctionBegin; 3047b6e2003SPierre Jolivet shell->matapply = matapply; 3050e0fe96bSStefano Zampini if (matapply) pc->ops->matapply = PCMatApply_Shell; 3060e0fe96bSStefano Zampini else pc->ops->matapply = NULL; 3077b6e2003SPierre Jolivet PetscFunctionReturn(0); 3087b6e2003SPierre Jolivet } 3097b6e2003SPierre Jolivet 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; 3350a545947SLisandro Dalcin else pc->ops->applyBA = NULL; 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; 3427cdd61b2SBarry Smith 3437cdd61b2SBarry Smith PetscFunctionBegin; 3447cdd61b2SBarry Smith shell->presolve = presolve; 345a06fd7f2SStefano Zampini if (presolve) { 346a06fd7f2SStefano Zampini pc->ops->presolve = PCPreSolve_Shell; 3479566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCPreSolveChangeRHS_C",PCPreSolveChangeRHS_Shell)); 348a06fd7f2SStefano Zampini } else { 3490a545947SLisandro Dalcin pc->ops->presolve = NULL; 3509566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCPreSolveChangeRHS_C",NULL)); 351a06fd7f2SStefano Zampini } 3527cdd61b2SBarry Smith PetscFunctionReturn(0); 3537cdd61b2SBarry Smith } 3547cdd61b2SBarry Smith 355f7a08781SBarry Smith static PetscErrorCode PCShellSetPostSolve_Shell(PC pc,PetscErrorCode (*postsolve)(PC,KSP,Vec,Vec)) 3567cdd61b2SBarry Smith { 357c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3587cdd61b2SBarry Smith 3597cdd61b2SBarry Smith PetscFunctionBegin; 3607cdd61b2SBarry Smith shell->postsolve = postsolve; 361d01c8aa3SLisandro Dalcin if (postsolve) pc->ops->postsolve = PCPostSolve_Shell; 3620a545947SLisandro Dalcin else pc->ops->postsolve = NULL; 3637cdd61b2SBarry Smith PetscFunctionReturn(0); 3647cdd61b2SBarry Smith } 3657cdd61b2SBarry Smith 366f7a08781SBarry Smith static PetscErrorCode PCShellSetView_Shell(PC pc,PetscErrorCode (*view)(PC,PetscViewer)) 3674b9ad928SBarry Smith { 368c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3694b9ad928SBarry Smith 3704b9ad928SBarry Smith PetscFunctionBegin; 3714b9ad928SBarry Smith shell->view = view; 3724b9ad928SBarry Smith PetscFunctionReturn(0); 3734b9ad928SBarry Smith } 3744b9ad928SBarry Smith 375f7a08781SBarry Smith static PetscErrorCode PCShellSetApplyTranspose_Shell(PC pc,PetscErrorCode (*applytranspose)(PC,Vec,Vec)) 3764b9ad928SBarry Smith { 377c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3784b9ad928SBarry Smith 3794b9ad928SBarry Smith PetscFunctionBegin; 3804b9ad928SBarry Smith shell->applytranspose = applytranspose; 381d01c8aa3SLisandro Dalcin if (applytranspose) pc->ops->applytranspose = PCApplyTranspose_Shell; 3820a545947SLisandro Dalcin else pc->ops->applytranspose = NULL; 383d01c8aa3SLisandro Dalcin PetscFunctionReturn(0); 384d01c8aa3SLisandro Dalcin } 385d01c8aa3SLisandro Dalcin 386f7a08781SBarry Smith static PetscErrorCode PCShellSetApplyRichardson_Shell(PC pc,PetscErrorCode (*applyrich)(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool ,PetscInt*,PCRichardsonConvergedReason*)) 387d01c8aa3SLisandro Dalcin { 388c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 389d01c8aa3SLisandro Dalcin 390d01c8aa3SLisandro Dalcin PetscFunctionBegin; 391d01c8aa3SLisandro Dalcin shell->applyrich = applyrich; 392d01c8aa3SLisandro Dalcin if (applyrich) pc->ops->applyrichardson = PCApplyRichardson_Shell; 3930a545947SLisandro Dalcin else pc->ops->applyrichardson = NULL; 3944b9ad928SBarry Smith PetscFunctionReturn(0); 3954b9ad928SBarry Smith } 3964b9ad928SBarry Smith 397f7a08781SBarry Smith static PetscErrorCode PCShellSetName_Shell(PC pc,const char name[]) 3984b9ad928SBarry Smith { 399c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 4004b9ad928SBarry Smith 4014b9ad928SBarry Smith PetscFunctionBegin; 4029566063dSJacob Faibussowitsch PetscCall(PetscFree(shell->name)); 4039566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(name,&shell->name)); 4044b9ad928SBarry Smith PetscFunctionReturn(0); 4054b9ad928SBarry Smith } 4064b9ad928SBarry Smith 407f7a08781SBarry Smith static PetscErrorCode PCShellGetName_Shell(PC pc,const char *name[]) 4084b9ad928SBarry Smith { 409c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 4104b9ad928SBarry Smith 4114b9ad928SBarry Smith PetscFunctionBegin; 4124b9ad928SBarry Smith *name = shell->name; 4134b9ad928SBarry Smith PetscFunctionReturn(0); 4144b9ad928SBarry Smith } 4154b9ad928SBarry Smith 4164b9ad928SBarry Smith /* -------------------------------------------------------------------------------*/ 4174b9ad928SBarry Smith 41818be62a5SSatish Balay /*@C 41918be62a5SSatish Balay PCShellSetDestroy - Sets routine to use to destroy the user-provided 42018be62a5SSatish Balay application context. 42118be62a5SSatish Balay 4223f9fe445SBarry Smith Logically Collective on PC 42318be62a5SSatish Balay 42418be62a5SSatish Balay Input Parameters: 42518be62a5SSatish Balay + pc - the preconditioner context 426a2b725a8SWilliam Gropp - destroy - the application-provided destroy routine 42718be62a5SSatish Balay 42818be62a5SSatish Balay Calling sequence of destroy: 42918be62a5SSatish Balay .vb 4306891c3e4SJed Brown PetscErrorCode destroy (PC) 43118be62a5SSatish Balay .ve 43218be62a5SSatish Balay 43318be62a5SSatish Balay . ptr - the application context 43418be62a5SSatish Balay 43595452b02SPatrick Sanan Notes: 43695452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 4374aa34b0aSBarry Smith 43818be62a5SSatish Balay Level: developer 43918be62a5SSatish Balay 440db781477SPatrick Sanan .seealso: `PCShellSetApply()`, `PCShellSetContext()` 44118be62a5SSatish Balay @*/ 4427087cfbeSBarry Smith PetscErrorCode PCShellSetDestroy(PC pc,PetscErrorCode (*destroy)(PC)) 44318be62a5SSatish Balay { 44418be62a5SSatish Balay PetscFunctionBegin; 4450700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 446cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetDestroy_C",(PC,PetscErrorCode (*)(PC)),(pc,destroy)); 44718be62a5SSatish Balay PetscFunctionReturn(0); 44818be62a5SSatish Balay } 44918be62a5SSatish Balay 4504b9ad928SBarry Smith /*@C 4514b9ad928SBarry Smith PCShellSetSetUp - Sets routine to use to "setup" the preconditioner whenever the 4524b9ad928SBarry Smith matrix operator is changed. 4534b9ad928SBarry Smith 4543f9fe445SBarry Smith Logically Collective on PC 4554b9ad928SBarry Smith 4564b9ad928SBarry Smith Input Parameters: 4574b9ad928SBarry Smith + pc - the preconditioner context 458a2b725a8SWilliam Gropp - setup - the application-provided setup routine 4594b9ad928SBarry Smith 4604b9ad928SBarry Smith Calling sequence of setup: 4614b9ad928SBarry Smith .vb 4626891c3e4SJed Brown PetscErrorCode setup (PC pc) 4634b9ad928SBarry Smith .ve 4644b9ad928SBarry Smith 4656891c3e4SJed Brown . pc - the preconditioner, get the application context with PCShellGetContext() 4664b9ad928SBarry Smith 46795452b02SPatrick Sanan Notes: 46895452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 4694aa34b0aSBarry Smith 4704b9ad928SBarry Smith Level: developer 4714b9ad928SBarry Smith 472db781477SPatrick Sanan .seealso: `PCShellSetApplyRichardson()`, `PCShellSetApply()`, `PCShellSetContext()` 4734b9ad928SBarry Smith @*/ 4747087cfbeSBarry Smith PetscErrorCode PCShellSetSetUp(PC pc,PetscErrorCode (*setup)(PC)) 4754b9ad928SBarry Smith { 4764b9ad928SBarry Smith PetscFunctionBegin; 4770700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 478cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetSetUp_C",(PC,PetscErrorCode (*)(PC)),(pc,setup)); 4794b9ad928SBarry Smith PetscFunctionReturn(0); 4804b9ad928SBarry Smith } 4814b9ad928SBarry Smith 4824b9ad928SBarry Smith /*@C 4834b9ad928SBarry Smith PCShellSetView - Sets routine to use as viewer of shell preconditioner 4844b9ad928SBarry Smith 4853f9fe445SBarry Smith Logically Collective on PC 4864b9ad928SBarry Smith 4874b9ad928SBarry Smith Input Parameters: 4884b9ad928SBarry Smith + pc - the preconditioner context 4894b9ad928SBarry Smith - view - the application-provided view routine 4904b9ad928SBarry Smith 49153a7a830SPatrick Sanan Calling sequence of view: 4924b9ad928SBarry Smith .vb 4936891c3e4SJed Brown PetscErrorCode view(PC pc,PetscViewer v) 4944b9ad928SBarry Smith .ve 4954b9ad928SBarry Smith 4966891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 4974b9ad928SBarry Smith - v - viewer 4984b9ad928SBarry Smith 49995452b02SPatrick Sanan Notes: 50095452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 5014aa34b0aSBarry Smith 5024b9ad928SBarry Smith Level: developer 5034b9ad928SBarry Smith 504db781477SPatrick Sanan .seealso: `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()` 5054b9ad928SBarry Smith @*/ 5067087cfbeSBarry Smith PetscErrorCode PCShellSetView(PC pc,PetscErrorCode (*view)(PC,PetscViewer)) 5074b9ad928SBarry Smith { 5084b9ad928SBarry Smith PetscFunctionBegin; 5090700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 510cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetView_C",(PC,PetscErrorCode (*)(PC,PetscViewer)),(pc,view)); 5114b9ad928SBarry Smith PetscFunctionReturn(0); 5124b9ad928SBarry Smith } 5134b9ad928SBarry Smith 5144b9ad928SBarry Smith /*@C 5154b9ad928SBarry Smith PCShellSetApply - Sets routine to use as preconditioner. 5164b9ad928SBarry Smith 5173f9fe445SBarry Smith Logically Collective on PC 5184b9ad928SBarry Smith 5194b9ad928SBarry Smith Input Parameters: 5204b9ad928SBarry Smith + pc - the preconditioner context 521be29d3c6SBarry Smith - apply - the application-provided preconditioning routine 5224b9ad928SBarry Smith 5234b9ad928SBarry Smith Calling sequence of apply: 5244b9ad928SBarry Smith .vb 5256891c3e4SJed Brown PetscErrorCode apply (PC pc,Vec xin,Vec xout) 5264b9ad928SBarry Smith .ve 5274b9ad928SBarry Smith 5286891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 5294b9ad928SBarry Smith . xin - input vector 5304b9ad928SBarry Smith - xout - output vector 5314b9ad928SBarry Smith 53295452b02SPatrick Sanan Notes: 53395452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 5344aa34b0aSBarry Smith 5354b9ad928SBarry Smith Level: developer 5364b9ad928SBarry Smith 537c2e3fba1SPatrick Sanan .seealso: `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetContext()`, `PCShellSetApplyBA()`, `PCShellSetApplySymmetricRight()`, `PCShellSetApplySymmetricLeft()` 5384b9ad928SBarry Smith @*/ 5397087cfbeSBarry Smith PetscErrorCode PCShellSetApply(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 5404b9ad928SBarry Smith { 5414b9ad928SBarry Smith PetscFunctionBegin; 5420700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 543cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetApply_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,apply)); 5444b9ad928SBarry Smith PetscFunctionReturn(0); 5454b9ad928SBarry Smith } 5464b9ad928SBarry Smith 5471b581b66SBarry Smith /*@C 5486437efc7SEric Chamberland PCShellSetMatApply - Sets routine to use as preconditioner on a block of vectors. 5497b6e2003SPierre Jolivet 5507b6e2003SPierre Jolivet Logically Collective on PC 5517b6e2003SPierre Jolivet 5527b6e2003SPierre Jolivet Input Parameters: 5537b6e2003SPierre Jolivet + pc - the preconditioner context 5547b6e2003SPierre Jolivet - apply - the application-provided preconditioning routine 5557b6e2003SPierre Jolivet 5567b6e2003SPierre Jolivet Calling sequence of apply: 5577b6e2003SPierre Jolivet .vb 5587b6e2003SPierre Jolivet PetscErrorCode apply (PC pc,Mat Xin,Mat Xout) 5597b6e2003SPierre Jolivet .ve 5607b6e2003SPierre Jolivet 5617b6e2003SPierre Jolivet + pc - the preconditioner, get the application context with PCShellGetContext() 5627b6e2003SPierre Jolivet . Xin - input block of vectors 5637b6e2003SPierre Jolivet - Xout - output block of vectors 5647b6e2003SPierre Jolivet 5657b6e2003SPierre Jolivet Notes: 5667b6e2003SPierre Jolivet the function MUST return an error code of 0 on success and nonzero on failure. 5677b6e2003SPierre Jolivet 5687b6e2003SPierre Jolivet Level: developer 5697b6e2003SPierre Jolivet 570db781477SPatrick Sanan .seealso: `PCShellSetApply()` 5717b6e2003SPierre Jolivet @*/ 5727b6e2003SPierre Jolivet PetscErrorCode PCShellSetMatApply(PC pc,PetscErrorCode (*matapply)(PC,Mat,Mat)) 5737b6e2003SPierre Jolivet { 5747b6e2003SPierre Jolivet PetscFunctionBegin; 5757b6e2003SPierre Jolivet PetscValidHeaderSpecific(pc,PC_CLASSID,1); 576cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetMatApply_C",(PC,PetscErrorCode (*)(PC,Mat,Mat)),(pc,matapply)); 5777b6e2003SPierre Jolivet PetscFunctionReturn(0); 5787b6e2003SPierre Jolivet } 5797b6e2003SPierre Jolivet 5807b6e2003SPierre Jolivet /*@C 5811b581b66SBarry Smith PCShellSetApplySymmetricLeft - Sets routine to use as left preconditioner (when the PC_SYMMETRIC is used). 5821b581b66SBarry Smith 5831b581b66SBarry Smith Logically Collective on PC 5841b581b66SBarry Smith 5851b581b66SBarry Smith Input Parameters: 5861b581b66SBarry Smith + pc - the preconditioner context 5871b581b66SBarry Smith - apply - the application-provided left preconditioning routine 5881b581b66SBarry Smith 5891b581b66SBarry Smith Calling sequence of apply: 5901b581b66SBarry Smith .vb 5911b581b66SBarry Smith PetscErrorCode apply (PC pc,Vec xin,Vec xout) 5921b581b66SBarry Smith .ve 5931b581b66SBarry Smith 5941b581b66SBarry Smith + pc - the preconditioner, get the application context with PCShellGetContext() 5951b581b66SBarry Smith . xin - input vector 5961b581b66SBarry Smith - xout - output vector 5971b581b66SBarry Smith 59895452b02SPatrick Sanan Notes: 59995452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 6001b581b66SBarry Smith 6011b581b66SBarry Smith Level: developer 6021b581b66SBarry Smith 603db781477SPatrick Sanan .seealso: `PCShellSetApply()`, `PCShellSetApplySymmetricLeft()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetContext()` 6041b581b66SBarry Smith @*/ 6051b581b66SBarry Smith PetscErrorCode PCShellSetApplySymmetricLeft(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 6061b581b66SBarry Smith { 6071b581b66SBarry Smith PetscFunctionBegin; 6081b581b66SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 609cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetApplySymmetricLeft_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,apply)); 6101b581b66SBarry Smith PetscFunctionReturn(0); 6111b581b66SBarry Smith } 6121b581b66SBarry Smith 6131b581b66SBarry Smith /*@C 614a4c07401SPatrick Sanan PCShellSetApplySymmetricRight - Sets routine to use as right preconditioner (when the PC_SYMMETRIC is used). 6151b581b66SBarry Smith 6161b581b66SBarry Smith Logically Collective on PC 6171b581b66SBarry Smith 6181b581b66SBarry Smith Input Parameters: 6191b581b66SBarry Smith + pc - the preconditioner context 6201b581b66SBarry Smith - apply - the application-provided right preconditioning routine 6211b581b66SBarry Smith 6221b581b66SBarry Smith Calling sequence of apply: 6231b581b66SBarry Smith .vb 6241b581b66SBarry Smith PetscErrorCode apply (PC pc,Vec xin,Vec xout) 6251b581b66SBarry Smith .ve 6261b581b66SBarry Smith 6271b581b66SBarry Smith + pc - the preconditioner, get the application context with PCShellGetContext() 6281b581b66SBarry Smith . xin - input vector 6291b581b66SBarry Smith - xout - output vector 6301b581b66SBarry Smith 63195452b02SPatrick Sanan Notes: 63295452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 6331b581b66SBarry Smith 6341b581b66SBarry Smith Level: developer 6351b581b66SBarry Smith 636db781477SPatrick Sanan .seealso: `PCShellSetApply()`, `PCShellSetApplySymmetricLeft()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetContext()` 6371b581b66SBarry Smith @*/ 6381b581b66SBarry Smith PetscErrorCode PCShellSetApplySymmetricRight(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 6391b581b66SBarry Smith { 6401b581b66SBarry Smith PetscFunctionBegin; 6411b581b66SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 642cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetApplySymmetricRight_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,apply)); 6431b581b66SBarry Smith PetscFunctionReturn(0); 6441b581b66SBarry Smith } 6451b581b66SBarry Smith 6462bb17772SBarry Smith /*@C 6472bb17772SBarry Smith PCShellSetApplyBA - Sets routine to use as preconditioner times operator. 6482bb17772SBarry Smith 6493f9fe445SBarry Smith Logically Collective on PC 6502bb17772SBarry Smith 6512bb17772SBarry Smith Input Parameters: 6522bb17772SBarry Smith + pc - the preconditioner context 6532bb17772SBarry Smith - applyBA - the application-provided BA routine 6542bb17772SBarry Smith 65553a7a830SPatrick Sanan Calling sequence of applyBA: 6562bb17772SBarry Smith .vb 6576891c3e4SJed Brown PetscErrorCode applyBA (PC pc,Vec xin,Vec xout) 6582bb17772SBarry Smith .ve 6592bb17772SBarry Smith 6606891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 6612bb17772SBarry Smith . xin - input vector 6622bb17772SBarry Smith - xout - output vector 6632bb17772SBarry Smith 66495452b02SPatrick Sanan Notes: 66595452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 6664aa34b0aSBarry Smith 6672bb17772SBarry Smith Level: developer 6682bb17772SBarry Smith 669db781477SPatrick Sanan .seealso: `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetContext()`, `PCShellSetApply()` 6702bb17772SBarry Smith @*/ 6717087cfbeSBarry Smith PetscErrorCode PCShellSetApplyBA(PC pc,PetscErrorCode (*applyBA)(PC,PCSide,Vec,Vec,Vec)) 6722bb17772SBarry Smith { 6732bb17772SBarry Smith PetscFunctionBegin; 6740700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 675cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetApplyBA_C",(PC,PetscErrorCode (*)(PC,PCSide,Vec,Vec,Vec)),(pc,applyBA)); 6762bb17772SBarry Smith PetscFunctionReturn(0); 6772bb17772SBarry Smith } 6782bb17772SBarry Smith 6794b9ad928SBarry Smith /*@C 6804b9ad928SBarry Smith PCShellSetApplyTranspose - Sets routine to use as preconditioner transpose. 6814b9ad928SBarry Smith 6823f9fe445SBarry Smith Logically Collective on PC 6834b9ad928SBarry Smith 6844b9ad928SBarry Smith Input Parameters: 6854b9ad928SBarry Smith + pc - the preconditioner context 6864b9ad928SBarry Smith - apply - the application-provided preconditioning transpose routine 6874b9ad928SBarry Smith 6884b9ad928SBarry Smith Calling sequence of apply: 6894b9ad928SBarry Smith .vb 6906891c3e4SJed Brown PetscErrorCode applytranspose (PC pc,Vec xin,Vec xout) 6914b9ad928SBarry Smith .ve 6924b9ad928SBarry Smith 6936891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 6944b9ad928SBarry Smith . xin - input vector 6954b9ad928SBarry Smith - xout - output vector 6964b9ad928SBarry Smith 69795452b02SPatrick Sanan Notes: 69895452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 6994aa34b0aSBarry Smith 7004b9ad928SBarry Smith Level: developer 7014b9ad928SBarry Smith 7024b9ad928SBarry Smith Notes: 7034b9ad928SBarry Smith Uses the same context variable as PCShellSetApply(). 7044b9ad928SBarry Smith 705db781477SPatrick Sanan .seealso: `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApply()`, `PCSetContext()`, `PCShellSetApplyBA()` 7064b9ad928SBarry Smith @*/ 7077087cfbeSBarry Smith PetscErrorCode PCShellSetApplyTranspose(PC pc,PetscErrorCode (*applytranspose)(PC,Vec,Vec)) 7084b9ad928SBarry Smith { 7094b9ad928SBarry Smith PetscFunctionBegin; 7100700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 711cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetApplyTranspose_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,applytranspose)); 7124b9ad928SBarry Smith PetscFunctionReturn(0); 7134b9ad928SBarry Smith } 7144b9ad928SBarry Smith 7157cdd61b2SBarry Smith /*@C 7167cdd61b2SBarry Smith PCShellSetPreSolve - Sets routine to apply to the operators/vectors before a KSPSolve() is 7177cdd61b2SBarry Smith applied. This usually does something like scale the linear system in some application 7187cdd61b2SBarry Smith specific way. 7197cdd61b2SBarry Smith 7203f9fe445SBarry Smith Logically Collective on PC 7217cdd61b2SBarry Smith 7227cdd61b2SBarry Smith Input Parameters: 7237cdd61b2SBarry Smith + pc - the preconditioner context 7247cdd61b2SBarry Smith - presolve - the application-provided presolve routine 7257cdd61b2SBarry Smith 7267cdd61b2SBarry Smith Calling sequence of presolve: 7277cdd61b2SBarry Smith .vb 7286891c3e4SJed Brown PetscErrorCode presolve (PC,KSP ksp,Vec b,Vec x) 7297cdd61b2SBarry Smith .ve 7307cdd61b2SBarry Smith 7316891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 7327cdd61b2SBarry Smith . xin - input vector 7337cdd61b2SBarry Smith - xout - output vector 7347cdd61b2SBarry Smith 73595452b02SPatrick Sanan Notes: 73695452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 7374aa34b0aSBarry Smith 7387cdd61b2SBarry Smith Level: developer 7397cdd61b2SBarry Smith 740db781477SPatrick Sanan .seealso: `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetPostSolve()`, `PCShellSetContext()` 7417cdd61b2SBarry Smith @*/ 7427087cfbeSBarry Smith PetscErrorCode PCShellSetPreSolve(PC pc,PetscErrorCode (*presolve)(PC,KSP,Vec,Vec)) 7437cdd61b2SBarry Smith { 7447cdd61b2SBarry Smith PetscFunctionBegin; 7450700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 746cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetPreSolve_C",(PC,PetscErrorCode (*)(PC,KSP,Vec,Vec)),(pc,presolve)); 7477cdd61b2SBarry Smith PetscFunctionReturn(0); 7487cdd61b2SBarry Smith } 7497cdd61b2SBarry Smith 7507cdd61b2SBarry Smith /*@C 7517cdd61b2SBarry Smith PCShellSetPostSolve - Sets routine to apply to the operators/vectors before a KSPSolve() is 7527cdd61b2SBarry Smith applied. This usually does something like scale the linear system in some application 7537cdd61b2SBarry Smith specific way. 7547cdd61b2SBarry Smith 7553f9fe445SBarry Smith Logically Collective on PC 7567cdd61b2SBarry Smith 7577cdd61b2SBarry Smith Input Parameters: 7587cdd61b2SBarry Smith + pc - the preconditioner context 7597cdd61b2SBarry Smith - postsolve - the application-provided presolve routine 7607cdd61b2SBarry Smith 7617cdd61b2SBarry Smith Calling sequence of postsolve: 7627cdd61b2SBarry Smith .vb 7636891c3e4SJed Brown PetscErrorCode postsolve(PC,KSP ksp,Vec b,Vec x) 7647cdd61b2SBarry Smith .ve 7657cdd61b2SBarry Smith 7666891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 7677cdd61b2SBarry Smith . xin - input vector 7687cdd61b2SBarry Smith - xout - output vector 7697cdd61b2SBarry Smith 77095452b02SPatrick Sanan Notes: 77195452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 7724aa34b0aSBarry Smith 7737cdd61b2SBarry Smith Level: developer 7747cdd61b2SBarry Smith 775db781477SPatrick Sanan .seealso: `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetPreSolve()`, `PCShellSetContext()` 7767cdd61b2SBarry Smith @*/ 7777087cfbeSBarry Smith PetscErrorCode PCShellSetPostSolve(PC pc,PetscErrorCode (*postsolve)(PC,KSP,Vec,Vec)) 7787cdd61b2SBarry Smith { 7797cdd61b2SBarry Smith PetscFunctionBegin; 7800700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 781cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetPostSolve_C",(PC,PetscErrorCode (*)(PC,KSP,Vec,Vec)),(pc,postsolve)); 7827cdd61b2SBarry Smith PetscFunctionReturn(0); 7837cdd61b2SBarry Smith } 7847cdd61b2SBarry Smith 7854b9ad928SBarry Smith /*@C 7864b9ad928SBarry Smith PCShellSetName - Sets an optional name to associate with a shell 7874b9ad928SBarry Smith preconditioner. 7884b9ad928SBarry Smith 7894b9ad928SBarry Smith Not Collective 7904b9ad928SBarry Smith 7914b9ad928SBarry Smith Input Parameters: 7924b9ad928SBarry Smith + pc - the preconditioner context 7934b9ad928SBarry Smith - name - character string describing shell preconditioner 7944b9ad928SBarry Smith 7954b9ad928SBarry Smith Level: developer 7964b9ad928SBarry Smith 797db781477SPatrick Sanan .seealso: `PCShellGetName()` 7984b9ad928SBarry Smith @*/ 7997087cfbeSBarry Smith PetscErrorCode PCShellSetName(PC pc,const char name[]) 8004b9ad928SBarry Smith { 8014b9ad928SBarry Smith PetscFunctionBegin; 8020700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 803cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetName_C",(PC,const char []),(pc,name)); 8044b9ad928SBarry Smith PetscFunctionReturn(0); 8054b9ad928SBarry Smith } 8064b9ad928SBarry Smith 8074b9ad928SBarry Smith /*@C 8084b9ad928SBarry Smith PCShellGetName - Gets an optional name that the user has set for a shell 8094b9ad928SBarry Smith preconditioner. 8104b9ad928SBarry Smith 8114b9ad928SBarry Smith Not Collective 8124b9ad928SBarry Smith 8134b9ad928SBarry Smith Input Parameter: 8144b9ad928SBarry Smith . pc - the preconditioner context 8154b9ad928SBarry Smith 8164b9ad928SBarry Smith Output Parameter: 8174b9ad928SBarry Smith . name - character string describing shell preconditioner (you should not free this) 8184b9ad928SBarry Smith 8194b9ad928SBarry Smith Level: developer 8204b9ad928SBarry Smith 821db781477SPatrick Sanan .seealso: `PCShellSetName()` 8224b9ad928SBarry Smith @*/ 823ccaf0856SBarry Smith PetscErrorCode PCShellGetName(PC pc,const char *name[]) 8244b9ad928SBarry Smith { 8254b9ad928SBarry Smith PetscFunctionBegin; 8260700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 8274482741eSBarry Smith PetscValidPointer(name,2); 828cac4c232SBarry Smith PetscUseMethod(pc,"PCShellGetName_C",(PC,const char*[]),(pc,name)); 8294b9ad928SBarry Smith PetscFunctionReturn(0); 8304b9ad928SBarry Smith } 8314b9ad928SBarry Smith 8324b9ad928SBarry Smith /*@C 8334b9ad928SBarry Smith PCShellSetApplyRichardson - Sets routine to use as preconditioner 8344b9ad928SBarry Smith in Richardson iteration. 8354b9ad928SBarry Smith 8363f9fe445SBarry Smith Logically Collective on PC 8374b9ad928SBarry Smith 8384b9ad928SBarry Smith Input Parameters: 8394b9ad928SBarry Smith + pc - the preconditioner context 840be29d3c6SBarry Smith - apply - the application-provided preconditioning routine 8414b9ad928SBarry Smith 8424b9ad928SBarry Smith Calling sequence of apply: 8434b9ad928SBarry Smith .vb 8446891c3e4SJed Brown PetscErrorCode apply (PC pc,Vec b,Vec x,Vec r,PetscReal rtol,PetscReal abstol,PetscReal dtol,PetscInt maxits) 8454b9ad928SBarry Smith .ve 8464b9ad928SBarry Smith 8476891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 8484b9ad928SBarry Smith . b - right-hand-side 8494b9ad928SBarry Smith . x - current iterate 8504b9ad928SBarry Smith . r - work space 8514b9ad928SBarry Smith . rtol - relative tolerance of residual norm to stop at 85270441072SBarry Smith . abstol - absolute tolerance of residual norm to stop at 8534b9ad928SBarry Smith . dtol - if residual norm increases by this factor than return 8544b9ad928SBarry Smith - maxits - number of iterations to run 8554b9ad928SBarry Smith 85695452b02SPatrick Sanan Notes: 85795452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 8584aa34b0aSBarry Smith 8594b9ad928SBarry Smith Level: developer 8604b9ad928SBarry Smith 861db781477SPatrick Sanan .seealso: `PCShellSetApply()`, `PCShellSetContext()` 8624b9ad928SBarry Smith @*/ 8637087cfbeSBarry Smith PetscErrorCode PCShellSetApplyRichardson(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool,PetscInt*,PCRichardsonConvergedReason*)) 8644b9ad928SBarry Smith { 8654b9ad928SBarry Smith PetscFunctionBegin; 8660700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 867cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetApplyRichardson_C",(PC,PetscErrorCode (*)(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool,PetscInt*,PCRichardsonConvergedReason*)),(pc,apply)); 8684b9ad928SBarry Smith PetscFunctionReturn(0); 8694b9ad928SBarry Smith } 8704b9ad928SBarry Smith 8714b9ad928SBarry Smith /*MC 8724b9ad928SBarry Smith PCSHELL - Creates a new preconditioner class for use with your 8734b9ad928SBarry Smith own private data storage format. 8744b9ad928SBarry Smith 8754b9ad928SBarry Smith Level: advanced 876e0bb08deSStefano Zampini 8774b9ad928SBarry Smith Usage: 8786891c3e4SJed Brown $ extern PetscErrorCode apply(PC,Vec,Vec); 8796891c3e4SJed Brown $ extern PetscErrorCode applyba(PC,PCSide,Vec,Vec,Vec); 8806891c3e4SJed Brown $ extern PetscErrorCode applytranspose(PC,Vec,Vec); 8816891c3e4SJed Brown $ extern PetscErrorCode setup(PC); 8826891c3e4SJed Brown $ extern PetscErrorCode destroy(PC); 8836891c3e4SJed Brown $ 8844b9ad928SBarry Smith $ PCCreate(comm,&pc); 8854b9ad928SBarry Smith $ PCSetType(pc,PCSHELL); 886be29d3c6SBarry Smith $ PCShellSetContext(pc,ctx) 8876891c3e4SJed Brown $ PCShellSetApply(pc,apply); 8886891c3e4SJed Brown $ PCShellSetApplyBA(pc,applyba); (optional) 8896891c3e4SJed Brown $ PCShellSetApplyTranspose(pc,applytranspose); (optional) 8904b9ad928SBarry Smith $ PCShellSetSetUp(pc,setup); (optional) 891d01c8aa3SLisandro Dalcin $ PCShellSetDestroy(pc,destroy); (optional) 8924b9ad928SBarry Smith 893db781477SPatrick Sanan .seealso: `PCCreate()`, `PCSetType()`, `PCType`, `PC`, 894db781477SPatrick Sanan `MATSHELL`, `PCShellSetSetUp()`, `PCShellSetApply()`, `PCShellSetView()`, 895db781477SPatrick Sanan `PCShellSetApplyTranspose()`, `PCShellSetName()`, `PCShellSetApplyRichardson()`, 896db781477SPatrick Sanan `PCShellGetName()`, `PCShellSetContext()`, `PCShellGetContext()`, `PCShellSetApplyBA()` 8974b9ad928SBarry Smith M*/ 8984b9ad928SBarry Smith 8998cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PCCreate_Shell(PC pc) 9004b9ad928SBarry Smith { 9014b9ad928SBarry Smith PC_Shell *shell; 9024b9ad928SBarry Smith 9034b9ad928SBarry Smith PetscFunctionBegin; 9049566063dSJacob Faibussowitsch PetscCall(PetscNewLog(pc,&shell)); 9054b9ad928SBarry Smith pc->data = (void*)shell; 9064b9ad928SBarry Smith 907d01c8aa3SLisandro Dalcin pc->ops->destroy = PCDestroy_Shell; 9084b9ad928SBarry Smith pc->ops->view = PCView_Shell; 909d01c8aa3SLisandro Dalcin pc->ops->apply = PCApply_Shell; 9101b581b66SBarry Smith pc->ops->applysymmetricleft = PCApplySymmetricLeft_Shell; 9111b581b66SBarry Smith pc->ops->applysymmetricright = PCApplySymmetricRight_Shell; 9120e0fe96bSStefano Zampini pc->ops->matapply = NULL; 9130a545947SLisandro Dalcin pc->ops->applytranspose = NULL; 9140a545947SLisandro Dalcin pc->ops->applyrichardson = NULL; 9150a545947SLisandro Dalcin pc->ops->setup = NULL; 9160a545947SLisandro Dalcin pc->ops->presolve = NULL; 9170a545947SLisandro Dalcin pc->ops->postsolve = NULL; 9184b9ad928SBarry Smith 9190a545947SLisandro Dalcin shell->apply = NULL; 9200a545947SLisandro Dalcin shell->applytranspose = NULL; 9210a545947SLisandro Dalcin shell->name = NULL; 9220a545947SLisandro Dalcin shell->applyrich = NULL; 9230a545947SLisandro Dalcin shell->presolve = NULL; 9240a545947SLisandro Dalcin shell->postsolve = NULL; 9250a545947SLisandro Dalcin shell->ctx = NULL; 9260a545947SLisandro Dalcin shell->setup = NULL; 9270a545947SLisandro Dalcin shell->view = NULL; 9280a545947SLisandro Dalcin shell->destroy = NULL; 9290a545947SLisandro Dalcin shell->applysymmetricleft = NULL; 9300a545947SLisandro Dalcin shell->applysymmetricright = NULL; 9314b9ad928SBarry Smith 9329566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetDestroy_C",PCShellSetDestroy_Shell)); 9339566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetSetUp_C",PCShellSetSetUp_Shell)); 9349566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApply_C",PCShellSetApply_Shell)); 9359566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetMatApply_C",PCShellSetMatApply_Shell)); 9369566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplySymmetricLeft_C",PCShellSetApplySymmetricLeft_Shell)); 9379566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplySymmetricRight_C",PCShellSetApplySymmetricRight_Shell)); 9389566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyBA_C",PCShellSetApplyBA_Shell)); 9399566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetPreSolve_C",PCShellSetPreSolve_Shell)); 9409566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetPostSolve_C",PCShellSetPostSolve_Shell)); 9419566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetView_C",PCShellSetView_Shell)); 9429566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyTranspose_C",PCShellSetApplyTranspose_Shell)); 9439566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetName_C",PCShellSetName_Shell)); 9449566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellGetName_C",PCShellGetName_Shell)); 9459566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyRichardson_C",PCShellSetApplyRichardson_Shell)); 9464b9ad928SBarry Smith PetscFunctionReturn(0); 9474b9ad928SBarry Smith } 948