Actual source code: snesshell.c

petsc-3.9.2 2018-05-20
Report Typos and Errors
  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: the function MUST return an error code of 0 on success and nonzero on failure.

 24:    Level: advanced

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

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

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

 40: PetscErrorCode SNESReset_Shell(SNES snes)
 41: {
 43:   return(0);
 44: }

 46: PetscErrorCode SNESDestroy_Shell(SNES snes)
 47: {

 51:   SNESReset_Shell(snes);
 52:   PetscFree(snes->data);
 53:   return(0);
 54: }

 56: PetscErrorCode SNESSetUp_Shell(SNES snes)
 57: {
 59:   return(0);
 60: }

 62: PetscErrorCode SNESSetFromOptions_Shell(PetscOptionItems *PetscOptionsObject,SNES snes)
 63: {

 67:   PetscOptionsHead(PetscOptionsObject,"SNES Shell options");
 68:   return(0);
 69: }

 71: PetscErrorCode SNESView_Shell(SNES snes, PetscViewer viewer)
 72: {
 74:   return(0);
 75: }

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

 80:     Not Collective

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

 85:     Output Parameter:
 86: .   ctx - the user provided context

 88:     Level: advanced

 90:     Notes:
 91:     This routine is intended for use within various shell routines

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

 95: .seealso: SNESCreateShell(), SNESShellSetContext()
 96: @*/
 97: PetscErrorCode  SNESShellGetContext(SNES snes,void **ctx)
 98: {
100:   PetscBool      flg;

105:   PetscObjectTypeCompare((PetscObject)snes,SNESSHELL,&flg);
106:   if (!flg) *ctx = 0;
107:   else      *ctx = ((SNES_Shell*)(snes->data))->ctx;
108:   return(0);
109: }

111: /*@
112:     SNESShellSetContext - sets the context for a shell SNES

114:    Logically Collective on SNES

116:     Input Parameters:
117: +   snes - the shell SNES
118: -   ctx - the context

120:    Level: advanced

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


126: .seealso: SNESCreateShell(), SNESShellGetContext()
127: @*/
128: PetscErrorCode  SNESShellSetContext(SNES snes,void *ctx)
129: {
130:   SNES_Shell     *shell = (SNES_Shell*)snes->data;
132:   PetscBool      flg;

136:   PetscObjectTypeCompare((PetscObject)snes,SNESSHELL,&flg);
137:   if (flg) shell->ctx = ctx;
138:   return(0);
139: }

141: PetscErrorCode SNESSolve_Shell(SNES snes)
142: {
143:   SNES_Shell     *shell = (SNES_Shell*) snes->data;

147:   if (!shell->solve) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"Must call SNESShellSetSolve() first");
148:   snes->reason = SNES_CONVERGED_ITS;
149:   (*shell->solve)(snes,snes->vec_sol);
150:   return(0);
151: }

153: PetscErrorCode  SNESShellSetSolve_Shell(SNES snes,PetscErrorCode (*solve)(SNES,Vec))
154: {
155:   SNES_Shell *shell = (SNES_Shell*)snes->data;

158:   shell->solve = solve;
159:   return(0);
160: }

162: /*MC
163:   SNESSHELL - a user provided nonlinear solver

165:    Level: advanced

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

170: PETSC_EXTERN PetscErrorCode SNESCreate_Shell(SNES snes)
171: {
172:   SNES_Shell     *shell;

176:   snes->ops->destroy        = SNESDestroy_Shell;
177:   snes->ops->setup          = SNESSetUp_Shell;
178:   snes->ops->setfromoptions = SNESSetFromOptions_Shell;
179:   snes->ops->view           = SNESView_Shell;
180:   snes->ops->solve          = SNESSolve_Shell;
181:   snes->ops->reset          = SNESReset_Shell;

183:   snes->usesksp = PETSC_FALSE;
184:   snes->usesnpc = PETSC_FALSE;

186:   snes->alwayscomputesfinalresidual = PETSC_FALSE;

188:   PetscNewLog(snes,&shell);
189:   snes->data = (void*) shell;
190:   PetscObjectComposeFunction((PetscObject)snes,"SNESShellSetSolve_C",SNESShellSetSolve_Shell);
191:   return(0);
192: }