Cantera  4.0.0a1
Loading...
Searching...
No Matches
FuncEval.cpp
2#include <sstream>
3
4namespace Cantera
5{
6
7int FuncEval::evalNoThrow(double t, span<const double> y, span<double> ydot)
8{
9 try {
10 eval(t, y, ydot, m_sens_params);
11 } catch (CanteraError& err) {
12 if (suppressErrors()) {
13 m_errors.push_back(err.what());
14 } else {
15 writelog(err.what());
16 }
17 return 1; // possibly recoverable error
18 } catch (std::exception& err) {
19 if (suppressErrors()) {
20 m_errors.push_back(err.what());
21 } else {
22 writelog("FuncEval::eval_nothrow: unhandled exception:\n");
23 writelog(err.what());
25 }
26 return -1; // unrecoverable error
27 } catch (...) {
28 string msg = "FuncEval::eval_nothrow: unhandled exception of unknown type\n";
29 if (suppressErrors()) {
30 m_errors.push_back(msg);
31 } else {
32 writelog(msg);
33 }
34 return -1; // unrecoverable error
35 }
36 return 0; // successful evaluation
37}
38
39int FuncEval::evalDaeNoThrow(double t, span<const double> y, span<const double> ydot,
40 span<double> r)
41{
42 try {
43 evalDae(t, y, ydot, m_sens_params, r);
44 } catch (CanteraError& err) {
45 if (suppressErrors()) {
46 m_errors.push_back(err.what());
47 } else {
48 writelog(err.what());
49 }
50 return 1; // possibly recoverable error
51 } catch (std::exception& err) {
52 if (suppressErrors()) {
53 m_errors.push_back(err.what());
54 } else {
55 writelog("FuncEval::eval_nothrow: unhandled exception:\n");
56 writelog(err.what());
58 }
59 return -1; // unrecoverable error
60 } catch (...) {
61 string msg = "FuncEval::eval_nothrow: unhandled exception of unknown type\n";
62 if (suppressErrors()) {
63 m_errors.push_back(msg);
64 } else {
65 writelog(msg);
66 }
67 return -1; // unrecoverable error
68 }
69 return 0; // successful evaluation
70}
71
72string FuncEval::getErrors() const {
73 std::stringstream errs;
74 for (const auto& err : m_errors) {
75 errs << err;
76 errs << "\n";
77 }
78 return errs.str();
79}
80
81int FuncEval::preconditioner_setup_nothrow(double t, span<const double> y, double gamma)
82{
83 try {
84 preconditionerSetup(t, y, gamma);
85 } catch (CanteraError& err) {
86 if (suppressErrors()) {
87 m_errors.push_back(err.what());
88 } else {
89 writelog(err.what());
90 }
91 return 1; // possibly recoverable error
92 } catch (std::exception& err) {
93 if (suppressErrors()) {
94 m_errors.push_back(err.what());
95 } else {
96 writelog("FuncEval::preconditioner_setup_nothrow: unhandled exception:\n");
97 writelog(err.what());
99 }
100 return -1; // unrecoverable error
101 } catch (...) {
102 string msg = "FuncEval::preconditioner_setup_nothrow: unhandled exception"
103 " of unknown type\n";
104 if (suppressErrors()) {
105 m_errors.push_back(msg);
106 } else {
107 writelog(msg);
108 }
109 return -1; // unrecoverable error
110 }
111 return 0; // successful evaluation
112}
113
114int FuncEval::preconditioner_solve_nothrow(span<const double> rhs, span<double> output)
115{
116 try {
117 preconditionerSolve(rhs, output); // perform preconditioner solve
118 } catch (CanteraError& err) {
119 if (suppressErrors()) {
120 m_errors.push_back(err.what());
121 } else {
122 writelog(err.what());
123 }
124 return 1; // possibly recoverable error
125 } catch (std::exception& err) {
126 if (suppressErrors()) {
127 m_errors.push_back(err.what());
128 } else {
129 writelog("FuncEval::preconditioner_solve_nothrow: unhandled exception:\n");
130 writelog(err.what());
131 writelogendl();
132 }
133 return -1; // unrecoverable error
134 } catch (...) {
135 string msg = "FuncEval::preconditioner_solve_nothrow: unhandled exception"
136 " of unknown type\n";
137 if (suppressErrors()) {
138 m_errors.push_back(msg);
139 } else {
140 writelog(msg);
141 }
142 return -1; // unrecoverable error
143 }
144 return 0; // successful evaluation
145}
146
147int FuncEval::evalRootFunctionsNoThrow(double t, span<const double> y, span<double> gout)
148{
149 try {
150 evalRootFunctions(t, y, gout);
151 } catch (CanteraError& err) {
152 if (suppressErrors()) {
153 m_errors.push_back(err.what());
154 } else {
155 writelog(err.what());
156 }
157 return 1; // possibly recoverable error
158 } catch (std::exception& err) {
159 if (suppressErrors()) {
160 m_errors.push_back(err.what());
161 } else {
162 writelog("FuncEval::evalRootFunctionsNoThrow: unhandled exception:\n");
163 writelog(err.what());
164 writelogendl();
165 }
166 return -1; // unrecoverable error
167 } catch (...) {
168 string msg = "FuncEval::evalRootFunctionsNoThrow: unhandled exception"
169 " of unknown type\n";
170 if (suppressErrors()) {
171 m_errors.push_back(msg);
172 } else {
173 writelog(msg);
174 }
175 return -1; // unrecoverable error
176 }
177 return 0; // successful evaluation
178}
179
180}
Base class for exceptions thrown by Cantera classes.
const char * what() const override
Get a description of the error.
virtual void eval(double t, span< const double > y, span< double > ydot, span< const double > p)
Evaluate the right-hand-side ODE function.
Definition FuncEval.h:44
int evalRootFunctionsNoThrow(double t, span< const double > y, span< double > gout)
Wrapper for evalRootFunctions that converts exceptions to return codes.
Definition FuncEval.cpp:147
virtual void preconditionerSolve(span< const double > rhs, span< double > output)
Evaluate the linear system Ax=b where A is the preconditioner.
Definition FuncEval.h:110
virtual void evalRootFunctions(double t, span< const double > y, span< double > gout)
Evaluate the event/root functions currently in play.
Definition FuncEval.h:157
bool suppressErrors() const
Get current state of error suppression.
Definition FuncEval.h:190
int preconditioner_setup_nothrow(double t, span< const double > y, double gamma)
Preconditioner setup that doesn't throw an error but returns a CVODES flag.
Definition FuncEval.cpp:81
int evalNoThrow(double t, span< const double > y, span< double > ydot)
Evaluate the right-hand side using return code to indicate status.
Definition FuncEval.cpp:7
int preconditioner_solve_nothrow(span< const double > rhs, span< double > output)
Preconditioner solve that doesn't throw an error but returns a CVODES flag.
Definition FuncEval.cpp:114
virtual void evalDae(double t, span< const double > y, span< const double > ydot, span< const double > p, span< double > residual)
Evaluate the right-hand-side DAE function.
Definition FuncEval.h:57
int evalDaeNoThrow(double t, span< const double > y, span< const double > ydot, span< double > residual)
Evaluate the right-hand side using return code to indicate status.
Definition FuncEval.cpp:39
vector< string > m_errors
Errors occurring during function evaluations.
Definition FuncEval.h:215
string getErrors() const
Return a string containing the text of any suppressed errors.
Definition FuncEval.cpp:72
virtual void preconditionerSetup(double t, span< const double > y, double gamma)
Evaluate the setup processes for the Jacobian preconditioner.
Definition FuncEval.h:99
vector< double > m_sens_params
Values for the problem parameters for which sensitivities are computed This is the array which is per...
Definition FuncEval.h:205
void writelog(const string &fmt, const Args &... args)
Write a formatted message to the screen.
Definition global.h:171
void writelogendl()
Write an end of line character to the screen and flush output.
Definition global.cpp:41
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595