def __init__(self, sim, steps=0, output_path=None, restart=False): Simulation.__init__(self, DryRun(), output_path=output_path, steps=steps, restart=restart) self.sim = sim
def test_no_output(self): # Disable writers completely s = Simulation(DryRun(), output_path=None, steps=10, enable_speedometer=False) s.run()
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_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_scheduler(self): class Simulation: def __init__(self): self.current_step = 0 s = Scheduler(3) sim = Simulation() inext = [] for i in range(8): sim.current_step = i inext.append(s(sim)) self.assertEqual(inext, [3, 3, 3, 6, 6, 6, 9, 9])
def __init__(self, backend, output_path=None, tolerance=None, steps=int(1e6)): """ Perform an optimization using the specified `backend` and optionally write output to `output_path`. This can be a file or directory path. Paths: to define output paths we rely on `output_path`, all other paths are defined based on it and on its base_path. """ Simulation.__init__(self, backend, output_path=output_path) self.steps = steps self.tolerance = tolerance self._check_interval = 1000
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_ram_lammps(self): import os import numpy import sys from atooms.backends.lammps import LAMMPS from atooms.simulation import Simulation 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) sim = Simulation(bck) sim.system.particle[0].position = numpy.zeros(3) t = TrajectoryRamFull() t[0] = sim.system sim.system.particle[0].position += 1.0 self.assertFalse( (sim.system.particle[0].position == t[0].particle[0].position ).all())
def test_composite(self): """ Test that composite simulation instances (a simulation within a simulation object) run independent of their parent instance. This checks that there are no regression against the bug fixed in 63a7e7863. """ class NewSimulation(Simulation): def __init__(self, sim, steps=0, output_path=None, restart=False): Simulation.__init__(self, DryRun(), output_path=output_path, steps=steps, restart=restart) self.sim = sim def __str__(self): return 'NewSimulation' def run_until(self, steps): self.sim.run() self.current_step = steps sim = Simulation(DryRun(), steps=3) new_sim = NewSimulation(sim, steps=1) new_sim.run() self.assertEqual(new_sim.current_step, 1) self.assertEqual(sim.current_step, 3)
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_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_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_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_properties(self): t = TrajectoryRUMD(self.input_file) s0 = t[-1] sim = Simulation(self.backend, output_path='/tmp/test_rumd_temp/trajectory', steps=1, restart=False) s1 = sim.system self.assertAlmostEqual(s1.temperature, s0.temperature) self.assertAlmostEqual(s1.cell.side[0], s0.cell.side[0]) self.assertAlmostEqual(s1.particle[0].position[0], s0.particle[0].position[0])
def test_system_copy(self): """Make sure rumd systems can be deepcopied""" import copy si = Simulation(self.backend, output_path='/tmp/test_rumd_single/trajectory', steps=2000, checkpoint_interval=100, restart=False) s0 = copy.copy(si.system) s1 = copy.deepcopy(si.system) self.assertEqual(si.system.particle[0].position[-1], s1.particle[0].position[-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_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_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_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 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 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_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_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_update(self): #self.skipTest('skipped test') from atooms.backends.rumd import System from atooms.trajectory.ram import TrajectoryRamFull si = Simulation(self.backend, output_path='/tmp/test_rumd_single/trajectory', steps=2000, checkpoint_interval=100, restart=False) itg = si.system.sample.GetIntegrator() trj = TrajectoryRamFull() trj[0] = si.system si.system.thermostat.temperature = 10.0 # Objects are different but underlying information is shared... # print trj[0].sample.GetIntegrator().GetInfoString(18), itg.GetInfoString(18) self.assertNotEqual(trj[0].sample.GetIntegrator(), itg) #self.assertEqual(trj[0].sample.GetIntegrator().GetInfoString(18), itg.GetInfoString(18)) trj[0] = si.system # This uses update # print trj[0].sample.GetIntegrator().GetInfoString(18), itg.GetInfoString(18) self.assertNotEqual(trj[0].sample.GetIntegrator(), itg)
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_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_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_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'))