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