Example #1
0
def main():

    # Create the MCell world
    world = m.mcell_create()
    m.mcell_init_state(world)

    # Set iterations, step size
    dt = 1e-5
    iterations = 100
    m.mcell_set_time_step(world, dt)
    m.mcell_set_iterations(world, iterations)

    # Define volume molecule
    vm1_sym = m.create_species(world, "vm1", 1e-6, False)

    # Create a scene
    scene_name = "Scene"
    scene = m.create_instance_object(world, scene_name)

    # Create release pattern with a delay
    rel_pattern = m.create_release_pattern(world,
                                           "Release Pattern",
                                           delay=5e-4)

    # Create box objects
    box_name = "Box_Union_Outer"
    r = (0.1, 0.1, 0.1)
    ctr = (-0.3, 0.3, 0)
    box_mesh = create_rectangle(world, scene, r, ctr, box_name)

    # List of mols to release
    mol_list = m.mcell_add_to_species_list(vm1_sym, False, 0, None)

    # Release
    rel_object = m.object()
    box_rel_object = m.mcell_create_region_release(world, scene, box_mesh,
                                                   "Box Release", "ALL",
                                                   mol_list, 1000, 0, 1,
                                                   rel_pattern, rel_object)
    m.mcell_delete_species_list(mol_list)

    # Create viz data
    viz_list = m.mcell_add_to_species_list(vm1_sym, False, 0, None)
    m.mcell_create_viz_output(world, "./viz_data/test", viz_list, 0,
                              iterations, 1)

    m.mcell_init_simulation(world)
    m.mcell_init_output(world)

    output_freq = 10
    for i in range(iterations):
        m.mcell_run_iteration(world, output_freq, 0)
    m.mcell_flush_data(world)
    m.mcell_print_final_warnings(world)
    m.mcell_print_final_statistics(world)
Example #2
0
def main():
    world = m.mcell_create()
    m.mcell_init_state(world)

    dt = 1e-5
    iterations = 100
    m.mcell_set_time_step(world, dt)
    m.mcell_set_iterations(world, iterations)

    # Define one volume molecule
    vm1_sym = m.create_species(world, "vm1", 1e-6, False)
    sm1_sym = m.create_species(world, "sm1", 1e-6, True)

    scene_name = "Scene"
    scene = m.create_instance_object(world, scene_name)

    # Create box object
    box_name = "Box"
    box_mesh = m.create_box(world, scene, 1.0, box_name)

    # Release site object
    # One volume list
    (vol_rel_obj, success_vol) = m.create_list_release_site(
        world, scene, [vm1_sym, vm1_sym], [1.0, 0.0], [0.0, 0.0], [0.0, 0.0],
        "rel_name_vol")
    # One surface list
    (surf_rel_obj,
     success_surf) = m.create_list_release_site(world,
                                                scene, [sm1_sym, sm1_sym],
                                                [1.0, 0.0], [0.0, 1.0],
                                                [0.0, 0.0],
                                                "rel_name_surf",
                                                surf_flags=[True, True],
                                                orientations=[1, 1])

    # Create viz data
    viz_list = m.mcell_add_to_species_list(vm1_sym, False, 0, None)
    viz_list = m.mcell_add_to_species_list(sm1_sym, True, 1, viz_list)
    m.mcell_create_viz_output(world, "./test_release_list_viz/test", viz_list,
                              0, iterations, 1)

    m.mcell_init_simulation(world)
    m.mcell_init_output(world)

    output_freq = 1
    for i in range(iterations):
        m.mcell_run_iteration(world, output_freq, 0)
    m.mcell_flush_data(world)
    m.mcell_print_final_warnings(world)
    m.mcell_print_final_statistics(world)
Example #3
0
def create_region_release_site(world, scene, mesh, release_name, reg_name,
                               number, number_type, mol_sym):
    """Creates a release site on a specific region

    Args:
        world (object) -- the world object which has been generated by
            mcell create_instance_object
        scene (instance object) -- scene for mcell simulation
        mesh (mesh object) -- scene object where the release will occur
        release_name (string) -- name of the region release site
        reg_name (string) -- name of the region for the release site
        number (int or float) -- number to be released at the region release
            site
        number_type (int) -- 0 for NUMBER, 1 for CONCENTRATION
        mol_sym (mcell_symbol) -- species to be released

    Returns:
        release object (object)

    """

    mol_list = m.mcell_add_to_species_list(mol_sym, False, 0, None)
    rel_object = m.object()
    release_object = m.mcell_create_region_release(world, scene, mesh,
                                                   release_name,
                                                   reg_name, mol_list,
                                                   float(number), number_type,
                                                   1, None, rel_object)
    m.mcell_delete_species_list(mol_list)

    return release_object
