def test_mcmc(dim, n_steps, n_skip, n_reuse_nblist): # kb = 1.38064852*10**(-13) # N*Å/K (Boltzmann constant) sigma_argon = 3.405 # Å epsilon_argon = 119.8 # # actually epsilon/kb (K) sigma_xenon = 4.07 # Å epsilon_xenon = 225.3 # actually epsilon/kb (K) boxsize = np.ones(dim)*np.maximum(sigma_argon, sigma_xenon)*20 # Å r_c = 2.5*0.5*(sigma_argon+sigma_xenon) # n_skip = int(n_steps/10) width = r_c / (n_skip*10) r_s = 2*n_skip*width pos1 = pbc.enforce_pbc(np.random.randn(dim), boxsize) pos2 = pbc.enforce_pbc(pos1 + r_c * np.random.randn(dim), boxsize) pos3 = pbc.enforce_pbc(pos2 + r_c * np.random.randn(dim), boxsize) pos4 = pbc.enforce_pbc(pos3 + r_c * np.random.randn(dim), boxsize) pos5 = pbc.enforce_pbc(pos4 + r_c * np.random.randn(dim), boxsize) pos6 = pbc.enforce_pbc(pos5 + r_c * np.random.randn(dim), boxsize) argon_1 = system.Particle(position = pos1, charge = 0, sigmaLJ = sigma_argon, epsilonLJ = epsilon_argon) argon_2 = system.Particle(position = pos2, charge = 0, sigmaLJ = sigma_argon, epsilonLJ = epsilon_argon) argon_3 = system.Particle(position = pos3, charge = 0, sigmaLJ = sigma_argon, epsilonLJ = epsilon_argon) xenon_1 = system.Particle(position = pos4, charge = 0, sigmaLJ = sigma_xenon, epsilonLJ = epsilon_xenon) xenon_2 = system.Particle(position = pos5, charge = 0, sigmaLJ = sigma_xenon, epsilonLJ = epsilon_xenon) xenon_3 = system.Particle(position = pos6, charge = 0, sigmaLJ = sigma_xenon, epsilonLJ = epsilon_xenon) particles = [argon_1, argon_2, argon_3, xenon_1, xenon_2, xenon_3] temp = 120.0 ourbox = system.Box(dimension = dim, size = boxsize, particles = particles, temp = temp) ourbox.compute_LJneighbourlist(r_c, r_s) ourbox.compute_LJ_potential(r_c, r_s) ourbox.make_positions_list() ourbox.positions, ourbox.LJpotential, positions_history, potLJ_history, p_acc_vec = metropolis.mcmc(ourbox, n_steps, width, n_skip, n_reuse_nblist, True, r_c, r_s) ourbox.update_particle_positions() ourbox.compute_LJneighbourlist(r_c, r_s) for positions in positions_history: temp_box = system.Box(dimension = dim, size = boxsize, particles = particles, temp = temp) for i in range(len(positions_history)): for j in range(len(temp_box.particles)): temp_box.particles[j].position = positions_history[i][j] temp_box.compute_LJ_potential(r_c, r_s) npt.assert_almost_equal(temp_box.LJpotential, potLJ_history[i], decimal = 3)
def optimize( filename='sodium-chloride-example.npz', dim=3, temp=120, width=0, tol_opt=1 / 500, ): print('optimize') parameters, positions, types, boxsize = load.load_input_file(filename) width = boxsize[1] / 1000 particles = create_particle_object(parameters, positions, types) print(parameters) box = system.Box(dim, boxsize, particles, temp) # return system.Box(dim, boxsize, particles, temp) if width == 0: # if no width specified width = boxsize[0] / 5000 r_skin_LJ = 2 * n_reuse_nblist * np.linalg.norm(width * np.ones(3)) save_system_history = True box.compute_LJneighbourlist(r_cut_LJ, r_skin_LJ) box.compute_LJ_potential(r_cut_LJ, r_skin_LJ) box.optimize(n_opt_max, n_steps_opt, tol_opt, n_reuse_nblist, n_skip, width, save_system_history, r_cut_LJ, r_skin_LJ) box.simulate(n_steps_sim, n_reuse_nblist, n_skip, width, save_system_history, r_cut_LJ, r_skin_LJ) pos_history = box.pos_history pot_history = np.asarray(box.pot_history) plot.create_plot(boxsize[1], pos_history, pot_history, r_cut_LJ, n_steps_opt, n_skip) print('running time:', time.time() - start)
def simulate(filename='sodium-chloride-example.npz'): print('simulate') parameters, positions, types, boxsize = load.load_input_file(filename) width = boxsize[1] / 1000 particles = create_particle_object(parameters, positions, types) box = system.Box(dim, boxsize, particles, temp) box.compute_LJneighbourlist(r_cut_LJ, r_skin_LJ) box.compute_LJ_potential(r_cut_LJ, r_skin_LJ) box.simulate(n_steps_sim, n_reuse_nblist, n_skip, width, save_system_history, r_cut_LJ, r_skin_LJ) pos_history = box.pos_history pot_history = np.asarray(box.pot_history) plot.create_plot(boxsize[1], pos_history, pot_history, r_cut_LJ, n_steps_opt, n_skip) print('running time:', time.time() - start)
def generate_test_system(dim=3, boxsize=5 * np.ones(3), nparticles=64, temp=120, charge=1, sigma=1, epsilon=1, kb=0.008314462175): no_of_gridpoints = int(np.cbrt(nparticles)) + 1 g = np.array( range(-no_of_gridpoints, no_of_gridpoints + 1) ) # generate a list of 3-dimensional pos vectors with combinations of -no_of_gridpoints-1, ...,-1, 0, 1, ..., no_of_gridpoints+1 while g[-1] >= min(boxsize / 2): g = g / 2 x, y, z = np.meshgrid(g, g, g) xyz = np.vstack((x.flat, y.flat, z.flat)).T xyz = np.ascontiguousarray(xyz) positions = sorted(xyz, key=lambda k: random.random())[0:nparticles] particles = [] for i in range(nparticles): particles.append( system.Particle(position=positions[i], charge=charge, sigmaLJ=sigma, epsilonLJ=epsilon)) testbox = system.Box(dimension=dim, size=boxsize, particles=particles, temp=temp, kb=kb) return testbox
for i in range(len(sysconfig)): sysconfig[i][0:3] = pbc.enforce_pbc(sysconfig[i][0:3]*boxsize, boxsize) ############################################################################################################ # Initialise particle list based on input and the system (ourbox): ############################################################################################################ particles = [] for i in range(len(sysconfig)): particles.append(system.Particle(position = np.asarray(sysconfig[i][0:3]), charge = sysconfig[i][3], sigmaLJ = sigma_argon, epsilonLJ = epsilon_argon)) ourbox = system.Box(dimension = dim, size = boxsize, particles = particles, temp = 120.0) ourbox.compute_LJneighbourlist(r_cut_LJ, r_skin_LJ) ourbox.compute_LJ_potential(r_cut_LJ, r_skin_LJ) ############################################################################################################ # Simulate system configuration evolution: ############################################################################################################ save_system_history = True n_opt = 50 # tol_opt = ourbox.size[0]/100 tol_opt = 1/50 ourbox.optimize(n_opt, tol_opt, 5*int(n_steps/n_opt), n_reuse_nblist, n_skip, width, save_system_history, r_cut_LJ, r_skin_LJ) ourbox.simulate(n_steps, n_reuse_nblist, n_skip, width, save_system_history, r_cut_LJ, r_skin_LJ) pos_history = ourbox.pos_history pot_history = kb*10**10*np.asarray(ourbox.pot_history) #kb*10**10 to get potential in Joule
def test_verlet_neighbourlist(dim): # kb = 1.38064852*10**(-13) # N*Å/K (Boltzmann constant) sigma_argon = 3.405 # Å epsilon_argon = 119.8 # actually epsilon/kb (K) sigma_xenon = 4.07 # Å epsilon_xenon = 225.3 # actually epsilon/kb (K) r_c = 2.5 * 0.5 * (sigma_argon + sigma_xenon) width = r_c / 10 n_skip = 10 r_s = 2 * n_skip * width boxsize = np.ones(dim) * np.maximum(sigma_argon, sigma_xenon) * 20 # Å # pos1 = pbc.enforce_pbc(np.random.randn(dim)/4, boxsize) # pos2 = pbc.enforce_pbc(np.random.randn(dim)/4, boxsize) # pos3 = pbc.enforce_pbc(np.random.randn(dim)/4, boxsize) # pos4 = pbc.enforce_pbc(np.random.randn(dim)/4, boxsize) # pos5 = pbc.enforce_pbc(np.random.randn(dim)/4, boxsize) # pos6 = pbc.enforce_pbc(np.random.randn(dim)/4, boxsize) pos1 = pbc.enforce_pbc(np.random.randn(dim), boxsize) pos2 = pbc.enforce_pbc(pos1 + r_c * np.random.randn(dim), boxsize) pos3 = pbc.enforce_pbc(pos2 + r_c * np.random.randn(dim), boxsize) pos4 = pbc.enforce_pbc(pos3 + r_c * np.random.randn(dim), boxsize) pos5 = pbc.enforce_pbc(pos4 + r_c * np.random.randn(dim), boxsize) pos6 = pbc.enforce_pbc(pos5 + r_c * np.random.randn(dim), boxsize) argon_1 = system.Particle(position=pos1, charge=0, sigmaLJ=sigma_argon, epsilonLJ=epsilon_argon) argon_2 = system.Particle(position=pos2, charge=0, sigmaLJ=sigma_argon, epsilonLJ=epsilon_argon) argon_3 = system.Particle(position=pos3, charge=0, sigmaLJ=sigma_argon, epsilonLJ=epsilon_argon) xenon_1 = system.Particle(position=pos4, charge=0, sigmaLJ=sigma_xenon, epsilonLJ=epsilon_xenon) xenon_2 = system.Particle(position=pos5, charge=0, sigmaLJ=sigma_xenon, epsilonLJ=epsilon_xenon) xenon_3 = system.Particle(position=pos6, charge=0, sigmaLJ=sigma_xenon, epsilonLJ=epsilon_xenon) particles = [argon_1, argon_2, argon_3, xenon_1, xenon_2, xenon_3] ourbox = system.Box(dimension=dim, size=boxsize, particles=particles, temp=120.0) argon_1.neighbourlist = [] for particlej in [argon_2, argon_3, xenon_1, xenon_2, xenon_3]: if np.linalg.norm(argon_1.position - particlej.position) < r_c + r_s: argon_1.neighbourlist.append(particlej) argon_2.neighbourlist = [] for particlej in [argon_1, argon_3, xenon_1, xenon_2, xenon_3]: if np.linalg.norm(argon_2.position - particlej.position) < r_c + r_s: argon_2.neighbourlist.append(particlej) argon_3.neighbourlist = [] for particlej in [argon_1, argon_2, xenon_1, xenon_2, xenon_3]: if np.linalg.norm(argon_3.position - particlej.position) < r_c + r_s: argon_3.neighbourlist.append(particlej) xenon_1.neighbourlist = [] for particlej in [argon_1, argon_2, argon_3, xenon_2, xenon_3]: if np.linalg.norm(xenon_1.position - particlej.position) < r_c + r_s: xenon_1.neighbourlist.append(particlej) xenon_2.neighbourlist = [] for particlej in [argon_1, argon_2, argon_3, xenon_1, xenon_3]: if np.linalg.norm(xenon_2.position - particlej.position) < r_c + r_s: xenon_2.neighbourlist.append(particlej) xenon_3.neighbourlist = [] for particlej in [argon_1, argon_2, argon_3, xenon_1, xenon_2]: if np.linalg.norm(xenon_3.position - particlej.position) < r_c + r_s: xenon_3.neighbourlist.append(particlej) ourbox.compute_LJneighbourlist(r_c, r_s) for i in range(len(particles)): npt.assert_equal(particles[i].neighbourlist, ourbox.particles[i].neighbourlist)
def plot_neighbourlist(): sigma_argon = 3.405 # Å epsilon_argon = 119.8 # actually epsilon/kb (K) sigma_xenon = 4.07 # Å epsilon_xenon = 225.3 # actually epsilon/kb (K) r_c = 2.5 * 0.5 * (sigma_argon + sigma_xenon) width = r_c / 10 n_skip = 10 r_s = 2 * n_skip * width boxsize = np.ones(2) * np.maximum(sigma_argon, sigma_xenon) * 20 # Å pos1 = pbc.enforce_pbc(np.random.randn(2), boxsize) pos2 = pbc.enforce_pbc(pos1 + r_c * np.random.randn(2), boxsize) pos3 = pbc.enforce_pbc(pos2 + r_c * np.random.randn(2), boxsize) pos4 = pbc.enforce_pbc(pos3 + r_c * np.random.randn(2), boxsize) pos5 = pbc.enforce_pbc(pos4 + r_c * np.random.randn(2), boxsize) pos6 = pbc.enforce_pbc(pos5 + r_c * np.random.randn(2), boxsize) argon_1 = system.Particle(position=pos1, charge=0, sigmaLJ=sigma_argon, epsilonLJ=epsilon_argon) argon_2 = system.Particle(position=pos2, charge=0, sigmaLJ=sigma_argon, epsilonLJ=epsilon_argon) argon_3 = system.Particle(position=pos3, charge=0, sigmaLJ=sigma_argon, epsilonLJ=epsilon_argon) xenon_1 = system.Particle(position=pos4, charge=0, sigmaLJ=sigma_xenon, epsilonLJ=epsilon_xenon) xenon_2 = system.Particle(position=pos5, charge=0, sigmaLJ=sigma_xenon, epsilonLJ=epsilon_xenon) xenon_3 = system.Particle(position=pos6, charge=0, sigmaLJ=sigma_xenon, epsilonLJ=epsilon_xenon) particles = [argon_1, argon_2, argon_3, xenon_1, xenon_2, xenon_3] ourbox = system.Box(dimension=2, size=boxsize, particles=particles, temp=120.0) ourbox.compute_LJneighbourlist(r_c, r_s) j = 1 nbhood_circle = plt.Circle(ourbox.particles[j].position, r_c + r_s, color='b', fill=False) fig = plt.figure() ax = fig.add_subplot(111) ax.set_xlim( (ourbox.center[0] - ourbox.size[0], ourbox.center[0] + ourbox.size[0])) ax.set_ylim( (ourbox.center[1] - ourbox.size[1], ourbox.center[1] + ourbox.size[1])) ax.add_artist(nbhood_circle) nbpositionsj = [ourbox.particles[j].position] for particle in ourbox.particles[j].neighbourlist: nbpositionsj.append(particle.position) nbpositionsj = np.asarray(nbpositionsj) print(ourbox.particles[j].neighbourlist) ax.scatter(*np.asarray(ourbox.positions).T, color="blue") ax.scatter(*nbpositionsj.T, color="red") plt.show()
def test_mcmc_step(dim, update_nblist): # kb = 1.38064852*10**(-13) # N*Å/K (Boltzmann constant) sigma_argon = 3.405 # Å epsilon_argon = 119.8 # # actually epsilon/kb (K) sigma_xenon = 4.07 # Å epsilon_xenon = 225.3 # actually epsilon/kb (K) boxsize = np.ones(dim)*np.maximum(sigma_argon, sigma_xenon)*20 # Å r_c = 2.5*0.5*(sigma_argon+sigma_xenon) n_skip = 5 width = r_c / (n_skip*10) r_s = 2*n_skip*width pos1 = pbc.enforce_pbc(np.random.randn(dim), boxsize) pos2 = pbc.enforce_pbc(pos1 + r_c * np.random.randn(dim), boxsize) pos3 = pbc.enforce_pbc(pos2 + r_c * np.random.randn(dim), boxsize) pos4 = pbc.enforce_pbc(pos3 + r_c * np.random.randn(dim), boxsize) pos5 = pbc.enforce_pbc(pos4 + r_c * np.random.randn(dim), boxsize) pos6 = pbc.enforce_pbc(pos5 + r_c * np.random.randn(dim), boxsize) argon_1 = system.Particle(position = pos1, charge = 0, sigmaLJ = sigma_argon, epsilonLJ = epsilon_argon) argon_2 = system.Particle(position = pos2, charge = 0, sigmaLJ = sigma_argon, epsilonLJ = epsilon_argon) argon_3 = system.Particle(position = pos3, charge = 0, sigmaLJ = sigma_argon, epsilonLJ = epsilon_argon) xenon_1 = system.Particle(position = pos4, charge = 0, sigmaLJ = sigma_xenon, epsilonLJ = epsilon_xenon) xenon_2 = system.Particle(position = pos5, charge = 0, sigmaLJ = sigma_xenon, epsilonLJ = epsilon_xenon) xenon_3 = system.Particle(position = pos6, charge = 0, sigmaLJ = sigma_xenon, epsilonLJ = epsilon_xenon) particles = [argon_1, argon_2, argon_3, xenon_1, xenon_2, xenon_3] temp = 120.0 ourbox = system.Box(dimension = dim, size = boxsize, particles = particles, temp = temp) ourbox.compute_LJneighbourlist(r_c, r_s) ourbox.compute_LJ_potential(r_c, r_s) ourbox.make_positions_list() ourbox_trial = copy.deepcopy(ourbox) ourbox_post_mcmc_step = system.Box(dimension = dim, size = boxsize, particles = particles, temp = temp) ourbox_post_mcmc_step.compute_LJneighbourlist(r_c, r_s) ourbox_post_mcmc_step.compute_LJ_potential(r_c, r_s) ourbox_post_mcmc_step.make_positions_list() ourbox_post_mcmc_step.positions, ourbox_post_mcmc_step.LJpotential, trial_step, _ = metropolis.mcmc_step(ourbox, width, r_c, r_s, update_nblist) ourbox_post_mcmc_step.update_particle_positions() ourbox_post_mcmc_step.compute_LJneighbourlist(r_c, r_s) for i, particle in enumerate(ourbox_trial.particles): ourbox_trial.particles[i].position = pbc.enforce_pbc(ourbox_trial.particles[i].position + trial_step[i], ourbox_trial.size) ourbox_trial.make_positions_list() ourbox_trial.compute_LJ_potential(r_c, r_s) if update_nblist: ourbox_trial.compute_LJneighbourlist(r_c, r_s) with warnings.catch_warnings(): warnings.simplefilter("ignore") prob_accept = np.minimum(1, np.exp(-(ourbox_trial.LJpotential - ourbox.LJpotential)/temp)) if np.random.rand() < prob_accept: return ourbox_trial, ourbox_post_mcmc_step else: return ourbox, ourbox_post_mcmc_step
def gen_test_box(dim): # kb = 1.38064852*10**(-13) # N*Å/K (Boltzmann constant) sigma_argon = 3.405 # Å epsilon_argon = 119.8 # actually epsilon/kb (K) sigma_xenon = 4.07 # Å epsilon_xenon = 225.3 # actually epsilon/kb (K) boxsize = np.ones(dim) * np.maximum(sigma_argon, sigma_xenon) * 20 r_c = 2.5 * 0.5 * (sigma_argon + sigma_xenon) n_skip = 5 width = r_c / (n_skip * 10) r_s = 2 * n_skip * width # pos1 = pbc.enforce_pbc(np.random.randn(dim)/4, boxsize) # pos2 = pbc.enforce_pbc(np.random.randn(dim)/4, boxsize) # pos3 = pbc.enforce_pbc(np.random.randn(dim)/4, boxsize) # pos4 = pbc.enforce_pbc(np.random.randn(dim)/4, boxsize) # pos5 = pbc.enforce_pbc(np.random.randn(dim)/4, boxsize) # pos6 = pbc.enforce_pbc(np.random.randn(dim)/4, boxsize) pos1 = pbc.enforce_pbc(np.random.randn(dim), boxsize) pos2 = pbc.enforce_pbc(pos1 + r_c * np.random.randn(dim), boxsize) pos3 = pbc.enforce_pbc(pos2 + r_c * np.random.randn(dim), boxsize) pos4 = pbc.enforce_pbc(pos3 + r_c * np.random.randn(dim), boxsize) pos5 = pbc.enforce_pbc(pos4 + r_c * np.random.randn(dim), boxsize) pos6 = pbc.enforce_pbc(pos5 + r_c * np.random.randn(dim), boxsize) argon_1 = system.Particle(position=pos1, charge=0, sigmaLJ=sigma_argon, epsilonLJ=epsilon_argon) argon_2 = system.Particle(position=pos2, charge=0, sigmaLJ=sigma_argon, epsilonLJ=epsilon_argon) argon_3 = system.Particle(position=pos3, charge=0, sigmaLJ=sigma_argon, epsilonLJ=epsilon_argon) xenon_1 = system.Particle(position=pos4, charge=0, sigmaLJ=sigma_xenon, epsilonLJ=epsilon_xenon) xenon_2 = system.Particle(position=pos5, charge=0, sigmaLJ=sigma_xenon, epsilonLJ=epsilon_xenon) xenon_3 = system.Particle(position=pos6, charge=0, sigmaLJ=sigma_xenon, epsilonLJ=epsilon_xenon) particles = [argon_1, argon_2, argon_3, xenon_1, xenon_2, xenon_3] return system.Box(dimension=dim, size=boxsize, particles=particles, temp=120.0)