Actual source code: linesearchshell.c

petsc-3.9.4 2018-09-11
Report Typos and Errors
  1:  #include <petsc/private/linesearchimpl.h>
  2:  #include <petsc/private/snesimpl.h>


  5: typedef struct {
  6:   SNESLineSearchUserFunc func;
  7:   void                   *ctx;
  8: } SNESLineSearch_Shell;

 10: /*@C
 11:    SNESLineSearchShellSetUserFunc - Sets the user function for the SNESLineSearch Shell implementation.

 13:    Not Collective

 15:    Input Parameters:
 16: +  linesearch - SNESLineSearch context
 17: .  func - function implementing the linesearch shell.
 18: -  ctx - context for func

 20:    Calling sequence of func:
 21: +  linesearch - the linesearch instance
 22: -  ctx - the above mentioned context

 24:    Usage:

 26: $  PetscErrorCode shellfunc(SNESLineSearch linesearch,void * ctx)
 27: $  {
 28: $     Vec  X,Y,F,W,G;
 29: $     SNES snes;
 31: $     SNESLineSearchGetSNES(linesearch,&snes);
 32: $     SNESLineSearchSetReason(linesearch,SNES_LINESEARCH_SUCCEEDED);
 33: $     SNESLineSearchGetVecs(linesearch,&X,&F,&Y,&W,&G);
 34: $     .. determine lambda using W and G as work vecs..
 35: $     VecAXPY(X,-lambda,Y);
 36: $     SNESComputeFunction(snes,X,F);
 37: $     SNESLineSearchComputeNorms(linesearch);
 38: $     return(0);
 39: $  }
 40: $
 41: $  ...
 42: $
 43: $  SNESGetLineSearch(snes, &linesearch);
 44: $  SNESLineSearchSetType(linesearch, SNESLINESEARCHSHELL);
 45: $  SNESLineSearchShellSetUserFunc(linesearch, shellfunc, NULL);

 47:    Level: advanced

 49:    .keywords: SNESLineSearch, Shell, user, function, set

 51:    .seealso: SNESLineSearchShellGetUserFunc(), SNESLINESEARCHSHELL
 52: @*/
 53: PetscErrorCode SNESLineSearchShellSetUserFunc(SNESLineSearch linesearch, SNESLineSearchUserFunc func, void *ctx)
 54: {
 55:   PetscErrorCode       ierr;
 56:   PetscBool            flg;
 57:   SNESLineSearch_Shell *shell = (SNESLineSearch_Shell*)linesearch->data;

 61:   PetscObjectTypeCompare((PetscObject)linesearch,SNESLINESEARCHSHELL,&flg);
 62:   if (flg) {
 63:     shell->ctx  = ctx;
 64:     shell->func = func;
 65:   }
 66:   return(0);
 67: }


 70: /*@C
 71:    SNESLineSearchShellGetUserFunc - Gets the user function and context for the shell implementation.

 73:    Not Collective

 75:    Input Parameter:
 76: .     linesearch - the line search object

 78:    Output Parameters:
 79: +    func  - the user function; can be NULL if you do not want it
 80: -    ctx   - the user function context; can be NULL if you do not want it

 82:    Level: advanced

 84:    .keywords: SNESLineSearch, get, Shell, user, function

 86:    .seealso: SNESLineSearchShellSetUserFunc()
 87: @*/
 88: PetscErrorCode SNESLineSearchShellGetUserFunc(SNESLineSearch linesearch, SNESLineSearchUserFunc *func, void **ctx)
 89: {
 90:   PetscErrorCode       ierr;
 91:   PetscBool            flg;
 92:   SNESLineSearch_Shell *shell = (SNESLineSearch_Shell*)linesearch->data;

 98:   PetscObjectTypeCompare((PetscObject)linesearch,SNESLINESEARCHSHELL,&flg);
 99:   if (flg) {
100:     if (func) *func = shell->func;
101:     if (ctx) *ctx  = shell->ctx;
102:   }
103:   return(0);
104: }


107: static PetscErrorCode  SNESLineSearchApply_Shell(SNESLineSearch linesearch)
108: {
109:   SNESLineSearch_Shell *shell = (SNESLineSearch_Shell*)linesearch->data;
110:   PetscErrorCode       ierr;

113:   /* apply the user function */
114:   if (shell->func) {
115:     (*shell->func)(linesearch, shell->ctx);
116:   } else SETERRQ(PetscObjectComm((PetscObject)linesearch), PETSC_ERR_USER, "SNESLineSearchShell needs to have a shell function set with SNESLineSearchShellSetUserFunc");
117:   return(0);
118: }

120: static PetscErrorCode  SNESLineSearchDestroy_Shell(SNESLineSearch linesearch)
121: {
122:   SNESLineSearch_Shell *shell = (SNESLineSearch_Shell*)linesearch->data;
123:   PetscErrorCode       ierr;

126:   PetscFree(shell);
127:   return(0);
128: }

130: /*MC

132: SNESLINESEARCHSHELL - Provides context for a user-provided line search routine.

134: The user routine has one argument, the SNESLineSearch context.  The user uses the interface to
135: extract line search parameters and set them accordingly when the computation is finished.

137: Any of the other line searches may serve as a guide to how this is to be done.  There is also a basic
138: template in the documentation for SNESLineSearchShellSetUserFunc().

140: Level: advanced

142: M*/
143: PETSC_EXTERN PetscErrorCode SNESLineSearchCreate_Shell(SNESLineSearch linesearch)
144: {

146:   SNESLineSearch_Shell *shell;
147:   PetscErrorCode       ierr;

150:   linesearch->ops->apply          = SNESLineSearchApply_Shell;
151:   linesearch->ops->destroy        = SNESLineSearchDestroy_Shell;
152:   linesearch->ops->setfromoptions = NULL;
153:   linesearch->ops->reset          = NULL;
154:   linesearch->ops->view           = NULL;
155:   linesearch->ops->setup          = NULL;

157:   PetscNewLog(linesearch,&shell);

159:   linesearch->data = (void*) shell;
160:   return(0);
161: }