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 22*95452b02SPatrick Sanan Notes: 23*95452b02SPatrick 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 .keywords: SNES, shell, set, apply, user-provided 28074cc835SBarry Smith 29074cc835SBarry Smith .seealso: SNESSHELL, SNESShellSetContext(), SNESShellGetContext() 30074cc835SBarry Smith @*/ 31074cc835SBarry Smith PetscErrorCode SNESShellSetSolve(SNES snes,PetscErrorCode (*solve)(SNES,Vec)) 32074cc835SBarry Smith { 33074cc835SBarry Smith PetscErrorCode ierr; 34074cc835SBarry Smith 35074cc835SBarry Smith PetscFunctionBegin; 36074cc835SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 37074cc835SBarry Smith ierr = PetscTryMethod(snes,"SNESShellSetSolve_C",(SNES,PetscErrorCode (*)(SNES,Vec)),(snes,solve));CHKERRQ(ierr); 38074cc835SBarry Smith PetscFunctionReturn(0); 39074cc835SBarry Smith } 40074cc835SBarry Smith 41074cc835SBarry Smith PetscErrorCode SNESReset_Shell(SNES snes) 42074cc835SBarry Smith { 43074cc835SBarry Smith PetscFunctionBegin; 44074cc835SBarry Smith PetscFunctionReturn(0); 45074cc835SBarry Smith } 46074cc835SBarry Smith 47074cc835SBarry Smith PetscErrorCode SNESDestroy_Shell(SNES snes) 48074cc835SBarry Smith { 49074cc835SBarry Smith PetscErrorCode ierr; 50074cc835SBarry Smith 51074cc835SBarry Smith PetscFunctionBegin; 52074cc835SBarry Smith ierr = SNESReset_Shell(snes);CHKERRQ(ierr); 5322d28d08SBarry Smith ierr = PetscFree(snes->data);CHKERRQ(ierr); 54074cc835SBarry Smith PetscFunctionReturn(0); 55074cc835SBarry Smith } 56074cc835SBarry Smith 57074cc835SBarry Smith PetscErrorCode SNESSetUp_Shell(SNES snes) 58074cc835SBarry Smith { 59074cc835SBarry Smith PetscFunctionBegin; 60074cc835SBarry Smith PetscFunctionReturn(0); 61074cc835SBarry Smith } 62074cc835SBarry Smith 634416b707SBarry Smith PetscErrorCode SNESSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject,SNES snes) 64074cc835SBarry Smith { 65074cc835SBarry Smith PetscErrorCode ierr; 66074cc835SBarry Smith 67074cc835SBarry Smith PetscFunctionBegin; 68e55864a3SBarry Smith ierr = PetscOptionsHead(PetscOptionsObject,"SNES Shell options");CHKERRQ(ierr); 69074cc835SBarry Smith PetscFunctionReturn(0); 70074cc835SBarry Smith } 71074cc835SBarry Smith 72074cc835SBarry Smith PetscErrorCode SNESView_Shell(SNES snes, PetscViewer viewer) 73074cc835SBarry Smith { 74074cc835SBarry Smith PetscFunctionBegin; 75074cc835SBarry Smith PetscFunctionReturn(0); 76074cc835SBarry Smith } 77074cc835SBarry Smith 7890b77ac2SPeter Brune /*@ 79074cc835SBarry Smith SNESShellGetContext - Returns the user-provided context associated with a shell SNES 80074cc835SBarry Smith 81074cc835SBarry Smith Not Collective 82074cc835SBarry Smith 83074cc835SBarry Smith Input Parameter: 84074cc835SBarry Smith . snes - should have been created with SNESSetType(snes,SNESSHELL); 85074cc835SBarry Smith 86074cc835SBarry Smith Output Parameter: 87074cc835SBarry Smith . ctx - the user provided context 88074cc835SBarry Smith 89074cc835SBarry Smith Level: advanced 90074cc835SBarry Smith 91074cc835SBarry Smith Notes: 92074cc835SBarry Smith This routine is intended for use within various shell routines 93074cc835SBarry Smith 94074cc835SBarry Smith .keywords: SNES, shell, get, context 95074cc835SBarry Smith 96074cc835SBarry Smith .seealso: SNESCreateShell(), SNESShellSetContext() 97074cc835SBarry Smith @*/ 98074cc835SBarry Smith PetscErrorCode SNESShellGetContext(SNES snes,void **ctx) 99074cc835SBarry Smith { 100074cc835SBarry Smith PetscErrorCode ierr; 101074cc835SBarry Smith PetscBool flg; 102074cc835SBarry Smith 103074cc835SBarry Smith PetscFunctionBegin; 104074cc835SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 105074cc835SBarry Smith PetscValidPointer(ctx,2); 106251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)snes,SNESSHELL,&flg);CHKERRQ(ierr); 107074cc835SBarry Smith if (!flg) *ctx = 0; 108074cc835SBarry Smith else *ctx = ((SNES_Shell*)(snes->data))->ctx; 109074cc835SBarry Smith PetscFunctionReturn(0); 110074cc835SBarry Smith } 111074cc835SBarry Smith 112074cc835SBarry Smith /*@ 113074cc835SBarry Smith SNESShellSetContext - sets the context for a shell SNES 114074cc835SBarry Smith 115074cc835SBarry Smith Logically Collective on SNES 116074cc835SBarry Smith 117074cc835SBarry Smith Input Parameters: 118074cc835SBarry Smith + snes - the shell SNES 119074cc835SBarry Smith - ctx - the context 120074cc835SBarry Smith 121074cc835SBarry Smith Level: advanced 122074cc835SBarry Smith 123*95452b02SPatrick Sanan Fortran Notes: 124*95452b02SPatrick Sanan The context can only be an integer or a PetscObject 125074cc835SBarry Smith unfortunately it cannot be a Fortran array or derived type. 126074cc835SBarry Smith 127074cc835SBarry Smith 128074cc835SBarry Smith .seealso: SNESCreateShell(), SNESShellGetContext() 129074cc835SBarry Smith @*/ 130074cc835SBarry Smith PetscErrorCode SNESShellSetContext(SNES snes,void *ctx) 131074cc835SBarry Smith { 132074cc835SBarry Smith SNES_Shell *shell = (SNES_Shell*)snes->data; 133074cc835SBarry Smith PetscErrorCode ierr; 134074cc835SBarry Smith PetscBool flg; 135074cc835SBarry Smith 136074cc835SBarry Smith PetscFunctionBegin; 137074cc835SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 138251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)snes,SNESSHELL,&flg);CHKERRQ(ierr); 1391aa26658SKarl Rupp if (flg) shell->ctx = ctx; 140074cc835SBarry Smith PetscFunctionReturn(0); 141074cc835SBarry Smith } 142074cc835SBarry Smith 143074cc835SBarry Smith PetscErrorCode SNESSolve_Shell(SNES snes) 144074cc835SBarry Smith { 145074cc835SBarry Smith SNES_Shell *shell = (SNES_Shell*) snes->data; 146074cc835SBarry Smith PetscErrorCode ierr; 147074cc835SBarry Smith 148074cc835SBarry Smith PetscFunctionBegin; 149ce94432eSBarry Smith if (!shell->solve) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"Must call SNESShellSetSolve() first"); 1501e633543SBarry Smith snes->reason = SNES_CONVERGED_ITS; 151074cc835SBarry Smith ierr = (*shell->solve)(snes,snes->vec_sol);CHKERRQ(ierr); 152074cc835SBarry Smith PetscFunctionReturn(0); 153074cc835SBarry Smith } 154074cc835SBarry Smith 155074cc835SBarry Smith PetscErrorCode SNESShellSetSolve_Shell(SNES snes,PetscErrorCode (*solve)(SNES,Vec)) 156074cc835SBarry Smith { 157074cc835SBarry Smith SNES_Shell *shell = (SNES_Shell*)snes->data; 158074cc835SBarry Smith 159074cc835SBarry Smith PetscFunctionBegin; 160074cc835SBarry Smith shell->solve = solve; 161074cc835SBarry Smith PetscFunctionReturn(0); 162074cc835SBarry Smith } 163074cc835SBarry Smith 164074cc835SBarry Smith /*MC 165074cc835SBarry Smith SNESSHELL - a user provided nonlinear solver 166074cc835SBarry Smith 167074cc835SBarry Smith Level: advanced 168074cc835SBarry Smith 169074cc835SBarry Smith .seealso: SNESCreate(), SNES, SNESSetType(), SNESType (for list of available types) 170074cc835SBarry Smith M*/ 171074cc835SBarry Smith 1728cc058d9SJed Brown PETSC_EXTERN PetscErrorCode SNESCreate_Shell(SNES snes) 173074cc835SBarry Smith { 174074cc835SBarry Smith SNES_Shell *shell; 175074cc835SBarry Smith PetscErrorCode ierr; 176074cc835SBarry Smith 177074cc835SBarry Smith PetscFunctionBegin; 178074cc835SBarry Smith snes->ops->destroy = SNESDestroy_Shell; 179074cc835SBarry Smith snes->ops->setup = SNESSetUp_Shell; 180074cc835SBarry Smith snes->ops->setfromoptions = SNESSetFromOptions_Shell; 181074cc835SBarry Smith snes->ops->view = SNESView_Shell; 182074cc835SBarry Smith snes->ops->solve = SNESSolve_Shell; 183074cc835SBarry Smith snes->ops->reset = SNESReset_Shell; 184074cc835SBarry Smith 185074cc835SBarry Smith snes->usesksp = PETSC_FALSE; 186efd4aadfSBarry Smith snes->usesnpc = PETSC_FALSE; 187074cc835SBarry Smith 1884fc747eaSLawrence Mitchell snes->alwayscomputesfinalresidual = PETSC_FALSE; 1894fc747eaSLawrence Mitchell 190b00a9115SJed Brown ierr = PetscNewLog(snes,&shell);CHKERRQ(ierr); 191074cc835SBarry Smith snes->data = (void*) shell; 192bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)snes,"SNESShellSetSolve_C",SNESShellSetSolve_Shell);CHKERRQ(ierr); 193074cc835SBarry Smith PetscFunctionReturn(0); 194074cc835SBarry Smith } 195