def test_state_from_gsd(simulation_factory, get_snapshot, device, state_args, tmp_path): snap_params, nsteps = state_args d = tmp_path / "sub" d.mkdir() filename = d / "temporary_test_file.gsd" with gsd.hoomd.open(name=filename, mode='wb+') as file: sim = simulation_factory( get_snapshot(n=snap_params[0], particle_types=snap_params[1])) snap = sim.state.snapshot snapshot_dict = {} snapshot_dict[0] = snap file.append(make_gsd_snapshot(snap)) box = sim.state.box for step in range(1, nsteps): particle_type = np.random.choice(snap_params[1]) snap = update_positions(sim.state.snapshot) set_types(snap, random_inds(snap_params[0]), snap_params[1], particle_type) file.append(make_gsd_snapshot(snap)) snapshot_dict[step] = snap for step, snap in snapshot_dict.items(): sim = hoomd.Simulation(device) sim.create_state_from_gsd(filename, frame=step) assert box == sim.state.box assert_equivalent_snapshots(snap, sim.state.snapshot)
def test_no_position_scale(device, get_snapshot, sys): sys1, make_sys_halfway, sys2 = sys sys_halfway = make_sys_halfway(_power) variant = hoomd.variant.Power(0., 1., _power, _t_start, _t_ramp) trigger = hoomd.trigger.After(variant.t_start) box_resize = hoomd.update.BoxResize(box1=sys1[0], box2=sys2[0], variant=variant, trigger=trigger, scale_particles=False) sim = hoomd.Simulation(device) sim.create_state_from_snapshot(get_snapshot()) sim.operations.updaters.append(box_resize) # Run up to halfway point sim.run(_t_mid + 1) assert sim.state.box == sys_halfway[0] assert_positions(sim, sys1[1]) # Finish run sim.run(_t_mid) assert sim.state.box == sys2[0] assert_positions(sim, sys1[1])
def test_update(device, get_snapshot, sys): sys1, _, sys2 = sys sim = hoomd.Simulation(device) sim.create_state_from_snapshot(get_snapshot()) hoomd.update.BoxResize.update(sim.state, sys2[0]) assert sim.state.box == sys2[0] assert_positions(sim, sys2[1])
def test_tps(simulation_factory, get_snapshot, device): sim = hoomd.Simulation(device) assert sim.tps is None sim = simulation_factory(get_snapshot()) sim.run(100) assert sim.tps > 0
def test_allows_compute_pressure(device, get_snapshot): sim = hoomd.Simulation(device) assert sim.always_compute_pressure is False with pytest.raises(RuntimeError): sim.always_compute_pressure = True sim.create_state_from_snapshot(get_snapshot()) sim.always_compute_pressure = True assert sim.always_compute_pressure is True
def test_run(simulation_factory, get_snapshot, device): sim = hoomd.Simulation(device) with pytest.raises(RuntimeError): sim.run(1) # Before setting state sim = simulation_factory(get_snapshot()) sim.run(1) assert sim.operations._scheduled
def factory(): sim = hoomd.Simulation(device) # reduce sorter grid to avoid Hilbert curve overhead in unit tests for tuner in sim.operations.tuners: if isinstance(tuner, hoomd.tune.ParticleSorter): tuner.grid = 8 sim.create_state_from_snapshot(base_snapshot) return sim
def test_timestep(simulation_factory, get_snapshot, device): sim = hoomd.Simulation(device) assert sim.timestep is None initial_steps = 10 sim.timestep = initial_steps assert sim.timestep == initial_steps sim.create_state_from_snapshot(get_snapshot()) assert sim.timestep == initial_steps with pytest.raises(RuntimeError): sim.timestep = 20
def test_get_box(device, get_snapshot, sys, box_resize): sys1, make_sys_halfway, sys2 = sys sys_halfway = make_sys_halfway(_power) sim = hoomd.Simulation(device) sim.create_state_from_snapshot(get_snapshot()) sim.operations.updaters.append(box_resize) sim.run(0) assert box_resize.get_box(0) == sys1[0] assert box_resize.get_box(_t_mid) == sys_halfway[0] assert box_resize.get_box(_t_start + _t_ramp) == sys2[0]
def test_run_with_timestep(simulation_factory, get_snapshot, device): sim = hoomd.Simulation(device) sim.create_state_from_snapshot(get_snapshot()) sim.operations._schedule() steps = 0 n_step_list = [1, 10, 100] for n_steps in n_step_list: steps += n_steps sim.run(n_steps) assert sim.timestep == steps assert sim.timestep == sum(n_step_list)
def create_simulation(job): cpu = hoomd.device.CPU() sim = hoomd.Simulation(device=cpu, seed=job.statepoint.seed) mc = hoomd.hpmc.integrate.ConvexPolyhedron() mc.shape['octahedron'] = dict(vertices=[ (-0.5, 0, 0), (0.5, 0, 0), (0, -0.5, 0), (0, 0.5, 0), (0, 0, -0.5), (0, 0, 0.5), ]) sim.operations.integrator = mc return sim
def test_seed(device, lattice_snapshot_factory): sim = hoomd.Simulation(device) assert sim.seed is None sim.seed = 42 assert sim.seed == 42 sim.seed = 0x123456789abcdef assert sim.seed == 0xcdef sim.create_state_from_snapshot(lattice_snapshot_factory()) assert sim.seed == 0xcdef sim.seed = 20 assert sim.seed == 20
def test_failure_with_cpu_device_and_gpu_buffer(): """Assert we cannot access gpu buffers with a cpu_device.""" device = hoomd.device.CPU() snap = _make_two_particle_snapshot(device) sim = hoomd.Simulation(device) sim.create_state_from_snapshot(snap) custom_force = MyForce('gpu_local_force_arrays') npt = md.methods.NPT(hoomd.filter.All(), kT=1, tau=1, S=1, tauS=1, couple="none") integrator = md.Integrator(dt=0.005, forces=[custom_force], methods=[npt]) sim.operations.integrator = integrator with pytest.raises(RuntimeError): sim.run(1)
def test_position_scale(device, get_snapshot, sys, box_resize): sys1, make_sys_halfway, sys2 = sys sys_halfway = make_sys_halfway(_power) sim = hoomd.Simulation(device) sim.create_state_from_snapshot(get_snapshot()) sim.operations.updaters.append(box_resize) # Run up to halfway point sim.run(_t_mid + 1) assert sim.state.box == sys_halfway[0] assert_positions(sim, sys_halfway[1]) # Finish run sim.run(_t_mid) assert sim.state.box == sys2[0] assert_positions(sim, sys2[1])
def test_state_from_gsd_snapshot(simulation_factory, lattice_snapshot_factory, device, state_args, tmp_path): snap_params, nsteps = state_args sim = simulation_factory( lattice_snapshot_factory(n=snap_params[0], particle_types=snap_params[1])) snap = sim.state.get_snapshot() snap = make_gsd_snapshot(snap) gsd_snapshot_list = [snap] box = sim.state.box for _ in range(1, nsteps): particle_type = np.random.choice(snap_params[1]) snap = update_positions(sim.state.get_snapshot()) set_types(snap, random_inds(snap_params[0]), snap_params[1], particle_type) snap = make_gsd_snapshot(snap) gsd_snapshot_list.append(snap) for snap in gsd_snapshot_list: sim = hoomd.Simulation(device) sim.create_state_from_snapshot(snap) assert box == sim.state.box assert_equivalent_snapshots(snap, sim.state.get_snapshot())
def test_initialization(simulation_factory): with pytest.raises(TypeError): sim = hoomd.Simulation() sim = simulation_factory() # noqa
def test_initialization(device): with pytest.raises(TypeError): sim = hoomd.Simulation() sim = hoomd.Simulation(device) # noqa
def test_device_property(device): sim = hoomd.Simulation(device) assert sim.device is device with pytest.raises(ValueError): sim.device = device
import hoomd device = hoomd.device.CPU() sim = hoomd.Simulation(device=device) sim.create_state_from_gsd( filename='../01-Introducing-Molecular-Dynamics/random.gsd') integrator = hoomd.md.Integrator(dt=0.005) cell = hoomd.md.nlist.Cell(buffer=0.4) lj = hoomd.md.pair.LJ(nlist=cell) lj.params[('A', 'A')] = dict(epsilon=1, sigma=1) lj.r_cut[('A', 'A')] = 2.5 integrator.forces.append(lj) nvt = hoomd.md.methods.NVT(kT=1.5, filter=hoomd.filter.All(), tau=1.0) integrator.methods.append(nvt) sim.operations.integrator = integrator sim.run(0)
def test_update_filters(device, get_snapshot, sys, filters): sys1, _, sys2 = sys filter_scale, _ = filters sim = hoomd.Simulation(device) sim.create_state_from_snapshot(get_snapshot()) hoomd.update.BoxResize.update(sim.state, sys2[0], filter=filter_scale)
snapshot.particles.types = ['A'] snapshot.particles.typeid = [0] * 5 snapshot.configuration.box = [20, 20, 20, 0, 0, 0] # Connect particles with bonds. snapshot.bonds.N = 4 snapshot.bonds.types = ['A-A'] snapshot.bonds.typeid = [0] * 4 snapshot.bonds.group = [[0, 1], [1, 2], [2, 3], [3, 4]] with gsd.hoomd.open(name='molecular.gsd', mode='xb') as f: f.append(snapshot) # Apply the harmonic potential on the bonds. harmonic = hoomd.md.bond.Harmonic() harmonic.params['A-A'] = dict(k=100, r0=1.0) # Perform the MD simulation. sim = hoomd.Simulation(device=hoomd.device.CPU(), seed=1) sim.create_state_from_gsd(filename='molecular.gsd') langevin = hoomd.md.methods.Langevin(filter=hoomd.filter.All(), kT=1.0) integrator = hoomd.md.Integrator(dt=0.005, methods=[langevin], forces=[harmonic]) gsd_writer = hoomd.write.GSD(filename='molecular_trajectory.gsd', trigger=hoomd.trigger.Periodic(1000), mode='xb') sim.operations.integrator = integrator sim.operations.writers.append(gsd_writer) sim.run(10e3)