Esempio n. 1
0
    def test_redirect_output(self):
        """ Test to make sure we can turn output redirection on and off """
        
        # By default output redirection is off, so we begin by doing an example
        # that should generate errors and making sure that no output is received.
        redir = Utility.Redirector()
        redir.start()
        # This example will generate errors because the maximum number of steps
        # (500) will be passed
        y, t, ypout, t_root, y_root, i_root = daeint(trig_res_func, tlist_trig,
                                                     y0_trig, yp0_trig,
                                                     rtol = reltol_trig,
                                                     atol = abstol_trig,
                                                     max_steps = 7500)
        messages = redir.stop()
        self.assertEqual(len(messages), 0)

        redir = Utility.Redirector()
        redir.start()
        # Now we do the same example again with output redirection off
        y, t, ypout, t_root, y_root, i_root = daeint(trig_res_func, tlist_trig,
                                                     y0_trig, yp0_trig,
                                                     rtol = reltol_trig,
                                                     atol = abstol_trig,
                                                     max_steps = 7500,
                                                     redir_output = False)

        messages = redir.stop()
        self.assertNotEqual(len(messages), 0)
Esempio n. 2
0
import unittest
import os

import scipy
import SloppyCell.Utility as Utility
from SloppyCell.ReactionNetworks import *

redir = Utility.Redirector()

# Load the stoichiometryMathML example from the SBML semantic test suite.
# To avoid extra dependencies on libsbml, we use verions built by SloppyCell.
from StoichTestNets import stoichMath_net

tlist_stoichMath_net = scipy.array([0] + [0.04 * x for x in range(1, 51)])


class test_StoichiometryMath(unittest.TestCase):
    def test_basic(self):
        """ Basic test of stoichiometry math """
        stoichMath_traj = Dynamics.integrate(stoichMath_net,
                                             tlist_stoichMath_net,
                                             rtol=scipy.array([1e-7, 1e-7]))

        self.assertAlmostEqual(stoichMath_traj.get_var_val('A', 0.32),
                               0.62283185811441, 6)
        self.assertAlmostEqual(stoichMath_traj.get_var_val('A', 1.28),
                               0.0129772159879822, 6)
        self.assertAlmostEqual(stoichMath_traj.get_var_val('A', 1.96),
                               0.000623972556903647, 6)
        self.assertAlmostEqual(stoichMath_traj.get_var_val('B', 0.16),
                               0.153505642887153, 6)
Esempio n. 3
0
def find_ics(y, yp, time, var_types, rtol, atol, constants, net, 
             redirect_msgs=False):
    # We use this to find consistent sets of initial conditions for our
    #  integrations. (We don't let ddaskr do it, because it doesn't calculate
    #  values for d(alg_var)/dt, and we need them for sensitivity integration.)

    # On some systems, the f2py'd functions don't like len(constants)=0.
    if len(constants) == 0:
        constants = [0]
    var_types = np.asarray(var_types)
    atol = np.asarray(atol)
    rtol = np.asarray(rtol)
    # Note that we're copying y and yprime
    y = np.array(y, scipy.float_)
    yp = np.array(yp, scipy.float_)

    N_alg = np.sum(var_types == -1)

    dv_typ_vals = np.asarray([net.get_var_typical_val(id)
                                 for id in list(net.dynamicVars.keys())])

    if N_alg:
        # First we calculate a consistent set of algebraic variable values
        alg_vars_guess = y[var_types == -1]
        alg_typ_vals = dv_typ_vals[var_types == -1]
        possible_guesses = [alg_vars_guess, alg_typ_vals, 
                            np.ones(N_alg, scipy.float_)]
        redir = Utility.Redirector()
        if redirect_msgs:
            redir.start()
        try:
            for guess in possible_guesses:
                sln, infodict, ier, mesg = \
                        scipy.optimize.fsolve(net.alg_res_func, x0 = guess,
                                              xtol = min(rtol), 
                                              args = (y, time, constants),
                                              full_output=True)
                sln = np.atleast_1d(sln)
                final_residuals = net.alg_res_func(sln, y, time, constants)
                if not np.any(abs(final_residuals)
                                 > abs(atol[var_types == -1])):
                    # This is success.
                    break
            else:
                message = ('Failed to calculate consistent algebraic values in '
                           'network %s.' % net.get_id())
                raise Utility.SloppyCellException(message)
        finally:
            messages = redir.stop()

        # Now plug those values into the current y
        y[var_types == -1] = sln
    # The non-algebraic variable yprimes come straight from the residuals
    yp_non_alg = net.res_function(time, y, y*0, constants)[var_types == 1]
    yp[var_types == 1] = yp_non_alg
                                              
    if not N_alg:
        return y, yp

    # Now we need to figure out yprime for the algebraic vars
    curr_alg_yp = yp[var_types == -1]
    ones_arr = np.ones(N_alg, scipy.float_)
    # We try a range of possible guesses. Note that this is really just
    # a linear system, so we continue to have difficulties with this part of
    # the calculation, or if it becomes a slow-down, we should consider doing
    # it by a linear solve, rather than using fsolve.
    possible_guesses = [curr_alg_yp, alg_typ_vals, 
                        ones_arr,
                        np.mean(abs(yp)) * ones_arr,
                        -np.mean(abs(yp)) * ones_arr,
                        max(abs(yp)) * ones_arr, 
                        -max(abs(yp)) * ones_arr]
    if redirect_msgs:
        redir.start()
    try:
        for guess in possible_guesses:
            sln, infodict, ier, mesg = \
                    scipy.optimize.fsolve(net.alg_deriv_func, x0 = guess,
                                          xtol = min(rtol),
                                          args = (y, yp, time, constants),
                                          full_output=True)
            sln = np.atleast_1d(sln)
            final_residuals = net.alg_deriv_func(sln, y, yp, time, constants)
            if not np.any(abs(final_residuals) > abs(atol[var_types == -1])):
                break
        else:
            raise Utility.SloppyCellException('Failed to calculate alg var '\
                                              'derivatives in network %s.' 
                                              % net.get_id())
    finally:
        messages=redir.stop()
    sln = np.atleast_1d(sln)
    yp[var_types == -1] = sln

    return y, yp