Actual source code: linesearchshell.c

petsc-3.6.1 2015-08-06
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;

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

 15:    Not Collective

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

 22:    Calling sequence of func:
 23: +  linesearch - the linesearch instance
 24: -  ctx - the above mentioned context

 26:    Usage:

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

 49:    Level: advanced

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

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

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


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

 77:    Not Collective

 79:    Level: advanced

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

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

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


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

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

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

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

133: /*MC

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

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

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

143: Level: advanced

145: M*/
146: PETSC_EXTERN PetscErrorCode SNESLineSearchCreate_Shell(SNESLineSearch linesearch)
147: {

149:   SNESLineSearch_Shell *shell;
150:   PetscErrorCode       ierr;

153:   linesearch->ops->apply          = SNESLineSearchApply_Shell;
154:   linesearch->ops->destroy        = SNESLineSearchDestroy_Shell;
155:   linesearch->ops->setfromoptions = NULL;
156:   linesearch->ops->reset          = NULL;
157:   linesearch->ops->view           = NULL;
158:   linesearch->ops->setup          = NULL;

160:   PetscNewLog(linesearch,&shell);

162:   linesearch->data = (void*) shell;
163:   return(0);
164: }