def setup_reaction_simulator(self,
                                 simulation_type,
                                 abundances,
                                 temperature,
                                 t_span,
                                 dt=0.01,
                                 system_volume=1e-15):
        """Build a simulator instance for reaction simulations.

        Parameters
        ----------
        simulation_type : str
            Type of simulation. "deterministic" or "stochastic"
        abundances : array_like
            Abundances of all species
        temperature : float
            Simulation temperature
        t_span : 2-tuple
            Time span of the reactions users want to simulate, from `t_span[0]`
            to ``t_span[1]``.
        dt : float
            Size of time steps users want to simulate. Relevant
            only for deterministic simulations.
        system_volume : float
            Volume of reaction system. Relevant only for stochastic
            simulations.

        Returns
        -------
        simulator : `ReactionSimulator`
            `ReactionSimulator` instance. Either child class
            `DeterministicSimulator` or `StochasticSimulator`.

        Examples
        --------
        >>> reader = XMLReader("tests/rxns.xml")
        >>> reaction_system = reader.get_reaction_systems()[0]
        >>> concs = np.array([1., 2., 1., 3., 1.])*1e-05
        >>> det_sim = reaction_system.setup_reaction_simulator(
        ... 'deterministic', concs, 800, [0, 0.1], dt=0.1)
        >>> det_sim.simulate()
        ([0, 0.1], [array([  1.00000000e-05,   2.00000000e-05,   1.00000000e-05,
                 3.00000000e-05,   1.00000000e-05]), array([  1.31232803e-05,   1.68767197e-05,   1.33004078e-05,
                 2.67881559e-05,   9.91143627e-06])])
        """
        choices = ['stochastic', 'deterministic']
        if simulation_type not in choices:
            raise ValueError

        if simulation_type == 'deterministic':
            determine_sim = simulator.DeterministicSimulator(
                self, abundances, temperature, t_span, dt)
            return determine_sim
        else:
            stochastic_sim = simulator.StochasticSimulator(
                self, abundances, temperature, t_span, system_volume)
            return stochastic_sim
def test_neg_temp():
    concs = [1., 2., 1., 3., 1.]
    reader = chemkin.XMLReader("tests/rxns.xml")
    reaction_system = reader.get_reaction_systems()[0]
    try:
        simulator.DeterministicSimulator(reaction_system,
                                         concs,
                                         -800, [0, 2],
                                         dt=1)
    except ValueError as err:
        assert (type(err) == ValueError)
def test_solver_not_implemented():
    concs = np.array([1., 2., 1., 3., 1.]) * 1e-05
    reader = chemkin.XMLReader("tests/rxns.xml")
    reaction_system = reader.get_reaction_systems()[0]
    det_sim = simulator.DeterministicSimulator(reaction_system,
                                               concs,
                                               800, [0, 0.01],
                                               dt=0.01)
    try:
        det_sim.simulate('forward_euler')
    except ValueError as err:
        assert (type(err) == ValueError)
def test_sim_rk45_neg_concentration():
    concs = np.array([1., 2., 1., 3., 1.]) * 1e-05
    reader = chemkin.XMLReader("tests/rxns.xml")
    reaction_system = reader.get_reaction_systems()[0]
    det_sim = simulator.DeterministicSimulator(reaction_system,
                                               concs,
                                               800, [0, 0.01],
                                               dt=0.01)
    try:
        det_sim.simulate('rk45')
    except ValueError as err:
        assert (type(err) == ValueError)
def test_sim_backward_euler():
    concs = np.array([1., 2., 1., 3., 1.]) * 1e-05
    reader = chemkin.XMLReader("tests/rxns.xml")
    reaction_system = reader.get_reaction_systems()[0]
    det_sim = simulator.DeterministicSimulator(reaction_system,
                                               concs,
                                               800, [0, 0.01],
                                               dt=0.01)
    t, y = det_sim.simulate('backward_euler')
    y0 = y[0]
    y1 = y[1]
    assert np.allclose(t, [0, 0.01])
    assert np.allclose(
        y0,
        np.array([
            1.00000000e-05, 2.00000000e-05, 1.00000000e-05, 3.00000000e-05,
            1.00000000e-05
        ]))
    assert np.allclose(
        y1,
        np.array([
            1.03940908e-05, 1.96059092e-05, 1.04083239e-05, 2.95987926e-05,
            9.99288345e-06
        ]))