Actual source code: snesshell.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  1: #include <petsc/private/snesimpl.h>             /*I   "petscsnes.h"   I*/

  3: typedef struct {PetscErrorCode (*solve)(SNES,Vec);void *ctx;} SNES_Shell;

  7: /*@C
  8:    SNESShellSetSolve - Sets routine to apply as solver

 10:    Logically Collective on SNES

 12:    Input Parameters:
 13: +  snes - the nonlinear solver context
 14: -  apply - the application-provided solver routine

 16:    Calling sequence of solve:
 17: .vb
 18:    PetscErrorCode apply (SNES snes,Vec xout)
 19: .ve

 21: +  snes - the preconditioner, get the application context with SNESShellGetContext()
 22: -  xout - solution vector

 24:    Notes: the function MUST return an error code of 0 on success and nonzero on failure.

 26:    Level: advanced

 28: .keywords: SNES, shell, set, apply, user-provided

 30: .seealso: SNESSHELL, SNESShellSetContext(), SNESShellGetContext()
 31: @*/
 32: PetscErrorCode  SNESShellSetSolve(SNES snes,PetscErrorCode (*solve)(SNES,Vec))
 33: {

 38:   PetscTryMethod(snes,"SNESShellSetSolve_C",(SNES,PetscErrorCode (*)(SNES,Vec)),(snes,solve));
 39:   return(0);
 40: }

 44: PetscErrorCode SNESReset_Shell(SNES snes)
 45: {
 47:   return(0);
 48: }

 52: PetscErrorCode SNESDestroy_Shell(SNES snes)
 53: {

 57:   SNESReset_Shell(snes);
 58:   PetscFree(snes->data);
 59:   return(0);
 60: }

 64: PetscErrorCode SNESSetUp_Shell(SNES snes)
 65: {
 67:   return(0);
 68: }

 72: PetscErrorCode SNESSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject,SNES snes)
 73: {

 77:   PetscOptionsHead(PetscOptionsObject,"SNES Shell options");
 78:   return(0);
 79: }

 83: PetscErrorCode SNESView_Shell(SNES snes, PetscViewer viewer)
 84: {
 86:   return(0);
 87: }

 91: /*@
 92:     SNESShellGetContext - Returns the user-provided context associated with a shell SNES

 94:     Not Collective

 96:     Input Parameter:
 97: .   snes - should have been created with SNESSetType(snes,SNESSHELL);

 99:     Output Parameter:
100: .   ctx - the user provided context

102:     Level: advanced

104:     Notes:
105:     This routine is intended for use within various shell routines

107: .keywords: SNES, shell, get, context

109: .seealso: SNESCreateShell(), SNESShellSetContext()
110: @*/
111: PetscErrorCode  SNESShellGetContext(SNES snes,void **ctx)
112: {
114:   PetscBool      flg;

119:   PetscObjectTypeCompare((PetscObject)snes,SNESSHELL,&flg);
120:   if (!flg) *ctx = 0;
121:   else      *ctx = ((SNES_Shell*)(snes->data))->ctx;
122:   return(0);
123: }

127: /*@
128:     SNESShellSetContext - sets the context for a shell SNES

130:    Logically Collective on SNES

132:     Input Parameters:
133: +   snes - the shell SNES
134: -   ctx - the context

136:    Level: advanced

138:    Fortran Notes: The context can only be an integer or a PetscObject
139:       unfortunately it cannot be a Fortran array or derived type.


142: .seealso: SNESCreateShell(), SNESShellGetContext()
143: @*/
144: PetscErrorCode  SNESShellSetContext(SNES snes,void *ctx)
145: {
146:   SNES_Shell     *shell = (SNES_Shell*)snes->data;
148:   PetscBool      flg;

152:   PetscObjectTypeCompare((PetscObject)snes,SNESSHELL,&flg);
153:   if (flg) shell->ctx = ctx;
154:   return(0);
155: }

159: PetscErrorCode SNESSolve_Shell(SNES snes)
160: {
161:   SNES_Shell     *shell = (SNES_Shell*) snes->data;

165:   if (!shell->solve) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"Must call SNESShellSetSolve() first");
166:   snes->reason = SNES_CONVERGED_ITS;
167:   (*shell->solve)(snes,snes->vec_sol);
168:   return(0);
169: }

173: PetscErrorCode  SNESShellSetSolve_Shell(SNES snes,PetscErrorCode (*solve)(SNES,Vec))
174: {
175:   SNES_Shell *shell = (SNES_Shell*)snes->data;

178:   shell->solve = solve;
179:   return(0);
180: }

182: /*MC
183:   SNESSHELL - a user provided nonlinear solver

185:    Level: advanced

187: .seealso: SNESCreate(), SNES, SNESSetType(), SNESType (for list of available types)
188: M*/

192: PETSC_EXTERN PetscErrorCode SNESCreate_Shell(SNES snes)
193: {
194:   SNES_Shell     *shell;

198:   snes->ops->destroy        = SNESDestroy_Shell;
199:   snes->ops->setup          = SNESSetUp_Shell;
200:   snes->ops->setfromoptions = SNESSetFromOptions_Shell;
201:   snes->ops->view           = SNESView_Shell;
202:   snes->ops->solve          = SNESSolve_Shell;
203:   snes->ops->reset          = SNESReset_Shell;

205:   snes->usesksp = PETSC_FALSE;
206:   snes->usespc  = PETSC_FALSE;

208:   PetscNewLog(snes,&shell);
209:   snes->data = (void*) shell;
210:   PetscObjectComposeFunction((PetscObject)snes,"SNESShellSetSolve_C",SNESShellSetSolve_Shell);
211:   return(0);
212: }