def test_velocity_verlet(self): a = md.initialise(2, 300, 8, "square") a.particles, a.distances, a.forces, a.energies = md.velocity_verlet( a.particles, 1, a.box_length, a.cut_off, a.constants, a.forcefield, a.mass) assert_almost_equal(a.particles["xprevious_position"] * 1e10, [2, 2]) assert_almost_equal(a.particles["yprevious_position"] * 1e10, [2, 6])
def test_initialise_square(self): a = md.initialise(2, 300, 8, "square") assert_equal(a.number_of_particles, 2) assert_almost_equal(a.box_length, 8e-10) assert_almost_equal(a.init_temp, 300) assert_almost_equal(a.particles["xposition"] * 1e10, [2, 2]) assert_almost_equal(a.particles["yposition"] * 1e10, [2, 6])
def periodic_boundary(number_of_steps, temperature): """This is a piece of exemplary code to show a single particle traveling across the periodic boundary. Parameters ---------- number_of_steps: int Number of step in simulation. temperature: float Temperature of simulation. """ number_of_particles = 1 sample_freq = 10 system = md.initialise(number_of_particles, temperature, 50, 'square') sampling = sample.JustCell(system) system.time = 0 for i in range(0, number_of_steps): system.particles, system.distances, system.forces = comp.compute_forces(system.particles, system.distances, system.forces, system.box_length) system.particles = md.velocity_verlet(system.particles, system.timestep_length, system.box_length) system = md.sample(system.particles, system.box_length, system.initial_particles, system) system.particles = comp.heat_bath(system.particles, system.temperature_sample, temperature) system.time += system.timestep_length system.step += 1 if system.step % sample_freq == 0: sampling.update(system)
def md_nve(number_of_particles, temperature, box_length, number_of_steps, sample_frequency): """This is an example NVE (constant number of particles, system volume and system energy) simulation. Parameters ---------- number_of_particles: int Number of particles to simulate. temperature: float Initial temperature of the particles, in Kelvin. box_length: float Length of a single dimension of the simulation square, in Angstrom. number_of_steps: int Number of integration steps to be performed. sample_frequency: int Frequency with which the visualisation environment is to be updated. Returns ------- System System information. """ system = md.initialise(number_of_particles, temperature, box_length, 'square') sample_system = sample.Interactions(system) system.time = 0 for i in range(0, number_of_steps): system.particles, system.distances, system.forces = comp.compute_forces(system.particles, system.distances, system.forces, system.box_length) system.particles = md.velocity_verlet(system.particles, system.timestep_length, system.box_length) system = md.sample(system.particles, system.box_length, system.initial_particles, system) system.time += system.timestep_length system.step += 1 if system.step % sample_frequency == 0: sample_system.update(system) return system
def pbc(): number_of_particles = 1 temperature = 100 box_length = 30 number_of_steps = 10000 sample_frequency = 25 # Initialise the system system = md.initialise(number_of_particles, temperature, box_length, 'square') system.particles['xvelocity'] = (np.random.randn() - 0.5) * 14 system.particles['yvelocity'] = (np.random.randn() - 0.5) * 14 # This sets the sampling class sample_system = sample.JustCell(system) # Start at time 0 system.time = 0 # Begin the molecular dynamics loop for i in range(0, number_of_steps): # At each step, calculate the forces on each particle and get # acceleration system.compute_force() # Run the equations of motion integrator algorithm system.integrate(md.velocity_verlet) # Sample the thermodynamic and structural parameters of the system system.md_sample() # Allow the system to interact with a heat bath system.heat_bath(temperature) # Iterate the time system.time += system.timestep_length system.step += 1 # At a given frequency sample the positions and plot the RDF if system.step % sample_frequency == 0: sample_system.update(system)
def test_calculate_temperature(self): a = md.initialise(1, 300, 8, "square") a.particles["xvelocity"] = [1e-10] a.particles["yvelocity"] = [1e-10] a.particles["xacceleration"] = [1e4] a.particles["yacceleration"] = [1e4] b = md.calculate_temperature(a.particles, mass=39.948) assert_almost_equal(b * 1e23, 4.8048103702737945)
def test_update_velocities(self): a = md.initialise(2, 300, 8, 'square') a.particles['xvelocity'] = 1e-10 a.particles['yvelocity'] = 1e-10 a.particles['xacceleration'] = 1e4 a.particles['yacceleration'] = 1e4 xacceleration_new = 2e4 yacceleration_new = 2e4 b = md.update_velocities( [a.particles['xvelocity'], a.particles['yvelocity']], [xacceleration_new, yacceleration_new], [a.particles['xacceleration'], a.particles['yacceleration']], a.timestep_length) assert_almost_equal(b[0][0] * 1e10, 2.5) assert_almost_equal(b[1][0] * 1e10, 2.5) assert_almost_equal(b[0][1] * 1e10, 2.5) assert_almost_equal(b[1][1] * 1e10, 2.5)
def test_update_positions(self): a = md.initialise(2, 300, 8, 'square') a.particles['xvelocity'] = 1e4 a.particles['yvelocity'] = 1e4 a.particles['xacceleration'] = 1e4 a.particles['yacceleration'] = 1e4 b, c = md.update_positions( [a.particles['xposition'], a.particles['yposition']], [ a.particles['xprevious_position'], a.particles['yprevious_position'] ], [a.particles['xvelocity'], a.particles['yvelocity']], [a.particles['xacceleration'], a.particles['yacceleration']], a.timestep_length, a.box_length) assert_almost_equal(b[0][0] * 1e10, 3) assert_almost_equal(b[1][0] * 1e10, 3) assert_almost_equal(b[0][1] * 1e10, 3) assert_almost_equal(b[1][1] * 1e10, 7)
def md_simulation( number_of_particles, temperature, box_length, number_of_steps ): system = md.initialise( number_of_particles, temperature, box_length, "square" ) sample_system = sample.Energy(system) system.time = 0 for i in range(0, number_of_steps): system.integrate(md.velocity_verlet) system.md_sample() system.heat_bath(temperature) system.time += system.timestep_length system.step += 1 if system.step % 10 == 0: sample_system.update(system) return system
def test_update_velocities(self): a = md.initialise(2, 300, 8, "square") a.particles["xvelocity"] = 1e-10 a.particles["yvelocity"] = 1e-10 a.particles["xacceleration"] = 1e4 a.particles["yacceleration"] = 1e4 xacceleration_new = 2e4 yacceleration_new = 2e4 b = md.update_velocities( [a.particles["xvelocity"], a.particles["yvelocity"]], [xacceleration_new, yacceleration_new], [a.particles["xacceleration"], a.particles["yacceleration"]], a.timestep_length, ) assert_almost_equal(b[0][0] * 1e10, 2.5) assert_almost_equal(b[1][0] * 1e10, 2.5) assert_almost_equal(b[0][1] * 1e10, 2.5) assert_almost_equal(b[1][1] * 1e10, 2.5)
def simulation(temperature): """ Runs a molecular dynamics simulation in suing the pylj molecular dynamics engine. Parameters ---------- number_of_particles: int The number of particles in the simulation temperature: float The temperature for the initialisation and thermostating box_length: float The length of the simulation square number_of_steps: int The number of molecular dynamics steps to run sample_frequency: How regularly the visualisation should be updated Returns ------- pylj.util.System The complete system information from pylj """ # Initialise the system system = md.initialise(20, temperature, 20, 'square') # This sets the sampling class sample_system = sample.JustCell(system) # Start at time 0 system.time = 0 # Begin the molecular dynamics loop for i in range(0, 1000): # Run the equations of motion integrator algorithm, this # includes the force calculation system.integrate(md.velocity_verlet) # Sample the thermodynamic and structural parameters of the system system.md_sample() # Allow the system to interact with a heat bath system.heat_bath(temperature) # Iterate the time system.time += system.timestep_length system.step += 1 # At a given frequency sample the positions and plot the RDF if system.step % 10 == 0: sample_system.update(system) return system
def simulation(temperature): """ Runs a molecular dynamics simulation in suing the pylj molecular dynamics engine. Parameters ---------- temperature: float The temperature for the initialisation and thermostating Returns ------- pylj.util.System The complete system information from pylj """ # Initialise the system system = md.initialise(2, temperature, 10, 'square', constants=[440.5, 1.522e-10], forcefield=bond) system.cut_off = 30 system.particles['xposition'] = [5e-10, 6e-10] system.particles['yposition'] = [5e-10, 6e-10] # This sets the sampling class sample_system = sample.JustCell(system, scale=2) # Start at time 0 system.time = 0 # Begin the molecular dynamics loop for i in range(0, 4000): # Run the equations of motion integrator algorithm, this # includes the force calculation system.integrate(md.velocity_verlet) # Sample the thermodynamic and structural parameters of the system system.md_sample() # Allow the system to interact with a heat bath system.heat_bath(temperature) # Iterate the time system.time += system.timestep_length system.step += 1 # At a given frequency sample the positions and plot the RDF if system.step % 10 == 0: sample_system.update(system) return system
def test_update_positions(self): a = md.initialise(2, 300, 8, "square") a.particles["xvelocity"] = 1e4 a.particles["yvelocity"] = 1e4 a.particles["xacceleration"] = 1e4 a.particles["yacceleration"] = 1e4 b, c = md.update_positions( [a.particles["xposition"], a.particles["yposition"]], [ a.particles["xprevious_position"], a.particles["yprevious_position"] ], [a.particles["xvelocity"], a.particles["yvelocity"]], [a.particles["xacceleration"], a.particles["yacceleration"]], a.timestep_length, a.box_length, ) assert_almost_equal(b[0][0] * 1e10, 3) assert_almost_equal(b[1][0] * 1e10, 3) assert_almost_equal(b[0][1] * 1e10, 3) assert_almost_equal(b[1][1] * 1e10, 7)
def test_calculate_msd_large(self): a = md.initialise(2, 300, 8, "square") a.particles["xposition"] = [7e-10, 3e-10] a.particles["yposition"] = [7e-10, 7e-10] b = md.calculate_msd(a.particles, a.initial_particles, a.box_length) assert_almost_equal(b, 10e-20)
def test_calculate_msd(self): a = md.initialise(2, 300, 8, 'square') a.particles['xposition'] = [3e-10, 3e-10] a.particles['yposition'] = [3e-10, 7e-10] b = md.calculate_msd(a.particles, a.initial_particles, a.box_length) assert_almost_equal(b, 2e-20)