From 1c0e1237137e74bef96bfd7f8ea0ced887197c14 Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Thu, 23 Jan 2020 13:56:44 -0600 Subject: [PATCH] [numerics] Use Const1 objects in Python - replace Python lambda function defining constant Func1 variants by pre-existing Const1 class defined in C++ --- interfaces/cython/cantera/_cantera.pxd | 9 +++++++-- interfaces/cython/cantera/func1.pyx | 17 ++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/interfaces/cython/cantera/_cantera.pxd b/interfaces/cython/cantera/_cantera.pxd index 2b4ebebbbee..542ebc334f4 100644 --- a/interfaces/cython/cantera/_cantera.pxd +++ b/interfaces/cython/cantera/_cantera.pxd @@ -43,6 +43,10 @@ cdef extern from "cantera/numerics/Func1.h": CxxTabulated1(vector[double]&, vector[double]&) except +translate_exception double eval(double) except +translate_exception + cdef cppclass CxxConst1 "Cantera::Const1": + CxxConst1(double) except +translate_exception + double eval(double) except +translate_exception + cdef extern from "cantera/base/xml.h" namespace "Cantera": cdef cppclass XML_Node: XML_Node* findByName(string) @@ -1015,8 +1019,9 @@ cdef class Func1: cdef CxxFunc1* func cdef object callable cdef object exception - cpdef void __set_callback(self, object) except * - cpdef void __set_tables(self, object, object) except * + cpdef void _set_callback(self, object) except * + cpdef void _set_const(self, double) except * + cpdef void _set_tables(self, object, object) except * cdef class ReactorBase: cdef CxxReactorBase* rbase diff --git a/interfaces/cython/cantera/func1.pyx b/interfaces/cython/cantera/func1.pyx index f70a3354a8f..a8f5e1ce15d 100644 --- a/interfaces/cython/cantera/func1.pyx +++ b/interfaces/cython/cantera/func1.pyx @@ -87,24 +87,24 @@ cdef class Func1: c = args[0] if hasattr(c, '__call__'): # callback function - self.__set_callback(c) + self._set_callback(c) else: arr = np.array(c) try: if arr.ndim == 0: # handle constants or unsized numpy arrays k = float(c) - self.__set_callback(lambda t: k) + self._set_const(k) elif arr.size == 1: # handle lists, tuples or numpy arrays with a single element k = float(c[0]) - self.__set_callback(lambda t: k) + self._set_const(k) elif arr.ndim == 2: # tabulated function (single argument) if arr.shape[1] == 2: time = arr[:, 0] fval = arr[:, 1] - self.__set_tables(time, fval) + self._set_tables(time, fval) else: raise ValueError( "Invalid dimensions: specification of " @@ -121,17 +121,20 @@ cdef class Func1: elif len(args) == 2: # tabulated function (two arguments mimic C++ interface) time, fval = args - self.__set_tables(time, fval) + self._set_tables(time, fval) else: raise ValueError("Invalid number of arguments") - cpdef void __set_callback(self, c) except *: + cpdef void _set_callback(self, c) except *: self.callable = c self.func = new CxxFunc1(func_callback, self) - cpdef void __set_tables(self, time, fval) except *: + cpdef void _set_const(self, double c) except *: + self.func = (new CxxConst1(c)) + + cpdef void _set_tables(self, time, fval) except *: cdef vector[double] tvec, fvec for t in time: tvec.push_back(t)