Actual source code: snesshell.c

  1: #include <petsc/private/snesimpl.h>

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

  5: /*@C
  6:    SNESShellSetSolve - Sets routine to apply as solver

  8:    Logically Collective on SNES

 10:    Input Parameters:
 11: +  snes - the nonlinear solver context
 12: -  apply - the application-provided solver routine

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

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

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

 25:    Level: advanced

 27: .seealso: SNESSHELL, SNESShellSetContext(), SNESShellGetContext()
 28: @*/
 29: PetscErrorCode  SNESShellSetSolve(SNES snes,PetscErrorCode (*solve)(SNES,Vec))
 30: {
 32:   PetscTryMethod(snes,"SNESShellSetSolve_C",(SNES,PetscErrorCode (*)(SNES,Vec)),(snes,solve));
 33:   return 0;
 34: }

 36: PetscErrorCode SNESReset_Shell(SNES snes)
 37: {
 38:   return 0;
 39: }

 41: PetscErrorCode SNESDestroy_Shell(SNES snes)
 42: {
 43:   SNESReset_Shell(snes);
 44:   PetscFree(snes->data);
 45:   return 0;
 46: }

 48: PetscErrorCode SNESSetUp_Shell(SNES snes)
 49: {
 50:   return 0;
 51: }

 53: PetscErrorCode SNESSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject,SNES snes)
 54: {
 55:   PetscOptionsHead(PetscOptionsObject,"SNES Shell options");
 56:   return 0;
 57: }

 59: PetscErrorCode SNESView_Shell(SNES snes, PetscViewer viewer)
 60: {
 61:   return 0;
 62: }

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

 67:     Not Collective

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

 72:     Output Parameter:
 73: .   ctx - the user provided context

 75:     Level: advanced

 77:     Notes:
 78:     This routine is intended for use within various shell routines

 80: .seealso: SNESCreateShell(), SNESShellSetContext()
 81: @*/
 82: PetscErrorCode  SNESShellGetContext(SNES snes,void *ctx)
 83: {
 84:   PetscBool      flg;

 88:   PetscObjectTypeCompare((PetscObject)snes,SNESSHELL,&flg);
 89:   if (!flg) *(void**)ctx = NULL;
 90:   else      *(void**)ctx = ((SNES_Shell*)(snes->data))->ctx;
 91:   return 0;
 92: }

 94: /*@
 95:     SNESShellSetContext - sets the context for a shell SNES

 97:    Logically Collective on SNES

 99:     Input Parameters:
100: +   snes - the shell SNES
101: -   ctx - the context

103:    Level: advanced

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

109: .seealso: SNESCreateShell(), SNESShellGetContext()
110: @*/
111: PetscErrorCode  SNESShellSetContext(SNES snes,void *ctx)
112: {
113:   SNES_Shell     *shell = (SNES_Shell*)snes->data;
114:   PetscBool      flg;

117:   PetscObjectTypeCompare((PetscObject)snes,SNESSHELL,&flg);
118:   if (flg) shell->ctx = ctx;
119:   return 0;
120: }

122: PetscErrorCode SNESSolve_Shell(SNES snes)
123: {
124:   SNES_Shell     *shell = (SNES_Shell*) snes->data;

127:   snes->reason = SNES_CONVERGED_ITS;
128:   (*shell->solve)(snes,snes->vec_sol);
129:   return 0;
130: }

132: PetscErrorCode  SNESShellSetSolve_Shell(SNES snes,PetscErrorCode (*solve)(SNES,Vec))
133: {
134:   SNES_Shell *shell = (SNES_Shell*)snes->data;

136:   shell->solve = solve;
137:   return 0;
138: }

140: /*MC
141:   SNESSHELL - a user provided nonlinear solver

143:    Level: advanced

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

148: PETSC_EXTERN PetscErrorCode SNESCreate_Shell(SNES snes)
149: {
150:   SNES_Shell     *shell;

152:   snes->ops->destroy        = SNESDestroy_Shell;
153:   snes->ops->setup          = SNESSetUp_Shell;
154:   snes->ops->setfromoptions = SNESSetFromOptions_Shell;
155:   snes->ops->view           = SNESView_Shell;
156:   snes->ops->solve          = SNESSolve_Shell;
157:   snes->ops->reset          = SNESReset_Shell;

159:   snes->usesksp = PETSC_FALSE;
160:   snes->usesnpc = PETSC_FALSE;

162:   snes->alwayscomputesfinalresidual = PETSC_FALSE;

164:   PetscNewLog(snes,&shell);
165:   snes->data = (void*) shell;
166:   PetscObjectComposeFunction((PetscObject)snes,"SNESShellSetSolve_C",SNESShellSetSolve_Shell);
167:   return 0;
168: }