xref: /petsc/src/ksp/pc/impls/shell/shellpc.c (revision 20f4b53cbb5e9bd9ef12b76a8697d60d197cda17)
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;
603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
61be29d3c6SBarry Smith }
62be29d3c6SBarry Smith 
639dd1005fSJed Brown /*@
64f1580f4eSBarry Smith     PCShellSetContext - sets the context for a shell `PC`
65be29d3c6SBarry Smith 
66c3339decSBarry 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;
893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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));
993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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));
1143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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));
1293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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));
1393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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));
1493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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));
1643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1652bb17772SBarry Smith }
1662bb17772SBarry Smith 
167d71ae5a4SJacob Faibussowitsch static PetscErrorCode PCPreSolveChangeRHS_Shell(PC pc, PetscBool *change)
168d71ae5a4SJacob Faibussowitsch {
169a06fd7f2SStefano Zampini   PetscFunctionBegin;
170a06fd7f2SStefano Zampini   *change = PETSC_TRUE;
1713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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));
1813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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));
1913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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));
2063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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));
2213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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));
2473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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   }
2663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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;
2753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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;
2863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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;
2953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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;
3063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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;
3153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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;
3243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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;
3353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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   }
3513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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;
3623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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;
3713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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;
3823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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;
3933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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));
4033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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;
4123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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 
419c3339decSBarry 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 
425*20f4b53cSBarry Smith    Calling sequence of `destroy`:
426*20f4b53cSBarry Smith $  PetscErrorCode destroy(PC pc)
427*20f4b53cSBarry Smith .  pc - the preconditioner, get the application context with `PCShellGetContext()`
4284aa34b0aSBarry Smith 
429f1580f4eSBarry Smith    Level: intermediate
43018be62a5SSatish Balay 
431f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApply()`, `PCShellSetContext()`
43218be62a5SSatish Balay @*/
433d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetDestroy(PC pc, PetscErrorCode (*destroy)(PC))
434d71ae5a4SJacob Faibussowitsch {
43518be62a5SSatish Balay   PetscFunctionBegin;
4360700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
437cac4c232SBarry Smith   PetscTryMethod(pc, "PCShellSetDestroy_C", (PC, PetscErrorCode(*)(PC)), (pc, destroy));
4383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
43918be62a5SSatish Balay }
44018be62a5SSatish Balay 
4414b9ad928SBarry Smith /*@C
4424b9ad928SBarry Smith    PCShellSetSetUp - Sets routine to use to "setup" the preconditioner whenever the
4434b9ad928SBarry Smith    matrix operator is changed.
4444b9ad928SBarry Smith 
445c3339decSBarry Smith    Logically Collective
4464b9ad928SBarry Smith 
4474b9ad928SBarry Smith    Input Parameters:
4484b9ad928SBarry Smith +  pc - the preconditioner context
449a2b725a8SWilliam Gropp -  setup - the application-provided setup routine
4504b9ad928SBarry Smith 
451*20f4b53cSBarry Smith    Calling sequence of `setup`:
452*20f4b53cSBarry Smith $   PetscErrorCode setup(PC pc)
453*20f4b53cSBarry Smith .  pc - the preconditioner, get the application context with `PCShellGetContext()`
4544b9ad928SBarry Smith 
455f1580f4eSBarry Smith    Note:
45695452b02SPatrick Sanan     the function MUST return an error code of 0 on success and nonzero on failure.
4574aa34b0aSBarry Smith 
458f1580f4eSBarry Smith    Level: intermediate
4594b9ad928SBarry Smith 
460f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApplyRichardson()`, `PCShellSetApply()`, `PCShellSetContext()`
4614b9ad928SBarry Smith @*/
462d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetSetUp(PC pc, PetscErrorCode (*setup)(PC))
463d71ae5a4SJacob Faibussowitsch {
4644b9ad928SBarry Smith   PetscFunctionBegin;
4650700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
466cac4c232SBarry Smith   PetscTryMethod(pc, "PCShellSetSetUp_C", (PC, PetscErrorCode(*)(PC)), (pc, setup));
4673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4684b9ad928SBarry Smith }
4694b9ad928SBarry Smith 
4704b9ad928SBarry Smith /*@C
4714b9ad928SBarry Smith    PCShellSetView - Sets routine to use as viewer of shell preconditioner
4724b9ad928SBarry Smith 
473c3339decSBarry Smith    Logically Collective
4744b9ad928SBarry Smith 
4754b9ad928SBarry Smith    Input Parameters:
4764b9ad928SBarry Smith +  pc - the preconditioner context
4774b9ad928SBarry Smith -  view - the application-provided view routine
4784b9ad928SBarry Smith 
479*20f4b53cSBarry Smith    Calling sequence of `view`:
4804b9ad928SBarry Smith .vb
4816891c3e4SJed Brown    PetscErrorCode view(PC pc, PetscViewer v)
4824b9ad928SBarry Smith .ve
483*20f4b53cSBarry Smith +  pc - the preconditioner, get the application context with `PCShellGetContext()`
4844b9ad928SBarry Smith -  v   - viewer
4854b9ad928SBarry Smith 
486f1580f4eSBarry Smith    Level: advanced
4874b9ad928SBarry Smith 
488f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`
4894b9ad928SBarry Smith @*/
490d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetView(PC pc, PetscErrorCode (*view)(PC, PetscViewer))
491d71ae5a4SJacob Faibussowitsch {
4924b9ad928SBarry Smith   PetscFunctionBegin;
4930700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
494cac4c232SBarry Smith   PetscTryMethod(pc, "PCShellSetView_C", (PC, PetscErrorCode(*)(PC, PetscViewer)), (pc, view));
4953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4964b9ad928SBarry Smith }
4974b9ad928SBarry Smith 
4984b9ad928SBarry Smith /*@C
4994b9ad928SBarry Smith    PCShellSetApply - Sets routine to use as preconditioner.
5004b9ad928SBarry Smith 
501c3339decSBarry Smith    Logically Collective
5024b9ad928SBarry Smith 
5034b9ad928SBarry Smith    Input Parameters:
5044b9ad928SBarry Smith +  pc - the preconditioner context
505be29d3c6SBarry Smith -  apply - the application-provided preconditioning routine
5064b9ad928SBarry Smith 
507*20f4b53cSBarry Smith    Calling sequence of `apply`:
5084b9ad928SBarry Smith .vb
5096891c3e4SJed Brown    PetscErrorCode apply(PC pc, Vec xin, Vec xout)
5104b9ad928SBarry Smith .ve
511*20f4b53cSBarry Smith +  pc - the preconditioner, get the application context with `PCShellGetContext()`
5124b9ad928SBarry Smith .  xin - input vector
5134b9ad928SBarry Smith -  xout - output vector
5144b9ad928SBarry Smith 
515f1580f4eSBarry Smith    Level: intermediate
5164b9ad928SBarry Smith 
517f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetContext()`, `PCShellSetApplyBA()`, `PCShellSetApplySymmetricRight()`, `PCShellSetApplySymmetricLeft()`
5184b9ad928SBarry Smith @*/
519d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetApply(PC pc, PetscErrorCode (*apply)(PC, Vec, Vec))
520d71ae5a4SJacob Faibussowitsch {
5214b9ad928SBarry Smith   PetscFunctionBegin;
5220700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
523cac4c232SBarry Smith   PetscTryMethod(pc, "PCShellSetApply_C", (PC, PetscErrorCode(*)(PC, Vec, Vec)), (pc, apply));
5243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5254b9ad928SBarry Smith }
5264b9ad928SBarry Smith 
5271b581b66SBarry Smith /*@C
5286437efc7SEric Chamberland    PCShellSetMatApply - Sets routine to use as preconditioner on a block of vectors.
5297b6e2003SPierre Jolivet 
530c3339decSBarry Smith    Logically Collective
5317b6e2003SPierre Jolivet 
5327b6e2003SPierre Jolivet    Input Parameters:
5337b6e2003SPierre Jolivet +  pc - the preconditioner context
5347b6e2003SPierre Jolivet -  apply - the application-provided preconditioning routine
5357b6e2003SPierre Jolivet 
536*20f4b53cSBarry Smith    Calling sequence of `apply`:
5377b6e2003SPierre Jolivet .vb
5387b6e2003SPierre Jolivet    PetscErrorCode apply(PC pc, Mat Xin, Mat Xout)
5397b6e2003SPierre Jolivet .ve
540*20f4b53cSBarry Smith +  pc - the preconditioner, get the application context with `PCShellGetContext()`
5417b6e2003SPierre Jolivet .  Xin - input block of vectors
5427b6e2003SPierre Jolivet -  Xout - output block of vectors
5437b6e2003SPierre Jolivet 
544f1580f4eSBarry Smith    Level: advanced
5457b6e2003SPierre Jolivet 
546f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApply()`
5477b6e2003SPierre Jolivet @*/
548d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetMatApply(PC pc, PetscErrorCode (*matapply)(PC, Mat, Mat))
549d71ae5a4SJacob Faibussowitsch {
5507b6e2003SPierre Jolivet   PetscFunctionBegin;
5517b6e2003SPierre Jolivet   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
552cac4c232SBarry Smith   PetscTryMethod(pc, "PCShellSetMatApply_C", (PC, PetscErrorCode(*)(PC, Mat, Mat)), (pc, matapply));
5533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5547b6e2003SPierre Jolivet }
5557b6e2003SPierre Jolivet 
5567b6e2003SPierre Jolivet /*@C
557f1580f4eSBarry Smith    PCShellSetApplySymmetricLeft - Sets routine to use as left preconditioner (when the `PC_SYMMETRIC` is used).
5581b581b66SBarry Smith 
559c3339decSBarry Smith    Logically Collective
5601b581b66SBarry Smith 
5611b581b66SBarry Smith    Input Parameters:
5621b581b66SBarry Smith +  pc - the preconditioner context
5631b581b66SBarry Smith -  apply - the application-provided left preconditioning routine
5641b581b66SBarry Smith 
565*20f4b53cSBarry Smith    Calling sequence of `apply`:
5661b581b66SBarry Smith .vb
5671b581b66SBarry Smith    PetscErrorCode apply(PC pc, Vec xin, Vec xout)
5681b581b66SBarry Smith .ve
569f1580f4eSBarry Smith +  pc - the preconditioner, get the application context with `PCShellGetContext()`
5701b581b66SBarry Smith .  xin - input vector
5711b581b66SBarry Smith -  xout - output vector
5721b581b66SBarry Smith 
573f1580f4eSBarry Smith    Level: advanced
5741b581b66SBarry Smith 
575f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApply()`, `PCShellSetApplySymmetricLeft()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetContext()`
5761b581b66SBarry Smith @*/
577d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetApplySymmetricLeft(PC pc, PetscErrorCode (*apply)(PC, Vec, Vec))
578d71ae5a4SJacob Faibussowitsch {
5791b581b66SBarry Smith   PetscFunctionBegin;
5801b581b66SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
581cac4c232SBarry Smith   PetscTryMethod(pc, "PCShellSetApplySymmetricLeft_C", (PC, PetscErrorCode(*)(PC, Vec, Vec)), (pc, apply));
5823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5831b581b66SBarry Smith }
5841b581b66SBarry Smith 
5851b581b66SBarry Smith /*@C
586f1580f4eSBarry Smith    PCShellSetApplySymmetricRight - Sets routine to use as right preconditioner (when the `PC_SYMMETRIC` is used).
5871b581b66SBarry Smith 
588c3339decSBarry Smith    Logically Collective
5891b581b66SBarry Smith 
5901b581b66SBarry Smith    Input Parameters:
5911b581b66SBarry Smith +  pc - the preconditioner context
5921b581b66SBarry Smith -  apply - the application-provided right preconditioning routine
5931b581b66SBarry Smith 
594*20f4b53cSBarry Smith    Calling sequence of `apply`:
5951b581b66SBarry Smith .vb
5961b581b66SBarry Smith    PetscErrorCode apply(PC pc, Vec xin, Vec xout)
5971b581b66SBarry Smith .ve
5981b581b66SBarry Smith +  pc - the preconditioner, get the application context with PCShellGetContext()
5991b581b66SBarry Smith .  xin - input vector
6001b581b66SBarry Smith -  xout - output vector
6011b581b66SBarry Smith 
602f1580f4eSBarry Smith    Level: advanced
6031b581b66SBarry Smith 
604f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApply()`, `PCShellSetApplySymmetricLeft()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetContext()`
6051b581b66SBarry Smith @*/
606d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetApplySymmetricRight(PC pc, PetscErrorCode (*apply)(PC, Vec, Vec))
607d71ae5a4SJacob Faibussowitsch {
6081b581b66SBarry Smith   PetscFunctionBegin;
6091b581b66SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
610cac4c232SBarry Smith   PetscTryMethod(pc, "PCShellSetApplySymmetricRight_C", (PC, PetscErrorCode(*)(PC, Vec, Vec)), (pc, apply));
6113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6121b581b66SBarry Smith }
6131b581b66SBarry Smith 
6142bb17772SBarry Smith /*@C
6152bb17772SBarry Smith    PCShellSetApplyBA - Sets routine to use as preconditioner times operator.
6162bb17772SBarry Smith 
617c3339decSBarry Smith    Logically Collective
6182bb17772SBarry Smith 
6192bb17772SBarry Smith    Input Parameters:
6202bb17772SBarry Smith +  pc - the preconditioner context
6212bb17772SBarry Smith -  applyBA - the application-provided BA routine
6222bb17772SBarry Smith 
623*20f4b53cSBarry Smith    Calling sequence of `applyBA`:
6242bb17772SBarry Smith .vb
6256891c3e4SJed Brown    PetscErrorCode applyBA(PC pc, Vec xin, Vec xout)
6262bb17772SBarry Smith .ve
627*20f4b53cSBarry Smith +  pc - the preconditioner, get the application context with `PCShellGetContext()`
6282bb17772SBarry Smith .  xin - input vector
6292bb17772SBarry Smith -  xout - output vector
6302bb17772SBarry Smith 
631f1580f4eSBarry Smith    Level: intermediate
6322bb17772SBarry Smith 
633f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetContext()`, `PCShellSetApply()`
6342bb17772SBarry Smith @*/
635d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetApplyBA(PC pc, PetscErrorCode (*applyBA)(PC, PCSide, Vec, Vec, Vec))
636d71ae5a4SJacob Faibussowitsch {
6372bb17772SBarry Smith   PetscFunctionBegin;
6380700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
639cac4c232SBarry Smith   PetscTryMethod(pc, "PCShellSetApplyBA_C", (PC, PetscErrorCode(*)(PC, PCSide, Vec, Vec, Vec)), (pc, applyBA));
6403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6412bb17772SBarry Smith }
6422bb17772SBarry Smith 
6434b9ad928SBarry Smith /*@C
6444b9ad928SBarry Smith    PCShellSetApplyTranspose - Sets routine to use as preconditioner transpose.
6454b9ad928SBarry Smith 
646c3339decSBarry Smith    Logically Collective
6474b9ad928SBarry Smith 
6484b9ad928SBarry Smith    Input Parameters:
6494b9ad928SBarry Smith +  pc - the preconditioner context
650*20f4b53cSBarry Smith -  applytranspose - the application-provided preconditioning transpose routine
6514b9ad928SBarry Smith 
652*20f4b53cSBarry Smith    Calling sequence of `applytranspose`:
6534b9ad928SBarry Smith .vb
6546891c3e4SJed Brown    PetscErrorCode applytranspose(PC pc, Vec xin, Vec xout)
6554b9ad928SBarry Smith .ve
656*20f4b53cSBarry Smith +  pc - the preconditioner, get the application context with `PCShellGetContext()`
6574b9ad928SBarry Smith .  xin - input vector
6584b9ad928SBarry Smith -  xout - output vector
6594b9ad928SBarry Smith 
660f1580f4eSBarry Smith    Level: intermediate
6614b9ad928SBarry Smith 
662f1580f4eSBarry Smith    Note:
663f1580f4eSBarry Smith    Uses the same context variable as `PCShellSetApply()`.
6644b9ad928SBarry Smith 
665f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApply()`, `PCSetContext()`, `PCShellSetApplyBA()`
6664b9ad928SBarry Smith @*/
667d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetApplyTranspose(PC pc, PetscErrorCode (*applytranspose)(PC, Vec, Vec))
668d71ae5a4SJacob Faibussowitsch {
6694b9ad928SBarry Smith   PetscFunctionBegin;
6700700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
671cac4c232SBarry Smith   PetscTryMethod(pc, "PCShellSetApplyTranspose_C", (PC, PetscErrorCode(*)(PC, Vec, Vec)), (pc, applytranspose));
6723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6734b9ad928SBarry Smith }
6744b9ad928SBarry Smith 
6757cdd61b2SBarry Smith /*@C
676f1580f4eSBarry Smith    PCShellSetPreSolve - Sets routine to apply to the operators/vectors before a `KSPSolve()` is
6777cdd61b2SBarry Smith       applied. This usually does something like scale the linear system in some application
6787cdd61b2SBarry Smith       specific way.
6797cdd61b2SBarry Smith 
680c3339decSBarry Smith    Logically Collective
6817cdd61b2SBarry Smith 
6827cdd61b2SBarry Smith    Input Parameters:
6837cdd61b2SBarry Smith +  pc - the preconditioner context
6847cdd61b2SBarry Smith -  presolve - the application-provided presolve routine
6857cdd61b2SBarry Smith 
686*20f4b53cSBarry Smith    Calling sequence of `presolve`:
6877cdd61b2SBarry Smith .vb
688*20f4b53cSBarry Smith    PetscErrorCode presolve(PC pc, KSP ksp, Vec b, Vec x)
6897cdd61b2SBarry Smith .ve
690*20f4b53cSBarry Smith +  pc - the preconditioner, get the application context with `PCShellGetContext()`
6917cdd61b2SBarry Smith .  xin - input vector
6927cdd61b2SBarry Smith -  xout - output vector
6937cdd61b2SBarry Smith 
694f1580f4eSBarry Smith    Level: advanced
6957cdd61b2SBarry Smith 
696f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetPostSolve()`, `PCShellSetContext()`
6977cdd61b2SBarry Smith @*/
698d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetPreSolve(PC pc, PetscErrorCode (*presolve)(PC, KSP, Vec, Vec))
699d71ae5a4SJacob Faibussowitsch {
7007cdd61b2SBarry Smith   PetscFunctionBegin;
7010700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
702cac4c232SBarry Smith   PetscTryMethod(pc, "PCShellSetPreSolve_C", (PC, PetscErrorCode(*)(PC, KSP, Vec, Vec)), (pc, presolve));
7033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7047cdd61b2SBarry Smith }
7057cdd61b2SBarry Smith 
7067cdd61b2SBarry Smith /*@C
707f1580f4eSBarry Smith    PCShellSetPostSolve - Sets routine to apply to the operators/vectors before a `KSPSolve()` is
7087cdd61b2SBarry Smith       applied. This usually does something like scale the linear system in some application
7097cdd61b2SBarry Smith       specific way.
7107cdd61b2SBarry Smith 
711c3339decSBarry Smith    Logically Collective
7127cdd61b2SBarry Smith 
7137cdd61b2SBarry Smith    Input Parameters:
7147cdd61b2SBarry Smith +  pc - the preconditioner context
7157cdd61b2SBarry Smith -  postsolve - the application-provided presolve routine
7167cdd61b2SBarry Smith 
717*20f4b53cSBarry Smith    Calling sequence of `postsolve`:
7187cdd61b2SBarry Smith .vb
719*20f4b53cSBarry Smith    PetscErrorCode postsolve(PC pc, KSP ksp, Vec b, Vec x)
7207cdd61b2SBarry Smith .ve
721f1580f4eSBarry Smith +  pc - the preconditioner, get the application context with `PCShellGetContext()`
7227cdd61b2SBarry Smith .  xin - input vector
7237cdd61b2SBarry Smith -  xout - output vector
7247cdd61b2SBarry Smith 
725f1580f4eSBarry Smith    Level: advanced
7267cdd61b2SBarry Smith 
727f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetApplyRichardson()`, `PCShellSetSetUp()`, `PCShellSetApplyTranspose()`, `PCShellSetPreSolve()`, `PCShellSetContext()`
7287cdd61b2SBarry Smith @*/
729d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetPostSolve(PC pc, PetscErrorCode (*postsolve)(PC, KSP, Vec, Vec))
730d71ae5a4SJacob Faibussowitsch {
7317cdd61b2SBarry Smith   PetscFunctionBegin;
7320700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
733cac4c232SBarry Smith   PetscTryMethod(pc, "PCShellSetPostSolve_C", (PC, PetscErrorCode(*)(PC, KSP, Vec, Vec)), (pc, postsolve));
7343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7357cdd61b2SBarry Smith }
7367cdd61b2SBarry Smith 
7374b9ad928SBarry Smith /*@C
738f1580f4eSBarry Smith    PCShellSetName - Sets an optional name to associate with a `PCSHELL`
7394b9ad928SBarry Smith    preconditioner.
7404b9ad928SBarry Smith 
7414b9ad928SBarry Smith    Not Collective
7424b9ad928SBarry Smith 
7434b9ad928SBarry Smith    Input Parameters:
7444b9ad928SBarry Smith +  pc - the preconditioner context
7454b9ad928SBarry Smith -  name - character string describing shell preconditioner
7464b9ad928SBarry Smith 
747f1580f4eSBarry Smith    Level: intermediate
7484b9ad928SBarry Smith 
749f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellGetName()`
7504b9ad928SBarry Smith @*/
751d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetName(PC pc, const char name[])
752d71ae5a4SJacob Faibussowitsch {
7534b9ad928SBarry Smith   PetscFunctionBegin;
7540700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
755cac4c232SBarry Smith   PetscTryMethod(pc, "PCShellSetName_C", (PC, const char[]), (pc, name));
7563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7574b9ad928SBarry Smith }
7584b9ad928SBarry Smith 
7594b9ad928SBarry Smith /*@C
760f1580f4eSBarry Smith    PCShellGetName - Gets an optional name that the user has set for a `PCSHELL`
7614b9ad928SBarry Smith    preconditioner.
7624b9ad928SBarry Smith 
7634b9ad928SBarry Smith    Not Collective
7644b9ad928SBarry Smith 
7654b9ad928SBarry Smith    Input Parameter:
7664b9ad928SBarry Smith .  pc - the preconditioner context
7674b9ad928SBarry Smith 
7684b9ad928SBarry Smith    Output Parameter:
7694b9ad928SBarry Smith .  name - character string describing shell preconditioner (you should not free this)
7704b9ad928SBarry Smith 
771f1580f4eSBarry Smith    Level: intermediate
7724b9ad928SBarry Smith 
773f1580f4eSBarry Smith .seealso: `PCSHELL`, `PCShellSetName()`
7744b9ad928SBarry Smith @*/
775d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellGetName(PC pc, const char *name[])
776d71ae5a4SJacob Faibussowitsch {
7774b9ad928SBarry Smith   PetscFunctionBegin;
7780700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
7794482741eSBarry Smith   PetscValidPointer(name, 2);
780cac4c232SBarry Smith   PetscUseMethod(pc, "PCShellGetName_C", (PC, const char *[]), (pc, name));
7813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7824b9ad928SBarry Smith }
7834b9ad928SBarry Smith 
7844b9ad928SBarry Smith /*@C
7854b9ad928SBarry Smith    PCShellSetApplyRichardson - Sets routine to use as preconditioner
7864b9ad928SBarry Smith    in Richardson iteration.
7874b9ad928SBarry Smith 
788c3339decSBarry Smith    Logically Collective
7894b9ad928SBarry Smith 
7904b9ad928SBarry Smith    Input Parameters:
7914b9ad928SBarry Smith +  pc - the preconditioner context
792be29d3c6SBarry Smith -  apply - the application-provided preconditioning routine
7934b9ad928SBarry Smith 
794*20f4b53cSBarry Smith    Calling sequence of `apply`:
7954b9ad928SBarry Smith .vb
7966891c3e4SJed Brown    PetscErrorCode apply(PC pc, Vec b, Vec x, Vec r, PetscReal rtol, PetscReal abstol, PetscReal dtol, PetscInt maxits)
7974b9ad928SBarry Smith .ve
798*20f4b53cSBarry Smith +  pc - the preconditioner, get the application context with `PCShellGetContext()`
7994b9ad928SBarry Smith .  b - right-hand-side
8004b9ad928SBarry Smith .  x - current iterate
8014b9ad928SBarry Smith .  r - work space
8024b9ad928SBarry Smith .  rtol - relative tolerance of residual norm to stop at
80370441072SBarry Smith .  abstol - absolute tolerance of residual norm to stop at
8044b9ad928SBarry Smith .  dtol - if residual norm increases by this factor than return
8054b9ad928SBarry Smith -  maxits - number of iterations to run
8064b9ad928SBarry Smith 
807f1580f4eSBarry Smith    Level: advanced
8084b9ad928SBarry Smith 
809db781477SPatrick Sanan .seealso: `PCShellSetApply()`, `PCShellSetContext()`
8104b9ad928SBarry Smith @*/
811d71ae5a4SJacob Faibussowitsch PetscErrorCode PCShellSetApplyRichardson(PC pc, PetscErrorCode (*apply)(PC, Vec, Vec, Vec, PetscReal, PetscReal, PetscReal, PetscInt, PetscBool, PetscInt *, PCRichardsonConvergedReason *))
812d71ae5a4SJacob Faibussowitsch {
8134b9ad928SBarry Smith   PetscFunctionBegin;
8140700a824SBarry Smith   PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
815cac4c232SBarry Smith   PetscTryMethod(pc, "PCShellSetApplyRichardson_C", (PC, PetscErrorCode(*)(PC, Vec, Vec, Vec, PetscReal, PetscReal, PetscReal, PetscInt, PetscBool, PetscInt *, PCRichardsonConvergedReason *)), (pc, apply));
8163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8174b9ad928SBarry Smith }
8184b9ad928SBarry Smith 
8194b9ad928SBarry Smith /*MC
820f1580f4eSBarry Smith    PCSHELL - Creates a new preconditioner class for use with a users
821f1580f4eSBarry Smith               own private data storage format and preconditioner application code
8224b9ad928SBarry Smith 
8234b9ad928SBarry Smith    Level: advanced
824e0bb08deSStefano Zampini 
8254b9ad928SBarry Smith   Usage:
826f1580f4eSBarry Smith .vb
827f1580f4eSBarry Smith        extern PetscErrorCode apply(PC,Vec,Vec);
828f1580f4eSBarry Smith        extern PetscErrorCode applyba(PC,PCSide,Vec,Vec,Vec);
829f1580f4eSBarry Smith        extern PetscErrorCode applytranspose(PC,Vec,Vec);
830f1580f4eSBarry Smith        extern PetscErrorCode setup(PC);
831f1580f4eSBarry Smith        extern PetscErrorCode destroy(PC);
832f1580f4eSBarry Smith 
833f1580f4eSBarry Smith        PCCreate(comm,&pc);
834f1580f4eSBarry Smith        PCSetType(pc,PCSHELL);
835f1580f4eSBarry Smith        PCShellSetContext(pc,ctx)
836f1580f4eSBarry Smith        PCShellSetApply(pc,apply);
837f1580f4eSBarry Smith        PCShellSetApplyBA(pc,applyba);               (optional)
838f1580f4eSBarry Smith        PCShellSetApplyTranspose(pc,applytranspose); (optional)
839f1580f4eSBarry Smith        PCShellSetSetUp(pc,setup);                   (optional)
840f1580f4eSBarry Smith        PCShellSetDestroy(pc,destroy);               (optional)
841f1580f4eSBarry Smith .ve
8424b9ad928SBarry Smith 
843db781477SPatrick Sanan .seealso: `PCCreate()`, `PCSetType()`, `PCType`, `PC`,
844f1580f4eSBarry Smith           `MATSHELL`, `PCShellSetSetUp()`, `PCShellSetApply()`, `PCShellSetView()`, `PCShellSetDestroy()`, `PCShellSetPostSolve()`,
845f1580f4eSBarry Smith           `PCShellSetApplyTranspose()`, `PCShellSetName()`, `PCShellSetApplyRichardson()`, `PCShellSetPreSolve()`, `PCShellSetView()`,
846f1580f4eSBarry Smith           `PCShellGetName()`, `PCShellSetContext()`, `PCShellGetContext()`, `PCShellSetApplyBA()`, `MATSHELL`, `PCShellSetMatApply()`,
8474b9ad928SBarry Smith M*/
8484b9ad928SBarry Smith 
849d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PCCreate_Shell(PC pc)
850d71ae5a4SJacob Faibussowitsch {
8514b9ad928SBarry Smith   PC_Shell *shell;
8524b9ad928SBarry Smith 
8534b9ad928SBarry Smith   PetscFunctionBegin;
8544dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&shell));
8554b9ad928SBarry Smith   pc->data = (void *)shell;
8564b9ad928SBarry Smith 
857d01c8aa3SLisandro Dalcin   pc->ops->destroy             = PCDestroy_Shell;
8584b9ad928SBarry Smith   pc->ops->view                = PCView_Shell;
859d01c8aa3SLisandro Dalcin   pc->ops->apply               = PCApply_Shell;
8601b581b66SBarry Smith   pc->ops->applysymmetricleft  = PCApplySymmetricLeft_Shell;
8611b581b66SBarry Smith   pc->ops->applysymmetricright = PCApplySymmetricRight_Shell;
8620e0fe96bSStefano Zampini   pc->ops->matapply            = NULL;
8630a545947SLisandro Dalcin   pc->ops->applytranspose      = NULL;
8640a545947SLisandro Dalcin   pc->ops->applyrichardson     = NULL;
8650a545947SLisandro Dalcin   pc->ops->setup               = NULL;
8660a545947SLisandro Dalcin   pc->ops->presolve            = NULL;
8670a545947SLisandro Dalcin   pc->ops->postsolve           = NULL;
8684b9ad928SBarry Smith 
8690a545947SLisandro Dalcin   shell->apply               = NULL;
8700a545947SLisandro Dalcin   shell->applytranspose      = NULL;
8710a545947SLisandro Dalcin   shell->name                = NULL;
8720a545947SLisandro Dalcin   shell->applyrich           = NULL;
8730a545947SLisandro Dalcin   shell->presolve            = NULL;
8740a545947SLisandro Dalcin   shell->postsolve           = NULL;
8750a545947SLisandro Dalcin   shell->ctx                 = NULL;
8760a545947SLisandro Dalcin   shell->setup               = NULL;
8770a545947SLisandro Dalcin   shell->view                = NULL;
8780a545947SLisandro Dalcin   shell->destroy             = NULL;
8790a545947SLisandro Dalcin   shell->applysymmetricleft  = NULL;
8800a545947SLisandro Dalcin   shell->applysymmetricright = NULL;
8814b9ad928SBarry Smith 
8829566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetDestroy_C", PCShellSetDestroy_Shell));
8839566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetSetUp_C", PCShellSetSetUp_Shell));
8849566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetApply_C", PCShellSetApply_Shell));
8859566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetMatApply_C", PCShellSetMatApply_Shell));
8869566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetApplySymmetricLeft_C", PCShellSetApplySymmetricLeft_Shell));
8879566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetApplySymmetricRight_C", PCShellSetApplySymmetricRight_Shell));
8889566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetApplyBA_C", PCShellSetApplyBA_Shell));
8899566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetPreSolve_C", PCShellSetPreSolve_Shell));
8909566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetPostSolve_C", PCShellSetPostSolve_Shell));
8919566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetView_C", PCShellSetView_Shell));
8929566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetApplyTranspose_C", PCShellSetApplyTranspose_Shell));
8939566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetName_C", PCShellSetName_Shell));
8949566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellGetName_C", PCShellGetName_Shell));
8959566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCShellSetApplyRichardson_C", PCShellSetApplyRichardson_Shell));
8963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8974b9ad928SBarry Smith }
898