Dag Sverre Seljebotn wrote:
> Jon Olav Vik wrote:
>
>> Yes, ydot is allocated by CVODE (though not initialized to zero, as I
>> learned
>> the hard way). I do need to allocate y, however.
>>
>> As a start, I would like Cython to compile the following line of code:
>>
>> y0 = N_VNew_Serial(N)
>>
>> However, I'm unsure how much of the C declarations I need to duplicate (with
>> ctypedef?). N_VNew_Serial returns an N_Vector:
>> include/nvector/nvector_serial.h:N_Vector N_VNew_Serial(long int vec_length);
>>
>> The generic definition of an N_Vector is here:
>>
>> == BEGIN include/sundials/sundials_nvector.h (excerpt) ==
>> #include <sundials/sundials_types.h>
>>
>> /*
>> * -----------------------------------------------------------------
>> * Generic definition of N_Vector
>> * -----------------------------------------------------------------
>> */
>>
>> /* Forward reference for pointer to N_Vector_Ops object */
>> typedef struct _generic_N_Vector_Ops *N_Vector_Ops;
>>
>> /* Forward reference for pointer to N_Vector object */
>> typedef struct _generic_N_Vector *N_Vector;
>>
>> /* Define array of N_Vectors */
>> typedef N_Vector *N_Vector_S;
>>
>> /* Structure containing function pointers to vector operations */
>> struct _generic_N_Vector_Ops {
>> N_Vector (*nvclone)(N_Vector);
>> N_Vector (*nvcloneempty)(N_Vector);
>> void (*nvdestroy)(N_Vector);
>>
> ...
>
>> struct _generic_N_Vector {
>> void *content;
>> struct _generic_N_Vector_Ops *ops;
>> };
>>
>
>
>> The abovementioned realtype (and booleantype) is defined in
>> include/sundials/sundials_types.h
>> from which I roughly quote some conditional defines:
>> #if defined(SUNDIALS_SINGLE_PRECISION)
>> typedef float realtype;
>> #elif defined(SUNDIALS_DOUBLE_PRECISION)
>> typedef double realtype;
>> #elif defined(SUNDIALS_EXTENDED_PRECISION)
>> typedef long double realtype;
>>
>> As for the structs, I've tried and failed along these lines:
>> cdef extern from "sundials/sundials_nvector.h":
>> ctypedef struct _generic_N_Vector
>> ctypedef struct _generic_N_Vector *N_Vector
>> ^
>> ------------------------------------------------------------
>> /xanadu/home/jonvi/svn/trunk/cysundials/hello.pyx:8:38: Syntax error in
>> struct
>> or union definition
>>
>> Do I really have to replicate the entire struct definition?
>>
>
> (Trivia: This is a lot like NumPy's source code.)
>
> No, you just have to write "enough" for Cython to know what C code to
> output. Knowing what that is probably takes some C experience though;
> I'll try to help.
>
> First, the #if-s cannot be expressed in Cython, but Cython is going in
> the direction of the type of external typedefs not to matter. I think
> you can safely do
>
> cdef extern from ...:
> ctypedef double realtype
>
> It should work also when Sundials is compiled with float or long double.
>
> Then N_Vector etc. can probably be expressed like this:
>
> cdef extern from ....:
> cdef struct _generic_N_Vector:
> void* content
> # not necesarry to declare ops unless you will be using
> # the contents of ops from Cython
>
> ctypedef _generic_N_Vector* N_vector
>
> N_Vector N_VNew_Serial(long int vec_length)
>
> Then you will be able to do
>
> cdef NVector v = N_VNew_Serial(10)
> cdef realtype* v_data = <realtype*>v.content
> v_data[0] = 34
>
Make sure to also free/delete the vector though! (There must be a
corresponding call there somewhere)
Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev