Cantera  2.1.2
IDA_Solver.h
Go to the documentation of this file.
1 /**
2  * @file IDA_Solver.h
3  * Header file for class IDA_Solver
4  */
5 
6 // Copyright 2006 California Institute of Technology
7 
8 #ifndef CT_IDA_SOLVER_H
9 #define CT_IDA_SOLVER_H
10 
11 #include "DAE_Solver.h"
13 
14 #if HAS_SUNDIALS
15 
16 #if SUNDIALS_VERSION == 22
17 #include "nvector_serial.h"
18 #else
19 #include "sundials/sundials_nvector.h"
20 
21 // These constants are defined internally in the ida package, ida.c
22 #define IDA_NN 0
23 #define IDA_SS 1
24 #define IDA_SV 2
25 #define IDA_WF 3
26 
27 #endif
28 #if SUNDIALS_VERSION >= 24
29 #define REAL_WORKSPACE_SIZE 0
30 #endif
31 
32 namespace Cantera
33 {
34 
35 /**
36  * Exception thrown when a IDA error is encountered.
37  */
38 class IDA_Err : public CanteraError
39 {
40 public:
41  explicit IDA_Err(const std::string& msg) : CanteraError("IDA_Solver", msg) {}
42 };
43 
44 
45 class ResidData; // forward reference
46 
47 class IDA_Solver : public DAE_Solver
48 {
49 public:
50 
51  //! Constructor.
52  /*!
53  * Default settings: dense jacobian, no user-supplied Jacobian function, Newton iteration.
54  *
55  * @param f Function that will supply the time dependent residual to be solved
56  */
57  IDA_Solver(ResidJacEval& f);
58 
59  virtual ~IDA_Solver();
60 
61  virtual void setTolerances(doublereal reltol,
62  doublereal* abstol);
63 
64  virtual void setTolerances(doublereal reltol, doublereal abstol);
65 
66  virtual void setLinearSolverType(int solverType);
67 
68  //! Set up the problem to use a dense linear direct solver
69  virtual void setDenseLinearSolver();
70 
71  //! Set up the problem to use a band solver
72  /*!
73  * @param m_upper upper band width of the matrix
74  * @param m_lower lower band width of the matrix
75  */
76  virtual void setBandedLinearSolver(int m_upper, int m_lower);
77 
78  virtual void setMaxOrder(int n);
79 
80  //! Set the maximum number of time steps
81  /*!
82  * @param n input of maximum number of time steps
83  */
84  virtual void setMaxNumSteps(int n);
85 
86  //! Sset the initial step size
87  /*!
88  * @param h0 initial step size value
89  */
90  virtual void setInitialStepSize(doublereal h0);
91 
92  //! Set the stop time
93  /*!
94  * @param tstop the independent variable value past which the solution is not to proceed.
95  */
96  virtual void setStopTime(doublereal tstop);
97 
98  //! Get the current step size from IDA via a call
99  /*!
100  * @return Returns the current step size.
101  */
102  virtual double getCurrentStepFromIDA();
103 
104 
105  //! Set the form of the jacobian
106  /*!
107  *
108  * @param formJac Form of the jacobian
109  *
110  * 0 numerical jacobian
111  * 1 analytical jacobian given by the evalJacobianDP() function
112  */
113  virtual void setJacobianType(int formJac);
114 
115 
116  virtual void setMaxErrTestFailures(int n);
117 
118  //! Set the maximum number of nonlinear iterations on a timestep
119  /*!
120  * @param n Set the max iterations. The default is 4, which seems awefully low to me.
121  */
122  virtual void setMaxNonlinIterations(int n);
123 
124  //! Set the maximum number of nonlinear solver convergence failures
125  /*!
126  * @param n Value of nonlin failures. If value is exceeded, the calculation terminates.
127  */
128  virtual void setMaxNonlinConvFailures(int n);
129 
130 
131  virtual void inclAlgebraicInErrorTest(bool yesno);
132 
133  /**
134  * Get the value of a solver-specific output parameter.
135  */
136  virtual doublereal getOutputParameter(int flag) const;
137 
138  virtual void correctInitial_Y_given_Yp(doublereal* y, doublereal* yp,
139  doublereal tout);
140 
141  virtual void correctInitial_YaYp_given_Yd(doublereal* y, doublereal* yp, doublereal tout);
142 
143  //! Step the system to a final value of the time
144  /*!
145  * @param tout Final value of the time
146  *
147  * @return Returns the IDASolve() return flag
148  *
149  * The return values for IDASolve are described below.
150  * (The numerical return values are defined above in this file.)
151  * All unsuccessful returns give a negative return value.
152  *
153  * IDA_SUCCESS
154  * IDASolve succeeded and no roots were found.
155  *
156  * IDA_ROOT_RETURN: IDASolve succeeded, and found one or more roots.
157  * If nrtfn > 1, call IDAGetRootInfo to see which g_i were found
158  * to have a root at (*tret).
159  *
160  * IDA_TSTOP_RETURN:
161  * IDASolve returns computed results for the independent variable
162  * value tstop. That is, tstop was reached.
163  *
164  * IDA_MEM_NULL:
165  * The IDA_mem argument was NULL.
166  *
167  * IDA_ILL_INPUT:
168  * One of the inputs to IDASolve is illegal. This includes the
169  * situation when a component of the error weight vectors
170  * becomes < 0 during internal stepping. It also includes the
171  * situation where a root of one of the root functions was found
172  * both at t0 and very near t0. The ILL_INPUT flag
173  * will also be returned if the linear solver function IDA---
174  * (called by the user after calling IDACreate) failed to set one
175  * of the linear solver-related fields in ida_mem or if the linear
176  * solver's init routine failed. In any case, the user should see
177  * the printed error message for more details.
178  *
179  * IDA_TOO_MUCH_WORK:
180  * The solver took mxstep internal steps but could not reach tout.
181  * The default value for mxstep is MXSTEP_DEFAULT = 500.
182  *
183  * IDA_TOO_MUCH_ACC:
184  * The solver could not satisfy the accuracy demanded by the user
185  * for some internal step.
186  *
187  * IDA_ERR_FAIL:
188  * Error test failures occurred too many times (=MXETF = 10) during
189  * one internal step.
190  *
191  * IDA_CONV_FAIL:
192  * Convergence test failures occurred too many times (= MXNCF = 10)
193  * during one internal step.
194  *
195  * IDA_LSETUP_FAIL:
196  * The linear solver's setup routine failed
197  * in an unrecoverable manner.
198  *
199  * IDA_LSOLVE_FAIL:
200  * The linear solver's solve routine failed
201  * in an unrecoverable manner.
202  *
203  * IDA_CONSTR_FAIL:
204  * The inequality constraints were violated,
205  * and the solver was unable to recover.
206  *
207  * IDA_REP_RES_ERR:
208  * The user's residual function repeatedly returned a recoverable
209  * error flag, but the solver was unable to recover.
210  *
211  * IDA_RES_FAIL:
212  * The user's residual function returned a nonrecoverable error
213  * flag.
214  */
215  virtual int solve(doublereal tout);
216 
217  virtual doublereal step(doublereal tout);
218 
219  virtual void init(doublereal t0);
220 
221  virtual doublereal solution(int k) const;
222 
223  virtual const doublereal* solutionVector() const;
224 
225  virtual doublereal derivative(int k) const;
226 
227  virtual const doublereal* derivativeVector() const;
228 
229  void* IDAMemory() {
230  return m_ida_mem;
231  }
232 
233 protected:
234  //! Pointer to the IDA memory for the problem
235  void* m_ida_mem;
236 
237  //! Initial value of the time
238  doublereal m_t0;
239 
240  //! Current value of the solution vector
241  N_Vector m_y;
242 
243  //! Current value of the derivative of the solution vector
244  N_Vector m_ydot;
245  N_Vector m_id;
246  N_Vector m_constraints;
247  N_Vector m_abstol;
248  int m_type;
249 
250  int m_itol;
251  int m_iter;
252  doublereal m_reltol;
253  doublereal m_abstols;
254  int m_nabs;
255 
256  //! Maximum value of the timestep allowed
257  doublereal m_hmax;
258 
259  //! Minimum value of the timestep allowd
260  doublereal m_hmin;
261 
262  //! Value of the initial time step
263  doublereal m_h0;
264 
265  //! Maximum number of time steps allowed
266  int m_maxsteps;
267 
268  //! maximum time step order of the method
269  int m_maxord;
270 
271  //! Form of the jacobian
272  /*!
273  * 0 numerical jacobian created by ida
274  * 1 analytical jacobian. Must have populated the evalJacobianDP()
275  * function in the ResidJacEval class.
276  * 2 numerical jacobian formed by the ResidJacEval class (unimplemented)
277  */
278  int m_formJac;
279 
280  //! maximum time
281  doublereal m_tstop;
282 
283  //! Value of the previous, previous time
284  doublereal m_told_old;
285 
286  //! Value of the previous time
287  doublereal m_told;
288 
289  //! Value of the current time
290  doublereal m_tcurrent;
291 
292  //! Value of deltaT for the current step
293  doublereal m_deltat;
294 
295  //! maximum number of error test failures
296  int m_maxErrTestFails;
297 
298  //! Maximum number of nonlinear solver iterations at one solution
299  /*!
300  * If zero, this is the default of 4.
301  */
302  int m_maxNonlinIters;
303 
304  //! Maximum number of nonlinear convergence failures
305  int m_maxNonlinConvFails;
306 
307  //! If true, the algebraic variables don't contribute to error tolerances
308  int m_setSuppressAlg;
309 
310  ResidData* m_fdata;
311  int m_mupper;
312  int m_mlower;
313 };
314 
315 }
316 
317 #endif
318 #endif
A simple class to hold an array of parameter values and a pointer to an instance of a subclass of Res...
Definition: IDA_Solver.cpp:34
int solve(DenseMatrix &A, double *b)
Solve Ax = b. Array b is overwritten on exit with x.
Wrappers for the function evaluators for Nonlinear solvers and Time steppers.
Definition: ResidJacEval.h:51
Header file for class DAE_Solver.
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:68
CanteraError()
Protected default constructor discourages throwing errors containing no information.
Definition: ctexceptions.h:101
Wrapper for DAE solvers.
Definition: DAE_Solver.h:70
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
Exception thrown when a IDA error is encountered.
Definition: IDA_Solver.h:38