Actual source code: taoimpl.h

  1: #pragma once

  3: #include <petsctao.h>
  4: #include <petsctaolinesearch.h>
  5: #include <petsc/private/petscimpl.h>

  7: PETSC_EXTERN PetscBool      TaoRegisterAllCalled;
  8: PETSC_EXTERN PetscErrorCode TaoRegisterAll(void);

 10: typedef struct _TaoOps *TaoOps;

 12: struct _TaoOps {
 13:   /* Methods set by application */
 14:   PetscErrorCode (*computeobjective)(Tao, Vec, PetscReal *, void *);
 15:   PetscErrorCode (*computeobjectiveandgradient)(Tao, Vec, PetscReal *, Vec, void *);
 16:   PetscErrorCode (*computegradient)(Tao, Vec, Vec, void *);
 17:   PetscErrorCode (*computehessian)(Tao, Vec, Mat, Mat, void *);
 18:   PetscErrorCode (*computeresidual)(Tao, Vec, Vec, void *);
 19:   PetscErrorCode (*computeresidualjacobian)(Tao, Vec, Mat, Mat, void *);
 20:   PetscErrorCode (*computeconstraints)(Tao, Vec, Vec, void *);
 21:   PetscErrorCode (*computeinequalityconstraints)(Tao, Vec, Vec, void *);
 22:   PetscErrorCode (*computeequalityconstraints)(Tao, Vec, Vec, void *);
 23:   PetscErrorCode (*computejacobian)(Tao, Vec, Mat, Mat, void *);
 24:   PetscErrorCode (*computejacobianstate)(Tao, Vec, Mat, Mat, Mat, void *);
 25:   PetscErrorCode (*computejacobiandesign)(Tao, Vec, Mat, void *);
 26:   PetscErrorCode (*computejacobianinequality)(Tao, Vec, Mat, Mat, void *);
 27:   PetscErrorCode (*computejacobianequality)(Tao, Vec, Mat, Mat, void *);
 28:   PetscErrorCode (*computebounds)(Tao, Vec, Vec, void *);
 29:   PetscErrorCode (*update)(Tao, PetscInt, void *);
 30:   PetscErrorCode (*convergencetest)(Tao, void *);
 31:   PetscErrorCode (*convergencedestroy)(void *);

 33:   /* Methods set by solver */
 34:   PetscErrorCode (*computedual)(Tao, Vec, Vec);
 35:   PetscErrorCode (*setup)(Tao);
 36:   PetscErrorCode (*solve)(Tao);
 37:   PetscErrorCode (*view)(Tao, PetscViewer);
 38:   PetscErrorCode (*setfromoptions)(Tao, PetscOptionItems *);
 39:   PetscErrorCode (*destroy)(Tao);
 40: };

 42: #define MAXTAOMONITORS 10

 44: struct _p_Tao {
 45:   PETSCHEADER(struct _TaoOps);
 46:   void *user;
 47:   void *user_objP;
 48:   void *user_objgradP;
 49:   void *user_gradP;
 50:   void *user_hessP;
 51:   void *user_lsresP;
 52:   void *user_lsjacP;
 53:   void *user_conP;
 54:   void *user_con_equalityP;
 55:   void *user_con_inequalityP;
 56:   void *user_jacP;
 57:   void *user_jac_equalityP;
 58:   void *user_jac_inequalityP;
 59:   void *user_jac_stateP;
 60:   void *user_jac_designP;
 61:   void *user_boundsP;
 62:   void *user_update;

 64:   PetscErrorCode (*monitor[MAXTAOMONITORS])(Tao, void *);
 65:   PetscErrorCode (*monitordestroy[MAXTAOMONITORS])(void **);
 66:   void              *monitorcontext[MAXTAOMONITORS];
 67:   PetscInt           numbermonitors;
 68:   void              *cnvP;
 69:   TaoConvergedReason reason;

 71:   PetscBool setupcalled;
 72:   void     *data;

 74:   Vec        solution;
 75:   Vec        gradient;
 76:   Vec        stepdirection;
 77:   Vec        XL;
 78:   Vec        XU;
 79:   Vec        IL;
 80:   Vec        IU;
 81:   Vec        DI;
 82:   Vec        DE;
 83:   Mat        hessian;
 84:   Mat        hessian_pre;
 85:   Mat        gradient_norm;
 86:   Vec        gradient_norm_tmp;
 87:   Vec        ls_res;
 88:   Mat        ls_jac;
 89:   Mat        ls_jac_pre;
 90:   Vec        res_weights_v;
 91:   PetscInt   res_weights_n;
 92:   PetscInt  *res_weights_rows;
 93:   PetscInt  *res_weights_cols;
 94:   PetscReal *res_weights_w;
 95:   Vec        constraints;
 96:   Vec        constraints_equality;
 97:   Vec        constraints_inequality;
 98:   Mat        jacobian;
 99:   Mat        jacobian_pre;
100:   Mat        jacobian_inequality;
101:   Mat        jacobian_inequality_pre;
102:   Mat        jacobian_equality;
103:   Mat        jacobian_equality_pre;
104:   Mat        jacobian_state;
105:   Mat        jacobian_state_inv;
106:   Mat        jacobian_design;
107:   Mat        jacobian_state_pre;
108:   Mat        jacobian_design_pre;
109:   IS         state_is;
110:   IS         design_is;
111:   PetscReal  step;
112:   PetscReal  residual;
113:   PetscReal  gnorm0;
114:   PetscReal  cnorm;
115:   PetscReal  cnorm0;
116:   PetscReal  fc;

118:   PetscInt max_it;
119:   PetscInt max_funcs;
120:   PetscInt max_constraints;
121:   PetscInt nfuncs;
122:   PetscInt ngrads;
123:   PetscInt nfuncgrads;
124:   PetscInt nhess;
125:   PetscInt niter;
126:   PetscInt ntotalits;
127:   PetscInt nconstraints;
128:   PetscInt niconstraints;
129:   PetscInt neconstraints;
130:   PetscInt njac;
131:   PetscInt njac_equality;
132:   PetscInt njac_inequality;
133:   PetscInt njac_state;
134:   PetscInt njac_design;

136:   PetscInt ksp_its;     /* KSP iterations for this solver iteration */
137:   PetscInt ksp_tot_its; /* Total (cumulative) KSP iterations */

139:   TaoLineSearch linesearch;
140:   PetscBool     lsflag; /* goes up when line search fails */
141:   KSP           ksp;
142:   PetscReal     trust0; /* initial trust region radius */
143:   PetscReal     trust;  /* Current trust region */

145:   /* EW type forcing term */
146:   PetscBool ksp_ewconv;
147:   SNES      snes_ewdummy;

149:   PetscReal gatol;
150:   PetscReal grtol;
151:   PetscReal gttol;
152:   PetscReal catol;
153:   PetscReal crtol;
154:   PetscReal steptol;
155:   PetscReal fmin;
156:   PetscBool max_funcs_changed;
157:   PetscBool max_it_changed;
158:   PetscBool gatol_changed;
159:   PetscBool grtol_changed;
160:   PetscBool gttol_changed;
161:   PetscBool fmin_changed;
162:   PetscBool catol_changed;
163:   PetscBool crtol_changed;
164:   PetscBool steptol_changed;
165:   PetscBool trust0_changed;
166:   PetscBool printreason;
167:   PetscBool viewsolution;
168:   PetscBool viewgradient;
169:   PetscBool viewconstraints;
170:   PetscBool viewhessian;
171:   PetscBool viewjacobian;
172:   PetscBool bounded;
173:   PetscBool constrained;
174:   PetscBool eq_constrained;
175:   PetscBool ineq_constrained;
176:   PetscBool ineq_doublesided;
177:   PetscBool header_printed;
178:   PetscBool recycle;

180:   TaoSubsetType subset_type;
181:   PetscInt      hist_max;   /* Number of iteration histories to keep */
182:   PetscReal    *hist_obj;   /* obj value at each iteration */
183:   PetscReal    *hist_resid; /* residual at each iteration */
184:   PetscReal    *hist_cnorm; /* constraint norm at each iteration */
185:   PetscInt     *hist_lits;  /* number of ksp its at each TAO iteration */
186:   PetscInt      hist_len;
187:   PetscBool     hist_reset;
188:   PetscBool     hist_malloc;
189: };

