Cantera

Previous topic

One-Dimensional Reacting Flows

Next topic

Error Handling

This Page

Warning

This documentation is for an old version of Cantera. You can find docs for newer versions here.

Func Module

Quick links:
  • Polynomial
  • Gaussian
  • Fourier
  • Arrhenius

The classes in this module are designed to allow constructing user-defined functions of one variable in Python that can be used with the Cantera C++ kernel. These classes are mostly shadow classes for corresponding classes in the C++ kernel.

class Cantera.Func.Func1(typ, n, coeffs=[])

Functors of one variable.

A Functor is an object that behaves like a function. Func1 is the base class from which several functor classes derive. These classes are designed to allow specifying functions of time from Python that can be used by the C++ kernel.

Functors can be added, multiplied, and divided to yield new functors.

>>> f1 = Polynomial([1.0, 0.0, 3.0])  # 3*t*t + 1
>>> f1(2.0)
13
>>> f2 = Polynomial([-1.0, 2.0])      # 2*t - 1
>>> f2(2.0)
5
>>> f3 = f1/f2                        # (3*t*t + 1)/(2*t - 1)
>>> f3(2.0)
4.3333333

The constructor is meant to be called from constructors of subclasses of Func1: Polynomial, Gaussian, Arrhenius, Fourier, Const, PeriodicFunction.

func_id()

Internal. Return the integer index used internally to access the kernel-level object.

write(arg='x', length=1000)
class Cantera.Func.Sin(omega=1.0)
class Cantera.Func.Cos(omega=1.0)
class Cantera.Func.Exp(A=1.0)
class Cantera.Func.Pow(n)
class Cantera.Func.Polynomial(coeffs=[])

A polynomial. Instances of class ‘Polynomial’ evaluate

\[f(t) = \sum_{n = 0}^N a_n t^n .\]

The coefficients are supplied as a list, beginning with \(a_N\) and ending with \(a_0\).

>>> p1 = Polynomial([1.0, -2.0, 3.0])   #    3t^2 - 2t + 1
>>> p2 = Polynomial([6.0, 8.0])         #    8t + 6

coeffs - polynomial coefficients

class Cantera.Func.Gaussian(A, t0, FWHM)

A Gaussian pulse. Instances of class ‘Gaussian’ evaluate

\[f(t) = A \exp[-(t - t_0) / \tau]\]

where

\[\tau = \frac{\mbox{FWHM}}{2.0\sqrt{\ln(2.0)}}\]

‘FWHM’ denotes the full width at half maximum.

As an example, here is how to create a Gaussian pulse with peak amplitude 10.0, centered at time 2.0, with full-width at half max = 0.2:

>>> f = Gaussian(A = 10.0, t0 = 2.0, FWHM = 0.2)
>>> f(2.0)
10
>>> f(1.9)
5
>>> f(2.1)
5
class Cantera.Func.Fourier(omega, coefficients)

Fourier series. Instances of class ‘Fourier’ evaluate the Fourier series

\[f(t) = \frac{a_0}{2} + \sum_{n=1}^N [a_n \cos(n\omega t) + b_n \sin(n \omega t)]\]

where

\[a_n = \frac{\omega}{\pi} \int_{-\pi/\omega}^{\pi/\omega} f(t) \cos(n \omega t) dt\]\[b_n = \frac{\omega}{\pi} \int_{-\pi/\omega}^{\pi/\omega} f(t) \sin(n \omega t) dt.\]

The function \(f(t)\) is periodic, with period \(T = 2\pi/\omega\).

As an example, a function with Fourier components up to the second harmonic is constructed as follows:

>>> coeffs = [(a0, b0), (a1, b1), (a2, b2)]
>>> f = Fourier(omega, coeffs)

Note that b0 must be specified, but is not used. The value of b0 is arbitrary.

Parameters:
  • omega – fundamental frequency [radians/sec].
  • coefficients – List of (a,b) pairs, beginning with n = 0.
class Cantera.Func.Arrhenius(coefficients)

Sum of modified Arrhenius terms. Instances of class ‘Arrhenius’ evaluate

\[f(T) = \sum_{n=1}^N A_n T^{b_n}\exp(-E_n/T)\]

Example:

>>> f = Arrhenius([(a0, b0, e0), (a1, b1, e1)])
Parameters:coefficients

sequence of (A, b, E) triplets.

