def __init__(self, dimension, size, particles, temp, optimisation_pos_history=[], optimisation_pot_history=[]): """Return a Box object of dimension *dimension* (between 1 and 3), whose length(&breadth&height) is *size*, is centered at *center*, and contains the particles in the numpy array *particles*""" self.dimension = dimension self.size = size self.center = np.zeros(dimension) self.particles = particles self.LJpotential = None self.temp = temp self.Cpotential = None self.pos_history = None self.pot_history = None self.optimisation_pos_history = optimisation_pos_history # variable to keep all position histories throughout the optimisation self.optimisation_pot_history = optimisation_pot_history for particle in particles: particle.position = pbc.enforce_pbc(particle.position, size) self.make_positions_list()
def mcmc_step(box, width, r_cut, r_skin, update_nblist): # kb = 1.38064852*10**(-13) # N*Å/K (Boltzmann constant) positions_trial = [np.zeros(box.dimension) for i in range(len(box.positions))] trial_step = width * np.random.randn(*np.asarray(positions_trial).shape)/4 #randn -> std norm. dist, divide by 4 to keep results mostly within (-0.5, 0.5) for i in range(len(positions_trial)): positions_trial[i] = pbc.enforce_pbc(box.positions[i] + trial_step[i], box.size) particles_trial = [system.Particle(positions_trial[i], box.particles[i].charge, box.particles[i].sigmaLJ, box.particles[i].epsilonLJ) for i in range(len(box.particles))] # set trial particle list with trial positions if update_nblist: particles_trial = neighbourlist.verlet_neighbourlist(particles_trial, r_cut, r_skin) # update neighbourlist for trial positions else: for i in range(len(particles_trial)): particles_trial[i].neighbourlist = box.particles[i].neighbourlist LJpotential_trial = lennardjones.LJ_potential(particles_trial, r_cut, r_skin) with warnings.catch_warnings(): warnings.simplefilter("ignore") acceptance_prob = min(1.0, np.exp(-(LJpotential_trial - box.LJpotential)/box.temp)) # note: potential is actually potential/kb # if (box_trial.LJpotential>box.LJpotential): # print("increase = " + repr(box_trial.LJpotential-box.LJpotential)) # print("acceptance prob. = " + repr(acceptance_prob)) # if update_nblist: # print(acceptance_prob) if np.random.rand() < acceptance_prob: return positions_trial, True, trial_step, acceptance_prob # Return True for accepted trial step return trial_step and acceptance_prob to use in unit testing return box.positions, False, trial_step, acceptance_prob # return False for rejected trial step (no need to update box object)
def gen_random_input_3D(filename, n_particles, boxsize, r_c): fid = open(filename, "w") fid_writer = csv.writer(fid, delimiter=',') sysconfig = np.zeros( (n_particles, 4)) # generate 3d particle position vectors and particle charge sysconfig.T[3] = np.random.randn(1, n_particles) # the charge sysconfig[0][0:3] = pbc.enforce_pbc( np.random.randn(3) * boxsize / 4, boxsize) # position of particle0 for i in range(1, len(sysconfig)): sysconfig[i][0:3] = pbc.enforce_pbc( sysconfig[i - 1][0:3] + np.random.randn(3) * r_c / (n_particles * 8), boxsize ) # perturb position of particle0 to get initial pos of particle1 sysconfig = sysconfig.tolist() # print(sysconfig) for i in range(len(sysconfig)): fid_writer.writerow(sysconfig[i]) fid.close()
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)
# Generate input system configuration: ############################################################################################################ input.inputgenerator.gen_random_input_3D(filename = "input/testinput.csv", n_particles = 30, boxsize = boxsize, r_c = r_cut_LJ + r_skin_LJ) sysconfig = [] fid = open("input/testinput.csv","r") fid_reader = csv.reader(fid, delimiter=',', quoting=csv.QUOTE_NONNUMERIC) for row in fid_reader: sysconfig.append(row) 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)
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)