Example #4
0
    def release_into_mesh_obj(self, relobj) -> None:
        """ Release the specified amount of species into an object. """

        mesh_obj = relobj.mesh_obj
        species = relobj.spec
        region = relobj.region

        if isinstance(species, m.OrientedSpecies):
            orient = species.orient_num
            species = species.spec
        else:
            orient = None
        if species.surface and orient is None:
            logging.info(
                "Error: must specify orientation when releasing surface "
                "molecule")
        if region:
            rel_name = "%s_%s_%s_rel" % (species.name, mesh_obj.name,
                                         region.reg_name)
        else:
            rel_name = "%s_%s_rel" % (species.name, mesh_obj.name)

        mol_sym = self._species[species.name]
        if orient:
            mol_list = m.mcell_add_to_species_list(mol_sym, True, orient, None)
        else:
            mol_list = m.mcell_add_to_species_list(mol_sym, False, 0, None)
        rel_object = m.object()
        if relobj.number:
            number_type = 0
            rel_amt = relobj.number
        elif relobj.conc:
            number_type = 1
            rel_amt = relobj.conc
        if region:
            reg_name = region.reg_name
        else:
            reg_name = "ALL"
        release_object = m.mcell_create_region_release(
            self._world, self._scene,
            self._mesh_objects[mesh_obj.name], rel_name, reg_name, mol_list,
            float(rel_amt), number_type, 1, None, rel_object)
        if rel_name not in self._releases:
            self._releases[rel_name] = release_object
        m.mcell_delete_species_list(mol_list)
Example #5
0
 def add_viz(self, species: Iterable[Species]) -> None:
     """ Set all the species in an Iterable to be visualized. """
     viz_list = None
     for spec in species:
         viz_list = m.mcell_add_to_species_list(self._species[spec.name],
                                                False, 0, viz_list)
         logging.info("Output '%s' for viz data." % spec.name)
     m.mcell_create_viz_output(self._world,
                               "./viz_data/seed_%04i/Scene" % self._seed,
                               viz_list, 0, self._iterations, 1)
Example #6
0
 def add_reaction(self, rxn: Reaction) -> None:
     """ Add a Reaction object to the simulation. """
     r_spec_list = None
     p_spec_list = None
     # Figure out if it's an iterable of reactants or a single reactant
     try:
         for r in rxn.reactants:
             if isinstance(r, m.OrientedSpecies):
                 r_sym = self._species[r.spec.name]
                 r_spec_list = m.mcell_add_to_species_list(
                     r_sym, True, r.orient_num, r_spec_list)
             else:
                 r_sym = self._species[r.name]
                 r_spec_list = m.mcell_add_to_species_list(
                     r_sym, False, 0, r_spec_list)
     except TypeError:
         r = rxn.reactants
         if isinstance(r, m.OrientedSpecies):
             r_sym = self._species[r.spec.name]
             r_spec_list = m.mcell_add_to_species_list(
                 r_sym, True, r.orient_num, r_spec_list)
         else:
             r_sym = self._species[r.name]
             r_spec_list = m.mcell_add_to_species_list(
                 r_sym, False, 0, r_spec_list)
     try:
         for p in rxn.products:
             if isinstance(r, m.OrientedSpecies):
                 p_sym = self._species[p.spec.name]
                 p_spec_list = m.mcell_add_to_species_list(
                     p_sym, True, p.orient_num, p_spec_list)
             else:
                 p_sym = self._species[p.name]
                 p_spec_list = m.mcell_add_to_species_list(
                     p_sym, False, 0, p_spec_list)
     except TypeError:
         p = rxn.products
         if isinstance(r, m.OrientedSpecies):
             p_sym = self._species[p.spec.name]
             p_spec_list = m.mcell_add_to_species_list(
                 p_sym, True, p.orient_num, p_spec_list)
         else:
             p_sym = self._species[p.name]
             p_spec_list = m.mcell_add_to_species_list(
                 p_sym, False, 0, p_spec_list)
     logging.info("Add reaction '%s' to simulation" %
                  rxn.print_friendly_repr)
     m.create_reaction(self._world,
                       r_spec_list,
                       p_spec_list,
                       rxn.rate_constant,
                       rxn.bkwd_rate_constant,
                       name=rxn.name)
