Cantera  2.3.0
ResidJacEval.h
Go to the documentation of this file.
1 /**
2  * @file ResidJacEval.h
3  *
4  * Dense, Square (not sparse) matrices.
5  */
6 
7 // This file is part of Cantera. See License.txt in the top-level directory or
8 // at http://www.cantera.org/license.txt for license and copyright information.
9 
10 #ifndef CT_RESIDJACEVAL_H
11 #define CT_RESIDJACEVAL_H
12 
13 #include "ResidEval.h"
14 #include "DenseMatrix.h"
15 
16 namespace Cantera
17 {
18 
19 //! Differentiates the type of residual evaluations according to functionality
21  //! Base residual calculation for the time-stepping function
23  //! Base residual calculation for the Jacobian calculation
25  //! Delta residual calculation for the Jacbobian calculation
27  //! Base residual calculation for the showSolution routine
28  /*!
29  * We calculate this when we want to display a solution
30  */
32  //! Base residual calculation containing any lagged components
33  /*!
34  * We use this to calculate residuals when doing line searches along
35  * irections determined by Jacobians that are missing contributions from
36  * lagged entries.
37  */
39 };
40 
41 //! Wrappers for the function evaluators for Nonlinear solvers and Time steppers
42 /*!
43  * @attention This class currently does not have any test cases or examples. Its
44  * implementation may be incomplete, and future changes to Cantera may
45  * unexpectedly cause this class to stop working. If you use this class,
46  * please consider contributing examples or test cases. In the absence of
47  * new tests or examples, this class may be deprecated and removed in a
48  * future version of Cantera. See
49  * https://github.com/Cantera/cantera/issues/267 for additional information.
50  *
51  * A class for full (non-sparse dense matrices with Fortran-compatible data
52  * storage. The class adds support for identifying what types of calls are made
53  * to the residual evaluator by adding the ResidEval_Type_Enum class.
54  */
55 class ResidJacEval : public ResidEval
56 {
57 public:
58  //!Default constructor
59  /*!
60  * @param atol Initial value of the global tolerance (defaults to 1.0E-13)
61  */
62  ResidJacEval(doublereal atol = 1.0e-13);
63 
64  //! @deprecated To be removed after Cantera 2.3.
65  ResidJacEval(const ResidJacEval& right);
66  //! @deprecated To be removed after Cantera 2.3.
67  ResidJacEval& operator=(const ResidJacEval& right);
68 
69  //! Duplication routine for objects derived from residJacEval
70  /*!
71  * This virtual routine can be used to duplicate objects which inherit from
72  * ResidJacEval even if the application only has a pointer to ResidJacEval
73  * to work with.
74  *
75  * These routines are basically wrappers around the derived copy
76  * constructor.
77  * @deprecated To be removed after Cantera 2.3.
78  */
79  virtual ResidJacEval* duplMyselfAsResidJacEval() const;
80 
81  //! Return the number of equations in the equation system
82  virtual int nEquations() const;
83 
84  //! Evaluate the residual function
85  /*!
86  * @param t Time (input)
87  * @param delta_t The current value of the time step (input)
88  * @param y Solution vector (input, do not modify)
89  * @param ydot Rate of change of solution vector. (input, do not modify)
90  * @param resid Value of the residual that is computed (output)
91  * @param evalType Type of the residual being computed (defaults to Base_ResidEval)
92  * @param id_x Index of the variable that is being numerically differenced to find
93  * the Jacobian (defaults to -1, which indicates that no variable is being
94  * differenced or that the residual doesn't take this issue into account)
95  * @param delta_x Value of the delta used in the numerical differencing
96  *
97  * @returns a flag to indicate that operation is successful.
98  * 1 Means a successful operation
99  * -0 or neg value Means an unsuccessful operation
100  */
101  virtual int evalResidNJ(const doublereal t, const doublereal delta_t,
102  const doublereal* const y,
103  const doublereal* const ydot,
104  doublereal* const resid,
105  const ResidEval_Type_Enum evalType = Base_ResidEval,
106  const int id_x = -1,
107  const doublereal delta_x = 0.0);
108 
109  virtual int eval(const doublereal t, const doublereal* const y,
110  const doublereal* const ydot,
111  doublereal* const r);
112 
113  virtual int getInitialConditions(const doublereal t0, doublereal* const y, doublereal* const ydot);
114 
115  //! Filter the solution predictions
116  /*!
117  * Codes might provide a predicted step change. This routine filters the
118  * predicted solution vector eliminating illegal directions.
119  *
120  * @param t Time (input)
121  * @param ybase Solution vector (input, output)
122  * @param step Proposed step in the solution that will be cropped
123  * @returns the norm of the amount of filtering
124  */
125  virtual doublereal filterNewStep(const doublereal t, const doublereal* const ybase,
126  doublereal* const step);
127 
128  //! Filter the solution predictions
129  /*!
130  * Codes might provide a predicted solution vector. This routine filters the
131  * predicted solution vector.
132  *
133  * @param t Time (input)
134  * @param y Solution vector (input, output)
135  * @returns the norm of the amount of filtering
136  */
137  virtual doublereal filterSolnPrediction(const doublereal t, doublereal* const y);
138 
139  //! Set a global value of the absolute tolerance
140  /*!
141  * @param atol Value of atol
142  */
143  void setAtol(doublereal atol);
144 
145  //! Evaluate the time tracking equations, if any
146  /*!
147  * Evaluate time integrated quantities that are calculated at the end of
148  * every successful time step. This call is made once at the end of every
149  * successful time step that advances the time. It's also made once at the
150  * start of the time stepping.
151  *
152  * @param t Time (input)
153  * @param delta_t The current value of the time step (input)
154  * @param y Solution vector (input, do not modify)
155  * @param ydot Rate of change of solution vector. (input, do not modify)
156  * @returns a flag to indicate that operation is successful.
157  * 1 Means a successful operation
158  * -0 or neg value Means an unsuccessful operation
159  */
160  virtual int evalTimeTrackingEqns(const doublereal t, const doublereal delta_t, const doublereal* const y,
161  const doublereal* const ydot);
162 
163  //! Evaluate any stopping criteria other than a final time limit
164  /*!
165  * If we are to stop the time integration for any reason other than reaching
166  * a final time limit, tout, provide a test here. This call is made at the
167  * end of every successful time step iteration
168  *
169  * @return If true, the the time stepping is stopped. If false, then time
170  * stepping is stopped if t >= tout Defaults to false.
171  * @param t Time (input)
172  * @param delta_t The current value of the time step (input)
173  * @param y Solution vector (input, do not modify)
174  * @param ydot Rate of change of solution vector. (input, do not modify)
175  */
176  virtual bool evalStoppingCritera(const doublereal t,
177  const doublereal delta_t,
178  const doublereal* const y,
179  const doublereal* const ydot);
180 
181  //! Return a vector of delta y's for calculation of the numerical Jacobian
182  /*!
183  * There is a default algorithm provided.
184  *
185  * delta_y[i] = atol[i] + 1.0E-6 ysoln[i]
186  * delta_y[i] = atol[i] + MAX(1.0E-6 ysoln[i] * 0.01 * solnWeights[i])
187  *
188  * @param t Time (input)
189  * @param y Solution vector (input, do not modify)
190  * @param ydot Rate of change of solution vector. (input, do not modify)
191  * @param delta_y Value of the delta to be used in calculating the numerical Jacobian
192  * @param solnWeights Value of the solution weights that are used in determining convergence (default = 0)
193  * @returns a flag to indicate that operation is successful.
194  * 1 Means a successful operation
195  * -0 or neg value Means an unsuccessful operation
196  */
197  virtual int
198  calcDeltaSolnVariables(const doublereal t,
199  const doublereal* const y,
200  const doublereal* const ydot,
201  doublereal* const delta_y,
202  const doublereal* const solnWeights = 0);
203 
204  //! Returns a vector of column scale factors that can be used to column
205  //! scale Jacobians.
206  /*!
207  * Default to yScales[] = 1.0
208  *
209  * @param t Time (input)
210  * @param y Solution vector (input, do not modify)
211  * @param y_old Old Solution vector (input, do not modify)
212  * @param yScales Value of the column scales
213  */
214  virtual void calcSolnScales(const doublereal t, const doublereal* const y,
215  const doublereal* const y_old, doublereal* const yScales);
216 
217  //! This function may be used to create output at various points in the
218  //! execution of an application.
219  /*!
220  * @param ifunc identity of the call
221  * 0 Initial call
222  * 1 Called at the end of every successful time step
223  * -1 Called at the end of every unsuccessful time step
224  * 2 Called at the end of every call to integrateRJE()
225  * @param t Time (input)
226  * @param delta_t The current value of the time step (input)
227  * @param y Solution vector (input, do not modify)
228  * @param ydot Rate of change of solution vector. (input)
229  */
230  virtual void user_out2(const int ifunc, const doublereal t,
231  const doublereal delta_t,
232  const doublereal* const y,
233  const doublereal* const ydot);
234 
235  //! This function may be used to create output at various points in the
236  //! execution of an application.
237  /*!
238  * This routine calls user_out2().
239  *
240  * @param ifunc identity of the call
241  * @param t Time (input)
242  * @param y Solution vector (input, do not modify)
243  * @param ydot Rate of change of solution vector. (input)
244  */
245  virtual void user_out(const int ifunc, const doublereal t,
246  const doublereal* y,
247  const doublereal* ydot);
248 
249  //! Multiply the matrix by another matrix that leads to better conditioning
250  /*!
251  * Provide a left sided matrix that will multiply the current Jacobian,
252  * after scaling and lead to a better conditioned system. This routine is
253  * called just before the matrix is factored.
254  *
255  * Original Problem:
256  * J delta_x = - Resid
257  *
258  * New problem:
259  * M (J delta_x) = - M Resid
260  *
261  * @param matrix Pointer to the current Jacobian (if zero, it's already been factored)
262  * @param nrows offsets for the matrix
263  * @param rhs residual vector. This also needs to be LHS multiplied by M
264  * @returns a flag to indicate that operation is successful.
265  * 1 Means a successful operation
266  * -0 or neg value Means an unsuccessful operation
267  */
268  virtual int matrixConditioning(doublereal* const matrix, const int nrows,
269  doublereal* const rhs);
270 
271  //! Calculate an analytical Jacobian and the residual at the current time
272  //! and values.
273  /*!
274  * Only called if the jacFormation method is set to analytical
275  *
276  * @param t Time (input)
277  * @param delta_t The current value of the time step (input)
278  * @param cj Coefficient of yprime used in the evaluation of the Jacobian
279  * @param y Solution vector (input, do not modify)
280  * @param ydot Rate of change of solution vector. (input, do not modify)
281  * @param J Reference to the DenseMatrix object to be calculated (output)
282  * @param resid Value of the residual that is computed (output)
283  * @returns a flag to indicate that operation is successful.
284  * 1 Means a successful operation
285  * -0 or neg value Means an unsuccessful operation
286  */
287  virtual int evalJacobian(const doublereal t, const doublereal delta_t, doublereal cj,
288  const doublereal* const y, const doublereal* const ydot,
289  DenseMatrix& J, doublereal* const resid);
290 
291  //! Calculate an analytical Jacobian and the residual at the current time and values.
292  /*!
293  * Only called if the jacFormation method is set to analytical
294  *
295  * @param t Time (input)
296  * @param delta_t The current value of the time step (input)
297  * @param cj Coefficient of yprime used in the evaluation of the Jacobian
298  * @param y Solution vector (input, do not modify)
299  * @param ydot Rate of change of solution vector. (input, do not modify)
300  * @param jacobianColPts Pointer to the vector of pts to columns of the DenseMatrix
301  * object to be calculated (output)
302  * @param resid Value of the residual that is computed (output)
303  * @returns a flag to indicate that operation is successful.
304  * 1 Means a successful operation
305  * -0 or neg value Means an unsuccessful operation
306  */
307  virtual int evalJacobianDP(const doublereal t, const doublereal delta_t, doublereal cj,
308  const doublereal* const y,
309  const doublereal* const ydot,
310  doublereal* const* jacobianColPts,
311  doublereal* const resid);
312 
313 protected:
314  //! constant value of atol
315  doublereal m_atol;
316 
317  //! Number of equations
318  int neq_;
319 };
320 }
321 
322 #endif
virtual int evalJacobianDP(const doublereal t, const doublereal delta_t, doublereal cj, const doublereal *const y, const doublereal *const ydot, doublereal *const *jacobianColPts, doublereal *const resid)
Calculate an analytical Jacobian and the residual at the current time and values. ...
virtual void user_out2(const int ifunc, const doublereal t, const doublereal delta_t, const doublereal *const y, const doublereal *const ydot)
This function may be used to create output at various points in the execution of an application...
Base residual calculation for the showSolution routine.
Definition: ResidJacEval.h:31
virtual int evalResidNJ(const doublereal t, const doublereal delta_t, const doublereal *const y, const doublereal *const ydot, doublereal *const resid, const ResidEval_Type_Enum evalType=Base_ResidEval, const int id_x=-1, const doublereal delta_x=0.0)
Evaluate the residual function.
virtual ResidJacEval * duplMyselfAsResidJacEval() const
Duplication routine for objects derived from residJacEval.
int neq_
Number of equations.
Definition: ResidJacEval.h:318
virtual int eval(const doublereal t, const doublereal *const y, const doublereal *const ydot, doublereal *const r)
Evaluate the residual function.
Base residual calculation for the Jacobian calculation.
Definition: ResidJacEval.h:24
virtual int getInitialConditions(const doublereal t0, doublereal *const y, doublereal *const ydot)
Fill in the initial conditions.
ResidJacEval(doublereal atol=1.0e-13)
Default constructor.
virtual void user_out(const int ifunc, const doublereal t, const doublereal *y, const doublereal *ydot)
This function may be used to create output at various points in the execution of an application...
ResidEval_Type_Enum
Differentiates the type of residual evaluations according to functionality.
Definition: ResidJacEval.h:20
Wrappers for the function evaluators for Nonlinear solvers and Time steppers.
Definition: ResidJacEval.h:55
virtual int nEquations() const
Return the number of equations in the equation system.
virtual int calcDeltaSolnVariables(const doublereal t, const doublereal *const y, const doublereal *const ydot, doublereal *const delta_y, const doublereal *const solnWeights=0)
Return a vector of delta y's for calculation of the numerical Jacobian.
virtual int matrixConditioning(doublereal *const matrix, const int nrows, doublereal *const rhs)
Multiply the matrix by another matrix that leads to better conditioning.
Virtual base class for DAE residual function evaluators.
Definition: ResidEval.h:33
virtual int evalJacobian(const doublereal t, const doublereal delta_t, doublereal cj, const doublereal *const y, const doublereal *const ydot, DenseMatrix &J, doublereal *const resid)
Calculate an analytical Jacobian and the residual at the current time and values. ...
doublereal m_atol
constant value of atol
Definition: ResidJacEval.h:315
virtual int evalTimeTrackingEqns(const doublereal t, const doublereal delta_t, const doublereal *const y, const doublereal *const ydot)
Evaluate the time tracking equations, if any.
virtual void calcSolnScales(const doublereal t, const doublereal *const y, const doublereal *const y_old, doublereal *const yScales)
Returns a vector of column scale factors that can be used to column scale Jacobians.
ResidJacEval & operator=(const ResidJacEval &right)
Headers for the DenseMatrix object, which deals with dense rectangular matrices and description of th...
Delta residual calculation for the Jacbobian calculation.
Definition: ResidJacEval.h:26
virtual doublereal filterSolnPrediction(const doublereal t, doublereal *const y)
Filter the solution predictions.
Namespace for the Cantera kernel.
Definition: application.cpp:29
virtual bool evalStoppingCritera(const doublereal t, const doublereal delta_t, const doublereal *const y, const doublereal *const ydot)
Evaluate any stopping criteria other than a final time limit.
void setAtol(doublereal atol)
Set a global value of the absolute tolerance.
virtual doublereal filterNewStep(const doublereal t, const doublereal *const ybase, doublereal *const step)
Filter the solution predictions.
Base residual calculation for the time-stepping function.
Definition: ResidJacEval.h:22
Base residual calculation containing any lagged components.
Definition: ResidJacEval.h:38
A class for full (non-sparse) matrices with Fortran-compatible data storage, which adds matrix operat...
Definition: DenseMatrix.h:72