MAINT: in integrate.ode, generate understandable error for f2py issue. #395

Merged
merged 1 commit into from Feb 5, 2013

Projects

None yet

2 participants

@rgommers
SciPy member

Closes ticket 1187. Note that this cannot be tested without a warning message
being printed by vodemodule.c, even when redirecting stderr::

import sys
from scipy import integrate

class RedirectStdStreams(object):
    def __init__(self, stdout=None, stderr=None):
        self._stdout = stdout or sys.stdout
        self._stderr = stderr or sys.stderr

    def __enter__(self):
        self.old_stdout, self.old_stderr = sys.stdout, sys.stderr
        self.old_stdout.flush(); self.old_stderr.flush()
        sys.stdout, sys.stderr = self._stdout, self._stderr

    def __exit__(self, exc_type, exc_value, traceback):
        self._stdout.flush(); self._stderr.flush()
        sys.stdout = self.old_stdout

y0, t0 = [1.0j, 2.0], 0
def f(t, y, arg1):
    return (1j*arg1*y[0] + y[1], -arg1*y[1]**2)

def jac(t, y, arg1):
    return [[1j*arg1, 1], [0, -arg1*2*y[1]]]

with RedirectStdStreams():
    r = integrate.ode(f, jac).set_integrator('zvode', method='bdf', with_jacobian=True)
    r.set_initial_value(y0, t0).set_f_params(2.0).set_jac_params(2.0)
    t1 = 10
    dt = 1
    while r.successful() and r.t < t1:
        r.integrate(r.t+dt)

For the f2py change suggested by Pearu, a numpy ticket should be opened.

@rgommers rgommers MAINT: in integrate.ode, generate understandable error for f2py issue.
Closes ticket 1187.  Note that this cannot be tested without a warning message
being printed by vodemodule.c, even when redirecting stderr::

    import sys
    from scipy import integrate

    class RedirectStdStreams(object):
        def __init__(self, stdout=None, stderr=None):
            self._stdout = stdout or sys.stdout
            self._stderr = stderr or sys.stderr

        def __enter__(self):
            self.old_stdout, self.old_stderr = sys.stdout, sys.stderr
            self.old_stdout.flush(); self.old_stderr.flush()
            sys.stdout, sys.stderr = self._stdout, self._stderr

        def __exit__(self, exc_type, exc_value, traceback):
            self._stdout.flush(); self._stderr.flush()
            sys.stdout = self.old_stdout

    y0, t0 = [1.0j, 2.0], 0
    def f(t, y, arg1):
        return (1j*arg1*y[0] + y[1], -arg1*y[1]**2)

    def jac(t, y, arg1):
        return [[1j*arg1, 1], [0, -arg1*2*y[1]]]

    with RedirectStdStreams():
        r = integrate.ode(f, jac).set_integrator('zvode', method='bdf', with_jacobian=True)
        r.set_initial_value(y0, t0).set_f_params(2.0).set_jac_params(2.0)
        t1 = 10
        dt = 1
        while r.successful() and r.t < t1:
            r.integrate(r.t+dt)
d567f79
@rgommers
SciPy member

This one is quite straightforward. Any objection to merging it?

@pv pv merged commit 96b3dbe into scipy:master Feb 5, 2013
@pv
SciPy member
pv commented Feb 5, 2013

Merged.

@rgommers rgommers deleted the rgommers:ticket-1187-ode-error branch Feb 5, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment