def test_create_semi_rigid_bodies_hierarchy(self, benzene_from_parts): n_benzenes = 10 filled = mb.fill_box(benzene_from_parts, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) filled.name = 'Benzene box' filled2 = mb.clone(filled) compound = mb.Compound(subcompounds=[filled, filled2]) compound.label_rigid_bodies(discrete_bodies='Benzene box') assert compound.max_rigid_id == 1 assert filled.max_rigid_id == 0 assert filled2.max_rigid_id == 1 assert len(list(compound.rigid_particles())) == n_benzenes * 2 * 12 compound.unlabel_rigid_bodies() compound.label_rigid_bodies(discrete_bodies='Benzene', rigid_particles='C') assert compound.max_rigid_id == (n_benzenes*2) - 1 assert filled.max_rigid_id == n_benzenes - 1 assert filled2.max_rigid_id == (n_benzenes*2) - 1 assert len(list(compound.rigid_particles())) == n_benzenes * 2 * 6 assert len(list(filled.rigid_particles())) == n_benzenes * 6 assert len(list(filled2.rigid_particles())) == n_benzenes * 6 compound.unlabel_rigid_bodies() compound.label_rigid_bodies(discrete_bodies='CH') assert compound.max_rigid_id == (n_benzenes*2*6) - 1 assert filled.max_rigid_id == (n_benzenes*6) - 1 assert filled2.max_rigid_id == (n_benzenes*2*6) - 1 assert len(list(compound.rigid_particles())) == n_benzenes * 2 * 12 assert len(list(filled.rigid_particles())) == n_benzenes * 12 assert len(list(filled2.rigid_particles())) == n_benzenes * 12
def test_fill_box_aspect_ratio(self, h2o): filled = mb.fill_box(h2o, n_compounds=1000, density=1000, aspect_ratio=[1, 2, 1]) assert np.isclose(filled.box.lengths[0] / filled.box.lengths[1], 0.5) assert np.isclose(filled.box.lengths[1] / filled.box.lengths[2], 2)
def test_fill_box_aspect_ratio(self, h2o): filled = mb.fill_box(h2o, n_compounds=1000, density=1000, aspect_ratio=[1, 2, 1]) assert filled.periodicity[0] / filled.periodicity[1] == 0.5 assert filled.periodicity[1] / filled.periodicity[2] == 2
def build_ethane_box(box, n_molecules): from mbuild.examples import Ethane import mbuild as mb ethane = Ethane() full_box = mb.fill_box(ethane, n_molecules, box) full_box.name = '{}_ethanes'.format(n_molecules) return full_box
def test_to_more_pybel_residues(self, methane, ethane): box = mb.fill_box([methane, ethane], n_compounds=[3,3], box=mb.Box([10,10,10])) pybel_mol = box.to_pybel(box=None, residues=['Ethane', 'Methane']) pybel_mol_resnames = {a.name for a in pybel_mol.residues} assert 'Ethane' in pybel_mol_resnames assert 'Methane' in pybel_mol_resnames
def test_label_rigid_bodies_duplicate_warn(self, rigid_benzene): with pytest.warns(UserWarning): n_benzenes = 10 filled = mb.fill_box(rigid_benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) filled.label_rigid_bodies(discrete_bodies='Benzene')
def __init__(self, chain_length, n_chains, density): """Initialize an AlkaneBox Compound. Parameters ---------- chain_length : int Length of the alkane chains (in number of carbons) n_chains : int Number of chains to place in the box density : float Density (in kg/m^3) at which the system should be created """ super(AlkaneBox, self).__init__() # Create alkane chain prototype using the class above chain = Alkane(chain_length=chain_length) # Generate a more relaxed structure chain.energy_minimization() # Fill a box with chains at a user-defined density box_of_alkanes = mb.fill_box(compound=chain, n_compounds=n_chains, density=density) # Rename all chains to `Alkane`, this speeds up the atom-typing process for child in box_of_alkanes.children: child.name = 'Alkane' self.add(box_of_alkanes)
def test_rotate(self, h2o): filled = mb.fill_box(h2o, 2, box=[1, 1, 1], fix_orientation=True) w0 = filled.xyz[:3] w1 = filled.xyz[3:] # Translate w0 and w1 to COM w0 -= w0.sum(0) / len(w0) w1 -= w1.sum(0) / len(w1) assert np.isclose(w0, w1).all()
def test_fill_box_compound_ratio(self, h2o, ethane): filled = mb.fill_box(compound=[h2o, ethane], density=800, compound_ratio=[2, 1], box=[2, 2, 2, 4, 4, 4]) n_ethane = len([c for c in filled.children if c.name == 'Ethane']) n_water = len([c for c in filled.children if c.name == 'H2O']) assert n_water / n_ethane == 2
def test_write_temp_file(self, h2o): cwd = os.getcwd() # Must keep track of the temp dir that pytest creates filled = mb.fill_box(h2o, n_compounds=10, box=[4, 4, 4], temp_file='temp_file1.pdb') region = mb.fill_region(h2o, 10, [2, 2, 2, 4, 4, 4], temp_file='temp_file2.pdb') solvated = mb.solvate(filled, h2o, 10, box=[4, 4, 4], temp_file='temp_file3.pdb') assert os.path.isfile(os.path.join(cwd, 'temp_file1.pdb')) assert os.path.isfile(os.path.join(cwd, 'temp_file2.pdb')) assert os.path.isfile(os.path.join(cwd, 'temp_file3.pdb'))
def box_of_benzenes(self, benzene): n_benzenes = 10 benzene.name = 'Benzene' filled = mb.fill_box(benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) filled.label_rigid_bodies(discrete_bodies='Benzene', rigid_particles='C') return filled
def test_no_rotate(self, h2o): filled = mb.fill_box([h2o, h2o], [1, 1], box=[1, 1, 1], fix_orientation=[False, True]) w0 = filled.xyz[:3] w1 = filled.xyz[3:] # Translate w0 and w1 to COM w0 -= w0.sum(0) / len(w0) w1 -= w1.sum(0) / len(w1) assert np.isclose(w0, w1).all() is not True
def test_box_dimensions(self, benzene): n_benzenes = 10 filled = mb.fill_box(benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) filled.save(filename='benzene.hoomdxml') for atom in mb.load('benzene.hoomdxml'): assert atom.pos.max() < 20 assert atom.pos.min() > -20
def test_empty_initial_snapshot(self): hoomd = import_("hoomd") hoomd_snapshot = import_("mbuild.formats.hoomd_snapshot") part = mb.Compound(name='Ar') box = mb.fill_box(part, n_compounds=10, box=mb.Box([5, 5, 5])) init_snap = hoomd.data.make_snapshot(N=0, box=hoomd.data.boxdim(L=10)) with pytest.raises(RuntimeError): snap, _ = hoomd_snapshot.to_hoomdsnapshot(box, hoomd_snapshot=init_snap)
def test_save_non_sequential_rigid_ids(self, benzene): n_benzenes = 10 filled = mb.fill_box(benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) filled.label_rigid_bodies(discrete_bodies='Benzene') filled.children[0]._increment_rigid_ids(increment=3) with pytest.warns(UserWarning): filled.save('benzene-box.hoomdxml')
def test_save_non_sequential_rigid_ids(self, benzene): n_benzenes = 10 filled = mb.fill_box(benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) filled.label_rigid_bodies(discrete_bodies="Benzene") filled.children[0]._increment_rigid_ids(increment=3) with pytest.warns(UserWarning): filled.save("benzene-box.hoomdxml")
def test_delete_body_multiple(self, rigid_benzene): n_benzenes = 10 filled = mb.fill_box(rigid_benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) filled.remove([filled.children[0], filled.children[1]]) assert filled.max_rigid_id == n_benzenes - 3 assert len(list(filled.rigid_particles())) == (n_benzenes - 2) * rigid_benzene.n_particles
def test_particles_to_snapshot(self): hoomd_snapshot = import_("mbuild.formats.hoomd_snapshot") part = mb.Compound(name='Ar') box = mb.fill_box(part, n_compounds=10, box=mb.Box([5, 5, 5])) snap, _ = hoomd_snapshot.to_hoomdsnapshot(box) assert snap.particles.N == 10 assert snap.bonds.N == 0 assert snap.angles.N == 0
def test_multiple_molecules(self, ethane): n_ethane = 2 ethane.name = 'Ethane' filled = mb.fill_box(ethane,n_compounds=n_ethane,box=[0,0,0,4,4,4]) with pytest.raises(ValueError, match=r'Not all components of the molecule'): filled.save(filename='box-ethane-opls.mcf', forcefield_name='oplsaa',angle_style='harmonic', dihedral_style='opls')
def test_label_semi_rigid_bodies_after_fill(self, benzene): n_benzenes = 10 filled = mb.fill_box(benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) filled.label_rigid_bodies(discrete_bodies='Benzene', rigid_particles='C') assert filled.max_rigid_id == n_benzenes - 1 assert len(list(filled.rigid_particles())) == n_benzenes * 6
def initialize(job): "Inialize the simulation" alkane = Alkane(job.statepoint()['C_n']) n_alkane = 200 # A cleaner packing approach would involve pull #372 system_box = mb.Box([4, 4, 4]) system = mb.fill_box(compound=alkane, n_compounds=n_alkane, box=system_box) system.save('init.gro', overwrite=True) system.save('init.top', forcefield_name='oplsaa', overwrite=True)
def test_delete_body_semi_rigid(self, benzene): n_benzenes = 10 filled = mb.fill_box(benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) filled.label_rigid_bodies(discrete_bodies='Benzene', rigid_particles='C') filled.remove(filled.children[0]) assert filled.max_rigid_id == n_benzenes - 2 assert len(list(filled.rigid_particles())) == (n_benzenes - 1) * 6
def test_delete_body_particle_by_particle(self, rigid_benzene): n_benzenes = 10 filled = mb.fill_box(rigid_benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) for particle in filled.children[0].particles(): filled.remove(particle) assert filled.max_rigid_id == n_benzenes - 2 assert len(list(filled.rigid_particles())) == (n_benzenes - 1) * rigid_benzene.n_particles
def test_fill_box_rigid(self, rigid_benzene): n_benzenes = 10 filled = mb.fill_box(rigid_benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) assert filled.contains_rigid is True assert filled.rigid_id is None assert filled.max_rigid_id == n_benzenes - 1 assert len(list(filled.rigid_particles())) == n_benzenes * rigid_benzene.n_particles
def test_opls(self): eth = Alkane(n=10) cmpd = mb.fill_box(eth, n_compounds=10, box=[10, 10, 10]) ff = foyer.Forcefield(name='oplsaa') structure = ff.apply(cmpd) df = commpare.openmm.build_run_measure_openmm(structure) assert 'bond' in df assert 'angle' in df assert 'dihedral' in df assert 'nonbond' in df
def ar_system(self): ar = mb.Compound(name='Ar') packed_system = mb.fill_box( compound=ar, n_compounds=100, box=mb.Box([3, 3, 3]), ) return from_mbuild(packed_system)
def _topology(n_sites=100): ar = mb.Compound(name='Ar') packed_system = mb.fill_box( compound=ar, n_compounds=n_sites, box=mb.Box([3, 3, 3]), ) return packed_system
def test_delete_body_multiple(self, rigid_benzene): n_benzenes = 10 filled = mb.fill_box(rigid_benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) filled.remove([filled.children[0], filled.children[1]]) assert filled.max_rigid_id == n_benzenes - 3 assert (len(list(filled.rigid_particles())) == (n_benzenes - 2) * rigid_benzene.n_particles)
def test_label_semi_rigid_bodies_after_fill(self, benzene): n_benzenes = 10 filled = mb.fill_box(benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) filled.label_rigid_bodies(discrete_bodies="Benzene", rigid_particles="C") assert filled.max_rigid_id == n_benzenes - 1 assert len(list(filled.rigid_particles())) == n_benzenes * 6
def test_particles_to_snapshot(self): 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) snap, _ = to_hoomdsnapshot(system) assert snap.particles.N == 10 assert snap.bonds.N == 0 assert snap.angles.N == 0
def test_fill_box_semi_rigid(self, benzene): n_benzenes = 10 benzene.label_rigid_bodies(rigid_particles='C') filled = mb.fill_box(benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) assert filled.contains_rigid is True assert filled.rigid_id is None assert filled.max_rigid_id == n_benzenes - 1 assert len(list(filled.rigid_particles())) == n_benzenes * 6
def water_system(self): water = mb.load(get_path('tip3p.mol2')) water.name = 'water' water[0].name = 'opls_111' water[1].name = water[2].name = 'opls_112' packed_system = mb.fill_box(compound=water, n_compounds=2, box=mb.Box([2, 2, 2])) return from_mbuild(packed_system)
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) init_snap = hoomd.data.make_snapshot(N=0, box=hoomd.data.boxdim(L=10)) with pytest.raises(RuntimeError): snap, _ = to_hoomdsnapshot(system, hoomd_snapshot=init_snap)
def test_bad_args(self, h2o): with pytest.raises(ValueError): mb.fill_box(h2o, n_compounds=10) with pytest.raises(ValueError): mb.fill_box(h2o, density=1000) with pytest.raises(ValueError): mb.fill_box(h2o, box=[2, 2, 2]) with pytest.raises(ValueError): mb.fill_box(h2o, n_compounds=10, density=1000, box=[2, 2, 2]) with pytest.raises(ValueError): mb.fill_box(compound=[h2o, h2o], n_compounds=[10], density=1000) with pytest.raises(ValueError): mb.solvate(solute=h2o, solvent=[h2o], n_solvent=[10, 10], box=[2, 2, 2]) with pytest.raises(ValueError): mb.fill_region(h2o, n_compounds=[10, 10], region=[2, 2, 2, 4, 4, 4])
def test_delete_body_semi_rigid(self, benzene): n_benzenes = 10 filled = mb.fill_box(benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) filled.label_rigid_bodies(discrete_bodies="Benzene", rigid_particles="C") filled.remove(filled.children[0]) assert filled.max_rigid_id == n_benzenes - 2 assert len(list(filled.rigid_particles())) == (n_benzenes - 1) * 6
def test_delete_body_particle_by_particle(self, rigid_benzene): n_benzenes = 10 filled = mb.fill_box(rigid_benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) for particle in list(filled.children[0].particles()): filled.remove(particle) assert filled.max_rigid_id == n_benzenes - 2 assert (len(list(filled.rigid_particles())) == (n_benzenes - 1) * rigid_benzene.n_particles)
def test_fill_box_semi_rigid(self, benzene): n_benzenes = 10 benzene.label_rigid_bodies(rigid_particles="C") filled = mb.fill_box(benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) assert filled.contains_rigid is True assert filled.rigid_id is None assert filled.max_rigid_id == n_benzenes - 1 assert len(list(filled.rigid_particles())) == n_benzenes * 6
def test_fill_box_rigid(self, rigid_benzene): n_benzenes = 10 filled = mb.fill_box(rigid_benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) assert filled.contains_rigid is True assert filled.rigid_id is None assert filled.max_rigid_id == n_benzenes - 1 assert (len(list(filled.rigid_particles())) == n_benzenes * rigid_benzene.n_particles)
def test_label_rigid_bodies_after_fill(self, benzene): n_benzenes = 10 filled = mb.fill_box(benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) filled.label_rigid_bodies(discrete_bodies="Benzene") assert filled.contains_rigid is True assert filled.rigid_id is None assert filled.max_rigid_id == n_benzenes - 1 assert (len(list(filled.rigid_particles())) == n_benzenes * benzene.n_particles)
def test_opls(self): eth = Alkane(n=10) cmpd = mb.fill_box(eth, n_compounds=10, box=[10, 10, 10]) ff = foyer.Forcefield(name='oplsaa') structure = ff.apply(cmpd) df = commpare.hoomd.build_run_measure_hoomd(structure, ref_energy=1 / 4.184, ref_distance=10) assert 'bond' in df assert 'angle' in df assert 'dihedral' in df assert 'nonbond' in df
def test_remove_from_box(self, ethane): n_ethanes = 5 box = mb.fill_box(ethane, n_ethanes, [3, 3, 3]) box.remove(box.children[3]) n_ethanes -= 1 assert box.n_particles == n_ethanes * ethane.n_particles assert len(box.children) == n_ethanes assert box.n_bonds == n_ethanes * ethane.n_bonds assert len([meth.referenced_ports() for eth in box.children for meth in eth.children]) == 2 * n_ethanes
def test_create_semi_rigid_bodies_filled_no_increment(self, benzene_from_parts): n_benzenes = 10 filled = mb.fill_box(benzene_from_parts, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) filled.label_rigid_bodies(discrete_bodies='Benzene', rigid_particles='C') filled2 = mb.clone(filled) filled.add(filled2, reset_rigid_ids=False) assert filled.max_rigid_id == n_benzenes - 1 assert len(list(filled.rigid_particles())) == n_benzenes * 2 * 6 for rigid_id in range(n_benzenes): assert len(list(filled.rigid_particles(rigid_id=rigid_id))) == 12
def test_label_rigid_bodies_list(self, benzene): n_benzenes = 10 filled = mb.fill_box(benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) filled.children[0].name = 'Benzene0' filled.children[1].name = 'Benzene1' filled.label_rigid_bodies(discrete_bodies=['Benzene0', 'Benzene1']) assert filled.contains_rigid is True assert filled.rigid_id is None assert filled.max_rigid_id == 1 assert len(list(filled.rigid_particles())) == 2 * benzene.n_particles
def solvate_bilayer(self): """Solvate the constructed bilayer. """ solvent_number_density = self.solvent.n_particles / np.prod(self.solvent.periodicity) lengths = self.lipid_box.lengths water_box_z = self.solvent_per_layer / (lengths[0] * lengths[1] * solvent_number_density) mins = self.lipid_box.mins maxs = self.lipid_box.maxs bilayer_solvent_box = mb.Box(mins=[mins[0], mins[1], maxs[2]], maxs=[maxs[0], maxs[1], maxs[2] + 2 * water_box_z]) self.solvent_components.add(mb.fill_box(self.solvent, bilayer_solvent_box))
def test_box_dimensions(self, benzene): import gsd n_benzenes = 10 filled = mb.fill_box(benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) filled.save(filename='benzene.gsd') gsd_file = gsd.pygsd.GSDFile(open('benzene.gsd', 'rb')) frame = gsd.hoomd.HOOMDTrajectory(gsd_file).read_frame(0) positions = frame.particles.position.astype(float) for coords in positions: assert coords.max() < 20 assert coords.min() > -20
def test_delete_body_all(self, rigid_benzene): n_benzenes = 10 filled = mb.fill_box(rigid_benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) for i, child in enumerate(filled.children[:-1]): filled.remove(child) assert filled.max_rigid_id == n_benzenes - 1 - (i + 1) assert len(list(filled.rigid_particles())) == (n_benzenes - (i + 1)) * rigid_benzene.n_particles assert filled.contains_rigid is True filled.remove(filled.children[0]) assert filled.contains_rigid is False assert filled.max_rigid_id is None
def test_rigid(self, benzene): n_benzenes = 10 benzene.name = 'Benzene' filled = mb.fill_box(benzene, n_compounds=n_benzenes, box=[0, 0, 0, 4, 4, 4]) filled.label_rigid_bodies(discrete_bodies='Benzene', rigid_particles='C') filled.save(filename='benzene.hoomdxml') xml_file = xml.etree.ElementTree.parse('benzene.hoomdxml').getroot() body_text = xml_file[0].find('body').text rigid_bodies = [int(body) for body in body_text.split('\n') if body] for body_id in range(10): assert rigid_bodies.count(body_id) == 6 assert rigid_bodies.count(-1) == n_benzenes * 6
def test_packmol_warning(self, h2o): with pytest.warns(UserWarning): filled = mb.fill_box(h2o, n_compounds=10, box=[1, 1, 1], overlap=10)
def test_bad_args(self, h2o): with pytest.raises(ValueError): mb.fill_box(h2o, n_compounds=10) with pytest.raises(ValueError): mb.fill_box(h2o, density=1000) with pytest.raises(ValueError): mb.fill_box(h2o, box=[2, 2, 2]) with pytest.raises(ValueError): mb.fill_box(h2o, n_compounds=10, density=1000, box=[2, 2, 2]) with pytest.raises(ValueError): mb.fill_box(compound=[h2o, h2o], n_compounds=[10], density=1000) with pytest.raises(ValueError): mb.solvate(solute=h2o, solvent=[h2o], n_solvent=[10, 10], box=[2, 2, 2]) with pytest.raises(ValueError): mb.fill_region(h2o, n_compounds=[10, 10], region=[2, 2, 2, 4, 4, 4]) with pytest.raises(ValueError): mb.fill_box(compound=[h2o, h2o], n_compounds=[10], density=1000, fix_orientation=[True, True, True])
def test_fill_box_density_box(self, h2o): filled = mb.fill_box(h2o, n_compounds=1000, density=1000) assert [3.1042931 < period < 3.1042932 for period in filled.periodicity]
def test_fill_box_density_n_compounds(self, h2o): filled = mb.fill_box(h2o, density=1000, box=mb.Box([3.1042931, 3.1042931, 3.1042931])) assert filled.n_particles == 3000
def test_fill_box_aspect_ratio(self, h2o): filled = mb.fill_box(h2o, n_compounds=1000, density=1000, aspect_ratio=[1, 2, 1]) assert filled.periodicity[0]/filled.periodicity[1] == 0.5 assert filled.periodicity[1]/filled.periodicity[2] == 2
def test_packmol_error(self, h2o): with pytest.raises(RuntimeError): filled = mb.fill_box(h2o, n_compounds=10, box=[0, 0, 0])
def test_packmol_log_error(self, h2o): try: filled = mb.fill_box(h2o, n_compounds=10, box=[0, 0, 0]) except(RuntimeError): with open("log.txt", "r") as logfile: assert "ERROR" in logfile.read()