13double linearInterp(
double x,
const vector<double>& xpts,
const vector<double>& fpts)
18 if (x >= xpts.back()) {
21 auto loc = lower_bound(xpts.begin(), xpts.end(), x);
22 int iloc = int(loc - xpts.begin()) - 1;
23 double ff = fpts[iloc] +
24 (x - xpts[iloc])*(fpts[iloc + 1]
25 - fpts[iloc])/(xpts[iloc + 1] - xpts[iloc]);
29double trapezoidal(
const Eigen::ArrayXd& f,
const Eigen::ArrayXd& x)
32 if (f.size() != x.size()) {
34 "Vector lengths need to be the same.");
37 Eigen::VectorXd f_av = f.tail(f.size() - 1) + f.head(f.size() - 1);
39 Eigen::VectorXd x_diff = x.tail(x.size() - 1) - x.head(x.size() - 1);
41 if ((x_diff.array() <= 0.0).any()) {
43 "x (coordinate) needs to be the monotonically increasing.");
45 return f_av.dot(x_diff) / 2.0;
59double basicSimpson(
const Eigen::ArrayXd& f,
const Eigen::ArrayXd& x)
63 "Vector lengths need to be larger than two.");
65 if (f.size()%2 == 0) {
67 "Vector lengths need to be an odd number.");
70 size_t N = f.size() - 1;
71 Eigen::VectorXd h = x.tail(N) - x.head(N);
74 for (
size_t i = 1; i < N; i+=2) {
80 sum += (hph / 6.0) * (
81 (2.0 - hdh) * f[i - 1] + (pow(hph, 2) / hmh) * f[i] +
82 (2.0 - 1.0 / hdh) * f[i + 1]);
87double simpson(
const Eigen::ArrayXd& f,
const Eigen::ArrayXd& x)
89 Eigen::ArrayXd h = x.tail(x.size() - 1) - x.head(x.size() - 1);
90 if ((h <= 0.0).any()) {
92 "Values of x need to be positive and monotonically increasing.");
94 if (f.size() != x.size()) {
95 throw CanteraError(
"simpson",
"Vector lengths need to be the same.");
98 if (f.size()%2 == 1) {
100 }
else if (f.size() == 2) {
101 return 0.5 * h[0] * (f[1] + f[0]);
103 size_t N = f.size() - 1;
107 double tailTrap = 0.5 * h[N-1] * (f[N] + f[N-1]);
108 return headSimps + tailTrap;
113 const Eigen::ArrayXd& f,
114 const Eigen::ArrayXd& x)
116 if (method ==
"simpson") {
118 }
else if (method ==
"trapezoidal") {
122 "Unknown method of numerical quadrature. "
123 "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.
double linearInterp(double x, const vector< double > &xpts, const vector< double > &fpts)
Linearly interpolate a function defined on a discrete grid.
double numericalQuadrature(const string &method, const Eigen::ArrayXd &f, const Eigen::ArrayXd &x)
Numerical integration of a function.
double trapezoidal(const Eigen::ArrayXd &f, const Eigen::ArrayXd &x)
Numerical integration of a function using the trapezoidal 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...
Namespace for the Cantera kernel.
double basicSimpson(const Eigen::ArrayXd &f, const Eigen::ArrayXd &x)
Numerical integration of a function using Simpson's rule.