Example #1
0
 def test_check_geom(self):
     """Test check_geom throws errors correctly"""
     self.assertTrue(Molecule.check_geom(self.water_geom))
     # Zero-length geometries are valid
     self.assertTrue(Molecule.check_geom([]))
     self.assertRaises(ValueError, Molecule.check_geom, [[[1, 2, 3]]])
     self.assertRaises(TypeError, Molecule.check_geom, ['H'], [[[0, 1, 2, 3]]])
Example #2
0
 def test_check_geom(self):
     """Test check_geom throws errors correctly"""
     water = [['H', 0, 0, 0], ['O', 0, 0, 1], ['H', 0, 1, 1]]
     self.assertTrue(Molecule.check_geom(water))
     # Zero-length geometries are valid
     self.assertTrue(Molecule.check_geom([]))
     self.assertRaises(SyntaxError, Molecule.check_geom, [[[1, 2, 3]]])
     self.assertRaises(SyntaxError, Molecule.check_geom, [[[0, 1, 2, 3]]])
     self.assertRaises(SyntaxError, Molecule.check_geom, [[['H', 1, 'a', 3]]])
Example #3
0
 def test_check_atom(self):
     """Test check_atom throws errors correctly"""
     atom, xyz = ['H', [0, 1, 2]]
     self.assertTrue(Molecule.check_atom(atom, xyz))
     self.assertRaises(SyntaxError, Molecule.check_atom, [[]], 'a')
     self.assertRaises(TypeError, Molecule.check_atom, [[1, 2, 3]])
     self.assertRaises(SyntaxError, Molecule.check_atom, [[0, 1, 2, 3]], 'a')
     self.assertRaises(SyntaxError, Molecule.check_atom, ['H', [1]], ['a', [3]])
Example #4
0
 def test_check_atom(self):
     """Test check_atom throws errors correctly"""
     atom = ['H', 0, 1, 2]
     self.assertTrue(Molecule.check_atom(atom))
     self.assertRaises(SyntaxError, Molecule.check_atom, [[]])
     self.assertRaises(SyntaxError, Molecule.check_atom, [[1, 2, 3]])
     self.assertRaises(SyntaxError, Molecule.check_atom, [[0, 1, 2, 3]])
     self.assertRaises(SyntaxError, Molecule.check_atom, [['H', 1, 'a', 3]])
Example #5
0
    def test_read_write_geometry(self):
        """Testing read and write"""
        geom_file = 'geom.xyz.tmp'
        self.water.write(geom_file, True)
        mol = Molecule.read_from(geom_file)
        mol.name = 'H2O'
        self.assertEqual(mol.geom, self.water.geom)
        mol.write(geom_file, style='latex')

        latex_geom = '''\
\\begin{verbatim}
H       0.000000      0.000000      0.000000
O       0.000000      0.000000      1.000000
H       0.000000      1.000000      1.000000
\\end{verbatim}'''
        with open(geom_file) as f:
            out_tex = f.read()
        self.assertEqual(latex_geom, out_tex)

        os.remove(geom_file)
