Пример #1
0
    def test_constructor(self):
        # Create a Reaction object
        r = pystospa.Reaction(0.0, lambda x, y: 1.0, [0])

        # Check that Reaction object attributes have been set correctly
        self.assertEqual(r.get_rate(), 0)
        self.assertEqual(r.get_propensity([10], 1.0), 0.0)
Пример #2
0
    def test_constructor(self):

        # Create a Simulator object
        v = pystospa.Voxel([10], 1.0)
        v.add_reaction(pystospa.Reaction(1.5, lambda x, y: x[0], [-1]))
        s = pystospa.Simulator([v])

        # Check that the Simulator attributes are as expected
        self.assertEqual(s.get_time(), 0.0)
        self.assertEqual(s.get_voxels()[0].get_molecules()[0], 10)
Пример #3
0
    def test_member_functions(self):
        # Create a Reaction object
        r = pystospa.Reaction(0.0, lambda x, y: 1.0, [0])

        # Check that reaction rate can be changed
        r.set_rate(1.55)
        self.assertEqual(r.get_rate(), 1.55)

        # Check that the correct propensity is returned
        self.assertEqual(r.get_propensity([10], 1.0), 1.55)
Пример #4
0
    def test_member_functions(self):

        # Create a Voxel object
        v = pystospa.Voxel([10], 1.0)
        r = pystospa.Reaction(1.5, lambda x, y: x[0], [-1])

        # Check that a Reaction object has been added correctly
        v.add_reaction(r)
        rs = v.get_reactions()
        self.assertEqual(len(rs), 1)
        self.assertEqual(rs[0].get_rate(), 1.5)
        self.assertEqual(v.get_total_propensity(), 15)
Пример #5
0
    def test_member_functions(self):

        # Create a Simulator object
        v = pystospa.Voxel([10], 1.0)
        v.add_reaction(pystospa.Reaction(1.5, lambda x, y: x[0], [-1]))
        s = pystospa.Simulator([v])

        # Check setting seed for generating random number
        s.set_seed(153)
        self.assertEqual(s.get_seed(), 153)

        # Check a single step in SSA works
        s.step()
        self.assertGreater(s.get_time(), 0)
        self.assertEqual(s.get_voxels()[0].get_molecules()[0], 9)

        # Check that getting to specific time point works
        s.advance(1.0)
        self.assertGreater(s.get_time(), 1.0)
Пример #6
0
import matplotlib.pyplot as plt
import numpy as np
import pystospa as ss

# Create voxel object. Arguments: vector of number of molecules, size of the voxel
initial_num = [100]  # number of molecules of species A
domain_size = 10.0  # size of the domain in cm
v = ss.Voxel(initial_num, domain_size)

# Create reaction object.
# Arguments: reaction rate, propensity func, stoichiometry vector
k = 1.0
propensity = lambda num_mols, area: num_mols[0]
stoch = [-1]
r = ss.Reaction(k, propensity, stoch)

# Add a reaction to a voxel
v.add_reaction(r)

# Pass the voxel with the reaction(s) to the simulator object
s = ss.Simulator([v])
# Run the simulation. Arguments: path to output file, time step, number of steps
s.run("cme_example.dat", 0.01, 500)

# Read the file containing the data
data = np.loadtxt("cme_example.dat")
time = data[:, 0]  # Time points
num_A = data[:, 1]  # Number of molecules of A

# Plot of the data
Пример #7
0
# Create a vector of voxel objects. Voxel arguments: vector of number of molecules, size of the voxel
initial_num = [10000]
voxel_size = 1.0
domain = [ss.Voxel(initial_num, voxel_size)]
# We add nine voxels with no molecules
for i in range(9):
    domain.append(ss.Voxel([0], voxel_size))

# Create and add the reaction objects
d = 1.0  # diffusion rate
k = 1.0  # decay rate
propensity = lambda num_mols, length : num_mols[0]
stoch = [-1]
for i, vox in enumerate(domain):
    # Add diffusion jump to the right from voxel i to voxel i+1 (if one exists)
    if i < len(domain)-1: vox.add_reaction(ss.Reaction(d, propensity, stoch, i+1))
    # Add diffusion jump to the left from voxel i+1 to voxel i (if one exists)
    if i > 0: vox.add_reaction(ss.Reaction(d, propensity, stoch, i-1))
    vox.add_reaction(ss.Reaction(k, propensity, stoch))

# Pass the voxels with the reaction(s) to the simulator object
s = ss.Simulator(domain)

