Cantera  3.1.0
Loading...
Searching...
No Matches
Func1.h
Go to the documentation of this file.
1/**
2 * @file Func1.h
3 */
4
5// This file is part of Cantera. See License.txt in the top-level directory or
6// at https://cantera.org/license.txt for license and copyright information.
7
8#ifndef CT_FUNC1_H
9#define CT_FUNC1_H
10
13
14#include <iostream>
15
16namespace Cantera
17{
18
19class TimesConstant1;
20
21//! @defgroup func1 Functor Objects
22//! Functors implement functions of a single variable @f$ f(x) @f$.
23//! Functor objects can be combined to form compound expressions, which allows for
24//! the implementation of generic mathematical expressions.
25//! @ingroup numerics
26
27//! @defgroup func1basic Basic Functors
28//! Basic functors implement standard mathematical expressions with a single
29//! parameter.
30//! The following simple functor types are implemented:
31//! - @c "sin" (class Sin1), @c "cos" (class Cos1),
32//! - @c "exp" (class Exp1), @c "log" (class Log1),
33//! - @c "pow" (class Pow1),
34//! - @c "constant" (class Const1).
35//! @ingroup func1
36
37//! @defgroup func1advanced Advanced Functors
38//! Advanced functors implement expressions that require multiple parameters.
39//! The following advanced functor types are implemented:
40//! - @c "tabulated-linear" and @c "tabulated-previous" (class Tabulated1),
41//! - @c "polynomial3" (class Poly13),
42//! - @c "Fourier" (class Fourier1),
43//! - @c "Gaussian" (class Gaussian1),
44//! - @c "Arrhenius" (class Arrhenius1).
45//! @ingroup func1
46
47//! @defgroup func1compound Compound Functors
48//! Compound functors implement expressions that are composed of other functors.
49//! The following compound functor types are implemented:
50//! - @c "sum" (class Sum1),
51//! - @c "diff" (class Diff1),
52//! - @c "product" (class Product1),
53//! - @c "ratio" (class Ratio1),
54//! - @c "composite" (class Composite1),
55//! @ingroup func1
56
57//! @defgroup func1modified Modified Functors
58//! Modified functors implement expressions that involve one functor and
59//! a single parameter.
60//! The following modified functor types are implemented:
61//! - @c "times-constant" (class TimesConstant1),
62//! - @c "plus-constant" (class PlusConstant1),
63//! - @c "periodic" (class Periodic1).
64//! @ingroup func1
65
66//! @defgroup func1helper Helper Functions
67//! Helper functions detect simplifications that can be made to compound expressions.
68//! @ingroup func1
69
70/**
71 * Base class for 'functor' classes that evaluate a function of one variable.
72 * @ingroup func1
73 */
74class Func1
75{
76public:
77 Func1() = default;
78
79 Func1(shared_ptr<Func1> f1, shared_ptr<Func1> f2) : m_f1(f1), m_f2(f2) {}
80
81 Func1(shared_ptr<Func1> f1, double A) : m_c(A), m_f1(f1) {}
82
83 virtual ~Func1() = default;
84
85 // Func1(const Func1& right) = delete; //! @todo Uncomment after %Cantera 3.1
86 Func1& operator=(const Func1& right) = delete;
87
88 //! Returns a string describing the type of the function
89 //! @since New in %Cantera 3.0.
90 virtual string type() const {
91 return "functor";
92 }
93
94 //! Returns a string with the class name of the functor
95 //! @since New in %Cantera 3.0.
96 string typeName() const;
97
98 //! Calls method eval to evaluate the function
99 double operator()(double t) const;
100
101 //! Evaluate the function.
102 virtual double eval(double t) const;
103
104 //! Creates a derivative to the current function
105 /*!
106 * @return shared pointer to new derivative function.
107 * @since Starting in %Cantera 3.1, the return type is a `shared_ptr`.
108 */
109 virtual shared_ptr<Func1> derivative() const;
110
111 //! Routine to determine if two functions are the same.
112 /*!
113 * Two functions are the same if they are the same function. For example, either
114 * ID and stored constant are the same, or the #m_f1 and #m_f2 are identical if they
115 * are non-null. Functors of the base class Func1 are by default not identical, as
116 * they are used by callback functions that cannot be differentiated. In instances
117 * where exact comparisons are not implemented, `false` is returned to prevent false
118 * positives that could lead to incorrect simplifications of compound functors.
119 */
120 virtual bool isIdentical(shared_ptr<Func1> other) const;
121
122 //! Routine to determine if two functions are the same.
123 /*!
124 * @deprecated Deprecated in %Cantera 3.1 and removed thereafter; replaced by
125 * isIdentical(shared_ptr<Func1>&).
126 * @todo Restore deleted copy constructor after removal.
127 */
128 virtual bool isIdentical(Func1& other) const;
129
130 /**
131 * @deprecated Deprecated in %Cantera 3.1 and removed thereafter; replaced by
132 * internal function.
133 */
134 virtual double isProportional(TimesConstant1& other);
135 /**
136 * @deprecated Deprecated in %Cantera 3.1 and removed thereafter; replaced by
137 * internal function.
138 */
139 virtual double isProportional(Func1& other);
140
141 //! Write LaTeX string describing function.
142 virtual string write(const string& arg) const;
143
144 //! Accessor function for the stored constant #m_c.
145 double c() const;
146
147 //! Accessor function for #m_f1.
148 //! @since New in %Cantera 3.0.
149 shared_ptr<Func1> func1_shared() const {
150 return m_f1;
151 }
152
153 //! Accessor function for #m_f2.
154 //! @since New in %Cantera 3.0.
155 shared_ptr<Func1> func2_shared() const {
156 return m_f2;
157 }
158
159 //! Return the order of the function, if it makes sense
160 virtual int order() const;
161
162protected:
163 double m_c = 0.0;
164 shared_ptr<Func1> m_f1;
165 shared_ptr<Func1> m_f2;
166};
167
168//! Sum of two functions.
169//! @ingroup func1helper
170shared_ptr<Func1> newSumFunction(shared_ptr<Func1> f1, shared_ptr<Func1> f2);
171
172//! Difference of two functions.
173//! @ingroup func1helper
174shared_ptr<Func1> newDiffFunction(shared_ptr<Func1> f1, shared_ptr<Func1> f2);
175
176//! Product of two functions.
177//! @ingroup func1helper
178shared_ptr<Func1> newProdFunction(shared_ptr<Func1> f1, shared_ptr<Func1> f2);
179
180//! Ratio of two functions.
181//! @ingroup func1helper
182shared_ptr<Func1> newRatioFunction(shared_ptr<Func1> f1, shared_ptr<Func1> f2);
183
184//! Composite of two functions.
185//! @ingroup func1helper
186shared_ptr<Func1> newCompositeFunction(shared_ptr<Func1> f1, shared_ptr<Func1> f2);
187
188//! Product of function and constant.
189//! @ingroup func1helper
190shared_ptr<Func1> newTimesConstFunction(shared_ptr<Func1> f1, double c);
191
192//! Sum of function and constant.
193//! @ingroup func1helper
194shared_ptr<Func1> newPlusConstFunction(shared_ptr<Func1> f1, double c);
195
196//! Implements the @c sin() function.
197/*!
198 * The functor class with type @c "sin" returns @f$ f(x) = \sin(\omega x) @f$,
199 * where the argument @f$ x @f$ is in radians.
200 * @param omega Frequency @f$ \omega @f$ (default=1.0)
201 * @ingroup func1basic
202 */
203class Sin1 : public Func1
204{
205public:
206 Sin1(double omega=1.0) {
207 m_c = omega;
208 }
209
210 //! Constructor uses single parameter (frequency)
211 Sin1(const vector<double>& params);
212
213 string write(const string& arg) const override;
214
215 string type() const override {
216 return "sin";
217 }
218
219 double eval(double t) const override{
220 return sin(m_c*t);
221 }
222
223 shared_ptr<Func1> derivative() const override;
224};
225
226
227//! Implements the @c cos() function.
228/*!
229 * The functor class with type @c "cos" returns @f$ f(x) = \cos(\omega x) @f$,
230 * where the argument @f$ x @f$ is in radians.
231 * @param omega Frequency @f$ \omega @f$ (default=1.0)
232 * @ingroup func1basic
233 */
234class Cos1 : public Func1
235{
236public:
237 Cos1(double omega=1.0) {
238 m_c = omega;
239 }
240
241 //! Constructor uses single parameter (frequency)
242 Cos1(const vector<double>& params);
243
244 string write(const string& arg) const override;
245
246 string type() const override {
247 return "cos";
248 }
249
250 double eval(double t) const override {
251 return cos(m_c * t);
252 }
253 shared_ptr<Func1> derivative() const override;
254};
255
256
257//! Implements the @c exp() (exponential) function.
258/*!
259 * The functor class with type @c "exp" returns @f$ f(x) = \exp(a x) @f$.
260 * @param a Factor (default=1.0)
261 * @ingroup func1basic
262 */
263class Exp1 : public Func1
264{
265public:
266 Exp1(double a=1.0) {
267 m_c = a;
268 }
269
270 //! Constructor uses single parameter (exponent factor)
271 Exp1(const vector<double>& params);
272
273 string write(const string& arg) const override;
274
275 string type() const override {
276 return "exp";
277 }
278
279 double eval(double t) const override {
280 return exp(m_c*t);
281 }
282
283 shared_ptr<Func1> derivative() const override;
284};
285
286
287//! Implements the @c log() (natural logarithm) function.
288/*!
289 * The functor class with type @c "log" returns @f$ f(x) = \ln(a x) @f$.
290 * @param a Factor (default=1.0)
291 * @ingroup func1basic
292 * @since New in %Cantera 3.0
293 */
294class Log1 : public Func1
295{
296public:
297 Log1(double a=1.0) {
298 m_c = a;
299 }
300
301 //! Constructor uses single parameter (factor)
302 Log1(const vector<double>& params);
303
304 string type() const override {
305 return "log";
306 }
307
308 double eval(double t) const override {
309 return log(m_c * t);
310 }
311
312 shared_ptr<Func1> derivative() const override;
313
314 string write(const string& arg) const override;
315};
316
317//! Implements the @c pow() (power) function.
318/*!
319 * The functor class with type @c "pow" returns @f$ f(x) = x^n @f$.
320 * @param n Exponent
321 * @ingroup func1basic
322 */
323class Pow1 : public Func1
324{
325public:
326 Pow1(double n) {
327 m_c = n;
328 }
329
330 //! Constructor uses single parameter (exponent)
331 Pow1(const vector<double>& params);
332
333 string write(const string& arg) const override;
334
335 string type() const override {
336 return "pow";
337 }
338
339 double eval(double t) const override {
340 return pow(t, m_c);
341 }
342 shared_ptr<Func1> derivative() const override;
343};
344
345//! Implements a tabulated function.
346/*!
347 * The functor class is based on tabulated arrays @c tvals and @c fvals, where
348 * @c tvals contain independent variables and @c fvals are corresponding function
349 * values. Depending on configuration, the function is either interpolated linearly
350 * between the tabulated points (type @c "tabulated-linear" ; default), or yields
351 * the last tabulated value until a new tabulated time value is reached (type
352 * @c "tabulated-previous" ).
353 * @ingroup func1advanced
354 */
355class Tabulated1 : public Func1
356{
357public:
358 //! Constructor.
359 /*!
360 * @param n Size of tabulated value arrays
361 * @param tvals Pointer to time value array
362 * @param fvals Pointer to function value array
363 * @param method Interpolation method ('linear' or 'previous')
364 */
365 Tabulated1(size_t n, const double* tvals, const double* fvals,
366 const string& method="linear");
367
368 //! Constructor uses @f$ 2 n @f$ parameters in the following order:
369 //! @f$ [t_0, t_1, \dots, t_{n-1}, f_0, f_1, \dots, f_{n-1}] @f$
370 Tabulated1(const vector<double>& params);
371
372 //! Set the interpolation method
373 //! @param method Evaluation method. If @c "linear" (default), a linear
374 //! interpolation between tabulated values is used; if @c "previous", the
375 //! last tabulated value is held until a new tabulated time value is reached.
376 //! @since New in %Cantera 3.0
377 void setMethod(const string& method);
378
379 bool isIdentical(shared_ptr<Func1> other) const override {
380 return false; // base class check is insufficient
381 }
382
383 string write(const string& arg) const override;
384
385 string type() const override {
386 if (m_isLinear) {
387 return "tabulated-linear";
388 }
389 return "tabulated-previous";
390 }
391
392 double eval(double t) const override;
393 shared_ptr<Func1> derivative() const override;
394private:
395 vector<double> m_tvec; //!< Vector of time values
396 vector<double> m_fvec; //!< Vector of function values
397 bool m_isLinear; //!< Boolean indicating interpolation method
398};
399
400
401//! Implements a constant.
402/*!
403 * The functor class with type @c "constant" returns @f$ f(x) = a @f$.
404 * @param a Constant
405 * @ingroup func1basic
406 */
407class Const1 : public Func1
408{
409public:
410 Const1(double a) {
411 m_c = a;
412 }
413
414 //! Constructor uses single parameter (constant)
415 Const1(const vector<double>& params);
416
417 string write(const string& arg) const override;
418
419 string type() const override {
420 return "constant";
421 }
422
423 double eval(double t) const override {
424 return m_c;
425 }
426 shared_ptr<Func1> derivative() const override {
427 return make_shared<Const1>(0.0);
428 }
429};
430
431
432/**
433 * Implements the sum of two functions.
434 * The functor class with type @c "sum" returns @f$ f(x) = f_1(x) + f_2(x) @f$.
435 * @param f1 Functor @f$ f_1(x) @f$
436 * @param f2 Functor @f$ f_2(x) @f$
437 * @ingroup func1compound
438 */
439class Sum1 : public Func1
440{
441public:
442 Sum1(shared_ptr<Func1> f1, shared_ptr<Func1> f2) : Func1(f1, f2) {}
443
444 string type() const override {
445 return "sum";
446 }
447
448 double eval(double t) const override {
449 return m_f1->eval(t) + m_f2->eval(t);
450 }
451
452 shared_ptr<Func1> derivative() const override {
453 return newSumFunction(m_f1->derivative(), m_f2->derivative());
454 }
455
456 int order() const override {
457 return 0;
458 }
459
460 string write(const string& arg) const override;
461};
462
463/**
464 * Implements the difference of two functions.
465 * The functor class with type @c "diff" returns @f$ f(x) = f_1(x) - f_2(x) @f$.
466 * @param f1 Functor @f$ f_1(x) @f$
467 * @param f2 Functor @f$ f_2(x) @f$
468 * @ingroup func1compound
469 */
470class Diff1 : public Func1
471{
472public:
473 Diff1(shared_ptr<Func1> f1, shared_ptr<Func1> f2) : Func1(f1, f2) {}
474
475 string type() const override {
476 return "diff";
477 }
478
479 double eval(double t) const override {
480 return m_f1->eval(t) - m_f2->eval(t);
481 }
482
483 shared_ptr<Func1> derivative() const override {
484 return newDiffFunction(m_f1->derivative(), m_f2->derivative());
485 }
486
487 int order() const override {
488 return 0;
489 }
490
491 string write(const string& arg) const override;
492};
493
494
495/**
496 * Implements the product of two functions.
497 * The functor class with type @c "product" returns @f$ f(x) = f_1(x) f_2(x) @f$.
498 * @param f1 Functor @f$ f_1(x) @f$
499 * @param f2 Functor @f$ f_2(x) @f$
500 * @ingroup func1compound
501 */
502class Product1 : public Func1
503{
504public:
505 Product1(shared_ptr<Func1> f1, shared_ptr<Func1> f2) : Func1(f1, f2) {}
506
507 string type() const override {
508 return "product";
509 }
510
511 string write(const string& arg) const override;
512
513 double eval(double t) const override {
514 return m_f1->eval(t) * m_f2->eval(t);
515 }
516
517 shared_ptr<Func1> derivative() const override;
518
519 int order() const override {
520 return 1;
521 }
522};
523
524/**
525 * Implements the product of a function and a constant.
526 * The functor class with type @c "times-constant" returns @f$ f(x) = a f_1(x) @f$.
527 * @param f1 Functor @f$ f_1(x) @f$
528 * @param a Constant @f$ a @f$
529 * @ingroup func1modified
530 */
531class TimesConstant1 : public Func1
532{
533public:
534 TimesConstant1(shared_ptr<Func1> f1, double a) : Func1(f1, a) {}
535
536 string type() const override {
537 return "times-constant";
538 }
539
540 double isProportional(TimesConstant1& other) override {
541 if (func1_shared()->isIdentical(*other.func1_shared())) {
542 return (other.c()/c());
543 } else {
544 return 0.0;
545 }
546 }
547
548 double isProportional(Func1& other) override {
549 if (func1_shared()->isIdentical(other)) {
550 return 1.0/c();
551 } else {
552 return 0.0;
553 }
554 }
555
556 double eval(double t) const override {
557 return m_f1->eval(t) * m_c;
558 }
559
560 shared_ptr<Func1> derivative() const override {
561 return newTimesConstFunction(m_f1->derivative(), m_c);
562 }
563
564 string write(const string& arg) const override;
565
566 int order() const override {
567 return 0;
568 }
569};
570
571/**
572 * Implements the sum of a function and a constant.
573 * The functor class with type @c "plus-constant" returns @f$ f(x) = f_1(x) + a @f$.
574 * @param f1 Functor @f$ f_1(x) @f$
575 * @param a Constant @f$ a @f$
576 * @ingroup func1modified
577 */
578class PlusConstant1 : public Func1
579{
580public:
581 PlusConstant1(shared_ptr<Func1> f1, double a) : Func1(f1, a) {}
582
583 string type() const override {
584 return "plus-constant";
585 }
586
587 double eval(double t) const override {
588 return m_f1->eval(t) + m_c;
589 }
590
591 shared_ptr<Func1> derivative() const override {
592 return m_f1->derivative();
593 }
594
595 string write(const string& arg) const override;
596
597 int order() const override {
598 return 0;
599 }
600};
601
602
603/**
604 * Implements the ratio of two functions.
605 * The functor class with type @c "ratio" returns @f$ f(x) = f_1(x) / f_2(x) @f$.
606 * @param f1 Functor @f$ f_1(x) @f$
607 * @param f2 Functor @f$ f_2(x) @f$
608 * @ingroup func1compound
609 */
610class Ratio1 : public Func1
611{
612public:
613 Ratio1(shared_ptr<Func1> f1, shared_ptr<Func1> f2) : Func1(f1, f2) {}
614
615 string type() const override {
616 return "ratio";
617 }
618
619 double eval(double t) const override {
620 return m_f1->eval(t) / m_f2->eval(t);
621 }
622
623 shared_ptr<Func1> derivative() const override;
624
625 string write(const string& arg) const override;
626
627 int order() const override {
628 return 1;
629 }
630};
631
632/**
633 * Implements a composite function.
634 * The functor class with type @c "composite" returns @f$ f(x) = f_1\left(f_2(x)\right) @f$.
635 * @param f1 Functor @f$ f_1(x) @f$
636 * @param f2 Functor @f$ f_2(x) @f$
637 * @ingroup func1compound
638 */
639class Composite1 : public Func1
640{
641public:
642 Composite1(shared_ptr<Func1> f1, shared_ptr<Func1> f2) : Func1(f1, f2) {}
643
644 string type() const override {
645 return "composite";
646 }
647
648 double eval(double t) const override {
649 return m_f1->eval(m_f2->eval(t));
650 }
651
652 shared_ptr<Func1> derivative() const override;
653
654 string write(const string& arg) const override;
655
656 int order() const override {
657 return 2;
658 }
659};
660
661// The functors below are the old-style ones. They still work,
662// but can't do derivatives.
663
664/**
665 * Implements a Gaussian function.
666 * The functor class with type @c "Gaussian" returns
667 * @f[
668 * f(t) = A e^{-[(t - t_0)/\tau]^2}
669 * @f]
670 * where @f$ \tau = \mathrm{fwhm} / (2 \sqrt{\ln 2}) @f$.
671 * @param A peak value
672 * @param t0 offset
673 * @param fwhm full width at half max
674 * @ingroup func1advanced
675 * @since New in %Cantera 3.0.
676 */
677class Gaussian1 : public Func1
678{
679public:
680 Gaussian1(double A, double t0, double fwhm) {
681 m_A = A;
682 m_t0 = t0;
683 m_tau = fwhm/(2.0*std::sqrt(std::log(2.0)));
684 }
685
686 //! Constructor uses 3 parameters in the following order:
687 //! @f$ [A, t_0, \mathrm{fwhm}] @f$
688 Gaussian1(const vector<double>& params);
689
690 string type() const override {
691 return "Gaussian";
692 }
693
694 bool isIdentical(shared_ptr<Func1> other) const override {
695 return false; // base class check is insufficient
696 }
697
698 double eval(double t) const override {
699 double x = (t - m_t0)/m_tau;
700 return m_A * std::exp(-x*x);
701 }
702
703protected:
704 double m_A, m_t0, m_tau;
705};
706
707
708/**
709 * Implements a polynomial of degree @e n.
710 * The functor class with type @c "polynomial3" returns
711 * @f[
712 * f(x) = a_n x^n + \dots + a_1 x + a_0
713 * @f]
714 * with coefficients provided in the order @f$ [a_n, \dots, a_1, a_0] @f$ (consistent
715 * with MATLAB and NumPy conventions). Note that %Cantera 3.1 reversed the coefficient
716 * order with respect to its earlier definition. A deprecation cycle is skipped as the
717 * functor class is not expected to be widely used; the transitional name Poly13 ensures
718 * that changed behavior does not go unnoticed. The class name will revert to @b Poly1
719 * after %Cantera 3.1.
720 * @since Changed in %Cantera 3.1.
721 * @todo Rename to Poly1 after %Cantera 3.1
722 * @ingroup func1advanced
723 */
724class Poly13 : public Func1
725{
726public:
727 Poly13(size_t n, const double* c) {
728 m_cpoly.resize(n+1);
729 std::copy(c, c+m_cpoly.size(), m_cpoly.begin());
730 }
731
732 //! Constructor uses @f$ n + 1 @f$ parameters in the following order:
733 //! @f$ [a_n, \dots, a_1, a_0] @f$
734 Poly13(const vector<double>& params);
735
736 string type() const override {
737 return "polynomial3";
738 }
739
740 bool isIdentical(shared_ptr<Func1> other) const override {
741 return false; // base class check is insufficient
742 }
743
744 double eval(double t) const override {
745 double r = m_cpoly[0];
746 for (size_t n = 1; n < m_cpoly.size(); n++) {
747 r *= t;
748 r += m_cpoly[n];
749 }
750 return r;
751 }
752
753 string write(const string& arg) const override;
754
755protected:
756 vector<double> m_cpoly;
757};
758
759
760/**
761 * Implements a Fourier cosine/sine series.
762 * The functor class with type @c "Fourier" returns
763 * @f[
764 * f(t) = \frac{a_0}{2} + \sum_{n=1}^N a_n \cos (n \omega t) + b_n \sin (n \omega t)
765 * @f]
766 * @ingroup func1advanced
767 */
768class Fourier1 : public Func1
769{
770public:
771 Fourier1(size_t n, double omega, double a0, const double* a, const double* b) {
772 m_omega = omega;
773 m_a0_2 = 0.5*a0;
774 m_ccos.resize(n);
775 m_csin.resize(n);
776 std::copy(a, a+n, m_ccos.begin());
777 std::copy(b, b+n, m_csin.begin());
778 }
779
780 //! Constructor uses @f$ 2 n + 2 @f$ parameters in the following order:
781 //! @f$ [a_0, a_1, \dots, a_n, \omega, b_1, \dots, b_n] @f$
782 Fourier1(const vector<double>& params);
783
784 string type() const override {
785 return "Fourier";
786 }
787
788 bool isIdentical(shared_ptr<Func1> other) const override {
789 return false; // base class check is insufficient
790 }
791
792 double eval(double t) const override {
793 size_t n, nn;
794 double sum = m_a0_2;
795 for (n = 0; n < m_ccos.size(); n++) {
796 nn = n + 1;
797 sum += m_ccos[n]*std::cos(m_omega*nn*t)
798 + m_csin[n]*std::sin(m_omega*nn*t);
799 }
800 return sum;
801 }
802
803protected:
804 double m_omega, m_a0_2;
805 vector<double> m_ccos, m_csin;
806};
807
808
809/**
810 * Implements a sum of Arrhenius terms.
811 * The functor class with type @c "Arrhenius" returns
812 * @f[
813 * f(T) = \sum_{n=1}^N A_n T^b_n \exp(-E_n/T)
814 * @f]
815 * @ingroup func1advanced
816 */
817class Arrhenius1 : public Func1
818{
819public:
820 Arrhenius1(size_t n, const double* c) {
821 m_A.resize(n);
822 m_b.resize(n);
823 m_E.resize(n);
824 for (size_t i = 0; i < n; i++) {
825 size_t loc = 3*i;
826 m_A[i] = c[loc];
827 m_b[i] = c[loc+1];
828 m_E[i] = c[loc+2];
829 }
830 }
831
832 //! Constructor uses @f$ 3 n @f$ parameters in the following order:
833 //! @f$ [A_1, b_1, E_1, A_2, b_2, E_2, \dots, A_n, b_n, E_n] @f$
834 Arrhenius1(const vector<double>& params);
835
836 string type() const override {
837 return "Arrhenius";
838 }
839
840 bool isIdentical(shared_ptr<Func1> other) const override {
841 return false; // base class check is insufficient
842 }
843
844 double eval(double t) const override {
845 double sum = 0.0;
846 for (size_t n = 0; n < m_A.size(); n++) {
847 sum += m_A[n]*std::pow(t,m_b[n])*std::exp(-m_E[n]/t);
848 }
849 return sum;
850 }
851
852protected:
853 vector<double> m_A, m_b, m_E;
854};
855
856/**
857 * Implements a periodic function.
858 * Takes any function and makes it periodic with period @f$ T @f$.
859 * @param f Functor to be made periodic
860 * @param T Period
861 * @ingroup func1modified
862 */
863class Periodic1 : public Func1
864{
865public:
866 Periodic1(shared_ptr<Func1> f, double A) : Func1(f, A) {}
867
868 string type() const override {
869 return "periodic";
870 }
871
872 double eval(double t) const override {
873 int np = int(t/m_c);
874 double time = t - np*m_c;
875 return m_f1->eval(time);
876 }
877};
878
879}
880
881#endif
Implements a sum of Arrhenius terms.
Definition Func1.h:818
double eval(double t) const override
Evaluate the function.
Definition Func1.h:844
string type() const override
Returns a string describing the type of the function.
Definition Func1.h:836
bool isIdentical(shared_ptr< Func1 > other) const override
Routine to determine if two functions are the same.
Definition Func1.h:840
Implements a composite function.
Definition Func1.h:640
double eval(double t) const override
Evaluate the function.
Definition Func1.h:648
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition Func1.cpp:513
string type() const override
Returns a string describing the type of the function.
Definition Func1.h:644
int order() const override
Return the order of the function, if it makes sense.
Definition Func1.h:656
string write(const string &arg) const override
Write LaTeX string describing function.
Definition Func1.cpp:507
Implements a constant.
Definition Func1.h:408
double eval(double t) const override
Evaluate the function.
Definition Func1.h:423
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition Func1.h:426
string type() const override
Returns a string describing the type of the function.
Definition Func1.h:419
string write(const string &arg) const override
Write LaTeX string describing function.
Definition Func1.cpp:448
Implements the cos() function.
Definition Func1.h:235
double eval(double t) const override
Evaluate the function.
Definition Func1.h:250
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition Func1.cpp:119
string type() const override
Returns a string describing the type of the function.
Definition Func1.h:246
string write(const string &arg) const override
Write LaTeX string describing function.
Definition Func1.cpp:125
Implements the difference of two functions.
Definition Func1.h:471
double eval(double t) const override
Evaluate the function.
Definition Func1.h:479
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition Func1.h:483
string type() const override
Returns a string describing the type of the function.
Definition Func1.h:475
int order() const override
Return the order of the function, if it makes sense.
Definition Func1.h:487
string write(const string &arg) const override
Write LaTeX string describing function.
Definition Func1.cpp:496
Implements the exp() (exponential) function.
Definition Func1.h:264
double eval(double t) const override
Evaluate the function.
Definition Func1.h:279
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition Func1.cpp:145
string type() const override
Returns a string describing the type of the function.
Definition Func1.h:275
string write(const string &arg) const override
Write LaTeX string describing function.
Definition Func1.cpp:154
Implements a Fourier cosine/sine series.
Definition Func1.h:769
double eval(double t) const override
Evaluate the function.
Definition Func1.h:792
string type() const override
Returns a string describing the type of the function.
Definition Func1.h:784
bool isIdentical(shared_ptr< Func1 > other) const override
Routine to determine if two functions are the same.
Definition Func1.h:788
Base class for 'functor' classes that evaluate a function of one variable.
Definition Func1.h:75
string typeName() const
Returns a string with the class name of the functor.
Definition Func1.cpp:16
virtual shared_ptr< Func1 > derivative() const
Creates a derivative to the current function.
Definition Func1.cpp:33
virtual string type() const
Returns a string describing the type of the function.
Definition Func1.h:90
virtual double eval(double t) const
Evaluate the function.
Definition Func1.cpp:28
shared_ptr< Func1 > func1_shared() const
Accessor function for m_f1.
Definition Func1.h:149
virtual double isProportional(TimesConstant1 &other)
Definition Func1.cpp:547
virtual string write(const string &arg) const
Write LaTeX string describing function.
Definition Func1.cpp:423
double operator()(double t) const
Calls method eval to evaluate the function.
Definition Func1.cpp:22
virtual bool isIdentical(shared_ptr< Func1 > other) const
Routine to determine if two functions are the same.
Definition Func1.cpp:39
virtual int order() const
Return the order of the function, if it makes sense.
Definition Func1.cpp:77
shared_ptr< Func1 > func2_shared() const
Accessor function for m_f2.
Definition Func1.h:155
double c() const
Accessor function for the stored constant m_c.
Definition Func1.cpp:72
Implements a Gaussian function.
Definition Func1.h:678
double eval(double t) const override
Evaluate the function.
Definition Func1.h:698
string type() const override
Returns a string describing the type of the function.
Definition Func1.h:690
bool isIdentical(shared_ptr< Func1 > other) const override
Routine to determine if two functions are the same.
Definition Func1.h:694
Implements the log() (natural logarithm) function.
Definition Func1.h:295
double eval(double t) const override
Evaluate the function.
Definition Func1.h:308
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition Func1.cpp:172
string type() const override
Returns a string describing the type of the function.
Definition Func1.h:304
string write(const string &arg) const override
Write LaTeX string describing function.
Definition Func1.cpp:181
Implements a periodic function.
Definition Func1.h:864
double eval(double t) const override
Evaluate the function.
Definition Func1.h:872
string type() const override
Returns a string describing the type of the function.
Definition Func1.h:868
Implements the sum of a function and a constant.
Definition Func1.h:579
double eval(double t) const override
Evaluate the function.
Definition Func1.h:587
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition Func1.h:591
string type() const override
Returns a string describing the type of the function.
Definition Func1.h:583
int order() const override
Return the order of the function, if it makes sense.
Definition Func1.h:597
string write(const string &arg) const override
Write LaTeX string describing function.
Definition Func1.cpp:539
Implements a polynomial of degree n.
Definition Func1.h:725
double eval(double t) const override
Evaluate the function.
Definition Func1.h:744
string type() const override
Returns a string describing the type of the function.
Definition Func1.h:736
bool isIdentical(shared_ptr< Func1 > other) const override
Routine to determine if two functions are the same.
Definition Func1.h:740
string write(const string &arg) const override
Write LaTeX string describing function.
Definition Func1.cpp:234
Implements the pow() (power) function.
Definition Func1.h:324
double eval(double t) const override
Evaluate the function.
Definition Func1.h:339
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition Func1.cpp:200
string type() const override
Returns a string describing the type of the function.
Definition Func1.h:335
string write(const string &arg) const override
Write LaTeX string describing function.
Definition Func1.cpp:428
Implements the product of two functions.
Definition Func1.h:503
double eval(double t) const override
Evaluate the function.
Definition Func1.h:513
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition Func1.cpp:479
string type() const override
Returns a string describing the type of the function.
Definition Func1.h:507
int order() const override
Return the order of the function, if it makes sense.
Definition Func1.h:519
string write(const string &arg) const override
Write LaTeX string describing function.
Definition Func1.cpp:466
Implements the ratio of two functions.
Definition Func1.h:611
double eval(double t) const override
Evaluate the function.
Definition Func1.h:619
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition Func1.cpp:458
string type() const override
Returns a string describing the type of the function.
Definition Func1.h:615
int order() const override
Return the order of the function, if it makes sense.
Definition Func1.h:627
string write(const string &arg) const override
Write LaTeX string describing function.
Definition Func1.cpp:453
Implements the sin() function.
Definition Func1.h:204
double eval(double t) const override
Evaluate the function.
Definition Func1.h:219
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition Func1.cpp:102
string type() const override
Returns a string describing the type of the function.
Definition Func1.h:215
string write(const string &arg) const override
Write LaTeX string describing function.
Definition Func1.cpp:93
Implements the sum of two functions.
Definition Func1.h:440
double eval(double t) const override
Evaluate the function.
Definition Func1.h:448
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition Func1.h:452
string type() const override
Returns a string describing the type of the function.
Definition Func1.h:444
int order() const override
Return the order of the function, if it makes sense.
Definition Func1.h:456
string write(const string &arg) const override
Write LaTeX string describing function.
Definition Func1.cpp:485
Implements a tabulated function.
Definition Func1.h:356
double eval(double t) const override
Evaluate the function.
Definition Func1.cpp:372
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition Func1.cpp:395
string type() const override
Returns a string describing the type of the function.
Definition Func1.h:385
bool isIdentical(shared_ptr< Func1 > other) const override
Routine to determine if two functions are the same.
Definition Func1.h:379
string write(const string &arg) const override
Write LaTeX string describing function.
Definition Func1.cpp:443
void setMethod(const string &method)
Set the interpolation method.
Definition Func1.cpp:360
vector< double > m_tvec
Vector of time values.
Definition Func1.h:395
bool m_isLinear
Boolean indicating interpolation method.
Definition Func1.h:397
vector< double > m_fvec
Vector of function values.
Definition Func1.h:396
Implements the product of a function and a constant.
Definition Func1.h:532
double eval(double t) const override
Evaluate the function.
Definition Func1.h:556
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition Func1.h:560
string type() const override
Returns a string describing the type of the function.
Definition Func1.h:536
int order() const override
Return the order of the function, if it makes sense.
Definition Func1.h:566
double isProportional(Func1 &other) override
Definition Func1.h:548
string write(const string &arg) const override
Write LaTeX string describing function.
Definition Func1.cpp:520
double isProportional(TimesConstant1 &other) override
Definition Func1.h:540
This file contains definitions of constants, types and terms that are used in internal routines and a...
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
shared_ptr< Func1 > newCompositeFunction(shared_ptr< Func1 > f1, shared_ptr< Func1 > f2)
Composite of two functions.
Definition Func1.cpp:763
shared_ptr< Func1 > newProdFunction(shared_ptr< Func1 > f1, shared_ptr< Func1 > f2)
Product of two functions.
Definition Func1.cpp:684
shared_ptr< Func1 > newDiffFunction(shared_ptr< Func1 > f1, shared_ptr< Func1 > f2)
Difference of two functions.
Definition Func1.cpp:660
shared_ptr< Func1 > newTimesConstFunction(shared_ptr< Func1 > f, double c)
Product of function and constant.
Definition Func1.cpp:783
shared_ptr< Func1 > newSumFunction(shared_ptr< Func1 > f1, shared_ptr< Func1 > f2)
Sum of two functions.
Definition Func1.cpp:633
shared_ptr< Func1 > newRatioFunction(shared_ptr< Func1 > f1, shared_ptr< Func1 > f2)
Ratio of two functions.
Definition Func1.cpp:737
shared_ptr< Func1 > newPlusConstFunction(shared_ptr< Func1 > f, double c)
Sum of function and constant.
Definition Func1.cpp:797
Namespace for the Cantera kernel.
Definition AnyMap.cpp:595