Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Python Func1 Interface #1521

Merged
merged 9 commits into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
speth marked this conversation as resolved.
Show resolved Hide resolved
.. autoclass:: Tabulated1

Base Classes
------------
Expand Down
3 changes: 1 addition & 2 deletions include/cantera/clib/ctfunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ extern "C" {

CANTERA_CAPI int func_new(int type, size_t n, size_t lenp, const double* p);
CANTERA_CAPI int func_new_basic(const char* type, double c);
CANTERA_CAPI int func_new_advanced(
const char* type, size_t lenp, const double* p, size_t n);
CANTERA_CAPI int func_new_advanced(const char* type, size_t lenp, const double* p);
CANTERA_CAPI int func_new_compound(const char* type, int a, int b);
CANTERA_CAPI int func_new_modified(const char* type, int a, double c);
CANTERA_CAPI int func_del(int i);
Expand Down
26 changes: 15 additions & 11 deletions include/cantera/numerics/Func1.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ class Func1
return "functor";
}

//! Returns a string describing the type of the function
//! @since New in Cantera 3.0.
string type_name() const;
ischoegl marked this conversation as resolved.
Show resolved Hide resolved

//! Calls method eval to evaluate the function
doublereal operator()(doublereal t) const;

Expand Down Expand Up @@ -274,7 +278,7 @@ class Sin1 : public Func1
}

//! Constructor uses single parameter (frequency)
Sin1(const vector<double>& params, size_t n=npos);
Sin1(const vector<double>& params);

Sin1(const Sin1& b) :
Func1(b) {
Expand Down Expand Up @@ -323,7 +327,7 @@ class Cos1 : public Func1
}

//! Constructor uses single parameter (frequency)
Cos1(const vector<double>& params, size_t n=npos);
Cos1(const vector<double>& params);

Cos1(const Cos1& b) :
Func1(b) {
Expand Down Expand Up @@ -368,7 +372,7 @@ class Exp1 : public Func1
}

//! Constructor uses single parameter (exponent factor)
Exp1(const vector<double>& params, size_t n=npos);
Exp1(const vector<double>& params);

Exp1(const Exp1& b) :
Func1(b) {
Expand Down Expand Up @@ -413,7 +417,7 @@ class Log1 : public Func1
}

//! Constructor uses single parameter (factor)
Log1(const vector<double>& params, size_t n=npos);
Log1(const vector<double>& params);

virtual string type() const {
return "log";
Expand Down Expand Up @@ -442,7 +446,7 @@ class Pow1 : public Func1
}

//! Constructor uses single parameter (exponent)
Pow1(const vector<double>& params, size_t n=npos);
Pow1(const vector<double>& params);

Pow1(const Pow1& b) :
Func1(b) {
Expand Down Expand Up @@ -495,7 +499,7 @@ class Tabulated1 : public Func1

//! Constructor uses \f$ 2 n\f$ parameters in the following order:
//! \f$ [t_0, t_1, \dots, t_{n-1}, f_0, f_1, \dots, f_{n-1}] \f$
Tabulated1(const vector<double>& params, size_t n);
Tabulated1(const vector<double>& params);

//! Set the interpolation method
//! @param method Evaluation method. If \c "linear" (default), a linear
Expand Down Expand Up @@ -540,7 +544,7 @@ class Const1 : public Func1
}

//! Constructor uses single parameter (constant)
Const1(const vector<double>& params, size_t n=npos);
Const1(const vector<double>& params);

