Beispiel #1
0
    def test_init(self):
        def dy_dt(t, y):
            return 0.0

        solver = ode_solver.Solver(dy_dt, y_0=1.0)
        assert solver is not None
        assert solver.dy_dt == dy_dt
Beispiel #2
0
    def test_solve_scalar_problem_1(self):
        def dy_dt(t, y):
            return 0.0

        y_0 = 11.0

        solver = ode_solver.Solver(dy_dt, y_0)

        time_steps = numpy.linspace(0.0, 10.0, 11)
        for t in time_steps:
            solver.step(t)
            assert solver.y == y_0
            assert solver.t == t
Beispiel #3
0
    def test_solve_scalar_problem_2(self):
        def dy_dt(t, y):
            return 1.0

        t_0 = 0.0
        y_0 = 15.0

        solver = ode_solver.Solver(dy_dt, y_0)

        time_steps = numpy.linspace(0.0, 10.0, 11)
        for t in time_steps:
            solver.step(t)
            numpy.testing.utils.assert_almost_equal(solver.y, y_0 + t)
            assert solver.t == t
Beispiel #4
0
    def test_prevent_write_to_y(self):
        def dy_dt(t, y):
            return numpy.ones(numpy.shape(y))

        y_0 = numpy.zeros((4, ))

        solver = ode_solver.Solver(dy_dt, y_0)

        time_steps = numpy.linspace(0.0, 10.0, 11)
        for t in time_steps:
            solver.step(t)
            assert solver.t == t
            y = solver.y
            numpy.testing.utils.assert_almost_equal(y, y_0 + t)
            # this operation should not corrupt the solver state ...
            y[:] = (-1.0e7, 1.0e4, 1.0e-4, 1.0e5)
Beispiel #5
0
 def test_prevent_backwards_steps(self):
     def dy_dt(t, y):
         return numpy.ones(numpy.shape(y))
     
     t_0 = 3.11
     y_0 = numpy.zeros((4,))
     
     solver = ode_solver.Solver(dy_dt, y_0, t_0)
     
     time_steps = numpy.linspace(0.0, 10.0, 11)
     for t in time_steps:
         if t < t_0:
             self.assertRaises(ValueError, solver.step, t)
         else:
             solver.step(t)
             assert solver.t == t
             numpy.testing.utils.assert_almost_equal(solver.y, y_0+(t-t_0))
Beispiel #6
0
 def test_solve_wrapped_problem(self):
     
     shape = (10, 10)
     
     def dz_dt(t, z):
         return numpy.ones(shape)
     
     z_0 = -3.0*numpy.ones(shape)
     
     solver = ode_solver.Solver(dz_dt, z_0)
     
     def pack(z):
         return numpy.ravel(z)
     def unpack(y):
         return numpy.reshape(y, shape)
     
     solver.set_packing(pack, unpack)
     
     time_steps = numpy.linspace(0.0, 10.0, 11)
     for t in time_steps:
         solver.step(t)
         assert solver.t == t
         numpy.testing.utils.assert_almost_equal(solver.y, z_0 + t)
Beispiel #7
0
    def test_init_set_inital_values_read_only(self):
        def dy_dt(t, y):
            return 0.0

        t_0 = 1.0
        y_0 = -3.0
        solver = ode_solver.Solver(dy_dt, y_0, t_0)
        assert solver is not None
        assert solver.dy_dt == dy_dt
        assert solver.t == t_0
        assert solver.y == y_0

        def externally_modify_solver_dy_dt():
            solver.dy_dt = None

        def externally_modify_solver_t():
            solver.t = 12345

        def externally_modify_solver_y():
            solver.y = 'banana'

        self.assertRaises(AttributeError, externally_modify_solver_dy_dt)
        self.assertRaises(AttributeError, externally_modify_solver_t)
        self.assertRaises(AttributeError, externally_modify_solver_y)
