Cantera  3.1.0a1
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 
11 #include "cantera/base/ct_defs.h"
13 
14 #include <iostream>
15 
16 namespace Cantera
17 {
18 
19 class 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 func1simple Simple Functors
28 //! Simple 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 "polynomial" (class Poly1),
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  */
74 class Func1
75 {
76 public:
77  Func1() = default;
78 
79  Func1(shared_ptr<Func1> f1, shared_ptr<Func1> f2)
80  : m_f1_shared(f1), m_f2_shared(f2)
81  {
82  m_f1 = f1.get();
83  m_f2 = f2.get();
84  }
85 
86  Func1(shared_ptr<Func1> f1, double A) : m_c(A), m_f1_shared(f1) {
87  m_f1 = f1.get();
88  }
89 
90  virtual ~Func1() = default;
91 
92  Func1(const Func1& right) = delete;
93  Func1& operator=(const Func1& right) = delete;
94 
95  //! Returns a string describing the type of the function
96  //! @since New in %Cantera 3.0.
97  virtual string type() const {
98  return "functor";
99  }
100 
101  //! Returns a string with the class name of the functor
102  //! @since New in %Cantera 3.0.
103  string typeName() const;
104 
105  //! Calls method eval to evaluate the function
106  double operator()(double t) const;
107 
108  //! Evaluate the function.
109  virtual double eval(double t) const;
110 
111  //! Creates a derivative to the current function
112  /*!
113  * @return shared pointer to new derivative function.
114  * @since Starting in Cantera 3.1, the return type is a `shared_ptr`.
115  */
116  virtual shared_ptr<Func1> derivative() const;
117 
118  //! Creates a derivative to the current function
119  /*!
120  * This will create a new derivative function
121  * @return shared pointer to new derivative function.
122  * @since New in %Cantera 3.0.
123  * @deprecated Transitional name for derivative()
124  */
125  shared_ptr<Func1> derivative3() const { return derivative(); }
126 
127  //! Routine to determine if two functions are the same.
128  /*!
129  * Two functions are the same if they are the same function. This means
130  * that the ID and stored constant is the same. This means that the m_f1
131  * and m_f2 are identical if they are non-null.
132  */
133  bool isIdentical(Func1& other) const;
134 
135  virtual double isProportional(TimesConstant1& other);
136  virtual double isProportional(Func1& other);
137 
138  //! Write LaTeX string describing function.
139  virtual string write(const string& arg) const;
140 
141  //! Accessor function for the stored constant
142  double c() const;
143 
144  //! Accessor function for m_f1_shared
145  //! @since New in %Cantera 3.0.
146  shared_ptr<Func1> func1_shared() const {
147  return m_f1_shared;
148  }
149 
150  //! Accessor function for m_f2_shared
151  //! @since New in %Cantera 3.0.
152  shared_ptr<Func1> func2_shared() const {
153  return m_f2_shared;
154  }
155 
156  //! Return the order of the function, if it makes sense
157  virtual int order() const;
158 
159 protected:
160  double m_c = 0.0;
161  Func1* m_f1 = nullptr;
162  Func1* m_f2 = nullptr;
163 
164  shared_ptr<Func1> m_f1_shared;
165  shared_ptr<Func1> m_f2_shared;
166 };
167 
168 //! Sum of two functions.
169 //! @ingroup func1helper
170 shared_ptr<Func1> newSumFunction(shared_ptr<Func1> f1, shared_ptr<Func1> f2);
171 
172 //! Difference of two functions.
173 //! @ingroup func1helper
174 shared_ptr<Func1> newDiffFunction(shared_ptr<Func1> f1, shared_ptr<Func1> f2);
175 
176 //! Product of two functions.
177 //! @ingroup func1helper
178 shared_ptr<Func1> newProdFunction(shared_ptr<Func1> f1, shared_ptr<Func1> f2);
179 
180 //! Ratio of two functions.
181 //! @ingroup func1helper
182 shared_ptr<Func1> newRatioFunction(shared_ptr<Func1> f1, shared_ptr<Func1> f2);
183 
184 //! Composite of two functions.
185 //! @ingroup func1helper
186 shared_ptr<Func1> newCompositeFunction(shared_ptr<Func1> f1, shared_ptr<Func1> f2);
187 
188 //! Product of function and constant.
189 //! @ingroup func1helper
190 shared_ptr<Func1> newTimesConstFunction(shared_ptr<Func1> f1, double c);
191 
192 //! Sum of function and constant.
193 //! @ingroup func1helper
194 shared_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) = \cos(\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 func1simple
202  */
203 class Sin1 : public Func1
204 {
205 public:
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 func1simple
233  */
234 class Cos1 : public Func1
235 {
236 public:
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 func1simple
262  */
263 class Exp1 : public Func1
264 {
265 public:
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 func1simple
292  * @since New in %Cantera 3.0
293  */
294 class Log1 : public Func1
295 {
296 public:
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 func1simple
322  */
323 class Pow1 : public Func1
324 {
325 public:
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  */
355 class Tabulated1 : public Func1
356 {
357 public:
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  string write(const string& arg) const override;
380 
381  string type() const override {
382  if (m_isLinear) {
383  return "tabulated-linear";
384  }
385  return "tabulated-previous";
386  }
387 
388  double eval(double t) const override;
389  shared_ptr<Func1> derivative() const override;
390 private:
391  vector<double> m_tvec; //!< Vector of time values
392  vector<double> m_fvec; //!< Vector of function values
393  bool m_isLinear; //!< Boolean indicating interpolation method
394 };
395 
396 
397 //! Implements a constant.
398 /*!
399  * The functor class with type @c "constant" returns @f$ f(x) = a @f$.
400  * @param a Constant
401  * @ingroup func1simple
402  */
403 class Const1 : public Func1
404 {
405 public:
406  Const1(double a) {
407  m_c = a;
408  }
409 
410  //! Constructor uses single parameter (constant)
411  Const1(const vector<double>& params);
412 
413  string write(const string& arg) const override;
414 
415  string type() const override {
416  return "constant";
417  }
418 
419  double eval(double t) const override {
420  return m_c;
421  }
422  shared_ptr<Func1> derivative() const override {
423  return make_shared<Const1>(0.0);
424  }
425 };
426 
427 
428 /**
429  * Implements the sum of two functions.
430  * The functor class with type @c "sum" returns @f$ f(x) = f_1(x) + f_2(x) @f$.
431  * @param f1 Functor @f$ f_1(x) @f$
432  * @param f2 Functor @f$ f_2(x) @f$
433  * @ingroup func1compound
434  */
435 class Sum1 : public Func1
436 {
437 public:
438  Sum1(Func1& f1, Func1& f2) {
439  m_f1 = &f1;
440  m_f2 = &f2;
441  }
442 
443  Sum1(shared_ptr<Func1> f1, shared_ptr<Func1> f2) : Func1(f1, f2) {}
444 
445  ~Sum1() override {
446  if (!m_f1_shared) {
447  delete m_f1;
448  }
449  if (!m_f2_shared) {
450  delete m_f2;
451  }
452  }
453 
454  string type() const override {
455  return "sum";
456  }
457 
458  double eval(double t) const override {
459  return m_f1->eval(t) + m_f2->eval(t);
460  }
461 
462  shared_ptr<Func1> derivative() const override {
463  return newSumFunction(m_f1_shared->derivative(), m_f2_shared->derivative());
464  }
465 
466  int order() const override {
467  return 0;
468  }
469 
470  string write(const string& arg) const override;
471 };
472 
473 /**
474  * Implements the difference of two functions.
475  * The functor class with type @c "diff" returns @f$ f(x) = f_1(x) - f_2(x) @f$.
476  * @param f1 Functor @f$ f_1(x) @f$
477  * @param f2 Functor @f$ f_2(x) @f$
478  * @ingroup func1compound
479  */
480 class Diff1 : public Func1
481 {
482 public:
483  Diff1(Func1& f1, Func1& f2) {
484  m_f1 = &f1;
485  m_f2 = &f2;
486  }
487 
488  Diff1(shared_ptr<Func1> f1, shared_ptr<Func1> f2) : Func1(f1, f2) {}
489 
490  ~Diff1() override {
491  if (!m_f1_shared) {
492  delete m_f1;
493  }
494  if (!m_f2_shared) {
495  delete m_f2;
496  }
497  }
498 
499  string type() const override {
500  return "diff";
501  }
502 
503  double eval(double t) const override {
504  return m_f1->eval(t) - m_f2->eval(t);
505  }
506 
507  shared_ptr<Func1> derivative() const override {
508  return newDiffFunction(m_f1_shared->derivative(), m_f2_shared->derivative());
509  }
510 
511  int order() const override {
512  return 0;
513  }
514 
515  string write(const string& arg) const override;
516 };
517 
518 
519 /**
520  * Implements the product of two functions.
521  * The functor class with type @c "product" returns @f$ f(x) = f_1(x) f_2(x) @f$.
522  * @param f1 Functor @f$ f_1(x) @f$
523  * @param f2 Functor @f$ f_2(x) @f$
524  * @ingroup func1compound
525  */
526 class Product1 : public Func1
527 {
528 public:
529  Product1(Func1& f1, Func1& f2) {
530  m_f1 = &f1;
531  m_f2 = &f2;
532  }
533 
534  Product1(shared_ptr<Func1> f1, shared_ptr<Func1> f2) : Func1(f1, f2) {}
535 
536  ~Product1() override {
537  if (!m_f1_shared) {
538  delete m_f1;
539  }
540  if (!m_f2_shared) {
541  delete m_f2;
542  }
543  }
544 
545  string type() const override {
546  return "product";
547  }
548 
549  string write(const string& arg) const override;
550 
551  double eval(double t) const override {
552  return m_f1->eval(t) * m_f2->eval(t);
553  }
554 
555  shared_ptr<Func1> derivative() const override;
556 
557  int order() const override {
558  return 1;
559  }
560 };
561 
562 /**
563  * Implements the product of a function and a constant.
564  * The functor class with type @c "times-constant" returns @f$ f(x) = a f_1(x) @f$.
565  * @param f1 Functor @f$ f_1(x) @f$
566  * @param a Constant @f$ a @f$
567  * @ingroup func1modified
568  */
569 class TimesConstant1 : public Func1
570 {
571 public:
572  TimesConstant1(Func1& f1, double a) {
573  m_f1 = &f1;
574  m_c = a;
575  }
576 
577  TimesConstant1(shared_ptr<Func1> f1, double a) : Func1(f1, a) {}
578 
579  ~TimesConstant1() override {
580  if (!m_f1_shared) {
581  delete m_f1;
582  }
583  }
584 
585  string type() const override {
586  return "times-constant";
587  }
588 
589  double isProportional(TimesConstant1& other) override {
590  if (func1_shared()->isIdentical(*other.func1_shared())) {
591  return (other.c()/c());
592  } else {
593  return 0.0;
594  }
595  }
596 
597  double isProportional(Func1& other) override {
598  if (func1_shared()->isIdentical(other)) {
599  return 1.0/c();
600  } else {
601  return 0.0;
602  }
603  }
604 
605  double eval(double t) const override {
606  return m_f1->eval(t) * m_c;
607  }
608 
609  shared_ptr<Func1> derivative() const override {
610  return newTimesConstFunction(m_f1_shared->derivative(), m_c);
611  }
612 
613  string write(const string& arg) const override;
614 
615  int order() const override {
616  return 0;
617  }
618 };
619 
620 /**
621  * Implements the sum of a function and a constant.
622  * The functor class with type @c "plus-constant" returns @f$ f(x) = f_1(x) + a @f$.
623  * @param f1 Functor @f$ f_1(x) @f$
624  * @param a Constant @f$ a @f$
625  * @ingroup func1modified
626  */
627 class PlusConstant1 : public Func1
628 {
629 public:
630  PlusConstant1(Func1& f1, double a) {
631  m_f1 = &f1;
632  m_c = a;
633  }
634 
635  PlusConstant1(shared_ptr<Func1> f1, double a) : Func1(f1, a) {}
636 
637  ~PlusConstant1() override {
638  if (!m_f1_shared) {
639  delete m_f1;
640  }
641  }
642 
643  string type() const override {
644  return "plus-constant";
645  }
646 
647  double eval(double t) const override {
648  return m_f1->eval(t) + m_c;
649  }
650 
651  shared_ptr<Func1> derivative() const override {
652  return m_f1_shared->derivative();
653  }
654 
655  string write(const string& arg) const override;
656 
657  int order() const override {
658  return 0;
659  }
660 };
661 
662 
663 /**
664  * Implements the ratio of two functions.
665  * The functor class with type @c "ratio" returns @f$ f(x) = f_1(x) / f_2(x) @f$.
666  * @param f1 Functor @f$ f_1(x) @f$
667  * @param f2 Functor @f$ f_2(x) @f$
668  * @ingroup func1compound
669  */
670 class Ratio1 : public Func1
671 {
672 public:
673  Ratio1(Func1& f1, Func1& f2) {
674  m_f1 = &f1;
675  m_f2 = &f2;
676  }
677 
678  Ratio1(shared_ptr<Func1> f1, shared_ptr<Func1> f2) : Func1(f1, f2) {}
679 
680  ~Ratio1() override {
681  if (!m_f1_shared) {
682  delete m_f1;
683  }
684  if (!m_f2_shared) {
685  delete m_f2;
686  }
687  }
688 
689  string type() const override {
690  return "ratio";
691  }
692 
693  double eval(double t) const override {
694  return m_f1->eval(t) / m_f2->eval(t);
695  }
696 
697  shared_ptr<Func1> derivative() const override;
698 
699  string write(const string& arg) const override;
700 
701  int order() const override {
702  return 1;
703  }
704 };
705 
706 /**
707  * Implements a composite function.
708  * The functor class with type @c "composite" returns @f$ f(x) = f_1\left(f_2(x)\right) @f$.
709  * @param f1 Functor @f$ f_1(x) @f$
710  * @param f2 Functor @f$ f_2(x) @f$
711  * @ingroup func1compound
712  */
713 class Composite1 : public Func1
714 {
715 public:
716  Composite1(Func1& f1, Func1& f2) {
717  m_f1 = &f1;
718  m_f2 = &f2;
719  }
720 
721  Composite1(shared_ptr<Func1> f1, shared_ptr<Func1> f2) : Func1(f1, f2) {}
722 
723  ~Composite1() override {
724  if (!m_f1_shared) {
725  delete m_f1;
726  }
727  if (!m_f2_shared) {
728  delete m_f2;
729  }
730  }
731 
732  string type() const override {
733  return "composite";
734  }
735 
736  double eval(double t) const override {
737  return m_f1->eval(m_f2->eval(t));
738  }
739 
740  shared_ptr<Func1> derivative() const override;
741 
742  string write(const string& arg) const override;
743 
744  int order() const override {
745  return 2;
746  }
747 };
748 
749 // The functors below are the old-style ones. They still work,
750 // but can't do derivatives.
751 
752 /**
753  * Implements a Gaussian function.
754  * The functor class with type @c "Gaussian" returns
755  * @f[
756  * f(t) = A e^{-[(t - t_0)/\tau]^2}
757  * @f]
758  * where @f$ \tau = \mathrm{fwhm} / (2 \sqrt{\ln 2}) @f$.
759  * @param A peak value
760  * @param t0 offset
761  * @param fwhm full width at half max
762  * @ingroup func1advanced
763  * @since New in %Cantera 3.0.
764  */
765 class Gaussian1 : public Func1
766 {
767 public:
768  Gaussian1(double A, double t0, double fwhm) {
769  m_A = A;
770  m_t0 = t0;
771  m_tau = fwhm/(2.0*std::sqrt(std::log(2.0)));
772  }
773 
774  //! Constructor uses 3 parameters in the following order:
775  //! @f$ [A, t_0, \mathrm{fwhm}] @f$
776  Gaussian1(const vector<double>& params);
777 
778  string type() const override {
779  return "Gaussian";
780  }
781 
782  double eval(double t) const override {
783  double x = (t - m_t0)/m_tau;
784  return m_A * std::exp(-x*x);
785  }
786 
787 protected:
788  double m_A, m_t0, m_tau;
789 };
790 
791 
792 /**
793  * Implements a polynomial of degree @e n.
794  * The functor class with type @c "polynomial" returns
795  * @f[
796  * f(x) = a_n x^n + \dots + a_1 x + a_0
797  * @f]
798  * @ingroup func1advanced
799  */
800 class Poly1 : public Func1
801 {
802 public:
803  Poly1(size_t n, const double* c) {
804  m_cpoly.resize(n+1);
805  std::copy(c, c+m_cpoly.size(), m_cpoly.begin());
806  }
807 
808  //! Constructor uses @f$ n + 1 @f$ parameters in the following order:
809  //! @f$ [a_n, \dots, a_1, a_0] @f$
810  Poly1(const vector<double>& params);
811 
812  string type() const override {
813  return "polynomial";
814  }
815 
816  double eval(double t) const override {
817  double r = m_cpoly[m_cpoly.size()-1];
818  for (size_t n = 1; n < m_cpoly.size(); n++) {
819  r *= t;
820  r += m_cpoly[m_cpoly.size() - n - 1];
821  }
822  return r;
823  }
824 
825 protected:
826  vector<double> m_cpoly;
827 };
828 
829 
830 /**
831  * Implements a Fourier cosine/sine series.
832  * The functor class with type @c "Fourier" returns
833  * @f[
834  * f(t) = \frac{A_0}{2} +
835  * \sum_{n=1}^N A_n \cos (n \omega t) + B_n \sin (n \omega t)
836  * @f]
837  * @ingroup func1advanced
838  */
839 class Fourier1 : public Func1
840 {
841 public:
842  Fourier1(size_t n, double omega, double a0, const double* a, const double* b) {
843  m_omega = omega;
844  m_a0_2 = 0.5*a0;
845  m_ccos.resize(n);
846  m_csin.resize(n);
847  std::copy(a, a+n, m_ccos.begin());
848  std::copy(b, b+n, m_csin.begin());
849  }
850 
851  //! Constructor uses @f$ 2 n + 2 @f$ parameters in the following order:
852  //! @f$ [a_0, a_1, \dots, a_n, \omega, b_1, \dots, b_n] @f$
853  Fourier1(const vector<double>& params);
854 
855  string type() const override {
856  return "Fourier";
857  }
858 
859  double eval(double t) const override {
860  size_t n, nn;
861  double sum = m_a0_2;
862  for (n = 0; n < m_ccos.size(); n++) {
863  nn = n + 1;
864  sum += m_ccos[n]*std::cos(m_omega*nn*t)
865  + m_csin[n]*std::sin(m_omega*nn*t);
866  }
867  return sum;
868  }
869 
870 protected:
871  double m_omega, m_a0_2;
872  vector<double> m_ccos, m_csin;
873 };
874 
875 
876 /**
877  * Implements a sum of Arrhenius terms.
878  * The functor class with type @c "Arrhenius" returns
879  * @f[
880  * f(T) = \sum_{n=1}^N A_n T^b_n \exp(-E_n/T)
881  * @f]
882  * @ingroup func1advanced
883  */
884 class Arrhenius1 : public Func1
885 {
886 public:
887  Arrhenius1(size_t n, const double* c) {
888  m_A.resize(n);
889  m_b.resize(n);
890  m_E.resize(n);
891  for (size_t i = 0; i < n; i++) {
892  size_t loc = 3*i;
893  m_A[i] = c[loc];
894  m_b[i] = c[loc+1];
895  m_E[i] = c[loc+2];
896  }
897  }
898 
899  //! Constructor uses @f$ 3 n @f$ parameters in the following order:
900  //! @f$ [A_1, b_1, E_1, A_2, b_2, E_2, \dots, A_n, b_n, E_n] @f$
901  Arrhenius1(const vector<double>& params);
902 
903  string type() const override {
904  return "Arrhenius";
905  }
906 
907  double eval(double t) const override {
908  double sum = 0.0;
909  for (size_t n = 0; n < m_A.size(); n++) {
910  sum += m_A[n]*std::pow(t,m_b[n])*std::exp(-m_E[n]/t);
911  }
912  return sum;
913  }
914 
915 protected:
916  vector<double> m_A, m_b, m_E;
917 };
918 
919 /**
920  * Implements a periodic function.
921  * Takes any function and makes it periodic with period @f$ T @f$.
922  * @param f Functor to be made periodic
923  * @param T Period
924  * @ingroup func1modified
925  */
926 class Periodic1 : public Func1
927 {
928 public:
929  Periodic1(Func1& f, double T) {
930  m_f1 = &f;
931  m_c = T;
932  }
933 
934  Periodic1(shared_ptr<Func1> f, double A) : Func1(f, A) {}
935 
936  string type() const override {
937  return "periodic";
938  }
939 
940  ~Periodic1() override {
941  if (!m_f1_shared) {
942  delete m_f1;
943  }
944  }
945 
946  double eval(double t) const override {
947  int np = int(t/m_c);
948  double time = t - np*m_c;
949  return m_f1->eval(time);
950  }
951 };
952 
953 }
954 
955 #endif
Implements a sum of Arrhenius terms.
Definition: Func1.h:885
double eval(double t) const override
Evaluate the function.
Definition: Func1.h:907
string type() const override
Returns a string describing the type of the function.
Definition: Func1.h:903
Implements a composite function.
Definition: Func1.h:714
double eval(double t) const override
Evaluate the function.
Definition: Func1.h:736
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition: Func1.cpp:471
string type() const override
Returns a string describing the type of the function.
Definition: Func1.h:732
int order() const override
Return the order of the function, if it makes sense.
Definition: Func1.h:744
string write(const string &arg) const override
Write LaTeX string describing function.
Definition: Func1.cpp:465
Implements a constant.
Definition: Func1.h:404
double eval(double t) const override
Evaluate the function.
Definition: Func1.h:419
string type() const override
Returns a string describing the type of the function.
Definition: Func1.h:415
string write(const string &arg) const override
Write LaTeX string describing function.
Definition: Func1.cpp:405
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition: Func1.h:422
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:111
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:117
Implements the difference of two functions.
Definition: Func1.h:481
double eval(double t) const override
Evaluate the function.
Definition: Func1.h:503
string type() const override
Returns a string describing the type of the function.
Definition: Func1.h:499
int order() const override
Return the order of the function, if it makes sense.
Definition: Func1.h:511
string write(const string &arg) const override
Write LaTeX string describing function.
Definition: Func1.cpp:454
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition: Func1.h:507
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:137
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:146
Implements a Fourier cosine/sine series.
Definition: Func1.h:840
double eval(double t) const override
Evaluate the function.
Definition: Func1.h:859
string type() const override
Returns a string describing the type of the function.
Definition: Func1.h:855
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:97
shared_ptr< Func1 > derivative3() const
Creates a derivative to the current function.
Definition: Func1.h:125
virtual double eval(double t) const
Evaluate the function.
Definition: Func1.cpp:28
virtual string write(const string &arg) const
Write LaTeX string describing function.
Definition: Func1.cpp:380
double operator()(double t) const
Calls method eval to evaluate the function.
Definition: Func1.cpp:22
shared_ptr< Func1 > func2_shared() const
Accessor function for m_f2_shared.
Definition: Func1.h:152
virtual int order() const
Return the order of the function, if it makes sense.
Definition: Func1.cpp:69
bool isIdentical(Func1 &other) const
Routine to determine if two functions are the same.
Definition: Func1.cpp:39
shared_ptr< Func1 > func1_shared() const
Accessor function for m_f1_shared.
Definition: Func1.h:146
double c() const
Accessor function for the stored constant.
Definition: Func1.cpp:64
Implements a Gaussian function.
Definition: Func1.h:766
double eval(double t) const override
Evaluate the function.
Definition: Func1.h:782
string type() const override
Returns a string describing the type of the function.
Definition: Func1.h:778
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:164
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:173
Implements a periodic function.
Definition: Func1.h:927
double eval(double t) const override
Evaluate the function.
Definition: Func1.h:946
string type() const override
Returns a string describing the type of the function.
Definition: Func1.h:936
Implements the sum of a function and a constant.
Definition: Func1.h:628
double eval(double t) const override
Evaluate the function.
Definition: Func1.h:647
string type() const override
Returns a string describing the type of the function.
Definition: Func1.h:643
int order() const override
Return the order of the function, if it makes sense.
Definition: Func1.h:657
string write(const string &arg) const override
Write LaTeX string describing function.
Definition: Func1.cpp:497
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition: Func1.h:651
Implements a polynomial of degree n.
Definition: Func1.h:801
double eval(double t) const override
Evaluate the function.
Definition: Func1.h:816
string type() const override
Returns a string describing the type of the function.
Definition: Func1.h:812
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:192
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:385
Implements the product of two functions.
Definition: Func1.h:527
double eval(double t) const override
Evaluate the function.
Definition: Func1.h:551
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition: Func1.cpp:437
string type() const override
Returns a string describing the type of the function.
Definition: Func1.h:545
int order() const override
Return the order of the function, if it makes sense.
Definition: Func1.h:557
string write(const string &arg) const override
Write LaTeX string describing function.
Definition: Func1.cpp:424
Implements the ratio of two functions.
Definition: Func1.h:671
double eval(double t) const override
Evaluate the function.
Definition: Func1.h:693
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition: Func1.cpp:416
string type() const override
Returns a string describing the type of the function.
Definition: Func1.h:689
int order() const override
Return the order of the function, if it makes sense.
Definition: Func1.h:701
string write(const string &arg) const override
Write LaTeX string describing function.
Definition: Func1.cpp:410
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:94
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:85
Implements the sum of two functions.
Definition: Func1.h:436
double eval(double t) const override
Evaluate the function.
Definition: Func1.h:458
string type() const override
Returns a string describing the type of the function.
Definition: Func1.h:454
int order() const override
Return the order of the function, if it makes sense.
Definition: Func1.h:466
string write(const string &arg) const override
Write LaTeX string describing function.
Definition: Func1.cpp:443
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition: Func1.h:462
Implements a tabulated function.
Definition: Func1.h:356
double eval(double t) const override
Evaluate the function.
Definition: Func1.cpp:329
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition: Func1.cpp:352
string type() const override
Returns a string describing the type of the function.
Definition: Func1.h:381
string write(const string &arg) const override
Write LaTeX string describing function.
Definition: Func1.cpp:400
void setMethod(const string &method)
Set the interpolation method.
Definition: Func1.cpp:317
vector< double > m_tvec
Vector of time values.
Definition: Func1.h:391
bool m_isLinear
Boolean indicating interpolation method.
Definition: Func1.h:393
vector< double > m_fvec
Vector of function values.
Definition: Func1.h:392
Tabulated1(size_t n, const double *tvals, const double *fvals, const string &method="linear")
Constructor.
Definition: Func1.cpp:278
Implements the product of a function and a constant.
Definition: Func1.h:570
double eval(double t) const override
Evaluate the function.
Definition: Func1.h:605
string type() const override
Returns a string describing the type of the function.
Definition: Func1.h:585
int order() const override
Return the order of the function, if it makes sense.
Definition: Func1.h:615
string write(const string &arg) const override
Write LaTeX string describing function.
Definition: Func1.cpp:478
shared_ptr< Func1 > derivative() const override
Creates a derivative to the current function.
Definition: Func1.h:609
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:673
shared_ptr< Func1 > newProdFunction(shared_ptr< Func1 > f1, shared_ptr< Func1 > f2)
Product of two functions.
Definition: Func1.cpp:600
shared_ptr< Func1 > newDiffFunction(shared_ptr< Func1 > f1, shared_ptr< Func1 > f2)
Difference of two functions.
Definition: Func1.cpp:577
shared_ptr< Func1 > newTimesConstFunction(shared_ptr< Func1 > f, double c)
Product of function and constant.
Definition: Func1.cpp:693
shared_ptr< Func1 > newSumFunction(shared_ptr< Func1 > f1, shared_ptr< Func1 > f2)
Sum of two functions.
Definition: Func1.cpp:556
shared_ptr< Func1 > newRatioFunction(shared_ptr< Func1 > f1, shared_ptr< Func1 > f2)
Ratio of two functions.
Definition: Func1.cpp:653
shared_ptr< Func1 > newPlusConstFunction(shared_ptr< Func1 > f, double c)
Sum of function and constant.
Definition: Func1.cpp:707
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:564