Actual source code: snesshell.c

petsc-3.11.4 2019-09-28
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:
 23:     the function MUST return an error code of 0 on success and nonzero on failure.

 25:    Level: advanced

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

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

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

 41: PetscErrorCode SNESReset_Shell(SNES snes)
 42: {
 44:   return(0);
 45: }

 47: PetscErrorCode SNESDestroy_Shell(SNES snes)
 48: {

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

 57: PetscErrorCode SNESSetUp_Shell(SNES snes)
 58: {
 60:   return(0);
 61: }

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

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

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

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

 81:     Not Collective

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

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

 89:     Level: advanced

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

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

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

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

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

115:    Logically Collective on SNES

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

121:    Level: advanced

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


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

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

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

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

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

160:   shell->solve = solve;
161:   return(0);
162: }

164: /*MC
165:   SNESSHELL - a user provided nonlinear solver

167:    Level: advanced

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

172: PETSC_EXTERN PetscErrorCode SNESCreate_Shell(SNES snes)
173: {
174:   SNES_Shell     *shell;

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

185:   snes->usesksp = PETSC_FALSE;
186:   snes->usesnpc = PETSC_FALSE;

188:   snes->alwayscomputesfinalresidual = PETSC_FALSE;

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