xref: /petsc/src/ksp/pc/impls/shell/shellpc.c (revision 6849ba73f22fecb8f92ef896a42e4e8bd4cd6965)
14b9ad928SBarry Smith /*
24b9ad928SBarry Smith    This provides a simple shell for Fortran (and C programmers) to
34b9ad928SBarry Smith   create their own preconditioner without writing much interface code.
44b9ad928SBarry Smith */
54b9ad928SBarry Smith 
64b9ad928SBarry Smith #include "src/ksp/pc/pcimpl.h"        /*I "petscpc.h" I*/
73c94ec11SBarry Smith #include "vecimpl.h"
84b9ad928SBarry Smith 
9ac226902SBarry Smith EXTERN_C_BEGIN
104b9ad928SBarry Smith typedef struct {
114b9ad928SBarry Smith   void *ctx,*ctxrich;    /* user provided contexts for preconditioner */
12*6849ba73SBarry Smith   PetscErrorCode (*setup)(void*);
13*6849ba73SBarry Smith   PetscErrorCode (*apply)(void*,Vec,Vec);
14*6849ba73SBarry Smith   PetscErrorCode (*view)(void*,PetscViewer);
15*6849ba73SBarry Smith   PetscErrorCode (*applytranspose)(void*,Vec,Vec);
16*6849ba73SBarry Smith   PetscErrorCode (*applyrich)(void*,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,int);
174b9ad928SBarry Smith   char *name;
184b9ad928SBarry Smith } PC_Shell;
19ac226902SBarry Smith EXTERN_C_END
204b9ad928SBarry Smith 
214b9ad928SBarry Smith #undef __FUNCT__
224b9ad928SBarry Smith #define __FUNCT__ "PCApply_SetUp"
23*6849ba73SBarry Smith static PetscErrorCode PCSetUp_Shell(PC pc)
244b9ad928SBarry Smith {
254b9ad928SBarry Smith   PC_Shell *shell;
26dfbe8321SBarry Smith   PetscErrorCode ierr;
274b9ad928SBarry Smith 
284b9ad928SBarry Smith   PetscFunctionBegin;
294b9ad928SBarry Smith   shell = (PC_Shell*)pc->data;
304b9ad928SBarry Smith   if (shell->setup) {
314b9ad928SBarry Smith     ierr  = (*shell->setup)(shell->ctx);CHKERRQ(ierr);
324b9ad928SBarry Smith   }
334b9ad928SBarry Smith   PetscFunctionReturn(0);
344b9ad928SBarry Smith }
354b9ad928SBarry Smith 
364b9ad928SBarry Smith #undef __FUNCT__
374b9ad928SBarry Smith #define __FUNCT__ "PCApply_Shell"
38*6849ba73SBarry Smith static PetscErrorCode PCApply_Shell(PC pc,Vec x,Vec y)
394b9ad928SBarry Smith {
404b9ad928SBarry Smith   PC_Shell *shell;
41dfbe8321SBarry Smith   PetscErrorCode ierr;
424b9ad928SBarry Smith 
434b9ad928SBarry Smith   PetscFunctionBegin;
444b9ad928SBarry Smith   shell = (PC_Shell*)pc->data;
454b9ad928SBarry Smith   if (!shell->apply) SETERRQ(1,"No apply() routine provided to Shell PC");
464b9ad928SBarry Smith   ierr  = (*shell->apply)(shell->ctx,x,y);CHKERRQ(ierr);
474b9ad928SBarry Smith   PetscFunctionReturn(0);
484b9ad928SBarry Smith }
494b9ad928SBarry Smith 
504b9ad928SBarry Smith #undef __FUNCT__
514b9ad928SBarry Smith #define __FUNCT__ "PCApplyTranspose_Shell"
52*6849ba73SBarry Smith static PetscErrorCode PCApplyTranspose_Shell(PC pc,Vec x,Vec y)
534b9ad928SBarry Smith {
544b9ad928SBarry Smith   PC_Shell *shell;
55dfbe8321SBarry Smith   PetscErrorCode ierr;
564b9ad928SBarry Smith 
574b9ad928SBarry Smith   PetscFunctionBegin;
584b9ad928SBarry Smith   shell = (PC_Shell*)pc->data;
594b9ad928SBarry Smith   if (!shell->applytranspose) SETERRQ(1,"No applytranspose() routine provided to Shell PC");
604b9ad928SBarry Smith   ierr  = (*shell->applytranspose)(shell->ctx,x,y);CHKERRQ(ierr);
614b9ad928SBarry Smith   PetscFunctionReturn(0);
624b9ad928SBarry Smith }
634b9ad928SBarry Smith 
644b9ad928SBarry Smith #undef __FUNCT__
654b9ad928SBarry Smith #define __FUNCT__ "PCApplyRichardson_Shell"
66*6849ba73SBarry Smith static PetscErrorCode PCApplyRichardson_Shell(PC pc,Vec x,Vec y,Vec w,PetscReal rtol,PetscReal atol, PetscReal dtol,int it)
674b9ad928SBarry Smith {
68dfbe8321SBarry Smith   PetscErrorCode ierr;
694b9ad928SBarry Smith   PC_Shell *shell;
704b9ad928SBarry Smith 
714b9ad928SBarry Smith   PetscFunctionBegin;
724b9ad928SBarry Smith   shell = (PC_Shell*)pc->data;
734b9ad928SBarry Smith   ierr  = (*shell->applyrich)(shell->ctxrich,x,y,w,rtol,atol,dtol,it);CHKERRQ(ierr);
744b9ad928SBarry Smith   PetscFunctionReturn(0);
754b9ad928SBarry Smith }
764b9ad928SBarry Smith 
774b9ad928SBarry Smith #undef __FUNCT__
784b9ad928SBarry Smith #define __FUNCT__ "PCDestroy_Shell"
79*6849ba73SBarry Smith static PetscErrorCode PCDestroy_Shell(PC pc)
804b9ad928SBarry Smith {
814b9ad928SBarry Smith   PC_Shell *shell = (PC_Shell*)pc->data;
82dfbe8321SBarry Smith   PetscErrorCode ierr;
834b9ad928SBarry Smith 
844b9ad928SBarry Smith   PetscFunctionBegin;
854b9ad928SBarry Smith   if (shell->name) {ierr = PetscFree(shell->name);}
864b9ad928SBarry Smith   ierr = PetscFree(shell);CHKERRQ(ierr);
874b9ad928SBarry Smith   PetscFunctionReturn(0);
884b9ad928SBarry Smith }
894b9ad928SBarry Smith 
904b9ad928SBarry Smith #undef __FUNCT__
914b9ad928SBarry Smith #define __FUNCT__ "PCView_Shell"
92*6849ba73SBarry Smith static PetscErrorCode PCView_Shell(PC pc,PetscViewer viewer)
934b9ad928SBarry Smith {
944b9ad928SBarry Smith   PC_Shell   *shell = (PC_Shell*)pc->data;
95dfbe8321SBarry Smith   PetscErrorCode ierr;
9632077d6dSBarry Smith   PetscTruth iascii;
974b9ad928SBarry Smith 
984b9ad928SBarry Smith   PetscFunctionBegin;
9932077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
10032077d6dSBarry Smith   if (iascii) {
1014b9ad928SBarry Smith     if (shell->name) {ierr = PetscViewerASCIIPrintf(viewer,"  Shell: %s\n",shell->name);CHKERRQ(ierr);}
1024b9ad928SBarry Smith     else             {ierr = PetscViewerASCIIPrintf(viewer,"  Shell: no name\n");CHKERRQ(ierr);}
1034b9ad928SBarry Smith   }
1044b9ad928SBarry Smith   if (shell->view) {
1054b9ad928SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1064b9ad928SBarry Smith     ierr  = (*shell->view)(shell->ctx,viewer);CHKERRQ(ierr);
1074b9ad928SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1084b9ad928SBarry Smith   }
1094b9ad928SBarry Smith   PetscFunctionReturn(0);
1104b9ad928SBarry Smith }
1114b9ad928SBarry Smith 
1124b9ad928SBarry Smith /* ------------------------------------------------------------------------------*/
1134b9ad928SBarry Smith EXTERN_C_BEGIN
1144b9ad928SBarry Smith #undef __FUNCT__
1154b9ad928SBarry Smith #define __FUNCT__ "PCShellSetSetUp_Shell"
116*6849ba73SBarry Smith PetscErrorCode PCShellSetSetUp_Shell(PC pc, PetscErrorCode (*setup)(void*))
1174b9ad928SBarry Smith {
1184b9ad928SBarry Smith   PC_Shell *shell;
1194b9ad928SBarry Smith 
1204b9ad928SBarry Smith   PetscFunctionBegin;
1214b9ad928SBarry Smith   shell        = (PC_Shell*)pc->data;
1224b9ad928SBarry Smith   shell->setup = setup;
1234b9ad928SBarry Smith   PetscFunctionReturn(0);
1244b9ad928SBarry Smith }
1254b9ad928SBarry Smith EXTERN_C_END
1264b9ad928SBarry Smith 
1274b9ad928SBarry Smith EXTERN_C_BEGIN
1284b9ad928SBarry Smith #undef __FUNCT__
1294b9ad928SBarry Smith #define __FUNCT__ "PCShellSetApply_Shell"
130*6849ba73SBarry Smith PetscErrorCode PCShellSetApply_Shell(PC pc,PetscErrorCode (*apply)(void*,Vec,Vec),void *ptr)
1314b9ad928SBarry Smith {
1324b9ad928SBarry Smith   PC_Shell *shell;
1334b9ad928SBarry Smith 
1344b9ad928SBarry Smith   PetscFunctionBegin;
1354b9ad928SBarry Smith   shell        = (PC_Shell*)pc->data;
1364b9ad928SBarry Smith   shell->apply = apply;
1374b9ad928SBarry Smith   shell->ctx   = ptr;
1384b9ad928SBarry Smith   PetscFunctionReturn(0);
1394b9ad928SBarry Smith }
1404b9ad928SBarry Smith EXTERN_C_END
1414b9ad928SBarry Smith 
1424b9ad928SBarry Smith EXTERN_C_BEGIN
1434b9ad928SBarry Smith #undef __FUNCT__
1444b9ad928SBarry Smith #define __FUNCT__ "PCShellSetView_Shell"
145*6849ba73SBarry Smith PetscErrorCode PCShellSetView_Shell(PC pc,PetscErrorCode (*view)(void*,PetscViewer))
1464b9ad928SBarry Smith {
1474b9ad928SBarry Smith   PC_Shell *shell;
1484b9ad928SBarry Smith 
1494b9ad928SBarry Smith   PetscFunctionBegin;
1504b9ad928SBarry Smith   shell        = (PC_Shell*)pc->data;
1514b9ad928SBarry Smith   shell->view = view;
1524b9ad928SBarry Smith   PetscFunctionReturn(0);
1534b9ad928SBarry Smith }
1544b9ad928SBarry Smith EXTERN_C_END
1554b9ad928SBarry Smith 
1564b9ad928SBarry Smith EXTERN_C_BEGIN
1574b9ad928SBarry Smith #undef __FUNCT__
1584b9ad928SBarry Smith #define __FUNCT__ "PCShellSetApplyTranspose_Shell"
159*6849ba73SBarry Smith PetscErrorCode PCShellSetApplyTranspose_Shell(PC pc,PetscErrorCode (*applytranspose)(void*,Vec,Vec))
1604b9ad928SBarry Smith {
1614b9ad928SBarry Smith   PC_Shell *shell;
1624b9ad928SBarry Smith 
1634b9ad928SBarry Smith   PetscFunctionBegin;
1644b9ad928SBarry Smith   shell                 = (PC_Shell*)pc->data;
1654b9ad928SBarry Smith   shell->applytranspose = applytranspose;
1664b9ad928SBarry Smith   PetscFunctionReturn(0);
1674b9ad928SBarry Smith }
1684b9ad928SBarry Smith EXTERN_C_END
1694b9ad928SBarry Smith 
1704b9ad928SBarry Smith EXTERN_C_BEGIN
1714b9ad928SBarry Smith #undef __FUNCT__
1724b9ad928SBarry Smith #define __FUNCT__ "PCShellSetName_Shell"
173dfbe8321SBarry Smith PetscErrorCode PCShellSetName_Shell(PC pc,const char name[])
1744b9ad928SBarry Smith {
1754b9ad928SBarry Smith   PC_Shell *shell;
176dfbe8321SBarry Smith   PetscErrorCode ierr;
1774b9ad928SBarry Smith 
1784b9ad928SBarry Smith   PetscFunctionBegin;
1794b9ad928SBarry Smith   shell = (PC_Shell*)pc->data;
1804b9ad928SBarry Smith   ierr  = PetscStrallocpy(name,&shell->name);CHKERRQ(ierr);
1814b9ad928SBarry Smith   PetscFunctionReturn(0);
1824b9ad928SBarry Smith }
1834b9ad928SBarry Smith EXTERN_C_END
1844b9ad928SBarry Smith 
1854b9ad928SBarry Smith EXTERN_C_BEGIN
1864b9ad928SBarry Smith #undef __FUNCT__
1874b9ad928SBarry Smith #define __FUNCT__ "PCShellGetName_Shell"
188dfbe8321SBarry Smith PetscErrorCode PCShellGetName_Shell(PC pc,char *name[])
1894b9ad928SBarry Smith {
1904b9ad928SBarry Smith   PC_Shell *shell;
1914b9ad928SBarry Smith 
1924b9ad928SBarry Smith   PetscFunctionBegin;
1934b9ad928SBarry Smith   shell  = (PC_Shell*)pc->data;
1944b9ad928SBarry Smith   *name  = shell->name;
1954b9ad928SBarry Smith   PetscFunctionReturn(0);
1964b9ad928SBarry Smith }
1974b9ad928SBarry Smith EXTERN_C_END
1984b9ad928SBarry Smith 
1994b9ad928SBarry Smith EXTERN_C_BEGIN
2004b9ad928SBarry Smith #undef __FUNCT__
2014b9ad928SBarry Smith #define __FUNCT__ "PCShellSetApplyRichardson_Shell"
202*6849ba73SBarry Smith PetscErrorCode PCShellSetApplyRichardson_Shell(PC pc,PetscErrorCode (*apply)(void*,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,int),void *ptr)
2034b9ad928SBarry Smith {
2044b9ad928SBarry Smith   PC_Shell *shell;
2054b9ad928SBarry Smith 
2064b9ad928SBarry Smith   PetscFunctionBegin;
2074b9ad928SBarry Smith   shell                     = (PC_Shell*)pc->data;
2084b9ad928SBarry Smith   pc->ops->applyrichardson  = PCApplyRichardson_Shell;
2094b9ad928SBarry Smith   shell->applyrich          = apply;
2104b9ad928SBarry Smith   shell->ctxrich            = ptr;
2114b9ad928SBarry Smith   PetscFunctionReturn(0);
2124b9ad928SBarry Smith }
2134b9ad928SBarry Smith EXTERN_C_END
2144b9ad928SBarry Smith 
2154b9ad928SBarry Smith /* -------------------------------------------------------------------------------*/
2164b9ad928SBarry Smith 
2174b9ad928SBarry Smith #undef __FUNCT__
2184b9ad928SBarry Smith #define __FUNCT__ "PCShellSetSetUp"
2194b9ad928SBarry Smith /*@C
2204b9ad928SBarry Smith    PCShellSetSetUp - Sets routine to use to "setup" the preconditioner whenever the
2214b9ad928SBarry Smith    matrix operator is changed.
2224b9ad928SBarry Smith 
2234b9ad928SBarry Smith    Collective on PC
2244b9ad928SBarry Smith 
2254b9ad928SBarry Smith    Input Parameters:
2264b9ad928SBarry Smith +  pc - the preconditioner context
2274b9ad928SBarry Smith .  setup - the application-provided setup routine
2284b9ad928SBarry Smith 
2294b9ad928SBarry Smith    Calling sequence of setup:
2304b9ad928SBarry Smith .vb
2314b9ad928SBarry Smith    int setup (void *ptr)
2324b9ad928SBarry Smith .ve
2334b9ad928SBarry Smith 
2344b9ad928SBarry Smith .  ptr - the application context
2354b9ad928SBarry Smith 
2364b9ad928SBarry Smith    Level: developer
2374b9ad928SBarry Smith 
2384b9ad928SBarry Smith .keywords: PC, shell, set, setup, user-provided
2394b9ad928SBarry Smith 
2404b9ad928SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetApply()
2414b9ad928SBarry Smith @*/
242*6849ba73SBarry Smith PetscErrorCode PCShellSetSetUp(PC pc,PetscErrorCode (*setup)(void*))
2434b9ad928SBarry Smith {
244*6849ba73SBarry Smith   PetscErrorCode ierr,(*f)(PC,PetscErrorCode (*)(void*));
2454b9ad928SBarry Smith 
2464b9ad928SBarry Smith   PetscFunctionBegin;
2474482741eSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
2484b9ad928SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)pc,"PCShellSetSetUp_C",(void (**)(void))&f);CHKERRQ(ierr);
2494b9ad928SBarry Smith   if (f) {
2504b9ad928SBarry Smith     ierr = (*f)(pc,setup);CHKERRQ(ierr);
2514b9ad928SBarry Smith   }
2524b9ad928SBarry Smith   PetscFunctionReturn(0);
2534b9ad928SBarry Smith }
2544b9ad928SBarry Smith 
2554b9ad928SBarry Smith 
2564b9ad928SBarry Smith #undef __FUNCT__
2574b9ad928SBarry Smith #define __FUNCT__ "PCShellSetView"
2584b9ad928SBarry Smith /*@C
2594b9ad928SBarry Smith    PCShellSetView - Sets routine to use as viewer of shell preconditioner
2604b9ad928SBarry Smith 
2614b9ad928SBarry Smith    Collective on PC
2624b9ad928SBarry Smith 
2634b9ad928SBarry Smith    Input Parameters:
2644b9ad928SBarry Smith +  pc - the preconditioner context
2654b9ad928SBarry Smith -  view - the application-provided view routine
2664b9ad928SBarry Smith 
2674b9ad928SBarry Smith    Calling sequence of apply:
2684b9ad928SBarry Smith .vb
2694b9ad928SBarry Smith    int view(void *ptr,PetscViewer v)
2704b9ad928SBarry Smith .ve
2714b9ad928SBarry Smith 
2724b9ad928SBarry Smith +  ptr - the application context
2734b9ad928SBarry Smith -  v   - viewer
2744b9ad928SBarry Smith 
2754b9ad928SBarry Smith    Level: developer
2764b9ad928SBarry Smith 
2774b9ad928SBarry Smith .keywords: PC, shell, set, apply, user-provided
2784b9ad928SBarry Smith 
2794b9ad928SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose()
2804b9ad928SBarry Smith @*/
281*6849ba73SBarry Smith PetscErrorCode PCShellSetView(PC pc,PetscErrorCode (*view)(void*,PetscViewer))
2824b9ad928SBarry Smith {
283*6849ba73SBarry Smith   PetscErrorCode ierr,(*f)(PC,PetscErrorCode (*)(void*,PetscViewer));
2844b9ad928SBarry Smith 
2854b9ad928SBarry Smith   PetscFunctionBegin;
2864482741eSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
2874b9ad928SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)pc,"PCShellSetView_C",(void (**)(void))&f);CHKERRQ(ierr);
2884b9ad928SBarry Smith   if (f) {
2894b9ad928SBarry Smith     ierr = (*f)(pc,view);CHKERRQ(ierr);
2904b9ad928SBarry Smith   }
2914b9ad928SBarry Smith   PetscFunctionReturn(0);
2924b9ad928SBarry Smith }
2934b9ad928SBarry Smith 
2944b9ad928SBarry Smith #undef __FUNCT__
2954b9ad928SBarry Smith #define __FUNCT__ "PCShellSetApply"
2964b9ad928SBarry Smith /*@C
2974b9ad928SBarry Smith    PCShellSetApply - Sets routine to use as preconditioner.
2984b9ad928SBarry Smith 
2994b9ad928SBarry Smith    Collective on PC
3004b9ad928SBarry Smith 
3014b9ad928SBarry Smith    Input Parameters:
3024b9ad928SBarry Smith +  pc - the preconditioner context
3034b9ad928SBarry Smith .  apply - the application-provided preconditioning routine
3044b9ad928SBarry Smith -  ptr - pointer to data needed by this routine
3054b9ad928SBarry Smith 
3064b9ad928SBarry Smith    Calling sequence of apply:
3074b9ad928SBarry Smith .vb
3084b9ad928SBarry Smith    int apply (void *ptr,Vec xin,Vec xout)
3094b9ad928SBarry Smith .ve
3104b9ad928SBarry Smith 
3114b9ad928SBarry Smith +  ptr - the application context
3124b9ad928SBarry Smith .  xin - input vector
3134b9ad928SBarry Smith -  xout - output vector
3144b9ad928SBarry Smith 
3154b9ad928SBarry Smith    Level: developer
3164b9ad928SBarry Smith 
3174b9ad928SBarry Smith .keywords: PC, shell, set, apply, user-provided
3184b9ad928SBarry Smith 
3194b9ad928SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose()
3204b9ad928SBarry Smith @*/
321*6849ba73SBarry Smith PetscErrorCode PCShellSetApply(PC pc,PetscErrorCode (*apply)(void*,Vec,Vec),void *ptr)
3224b9ad928SBarry Smith {
323*6849ba73SBarry Smith   PetscErrorCode ierr,(*f)(PC,PetscErrorCode (*)(void*,Vec,Vec),void *);
3244b9ad928SBarry Smith 
3254b9ad928SBarry Smith   PetscFunctionBegin;
3264482741eSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
3274b9ad928SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)pc,"PCShellSetApply_C",(void (**)(void))&f);CHKERRQ(ierr);
3284b9ad928SBarry Smith   if (f) {
3294b9ad928SBarry Smith     ierr = (*f)(pc,apply,ptr);CHKERRQ(ierr);
3304b9ad928SBarry Smith   }
3314b9ad928SBarry Smith   PetscFunctionReturn(0);
3324b9ad928SBarry Smith }
3334b9ad928SBarry Smith 
3344b9ad928SBarry Smith #undef __FUNCT__
3354b9ad928SBarry Smith #define __FUNCT__ "PCShellSetApplyTranspose"
3364b9ad928SBarry Smith /*@C
3374b9ad928SBarry Smith    PCShellSetApplyTranspose - Sets routine to use as preconditioner transpose.
3384b9ad928SBarry Smith 
3394b9ad928SBarry Smith    Collective on PC
3404b9ad928SBarry Smith 
3414b9ad928SBarry Smith    Input Parameters:
3424b9ad928SBarry Smith +  pc - the preconditioner context
3434b9ad928SBarry Smith -  apply - the application-provided preconditioning transpose routine
3444b9ad928SBarry Smith 
3454b9ad928SBarry Smith    Calling sequence of apply:
3464b9ad928SBarry Smith .vb
3474b9ad928SBarry Smith    int applytranspose (void *ptr,Vec xin,Vec xout)
3484b9ad928SBarry Smith .ve
3494b9ad928SBarry Smith 
3504b9ad928SBarry Smith +  ptr - the application context
3514b9ad928SBarry Smith .  xin - input vector
3524b9ad928SBarry Smith -  xout - output vector
3534b9ad928SBarry Smith 
3544b9ad928SBarry Smith    Level: developer
3554b9ad928SBarry Smith 
3564b9ad928SBarry Smith    Notes:
3574b9ad928SBarry Smith    Uses the same context variable as PCShellSetApply().
3584b9ad928SBarry Smith 
3594b9ad928SBarry Smith .keywords: PC, shell, set, apply, user-provided
3604b9ad928SBarry Smith 
3614b9ad928SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApply()
3624b9ad928SBarry Smith @*/
363*6849ba73SBarry Smith PetscErrorCode PCShellSetApplyTranspose(PC pc,PetscErrorCode (*applytranspose)(void*,Vec,Vec))
3644b9ad928SBarry Smith {
365*6849ba73SBarry Smith   PetscErrorCode ierr,(*f)(PC,PetscErrorCode (*)(void*,Vec,Vec));
3664b9ad928SBarry Smith 
3674b9ad928SBarry Smith   PetscFunctionBegin;
3684482741eSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
3694b9ad928SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)pc,"PCShellSetApplyTranspose_C",(void (**)(void))&f);CHKERRQ(ierr);
3704b9ad928SBarry Smith   if (f) {
3714b9ad928SBarry Smith     ierr = (*f)(pc,applytranspose);CHKERRQ(ierr);
3724b9ad928SBarry Smith   }
3734b9ad928SBarry Smith   PetscFunctionReturn(0);
3744b9ad928SBarry Smith }
3754b9ad928SBarry Smith 
3764b9ad928SBarry Smith #undef __FUNCT__
3774b9ad928SBarry Smith #define __FUNCT__ "PCShellSetName"
3784b9ad928SBarry Smith /*@C
3794b9ad928SBarry Smith    PCShellSetName - Sets an optional name to associate with a shell
3804b9ad928SBarry Smith    preconditioner.
3814b9ad928SBarry Smith 
3824b9ad928SBarry Smith    Not Collective
3834b9ad928SBarry Smith 
3844b9ad928SBarry Smith    Input Parameters:
3854b9ad928SBarry Smith +  pc - the preconditioner context
3864b9ad928SBarry Smith -  name - character string describing shell preconditioner
3874b9ad928SBarry Smith 
3884b9ad928SBarry Smith    Level: developer
3894b9ad928SBarry Smith 
3904b9ad928SBarry Smith .keywords: PC, shell, set, name, user-provided
3914b9ad928SBarry Smith 
3924b9ad928SBarry Smith .seealso: PCShellGetName()
3934b9ad928SBarry Smith @*/
394dfbe8321SBarry Smith PetscErrorCode PCShellSetName(PC pc,const char name[])
3954b9ad928SBarry Smith {
396dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(PC,const char []);
3974b9ad928SBarry Smith 
3984b9ad928SBarry Smith   PetscFunctionBegin;
3994482741eSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
4004b9ad928SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)pc,"PCShellSetName_C",(void (**)(void))&f);CHKERRQ(ierr);
4014b9ad928SBarry Smith   if (f) {
4024b9ad928SBarry Smith     ierr = (*f)(pc,name);CHKERRQ(ierr);
4034b9ad928SBarry Smith   }
4044b9ad928SBarry Smith   PetscFunctionReturn(0);
4054b9ad928SBarry Smith }
4064b9ad928SBarry Smith 
4074b9ad928SBarry Smith #undef __FUNCT__
4084b9ad928SBarry Smith #define __FUNCT__ "PCShellGetName"
4094b9ad928SBarry Smith /*@C
4104b9ad928SBarry Smith    PCShellGetName - Gets an optional name that the user has set for a shell
4114b9ad928SBarry Smith    preconditioner.
4124b9ad928SBarry Smith 
4134b9ad928SBarry Smith    Not Collective
4144b9ad928SBarry Smith 
4154b9ad928SBarry Smith    Input Parameter:
4164b9ad928SBarry Smith .  pc - the preconditioner context
4174b9ad928SBarry Smith 
4184b9ad928SBarry Smith    Output Parameter:
4194b9ad928SBarry Smith .  name - character string describing shell preconditioner (you should not free this)
4204b9ad928SBarry Smith 
4214b9ad928SBarry Smith    Level: developer
4224b9ad928SBarry Smith 
4234b9ad928SBarry Smith .keywords: PC, shell, get, name, user-provided
4244b9ad928SBarry Smith 
4254b9ad928SBarry Smith .seealso: PCShellSetName()
4264b9ad928SBarry Smith @*/
427dfbe8321SBarry Smith PetscErrorCode PCShellGetName(PC pc,char *name[])
4284b9ad928SBarry Smith {
429dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(PC,char *[]);
4304b9ad928SBarry Smith 
4314b9ad928SBarry Smith   PetscFunctionBegin;
4324482741eSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
4334482741eSBarry Smith   PetscValidPointer(name,2);
4344b9ad928SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)pc,"PCShellGetName_C",(void (**)(void))&f);CHKERRQ(ierr);
4354b9ad928SBarry Smith   if (f) {
4364b9ad928SBarry Smith     ierr = (*f)(pc,name);CHKERRQ(ierr);
4374b9ad928SBarry Smith   } else {
4384b9ad928SBarry Smith     SETERRQ(1,"Not shell preconditioner, cannot get name");
4394b9ad928SBarry Smith   }
4404b9ad928SBarry Smith   PetscFunctionReturn(0);
4414b9ad928SBarry Smith }
4424b9ad928SBarry Smith 
4434b9ad928SBarry Smith #undef __FUNCT__
4444b9ad928SBarry Smith #define __FUNCT__ "PCShellSetApplyRichardson"
4454b9ad928SBarry Smith /*@C
4464b9ad928SBarry Smith    PCShellSetApplyRichardson - Sets routine to use as preconditioner
4474b9ad928SBarry Smith    in Richardson iteration.
4484b9ad928SBarry Smith 
4494b9ad928SBarry Smith    Collective on PC
4504b9ad928SBarry Smith 
4514b9ad928SBarry Smith    Input Parameters:
4524b9ad928SBarry Smith +  pc - the preconditioner context
4534b9ad928SBarry Smith .  apply - the application-provided preconditioning routine
4544b9ad928SBarry Smith -  ptr - pointer to data needed by this routine
4554b9ad928SBarry Smith 
4564b9ad928SBarry Smith    Calling sequence of apply:
4574b9ad928SBarry Smith .vb
4584b9ad928SBarry Smith    int apply (void *ptr,Vec b,Vec x,Vec r,PetscReal rtol,PetscReal atol,PetscReal dtol,int maxits)
4594b9ad928SBarry Smith .ve
4604b9ad928SBarry Smith 
4614b9ad928SBarry Smith +  ptr - the application context
4624b9ad928SBarry Smith .  b - right-hand-side
4634b9ad928SBarry Smith .  x - current iterate
4644b9ad928SBarry Smith .  r - work space
4654b9ad928SBarry Smith .  rtol - relative tolerance of residual norm to stop at
4664b9ad928SBarry Smith .  atol - absolute tolerance of residual norm to stop at
4674b9ad928SBarry Smith .  dtol - if residual norm increases by this factor than return
4684b9ad928SBarry Smith -  maxits - number of iterations to run
4694b9ad928SBarry Smith 
4704b9ad928SBarry Smith    Level: developer
4714b9ad928SBarry Smith 
4724b9ad928SBarry Smith .keywords: PC, shell, set, apply, Richardson, user-provided
4734b9ad928SBarry Smith 
4744b9ad928SBarry Smith .seealso: PCShellSetApply()
4754b9ad928SBarry Smith @*/
476*6849ba73SBarry Smith PetscErrorCode PCShellSetApplyRichardson(PC pc,PetscErrorCode (*apply)(void*,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,int),void *ptr)
4774b9ad928SBarry Smith {
478*6849ba73SBarry Smith   PetscErrorCode ierr,(*f)(PC,PetscErrorCode (*)(void*,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,int),void *);
4794b9ad928SBarry Smith 
4804b9ad928SBarry Smith   PetscFunctionBegin;
4814482741eSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
4824b9ad928SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)pc,"PCShellSetApplyRichardson_C",(void (**)(void))&f);CHKERRQ(ierr);
4834b9ad928SBarry Smith   if (f) {
4844b9ad928SBarry Smith     ierr = (*f)(pc,apply,ptr);CHKERRQ(ierr);
4854b9ad928SBarry Smith   }
4864b9ad928SBarry Smith   PetscFunctionReturn(0);
4874b9ad928SBarry Smith }
4884b9ad928SBarry Smith 
4894b9ad928SBarry Smith /*MC
4904b9ad928SBarry Smith    PCSHELL - Creates a new preconditioner class for use with your
4914b9ad928SBarry Smith               own private data storage format.
4924b9ad928SBarry Smith 
4934b9ad928SBarry Smith    Level: advanced
4944b9ad928SBarry Smith 
4954b9ad928SBarry Smith    Concepts: providing your own preconditioner
4964b9ad928SBarry Smith 
4974b9ad928SBarry Smith   Usage:
498*6849ba73SBarry Smith $             PetscErrorCode (*mult)(void*,Vec,Vec);
499*6849ba73SBarry Smith $             PetscErrorCode (*setup)(void*);
5004b9ad928SBarry Smith $             PCCreate(comm,&pc);
5014b9ad928SBarry Smith $             PCSetType(pc,PCSHELL);
5024b9ad928SBarry Smith $             PCShellSetApply(pc,mult,ctx);
5034b9ad928SBarry Smith $             PCShellSetSetUp(pc,setup);       (optional)
5044b9ad928SBarry Smith 
5054b9ad928SBarry Smith .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC,
5064b9ad928SBarry Smith            KSPSHELL(), MATSHELL(), PCShellSetUp(), PCShellSetApply(), PCShellSetView(),
5074b9ad928SBarry Smith            PCShellSetApplyTranpose(), PCShellSetName(), PCShellSetApplyRichardson(),
5084b9ad928SBarry Smith            PCShellGetName()
5094b9ad928SBarry Smith M*/
5104b9ad928SBarry Smith 
5114b9ad928SBarry Smith EXTERN_C_BEGIN
5124b9ad928SBarry Smith #undef __FUNCT__
5134b9ad928SBarry Smith #define __FUNCT__ "PCCreate_Shell"
514dfbe8321SBarry Smith PetscErrorCode PCCreate_Shell(PC pc)
5154b9ad928SBarry Smith {
516dfbe8321SBarry Smith   PetscErrorCode ierr;
5174b9ad928SBarry Smith   PC_Shell *shell;
5184b9ad928SBarry Smith 
5194b9ad928SBarry Smith   PetscFunctionBegin;
5204b9ad928SBarry Smith   pc->ops->destroy    = PCDestroy_Shell;
5214b9ad928SBarry Smith   ierr                = PetscNew(PC_Shell,&shell);CHKERRQ(ierr);
5224b9ad928SBarry Smith   PetscLogObjectMemory(pc,sizeof(PC_Shell));
5234b9ad928SBarry Smith 
5244b9ad928SBarry Smith   pc->data         = (void*)shell;
5254b9ad928SBarry Smith   pc->name         = 0;
5264b9ad928SBarry Smith 
5274b9ad928SBarry Smith   pc->ops->apply           = PCApply_Shell;
5284b9ad928SBarry Smith   pc->ops->view            = PCView_Shell;
5294b9ad928SBarry Smith   pc->ops->applytranspose  = PCApplyTranspose_Shell;
5304b9ad928SBarry Smith   pc->ops->applyrichardson = 0;
5314b9ad928SBarry Smith   pc->ops->setup           = PCSetUp_Shell;
5324b9ad928SBarry Smith   pc->ops->view            = PCView_Shell;
5334b9ad928SBarry Smith 
5344b9ad928SBarry Smith   shell->apply          = 0;
5354b9ad928SBarry Smith   shell->applytranspose = 0;
5364b9ad928SBarry Smith   shell->name           = 0;
5374b9ad928SBarry Smith   shell->applyrich      = 0;
5384b9ad928SBarry Smith   shell->ctxrich        = 0;
5394b9ad928SBarry Smith   shell->ctx            = 0;
5404b9ad928SBarry Smith   shell->setup          = 0;
5414b9ad928SBarry Smith   shell->view           = 0;
5424b9ad928SBarry Smith 
5434b9ad928SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCShellSetSetUp_C","PCShellSetSetUp_Shell",
5444b9ad928SBarry Smith                     PCShellSetSetUp_Shell);CHKERRQ(ierr);
5454b9ad928SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCShellSetApply_C","PCShellSetApply_Shell",
5464b9ad928SBarry Smith                     PCShellSetApply_Shell);CHKERRQ(ierr);
5474b9ad928SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCShellSetView_C","PCShellSetView_Shell",
5484b9ad928SBarry Smith                     PCShellSetView_Shell);CHKERRQ(ierr);
5494b9ad928SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCShellSetApplyTranspose_C",
5504b9ad928SBarry Smith                     "PCShellSetApplyTranspose_Shell",
5514b9ad928SBarry Smith                     PCShellSetApplyTranspose_Shell);CHKERRQ(ierr);
5524b9ad928SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCShellSetName_C","PCShellSetName_Shell",
5534b9ad928SBarry Smith                     PCShellSetName_Shell);CHKERRQ(ierr);
5544b9ad928SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCShellGetName_C","PCShellGetName_Shell",
5554b9ad928SBarry Smith                     PCShellGetName_Shell);CHKERRQ(ierr);
5564b9ad928SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCShellSetApplyRichardson_C",
5574b9ad928SBarry Smith                     "PCShellSetApplyRichardson_Shell",
5584b9ad928SBarry Smith                     PCShellSetApplyRichardson_Shell);CHKERRQ(ierr);
5594b9ad928SBarry Smith 
5604b9ad928SBarry Smith   PetscFunctionReturn(0);
5614b9ad928SBarry Smith }
5624b9ad928SBarry Smith EXTERN_C_END
5634b9ad928SBarry Smith 
5644b9ad928SBarry Smith 
5654b9ad928SBarry Smith 
5664b9ad928SBarry Smith 
5674b9ad928SBarry Smith 
5684b9ad928SBarry Smith 
569