def simulation(n_atoms, n_steps, rho, T_want, eps, dt): dist = [] t_together = 0.0 t_apart = 0.0 N_a = 2 * n_atoms R, V, F, box_width = md.initialize_particles(n_atoms, T_want, rho, eps) KinE = [] PotE = [] TotE = [] Temp = [] for i in range(n_steps): Rnew = R + V*dt + 0.5*dt**2*F if i%1000 ==0: video.add_frame(R[0],R[1], i) if i%100000 == 0: print i energy_potential, Fnew = md.lennard_jones(Rnew, box_width, eps) V = scale_temp(V, T_want, N_a) ke = (1.0/2.0) * np.sum(V*V) d = dister(0,1,R, box_width) dist.append(d) if d < 1.52: t_together += 1.0 if d > 1.52: t_apart += 1.0 KinE.append(ke) PotE.append(energy_potential) TotE.append(ke + energy_potential) Temp.append(2 * ke / (N_a)) Vnew = V + 0.5*dt*(F + Fnew) V = Vnew.copy() R = Rnew.copy() video.save("colorparticle", periodic_boundary=True) return PotE, KinE, TotE, Temp, dist, t_together/t_apart
# velocity if abs(positions_x[i]) > box_width: velocities_x[i] = -velocities_x[i] positions_x[i] = positions_x[i] + velocities_x[i]*dt if abs(positions_y[i]) > box_width: velocities_y[i] = -velocities_y[i] positions_y[i] = positions_y[i] + velocities_y[i]*dt # save a 'frame' for the video every 10 step if n % 10 == 0: video.add_frame(positions_x, positions_y) if n % 100 == 0: print 'step', n # Plot the x- and y- coordinates plt.clf() # clear figure plt.plot(positions_x, positions_y, 'ro') plt.axis((-box_width, box_width, -box_width, box_width)) plt.savefig("coordinates_end.png") plt.clf() # Save video video.save('video1')
plt.savefig('start.png') ## Create file for saving energy f = open('simulation_energy', 'w') ## Take simulation steps for n in range(n_steps): X, Y, Vx, Vy, Fx, Fy, energy = velo_verlet(X, Y, Vx, Vy, Fx, Fy, box_width, n_particles, dt) if n % 5 == 0: # calculate kinetic energy energy_kinetic = kinetic(Vx, Vy) line = str(n) + "," + str(energy) + ',' + str(energy_kinetic) f.write(line) f.write('\n') if n % 30 == 0: video.add_frame(X, Y) if n % 100 == 0: print "step", n, "of", n_steps # Save video video.save("video3", box_width)
def simulation(n_atoms, n_steps, rho, temperature, dt, eps = [], save_freq=60, video_filename='', constant_temperature=False): n_eq = 200*save_freq if video_filename != '': save_video = True if len(eps) == 0: eps = np.ones((n_atoms, n_atoms)) else: # set particle 1 and 2 to other color colors = ['#4daf4a' for _ in range(n_atoms)] colors[0] = "#377eb8" colors[1] = "#377eb8" video.add_color(colors) # Initialize simulation R, V, F, box_width = md.initialize_particles(n_atoms, temperature, rho, eps) print 'Initialized' # # Calculate the tail correction # p_tail = 16.0/3.0 * np.pi * rho * (2.0/3.0 * (2.5**-9) - (2.5**-3)) # Storage arrays ek_list = [] # Kinetic energy ep_list = [] # Potential energy et_list = [] # Total energy # pr_list = [] # Pressure te_list = [] # Temperature r_list = [] # distance list # Run simulation for n_steps for n in range(n_steps + 1): # Step 1: Calculate new positions R += V*dt + 0.5*F*dt*dt # Step 2: Calculate new forces F0 = copy.copy(F) potential_energy, F = md.lennard_jones(R, box_width, eps) # Step 3: Calculate new velocities V += 0.5*(F + F0)*dt # if temperture is set to constant, then scale the velocities if constant_temperature: V = md.scale_temp(V, temperature) # for every save_freq steps if n % save_freq == 0 and n > n_eq: print "step:", n # Calculate properties kinetic_energy = calc_kinetic(V) total_energy = kinetic_energy + potential_energy temperature = calc_temperature(V) # pressure = rho*temperature + 1.0/(3.0*box_width**3)*vir + p_tail ek_list.append(kinetic_energy) ep_list.append(potential_energy) et_list.append(total_energy) # pr_list.append(pressure) te_list.append(temperature) # calculate the distance between 0 and 1 X = R[0, 0] - R[0, 1] Y = R[1, 0] - R[1, 1] # Periodic boundary condition X -= box_width * np.rint(X/box_width) Y -= box_width * np.rint(Y/box_width) d = np.sqrt(X**2 + Y**2) r_list.append(d) # Add frame to video if save_video: video.add_frame(R[0], R[1]) # Remember not to save a video for large n_steps if save_video: video.save(video_filename, box_width, periodic_boundary=True) return ek_list, ep_list, et_list, te_list, r_list
def simulation(n_atoms, n_steps, rho, temperature, dt, eps = [], save_freq=60, video_filename='', constant_temperature=False): n_eq = 200*save_freq if video_filename != '': save_video = True if len(eps) == 0: eps = np.ones((n_atoms, n_atoms)) else: # set particle 1 and 2 to other color colors = ['#4daf4a' for _ in range(n_atoms)] colors[0] = "#377eb8" colors[1] = "#377eb8" video.add_color(colors) # Initialize simulation R, V, F, box_width = md.initialize_particles(n_atoms, temperature, rho, eps) print 'Initialized' # # Calculate the tail correction # p_tail = 16.0/3.0 * np.pi * rho * (2.0/3.0 * (2.5**-9) - (2.5**-3)) # Storage arrays ek_list = [] # Kinetic energy ep_list = [] # Potential energy et_list = [] # Total energy # pr_list = [] # Pressure te_list = [] # Temperature r_list = [] # distance list # Run simulation for n_steps for n in range(n_steps + 1): # Step 1: Calculate new positions R += V*dt + 0.5*F*dt*dt # Step 2: Calculate new forces F0 = copy.copy(F) potential_energy, F = md.lennard_jones(R, box_width, eps) # Step 3: Calculate new velocities V += 0.5*(F + F0)*dt # if temperture is set to constant, then scale the velocities if constant_temperature: V = md.scaletemp(V, temperature) # for every save_freq steps if n % save_freq == 0 and n > n_eq: print "step:", n # Calculate properties kinetic_energy = calc_kinetic(V) total_energy = kinetic_energy + potential_energy temperature = calc_temperature(V) # pressure = rho*temperature + 1.0/(3.0*box_width**3)*vir + p_tail ek_list.append(kinetic_energy) ep_list.append(potential_energy) et_list.append(total_energy) # pr_list.append(pressure) te_list.append(temperature) # calculate the distance between 0 and 1 X = R[0, 0] - R[0, 1] Y = R[1, 0] - R[1, 1] # Periodic boundary condition X -= box_width * np.rint(X/box_width) Y -= box_width * np.rint(Y/box_width) d = np.sqrt(X**2 + Y**2) r_list.append(d) # Add frame to video if save_video: video.add_frame(R[0], R[1]) # Remember not to save a video for large n_steps if save_video: video.save(video_filename, box_width, periodic_boundary=True) return ek_list, ep_list, et_list, te_list, r_list