Example #7
0
def create_release_site(world, scene, pos, diam, shape, number, number_type,
                        mol_sym, name):
    """Creates a molecule release site

    Args:
        world (object) -- the world object which has been generated by
            mcell create_instance_object
        scene (instance object) -- scene for mcell simulation
        pos (vector3) -- position of release site
        diam (vector3) -- diameter of release site
        number (int or float) -- number to be release at release site
        number_type (int) -- 0 for NUMBER, 1 for CONCENTRATION
        mol_sym (mcell_symbol) -- species to be released
        name (string) -- name of the release site

    Returns:
        void -- generates a species release site

    """

    position = m.vector3()
    position.x = pos.x
    position.y = pos.y
    position.z = pos.z
    diameter = m.vector3()
    diameter.x = diam.x
    diameter.y = diam.y
    diameter.z = diam.z

    mol_list = m.mcell_add_to_species_list(mol_sym, False, 0, None)
    rel_object = m.object()
    release_object = m.mcell_create_geometrical_release_site(
        world, scene, name, shape, position, diameter, mol_list, float(number),
        number_type, 1, None, rel_object)
    m.mcell_delete_species_list(mol_list)

    return (position, diameter, release_object)
Example #8
0
def main(lexer, parser):

    # Create the MCell world
    world = m.mcell_create()
    m.mcell_init_state(world)

    # Set iterations, step size
    dt = 1e-5
    iterations = 100
    m.mcell_set_time_step(world, dt)
    m.mcell_set_iterations(world, iterations)

    # Define volume molecule
    vm1_sym = m.create_species(world, "vm1", 1e-6, False)

    # Create a scene
    scene_name = "Scene"
    scene = m.create_instance_object(world, scene_name)

    ##########
    # Create each of the three binary operations
    # Each example consists of two box objects
    ##########

    # Dictionary of release evaluator objects
    rel_eval_dict = {}

    ##########
    # Union
    ##########

    # Create box objects
    box_union_outer_name = "Box_Union_Outer"
    r_union_outer = (0.1, 0.1, 0.1)
    ctr_union_outer = (-0.3, 0.3, 0)
    box_union_outer_mesh = create_rectangle(world, scene, r_union_outer,
                                            ctr_union_outer,
                                            box_union_outer_name)

    box_union_inner_name = "Box_Union_Inner"
    r_union_inner = (0.1, 0.1, 0.12)
    ctr_union_inner = (-0.3 + 0.1, 0.3 + 0.1, 0.0)
    box_union_inner_mesh = create_rectangle(world, scene, r_union_inner,
                                            ctr_union_inner,
                                            box_union_inner_name)

    # Add to the dictionary of objects for the parser to see
    lexer.obj_dict[box_union_outer_name] = box_union_outer_mesh
    lexer.obj_dict[box_union_inner_name] = box_union_inner_mesh

    # We will use the "ALL" region specification
    # We could also define a surface region using the following code
    '''
	box_union_outer_region_name = "Box_Union_Outer_Reg"
	box_union_outer_face_list = [0,1,2,3,4,5,6,7,8,9,10,11]
	box_union_outer_region = m.create_surface_region(world, box_union_outer_mesh, box_union_outer_face_list, box_union_outer_region_name)
	box_union_inner_region_name = "Box_Union_Inner_Reg"
	box_union_inner_face_list = [0,1,2,3,4,5,6,7,8,9,10,11]
	box_union_inner_region = m.create_surface_region(world, box_union_inner_mesh, box_union_inner_face_list, box_union_inner_region_name)
	'''

    # Parse a string to create a release_evaluator
    s_union = box_union_outer_name + "[ALL]" + " + " + box_union_inner_name + "[ALL]"
    print("> Parsing: " + s_union)
    lexer.mcell_world = world
    parser.parse(s_union)
    rel_eval_dict[s_union] = lexer.release_evaluator
    print("> Finished parsing.")

    ##########
    # Subtraction
    ##########

    # Create box objects
    box_sub_outer_name = "Box_Sub_Outer"
    r_sub_outer = (0.1, 0.1, 0.1)
    ctr_sub_outer = (0.3, 0.3, 0)
    box_sub_outer_mesh = create_rectangle(world, scene, r_sub_outer,
                                          ctr_sub_outer, box_sub_outer_name)

    box_sub_inner_name = "Box_Sub_Inner"
    r_sub_inner = (0.1, 0.1, 0.12)
    ctr_sub_inner = (0.3 + 0.1, 0.3 + 0.1, 0.0)
    box_sub_inner_mesh = create_rectangle(world, scene, r_sub_inner,
                                          ctr_sub_inner, box_sub_inner_name)

    # Add to the dictionary of objects for the parser to see
    lexer.obj_dict[box_sub_outer_name] = box_sub_outer_mesh
    lexer.obj_dict[box_sub_inner_name] = box_sub_inner_mesh

    # Parse a string to create a release_evaluator
    s_sub = box_sub_outer_name + "[ALL]" + " - " + box_sub_inner_name + "[ALL]"
    print("> Parsing: " + s_sub)
    lexer.mcell_world = world
    parser.parse(s_sub)
    rel_eval_dict[s_sub] = lexer.release_evaluator
    print("> Finished parsing.")

    ##########
    # Intersection
    ##########

    # Create box objects
    box_inter_outer_name = "Box_Inter_Outer"
    r_inter_outer = (0.1, 0.1, 0.1)
    ctr_inter_outer = (0.0, -0.3, 0.0)
    box_inter_outer_mesh = create_rectangle(world, scene, r_inter_outer,
                                            ctr_inter_outer,
                                            box_inter_outer_name)

    box_inter_inner_name = "Box_Inter_Inner"
    r_inter_inner = (0.1, 0.1, 0.12)
    ctr_inter_inner = (0.1, -0.3 + 0.1, 0.0)
    box_inter_inner_mesh = create_rectangle(world, scene, r_inter_inner,
                                            ctr_inter_inner,
                                            box_inter_inner_name)

    # Add to the dictionary of objects for the parser to see
    lexer.obj_dict[box_inter_outer_name] = box_inter_outer_mesh
    lexer.obj_dict[box_inter_inner_name] = box_inter_inner_mesh

    # Parse a string to create a release_evaluator
    s_inter = box_inter_outer_name + "[ALL]" + " * " + box_inter_inner_name + "[ALL]"
    print("> Parsing: " + s_inter)
    lexer.mcell_world = world
    parser.parse(s_inter)
    rel_eval_dict[s_inter] = lexer.release_evaluator
    print("> Finished parsing.")

    ##########
    # Set up all the release sites for each: union/subtraction/intersection
    ##########

    # List of mols to release
    mol_list = m.mcell_add_to_species_list(vm1_sym, False, 0, None)

    # Union
    rel_object = m.object()
    box_union_outer_rel_object = m.mcell_create_region_release_boolean(
        world, scene, "Box Union Outer Release", mol_list, 1000, 0, 1, None,
        rel_eval_dict[s_union], rel_object)

    # Subtraction
    rel_object = m.object()
    box_sub_outer_rel_object = m.mcell_create_region_release_boolean(
        world, scene, "Box Subtraction Outer Release", mol_list, 1000, 0, 1,
        None, rel_eval_dict[s_sub], rel_object)

    # Union
    mol_list = m.mcell_add_to_species_list(vm1_sym, False, 0, None)
    rel_object = m.object()
    box_inter_outer_rel_object = m.mcell_create_region_release_boolean(
        world, scene, "Box Intersection Outer Release", mol_list, 1000, 0, 1,
        None, rel_eval_dict[s_inter], rel_object)

    m.mcell_delete_species_list(mol_list)

    ##########
    # Create viz data
    ##########

    viz_list = m.mcell_add_to_species_list(vm1_sym, False, 0, None)
    m.mcell_create_viz_output(world, "./viz_data/test", viz_list, 0,
                              iterations, 1)

    m.mcell_init_simulation(world)
    m.mcell_init_output(world)

    output_freq = 10
    for i in range(iterations):
        m.mcell_run_iteration(world, output_freq, 0)
    m.mcell_flush_data(world)
    m.mcell_print_final_warnings(world)
    m.mcell_print_final_statistics(world)
