def assertAlmostEqualMolecules(self, act, exp, msg=None, places=7): a_atoms = copy(exp.atoms) b_atoms = copy(act.atoms) for atom in a_atoms: atom.position = Vector( [round(num, places) for num in atom.position]) for atom in b_atoms: atom.position = Vector( [round(num, places) for num in atom.position]) round_a = Molecule(a_atoms) round_b = Molecule(b_atoms) if not round_a == round_b: seq_a = [] for a in round_a: seq_a.append((a.symbol, a.position)) seq_b = [] for b in round_b: seq_b.append((b.symbol, b.position)) seq_msg = None try: self.assertSequenceEqual(seq_a, seq_b) except AssertionError as err: seq_msg = err.message if not seq_msg: raise RuntimeError( "Molecule almost equal assertion inconclusive: Rounded sequences are equal but rounded molecules are not." ) if msg is None: msg = "\nExpected:\n{exp}\nGot:\n{act}\n\nAs sequences:\n".format( exp=exp, act=act, ndigits=places) + seq_msg raise unittest.TestCase.failureException(msg)
def test_displace(self): mol = Molecule.from_z_matrix('H') mol.displace(Vector(1., -1., 0.)) self.assertEqual(mol, Molecule("H 1 -1 0")) mol = Molecule.from_z_matrix('H\nH 1 0.9') mol.displace(Vector(1., 0., 0., 1., 0., 0.)) self.assertEqual(mol, Molecule("H 1 0 0.0\nH 1 0 0.9 ")) with self.assertRaisesRegexp(ValueError, 'dimension mismatch'): mol.displace(Vector(1., 0., 0.)) with self.assertRaises(TypeError): mol.displace(Matrix(1., 0., 0.))
def phi(i, j, k): mol = Molecule([ Atom('H', args[i - 1]), Atom('H', args[j - 1]), Atom('H', args[k - 1]) ]) return BondAngle(mol[0], mol[1], mol[2])
def test_groups_1(self): m = Molecule([ Atom('C', [0.00000000, 0.00000000, 0.00000000]), Atom('C', [0.00000000, 0.00000000, 4.76992933]), Atom('O', [0.00000000, -1.04316184, 0.61707065]), Atom('O', [0.01905095, 1.04298787, 4.15285868]), Atom('C', [-0.11039651, 1.34908096, 0.68132447]), Atom('C', [-0.13501595, -1.34683982, 4.08860486]), Atom('C', [0.10780157, 0.01502933, -1.51597276]), Atom('C', [0.10750912, -0.01699557, 6.28590209]), Atom('H', [-0.08557151, 1.24276213, 1.76717696]), Atom('H', [-0.10825342, -1.24099210, 3.00275237]), Atom('H', [0.69789248, 2.01081145, 0.34934100]), Atom('H', [0.66105324, -2.02322149, 4.42058833]), Atom('H', [-1.04824273, 1.83250625, 0.38051647]), Atom('H', [-1.08153441, -1.81305690, 4.38941286]), Atom('H', [0.11566492, -1.00528185, -1.90094854]), Atom('H', [0.13400478, 1.00300183, 6.67087787]), Atom('H', [-0.72590461, 0.57377279, -1.95554705]), Atom('H', [-0.73626218, -0.56042012, 6.72547638]), Atom('H', [1.02679855, 0.52908924, -1.82068069]), Atom('H', [1.01696471, -0.54775311, 6.59061002]) ], units=Angstroms) groups = m.geometric_subgroups() self.assertEqual(len(groups), 2)
def test_errors_14(self): with self.assertRaises(InvalidXYZFormatError): Molecule(""" H 1.00 1.00 7.5 C 3.2a 1.0 1.0 O 2.2 2.2 2.2 """)
def _parse_stream(self, f): if self.inner_regex.groups < 4: raise ValueError("Not enough capturing groups in inner regex.") #----------------------------------------# data = f.read() matches = list(self.outer_regex.finditer(data)) if len(matches) == 0: raise ValueError("No molecule section found in file {}".format(self.file_path)) elif len(matches) > 1: raise ValueError("More than one molecule section in file {} (total of {} matches found)".format( self.file_path, len(matches) )) else: xyz_lines = [] natoms = 0 mol_section = matches[0].group(1) for m in self.inner_regex.finditer(mol_section): xyz_lines.append("{0} {1} {2} {3}".format(*m.groups())) natoms += 1 if natoms == 0: raise ValueError("No atoms found in geometry section of {}".format(self.file_path)) return Molecule( xyz_string="\n".join(xyz_lines) )
def test_misc(self): # getitem self.assertEqual(Molecule.from_z_matrix("O")[0].symbol, "O") # contains mol = Molecule.from_z_matrix("O") self.assertIn(mol[0], mol) self.assertNotIn(Atom("H", 0, 0, 0), mol) # less than self.assertLess(mol, Molecule("O 0.0 0.0 1.0"))
def _parse_stream(self, f): data = f.read() matches = list(self.regex.finditer(data)) if len(matches) == 0: raise ValueError("No geometry found in file {}".format(self.file_path)) elif len(matches) > 1: raise ValueError("More than 1 geometry found in file {} (total of {} matches found)".format( self.file_path, len(matches) )) else: return Molecule( xyz_string=matches[0].group(1) )
def test_zmat(self): self.assertEqual( Molecule([Atom("O", [0.0, 0.0, 0.0])]), Molecule.from_z_matrix(""" O """)) self.assertEqual( Molecule([Atom("O", [0.0, 0.0, 0.0]), Atom("O", [0.0, 0.0, 1.0])]), Molecule.from_z_matrix(""" O1 O2 O1 1.0 """)) self.assertEqual( Molecule([ Atom("O", [0.0, 0.0, 0.0]), Atom("H", [0.0, 0.0, 1.0]), Atom("H", [0.0, -1.0, 0.0]) ]), Molecule.from_z_matrix(""" O H1 O 1.0 H2 O 1.0 H1 90 """))
def setUp(self): self.mol = Molecule(""" 4 H2O2 H -1.21670000 -0.75630000 0.00000000 O -0.73020000 0.07940000 -0.00000000 O 0.73020000 -0.07940000 -0.00000000 H 1.21670000 0.75630000 0.00000000 """) self.rep = InternalRepresentation(self.mol, [ BondLength(1, 2, self.mol), BondLength(1, 3, self.mol), BondAngle(2, 1, 3, self.mol), InternalCartesianX(1, 2, 3, 4, self.mol), InternalCartesianY(1, 2, 3, 4, self.mol), InternalCartesianZ(1, 2, 3, 4, self.mol) ])
def _convert_obmol(self, obmol): symbols = [] positions = [] for atom in openbabel.OBMolAtomIter(obmol): symbols.append(ElementData.get(atomic_number=atom.GetAtomicNum()).symbol) positions.append((atom.x(), atom.y(), atom.z())) charge = self._get_charge(obmol) multiplicity = self._get_multiplicity(obmol) description = self._get_description(obmol) rv = Molecule( atom_names=symbols, cart_mat=positions, charge=charge, multiplicity=multiplicity, description=description ) rv.obmol = obmol return rv
def R(i, j): # This is ugly... # TODO figure out a better way to do this mol = Molecule([Atom('H', args[i - 1]), Atom('H', args[j - 1])]) return BondLength(mol[0], mol[1])
def test_init(self): self.assertEqual(Molecule("H 1 -1 0"), Molecule([Atom('H', [1, -1, 0])]))
def test_reorient_2(self): m = Molecule('H 0 0 0\nH 0 1 0') self.assertEqual(m.reoriented('II'), Molecule('H 0 -0.5 0\nH 0 0.5 0'))