def test_lj_energy(self): model_dir = build_examples.lj_graph(9 - 1, self.tmp) with hoomd.htf.tfcompute(model_dir) as tfcompute: hoomd.context.initialize() N = 3 * 3 NN = N - 1 T = 10 rcut = 5.0 system = hoomd.init.create_lattice( unitcell=hoomd.lattice.sq(a=4.0), n=[3, 3]) nlist = hoomd.md.nlist.cell(check_period=1) hoomd.md.integrate.mode_standard(dt=0.001) hoomd.md.integrate.nve(group=hoomd.group.all( )).randomize_velocities(seed=1, kT=0.8) log = hoomd.analyze.log(filename=None, quantities=['potential_energy', 'kinetic_energy'], period=1) tfcompute.attach(nlist, r_cut=rcut) energy = [] for i in range(T): hoomd.run(250) energy.append(log.query('potential_energy' ) + log.query('kinetic_energy')) if i > 1: np.testing.assert_allclose(energy[-1], energy[-2], atol=1e-3)
def test_lj_pressure(self): # TODO The virials are off by 1e-6, leading to # pressure differences of 1e-3. # I can't figure out why, but since PE and forces are # matching exactly, I'll leave the tol # set that high. model_dir = build_examples.lj_graph(9 - 1) with hoomd.htf.tfcompute(model_dir) as tfcompute: hoomd.context.initialize() N = 3 * 3 NN = N - 1 rcut = 5.0 system = hoomd.init.create_lattice( unitcell=hoomd.lattice.sq(a=4.0), n=[3, 3]) nlist = hoomd.md.nlist.cell(check_period=1) hoomd.md.integrate.mode_standard(dt=0.005) hoomd.md.integrate.nvt(group=hoomd.group.all(), kT=1, tau=0.2).randomize_velocities(seed=1) tfcompute.attach(nlist, r_cut=rcut) log = hoomd.analyze.log( filename=None, quantities=['potential_energy', 'pressure'], period=1) thermo_scalars = [] tf_virial = [] for i in range(5): hoomd.run(3) snapshot = system.take_snapshot() tf_virial.append(tfcompute.get_virial_array()) thermo_scalars.append( [log.query('potential_energy'), log.query('pressure')]) # now run with stock lj hoomd.context.initialize() system = hoomd.init.create_lattice(unitcell=hoomd.lattice.sq(a=4.0), n=[3, 3]) nlist = hoomd.md.nlist.cell(check_period=1) hoomd.md.integrate.mode_standard(dt=0.005) hoomd.md.integrate.nvt(group=hoomd.group.all(), kT=1, tau=0.2).randomize_velocities(seed=1) lj = hoomd.md.pair.lj(r_cut=5.0, nlist=nlist) lj.pair_coeff.set('A', 'A', epsilon=1.0, sigma=1.0) log = hoomd.analyze.log(filename=None, quantities=['potential_energy', 'pressure'], period=1) for i in range(5): hoomd.run(3) snapshot = system.take_snapshot() v = snapshot.particles.velocity lj_virial = np.array([lj.forces[j].virial for j in range(N)]) for j in range(N): np.testing.assert_allclose(lj_virial[j][0:2], tf_virial[i][j][0:2], atol=1e-5)
def test_lj_forces(self): N = 3 * 3 model_dir = build_examples.lj_graph(N - 1, self.tmp) with hoomd.htf.tfcompute(model_dir) as tfcompute: hoomd.context.initialize() T = 10 rcut = 5.0 system = hoomd.init.create_lattice( unitcell=hoomd.lattice.sq(a=4.0), n=[3, 3]) nlist = hoomd.md.nlist.cell(check_period=1) hoomd.md.integrate.mode_standard(dt=0.005) hoomd.md.integrate.nvt(group=hoomd.group.all(), kT=1, tau=0.2 ).randomize_velocities(seed=1) tfcompute.attach(nlist, r_cut=rcut) hoomd.run(2) tf_forces = [] for i in range(T): hoomd.run(1) snapshot = system.take_snapshot() tf_forces.append([system.particles[j].net_force for j in range(N)]) tf_forces = np.array(tf_forces) # now run with stock lj hoomd.context.initialize() system = hoomd.init.create_lattice( unitcell=hoomd.lattice.sq(a=4.0), n=[3, 3]) nlist = hoomd.md.nlist.cell(check_period=1) hoomd.md.integrate.mode_standard(dt=0.005) hoomd.md.integrate.nvt(group=hoomd.group.all(), kT=1, tau=0.2 ).randomize_velocities(seed=1) lj = hoomd.md.pair.lj(r_cut=5.0, nlist=nlist) lj.pair_coeff.set('A', 'A', epsilon=1.0, sigma=1.0) hoomd.run(2) lj_forces = [] for i in range(T): hoomd.run(1) snapshot = system.take_snapshot() lj_forces.append([system.particles[j].net_force for j in range(N)]) lj_forces = np.array(lj_forces) for i in range(T): for j in range(N): np.testing.assert_allclose(tf_forces[i, j], lj_forces[i, j], atol=1e-5)
def run_tf_lj(N, T): model_dir = build_examples.lj_graph(N - 1) with hoomd.htf.tfcompute(model_dir, _mock_mode=False) as tfcompute: rcut = 5.0 system = init.create_lattice( unitcell=hoomd.lattice.sq(a=4.0), n=[np.sqrt(N).astype(np.int), np.sqrt(N).astype(np.int)]) nlist = md.nlist.cell() md.integrate.mode_standard(dt=0.005) md.integrate.nvt(group=hoomd.group.all(), kT=1, tau=0.2).randomize_velocities(seed=1) tfcompute.attach(nlist, r_cut=rcut) hoomd.run(2) tf_forces = [] for i in range(T): hoomd.run(1) snapshot = system.take_snapshot() tf_forces.append([system.particles[j].net_force for j in range(N)]) tf_forces = np.array(tf_forces) return tf_forces
def test_overflow(self): '''Use too small neighbor list and ensure error is thrown TODO: It works, but the tfmanager thread early exit causes main thread to hang ''' N = 8 * 8 model_dir = build_examples.lj_graph(4, self.tmp, check_nlist=True) with hoomd.htf.tfcompute(model_dir) as tfcompute: hoomd.context.initialize() T = 10 rcut = 10.0 system = hoomd.init.create_lattice( unitcell=hoomd.lattice.sq(a=4.0), n=[8, 8]) nlist = hoomd.md.nlist.cell(check_period=1) hoomd.md.integrate.mode_standard(dt=0.005) hoomd.md.integrate.nvt(group=hoomd.group.all(), kT=1, tau=0.2 ).randomize_velocities(seed=1) tfcompute.attach(nlist, r_cut=rcut) with self.assertRaises(tf.errors.InvalidArgumentError): hoomd.run(2)
def test_nlist_count(self): '''Make sure nlist is full, not half ''' model_dir = build_examples.lj_graph(9 - 1, self.tmp) with hoomd.htf.tfcompute(model_dir) as tfcompute: hoomd.context.initialize() N = 3 * 3 NN = N - 1 T = 10 rcut = 5.0 system = hoomd.init.create_lattice( unitcell=hoomd.lattice.sq(a=4.0), n=[3, 3]) nlist = hoomd.md.nlist.cell() hoomd.md.integrate.mode_standard(dt=0.001) hoomd.md.integrate.nve(group=hoomd.group.all( )).randomize_velocities(seed=1, kT=0.8) tfcompute.attach(nlist, r_cut=rcut) hoomd.run(1) # in lattice, should have 4 neighbors nl = tfcompute.get_nlist_array() ncount = np.sum(np.sum(nl**2, axis=2) > 0.1, axis=1) self.assertEqual(np.min(ncount), 4)