1#ifndef STAN_MATH_PRIM_ERR_CHECK_FLAG_SUNDIALS_HPP
2#define STAN_MATH_PRIM_ERR_CHECK_FLAG_SUNDIALS_HPP
6#include <kinsol/kinsol.h>
7#include <cvodes/cvodes.h>
13#define CHECK_CVODES_CALL(call) cvodes_check(call, #call)
14#define CHECK_IDAS_CALL(call) idas_check(call, #call)
15#define CHECK_KINSOL_CALL(call) kinsol_check(call, #call)
29 std::array<std::string, 2> msg;
32 msg = {
"CV_TOO_MUCH_WORK",
33 "The solver took mxstep internal steps but could not reach tout"};
36 msg = {
"CV_TOO_MUCH_ACC",
37 "The solver could not satisfy the accuracy demanded by the user "
38 "for some internal step"};
41 msg = {
"CV_ERR_FAILURE",
42 "Error test failures occurred too many times during one internal "
43 "time step or minimum step size was reached"};
46 msg = {
"CV_CONV_FAILURE",
47 "Convergence test failures occurred too many times during one "
48 "internal time step or minimum step size was reached"};
51 msg = {
"CV_RHSFUNC_FAIL",
52 "The right-hand side function failed in an unrecoverable manner"};
55 msg = {
"CV_FIRST_RHSFUNC_ERR",
56 "The right-hand side function failed at the first call"};
59 msg = {
"CV_REPTD_RHSFUNC_ERR",
60 "The right-hand side function had repetead recoverable errors"};
63 msg = {
"CV_UNREC_RHSFUNC_ERR",
64 "The right-hand side function had a recoverable error, but no "
65 "recovery is possible"};
68 msg = {
"CV_TOO_CLOSE",
69 "The output and initial times are too close to each other"};
74 msg = {
"CV_LINIT_FAIL",
75 "The linear solver's initialization function failed"};
78 msg = {
"CV_LSETUP_FAIL",
79 "The linear solver's setup function failed in an "
80 "unrecoverable manner"};
83 msg = {
"CV_LSOLVE_FAIL",
84 "The linear solver's solve function failed in an "
85 "unrecoverable manner"};
88 msg = {
"CV_MEM_FAIL",
"A memory allocation failed"};
91 msg = {
"CV_MEM_NULL",
"The cvode_mem argument was NULL"};
94 msg = {
"CV_ILL_INPUT",
"One of the function inputs is illegal"};
97 msg = {
"CV_NO_MALLOC",
98 "The CVODE memory block was not allocated by a call to "
103 "The derivative order k is larger than the order used"};
106 msg = {
"CV_BAD_T",
"The time t s outside the last step taken"};
109 msg = {
"CV_BAD_DKY",
"The output derivative vector is NULL"};
113 "The sensitivity index is larger than the number of "
114 "sensitivities computed"};
118 "Forward sensitivity integration was not activated"};
121 msg = {
"CV_SRHSFUNC_FAIL",
122 "The sensitivity right-hand side function failed in an "
123 "unrecoverable manner"};
126 msg = {
"CV_FIRST_SRHSFUNC_ER",
127 "The sensitivity right-hand side function failed at the first "
131 msg = {
"CV_REPTD_SRHSFUNC_ER",
132 "The sensitivity ight-hand side function had repetead "
133 "recoverable errors"};
136 msg = {
"CV_UNREC_SRHSFUNC_ER",
137 "The sensitivity right-hand side function had a recoverable "
138 "error, but no recovery is possible"};
141 msg = {
"CV_ADJMEM_NULL",
"The cvadj_mem argument was NULL"};
145 "The final time for the adjoint problem is outside the "
146 "interval over which the forward problem was solved"};
149 msg = {
"CV_BCKMEM_NULL",
150 "The cvodes memory for the backward problem was not created"};
153 msg = {
"CV_REIFWD_FAIL",
154 "Reinitialization of the forward problem failed at the first "
160 "An error occured during the integration of the forward problem"};
163 msg = {
"CV_BAD_ITASK",
"Wrong task for backward integration"};
166 msg = {
"CV_BAD_TBOUT",
167 "The desired output time is outside the interval over which "
168 "the forward problem was solved"};
171 msg = {
"CV_GETY_BADT",
"Wrong time in interpolation function"};
188 std::ostringstream ss;
189 ss << func_name <<
" failed with error flag " << flag <<
": \n"
191 throw std::domain_error(ss.str());
196 std::array<std::string, 2> msg;
199 msg = {
"IDA_TOO_MUCH_WORK",
200 "The solver took mxstep internal steps but could not reach tout."};
203 msg = {
"IDA_TOO_MUCH_ACC",
204 "The solver could not satisfy the accuracy demanded by the user "
205 "for some internal step."};
208 msg = {
"IDA_ERR_FAIL",
209 "Error test failures occurred too many times during one internal "
210 "time step or minimum step size was reached."};
213 msg = {
"IDA_CONV_FAIL",
214 "Convergence test failures occurred too many times during one "
215 "internal time step or minimum step size was reached."};
218 msg = {
"IDA_LINIT_FAIL",
219 "The linear solver’s initialization function failed."};
222 msg = {
"IDA_LSETUP_FAIL",
223 "The linear solver’s setup function failed in an unrecoverable "
227 msg = {
"IDA_LSOLVE_FAIL",
228 "The linear solver’s solve function failed in an unrecoverable "
232 msg = {
"IDA_RES_FAIL",
233 "The user-provided residual function failed in an unrecoverable "
237 msg = {
"IDA_REP_RES_FAIL",
238 "The user-provided residual function repeatedly returned a "
239 "recoverable error flag, but the solver was unable to recover."};
242 msg = {
"IDA_RTFUNC_FAIL",
243 "The rootfinding function failed in an unrecoverable manner."};
246 msg = {
"IDA_CONSTR_FAIL",
247 "The inequality constraints were violated and the solver was "
248 "unable to recover."};
251 msg = {
"IDA_FIRST_RES_FAIL",
252 "The user-provided residual function failed recoverably on the "
256 msg = {
"IDA_LINESEARCH_FAIL",
"The line search failed."};
261 msg = {
"IDA_NO_RECOVERY",
262 "The residual function, linear solver setup function, or "
263 "linear solver solve function had a recoverable failure, but "
264 "IDACalcIC could not recover."};
267 msg = {
"IDA_NLS_INIT_FAIL",
268 "The nonlinear solver’s init routine failed."};
271 msg = {
"IDA_NLS_SETUP_FAIL",
272 "The nonlinear solver’s setup routine failed."};
275 msg = {
"IDA_MEM_NULL",
"The ida mem argument was NULL."};
278 msg = {
"IDA_MEM_FAIL",
"A memory allocation failed."};
281 msg = {
"IDA_ILL_INPUT",
"One of the function inputs is illegal."};
284 msg = {
"IDA_NO_MALLOC",
285 "The idas memory was not allocated by a call to IDAInit."};
288 msg = {
"IDA_BAD_EWT",
"Zero value of some error weight component."};
291 msg = {
"IDA_BAD_K",
"The k-th derivative is not available."};
294 msg = {
"IDA_BAD_T",
"The time t is outside the last step taken."};
299 "The vector argument where derivative should be stored is NULL."};
302 msg = {
"IDA_NO_QUAD",
"Quadratures were not initialized."};
305 msg = {
"IDA_QRHS_FAIL",
306 "The user-provided right-hand side function for quadratures "
307 "failed in an unrecoverable manner."};
310 msg = {
"IDA_FIRST_QRHS_ERR",
311 "The user-provided right-hand side function for quadratures "
312 "failed -in an unrecoverable manner on the first call."};
315 msg = {
"IDA_REP_QRHS_ERR",
316 "The user-provided right-hand side repeatedly returned a re- "
317 "coverable error flag, but the solver was unable to recover."};
320 msg = {
"IDA_NO_SENS",
"Sensitivities were not initialized."};
323 msg = {
"IDA_SRES_FAIL",
324 "The user-provided sensitivity residual function failed in an "
325 "unrecoverable manner."};
328 msg = {
"IDA_REP_SRES_ERR",
329 "The user-provided sensitivity residual function repeatedly "
330 "re- turned a recoverable error flag, but the solver was "
331 "unable to recover."};
334 msg = {
"IDA_BAD_IS",
"The sensitivity identifier is not valid."};
337 msg = {
"IDA_NO_QUADSENS",
338 "Sensitivity-dependent quadratures were not initialized."};
341 msg = {
"IDA_QSRHS_FAIL",
342 "The user-provided sensitivity-dependent quadrature right- "
343 "hand side function failed in an unrecoverable manner."};
346 msg = {
"IDA_FIRST_QSRHS_ERR",
347 "The user-provided sensitivity-dependent quadrature right- "
348 "hand side function failed in an unrecoverable manner on the "
352 msg = {
"IDA_REP_QSRHS_ERR",
353 "The user-provided sensitivity-dependent quadrature right- "
354 "hand side repeatedly returned a recoverable error flag, but "
355 "the solver was unable to recover."};
364 std::ostringstream ss;
365 ss << func_name <<
" failed with error flag " << flag <<
": \n"
367 throw std::domain_error(ss.str());
382 std::ostringstream ss;
383 ss <<
"algebra_solver failed with error flag " << flag <<
".";
384 throw std::runtime_error(ss.str());
402 long int max_num_steps) {
403 std::ostringstream ss;
405 domain_error(
"algebra_solver",
"maximum number of iterations",
406 max_num_steps,
"(",
") was exceeded in the solve.");
407 }
else if (flag == -11) {
408 ss <<
"The linear solver’s setup function failed "
409 <<
"in an unrecoverable manner.";
410 throw std::runtime_error(ss.str());
411 }
else if (flag < 0) {
412 ss <<
"algebra_solver failed with error flag " << flag <<
".";
413 throw std::runtime_error(ss.str());
std::array< std::string, 2 > idas_flag_msg(int flag)
std::array< std::string, 2 > cvodes_flag_msg(int flag)
Map cvodes error flag to acutally error msg.
void idas_check(int flag, const char *func_name)
void cvodes_check(int flag, const char *func_name)
Throws a std::domain_error exception when a Sundial function fails (i.e.
void kinsol_check(int flag, const char *func_name)
Throws an exception message when the functions in KINSOL fails.
void domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...