class TestGamessifier(unittest.TestCase):
    """Tests the Molecule class"""
    def setUp(self):
        """Set up for every test"""
        self.g = Gamessifier()
        self.formaldehyde_xyz = Molecule([['O', [0, 1.394, 0]],
                                          ['C', [0, 0, 0]],
                                          ['H', [0.994, -0.492, 0]],
                                          ['H', [-0.994, -0.492, 0]]])

    def test_read_mol(self):
        """Test reading a molecule from a file"""
        tmp_geom_file = 'geom.xyz.tmp'
        self.formaldehyde_xyz.write(tmp_geom_file)
        self.g.read_mol(tmp_geom_file)
        self.assertEqual(str(self.g.mol), str(self.formaldehyde_xyz))

        os.remove(tmp_geom_file)

    def test_read_basis_set(self):
        """Test reading a basis from a file"""
        tmp_basis_file = 'basis.gbs.tmp1'
        basis_set = BasisSet()
        basis_set['B'] = Basis('B',
                               [BasisFunction('S', [0.2, 0.4], [0.3, 0.7])])
        with open(tmp_basis_file, 'w') as f:
            f.write(basis_set.print('gamess'))
        self.g.read_basis_set(tmp_basis_file)
        self.assertEqual(basis_set, self.g.basis_set)

        os.remove(tmp_basis_file)

    def test_read_ecp(self):
        """Testing reading an ecp file"""
        tmp_ecp_file = 'ecp.dat.tmp'
        ecp = """\
H-ECP NONE
C-ECP GEN   10  2
    3      ----- d-ul potential     -----
      -10.0000000        1    357.3914469
      -60.2990287        2     64.6477389
      -10.4217834        2     16.0960833
O-ECP NONE"""
        with open(tmp_ecp_file, 'w') as f:
            f.write(ecp)
        self.g.read_ecp(tmp_ecp_file)
        os.remove(tmp_ecp_file)

    def test_read_options(self):
        """Testing read options"""
        self.g.read_options({})
        self.assertEqual({}, self.g.options_dict)
        self.assertEqual('', self.g.write_options_str())
        options_dir = OrderedDict(
            [['CONTRL', OrderedDict([['SCFTYP', 'RHF']])],
             ['SCF', OrderedDict([['DIRSCF', '.TRUE.']])]])
        options_str = ' $CONTRL\n    SCFTYP=RHF\n $END\n\n $SCF\n    DIRSCF=.TRUE.\n $END\n\n'
        self.g.read_options(options_dir)
        self.assertEqual(options_str, self.g.write_options_str())

        tmp_options_file = 'options.dat.tmp'
        with open(tmp_options_file, 'w') as f:
            f.write(options_str)
        self.g.read_options(tmp_options_file)

    def test_read_other_data(self):
        """Testing read_other_data"""
        tmp_dat_file = 'dat.tmp'
        vec = ' $VEC\n12 23 31\n33241 32523 11.0\n $END'
        hess = ' $HESS\n32 43 987\n453 443 11.0\n $END'
        data = 'Hey\n' + vec + ' \n random other text\n' + hess + '\n more text\n122\n'
        with open(tmp_dat_file, 'w') as f:
            f.write(data)
        self.g.read_data(tmp_dat_file)
        self.assertEqual(vec, self.g.vec)
        self.assertEqual(hess, self.g.hess)

        os.remove(tmp_dat_file)

    def test_update_options(self):
        """Test update_options"""
        self.g.vec = ' $VEC\n123132\n $END'
        self.g.options_dict = {'GUESS': {'GUESS': 'HUCKEL'}}
        self.g.update_options()
        self.assertEqual('MOREAD', self.g.options_dict['GUESS']['GUESS'])
        self.g.vec = ' '

    def test_write_input(self):
        """Test writing an input to a basis file"""
        tmp_basis_file = 'basis.gbs.tmp'
        basis_set = BasisSet()
        basis_set['H'] = Basis('H', [BasisFunction('S', [1], [1])])
        basis_set['O'] = Basis(
            'O', [BasisFunction('S', [1], [1]),
                  BasisFunction('S', [2], [1])])
        basis_set['C'] = Basis('C', [
            BasisFunction('S', [1], [1]),
            BasisFunction('SP', [1, 2], [[0.4, 0.6], [0.1, 0.9]])
        ])
        with open(tmp_basis_file, 'w') as f:
            f.write(basis_set.print('gamess'))
        tmp_geom_file = 'geom.xyz.tmp'
        self.formaldehyde_xyz.write(tmp_geom_file)
        tmp_ecp_file = 'ecp.dat.tmp'
        ecp = """\
C-ECP GEN   10  2
    3      ----- d-ul potential     -----
      -10.0000000        1    357.3914469
      -60.2990287        2     64.6477389
      -10.4217834        2     16.0960833
O-ECP NONE"""
        with open(tmp_ecp_file, 'w') as f:
            f.write(ecp)
        tmp_options_file = 'options.dat.tmp'
        options_str = ' $CONTRL\n    SCFTYP=RHF\n $END\n\n $SCF\n    DIRSCF=.TRUE.\n $END\n\n'
        with open(tmp_options_file, 'w') as f:
            f.write(options_str)
        tmp_dat_file = 'dat.tmp'
        vec = ' $VEC\n12 23 31\n33241 32523 11.0\n $END'
        hess = ' $HESS\n32 43 987\n453 443 11.0\n $END'
        data = 'Hey\n' + vec + ' \n random other text\n' + hess + '\n more text\n122\n'
        with open(tmp_dat_file, 'w') as f:
            f.write(data)

        self.g.read(tmp_geom_file, tmp_basis_file, tmp_ecp_file,
                    tmp_options_file, tmp_dat_file)

        tmp_input_file = 'input.inp.tmp'
        self.g.write_input(tmp_input_file)

        os.remove(tmp_geom_file)
        os.remove(tmp_basis_file)
        os.remove(tmp_ecp_file)
        os.remove(tmp_options_file)
        os.remove(tmp_input_file)
        os.remove(tmp_dat_file)
