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