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 48c5ae4b9aSBarry Smith .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 78c5ae4b9aSBarry Smith .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"); 989566063dSJacob Faibussowitsch PetscStackCall("PCSHELL user function setup()",PetscCall((*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)); 1109566063dSJacob Faibussowitsch PetscStackCall("PCSHELL user function apply()",PetscCall((*shell->apply)(pc,x,y))); 1119566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)y, &outstate)); 112e3f487b0SBarry Smith if (instate == outstate) { 113e3f487b0SBarry Smith /* increase the state of the output vector since the user did not update its state themselve as should have been done */ 1149566063dSJacob Faibussowitsch PetscCall(PetscObjectStateIncrease((PetscObject)y)); 115e3f487b0SBarry Smith } 1164b9ad928SBarry Smith PetscFunctionReturn(0); 1174b9ad928SBarry Smith } 1184b9ad928SBarry Smith 1197b6e2003SPierre Jolivet static PetscErrorCode PCMatApply_Shell(PC pc,Mat X,Mat Y) 1207b6e2003SPierre Jolivet { 1217b6e2003SPierre Jolivet PC_Shell *shell = (PC_Shell*)pc->data; 1227b6e2003SPierre Jolivet PetscObjectState instate,outstate; 1237b6e2003SPierre Jolivet 1247b6e2003SPierre Jolivet PetscFunctionBegin; 12528b400f6SJacob Faibussowitsch PetscCheck(shell->matapply,PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No apply() routine provided to Shell PC"); 1269566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)Y, &instate)); 1279566063dSJacob Faibussowitsch PetscStackCall("PCSHELL user function apply()",PetscCall((*shell->matapply)(pc,X,Y))); 1289566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)Y, &outstate)); 1297b6e2003SPierre Jolivet if (instate == outstate) { 1307b6e2003SPierre Jolivet /* increase the state of the output vector since the user did not update its state themselve as should have been done */ 1319566063dSJacob Faibussowitsch PetscCall(PetscObjectStateIncrease((PetscObject)Y)); 1327b6e2003SPierre Jolivet } 1337b6e2003SPierre Jolivet PetscFunctionReturn(0); 1347b6e2003SPierre Jolivet } 1357b6e2003SPierre Jolivet 1361b581b66SBarry Smith static PetscErrorCode PCApplySymmetricLeft_Shell(PC pc,Vec x,Vec y) 1371b581b66SBarry Smith { 1381b581b66SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 1391b581b66SBarry Smith 1401b581b66SBarry Smith PetscFunctionBegin; 14128b400f6SJacob Faibussowitsch PetscCheck(shell->applysymmetricleft,PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No apply() routine provided to Shell PC"); 1429566063dSJacob Faibussowitsch PetscStackCall("PCSHELL user function apply()",PetscCall((*shell->applysymmetricleft)(pc,x,y))); 1431b581b66SBarry Smith PetscFunctionReturn(0); 1441b581b66SBarry Smith } 1451b581b66SBarry Smith 1461b581b66SBarry Smith static PetscErrorCode PCApplySymmetricRight_Shell(PC pc,Vec x,Vec y) 1471b581b66SBarry Smith { 1481b581b66SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 1491b581b66SBarry Smith 1501b581b66SBarry Smith PetscFunctionBegin; 15128b400f6SJacob Faibussowitsch PetscCheck(shell->applysymmetricright,PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No apply() routine provided to Shell PC"); 1529566063dSJacob Faibussowitsch PetscStackCall("PCSHELL user function apply()",PetscCall((*shell->applysymmetricright)(pc,x,y))); 1531b581b66SBarry Smith PetscFunctionReturn(0); 1541b581b66SBarry Smith } 1551b581b66SBarry Smith 1562bb17772SBarry Smith static PetscErrorCode PCApplyBA_Shell(PC pc,PCSide side,Vec x,Vec y,Vec w) 1572bb17772SBarry Smith { 158c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 159e3f487b0SBarry Smith PetscObjectState instate,outstate; 1602bb17772SBarry Smith 1612bb17772SBarry Smith PetscFunctionBegin; 16228b400f6SJacob Faibussowitsch PetscCheck(shell->applyBA,PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No applyBA() routine provided to Shell PC"); 1639566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)w, &instate)); 1649566063dSJacob Faibussowitsch PetscStackCall("PCSHELL user function applyBA()",PetscCall((*shell->applyBA)(pc,side,x,y,w))); 1659566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)w, &outstate)); 166e3f487b0SBarry Smith if (instate == outstate) { 167e3f487b0SBarry Smith /* increase the state of the output vector since the user did not update its state themselve as should have been done */ 1689566063dSJacob Faibussowitsch PetscCall(PetscObjectStateIncrease((PetscObject)w)); 169e3f487b0SBarry Smith } 1702bb17772SBarry Smith PetscFunctionReturn(0); 1712bb17772SBarry Smith } 1722bb17772SBarry Smith 173a06fd7f2SStefano Zampini static PetscErrorCode PCPreSolveChangeRHS_Shell(PC pc,PetscBool* change) 174a06fd7f2SStefano Zampini { 175a06fd7f2SStefano Zampini PetscFunctionBegin; 176a06fd7f2SStefano Zampini *change = PETSC_TRUE; 177a06fd7f2SStefano Zampini PetscFunctionReturn(0); 178a06fd7f2SStefano Zampini } 179a06fd7f2SStefano Zampini 1807cdd61b2SBarry Smith static PetscErrorCode PCPreSolve_Shell(PC pc,KSP ksp,Vec b,Vec x) 1817cdd61b2SBarry Smith { 182c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 1837cdd61b2SBarry Smith 1847cdd61b2SBarry Smith PetscFunctionBegin; 18528b400f6SJacob Faibussowitsch PetscCheck(shell->presolve,PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No presolve() routine provided to Shell PC"); 1869566063dSJacob Faibussowitsch PetscStackCall("PCSHELL user function presolve()",PetscCall((*shell->presolve)(pc,ksp,b,x))); 1877cdd61b2SBarry Smith PetscFunctionReturn(0); 1887cdd61b2SBarry Smith } 1897cdd61b2SBarry Smith 1907cdd61b2SBarry Smith static PetscErrorCode PCPostSolve_Shell(PC pc,KSP ksp,Vec b,Vec x) 1917cdd61b2SBarry Smith { 192c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 1937cdd61b2SBarry Smith 1947cdd61b2SBarry Smith PetscFunctionBegin; 19528b400f6SJacob Faibussowitsch PetscCheck(shell->postsolve,PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No postsolve() routine provided to Shell PC"); 1969566063dSJacob Faibussowitsch PetscStackCall("PCSHELL user function postsolve()",PetscCall((*shell->postsolve)(pc,ksp,b,x))); 1977cdd61b2SBarry Smith PetscFunctionReturn(0); 1987cdd61b2SBarry Smith } 1997cdd61b2SBarry Smith 2006849ba73SBarry Smith static PetscErrorCode PCApplyTranspose_Shell(PC pc,Vec x,Vec y) 2014b9ad928SBarry Smith { 202c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 203e3f487b0SBarry Smith PetscObjectState instate,outstate; 2044b9ad928SBarry Smith 2054b9ad928SBarry Smith PetscFunctionBegin; 20628b400f6SJacob Faibussowitsch PetscCheck(shell->applytranspose,PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No applytranspose() routine provided to Shell PC"); 2079566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)y, &instate)); 2089566063dSJacob Faibussowitsch PetscStackCall("PCSHELL user function applytranspose()",PetscCall((*shell->applytranspose)(pc,x,y))); 2099566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)y, &outstate)); 210e3f487b0SBarry Smith if (instate == outstate) { 211e3f487b0SBarry Smith /* increase the state of the output vector since the user did not update its state themself as should have been done */ 2129566063dSJacob Faibussowitsch PetscCall(PetscObjectStateIncrease((PetscObject)y)); 213e3f487b0SBarry Smith } 2144b9ad928SBarry Smith PetscFunctionReturn(0); 2154b9ad928SBarry Smith } 2164b9ad928SBarry Smith 217ace3abfcSBarry 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) 2184b9ad928SBarry Smith { 219c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 220e3f487b0SBarry Smith PetscObjectState instate,outstate; 2214b9ad928SBarry Smith 2224b9ad928SBarry Smith PetscFunctionBegin; 22328b400f6SJacob Faibussowitsch PetscCheck(shell->applyrich,PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No applyrichardson() routine provided to Shell PC"); 2249566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)y, &instate)); 2259566063dSJacob Faibussowitsch PetscStackCall("PCSHELL user function applyrichardson()",PetscCall((*shell->applyrich)(pc,x,y,w,rtol,abstol,dtol,it,guesszero,outits,reason))); 2269566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)y, &outstate)); 227e3f487b0SBarry Smith if (instate == outstate) { 228e3f487b0SBarry Smith /* increase the state of the output vector since the user did not update its state themself as should have been done */ 2299566063dSJacob Faibussowitsch PetscCall(PetscObjectStateIncrease((PetscObject)y)); 230e3f487b0SBarry Smith } 2314b9ad928SBarry Smith PetscFunctionReturn(0); 2324b9ad928SBarry Smith } 2334b9ad928SBarry Smith 2346849ba73SBarry Smith static PetscErrorCode PCDestroy_Shell(PC pc) 2354b9ad928SBarry Smith { 2364b9ad928SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 2374b9ad928SBarry Smith 2384b9ad928SBarry Smith PetscFunctionBegin; 2399566063dSJacob Faibussowitsch PetscCall(PetscFree(shell->name)); 2409566063dSJacob Faibussowitsch if (shell->destroy) PetscStackCall("PCSHELL user function destroy()",PetscCall((*shell->destroy)(pc))); 2419566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetDestroy_C",NULL)); 2429566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetSetUp_C",NULL)); 2439566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApply_C",NULL)); 2449566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetMatApply_C",NULL)); 2459566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplySymmetricLeft_C",NULL)); 2469566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplySymmetricRight_C",NULL)); 2479566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyBA_C",NULL)); 2489566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetPreSolve_C",NULL)); 2499566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetPostSolve_C",NULL)); 2509566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetView_C",NULL)); 2519566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyTranspose_C",NULL)); 2529566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetName_C",NULL)); 2539566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellGetName_C",NULL)); 2549566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyRichardson_C",NULL)); 2559566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCPreSolveChangeRHS_C",NULL)); 2569566063dSJacob Faibussowitsch PetscCall(PetscFree(pc->data)); 2574b9ad928SBarry Smith PetscFunctionReturn(0); 2584b9ad928SBarry Smith } 2594b9ad928SBarry Smith 2606849ba73SBarry Smith static PetscErrorCode PCView_Shell(PC pc,PetscViewer viewer) 2614b9ad928SBarry Smith { 2624b9ad928SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 263ace3abfcSBarry Smith PetscBool iascii; 2644b9ad928SBarry Smith 2654b9ad928SBarry Smith PetscFunctionBegin; 2669566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii)); 26732077d6dSBarry Smith if (iascii) { 2682fa5cd67SKarl Rupp if (shell->name) { 2699566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," %s\n",shell->name)); 2702fa5cd67SKarl Rupp } else { 2719566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," no name\n")); 2722fa5cd67SKarl Rupp } 2734b9ad928SBarry Smith } 2744b9ad928SBarry Smith if (shell->view) { 2759566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 2769566063dSJacob Faibussowitsch PetscCall((*shell->view)(pc,viewer)); 2779566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 2784b9ad928SBarry Smith } 2794b9ad928SBarry Smith PetscFunctionReturn(0); 2804b9ad928SBarry Smith } 2814b9ad928SBarry Smith 2824b9ad928SBarry Smith /* ------------------------------------------------------------------------------*/ 283f7a08781SBarry Smith static PetscErrorCode PCShellSetDestroy_Shell(PC pc, PetscErrorCode (*destroy)(PC)) 28418be62a5SSatish Balay { 285c5ae4b9aSBarry Smith PC_Shell *shell= (PC_Shell*)pc->data; 28618be62a5SSatish Balay 28718be62a5SSatish Balay PetscFunctionBegin; 28818be62a5SSatish Balay shell->destroy = destroy; 28918be62a5SSatish Balay PetscFunctionReturn(0); 29018be62a5SSatish Balay } 29118be62a5SSatish Balay 292f7a08781SBarry Smith static PetscErrorCode PCShellSetSetUp_Shell(PC pc, PetscErrorCode (*setup)(PC)) 2934b9ad928SBarry Smith { 294feb237baSPierre Jolivet PC_Shell *shell = (PC_Shell*)pc->data; 2954b9ad928SBarry Smith 2964b9ad928SBarry Smith PetscFunctionBegin; 2974b9ad928SBarry Smith shell->setup = setup; 298d01c8aa3SLisandro Dalcin if (setup) pc->ops->setup = PCSetUp_Shell; 2990a545947SLisandro Dalcin else pc->ops->setup = NULL; 3004b9ad928SBarry Smith PetscFunctionReturn(0); 3014b9ad928SBarry Smith } 3024b9ad928SBarry Smith 303f7a08781SBarry Smith static PetscErrorCode PCShellSetApply_Shell(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 3044b9ad928SBarry Smith { 305c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3064b9ad928SBarry Smith 3074b9ad928SBarry Smith PetscFunctionBegin; 3084b9ad928SBarry Smith shell->apply = apply; 3094b9ad928SBarry Smith PetscFunctionReturn(0); 3104b9ad928SBarry Smith } 3114b9ad928SBarry Smith 3127b6e2003SPierre Jolivet static PetscErrorCode PCShellSetMatApply_Shell(PC pc,PetscErrorCode (*matapply)(PC,Mat,Mat)) 3137b6e2003SPierre Jolivet { 3147b6e2003SPierre Jolivet PC_Shell *shell = (PC_Shell*)pc->data; 3157b6e2003SPierre Jolivet 3167b6e2003SPierre Jolivet PetscFunctionBegin; 3177b6e2003SPierre Jolivet shell->matapply = matapply; 3187b6e2003SPierre Jolivet PetscFunctionReturn(0); 3197b6e2003SPierre Jolivet } 3207b6e2003SPierre Jolivet 3211b581b66SBarry Smith static PetscErrorCode PCShellSetApplySymmetricLeft_Shell(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 3221b581b66SBarry Smith { 3231b581b66SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3241b581b66SBarry Smith 3251b581b66SBarry Smith PetscFunctionBegin; 3261b581b66SBarry Smith shell->applysymmetricleft = apply; 3271b581b66SBarry Smith PetscFunctionReturn(0); 3281b581b66SBarry Smith } 3291b581b66SBarry Smith 3301b581b66SBarry Smith static PetscErrorCode PCShellSetApplySymmetricRight_Shell(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 3311b581b66SBarry Smith { 3321b581b66SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3331b581b66SBarry Smith 3341b581b66SBarry Smith PetscFunctionBegin; 3351b581b66SBarry Smith shell->applysymmetricright = apply; 3361b581b66SBarry Smith PetscFunctionReturn(0); 3371b581b66SBarry Smith } 3381b581b66SBarry Smith 339f7a08781SBarry Smith static PetscErrorCode PCShellSetApplyBA_Shell(PC pc,PetscErrorCode (*applyBA)(PC,PCSide,Vec,Vec,Vec)) 3402bb17772SBarry Smith { 341c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3422bb17772SBarry Smith 3432bb17772SBarry Smith PetscFunctionBegin; 344d01c8aa3SLisandro Dalcin shell->applyBA = applyBA; 345d01c8aa3SLisandro Dalcin if (applyBA) pc->ops->applyBA = PCApplyBA_Shell; 3460a545947SLisandro Dalcin else pc->ops->applyBA = NULL; 3472bb17772SBarry Smith PetscFunctionReturn(0); 3482bb17772SBarry Smith } 3492bb17772SBarry Smith 350f7a08781SBarry Smith static PetscErrorCode PCShellSetPreSolve_Shell(PC pc,PetscErrorCode (*presolve)(PC,KSP,Vec,Vec)) 3517cdd61b2SBarry Smith { 352c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3537cdd61b2SBarry Smith 3547cdd61b2SBarry Smith PetscFunctionBegin; 3557cdd61b2SBarry Smith shell->presolve = presolve; 356a06fd7f2SStefano Zampini if (presolve) { 357a06fd7f2SStefano Zampini pc->ops->presolve = PCPreSolve_Shell; 3589566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCPreSolveChangeRHS_C",PCPreSolveChangeRHS_Shell)); 359a06fd7f2SStefano Zampini } else { 3600a545947SLisandro Dalcin pc->ops->presolve = NULL; 3619566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCPreSolveChangeRHS_C",NULL)); 362a06fd7f2SStefano Zampini } 3637cdd61b2SBarry Smith PetscFunctionReturn(0); 3647cdd61b2SBarry Smith } 3657cdd61b2SBarry Smith 366f7a08781SBarry Smith static PetscErrorCode PCShellSetPostSolve_Shell(PC pc,PetscErrorCode (*postsolve)(PC,KSP,Vec,Vec)) 3677cdd61b2SBarry Smith { 368c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3697cdd61b2SBarry Smith 3707cdd61b2SBarry Smith PetscFunctionBegin; 3717cdd61b2SBarry Smith shell->postsolve = postsolve; 372d01c8aa3SLisandro Dalcin if (postsolve) pc->ops->postsolve = PCPostSolve_Shell; 3730a545947SLisandro Dalcin else pc->ops->postsolve = NULL; 3747cdd61b2SBarry Smith PetscFunctionReturn(0); 3757cdd61b2SBarry Smith } 3767cdd61b2SBarry Smith 377f7a08781SBarry Smith static PetscErrorCode PCShellSetView_Shell(PC pc,PetscErrorCode (*view)(PC,PetscViewer)) 3784b9ad928SBarry Smith { 379c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3804b9ad928SBarry Smith 3814b9ad928SBarry Smith PetscFunctionBegin; 3824b9ad928SBarry Smith shell->view = view; 3834b9ad928SBarry Smith PetscFunctionReturn(0); 3844b9ad928SBarry Smith } 3854b9ad928SBarry Smith 386f7a08781SBarry Smith static PetscErrorCode PCShellSetApplyTranspose_Shell(PC pc,PetscErrorCode (*applytranspose)(PC,Vec,Vec)) 3874b9ad928SBarry Smith { 388c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 3894b9ad928SBarry Smith 3904b9ad928SBarry Smith PetscFunctionBegin; 3914b9ad928SBarry Smith shell->applytranspose = applytranspose; 392d01c8aa3SLisandro Dalcin if (applytranspose) pc->ops->applytranspose = PCApplyTranspose_Shell; 3930a545947SLisandro Dalcin else pc->ops->applytranspose = NULL; 394d01c8aa3SLisandro Dalcin PetscFunctionReturn(0); 395d01c8aa3SLisandro Dalcin } 396d01c8aa3SLisandro Dalcin 397f7a08781SBarry Smith static PetscErrorCode PCShellSetApplyRichardson_Shell(PC pc,PetscErrorCode (*applyrich)(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool ,PetscInt*,PCRichardsonConvergedReason*)) 398d01c8aa3SLisandro Dalcin { 399c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 400d01c8aa3SLisandro Dalcin 401d01c8aa3SLisandro Dalcin PetscFunctionBegin; 402d01c8aa3SLisandro Dalcin shell->applyrich = applyrich; 403d01c8aa3SLisandro Dalcin if (applyrich) pc->ops->applyrichardson = PCApplyRichardson_Shell; 4040a545947SLisandro Dalcin else pc->ops->applyrichardson = NULL; 4054b9ad928SBarry Smith PetscFunctionReturn(0); 4064b9ad928SBarry Smith } 4074b9ad928SBarry Smith 408f7a08781SBarry Smith static PetscErrorCode PCShellSetName_Shell(PC pc,const char name[]) 4094b9ad928SBarry Smith { 410c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 4114b9ad928SBarry Smith 4124b9ad928SBarry Smith PetscFunctionBegin; 4139566063dSJacob Faibussowitsch PetscCall(PetscFree(shell->name)); 4149566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(name,&shell->name)); 4154b9ad928SBarry Smith PetscFunctionReturn(0); 4164b9ad928SBarry Smith } 4174b9ad928SBarry Smith 418f7a08781SBarry Smith static PetscErrorCode PCShellGetName_Shell(PC pc,const char *name[]) 4194b9ad928SBarry Smith { 420c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 4214b9ad928SBarry Smith 4224b9ad928SBarry Smith PetscFunctionBegin; 4234b9ad928SBarry Smith *name = shell->name; 4244b9ad928SBarry Smith PetscFunctionReturn(0); 4254b9ad928SBarry Smith } 4264b9ad928SBarry Smith 4274b9ad928SBarry Smith /* -------------------------------------------------------------------------------*/ 4284b9ad928SBarry Smith 42918be62a5SSatish Balay /*@C 43018be62a5SSatish Balay PCShellSetDestroy - Sets routine to use to destroy the user-provided 43118be62a5SSatish Balay application context. 43218be62a5SSatish Balay 4333f9fe445SBarry Smith Logically Collective on PC 43418be62a5SSatish Balay 43518be62a5SSatish Balay Input Parameters: 43618be62a5SSatish Balay + pc - the preconditioner context 437a2b725a8SWilliam Gropp - destroy - the application-provided destroy routine 43818be62a5SSatish Balay 43918be62a5SSatish Balay Calling sequence of destroy: 44018be62a5SSatish Balay .vb 4416891c3e4SJed Brown PetscErrorCode destroy (PC) 44218be62a5SSatish Balay .ve 44318be62a5SSatish Balay 44418be62a5SSatish Balay . ptr - the application context 44518be62a5SSatish Balay 44695452b02SPatrick Sanan Notes: 44795452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 4484aa34b0aSBarry Smith 44918be62a5SSatish Balay Level: developer 45018be62a5SSatish Balay 45118be62a5SSatish Balay .seealso: PCShellSetApply(), PCShellSetContext() 45218be62a5SSatish Balay @*/ 4537087cfbeSBarry Smith PetscErrorCode PCShellSetDestroy(PC pc,PetscErrorCode (*destroy)(PC)) 45418be62a5SSatish Balay { 45518be62a5SSatish Balay PetscFunctionBegin; 4560700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 457*cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetDestroy_C",(PC,PetscErrorCode (*)(PC)),(pc,destroy)); 45818be62a5SSatish Balay PetscFunctionReturn(0); 45918be62a5SSatish Balay } 46018be62a5SSatish Balay 4614b9ad928SBarry Smith /*@C 4624b9ad928SBarry Smith PCShellSetSetUp - Sets routine to use to "setup" the preconditioner whenever the 4634b9ad928SBarry Smith matrix operator is changed. 4644b9ad928SBarry Smith 4653f9fe445SBarry Smith Logically Collective on PC 4664b9ad928SBarry Smith 4674b9ad928SBarry Smith Input Parameters: 4684b9ad928SBarry Smith + pc - the preconditioner context 469a2b725a8SWilliam Gropp - setup - the application-provided setup routine 4704b9ad928SBarry Smith 4714b9ad928SBarry Smith Calling sequence of setup: 4724b9ad928SBarry Smith .vb 4736891c3e4SJed Brown PetscErrorCode setup (PC pc) 4744b9ad928SBarry Smith .ve 4754b9ad928SBarry Smith 4766891c3e4SJed Brown . pc - the preconditioner, get the application context with PCShellGetContext() 4774b9ad928SBarry Smith 47895452b02SPatrick Sanan Notes: 47995452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 4804aa34b0aSBarry Smith 4814b9ad928SBarry Smith Level: developer 4824b9ad928SBarry Smith 483be29d3c6SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetApply(), PCShellSetContext() 4844b9ad928SBarry Smith @*/ 4857087cfbeSBarry Smith PetscErrorCode PCShellSetSetUp(PC pc,PetscErrorCode (*setup)(PC)) 4864b9ad928SBarry Smith { 4874b9ad928SBarry Smith PetscFunctionBegin; 4880700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 489*cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetSetUp_C",(PC,PetscErrorCode (*)(PC)),(pc,setup)); 4904b9ad928SBarry Smith PetscFunctionReturn(0); 4914b9ad928SBarry Smith } 4924b9ad928SBarry Smith 4934b9ad928SBarry Smith /*@C 4944b9ad928SBarry Smith PCShellSetView - Sets routine to use as viewer of shell preconditioner 4954b9ad928SBarry Smith 4963f9fe445SBarry Smith Logically Collective on PC 4974b9ad928SBarry Smith 4984b9ad928SBarry Smith Input Parameters: 4994b9ad928SBarry Smith + pc - the preconditioner context 5004b9ad928SBarry Smith - view - the application-provided view routine 5014b9ad928SBarry Smith 50253a7a830SPatrick Sanan Calling sequence of view: 5034b9ad928SBarry Smith .vb 5046891c3e4SJed Brown PetscErrorCode view(PC pc,PetscViewer v) 5054b9ad928SBarry Smith .ve 5064b9ad928SBarry Smith 5076891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 5084b9ad928SBarry Smith - v - viewer 5094b9ad928SBarry Smith 51095452b02SPatrick Sanan Notes: 51195452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 5124aa34b0aSBarry Smith 5134b9ad928SBarry Smith Level: developer 5144b9ad928SBarry Smith 5154b9ad928SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose() 5164b9ad928SBarry Smith @*/ 5177087cfbeSBarry Smith PetscErrorCode PCShellSetView(PC pc,PetscErrorCode (*view)(PC,PetscViewer)) 5184b9ad928SBarry Smith { 5194b9ad928SBarry Smith PetscFunctionBegin; 5200700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 521*cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetView_C",(PC,PetscErrorCode (*)(PC,PetscViewer)),(pc,view)); 5224b9ad928SBarry Smith PetscFunctionReturn(0); 5234b9ad928SBarry Smith } 5244b9ad928SBarry Smith 5254b9ad928SBarry Smith /*@C 5264b9ad928SBarry Smith PCShellSetApply - Sets routine to use as preconditioner. 5274b9ad928SBarry Smith 5283f9fe445SBarry Smith Logically Collective on PC 5294b9ad928SBarry Smith 5304b9ad928SBarry Smith Input Parameters: 5314b9ad928SBarry Smith + pc - the preconditioner context 532be29d3c6SBarry Smith - apply - the application-provided preconditioning routine 5334b9ad928SBarry Smith 5344b9ad928SBarry Smith Calling sequence of apply: 5354b9ad928SBarry Smith .vb 5366891c3e4SJed Brown PetscErrorCode apply (PC pc,Vec xin,Vec xout) 5374b9ad928SBarry Smith .ve 5384b9ad928SBarry Smith 5396891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 5404b9ad928SBarry Smith . xin - input vector 5414b9ad928SBarry Smith - xout - output vector 5424b9ad928SBarry Smith 54395452b02SPatrick Sanan Notes: 54495452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 5454aa34b0aSBarry Smith 5464b9ad928SBarry Smith Level: developer 5474b9ad928SBarry Smith 548a4c07401SPatrick Sanan .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetContext(), PCShellSetApplyBA(), PCShellSetApplySymmetricRight(),PCShellSetApplySymmetricLeft() 5494b9ad928SBarry Smith @*/ 5507087cfbeSBarry Smith PetscErrorCode PCShellSetApply(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 5514b9ad928SBarry Smith { 5524b9ad928SBarry Smith PetscFunctionBegin; 5530700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 554*cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetApply_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,apply)); 5554b9ad928SBarry Smith PetscFunctionReturn(0); 5564b9ad928SBarry Smith } 5574b9ad928SBarry Smith 5581b581b66SBarry Smith /*@C 5596437efc7SEric Chamberland PCShellSetMatApply - Sets routine to use as preconditioner on a block of vectors. 5607b6e2003SPierre Jolivet 5617b6e2003SPierre Jolivet Logically Collective on PC 5627b6e2003SPierre Jolivet 5637b6e2003SPierre Jolivet Input Parameters: 5647b6e2003SPierre Jolivet + pc - the preconditioner context 5657b6e2003SPierre Jolivet - apply - the application-provided preconditioning routine 5667b6e2003SPierre Jolivet 5677b6e2003SPierre Jolivet Calling sequence of apply: 5687b6e2003SPierre Jolivet .vb 5697b6e2003SPierre Jolivet PetscErrorCode apply (PC pc,Mat Xin,Mat Xout) 5707b6e2003SPierre Jolivet .ve 5717b6e2003SPierre Jolivet 5727b6e2003SPierre Jolivet + pc - the preconditioner, get the application context with PCShellGetContext() 5737b6e2003SPierre Jolivet . Xin - input block of vectors 5747b6e2003SPierre Jolivet - Xout - output block of vectors 5757b6e2003SPierre Jolivet 5767b6e2003SPierre Jolivet Notes: 5777b6e2003SPierre Jolivet the function MUST return an error code of 0 on success and nonzero on failure. 5787b6e2003SPierre Jolivet 5797b6e2003SPierre Jolivet Level: developer 5807b6e2003SPierre Jolivet 5817b6e2003SPierre Jolivet .seealso: PCShellSetApply() 5827b6e2003SPierre Jolivet @*/ 5837b6e2003SPierre Jolivet PetscErrorCode PCShellSetMatApply(PC pc,PetscErrorCode (*matapply)(PC,Mat,Mat)) 5847b6e2003SPierre Jolivet { 5857b6e2003SPierre Jolivet PetscFunctionBegin; 5867b6e2003SPierre Jolivet PetscValidHeaderSpecific(pc,PC_CLASSID,1); 587*cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetMatApply_C",(PC,PetscErrorCode (*)(PC,Mat,Mat)),(pc,matapply)); 5887b6e2003SPierre Jolivet PetscFunctionReturn(0); 5897b6e2003SPierre Jolivet } 5907b6e2003SPierre Jolivet 5917b6e2003SPierre Jolivet /*@C 5921b581b66SBarry Smith PCShellSetApplySymmetricLeft - Sets routine to use as left preconditioner (when the PC_SYMMETRIC is used). 5931b581b66SBarry Smith 5941b581b66SBarry Smith Logically Collective on PC 5951b581b66SBarry Smith 5961b581b66SBarry Smith Input Parameters: 5971b581b66SBarry Smith + pc - the preconditioner context 5981b581b66SBarry Smith - apply - the application-provided left preconditioning routine 5991b581b66SBarry Smith 6001b581b66SBarry Smith Calling sequence of apply: 6011b581b66SBarry Smith .vb 6021b581b66SBarry Smith PetscErrorCode apply (PC pc,Vec xin,Vec xout) 6031b581b66SBarry Smith .ve 6041b581b66SBarry Smith 6051b581b66SBarry Smith + pc - the preconditioner, get the application context with PCShellGetContext() 6061b581b66SBarry Smith . xin - input vector 6071b581b66SBarry Smith - xout - output vector 6081b581b66SBarry Smith 60995452b02SPatrick Sanan Notes: 61095452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 6111b581b66SBarry Smith 6121b581b66SBarry Smith Level: developer 6131b581b66SBarry Smith 6141b581b66SBarry Smith .seealso: PCShellSetApply(), PCShellSetApplySymmetricLeft(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetContext() 6151b581b66SBarry Smith @*/ 6161b581b66SBarry Smith PetscErrorCode PCShellSetApplySymmetricLeft(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 6171b581b66SBarry Smith { 6181b581b66SBarry Smith PetscFunctionBegin; 6191b581b66SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 620*cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetApplySymmetricLeft_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,apply)); 6211b581b66SBarry Smith PetscFunctionReturn(0); 6221b581b66SBarry Smith } 6231b581b66SBarry Smith 6241b581b66SBarry Smith /*@C 625a4c07401SPatrick Sanan PCShellSetApplySymmetricRight - Sets routine to use as right preconditioner (when the PC_SYMMETRIC is used). 6261b581b66SBarry Smith 6271b581b66SBarry Smith Logically Collective on PC 6281b581b66SBarry Smith 6291b581b66SBarry Smith Input Parameters: 6301b581b66SBarry Smith + pc - the preconditioner context 6311b581b66SBarry Smith - apply - the application-provided right preconditioning routine 6321b581b66SBarry Smith 6331b581b66SBarry Smith Calling sequence of apply: 6341b581b66SBarry Smith .vb 6351b581b66SBarry Smith PetscErrorCode apply (PC pc,Vec xin,Vec xout) 6361b581b66SBarry Smith .ve 6371b581b66SBarry Smith 6381b581b66SBarry Smith + pc - the preconditioner, get the application context with PCShellGetContext() 6391b581b66SBarry Smith . xin - input vector 6401b581b66SBarry Smith - xout - output vector 6411b581b66SBarry Smith 64295452b02SPatrick Sanan Notes: 64395452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 6441b581b66SBarry Smith 6451b581b66SBarry Smith Level: developer 6461b581b66SBarry Smith 6471b581b66SBarry Smith .seealso: PCShellSetApply(), PCShellSetApplySymmetricLeft(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetContext() 6481b581b66SBarry Smith @*/ 6491b581b66SBarry Smith PetscErrorCode PCShellSetApplySymmetricRight(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec)) 6501b581b66SBarry Smith { 6511b581b66SBarry Smith PetscFunctionBegin; 6521b581b66SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 653*cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetApplySymmetricRight_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,apply)); 6541b581b66SBarry Smith PetscFunctionReturn(0); 6551b581b66SBarry Smith } 6561b581b66SBarry Smith 6572bb17772SBarry Smith /*@C 6582bb17772SBarry Smith PCShellSetApplyBA - Sets routine to use as preconditioner times operator. 6592bb17772SBarry Smith 6603f9fe445SBarry Smith Logically Collective on PC 6612bb17772SBarry Smith 6622bb17772SBarry Smith Input Parameters: 6632bb17772SBarry Smith + pc - the preconditioner context 6642bb17772SBarry Smith - applyBA - the application-provided BA routine 6652bb17772SBarry Smith 66653a7a830SPatrick Sanan Calling sequence of applyBA: 6672bb17772SBarry Smith .vb 6686891c3e4SJed Brown PetscErrorCode applyBA (PC pc,Vec xin,Vec xout) 6692bb17772SBarry Smith .ve 6702bb17772SBarry Smith 6716891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 6722bb17772SBarry Smith . xin - input vector 6732bb17772SBarry Smith - xout - output vector 6742bb17772SBarry Smith 67595452b02SPatrick Sanan Notes: 67695452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 6774aa34b0aSBarry Smith 6782bb17772SBarry Smith Level: developer 6792bb17772SBarry Smith 6802bb17772SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetContext(), PCShellSetApply() 6812bb17772SBarry Smith @*/ 6827087cfbeSBarry Smith PetscErrorCode PCShellSetApplyBA(PC pc,PetscErrorCode (*applyBA)(PC,PCSide,Vec,Vec,Vec)) 6832bb17772SBarry Smith { 6842bb17772SBarry Smith PetscFunctionBegin; 6850700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 686*cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetApplyBA_C",(PC,PetscErrorCode (*)(PC,PCSide,Vec,Vec,Vec)),(pc,applyBA)); 6872bb17772SBarry Smith PetscFunctionReturn(0); 6882bb17772SBarry Smith } 6892bb17772SBarry Smith 6904b9ad928SBarry Smith /*@C 6914b9ad928SBarry Smith PCShellSetApplyTranspose - Sets routine to use as preconditioner transpose. 6924b9ad928SBarry Smith 6933f9fe445SBarry Smith Logically Collective on PC 6944b9ad928SBarry Smith 6954b9ad928SBarry Smith Input Parameters: 6964b9ad928SBarry Smith + pc - the preconditioner context 6974b9ad928SBarry Smith - apply - the application-provided preconditioning transpose routine 6984b9ad928SBarry Smith 6994b9ad928SBarry Smith Calling sequence of apply: 7004b9ad928SBarry Smith .vb 7016891c3e4SJed Brown PetscErrorCode applytranspose (PC pc,Vec xin,Vec xout) 7024b9ad928SBarry Smith .ve 7034b9ad928SBarry Smith 7046891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 7054b9ad928SBarry Smith . xin - input vector 7064b9ad928SBarry Smith - xout - output vector 7074b9ad928SBarry Smith 70895452b02SPatrick Sanan Notes: 70995452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 7104aa34b0aSBarry Smith 7114b9ad928SBarry Smith Level: developer 7124b9ad928SBarry Smith 7134b9ad928SBarry Smith Notes: 7144b9ad928SBarry Smith Uses the same context variable as PCShellSetApply(). 7154b9ad928SBarry Smith 7162bb17772SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApply(), PCSetContext(), PCShellSetApplyBA() 7174b9ad928SBarry Smith @*/ 7187087cfbeSBarry Smith PetscErrorCode PCShellSetApplyTranspose(PC pc,PetscErrorCode (*applytranspose)(PC,Vec,Vec)) 7194b9ad928SBarry Smith { 7204b9ad928SBarry Smith PetscFunctionBegin; 7210700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 722*cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetApplyTranspose_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,applytranspose)); 7234b9ad928SBarry Smith PetscFunctionReturn(0); 7244b9ad928SBarry Smith } 7254b9ad928SBarry Smith 7267cdd61b2SBarry Smith /*@C 7277cdd61b2SBarry Smith PCShellSetPreSolve - Sets routine to apply to the operators/vectors before a KSPSolve() is 7287cdd61b2SBarry Smith applied. This usually does something like scale the linear system in some application 7297cdd61b2SBarry Smith specific way. 7307cdd61b2SBarry Smith 7313f9fe445SBarry Smith Logically Collective on PC 7327cdd61b2SBarry Smith 7337cdd61b2SBarry Smith Input Parameters: 7347cdd61b2SBarry Smith + pc - the preconditioner context 7357cdd61b2SBarry Smith - presolve - the application-provided presolve routine 7367cdd61b2SBarry Smith 7377cdd61b2SBarry Smith Calling sequence of presolve: 7387cdd61b2SBarry Smith .vb 7396891c3e4SJed Brown PetscErrorCode presolve (PC,KSP ksp,Vec b,Vec x) 7407cdd61b2SBarry Smith .ve 7417cdd61b2SBarry Smith 7426891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 7437cdd61b2SBarry Smith . xin - input vector 7447cdd61b2SBarry Smith - xout - output vector 7457cdd61b2SBarry Smith 74695452b02SPatrick Sanan Notes: 74795452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 7484aa34b0aSBarry Smith 7497cdd61b2SBarry Smith Level: developer 7507cdd61b2SBarry Smith 751be29d3c6SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetPostSolve(), PCShellSetContext() 7527cdd61b2SBarry Smith @*/ 7537087cfbeSBarry Smith PetscErrorCode PCShellSetPreSolve(PC pc,PetscErrorCode (*presolve)(PC,KSP,Vec,Vec)) 7547cdd61b2SBarry Smith { 7557cdd61b2SBarry Smith PetscFunctionBegin; 7560700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 757*cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetPreSolve_C",(PC,PetscErrorCode (*)(PC,KSP,Vec,Vec)),(pc,presolve)); 7587cdd61b2SBarry Smith PetscFunctionReturn(0); 7597cdd61b2SBarry Smith } 7607cdd61b2SBarry Smith 7617cdd61b2SBarry Smith /*@C 7627cdd61b2SBarry Smith PCShellSetPostSolve - Sets routine to apply to the operators/vectors before a KSPSolve() is 7637cdd61b2SBarry Smith applied. This usually does something like scale the linear system in some application 7647cdd61b2SBarry Smith specific way. 7657cdd61b2SBarry Smith 7663f9fe445SBarry Smith Logically Collective on PC 7677cdd61b2SBarry Smith 7687cdd61b2SBarry Smith Input Parameters: 7697cdd61b2SBarry Smith + pc - the preconditioner context 7707cdd61b2SBarry Smith - postsolve - the application-provided presolve routine 7717cdd61b2SBarry Smith 7727cdd61b2SBarry Smith Calling sequence of postsolve: 7737cdd61b2SBarry Smith .vb 7746891c3e4SJed Brown PetscErrorCode postsolve(PC,KSP ksp,Vec b,Vec x) 7757cdd61b2SBarry Smith .ve 7767cdd61b2SBarry Smith 7776891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 7787cdd61b2SBarry Smith . xin - input vector 7797cdd61b2SBarry Smith - xout - output vector 7807cdd61b2SBarry Smith 78195452b02SPatrick Sanan Notes: 78295452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 7834aa34b0aSBarry Smith 7847cdd61b2SBarry Smith Level: developer 7857cdd61b2SBarry Smith 786be29d3c6SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose(), PCShellSetPreSolve(), PCShellSetContext() 7877cdd61b2SBarry Smith @*/ 7887087cfbeSBarry Smith PetscErrorCode PCShellSetPostSolve(PC pc,PetscErrorCode (*postsolve)(PC,KSP,Vec,Vec)) 7897cdd61b2SBarry Smith { 7907cdd61b2SBarry Smith PetscFunctionBegin; 7910700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 792*cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetPostSolve_C",(PC,PetscErrorCode (*)(PC,KSP,Vec,Vec)),(pc,postsolve)); 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 .seealso: PCShellGetName() 8094b9ad928SBarry Smith @*/ 8107087cfbeSBarry Smith PetscErrorCode PCShellSetName(PC pc,const char name[]) 8114b9ad928SBarry Smith { 8124b9ad928SBarry Smith PetscFunctionBegin; 8130700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 814*cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetName_C",(PC,const char []),(pc,name)); 8154b9ad928SBarry Smith PetscFunctionReturn(0); 8164b9ad928SBarry Smith } 8174b9ad928SBarry Smith 8184b9ad928SBarry Smith /*@C 8194b9ad928SBarry Smith PCShellGetName - Gets an optional name that the user has set for a shell 8204b9ad928SBarry Smith preconditioner. 8214b9ad928SBarry Smith 8224b9ad928SBarry Smith Not Collective 8234b9ad928SBarry Smith 8244b9ad928SBarry Smith Input Parameter: 8254b9ad928SBarry Smith . pc - the preconditioner context 8264b9ad928SBarry Smith 8274b9ad928SBarry Smith Output Parameter: 8284b9ad928SBarry Smith . name - character string describing shell preconditioner (you should not free this) 8294b9ad928SBarry Smith 8304b9ad928SBarry Smith Level: developer 8314b9ad928SBarry Smith 8324b9ad928SBarry Smith .seealso: PCShellSetName() 8334b9ad928SBarry Smith @*/ 834ccaf0856SBarry Smith PetscErrorCode PCShellGetName(PC pc,const char *name[]) 8354b9ad928SBarry Smith { 8364b9ad928SBarry Smith PetscFunctionBegin; 8370700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 8384482741eSBarry Smith PetscValidPointer(name,2); 839*cac4c232SBarry Smith PetscUseMethod(pc,"PCShellGetName_C",(PC,const char*[]),(pc,name)); 8404b9ad928SBarry Smith PetscFunctionReturn(0); 8414b9ad928SBarry Smith } 8424b9ad928SBarry Smith 8434b9ad928SBarry Smith /*@C 8444b9ad928SBarry Smith PCShellSetApplyRichardson - Sets routine to use as preconditioner 8454b9ad928SBarry Smith in Richardson iteration. 8464b9ad928SBarry Smith 8473f9fe445SBarry Smith Logically Collective on PC 8484b9ad928SBarry Smith 8494b9ad928SBarry Smith Input Parameters: 8504b9ad928SBarry Smith + pc - the preconditioner context 851be29d3c6SBarry Smith - apply - the application-provided preconditioning routine 8524b9ad928SBarry Smith 8534b9ad928SBarry Smith Calling sequence of apply: 8544b9ad928SBarry Smith .vb 8556891c3e4SJed Brown PetscErrorCode apply (PC pc,Vec b,Vec x,Vec r,PetscReal rtol,PetscReal abstol,PetscReal dtol,PetscInt maxits) 8564b9ad928SBarry Smith .ve 8574b9ad928SBarry Smith 8586891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 8594b9ad928SBarry Smith . b - right-hand-side 8604b9ad928SBarry Smith . x - current iterate 8614b9ad928SBarry Smith . r - work space 8624b9ad928SBarry Smith . rtol - relative tolerance of residual norm to stop at 86370441072SBarry Smith . abstol - absolute tolerance of residual norm to stop at 8644b9ad928SBarry Smith . dtol - if residual norm increases by this factor than return 8654b9ad928SBarry Smith - maxits - number of iterations to run 8664b9ad928SBarry Smith 86795452b02SPatrick Sanan Notes: 86895452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 8694aa34b0aSBarry Smith 8704b9ad928SBarry Smith Level: developer 8714b9ad928SBarry Smith 872be29d3c6SBarry Smith .seealso: PCShellSetApply(), PCShellSetContext() 8734b9ad928SBarry Smith @*/ 8747087cfbeSBarry Smith PetscErrorCode PCShellSetApplyRichardson(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool,PetscInt*,PCRichardsonConvergedReason*)) 8754b9ad928SBarry Smith { 8764b9ad928SBarry Smith PetscFunctionBegin; 8770700a824SBarry Smith PetscValidHeaderSpecific(pc,PC_CLASSID,1); 878*cac4c232SBarry Smith PetscTryMethod(pc,"PCShellSetApplyRichardson_C",(PC,PetscErrorCode (*)(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool,PetscInt*,PCRichardsonConvergedReason*)),(pc,apply)); 8794b9ad928SBarry Smith PetscFunctionReturn(0); 8804b9ad928SBarry Smith } 8814b9ad928SBarry Smith 8824b9ad928SBarry Smith /*MC 8834b9ad928SBarry Smith PCSHELL - Creates a new preconditioner class for use with your 8844b9ad928SBarry Smith own private data storage format. 8854b9ad928SBarry Smith 8864b9ad928SBarry Smith Level: advanced 887e0bb08deSStefano Zampini 8884b9ad928SBarry Smith Usage: 8896891c3e4SJed Brown $ extern PetscErrorCode apply(PC,Vec,Vec); 8906891c3e4SJed Brown $ extern PetscErrorCode applyba(PC,PCSide,Vec,Vec,Vec); 8916891c3e4SJed Brown $ extern PetscErrorCode applytranspose(PC,Vec,Vec); 8926891c3e4SJed Brown $ extern PetscErrorCode setup(PC); 8936891c3e4SJed Brown $ extern PetscErrorCode destroy(PC); 8946891c3e4SJed Brown $ 8954b9ad928SBarry Smith $ PCCreate(comm,&pc); 8964b9ad928SBarry Smith $ PCSetType(pc,PCSHELL); 897be29d3c6SBarry Smith $ PCShellSetContext(pc,ctx) 8986891c3e4SJed Brown $ PCShellSetApply(pc,apply); 8996891c3e4SJed Brown $ PCShellSetApplyBA(pc,applyba); (optional) 9006891c3e4SJed Brown $ PCShellSetApplyTranspose(pc,applytranspose); (optional) 9014b9ad928SBarry Smith $ PCShellSetSetUp(pc,setup); (optional) 902d01c8aa3SLisandro Dalcin $ PCShellSetDestroy(pc,destroy); (optional) 9034b9ad928SBarry Smith 9044b9ad928SBarry Smith .seealso: PCCreate(), PCSetType(), PCType (for list of available types), PC, 905fd2d0fe1Svictor MATSHELL, PCShellSetSetUp(), PCShellSetApply(), PCShellSetView(), 906fd2d0fe1Svictor PCShellSetApplyTranspose(), PCShellSetName(), PCShellSetApplyRichardson(), 9072bb17772SBarry Smith PCShellGetName(), PCShellSetContext(), PCShellGetContext(), PCShellSetApplyBA() 9084b9ad928SBarry Smith M*/ 9094b9ad928SBarry Smith 9108cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PCCreate_Shell(PC pc) 9114b9ad928SBarry Smith { 9124b9ad928SBarry Smith PC_Shell *shell; 9134b9ad928SBarry Smith 9144b9ad928SBarry Smith PetscFunctionBegin; 9159566063dSJacob Faibussowitsch PetscCall(PetscNewLog(pc,&shell)); 9164b9ad928SBarry Smith pc->data = (void*)shell; 9174b9ad928SBarry Smith 918d01c8aa3SLisandro Dalcin pc->ops->destroy = PCDestroy_Shell; 9194b9ad928SBarry Smith pc->ops->view = PCView_Shell; 920d01c8aa3SLisandro Dalcin pc->ops->apply = PCApply_Shell; 9217b6e2003SPierre Jolivet pc->ops->matapply = PCMatApply_Shell; 9221b581b66SBarry Smith pc->ops->applysymmetricleft = PCApplySymmetricLeft_Shell; 9231b581b66SBarry Smith pc->ops->applysymmetricright = PCApplySymmetricRight_Shell; 9240a545947SLisandro Dalcin pc->ops->applytranspose = NULL; 9250a545947SLisandro Dalcin pc->ops->applyrichardson = NULL; 9260a545947SLisandro Dalcin pc->ops->setup = NULL; 9270a545947SLisandro Dalcin pc->ops->presolve = NULL; 9280a545947SLisandro Dalcin pc->ops->postsolve = NULL; 9294b9ad928SBarry Smith 9300a545947SLisandro Dalcin shell->apply = NULL; 9310a545947SLisandro Dalcin shell->applytranspose = NULL; 9320a545947SLisandro Dalcin shell->name = NULL; 9330a545947SLisandro Dalcin shell->applyrich = NULL; 9340a545947SLisandro Dalcin shell->presolve = NULL; 9350a545947SLisandro Dalcin shell->postsolve = NULL; 9360a545947SLisandro Dalcin shell->ctx = NULL; 9370a545947SLisandro Dalcin shell->setup = NULL; 9380a545947SLisandro Dalcin shell->view = NULL; 9390a545947SLisandro Dalcin shell->destroy = NULL; 9400a545947SLisandro Dalcin shell->applysymmetricleft = NULL; 9410a545947SLisandro Dalcin shell->applysymmetricright = NULL; 9424b9ad928SBarry Smith 9439566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetDestroy_C",PCShellSetDestroy_Shell)); 9449566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetSetUp_C",PCShellSetSetUp_Shell)); 9459566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApply_C",PCShellSetApply_Shell)); 9469566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetMatApply_C",PCShellSetMatApply_Shell)); 9479566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplySymmetricLeft_C",PCShellSetApplySymmetricLeft_Shell)); 9489566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplySymmetricRight_C",PCShellSetApplySymmetricRight_Shell)); 9499566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyBA_C",PCShellSetApplyBA_Shell)); 9509566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetPreSolve_C",PCShellSetPreSolve_Shell)); 9519566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetPostSolve_C",PCShellSetPostSolve_Shell)); 9529566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetView_C",PCShellSetView_Shell)); 9539566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyTranspose_C",PCShellSetApplyTranspose_Shell)); 9549566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetName_C",PCShellSetName_Shell)); 9559566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellGetName_C",PCShellGetName_Shell)); 9569566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyRichardson_C",PCShellSetApplyRichardson_Shell)); 9574b9ad928SBarry Smith PetscFunctionReturn(0); 9584b9ad928SBarry Smith } 959