Example #7
0
 def setUp(self):
     """Set up for every test"""
     self.water_geom = [['H', [0, 0, 0]], ['O', [0, 0, 1]],
                        ['H', [0, 1, 1]]]
     self.water = Molecule(self.water_geom)
Example #8
0
class TestMolecule(unittest.TestCase):
    """Tests the Molecule class"""
    def setUp(self):
        """Set up for every test"""
        self.water_geom = [['H', [0, 0, 0]], ['O', [0, 0, 1]],
                           ['H', [0, 1, 1]]]
        self.water = Molecule(self.water_geom)

    def test_len(self):
        """Testing __len__"""
        self.assertEqual(len(self.water), 3)

    def test_getsetdeleqinsert(self):
        """Test getting, setting and deleting atoms"""
        self.assertEqual(self.water[0][0], 'H')
        self.assertTrue(all(self.water[0][1] == np.array([0, 0, 0])))
        del self.water[1]
        self.assertEqual(self.water[1][0], 'H')
        self.assertTrue(all(self.water[1][1] == np.array([0, 1, 1])))
        self.water[0] = ['H', [0, 0, 0]]
        self.water[1] = ['H', [0, -1, 1]]
        self.assertEqual(self.water[1][0], 'H')
        self.assertTrue(all(self.water[1][1] == np.array([0, -1, 1])))
        self.water.insert(1, 'O', [0, 0, 1])
        self.assertEqual(self.water[1][0], 'O')
        self.assertTrue(all(self.water[1][1] == np.array([0, 0, 1])))
        self.assertEqual(self.water[2][0], 'H')
        self.assertTrue(all(self.water[2][1] == np.array([0, -1, 1])))
        new_water = Molecule([['H', [0, 0, 0]], ['O', [0, 0, 1]],
                              ['H', [0, -1, 1]]])
        self.assertEqual(self.water, new_water)

    def test_str(self):
        """Testing __str__"""
        water_string = """\
H       0.00000000    0.00000000    0.00000000
O       0.00000000    0.00000000    1.00000000
H       0.00000000    1.00000000    1.00000000"""
        self.assertEqual(str(self.water), water_string)

    def test_check_atom(self):
        """Test check_atom throws errors correctly"""
        atom, xyz = ['H', [0, 1, 2]]
        self.assertTrue(Molecule.check_atom(atom, xyz))
        self.assertRaises(SyntaxError, Molecule.check_atom, [[]], 'a')
        self.assertRaises(TypeError, Molecule.check_atom, [[1, 2, 3]])
        self.assertRaises(SyntaxError, Molecule.check_atom, [[0, 1, 2, 3]],
                          'a')
        self.assertRaises(SyntaxError, Molecule.check_atom, ['H', [1]],
                          ['a', [3]])

    def test_check_geom(self):
        """Test check_geom throws errors correctly"""
        self.assertTrue(Molecule.check_geom(self.water_geom))
        # Zero-length geometries are valid
        self.assertTrue(Molecule.check_geom([]))
        self.assertRaises(ValueError, Molecule.check_geom, [[[1, 2, 3]]])
        self.assertRaises(TypeError, Molecule.check_geom, ['H'],
                          [[[0, 1, 2, 3]]])
        self.assertRaises(SyntaxError, Molecule.check_geom,
                          [['H', [1, 'a', 3]]])

    def test_read_write_geometry(self):
        """Testing read and write"""
        test_file = 'geom.xyz.tmp'
        self.water.write(test_file, True)
        mol = Molecule.read_from(test_file)
        mol.name = 'H2O'
        self.assertEqual(mol.geom, self.water.geom)
        mol.write(test_file, style='latex')
        latex_geom = '''\
\\begin{verbatim}
H       0.000000      0.000000      0.000000
O       0.000000      0.000000      1.000000
H       0.000000      1.000000      1.000000
\\end{verbatim}'''
        with open(test_file) as f:
            out_tex = f.read()
        self.assertEqual(latex_geom, out_tex)

        os.remove(test_file)
