Cantera  3.1.0a1
Loading...
Searching...
No Matches
FuncEval.cpp
2#include <sstream>
3
4namespace Cantera
5{
6
7int FuncEval::evalNoThrow(double t, double* y, double* ydot)
8{
9 try {
10 eval(t, y, ydot, m_sens_params.data());
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, double* y, double* ydot, double* r)
40{
41 try {
42 evalDae(t, y, ydot, m_sens_params.data(), r);
43 } catch (CanteraError& err) {
44 if (suppressErrors()) {
45 m_errors.push_back(err.what());
46 } else {
47 writelog(err.what());
48 }
49 return 1; // possibly recoverable error
50 } catch (std::exception& err) {
51 if (suppressErrors()) {
52 m_errors.push_back(err.what());
53 } else {
54 writelog("FuncEval::eval_nothrow: unhandled exception:\n");
55 writelog(err.what());
57 }
58 return -1; // unrecoverable error
59 } catch (...) {
60 string msg = "FuncEval::eval_nothrow: unhandled exception of unknown type\n";
61 if (suppressErrors()) {
62 m_errors.push_back(msg);
63 } else {
64 writelog(msg);
65 }
66 return -1; // unrecoverable error
67 }
68 return 0; // successful evaluation
69}
70
71string FuncEval::getErrors() const {
72 std::stringstream errs;
73 for (const auto& err : m_errors) {
74 errs << err;
75 errs << "\n";
76 }
77 return errs.str();
78}
79
80int FuncEval::preconditioner_setup_nothrow(double t, double* y, double gamma)
81{
82 try {
83 preconditionerSetup(t, y, gamma);
84 } catch (CanteraError& err) {
85 if (suppressErrors()) {
86 m_errors.push_back(err.what());
87 } else {
88 writelog(err.what());
89 }
90 return 1; // possibly recoverable error
91 } catch (std::exception& err) {
92 if (suppressErrors()) {
93 m_errors.push_back(err.what());
94 } else {
95 writelog("FuncEval::preconditioner_setup_nothrow: unhandled exception:\n");
96 writelog(err.what());
98 }
99 return -1; // unrecoverable error
100 } catch (...) {
101 string msg = "FuncEval::preconditioner_setup_nothrow: unhandled exception"
102 " of unknown type\n";
103 if (suppressErrors()) {
104 m_errors.push_back(msg);
105 } else {
106 writelog(msg);
107 }
108 return -1; // unrecoverable error
109 }
110 return 0; // successful evaluation
111}
112
113int FuncEval::preconditioner_solve_nothrow(double* rhs, double* output)
114{
115 try {
116 preconditionerSolve(rhs, output); // perform preconditioner solve
117 } catch (CanteraError& err) {
118 if (suppressErrors()) {
119 m_errors.push_back(err.what());
120 } else {
121 writelog(err.what());
122 }
123 return 1; // possibly recoverable error
124 } catch (std::exception& err) {
125 if (suppressErrors()) {
126 m_errors.push_back(err.what());
127 } else {
128 writelog("FuncEval::preconditioner_solve_nothrow: unhandled exception:\n");
129 writelog(err.what());
130 writelogendl();
131 }
132 return -1; // unrecoverable error
133 } catch (...) {
134 string msg = "FuncEval::preconditioner_solve_nothrow: unhandled exception"
135 " of unknown type\n";
136 if (suppressErrors()) {
137 m_errors.push_back(msg);
138 } else {
139 writelog(msg);
140 }
141 return -1; // unrecoverable error
142 }
143 return 0; // successful evaluation
144}
145
146}
Base class for exceptions thrown by Cantera classes.
const char * what() const override
Get a description of the error.
virtual void evalDae(double t, double *y, double *ydot, double *p, double *residual)
Evaluate the right-hand-side DAE function.
Definition FuncEval.h:56
int preconditioner_solve_nothrow(double *rhs, double *output)
Preconditioner solve that doesn't throw an error but returns a CVODES flag.
Definition FuncEval.cpp:113
int preconditioner_setup_nothrow(double t, double *y, double gamma)
Preconditioner setup that doesn't throw an error but returns a CVODES flag.
Definition FuncEval.cpp:80
int evalDaeNoThrow(double t, double *y, double *ydot, double *residual)
Evaluate the right-hand side using return code to indicate status.
Definition FuncEval.cpp:39
int evalNoThrow(double t, double *y, double *ydot)
Evaluate the right-hand side using return code to indicate status.
Definition FuncEval.cpp:7
bool suppressErrors() const
Get current state of error suppression.
Definition FuncEval.h:166
virtual void eval(double t, double *y, double *ydot, double *p)
Evaluate the right-hand-side ODE function.
Definition FuncEval.h:44
vector< string > m_errors
Errors occurring during function evaluations.
Definition FuncEval.h:191
string getErrors() const
Return a string containing the text of any suppressed errors.
Definition FuncEval.cpp:71
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:181
virtual void preconditionerSolve(double *rhs, double *output)
Evaluate the linear system Ax=b where A is the preconditioner.
Definition FuncEval.h:108
virtual void preconditionerSetup(double t, double *y, double gamma)
Evaluate the setup processes for the Jacobian preconditioner.
Definition FuncEval.h:97
void writelog(const string &fmt, const Args &... args)
Write a formatted message to the screen.
Definition global.h:175
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:564