if __name__ == "__main__":

    # Create pyMCell instance
    world = m.mcell_create()
    m.mcell_init_state(world)

    dt = 1e-6
    iterations = 300
    m.mcell_set_time_step(world, dt)
    m.mcell_set_iterations(world, iterations)

    # Define one surface molecule and three volume molecules
    vm1_sym = m.create_species(world, "vm1", 1e-6, False)

    # vm1 -> NULL [1e5]
    reactants2 = m.mcell_add_to_species_list(vm1_sym, False, 0, None)
    m.create_reaction(world, reactants2, None, 0.01, name="rxn")

    # Create Scene for simulation
    scene_name = "Scene"
    scene = m.create_instance_object(world, scene_name)

    # Create a spherical release site
    pos_vec3 = m.Vector3()
    diam_vec3 = m.Vector3(0.015, 0.015, 0.015)

    position, diameter, sphere_release_object = m.create_release_site(
        world, scene, pos_vec3, diam_vec3, m.SHAPE_SPHERICAL, 500, 0, vm1_sym,
        "vm1_rel")

    obj_name = "Torus"
Example #10
0
def create_list_release_site(world,
                             scene,
                             mol_list,
                             xpos,
                             ypos,
                             zpos,
                             name,
                             surf_flags=None,
                             orientations=None,
                             diameter=1e-4):
    '''
    Creates a list release site
    All is self explanatory except mol_list:
    This is a list of "mol_sym" that you get back when you create the species.
    This is a Python list - it is converted to a species list in this function
    for you. By default, assumes all molecules are volume molecules. Else, need
    to pass surf_flags=[True,True,False,...] and their orientations =
    [1,0,1,...] Diameter is the diameter we search for to place a surface mol
    It can be None (= NULL in C) but then we do a poor job of placing surface
    mols
    '''

    # Check that they're all the same length
    n = len(mol_list)
    if len(xpos) != n or len(ypos) != n or len(zpos) != n:
        raise ValueError("All lists must have the same length.")

    # Check that if there are surface molecules
    if surf_flags is not None:
        # Check that there are enough
        if len(surf_flags) != n or len(orientations) != n:
            raise ValueError(
                "surf_flags and orientations lists must have the same lengths "
                "as the others.")

    # Convert to floats (can't be int)
    xpos = [float(q) for q in xpos]
    ypos = [float(q) for q in ypos]
    zpos = [float(q) for q in zpos]

    # Diameter
    diam = m.vector3()
    diam.x = diameter
    diam.y = diameter
    diam.z = diameter

    # Mols
    mol_list.reverse()
    species_list = None
    # All volume molecules
    if surf_flags is None:
        for mol_sym in mol_list:
            species_list = m.mcell_add_to_species_list(mol_sym, False, 0,
                                                       species_list)
    else:
        for i, mol_sym in enumerate(mol_list):
            species_list = m.mcell_add_to_species_list(mol_sym, surf_flags[i],
                                                       orientations[i],
                                                       species_list)

    rel_object = m.object()
    ret = m.mcell_create_list_release_site(world, scene, name, species_list,
                                           xpos, ypos, zpos, n, diam,
                                           rel_object)
    # Delete the species list
    m.mcell_delete_species_list(species_list)

    # VERY IMPORTANT HERE - MUST RETURN "ret"
    # If we throw this away, all is lost....
    return (rel_object, ret)
