Cantera  4.0.0a2
Loading...
Searching...
No Matches
FuncEval.cpp
Go to the documentation of this file.
1//! @file FuncEval.cpp
2
3// This file is part of Cantera. See License.txt in the top-level directory or
4// at https://cantera.org/license.txt for license and copyright information.
5
7#include <sstream>
8
9namespace Cantera
10{
11
12int FuncEval::evalNoThrow(double t, span<const double> y, span<double> ydot)
13{
14 try {
15 eval(t, y, ydot, m_sens_params);
16 } catch (CanteraError& err) {
17 if (suppressErrors()) {
18 m_errors.push_back(err.what());
19 } else {
20 writelog(err.what());
21 }
22 return 1; // possibly recoverable error
23 } catch (std::exception& err) {
24 if (suppressErrors()) {
25 m_errors.push_back(err.what());
26 } else {
27 writelog("FuncEval::eval_nothrow: unhandled exception:\n");
28 writelog(err.what());
30 }
31 return -1; // unrecoverable error
32 } catch (...) {
33 string msg = "FuncEval::eval_nothrow: unhandled exception of unknown type\n";
34 if (suppressErrors()) {
35 m_errors.push_back(msg);
36 } else {
37 writelog(msg);
38 }
39 return -1; // unrecoverable error
40 }
41 return 0; // successful evaluation
42}
43
44int FuncEval::evalDaeNoThrow(double t, span<const double> y, span<const double> ydot,
45 span<double> r)
46{
47 try {
48 evalDae(t, y, ydot, m_sens_params, r);
49 } catch (CanteraError& err) {
50 if (suppressErrors()) {
51 m_errors.push_back(err.what());
52 } else {
53 writelog(err.what());
54 }
55 return 1; // possibly recoverable error
56 } catch (std::exception& err) {
57 if (suppressErrors()) {
58 m_errors.push_back(err.what());
59 } else {
60 writelog("FuncEval::eval_nothrow: unhandled exception:\n");
61 writelog(err.what());
63 }
64 return -1; // unrecoverable error
65 } catch (...) {
66 string msg = "FuncEval::eval_nothrow: unhandled exception of unknown type\n";
67 if (suppressErrors()) {
68 m_errors.push_back(msg);
69 } else {
70 writelog(msg);
71 }
72 return -1; // unrecoverable error
73 }
74 return 0; // successful evaluation
75}
76
77string FuncEval::getErrors() const {
78 std::stringstream errs;
79 for (const auto& err : m_errors) {
80 errs << err;
81 errs << "\n";
82 }
83 return errs.str();
84}
85
86int FuncEval::preconditioner_setup_nothrow(double t, span<const double> y, double gamma)
87{
88 try {
89 preconditionerSetup(t, y, gamma);
90 } catch (CanteraError& err) {
91 if (suppressErrors()) {
92 m_errors.push_back(err.what());
93 } else {
94 writelog(err.what());
95 }
96 return 1; // possibly recoverable error
97 } catch (std::exception& err) {
98 if (suppressErrors()) {
99 m_errors.push_back(err.what());
100 } else {
101 writelog("FuncEval::preconditioner_setup_nothrow: unhandled exception:\n");
102 writelog(err.what());
103 writelogendl();
104 }
105 return -1; // unrecoverable error
106 } catch (...) {
107 string msg = "FuncEval::preconditioner_setup_nothrow: unhandled exception"
108 " of unknown type\n";
109 if (suppressErrors()) {
110 m_errors.push_back(msg);
111 } else {
112 writelog(msg);
113 }
114 return -1; // unrecoverable error
115 }
116 return 0; // successful evaluation
117}
118
119int FuncEval::preconditioner_solve_nothrow(span<const double> rhs, span<double> output)
120{
121 try {
122 preconditionerSolve(rhs, output); // perform preconditioner solve
123 } catch (CanteraError& err) {
124 if (suppressErrors()) {
125 m_errors.push_back(err.what());
126 } else {
127 writelog(err.what());
128 }
129 return 1; // possibly recoverable error
130 } catch (std::exception& err) {
131 if (suppressErrors()) {
132 m_errors.push_back(err.what());
133 } else {
134 writelog("FuncEval::preconditioner_solve_nothrow: unhandled exception:\n");
135 writelog(err.what());
136 writelogendl();
137 }
138 return -1; // unrecoverable error
139 } catch (...) {
140 string msg = "FuncEval::preconditioner_solve_nothrow: unhandled exception"
141 " of unknown type\n";
142 if (suppressErrors()) {
143 m_errors.push_back(msg);
144 } else {
145 writelog(msg);
146 }
147 return -1; // unrecoverable error
148 }
149 return 0; // successful evaluation
150}
151
152int FuncEval::evalRootFunctionsNoThrow(double t, span<const double> y, span<double> gout)
153{
154 try {
155 evalRootFunctions(t, y, gout);
156 } catch (CanteraError& err) {
157 if (suppressErrors()) {
158 m_errors.push_back(err.what());
159 } else {
160 writelog(err.what());
161 }
162 return 1; // possibly recoverable error
163 } catch (std::exception& err) {
164 if (suppressErrors()) {
165 m_errors.push_back(err.what());
166 } else {
167 writelog("FuncEval::evalRootFunctionsNoThrow: unhandled exception:\n");
168 writelog(err.what());
169 writelogendl();
170 }
171 return -1; // unrecoverable error
172 } catch (...) {
173 string msg = "FuncEval::evalRootFunctionsNoThrow: unhandled exception"
174 " of unknown type\n";
175 if (suppressErrors()) {
176 m_errors.push_back(msg);
177 } else {
178 writelog(msg);
179 }
180 return -1; // unrecoverable error
181 }
182 return 0; // successful evaluation
183}
184
185}
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:152
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:86
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:12
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:119
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:44
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:77
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