def simulate(self, n_steps): # set up initial system from optimized system current_system = System(self.opt_systems[-1].particles, self.parameters) # pass particle positions and neighbourlist to the energy calculator class self.energy_calculator.set_system( current_system.neighbourlist.particle_positions, current_system.neighbourlist.cell_list, current_system.neighbourlist.particle_neighbour_list) # calculate energy of initial system current_system.energy = self._calculate_overall_energy() # append to trajectory self.sim_systems.append(current_system) for i in range(n_steps): # generate trial system trial_system = MetropolisMonteCarlo.generate_trial_configuration( self.sim_systems[-1], self.parameters) # update particle positions and neighbourlist self.energy_calculator.set_system( trial_system.neighbourlist.particle_positions, trial_system.neighbourlist.cell_list, trial_system.neighbourlist.particle_neighbour_list) # calculate energy of trial system trial_system.energy = self._calculate_overall_energy() # evaluate system and trial system and append the accepted system to the trajectory self.sim_systems.append( MetropolisMonteCarlo.evaluate_trial_configuration( self.sim_systems[-1], trial_system, self.parameters))
def optimize(self, n_steps): # set up initial system current_system = System(self.system.particles, self.parameters) # pass particle positions and neighbourlist to the energy calculator class self.energy_calculator.set_system( current_system.neighbourlist.particle_positions, current_system.neighbourlist.cell_list, current_system.neighbourlist.particle_neighbour_list) # calculate energy of initial system current_system.energy = self._calculate_overall_energy() # append to optimize trajectory self.opt_systems.append(current_system) # crude optimization for i in range(n_steps): # generate trial system trial_system = MetropolisMonteCarlo.generate_trial_configuration( self.opt_systems[-1], self.parameters) # update particle positions and neighbourlist self.energy_calculator.set_system( trial_system.neighbourlist.particle_positions, trial_system.neighbourlist.cell_list, trial_system.neighbourlist.particle_neighbour_list) # calculate energy of trial system trial_system.energy = self._calculate_overall_energy() # evaluate system and trial system and append the accepted system to the trajectory self.opt_systems.append( MetropolisMonteCarlo.evaluate_trial_configuration_greedy( self.opt_systems[-1], trial_system)) # interim update_radius update_radius = self.parameters.update_radius self.parameters.update_radius = update_radius / 10 # fine optimization for i in range(100): # generate trial system trial_system = MetropolisMonteCarlo.generate_trial_configuration( self.opt_systems[-1], self.parameters) # update particle positions and neighbourlist self.energy_calculator.set_system( trial_system.neighbourlist.particle_positions, trial_system.neighbourlist.cell_list, trial_system.neighbourlist.particle_neighbour_list) # calculate energy of trial system trial_system.energy = self._calculate_overall_energy() # evaluate system and trial system and append the accepted system to the trajectory self.opt_systems.append( MetropolisMonteCarlo.evaluate_trial_configuration_greedy( self.opt_systems[-1], trial_system)) # resetting update_radius to desired value self.parameters.update_radius = update_radius
def test_evaluate_trial_configuration_greedy_3(self): # set test parameters charges = np.ones(10).astype(np.float32) lj_sigmas = np.ones(10).astype(np.float32) lj_epsilons = np.ones(10).astype(np.float32) para = Parameters(temperature=1, box=np.array([1, 1, 1]), es_sigma=1, update_radius=1, charges=charges, lj_sigmas=lj_sigmas, lj_epsilons=lj_epsilons, update_probability=0.5, accuracy=1) particle1 = Particle(np.array([0.5, 0.5, 0.5])) particle2 = Particle(np.array([0.5, 0.5, 0.1])) particles = [particle1, particle2] system = System(particles=particles, parameters=para) system.energy.overall_energy = 1.1 trial_system = System(particles=particles, parameters=para) trial_system.energy.overall_energy = 1 actual = MetropolisMonteCarlo.evaluate_trial_configuration_greedy( system, trial_system) npt.assert_equal(actual, trial_system)
def simulate(self, n_steps): raise NotImplementedError() current_system = System(self.system.particles, self.parameters) current_system.energy = calculate_energy() # not implemented yet self.sim_systems.append(current_system) for i in range(n_steps): trial_system = MetropolisMonteCarlo.generate_trial_configuration( self.sim_systems[-1], self.parameters) trial_system.energy = calculate_energy() # not implemented yet self.sim_systems.append( MetropolisMonteCarlo.evaluate_trial_configuration( self.sim_systems[-1], trial_system))
def optimize(self, n_steps): raise NotImplementedError() current_system = System(self.system.particles, self.parameters) current_system.energy = calculate_energy() # not implemented yet for i in range(n_steps): trial_system = MetropolisMonteCarlo.generate_trial_configuration( current_system, self.parameters) trial_system.energy = calculate_energy() # not implemented yet current_system = MetropolisMonteCarlo.evaluate_trial_configuration_greedy( current_system, trial_system) self.opt_system = current_system
def test_longrange(self): particle_type = ParticleType(name="Natrium", mass=2, charge=2, lj_epsilon=1.25, lj_sigma=0.5) particle_type = np.array([particle_type]) parameters = Parameters(temperature=0, box=np.array([1, 1, 1]), es_sigma=0.5, update_radius=1, particle_types=particle_type, cutoff_radius=0.5, K_cutoff=2) particle_1 = Particle(position=np.array([1, 2, 3]), type_index=0) particle_2 = Particle(position=np.array([2, 3.5, 6]), type_index=0) particles = np.array([particle_1, particle_2]) system = System(particles, parameters)
def generate_trial_configuration(system, parameters): n_particles = len(system.particles) update_probability = parameters.update_probability trial_particles = [] for i in range(n_particles): trial_particles.append( Particle(np.zeros(len(system.particles[0].position)))) for i in range(0, n_particles): random_number = np.random.rand(1)[0] if random_number <= update_probability: trial_particles[ i].position = MetropolisMonteCarlo._generate_trial_position( system.particles[i].position, parameters) else: trial_particles[i].position = system.particles[i].position return System(trial_particles, parameters)
def setup_random_simulation(box_length): cr = [] for i in range(box_length): cr.append(i + 0.5) x, y, z = np.meshgrid(cr, cr, cr) xyz = np.vstack((x.flat, y.flat, z.flat)) xyz = np.ascontiguousarray(xyz) particles = [] for i in range(len(xyz[0, :])): particles.append(Particle(np.array(xyz[:, i]))) charges = np.zeros(len(particles)) lj_sigmas = np.zeros(len(particles)) lj_epsilons = np.zeros(len(particles)) avogadro_constant = 6.02214085774 * 10**(23) for i in range(len(particles)): if i % 2 == 0: charges[i] = 1 lj_sigmas[i] = 0.33 lj_epsilons[i] = 11.6 / avogadro_constant elif i % 2 == 1: charges[i] = -1 lj_sigmas[i] = 0.44 lj_epsilons[i] = 418.4 / avogadro_constant parameters = Parameters(temperature=300, box=np.array( [box_length, box_length, box_length]), charges=charges, lj_sigmas=lj_sigmas, lj_epsilons=lj_epsilons, accuracy=10) system = System(particles, parameters) simulation = Simulation(system, parameters) return simulation
# initialize parameters object parameters = Parameters(temperature=100, box=box, charges=charges, lj_epsilons=lj_epsilons, lj_sigmas=lj_sigmas, accuracy=2) print("Starting simulation with the following parameters:") print("Temperature: ", parameters.temperature) print("Accuracy: ", parameters.accuracy) print("Cutoff radius: ", parameters.cutoff_radius) print("K cutoff: ", parameters.K_cutoff) print("Update probability: ", parameters.update_probability) print("Update radius: ", parameters.update_radius) print("ES sigma: ", parameters.es_sigma) initial_system = System(particles, parameters) # ToolBox.plot_system(initial_system, parameters) steps_opt = 5000 steps_sim = 2000 simulation = Simulation(initial_system, parameters) simulation.parameters.update_radius = np.sum(simulation.parameters.box) / len(simulation.parameters.box) * 0.0005 simulation.optimize(steps_opt) simulation.parameters.temperature = 1000 simulation.parameters.update_radius = np.sum(simulation.parameters.box) / len(simulation.parameters.box) * 0.0005 simulation.simulate(steps_sim) i = 0 j = 0 ToolBox.plot_overall_energy_trend(simulation.opt_systems)