From 21169b3a380961ce4e5d882434f65a4065074749 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Thu, 6 Sep 2012 19:56:20 +0000 Subject: [PATCH] [Python] Added basic implementation of Func1 and its children --- interfaces/cython/cantera/_cantera.pxd | 12 ++++++++++++ interfaces/cython/cantera/_cantera.pyx | 1 + interfaces/cython/cantera/func1.pyx | 20 ++++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 interfaces/cython/cantera/func1.pyx diff --git a/interfaces/cython/cantera/_cantera.pxd b/interfaces/cython/cantera/_cantera.pxd index 321dca388..c85bb78ca 100644 --- a/interfaces/cython/cantera/_cantera.pxd +++ b/interfaces/cython/cantera/_cantera.pxd @@ -46,6 +46,15 @@ cdef extern from "cantera/equil/MultiPhase.h" namespace "Cantera": void setPressure(double) double pressure() +cdef extern from "cantera/numerics/Func1.h": + cdef cppclass CxxFunc1 "Cantera::Func1": + CxxFunc1() + double eval(double) + string write(string) + + cdef cppclass CxxSin1 "Cantera::Sin1": + CxxSin1(double) + cdef extern from "cantera/thermo/ThermoFactory.h" namespace "Cantera": cdef CxxThermoPhase* newPhase(string, string) except + cdef CxxThermoPhase* newPhase(XML_Node&) except + @@ -67,3 +76,6 @@ cdef class _SolutionBase: cdef class Mixture: cdef CxxMultiPhase* mix cdef list _phases + +cdef class Func1: + cdef CxxFunc1* func diff --git a/interfaces/cython/cantera/_cantera.pyx b/interfaces/cython/cantera/_cantera.pyx index 645c3ca6c..f6cd7850d 100644 --- a/interfaces/cython/cantera/_cantera.pyx +++ b/interfaces/cython/cantera/_cantera.pyx @@ -7,6 +7,7 @@ from _cantera cimport * include "utils.pyx" include "constants.pyx" +include "func1.pyx" include "base.pyx" include "thermo.pyx" diff --git a/interfaces/cython/cantera/func1.pyx b/interfaces/cython/cantera/func1.pyx new file mode 100644 index 000000000..2bc073980 --- /dev/null +++ b/interfaces/cython/cantera/func1.pyx @@ -0,0 +1,20 @@ +cdef class Func1: + def __cinit__(self, *args, **kwargs): + self.func = NULL # derived classes should allocate this object + + def __dealloc__(self): + del self.func + + def __init__(self, *args, **kwargs): + assert self.func != NULL + + def __call__(self, t): + return self.func.eval(t) + + def __str__(self): + cdef bytes s = self.func.write(stringify("t")).c_str() + return s.decode() + +cdef class Sin1(Func1): + def __cinit__(self, freq): + self.func = (new CxxSin1(freq))