13double linearInterp(
double x,
const vector<double>& xpts,
const vector<double>& fpts)
18 "len(xpts) = {}, len(fpts) = {}", xpts.size(), fpts.size());
22 if (x >= xpts.back()) {
25 auto loc = lower_bound(xpts.begin(), xpts.end(), x);
26 int iloc = int(loc - xpts.begin()) - 1;
27 double ff = fpts[iloc] +
28 (x - xpts[iloc])*(fpts[iloc + 1]
29 - fpts[iloc])/(xpts[iloc + 1] - xpts[iloc]);
33double trapezoidal(
const Eigen::ArrayXd& f,
const Eigen::ArrayXd& x)
36 if (f.size() != x.size()) {
38 "Vector lengths need to be the same.");
41 Eigen::VectorXd f_av = f.tail(f.size() - 1) + f.head(f.size() - 1);
43 Eigen::VectorXd x_diff = x.tail(x.size() - 1) - x.head(x.size() - 1);
45 if ((x_diff.array() <= 0.0).any()) {
47 "x (coordinate) needs to be the monotonically increasing.");
49 return f_av.dot(x_diff) / 2.0;
63double basicSimpson(
const Eigen::ArrayXd& f,
const Eigen::ArrayXd& x)
67 "Vector lengths need to be larger than two.");
69 if (f.size()%2 == 0) {
71 "Vector lengths need to be an odd number.");
74 size_t N = f.size() - 1;
75 Eigen::VectorXd h = x.tail(N) - x.head(N);
78 for (
size_t i = 1; i < N; i+=2) {
84 sum += (hph / 6.0) * (
85 (2.0 - hdh) * f[i - 1] + (pow(hph, 2) / hmh) * f[i] +
86 (2.0 - 1.0 / hdh) * f[i + 1]);
91double simpson(
const Eigen::ArrayXd& f,
const Eigen::ArrayXd& x)
93 Eigen::ArrayXd h = x.tail(x.size() - 1) - x.head(x.size() - 1);
94 if ((h <= 0.0).any()) {
96 "Values of x need to be positive and monotonically increasing.");
98 if (f.size() != x.size()) {
99 throw CanteraError(
"simpson",
"Vector lengths need to be the same.");
102 if (f.size()%2 == 1) {
104 }
else if (f.size() == 2) {
105 return 0.5 * h[0] * (f[1] + f[0]);
107 size_t N = f.size() - 1;
111 double tailTrap = 0.5 * h[N-1] * (f[N] + f[N-1]);
112 return headSimps + tailTrap;
117 const Eigen::ArrayXd& f,
118 const Eigen::ArrayXd& x)
120 if (method ==
"simpson") {
122 }
else if (method ==
"trapezoidal") {
126 "Unknown method of numerical quadrature. "
127 "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.
#define AssertThrowMsg(expr, procedure,...)
Assertion must be true or an error is thrown.
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.