|
4 | 4 | #include <kinsol/kinsol.h> |
5 | 5 | #include <cvodes/cvodes.h> |
6 | 6 | #include <stan/math/prim/meta.hpp> |
7 | | -#include <stan/math/prim/err/throw_domain_error.hpp> |
| 7 | +#include <stan/math/prim/err/domain_error.hpp> |
8 | 8 |
|
9 | 9 | namespace stan { |
10 | 10 | namespace math { |
@@ -34,26 +34,50 @@ inline void cvodes_check(int flag, const char* func_name) { |
34 | 34 | } |
35 | 35 |
|
36 | 36 | /** |
37 | | - * Throws an exception message when the function KINSol() |
38 | | - * (call to the solver) fails. When the exception is caused |
| 37 | + * Throws an exception message when the functions in KINSOL |
| 38 | + * fails. When the exception is caused |
| 39 | + * by a tuning parameter the user controls, gives a specific |
| 40 | + * error. "KINGetReturnFlagName()" from sundials has a mem leak bug so |
| 41 | + * until it's fixed we cannot use it to extract flag error string. |
| 42 | + * |
| 43 | + * @param flag Error flag |
| 44 | + * @param func_name calling function name |
| 45 | + * @throw <code>std::runtime_error</code> if the flag is negative. |
| 46 | + */ |
| 47 | + inline void kinsol_check(int flag, const char* func_name) { |
| 48 | + std::ostringstream ss; |
| 49 | + if (flag < 0) { |
| 50 | + ss << "algebra_solver failed with error flag " << flag << "."; |
| 51 | + throw std::runtime_error(ss.str()); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | +/** |
| 56 | + * Throws an exception message when the KINSol() call fails. |
| 57 | + * When the exception is caused |
39 | 58 | * by a tuning parameter the user controls, gives a specific |
40 | 59 | * error. |
41 | 60 | * |
42 | 61 | * @param flag Error flag |
| 62 | + * @param func_name calling function name |
| 63 | + * @param max_num_steps max number of nonlinear iters |
43 | 64 | * @throw <code>std::runtime_error</code> if the flag is negative. |
| 65 | + * @throw <code>std::domain_error</code> if the flag indicates max |
| 66 | + * number of steps is exceeded.. |
44 | 67 | */ |
45 | | -inline void kinsol_check(int flag, const char* func_name) { |
46 | | - std::ostringstream ss; |
47 | | - if (flag < 0) { |
48 | | - ss << func_name << " failed with error flag " << flag << ": " |
49 | | - << KINGetReturnFlagName(flag) << "."; |
| 68 | + inline void kinsol_check(int flag, const char* func_name, long int max_num_steps) { // NOLINT(runtime/int) |
| 69 | + std::ostringstream ss; |
50 | 70 | if (flag == -6) { |
51 | | - throw std::domain_error(ss.str()); |
52 | | - } else { |
| 71 | + domain_error("algebra_solver", "maximum number of iterations", |
| 72 | + max_num_steps, "(", ") was exceeded in the solve."); |
| 73 | + } else if (flag == -11) { |
| 74 | + ss << "The linear solver’s setup function failed in an unrecoverable manner."; |
| 75 | + throw std::runtime_error(ss.str()); |
| 76 | + } else if (flag < 0) { |
| 77 | + ss << "algebra_solver failed with error flag " << flag << "."; |
53 | 78 | throw std::runtime_error(ss.str()); |
54 | 79 | } |
55 | 80 | } |
56 | | -} |
57 | 81 |
|
58 | 82 | } // namespace math |
59 | 83 | } // namespace stan |
|
0 commit comments