Example #9
0
class TestMolecule(unittest.TestCase):
    """Tests the Molecule class"""

    def setUp(self):
        """Set up for every test"""
        self.water_geom = [['H', [0, 0, 0]],
                           ['O', [0, 0, 1]],
                           ['H', [0, 1, 1]]]
        self.water = Molecule(self.water_geom)

    def test_len(self):
        """Testing __len__"""
        self.assertEqual(len(self.water), 3)

    def test_getsetdeleqinsert(self):
        """Test getting, setting and deleting atoms"""
        self.assertEqual(self.water[0][0], 'H')
        assert_almost_equal(self.water[0][1], np.array([0, 0, 0]))

        del self.water[1]
        self.assertEqual(self.water[1][0], 'H')
        assert_almost_equal(self.water[1][1], np.array([0, 1, 1]))

        self.water[0] = ['H', [0, 0, 0]]
        self.water[1] = ['H', [0, -1, 1]]
        self.assertEqual(self.water[1][0], 'H')
        assert_almost_equal(self.water[1][1], np.array([0, -1, 1]))

        self.water.insert(1, 'O', [0, 0, 1])
        self.assertEqual(self.water[1][0], 'O')
        assert_almost_equal(self.water[1][1], np.array([0, 0, 1]))
        self.assertEqual(self.water[2][0], 'H')
        assert_almost_equal(self.water[2][1], np.array([0, -1, 1]))

        new_water = Molecule([['H', [0, 0, 0]], ['O', [0, 0, 1]], ['H', [0, -1, 1]]])
        self.assertEqual(self.water, new_water)

    def test_str(self):
        """Testing __str__"""
        water_string = """\
H       0.00000000    0.00000000    0.00000000
O       0.00000000    0.00000000    1.00000000
H       0.00000000    1.00000000    1.00000000"""
        self.assertEqual(str(self.water), water_string)

    def test_check_atom(self):
        """Test check_atom throws errors correctly"""
        atom, xyz = ['H', [0, 1, 2]]
        self.assertTrue(Molecule.check_atom(atom, xyz))
        self.assertRaises(SyntaxError, Molecule.check_atom, [[]], 'a')
        self.assertRaises(TypeError, Molecule.check_atom, [[1, 2, 3]])
        self.assertRaises(SyntaxError, Molecule.check_atom, [[0, 1, 2, 3]], 'a')
        self.assertRaises(SyntaxError, Molecule.check_atom, ['H', [1]], ['a', [3]])

    def test_check_geom(self):
        """Test check_geom throws errors correctly"""
        self.assertTrue(Molecule.check_geom(self.water_geom))
        # Zero-length geometries are valid
        self.assertTrue(Molecule.check_geom([]))
        self.assertRaises(ValueError, Molecule.check_geom, [[[1, 2, 3]]])
        self.assertRaises(TypeError, Molecule.check_geom, ['H'], [[[0, 1, 2, 3]]])

    def test_read_write_geometry(self):
        """Testing read and write"""
        geom_file = 'geom.xyz.tmp'
        self.water.write(geom_file, True)
        mol = Molecule.read_from(geom_file)
        mol.name = 'H2O'
        self.assertEqual(mol.geom, self.water.geom)
        mol.write(geom_file, style='latex')

        latex_geom = '''\
\\begin{verbatim}
H       0.000000      0.000000      0.000000
O       0.000000      0.000000      1.000000
H       0.000000      1.000000      1.000000
\\end{verbatim}'''
        with open(geom_file) as f:
            out_tex = f.read()
        self.assertEqual(latex_geom, out_tex)

        os.remove(geom_file)

    def test_com(self):
        """ Test the center of mass """
        water_com = np.array([0, 0.05595744, 0.94404256])
        assert_almost_equal(self.water.center_of_mass(), water_com)

        # Translations should shift the center of mass the same amount
        translation = [7, 8, 9]
        self.water.xyz += translation
        assert_almost_equal(self.water.center_of_mass(), water_com + translation)

    def test_moi_tensor(self):
        """ Test the moment of inertia tensor """
        water_moi_tensor = np.array([
                [ 1.9028595,  0        ,  0        ],
                [ 0        ,  0.9514297, -0.0563953],
                [ 0        , -0.0563953,  0.9514297]
        ])

        assert_almost_equal(self.water.moment_of_inertia_tensor(), water_moi_tensor)

        # Translations should not change moi tensor
        self.water.xyz += [7, 8, 9]
        assert_almost_equal(self.water.moment_of_inertia_tensor(), water_moi_tensor)

    def test_reorder(self):
        """ Test the reordering of atoms """
        w1 = self.water
        w2 = w1.reorder([1, 0, 2])
        assert w1[0][0] == w2[1][0]
        assert all(w1[0][1] == w2[1][1])
        assert w1[1][0] == w2[0][0]
        assert all(w1[1][1] == w2[0][1])
        assert w1[2][0] == w2[2][0]
        assert all(w1[2][1] == w2[2][1])
