1*4b9ad928SBarry Smith /*$Id: shellpc.c,v 1.77 2001/08/21 21:03:18 bsmith Exp $*/ 2*4b9ad928SBarry Smith 3*4b9ad928SBarry Smith /* 4*4b9ad928SBarry Smith This provides a simple shell for Fortran (and C programmers) to 5*4b9ad928SBarry Smith create their own preconditioner without writing much interface code. 6*4b9ad928SBarry Smith */ 7*4b9ad928SBarry Smith 8*4b9ad928SBarry Smith #include "src/ksp/pc/pcimpl.h" /*I "petscpc.h" I*/ 9*4b9ad928SBarry Smith #include "src/vec/vecimpl.h" 10*4b9ad928SBarry Smith 11*4b9ad928SBarry Smith typedef struct { 12*4b9ad928SBarry Smith void *ctx,*ctxrich; /* user provided contexts for preconditioner */ 13*4b9ad928SBarry Smith int (*setup)(void *); 14*4b9ad928SBarry Smith int (*apply)(void *,Vec,Vec); 15*4b9ad928SBarry Smith int (*view)(void *,PetscViewer); 16*4b9ad928SBarry Smith int (*applytranspose)(void *,Vec,Vec); 17*4b9ad928SBarry Smith int (*applyrich)(void *,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,int); 18*4b9ad928SBarry Smith char *name; 19*4b9ad928SBarry Smith } PC_Shell; 20*4b9ad928SBarry Smith 21*4b9ad928SBarry Smith #undef __FUNCT__ 22*4b9ad928SBarry Smith #define __FUNCT__ "PCApply_SetUp" 23*4b9ad928SBarry Smith static int PCSetUp_Shell(PC pc) 24*4b9ad928SBarry Smith { 25*4b9ad928SBarry Smith PC_Shell *shell; 26*4b9ad928SBarry Smith int ierr; 27*4b9ad928SBarry Smith 28*4b9ad928SBarry Smith PetscFunctionBegin; 29*4b9ad928SBarry Smith shell = (PC_Shell*)pc->data; 30*4b9ad928SBarry Smith if (shell->setup) { 31*4b9ad928SBarry Smith ierr = (*shell->setup)(shell->ctx);CHKERRQ(ierr); 32*4b9ad928SBarry Smith } 33*4b9ad928SBarry Smith PetscFunctionReturn(0); 34*4b9ad928SBarry Smith } 35*4b9ad928SBarry Smith 36*4b9ad928SBarry Smith #undef __FUNCT__ 37*4b9ad928SBarry Smith #define __FUNCT__ "PCApply_Shell" 38*4b9ad928SBarry Smith static int PCApply_Shell(PC pc,Vec x,Vec y) 39*4b9ad928SBarry Smith { 40*4b9ad928SBarry Smith PC_Shell *shell; 41*4b9ad928SBarry Smith int ierr; 42*4b9ad928SBarry Smith 43*4b9ad928SBarry Smith PetscFunctionBegin; 44*4b9ad928SBarry Smith shell = (PC_Shell*)pc->data; 45*4b9ad928SBarry Smith if (!shell->apply) SETERRQ(1,"No apply() routine provided to Shell PC"); 46*4b9ad928SBarry Smith ierr = (*shell->apply)(shell->ctx,x,y);CHKERRQ(ierr); 47*4b9ad928SBarry Smith PetscFunctionReturn(0); 48*4b9ad928SBarry Smith } 49*4b9ad928SBarry Smith 50*4b9ad928SBarry Smith #undef __FUNCT__ 51*4b9ad928SBarry Smith #define __FUNCT__ "PCApplyTranspose_Shell" 52*4b9ad928SBarry Smith static int PCApplyTranspose_Shell(PC pc,Vec x,Vec y) 53*4b9ad928SBarry Smith { 54*4b9ad928SBarry Smith PC_Shell *shell; 55*4b9ad928SBarry Smith int ierr; 56*4b9ad928SBarry Smith 57*4b9ad928SBarry Smith PetscFunctionBegin; 58*4b9ad928SBarry Smith shell = (PC_Shell*)pc->data; 59*4b9ad928SBarry Smith if (!shell->applytranspose) SETERRQ(1,"No applytranspose() routine provided to Shell PC"); 60*4b9ad928SBarry Smith ierr = (*shell->applytranspose)(shell->ctx,x,y);CHKERRQ(ierr); 61*4b9ad928SBarry Smith PetscFunctionReturn(0); 62*4b9ad928SBarry Smith } 63*4b9ad928SBarry Smith 64*4b9ad928SBarry Smith #undef __FUNCT__ 65*4b9ad928SBarry Smith #define __FUNCT__ "PCApplyRichardson_Shell" 66*4b9ad928SBarry Smith static int PCApplyRichardson_Shell(PC pc,Vec x,Vec y,Vec w,PetscReal rtol,PetscReal atol, PetscReal dtol,int it) 67*4b9ad928SBarry Smith { 68*4b9ad928SBarry Smith int ierr; 69*4b9ad928SBarry Smith PC_Shell *shell; 70*4b9ad928SBarry Smith 71*4b9ad928SBarry Smith PetscFunctionBegin; 72*4b9ad928SBarry Smith shell = (PC_Shell*)pc->data; 73*4b9ad928SBarry Smith ierr = (*shell->applyrich)(shell->ctxrich,x,y,w,rtol,atol,dtol,it);CHKERRQ(ierr); 74*4b9ad928SBarry Smith PetscFunctionReturn(0); 75*4b9ad928SBarry Smith } 76*4b9ad928SBarry Smith 77*4b9ad928SBarry Smith #undef __FUNCT__ 78*4b9ad928SBarry Smith #define __FUNCT__ "PCDestroy_Shell" 79*4b9ad928SBarry Smith static int PCDestroy_Shell(PC pc) 80*4b9ad928SBarry Smith { 81*4b9ad928SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 82*4b9ad928SBarry Smith int ierr; 83*4b9ad928SBarry Smith 84*4b9ad928SBarry Smith PetscFunctionBegin; 85*4b9ad928SBarry Smith if (shell->name) {ierr = PetscFree(shell->name);} 86*4b9ad928SBarry Smith ierr = PetscFree(shell);CHKERRQ(ierr); 87*4b9ad928SBarry Smith PetscFunctionReturn(0); 88*4b9ad928SBarry Smith } 89*4b9ad928SBarry Smith 90*4b9ad928SBarry Smith #undef __FUNCT__ 91*4b9ad928SBarry Smith #define __FUNCT__ "PCView_Shell" 92*4b9ad928SBarry Smith static int PCView_Shell(PC pc,PetscViewer viewer) 93*4b9ad928SBarry Smith { 94*4b9ad928SBarry Smith PC_Shell *shell = (PC_Shell*)pc->data; 95*4b9ad928SBarry Smith int ierr; 96*4b9ad928SBarry Smith PetscTruth isascii; 97*4b9ad928SBarry Smith 98*4b9ad928SBarry Smith PetscFunctionBegin; 99*4b9ad928SBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr); 100*4b9ad928SBarry Smith if (isascii) { 101*4b9ad928SBarry Smith if (shell->name) {ierr = PetscViewerASCIIPrintf(viewer," Shell: %s\n",shell->name);CHKERRQ(ierr);} 102*4b9ad928SBarry Smith else {ierr = PetscViewerASCIIPrintf(viewer," Shell: no name\n");CHKERRQ(ierr);} 103*4b9ad928SBarry Smith } 104*4b9ad928SBarry Smith if (shell->view) { 105*4b9ad928SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 106*4b9ad928SBarry Smith ierr = (*shell->view)(shell->ctx,viewer);CHKERRQ(ierr); 107*4b9ad928SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 108*4b9ad928SBarry Smith } 109*4b9ad928SBarry Smith PetscFunctionReturn(0); 110*4b9ad928SBarry Smith } 111*4b9ad928SBarry Smith 112*4b9ad928SBarry Smith /* ------------------------------------------------------------------------------*/ 113*4b9ad928SBarry Smith EXTERN_C_BEGIN 114*4b9ad928SBarry Smith #undef __FUNCT__ 115*4b9ad928SBarry Smith #define __FUNCT__ "PCShellSetSetUp_Shell" 116*4b9ad928SBarry Smith int PCShellSetSetUp_Shell(PC pc, int (*setup)(void*)) 117*4b9ad928SBarry Smith { 118*4b9ad928SBarry Smith PC_Shell *shell; 119*4b9ad928SBarry Smith 120*4b9ad928SBarry Smith PetscFunctionBegin; 121*4b9ad928SBarry Smith shell = (PC_Shell*)pc->data; 122*4b9ad928SBarry Smith shell->setup = setup; 123*4b9ad928SBarry Smith PetscFunctionReturn(0); 124*4b9ad928SBarry Smith } 125*4b9ad928SBarry Smith EXTERN_C_END 126*4b9ad928SBarry Smith 127*4b9ad928SBarry Smith EXTERN_C_BEGIN 128*4b9ad928SBarry Smith #undef __FUNCT__ 129*4b9ad928SBarry Smith #define __FUNCT__ "PCShellSetApply_Shell" 130*4b9ad928SBarry Smith int PCShellSetApply_Shell(PC pc,int (*apply)(void*,Vec,Vec),void *ptr) 131*4b9ad928SBarry Smith { 132*4b9ad928SBarry Smith PC_Shell *shell; 133*4b9ad928SBarry Smith 134*4b9ad928SBarry Smith PetscFunctionBegin; 135*4b9ad928SBarry Smith shell = (PC_Shell*)pc->data; 136*4b9ad928SBarry Smith shell->apply = apply; 137*4b9ad928SBarry Smith shell->ctx = ptr; 138*4b9ad928SBarry Smith PetscFunctionReturn(0); 139*4b9ad928SBarry Smith } 140*4b9ad928SBarry Smith EXTERN_C_END 141*4b9ad928SBarry Smith 142*4b9ad928SBarry Smith EXTERN_C_BEGIN 143*4b9ad928SBarry Smith #undef __FUNCT__ 144*4b9ad928SBarry Smith #define __FUNCT__ "PCShellSetView_Shell" 145*4b9ad928SBarry Smith int PCShellSetView_Shell(PC pc,int (*view)(void*,PetscViewer)) 146*4b9ad928SBarry Smith { 147*4b9ad928SBarry Smith PC_Shell *shell; 148*4b9ad928SBarry Smith 149*4b9ad928SBarry Smith PetscFunctionBegin; 150*4b9ad928SBarry Smith shell = (PC_Shell*)pc->data; 151*4b9ad928SBarry Smith shell->view = view; 152*4b9ad928SBarry Smith PetscFunctionReturn(0); 153*4b9ad928SBarry Smith } 154*4b9ad928SBarry Smith EXTERN_C_END 155*4b9ad928SBarry Smith 156*4b9ad928SBarry Smith EXTERN_C_BEGIN 157*4b9ad928SBarry Smith #undef __FUNCT__ 158*4b9ad928SBarry Smith #define __FUNCT__ "PCShellSetApplyTranspose_Shell" 159*4b9ad928SBarry Smith int PCShellSetApplyTranspose_Shell(PC pc,int (*applytranspose)(void*,Vec,Vec)) 160*4b9ad928SBarry Smith { 161*4b9ad928SBarry Smith PC_Shell *shell; 162*4b9ad928SBarry Smith 163*4b9ad928SBarry Smith PetscFunctionBegin; 164*4b9ad928SBarry Smith shell = (PC_Shell*)pc->data; 165*4b9ad928SBarry Smith shell->applytranspose = applytranspose; 166*4b9ad928SBarry Smith PetscFunctionReturn(0); 167*4b9ad928SBarry Smith } 168*4b9ad928SBarry Smith EXTERN_C_END 169*4b9ad928SBarry Smith 170*4b9ad928SBarry Smith EXTERN_C_BEGIN 171*4b9ad928SBarry Smith #undef __FUNCT__ 172*4b9ad928SBarry Smith #define __FUNCT__ "PCShellSetName_Shell" 173*4b9ad928SBarry Smith int PCShellSetName_Shell(PC pc,const char name[]) 174*4b9ad928SBarry Smith { 175*4b9ad928SBarry Smith PC_Shell *shell; 176*4b9ad928SBarry Smith int ierr; 177*4b9ad928SBarry Smith 178*4b9ad928SBarry Smith PetscFunctionBegin; 179*4b9ad928SBarry Smith shell = (PC_Shell*)pc->data; 180*4b9ad928SBarry Smith ierr = PetscStrallocpy(name,&shell->name);CHKERRQ(ierr); 181*4b9ad928SBarry Smith PetscFunctionReturn(0); 182*4b9ad928SBarry Smith } 183*4b9ad928SBarry Smith EXTERN_C_END 184*4b9ad928SBarry Smith 185*4b9ad928SBarry Smith EXTERN_C_BEGIN 186*4b9ad928SBarry Smith #undef __FUNCT__ 187*4b9ad928SBarry Smith #define __FUNCT__ "PCShellGetName_Shell" 188*4b9ad928SBarry Smith int PCShellGetName_Shell(PC pc,char *name[]) 189*4b9ad928SBarry Smith { 190*4b9ad928SBarry Smith PC_Shell *shell; 191*4b9ad928SBarry Smith 192*4b9ad928SBarry Smith PetscFunctionBegin; 193*4b9ad928SBarry Smith shell = (PC_Shell*)pc->data; 194*4b9ad928SBarry Smith *name = shell->name; 195*4b9ad928SBarry Smith PetscFunctionReturn(0); 196*4b9ad928SBarry Smith } 197*4b9ad928SBarry Smith EXTERN_C_END 198*4b9ad928SBarry Smith 199*4b9ad928SBarry Smith EXTERN_C_BEGIN 200*4b9ad928SBarry Smith #undef __FUNCT__ 201*4b9ad928SBarry Smith #define __FUNCT__ "PCShellSetApplyRichardson_Shell" 202*4b9ad928SBarry Smith int PCShellSetApplyRichardson_Shell(PC pc,int (*apply)(void*,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,int),void *ptr) 203*4b9ad928SBarry Smith { 204*4b9ad928SBarry Smith PC_Shell *shell; 205*4b9ad928SBarry Smith 206*4b9ad928SBarry Smith PetscFunctionBegin; 207*4b9ad928SBarry Smith shell = (PC_Shell*)pc->data; 208*4b9ad928SBarry Smith pc->ops->applyrichardson = PCApplyRichardson_Shell; 209*4b9ad928SBarry Smith shell->applyrich = apply; 210*4b9ad928SBarry Smith shell->ctxrich = ptr; 211*4b9ad928SBarry Smith PetscFunctionReturn(0); 212*4b9ad928SBarry Smith } 213*4b9ad928SBarry Smith EXTERN_C_END 214*4b9ad928SBarry Smith 215*4b9ad928SBarry Smith /* -------------------------------------------------------------------------------*/ 216*4b9ad928SBarry Smith 217*4b9ad928SBarry Smith #undef __FUNCT__ 218*4b9ad928SBarry Smith #define __FUNCT__ "PCShellSetSetUp" 219*4b9ad928SBarry Smith /*@C 220*4b9ad928SBarry Smith PCShellSetSetUp - Sets routine to use to "setup" the preconditioner whenever the 221*4b9ad928SBarry Smith matrix operator is changed. 222*4b9ad928SBarry Smith 223*4b9ad928SBarry Smith Collective on PC 224*4b9ad928SBarry Smith 225*4b9ad928SBarry Smith Input Parameters: 226*4b9ad928SBarry Smith + pc - the preconditioner context 227*4b9ad928SBarry Smith . setup - the application-provided setup routine 228*4b9ad928SBarry Smith 229*4b9ad928SBarry Smith Calling sequence of setup: 230*4b9ad928SBarry Smith .vb 231*4b9ad928SBarry Smith int setup (void *ptr) 232*4b9ad928SBarry Smith .ve 233*4b9ad928SBarry Smith 234*4b9ad928SBarry Smith . ptr - the application context 235*4b9ad928SBarry Smith 236*4b9ad928SBarry Smith Level: developer 237*4b9ad928SBarry Smith 238*4b9ad928SBarry Smith .keywords: PC, shell, set, setup, user-provided 239*4b9ad928SBarry Smith 240*4b9ad928SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetApply() 241*4b9ad928SBarry Smith @*/ 242*4b9ad928SBarry Smith int PCShellSetSetUp(PC pc,int (*setup)(void*)) 243*4b9ad928SBarry Smith { 244*4b9ad928SBarry Smith int ierr,(*f)(PC,int (*)(void*)); 245*4b9ad928SBarry Smith 246*4b9ad928SBarry Smith PetscFunctionBegin; 247*4b9ad928SBarry Smith PetscValidHeaderSpecific(pc,PC_COOKIE); 248*4b9ad928SBarry Smith ierr = PetscObjectQueryFunction((PetscObject)pc,"PCShellSetSetUp_C",(void (**)(void))&f);CHKERRQ(ierr); 249*4b9ad928SBarry Smith if (f) { 250*4b9ad928SBarry Smith ierr = (*f)(pc,setup);CHKERRQ(ierr); 251*4b9ad928SBarry Smith } 252*4b9ad928SBarry Smith PetscFunctionReturn(0); 253*4b9ad928SBarry Smith } 254*4b9ad928SBarry Smith 255*4b9ad928SBarry Smith 256*4b9ad928SBarry Smith #undef __FUNCT__ 257*4b9ad928SBarry Smith #define __FUNCT__ "PCShellSetView" 258*4b9ad928SBarry Smith /*@C 259*4b9ad928SBarry Smith PCShellSetView - Sets routine to use as viewer of shell preconditioner 260*4b9ad928SBarry Smith 261*4b9ad928SBarry Smith Collective on PC 262*4b9ad928SBarry Smith 263*4b9ad928SBarry Smith Input Parameters: 264*4b9ad928SBarry Smith + pc - the preconditioner context 265*4b9ad928SBarry Smith - view - the application-provided view routine 266*4b9ad928SBarry Smith 267*4b9ad928SBarry Smith Calling sequence of apply: 268*4b9ad928SBarry Smith .vb 269*4b9ad928SBarry Smith int view(void *ptr,PetscViewer v) 270*4b9ad928SBarry Smith .ve 271*4b9ad928SBarry Smith 272*4b9ad928SBarry Smith + ptr - the application context 273*4b9ad928SBarry Smith - v - viewer 274*4b9ad928SBarry Smith 275*4b9ad928SBarry Smith Level: developer 276*4b9ad928SBarry Smith 277*4b9ad928SBarry Smith .keywords: PC, shell, set, apply, user-provided 278*4b9ad928SBarry Smith 279*4b9ad928SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose() 280*4b9ad928SBarry Smith @*/ 281*4b9ad928SBarry Smith int PCShellSetView(PC pc,int (*view)(void*,PetscViewer)) 282*4b9ad928SBarry Smith { 283*4b9ad928SBarry Smith int ierr,(*f)(PC,int (*)(void*,PetscViewer)); 284*4b9ad928SBarry Smith 285*4b9ad928SBarry Smith PetscFunctionBegin; 286*4b9ad928SBarry Smith PetscValidHeaderSpecific(pc,PC_COOKIE); 287*4b9ad928SBarry Smith ierr = PetscObjectQueryFunction((PetscObject)pc,"PCShellSetView_C",(void (**)(void))&f);CHKERRQ(ierr); 288*4b9ad928SBarry Smith if (f) { 289*4b9ad928SBarry Smith ierr = (*f)(pc,view);CHKERRQ(ierr); 290*4b9ad928SBarry Smith } 291*4b9ad928SBarry Smith PetscFunctionReturn(0); 292*4b9ad928SBarry Smith } 293*4b9ad928SBarry Smith 294*4b9ad928SBarry Smith #undef __FUNCT__ 295*4b9ad928SBarry Smith #define __FUNCT__ "PCShellSetApply" 296*4b9ad928SBarry Smith /*@C 297*4b9ad928SBarry Smith PCShellSetApply - Sets routine to use as preconditioner. 298*4b9ad928SBarry Smith 299*4b9ad928SBarry Smith Collective on PC 300*4b9ad928SBarry Smith 301*4b9ad928SBarry Smith Input Parameters: 302*4b9ad928SBarry Smith + pc - the preconditioner context 303*4b9ad928SBarry Smith . apply - the application-provided preconditioning routine 304*4b9ad928SBarry Smith - ptr - pointer to data needed by this routine 305*4b9ad928SBarry Smith 306*4b9ad928SBarry Smith Calling sequence of apply: 307*4b9ad928SBarry Smith .vb 308*4b9ad928SBarry Smith int apply (void *ptr,Vec xin,Vec xout) 309*4b9ad928SBarry Smith .ve 310*4b9ad928SBarry Smith 311*4b9ad928SBarry Smith + ptr - the application context 312*4b9ad928SBarry Smith . xin - input vector 313*4b9ad928SBarry Smith - xout - output vector 314*4b9ad928SBarry Smith 315*4b9ad928SBarry Smith Level: developer 316*4b9ad928SBarry Smith 317*4b9ad928SBarry Smith .keywords: PC, shell, set, apply, user-provided 318*4b9ad928SBarry Smith 319*4b9ad928SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApplyTranspose() 320*4b9ad928SBarry Smith @*/ 321*4b9ad928SBarry Smith int PCShellSetApply(PC pc,int (*apply)(void*,Vec,Vec),void *ptr) 322*4b9ad928SBarry Smith { 323*4b9ad928SBarry Smith int ierr,(*f)(PC,int (*)(void*,Vec,Vec),void *); 324*4b9ad928SBarry Smith 325*4b9ad928SBarry Smith PetscFunctionBegin; 326*4b9ad928SBarry Smith PetscValidHeaderSpecific(pc,PC_COOKIE); 327*4b9ad928SBarry Smith ierr = PetscObjectQueryFunction((PetscObject)pc,"PCShellSetApply_C",(void (**)(void))&f);CHKERRQ(ierr); 328*4b9ad928SBarry Smith if (f) { 329*4b9ad928SBarry Smith ierr = (*f)(pc,apply,ptr);CHKERRQ(ierr); 330*4b9ad928SBarry Smith } 331*4b9ad928SBarry Smith PetscFunctionReturn(0); 332*4b9ad928SBarry Smith } 333*4b9ad928SBarry Smith 334*4b9ad928SBarry Smith #undef __FUNCT__ 335*4b9ad928SBarry Smith #define __FUNCT__ "PCShellSetApplyTranspose" 336*4b9ad928SBarry Smith /*@C 337*4b9ad928SBarry Smith PCShellSetApplyTranspose - Sets routine to use as preconditioner transpose. 338*4b9ad928SBarry Smith 339*4b9ad928SBarry Smith Collective on PC 340*4b9ad928SBarry Smith 341*4b9ad928SBarry Smith Input Parameters: 342*4b9ad928SBarry Smith + pc - the preconditioner context 343*4b9ad928SBarry Smith - apply - the application-provided preconditioning transpose routine 344*4b9ad928SBarry Smith 345*4b9ad928SBarry Smith Calling sequence of apply: 346*4b9ad928SBarry Smith .vb 347*4b9ad928SBarry Smith int applytranspose (void *ptr,Vec xin,Vec xout) 348*4b9ad928SBarry Smith .ve 349*4b9ad928SBarry Smith 350*4b9ad928SBarry Smith + ptr - the application context 351*4b9ad928SBarry Smith . xin - input vector 352*4b9ad928SBarry Smith - xout - output vector 353*4b9ad928SBarry Smith 354*4b9ad928SBarry Smith Level: developer 355*4b9ad928SBarry Smith 356*4b9ad928SBarry Smith Notes: 357*4b9ad928SBarry Smith Uses the same context variable as PCShellSetApply(). 358*4b9ad928SBarry Smith 359*4b9ad928SBarry Smith .keywords: PC, shell, set, apply, user-provided 360*4b9ad928SBarry Smith 361*4b9ad928SBarry Smith .seealso: PCShellSetApplyRichardson(), PCShellSetSetUp(), PCShellSetApply() 362*4b9ad928SBarry Smith @*/ 363*4b9ad928SBarry Smith int PCShellSetApplyTranspose(PC pc,int (*applytranspose)(void*,Vec,Vec)) 364*4b9ad928SBarry Smith { 365*4b9ad928SBarry Smith int ierr,(*f)(PC,int (*)(void*,Vec,Vec)); 366*4b9ad928SBarry Smith 367*4b9ad928SBarry Smith PetscFunctionBegin; 368*4b9ad928SBarry Smith PetscValidHeaderSpecific(pc,PC_COOKIE); 369*4b9ad928SBarry Smith ierr = PetscObjectQueryFunction((PetscObject)pc,"PCShellSetApplyTranspose_C",(void (**)(void))&f);CHKERRQ(ierr); 370*4b9ad928SBarry Smith if (f) { 371*4b9ad928SBarry Smith ierr = (*f)(pc,applytranspose);CHKERRQ(ierr); 372*4b9ad928SBarry Smith } 373*4b9ad928SBarry Smith PetscFunctionReturn(0); 374*4b9ad928SBarry Smith } 375*4b9ad928SBarry Smith 376*4b9ad928SBarry Smith #undef __FUNCT__ 377*4b9ad928SBarry Smith #define __FUNCT__ "PCShellSetName" 378*4b9ad928SBarry Smith /*@C 379*4b9ad928SBarry Smith PCShellSetName - Sets an optional name to associate with a shell 380*4b9ad928SBarry Smith preconditioner. 381*4b9ad928SBarry Smith 382*4b9ad928SBarry Smith Not Collective 383*4b9ad928SBarry Smith 384*4b9ad928SBarry Smith Input Parameters: 385*4b9ad928SBarry Smith + pc - the preconditioner context 386*4b9ad928SBarry Smith - name - character string describing shell preconditioner 387*4b9ad928SBarry Smith 388*4b9ad928SBarry Smith Level: developer 389*4b9ad928SBarry Smith 390*4b9ad928SBarry Smith .keywords: PC, shell, set, name, user-provided 391*4b9ad928SBarry Smith 392*4b9ad928SBarry Smith .seealso: PCShellGetName() 393*4b9ad928SBarry Smith @*/ 394*4b9ad928SBarry Smith int PCShellSetName(PC pc,const char name[]) 395*4b9ad928SBarry Smith { 396*4b9ad928SBarry Smith int ierr,(*f)(PC,const char []); 397*4b9ad928SBarry Smith 398*4b9ad928SBarry Smith PetscFunctionBegin; 399*4b9ad928SBarry Smith PetscValidHeaderSpecific(pc,PC_COOKIE); 400*4b9ad928SBarry Smith ierr = PetscObjectQueryFunction((PetscObject)pc,"PCShellSetName_C",(void (**)(void))&f);CHKERRQ(ierr); 401*4b9ad928SBarry Smith if (f) { 402*4b9ad928SBarry Smith ierr = (*f)(pc,name);CHKERRQ(ierr); 403*4b9ad928SBarry Smith } 404*4b9ad928SBarry Smith PetscFunctionReturn(0); 405*4b9ad928SBarry Smith } 406*4b9ad928SBarry Smith 407*4b9ad928SBarry Smith #undef __FUNCT__ 408*4b9ad928SBarry Smith #define __FUNCT__ "PCShellGetName" 409*4b9ad928SBarry Smith /*@C 410*4b9ad928SBarry Smith PCShellGetName - Gets an optional name that the user has set for a shell 411*4b9ad928SBarry Smith preconditioner. 412*4b9ad928SBarry Smith 413*4b9ad928SBarry Smith Not Collective 414*4b9ad928SBarry Smith 415*4b9ad928SBarry Smith Input Parameter: 416*4b9ad928SBarry Smith . pc - the preconditioner context 417*4b9ad928SBarry Smith 418*4b9ad928SBarry Smith Output Parameter: 419*4b9ad928SBarry Smith . name - character string describing shell preconditioner (you should not free this) 420*4b9ad928SBarry Smith 421*4b9ad928SBarry Smith Level: developer 422*4b9ad928SBarry Smith 423*4b9ad928SBarry Smith .keywords: PC, shell, get, name, user-provided 424*4b9ad928SBarry Smith 425*4b9ad928SBarry Smith .seealso: PCShellSetName() 426*4b9ad928SBarry Smith @*/ 427*4b9ad928SBarry Smith int PCShellGetName(PC pc,char *name[]) 428*4b9ad928SBarry Smith { 429*4b9ad928SBarry Smith int ierr,(*f)(PC,char *[]); 430*4b9ad928SBarry Smith 431*4b9ad928SBarry Smith PetscFunctionBegin; 432*4b9ad928SBarry Smith PetscValidHeaderSpecific(pc,PC_COOKIE); 433*4b9ad928SBarry Smith ierr = PetscObjectQueryFunction((PetscObject)pc,"PCShellGetName_C",(void (**)(void))&f);CHKERRQ(ierr); 434*4b9ad928SBarry Smith if (f) { 435*4b9ad928SBarry Smith ierr = (*f)(pc,name);CHKERRQ(ierr); 436*4b9ad928SBarry Smith } else { 437*4b9ad928SBarry Smith SETERRQ(1,"Not shell preconditioner, cannot get name"); 438*4b9ad928SBarry Smith } 439*4b9ad928SBarry Smith PetscFunctionReturn(0); 440*4b9ad928SBarry Smith } 441*4b9ad928SBarry Smith 442*4b9ad928SBarry Smith #undef __FUNCT__ 443*4b9ad928SBarry Smith #define __FUNCT__ "PCShellSetApplyRichardson" 444*4b9ad928SBarry Smith /*@C 445*4b9ad928SBarry Smith PCShellSetApplyRichardson - Sets routine to use as preconditioner 446*4b9ad928SBarry Smith in Richardson iteration. 447*4b9ad928SBarry Smith 448*4b9ad928SBarry Smith Collective on PC 449*4b9ad928SBarry Smith 450*4b9ad928SBarry Smith Input Parameters: 451*4b9ad928SBarry Smith + pc - the preconditioner context 452*4b9ad928SBarry Smith . apply - the application-provided preconditioning routine 453*4b9ad928SBarry Smith - ptr - pointer to data needed by this routine 454*4b9ad928SBarry Smith 455*4b9ad928SBarry Smith Calling sequence of apply: 456*4b9ad928SBarry Smith .vb 457*4b9ad928SBarry Smith int apply (void *ptr,Vec b,Vec x,Vec r,PetscReal rtol,PetscReal atol,PetscReal dtol,int maxits) 458*4b9ad928SBarry Smith .ve 459*4b9ad928SBarry Smith 460*4b9ad928SBarry Smith + ptr - the application context 461*4b9ad928SBarry Smith . b - right-hand-side 462*4b9ad928SBarry Smith . x - current iterate 463*4b9ad928SBarry Smith . r - work space 464*4b9ad928SBarry Smith . rtol - relative tolerance of residual norm to stop at 465*4b9ad928SBarry Smith . atol - absolute tolerance of residual norm to stop at 466*4b9ad928SBarry Smith . dtol - if residual norm increases by this factor than return 467*4b9ad928SBarry Smith - maxits - number of iterations to run 468*4b9ad928SBarry Smith 469*4b9ad928SBarry Smith Level: developer 470*4b9ad928SBarry Smith 471*4b9ad928SBarry Smith .keywords: PC, shell, set, apply, Richardson, user-provided 472*4b9ad928SBarry Smith 473*4b9ad928SBarry Smith .seealso: PCShellSetApply() 474*4b9ad928SBarry Smith @*/ 475*4b9ad928SBarry Smith int PCShellSetApplyRichardson(PC pc,int (*apply)(void*,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,int),void *ptr) 476*4b9ad928SBarry Smith { 477*4b9ad928SBarry Smith int ierr,(*f)(PC,int (*)(void*,Vec,Vec,Vec,PetscReal,PetscReal,PetscReal,int),void *); 478*4b9ad928SBarry Smith 479*4b9ad928SBarry Smith PetscFunctionBegin; 480*4b9ad928SBarry Smith PetscValidHeaderSpecific(pc,PC_COOKIE); 481*4b9ad928SBarry Smith ierr = PetscObjectQueryFunction((PetscObject)pc,"PCShellSetApplyRichardson_C",(void (**)(void))&f);CHKERRQ(ierr); 482*4b9ad928SBarry Smith if (f) { 483*4b9ad928SBarry Smith ierr = (*f)(pc,apply,ptr);CHKERRQ(ierr); 484*4b9ad928SBarry Smith } 485*4b9ad928SBarry Smith PetscFunctionReturn(0); 486*4b9ad928SBarry Smith } 487*4b9ad928SBarry Smith 488*4b9ad928SBarry Smith /*MC 489*4b9ad928SBarry Smith PCSHELL - Creates a new preconditioner class for use with your 490*4b9ad928SBarry Smith own private data storage format. 491*4b9ad928SBarry Smith 492*4b9ad928SBarry Smith Level: advanced 493*4b9ad928SBarry Smith 494*4b9ad928SBarry Smith Concepts: providing your own preconditioner 495*4b9ad928SBarry Smith 496*4b9ad928SBarry Smith Usage: 497*4b9ad928SBarry Smith $ int (*mult)(void *,Vec,Vec); 498*4b9ad928SBarry Smith $ int (*setup)(void *); 499*4b9ad928SBarry Smith $ PCCreate(comm,&pc); 500*4b9ad928SBarry Smith $ PCSetType(pc,PCSHELL); 501*4b9ad928SBarry Smith $ PCShellSetApply(pc,mult,ctx); 502*4b9ad928SBarry Smith $ PCShellSetSetUp(pc,setup); (optional) 503*4b9ad928SBarry Smith 504*4b9ad928SBarry Smith .seealso: PCCreate(), PCSetType(), PCType (for list of available types), PC, 505*4b9ad928SBarry Smith KSPSHELL(), MATSHELL(), PCShellSetUp(), PCShellSetApply(), PCShellSetView(), 506*4b9ad928SBarry Smith PCShellSetApplyTranpose(), PCShellSetName(), PCShellSetApplyRichardson(), 507*4b9ad928SBarry Smith PCShellGetName() 508*4b9ad928SBarry Smith M*/ 509*4b9ad928SBarry Smith 510*4b9ad928SBarry Smith EXTERN_C_BEGIN 511*4b9ad928SBarry Smith #undef __FUNCT__ 512*4b9ad928SBarry Smith #define __FUNCT__ "PCCreate_Shell" 513*4b9ad928SBarry Smith int PCCreate_Shell(PC pc) 514*4b9ad928SBarry Smith { 515*4b9ad928SBarry Smith int ierr; 516*4b9ad928SBarry Smith PC_Shell *shell; 517*4b9ad928SBarry Smith 518*4b9ad928SBarry Smith PetscFunctionBegin; 519*4b9ad928SBarry Smith pc->ops->destroy = PCDestroy_Shell; 520*4b9ad928SBarry Smith ierr = PetscNew(PC_Shell,&shell);CHKERRQ(ierr); 521*4b9ad928SBarry Smith PetscLogObjectMemory(pc,sizeof(PC_Shell)); 522*4b9ad928SBarry Smith 523*4b9ad928SBarry Smith pc->data = (void*)shell; 524*4b9ad928SBarry Smith pc->name = 0; 525*4b9ad928SBarry Smith 526*4b9ad928SBarry Smith pc->ops->apply = PCApply_Shell; 527*4b9ad928SBarry Smith pc->ops->view = PCView_Shell; 528*4b9ad928SBarry Smith pc->ops->applytranspose = PCApplyTranspose_Shell; 529*4b9ad928SBarry Smith pc->ops->applyrichardson = 0; 530*4b9ad928SBarry Smith pc->ops->setup = PCSetUp_Shell; 531*4b9ad928SBarry Smith pc->ops->view = PCView_Shell; 532*4b9ad928SBarry Smith 533*4b9ad928SBarry Smith shell->apply = 0; 534*4b9ad928SBarry Smith shell->applytranspose = 0; 535*4b9ad928SBarry Smith shell->name = 0; 536*4b9ad928SBarry Smith shell->applyrich = 0; 537*4b9ad928SBarry Smith shell->ctxrich = 0; 538*4b9ad928SBarry Smith shell->ctx = 0; 539*4b9ad928SBarry Smith shell->setup = 0; 540*4b9ad928SBarry Smith shell->view = 0; 541*4b9ad928SBarry Smith 542*4b9ad928SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCShellSetSetUp_C","PCShellSetSetUp_Shell", 543*4b9ad928SBarry Smith PCShellSetSetUp_Shell);CHKERRQ(ierr); 544*4b9ad928SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCShellSetApply_C","PCShellSetApply_Shell", 545*4b9ad928SBarry Smith PCShellSetApply_Shell);CHKERRQ(ierr); 546*4b9ad928SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCShellSetView_C","PCShellSetView_Shell", 547*4b9ad928SBarry Smith PCShellSetView_Shell);CHKERRQ(ierr); 548*4b9ad928SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCShellSetApplyTranspose_C", 549*4b9ad928SBarry Smith "PCShellSetApplyTranspose_Shell", 550*4b9ad928SBarry Smith PCShellSetApplyTranspose_Shell);CHKERRQ(ierr); 551*4b9ad928SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCShellSetName_C","PCShellSetName_Shell", 552*4b9ad928SBarry Smith PCShellSetName_Shell);CHKERRQ(ierr); 553*4b9ad928SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCShellGetName_C","PCShellGetName_Shell", 554*4b9ad928SBarry Smith PCShellGetName_Shell);CHKERRQ(ierr); 555*4b9ad928SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCShellSetApplyRichardson_C", 556*4b9ad928SBarry Smith "PCShellSetApplyRichardson_Shell", 557*4b9ad928SBarry Smith PCShellSetApplyRichardson_Shell);CHKERRQ(ierr); 558*4b9ad928SBarry Smith 559*4b9ad928SBarry Smith PetscFunctionReturn(0); 560*4b9ad928SBarry Smith } 561*4b9ad928SBarry Smith EXTERN_C_END 562*4b9ad928SBarry Smith 563*4b9ad928SBarry Smith 564*4b9ad928SBarry Smith 565*4b9ad928SBarry Smith 566*4b9ad928SBarry Smith 567*4b9ad928SBarry Smith 568