21 if (x >= xpts.back()) {
24 auto loc = lower_bound(xpts.begin(), xpts.end(), x);
25 int iloc = int(loc - xpts.begin()) - 1;
26 doublereal ff = fpts[iloc] +
27 (x - xpts[iloc])*(fpts[iloc + 1]
28 - fpts[iloc])/(xpts[iloc + 1] - xpts[iloc]);
32double trapezoidal(
const Eigen::ArrayXd& f,
const Eigen::ArrayXd& x)
35 if (f.size() != x.size()) {
37 "Vector lengths need to be the same.");
40 Eigen::VectorXd f_av = f.tail(f.size() - 1) + f.head(f.size() - 1);
42 Eigen::VectorXd x_diff = x.tail(x.size() - 1) - x.head(x.size() - 1);
44 if ((x_diff.array() <= 0.0).any()) {
46 "x (coordinate) needs to be the monotonically increasing.");
48 return f_av.dot(x_diff) / 2.0;
62double basicSimpson(
const Eigen::ArrayXd& f,
const Eigen::ArrayXd& x)
66 "Vector lengths need to be larger than two.");
68 if (f.size()%2 == 0) {
70 "Vector lengths need to be an odd number.");
73 size_t N = f.size() - 1;
74 Eigen::VectorXd h = x.tail(N) - x.head(N);
77 for (
size_t i = 1; i < N; i+=2) {
83 sum += (hph / 6.0) * (
84 (2.0 - hdh) * f[i - 1] + (pow(hph, 2) / hmh) * f[i] +
85 (2.0 - 1.0 / hdh) * f[i + 1]);
90double simpson(
const Eigen::ArrayXd& f,
const Eigen::ArrayXd& x)
92 Eigen::ArrayXd h = x.tail(x.size() - 1) - x.head(x.size() - 1);
93 if ((h <= 0.0).any()) {
95 "Values of x need to be positive and monotonically increasing.");
97 if (f.size() != x.size()) {
98 throw CanteraError(
"simpson",
"Vector lengths need to be the same.");
101 if (f.size()%2 == 1) {
103 }
else if (f.size() == 2) {
104 return 0.5 * h[0] * (f[1] + f[0]);
106 size_t N = f.size() - 1;
110 double tailTrap = 0.5 * h[N-1] * (f[N] + f[N-1]);
111 return headSimps + tailTrap;
116 const Eigen::ArrayXd& f,
117 const Eigen::ArrayXd& x)
119 if (method ==
"simpson") {
121 }
else if (method ==
"trapezoidal") {
125 "Unknown method of numerical quadrature. "
126 "Please use 'simpson' or 'trapezoidal'");
Base class for exceptions thrown by Cantera classes.
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
Header for a file containing miscellaneous numerical functions.
Namespace for the Cantera kernel.
doublereal linearInterp(doublereal x, const vector_fp &xpts, const vector_fp &fpts)
Linearly interpolate a function defined on a discrete grid.
double trapezoidal(const Eigen::ArrayXd &f, const Eigen::ArrayXd &x)
Numerical integration of a function using the trapezoidal rule.
std::vector< double > vector_fp
Turn on the use of stl vectors for the basic array type within cantera Vector of doubles.
double numericalQuadrature(const std::string &method, const Eigen::ArrayXd &f, const Eigen::ArrayXd &x)
Numerical integration of a function.
double basicSimpson(const Eigen::ArrayXd &f, const Eigen::ArrayXd &x)
Numerical integration of a function using Simpson's rule.
double simpson(const Eigen::ArrayXd &f, const Eigen::ArrayXd &x)
Numerical integration of a function using Simpson's rule with flexibility of taking odd and even numb...