Example #10
0
 def setUp(self):
     """Set up for every test"""
     self.g = Gamessifier()
     self.formaldehyde_xyz = Molecule([['O', 0, 1.394, 0], ['C', 0, 0, 0], ['H', 0.994, -0.492, 0], ['H', -0.994, -0.492, 0]])
Example #11
0
class TestGamessifier(unittest.TestCase):
    """Tests the Molecule class"""

    def setUp(self):
        """Set up for every test"""
        self.g = Gamessifier()
        self.formaldehyde_xyz = Molecule([['O', 0, 1.394, 0], ['C', 0, 0, 0], ['H', 0.994, -0.492, 0], ['H', -0.994, -0.492, 0]])

    def test_read_mol(self):
        """Test reading a molecule from a file"""
        tmp_geom_file = 'geom.xyz.tmp'
        self.formaldehyde_xyz.write(tmp_geom_file)
        self.g.read_mol(tmp_geom_file)
        self.assertEqual(str(self.g.mol), str(self.formaldehyde_xyz))

        os.remove(tmp_geom_file)

    def test_read_basis_set(self):
        """Test reading a basis from a file"""
        tmp_basis_file = 'basis.gbs.tmp1'
        basis_set = BasisSet()
        basis_set['B'] = Basis('B', [BasisFunction('S', [0.2, 0.4], [0.3, 0.7])])
        open(tmp_basis_file, 'w').write(basis_set.print('gamess'))
        self.g.read_basis_set(tmp_basis_file)
        self.assertEqual(basis_set, self.g.basis_set)

        os.remove(tmp_basis_file)

    def test_read_ecp(self):
        """Testing reading an ecp file"""
        tmp_ecp_file = 'ecp.dat.tmp'
        ecp = """\
H-ECP NONE
C-ECP GEN   10  2
    3      ----- d-ul potential     -----
      -10.0000000        1    357.3914469
      -60.2990287        2     64.6477389
      -10.4217834        2     16.0960833
O-ECP NONE"""
        open(tmp_ecp_file, 'w').write(ecp)
        self.g.read_ecp(tmp_ecp_file)
        os.remove(tmp_ecp_file)

    def test_read_options(self):
        """Testing read options"""
        self.g.read_options({})
        self.assertEqual({}, self.g.options_dict)
        self.assertEqual('', self.g.write_options_str())
        options_dir = OrderedDict([
            ['CONTRL', OrderedDict([['SCFTYP', 'RHF']])],
            ['SCF', OrderedDict([['DIRSCF', '.TRUE.']])]
        ])
        options_str = ' $CONTRL\n    SCFTYP=RHF\n $END\n\n $SCF\n    DIRSCF=.TRUE.\n $END\n\n'
        self.g.read_options(options_dir)
        self.assertEqual(options_str, self.g.write_options_str())

        tmp_options_file = 'options.dat.tmp'
        open(tmp_options_file, 'w').write(options_str)
        self.g.read_options(tmp_options_file)

    def test_read_other_data(self):
        """Testing read_other_data"""
        tmp_dat_file = 'dat.tmp'
        vec = ' $VEC\n12 23 31\n33241 32523 11.0\n $END'
        hess = ' $HESS\n32 43 987\n453 443 11.0\n $END'
        data = 'Hey\n' + vec + ' \n random other text\n' + hess + '\n more text\n122\n'
        open(tmp_dat_file, 'w').write(data)
        self.g.read_data(tmp_dat_file)
        self.assertEqual(vec, self.g.vec)
        self.assertEqual(hess, self.g.hess)

        os.remove(tmp_dat_file)

    def test_update_options(self):
        """Test update_options"""
        self.g.vec = ' $VEC\n123132\n $END'
        self.g.options_dict = {'GUESS': {'GUESS': 'HUCKEL'}}
        self.g.update_options()
        self.assertEqual('MOREAD', self.g.options_dict['GUESS']['GUESS'])
        self.g.vec = ' '

    def test_write_input(self):
        """Test writing an input to a basis file"""
        tmp_basis_file = 'basis.gbs.tmp'
        basis_set = BasisSet()
        basis_set['H'] = Basis('H', [BasisFunction('S', [1], [1])])
        basis_set['O'] = Basis('O', [BasisFunction('S', [1], [1]), BasisFunction('S', [2], [1])])
        basis_set['C'] = Basis('C', [BasisFunction('S', [1], [1]), BasisFunction('SP', [1, 2], [0.4, 0.6], [0.1, 0.9])])
        open(tmp_basis_file, 'w').write(basis_set.print('gamess'))
        tmp_geom_file = 'geom.xyz.tmp'
        self.formaldehyde_xyz.write(tmp_geom_file)
        tmp_ecp_file = 'ecp.dat.tmp'
        ecp = """\
C-ECP GEN   10  2
    3      ----- d-ul potential     -----
      -10.0000000        1    357.3914469
      -60.2990287        2     64.6477389
      -10.4217834        2     16.0960833
O-ECP NONE"""
        open(tmp_ecp_file, 'w').write(ecp)
        tmp_options_file = 'options.dat.tmp'
        options_str = ' $CONTRL\n    SCFTYP=RHF\n $END\n\n $SCF\n    DIRSCF=.TRUE.\n $END\n\n'
        open(tmp_options_file, 'w').write(options_str)
        tmp_dat_file = 'dat.tmp'
        vec = ' $VEC\n12 23 31\n33241 32523 11.0\n $END'
        hess = ' $HESS\n32 43 987\n453 443 11.0\n $END'
        data = 'Hey\n' + vec + ' \n random other text\n' + hess + '\n more text\n122\n'
        open(tmp_dat_file, 'w').write(data)

        self.g.read(tmp_geom_file, tmp_basis_file, tmp_ecp_file, tmp_options_file, tmp_dat_file)

        tmp_input_file = 'input.inp.tmp'
        self.g.write_input(tmp_input_file)

        os.remove(tmp_geom_file)
        os.remove(tmp_basis_file)
        os.remove(tmp_ecp_file)
        os.remove(tmp_options_file)
        os.remove(tmp_input_file)
        os.remove(tmp_dat_file)