# Run the simulation. Arguments: path to output file, time step, number of steps
s.run("rdme_example_2.dat", 0.01, 200)

# Read the file containing the data
data = np.loadtxt("rdme_example_2.dat")
time = data[:, 0]
num_A = data[:, 1:]
x = np.arange(0.5, 10.5)
Пример #8
0
import matplotlib.pyplot as plt
import numpy as np
import pystospa as ss


# Create voxel object. Arguments: vector of number of molecules, size of the voxel
initial_num = [100, 50, 20]  # number of molecules of species A, B and C respectively
domain_size = 10.0  # size of the domain in cm
v = ss.Voxel(initial_num, domain_size)

# Create reaction object for reaction A + B -> C.
# Arguments: reaction rate, propensity func, stoichiometry vector
k1 = 0.1
propensity1 = lambda num_mols, area : num_mols[0]*num_mols[1]/area
stoch1 = [-1, -1, 1]
r1 = ss.Reaction(k1, propensity1, stoch1)

# Create reaction object for reaction B + 2C -> 0. Arguments: reaction rate, propensity func, stoichiometry vector
k2 = 10.0
propensity2 = lambda num_mols, area : num_mols[1] * num_mols[2] * (num_mols[2] - 1) / (area**2)
stoch2 = [0, -1, -2]
r2 = ss.Reaction(k2, propensity2, stoch2)

# Add a reaction to the voxel
v.add_reaction(r1)
v.add_reaction(r2)

# Pass the voxel with the reaction(s) to the simulator object
s = ss.Simulator([v])

# Run the simulation. Arguments: path to output file, time step, number of steps
Пример #9
0
# Create a vector of voxel objects. Voxel arguments: vector of number of molecules, size of the voxel
initial_num = [10000]
voxel_size = 1.0
growth = lambda t: np.exp(0.2 * t)
domain = [ss.Voxel(initial_num, voxel_size, growth)]
# We add nine voxels with no molecules
for i in range(9):
    domain.append(ss.Voxel([0], voxel_size, growth))

# Create and add the reaction objects
d = 1.0  # diffusion rate
propensity = lambda num_mols, length: num_mols[0]
stoch = [-1]
for i in range(9):
    # Add diffusion jump to the right from voxel i to voxel i+1
    domain[i].add_reaction(ss.Reaction(d, propensity, stoch, i + 1))
    # Add diffusion jump to the left from voxel i+1 to voxel i
    domain[i + 1].add_reaction(ss.Reaction(d, propensity, stoch, i))

# Pass the voxels with the reaction(s) to the simulator object
s = ss.Simulator(domain)

# Run the simulation. Arguments: path to output file, time step, number of steps
s.run("rdme_example_4.dat", 0.01, 200)

# Read the file containing the data
data = np.loadtxt("rdme_example_4.dat")
data_static = np.loadtxt("rdme_example.dat")
time = data[:, 0]
num_A = data[:, 1:]
num_A_static = data_static[:, 1:]
Пример #10
0
for i in range(9):
    domain.append(ss.Voxel([0, 0], voxel_size))

# Create and add the reaction objects
d = 1.0  # diffusion rate
k = 0.001
prop_diff_A = lambda num_mols, length : num_mols[0]
prop_diff_B = lambda num_mols, length : num_mols[1]
prop_other = lambda num_mols, length : num_mols[0] * (num_mols[0] - 1) / length
stoch_diff_A = [-1, 0]
stoch_diff_B = [0, -1]
stoch_other = [-2, 1]
for i, vox in enumerate(domain):
    # Add diffusion jump to the right from voxel i to voxel i+1 (if one exists)
    if i < len(domain)-1:
        vox.add_reaction(ss.Reaction(d, prop_diff_A, stoch_diff_A, i+1))
        vox.add_reaction(ss.Reaction(d, prop_diff_B, stoch_diff_B, i+1))
    # Add diffusion jump to the left from voxel i+1 to voxel i (if one exists)
    if i > 0:
        vox.add_reaction(ss.Reaction(d, prop_diff_A, stoch_diff_A, i-1))
        vox.add_reaction(ss.Reaction(d, prop_diff_B, stoch_diff_B, i-1))
    vox.add_reaction(ss.Reaction(k, prop_other, stoch_other))

# Pass the voxels with the reaction(s) to the simulator object
s = ss.Simulator(domain)

# Run the simulation. Arguments: path to output file, time step, number of steps
s.run("rdme_example_3.dat", 0.01, 200)

# Read the file containing the data
data = np.loadtxt("rdme_example_3.dat")