Actual source code: linesearchshell.c
petsc-3.14.6 2021-03-30
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;
30: $ SNESLineSearchGetSNES(linesearch,&snes);
31: $ SNESLineSearchSetReason(linesearch,SNES_LINESEARCH_SUCCEEDED);
32: $ SNESLineSearchGetVecs(linesearch,&X,&F,&Y,&W,&G);
33: $ .. determine lambda using W and G as work vecs..
34: $ VecAXPY(X,-lambda,Y);
35: $ SNESComputeFunction(snes,X,F);
36: $ SNESLineSearchComputeNorms(linesearch);
37: $ return(0);
38: $ }
39: $
40: $ ...
41: $
42: $ SNESGetLineSearch(snes, &linesearch);
43: $ SNESLineSearchSetType(linesearch, SNESLINESEARCHSHELL);
44: $ SNESLineSearchShellSetUserFunc(linesearch, shellfunc, NULL);
46: Level: advanced
48: .seealso: SNESLineSearchShellGetUserFunc(), SNESLINESEARCHSHELL
49: @*/
50: PetscErrorCode SNESLineSearchShellSetUserFunc(SNESLineSearch linesearch, SNESLineSearchUserFunc func, void *ctx)
51: {
52: PetscErrorCode ierr;
53: PetscBool flg;
54: SNESLineSearch_Shell *shell = (SNESLineSearch_Shell*)linesearch->data;
58: PetscObjectTypeCompare((PetscObject)linesearch,SNESLINESEARCHSHELL,&flg);
59: if (flg) {
60: shell->ctx = ctx;
61: shell->func = func;
62: }
63: return(0);
64: }
66: /*@C
67: SNESLineSearchShellGetUserFunc - Gets the user function and context for the shell implementation.
69: Not Collective
71: Input Parameter:
72: . linesearch - the line search object
74: Output Parameters:
75: + func - the user function; can be NULL if you do not want it
76: - ctx - the user function context; can be NULL if you do not want it
78: Level: advanced
80: .seealso: SNESLineSearchShellSetUserFunc()
81: @*/
82: PetscErrorCode SNESLineSearchShellGetUserFunc(SNESLineSearch linesearch, SNESLineSearchUserFunc *func, void **ctx)
83: {
84: PetscErrorCode ierr;
85: PetscBool flg;
86: SNESLineSearch_Shell *shell = (SNESLineSearch_Shell*)linesearch->data;
92: PetscObjectTypeCompare((PetscObject)linesearch,SNESLINESEARCHSHELL,&flg);
93: if (flg) {
94: if (func) *func = shell->func;
95: if (ctx) *ctx = shell->ctx;
96: }
97: return(0);
98: }
100: static PetscErrorCode SNESLineSearchApply_Shell(SNESLineSearch linesearch)
101: {
102: SNESLineSearch_Shell *shell = (SNESLineSearch_Shell*)linesearch->data;
103: PetscErrorCode ierr;
106: /* apply the user function */
107: if (shell->func) {
108: (*shell->func)(linesearch, shell->ctx);
109: } else SETERRQ(PetscObjectComm((PetscObject)linesearch), PETSC_ERR_USER, "SNESLineSearchShell needs to have a shell function set with SNESLineSearchShellSetUserFunc");
110: return(0);
111: }
113: static PetscErrorCode SNESLineSearchDestroy_Shell(SNESLineSearch linesearch)
114: {
115: SNESLineSearch_Shell *shell = (SNESLineSearch_Shell*)linesearch->data;
116: PetscErrorCode ierr;
119: PetscFree(shell);
120: return(0);
121: }
123: /*MC
124: SNESLINESEARCHSHELL - Provides context for a user-provided line search routine.
126: The user routine has one argument, the SNESLineSearch context. The user uses the interface to
127: extract line search parameters and set them accordingly when the computation is finished.
129: Any of the other line searches may serve as a guide to how this is to be done. There is also a basic
130: template in the documentation for SNESLineSearchShellSetUserFunc().
132: Level: advanced
134: M*/
135: PETSC_EXTERN PetscErrorCode SNESLineSearchCreate_Shell(SNESLineSearch linesearch)
136: {
138: SNESLineSearch_Shell *shell;
139: PetscErrorCode ierr;
142: linesearch->ops->apply = SNESLineSearchApply_Shell;
143: linesearch->ops->destroy = SNESLineSearchDestroy_Shell;
144: linesearch->ops->setfromoptions = NULL;
145: linesearch->ops->reset = NULL;
146: linesearch->ops->view = NULL;
147: linesearch->ops->setup = NULL;
149: PetscNewLog(linesearch,&shell);
151: linesearch->data = (void*) shell;
152: return(0);
153: }