xref: /petsc/src/tao/linesearch/impls/unit/unit.c (revision ef46b1a67e276116c83b5d4ce8efc2932ea4fc0a)
1 
2 #include <petsc/private/taolinesearchimpl.h>
3 
4 static PetscErrorCode TaoLineSearchDestroy_Unit(TaoLineSearch ls)
5 {
6   PetscFunctionBegin;
7   PetscFunctionReturn(0);
8 }
9 
10 static PetscErrorCode TaoLineSearchSetFromOptions_Unit(PetscOptionItems *PetscOptionsObject,TaoLineSearch ls)
11 {
12   PetscFunctionBegin;
13   PetscFunctionReturn(0);
14 }
15 
16 static PetscErrorCode TaoLineSearchView_Unit(TaoLineSearch ls,PetscViewer viewer)
17 {
18   PetscBool      isascii;
19 
20   PetscFunctionBegin;
21   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
22   if (isascii) {
23     PetscCall(PetscViewerASCIIPrintf(viewer,"  Line Search: Unit Step %g.\n",ls->initstep));
24   }
25   PetscFunctionReturn(0);
26 }
27 
28 /* Take unit step (newx = startx + initstep*step_direction) */
29 static PetscErrorCode TaoLineSearchApply_Unit(TaoLineSearch ls,Vec x,PetscReal *f,Vec g,Vec step_direction)
30 {
31   PetscFunctionBegin;
32   PetscCall(TaoLineSearchMonitor(ls,0,*f,0.0));
33   ls->step = ls->initstep;
34   PetscCall(VecAXPY(x,ls->step,step_direction));
35   PetscCall(TaoLineSearchComputeObjectiveAndGradient(ls,x,f,g));
36   PetscCall(TaoLineSearchMonitor(ls,1,*f,ls->step));
37   ls->reason = TAOLINESEARCH_SUCCESS;
38   PetscFunctionReturn(0);
39 }
40 
41 /*MC
42    TAOLINESEARCHUNIT - Line-search type that disables line search and accepts the unit step length every time
43 
44   Options Database Keys:
45 . -tao_ls_stepinit <step> - steplength
46 
47    Level: developer
48 
49 .seealso: TaoLineSearchCreate(), TaoLineSearchSetType(), TaoLineSearchApply()
50 
51 .keywords: Tao, linesearch
52 M*/
53 PETSC_EXTERN PetscErrorCode TaoLineSearchCreate_Unit(TaoLineSearch ls)
54 {
55   PetscFunctionBegin;
56   ls->ops->setup = NULL;
57   ls->ops->reset = NULL;
58   ls->ops->monitor = NULL;
59   ls->ops->apply = TaoLineSearchApply_Unit;
60   ls->ops->view = TaoLineSearchView_Unit;
61   ls->ops->destroy = TaoLineSearchDestroy_Unit;
62   ls->ops->setfromoptions = TaoLineSearchSetFromOptions_Unit;
63   PetscFunctionReturn(0);
64 }
65