Actual source code: linesearchshell.c

petsc-3.3-p7 2013-05-11
  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: $     Vec  X, Y, F, W, G;
 30: $     SNES snes;
 32: $     SNESLineSearchGetSNES(linesearch, &snes);
 33: $     SNESLineSearchSetSuccess(linesearch, PETSC_TRUE);
 34: $     SNESLineSearchGetVecs(linesearch, X, Y, F, W, G);
 35: $     .. determine lambda using W and G as work vecs..
 36: $     VecAXPY(X, -lambda, Y);
 37: $     SNESComputeFunction(snes, X, F);
 38: $     SNESLineSearchComputeNorms(linesearch);
 39: $     return(0);
 40: $  }
 41: $
 42: $  ...
 43: $
 44: $  SNESGetSNESLineSearch(snes, &linesearch);
 45: $  SNESLineSearchSetType(linesearch, SNESLINESEARCHSHELL);
 46: $  SNESLineSearchShellSetUserFunc(linesearch, shellfunc, PETSC_NULL);

 48:    Level: advanced

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

 52:    .seealso: SNESLineSearchShellGetUserFunc(), SNESLINESEARCHSHELL
 53: @*/
 54: PetscErrorCode SNESLineSearchShellSetUserFunc(SNESLineSearch linesearch, SNESLineSearchUserFunc func, void *ctx) {

 56:   PetscErrorCode   ierr;
 57:   PetscBool        flg;
 58:   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: }


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

 75:    Not Collective

 77:    Level: advanced

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

 81:    .seealso: SNESLineSearchShellSetUserFunc()
 82: @*/
 83: PetscErrorCode SNESLineSearchShellGetUserFunc(SNESLineSearch linesearch, SNESLineSearchUserFunc *func, void **ctx) {

 85:   PetscErrorCode   ierr;
 86:   PetscBool        flg;
 87:   SNESLineSearch_Shell *shell = (SNESLineSearch_Shell *)linesearch->data;
 92:   PetscObjectTypeCompare((PetscObject)linesearch,SNESLINESEARCHSHELL,&flg);
 93:   if (flg) {
 94:     *ctx  = shell->ctx;
 95:     *func = shell->func;
 96:   }
 97:   return(0);
 98: }


103: static PetscErrorCode  SNESLineSearchApply_Shell(SNESLineSearch linesearch)
104: {
105:   SNESLineSearch_Shell *shell = (SNESLineSearch_Shell *)linesearch->data;
106:   PetscErrorCode   ierr;


110:   /* apply the user function */
111:   if (shell->func) {
112:     (*shell->func)(linesearch, shell->ctx);
113:   } else {
114:     SETERRQ(((PetscObject)linesearch)->comm, PETSC_ERR_USER, "SNESLineSearchShell needs to have a shell function set with SNESLineSearchShellSetUserFunc");
115:   }
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*/
147: {

149:   SNESLineSearch_Shell     *shell;
150:   PetscErrorCode       ierr;


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

161:   PetscNewLog(linesearch, SNESLineSearch_Shell, &shell);
162:   linesearch->data = (void*) shell;
163:   return(0);
164: }