1 #ifndef __TAO_IMPL_H
2 #define __TAO_IMPL_H
3 
4 #include <petsctao.h>
5 #include <petsctaolinesearch.h>
6 #include <petsc/private/petscimpl.h>
7 
8 PETSC_EXTERN PetscBool TaoRegisterAllCalled;
9 PETSC_EXTERN PetscErrorCode TaoRegisterAll(void);
10 
11 typedef struct _TaoOps *TaoOps;
12 
13 struct _TaoOps {
14   /* Methods set by application */
15     PetscErrorCode (*computeobjective)(Tao, Vec, PetscReal*, void*);
16     PetscErrorCode (*computeobjectiveandgradient)(Tao, Vec, PetscReal*, Vec, void*);
17     PetscErrorCode (*computegradient)(Tao, Vec, Vec, void*);
18     PetscErrorCode (*computehessian)(Tao, Vec, Mat, Mat,  void*);
19     PetscErrorCode (*computeresidual)(Tao, Vec, Vec, void*);
20     PetscErrorCode (*computeresidualjacobian)(Tao, Vec, Mat, Mat, void*);
21     PetscErrorCode (*computeconstraints)(Tao, Vec, Vec, void*);
22     PetscErrorCode (*computeinequalityconstraints)(Tao, Vec, Vec, void*);
23     PetscErrorCode (*computeequalityconstraints)(Tao, Vec, Vec, void*);
24     PetscErrorCode (*computejacobian)(Tao, Vec, Mat, Mat,  void*);
25     PetscErrorCode (*computejacobianstate)(Tao, Vec, Mat, Mat, Mat,  void*);
26     PetscErrorCode (*computejacobiandesign)(Tao, Vec, Mat, void*);
27     PetscErrorCode (*computejacobianinequality)(Tao, Vec, Mat, Mat,  void*);
28     PetscErrorCode (*computejacobianequality)(Tao, Vec, Mat, Mat,  void*);
29     PetscErrorCode (*computebounds)(Tao, Vec, Vec, void*);
30     PetscErrorCode (*update)(Tao,PetscInt,void*);
31     PetscErrorCode (*convergencetest)(Tao,void*);
32     PetscErrorCode (*convergencedestroy)(void*);
33 
34   /* Methods set by solver */
35     PetscErrorCode (*computedual)(Tao, Vec, Vec);
36     PetscErrorCode (*setup)(Tao);
37     PetscErrorCode (*solve)(Tao);
38     PetscErrorCode (*view)(Tao, PetscViewer);
39     PetscErrorCode (*setfromoptions)(PetscOptionItems*,Tao);
40     PetscErrorCode (*destroy)(Tao);
41 };
42 
43 #define MAXTAOMONITORS 10
44 
45 struct _p_Tao {
46     PETSCHEADER(struct _TaoOps);
47     void *user;
48     void *user_objP;
49     void *user_objgradP;
50     void *user_gradP;
51     void *user_hessP;
52     void *user_lsresP;
53     void *user_lsjacP;
54     void *user_conP;
55     void *user_con_equalityP;
56     void *user_con_inequalityP;
57     void *user_jacP;
58     void *user_jac_equalityP;
59     void *user_jac_inequalityP;
60     void *user_jac_stateP;
61     void *user_jac_designP;
62     void *user_boundsP;
63     void *user_update;
64 
65     PetscErrorCode (*monitor[MAXTAOMONITORS])(Tao,void*);
66     PetscErrorCode (*monitordestroy[MAXTAOMONITORS])(void**);
67     void *monitorcontext[MAXTAOMONITORS];
68     PetscInt numbermonitors;
69     void *cnvP;
70     TaoConvergedReason reason;
71 
72     PetscBool setupcalled;
73     void *data;
74 
75     Vec solution;
76     Vec gradient;
77     Vec stepdirection;
78     Vec XL;
79     Vec XU;
80     Vec IL;
81     Vec IU;
82     Vec DI;
83     Vec DE;
84     Mat hessian;
85     Mat hessian_pre;
86     Mat gradient_norm;
87     Vec gradient_norm_tmp;
88     Vec ls_res;
89     Mat ls_jac;
90     Mat ls_jac_pre;
91     Vec res_weights_v;
92     PetscInt res_weights_n;
93     PetscInt *res_weights_rows;
94     PetscInt *res_weights_cols;
95     PetscReal *res_weights_w;
96     Vec constraints;
97     Vec constraints_equality;
98     Vec constraints_inequality;
99     Mat jacobian;
100     Mat jacobian_pre;
101     Mat jacobian_inequality;
102     Mat jacobian_inequality_pre;
103     Mat jacobian_equality;
104     Mat jacobian_equality_pre;
105     Mat jacobian_state;
106     Mat jacobian_state_inv;
107     Mat jacobian_design;
108     Mat jacobian_state_pre;
109     Mat jacobian_design_pre;
110     IS state_is;
111     IS design_is;
112     PetscReal step;
113     PetscReal residual;
114     PetscReal gnorm0;
115     PetscReal cnorm;
116     PetscReal cnorm0;
117     PetscReal fc;
118 
119 
120     PetscInt  max_it;
121     PetscInt  max_funcs;
122     PetscInt  max_constraints;
123     PetscInt  nfuncs;
124     PetscInt  ngrads;
125     PetscInt  nfuncgrads;
126     PetscInt  nhess;
127     PetscInt  niter;
128     PetscInt  ntotalits;
129     PetscInt  nconstraints;
130     PetscInt  niconstraints;
131     PetscInt  neconstraints;
132     PetscInt  njac;
133     PetscInt  njac_equality;
134     PetscInt  njac_inequality;
135     PetscInt  njac_state;
136     PetscInt  njac_design;
137 
138     PetscInt  ksp_its; /* KSP iterations for this solver iteration */
139     PetscInt  ksp_tot_its; /* Total (cumulative) KSP iterations */
140 
141 
142     TaoLineSearch linesearch;
143     PetscBool lsflag; /* goes up when line search fails */
144     KSP ksp;
145     PetscReal trust0; /* initial trust region radius */
146     PetscReal trust;  /* Current trust region */
147 
148     PetscReal gatol;
149     PetscReal grtol;
150     PetscReal gttol;
151     PetscReal catol;
152     PetscReal crtol;
153     PetscReal steptol;
154     PetscReal fmin;
155     PetscBool max_funcs_changed;
156     PetscBool max_it_changed;
157     PetscBool gatol_changed;
158     PetscBool grtol_changed;
159     PetscBool gttol_changed;
160     PetscBool fmin_changed;
161     PetscBool catol_changed;
162     PetscBool crtol_changed;
163     PetscBool steptol_changed;
164     PetscBool trust0_changed;
165     PetscBool printreason;
166     PetscBool viewsolution;
167     PetscBool viewgradient;
168     PetscBool viewconstraints;
169     PetscBool viewhessian;
170     PetscBool viewjacobian;
171     PetscBool bounded;
172     PetscBool constrained;
173     PetscBool header_printed;
174 
175     TaoSubsetType subset_type;
176     PetscInt      hist_max;/* Number of iteration histories to keep */
177     PetscReal     *hist_obj; /* obj value at each iteration */
178     PetscReal     *hist_resid; /* residual at each iteration */
179     PetscReal     *hist_cnorm; /* constraint norm at each iteration */
180     PetscInt      *hist_lits; /* number of ksp its at each TAO iteration */
181     PetscInt      hist_len;
182     PetscBool     hist_reset;
183     PetscBool     hist_malloc;
184 };
185 
186 PETSC_EXTERN PetscLogEvent TAO_Solve;
187 PETSC_EXTERN PetscLogEvent TAO_ObjectiveEval;
188 PETSC_EXTERN PetscLogEvent TAO_GradientEval;
189 PETSC_EXTERN PetscLogEvent TAO_ObjGradEval;
190 PETSC_EXTERN PetscLogEvent TAO_HessianEval;
191 PETSC_EXTERN PetscLogEvent TAO_ConstraintsEval;
192 PETSC_EXTERN PetscLogEvent TAO_JacobianEval;
193 
TaoLogConvergenceHistory(Tao tao,PetscReal obj,PetscReal resid,PetscReal cnorm,PetscInt totits)194 PETSC_STATIC_INLINE PetscErrorCode TaoLogConvergenceHistory(Tao tao, PetscReal obj, PetscReal resid, PetscReal cnorm, PetscInt totits)
195 {
196   PetscFunctionBegin;
197   if (tao->hist_max > tao->hist_len) {
198     if (tao->hist_obj) tao->hist_obj[tao->hist_len]=obj;
199     if (tao->hist_resid) tao->hist_resid[tao->hist_len]=resid;
200     if (tao->hist_cnorm) tao->hist_cnorm[tao->hist_len]=cnorm;
201     if (tao->hist_lits) {
202       if (tao->hist_len <= 0) {
203         tao->hist_lits[0] = totits;
204       } else {
205         tao->hist_lits[tao->hist_len]=totits - tao->hist_lits[tao->hist_len-1];
206       }
207     }
208     tao->hist_len++;
209   }
210   PetscFunctionReturn(0);
211 }
212 
213 PETSC_INTERN PetscErrorCode TaoVecGetSubVec(Vec, IS, TaoSubsetType, PetscReal, Vec*);
214 PETSC_INTERN PetscErrorCode TaoMatGetSubMat(Mat, IS, Vec, TaoSubsetType, Mat*);
215 PETSC_INTERN PetscErrorCode TaoGradientNorm(Tao, Vec, NormType, PetscReal*);
216 PETSC_INTERN PetscErrorCode TaoEstimateActiveBounds(Vec, Vec, Vec, Vec, Vec, Vec, PetscReal, PetscReal*, IS*, IS*, IS*, IS*, IS*);
217 PETSC_INTERN PetscErrorCode TaoBoundStep(Vec, Vec, Vec, IS, IS, IS, PetscReal, Vec);
218 PETSC_INTERN PetscErrorCode TaoBoundSolution(Vec, Vec, Vec, PetscReal, PetscInt*, Vec);
219 
220 #endif
221