def run_single(T, V, N, R, D): L = numpy.power(V, 1.0/3.0) # cuboid side length # matrix_size = 3 # min matrix size matrix_size = max(3, int(min(numpy.power(N, 1.0 / 3.0), L / (2 * R)))) m = model.ParticleModel(L) A = model.Species('A', D, R) m.add_species_type(A) m.set_all_repulsive() w = gfrdbase.create_world(m, matrix_size) gfrdbase.throw_in_particles(w, A, N) nrw = gfrdbase.create_network_rules_wrapper(m) sim = _gfrd._EGFRDSimulator(w, nrw, myrandom.rng) stirTime = T*0.01 t = 0.0 gc.disable while (sim.step(stirTime)): pass print "Now running",int(N),"molecules for",T,"s" endTime = stirTime+T start = time.time() while (sim.step(endTime)): pass print "time",sim.t,endTime end = time.time() gc.collect() gc.enable() duration = end-start print duration,"s" return duration
def singlerun(T, S): m = model.ParticleModel(1e-3) A = model.Species('A', 1e-12, 5e-9) m.add_species_type(A) w = gfrdbase.create_world(m, 3) nrw = gfrdbase.create_network_rules_wrapper(m) # s = EGFRDSimulator(w, myrandom.rng, nrw) # s.set_user_max_shell_size(S) s = _gfrd._EGFRDSimulator(w, nrw, myrandom.rng, 1, 1e-5, S) particleA = gfrdbase.place_particle(w, A, [0, 0, 0]) end_time = T s.step() while 1: next_time = s.t + s.dt if next_time > end_time: # s.stop(end_time) s.step(end_time) break s.step() pos = w.get_particle(iter(w.get_particle_ids(A.id)).next())[1].position distance = w.distance([0,0,0], pos) return distance, s.t
def singlerun(T, S): m = model.ParticleModel(1e-3) A = model.Species('A', 1e-12, 5e-9) m.add_species_type(A) w = gfrdbase.create_world(m, 3) nrw = gfrdbase.create_network_rules_wrapper(m) # s = EGFRDSimulator(w, myrandom.rng, nrw) # s.set_user_max_shell_size(S) s = _gfrd._EGFRDSimulator(w, nrw, myrandom.rng, 1, 1e-5, S) particleA = gfrdbase.place_particle(w, A, [0, 0, 0]) end_time = T s.step() while 1: next_time = s.t + s.dt if next_time > end_time: # s.stop(end_time) s.step(end_time) break s.step() pos = w.get_particle(iter(w.get_particle_ids(A.id)).next())[1].position distance = w.distance([0, 0, 0], pos) return distance, s.t
def singlerun(T): sigma = 5e-9 r0 = sigma D = 1e-12 D_tot = D * 2 kf = 100 * sigma * D_tot m = model.ParticleModel(1e-3) A = model.Species('A', D, sigma / 2) m.add_species_type(A) B = model.Species('B', D, sigma / 2) m.add_species_type(B) C = model.Species('C', D, sigma / 2) m.add_species_type(C) r1 = model.create_binding_reaction_rule(A, B, C, kf) m.network_rules.add_reaction_rule(r1) w = gfrdbase.create_world(m, 3) nrw = gfrdbase.create_network_rules_wrapper(m) s = _gfrd._EGFRDSimulator(w, nrw, myrandom.rng) class check_reactions: def __init__(self): self.reactions = [] def __call__(self, ri): self.reactions.append(ri) cr = check_reactions() s.reaction_recorder = cr pid1 = gfrdbase.place_particle(w, A, [0, 0, 0])[0] pid2 = gfrdbase.place_particle( w, B, [float(A['radius']) + float(B['radius']) + 1e-23, 0, 0])[0] end_time = T while s.step(end_time): if len(cr.reactions) != 0: cr.reactions = [] return 0, s.t p1 = s.world.get_particle(pid1)[1] p2 = s.world.get_particle(pid2)[1] distance = w.distance(p1.position, p2.position) return distance, s.t
def singlerun(T): sigma = 5e-9 r0 = sigma D = 1e-12 D_tot = D * 2 kf = 100 * sigma * D_tot m = model.ParticleModel(1e-3) A = model.Species('A', D, sigma/2) m.add_species_type(A) B = model.Species('B', D, sigma/2) m.add_species_type(B) C = model.Species('C', D, sigma/2) m.add_species_type(C) r1 = model.create_binding_reaction_rule(A, B, C, kf) m.network_rules.add_reaction_rule(r1) w = gfrdbase.create_world(m, 3) nrw = gfrdbase.create_network_rules_wrapper(m) s = _gfrd._EGFRDSimulator(w, nrw, myrandom.rng) class check_reactions: def __init__(self): self.reactions = [] def __call__(self, ri): self.reactions.append(ri) cr = check_reactions() s.reaction_recorder = cr pid1 = gfrdbase.place_particle(w, A, [0,0,0])[0] pid2 = gfrdbase.place_particle(w, B, [float(A['radius']) + float(B['radius'])+1e-23,0,0])[0] end_time = T while s.step(end_time): if len(cr.reactions) != 0: cr.reactions = [] return 0, s.t p1 = s.world.get_particle(pid1)[1] p2 = s.world.get_particle(pid2)[1] distance = w.distance(p1.position, p2.position) return distance, s.t
def singlerun2(T): #s.set_user_max_shell_size(1e-7) #s.set_user_max_shell_size(1e-3) sigma = 5e-9 r0 = sigma D = 1e-12 D_tot = D * 2 kf = 100 * sigma * D_tot m = model.ParticleModel(1e-3) A = model.Species('A', D, sigma / 2) m.add_species_type(A) B = model.Species('B', D, sigma / 2) m.add_species_type(B) C = model.Species('C', D, sigma / 2) m.add_species_type(C) r1 = model.create_binding_reaction_rule(A, B, C, kf) m.network_rules.add_reaction_rule(r1) w = gfrdbase.create_world(m, 3) nrw = gfrdbase.create_network_rules_wrapper(m) s = EGFRDSimulator(w, myrandom.rng, nrw) particleA = gfrdbase.place_particle(w, A, [0, 0, 0]) particleB = gfrdbase.place_particle( w, B, [float(A['radius']) + float(B['radius']) + 1e-23, 0, 0]) end_time = T while 1: s.step() if s.last_reaction: #print 'reaction' return 0.0, s.t next_time = s.get_next_time() if next_time > end_time: s.stop(end_time) break distance = w.distance(s.get_position(particleA), s.get_position(particleB)) return distance, s.t
def singlerun2(T): #s.set_user_max_shell_size(1e-7) #s.set_user_max_shell_size(1e-3) sigma = 5e-9 r0 = sigma D = 1e-12 D_tot = D * 2 kf = 100 * sigma * D_tot m = model.ParticleModel(1e-3) A = model.Species('A', D, sigma/2) m.add_species_type(A) B = model.Species('B', D, sigma/2) m.add_species_type(B) C = model.Species('C', D, sigma/2) m.add_species_type(C) r1 = model.create_binding_reaction_rule(A, B, C, kf) m.network_rules.add_reaction_rule(r1) w = gfrdbase.create_world(m, 3) nrw = gfrdbase.create_network_rules_wrapper(m) s = EGFRDSimulator(w, myrandom.rng, nrw) particleA = gfrdbase.place_particle(w, A, [0,0,0]) particleB = gfrdbase.place_particle(w, B, [float(A['radius']) + float(B['radius'])+1e-23,0,0]) end_time = T while 1: s.step() if s.last_reaction: #print 'reaction' return 0.0, s.t next_time = s.get_next_time() if next_time > end_time: s.stop(end_time) break distance = w.distance(s.get_position(particleA), s.get_position(particleB)) return distance, s.t
def singlerun(T_list, N_B, N_X): """ Function that performs one simulation run of binding/ rebinding. Arguments: - T_list: List of times at which distance between particles is evaluated and logged. - N_B: Number of instances ("molecules") of particle B. - N_X: Number of instances ("molecules") of particle X. """ ################# Define the model: # Create model class m = model.ParticleModel(L) # Particle species classes A = model.Species('A', D, radius) m.add_species_type(A) B = model.Species('B', D, radius) m.add_species_type(B) C = model.Species('C', D, radius) m.add_species_type(C) X = model.Species('X', DX, radius) m.add_species_type(X) # Reaction rules r1 = model.create_binding_reaction_rule(A, B, C, kf) m.network_rules.add_reaction_rule(r1) r2 = model.create_unbinding_reaction_rule(C, A, B, 1e3) m.network_rules.add_reaction_rule(r2) ################# Set up simulator w = gfrdbase.create_world(m, matrix_size) nrw = gfrdbase.create_network_rules_wrapper(m) s = EGFRDSimulator(w, myrandom.rng, nrw) ################# Place particles # Throw in some X particles and stirr if N_X != 0: gfrdbase.throw_in_particles(w, X, N_X) end_time = tau * 1 while 1: s.step() next_time = s.get_next_time() if next_time > end_time: s.stop(end_time) break # Reset simulation, so stirring has no effect on output data s.reset() # Define positions for particles to place. A_pos = [0,0,0] B_pos = [(float(A['radius']) + float(B['radius']))+1e-23,0,0] # Clear an area at position A_pos and place particle A there # # How this should be done: # - find particles at position A_pos within species A radius # - delete those # - add a number of X particles _randomly to the box_, the # number being equal to # removed particles. # - if a particle is accidently placed within the "cleared" # area, repeat the process (in very crowded box, this # leads to infinite loop) # # Currently, however, it doesn't get the particles within a certain radius, # but the particles which domain lies within a certain radius. # # Therefore, you might be removing particles that after bursting are not # within the radius anymore, but whose (now bursted) domains were. # # TODO # This code should thus be adapted to solve this problem. # # Perhaps it would be more easy to just place all the particles at # the beginning, but give A and B initially a diffusion constant of # 0, then stirr, and then set the diffusion constant to their # desired values..""" while 1: # Burst domains within a certain volume and collect their ids dd = s.clear_volume(A_pos, float(A['radius'])) if not dd: break for d in dd: for p in d.particle_id_pair: s.remove_particle(p) s.throw_in_particles(X, len(pp), box1) place_particle(w, A, A_pos) # Idem for a particle B: while 1: # Burst domains within a certain volume and collect their ids dd = s.clear_volume(B_pos, float(B['radius'])) if not dd: break for d in dd: for p in d.particle_id_pair: s.remove_particle(p) s.throw_in_particles(X, len(pp), box1) place_particle(w, B, B_pos) # Place rest of B at random positions if N_B > 1: gfrdbase.throw_in_particles(w, B, N_B-1) # Create some vars and perform first simulation step r_list = [] t_list = [] t_last = 0 s.step() next_stop = T_list[0] i_T = 0 # ### Start simulating while 1: # What happens in the following if-statement: # Create a list of the durations particles were in bound state. # # If there was a reaction: # - Binding: Indicated by the fact that there are no C-particles. # In this case: record the current time (s.t) to t_last. # - Unbinding: Indicated because this is the only other case when # a reaction has taken place. # In this case: record the time binding lasted, i.e. dt between # current time (s.t) and last reaction time (t_last). if s.last_reaction: # aka if there was a reaction if len(s.world.get_particle_ids(C.id)) == 0: #A,B print ' - set t_last', str(s.t) # set t_last to "current time" in simulator t_last = s.t else: print ' - reaction: ', str(s.t - t_last) t_list.append(s.t - t_last) # If it's time to log, then log distance between particles. next_time = s.get_next_time() if next_time > next_stop: print '* Measuring distances at ', str(next_stop),\ '(measurement #', str(i_T), ')' s.stop(next_stop) print '* stopped' # If there is a particle C, the distance is "0". if len(s.world.get_particle_ids(C.id)) != 0: #A,B r_list.append(0) # If particles are manifested as A and B, log distance # inbetween. else: # C = 0, i.e. there are particles A and B # Calculate distances: A_pos = s.get_position(A.id) distance_list = [] # If there are >1 B particles, we want to log all # distances seperately, hence the loop: for particle_id in w.get_particle_ids(B.id): B_pos = s.get_position(particle_id) distance = w.distance(A_pos, B_pos) distance_list.append(distance) # Write list to distances table r_list.append(distance_list) i_T += 1 next_stop = T_list[i_T] # If there are no measuring moments on the list any more, and # a duration of being bounded has been recorded, then stop if (next_stop == INF) and (len(t_list) != 0): print 'break', s.t break s.step() return r_list, t_list
m.add_species_type(ES) m.add_species_type(P) fwd = model.create_binding_reaction_rule(E, S, ES, 0.01e-18) back = model.create_unbinding_reaction_rule(ES, E, S, 0.1) prod = model.create_unbinding_reaction_rule(ES, E, P, 0.1) m.network_rules.add_reaction_rule(fwd) m.network_rules.add_reaction_rule(back) m.network_rules.add_reaction_rule(prod) m.set_all_repulsive() w = gfrdbase.create_world(m, matrix_size) gfrdbase.throw_in_particles(w, E, nE) gfrdbase.throw_in_particles(w, S, nS) nrw = gfrdbase.create_network_rules_wrapper(m) sim = _gfrd._EGFRDSimulator(w, nrw, myrandom.rng) start = time.time() t = 0 f = open('egfrd_small_r.csv','w') while (sim.step(T)): t = t+1e-2 while (sim.step(t)): pass f.write(str(sim.t)+','+str(len(w.get_particle_ids(E)))+','+str(len(w.get_particle_ids(S)))+','+str(len(w.get_particle_ids(ES)))+','+str(len(w.get_particle_ids(P)))+'\n') f.flush() print "time",sim.t,T end = time.time() duration = end-start print duration,"s"
def singlerun(T_list, N_B, N_X): """ Function that performs one simulation run of binding/ rebinding. Arguments: - T_list: List of times at which distance between particles is evaluated and logged. - N_B: Number of instances ("molecules") of particle B. - N_X: Number of instances ("molecules") of particle X. """ ################# Define the model: # Create model class m = model.ParticleModel(L) # Particle species classes A = model.Species('A', D, radius) m.add_species_type(A) B = model.Species('B', D, radius) m.add_species_type(B) C = model.Species('C', D, radius) m.add_species_type(C) X = model.Species('X', DX, radius) m.add_species_type(X) # Reaction rules r1 = model.create_binding_reaction_rule(A, B, C, kf) m.network_rules.add_reaction_rule(r1) r2 = model.create_unbinding_reaction_rule(C, A, B, 1e3) m.network_rules.add_reaction_rule(r2) ################# Set up simulator w = gfrdbase.create_world(m, matrix_size) nrw = gfrdbase.create_network_rules_wrapper(m) s = EGFRDSimulator(w, myrandom.rng, nrw) ################# Place particles # Throw in some X particles and stirr if N_X != 0: gfrdbase.throw_in_particles(w, X, N_X) end_time = tau * 1 while 1: s.step() next_time = s.get_next_time() if next_time > end_time: s.stop(end_time) break # Reset simulation, so stirring has no effect on output data s.reset() # Define positions for particles to place. A_pos = [0, 0, 0] B_pos = [(float(A['radius']) + float(B['radius'])) + 1e-23, 0, 0] # Clear an area at position A_pos and place particle A there # # How this should be done: # - find particles at position A_pos within species A radius # - delete those # - add a number of X particles _randomly to the box_, the # number being equal to # removed particles. # - if a particle is accidently placed within the "cleared" # area, repeat the process (in very crowded box, this # leads to infinite loop) # # Currently, however, it doesn't get the particles within a certain radius, # but the particles which domain lies within a certain radius. # # Therefore, you might be removing particles that after bursting are not # within the radius anymore, but whose (now bursted) domains were. # # TODO # This code should thus be adapted to solve this problem. # # Perhaps it would be more easy to just place all the particles at # the beginning, but give A and B initially a diffusion constant of # 0, then stirr, and then set the diffusion constant to their # desired values..""" while 1: # Burst domains within a certain volume and collect their ids dd = s.clear_volume(A_pos, float(A['radius'])) if not dd: break for d in dd: for p in d.particle_id_pair: s.remove_particle(p) s.throw_in_particles(X, len(pp), box1) place_particle(w, A, A_pos) # Idem for a particle B: while 1: # Burst domains within a certain volume and collect their ids dd = s.clear_volume(B_pos, float(B['radius'])) if not dd: break for d in dd: for p in d.particle_id_pair: s.remove_particle(p) s.throw_in_particles(X, len(pp), box1) place_particle(w, B, B_pos) # Place rest of B at random positions if N_B > 1: gfrdbase.throw_in_particles(w, B, N_B - 1) # Create some vars and perform first simulation step r_list = [] t_list = [] t_last = 0 s.step() next_stop = T_list[0] i_T = 0 # ### Start simulating while 1: # What happens in the following if-statement: # Create a list of the durations particles were in bound state. # # If there was a reaction: # - Binding: Indicated by the fact that there are no C-particles. # In this case: record the current time (s.t) to t_last. # - Unbinding: Indicated because this is the only other case when # a reaction has taken place. # In this case: record the time binding lasted, i.e. dt between # current time (s.t) and last reaction time (t_last). if s.last_reaction: # aka if there was a reaction if len(s.world.get_particle_ids(C.id)) == 0: #A,B print ' - set t_last', str(s.t) # set t_last to "current time" in simulator t_last = s.t else: print ' - reaction: ', str(s.t - t_last) t_list.append(s.t - t_last) # If it's time to log, then log distance between particles. next_time = s.get_next_time() if next_time > next_stop: print '* Measuring distances at ', str(next_stop),\ '(measurement #', str(i_T), ')' s.stop(next_stop) print '* stopped' # If there is a particle C, the distance is "0". if len(s.world.get_particle_ids(C.id)) != 0: #A,B r_list.append(0) # If particles are manifested as A and B, log distance # inbetween. else: # C = 0, i.e. there are particles A and B # Calculate distances: A_pos = s.get_position(A.id) distance_list = [] # If there are >1 B particles, we want to log all # distances seperately, hence the loop: for particle_id in w.get_particle_ids(B.id): B_pos = s.get_position(particle_id) distance = w.distance(A_pos, B_pos) distance_list.append(distance) # Write list to distances table r_list.append(distance_list) i_T += 1 next_stop = T_list[i_T] # If there are no measuring moments on the list any more, and # a duration of being bounded has been recorded, then stop if (next_stop == INF) and (len(t_list) != 0): print 'break', s.t break s.step() return r_list, t_list