def test_generate_trial_position_2(self): position = np.array([0.5, 0.5, 0.5]) # 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) distances = [] for i in range(10000): trial_position = MetropolisMonteCarlo._generate_trial_position( position, para) distances.append(np.linalg.norm(position - trial_position)) distances = np.array(distances) npt.assert_approx_equal(distances.sum() / 10000, 0.5, significant=2)
def test_shift_position(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., 14.]), es_sigma=0.5, update_radius=1, particle_types=particle_type, cutoff_radius=3, K_cutoff=1) reference_pos1 = np.array([1.5, 1.0, 1.5]) reference_pos2 = np.array([10.5, 12.0, 5.5]) particle_position_1 = np.array([13.5, 14., 15.5]) shifted_position_1 = MetropolisMonteCarlo._shift_position( particle_position_1, parameters) particle_position_2 = np.array([-1.5, -1., 5.5]) shifted_position_2 = MetropolisMonteCarlo._shift_position( particle_position_2, parameters) npt.assert_equal(reference_pos1, shifted_position_1, 'Failed', verbose=True) npt.assert_equal(reference_pos2, shifted_position_2, 'Failed', verbose=True)
def test_generate_trial_position_1(self): position = np.array([0.5, 0.5, 0.5]) # 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) for i in range(1000): trial_position = MetropolisMonteCarlo._generate_trial_position( position, para) distance = np.linalg.norm(position - trial_position) if distance > para.update_radius: raise AssertionError("Distance greater than update radius")
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_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_kvector(self): Parameter = Parameters(temperature=1, box=[1, 1, 1], es_sigma=2, update_radius=0.5, cutoff_radius=1, K_cutoff=1, particle_types=[0]) print(Parameter.k_vector)
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 test_1Dkvector(self): # setting up 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) parameters = Parameters(temperature=1, box=np.array([1]), update_radius=0.5, accuracy=0.5, charges=charges, lj_sigmas=lj_sigmas, lj_epsilons=lj_epsilons, update_probability=0.5) reference = [[-2], [-1]] npt.assert_array_equal(parameters.k_vector, reference)
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 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_shift_position_1(self): position = np.array([0.5, 0.5, 0.5]) # 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]), update_radius=1, charges=charges, lj_sigmas=lj_sigmas, lj_epsilons=lj_epsilons, update_probability=0.5, accuracy=1) actual = MetropolisMonteCarlo._shift_position(position, para) npt.assert_array_equal(actual, position)
def test_shift_position_4(self): position = np.array([-1.3, 2.4, -3.4]) # 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) actual = MetropolisMonteCarlo._shift_position(position, para) reference = np.array([0.7, 0.4, 0.6]) npt.assert_array_almost_equal(actual, reference)
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)
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_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)
# 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) 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
from Particle_Simulation.ToolBox import ToolBox from Particle_Simulation.Parameters import Parameters from Particle_Simulation.System import System from Particle_Simulation.Simulation import Simulation data = np.load('/home/max/Downloads/sodium-chloride-example.npz') print(data['parameters']) 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) lj_epsilons = lj_epsilons * 1000 / avogadro_constant lj_sigmas = lj_sigmas / 10 parameters = Parameters(temperature=800, box=box, charges=charges, lj_epsilons=lj_epsilons, lj_sigmas=lj_sigmas, accuracy=15, update_radius=0.01) initial_system = System(particles, parameters) simulation = Simulation(initial_system, parameters) simulation.optimize_annealing(6000) simulation.parameters.temperature = 300 simulation.simulate(2500) i = 0 j = 0 ToolBox.plot_overall_energy_trend(simulation.opt_systems) ToolBox.plot_overall_energy_trend(simulation.sim_systems) ToolBox.plot_system(simulation.opt_systems[-1], simulation.parameters)