Actual source code: snesshell.c

petsc-3.13.6 2020-09-29
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 Section 1.5 Writing Application Codes with PETSc-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 Section 1.5 Writing Application Codes with PETSc 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: {

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

 39: PetscErrorCode SNESReset_Shell(SNES snes)
 40: {
 42:   return(0);
 43: }

 45: PetscErrorCode SNESDestroy_Shell(SNES snes)
 46: {

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

 55: PetscErrorCode SNESSetUp_Shell(SNES snes)
 56: {
 58:   return(0);
 59: }

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

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

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

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

 79:     Not Collective

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

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

 87:     Level: advanced

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

 92: .seealso: SNESCreateShell(), SNESShellSetContext()
 93: @*/
 94: PetscErrorCode  SNESShellGetContext(SNES snes,void **ctx)
 95: {
 97:   PetscBool      flg;

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

108: /*@
109:     SNESShellSetContext - sets the context for a shell SNES

111:    Logically Collective on SNES

113:     Input Parameters:
114: +   snes - the shell SNES
115: -   ctx - the context

117:    Level: advanced

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


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

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

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

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

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

156:   shell->solve = solve;
157:   return(0);
158: }

160: /*MC
161:   SNESSHELL - a user provided nonlinear solver

163:    Level: advanced

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

168: PETSC_EXTERN PetscErrorCode SNESCreate_Shell(SNES snes)
169: {
170:   SNES_Shell     *shell;

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

181:   snes->usesksp = PETSC_FALSE;
182:   snes->usesnpc = PETSC_FALSE;

184:   snes->alwayscomputesfinalresidual = PETSC_FALSE;

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