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 29f1580f4eSBarry Smith PCShellGetContext - Returns the user-provided context associated with a shell `PC` 30be29d3c6SBarry Smith 31be29d3c6SBarry Smith Not Collective 32be29d3c6SBarry Smith 33be29d3c6SBarry Smith Input Parameter: 34f1580f4eSBarry Smith . pc - of type `PCSHELL` 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 41f1580f4eSBarry Smith Note: 42be29d3c6SBarry Smith This routine is intended for use within various shell routines 43be29d3c6SBarry Smith 44f1580f4eSBarry Smith Fortran Note: 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 48f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetContext()` 49be29d3c6SBarry Smith @*/ 50d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellGetContext(PC pc, void *ctx) 51d71ae5a4SJacob Faibussowitsch { 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 /*@ 64f1580f4eSBarry Smith PCShellSetContext - sets the context for a shell `PC` 65be29d3c6SBarry Smith 66*c3339decSBarry Smith Logically Collective 67be29d3c6SBarry Smith 68be29d3c6SBarry Smith Input Parameters: 69f1580f4eSBarry Smith + pc - the `PC` of type `PCSHELL` 70be29d3c6SBarry Smith - ctx - the context 71be29d3c6SBarry Smith 72be29d3c6SBarry Smith Level: advanced 73be29d3c6SBarry Smith 74f1580f4eSBarry Smith Fortran Note: 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 @*/ 80d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetContext(PC pc, void *ctx) 81d71ae5a4SJacob Faibussowitsch { 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 92d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCSetUp_Shell(PC pc) 93d71ae5a4SJacob Faibussowitsch { 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 102d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApply_Shell(PC pc, Vec x, Vec y) 103d71ae5a4SJacob Faibussowitsch { 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)); 1121e66621cSBarry Smith /* increase the state of the output vector if the user did not update its state themself as should have been done */ 1131e66621cSBarry Smith if (instate == outstate) PetscCall(PetscObjectStateIncrease((PetscObject)y)); 1144b9ad928SBarry Smith PetscFunctionReturn(0); 1154b9ad928SBarry Smith } 1164b9ad928SBarry Smith 117d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCMatApply_Shell(PC pc, Mat X, Mat Y) 118d71ae5a4SJacob Faibussowitsch { 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)); 1271e66621cSBarry Smith /* increase the state of the output vector if the user did not update its state themself as should have been done */ 1281e66621cSBarry Smith if (instate == outstate) PetscCall(PetscObjectStateIncrease((PetscObject)Y)); 1297b6e2003SPierre Jolivet PetscFunctionReturn(0); 1307b6e2003SPierre Jolivet } 1317b6e2003SPierre Jolivet 132d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApplySymmetricLeft_Shell(PC pc, Vec x, Vec y) 133d71ae5a4SJacob Faibussowitsch { 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 142d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApplySymmetricRight_Shell(PC pc, Vec x, Vec y) 143d71ae5a4SJacob Faibussowitsch { 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 152d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApplyBA_Shell(PC pc, PCSide side, Vec x, Vec y, Vec w) 153d71ae5a4SJacob Faibussowitsch { 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)); 1621e66621cSBarry Smith /* increase the state of the output vector if the user did not update its state themself as should have been done */ 1631e66621cSBarry Smith if (instate == outstate) PetscCall(PetscObjectStateIncrease((PetscObject)w)); 1642bb17772SBarry Smith PetscFunctionReturn(0); 1652bb17772SBarry Smith } 1662bb17772SBarry Smith 167d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPreSolveChangeRHS_Shell(PC pc, PetscBool *change) 168d71ae5a4SJacob Faibussowitsch { 169a06fd7f2SStefano Zampini PetscFunctionBegin; 170a06fd7f2SStefano Zampini *change = PETSC_TRUE; 171a06fd7f2SStefano Zampini PetscFunctionReturn(0); 172a06fd7f2SStefano Zampini } 173a06fd7f2SStefano Zampini 174d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPreSolve_Shell(PC pc, KSP ksp, Vec b, Vec x) 175d71ae5a4SJacob Faibussowitsch { 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 184d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPostSolve_Shell(PC pc, KSP ksp, Vec b, Vec x) 185d71ae5a4SJacob Faibussowitsch { 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 194d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCApplyTranspose_Shell(PC pc, Vec x, Vec y) 195d71ae5a4SJacob Faibussowitsch { 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)); 2041e66621cSBarry Smith /* increase the state of the output vector if the user did not update its state themself as should have been done */ 2051e66621cSBarry Smith if (instate == outstate) PetscCall(PetscObjectStateIncrease((PetscObject)y)); 2064b9ad928SBarry Smith PetscFunctionReturn(0); 2074b9ad928SBarry Smith } 2084b9ad928SBarry Smith 209d71ae5a4SJacob Faibussowitsch 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) 210d71ae5a4SJacob Faibussowitsch { 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 */ 2201e66621cSBarry Smith if (instate == outstate) PetscCall(PetscObjectStateIncrease((PetscObject)y)); 2214b9ad928SBarry Smith PetscFunctionReturn(0); 2224b9ad928SBarry Smith } 2234b9ad928SBarry Smith 224d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCDestroy_Shell(PC pc) 225d71ae5a4SJacob Faibussowitsch { 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 250d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCView_Shell(PC pc, PetscViewer viewer) 251d71ae5a4SJacob Faibussowitsch { 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) { 2581e66621cSBarry Smith if (shell->name) PetscCall(PetscViewerASCIIPrintf(viewer, " %s\n", shell->name)); 2591e66621cSBarry 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 269d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCShellSetDestroy_Shell(PC pc, PetscErrorCode (*destroy)(PC)) 270d71ae5a4SJacob Faibussowitsch { 271c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell *)pc->data; 27218be62a5SSatish Balay 27318be62a5SSatish Balay PetscFunctionBegin; 27418be62a5SSatish Balay shell->destroy = destroy; 27518be62a5SSatish Balay PetscFunctionReturn(0); 27618be62a5SSatish Balay } 27718be62a5SSatish Balay 278d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCShellSetSetUp_Shell(PC pc, PetscErrorCode (*setup)(PC)) 279d71ae5a4SJacob Faibussowitsch { 280feb237baSPierre Jolivet PC_Shell *shell = (PC_Shell *)pc->data; 2814b9ad928SBarry Smith 2824b9ad928SBarry Smith PetscFunctionBegin; 2834b9ad928SBarry Smith shell->setup = setup; 284d01c8aa3SLisandro Dalcin if (setup) pc->ops->setup = PCSetUp_Shell; 2850a545947SLisandro Dalcin else pc->ops->setup = NULL; 2864b9ad928SBarry Smith PetscFunctionReturn(0); 2874b9ad928SBarry Smith } 2884b9ad928SBarry Smith 289d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCShellSetApply_Shell(PC pc, PetscErrorCode (*apply)(PC, Vec, Vec)) 290d71ae5a4SJacob Faibussowitsch { 291c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell *)pc->data; 2924b9ad928SBarry Smith 2934b9ad928SBarry Smith PetscFunctionBegin; 2944b9ad928SBarry Smith shell->apply = apply; 2954b9ad928SBarry Smith PetscFunctionReturn(0); 2964b9ad928SBarry Smith } 2974b9ad928SBarry Smith 298d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCShellSetMatApply_Shell(PC pc, PetscErrorCode (*matapply)(PC, Mat, Mat)) 299d71ae5a4SJacob Faibussowitsch { 3007b6e2003SPierre Jolivet PC_Shell *shell = (PC_Shell *)pc->data; 3017b6e2003SPierre Jolivet 3027b6e2003SPierre Jolivet PetscFunctionBegin; 3037b6e2003SPierre Jolivet shell->matapply = matapply; 3040e0fe96bSStefano Zampini if (matapply) pc->ops->matapply = PCMatApply_Shell; 3050e0fe96bSStefano Zampini else pc->ops->matapply = NULL; 3067b6e2003SPierre Jolivet PetscFunctionReturn(0); 3077b6e2003SPierre Jolivet } 3087b6e2003SPierre Jolivet 309d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCShellSetApplySymmetricLeft_Shell(PC pc, PetscErrorCode (*apply)(PC, Vec, Vec)) 310d71ae5a4SJacob Faibussowitsch { 3111b581b66SBarry Smith PC_Shell *shell = (PC_Shell *)pc->data; 3121b581b66SBarry Smith 3131b581b66SBarry Smith PetscFunctionBegin; 3141b581b66SBarry Smith shell->applysymmetricleft = apply; 3151b581b66SBarry Smith PetscFunctionReturn(0); 3161b581b66SBarry Smith } 3171b581b66SBarry Smith 318d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCShellSetApplySymmetricRight_Shell(PC pc, PetscErrorCode (*apply)(PC, Vec, Vec)) 319d71ae5a4SJacob Faibussowitsch { 3201b581b66SBarry Smith PC_Shell *shell = (PC_Shell *)pc->data; 3211b581b66SBarry Smith 3221b581b66SBarry Smith PetscFunctionBegin; 3231b581b66SBarry Smith shell->applysymmetricright = apply; 3241b581b66SBarry Smith PetscFunctionReturn(0); 3251b581b66SBarry Smith } 3261b581b66SBarry Smith 327d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCShellSetApplyBA_Shell(PC pc, PetscErrorCode (*applyBA)(PC, PCSide, Vec, Vec, Vec)) 328d71ae5a4SJacob Faibussowitsch { 329c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell *)pc->data; 3302bb17772SBarry Smith 3312bb17772SBarry Smith PetscFunctionBegin; 332d01c8aa3SLisandro Dalcin shell->applyBA = applyBA; 333d01c8aa3SLisandro Dalcin if (applyBA) pc->ops->applyBA = PCApplyBA_Shell; 3340a545947SLisandro Dalcin else pc->ops->applyBA = NULL; 3352bb17772SBarry Smith PetscFunctionReturn(0); 3362bb17772SBarry Smith } 3372bb17772SBarry Smith 338d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCShellSetPreSolve_Shell(PC pc, PetscErrorCode (*presolve)(PC, KSP, Vec, Vec)) 339d71ae5a4SJacob Faibussowitsch { 340c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell *)pc->data; 3417cdd61b2SBarry Smith 3427cdd61b2SBarry Smith PetscFunctionBegin; 3437cdd61b2SBarry Smith shell->presolve = presolve; 344a06fd7f2SStefano Zampini if (presolve) { 345a06fd7f2SStefano Zampini pc->ops->presolve = PCPreSolve_Shell; 3469566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCPreSolveChangeRHS_C", PCPreSolveChangeRHS_Shell)); 347a06fd7f2SStefano Zampini } else { 3480a545947SLisandro Dalcin pc->ops->presolve = NULL; 3499566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCPreSolveChangeRHS_C", NULL)); 350a06fd7f2SStefano Zampini } 3517cdd61b2SBarry Smith PetscFunctionReturn(0); 3527cdd61b2SBarry Smith } 3537cdd61b2SBarry Smith 354d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCShellSetPostSolve_Shell(PC pc, PetscErrorCode (*postsolve)(PC, KSP, Vec, Vec)) 355d71ae5a4SJacob Faibussowitsch { 356c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell *)pc->data; 3577cdd61b2SBarry Smith 3587cdd61b2SBarry Smith PetscFunctionBegin; 3597cdd61b2SBarry Smith shell->postsolve = postsolve; 360d01c8aa3SLisandro Dalcin if (postsolve) pc->ops->postsolve = PCPostSolve_Shell; 3610a545947SLisandro Dalcin else pc->ops->postsolve = NULL; 3627cdd61b2SBarry Smith PetscFunctionReturn(0); 3637cdd61b2SBarry Smith } 3647cdd61b2SBarry Smith 365d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCShellSetView_Shell(PC pc, PetscErrorCode (*view)(PC, PetscViewer)) 366d71ae5a4SJacob Faibussowitsch { 367c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell *)pc->data; 3684b9ad928SBarry Smith 3694b9ad928SBarry Smith PetscFunctionBegin; 3704b9ad928SBarry Smith shell->view = view; 3714b9ad928SBarry Smith PetscFunctionReturn(0); 3724b9ad928SBarry Smith } 3734b9ad928SBarry Smith 374d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCShellSetApplyTranspose_Shell(PC pc, PetscErrorCode (*applytranspose)(PC, Vec, Vec)) 375d71ae5a4SJacob Faibussowitsch { 376c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell *)pc->data; 3774b9ad928SBarry Smith 3784b9ad928SBarry Smith PetscFunctionBegin; 3794b9ad928SBarry Smith shell->applytranspose = applytranspose; 380d01c8aa3SLisandro Dalcin if (applytranspose) pc->ops->applytranspose = PCApplyTranspose_Shell; 3810a545947SLisandro Dalcin else pc->ops->applytranspose = NULL; 382d01c8aa3SLisandro Dalcin PetscFunctionReturn(0); 383d01c8aa3SLisandro Dalcin } 384d01c8aa3SLisandro Dalcin 385d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCShellSetApplyRichardson_Shell(PC pc, PetscErrorCode (*applyrich)(PC, Vec, Vec, Vec, PetscReal, PetscReal, PetscReal, PetscInt, PetscBool, PetscInt *, PCRichardsonConvergedReason *)) 386d71ae5a4SJacob Faibussowitsch { 387c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell *)pc->data; 388d01c8aa3SLisandro Dalcin 389d01c8aa3SLisandro Dalcin PetscFunctionBegin; 390d01c8aa3SLisandro Dalcin shell->applyrich = applyrich; 391d01c8aa3SLisandro Dalcin if (applyrich) pc->ops->applyrichardson = PCApplyRichardson_Shell; 3920a545947SLisandro Dalcin else pc->ops->applyrichardson = NULL; 3934b9ad928SBarry Smith PetscFunctionReturn(0); 3944b9ad928SBarry Smith } 3954b9ad928SBarry Smith 396d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCShellSetName_Shell(PC pc, const char name[]) 397d71ae5a4SJacob Faibussowitsch { 398c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell *)pc->data; 3994b9ad928SBarry Smith 4004b9ad928SBarry Smith PetscFunctionBegin; 4019566063dSJacob Faibussowitsch PetscCall(PetscFree(shell->name)); 4029566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(name, &shell->name)); 4034b9ad928SBarry Smith PetscFunctionReturn(0); 4044b9ad928SBarry Smith } 4054b9ad928SBarry Smith 406d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCShellGetName_Shell(PC pc, const char *name[]) 407d71ae5a4SJacob Faibussowitsch { 408c5ae4b9aSBarry Smith PC_Shell *shell = (PC_Shell *)pc->data; 4094b9ad928SBarry Smith 4104b9ad928SBarry Smith PetscFunctionBegin; 4114b9ad928SBarry Smith *name = shell->name; 4124b9ad928SBarry Smith PetscFunctionReturn(0); 4134b9ad928SBarry Smith } 4144b9ad928SBarry Smith 41518be62a5SSatish Balay /*@C 41618be62a5SSatish Balay PCShellSetDestroy - Sets routine to use to destroy the user-provided 41718be62a5SSatish Balay application context. 41818be62a5SSatish Balay 419*c3339decSBarry Smith Logically Collective 42018be62a5SSatish Balay 42118be62a5SSatish Balay Input Parameters: 42218be62a5SSatish Balay + pc - the preconditioner context 423a2b725a8SWilliam Gropp - destroy - the application-provided destroy routine 42418be62a5SSatish Balay 42518be62a5SSatish Balay Calling sequence of destroy: 42618be62a5SSatish Balay .vb 4276891c3e4SJed Brown PetscErrorCode destroy (PC) 42818be62a5SSatish Balay .ve 42918be62a5SSatish Balay 43018be62a5SSatish Balay . ptr - the application context 43118be62a5SSatish Balay 432f1580f4eSBarry Smith Note: 43395452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 4344aa34b0aSBarry Smith 435f1580f4eSBarry Smith Level: intermediate 43618be62a5SSatish Balay 437f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApply()`, `PCShellSetContext()` 43818be62a5SSatish Balay @*/ 439d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetDestroy(PC pc, PetscErrorCode (*destroy)(PC)) 440d71ae5a4SJacob Faibussowitsch { 44118be62a5SSatish Balay PetscFunctionBegin; 4420700a824SBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 443cac4c232SBarry Smith PetscTryMethod(pc, "PCShellSetDestroy_C", (PC, PetscErrorCode(*)(PC)), (pc, destroy)); 44418be62a5SSatish Balay PetscFunctionReturn(0); 44518be62a5SSatish Balay } 44618be62a5SSatish Balay 4474b9ad928SBarry Smith /*@C 4484b9ad928SBarry Smith PCShellSetSetUp - Sets routine to use to "setup" the preconditioner whenever the 4494b9ad928SBarry Smith matrix operator is changed. 4504b9ad928SBarry Smith 451*c3339decSBarry Smith Logically Collective 4524b9ad928SBarry Smith 4534b9ad928SBarry Smith Input Parameters: 4544b9ad928SBarry Smith + pc - the preconditioner context 455a2b725a8SWilliam Gropp - setup - the application-provided setup routine 4564b9ad928SBarry Smith 4574b9ad928SBarry Smith Calling sequence of setup: 4584b9ad928SBarry Smith .vb 4596891c3e4SJed Brown PetscErrorCode setup (PC pc) 4604b9ad928SBarry Smith .ve 4614b9ad928SBarry Smith 4626891c3e4SJed Brown . pc - the preconditioner, get the application context with PCShellGetContext() 4634b9ad928SBarry Smith 464f1580f4eSBarry Smith Note: 46595452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 4664aa34b0aSBarry Smith 467f1580f4eSBarry Smith Level: intermediate 4684b9ad928SBarry Smith 469f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApplyRichardson()`, `PCShellSetApply()`, `PCShellSetContext()` 4704b9ad928SBarry Smith @*/ 471d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetSetUp(PC pc, PetscErrorCode (*setup)(PC)) 472d71ae5a4SJacob Faibussowitsch { 4734b9ad928SBarry Smith PetscFunctionBegin; 4740700a824SBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 475cac4c232SBarry Smith PetscTryMethod(pc, "PCShellSetSetUp_C", (PC, PetscErrorCode(*)(PC)), (pc, setup)); 4764b9ad928SBarry Smith PetscFunctionReturn(0); 4774b9ad928SBarry Smith } 4784b9ad928SBarry Smith 4794b9ad928SBarry Smith /*@C 4804b9ad928SBarry Smith PCShellSetView - Sets routine to use as viewer of shell preconditioner 4814b9ad928SBarry Smith 482*c3339decSBarry Smith Logically Collective 4834b9ad928SBarry Smith 4844b9ad928SBarry Smith Input Parameters: 4854b9ad928SBarry Smith + pc - the preconditioner context 4864b9ad928SBarry Smith - view - the application-provided view routine 4874b9ad928SBarry Smith 48853a7a830SPatrick Sanan Calling sequence of view: 4894b9ad928SBarry Smith .vb 4906891c3e4SJed Brown PetscErrorCode view(PC pc,PetscViewer v) 4914b9ad928SBarry Smith .ve 4924b9ad928SBarry Smith 4936891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 4944b9ad928SBarry Smith - v - viewer 4954b9ad928SBarry Smith 496f1580f4eSBarry Smith Note: 49795452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 4984aa34b0aSBarry Smith 499f1580f4eSBarry Smith Level: advanced 5004b9ad928SBarry Smith 501f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()` 5024b9ad928SBarry Smith @*/ 503d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetView(PC pc, PetscErrorCode (*view)(PC, PetscViewer)) 504d71ae5a4SJacob Faibussowitsch { 5054b9ad928SBarry Smith PetscFunctionBegin; 5060700a824SBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 507cac4c232SBarry Smith PetscTryMethod(pc, "PCShellSetView_C", (PC, PetscErrorCode(*)(PC, PetscViewer)), (pc, view)); 5084b9ad928SBarry Smith PetscFunctionReturn(0); 5094b9ad928SBarry Smith } 5104b9ad928SBarry Smith 5114b9ad928SBarry Smith /*@C 5124b9ad928SBarry Smith PCShellSetApply - Sets routine to use as preconditioner. 5134b9ad928SBarry Smith 514*c3339decSBarry Smith Logically Collective 5154b9ad928SBarry Smith 5164b9ad928SBarry Smith Input Parameters: 5174b9ad928SBarry Smith + pc - the preconditioner context 518be29d3c6SBarry Smith - apply - the application-provided preconditioning routine 5194b9ad928SBarry Smith 5204b9ad928SBarry Smith Calling sequence of apply: 5214b9ad928SBarry Smith .vb 5226891c3e4SJed Brown PetscErrorCode apply (PC pc,Vec xin,Vec xout) 5234b9ad928SBarry Smith .ve 5244b9ad928SBarry Smith 5256891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 5264b9ad928SBarry Smith . xin - input vector 5274b9ad928SBarry Smith - xout - output vector 5284b9ad928SBarry Smith 529f1580f4eSBarry Smith Note: 53095452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 5314aa34b0aSBarry Smith 532f1580f4eSBarry Smith Level: intermediate 5334b9ad928SBarry Smith 534f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetContext()`, `PCShellSetApplyBA()`, `PCShellSetApplySymmetricRight()`, `PCShellSetApplySymmetricLeft()` 5354b9ad928SBarry Smith @*/ 536d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetApply(PC pc, PetscErrorCode (*apply)(PC, Vec, Vec)) 537d71ae5a4SJacob Faibussowitsch { 5384b9ad928SBarry Smith PetscFunctionBegin; 5390700a824SBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 540cac4c232SBarry Smith PetscTryMethod(pc, "PCShellSetApply_C", (PC, PetscErrorCode(*)(PC, Vec, Vec)), (pc, apply)); 5414b9ad928SBarry Smith PetscFunctionReturn(0); 5424b9ad928SBarry Smith } 5434b9ad928SBarry Smith 5441b581b66SBarry Smith /*@C 5456437efc7SEric Chamberland PCShellSetMatApply - Sets routine to use as preconditioner on a block of vectors. 5467b6e2003SPierre Jolivet 547*c3339decSBarry Smith Logically Collective 5487b6e2003SPierre Jolivet 5497b6e2003SPierre Jolivet Input Parameters: 5507b6e2003SPierre Jolivet + pc - the preconditioner context 5517b6e2003SPierre Jolivet - apply - the application-provided preconditioning routine 5527b6e2003SPierre Jolivet 5537b6e2003SPierre Jolivet Calling sequence of apply: 5547b6e2003SPierre Jolivet .vb 5557b6e2003SPierre Jolivet PetscErrorCode apply (PC pc,Mat Xin,Mat Xout) 5567b6e2003SPierre Jolivet .ve 5577b6e2003SPierre Jolivet 5587b6e2003SPierre Jolivet + pc - the preconditioner, get the application context with PCShellGetContext() 5597b6e2003SPierre Jolivet . Xin - input block of vectors 5607b6e2003SPierre Jolivet - Xout - output block of vectors 5617b6e2003SPierre Jolivet 562f1580f4eSBarry Smith Note: 563f1580f4eSBarry Smith The function MUST return an error code of 0 on success and nonzero on failure. 5647b6e2003SPierre Jolivet 565f1580f4eSBarry Smith Level: advanced 5667b6e2003SPierre Jolivet 567f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApply()` 5687b6e2003SPierre Jolivet @*/ 569d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetMatApply(PC pc, PetscErrorCode (*matapply)(PC, Mat, Mat)) 570d71ae5a4SJacob Faibussowitsch { 5717b6e2003SPierre Jolivet PetscFunctionBegin; 5727b6e2003SPierre Jolivet PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 573cac4c232SBarry Smith PetscTryMethod(pc, "PCShellSetMatApply_C", (PC, PetscErrorCode(*)(PC, Mat, Mat)), (pc, matapply)); 5747b6e2003SPierre Jolivet PetscFunctionReturn(0); 5757b6e2003SPierre Jolivet } 5767b6e2003SPierre Jolivet 5777b6e2003SPierre Jolivet /*@C 578f1580f4eSBarry Smith PCShellSetApplySymmetricLeft - Sets routine to use as left preconditioner (when the `PC_SYMMETRIC` is used). 5791b581b66SBarry Smith 580*c3339decSBarry Smith Logically Collective 5811b581b66SBarry Smith 5821b581b66SBarry Smith Input Parameters: 5831b581b66SBarry Smith + pc - the preconditioner context 5841b581b66SBarry Smith - apply - the application-provided left preconditioning routine 5851b581b66SBarry Smith 5861b581b66SBarry Smith Calling sequence of apply: 5871b581b66SBarry Smith .vb 5881b581b66SBarry Smith PetscErrorCode apply (PC pc,Vec xin,Vec xout) 5891b581b66SBarry Smith .ve 5901b581b66SBarry Smith 591f1580f4eSBarry Smith + pc - the preconditioner, get the application context with `PCShellGetContext()` 5921b581b66SBarry Smith . xin - input vector 5931b581b66SBarry Smith - xout - output vector 5941b581b66SBarry Smith 595f1580f4eSBarry Smith Note: 596f1580f4eSBarry Smith The function MUST return an error code of 0 on success and nonzero on failure. 5971b581b66SBarry Smith 598f1580f4eSBarry Smith Level: advanced 5991b581b66SBarry Smith 600f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApply()`, `PCShellSetApplySymmetricLeft()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetContext()` 6011b581b66SBarry Smith @*/ 602d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetApplySymmetricLeft(PC pc, PetscErrorCode (*apply)(PC, Vec, Vec)) 603d71ae5a4SJacob Faibussowitsch { 6041b581b66SBarry Smith PetscFunctionBegin; 6051b581b66SBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 606cac4c232SBarry Smith PetscTryMethod(pc, "PCShellSetApplySymmetricLeft_C", (PC, PetscErrorCode(*)(PC, Vec, Vec)), (pc, apply)); 6071b581b66SBarry Smith PetscFunctionReturn(0); 6081b581b66SBarry Smith } 6091b581b66SBarry Smith 6101b581b66SBarry Smith /*@C 611f1580f4eSBarry Smith PCShellSetApplySymmetricRight - Sets routine to use as right preconditioner (when the `PC_SYMMETRIC` is used). 6121b581b66SBarry Smith 613*c3339decSBarry Smith Logically Collective 6141b581b66SBarry Smith 6151b581b66SBarry Smith Input Parameters: 6161b581b66SBarry Smith + pc - the preconditioner context 6171b581b66SBarry Smith - apply - the application-provided right preconditioning routine 6181b581b66SBarry Smith 6191b581b66SBarry Smith Calling sequence of apply: 6201b581b66SBarry Smith .vb 6211b581b66SBarry Smith PetscErrorCode apply (PC pc,Vec xin,Vec xout) 6221b581b66SBarry Smith .ve 6231b581b66SBarry Smith 6241b581b66SBarry Smith + pc - the preconditioner, get the application context with PCShellGetContext() 6251b581b66SBarry Smith . xin - input vector 6261b581b66SBarry Smith - xout - output vector 6271b581b66SBarry Smith 628f1580f4eSBarry Smith Note: 629f1580f4eSBarry Smith The function MUST return an error code of 0 on success and nonzero on failure. 6301b581b66SBarry Smith 631f1580f4eSBarry Smith Level: advanced 6321b581b66SBarry Smith 633f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApply()`, `PCShellSetApplySymmetricLeft()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetContext()` 6341b581b66SBarry Smith @*/ 635d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetApplySymmetricRight(PC pc, PetscErrorCode (*apply)(PC, Vec, Vec)) 636d71ae5a4SJacob Faibussowitsch { 6371b581b66SBarry Smith PetscFunctionBegin; 6381b581b66SBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 639cac4c232SBarry Smith PetscTryMethod(pc, "PCShellSetApplySymmetricRight_C", (PC, PetscErrorCode(*)(PC, Vec, Vec)), (pc, apply)); 6401b581b66SBarry Smith PetscFunctionReturn(0); 6411b581b66SBarry Smith } 6421b581b66SBarry Smith 6432bb17772SBarry Smith /*@C 6442bb17772SBarry Smith PCShellSetApplyBA - Sets routine to use as preconditioner times operator. 6452bb17772SBarry Smith 646*c3339decSBarry Smith Logically Collective 6472bb17772SBarry Smith 6482bb17772SBarry Smith Input Parameters: 6492bb17772SBarry Smith + pc - the preconditioner context 6502bb17772SBarry Smith - applyBA - the application-provided BA routine 6512bb17772SBarry Smith 65253a7a830SPatrick Sanan Calling sequence of applyBA: 6532bb17772SBarry Smith .vb 6546891c3e4SJed Brown PetscErrorCode applyBA (PC pc,Vec xin,Vec xout) 6552bb17772SBarry Smith .ve 6562bb17772SBarry Smith 6576891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 6582bb17772SBarry Smith . xin - input vector 6592bb17772SBarry Smith - xout - output vector 6602bb17772SBarry Smith 661f1580f4eSBarry Smith Note: 662f1580f4eSBarry Smith The function MUST return an error code of 0 on success and nonzero on failure. 6634aa34b0aSBarry Smith 664f1580f4eSBarry Smith Level: intermediate 6652bb17772SBarry Smith 666f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetContext()`, `PCShellSetApply()` 6672bb17772SBarry Smith @*/ 668d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetApplyBA(PC pc, PetscErrorCode (*applyBA)(PC, PCSide, Vec, Vec, Vec)) 669d71ae5a4SJacob Faibussowitsch { 6702bb17772SBarry Smith PetscFunctionBegin; 6710700a824SBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 672cac4c232SBarry Smith PetscTryMethod(pc, "PCShellSetApplyBA_C", (PC, PetscErrorCode(*)(PC, PCSide, Vec, Vec, Vec)), (pc, applyBA)); 6732bb17772SBarry Smith PetscFunctionReturn(0); 6742bb17772SBarry Smith } 6752bb17772SBarry Smith 6764b9ad928SBarry Smith /*@C 6774b9ad928SBarry Smith PCShellSetApplyTranspose - Sets routine to use as preconditioner transpose. 6784b9ad928SBarry Smith 679*c3339decSBarry Smith Logically Collective 6804b9ad928SBarry Smith 6814b9ad928SBarry Smith Input Parameters: 6824b9ad928SBarry Smith + pc - the preconditioner context 6834b9ad928SBarry Smith - apply - the application-provided preconditioning transpose routine 6844b9ad928SBarry Smith 6854b9ad928SBarry Smith Calling sequence of apply: 6864b9ad928SBarry Smith .vb 6876891c3e4SJed Brown PetscErrorCode applytranspose (PC pc,Vec xin,Vec xout) 6884b9ad928SBarry Smith .ve 6894b9ad928SBarry Smith 6906891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 6914b9ad928SBarry Smith . xin - input vector 6924b9ad928SBarry Smith - xout - output vector 6934b9ad928SBarry Smith 694f1580f4eSBarry Smith Note: 69595452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 6964aa34b0aSBarry Smith 697f1580f4eSBarry Smith Level: intermediate 6984b9ad928SBarry Smith 699f1580f4eSBarry Smith Note: 700f1580f4eSBarry Smith Uses the same context variable as `PCShellSetApply()`. 7014b9ad928SBarry Smith 702f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApply()`, `PCSetContext()`, `PCShellSetApplyBA()` 7034b9ad928SBarry Smith @*/ 704d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetApplyTranspose(PC pc, PetscErrorCode (*applytranspose)(PC, Vec, Vec)) 705d71ae5a4SJacob Faibussowitsch { 7064b9ad928SBarry Smith PetscFunctionBegin; 7070700a824SBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 708cac4c232SBarry Smith PetscTryMethod(pc, "PCShellSetApplyTranspose_C", (PC, PetscErrorCode(*)(PC, Vec, Vec)), (pc, applytranspose)); 7094b9ad928SBarry Smith PetscFunctionReturn(0); 7104b9ad928SBarry Smith } 7114b9ad928SBarry Smith 7127cdd61b2SBarry Smith /*@C 713f1580f4eSBarry Smith PCShellSetPreSolve - Sets routine to apply to the operators/vectors before a `KSPSolve()` is 7147cdd61b2SBarry Smith applied. This usually does something like scale the linear system in some application 7157cdd61b2SBarry Smith specific way. 7167cdd61b2SBarry Smith 717*c3339decSBarry Smith Logically Collective 7187cdd61b2SBarry Smith 7197cdd61b2SBarry Smith Input Parameters: 7207cdd61b2SBarry Smith + pc - the preconditioner context 7217cdd61b2SBarry Smith - presolve - the application-provided presolve routine 7227cdd61b2SBarry Smith 7237cdd61b2SBarry Smith Calling sequence of presolve: 7247cdd61b2SBarry Smith .vb 7256891c3e4SJed Brown PetscErrorCode presolve (PC,KSP ksp,Vec b,Vec x) 7267cdd61b2SBarry Smith .ve 7277cdd61b2SBarry Smith 7286891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 7297cdd61b2SBarry Smith . xin - input vector 7307cdd61b2SBarry Smith - xout - output vector 7317cdd61b2SBarry Smith 732f1580f4eSBarry Smith Note: 733f1580f4eSBarry Smith The function MUST return an error code of 0 on success and nonzero on failure. 7344aa34b0aSBarry Smith 735f1580f4eSBarry Smith Level: advanced 7367cdd61b2SBarry Smith 737f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetPostSolve()`, `PCShellSetContext()` 7387cdd61b2SBarry Smith @*/ 739d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetPreSolve(PC pc, PetscErrorCode (*presolve)(PC, KSP, Vec, Vec)) 740d71ae5a4SJacob Faibussowitsch { 7417cdd61b2SBarry Smith PetscFunctionBegin; 7420700a824SBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 743cac4c232SBarry Smith PetscTryMethod(pc, "PCShellSetPreSolve_C", (PC, PetscErrorCode(*)(PC, KSP, Vec, Vec)), (pc, presolve)); 7447cdd61b2SBarry Smith PetscFunctionReturn(0); 7457cdd61b2SBarry Smith } 7467cdd61b2SBarry Smith 7477cdd61b2SBarry Smith /*@C 748f1580f4eSBarry Smith PCShellSetPostSolve - Sets routine to apply to the operators/vectors before a `KSPSolve()` is 7497cdd61b2SBarry Smith applied. This usually does something like scale the linear system in some application 7507cdd61b2SBarry Smith specific way. 7517cdd61b2SBarry Smith 752*c3339decSBarry Smith Logically Collective 7537cdd61b2SBarry Smith 7547cdd61b2SBarry Smith Input Parameters: 7557cdd61b2SBarry Smith + pc - the preconditioner context 7567cdd61b2SBarry Smith - postsolve - the application-provided presolve routine 7577cdd61b2SBarry Smith 7587cdd61b2SBarry Smith Calling sequence of postsolve: 7597cdd61b2SBarry Smith .vb 7606891c3e4SJed Brown PetscErrorCode postsolve(PC,KSP ksp,Vec b,Vec x) 7617cdd61b2SBarry Smith .ve 7627cdd61b2SBarry Smith 763f1580f4eSBarry Smith + pc - the preconditioner, get the application context with `PCShellGetContext()` 7647cdd61b2SBarry Smith . xin - input vector 7657cdd61b2SBarry Smith - xout - output vector 7667cdd61b2SBarry Smith 767f1580f4eSBarry Smith Note: 76895452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 7694aa34b0aSBarry Smith 770f1580f4eSBarry Smith Level: advanced 7717cdd61b2SBarry Smith 772f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetPreSolve()`, `PCShellSetContext()` 7737cdd61b2SBarry Smith @*/ 774d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetPostSolve(PC pc, PetscErrorCode (*postsolve)(PC, KSP, Vec, Vec)) 775d71ae5a4SJacob Faibussowitsch { 7767cdd61b2SBarry Smith PetscFunctionBegin; 7770700a824SBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 778cac4c232SBarry Smith PetscTryMethod(pc, "PCShellSetPostSolve_C", (PC, PetscErrorCode(*)(PC, KSP, Vec, Vec)), (pc, postsolve)); 7797cdd61b2SBarry Smith PetscFunctionReturn(0); 7807cdd61b2SBarry Smith } 7817cdd61b2SBarry Smith 7824b9ad928SBarry Smith /*@C 783f1580f4eSBarry Smith PCShellSetName - Sets an optional name to associate with a `PCSHELL` 7844b9ad928SBarry Smith preconditioner. 7854b9ad928SBarry Smith 7864b9ad928SBarry Smith Not Collective 7874b9ad928SBarry Smith 7884b9ad928SBarry Smith Input Parameters: 7894b9ad928SBarry Smith + pc - the preconditioner context 7904b9ad928SBarry Smith - name - character string describing shell preconditioner 7914b9ad928SBarry Smith 792f1580f4eSBarry Smith Level: intermediate 7934b9ad928SBarry Smith 794f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellGetName()` 7954b9ad928SBarry Smith @*/ 796d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetName(PC pc, const char name[]) 797d71ae5a4SJacob Faibussowitsch { 7984b9ad928SBarry Smith PetscFunctionBegin; 7990700a824SBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 800cac4c232SBarry Smith PetscTryMethod(pc, "PCShellSetName_C", (PC, const char[]), (pc, name)); 8014b9ad928SBarry Smith PetscFunctionReturn(0); 8024b9ad928SBarry Smith } 8034b9ad928SBarry Smith 8044b9ad928SBarry Smith /*@C 805f1580f4eSBarry Smith PCShellGetName - Gets an optional name that the user has set for a `PCSHELL` 8064b9ad928SBarry Smith preconditioner. 8074b9ad928SBarry Smith 8084b9ad928SBarry Smith Not Collective 8094b9ad928SBarry Smith 8104b9ad928SBarry Smith Input Parameter: 8114b9ad928SBarry Smith . pc - the preconditioner context 8124b9ad928SBarry Smith 8134b9ad928SBarry Smith Output Parameter: 8144b9ad928SBarry Smith . name - character string describing shell preconditioner (you should not free this) 8154b9ad928SBarry Smith 816f1580f4eSBarry Smith Level: intermediate 8174b9ad928SBarry Smith 818f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetName()` 8194b9ad928SBarry Smith @*/ 820d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellGetName(PC pc, const char *name[]) 821d71ae5a4SJacob Faibussowitsch { 8224b9ad928SBarry Smith PetscFunctionBegin; 8230700a824SBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 8244482741eSBarry Smith PetscValidPointer(name, 2); 825cac4c232SBarry Smith PetscUseMethod(pc, "PCShellGetName_C", (PC, const char *[]), (pc, name)); 8264b9ad928SBarry Smith PetscFunctionReturn(0); 8274b9ad928SBarry Smith } 8284b9ad928SBarry Smith 8294b9ad928SBarry Smith /*@C 8304b9ad928SBarry Smith PCShellSetApplyRichardson - Sets routine to use as preconditioner 8314b9ad928SBarry Smith in Richardson iteration. 8324b9ad928SBarry Smith 833*c3339decSBarry Smith Logically Collective 8344b9ad928SBarry Smith 8354b9ad928SBarry Smith Input Parameters: 8364b9ad928SBarry Smith + pc - the preconditioner context 837be29d3c6SBarry Smith - apply - the application-provided preconditioning routine 8384b9ad928SBarry Smith 8394b9ad928SBarry Smith Calling sequence of apply: 8404b9ad928SBarry Smith .vb 8416891c3e4SJed Brown PetscErrorCode apply (PC pc,Vec b,Vec x,Vec r,PetscReal rtol,PetscReal abstol,PetscReal dtol,PetscInt maxits) 8424b9ad928SBarry Smith .ve 8434b9ad928SBarry Smith 8446891c3e4SJed Brown + pc - the preconditioner, get the application context with PCShellGetContext() 8454b9ad928SBarry Smith . b - right-hand-side 8464b9ad928SBarry Smith . x - current iterate 8474b9ad928SBarry Smith . r - work space 8484b9ad928SBarry Smith . rtol - relative tolerance of residual norm to stop at 84970441072SBarry Smith . abstol - absolute tolerance of residual norm to stop at 8504b9ad928SBarry Smith . dtol - if residual norm increases by this factor than return 8514b9ad928SBarry Smith - maxits - number of iterations to run 8524b9ad928SBarry Smith 853f1580f4eSBarry Smith Note: 85495452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 8554aa34b0aSBarry Smith 856f1580f4eSBarry Smith Level: advanced 8574b9ad928SBarry Smith 858db781477SPatrick Sanan .seealso: `PCShellSetApply()`, `PCShellSetContext()` 8594b9ad928SBarry Smith @*/ 860d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetApplyRichardson(PC pc, PetscErrorCode (*apply)(PC, Vec, Vec, Vec, PetscReal, PetscReal, PetscReal, PetscInt, PetscBool, PetscInt *, PCRichardsonConvergedReason *)) 861d71ae5a4SJacob Faibussowitsch { 8624b9ad928SBarry Smith PetscFunctionBegin; 8630700a824SBarry Smith PetscValidHeaderSpecific(pc, PC_CLASSID, 1); 864cac4c232SBarry Smith PetscTryMethod(pc, "PCShellSetApplyRichardson_C", (PC, PetscErrorCode(*)(PC, Vec, Vec, Vec, PetscReal, PetscReal, PetscReal, PetscInt, PetscBool, PetscInt *, PCRichardsonConvergedReason *)), (pc, apply)); 8654b9ad928SBarry Smith PetscFunctionReturn(0); 8664b9ad928SBarry Smith } 8674b9ad928SBarry Smith 8684b9ad928SBarry Smith /*MC 869f1580f4eSBarry Smith PCSHELL - Creates a new preconditioner class for use with a users 870f1580f4eSBarry Smith own private data storage format and preconditioner application code 8714b9ad928SBarry Smith 8724b9ad928SBarry Smith Level: advanced 873e0bb08deSStefano Zampini 8744b9ad928SBarry Smith Usage: 875f1580f4eSBarry Smith .vb 876f1580f4eSBarry Smith extern PetscErrorCode apply(PC,Vec,Vec); 877f1580f4eSBarry Smith extern PetscErrorCode applyba(PC,PCSide,Vec,Vec,Vec); 878f1580f4eSBarry Smith extern PetscErrorCode applytranspose(PC,Vec,Vec); 879f1580f4eSBarry Smith extern PetscErrorCode setup(PC); 880f1580f4eSBarry Smith extern PetscErrorCode destroy(PC); 881f1580f4eSBarry Smith 882f1580f4eSBarry Smith PCCreate(comm,&pc); 883f1580f4eSBarry Smith PCSetType(pc,PCSHELL); 884f1580f4eSBarry Smith PCShellSetContext(pc,ctx) 885f1580f4eSBarry Smith PCShellSetApply(pc,apply); 886f1580f4eSBarry Smith PCShellSetApplyBA(pc,applyba); (optional) 887f1580f4eSBarry Smith PCShellSetApplyTranspose(pc,applytranspose); (optional) 888f1580f4eSBarry Smith PCShellSetSetUp(pc,setup); (optional) 889f1580f4eSBarry Smith PCShellSetDestroy(pc,destroy); (optional) 890f1580f4eSBarry Smith .ve 8914b9ad928SBarry Smith 892db781477SPatrick Sanan .seealso: `PCCreate()`, `PCSetType()`, `PCType`, `PC`, 893f1580f4eSBarry Smith `MATSHELL`, `PCShellSetSetUp()`, `PCShellSetApply()`, `PCShellSetView()`, `PCShellSetDestroy()`, `PCShellSetPostSolve()`, 894f1580f4eSBarry Smith `PCShellSetApplyTranspose()`, `PCShellSetName()`, `PCShellSetApplyRichardson()`, `PCShellSetPreSolve()`, `PCShellSetView()`, 895f1580f4eSBarry Smith `PCShellGetName()`, `PCShellSetContext()`, `PCShellGetContext()`, `PCShellSetApplyBA()`, `MATSHELL`, `PCShellSetMatApply()`, 8964b9ad928SBarry Smith M*/ 8974b9ad928SBarry Smith 898d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PCCreate_Shell(PC pc) 899d71ae5a4SJacob Faibussowitsch { 9004b9ad928SBarry Smith PC_Shell *shell; 9014b9ad928SBarry Smith 9024b9ad928SBarry Smith PetscFunctionBegin; 9034dfa11a4SJacob Faibussowitsch PetscCall(PetscNew(&shell)); 9044b9ad928SBarry Smith pc->data = (void *)shell; 9054b9ad928SBarry Smith 906d01c8aa3SLisandro Dalcin pc->ops->destroy = PCDestroy_Shell; 9074b9ad928SBarry Smith pc->ops->view = PCView_Shell; 908d01c8aa3SLisandro Dalcin pc->ops->apply = PCApply_Shell; 9091b581b66SBarry Smith pc->ops->applysymmetricleft = PCApplySymmetricLeft_Shell; 9101b581b66SBarry Smith pc->ops->applysymmetricright = PCApplySymmetricRight_Shell; 9110e0fe96bSStefano Zampini pc->ops->matapply = NULL; 9120a545947SLisandro Dalcin pc->ops->applytranspose = NULL; 9130a545947SLisandro Dalcin pc->ops->applyrichardson = NULL; 9140a545947SLisandro Dalcin pc->ops->setup = NULL; 9150a545947SLisandro Dalcin pc->ops->presolve = NULL; 9160a545947SLisandro Dalcin pc->ops->postsolve = NULL; 9174b9ad928SBarry Smith 9180a545947SLisandro Dalcin shell->apply = NULL; 9190a545947SLisandro Dalcin shell->applytranspose = NULL; 9200a545947SLisandro Dalcin shell->name = NULL; 9210a545947SLisandro Dalcin shell->applyrich = NULL; 9220a545947SLisandro Dalcin shell->presolve = NULL; 9230a545947SLisandro Dalcin shell->postsolve = NULL; 9240a545947SLisandro Dalcin shell->ctx = NULL; 9250a545947SLisandro Dalcin shell->setup = NULL; 9260a545947SLisandro Dalcin shell->view = NULL; 9270a545947SLisandro Dalcin shell->destroy = NULL; 9280a545947SLisandro Dalcin shell->applysymmetricleft = NULL; 9290a545947SLisandro Dalcin shell->applysymmetricright = NULL; 9304b9ad928SBarry Smith 9319566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetDestroy_C", PCShellSetDestroy_Shell)); 9329566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetSetUp_C", PCShellSetSetUp_Shell)); 9339566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetApply_C", PCShellSetApply_Shell)); 9349566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetMatApply_C", PCShellSetMatApply_Shell)); 9359566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetApplySymmetricLeft_C", PCShellSetApplySymmetricLeft_Shell)); 9369566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetApplySymmetricRight_C", PCShellSetApplySymmetricRight_Shell)); 9379566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetApplyBA_C", PCShellSetApplyBA_Shell)); 9389566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetPreSolve_C", PCShellSetPreSolve_Shell)); 9399566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetPostSolve_C", PCShellSetPostSolve_Shell)); 9409566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetView_C", PCShellSetView_Shell)); 9419566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetApplyTranspose_C", PCShellSetApplyTranspose_Shell)); 9429566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetName_C", PCShellSetName_Shell)); 9439566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellGetName_C", PCShellGetName_Shell)); 9449566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetApplyRichardson_C", PCShellSetApplyRichardson_Shell)); 9454b9ad928SBarry Smith PetscFunctionReturn(0); 9464b9ad928SBarry Smith } 947