Const1(const Const1& b) :
Func1(b) {
Expand Down Expand Up @@ -1098,7 +1102,7 @@ class Gaussian1 : public Func1

//! Constructor uses 3 parameters in the following order:
//! \f$ [A, t_0, \mathrm{fwhm}] \f$
Gaussian1(const vector<double>& params, size_t n=npos);
Gaussian1(const vector<double>& params);

Gaussian1(const Gaussian1& b) :
Func1(b) {
Expand Down Expand Up @@ -1171,7 +1175,7 @@ class Poly1 : public Func1

//! Constructor uses \f$ n + 1 \f$ parameters in the following order:
//! \f$ [a_n, \dots, a_1, a_0] \f$
Poly1(const vector<double>& params, size_t n);
Poly1(const vector<double>& params);

Poly1(const Poly1& b) :
Func1(b) {
Expand Down Expand Up @@ -1231,7 +1235,7 @@ class Fourier1 : public Func1

//! Constructor uses \f$ 2 n + 2 \f$ parameters in the following order:
//! \f$ [a_0, a_1, \dots, a_n, \omega, b_1, \dots, b_n] \f$
Fourier1(const vector<double>& params, size_t n);
Fourier1(const vector<double>& params);

Fourier1(const Fourier1& b) :
Func1(b) {
Expand Down Expand Up @@ -1299,7 +1303,7 @@ class Arrhenius1 : public Func1

//! Constructor uses \f$ 3 n\f$ parameters in the following order:
//! \f$ [A_1, b_1, E_1, A_2, b_2, E_2, \dots, A_n, b_n, E_n] \f$
Arrhenius1(const vector<double>& params, size_t n=1);
Arrhenius1(const vector<double>& params);

Arrhenius1(const Arrhenius1& b) :
Func1() {
Expand Down
6 changes: 2 additions & 4 deletions include/cantera/numerics/Func1Factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Cantera
//! shared_ptr<Func1> d1 = newFunc1("sin", {1.0});
//! ```
//! @since New in Cantera 3.0
class Func1Factory : public Factory<Func1, const vector<double>&, size_t>
class Func1Factory : public Factory<Func1, const vector<double>&>
{
public:
/**
Expand Down Expand Up @@ -121,11 +121,9 @@ shared_ptr<Func1> newFunc1(const string& func1Type, double coeff=1.);
//! Create a new advanced functor object (see \ref func1advanced).
//! @param func1Type String identifying functor type.
//! @param params Parameter vector; definition depends on functor type.
//! @param n Integer; definition depends on function type and may or may not be used.
//! @ingroup func1advanced
//! @since New in Cantera 3.0
shared_ptr<Func1> newFunc1(const string& func1Type,
const vector<double>& params, size_t n=1);
shared_ptr<Func1> newFunc1(const string& func1Type, const vector<double>& params);

//! Create a new compound functor object (see \ref func1compound).
//! @param func1Type String identifying functor type.
Expand Down
30 changes: 20 additions & 10 deletions interfaces/cython/cantera/func1.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@

from .ctcxx cimport *


cdef extern from "cantera/numerics/Func1.h":
cdef cppclass CxxFunc1 "Cantera::Func1":
double eval(double) except +translate_exception
string type()
string type_name()
string write(string)


cdef extern from "cantera/cython/funcWrapper.h":
ctypedef double (*callback_wrapper)(double, void*, void**) except? 0.0
cdef int translate_exception()

cdef cppclass CxxFunc1 "Func1Py":
CxxFunc1(callback_wrapper, void*)
double eval(double) except +translate_exception
cdef cppclass CxxFunc1Py "Func1Py" (CxxFunc1):
CxxFunc1Py(callback_wrapper, void*)

cdef cppclass PyFuncInfo:
PyFuncInfo()
Expand All @@ -24,10 +32,15 @@ cdef extern from "cantera/cython/funcWrapper.h":
void setExceptionValue(PyObject*)


cdef extern from "cantera/numerics/Func1.h":
cdef cppclass CxxTabulated1 "Cantera::Tabulated1":
CxxTabulated1(int, double*, double*, string) except +translate_exception
double eval(double) except +translate_exception
cdef extern from "cantera/numerics/Func1Factory.h":
cdef shared_ptr[CxxFunc1] CxxNewFunc1 "Cantera::newFunc1" (
string, double) except +translate_exception
cdef shared_ptr[CxxFunc1] CxxNewFunc1 "Cantera::newFunc1" (
string, vector[double]&) except +translate_exception
cdef shared_ptr[CxxFunc1] CxxNewFunc1 "Cantera::newFunc1" (
string, shared_ptr[CxxFunc1], shared_ptr[CxxFunc1]) except +translate_exception
cdef shared_ptr[CxxFunc1] CxxNewFunc1 "Cantera::newFunc1" (
string, shared_ptr[CxxFunc1], double) except +translate_exception


cdef class Func1:
Expand All @@ -36,6 +49,3 @@ cdef class Func1:
cdef object callable
cdef object exception
cpdef void _set_callback(self, object) except *

cdef class TabulatedFunction(Func1):
cpdef void _set_tables(self, object, object, string) except *
Loading