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>
9#include <arkode/arkode.h>
15#define CHECK_CVODES_CALL(call) cvodes_check(call, #call)
16#define CHECK_IDAS_CALL(call) idas_check(call, #call)
17#define CHECK_KINSOL_CALL(call) kinsol_check(call, #call)
18#define CHECK_ARKODE_CALL(call) arkode_check(call, #call)
32 case CV_TOO_MUCH_WORK:
33 return {
"CV_TOO_MUCH_WORK",
34 "The solver took mxstep internal steps but could not reach tout"};
36 return {
"CV_TOO_MUCH_ACC",
37 "The solver could not satisfy the accuracy demanded by the user "
38 "for some internal step"};
40 return {
"CV_ERR_FAILURE",
41 "Error test failures occurred too many times during one internal "
42 "time step or minimum step size was reached"};
44 return {
"CV_CONV_FAILURE",
45 "Convergence test failures occurred too many times during one "
46 "internal time step or minimum step size was reached"};
48 return {
"CV_LINIT_FAIL",
49 "The linear solver's initialization function failed"};
51 return {
"CV_LSETUP_FAIL",
52 "The linear solver's setup function failed in an "
53 "unrecoverable manner"};
55 return {
"CV_LSOLVE_FAIL",
56 "The linear solver's solve function failed in an "
57 "unrecoverable manner"};
59 return {
"CV_RHSFUNC_FAIL",
60 "The right-hand side function failed in an unrecoverable manner"};
61 case CV_FIRST_RHSFUNC_ERR:
62 return {
"CV_FIRST_RHSFUNC_ERR",
63 "The right-hand side function failed at the first call"};
64 case CV_REPTD_RHSFUNC_ERR:
65 return {
"CV_REPTD_RHSFUNC_ERR",
66 "The right-hand side function had repeated recoverable errors"};
67 case CV_UNREC_RHSFUNC_ERR:
68 return {
"CV_UNREC_RHSFUNC_ERR",
69 "The right-hand side function had a recoverable error, but no "
70 "recovery is possible"};
72 return {
"CV_RTFUNC_FAIL",
73 "The rootfinding function failed in an unrecoverable manner"};
74 case CV_NLS_INIT_FAIL:
75 return {
"CV_NLS_INIT_FAIL",
"The nonlinear solver's init routine failed"};
76 case CV_NLS_SETUP_FAIL:
77 return {
"CV_NLS_SETUP_FAIL",
78 "The nonlinear solver's setup routine failed"};
80 return {
"CV_CONSTR_FAIL",
81 "The inequality constraints were violated and the solver was "
84 return {
"CV_MEM_FAIL",
"A memory allocation failed"};
86 return {
"CV_MEM_NULL",
"The cvode_mem argument was NULL"};
88 return {
"CV_ILL_INPUT",
"One of the function inputs is illegal"};
90 return {
"CV_NO_MALLOC",
91 "The CVODE memory block was not allocated by a call to "
95 "The derivative order k is larger than the order used"};
97 return {
"CV_BAD_T",
"The time t is outside the last step taken"};
99 return {
"CV_BAD_DKY",
"The output derivative vector is NULL"};
101 return {
"CV_TOO_CLOSE",
102 "The output and initial times are too close to each other"};
104 return {
"CV_NO_QUAD",
"Quadrature integration was not activated"};
105 case CV_QRHSFUNC_FAIL:
106 return {
"CV_QRHSFUNC_FAIL",
107 "The quadrature right-hand side function failed in an "
108 "unrecoverable manner"};
109 case CV_FIRST_QRHSFUNC_ERR:
110 return {
"CV_FIRST_QRHSFUNC_ERR",
111 "The quadrature right-hand side function failed at the first "
113 case CV_REPTD_QRHSFUNC_ERR:
114 return {
"CV_REPTD_QRHSFUNC_ERR",
115 "The quadrature right-hand side function had repeated "
116 "recoverable errors"};
117 case CV_UNREC_QRHSFUNC_ERR:
118 return {
"CV_UNREC_QRHSFUNC_ERR",
119 "The quadrature right-hand side function had a recoverable "
120 "error, but no recovery is possible"};
122 return {
"CV_NO_SENS",
123 "Forward sensitivity integration was not activated"};
124 case CV_SRHSFUNC_FAIL:
125 return {
"CV_SRHSFUNC_FAIL",
126 "The sensitivity right-hand side function failed in an "
127 "unrecoverable manner"};
128 case CV_FIRST_SRHSFUNC_ERR:
129 return {
"CV_FIRST_SRHSFUNC_ERR",
130 "The sensitivity right-hand side function failed at the first "
132 case CV_REPTD_SRHSFUNC_ERR:
133 return {
"CV_REPTD_SRHSFUNC_ERR",
134 "The sensitivity right-hand side function had repeated "
135 "recoverable errors"};
136 case CV_UNREC_SRHSFUNC_ERR:
137 return {
"CV_UNREC_SRHSFUNC_ERR",
138 "The sensitivity right-hand side function had a recoverable "
139 "error, but no recovery is possible"};
142 "The sensitivity index is larger than the number of "
143 "sensitivities computed"};
145 return {
"CV_NO_QUADSENS",
146 "Forward quadrature sensitivity integration was not activated"};
147 case CV_QSRHSFUNC_FAIL:
148 return {
"CV_QSRHSFUNC_FAIL",
149 "The quadrature sensitivity right-hand side function failed in "
150 "an unrecoverable manner"};
151 case CV_FIRST_QSRHSFUNC_ERR:
152 return {
"CV_FIRST_QSRHSFUNC_ERR",
153 "The quadrature sensitivity right-hand side function failed at "
155 case CV_REPTD_QSRHSFUNC_ERR:
156 return {
"CV_REPTD_QSRHSFUNC_ERR",
157 "The quadrature sensitivity right-hand side function had "
158 "repeated recoverable errors"};
159 case CV_UNREC_QSRHSFUNC_ERR:
160 return {
"CV_UNREC_QSRHSFUNC_ERR",
161 "The quadrature sensitivity right-hand side function had a "
162 "recoverable error, but no recovery is possible"};
164 return {
"CV_CONTEXT_ERR",
"The SUNContext object is NULL"};
166 return {
"CV_NO_ADJ",
"Adjoint module was not initialized"};
168 return {
"CV_NO_FWD",
"The forward integration was not yet performed"};
170 return {
"CV_NO_BCK",
"No backward problem was specified"};
172 return {
"CV_BAD_TB0",
173 "The final time for the adjoint problem is outside the "
174 "interval over which the forward problem was solved"};
176 return {
"CV_REIFWD_FAIL",
177 "Reinitialization of the forward problem failed at the first "
180 return {
"CV_FWD_FAIL",
181 "An error occurred during the integration of the forward "
184 return {
"CV_GETY_BADT",
"Wrong time in interpolation function"};
186 return {
"CV_UNKNOWN_ERROR",
"Unrecognized CVODES error flag"};
199 std::ostringstream ss;
200 ss << func_name <<
" failed with error flag " << flag <<
": \n"
202 throw std::domain_error(ss.str());
216 case IDA_TOO_MUCH_WORK:
219 "The solver took mxstep internal steps but could not reach tout."};
220 case IDA_TOO_MUCH_ACC:
221 return {
"IDA_TOO_MUCH_ACC",
222 "The solver could not satisfy the accuracy demanded by the user "
223 "for some internal step."};
225 return {
"IDA_ERR_FAIL",
226 "Error test failures occurred too many times during one internal "
227 "time step or minimum step size was reached."};
229 return {
"IDA_CONV_FAIL",
230 "Convergence test failures occurred too many times during one "
231 "internal time step or minimum step size was reached."};
233 return {
"IDA_LINIT_FAIL",
234 "The linear solver's initialization function failed."};
235 case IDA_LSETUP_FAIL:
236 return {
"IDA_LSETUP_FAIL",
237 "The linear solver's setup function failed in an unrecoverable "
239 case IDA_LSOLVE_FAIL:
240 return {
"IDA_LSOLVE_FAIL",
241 "The linear solver's solve function failed in an unrecoverable "
244 return {
"IDA_RES_FAIL",
245 "The user-provided residual function failed in an unrecoverable "
247 case IDA_REP_RES_ERR:
248 return {
"IDA_REP_RES_ERR",
249 "The user-provided residual function repeatedly returned a "
250 "recoverable error flag, but the solver was unable to recover."};
251 case IDA_RTFUNC_FAIL:
252 return {
"IDA_RTFUNC_FAIL",
253 "The rootfinding function failed in an unrecoverable manner."};
254 case IDA_CONSTR_FAIL:
255 return {
"IDA_CONSTR_FAIL",
256 "The inequality constraints were violated and the solver was "
257 "unable to recover."};
258 case IDA_FIRST_RES_FAIL:
259 return {
"IDA_FIRST_RES_FAIL",
260 "The user-provided residual function failed recoverably on the "
262 case IDA_LINESEARCH_FAIL:
263 return {
"IDA_LINESEARCH_FAIL",
"The line search failed."};
264 case IDA_NO_RECOVERY:
265 return {
"IDA_NO_RECOVERY",
266 "The residual function, linear solver setup function, or "
267 "linear solver solve function had a recoverable failure, but "
268 "IDACalcIC could not recover."};
269 case IDA_NLS_INIT_FAIL:
270 return {
"IDA_NLS_INIT_FAIL",
271 "The nonlinear solver's init routine failed."};
272 case IDA_NLS_SETUP_FAIL:
273 return {
"IDA_NLS_SETUP_FAIL",
274 "The nonlinear solver's setup routine failed."};
276 return {
"IDA_MEM_NULL",
"The ida_mem argument was NULL."};
278 return {
"IDA_MEM_FAIL",
"A memory allocation failed."};
280 return {
"IDA_ILL_INPUT",
"One of the function inputs is illegal."};
282 return {
"IDA_NO_MALLOC",
283 "The IDAS memory was not allocated by a call to IDAInit."};
285 return {
"IDA_BAD_EWT",
"Zero value of some error weight component."};
287 return {
"IDA_BAD_K",
"The k-th derivative is not available."};
289 return {
"IDA_BAD_T",
"The time t is outside the last step taken."};
291 return {
"IDA_BAD_DKY",
292 "The vector argument where derivative should be stored is NULL."};
293 case IDA_VECTOROP_ERR:
294 return {
"IDA_VECTOROP_ERR",
"A vector operation failed."};
295 case IDA_CONTEXT_ERR:
296 return {
"IDA_CONTEXT_ERR",
297 "An error occurred in the SUNDIALS context object."};
299 return {
"IDA_NO_QUAD",
"Quadratures were not initialized."};
301 return {
"IDA_QRHS_FAIL",
302 "The user-provided right-hand side function for quadratures "
303 "failed in an unrecoverable manner."};
304 case IDA_FIRST_QRHS_ERR:
305 return {
"IDA_FIRST_QRHS_ERR",
306 "The user-provided right-hand side function for quadratures "
307 "failed in an unrecoverable manner on the first call."};
308 case IDA_REP_QRHS_ERR:
309 return {
"IDA_REP_QRHS_ERR",
310 "The user-provided right-hand side repeatedly returned a "
311 "recoverable error flag, but the solver was unable to recover."};
313 return {
"IDA_NO_SENS",
"Sensitivities were not initialized."};
315 return {
"IDA_SRES_FAIL",
316 "The user-provided sensitivity residual function failed in an "
317 "unrecoverable manner."};
318 case IDA_REP_SRES_ERR:
319 return {
"IDA_REP_SRES_ERR",
320 "The user-provided sensitivity residual function repeatedly "
321 "returned a recoverable error flag, but the solver was "
322 "unable to recover."};
324 return {
"IDA_BAD_IS",
"The sensitivity identifier is not valid."};
325 case IDA_NO_QUADSENS:
326 return {
"IDA_NO_QUADSENS",
327 "Sensitivity-dependent quadratures were not initialized."};
329 return {
"IDA_QSRHS_FAIL",
330 "The user-provided sensitivity-dependent quadrature right-hand "
331 "side function failed in an unrecoverable manner."};
332 case IDA_FIRST_QSRHS_ERR:
333 return {
"IDA_FIRST_QSRHS_ERR",
334 "The user-provided sensitivity-dependent quadrature right-hand "
335 "side function failed in an unrecoverable manner on the first "
337 case IDA_REP_QSRHS_ERR:
338 return {
"IDA_REP_QSRHS_ERR",
339 "The user-provided sensitivity-dependent quadrature right-hand "
340 "side repeatedly returned a recoverable error flag, but the "
341 "solver was unable to recover."};
343 return {
"IDA_NO_ADJ",
344 "The combined forward-backward problem has not been "
347 return {
"IDA_NO_FWD",
"IDASolveF has not been previously called."};
349 return {
"IDA_NO_BCK",
"No backward problem was specified."};
351 return {
"IDA_BAD_TB0",
352 "The desired output for backward problem is outside the "
353 "interval over which the forward problem was solved."};
354 case IDA_REIFWD_FAIL:
355 return {
"IDA_REIFWD_FAIL",
356 "No checkpoint is available for this hot start."};
358 return {
"IDA_FWD_FAIL",
359 "IDASolveB failed because IDASolve was unable to store data "
360 "between two consecutive checkpoints."};
362 return {
"IDA_GETY_BADT",
"Wrong time in interpolation function."};
364 return {
"IDA_UNKNOWN_ERROR",
"Unrecognized IDAS error flag."};
369 std::ostringstream ss;
370 ss << func_name <<
" failed with error flag " << flag <<
": \n"
372 throw std::domain_error(ss.str());
388 case ARK_TOO_MUCH_WORK:
389 return {
"ARK_TOO_MUCH_WORK",
390 "The solver took mxstep internal steps but could not reach "
392 case ARK_TOO_MUCH_ACC:
393 return {
"ARK_TOO_MUCH_ACC",
394 "The solver could not satisfy the accuracy demanded by the user "
395 "for some internal step"};
396 case ARK_ERR_FAILURE:
397 return {
"ARK_ERR_FAILURE",
398 "Error test failures occurred too many times during one "
399 "internal time step, or the minimum step size was reached"};
400 case ARK_CONV_FAILURE:
401 return {
"ARK_CONV_FAILURE",
402 "Convergence test failures occurred too many times during one "
403 "internal time step, or the minimum step size was reached"};
405 return {
"ARK_LINIT_FAIL",
406 "The linear solver's initialization function failed"};
407 case ARK_LSETUP_FAIL:
408 return {
"ARK_LSETUP_FAIL",
409 "The linear solver's setup function failed in an "
410 "unrecoverable manner"};
411 case ARK_LSOLVE_FAIL:
412 return {
"ARK_LSOLVE_FAIL",
413 "The linear solver's solve function failed in an "
414 "unrecoverable manner"};
415 case ARK_RHSFUNC_FAIL:
416 return {
"ARK_RHSFUNC_FAIL",
417 "The right-hand side function failed in an unrecoverable "
419 case ARK_FIRST_RHSFUNC_ERR:
420 return {
"ARK_FIRST_RHSFUNC_ERR",
421 "The right-hand side function failed at the first call"};
422 case ARK_REPTD_RHSFUNC_ERR:
423 return {
"ARK_REPTD_RHSFUNC_ERR",
424 "The right-hand side function had repeated recoverable errors"};
425 case ARK_UNREC_RHSFUNC_ERR:
426 return {
"ARK_UNREC_RHSFUNC_ERR",
427 "The right-hand side function had a recoverable error, but no "
428 "recovery is possible"};
429 case ARK_RTFUNC_FAIL:
430 return {
"ARK_RTFUNC_FAIL",
431 "The rootfinding function failed in an unrecoverable manner"};
433 return {
"ARK_LFREE_FAIL",
434 "The linear solver's memory deallocation function failed"};
435 case ARK_MASSINIT_FAIL:
436 return {
"ARK_MASSINIT_FAIL",
437 "The mass matrix linear solver's initialization function "
439 case ARK_MASSSETUP_FAIL:
440 return {
"ARK_MASSSETUP_FAIL",
441 "The mass matrix linear solver's setup function failed in an "
442 "unrecoverable manner"};
443 case ARK_MASSSOLVE_FAIL:
444 return {
"ARK_MASSSOLVE_FAIL",
445 "The mass matrix linear solver's solve function failed in an "
446 "unrecoverable manner"};
447 case ARK_MASSFREE_FAIL:
448 return {
"ARK_MASSFREE_FAIL",
449 "The mass matrix linear solver's memory deallocation "
451 case ARK_MASSMULT_FAIL:
452 return {
"ARK_MASSMULT_FAIL",
453 "The mass matrix-vector product function failed"};
454 case ARK_CONSTR_FAIL:
455 return {
"ARK_CONSTR_FAIL",
456 "The inequality constraint test failed repeatedly or failed "
457 "with the minimum step size"};
459 return {
"ARK_MEM_FAIL",
"A memory allocation failed"};
461 return {
"ARK_MEM_NULL",
"The arkode_mem argument was NULL"};
463 return {
"ARK_ILL_INPUT",
"One of the function inputs is illegal"};
465 return {
"ARK_NO_MALLOC",
466 "The ARKODE memory block was not allocated by a call to "
467 "ARKStepCreate, ERKStepCreate, or MRIStepCreate"};
469 return {
"ARK_BAD_K",
"The derivative order k is larger than allowed"};
471 return {
"ARK_BAD_T",
"The time t is outside the last step taken"};
473 return {
"ARK_BAD_DKY",
"The output derivative vector is NULL"};
475 return {
"ARK_TOO_CLOSE",
476 "The output and initial times are too close to each other"};
477 case ARK_VECTOROP_ERR:
478 return {
"ARK_VECTOROP_ERR",
479 "An error occurred when calling an N_Vector routine"};
480 case ARK_NLS_INIT_FAIL:
481 return {
"ARK_NLS_INIT_FAIL",
482 "An error occurred when initializing a SUNNonlinSol module"};
483 case ARK_NLS_SETUP_FAIL:
484 return {
"ARK_NLS_SETUP_FAIL",
485 "A non-recoverable error occurred when setting up a "
486 "SUNNonlinSol module"};
487 case ARK_NLS_SETUP_RECVR:
488 return {
"ARK_NLS_SETUP_RECVR",
489 "A recoverable error occurred when setting up a SUNNonlinSol "
492 return {
"ARK_NLS_OP_ERR",
493 "An error occurred when calling a set/get routine in a "
494 "SUNNonlinSol module"};
495 case ARK_INNERSTEP_ATTACH_ERR:
496 return {
"ARK_INNERSTEP_ATTACH_ERR",
497 "An error occurred when attaching the inner stepper module"};
498 case ARK_INNERSTEP_FAIL:
499 return {
"ARK_INNERSTEP_FAIL",
500 "An error occurred in the inner stepper module"};
501 case ARK_OUTERTOINNER_FAIL:
502 return {
"ARK_OUTERTOINNER_FAIL",
503 "An error occurred in the MRIStep pre inner integrator "
505 case ARK_INNERTOOUTER_FAIL:
506 return {
"ARK_INNERTOOUTER_FAIL",
507 "An error occurred in the MRIStep post inner integrator "
509 case ARK_POSTPROCESS_STEP_FAIL:
510 return {
"ARK_POSTPROCESS_STEP_FAIL",
511 "An error occurred in a step postprocessing function"};
512 case ARK_POSTPROCESS_STAGE_FAIL:
513 return {
"ARK_POSTPROCESS_STAGE_FAIL",
514 "An error occurred in a stage postprocessing function"};
515 case ARK_USER_PREDICT_FAIL:
516 return {
"ARK_USER_PREDICT_FAIL",
517 "An error occurred in a user predictor function"};
518 case ARK_INTERP_FAIL:
519 return {
"ARK_INTERP_FAIL",
520 "An error occurred in the ARKODE polynomial interpolation "
522 case ARK_INVALID_TABLE:
523 return {
"ARK_INVALID_TABLE",
524 "An invalid Butcher or MRI table was encountered"};
525 case ARK_CONTEXT_ERR:
526 return {
"ARK_CONTEXT_ERR",
527 "An error occurred with the SUNDIALS context object"};
528 case ARK_UNRECOGNIZED_ERROR:
529 return {
"ARK_UNRECOGNIZED_ERROR",
"An unknown error was encountered"};
531 return {
"ARK_UNKNOWN_ERROR",
"Unrecognized ARKODE error flag"};
536 std::ostringstream ss;
537 ss << func_name <<
" failed with error flag " << flag <<
": \n"
539 throw std::domain_error(ss.str());
554 std::ostringstream ss;
555 ss <<
"algebra_solver failed with error flag " << flag <<
".";
556 throw std::runtime_error(ss.str());
574 long int max_num_steps) {
575 std::ostringstream ss;
577 domain_error(
"algebra_solver",
"maximum number of iterations",
578 max_num_steps,
"(",
") was exceeded in the solve.");
579 }
else if (flag == -11) {
580 ss <<
"The linear solver’s setup function failed "
581 <<
"in an unrecoverable manner.";
582 throw std::runtime_error(ss.str());
583 }
else if (flag < 0) {
584 ss <<
"algebra_solver failed with error flag " << flag <<
".";
585 throw std::runtime_error(ss.str());
std::array< std::string, 2 > idas_flag_msg(int flag)
Map idas error flag to actually error msg.
std::array< std::string, 2 > cvodes_flag_msg(int flag)
Map cvodes error flag to actually error msg.
void arkode_check(int flag, const char *func_name)
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.
std::array< std::string, 2 > arkode_flag_msg(int flag)
Map ARKODE error flag to an error msg.
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 ...