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 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_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_create_representation_bond(self): with self.assertRaisesRegexp(ValueError, r'atomics.*only representable as.*Cannot create'): Molecule.from_z_matrix('O', create_representation=True) mol = Molecule.from_z_matrix('H\nH 1 0.9', create_representation=True) self.assertEqual(len(mol.internal_representations), 1) self.assertEqual(len(mol.internal_representations[0]), 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 test_errors_19(self): with self.assertRaisesRegexp(InvalidZMatrixException, r'atom indices in z-matrix line'): Molecule.from_z_matrix(''' O H 1 0.93 C 2 1.23 1 104.5 B 2 1.35 1 89.5 1 84.3 ''', create_representation=True)
def test_errors_18(self): with self.assertRaisesRegexp(IndexError, r"z-matrix index '0' is out of range"): Molecule.from_z_matrix(''' O H 1 0.93 C 2 1.23 1 104.5 B 2 1.35 1 89.5 0 84.3 ''', create_representation=True)
def test_future_errors(self): with self.assertRaisesRegexp(ValueError, r'Ambiguous atom identifier in z-matrix'): Molecule.from_z_matrix(""" O H O 1.0 H H 1.0 O 3.7 """)
def test_create_representation_bond(self): with self.assertRaisesRegexp( ValueError, r'atomics.*only representable as.*Cannot create'): Molecule.from_z_matrix('O', create_representation=True) mol = Molecule.from_z_matrix('H\nH 1 0.9', create_representation=True) self.assertEqual(len(mol.internal_representations), 1) self.assertEqual(len(mol.internal_representations[0]), 1)
def test_errors_11(self): with self.assertRaisesRegexp(ValueError, r'Ambiguous atom identifier in z-matrix'): Molecule.from_z_matrix(""" O H O 1.0 H O -1.0 H 37 O2 H 1.5 O 150 H 25 """)
def test_future_errors(self): with self.assertRaisesRegexp(ValueError, r'Ambiguous atom identifier in z-matrix'): Molecule.from_z_matrix( """ O H O 1.0 H H 1.0 O 3.7 """ )
def test_errors_18(self): with self.assertRaisesRegexp(IndexError, r"z-matrix index '0' is out of range"): Molecule.from_z_matrix(''' O H 1 0.93 C 2 1.23 1 104.5 B 2 1.35 1 89.5 0 84.3 ''', create_representation=True)
def test_errors_11(self): with self.assertRaisesRegexp(ValueError, r'Ambiguous atom identifier in z-matrix'): Molecule.from_z_matrix( """ O H O 1.0 H O -1.0 H 37 O2 H 1.5 O 150 H 25 """ )
def test_errors_19(self): with self.assertRaisesRegexp(InvalidZMatrixException, r'atom indices in z-matrix line'): Molecule.from_z_matrix(''' O H 1 0.93 C 2 1.23 1 104.5 B 2 1.35 1 89.5 1 84.3 ''', create_representation=True)
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 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 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 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 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 test_bond_length_2(self): mol = Molecule.from_z_matrix('H\nO 1 0.9', create_representation=True) mol.recenter() # 'randomize' the position in xyz space to avoid miniscule displacement amounts mol.rotate(Vector(1, 2, 1), 32. * Degrees) coord = mol.internal_representation[0] assert_has_valid_b_tensor(coord, 2)
def test_2_torsions_4_dry_run(self): mol = Molecule.from_z_matrix(''' O H 1 0.93 C 2 1.23 1 104.5 B 3 1.35 2 89.5 1 83.2 O 4 1.25 3 89.5 2 -23.2 ''', create_representation=True) mol.recenter() order = 4 coord = mol.internal_representation[5] natoms = mol.natoms btens = coord.parent_representation.b_tensor() analytic_tensor = Tensor(shape=(3 * natoms, ) * order) for cartcoords in product(coord.variables, repeat=order): analytic_tensor[tuple( c.index for c in cartcoords)] = btens[(coord, ) + cartcoords] coord = mol.internal_representation[8] natoms = mol.natoms btens = coord.parent_representation.b_tensor() analytic_tensor = Tensor(shape=(3 * natoms, ) * order) for cartcoords in product(coord.variables, repeat=order): analytic_tensor[tuple( c.index for c in cartcoords)] = btens[(coord, ) + cartcoords]
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 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_bond_length_2(self): mol = Molecule.from_z_matrix('H\nO 1 0.9', create_representation=True) mol.recenter() # 'randomize' the position in xyz space to avoid miniscule displacement amounts mol.rotate(Vector(1,2,1), 32.*Degrees) coord = mol.internal_representation[0] assert_has_valid_b_tensor(coord, 2)
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 test_create_representation_tors_2(self): mol = Molecule.from_z_matrix(""" H O 1 1.0 O 2 1.0 1 90 H 3 1.0 2 90 1 -90 """, create_representation=True ) self.assertEqual(mol.internal_representations[0].coords[5].value_with_units.in_units(AngularUnit.default), (-90.*Degrees).in_units(AngularUnit.default))
def test_bond_length_7_dry_run(self): self.setUp() order = 7 mol = Molecule.from_z_matrix('H\nO 1 0.9', create_representation=True) mol.recenter() coord = mol.internal_representation[0] natoms = mol.natoms btens = coord.parent_representation.b_tensor() analytic_tensor = Tensor(shape=(3*natoms,)*order) for cartcoords in product(coord.variables, repeat=order): analytic_tensor[tuple(c.index for c in cartcoords)] = btens[(coord,) + cartcoords]
def test_bond_length_7_dry_run(self): self.setUp() order = 7 mol = Molecule.from_z_matrix('H\nO 1 0.9', create_representation=True) mol.recenter() coord = mol.internal_representation[0] natoms = mol.natoms btens = coord.parent_representation.b_tensor() analytic_tensor = Tensor(shape=(3 * natoms, ) * order) for cartcoords in product(coord.variables, repeat=order): analytic_tensor[tuple( c.index for c in cartcoords)] = btens[(coord, ) + cartcoords]
def test_create_representation_tors_2(self): mol = Molecule.from_z_matrix(""" H O 1 1.0 O 2 1.0 1 90 H 3 1.0 2 90 1 -90 """, create_representation=True) self.assertEqual( mol.internal_representations[0].coords[5].value_with_units. in_units(AngularUnit.default), (-90. * Degrees).in_units(AngularUnit.default))
def test_create_rep_hooh(self): mol = Molecule.from_z_matrix(""" H O 1 0.963242 O 2 1.449863 1 100.120071 H 3 0.963242 2 100.120071 1 -67.344079 """, create_representation=True) rep = mol.internal_representation deg_to_def = Degrees.to(AngularUnit.default) self.assertAlmostEqual( rep[5].value_with_units.in_units(AngularUnit.default), -67.344079 * deg_to_def)
def test_create_representation_ang(self): mol = Molecule.from_z_matrix(""" O H1 O 1.0 H2 O 1.0 H1 90 """, create_representation=True) self.assertEqual(len(mol.internal_representations), 1) self.assertEqual(len(mol.internal_representations[0]), 3) self.assertEqual( mol.internal_representations[0].coords[2].value_with_units. in_units(AngularUnit.default), (90. * Degrees).in_units(AngularUnit.default))
def test_create_representation_ang(self): mol = Molecule.from_z_matrix(""" O H1 O 1.0 H2 O 1.0 H1 90 """, create_representation=True ) self.assertEqual(len(mol.internal_representations), 1) self.assertEqual(len(mol.internal_representations[0]), 3) self.assertEqual( mol.internal_representations[0].coords[2].value_with_units.in_units(AngularUnit.default), (90.*Degrees).in_units(AngularUnit.default) )
def test_create_rep_hooh(self): mol = Molecule.from_z_matrix(""" H O 1 0.963242 O 2 1.449863 1 100.120071 H 3 0.963242 2 100.120071 1 -67.344079 """, create_representation=True ) rep = mol.internal_representation deg_to_def = Degrees.to(AngularUnit.default) self.assertAlmostEqual( rep[5].value_with_units.in_units(AngularUnit.default), -67.344079 * deg_to_def)
def setUpForValue(self, val, randomize_cartesian_space=True): self.mol = Molecule.from_z_matrix(''' O H 1 0.93 C 2 1.23 1 104.5 B 3 1.35 2 89.5 1 ''' + str(val), create_representation=True) self.mol.recenter() if randomize_cartesian_space: # 'randomize' the position in xyz space to avoid miniscule displacement amounts self.mol.rotate(Vector(0,0,1), 32 * Degrees.to(Radians)) self.mol.rotate(Vector(0,1,0), 32 * Degrees.to(Radians)) self.mol.rotate(Vector(1,0,0), 32 * Degrees.to(Radians)) self.coord = self.mol.internal_representation[5]
def setUpForValue(self, val, randomize_cartesian_space=True): self.mol = Molecule.from_z_matrix(''' O H 1 0.93 C 2 1.23 1 104.5 B 3 1.35 2 89.5 1 ''' + str(val), create_representation=True) self.mol.recenter() if randomize_cartesian_space: # 'randomize' the position in xyz space to avoid miniscule displacement amounts self.mol.rotate(Vector(0, 0, 1), 32 * Degrees.to(Radians)) self.mol.rotate(Vector(0, 1, 0), 32 * Degrees.to(Radians)) self.mol.rotate(Vector(1, 0, 0), 32 * Degrees.to(Radians)) self.coord = self.mol.internal_representation[5]
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 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 test_2_torsions_4_dry_run(self): mol = Molecule.from_z_matrix(''' O H 1 0.93 C 2 1.23 1 104.5 B 3 1.35 2 89.5 1 83.2 O 4 1.25 3 89.5 2 -23.2 ''', create_representation=True) mol.recenter() order = 4 coord = mol.internal_representation[5] natoms = mol.natoms btens = coord.parent_representation.b_tensor() analytic_tensor = Tensor(shape=(3*natoms,)*order) for cartcoords in product(coord.variables, repeat=order): analytic_tensor[tuple(c.index for c in cartcoords)] = btens[(coord,) + cartcoords] coord = mol.internal_representation[8] natoms = mol.natoms btens = coord.parent_representation.b_tensor() analytic_tensor = Tensor(shape=(3*natoms,)*order) for cartcoords in product(coord.variables, repeat=order): analytic_tensor[tuple(c.index for c in cartcoords)] = btens[(coord,) + cartcoords]
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 _generated(self, aval): mol = Molecule.from_z_matrix('H\nO 1 1.0\nH 2 1.0 1 ' + str(aval), create_representation=True) coord = mol.internal_representation[2] self.assertHasValidBVector(coord)
def test_bond_angle(self): mol = Molecule.from_z_matrix('H\nO 1 1.0\nH 2 1.0 1 90', create_representation=True) coord = mol.internal_representation[2] self.assertHasValidBVector(coord)
def test_bond_length(self): mol = Molecule.from_z_matrix('H\nO 1 1.0', create_representation=True) coord = mol.internal_representation[0] self.assertHasValidBVector(coord)
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'))
def test_bond_length_6(self): mol = Molecule.from_z_matrix('H\nO 1 0.9', create_representation=True) mol.recenter() coord = mol.internal_representation[0] assert_has_valid_b_tensor(coord, 6)
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'))
def test_init(self): self.assertEqual(Molecule("H 1 -1 0"), Molecule([Atom('H', [1, -1, 0])]))
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 _generated(self, aval): mol = Molecule.from_z_matrix('H\nO 1 1.0\nH 2 1.0 1 ' + str(aval), create_representation=True) coord = mol.internal_representation[2] self.assertHasValidBVector(coord)
def test_bond_angle(self): mol = Molecule.from_z_matrix('H\nO 1 1.0\nH 2 1.0 1 90', create_representation=True) coord = mol.internal_representation[2] self.assertHasValidBVector(coord)
def test_bond_length(self): mol = Molecule.from_z_matrix('H\nO 1 1.0', create_representation=True) coord = mol.internal_representation[0] self.assertHasValidBVector(coord)
def test_bond_length_6(self): mol = Molecule.from_z_matrix('H\nO 1 0.9', create_representation=True) mol.recenter() coord = mol.internal_representation[0] assert_has_valid_b_tensor(coord, 6)