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_multiple_run_calls(self): """ Multiple calls to run() with varying number of steps should add up correctly. This was not the case in version <= 1.4.3. """ from atooms.system import System # Minimal backend class Backend(object): def __init__(self): self.system = System() def run(self, steps): for i in range(steps): pass backend = Backend() # The run_until() method should work correctly from atooms.simulation import Simulation simulation = Simulation(backend) simulation.run(10) simulation.run_until(30) self.assertEqual(simulation.current_step, 30) # The run() method called twice should also work correctly from atooms.simulation import Simulation simulation = Simulation(backend) simulation.run(10) simulation.run(20) self.assertEqual(simulation.current_step, 30)
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_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_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_no_output(self): # Disable writers completely s = Simulation(DryRun(), output_path=None, steps=10, enable_speedometer=False) s.run()
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_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_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_system(self): """ Test that system in Simulation tracks the one in the backend even when the latter is reassigned. """ s = Simulation(DryRun(), output_path=None, steps=10) s.run() s.backend.system = None self.assertTrue(s.system is s.backend.system)
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_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_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_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_potential(self): import copy si = Simulation(self.backend, output_path='/tmp/test_rumd_single/trajectory', steps=2000, checkpoint_interval=100, restart=False) pos0 = si.system.particle[0].position[0] s = copy.deepcopy(si.system) si.run(100) pos1 = si.system.particle[0].position[0] si.system = s si.run(100) pos2 = si.system.particle[0].position[0] self.assertTrue(abs(pos1 - pos0) > 1e-2) self.assertTrue(abs(pos1 - pos2) < 1e-4)
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_single(self): import sys 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(self.input_file, cmd) sim = Simulation(bck) x = sim.system.particle[0].position[0] self.assertAlmostEqual(x, 3.62635, places=5) sim.run(10) x = sim.system.particle[0].position[0] self.assertAlmostEqual(x, 3.64526, places=5) sim.run(10) x = sim.system.particle[0].position[0] self.assertAlmostEqual(x, 3.675987, places=5)
def test_copy_and_run(self): self.skipTest('skipped test') import copy import atooms.trajectory.ram from atooms.backends.rumd import RUMD import rumd potential = rumd.Pot_LJ_12_6(cutoff_method=rumd.ShiftedPotential) potential.SetVerbose(False) potential.SetParams(i=0, j=0, Epsilon=1.0, Sigma=1.0, Rcut=2.5) potential.SetParams(i=1, j=0, Epsilon=1.5, Sigma=0.8, Rcut=2.5) potential.SetParams(i=0, j=1, Epsilon=1.5, Sigma=0.8, Rcut=2.5) potential.SetParams(i=1, j=1, Epsilon=0.5, Sigma=0.88, Rcut=2.5) input_file = os.path.join(os.path.dirname(__file__), '../data/ka_N256_rho1.185_rumd.xyz.gz') backend = RUMD(input_file, integrator='nvt', temperature=0.8, dt=0.002, forcefield=[potential]) sim = Simulation(backend, output_path='/tmp/test_rumd_single/trajectory', steps=100, restart=False) # First set if runs # print sim.system.particle[0].position[0] tr = atooms.trajectory.ram.TrajectoryRamFull() th = atooms.trajectory.ram.TrajectoryRamFull() tr[0] = sim.system # print tr[0].particle[0].position[0] sim.run(1000) # sometimes is nan, depending on steps # Ram does not work with rumd because of change in System init th[0] = sim.system # print sim.system.particle[0].position, sim.system.potential_energy(per_particle=True) sim.run(100) # print sim.system.particle[0].position[0] # this assignment leads to trouble, BUT ONLY IF WE STORE THE SAMPLES IN tr TRAJECTORY # assigning tr[0] above also leads to a different trajectory...!! sim.system = th[0]
def test_leakage(self): #self.skipTest('skipped test') from atooms.backends.rumd import System from atooms.trajectory.ram import TrajectoryRamFull from atooms.backends.rumd import unfold si = Simulation(self.backend, output_path='/tmp/test_rumd_single/trajectory', steps=2000, checkpoint_interval=100, restart=False) # self.backend.rumd_simulation.sample.__swig_destroy__(self.backend.rumd_simulation.sample) #del self.backend.rumd_simulation.sample # This does not leak memory trj = TrajectoryRamFull() trj[0] = si.system for i in range(5): si.system = trj[0] si.run() unfold(si.system).particle[0].position[0], unfold( trj[0]).particle[0].position[0]
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_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_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)
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()
def test_checkpoint(self): s = Simulation(self.backend, self.dout, steps=10, checkpoint_interval=10) s.run()
def test_target(self): s = Simulation(DryRun(), output_path='/tmp/test_simulation/trajectory', steps=100) s.run()
def run(self, steps=None): if self.tolerance is not None: # We need to add this check only if targeters are present self.add(_target_backend_tolerance, self._check_interval, 0.0) self.add(_target_force_norm_square, self._check_interval, self.tolerance) Simulation.run(self, steps)