Example #11
0
def create_reaction(world,
                    reactants,
                    products,
                    rate_constant,
                    backward_rate_constant=0.0,
                    surf_class=None,
                    name=None):
    """Creates a molecular reaction

    Args:
        world (object) -- the world object which has been generated by
            mcell create_instance_object
        reactants (mcell_species_list) -- list of species that are the
            reactants for the reaction
        products (mcell_species_list) -- list of species that are the
            products for the reaction
        rate_constant (double) -- the rate constant for the forward
            direction reaction -> product
        backward_rate_constant (double)(optional) -- the rate constant
            for the backward direction reaction <- product
        surf_class (mcell species list surface class)(optional) -- the
            surface class upon which the reaction will happen
        name (string)(optional) -- Name of the reaction

    Returns:
        void -- creates a reaction, by generating reaction_rates
            structure

    """

    if surf_class:
        # Do nothing, surf_class has been added and a null object is not needed
        pass
    else:
        surf_class = m.mcell_add_to_species_list(None, False, 0, None)

    arrow = m.reaction_arrow()
    # reversible reaction e.g. A<->B
    if backward_rate_constant:
        arrow.flags = m.ARROW_BIDIRECTIONAL
        rate_constant = m.mcell_create_reaction_rates(m.RATE_CONSTANT,
                                                      rate_constant,
                                                      m.RATE_CONSTANT,
                                                      backward_rate_constant)
    # irreversible reaction e.g. A->B
    else:
        arrow.flags = m.REGULAR_ARROW
        rate_constant = m.mcell_create_reaction_rates(m.RATE_CONSTANT,
                                                      rate_constant,
                                                      m.RATE_UNSET, 0)
    arrow.catalyst = m.mcell_species()
    arrow.catalyst.next = None
    arrow.catalyst.mol_type = None
    arrow.catalyst.orient_set = 0
    arrow.catalyst.orient = 0

    if (name):
        name_sym = m.mcell_new_rxn_pathname(world, name)
    else:
        name_sym = None
    m.mcell_add_reaction_simplified(world, reactants, arrow, surf_class,
                                    products, rate_constant, name_sym)