1af0996ceSBarry Smith #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 /*@C 6074cc835SBarry Smith SNESShellSetSolve - Sets routine to apply as solver 7074cc835SBarry Smith 8074cc835SBarry Smith Logically Collective on SNES 9074cc835SBarry Smith 10074cc835SBarry Smith Input Parameters: 11074cc835SBarry Smith + snes - the nonlinear solver context 12074cc835SBarry Smith - apply - the application-provided solver routine 13074cc835SBarry Smith 14074cc835SBarry Smith Calling sequence of solve: 15074cc835SBarry Smith .vb 16074cc835SBarry Smith PetscErrorCode apply (SNES snes,Vec xout) 17074cc835SBarry Smith .ve 18074cc835SBarry Smith 19074cc835SBarry Smith + snes - the preconditioner, get the application context with SNESShellGetContext() 20074cc835SBarry Smith - xout - solution vector 21074cc835SBarry Smith 2295452b02SPatrick Sanan Notes: 2395452b02SPatrick Sanan the function MUST return an error code of 0 on success and nonzero on failure. 24074cc835SBarry Smith 25074cc835SBarry Smith Level: advanced 26074cc835SBarry Smith 27074cc835SBarry Smith .seealso: SNESSHELL, SNESShellSetContext(), SNESShellGetContext() 28074cc835SBarry Smith @*/ 29074cc835SBarry Smith PetscErrorCode SNESShellSetSolve(SNES snes,PetscErrorCode (*solve)(SNES,Vec)) 30074cc835SBarry Smith { 31074cc835SBarry Smith PetscErrorCode ierr; 32074cc835SBarry Smith 33074cc835SBarry Smith PetscFunctionBegin; 34074cc835SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 35074cc835SBarry Smith ierr = PetscTryMethod(snes,"SNESShellSetSolve_C",(SNES,PetscErrorCode (*)(SNES,Vec)),(snes,solve));CHKERRQ(ierr); 36074cc835SBarry Smith PetscFunctionReturn(0); 37074cc835SBarry Smith } 38074cc835SBarry Smith 39074cc835SBarry Smith PetscErrorCode SNESReset_Shell(SNES snes) 40074cc835SBarry Smith { 41074cc835SBarry Smith PetscFunctionBegin; 42074cc835SBarry Smith PetscFunctionReturn(0); 43074cc835SBarry Smith } 44074cc835SBarry Smith 45074cc835SBarry Smith PetscErrorCode SNESDestroy_Shell(SNES snes) 46074cc835SBarry Smith { 47074cc835SBarry Smith PetscErrorCode ierr; 48074cc835SBarry Smith 49074cc835SBarry Smith PetscFunctionBegin; 50074cc835SBarry Smith ierr = SNESReset_Shell(snes);CHKERRQ(ierr); 5122d28d08SBarry Smith ierr = PetscFree(snes->data);CHKERRQ(ierr); 52074cc835SBarry Smith PetscFunctionReturn(0); 53074cc835SBarry Smith } 54074cc835SBarry Smith 55074cc835SBarry Smith PetscErrorCode SNESSetUp_Shell(SNES snes) 56074cc835SBarry Smith { 57074cc835SBarry Smith PetscFunctionBegin; 58074cc835SBarry Smith PetscFunctionReturn(0); 59074cc835SBarry Smith } 60074cc835SBarry Smith 614416b707SBarry Smith PetscErrorCode SNESSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject,SNES snes) 62074cc835SBarry Smith { 63074cc835SBarry Smith PetscErrorCode ierr; 64074cc835SBarry Smith 65074cc835SBarry Smith PetscFunctionBegin; 66e55864a3SBarry Smith ierr = PetscOptionsHead(PetscOptionsObject,"SNES Shell options");CHKERRQ(ierr); 67074cc835SBarry Smith PetscFunctionReturn(0); 68074cc835SBarry Smith } 69074cc835SBarry Smith 70074cc835SBarry Smith PetscErrorCode SNESView_Shell(SNES snes, PetscViewer viewer) 71074cc835SBarry Smith { 72074cc835SBarry Smith PetscFunctionBegin; 73074cc835SBarry Smith PetscFunctionReturn(0); 74074cc835SBarry Smith } 75074cc835SBarry Smith 7690b77ac2SPeter Brune /*@ 77074cc835SBarry Smith SNESShellGetContext - Returns the user-provided context associated with a shell SNES 78074cc835SBarry Smith 79074cc835SBarry Smith Not Collective 80074cc835SBarry Smith 81074cc835SBarry Smith Input Parameter: 82074cc835SBarry Smith . snes - should have been created with SNESSetType(snes,SNESSHELL); 83074cc835SBarry Smith 84074cc835SBarry Smith Output Parameter: 85074cc835SBarry Smith . ctx - the user provided context 86074cc835SBarry Smith 87074cc835SBarry Smith Level: advanced 88074cc835SBarry Smith 89074cc835SBarry Smith Notes: 90074cc835SBarry Smith This routine is intended for use within various shell routines 91074cc835SBarry Smith 92074cc835SBarry Smith .seealso: SNESCreateShell(), SNESShellSetContext() 93074cc835SBarry Smith @*/ 943ec1f749SStefano Zampini PetscErrorCode SNESShellGetContext(SNES snes,void *ctx) 95074cc835SBarry Smith { 96074cc835SBarry Smith PetscErrorCode ierr; 97074cc835SBarry Smith PetscBool flg; 98074cc835SBarry Smith 99074cc835SBarry Smith PetscFunctionBegin; 100074cc835SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 101074cc835SBarry Smith PetscValidPointer(ctx,2); 102251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)snes,SNESSHELL,&flg);CHKERRQ(ierr); 1033ec1f749SStefano Zampini if (!flg) *(void**)ctx = NULL; 1043ec1f749SStefano Zampini else *(void**)ctx = ((SNES_Shell*)(snes->data))->ctx; 105074cc835SBarry Smith PetscFunctionReturn(0); 106074cc835SBarry Smith } 107074cc835SBarry Smith 108074cc835SBarry Smith /*@ 109074cc835SBarry Smith SNESShellSetContext - sets the context for a shell SNES 110074cc835SBarry Smith 111074cc835SBarry Smith Logically Collective on SNES 112074cc835SBarry Smith 113074cc835SBarry Smith Input Parameters: 114074cc835SBarry Smith + snes - the shell SNES 115074cc835SBarry Smith - ctx - the context 116074cc835SBarry Smith 117074cc835SBarry Smith Level: advanced 118074cc835SBarry Smith 11995452b02SPatrick Sanan Fortran Notes: 12095452b02SPatrick Sanan The context can only be an integer or a PetscObject 121074cc835SBarry Smith unfortunately it cannot be a Fortran array or derived type. 122074cc835SBarry Smith 123074cc835SBarry Smith .seealso: SNESCreateShell(), SNESShellGetContext() 124074cc835SBarry Smith @*/ 125074cc835SBarry Smith PetscErrorCode SNESShellSetContext(SNES snes,void *ctx) 126074cc835SBarry Smith { 127074cc835SBarry Smith SNES_Shell *shell = (SNES_Shell*)snes->data; 128074cc835SBarry Smith PetscErrorCode ierr; 129074cc835SBarry Smith PetscBool flg; 130074cc835SBarry Smith 131074cc835SBarry Smith PetscFunctionBegin; 132074cc835SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 133251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)snes,SNESSHELL,&flg);CHKERRQ(ierr); 1341aa26658SKarl Rupp if (flg) shell->ctx = ctx; 135074cc835SBarry Smith PetscFunctionReturn(0); 136074cc835SBarry Smith } 137074cc835SBarry Smith 138074cc835SBarry Smith PetscErrorCode SNESSolve_Shell(SNES snes) 139074cc835SBarry Smith { 140074cc835SBarry Smith SNES_Shell *shell = (SNES_Shell*) snes->data; 141074cc835SBarry Smith PetscErrorCode ierr; 142074cc835SBarry Smith 143074cc835SBarry Smith PetscFunctionBegin; 144*2c71b3e2SJacob Faibussowitsch PetscCheckFalse(!shell->solve,PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"Must call SNESShellSetSolve() first"); 1451e633543SBarry Smith snes->reason = SNES_CONVERGED_ITS; 146074cc835SBarry Smith ierr = (*shell->solve)(snes,snes->vec_sol);CHKERRQ(ierr); 147074cc835SBarry Smith PetscFunctionReturn(0); 148074cc835SBarry Smith } 149074cc835SBarry Smith 150074cc835SBarry Smith PetscErrorCode SNESShellSetSolve_Shell(SNES snes,PetscErrorCode (*solve)(SNES,Vec)) 151074cc835SBarry Smith { 152074cc835SBarry Smith SNES_Shell *shell = (SNES_Shell*)snes->data; 153074cc835SBarry Smith 154074cc835SBarry Smith PetscFunctionBegin; 155074cc835SBarry Smith shell->solve = solve; 156074cc835SBarry Smith PetscFunctionReturn(0); 157074cc835SBarry Smith } 158074cc835SBarry Smith 159074cc835SBarry Smith /*MC 160074cc835SBarry Smith SNESSHELL - a user provided nonlinear solver 161074cc835SBarry Smith 162074cc835SBarry Smith Level: advanced 163074cc835SBarry Smith 164074cc835SBarry Smith .seealso: SNESCreate(), SNES, SNESSetType(), SNESType (for list of available types) 165074cc835SBarry Smith M*/ 166074cc835SBarry Smith 1678cc058d9SJed Brown PETSC_EXTERN PetscErrorCode SNESCreate_Shell(SNES snes) 168074cc835SBarry Smith { 169074cc835SBarry Smith SNES_Shell *shell; 170074cc835SBarry Smith PetscErrorCode ierr; 171074cc835SBarry Smith 172074cc835SBarry Smith PetscFunctionBegin; 173074cc835SBarry Smith snes->ops->destroy = SNESDestroy_Shell; 174074cc835SBarry Smith snes->ops->setup = SNESSetUp_Shell; 175074cc835SBarry Smith snes->ops->setfromoptions = SNESSetFromOptions_Shell; 176074cc835SBarry Smith snes->ops->view = SNESView_Shell; 177074cc835SBarry Smith snes->ops->solve = SNESSolve_Shell; 178074cc835SBarry Smith snes->ops->reset = SNESReset_Shell; 179074cc835SBarry Smith 180074cc835SBarry Smith snes->usesksp = PETSC_FALSE; 181efd4aadfSBarry Smith snes->usesnpc = PETSC_FALSE; 182074cc835SBarry Smith 1834fc747eaSLawrence Mitchell snes->alwayscomputesfinalresidual = PETSC_FALSE; 1844fc747eaSLawrence Mitchell 185b00a9115SJed Brown ierr = PetscNewLog(snes,&shell);CHKERRQ(ierr); 186074cc835SBarry Smith snes->data = (void*) shell; 187bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)snes,"SNESShellSetSolve_C",SNESShellSetSolve_Shell);CHKERRQ(ierr); 188074cc835SBarry Smith PetscFunctionReturn(0); 189074cc835SBarry Smith } 190