def test_system_rotational_dof(simulation_factory, device): snap = hoomd.Snapshot(device.communicator) if snap.exists: box = [10, 10, 10, 0, 0, 0] snap.configuration.box = box snap.particles.N = 3 snap.particles.position[:] = [[0, 1, 0], [-1, 1, 0], [1, 1, 0]] snap.particles.velocity[:] = [[0, 0, 0], [0, -1, 0], [0, 1, 0]] snap.particles.moment_inertia[:] = [[2.0, 0, 0], [1, 1, 1], [1, 1, 1]] snap.particles.angmom[:] = [[0, 2, 4, 6]] * 3 snap.particles.types = ['A'] filt = hoomd.filter.All() thermo = hoomd.md.compute.ThermodynamicQuantities(filter=filt) sim = simulation_factory(snap) sim.always_compute_pressure = True sim.operations.add(thermo) integrator = hoomd.md.Integrator(dt=0.0001) integrator.aniso = True integrator.methods.append(hoomd.md.methods.NVT(filt, tau=1, kT=1)) sim.operations.integrator = integrator sim.run(1) _assert_thermo_properties( thermo, 3, 7, 6, 0.0, 57 / 4., 1.0, 61 / 4., 2. / 3 * thermo.translational_kinetic_energy / 10.0**3, (0., 0., 0., 2. / 10**3, 0., 0.))
def make_snapshot(d=1.0, phi_deg=45, particle_types=['A'], L=20): phi_rad = phi_deg * (numpy.pi / 180) # the central particles are along the x-axis, so phi is determined from # the angle in the yz plane. snapshot = hoomd.Snapshot(device.communicator) N = 4 if snapshot.communicator.rank == 0: box = [L, L, L, 0, 0, 0] snapshot.configuration.box = box snapshot.particles.N = N snapshot.particles.types = particle_types # shift particle positions slightly in z so MPI tests pass snapshot.particles.position[:] = [ [ 0.0, d * numpy.cos(phi_rad / 2), d * numpy.sin(phi_rad / 2) + 0.1, ], [0.0, 0.0, 0.1], [d, 0.0, 0.1], [ d, d * numpy.cos(phi_rad / 2), -d * numpy.sin(phi_rad / 2) + 0.1, ], ] snapshot.dihedrals.N = 1 snapshot.dihedrals.types = ['A-A-A-A'] snapshot.dihedrals.typeid[0] = 0 snapshot.dihedrals.group[0] = (0, 1, 2, 3) return snapshot
def make_snapshot(d=1.0, theta_deg=60, particle_types=['A'], dimensions=3, L=20): theta_rad = theta_deg * (np.pi / 180) s = hoomd.Snapshot(device.communicator) N = 3 if s.communicator.rank == 0: box = [L, L, L, 0, 0, 0] if dimensions == 2: box[2] = 0 s.configuration.box = box s.particles.N = N base_positions = np.array( [[-d * np.sin(theta_rad / 2), d * np.cos(theta_rad / 2), 0.0], [0.0, 0.0, 0.0], [d * np.sin(theta_rad / 2), d * np.cos(theta_rad / 2), 0.0]]) # move particles slightly in direction of MPI decomposition which # varies by simulation dimension nudge_dimension = 2 if dimensions == 3 else 1 base_positions[:, nudge_dimension] += 0.1 s.particles.position[:] = base_positions s.particles.types = particle_types return s
def one_cube_simulation(simulation_factory): """Construct a simulation box with one cube. The box has L=1.2 and the cube has side length 1. Some orientations of the cube have no overlaps and some do. """ snap = hoomd.Snapshot() snap.particles.N = 1 snap.particles.types = ['A'] snap.particles.position[:] = [[0, 0, 0]] snap.configuration.box = [1.2, 1.2, 1.2, 0, 0, 0] sim = simulation_factory(snap) mc = hoomd.hpmc.integrate.ConvexPolyhedron() mc.shape['A'] = dict(vertices=[ (-0.5, -0.5, -0.5), (-0.5, -0.5, 0.5), (-0.5, 0.5, -0.5), (-0.5, 0.5, 0.5), (0.5, -0.5, -0.5), (0.5, -0.5, 0.5), (0.5, 0.5, -0.5), (0.5, 0.5, 0.5), ]) sim.operations.integrator = mc return sim
def test_snapshot_from_initial(self): import hoomd from mbuild.formats.hoomd_snapshot import to_hoomdsnapshot part = mb.Compound(name="Ar") box = mb.Box(lengths=[5, 5, 5], angles=[90, 90, 90]) system = mb.fill_box(part, n_compounds=10, box=box) if hoomd_version.major == 2: hoomd.context.initialize("") init_snap = hoomd.data.make_snapshot(N=10, box=hoomd.data.boxdim(L=10)) else: init_snap = hoomd.Snapshot() init_snap.particles.N = 10 init_snap.configuration.box = hoomd.Box.cube(L=10) snap, _ = to_hoomdsnapshot(system, hoomd_snapshot=init_snap) assert snap.particles.N == 20 assert snap.bonds.N == 0 assert snap.angles.N == 0 if hoomd_version.major == 2: assert (snap.box.Lx, snap.box.Ly, snap.box.Lz) == (50, 50, 50) assert (snap.box.xy, snap.box.xz, snap.box.yz) == (0, 0, 0) else: np.testing.assert_allclose(snap.configuration.box, [50, 50, 50, 0, 0, 0])
def base_snapshot(device): """Defines a snapshot using the data given above.""" def set_snapshot(snap, data, base): """Sets individual sections of snapshot (e.g. particles).""" snap_section = getattr(snap, base) for k in data: if k.startswith('_'): setattr(snap_section, k[1:], data[k]) continue elif data[k]['value'] is None: continue try: array = getattr(snap_section, k) array[:] = data[k]['value'] except TypeError: setattr(snap_section, k, data[k]['value']) snapshot = hoomd.Snapshot(device.communicator) if snapshot.communicator.rank == 0: snapshot.configuration.box = [2.1, 2.1, 2.1, 0, 0, 0] set_snapshot(snapshot, _particle_data, 'particles') set_snapshot(snapshot, _bond_data, 'bonds') set_snapshot(snapshot, _angle_data, 'angles') set_snapshot(snapshot, _dihedral_data, 'dihedrals') set_snapshot(snapshot, _improper_data, 'impropers') set_snapshot(snapshot, _constraint_data, 'constraints') set_snapshot(snapshot, _pair_data, 'pairs') return snapshot
def make_snapshot(polymer_length=10, N_polymers=10, polymer_spacing=1.2, bead_spacing=1.1): """Make the snapshot. Args: polymer_length: Number of particles in each polymer N_polymers: Number of polymers to place polymer_spacing: distance between the polymers bead_spacing: distance between the beads in the polymer Place N_polymers polymers in a 2D simulation with distance constraints between beads in each polymer. """ s = hoomd.Snapshot(device.communicator) if s.communicator.rank == 0: s.configuration.box = [ polymer_spacing * N_polymers, bead_spacing * polymer_length, 0, 0, 0, 0 ] s.particles.N = polymer_length * N_polymers s.particles.types = ['A'] x_coords = numpy.linspace(-polymer_spacing * N_polymers / 2, polymer_spacing * N_polymers / 2, num=N_polymers, endpoint=False) + polymer_spacing / 2 y_coords = numpy.linspace(-bead_spacing * polymer_length / 2, bead_spacing * polymer_length / 2, num=N_polymers, endpoint=False) + bead_spacing / 2 position = [] constraint_values = [] constraint_groups = [] for x in x_coords: for i, y in enumerate(y_coords): position.append([x, y, 0]) if i & 1: constraint_values.append(bead_spacing) tag = len(position) - 1 constraint_groups.append([tag, tag - 1]) s.particles.position[:] = position s.constraints.N = len(constraint_values) s.constraints.value[:] = constraint_values s.constraints.group[:] = constraint_groups return s
def make_snapshot(): """Make the snapshot.""" snap = hoomd.Snapshot(device.communicator) if snap.communicator.rank == 0: box = [10, 10, 10, 0, 0, 0] snap.configuration.box = box snap.particles.N = 3 snap.particles.position[:] = [[0, 1, 0], [-1, 1, 0], [1, 1, 0]] snap.particles.velocity[:] = [[0, 0, 0], [0, -1, 0], [0, 1, 0]] snap.particles.moment_inertia[:] = [ [2.0, 0, 0], [1, 1, 1], [1, 1, 1], ] snap.particles.angmom[:] = [[0, 2, 4, 6]] * 3 snap.particles.types = ['A', 'B'] return snap
def test_empty_initial_snapshot(self): import hoomd from mbuild.formats.hoomd_snapshot import to_hoomdsnapshot part = mb.Compound(name="Ar") box = mb.Box(lengths=[5, 5, 5], angles=[90, 90, 90]) system = mb.fill_box(part, n_compounds=10, box=box) if hoomd_version.major == 2: hoomd.context.initialize("") init_snap = hoomd.data.make_snapshot(N=0, box=hoomd.data.boxdim(L=10)) else: init_snap = hoomd.Snapshot() init_snap.configuration.box = hoomd.Box.cube(L=10) with pytest.raises(RuntimeError): snap, _ = to_hoomdsnapshot(system, hoomd_snapshot=init_snap)
def make_snapshot(d=1.0, phi_deg=45, particle_types=['A'], L=20): phi_rad = phi_deg * (np.pi / 180) # the central particles are along the x-axis, so phi is determined from # the angle in the yz plane. s = hoomd.Snapshot(device.communicator) N = 4 if s.communicator.rank == 0: box = [L, L, L, 0, 0, 0] s.configuration.box = box s.particles.N = N s.particles.types = particle_types # shift particle positions slightly in z so MPI tests pass s.particles.position[:] = [ [0.0, d * np.cos(phi_rad / 2), d * np.sin(phi_rad / 2) + 0.1], [0.0, 0.0, 0.1], [d, 0.0, 0.1], [d, d * np.cos(phi_rad / 2), -d * np.sin(phi_rad / 2) + 0.1] ] return s
def make_snapshot(): snapshot = hoomd.Snapshot(device.communicator) N = 4 L = 10 if snapshot.communicator.rank == 0: box = [L, L, L, 0, 0, 0] snapshot.configuration.box = box snapshot.particles.N = N snapshot.particles.types = ['A'] # shift particle positions slightly in z so MPI tests pass snapshot.particles.position[:] = [ [0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0.1], ] snapshot.impropers.N = 1 snapshot.impropers.types = ['A-A-A-A'] snapshot.impropers.typeid[0] = 0 snapshot.impropers.group[0] = (0, 1, 2, 3) return snapshot
def _make_two_particle_snapshot(device, particle_types=['A'], d=1, L=20): """Make the snapshot. Args: device: hoomd device object. particle_types: List of particle type names dimensions: Number of dimensions (2 or 3) d: Distance apart to place particles L: Box length The two particles are placed at (-d/2, 0, 0) and (d/2,0,0). The box is L by L by L. """ s = hoomd.Snapshot(device.communicator) if s.communicator.rank == 0: box = [L, L, L, 0, 0, 0] s.configuration.box = box s.particles.N = 2 # shift particle positions slightly in z so MPI tests pass s.particles.position[:] = [[-d / 2, 0, .1], [d / 2, 0, .1]] s.particles.types = particle_types return s