Example #12
0
 def setUp(self):
     """Set up for every test"""
     self.water_geom = [['H', [0, 0, 0]],
                        ['O', [0, 0, 1]],
                        ['H', [0, 1, 1]]]
     self.water = Molecule(self.water_geom)
Example #13
0
class TestMolecule(unittest.TestCase):
    """Tests the Molecule class"""

    def setUp(self):
        """Set up for every test"""
        self.water_geom = [['H', [0, 0, 0]],
                           ['O', [0, 0, 1]],
                           ['H', [0, 1, 1]]]
        self.water = Molecule(self.water_geom)

    def test_len(self):
        """Testing __len__"""
        self.assertEqual(len(self.water), 3)

    def test_getsetdeleqinsert(self):
        """Test getting, setting and deleting atoms"""
        self.assertEqual(self.water[0][0], 'H')
        assert_almost_equal(self.water[0][1], np.array([0, 0, 0]))

        del self.water[1]
        self.assertEqual(self.water[1][0], 'H')
        assert_almost_equal(self.water[1][1], np.array([0, 1, 1]))

        self.water[0] = ['H', [0, 0, 0]]
        self.water[1] = ['H', [0, -1, 1]]
        self.assertEqual(self.water[1][0], 'H')
        assert_almost_equal(self.water[1][1], np.array([0, -1, 1]))

        self.water.insert(1, 'O', [0, 0, 1])
        self.assertEqual(self.water[1][0], 'O')
        assert_almost_equal(self.water[1][1], np.array([0, 0, 1]))
        self.assertEqual(self.water[2][0], 'H')
        assert_almost_equal(self.water[2][1], np.array([0, -1, 1]))

        new_water = Molecule([['H', [0, 0, 0]], ['O', [0, 0, 1]], ['H', [0, -1, 1]]])
        self.assertEqual(self.water, new_water)

    def test_str(self):
        """Testing __str__"""
        water_string = """\
H       0.00000000    0.00000000    0.00000000
O       0.00000000    0.00000000    1.00000000
H       0.00000000    1.00000000    1.00000000"""
        self.assertEqual(str(self.water), water_string)

    def test_check_atom(self):
        """Test check_atom throws errors correctly"""
        atom, xyz = ['H', [0, 1, 2]]
        self.assertTrue(Molecule.check_atom(atom, xyz))
        self.assertRaises(SyntaxError, Molecule.check_atom, [[]], 'a')
        self.assertRaises(TypeError, Molecule.check_atom, [[1, 2, 3]])
        self.assertRaises(SyntaxError, Molecule.check_atom, [[0, 1, 2, 3]], 'a')
        self.assertRaises(SyntaxError, Molecule.check_atom, ['H', [1]], ['a', [3]])

    def test_check_geom(self):
        """Test check_geom throws errors correctly"""
        self.assertTrue(Molecule.check_geom(self.water_geom))
        # Zero-length geometries are valid
        self.assertTrue(Molecule.check_geom([]))
        self.assertRaises(ValueError, Molecule.check_geom, [[[1, 2, 3]]])
        self.assertRaises(TypeError, Molecule.check_geom, ['H'], [[[0, 1, 2, 3]]])

    def test_read_write_geometry(self):
        """Testing read and write"""
        geom_file = 'geom.xyz.tmp'
        self.water.write(geom_file, True)
        mol = Molecule.read_from(geom_file)
        mol.name = 'H2O'
        self.assertEqual(mol.geom, self.water.geom)
        mol.write(geom_file, style='latex')

        latex_geom = '''\
\\begin{verbatim}
H       0.000000      0.000000      0.000000
O       0.000000      0.000000      1.000000
H       0.000000      1.000000      1.000000
\\end{verbatim}'''
        with open(geom_file) as f:
            out_tex = f.read()
        self.assertEqual(latex_geom, out_tex)

        os.remove(geom_file)

    def test_com(self):
        """ Test the center of mass """
        water_com = np.array([0, 0.05595744, 0.94404256])
        assert_almost_equal(self.water.center_of_mass(), water_com)

        # Translations should shift the center of mass the same amount
        translation = [7, 8, 9]
        self.water.xyz += translation
        assert_almost_equal(self.water.center_of_mass(), water_com + translation)

    def test_moi_tensor(self):
        """ Test the moment of inertia tensor """
        water_moi_tensor = np.array([
                [ 1.9028595,  0        ,  0        ],
                [ 0        ,  0.9514297, -0.0563953],
                [ 0        , -0.0563953,  0.9514297]
        ])

        assert_almost_equal(self.water.moment_of_inertia_tensor(), water_moi_tensor)

        # Translations should not change moi tensor
        self.water.xyz += [7, 8, 9]
        assert_almost_equal(self.water.moment_of_inertia_tensor(), water_moi_tensor)
