Actual source code: snesshell.c

petsc-3.3-p7 2013-05-11
  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(SNES snes)
 73: {
 75: 
 77:   PetscOptionsHead("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
106:     
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) {
154:     shell->ctx = ctx;
155:   }
156:   return(0);
157: }

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

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

173: EXTERN_C_BEGIN
176: PetscErrorCode  SNESShellSetSolve_Shell(SNES snes,PetscErrorCode (*solve)(SNES,Vec))
177: {
178:   SNES_Shell *shell = (SNES_Shell*)snes->data;

181:   shell->solve = solve;
182:   return(0);
183: }
184: EXTERN_C_END

186: /*MC
187:   SNESSHELL - a user provided nonlinear solver 

189:    Level: advanced

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

194: EXTERN_C_BEGIN
197: PetscErrorCode SNESCreate_Shell(SNES snes)
198: {
199:   SNES_Shell     *shell;

203:   snes->ops->destroy        = SNESDestroy_Shell;
204:   snes->ops->setup          = SNESSetUp_Shell;
205:   snes->ops->setfromoptions = SNESSetFromOptions_Shell;
206:   snes->ops->view           = SNESView_Shell;
207:   snes->ops->solve          = SNESSolve_Shell;
208:   snes->ops->reset          = SNESReset_Shell;

210:   snes->usesksp             = PETSC_FALSE;
211:   snes->usespc              = PETSC_FALSE;

213:   PetscNewLog(snes, SNES_Shell, &shell);
214:   snes->data = (void*) shell;
215:   PetscObjectComposeFunctionDynamic((PetscObject)snes,"SNESShellSetSolve_C","SNESShellSetSolve_Shell",SNESShellSetSolve_Shell);
216:   return(0);
217: }
218: EXTERN_C_END