def test_diffusion_sim_random_state(): # Initialize the random state rs = np.random.RandomState(_SEED) # Diffusion coefficient Du = 12.0 # um^2 / s D1 = Du * (1e-6)**2 # m^2 / s D2 = D1 / 2 # Simulation box definition box = pbm.Box(x1=-4.e-6, x2=4.e-6, y1=-4.e-6, y2=4.e-6, z1=-6e-6, z2=6e-6) # PSF definition psf = pbm.NumericPSF() # Particles definition P = pbm.Particles(num_particles=20, D=D1, box=box, rs=rs) P.add(num_particles=15, D=D2) # Simulation time step (seconds) t_step = 0.5e-6 # Time duration of the simulation (seconds) t_max = 0.01 # Particle simulation definition S = pbm.ParticlesSimulation(t_step=t_step, t_max=t_max, particles=P, box=box, psf=psf) rs_prediffusion = rs.get_state() S.simulate_diffusion(total_emission=False, save_pos=True, verbose=True, rs=rs, chunksize=2**13, chunkslice='times') rs_postdiffusion = rs.get_state() # Test diffusion random states saved_rs = S.traj_group._v_attrs['init_random_state'] assert randomstate_equal(saved_rs, rs_prediffusion) saved_rs = S.traj_group._v_attrs['last_random_state'] assert randomstate_equal(saved_rs, rs_postdiffusion)
def create_diffusion_sim(): rs = np.random.RandomState(_SEED) Du = 12.0 # um^2 / s D = Du * (1e-6)**2 # m^2 / s box = pbm.Box(x1=-4.e-6, x2=4.e-6, y1=-4.e-6, y2=4.e-6, z1=-6e-6, z2=6e-6) psf = pbm.NumericPSF() P = pbm.Particles(num_particles=100, D=D, box=box, rs=rs) t_step = 0.5e-6 t_max = 0.1 S = pbm.ParticlesSimulation(t_step=t_step, t_max=t_max, particles=P, box=box, psf=psf) S.simulate_diffusion(save_pos=True, total_emission=False, radial=True, rs=rs) S.store.close() return S.hash()[:6]
def create_diffusion_sim(psf=pbm.NumericPSF()): rs = np.random.RandomState(_SEED) specs = { k: v for k, v in particles_specs.items() if k in ['num_particles', 'D', 'box'] } P = pbm.Particles.from_specs(**specs, rs=rs) S = pbm.ParticlesSimulation(t_step=t_step, t_max=t_max, particles=P, box=box, psf=psf) S.simulate_diffusion(save_pos=True, total_emission=False, radial=False, rs=rs) S.store.close() return S.hash()[:6]
def test_diffusion_sim_core(): # Initialize the random state rs = np.random.RandomState(_SEED) Du = 12.0 # um^2 / s D = Du * (1e-6)**2 # m^2 / s box = pbm.Box(x1=-4.e-6, x2=4.e-6, y1=-4.e-6, y2=4.e-6, z1=-6e-6, z2=6e-6) psf = pbm.NumericPSF() P = pbm.Particles(num_particles=100, D=D, box=box, rs=rs) t_step = 0.5e-6 t_max = 0.001 time_size = t_max / t_step assert t_max < 1e4 S = pbm.ParticlesSimulation(t_step=t_step, t_max=t_max, particles=P, box=box, psf=psf) start_pos = [p.r0 for p in S.particles] start_pos = np.vstack(start_pos).reshape(S.num_particles, 3, 1) for wrap_func in [pbm.diffusion.wrap_mirror, pbm.diffusion.wrap_periodic]: for total_emission in [True, False]: sim = S._sim_trajectories(time_size, start_pos, rs=rs, total_emission=total_emission, save_pos=True, wrap_func=wrap_func) POS, em = sim POS = np.concatenate(POS, axis=0) #x, y, z = POS[:, :, 0], POS[:, :, 1], POS[:, :, 2] #r_squared = x**2 + y**2 + z**2 DR = np.diff(POS, axis=2) dx, dy, dz = DR[:, :, 0], DR[:, :, 1], DR[:, :, 2] dr_squared = dx**2 + dy**2 + dz**2 D_fitted = dr_squared.mean() / (6 * t_max) # Fitted diffusion coefficient assert np.abs(D - D_fitted) < 0.01
def test_diffusion_sim_random_state(): for psf in (pbm.NumericPSF(), pbm.GaussianPSF()): # Initialize the random state rs = np.random.RandomState(_SEED) # Particles definition P = pbm.Particles.from_specs(num_particles=(5, 7), D=(D1, D2), box=box, rs=rs) # Time duration of the simulation (seconds) t_max = 0.01 # Particle simulation definition S = pbm.ParticlesSimulation(t_step=t_step, t_max=t_max, particles=P, box=box, psf=psf) rs_prediffusion = rs.get_state() S.simulate_diffusion(total_emission=False, save_pos=True, verbose=True, rs=rs, chunksize=2**13, chunkslice='times') rs_postdiffusion = rs.get_state() # Test diffusion random states saved_rs = S.traj_group._v_attrs['init_random_state'] assert randomstate_equal(saved_rs, rs_prediffusion) saved_rs = S.traj_group._v_attrs['last_random_state'] assert randomstate_equal(saved_rs, rs_postdiffusion) S.store.close()
# Particles definition n1 = 10 n2 = 0 #nn = n1+n2 P = pbm.Particles.from_specs(num_particles=(n1,) ,D=(D1,) ,box=box ,rs=rs) # PSF definition #psf = pbm.NumericPSF() #psf = pbm.GaussianPSF(sx=0.3e-6,sy=0.3e-6,sz=0.5e-6) psf = pbm.NumericPSF() # Particle simulation definition S = pbm.ParticlesSimulation(t_step=t_step, t_max=t_max, particles=P, box=box, psf=psf) ###Run simulation: #Note that the save_pos=True seems to be very important in order to use the #particle simulation visualizer (PSV) print('Current random state:', pbm.hashfunc(rs.get_state())) S.simulate_diffusion(total_emission=False, save_pos=True, verbose=True, rs=rs, chunksize=2**19, chunkslice='times') print('Current random state:', pbm.hashfunc(rs.get_state())) print(S.compact_name()) #%% Simulate FRET: #E1 = efficiency(40.,56.) #E2 = efficiency(65.,56.) params1 = dict( em_rates = (200e3,200e3), #em_list, # Peak emission rates (cps) for each population (D+A)