Beispiel #8
0
def create(model,
           sink,
           p_0=None,
           t_0=None,
           sink_0=None,
           time_dependencies=None,
           domain_states=None):
    """
    Returns a solver for the Chemical Master Equation of the given model.
    
    arguments:
    
        model : the CME model to solve
        
        sink : If sink is True, the solver will include a 'sink' state used
            to accumulate any probability that may flow outside the domain.
            This can be used to measure the error in the solution due to
            truncation of the domain. If sink is False, the solver will not
            include a 'sink' state, and probability will be artificially
            prevented from flowing outside of the domain.
        
        p_0 : (optional) mapping from states in the domain to probabilities,
            for the initial probability distribution. If not specified,
            and the initial state of the state space is given by the model,
            defaults to all probability concentrated at the initial state,
            otherwise, a ValueError will be raised.
        
        t_0 : (optional) initial time, defaults to 0.0
        
        sink_0 : (optional) initial sink probability, defaults to 0.0
            Only a valid argument if sink is set to True.
        
        time_dependencies : (optional) By default, reaction propensities are
            time independent. If specified, time_dependencies must be of the
            form { s_1 : phi_1, ..., s_n : phi_n }, where each (s_j, phi_j)
            item satisifes :
            
                s_j : set of reaction indices
                phi_j : phi_j(t) -> time dependent coefficient 
            
            The propensities of the reactions with indicies contained in s_j
            will all be multiplied by the coefficient phi_j(t), at time t.
            Reactions are indexed according to the ordering of the propensities
            in the model.
            
            The reaction index sets s_j must be *disjoint*. It is not necessary
            for the union of the s_j to include all the reaction indices.
            If a reaction's index is not contained in any s_j then the reaction
            is treated as time-independent. 
        
        mapping of time dependent coefficient
            functions keyed by subsets of reaction indices, with respect to the
            ordering of reactions determined by the order of the propensity
            functions inside the model. The propensities of the reactions
            with indices included in each subset are multiplied by the time
            dependent coefficient function. By default, no time dependent
            coefficient functions are specified, that is, the CME has
            time-independent propensities.
        
        domain_states : (optional) array of states in the domain.
            By default, generate the rectangular lattice of states defined by
            the 'shape' entry of the model. A ValueError is raised if both
            domain_states and 'shape' are unspecified.
    """

    mdl.validate_model(model)

    if sink_0 is not None:
        if not sink:
            raise ValueError('sink_0 may not be specified if sink is False')
        sink_0 = float(sink_0)
    else:
        sink_0 = 0.0

    # determine states in domain, then construct an enumeration of the
    # domain states
    if domain_states is None:
        if mdl.SHAPE not in model:
            lament = 'if no states given, model must contain key \'%s\''
            raise KeyError(lament % mdl.SHAPE)
        else:
            domain_states = domain.from_rect(shape=model.shape)

    domain_enum = state_enum.create(domain_states)

    # determine p_0, then construct a dense representation with respect to
    # the domain enumeration
    initial_state = model.get(mdl.INITIAL_STATE, None)
    if p_0 is None:
        if initial_state is None:
            lament = 'if no p_0 given, model must contain key \'%s\''
            raise ValueError(lament % mdl.INITIAL_STATE)
        else:
            p_0 = {initial_state: 1.0}

    if t_0 is None:
        t_0 = 0.0

    member_flags = domain_enum.contains(domain.from_iter(p_0))
    if not numpy.logical_and.reduce(member_flags):
        raise ValueError('support of p_0 is not a subset of domain_states')

    # compute reaction matrices and use them to define differential equations
    gen_matrices = cme_matrix.gen_reaction_matrices(model, domain_enum, sink,
                                                    cme_matrix.non_neg_states)
    reaction_matrices = list(gen_matrices)
    dy_dt = cme_matrix.create_diff_eqs(reaction_matrices,
                                       phi=time_dependencies)

    # construct and initialise solver
    if sink:
        cme_solver = ode_solver.Solver(dy_dt, y_0=(p_0, sink_0), t_0=t_0)
        pack, unpack = create_packing_functions(domain_enum)
        cme_solver.set_packing(pack, unpack, transform_dy_dt=False)
    else:
        pack = domain_enum.pack_distribution
        unpack = domain_enum.unpack_distribution
        cme_solver = ode_solver.Solver(dy_dt, y_0=p_0, t_0=t_0)
        cme_solver.set_packing(pack, unpack, transform_dy_dt=False)
    return cme_solver