36 "Needs to be overloaded by Func1 specialization.");
41 if (
type() ==
"functor" ||
type() != other->type() || m_c != other->m_c) {
48 if (!m_f1->isIdentical(other->m_f1)) {
56 if (!m_f2->isIdentical(other->m_f2)) {
76Sin1::Sin1(span<const double> params)
78 if (params.size() != 1) {
80 "Constructor needs exactly one parameter (frequency).");
88 return fmt::format(
"\\sin({})", arg);
90 return fmt::format(
"\\sin({}{})", m_c, arg);
96 auto c = make_shared<Cos1>(m_c);
102Cos1::Cos1(span<const double> params)
104 if (params.size() != 1) {
106 "Constructor needs exactly one parameter (frequency).");
113 auto s = make_shared<Sin1>(m_c);
120 return fmt::format(
"\\cos({})", arg);
122 return fmt::format(
"\\cos({}{})", m_c, arg);
128Exp1::Exp1(span<const double> params)
130 if (params.size() != 1) {
132 "Constructor needs exactly one parameter (exponent factor).");
139 auto f = make_shared<Exp1>(m_c);
149 return fmt::format(
"\\exp({})", arg);
151 return fmt::format(
"\\exp({}{})", m_c, arg);
155Log1::Log1(span<const double> params)
157 if (params.size() != 1) {
159 "Constructor needs exactly one parameter (factor).");
166 auto f = make_shared<Pow1>(-1.);
176 return fmt::format(
"\\log({})", arg);
178 return fmt::format(
"\\log({}{})", m_c, arg);
183Pow1::Pow1(span<const double> params)
185 if (params.size() != 1) {
187 "Constructor needs exactly one parameter (exponent).");
195 return make_shared<Const1>(0.0);
198 return make_shared<Const1>(1.0);
200 auto f = make_shared<Pow1>(m_c - 1.);
206Const1::Const1(span<const double> params)
208 if (params.size() != 1) {
210 "Constructor needs exactly one parameter (constant).");
217 if (params.size() == 0) {
219 "Constructor needs an array that is not empty.");
221 size_t n = params.size() - 1;
222 m_cpoly.resize(n + 1);
223 copy(params.data(), params.data() + m_cpoly.size(), m_cpoly.begin());
230 if (m_cpoly[m_cpoly.size()-1] != 0.) {
231 out = fmt::format(
"{}", m_cpoly[m_cpoly.size()-1]);
233 for (
size_t n=1; n<m_cpoly.size(); n++) {
234 if (m_cpoly[m_cpoly.size()-1-n] == 0.) {
238 if (m_cpoly[m_cpoly.size()-1-n] == 1.) {
239 term = fmt::format(
"{}", arg);
240 }
else if (m_cpoly[m_cpoly.size()-1-n] == -1.) {
241 term = fmt::format(
"-{}", arg);
243 term = fmt::format(
"{}{}", m_cpoly[m_cpoly.size()-1-n], arg);
246 term = fmt::format(
"{}^{{{}}}", term, n);
248 term = fmt::format(
"{}^{}", term, n);
252 }
else if (out[0] ==
'-') {
253 out = fmt::format(
"{} - {}", term, out.substr(1));
255 out = fmt::format(
"{} + {}", term, out);
261Fourier1::Fourier1(span<const double> params)
263 if (params.size() < 4) {
265 "Constructor needs an array with at least 4 entries.");
267 if (params.size() % 2 != 0) {
269 "Constructor needs an array with an even number of entries.");
271 size_t n = params.size() / 2 - 1;
272 m_omega = params[n + 1];
273 m_a0_2 = 0.5 * params[0];
276 copy(params.data() + 1, params.data() + n + 1, m_ccos.begin());
277 copy(params.data() + n + 2, params.data() + 2 * n + 2, m_csin.begin());
280Gaussian1::Gaussian1(span<const double> params)
282 if (params.size() != 3) {
284 "Constructor needs exactly 3 parameters (amplitude, center, width).");
288 m_tau = params[2] / (2. * sqrt(log(2.)));
293 if (params.size() < 3) {
295 "Constructor needs an array with at least 3 entries.");
297 if (params.size() % 3 != 0) {
299 "Constructor needs an array with multiples of 3 entries.");
301 size_t n = params.size() / 3;
305 for (
size_t i = 0; i < n; i++) {
307 m_A[i] = params[loc];
308 m_b[i] = params[loc + 1];
309 m_E[i] = params[loc + 2];
314 const string& method)
316 if (tvals.size() != fvals.size()) {
318 "Expected matching time/value lengths, got {} and {}.",
319 tvals.size(), fvals.size());
321 m_tvec.assign(tvals.begin(), tvals.end());
322 for (
auto it = std::begin(
m_tvec) + 1; it != std::end(
m_tvec); it++) {
323 if (*(it - 1) > *it) {
325 "time values are not increasing monotonically.");
328 m_fvec.assign(fvals.begin(), fvals.end());
334 if (params.size() < 4) {
336 "Constructor needs an array with at least 4 entries.");
338 if (params.size() % 2 != 0) {
340 "Constructor needs an array with an even number of entries.");
342 size_t n = params.size() / 2;
343 m_tvec.assign(params.data(), params.data() + n);
344 for (
auto it = std::begin(
m_tvec) + 1; it != std::end(
m_tvec); it++) {
345 if (*(it - 1) > *it) {
347 "Time values are not monotonically increasing.");
350 m_fvec.assign(params.data() + n, params.data() + 2 * n);
355 if (method ==
"linear") {
357 }
else if (method ==
"previous") {
361 "Interpolation method '{}' is not implemented.", method);
366 size_t siz =
m_tvec.size();
370 }
else if (t >=
m_tvec[siz-1]) {
374 while (t >
m_tvec[ix+1]) {
391 size_t siz =
m_tvec.size();
395 for (
size_t i=1; i<siz; i++) {
398 tvec.push_back(
m_tvec[i-1]);
402 tvec.push_back(
m_tvec[siz-1]);
406 tvec.push_back(
m_tvec[0]);
407 tvec.push_back(
m_tvec[siz-1]);
411 return make_shared<Tabulated1>(tvec, dvec,
"previous");
418 return fmt::format(
"\\mathrm{{{}}}({})",
type(), arg);
424 return "\\sqrt{" + arg +
"}";
427 return "\\frac{1}{\\sqrt{" + arg +
"}}";
430 return fmt::format(
"\\left({}\\right)^{{{}}}", arg, m_c);
438 return fmt::format(
"\\mathrm{{Tabulated}}({})", arg);
443 return fmt::format(
"{}", m_c);
448 return "\\frac{" + m_f1->write(arg) +
"}{" + m_f2->write(arg) +
"}";
461 string s = m_f1->write(arg);
462 if (m_f1->order() <
order()) {
463 s =
"\\left(" + s +
"\\right)";
465 string s2 = m_f2->write(arg);
466 if (m_f2->order() <
order()) {
467 s2 =
"\\left(" + s2 +
"\\right)";
480 string s1 = m_f1->write(arg);
481 string s2 = m_f2->write(arg);
483 return s1 +
" - " + s2.substr(1,s2.size());
485 return s1 +
" + " + s2;
491 string s1 = m_f1->write(arg);
492 string s2 = m_f2->write(arg);
494 return s1 +
" + " + s2.substr(1,s2.size());
496 return s1 +
" - " + s2;
502 string g = m_f2->write(arg);
503 return m_f1->write(g);
507 auto d1 = m_f1->derivative();
508 auto d2 = m_f2->derivative();
515 string s = m_f1->write(arg);
516 if (m_f1->order() <
order()) {
517 s =
"\\left(" + s +
"\\right)";
526 if (n >=
'0' && n <=
'9') {
527 s =
"\\left(" + s +
"\\right)";
529 return fmt::format(
"{}{}", m_c, s);
535 return m_f1->write(arg);
537 return fmt::format(
"{} + {}", m_f1->write(arg), m_c);
542bool isConstant(
const shared_ptr<Func1>& f)
544 return f->type() ==
"constant";
547bool isZero(
const shared_ptr<Func1>& f)
549 return f->type() ==
"constant" && f->c() == 0.0;
552bool isOne(
const shared_ptr<Func1>& f)
554 return f->type() ==
"constant" && f->c() == 1.0;
557bool isTimesConst(
const shared_ptr<Func1>& f)
559 return f->type() ==
"times-constant";
562bool isExp(
const shared_ptr<Func1>& f)
564 return f->type() ==
"exp";
567bool isPow(
const shared_ptr<Func1>& f)
569 return f->type() ==
"pow";
572pair<bool, double> isProportional(
573 const shared_ptr<Func1>&f1,
const shared_ptr<Func1>&f2)
575 bool tc1 = isTimesConst(f1);
576 bool tc2 = isTimesConst(f2);
578 if (f1->isIdentical(f2)) {
584 if (f1->isIdentical((f2->func1_shared()))) {
585 return {
true, f2->c()};
590 if (f2->isIdentical((f1->func1_shared()))) {
591 return {
true, 1. / f1->c()};
595 if (f2->func1_shared()->isIdentical((f1->func1_shared()))) {
596 return {
true, f2->c() / f1->c()};
605 if (f1->isIdentical(f2)) {
614 if (isConstant(f2)) {
617 if (isConstant(f1)) {
620 auto [prop, c] = isProportional(f1, f2);
623 return make_shared<Const1>(0.);
627 return make_shared<Sum1>(f1, f2);
638 if (f1->isIdentical(f2)) {
639 return make_shared<Const1>(0.);
641 if (isConstant(f2)) {
644 auto [prop, c] = isProportional(f1, f2);
647 return make_shared<Const1>(0.);
651 return make_shared<Diff1>(f1, f2);
662 if (isZero(f1) || isZero(f2)) {
663 return make_shared<Const1>(0.);
665 if (isConstant(f1) && isConstant(f2)) {
666 return make_shared<Const1>(f1->c() * f2->c());
668 if (isConstant(f1)) {
671 if (isConstant(f2)) {
674 if (isPow(f1) && isPow(f2)) {
675 return make_shared<Pow1>(f1->c() + f2->c());
677 if (isExp(f1) && isExp(f2)) {
678 return make_shared<Exp1>(f1->c() + f2->c());
681 bool tc1 = isTimesConst(f1);
682 bool tc2 = isTimesConst(f2);
689 ff1 = f1->func1_shared();
695 ff2 = f2->func1_shared();
699 if (c1 * c2 != 1.0) {
704 return make_shared<Product1>(f1, f2);
713 return make_shared<Const1>(0.);
716 throw CanteraError(
"newRatioFunction",
"Division by zero.");
718 if (f1->isIdentical(f2)) {
719 return make_shared<Const1>(1.);
721 if (isConstant(f2)) {
724 if (isPow(f1) && isPow(f2)) {
725 return make_shared<Pow1>(f1->c() - f2->c());
727 if (isExp(f1) && isExp(f2)) {
728 return make_shared<Exp1>(f1->c() - f2->c());
730 return make_shared<Ratio1>(f1, f2);
736 return make_shared<Const1>(0.0);
738 if (isConstant(f1)) {
741 if (isPow(f1) && f1->c() == 1.0) {
744 if (isPow(f1) && f1->c() == 0.0) {
745 return make_shared<Const1>(1.);
747 if (isPow(f1) && isPow(f2)) {
748 return make_shared<Pow1>(f1->c() * f2->c());
750 return make_shared<Composite1>(f1, f2);
756 return make_shared<Const1>(0.0);
761 if (f->type() ==
"times-constant") {
762 return make_shared<TimesConstant1>(f->func1_shared(), f->c() * c);
764 return make_shared<TimesConstant1>(f, c);
773 return make_shared<Const1>(f->c() + c);
775 if (f->type() ==
"plus-constant") {
776 return make_shared<PlusConstant1>(f->func1_shared(), f->c() + c);
778 return make_shared<PlusConstant1>(f, c);
Arrhenius1(span< const double > params)
Constructor uses parameters in the following order: .
Base class for exceptions thrown by Cantera classes.
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
string write(const string &arg) const override
Write LaTeX string describing function.
string write(const string &arg) const override
Write LaTeX string describing function.
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
string write(const string &arg) const override
Write LaTeX string describing function.
string write(const string &arg) const override
Write LaTeX string describing function.
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
string write(const string &arg) const override
Write LaTeX string describing function.
string typeName() const
Returns a string with the class name of the functor.
virtual shared_ptr< Func1 > derivative() const
Creates a derivative to the current function.
virtual string type() const
Returns a string describing the type of the function.
virtual double eval(double t) const
Evaluate the function.
virtual string write(const string &arg) const
Write LaTeX string describing function.
double operator()(double t) const
Calls method eval to evaluate the function.
virtual bool isIdentical(shared_ptr< Func1 > other) const
Routine to determine if two functions are the same.
virtual int order() const
Return the order of the function, if it makes sense.
double c() const
Accessor function for the stored constant m_c.
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
string write(const string &arg) const override
Write LaTeX string describing function.
An error indicating that an unimplemented function has been called.
string write(const string &arg) const override
Write LaTeX string describing function.
string write(const string &arg) const override
Write LaTeX string describing function.
Poly1(span< const double > params)
Constructor uses parameters in the following order: .
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
string write(const string &arg) const override
Write LaTeX string describing function.
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
int order() const override
Return the order of the function, if it makes sense.
string write(const string &arg) const override
Write LaTeX string describing function.
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
string write(const string &arg) const override
Write LaTeX string describing function.
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
string write(const string &arg) const override
Write LaTeX string describing function.
string write(const string &arg) const override
Write LaTeX string describing function.
double eval(double t) const override
Evaluate the function.
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
string write(const string &arg) const override
Write LaTeX string describing function.
void setMethod(const string &method)
Set the interpolation method.
vector< double > m_tvec
Vector of time values.
bool m_isLinear
Boolean indicating interpolation method.
vector< double > m_fvec
Vector of function values.
Tabulated1(span< const double > tvals, span< const double > fvals, const string &method="linear")
Constructor.
int order() const override
Return the order of the function, if it makes sense.
string write(const string &arg) const override
Write LaTeX string describing function.
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
This file contains definitions for utility functions and text for modules, inputfiles and logging,...
shared_ptr< Func1 > newCompositeFunction(shared_ptr< Func1 > f1, shared_ptr< Func1 > f2)
Composite of two functions.
shared_ptr< Func1 > newProdFunction(shared_ptr< Func1 > f1, shared_ptr< Func1 > f2)
Product of two functions.
shared_ptr< Func1 > newDiffFunction(shared_ptr< Func1 > f1, shared_ptr< Func1 > f2)
Difference of two functions.
shared_ptr< Func1 > newTimesConstFunction(shared_ptr< Func1 > f, double c)
Product of function and constant.
shared_ptr< Func1 > newSumFunction(shared_ptr< Func1 > f1, shared_ptr< Func1 > f2)
Sum of two functions.
shared_ptr< Func1 > newRatioFunction(shared_ptr< Func1 > f1, shared_ptr< Func1 > f2)
Ratio of two functions.
shared_ptr< Func1 > newPlusConstFunction(shared_ptr< Func1 > f, double c)
Sum of function and constant.
string demangle(const std::type_info &type)
Convert a type name to a human readable string, using boost::core::demangle if available.
Namespace for the Cantera kernel.
Contains declarations for string manipulation functions within Cantera.