191: PETSC_EXTERN PetscLogEvent TAO_Solve;
192: PETSC_EXTERN PetscLogEvent TAO_ObjectiveEval;
193: PETSC_EXTERN PetscLogEvent TAO_GradientEval;
194: PETSC_EXTERN PetscLogEvent TAO_ObjGradEval;
195: PETSC_EXTERN PetscLogEvent TAO_HessianEval;
196: PETSC_EXTERN PetscLogEvent TAO_ConstraintsEval;
197: PETSC_EXTERN PetscLogEvent TAO_JacobianEval;

199: static inline PetscErrorCode TaoLogConvergenceHistory(Tao tao, PetscReal obj, PetscReal resid, PetscReal cnorm, PetscInt totits)
200: {
201:   PetscFunctionBegin;
202:   if (tao->hist_max > tao->hist_len) {
203:     if (tao->hist_obj) tao->hist_obj[tao->hist_len] = obj;
204:     if (tao->hist_resid) tao->hist_resid[tao->hist_len] = resid;
205:     if (tao->hist_cnorm) tao->hist_cnorm[tao->hist_len] = cnorm;
206:     if (tao->hist_lits) {
207:       PetscInt sits = totits;
208:       PetscCheck(tao->hist_len >= 0, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "History length cannot be negative");
209:       for (PetscInt i = 0; i < tao->hist_len; i++) sits -= tao->hist_lits[i];
210:       tao->hist_lits[tao->hist_len] = sits;
211:     }
212:     tao->hist_len++;
213:   }
214:   PetscFunctionReturn(PETSC_SUCCESS);
215: }