class Cantera.Func.Const(value)

Constant function. Objects created by function Const act as functions that have a constant value. These are used internally whenever a statement like

>>> f = Gausian(2.0, 1.0, 0.1) + 4.0

is encountered. The addition operator of class Func1 is defined so that this is equivalent to

>>> f = SumFunction(Gaussian(2.0, 1.0, 0.1), Const(4.0))

Function Const returns instances of class Polynomial that have degree zero, with the constant term set to the desired value.

class Cantera.Func.PeriodicFunction(func, T)

Converts a function into a periodic function with period T.

Parameters:
  • func – initial non-periodic function
  • T – period [s]
class Cantera.Func.ComboFunc1(typ, f1, f2)

Combines two functions. This class is the base class for functors that combine two other functors in a binary operation.

class Cantera.Func.SumFunction(f1, f2)

Sum of two functions. Instances of class SumFunction evaluate the sum of two supplied functors. It is not necessary to explicitly create an instance of SumFunction, since the addition operator of the base class is overloaded to return a SumFunction instance.

>>> f1 = Polynomial([2.0, 1.0])
>>> f2 = Polynomial([3.0, -5.0])
>>> f3 = f1 + f2     # functor to evaluate (2t + 1) + (3t - 5)

In this example, object ‘f3’ is a functor of class’SumFunction’ that calls f1 and f2 and returns their sum.

Parameters:
  • f1 – first functor.
  • f2 – second functor.
class Cantera.Func.DiffFunction(f1, f2)

Difference of two functions. Instances of class DiffFunction evaluate the difference of two supplied functors. It is not necessary to explicitly create an instance of DiffFunction, since the subtraction operator of the base class is overloaded to return a DiffFunction instance.

>>> f1 = Polynomial([2.0, 1.0])
>>> f2 = Polynomial([3.0, -5.0])
>>> f3 = f1 - f2     # functor to evaluate (2t + 1) - (3t - 5)

In this example, object ‘f3’ is a functor of class’DiffFunction’ that calls f1 and f2 and returns their difference.

Parameters:
  • f1 – first functor.
  • f2 – second functor.
class Cantera.Func.ProdFunction(f1, f2)

Product of two functions. Instances of class ProdFunction evaluate the product of two supplied functors. It is not necessary to explicitly create an instance of ‘ProdFunction’, since the multiplication operator of the base class is overloaded to return a ‘ProdFunction’ instance.

>>> f1 = Polynomial([2.0, 1.0])
>>> f2 = Polynomial([3.0, -5.0])
>>> f3 = f1 * f2     # functor to evaluate (2t + 1)*(3t - 5)

In this example, object ‘f3’ is a functor of class’ProdFunction’ that calls f1 and f2 and returns their product.

Parameters:
  • f1 – first functor.
  • f2 – second functor.
class Cantera.Func.RatioFunction(f1, f2)

Ratio of two functions. Instances of class RatioFunction evaluate the ratio of two supplied functors. It is not necessary to explicitly create an instance of ‘RatioFunction’, since the division operator of the base class is overloaded to return a RatioFunction instance.

>>> f1 = Polynomial([2.0, 1.0])
>>> f2 = Polynomial([3.0, -5.0])
>>> f3 = f1 / f2     # functor to evaluate (2t + 1)/(3t - 5)

In this example, object ‘f3’ is a functor of class’RatioFunction’ that calls f1 and f2 and returns their ratio.

Parameters:
  • f1 – first functor.
  • f2 – second functor.
class Cantera.Func.CompositeFunction(f1, f2)

Function of a function. Instances of class CompositeFunction evaluate f(g(t)) for two supplied functors f and g. It is not necessary to explicitly create an instance of ‘CompositeFunction’, since the () operator of the base class is overloaded to return a CompositeFunction when called with a functor argument.

>>> f1 = Polynomial([2.0, 1.0])
>>> f2 = Polynomial([3.0, -5.0])
>>> f3 = f1(f2)     # functor to evaluate 2(3t - 5) + 1

In this example, object ‘f3’ is a functor of class’CompositeFunction’ that calls f1 and f2 and returns f1(f2(t)).

Parameters:
  • f1 – first functor.
  • f2 – second functor.
class Cantera.Func.DerivativeFunction(f)
Cantera.Func.derivative(f)

Take the derivative of a functor f