1b45d2f2cSJed Brown #include <petsc-private/snesimpl.h> /*I "petscsnes.h" I*/ 2074cc835SBarry Smith 3074cc835SBarry Smith typedef struct {PetscErrorCode (*solve)(SNES,Vec);void *ctx;} SNES_Shell; 4074cc835SBarry Smith 5074cc835SBarry Smith #undef __FUNCT__ 6074cc835SBarry Smith #define __FUNCT__ "SNESShellSetSolve" 7074cc835SBarry Smith /*@C 8074cc835SBarry Smith SNESShellSetSolve - Sets routine to apply as solver 9074cc835SBarry Smith 10074cc835SBarry Smith Logically Collective on SNES 11074cc835SBarry Smith 12074cc835SBarry Smith Input Parameters: 13074cc835SBarry Smith + snes - the nonlinear solver context 14074cc835SBarry Smith - apply - the application-provided solver routine 15074cc835SBarry Smith 16074cc835SBarry Smith Calling sequence of solve: 17074cc835SBarry Smith .vb 18074cc835SBarry Smith PetscErrorCode apply (SNES snes,Vec xout) 19074cc835SBarry Smith .ve 20074cc835SBarry Smith 21074cc835SBarry Smith + snes - the preconditioner, get the application context with SNESShellGetContext() 22074cc835SBarry Smith - xout - solution vector 23074cc835SBarry Smith 24074cc835SBarry Smith Notes: the function MUST return an error code of 0 on success and nonzero on failure. 25074cc835SBarry Smith 26074cc835SBarry Smith Level: advanced 27074cc835SBarry Smith 28074cc835SBarry Smith .keywords: SNES, shell, set, apply, user-provided 29074cc835SBarry Smith 30074cc835SBarry Smith .seealso: SNESSHELL, SNESShellSetContext(), SNESShellGetContext() 31074cc835SBarry Smith @*/ 32074cc835SBarry Smith PetscErrorCode SNESShellSetSolve(SNES snes,PetscErrorCode (*solve)(SNES,Vec)) 33074cc835SBarry Smith { 34074cc835SBarry Smith PetscErrorCode ierr; 35074cc835SBarry Smith 36074cc835SBarry Smith PetscFunctionBegin; 37074cc835SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 38074cc835SBarry Smith ierr = PetscTryMethod(snes,"SNESShellSetSolve_C",(SNES,PetscErrorCode (*)(SNES,Vec)),(snes,solve));CHKERRQ(ierr); 39074cc835SBarry Smith PetscFunctionReturn(0); 40074cc835SBarry Smith } 41074cc835SBarry Smith 42074cc835SBarry Smith #undef __FUNCT__ 43074cc835SBarry Smith #define __FUNCT__ "SNESReset_Shell" 44074cc835SBarry Smith PetscErrorCode SNESReset_Shell(SNES snes) 45074cc835SBarry Smith { 46074cc835SBarry Smith PetscFunctionBegin; 47074cc835SBarry Smith PetscFunctionReturn(0); 48074cc835SBarry Smith } 49074cc835SBarry Smith 50074cc835SBarry Smith #undef __FUNCT__ 51074cc835SBarry Smith #define __FUNCT__ "SNESDestroy_Shell" 52074cc835SBarry Smith PetscErrorCode SNESDestroy_Shell(SNES snes) 53074cc835SBarry Smith { 54074cc835SBarry Smith PetscErrorCode ierr; 55074cc835SBarry Smith 56074cc835SBarry Smith PetscFunctionBegin; 57074cc835SBarry Smith ierr = SNESReset_Shell(snes);CHKERRQ(ierr); 5822d28d08SBarry Smith ierr = PetscFree(snes->data);CHKERRQ(ierr); 59074cc835SBarry Smith PetscFunctionReturn(0); 60074cc835SBarry Smith } 61074cc835SBarry Smith 62074cc835SBarry Smith #undef __FUNCT__ 63074cc835SBarry Smith #define __FUNCT__ "SNESSetUp_Shell" 64074cc835SBarry Smith PetscErrorCode SNESSetUp_Shell(SNES snes) 65074cc835SBarry Smith { 66074cc835SBarry Smith PetscFunctionBegin; 67074cc835SBarry Smith PetscFunctionReturn(0); 68074cc835SBarry Smith } 69074cc835SBarry Smith 70074cc835SBarry Smith #undef __FUNCT__ 71074cc835SBarry Smith #define __FUNCT__ "SNESSetFromOptions_Shell" 72074cc835SBarry Smith PetscErrorCode SNESSetFromOptions_Shell(SNES snes) 73074cc835SBarry Smith { 74074cc835SBarry Smith PetscErrorCode ierr; 75074cc835SBarry Smith 76074cc835SBarry Smith PetscFunctionBegin; 77074cc835SBarry Smith ierr = PetscOptionsHead("SNES Shell options");CHKERRQ(ierr); 78074cc835SBarry Smith PetscFunctionReturn(0); 79074cc835SBarry Smith } 80074cc835SBarry Smith 81074cc835SBarry Smith #undef __FUNCT__ 82074cc835SBarry Smith #define __FUNCT__ "SNESView_Shell" 83074cc835SBarry Smith PetscErrorCode SNESView_Shell(SNES snes, PetscViewer viewer) 84074cc835SBarry Smith { 85074cc835SBarry Smith PetscFunctionBegin; 86074cc835SBarry Smith PetscFunctionReturn(0); 87074cc835SBarry Smith } 88074cc835SBarry Smith 89074cc835SBarry Smith #undef __FUNCT__ 90074cc835SBarry Smith #define __FUNCT__ "SNESShellGetContext" 9190b77ac2SPeter Brune /*@ 92074cc835SBarry Smith SNESShellGetContext - Returns the user-provided context associated with a shell SNES 93074cc835SBarry Smith 94074cc835SBarry Smith Not Collective 95074cc835SBarry Smith 96074cc835SBarry Smith Input Parameter: 97074cc835SBarry Smith . snes - should have been created with SNESSetType(snes,SNESSHELL); 98074cc835SBarry Smith 99074cc835SBarry Smith Output Parameter: 100074cc835SBarry Smith . ctx - the user provided context 101074cc835SBarry Smith 102074cc835SBarry Smith Level: advanced 103074cc835SBarry Smith 104074cc835SBarry Smith Notes: 105074cc835SBarry Smith This routine is intended for use within various shell routines 106074cc835SBarry Smith 107074cc835SBarry Smith .keywords: SNES, shell, get, context 108074cc835SBarry Smith 109074cc835SBarry Smith .seealso: SNESCreateShell(), SNESShellSetContext() 110074cc835SBarry Smith @*/ 111074cc835SBarry Smith PetscErrorCode SNESShellGetContext(SNES snes,void **ctx) 112074cc835SBarry Smith { 113074cc835SBarry Smith PetscErrorCode ierr; 114074cc835SBarry Smith PetscBool flg; 115074cc835SBarry Smith 116074cc835SBarry Smith PetscFunctionBegin; 117074cc835SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 118074cc835SBarry Smith PetscValidPointer(ctx,2); 119251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)snes,SNESSHELL,&flg);CHKERRQ(ierr); 120074cc835SBarry Smith if (!flg) *ctx = 0; 121074cc835SBarry Smith else *ctx = ((SNES_Shell*)(snes->data))->ctx; 122074cc835SBarry Smith PetscFunctionReturn(0); 123074cc835SBarry Smith } 124074cc835SBarry Smith 125074cc835SBarry Smith #undef __FUNCT__ 126074cc835SBarry Smith #define __FUNCT__ "SNESShellSetContext" 127074cc835SBarry Smith /*@ 128074cc835SBarry Smith SNESShellSetContext - sets the context for a shell SNES 129074cc835SBarry Smith 130074cc835SBarry Smith Logically Collective on SNES 131074cc835SBarry Smith 132074cc835SBarry Smith Input Parameters: 133074cc835SBarry Smith + snes - the shell SNES 134074cc835SBarry Smith - ctx - the context 135074cc835SBarry Smith 136074cc835SBarry Smith Level: advanced 137074cc835SBarry Smith 138074cc835SBarry Smith Fortran Notes: The context can only be an integer or a PetscObject 139074cc835SBarry Smith unfortunately it cannot be a Fortran array or derived type. 140074cc835SBarry Smith 141074cc835SBarry Smith 142074cc835SBarry Smith .seealso: SNESCreateShell(), SNESShellGetContext() 143074cc835SBarry Smith @*/ 144074cc835SBarry Smith PetscErrorCode SNESShellSetContext(SNES snes,void *ctx) 145074cc835SBarry Smith { 146074cc835SBarry Smith SNES_Shell *shell = (SNES_Shell*)snes->data; 147074cc835SBarry Smith PetscErrorCode ierr; 148074cc835SBarry Smith PetscBool flg; 149074cc835SBarry Smith 150074cc835SBarry Smith PetscFunctionBegin; 151074cc835SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 152251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)snes,SNESSHELL,&flg);CHKERRQ(ierr); 1531aa26658SKarl Rupp if (flg) shell->ctx = ctx; 154074cc835SBarry Smith PetscFunctionReturn(0); 155074cc835SBarry Smith } 156074cc835SBarry Smith 157074cc835SBarry Smith #undef __FUNCT__ 158074cc835SBarry Smith #define __FUNCT__ "SNESSolve_Shell" 159074cc835SBarry Smith PetscErrorCode SNESSolve_Shell(SNES snes) 160074cc835SBarry Smith { 161074cc835SBarry Smith SNES_Shell *shell = (SNES_Shell*) snes->data; 162074cc835SBarry Smith PetscErrorCode ierr; 163074cc835SBarry Smith 164074cc835SBarry Smith PetscFunctionBegin; 165*ce94432eSBarry Smith if (!shell->solve) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"Must call SNESShellSetSolve() first"); 1661e633543SBarry Smith snes->reason = SNES_CONVERGED_ITS; 167074cc835SBarry Smith ierr = (*shell->solve)(snes,snes->vec_sol);CHKERRQ(ierr); 168074cc835SBarry Smith PetscFunctionReturn(0); 169074cc835SBarry Smith } 170074cc835SBarry Smith 171074cc835SBarry Smith EXTERN_C_BEGIN 172074cc835SBarry Smith #undef __FUNCT__ 173074cc835SBarry Smith #define __FUNCT__ "SNESShellSetSolve_Shell" 174074cc835SBarry Smith PetscErrorCode SNESShellSetSolve_Shell(SNES snes,PetscErrorCode (*solve)(SNES,Vec)) 175074cc835SBarry Smith { 176074cc835SBarry Smith SNES_Shell *shell = (SNES_Shell*)snes->data; 177074cc835SBarry Smith 178074cc835SBarry Smith PetscFunctionBegin; 179074cc835SBarry Smith shell->solve = solve; 180074cc835SBarry Smith PetscFunctionReturn(0); 181074cc835SBarry Smith } 182074cc835SBarry Smith EXTERN_C_END 183074cc835SBarry Smith 184074cc835SBarry Smith /*MC 185074cc835SBarry Smith SNESSHELL - a user provided nonlinear solver 186074cc835SBarry Smith 187074cc835SBarry Smith Level: advanced 188074cc835SBarry Smith 189074cc835SBarry Smith .seealso: SNESCreate(), SNES, SNESSetType(), SNESType (for list of available types) 190074cc835SBarry Smith M*/ 191074cc835SBarry Smith 192074cc835SBarry Smith EXTERN_C_BEGIN 193074cc835SBarry Smith #undef __FUNCT__ 194074cc835SBarry Smith #define __FUNCT__ "SNESCreate_Shell" 195074cc835SBarry Smith PetscErrorCode SNESCreate_Shell(SNES snes) 196074cc835SBarry Smith { 197074cc835SBarry Smith SNES_Shell *shell; 198074cc835SBarry Smith PetscErrorCode ierr; 199074cc835SBarry Smith 200074cc835SBarry Smith PetscFunctionBegin; 201074cc835SBarry Smith snes->ops->destroy = SNESDestroy_Shell; 202074cc835SBarry Smith snes->ops->setup = SNESSetUp_Shell; 203074cc835SBarry Smith snes->ops->setfromoptions = SNESSetFromOptions_Shell; 204074cc835SBarry Smith snes->ops->view = SNESView_Shell; 205074cc835SBarry Smith snes->ops->solve = SNESSolve_Shell; 206074cc835SBarry Smith snes->ops->reset = SNESReset_Shell; 207074cc835SBarry Smith 208074cc835SBarry Smith snes->usesksp = PETSC_FALSE; 20942f4f86dSBarry Smith snes->usespc = PETSC_FALSE; 210074cc835SBarry Smith 211074cc835SBarry Smith ierr = PetscNewLog(snes, SNES_Shell, &shell);CHKERRQ(ierr); 212074cc835SBarry Smith snes->data = (void*) shell; 213074cc835SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)snes,"SNESShellSetSolve_C","SNESShellSetSolve_Shell",SNESShellSetSolve_Shell);CHKERRQ(ierr); 214074cc835SBarry Smith PetscFunctionReturn(0); 215074cc835SBarry Smith } 216074cc835SBarry Smith EXTERN_C_END 217