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 test__generate_trial_position_redundancy(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([12., 13., 14.]), es_sigma=0.5, update_radius=1, particle_types=particle_type, cutoff_radius=0.5, K_cutoff=1) particle_1 = Particle(position=np.array([1, 2, 3]), type_index=0) particle_2 = Particle(position=np.array([1, 2, 3]), type_index=0) trial_position1 = np.array( MetropolisMonteCarlo._generate_trial_position( particle_1.position, parameters)) trial_position2 = np.array( MetropolisMonteCarlo._generate_trial_position( particle_1.position, parameters)) x = 0 if np.array_equal(trial_position1, trial_position2): x = 1 else: x = 2 npt.assert_equal(x, 2, 'Failed', verbose=True)
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 test_shortrange_potential(self): reference_potential = -0.000042499 print(reference_potential) particle_type = ParticleType(name="Hydrogen", mass=3, charge=1, shortrange_epsilon=1.25, shortrange_sigma=0.5) particle_type = np.array([particle_type]) parameters = Parameters(temperature=0, es_sigma=0, update_radius=1, particle_types=particle_type, box=np.array([1, 2, 1]), cutoff_radius=1) particle_1 = Particle(position=np.array([1, 2, 2]), type_index=0) particle_2 = Particle(position=np.array([2, 3.5, 4]), type_index=0) shortrange_value = shortrange._calculate_potential(particle_1, particle_2, parameters) shortrange_value_rounded = shortrange_value.round(decimals=9) print(shortrange_value_rounded) npt.assert_equal(reference_potential, shortrange_value_rounded, 'Failed', verbose=True)
def test_calculate_lennardjones_potential(self): reference_potential = -0.000042499 print(reference_potential) 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, es_sigma=0, update_radius=1, particle_types=particle_type, box=np.array([1, 1, 1]), cutoff_radius=1, 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) lg_value = LennardJones._calculate_potential(particle_1, particle_2, parameters) lg_value_rounded = lg_value.round(decimals=9) print(lg_value_rounded) npt.assert_equal(reference_potential, lg_value_rounded, 'Failed', verbose=True)
def get_inputs(file_path): #Load arrays from .npz file with np.load(file_path) as fh : box = fh['box'] particle_positions = fh['positions'] types = fh['types'] readme = fh['readme'] #scalarization (parameters.npy to dictionary) parameters = fh['parameters'].item() #fetch values from dictionary and create input ndarrays prmtr_vals = [] for i in range(len(types)): prmtr_vals.append(parameters.get(types[i])) prmtr_vals = np.asarray(prmtr_vals) name = types lj_sigmas = prmtr_vals[ : , 0] lj_epsilons = prmtr_vals[ : , 1] mass = prmtr_vals[ : , 2] charges = prmtr_vals[ : , 3] #Particle object: particle_list = [] for i in range(len(particle_positions)): particle_obj = Particle(position=particle_positions[i]) particle_list.append(particle_obj) particle = np.array(particle_list) return particle, box, particle_positions, types, name, lj_sigmas, lj_epsilons, mass, charges, readme
def calculate_shortranged_energy(self, system): lj_energy = 0 short_ranged_energy = 0 neighbour_cell_number = 3**system.neighbourlist.dim for i in range(system.neighbourlist.total_cell_number): particle_index_1 = system.neighbourlist.cell_list[i] while particle_index_1 != -1: for k in range(neighbour_cell_number): cell_index = System.cell_neighbour_list[k][i][0] particle_index_2 = system.neighbourlist.cell_list[ cell_index] while particle_index_2 != -1: particle_1 = system.particles[particle_index_1] particle_2 = system.particles[particle_index_2] if particle_index_1 != particle_index_2: if System.cell_neighbour_list[k][i][1] == 0: if particle_index_1 < particle_index_2: particle_distance = np.linalg.norm( particle_1.position - particle_2.position) if particle_distance < self.parameters.cutoff_radius: lj_energy += LennardJones.calculate_potential( particle_1, particle_2, self.parameters) short_ranged_energy += EwaldSummation.calculate_shortranged_potential( particle_1, particle_2, self.parameters) elif System.cell_neighbour_list[k][i][1] != 0: box_shift = self._determine_box_shift(i, k) particle_2 = Particle( type_index=particle_2.type_index, position=particle_2.position + box_shift) particle_distance = np.linalg.norm( particle_1.position - particle_2.position) if particle_distance < self.parameters.cutoff_radius: lj_energy += LennardJones.calculate_potential( particle_1, particle_2, self.parameters) short_ranged_energy += EwaldSummation.calculate_shortranged_potential( particle_1, particle_2, self.parameters) particle_index_2 = system.neighbourlist.particle_neighbour_list[ particle_index_2] particle_index_1 = system.neighbourlist.particle_neighbour_list[ particle_index_1] short_ranged_energy *= 1 / (8 * np.pi * Parameters.VACUUM_PERMITTIVITY) return [lj_energy, short_ranged_energy]
def test__generate_trial_position_3(self): particle_type = ParticleType(name="Hydrogen", mass=1.008, charge=1.602, lj_epsilon=0.21, lj_sigma=2.5) particle_type = np.array([particle_type]) parameters = Parameters(temperature=0, box=np.array([12., 13., 13.]), es_sigma=0.5, update_radius=1, particle_types=particle_type, cutoff_radius=0.5) particle_1 = Particle(position=np.array([1, 2, 4]), type_index=1) trial_position1 = np.array( MetropolisMonteCarlo._generate_trial_position( particle_1.position, 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
def test_selfinteraction_potential(self): particle_1 = Particle(position=np.array([1, 0, 0]), type_index=1) particle_2 = Particle(position=np.array([12, 0, 0]), type_index=1) parameters = Parameters(temperature=0, es_sigma=0, update_radius=1, particle_types=particle_type, box=np.array([2, 1, 1]), cutoff_radius=1)
box=np.array([1, 2, 1]), cutoff_radius=1) particle_1 = Particle(position=np.array([1, 2, 2]), type_index=0) particle_2 = Particle(position=np.array([2, 3.5, 4]), type_index=0) shortrange_value = shortrange._calculate_potential(particle_1, particle_2, parameters) shortrange_value_rounded = shortrange_value.round(decimals=9) print(shortrange_value_rounded) npt.assert_equal(reference_potential, shortrange_value_rounded, 'Failed', verbose=True) def test_wrapped_shortrange_potential(self): reference_potential = -0.000042499 print(reference_potential) particle_type = ParticleType(name="Natgerium", mass=1, charge=3, shortrange_epsilon=1.25, shortrange_sigma=0.5) particle_type = np.array([particle_type]) parameters = Parameters(temperature=0, es_sigma=0, update_radius=1, particle_types=particle_type, box=np.array([2, 1, 1]), cutoff_radius=1) particle_1 = Particle(position=np.array([1, 2, 2]), type_index=0) particle_2 = Particle(position=np.array([2, 3.5, 2]), type_index=0) shortrange_value = shortrange. _calculate_potential(particle_1, particle_2, parameters) shortrange_value_rounded = shortrange_value.round(decimals=9) print(shortrange_value_rounded) npt.assert_equal(reference_potential, shortrange_value_rounded, 'Failed', verbose=True) distance = shortrange._calculate_distance(particle_1, particle_2) npt.assert_equal(reference_distance, distance, 'Failed', verbose=True) def test_selfinteraction_potential(self): particle_1 = Particle(position=np.array([1, 0, 0]), type_index=1) particle_2 = Particle(position=np.array([12, 0, 0]), type_index=1) parameters = Parameters(temperature=0, es_sigma=0, update_radius=1, particle_types=particle_type, box=np.array([2, 1, 1]), cutoff_radius=1) npt.assert_equal(selfinteraction_potential, 'Failed', verbose=True)
def test_calculate_distance_2(self): reference_distance = 3.5 particle_1 = Particle(position=np.array([1, 2, 3]), type_index=1) particle_2 = Particle(position=np.array([2, 3.5, 6]), type_index=1) distance = LennardJones._calculate_distance(particle_1, particle_2) npt.assert_equal(reference_distance, distance, 'Failed', verbose=True)
particles, box, particle_positions, types, name, lj_sigmas, lj_epsilons, mass, charges, readme = ToolBox.get_inputs( '/home/max/Downloads/sodium-chloride-example.npz') avogadro_constant = 6.02214085774 * 10 ** (23) # scale parameters lj_epsilons = lj_epsilons * 1000 / avogadro_constant lj_sigmas = lj_sigmas / 10 particle_positions = particle_positions / 10 box = box / 10 particles = [] # create particle array for i in range(len(particle_positions)): particle_obj = Particle(position=particle_positions[i]) particles.append(particle_obj) particles = np.array(particles) # 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)