def test_species(self): """ This test is known to fail because RUMD sample do not return the particle types as array and therefore they are not propagated when particles are not sorted by species """ from atooms.backends.rumd import System # Create a new input file with one particle species changed self.input_file = os.path.join(os.path.dirname(__file__), '../data/ka_N256_rho1.185_rumd.xyz.gz') from atooms.core.utils import mkdir mkdir('/tmp/test_rumd_species/') with TrajectoryRUMD(self.input_file) as th: system = th[-1] system.particle[0].species = system.particle[-1].species with TrajectoryRUMD('/tmp/test_rumd_species/input.xyz.gz', 'w') as th: th.write(system) si = Simulation(self.backend, output_path='/tmp/test_rumd_species/trajectory', steps=2000, checkpoint_interval=100, restart=False) self.assertEqual(system.particle[0].species, system.particle[-1].species) si.add(write_config, 100) si.run() with SuperTrajectoryRUMD('/tmp/test_rumd_species/trajectory') as th: system = th[-1] self.assertEqual(system.particle[0].species, system.particle[-1].species, 'rumd requires ordered types')
def test_nvt_nofix(self): import sys import random from atooms.system import Thermostat cmd = """ pair_style lj/cut 2.5 pair_coeff 1 1 1.0 1.0 2.5 neighbor 0.3 bin neigh_modify every 20 delay 0 check no timestep 0.002 """ random.seed(1) T = [] def store(sim, T): T.append(sim.system.temperature) bck = LAMMPS(self.input_file, cmd) sim = Simulation(bck) sim.system.temperature = 1.4 sim.system.thermostat = Thermostat(2.0) sim.add(store, 500, T) sim.run(4000) ave = sum(T[3:]) / len(T[3:]) self.assertAlmostEqual(ave, 2.0, places=1)
def test_nvt(self): import sys import random cmd = """ pair_style lj/cut 2.5 pair_coeff 1 1 1.0 1.0 2.5 neighbor 0.3 bin neigh_modify every 20 delay 0 check no fix 1 all nvt temp 2.0 2.0 0.2 timestep 0.002 """ def store(sim, T, U): T.append(sim.system.temperature) U.append(sim.system.potential_energy(per_particle=True)) with TrajectoryXYZ(self.input_file) as th: system = th[-1] for inp in [self.input_file, TrajectoryXYZ(self.input_file), system]: T, U = [], [] random.seed(1) bck = LAMMPS(inp, cmd) sim = Simulation(bck) sim.system.temperature = 1.5 sim.add(store, 500, T, U) sim.run(2000) ave = sum(T[3:]) / len(T[3:]) self.assertAlmostEqual(ave, 2.0, places=1) if isinstance(inp, TrajectoryXYZ): inp.close()
def test_target_walltime(self): """Check that walltime targeting works.""" from atooms.simulation.observers import target_walltime f = '/tmp/test_simulation/config/trajectory' s = Simulation(DryRun(), output_path=f, steps=1000000000) s.add(target_walltime, Scheduler(20), 1.) s.run() self.assertTrue(s.wall_time() > 1.)
def test_python_stop(self): from atooms.simulation import target_python_stop f = '/tmp/test_simulation/python/trajectory' s = Simulation(DryRun(), output_path=f) s.add(target_python_stop, Scheduler(20), '{current_step} == 40') s.add(write_thermo, Scheduler(10)) s.run(100) self.assertEqual(s.current_step, 40)
def test_target_rmsd(self): f = '/tmp/test_simulation/config/trajectory' with self.assertRaises(IndexError): s = Simulation(DryRun(), output_path=f, steps=100) s.add(target_rmsd, Scheduler(20)) s.run() s = Simulation(DryRun(), output_path=f, steps=100) s.add(target_rmsd, Scheduler(20), 1.0) s.run()
def test_target_restart_fake(self): f = '/tmp/test_simulation/restart/trajectory' s = Simulation(DryRun(), output_path=f) #s.add(WriterThermo(), Scheduler(20)) s.add(write_thermo, Scheduler(20)) s.run(100) s.run(100) data = numpy.loadtxt(f + '.thermo', unpack=True) self.assertEqual(int(data[0][-1]), 200)
def test_block(self): """Test periodic block scheduling""" def store_list(s, db): db.append(s.current_step) db = [] s = Simulation(DryRun(), output_path=None, steps=18) s.add(store_list, Scheduler(block=[1, 2, 4, 8]), db=db) s.run() self.assertEqual(db, [0, 1, 2, 4, 8, 9, 10, 12, 16, 17, 18])
def test_multi_rmsd(self): si = Simulation(self.backend, output_path='/tmp/test_rumd_multi_rmsd/trajectory', checkpoint_interval=100, steps=1000000000, restart=False) si.add(write_thermo, 100) si.add(write_config, 100) si.add(target, 1000, 'rmsd', 1.0) si.run()
def test_steps(self): """Test steps scheduling""" def store_list(s, db): db.append(s.current_step) db = [] s = Simulation(DryRun(), output_path=None, steps=18) s.add(store_list, Scheduler(steps=[1, 2, 4, 8]), db=db) s.run() self.assertEqual(db, [0, 1, 2, 4, 8])
def main(params): if params.verbose: setup_logging(level=20) if params.debug: setup_logging(level=10) if params.T is not None: params.integrator = 'nvt' if os.path.exists(params.input_file + '.ff'): params.forcefield = params.input_file + '.ff' output_base = os.path.join(params.output_dir, 'trajectory') mkdir(output_base) cp(params.forcefield, output_base + '.ff') cp(params.forcefield, output_base + '.chk.ff') report_parameters(params.__dict__, output_base + '.params', '%s+%s' % (__version__, __commit__)) report_command(sys.argv[0], params.__dict__, ['output_dir'], output_base + '.cmd') s = RUMD(params.input_file, params.forcefield, integrator=params.integrator, temperature=params.T, dt=params.dt, fixcm_interval=params.fixcm_interval) sa = Simulation(s, output_path=output_base, checkpoint_interval=params.checkpoint_interval, steps=params.steps, restart=params.restart) if params.backend_output: s._suppress_all_output = False s._initialize_output = True s.rumd_simulation.sample.SetOutputDirectory(output_base) s.rumd_simulation.SetOutputScheduling("energies", "linear", interval=params.thermo_interval) s.rumd_simulation.SetOutputScheduling("trajectory", params.config_sampling, interval=params.config_interval) s.rumd_simulation.SetOutputMetaData("trajectory", precision=6, virials=False) if params.config_interval > 0 and params.config_sampling == "logarithmic": s.rumd_simulation.SetBlockSize(params.config_interval) else: s.rumd_simulation.SetBlockSize(params.steps) # Trim target steps to be a multiple of config_interval # params.steps = params.steps / params.config_interval * params.config_interval else: sa.add(write_thermo, Scheduler(params.thermo_interval)) sa.add(write_config, Scheduler(params.config_interval)) sa.run()
def test_unfold(self): from atooms.backends.rumd import unfold def unf(sim): unfold(sim.system).particle[0].position si = Simulation(self.backend, output_path='/tmp/test_rumd_single/trajectory', steps=2000, restart=False) si.add(unf, 100) si.run()
def test_single(self): from atooms.backends.rumd import System si = Simulation(self.backend, output_path='/tmp/test_rumd_single/trajectory', steps=2000, checkpoint_interval=100, restart=False) s = System(self.backend.rumd_simulation.sample) si.add(write_thermo, 100) si.add(write_config, 100) si.run() ls = glob.glob('/tmp/test_rumd_single/trajectory/*') self.assertEqual(len(ls), 21)
def test_multi_2(self): si = Simulation(self.backend, output_path='/tmp/test_rumd_multi_2/trajectory', steps=2000, checkpoint_interval=100, restart=False) si.add(write_thermo, 100) si.add(write_config, 100) si.run() si = Simulation(self.backend_2, output_path='/tmp/test_rumd_multi_2/trajectory', steps=1000, checkpoint_interval=100, restart=False) si.add(write_thermo, 100) si.add(write_config, 100) si.run()
def test_multi(self): si = Simulation(self.backend, output_path='/tmp/test_rumd_multi/trajectory', steps=2000, checkpoint_interval=100, restart=False) si.add(write_thermo, 50) si.add(write_config, 100) si.run() si.run(1000) ls = glob.glob('/tmp/test_rumd_multi/trajectory/*') self.assertEqual(len(ls), 31) self.assertEqual(si.current_step, 3000) tmp = open('/tmp/test_rumd_multi/trajectory.thermo', 'r').readlines() steps = int(tmp[-1].split()[0]) self.assertEqual(steps, 3000) self.assertEqual(len(tmp), 61 + 1) # one is for comment line
def test_shell_stop(self): from atooms.simulation import shell_stop f = '/tmp/test_simulation/shell/trajectory' s = Simulation(DryRun(), output_path=f) s.add(shell_stop, Scheduler(steps=[20]), 'exit 1') s.add(write_thermo, Scheduler(10)) s.run(100) self.assertEqual(s.current_step, 20) s = Simulation(DryRun(), output_path=f) s.add(shell_stop, Scheduler(20), 'exit 0') s.add(write_thermo, Scheduler(10)) s.run(100) self.assertEqual(s.current_step, 100) # Test formatted string s = Simulation(DryRun(), output_path=f) s.add(shell_stop, Scheduler(20), '[ {sim.current_step} -eq 40 ] && exit 1 || exit 0') s.run(100) self.assertEqual(s.current_step, 40)
def test_config(self): from atooms.trajectory import TrajectoryXYZ f = '/tmp/test_simulation/config/trajectory.xyz' # We do not accept too deep introspection with self.assertRaises(ValueError): # Mute errors temporarily setup_logging(level=50, update=True) s = Simulation(DryRun(), output_path=f, enable_speedometer=False, steps=100) s.add(write, Scheduler(20), 'output', ['system.particle.position']) s.run() # Test generic writer and write_config setup_logging(level=40, update=True) s = Simulation(DryRun(), output_path=f, enable_speedometer=True, steps=100) s.trajectory_class = TrajectoryXYZ s.add(write_config, Scheduler(20)) s.add(write, Scheduler(20), 'output', ['current_step', 'system.cell']) s.run() import os self.assertTrue(os.path.exists(f)) self.assertTrue(os.path.exists(f + '.output'))
def test_ram_lammps_write(self): import os import numpy import sys from atooms.backends.lammps import LAMMPS from atooms.simulation import Simulation from atooms.simulation.observers import write_to_ram input_file = os.path.join(os.path.dirname(__file__), '../data/lj_N1000_rho1.0.xyz') cmd = """ pair_style lj/cut 2.5 pair_coeff 1 1 1.0 1.0 2.5 neighbor 0.3 bin neigh_modify every 20 delay 0 check no fix 1 all nve """ bck = LAMMPS(input_file, cmd) ram = TrajectoryRamFull() sim = Simulation(bck) sim.add(write_to_ram, 10, ram) sim.run(100) self.assertEqual(len(ram.steps), 11)
def test_multi_writing(self): # Test that we cumulate current_step and configurations si = Simulation(self.backend, output_path='/tmp/test_rumd_multi_writing/trajectory', enable_speedometer=False) si.add(write_config, 500) si.run(10000) si.run(5000) ls = glob.glob('/tmp/test_rumd_multi_writing/trajectory/*') self.assertEqual(len(ls), 31) self.assertEqual(si.current_step, 15000) # This second simulation tests that the folder gets cleared up si = Simulation(self.backend, steps=5000, output_path='/tmp/test_rumd_multi_writing/trajectory', enable_speedometer=False) si.add(write_config, 500) si.run() si.run() ls = glob.glob('/tmp/test_rumd_multi_writing/trajectory/*') self.assertEqual(len(ls), 21) self.assertEqual(si.current_step, 10000)
def test_target_rmsd(self): s = Simulation(self.backend, self.dout, steps=sys.maxsize) s.add(target, 10, 'rmsd', 0.3) s.run() self.assertGreater(s.current_step, 1) self.assertGreater(s.rmsd, 0.3)
self.system = system self.delta = delta def run(self, steps): for i in range(steps): for nr, particle in enumerate(self.system.particle): #TODO: dodaj zmianę pozycji w każdym kroku o wartość [nr+1] w każdym kierunku pass #callback function, called by atooms after specified amount of steps def callback(sim, initial_position, db=None): positions = numpy.array([x.position for x in sim.system.particle]) #TODO: narysuj pozycje punktów w każdym kroku # hint: ax2d.scatter(?,?,label=sim.current_step), ax3d.scatter(?,?,?,label=sim.current_step) ax2d = plt.subplot(121) ax3d = plt.subplot(122, projection='3d') system = System( particle=[Particle(position=[10.0, 10.0, 10.0]) for _ in range(10)]) simulation = Simulation(RandomWalk(system)) simulation.add(callback, 5, initial_position=[p.position.copy() for p in system.particle]) simulation.run(10) simulation.run(10) simulation.run(10) plt.show()