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
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)
def create_instance_object(world, name): """Creates an instance object. Simple translation from wrapped code to python function. Frees the user from having to initialize the scene object and then pass it in and generate the object. Args: world (object) -- the world object which has been generated by mcell create_instance_object name (string) -- name of the instance object Returns: instance object """ scene_temp = m.object() return m.mcell_create_instance_object(world, name, scene_temp)
def create_polygon_object(world, vert_list, face_list, scene, name, translation=None): """Creates a polygon object from a vertex and element lest Args: world (object) -- the world object which has been generated by mcell create_instance_object vert_list (vertex list) -- verteces for the polygon face_list (element connection list) -- faces for the polygon scene (instance object) -- scene for mcell simulation name (string) -- name of polygon object that will be created Returns: polygon object """ verts = None vert_list = vert_list[::-1] t = translation if t: for x, y, z in vert_list: verts = m.mcell_add_to_vertex_list(x + t[0], y + t[1], z + t[2], verts) else: for x, y, z in vert_list: verts = m.mcell_add_to_vertex_list(x, y, z, verts) elems = None face_list = face_list[::-1] for x, y, z in face_list: elems = m.mcell_add_to_connection_list(x, y, z, elems) pobj = m.poly_object() pobj.obj_name = name pobj.vertices = verts pobj.num_vert = len(vert_list) pobj.connections = elems pobj.num_conn = len(face_list) mesh_temp = m.object() mesh = m.mcell_create_poly_object(world, scene, pobj, mesh_temp) return mesh
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)
def create_box(world, scene, half_length, name): """Creates the verteces and lines of a cube object at the origin Args: half_length (double) -- half length of the cube Returns: vertex list and element connection list """ hl = half_length verts = m.mcell_add_to_vertex_list(hl, hl, -hl, None) verts = m.mcell_add_to_vertex_list(hl, -hl, -hl, verts) verts = m.mcell_add_to_vertex_list(-hl, -hl, -hl, verts) verts = m.mcell_add_to_vertex_list(-hl, hl, -hl, verts) verts = m.mcell_add_to_vertex_list(hl, hl, hl, verts) verts = m.mcell_add_to_vertex_list(hl, -hl, hl, verts) verts = m.mcell_add_to_vertex_list(-hl, -hl, hl, verts) verts = m.mcell_add_to_vertex_list(-hl, hl, hl, verts) elems = m.mcell_add_to_connection_list(1, 2, 3, None) elems = m.mcell_add_to_connection_list(7, 6, 5, elems) elems = m.mcell_add_to_connection_list(0, 4, 5, elems) elems = m.mcell_add_to_connection_list(1, 5, 6, elems) elems = m.mcell_add_to_connection_list(6, 7, 3, elems) elems = m.mcell_add_to_connection_list(0, 3, 7, elems) elems = m.mcell_add_to_connection_list(0, 1, 3, elems) elems = m.mcell_add_to_connection_list(4, 7, 5, elems) elems = m.mcell_add_to_connection_list(1, 0, 5, elems) elems = m.mcell_add_to_connection_list(2, 1, 6, elems) elems = m.mcell_add_to_connection_list(2, 6, 3, elems) elems = m.mcell_add_to_connection_list(4, 0, 7, elems) pobj = m.poly_object() pobj.obj_name = name pobj.vertices = verts pobj.num_vert = 8 pobj.connections = elems pobj.num_conn = 12 mesh_temp = m.object() mesh = m.mcell_create_poly_object(world, scene, pobj, mesh_temp) return mesh
def create_rectangle(world, scene, r, ctr, name): xp = ctr[0] + r[0] yp = ctr[1] + r[1] zp = ctr[2] + r[2] xm = ctr[0] - r[0] ym = ctr[1] - r[1] zm = ctr[2] - r[2] verts = m.mcell_add_to_vertex_list(xp, yp, zm, None) verts = m.mcell_add_to_vertex_list(xp, ym, zm, verts) verts = m.mcell_add_to_vertex_list(xm, ym, zm, verts) verts = m.mcell_add_to_vertex_list(xm, yp, zm, verts) verts = m.mcell_add_to_vertex_list(xp, yp, zp, verts) verts = m.mcell_add_to_vertex_list(xp, ym, zp, verts) verts = m.mcell_add_to_vertex_list(xm, ym, zp, verts) verts = m.mcell_add_to_vertex_list(xm, yp, zp, verts) elems = m.mcell_add_to_connection_list(1, 2, 3, None) elems = m.mcell_add_to_connection_list(7, 6, 5, elems) elems = m.mcell_add_to_connection_list(0, 4, 5, elems) elems = m.mcell_add_to_connection_list(1, 5, 6, elems) elems = m.mcell_add_to_connection_list(6, 7, 3, elems) elems = m.mcell_add_to_connection_list(0, 3, 7, elems) elems = m.mcell_add_to_connection_list(0, 1, 3, elems) elems = m.mcell_add_to_connection_list(4, 7, 5, elems) elems = m.mcell_add_to_connection_list(1, 0, 5, elems) elems = m.mcell_add_to_connection_list(2, 1, 6, elems) elems = m.mcell_add_to_connection_list(2, 6, 3, elems) elems = m.mcell_add_to_connection_list(4, 0, 7, elems) pobj = m.poly_object() pobj.obj_name = name pobj.vertices = verts pobj.num_vert = 8 pobj.connections = elems pobj.num_conn = 12 mesh_temp = m.object() mesh = m.mcell_create_poly_object(world, scene, pobj, mesh_temp) return mesh
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)
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)
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)