Actual source code: linesearchshell.c
1: #include <petsc/private/linesearchimpl.h>
2: #include <petsc/private/snesimpl.h>
4: typedef struct {
5: SNESLineSearchUserFunc func;
6: void *ctx;
7: } SNESLineSearch_Shell;
9: /*@C
10: SNESLineSearchShellSetUserFunc - Sets the user function for the SNESLineSearch Shell implementation.
12: Not Collective
14: Input Parameters:
15: + linesearch - SNESLineSearch context
16: . func - function implementing the linesearch shell.
17: - ctx - context for func
19: Calling sequence of func:
20: + linesearch - the linesearch instance
21: - ctx - the above mentioned context
23: Usage:
25: $ PetscErrorCode shellfunc(SNESLineSearch linesearch,void * ctx)
26: $ {
27: $ Vec X,Y,F,W,G;
28: $ SNES snes;
29: $ SNESLineSearchGetSNES(linesearch,&snes);
30: $ SNESLineSearchSetReason(linesearch,SNES_LINESEARCH_SUCCEEDED);
31: $ SNESLineSearchGetVecs(linesearch,&X,&F,&Y,&W,&G);
32: $ .. determine lambda using W and G as work vecs..
33: $ VecAXPY(X,-lambda,Y);
34: $ SNESComputeFunction(snes,X,F);
35: $ SNESLineSearchComputeNorms(linesearch);
36: $ return 0;
37: $ }
38: $
39: $ ...
40: $
41: $ SNESGetLineSearch(snes, &linesearch);
42: $ SNESLineSearchSetType(linesearch, SNESLINESEARCHSHELL);
43: $ SNESLineSearchShellSetUserFunc(linesearch, shellfunc, NULL);
45: Level: advanced
47: .seealso: SNESLineSearchShellGetUserFunc(), SNESLINESEARCHSHELL
48: @*/
49: PetscErrorCode SNESLineSearchShellSetUserFunc(SNESLineSearch linesearch, SNESLineSearchUserFunc func, void *ctx)
50: {
51: PetscBool flg;
52: SNESLineSearch_Shell *shell = (SNESLineSearch_Shell*)linesearch->data;
55: PetscObjectTypeCompare((PetscObject)linesearch,SNESLINESEARCHSHELL,&flg);
56: if (flg) {
57: shell->ctx = ctx;
58: shell->func = func;
59: }
60: return 0;
61: }
63: /*@C
64: SNESLineSearchShellGetUserFunc - Gets the user function and context for the shell implementation.
66: Not Collective
68: Input Parameter:
69: . linesearch - the line search object
71: Output Parameters:
72: + func - the user function; can be NULL if you do not want it
73: - ctx - the user function context; can be NULL if you do not want it
75: Level: advanced
77: .seealso: SNESLineSearchShellSetUserFunc()
78: @*/
79: PetscErrorCode SNESLineSearchShellGetUserFunc(SNESLineSearch linesearch, SNESLineSearchUserFunc *func, void **ctx)
80: {
81: PetscBool flg;
82: SNESLineSearch_Shell *shell = (SNESLineSearch_Shell*)linesearch->data;
87: PetscObjectTypeCompare((PetscObject)linesearch,SNESLINESEARCHSHELL,&flg);
88: if (flg) {
89: if (func) *func = shell->func;
90: if (ctx) *ctx = shell->ctx;
91: }
92: return 0;
93: }
95: static PetscErrorCode SNESLineSearchApply_Shell(SNESLineSearch linesearch)
96: {
97: SNESLineSearch_Shell *shell = (SNESLineSearch_Shell*)linesearch->data;
99: /* apply the user function */
100: if (shell->func) {
101: (*shell->func)(linesearch, shell->ctx);
102: } else SETERRQ(PetscObjectComm((PetscObject)linesearch), PETSC_ERR_USER, "SNESLineSearchShell needs to have a shell function set with SNESLineSearchShellSetUserFunc");
103: return 0;
104: }
106: static PetscErrorCode SNESLineSearchDestroy_Shell(SNESLineSearch linesearch)
107: {
108: SNESLineSearch_Shell *shell = (SNESLineSearch_Shell*)linesearch->data;
110: PetscFree(shell);
111: return 0;
112: }
114: /*MC
115: SNESLINESEARCHSHELL - Provides context for a user-provided line search routine.
117: The user routine has one argument, the SNESLineSearch context. The user uses the interface to
118: extract line search parameters and set them accordingly when the computation is finished.
120: Any of the other line searches may serve as a guide to how this is to be done. There is also a basic
121: template in the documentation for SNESLineSearchShellSetUserFunc().
123: Level: advanced
125: M*/
126: PETSC_EXTERN PetscErrorCode SNESLineSearchCreate_Shell(SNESLineSearch linesearch)
127: {
129: SNESLineSearch_Shell *shell;
131: linesearch->ops->apply = SNESLineSearchApply_Shell;
132: linesearch->ops->destroy = SNESLineSearchDestroy_Shell;
133: linesearch->ops->setfromoptions = NULL;
134: linesearch->ops->reset = NULL;
135: linesearch->ops->view = NULL;
136: linesearch->ops->setup = NULL;
138: PetscNewLog(linesearch,&shell);
140: linesearch->data = (void*) shell;
141: return 0;
142: }