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