def species_topologies(self, species_topologies): self._constrained_species = [] if self._species_topologies is None: if not isinstance(species_topologies, list): raise TypeError( '"species_topologies" should be a list. ' "See help(mosdef_Cassandra.System) for details.") for topology in species_topologies: if not isinstance(topology, parmed.Structure): raise TypeError("Each species should be a " "parmed.Structure") # If no bonds in topology don't try to apply constraints # Store "None" in _constrained_species instead if len(topology.bonds) > 0: constrain = ConstrainedMolecule(topology) constrain.solve() topology.coordinates = constrain.xyz self._constrained_species.append(constrain) else: self._constrained_species.append(None) self._species_topologies = [ parmed.structure.copy(s) for s in species_topologies ] else: raise AttributeError("species_topologies cannot be " "modified after System object is created. " "Create a new System object if you wish to " "edit the species_topolgies")
def test_benzene(self, benzene_oplsaa): constrain_mol = ConstrainedMolecule(benzene_oplsaa) constrain_mol.solve() optimized = constrain_mol.structure xyz = optimized.coordinates for bond in optimized.bonds: idx1 = bond.atom1.idx idx2 = bond.atom2.idx dist = np.sqrt(np.sum((xyz[idx2] - xyz[idx1])**2)) assert np.allclose(dist, bond.type.req)
def test_solved_model(self, ethane_ua): constrain_mol = ConstrainedMolecule(ethane_ua) constrain_mol.solve() assert np.allclose(constrain_mol.model.x_start[0].value, ethane_ua.coordinates[0, 0]) assert np.allclose(constrain_mol.model.x_start[1].value, ethane_ua.coordinates[1, 0]) assert np.allclose(constrain_mol.model.y_start[0].value, ethane_ua.coordinates[0, 1]) assert np.allclose(constrain_mol.model.y_start[1].value, ethane_ua.coordinates[1, 1]) assert np.allclose(constrain_mol.model.z_start[0].value, ethane_ua.coordinates[0, 2]) assert np.allclose(constrain_mol.model.z_start[1].value, ethane_ua.coordinates[1, 2]) assert np.allclose(constrain_mol.model.x[0].value, ethane_ua.coordinates[0, 0]) assert np.allclose(constrain_mol.model.x[1].value, ethane_ua.coordinates[1, 0]) assert np.allclose(constrain_mol.model.y[0].value, ethane_ua.coordinates[0, 1]) assert np.allclose(constrain_mol.model.y[1].value, ethane_ua.coordinates[1, 1]) assert np.allclose(constrain_mol.model.z[0].value, ethane_ua.coordinates[0, 2]) assert np.allclose(constrain_mol.model.z[1].value, ethane_ua.coordinates[1, 2]) assert constrain_mol.model_solved is True
def test_resolve_model(self, propane_ua): constrain_mol = ConstrainedMolecule(propane_ua) constrain_mol.solve() assert constrain_mol.model_solved is True propane_solved = parmed.structure.copy(constrain_mol.structure) constrain_mol.update_xyz(propane_ua.coordinates) assert constrain_mol.model_solved is False constrain_mol.solve() assert constrain_mol.model_solved is True assert np.allclose(propane_solved.coordinates, constrain_mol.structure.coordinates)
def test_update_xyz(self, ethane_ua): constrain_mol = ConstrainedMolecule(ethane_ua) ethane_ua.coordinates[0, 0] = -1.0 ethane_ua.coordinates[0, 1] = -0.3 ethane_ua.coordinates[0, 2] = 0.2 constrain_mol.update_xyz(ethane_ua.coordinates) assert np.allclose(constrain_mol.structure.coordinates, ethane_ua.coordinates) assert np.allclose(constrain_mol.model.x_start[0].value, ethane_ua.coordinates[0, 0]) assert np.allclose(constrain_mol.model.x_start[1].value, ethane_ua.coordinates[1, 0]) assert np.allclose(constrain_mol.model.y_start[0].value, ethane_ua.coordinates[0, 1]) assert np.allclose(constrain_mol.model.y_start[1].value, ethane_ua.coordinates[1, 1]) assert np.allclose(constrain_mol.model.z_start[0].value, ethane_ua.coordinates[0, 2]) assert np.allclose(constrain_mol.model.z_start[1].value, ethane_ua.coordinates[1, 2]) assert np.allclose(constrain_mol.model.x[0].value, ethane_ua.coordinates[0, 0]) assert np.allclose(constrain_mol.model.x[1].value, ethane_ua.coordinates[1, 0]) assert np.allclose(constrain_mol.model.y[0].value, ethane_ua.coordinates[0, 1]) assert np.allclose(constrain_mol.model.y[1].value, ethane_ua.coordinates[1, 1]) assert np.allclose(constrain_mol.model.z[0].value, ethane_ua.coordinates[0, 2]) assert np.allclose(constrain_mol.model.z[1].value, ethane_ua.coordinates[1, 2])
def test_box_diethylether(self, diethylether_box): (dee_ff, box) = diethylether_box constrain_mol = ConstrainedMolecule(dee_ff) for mol in box.children: constrain_mol.update_xyz(mol.xyz * 10) # nm to angstrom constrain_mol.solve() optimized = constrain_mol.structure xyz = optimized.coordinates for bond in optimized.bonds: idx1 = bond.atom1.idx idx2 = bond.atom2.idx dist = np.sqrt(np.sum((xyz[idx2] - xyz[idx1])**2)) assert np.allclose(dist, bond.type.req)
def test_copy_coordinates(self, ethane_ua): constrain_mol = ConstrainedMolecule(ethane_ua) assert np.allclose(constrain_mol.structure.coordinates, ethane_ua.coordinates)
def test_invalid_update(self, ethane_ua): constrain_mol = ConstrainedMolecule(ethane_ua) with pytest.raises(TypeError): constrain_mol.update_xyz("string") with pytest.raises(ValueError): constrain_mol.update_xyz([[1.0, 1.0, 1.0]])
def test_invalid_init(self): with pytest.raises(TypeError): ConstrainedMolecule("structure")
def test_model_constraints(self, ethane_ua): constrain_mol = ConstrainedMolecule(ethane_ua) assert np.allclose(constrain_mol.model.bond_lengths[(0, 1)], ethane_ua.bonds[0].type.req)
def test_model_solved(self, ethane_ua): constrain_mol = ConstrainedMolecule(ethane_ua) assert constrain_mol.model_solved is False
def test_create_model(self, ethane_ua): constrain_mol = ConstrainedMolecule(ethane_ua) assert isinstance(constrain_mol.model, pyo.ConcreteModel)
import mbuild import foyer from constrainmol import ConstrainedMolecule ff = foyer.forcefields.load_OPLSAA() mol = mbuild.load("c1ccccc1O", smiles=True) mol_ff = ff.apply(mol) mol_ff.save("unconstrained.pdb", overwrite=True) constrain_mol = ConstrainedMolecule(mol_ff) constrain_mol.solve() # Before we update the coordinates let's see # how much they changed diff = mol_ff.coordinates - constrain_mol.xyz print("Final (x,y,z) - initial (x,y,z)") print(diff) mol_ff.coordinates = constrain_mol.xyz mol_ff.save("constrained.pdb", overwrite=True)
import parmed import numpy as np from constrainmol import ConstrainedMolecule # Load from gro/topology system = parmed.load_file("system.top", xyz="system.gro") system.save("unconstrained.pdb", overwrite=True) constrained_coordinates = np.zeros(system.coordinates.shape) unique_res = system.split() for (res, resids) in unique_res: constrain_mol = ConstrainedMolecule(res) for resid in resids: constrain_mol.update_xyz(system[resid, :].coordinates) constrain_mol.solve() atom_ids = [atom.idx for atom in system.residues[resid].atoms] constrained_coordinates[atom_ids] = constrain_mol.structure.coordinates system.coordinates = constrained_coordinates system.save("constrained.pdb", overwrite=True)
def test_xyz_setter(self, benzene_oplsaa): mol = ConstrainedMolecule(benzene_oplsaa) new_xyz = np.zeros(benzene_oplsaa.coordinates.shape) with pytest.raises(ValueError): mol.xyz = new_xyz
def test_xyz_getter(self, benzene_oplsaa): mol = ConstrainedMolecule(benzene_oplsaa) assert np.allclose(mol.xyz, benzene_oplsaa.coordinates)
def test_no_bonds(self): with pytest.raises(ValueError): ConstrainedMolecule(parmed.Structure())
import mbuild import foyer from constrainmol import ConstrainedMolecule ff = foyer.forcefields.load_OPLSAA() dee = mbuild.load("CCOCC", smiles=True) dee_ff = ff.apply(dee) box = mbuild.fill_box(dee, 500, density=600) box.save("unconstrained.pdb", overwrite=True) constrain_mol = ConstrainedMolecule(dee_ff) for mol in box.children: constrain_mol.update_xyz(mol.xyz * 10) # nm to angstrom constrain_mol.solve() mol.xyz = constrain_mol.xyz / 10.0 # angstrom to nm box.save("constrained.pdb", overwrite=True)
def test_copy_bonds(self, ethane_ua): constrain_mol = ConstrainedMolecule(ethane_ua) assert len(constrain_mol.structure.bonds) == len(ethane_ua.bonds)