Actual source code: snesshell.c

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

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

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

 11:    Logically Collective

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

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

 22: +  snes - the preconditioner, get the application context with `SNESShellGetContext()` provided with `SNESShelletContext()`
 23: -  xout - solution vector

 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(SNES snes, PetscOptionItems *PetscOptionsObject)
 54: {
 55:   PetscOptionsHeadBegin(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 `SNESSHELL`

 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: .seealso: `SNESSHELL`, `SNESCreateShell()`, `SNESShellSetContext()`
 78: @*/
 79: PetscErrorCode SNESShellGetContext(SNES snes, void *ctx)
 80: {
 81:   PetscBool flg;

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

 91: /*@
 92:     SNESShellSetContext - sets the context for a `SNESSHELL`

 94:    Logically Collective

 96:     Input Parameters:
 97: +   snes - the `SNESSHELL`
 98: -   ctx - the context

100:    Level: advanced

102:    Fortran Note:
103:    The context can only be an integer or a `PetscObject` it cannot be a Fortran array or derived type.

105: .seealso: `SNESSHELL`, `SNESCreateShell()`, `SNESShellGetContext()`
106: @*/
107: PetscErrorCode SNESShellSetContext(SNES snes, void *ctx)
108: {
109:   SNES_Shell *shell = (SNES_Shell *)snes->data;
110:   PetscBool   flg;

113:   PetscObjectTypeCompare((PetscObject)snes, SNESSHELL, &flg);
114:   if (flg) shell->ctx = ctx;
115:   return 0;
116: }

118: PetscErrorCode SNESSolve_Shell(SNES snes)
119: {
120:   SNES_Shell *shell = (SNES_Shell *)snes->data;

123:   snes->reason = SNES_CONVERGED_ITS;
124:   (*shell->solve)(snes, snes->vec_sol);
125:   return 0;
126: }

128: PetscErrorCode SNESShellSetSolve_Shell(SNES snes, PetscErrorCode (*solve)(SNES, Vec))
129: {
130:   SNES_Shell *shell = (SNES_Shell *)snes->data;

132:   shell->solve = solve;
133:   return 0;
134: }

136: /*MC
137:   SNESSHELL - a user provided nonlinear solver

139:    Level: advanced

141: .seealso: `SNESCreate()`, `SNES`, `SNESSetType()`, `SNESType`, `SNESShellGetContext()`, `SNESShellSetContext()`, `SNESShellSetSolve()`
142: M*/

144: PETSC_EXTERN PetscErrorCode SNESCreate_Shell(SNES snes)
145: {
146:   SNES_Shell *shell;

148:   snes->ops->destroy        = SNESDestroy_Shell;
149:   snes->ops->setup          = SNESSetUp_Shell;
150:   snes->ops->setfromoptions = SNESSetFromOptions_Shell;
151:   snes->ops->view           = SNESView_Shell;
152:   snes->ops->solve          = SNESSolve_Shell;
153:   snes->ops->reset          = SNESReset_Shell;

155:   snes->usesksp = PETSC_FALSE;
156:   snes->usesnpc = PETSC_FALSE;

158:   snes->alwayscomputesfinalresidual = PETSC_FALSE;

160:   PetscNew(&shell);
161:   snes->data = (void *)shell;
162:   PetscObjectComposeFunction((PetscObject)snes, "SNESShellSetSolve_C", SNESShellSetSolve_Shell);
163:   return 0;
164: }