xref: /petsc/src/ksp/pc/impls/shell/shellpc.c (revision 0e0fe96bc1d92a12801fe7969fb2494a674124d9)
14d0a8057SBarry Smith 
24b9ad928SBarry Smith /*
34b9ad928SBarry Smith    This provides a simple shell for Fortran (and C programmers) to
44b9ad928SBarry Smith   create their own preconditioner without writing much interface code.
54b9ad928SBarry Smith */
64b9ad928SBarry Smith 
7af0996ceSBarry Smith #include <petsc/private/pcimpl.h>        /*I "petscpc.h" I*/
84b9ad928SBarry Smith 
94b9ad928SBarry Smith typedef struct {
10be29d3c6SBarry Smith   void *ctx;                     /* user provided contexts for preconditioner */
112fa5cd67SKarl Rupp 
126891c3e4SJed Brown   PetscErrorCode (*destroy)(PC);
136891c3e4SJed Brown   PetscErrorCode (*setup)(PC);
146891c3e4SJed Brown   PetscErrorCode (*apply)(PC,Vec,Vec);
157b6e2003SPierre Jolivet   PetscErrorCode (*matapply)(PC,Mat,Mat);
161b581b66SBarry Smith   PetscErrorCode (*applysymmetricleft)(PC,Vec,Vec);
171b581b66SBarry Smith   PetscErrorCode (*applysymmetricright)(PC,Vec,Vec);
186891c3e4SJed Brown   PetscErrorCode (*applyBA)(PC,PCSide,Vec,Vec,Vec);
196891c3e4SJed Brown   PetscErrorCode (*presolve)(PC,KSP,Vec,Vec);
206891c3e4SJed Brown   PetscErrorCode (*postsolve)(PC,KSP,Vec,Vec);
216891c3e4SJed Brown   PetscErrorCode (*view)(PC,PetscViewer);
226891c3e4SJed Brown   PetscErrorCode (*applytranspose)(PC,Vec,Vec);
23ace3abfcSBarry Smith   PetscErrorCode (*applyrich)(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool,PetscInt*,PCRichardsonConvergedReason*);
242fa5cd67SKarl Rupp 
254b9ad928SBarry Smith   char *name;
264b9ad928SBarry Smith } PC_Shell;
274b9ad928SBarry Smith 
28b29801fcSSatish Balay /*@C
29be29d3c6SBarry Smith     PCShellGetContext - Returns the user-provided context associated with a shell PC
30be29d3c6SBarry Smith 
31be29d3c6SBarry Smith     Not Collective
32be29d3c6SBarry Smith 
33be29d3c6SBarry Smith     Input Parameter:
34c5ae4b9aSBarry Smith .   pc - should have been created with PCSetType(pc,shell)
35be29d3c6SBarry Smith 
36be29d3c6SBarry Smith     Output Parameter:
37be29d3c6SBarry Smith .   ctx - the user provided context
38be29d3c6SBarry Smith 
39be29d3c6SBarry Smith     Level: advanced
40be29d3c6SBarry Smith 
41be29d3c6SBarry Smith     Notes:
42be29d3c6SBarry Smith     This routine is intended for use within various shell routines
43be29d3c6SBarry Smith 
4495452b02SPatrick Sanan    Fortran Notes:
4595452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
46daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
47daf670e6SBarry Smith 
48db781477SPatrick Sanan .seealso: `PCShellSetContext()`
49be29d3c6SBarry Smith @*/
503ec1f749SStefano Zampini PetscErrorCode  PCShellGetContext(PC pc,void *ctx)
51be29d3c6SBarry Smith {
52ace3abfcSBarry Smith   PetscBool      flg;
53be29d3c6SBarry Smith 
54be29d3c6SBarry Smith   PetscFunctionBegin;
550700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
56be29d3c6SBarry Smith   PetscValidPointer(ctx,2);
579566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pc,PCSHELL,&flg));
583ec1f749SStefano Zampini   if (!flg) *(void**)ctx = NULL;
593ec1f749SStefano Zampini   else      *(void**)ctx = ((PC_Shell*)(pc->data))->ctx;
60be29d3c6SBarry Smith   PetscFunctionReturn(0);
61be29d3c6SBarry Smith }
62be29d3c6SBarry Smith 
639dd1005fSJed Brown /*@
64be29d3c6SBarry Smith     PCShellSetContext - sets the context for a shell PC
65be29d3c6SBarry Smith 
663f9fe445SBarry Smith    Logically Collective on PC
67be29d3c6SBarry Smith 
68be29d3c6SBarry Smith     Input Parameters:
69be29d3c6SBarry Smith +   pc - the shell PC
70be29d3c6SBarry Smith -   ctx - the context
71be29d3c6SBarry Smith 
72be29d3c6SBarry Smith    Level: advanced
73be29d3c6SBarry Smith 
7495452b02SPatrick Sanan    Fortran Notes:
7595452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
76daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
77daf670e6SBarry Smith 
78db781477SPatrick Sanan .seealso: `PCShellGetContext()`, `PCSHELL`
79be29d3c6SBarry Smith @*/
807087cfbeSBarry Smith PetscErrorCode  PCShellSetContext(PC pc,void *ctx)
81be29d3c6SBarry Smith {
82c5ae4b9aSBarry Smith   PC_Shell       *shell = (PC_Shell*)pc->data;
83ace3abfcSBarry Smith   PetscBool      flg;
84be29d3c6SBarry Smith 
85be29d3c6SBarry Smith   PetscFunctionBegin;
860700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
879566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)pc,PCSHELL,&flg));
882fa5cd67SKarl Rupp   if (flg) shell->ctx = ctx;
89be29d3c6SBarry Smith   PetscFunctionReturn(0);
90be29d3c6SBarry Smith }
91be29d3c6SBarry Smith 
926849ba73SBarry Smith static PetscErrorCode PCSetUp_Shell(PC pc)
934b9ad928SBarry Smith {
94c5ae4b9aSBarry Smith   PC_Shell       *shell = (PC_Shell*)pc->data;
954b9ad928SBarry Smith 
964b9ad928SBarry Smith   PetscFunctionBegin;
9728b400f6SJacob Faibussowitsch   PetscCheck(shell->setup,PetscObjectComm((PetscObject)pc),PETSC_ERR_USER,"No setup() routine provided to Shell PC");
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) {
1136aad120cSJose E. Roman     /* increase the state of the output vector since the user did not update its state themself 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) {
1306aad120cSJose E. Roman     /* increase the state of the output vector since the user did not update its state themself 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) {
1676aad120cSJose E. Roman     /* increase the state of the output vector since the user did not update its state themself 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;
318*0e0fe96bSStefano Zampini   if (matapply) pc->ops->matapply = PCMatApply_Shell;
319*0e0fe96bSStefano Zampini   else          pc->ops->matapply = NULL;
3207b6e2003SPierre Jolivet   PetscFunctionReturn(0);
3217b6e2003SPierre Jolivet }
3227b6e2003SPierre Jolivet 
3231b581b66SBarry Smith static PetscErrorCode  PCShellSetApplySymmetricLeft_Shell(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec))
3241b581b66SBarry Smith {
3251b581b66SBarry Smith   PC_Shell *shell = (PC_Shell*)pc->data;
3261b581b66SBarry Smith 
3271b581b66SBarry Smith   PetscFunctionBegin;
3281b581b66SBarry Smith   shell->applysymmetricleft = apply;
3291b581b66SBarry Smith   PetscFunctionReturn(0);
3301b581b66SBarry Smith }
3311b581b66SBarry Smith 
3321b581b66SBarry Smith static PetscErrorCode  PCShellSetApplySymmetricRight_Shell(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec))
3331b581b66SBarry Smith {
3341b581b66SBarry Smith   PC_Shell *shell = (PC_Shell*)pc->data;
3351b581b66SBarry Smith 
3361b581b66SBarry Smith   PetscFunctionBegin;
3371b581b66SBarry Smith   shell->applysymmetricright = apply;
3381b581b66SBarry Smith   PetscFunctionReturn(0);
3391b581b66SBarry Smith }
3401b581b66SBarry Smith 
341f7a08781SBarry Smith static PetscErrorCode  PCShellSetApplyBA_Shell(PC pc,PetscErrorCode (*applyBA)(PC,PCSide,Vec,Vec,Vec))
3422bb17772SBarry Smith {
343c5ae4b9aSBarry Smith   PC_Shell *shell = (PC_Shell*)pc->data;
3442bb17772SBarry Smith 
3452bb17772SBarry Smith   PetscFunctionBegin;
346d01c8aa3SLisandro Dalcin   shell->applyBA = applyBA;
347d01c8aa3SLisandro Dalcin   if (applyBA) pc->ops->applyBA  = PCApplyBA_Shell;
3480a545947SLisandro Dalcin   else         pc->ops->applyBA  = NULL;
3492bb17772SBarry Smith   PetscFunctionReturn(0);
3502bb17772SBarry Smith }
3512bb17772SBarry Smith 
352f7a08781SBarry Smith static PetscErrorCode  PCShellSetPreSolve_Shell(PC pc,PetscErrorCode (*presolve)(PC,KSP,Vec,Vec))
3537cdd61b2SBarry Smith {
354c5ae4b9aSBarry Smith   PC_Shell       *shell = (PC_Shell*)pc->data;
3557cdd61b2SBarry Smith 
3567cdd61b2SBarry Smith   PetscFunctionBegin;
3577cdd61b2SBarry Smith   shell->presolve = presolve;
358a06fd7f2SStefano Zampini   if (presolve) {
359a06fd7f2SStefano Zampini     pc->ops->presolve = PCPreSolve_Shell;
3609566063dSJacob Faibussowitsch     PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCPreSolveChangeRHS_C",PCPreSolveChangeRHS_Shell));
361a06fd7f2SStefano Zampini   } else {
3620a545947SLisandro Dalcin     pc->ops->presolve = NULL;
3639566063dSJacob Faibussowitsch     PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCPreSolveChangeRHS_C",NULL));
364a06fd7f2SStefano Zampini   }
3657cdd61b2SBarry Smith   PetscFunctionReturn(0);
3667cdd61b2SBarry Smith }
3677cdd61b2SBarry Smith 
368f7a08781SBarry Smith static PetscErrorCode  PCShellSetPostSolve_Shell(PC pc,PetscErrorCode (*postsolve)(PC,KSP,Vec,Vec))
3697cdd61b2SBarry Smith {
370c5ae4b9aSBarry Smith   PC_Shell *shell = (PC_Shell*)pc->data;
3717cdd61b2SBarry Smith 
3727cdd61b2SBarry Smith   PetscFunctionBegin;
3737cdd61b2SBarry Smith   shell->postsolve = postsolve;
374d01c8aa3SLisandro Dalcin   if (postsolve) pc->ops->postsolve = PCPostSolve_Shell;
3750a545947SLisandro Dalcin   else           pc->ops->postsolve = NULL;
3767cdd61b2SBarry Smith   PetscFunctionReturn(0);
3777cdd61b2SBarry Smith }
3787cdd61b2SBarry Smith 
379f7a08781SBarry Smith static PetscErrorCode  PCShellSetView_Shell(PC pc,PetscErrorCode (*view)(PC,PetscViewer))
3804b9ad928SBarry Smith {
381c5ae4b9aSBarry Smith   PC_Shell *shell = (PC_Shell*)pc->data;
3824b9ad928SBarry Smith 
3834b9ad928SBarry Smith   PetscFunctionBegin;
3844b9ad928SBarry Smith   shell->view = view;
3854b9ad928SBarry Smith   PetscFunctionReturn(0);
3864b9ad928SBarry Smith }
3874b9ad928SBarry Smith 
388f7a08781SBarry Smith static PetscErrorCode  PCShellSetApplyTranspose_Shell(PC pc,PetscErrorCode (*applytranspose)(PC,Vec,Vec))
3894b9ad928SBarry Smith {
390c5ae4b9aSBarry Smith   PC_Shell *shell = (PC_Shell*)pc->data;
3914b9ad928SBarry Smith 
3924b9ad928SBarry Smith   PetscFunctionBegin;
3934b9ad928SBarry Smith   shell->applytranspose = applytranspose;
394d01c8aa3SLisandro Dalcin   if (applytranspose) pc->ops->applytranspose = PCApplyTranspose_Shell;
3950a545947SLisandro Dalcin   else                pc->ops->applytranspose = NULL;
396d01c8aa3SLisandro Dalcin   PetscFunctionReturn(0);
397d01c8aa3SLisandro Dalcin }
398d01c8aa3SLisandro Dalcin 
399f7a08781SBarry Smith static PetscErrorCode  PCShellSetApplyRichardson_Shell(PC pc,PetscErrorCode (*applyrich)(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool ,PetscInt*,PCRichardsonConvergedReason*))
400d01c8aa3SLisandro Dalcin {
401c5ae4b9aSBarry Smith   PC_Shell *shell = (PC_Shell*)pc->data;
402d01c8aa3SLisandro Dalcin 
403d01c8aa3SLisandro Dalcin   PetscFunctionBegin;
404d01c8aa3SLisandro Dalcin   shell->applyrich = applyrich;
405d01c8aa3SLisandro Dalcin   if (applyrich) pc->ops->applyrichardson = PCApplyRichardson_Shell;
4060a545947SLisandro Dalcin   else           pc->ops->applyrichardson = NULL;
4074b9ad928SBarry Smith   PetscFunctionReturn(0);
4084b9ad928SBarry Smith }
4094b9ad928SBarry Smith 
410f7a08781SBarry Smith static PetscErrorCode  PCShellSetName_Shell(PC pc,const char name[])
4114b9ad928SBarry Smith {
412c5ae4b9aSBarry Smith   PC_Shell       *shell = (PC_Shell*)pc->data;
4134b9ad928SBarry Smith 
4144b9ad928SBarry Smith   PetscFunctionBegin;
4159566063dSJacob Faibussowitsch   PetscCall(PetscFree(shell->name));
4169566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(name,&shell->name));
4174b9ad928SBarry Smith   PetscFunctionReturn(0);
4184b9ad928SBarry Smith }
4194b9ad928SBarry Smith 
420f7a08781SBarry Smith static PetscErrorCode  PCShellGetName_Shell(PC pc,const char *name[])
4214b9ad928SBarry Smith {
422c5ae4b9aSBarry Smith   PC_Shell *shell = (PC_Shell*)pc->data;
4234b9ad928SBarry Smith 
4244b9ad928SBarry Smith   PetscFunctionBegin;
4254b9ad928SBarry Smith   *name = shell->name;
4264b9ad928SBarry Smith   PetscFunctionReturn(0);
4274b9ad928SBarry Smith }
4284b9ad928SBarry Smith 
4294b9ad928SBarry Smith /* -------------------------------------------------------------------------------*/
4304b9ad928SBarry Smith 
43118be62a5SSatish Balay /*@C
43218be62a5SSatish Balay    PCShellSetDestroy - Sets routine to use to destroy the user-provided
43318be62a5SSatish Balay    application context.
43418be62a5SSatish Balay 
4353f9fe445SBarry Smith    Logically Collective on PC
43618be62a5SSatish Balay 
43718be62a5SSatish Balay    Input Parameters:
43818be62a5SSatish Balay +  pc - the preconditioner context
439a2b725a8SWilliam Gropp -  destroy - the application-provided destroy routine
44018be62a5SSatish Balay 
44118be62a5SSatish Balay    Calling sequence of destroy:
44218be62a5SSatish Balay .vb
4436891c3e4SJed Brown    PetscErrorCode destroy (PC)
44418be62a5SSatish Balay .ve
44518be62a5SSatish Balay 
44618be62a5SSatish Balay .  ptr - the application context
44718be62a5SSatish Balay 
44895452b02SPatrick Sanan    Notes:
44995452b02SPatrick Sanan     the function MUST return an error code of 0 on success and nonzero on failure.
4504aa34b0aSBarry Smith 
45118be62a5SSatish Balay    Level: developer
45218be62a5SSatish Balay 
453db781477SPatrick Sanan .seealso: `PCShellSetApply()`, `PCShellSetContext()`
45418be62a5SSatish Balay @*/
4557087cfbeSBarry Smith PetscErrorCode  PCShellSetDestroy(PC pc,PetscErrorCode (*destroy)(PC))
45618be62a5SSatish Balay {
45718be62a5SSatish Balay   PetscFunctionBegin;
4580700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
459cac4c232SBarry Smith   PetscTryMethod(pc,"PCShellSetDestroy_C",(PC,PetscErrorCode (*)(PC)),(pc,destroy));
46018be62a5SSatish Balay   PetscFunctionReturn(0);
46118be62a5SSatish Balay }
46218be62a5SSatish Balay 
4634b9ad928SBarry Smith /*@C
4644b9ad928SBarry Smith    PCShellSetSetUp - Sets routine to use to "setup" the preconditioner whenever the
4654b9ad928SBarry Smith    matrix operator is changed.
4664b9ad928SBarry Smith 
4673f9fe445SBarry Smith    Logically Collective on PC
4684b9ad928SBarry Smith 
4694b9ad928SBarry Smith    Input Parameters:
4704b9ad928SBarry Smith +  pc - the preconditioner context
471a2b725a8SWilliam Gropp -  setup - the application-provided setup routine
4724b9ad928SBarry Smith 
4734b9ad928SBarry Smith    Calling sequence of setup:
4744b9ad928SBarry Smith .vb
4756891c3e4SJed Brown    PetscErrorCode setup (PC pc)
4764b9ad928SBarry Smith .ve
4774b9ad928SBarry Smith 
4786891c3e4SJed Brown .  pc - the preconditioner, get the application context with PCShellGetContext()
4794b9ad928SBarry Smith 
48095452b02SPatrick Sanan    Notes:
48195452b02SPatrick Sanan     the function MUST return an error code of 0 on success and nonzero on failure.
4824aa34b0aSBarry Smith 
4834b9ad928SBarry Smith    Level: developer
4844b9ad928SBarry Smith 
485db781477SPatrick Sanan .seealso: `PCShellSetApplyRichardson()`, `PCShellSetApply()`, `PCShellSetContext()`
4864b9ad928SBarry Smith @*/
4877087cfbeSBarry Smith PetscErrorCode  PCShellSetSetUp(PC pc,PetscErrorCode (*setup)(PC))
4884b9ad928SBarry Smith {
4894b9ad928SBarry Smith   PetscFunctionBegin;
4900700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
491cac4c232SBarry Smith   PetscTryMethod(pc,"PCShellSetSetUp_C",(PC,PetscErrorCode (*)(PC)),(pc,setup));
4924b9ad928SBarry Smith   PetscFunctionReturn(0);
4934b9ad928SBarry Smith }
4944b9ad928SBarry Smith 
4954b9ad928SBarry Smith /*@C
4964b9ad928SBarry Smith    PCShellSetView - Sets routine to use as viewer of shell preconditioner
4974b9ad928SBarry Smith 
4983f9fe445SBarry Smith    Logically Collective on PC
4994b9ad928SBarry Smith 
5004b9ad928SBarry Smith    Input Parameters:
5014b9ad928SBarry Smith +  pc - the preconditioner context
5024b9ad928SBarry Smith -  view - the application-provided view routine
5034b9ad928SBarry Smith 
50453a7a830SPatrick Sanan    Calling sequence of view:
5054b9ad928SBarry Smith .vb
5066891c3e4SJed Brown    PetscErrorCode view(PC pc,PetscViewer v)
5074b9ad928SBarry Smith .ve
5084b9ad928SBarry Smith 
5096891c3e4SJed Brown +  pc - the preconditioner, get the application context with PCShellGetContext()
5104b9ad928SBarry Smith -  v   - viewer
5114b9ad928SBarry Smith 
51295452b02SPatrick Sanan    Notes:
51395452b02SPatrick Sanan     the function MUST return an error code of 0 on success and nonzero on failure.
5144aa34b0aSBarry Smith 
5154b9ad928SBarry Smith    Level: developer
5164b9ad928SBarry Smith 
517db781477SPatrick Sanan .seealso: `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`
5184b9ad928SBarry Smith @*/
5197087cfbeSBarry Smith PetscErrorCode  PCShellSetView(PC pc,PetscErrorCode (*view)(PC,PetscViewer))
5204b9ad928SBarry Smith {
5214b9ad928SBarry Smith   PetscFunctionBegin;
5220700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
523cac4c232SBarry Smith   PetscTryMethod(pc,"PCShellSetView_C",(PC,PetscErrorCode (*)(PC,PetscViewer)),(pc,view));
5244b9ad928SBarry Smith   PetscFunctionReturn(0);
5254b9ad928SBarry Smith }
5264b9ad928SBarry Smith 
5274b9ad928SBarry Smith /*@C
5284b9ad928SBarry Smith    PCShellSetApply - Sets routine to use as preconditioner.
5294b9ad928SBarry Smith 
5303f9fe445SBarry Smith    Logically Collective on PC
5314b9ad928SBarry Smith 
5324b9ad928SBarry Smith    Input Parameters:
5334b9ad928SBarry Smith +  pc - the preconditioner context
534be29d3c6SBarry Smith -  apply - the application-provided preconditioning routine
5354b9ad928SBarry Smith 
5364b9ad928SBarry Smith    Calling sequence of apply:
5374b9ad928SBarry Smith .vb
5386891c3e4SJed Brown    PetscErrorCode apply (PC pc,Vec xin,Vec xout)
5394b9ad928SBarry Smith .ve
5404b9ad928SBarry Smith 
5416891c3e4SJed Brown +  pc - the preconditioner, get the application context with PCShellGetContext()
5424b9ad928SBarry Smith .  xin - input vector
5434b9ad928SBarry Smith -  xout - output vector
5444b9ad928SBarry Smith 
54595452b02SPatrick Sanan    Notes:
54695452b02SPatrick Sanan     the function MUST return an error code of 0 on success and nonzero on failure.
5474aa34b0aSBarry Smith 
5484b9ad928SBarry Smith    Level: developer
5494b9ad928SBarry Smith 
550c2e3fba1SPatrick Sanan .seealso: `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetContext()`, `PCShellSetApplyBA()`, `PCShellSetApplySymmetricRight()`, `PCShellSetApplySymmetricLeft()`
5514b9ad928SBarry Smith @*/
5527087cfbeSBarry Smith PetscErrorCode  PCShellSetApply(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec))
5534b9ad928SBarry Smith {
5544b9ad928SBarry Smith   PetscFunctionBegin;
5550700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
556cac4c232SBarry Smith   PetscTryMethod(pc,"PCShellSetApply_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,apply));
5574b9ad928SBarry Smith   PetscFunctionReturn(0);
5584b9ad928SBarry Smith }
5594b9ad928SBarry Smith 
5601b581b66SBarry Smith /*@C
5616437efc7SEric Chamberland    PCShellSetMatApply - Sets routine to use as preconditioner on a block of vectors.
5627b6e2003SPierre Jolivet 
5637b6e2003SPierre Jolivet    Logically Collective on PC
5647b6e2003SPierre Jolivet 
5657b6e2003SPierre Jolivet    Input Parameters:
5667b6e2003SPierre Jolivet +  pc - the preconditioner context
5677b6e2003SPierre Jolivet -  apply - the application-provided preconditioning routine
5687b6e2003SPierre Jolivet 
5697b6e2003SPierre Jolivet    Calling sequence of apply:
5707b6e2003SPierre Jolivet .vb
5717b6e2003SPierre Jolivet    PetscErrorCode apply (PC pc,Mat Xin,Mat Xout)
5727b6e2003SPierre Jolivet .ve
5737b6e2003SPierre Jolivet 
5747b6e2003SPierre Jolivet +  pc - the preconditioner, get the application context with PCShellGetContext()
5757b6e2003SPierre Jolivet .  Xin - input block of vectors
5767b6e2003SPierre Jolivet -  Xout - output block of vectors
5777b6e2003SPierre Jolivet 
5787b6e2003SPierre Jolivet    Notes:
5797b6e2003SPierre Jolivet     the function MUST return an error code of 0 on success and nonzero on failure.
5807b6e2003SPierre Jolivet 
5817b6e2003SPierre Jolivet    Level: developer
5827b6e2003SPierre Jolivet 
583db781477SPatrick Sanan .seealso: `PCShellSetApply()`
5847b6e2003SPierre Jolivet @*/
5857b6e2003SPierre Jolivet PetscErrorCode  PCShellSetMatApply(PC pc,PetscErrorCode (*matapply)(PC,Mat,Mat))
5867b6e2003SPierre Jolivet {
5877b6e2003SPierre Jolivet   PetscFunctionBegin;
5887b6e2003SPierre Jolivet   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
589cac4c232SBarry Smith   PetscTryMethod(pc,"PCShellSetMatApply_C",(PC,PetscErrorCode (*)(PC,Mat,Mat)),(pc,matapply));
5907b6e2003SPierre Jolivet   PetscFunctionReturn(0);
5917b6e2003SPierre Jolivet }
5927b6e2003SPierre Jolivet 
5937b6e2003SPierre Jolivet /*@C
5941b581b66SBarry Smith    PCShellSetApplySymmetricLeft - Sets routine to use as left preconditioner (when the PC_SYMMETRIC is used).
5951b581b66SBarry Smith 
5961b581b66SBarry Smith    Logically Collective on PC
5971b581b66SBarry Smith 
5981b581b66SBarry Smith    Input Parameters:
5991b581b66SBarry Smith +  pc - the preconditioner context
6001b581b66SBarry Smith -  apply - the application-provided left preconditioning routine
6011b581b66SBarry Smith 
6021b581b66SBarry Smith    Calling sequence of apply:
6031b581b66SBarry Smith .vb
6041b581b66SBarry Smith    PetscErrorCode apply (PC pc,Vec xin,Vec xout)
6051b581b66SBarry Smith .ve
6061b581b66SBarry Smith 
6071b581b66SBarry Smith +  pc - the preconditioner, get the application context with PCShellGetContext()
6081b581b66SBarry Smith .  xin - input vector
6091b581b66SBarry Smith -  xout - output vector
6101b581b66SBarry Smith 
61195452b02SPatrick Sanan    Notes:
61295452b02SPatrick Sanan     the function MUST return an error code of 0 on success and nonzero on failure.
6131b581b66SBarry Smith 
6141b581b66SBarry Smith    Level: developer
6151b581b66SBarry Smith 
616db781477SPatrick Sanan .seealso: `PCShellSetApply()`, `PCShellSetApplySymmetricLeft()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetContext()`
6171b581b66SBarry Smith @*/
6181b581b66SBarry Smith PetscErrorCode  PCShellSetApplySymmetricLeft(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec))
6191b581b66SBarry Smith {
6201b581b66SBarry Smith   PetscFunctionBegin;
6211b581b66SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
622cac4c232SBarry Smith   PetscTryMethod(pc,"PCShellSetApplySymmetricLeft_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,apply));
6231b581b66SBarry Smith   PetscFunctionReturn(0);
6241b581b66SBarry Smith }
6251b581b66SBarry Smith 
6261b581b66SBarry Smith /*@C
627a4c07401SPatrick Sanan    PCShellSetApplySymmetricRight - Sets routine to use as right preconditioner (when the PC_SYMMETRIC is used).
6281b581b66SBarry Smith 
6291b581b66SBarry Smith    Logically Collective on PC
6301b581b66SBarry Smith 
6311b581b66SBarry Smith    Input Parameters:
6321b581b66SBarry Smith +  pc - the preconditioner context
6331b581b66SBarry Smith -  apply - the application-provided right preconditioning routine
6341b581b66SBarry Smith 
6351b581b66SBarry Smith    Calling sequence of apply:
6361b581b66SBarry Smith .vb
6371b581b66SBarry Smith    PetscErrorCode apply (PC pc,Vec xin,Vec xout)
6381b581b66SBarry Smith .ve
6391b581b66SBarry Smith 
6401b581b66SBarry Smith +  pc - the preconditioner, get the application context with PCShellGetContext()
6411b581b66SBarry Smith .  xin - input vector
6421b581b66SBarry Smith -  xout - output vector
6431b581b66SBarry Smith 
64495452b02SPatrick Sanan    Notes:
64595452b02SPatrick Sanan     the function MUST return an error code of 0 on success and nonzero on failure.
6461b581b66SBarry Smith 
6471b581b66SBarry Smith    Level: developer
6481b581b66SBarry Smith 
649db781477SPatrick Sanan .seealso: `PCShellSetApply()`, `PCShellSetApplySymmetricLeft()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetContext()`
6501b581b66SBarry Smith @*/
6511b581b66SBarry Smith PetscErrorCode  PCShellSetApplySymmetricRight(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec))
6521b581b66SBarry Smith {
6531b581b66SBarry Smith   PetscFunctionBegin;
6541b581b66SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
655cac4c232SBarry Smith   PetscTryMethod(pc,"PCShellSetApplySymmetricRight_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,apply));
6561b581b66SBarry Smith   PetscFunctionReturn(0);
6571b581b66SBarry Smith }
6581b581b66SBarry Smith 
6592bb17772SBarry Smith /*@C
6602bb17772SBarry Smith    PCShellSetApplyBA - Sets routine to use as preconditioner times operator.
6612bb17772SBarry Smith 
6623f9fe445SBarry Smith    Logically Collective on PC
6632bb17772SBarry Smith 
6642bb17772SBarry Smith    Input Parameters:
6652bb17772SBarry Smith +  pc - the preconditioner context
6662bb17772SBarry Smith -  applyBA - the application-provided BA routine
6672bb17772SBarry Smith 
66853a7a830SPatrick Sanan    Calling sequence of applyBA:
6692bb17772SBarry Smith .vb
6706891c3e4SJed Brown    PetscErrorCode applyBA (PC pc,Vec xin,Vec xout)
6712bb17772SBarry Smith .ve
6722bb17772SBarry Smith 
6736891c3e4SJed Brown +  pc - the preconditioner, get the application context with PCShellGetContext()
6742bb17772SBarry Smith .  xin - input vector
6752bb17772SBarry Smith -  xout - output vector
6762bb17772SBarry Smith 
67795452b02SPatrick Sanan    Notes:
67895452b02SPatrick Sanan     the function MUST return an error code of 0 on success and nonzero on failure.
6794aa34b0aSBarry Smith 
6802bb17772SBarry Smith    Level: developer
6812bb17772SBarry Smith 
682db781477SPatrick Sanan .seealso: `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetContext()`, `PCShellSetApply()`
6832bb17772SBarry Smith @*/
6847087cfbeSBarry Smith PetscErrorCode  PCShellSetApplyBA(PC pc,PetscErrorCode (*applyBA)(PC,PCSide,Vec,Vec,Vec))
6852bb17772SBarry Smith {
6862bb17772SBarry Smith   PetscFunctionBegin;
6870700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
688cac4c232SBarry Smith   PetscTryMethod(pc,"PCShellSetApplyBA_C",(PC,PetscErrorCode (*)(PC,PCSide,Vec,Vec,Vec)),(pc,applyBA));
6892bb17772SBarry Smith   PetscFunctionReturn(0);
6902bb17772SBarry Smith }
6912bb17772SBarry Smith 
6924b9ad928SBarry Smith /*@C
6934b9ad928SBarry Smith    PCShellSetApplyTranspose - Sets routine to use as preconditioner transpose.
6944b9ad928SBarry Smith 
6953f9fe445SBarry Smith    Logically Collective on PC
6964b9ad928SBarry Smith 
6974b9ad928SBarry Smith    Input Parameters:
6984b9ad928SBarry Smith +  pc - the preconditioner context
6994b9ad928SBarry Smith -  apply - the application-provided preconditioning transpose routine
7004b9ad928SBarry Smith 
7014b9ad928SBarry Smith    Calling sequence of apply:
7024b9ad928SBarry Smith .vb
7036891c3e4SJed Brown    PetscErrorCode applytranspose (PC pc,Vec xin,Vec xout)
7044b9ad928SBarry Smith .ve
7054b9ad928SBarry Smith 
7066891c3e4SJed Brown +  pc - the preconditioner, get the application context with PCShellGetContext()
7074b9ad928SBarry Smith .  xin - input vector
7084b9ad928SBarry Smith -  xout - output vector
7094b9ad928SBarry Smith 
71095452b02SPatrick Sanan    Notes:
71195452b02SPatrick Sanan     the function MUST return an error code of 0 on success and nonzero on failure.
7124aa34b0aSBarry Smith 
7134b9ad928SBarry Smith    Level: developer
7144b9ad928SBarry Smith 
7154b9ad928SBarry Smith    Notes:
7164b9ad928SBarry Smith    Uses the same context variable as PCShellSetApply().
7174b9ad928SBarry Smith 
718db781477SPatrick Sanan .seealso: `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApply()`, `PCSetContext()`, `PCShellSetApplyBA()`
7194b9ad928SBarry Smith @*/
7207087cfbeSBarry Smith PetscErrorCode  PCShellSetApplyTranspose(PC pc,PetscErrorCode (*applytranspose)(PC,Vec,Vec))
7214b9ad928SBarry Smith {
7224b9ad928SBarry Smith   PetscFunctionBegin;
7230700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
724cac4c232SBarry Smith   PetscTryMethod(pc,"PCShellSetApplyTranspose_C",(PC,PetscErrorCode (*)(PC,Vec,Vec)),(pc,applytranspose));
7254b9ad928SBarry Smith   PetscFunctionReturn(0);
7264b9ad928SBarry Smith }
7274b9ad928SBarry Smith 
7287cdd61b2SBarry Smith /*@C
7297cdd61b2SBarry Smith    PCShellSetPreSolve - Sets routine to apply to the operators/vectors before a KSPSolve() is
7307cdd61b2SBarry Smith       applied. This usually does something like scale the linear system in some application
7317cdd61b2SBarry Smith       specific way.
7327cdd61b2SBarry Smith 
7333f9fe445SBarry Smith    Logically Collective on PC
7347cdd61b2SBarry Smith 
7357cdd61b2SBarry Smith    Input Parameters:
7367cdd61b2SBarry Smith +  pc - the preconditioner context
7377cdd61b2SBarry Smith -  presolve - the application-provided presolve routine
7387cdd61b2SBarry Smith 
7397cdd61b2SBarry Smith    Calling sequence of presolve:
7407cdd61b2SBarry Smith .vb
7416891c3e4SJed Brown    PetscErrorCode presolve (PC,KSP ksp,Vec b,Vec x)
7427cdd61b2SBarry Smith .ve
7437cdd61b2SBarry Smith 
7446891c3e4SJed Brown +  pc - the preconditioner, get the application context with PCShellGetContext()
7457cdd61b2SBarry Smith .  xin - input vector
7467cdd61b2SBarry Smith -  xout - output vector
7477cdd61b2SBarry Smith 
74895452b02SPatrick Sanan    Notes:
74995452b02SPatrick Sanan     the function MUST return an error code of 0 on success and nonzero on failure.
7504aa34b0aSBarry Smith 
7517cdd61b2SBarry Smith    Level: developer
7527cdd61b2SBarry Smith 
753db781477SPatrick Sanan .seealso: `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetPostSolve()`, `PCShellSetContext()`
7547cdd61b2SBarry Smith @*/
7557087cfbeSBarry Smith PetscErrorCode  PCShellSetPreSolve(PC pc,PetscErrorCode (*presolve)(PC,KSP,Vec,Vec))
7567cdd61b2SBarry Smith {
7577cdd61b2SBarry Smith   PetscFunctionBegin;
7580700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
759cac4c232SBarry Smith   PetscTryMethod(pc,"PCShellSetPreSolve_C",(PC,PetscErrorCode (*)(PC,KSP,Vec,Vec)),(pc,presolve));
7607cdd61b2SBarry Smith   PetscFunctionReturn(0);
7617cdd61b2SBarry Smith }
7627cdd61b2SBarry Smith 
7637cdd61b2SBarry Smith /*@C
7647cdd61b2SBarry Smith    PCShellSetPostSolve - Sets routine to apply to the operators/vectors before a KSPSolve() is
7657cdd61b2SBarry Smith       applied. This usually does something like scale the linear system in some application
7667cdd61b2SBarry Smith       specific way.
7677cdd61b2SBarry Smith 
7683f9fe445SBarry Smith    Logically Collective on PC
7697cdd61b2SBarry Smith 
7707cdd61b2SBarry Smith    Input Parameters:
7717cdd61b2SBarry Smith +  pc - the preconditioner context
7727cdd61b2SBarry Smith -  postsolve - the application-provided presolve routine
7737cdd61b2SBarry Smith 
7747cdd61b2SBarry Smith    Calling sequence of postsolve:
7757cdd61b2SBarry Smith .vb
7766891c3e4SJed Brown    PetscErrorCode postsolve(PC,KSP ksp,Vec b,Vec x)
7777cdd61b2SBarry Smith .ve
7787cdd61b2SBarry Smith 
7796891c3e4SJed Brown +  pc - the preconditioner, get the application context with PCShellGetContext()
7807cdd61b2SBarry Smith .  xin - input vector
7817cdd61b2SBarry Smith -  xout - output vector
7827cdd61b2SBarry Smith 
78395452b02SPatrick Sanan    Notes:
78495452b02SPatrick Sanan     the function MUST return an error code of 0 on success and nonzero on failure.
7854aa34b0aSBarry Smith 
7867cdd61b2SBarry Smith    Level: developer
7877cdd61b2SBarry Smith 
788db781477SPatrick Sanan .seealso: `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetPreSolve()`, `PCShellSetContext()`
7897cdd61b2SBarry Smith @*/
7907087cfbeSBarry Smith PetscErrorCode  PCShellSetPostSolve(PC pc,PetscErrorCode (*postsolve)(PC,KSP,Vec,Vec))
7917cdd61b2SBarry Smith {
7927cdd61b2SBarry Smith   PetscFunctionBegin;
7930700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
794cac4c232SBarry Smith   PetscTryMethod(pc,"PCShellSetPostSolve_C",(PC,PetscErrorCode (*)(PC,KSP,Vec,Vec)),(pc,postsolve));
7957cdd61b2SBarry Smith   PetscFunctionReturn(0);
7967cdd61b2SBarry Smith }
7977cdd61b2SBarry Smith 
7984b9ad928SBarry Smith /*@C
7994b9ad928SBarry Smith    PCShellSetName - Sets an optional name to associate with a shell
8004b9ad928SBarry Smith    preconditioner.
8014b9ad928SBarry Smith 
8024b9ad928SBarry Smith    Not Collective
8034b9ad928SBarry Smith 
8044b9ad928SBarry Smith    Input Parameters:
8054b9ad928SBarry Smith +  pc - the preconditioner context
8064b9ad928SBarry Smith -  name - character string describing shell preconditioner
8074b9ad928SBarry Smith 
8084b9ad928SBarry Smith    Level: developer
8094b9ad928SBarry Smith 
810db781477SPatrick Sanan .seealso: `PCShellGetName()`
8114b9ad928SBarry Smith @*/
8127087cfbeSBarry Smith PetscErrorCode  PCShellSetName(PC pc,const char name[])
8134b9ad928SBarry Smith {
8144b9ad928SBarry Smith   PetscFunctionBegin;
8150700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
816cac4c232SBarry Smith   PetscTryMethod(pc,"PCShellSetName_C",(PC,const char []),(pc,name));
8174b9ad928SBarry Smith   PetscFunctionReturn(0);
8184b9ad928SBarry Smith }
8194b9ad928SBarry Smith 
8204b9ad928SBarry Smith /*@C
8214b9ad928SBarry Smith    PCShellGetName - Gets an optional name that the user has set for a shell
8224b9ad928SBarry Smith    preconditioner.
8234b9ad928SBarry Smith 
8244b9ad928SBarry Smith    Not Collective
8254b9ad928SBarry Smith 
8264b9ad928SBarry Smith    Input Parameter:
8274b9ad928SBarry Smith .  pc - the preconditioner context
8284b9ad928SBarry Smith 
8294b9ad928SBarry Smith    Output Parameter:
8304b9ad928SBarry Smith .  name - character string describing shell preconditioner (you should not free this)
8314b9ad928SBarry Smith 
8324b9ad928SBarry Smith    Level: developer
8334b9ad928SBarry Smith 
834db781477SPatrick Sanan .seealso: `PCShellSetName()`
8354b9ad928SBarry Smith @*/
836ccaf0856SBarry Smith PetscErrorCode  PCShellGetName(PC pc,const char *name[])
8374b9ad928SBarry Smith {
8384b9ad928SBarry Smith   PetscFunctionBegin;
8390700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
8404482741eSBarry Smith   PetscValidPointer(name,2);
841cac4c232SBarry Smith   PetscUseMethod(pc,"PCShellGetName_C",(PC,const char*[]),(pc,name));
8424b9ad928SBarry Smith   PetscFunctionReturn(0);
8434b9ad928SBarry Smith }
8444b9ad928SBarry Smith 
8454b9ad928SBarry Smith /*@C
8464b9ad928SBarry Smith    PCShellSetApplyRichardson - Sets routine to use as preconditioner
8474b9ad928SBarry Smith    in Richardson iteration.
8484b9ad928SBarry Smith 
8493f9fe445SBarry Smith    Logically Collective on PC
8504b9ad928SBarry Smith 
8514b9ad928SBarry Smith    Input Parameters:
8524b9ad928SBarry Smith +  pc - the preconditioner context
853be29d3c6SBarry Smith -  apply - the application-provided preconditioning routine
8544b9ad928SBarry Smith 
8554b9ad928SBarry Smith    Calling sequence of apply:
8564b9ad928SBarry Smith .vb
8576891c3e4SJed Brown    PetscErrorCode apply (PC pc,Vec b,Vec x,Vec r,PetscReal rtol,PetscReal abstol,PetscReal dtol,PetscInt maxits)
8584b9ad928SBarry Smith .ve
8594b9ad928SBarry Smith 
8606891c3e4SJed Brown +  pc - the preconditioner, get the application context with PCShellGetContext()
8614b9ad928SBarry Smith .  b - right-hand-side
8624b9ad928SBarry Smith .  x - current iterate
8634b9ad928SBarry Smith .  r - work space
8644b9ad928SBarry Smith .  rtol - relative tolerance of residual norm to stop at
86570441072SBarry Smith .  abstol - absolute tolerance of residual norm to stop at
8664b9ad928SBarry Smith .  dtol - if residual norm increases by this factor than return
8674b9ad928SBarry Smith -  maxits - number of iterations to run
8684b9ad928SBarry Smith 
86995452b02SPatrick Sanan    Notes:
87095452b02SPatrick Sanan     the function MUST return an error code of 0 on success and nonzero on failure.
8714aa34b0aSBarry Smith 
8724b9ad928SBarry Smith    Level: developer
8734b9ad928SBarry Smith 
874db781477SPatrick Sanan .seealso: `PCShellSetApply()`, `PCShellSetContext()`
8754b9ad928SBarry Smith @*/
8767087cfbeSBarry Smith PetscErrorCode  PCShellSetApplyRichardson(PC pc,PetscErrorCode (*apply)(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool,PetscInt*,PCRichardsonConvergedReason*))
8774b9ad928SBarry Smith {
8784b9ad928SBarry Smith   PetscFunctionBegin;
8790700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
880cac4c232SBarry Smith   PetscTryMethod(pc,"PCShellSetApplyRichardson_C",(PC,PetscErrorCode (*)(PC,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,PetscInt,PetscBool,PetscInt*,PCRichardsonConvergedReason*)),(pc,apply));
8814b9ad928SBarry Smith   PetscFunctionReturn(0);
8824b9ad928SBarry Smith }
8834b9ad928SBarry Smith 
8844b9ad928SBarry Smith /*MC
8854b9ad928SBarry Smith    PCSHELL - Creates a new preconditioner class for use with your
8864b9ad928SBarry Smith               own private data storage format.
8874b9ad928SBarry Smith 
8884b9ad928SBarry Smith    Level: advanced
889e0bb08deSStefano Zampini 
8904b9ad928SBarry Smith   Usage:
8916891c3e4SJed Brown $             extern PetscErrorCode apply(PC,Vec,Vec);
8926891c3e4SJed Brown $             extern PetscErrorCode applyba(PC,PCSide,Vec,Vec,Vec);
8936891c3e4SJed Brown $             extern PetscErrorCode applytranspose(PC,Vec,Vec);
8946891c3e4SJed Brown $             extern PetscErrorCode setup(PC);
8956891c3e4SJed Brown $             extern PetscErrorCode destroy(PC);
8966891c3e4SJed Brown $
8974b9ad928SBarry Smith $             PCCreate(comm,&pc);
8984b9ad928SBarry Smith $             PCSetType(pc,PCSHELL);
899be29d3c6SBarry Smith $             PCShellSetContext(pc,ctx)
9006891c3e4SJed Brown $             PCShellSetApply(pc,apply);
9016891c3e4SJed Brown $             PCShellSetApplyBA(pc,applyba);               (optional)
9026891c3e4SJed Brown $             PCShellSetApplyTranspose(pc,applytranspose); (optional)
9034b9ad928SBarry Smith $             PCShellSetSetUp(pc,setup);                   (optional)
904d01c8aa3SLisandro Dalcin $             PCShellSetDestroy(pc,destroy);               (optional)
9054b9ad928SBarry Smith 
906db781477SPatrick Sanan .seealso: `PCCreate()`, `PCSetType()`, `PCType`, `PC`,
907db781477SPatrick Sanan           `MATSHELL`, `PCShellSetSetUp()`, `PCShellSetApply()`, `PCShellSetView()`,
908db781477SPatrick Sanan           `PCShellSetApplyTranspose()`, `PCShellSetName()`, `PCShellSetApplyRichardson()`,
909db781477SPatrick Sanan           `PCShellGetName()`, `PCShellSetContext()`, `PCShellGetContext()`, `PCShellSetApplyBA()`
9104b9ad928SBarry Smith M*/
9114b9ad928SBarry Smith 
9128cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PCCreate_Shell(PC pc)
9134b9ad928SBarry Smith {
9144b9ad928SBarry Smith   PC_Shell       *shell;
9154b9ad928SBarry Smith 
9164b9ad928SBarry Smith   PetscFunctionBegin;
9179566063dSJacob Faibussowitsch   PetscCall(PetscNewLog(pc,&shell));
9184b9ad928SBarry Smith   pc->data = (void*)shell;
9194b9ad928SBarry Smith 
920d01c8aa3SLisandro Dalcin   pc->ops->destroy         = PCDestroy_Shell;
9214b9ad928SBarry Smith   pc->ops->view            = PCView_Shell;
922d01c8aa3SLisandro Dalcin   pc->ops->apply           = PCApply_Shell;
9231b581b66SBarry Smith   pc->ops->applysymmetricleft  = PCApplySymmetricLeft_Shell;
9241b581b66SBarry Smith   pc->ops->applysymmetricright = PCApplySymmetricRight_Shell;
925*0e0fe96bSStefano Zampini   pc->ops->matapply        = NULL;
9260a545947SLisandro Dalcin   pc->ops->applytranspose  = NULL;
9270a545947SLisandro Dalcin   pc->ops->applyrichardson = NULL;
9280a545947SLisandro Dalcin   pc->ops->setup           = NULL;
9290a545947SLisandro Dalcin   pc->ops->presolve        = NULL;
9300a545947SLisandro Dalcin   pc->ops->postsolve       = NULL;
9314b9ad928SBarry Smith 
9320a545947SLisandro Dalcin   shell->apply          = NULL;
9330a545947SLisandro Dalcin   shell->applytranspose = NULL;
9340a545947SLisandro Dalcin   shell->name           = NULL;
9350a545947SLisandro Dalcin   shell->applyrich      = NULL;
9360a545947SLisandro Dalcin   shell->presolve       = NULL;
9370a545947SLisandro Dalcin   shell->postsolve      = NULL;
9380a545947SLisandro Dalcin   shell->ctx            = NULL;
9390a545947SLisandro Dalcin   shell->setup          = NULL;
9400a545947SLisandro Dalcin   shell->view           = NULL;
9410a545947SLisandro Dalcin   shell->destroy        = NULL;
9420a545947SLisandro Dalcin   shell->applysymmetricleft  = NULL;
9430a545947SLisandro Dalcin   shell->applysymmetricright = NULL;
9444b9ad928SBarry Smith 
9459566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetDestroy_C",PCShellSetDestroy_Shell));
9469566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetSetUp_C",PCShellSetSetUp_Shell));
9479566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApply_C",PCShellSetApply_Shell));
9489566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetMatApply_C",PCShellSetMatApply_Shell));
9499566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplySymmetricLeft_C",PCShellSetApplySymmetricLeft_Shell));
9509566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplySymmetricRight_C",PCShellSetApplySymmetricRight_Shell));
9519566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyBA_C",PCShellSetApplyBA_Shell));
9529566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetPreSolve_C",PCShellSetPreSolve_Shell));
9539566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetPostSolve_C",PCShellSetPostSolve_Shell));
9549566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetView_C",PCShellSetView_Shell));
9559566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyTranspose_C",PCShellSetApplyTranspose_Shell));
9569566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetName_C",PCShellSetName_Shell));
9579566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellGetName_C",PCShellGetName_Shell));
9589566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc,"PCShellSetApplyRichardson_C",PCShellSetApplyRichardson_Shell));
9594b9ad928SBarry Smith   PetscFunctionReturn(0);
9604b9ad928SBarry Smith }
961