def setUp(self): bfs = BasisFunction('S', [1, 2], [0.5, 0.5]) bfp = BasisFunction('P', [0.01, 0.2, 1], [0.3, 0.4, 0.3]) self.h = Basis('H', [bfs, bfp], 'simple') bfs = BasisFunction('S', [0.1, 0.4], [0.6, 0.4]) bfp = BasisFunction('P', [0.1, 0.4, 3], [0.2, 0.3, 0.5]) self.c = Basis('C', [bfs, bfp], 'simple') atoms = OrderedDict([('H', self.h), ('C', self.c)]) self.basis_set = BasisSet(atoms)
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)
def test_change_basis_set(self): """Test change_basis_set""" atoms = OrderedDict([('C', Basis('C', [BasisFunction('S', [1], [1])]))]) self.basis_set.change_basis_set(atoms) bad_bs = OrderedDict([('H', 1)]) self.assertRaises(SyntaxError, self.basis_set.change_basis_set, (bad_bs, ))
def test_getsetdeleq(self): """Test __getitem__, __setitem__, __delitem__, and __eq__""" bfs = BasisFunction('S', [0.2, 0.4], [0.3, 0.7]) b = Basis('B', [bfs]) self.basis_set['B'] = b self.assertEqual(self.c, self.basis_set['C']) self.assertEqual(b, self.basis_set['B']) del self.basis_set['B'] self.assertRaises(KeyError, self.basis_set.__getitem__, 'B') bfs = BasisFunction('S', [1, 2], [0.5, 0.5]) bfp = BasisFunction('P', [0.01, 0.2, 1], [0.3, 0.4, 0.3]) h = Basis('H', [bfs, bfp]) bfs = BasisFunction('S', [0.1, 0.4], [0.6, 0.4]) bfp = BasisFunction('P', [0.1, 0.4, 3], [[0.2, 0.3, 0.5], [0.1, 0.3, 0.6]]) c = Basis('C', [bfs, bfp]) atoms = OrderedDict([('H', h), ('C', c)]) atoms = OrderedDict([('H', h)]) basis_set2 = BasisSet(atoms) self.assertEqual(basis_set2, self.basis_set)
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_lengetsetdeleq(self): """Test __len__, __getitem__ and __setitem__, __delitem__, __eq__""" self.assertEqual(3, len(self.basis)) bf = BasisFunction('F', [4, 9], [0.1, 0.9]) self.basis[0] = bf self.assertEqual(bf, self.basis[0]) self.assertRaises(SyntaxError, self.basis.__setitem__, 0, 1) del self.basis[2] self.assertRaises(IndexError, self.basis.__getitem__, 2) basis = Basis('C', [self.bfsp]) basis2 = Basis('C', [self.bfsp]) self.assertEqual(basis, basis2)
def test_decontracted(self): """Tests decontraction of basis function""" bf1 = BasisFunction('S', [1], [1]) bf2 = BasisFunction('S', [2], [1]) decon1 = list(self.bfs.decontracted()) self.assertEqual(decon1[0], bf1) self.assertEqual(decon1[1], bf2) bfs1 = BasisFunction('S', [0.1], [1]) bfs2 = BasisFunction('S', [0.4], [1]) bfs3 = BasisFunction('S', [3], [1]) bfp1 = BasisFunction('P', [0.1], [1]) bfp2 = BasisFunction('P', [0.4], [1]) bfp3 = BasisFunction('P', [3], [1]) decon2 = list(self.bfsp.decontracted()) self.assertEqual(decon2[0], bfs1) self.assertEqual(decon2[1], bfs2) self.assertEqual(decon2[2], bfs3) self.assertEqual(decon2[3], bfp1) self.assertEqual(decon2[4], bfp2) self.assertEqual(decon2[5], bfp3)
def test_check_coeffs(self): """Test check_exps""" BasisFunction.check_coeffs(np.arange(5)) BasisFunction.check_coeffs(np.arange(9).reshape((3, 3))) self.assertRaises(ValueError, BasisFunction.check_exps, np.arange(8).reshape((2, 2, 2)))
def test_check_exps(self): """Test check_exps""" BasisFunction.check_exps([1, 2]) self.assertRaises(ValueError, BasisFunction.check_exps, [1, 2, -3])
def setUp(self): self.bfs = BasisFunction('S', [1, 2], [0.5, 0.5]) self.bfp = BasisFunction('P', [0.01, 0.2, 1], [0.3, 0.4, 0.3]) self.bfd = BasisFunction('D', [1, 2], [[0.3, 0.7], [0.4, 0.6]]) self.bfsp = BasisFunction('SP', [0.1, 0.4, 3], [[0.2, 0.3, 0.5], [0.1, 0.3, 0.6]])
class TestBasisFunction(unittest.TestCase): """Tests a BasisFunction""" def setUp(self): self.bfs = BasisFunction('S', [1, 2], [0.5, 0.5]) self.bfp = BasisFunction('P', [0.01, 0.2, 1], [0.3, 0.4, 0.3]) self.bfd = BasisFunction('D', [1, 2], [[0.3, 0.7], [0.4, 0.6]]) self.bfsp = BasisFunction('SP', [0.1, 0.4, 3], [[0.2, 0.3, 0.5], [0.1, 0.3, 0.6]]) def test_len(self): """Test __len__""" self.assertEqual(2, len(self.bfs)) self.assertEqual(3, len(self.bfp)) self.assertEqual(2, len(self.bfd)) self.assertEqual(3, len(self.bfsp)) def test_getseteq(self): """Test __getitem__ and __setitem__""" # Convert np.array to list to prevent error in case.py as the truth value of an array is ambiguous self.assertEqual([1.0, 0.5], list(self.bfs[0])) # Convert to a list of lists self.assertEqual([[0.01, 0.3], [1.0, 0.3]], list(map(list, self.bfp[::2]))) self.bfs[0] = [1, 2] self.assertEqual([1.0, 2], list(self.bfs[0])) self.assertRaises(ValueError, self.bfs.__setitem__, 1, [1, 2, 4]) self.bfd[0] = [1, 0.2, 0.3] self.assertEqual([1.0, 0.2, 0.3], list(self.bfd[0])) self.bfsp[0] = [1, 2, 4] self.assertEqual([1, 2, 4], list(self.bfsp[0])) self.assertRaises(ValueError, self.bfsp.__setitem__, 1, [1, 2]) self.assertEqual(self.bfs, self.bfs) def test_check_exps(self): """Test check_exps""" BasisFunction.check_exps([1, 2]) self.assertRaises(ValueError, BasisFunction.check_exps, [1, 2, -3]) def test_check_coeffs(self): """Test check_exps""" BasisFunction.check_coeffs(np.arange(5)) BasisFunction.check_coeffs(np.arange(9).reshape((3, 3))) self.assertRaises(ValueError, BasisFunction.check_exps, np.arange(8).reshape((2, 2, 2))) def test_exps_coeffs(self): """Test exps and coeffs""" self.assertEqual([1, 2], list(self.bfs.exps)) self.assertEqual([0.3, 0.4, 0.3], list(self.bfp.coeffs)) cs = np.array([[0.3, 0.4], [0.7, 0.6]]) aaa_equal(cs, self.bfd.coeffs) def test_reprprint(self): """Test print""" s_gamess = '''S 2 1 1.0000000 0.5000000 2 2.0000000 0.5000000 ''' p_gaussian94 = '''P 3 0.0100000 0.3000000 0.2000000 0.4000000 1.0000000 0.3000000 ''' sp_gaussian94 = '''SP 3 0.1000000 0.2000000 0.1000000 0.4000000 0.3000000 0.3000000 3.0000000 0.5000000 0.6000000 ''' self.assertEqual('<BasisFunction S 2>', repr(self.bfs)) self.assertEqual('<BasisFunction P 3>', repr(self.bfp)) self.assertEqual('<BasisFunction SP 3x2>', repr(self.bfsp)) self.assertEqual(s_gamess, self.bfs.print('gamess')) self.assertEqual(p_gaussian94, self.bfp.print()) self.assertEqual(sp_gaussian94, self.bfsp.print()) def test_decontracted(self): """Tests decontraction of basis function""" bf1 = BasisFunction('S', [1], [1]) bf2 = BasisFunction('S', [2], [1]) decon1 = list(self.bfs.decontracted()) self.assertEqual(decon1[0], bf1) self.assertEqual(decon1[1], bf2) bfs1 = BasisFunction('S', [0.1], [1]) bfs2 = BasisFunction('S', [0.4], [1]) bfs3 = BasisFunction('S', [3], [1]) bfp1 = BasisFunction('P', [0.1], [1]) bfp2 = BasisFunction('P', [0.4], [1]) bfp3 = BasisFunction('P', [3], [1]) decon2 = list(self.bfsp.decontracted()) self.assertEqual(decon2[0], bfs1) self.assertEqual(decon2[1], bfs2) self.assertEqual(decon2[2], bfs3) self.assertEqual(decon2[3], bfp1) self.assertEqual(decon2[4], bfp2) self.assertEqual(decon2[5], bfp3)
def setUp(self): self.bfs = BasisFunction('S', [1, 2], [0.5, 0.5]) self.bfp = BasisFunction('P', [0.01, 0.2, 1], [0.3, 0.4, 0.3]) self.bfsp = BasisFunction('SP', [0.1, 0.4, 3], [[0.2, 0.3, 0.5], [0.1, 0.3, 0.6]]) self.basis = Basis('C', [self.bfs, self.bfp, self.bfsp])
def setUp(self): self.bfs = BasisFunction('S', [1, 2], [0.5, 0.5]) self.bfp = BasisFunction('P', [0.01, 0.2, 1], [0.3, 0.4, 0.3]) self.bfd = BasisFunction('D', [1, 2], [0.3, 0.7], [0.4, 0.6]) self.bfsp = BasisFunction('SP', [0.1, 0.4, 3], [0.2, 0.3, 0.5], [0.1, 0.3, 0.6])
class TestBasisFunction(unittest.TestCase): """Tests a BasisFunction""" def setUp(self): self.bfs = BasisFunction('S', [1, 2], [0.5, 0.5]) self.bfp = BasisFunction('P', [0.01, 0.2, 1], [0.3, 0.4, 0.3]) self.bfd = BasisFunction('D', [1, 2], [0.3, 0.7], [0.4, 0.6]) self.bfsp = BasisFunction('SP', [0.1, 0.4, 3], [0.2, 0.3, 0.5], [0.1, 0.3, 0.6]) def test_len(self): """Test __len__""" self.assertEqual(2, len(self.bfs)) self.assertEqual(3, len(self.bfp)) self.assertEqual(2, len(self.bfd)) self.assertEqual(3, len(self.bfsp)) def test_getseteq(self): """Test __getitem__ and __setitem__""" # Convert np.array to list to prevent error in case.py as the truth value of an array is ambiguous self.assertEqual([1.0, 0.5], list(self.bfs[0])) # Convert to a list of lists self.assertEqual([[0.01, 0.3], [1.0, 0.3]], list(map(list, self.bfp[::2]))) self.bfs[0] = [1, 2] self.assertEqual([1.0, 2], list(self.bfs[0])) self.assertRaises(ValueError, self.bfs.__setitem__, 1, [1, 2, 4]) self.bfd[0] = [1, 0.2, 0.3] self.assertEqual([1.0, 0.2, 0.3], list(self.bfd[0])) self.bfsp[0] = [1, 2, 4] self.assertEqual([1, 2, 4], list(self.bfsp[0])) self.assertRaises(ValueError, self.bfsp.__setitem__, 1, [1, 2]) self.assertEqual(self.bfs, self.bfs) def test_check_exps(self): """Test check_exps""" BasisFunction.check_exps([1, 2]) self.assertRaises(ValueError, BasisFunction.check_exps, [1, 2, -3]) def test_exps_coeffs_coeffs2(self): """Test exps, coeffs, and coeffs2""" self.assertEqual([1, 2], list(self.bfs.exps)) self.assertEqual([0.3, 0.4, 0.3], list(self.bfp.coeffs)) self.assertEqual([0.4, 0.6], list(self.bfd.coeffs2)) def test_reprprint(self): """Test print""" s_gamess = '''S 2 1 1.0000000 0.5000000 2 2.0000000 0.5000000 ''' p_gaussian94 = '''P 3 0.0100000 0.3000000 0.2000000 0.4000000 1.0000000 0.3000000 ''' sp_gaussian94 = '''SP 3 0.1000000 0.2000000 0.1000000 0.4000000 0.3000000 0.3000000 3.0000000 0.5000000 0.6000000 ''' self.assertEqual('<BasisFunction S 2>', repr(self.bfs)) self.assertEqual('<BasisFunction P 3>', repr(self.bfp)) self.assertEqual('<BasisFunction SP 3x2>', repr(self.bfsp)) self.assertEqual(s_gamess, self.bfs.print('gamess')) self.assertEqual(p_gaussian94, self.bfp.print()) #print(self.bfsp.print()) self.assertEqual(sp_gaussian94, self.bfsp.print()) def test_decontracted(self): """Tests decontraction of basis function""" bf1 = BasisFunction('S', [1], [1]) bf2 = BasisFunction('S', [2], [1]) decon1 = list(self.bfs.decontracted()) self.assertEqual(decon1[0], bf1) self.assertEqual(decon1[1], bf2) bfs1 = BasisFunction('S', [0.1], [1]) bfs2 = BasisFunction('S', [0.4], [1]) bfs3 = BasisFunction('S', [3 ], [1]) bfp1 = BasisFunction('P', [0.1], [1]) bfp2 = BasisFunction('P', [0.4], [1]) bfp3 = BasisFunction('P', [3 ], [1]) decon2 = list(self.bfsp.decontracted()) self.assertEqual(decon2[0], bfs1) self.assertEqual(decon2[1], bfs2) self.assertEqual(decon2[2], bfs3) self.assertEqual(decon2[3], bfp1) self.assertEqual(decon2[4], bfp2) self.assertEqual(decon2[5], bfp3)