Skip to content

Commit

Permalink
[Python] Rename TabulatedFunction to Tabulated1
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Jul 1, 2023
1 parent ab6134f commit 641bef6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 17 deletions.
2 changes: 1 addition & 1 deletion doc/sphinx/cython/zerodim.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Defining Functions

.. autoclass:: Func1

.. autoclass:: TabulatedFunction
.. autoclass:: Tabulated1

Base Classes
------------
Expand Down
59 changes: 52 additions & 7 deletions interfaces/cython/cantera/func1.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
cimport numpy as np
import numpy as np
import warnings

from ._utils cimport *

Expand Down Expand Up @@ -180,7 +181,48 @@ cdef class Func1:
raise NotImplementedError(msg)


cdef class TabulatedFunction(Func1):
cdef class Tabulated1(Func1):
"""
A `Tabulated1` object representing a tabulated function is defined by
sample points and corresponding function values. Inputs are specified by
two iterable objects containing sample point location and function values.
Between sample points, values are evaluated based on the optional argument
``method``; options are ``'linear'`` (linear interpolation, default) or
``'previous'`` (nearest previous value). Outside the sample interval, the
value at the closest end point is returned.
Examples for `Tabulated1` objects are::
>>> t1 = Tabulated1([0, 1, 2], [2, 1, 0])
>>> [t1(v) for v in [-0.5, 0, 0.5, 1.5, 2, 2.5]]
[2.0, 2.0, 1.5, 0.5, 0.0, 0.0]
>>> t2 = Tabulated1(np.array([0, 1, 2]), np.array([2, 1, 0]))
>>> [t2(v) for v in [-0.5, 0, 0.5, 1.5, 2, 2.5]]
[2.0, 2.0, 1.5, 0.5, 0.0, 0.0]
The optional ``method`` keyword argument changes the type of interpolation
from the ``'linear'`` default to ``'previous'``::
>>> t3 = Tabulated1([0, 1, 2], [2, 1, 0], method='previous')
>>> [t3(v) for v in [-0.5, 0, 0.5, 1.5, 2, 2.5]]
[2.0, 2.0, 2.0, 1.0, 0.0, 0.0]
.. versionadded:: 3.0
"""

def __init__(self, time, fval, method='linear'):
cdef vector[double] arr
for v in time:
arr.push_back(v)
for v in fval:
arr.push_back(v)
cdef string cxx_string = stringify(f"tabulated-{method}")
self._func = CxxNewFunc1(cxx_string, arr)
self.func = self._func.get()


cdef class TabulatedFunction(Tabulated1):
"""
A `TabulatedFunction` object representing a tabulated function is defined by
sample points and corresponding function values. Inputs are specified by
Expand All @@ -206,10 +248,13 @@ cdef class TabulatedFunction(Func1):
>>> t3 = TabulatedFunction([0, 1, 2], [2, 1, 0], method='previous')
>>> [t3(v) for v in [-0.5, 0, 0.5, 1.5, 2, 2.5]]
[2.0, 2.0, 2.0, 1.0, 0.0, 0.0]
"""
def __init__(self, time, fval, method='linear'):
arr = np.hstack([np.array(time), np.array(fval)])
cdef Func1 func = Func1.cxx_functor(f"tabulated-{method}", arr)
self._func = func._func
self.func = self._func.get()
.. deprecated:: 3.0
To be removed after Cantera 3.0. Renamed to `Tabulated1`.
"""
def __init__(self, *args, **kwargs):
warnings.warn(
"TabulatedFunction: To be removed after Cantera 3.0. "
"Renamed to 'Tabulated1'.", DeprecationWarning)
super().__init__(*args, **kwargs)
18 changes: 9 additions & 9 deletions test/python/test_func1.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,30 +127,30 @@ def test_tabulated1(self):
arr = np.array([[0, 2], [1, 1], [2, 0]])
time = arr[:, 0]
fval = arr[:, 1]
fcn = ct.TabulatedFunction(time, fval)
fcn = ct.Tabulated1(time, fval)
assert fcn.type == "tabulated-linear"
for t, f in zip(time, fval):
self.assertNear(f, fcn(t))

def test_tabulated2(self):
time = [0, 1, 2]
fval = [2, 1, 0]
fcn = ct.TabulatedFunction(time, fval)
fcn = ct.Tabulated1(time, fval)
assert fcn.type == "tabulated-linear"
for t, f in zip(time, fval):
self.assertNear(f, fcn(t))

def test_tabulated3(self):
time = 0, 1, 2,
fval = 2, 1, 0,
fcn = ct.TabulatedFunction(time, fval)
fcn = ct.Tabulated1(time, fval)
self.assertNear(fcn(-1), fval[0])
self.assertNear(fcn(3), fval[-1])

def test_tabulated4(self):
time = np.array([0, 1, 2])
fval = np.array([2, 1, 0])
fcn = ct.TabulatedFunction(time, fval)
fcn = ct.Tabulated1(time, fval)
tt = .5*(time[1:] + time[:-1])
ff = .5*(fval[1:] + fval[:-1])
for t, f in zip(tt, ff):
Expand All @@ -159,17 +159,17 @@ def test_tabulated4(self):
def test_tabulated5(self):
time = [0, 1, 2]
fval = [2, 1, 0]
fcn = ct.TabulatedFunction(time, fval, method='previous')
fcn = ct.Tabulated1(time, fval, method='previous')
assert fcn.type == "tabulated-previous"
val = np.array([fcn(v) for v in [-0.5, 0, 0.5, 1.5, 2, 2.5]])
self.assertArrayNear(val, np.array([2.0, 2.0, 2.0, 1.0, 0.0, 0.0]))

def test_tabulated_failures(self):
with pytest.raises(ct.CanteraError, match="even number of entries"):
ct.TabulatedFunction(range(2), range(3))
ct.Tabulated1(range(2), range(3))
with pytest.raises(ct.CanteraError, match="at least 4 entries"):
ct.TabulatedFunction([], [])
ct.Tabulated1([], [])
with pytest.raises(ct.CanteraError, match="monotonically"):
ct.TabulatedFunction((0, 1, 0.5, 2), (2, 1, 1, 0))
ct.Tabulated1((0, 1, 0.5, 2), (2, 1, 1, 0))
with pytest.raises(ct.CanteraError, match="No such type"):
ct.TabulatedFunction((0, 1, 1, 2), (2, 1, 1, 0), method='spam')
ct.Tabulated1((0, 1, 1, 2), (2, 1, 1, 0), method='spam')

0 comments on commit 641bef6

Please sign in to comment.