Example #14
0
class TestMolecule(unittest.TestCase):
    """Tests the Molecule class"""

    def setUp(self):
        """Set up for every test"""
        self.water = Molecule([['H', 0, 0, 10],
                                   ['O', 0, 0, 11],
                                   ['H', 0, -1, 11]])

    def test_len(self):
        """Testing __len__"""
        self.assertEqual(len(self.water), 3)

    def test_getsetdeleqinsert(self):
        """Test getting, setting and deleting atoms"""
        self.assertEqual(self.water[0], ['H', 0, 0, 10])
        del self.water[1]
        self.assertEqual(self.water[1], ['H', 0, -1, 11])
        self.water[0] = ['H', 0, 0, 0]
        self.water[1] = ['H', 0, 1, 1]
        self.assertEqual(self.water[1], ['H', 0, 1, 1])
        self.water.insert(1, ['O', 0, 0, 1])
        self.assertEqual(self.water[1], ['O', 0, 0, 1])
        self.assertEqual(self.water[2], ['H', 0, 1, 1])
        self.assertEqual(self.water, Molecule([['H', 0, 0, 0], ['O', 0, 0, 1], ['H', 0, 1, 1]]))

    def test_str(self):
        """Testing __str__"""
        water_string = """\
H       0.00000000    0.00000000   10.00000000
O       0.00000000    0.00000000   11.00000000
H       0.00000000   -1.00000000   11.00000000"""
        self.assertEqual(str(self.water), water_string)

    def test_check_atom(self):
        """Test check_atom throws errors correctly"""
        atom = ['H', 0, 1, 2]
        self.assertTrue(Molecule.check_atom(atom))
        self.assertRaises(SyntaxError, Molecule.check_atom, [[]])
        self.assertRaises(SyntaxError, Molecule.check_atom, [[1, 2, 3]])
        self.assertRaises(SyntaxError, Molecule.check_atom, [[0, 1, 2, 3]])
        self.assertRaises(SyntaxError, Molecule.check_atom, [['H', 1, 'a', 3]])

    def test_check_geom(self):
        """Test check_geom throws errors correctly"""
        water = [['H', 0, 0, 0], ['O', 0, 0, 1], ['H', 0, 1, 1]]
        self.assertTrue(Molecule.check_geom(water))
        # Zero-length geometries are valid
        self.assertTrue(Molecule.check_geom([]))
        self.assertRaises(SyntaxError, Molecule.check_geom, [[[1, 2, 3]]])
        self.assertRaises(SyntaxError, Molecule.check_geom, [[[0, 1, 2, 3]]])
        self.assertRaises(SyntaxError, Molecule.check_geom, [[['H', 1, 'a', 3]]])

    def test_read_write_geometry(self):
        """Testing read and write"""
        test_file = 'geom.xyz.tmp'
        self.water.write(test_file, True)
        mol = Molecule()
        mol.read(test_file)
        mol.name = 'H2O'
        self.assertEqual(mol.geom, self.water.geom)
        mol.write(test_file, style='latex')
        latex_geom = '''\
\\begin{verbatim}
H       0.000000      0.000000     10.000000
O       0.000000      0.000000     11.000000
H       0.000000     -1.000000     11.000000
\\end{verbatim}'''
        with open(test_file) as f:
            out_tex = f.read()
        self.assertEqual(latex_geom, out_tex)

        os.remove(test_file)
Example #15
0
 def setUp(self):
     """Set up for every test"""
     self.water = Molecule([['H', 0, 0, 10],
                                ['O', 0, 0, 11],
                                ['H', 0, -1, 11]])