From charlesreid1

This page details my installation procedure for the python-sundials library: https://code.google.com/p/python-sundials/

Installation

Mac OS X

The usual setup.py installation procedure was followed, although I had to make several modifications to the source code and the compile line to get it working (see the #Errors section below). The final command that I used to build the library was:

sudo CFLAGS='-I/Users/charlesreid/pkg/sundials/std/include -L/Users/charlesreid/pkg/sundials/std/lib' LDFLAGS="$CFLAGS" python setup.py build

Errors

NVECTOR.pxi: Taking Address of Non-Lvalue

On my first attempt to install python-sundials, I got this error:

sundials/NVECTOR.pxi:41:37: Taking address of non-lvalue

Here is the full error context:

$ python setup.py build
running build
running build_ext
cythoning sundials/sundials.pyx to sundials/sundials.c

Error compiling Cython file:
------------------------------------------------------------
...
        buffer.obj = self
        buffer.len = self.getsize() * sizeof(double)
        buffer.readonly = 0
        buffer.format = <char*>"d"
        buffer.ndim = 1
        buffer.shape = <Py_ssize_t *>&self.__shape
                                    ^
------------------------------------------------------------

sundials/NVECTOR.pxi:41:37: Taking address of non-lvalue
warning: sundials/KINSOL.pxi:183:39: Not all members given for struct 'kinsol_userdata'
warning: sundials/KINSOL.pxi:183:39: Not all members given for struct 'kinsol_userdata'
building 'sundials' extension
creating build
creating build/temp.macosx-10.8-intel-2.7
creating build/temp.macosx-10.8-intel-2.7/sundials
clang -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -Isundials -Isundials/include -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c sundials/sundials.c -o build/temp.macosx-10.8-intel-2.7/sundials/sundials.o
clang: warning: argument unused during compilation: '-mno-fused-madd'
sundials/sundials.c:1:2: error: #error Do not use this file, it is the result of a failed Cython compilation.
#error Do not use this file, it is the result of a failed Cython compilation.
 ^
1 error generated.
Traceback
:error: command 'clang' failed with exit status 1

I was able to resolve this issue by modifying the source code of NVECTOR.pxi. I changed this line (the line the error referred to):

        buffer.shape = <Py_ssize_t *>&self.__shape

to this line:

        buffer.shape = <Py_ssize_t *>self.__shape

removing the ampersand &.

Header Files Not Found

If you run into an error about header files not being found, like this one:

$ python setup.py build
running build
running build_ext
cythoning sundials/sundials.pyx to sundials/sundials.c
warning: sundials/KINSOL.pxi:183:39: Not all members given for struct 'kinsol_userdata'
warning: sundials/KINSOL.pxi:183:39: Not all members given for struct 'kinsol_userdata'
building 'sundials' extension
clang -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -Isundials -Isundials/include -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c sundials/sundials.c -o build/temp.macosx-10.8-intel-2.7/sundials/sundials.o
clang: warning: argument unused during compilation: '-mno-fused-madd'
sundials/sundials.c:313:10: fatal error: 'sundials/sundials_types.h' file not found
#include "sundials/sundials_types.h"
         ^
1 error generated.
Traceback
:error: command 'clang' failed with exit status 1

then you probably installed Sundials to a non-standard location. To instruct setup.py to use the Sundials libraries in a non-standard location, use the CFLAGS environment variable when calling setup.py:

CFLAGS="-L/Users/charlesreid/pkg/sundails/std/lib -I/Users/charlesreid/pkg/sundials/std/include" python setup.py build

Linker Command Failed: Library Not Found For lsundials_cvode

Once I fixed the pxi file, and instructed setup.py to use a C compiler command that would point to my non-standard sundials library install locations, I saw an error with the linker:

creating build/lib.macosx-10.8-intel-2.7
clang -bundle -undefined dynamic_lookup -Wl,-F. -arch i386 -arch x86_64 build/temp.macosx-10.8-intel-2.7/sundials/sundials.o -Lsundials -lsundials_cvode -lsundials_ida -lsundials_kinsol -lsundials_nvecserial -o build/lib.macosx-10.8-intel-2.7/sundials.so
ld: library not found for -lsundials_cvode
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Traceback
:error: command 'clang' failed with exit status 1

Press enter to continue

This was easily solved by doing the same thing with the LDFLAGS variable as was done with the CFLAGS variable:

CFLAGS="-L/Users/charlesreid/pkg/sundails/std/lib -I/Users/charlesreid/pkg/sundials/std/include" LDFLAGS="$CFLAGS" python setup.py build

Must Be Compiled On Windows 32 Platform

creating 'dist/python-sundials-0.5.zip' and adding 'python-sundials-0.5' to it
adding 'python-sundials-0.5/COPYING.txt'
adding 'python-sundials-0.5/MANIFEST.in'
adding 'python-sundials-0.5/PKG-INFO'
adding 'python-sundials-0.5/README.txt'
adding 'python-sundials-0.5/setup.py'
adding 'python-sundials-0.5/sundials/CVODE.pxi'
adding 'python-sundials-0.5/sundials/IDA.pxi'
adding 'python-sundials-0.5/sundials/KINSOL.pxi'
adding 'python-sundials-0.5/sundials/Makefile_embedded'
adding 'python-sundials-0.5/sundials/NVECTOR.pxi'
adding 'python-sundials-0.5/sundials/sundials.c'
adding 'python-sundials-0.5/sundials/sundials.pxd'
adding 'python-sundials-0.5/sundials/SundialsLib.pxd'
adding 'python-sundials-0.5/sundials/test_cv_ball.py'
adding 'python-sundials-0.5/sundials/test_cv_simple.py'
adding 'python-sundials-0.5/sundials/test_cvRoberts_dns.py'
adding 'python-sundials-0.5/sundials/test_embedded.pyx'
adding 'python-sundials-0.5/sundials/test_idaRoberts_dns.py'
adding 'python-sundials-0.5/sundials/test_kinsol.py'
removing 'python-sundials-0.5' (and everything under it)
running bdist_wininst
Traceback
:error: distribution contains extensions and/or C libraries; must be compiled on a Windows 32 platform

This was resolved by manually editing setup.py and removing the following line:

sys.argv.append('bdist_wininst')

Usage

Test Problem

The main python-sundials website gives a test problem:

import array
import numpy as np
from sundials import *

class Ball:
    TINY = 1e-16
    def __init__(self, e = 0.7, g = 9.81):
        self.e = e
        self.g = g

    def f(self, t, y, sw):
        if sw[0]:
            return [0., 0.]
        else:
            return [-self.g, y[0]]
        
    def rootf(self, t, y, sw):
        return [y[1] + Ball.TINY]
    
    def solve(self, plot = True):
        t0 = 0
        y0 = array.array('d', [0.,1.])
        solver = CVodeSolver(RHS = self.f, ROOT = self.rootf, SW = [False],
                       abstol = 1.0e-6, reltol = 1.0e-6)

        solver.init(t0,y0)

        dt = .01
        iter = solver.iter(t0, dt)
        tres = array.array('d', [t0])
        hres = array.array('d', [y0[1]])
        while True:
            try:
                t, y = next(iter)
                tres.append(t)
                hres.append(y[1])
                    
            except CVodeRootException, info:
                if abs(info.y[0]) < 0.01:
                    solver.SW[0] = True
                    
                tres.append(info.t)
                hres.append(0.)
                
                solver.init(info.t, [-self.e*info.y[0], 0.])
            
            if t > 3.0:
                break
        
        if plot:
            import matplotlib
            import pylab

            pylab.plot(tres,hres, linewidth=1.0)
            pylab.xlabel('time (s)')
            pylab.ylabel('height (m)')
            pylab.title('Bouncing ball demo')
            pylab.grid(True)
            pylab.show()
        else:
            return tres, hres

ball = Ball()
ball.solve()