def test_hfw(self): with lib.light_speed(10) as c: x2c_1 = sfx2c1e.SpinFreeX2C(mol1) x2c_2 = sfx2c1e.SpinFreeX2C(mol2) x2cobj = sfx2c1e.SpinFreeX2C(mol) fh_ref = (x2c_1.get_hcore() - x2c_2.get_hcore()) / 0.0002 * lib.param.BOHR fh = x2cobj.hcore_deriv_generator(deriv=1) self.assertAlmostEqual(abs(fh(0)[2] - fh_ref).max(), 0, 7) x2c_1.xuncontract = 0 x2c_2.xuncontract = 0 x2cobj.xuncontract = 0 fh_ref = (x2c_1.get_hcore() - x2c_2.get_hcore()) / 0.0002 * lib.param.BOHR fh = x2cobj.hcore_deriv_generator(deriv=1) self.assertAlmostEqual(abs(fh(0)[2] - fh_ref).max(), 0, 7) x2c_1.xuncontract = 1 x2c_2.xuncontract = 1 x2cobj.xuncontract = 1 x2c_1.approx = 'ATOM1E' x2c_2.approx = 'ATOM1E' x2cobj.approx = 'ATOM1E' fh_ref = (x2c_1.get_hcore() - x2c_2.get_hcore()) / 0.0002 * lib.param.BOHR fh = x2cobj.hcore_deriv_generator(deriv=1) self.assertAlmostEqual(abs(fh(0)[2] - fh_ref).max(), 0, 7)
def test_sqrt_second_order(self): with lib.light_speed(10) as c: nao = mol.nao_nr() aoslices = mol.aoslice_by_atom() p0, p1 = aoslices[0][2:] s1p1 = mol1.intor('int1e_ipovlp', comp=3) s1p2 = mol2.intor('int1e_ipovlp', comp=3) s1_1 = numpy.zeros((3,nao,nao)) s1_1[:,p0:p1] = -s1p1[:,p0:p1] s1_1 = s1_1 + s1_1.transpose(0,2,1) s1_2 = numpy.zeros((3,nao,nao)) s1_2[:,p0:p1] = -s1p2[:,p0:p1] s1_2 = s1_2 + s1_2.transpose(0,2,1) s2sqrt_ref = (_sqrt1(mol1.intor('int1e_ovlp'), s1_1[2]) - _sqrt1(mol2.intor('int1e_ovlp'), s1_2[2])) / 0.0002 * lib.param.BOHR s2invsqrt_ref = (_invsqrt1(mol1.intor('int1e_ovlp'), s1_1[2]) - _invsqrt1(mol2.intor('int1e_ovlp'), s1_2[2])) / 0.0002 * lib.param.BOHR s1p = mol.intor('int1e_ipovlp', comp=3) s1i = numpy.zeros((3,nao,nao)) s1i[:,p0:p1] = -s1p[:,p0:p1] s1i = s1i + s1i.transpose(0,2,1) s2aap = mol.intor('int1e_ipipovlp', comp=9).reshape(3,3,nao,nao) s2abp = mol.intor('int1e_ipovlpip', comp=9).reshape(3,3,nao,nao) s2 = numpy.zeros((3,3,nao,nao)) s2[:,:,p0:p1] = s2aap[:,:,p0:p1] s2[:,:,p0:p1,p0:p1] += s2abp[:,:,p0:p1,p0:p1] s2 = s2 + s2.transpose(0,1,3,2) s2sqrt = _sqrt2(mol.intor('int1e_ovlp'), s1i[2], s1i[2], s2[2,2]) s2invsqrt = _invsqrt2(mol.intor('int1e_ovlp'), s1i[2], s1i[2], s2[2,2]) self.assertAlmostEqual(abs(s2sqrt-s2sqrt_ref).max(), 0, 7) self.assertAlmostEqual(abs(s2invsqrt-s2invsqrt_ref).max(), 0, 7) p0, p1 = aoslices[1][2:] s1_1 = numpy.zeros((3,nao,nao)) s1_1[:,p0:p1] = -s1p1[:,p0:p1] s1_1 = s1_1 + s1_1.transpose(0,2,1) s1_2 = numpy.zeros((3,nao,nao)) s1_2[:,p0:p1] = -s1p2[:,p0:p1] s1_2 = s1_2 + s1_2.transpose(0,2,1) s2sqrt_ref = (_sqrt1(mol1.intor('int1e_ovlp'), s1_1[2]) - _sqrt1(mol2.intor('int1e_ovlp'), s1_2[2])) / 0.0002 * lib.param.BOHR s2invsqrt_ref = (_invsqrt1(mol1.intor('int1e_ovlp'), s1_1[2]) - _invsqrt1(mol2.intor('int1e_ovlp'), s1_2[2])) / 0.0002 * lib.param.BOHR q0, q1 = aoslices[0][2:] s1i = numpy.zeros((3,nao,nao)) s1i[:,p0:p1] = -s1p[:,p0:p1] s1i = s1i + s1i.transpose(0,2,1) s1j = numpy.zeros((3,nao,nao)) s1j[:,q0:q1] = -s1p[:,q0:q1] s1j = s1j + s1j.transpose(0,2,1) s2 = numpy.zeros((3,3,nao,nao)) s2[:,:,p0:p1,q0:q1] = s2abp[:,:,p0:p1,q0:q1] s2 = s2 + s2.transpose(0,1,3,2) s2sqrt = _sqrt2(mol.intor('int1e_ovlp'), s1i[2], s1j[2], s2[2,2]) s2invsqrt = _invsqrt2(mol.intor('int1e_ovlp'), s1i[2], s1j[2], s2[2,2]) self.assertAlmostEqual(abs(s2sqrt-s2sqrt_ref).max(), 0, 7) self.assertAlmostEqual(abs(s2invsqrt-s2invsqrt_ref).max(), 0, 7)
def test_khf(self): with lib.light_speed(2) as c: mf = scf.KRHF(cell).sfx2c1e() mf.with_df = df.AFTDF(cell) mf.kpts = cell.make_kpts([3,1,1]) dm = mf.get_init_guess() h1 = mf.get_hcore() self.assertAlmostEqual(numpy.einsum('ij,ji', dm[0], h1[0]),6/8.98384358326*-0.47578184212352159+0j, 8) self.assertAlmostEqual(numpy.einsum('ij,ji', dm[1], h1[1]),6/8.98384358326*-0.09637799091491725+0j, 8) self.assertAlmostEqual(numpy.einsum('ij,ji', dm[2], h1[2]),6/8.98384358326*-0.09637799091491725+0j, 8)
def test_with_x2c_scanner(self): with lib.light_speed(20.): mcs = mcscf.CASCI(mf, 4, 4).as_scanner().x2c() gscan = mcs.nuc_grad_method().as_scanner() g1 = gscan(mol)[1] self.assertAlmostEqual(lib.finger(g1), -0.070708933966346407, 7) e1 = mcs('N 0 0 0; N 0 0 1.201; H 1 1 0; H 1 1 1.2') e2 = mcs('N 0 0 0; N 0 0 1.199; H 1 1 0; H 1 1 1.2') self.assertAlmostEqual(g1[1,2], (e1-e2)/0.002*lib.param.BOHR, 5)
def test_khf(self): with lib.light_speed(2) as c: mf = scf.KRHF(cell).sfx2c1e() mf.with_df = df.AFTDF(cell) mf.kpts = cell.make_kpts([3,1,1]) dm = mf.get_init_guess() h1 = mf.get_hcore() self.assertAlmostEqual(numpy.einsum('ij,ji', dm[0], h1[0]),-0.47578184212352159+0j, 8) self.assertAlmostEqual(numpy.einsum('ij,ji', dm[1], h1[1]),-0.09637799091491725+0j, 8) self.assertAlmostEqual(numpy.einsum('ij,ji', dm[2], h1[2]),-0.09637799091491725+0j, 8)
def test_hf(self): with lib.light_speed(2) as c: mf = scf.RHF(cell).sfx2c1e() mf.with_df = df.AFTDF(cell) dm = mf.get_init_guess() h1 = mf.get_hcore() self.assertAlmostEqual(numpy.einsum('ij,ji', dm, h1), 2/3.7865300454631*-0.47578184212352159+0j, 8) kpts = cell.make_kpts([3,1,1]) h1 = mf.get_hcore(kpt=kpts[1]) self.assertAlmostEqual(numpy.einsum('ij,ji', dm, h1), 2/3.7865300454631*-0.09637799091491725+0j, 8)
def test_with_x2c_scanner(self): with lib.light_speed(20.): mc = mcscf.CASSCF(mf, 4, 4).x2c().run() gscan = mc.nuc_grad_method().as_scanner() g1 = gscan(mol)[1] self.assertAlmostEqual(lib.finger(g1), -0.070281684620797591, 7) mcs = mcscf.CASSCF(mf, 4, 4).as_scanner().x2c() e1 = mcs('N 0 0 0; N 0 0 1.201; H 1 1 0; H 1 1 1.2') e2 = mcs('N 0 0 0; N 0 0 1.199; H 1 1 0; H 1 1 1.2') self.assertAlmostEqual(g1[1,2], (e1-e2)/0.002*lib.param.BOHR, 5)
def test_with_x2c_scanner(self): with lib.light_speed(20.): mcs = mcscf.CASCI(mf, 4, 4).as_scanner().x2c() gscan = mcs.nuc_grad_method().as_scanner() g1 = gscan(mol)[1] self.assertAlmostEqual(lib.finger(g1), -0.070708933966346407, 7) e1 = mcs('N 0 0 0; N 0 0 1.201; H 1 1 0; H 1 1 1.2') e2 = mcs('N 0 0 0; N 0 0 1.199; H 1 1 0; H 1 1 1.2') self.assertAlmostEqual(g1[1, 2], (e1 - e2) / 0.002 * lib.param.BOHR, 5)
def test_with_x2c_scanner(self): with lib.light_speed(20.): mc = mcscf.CASSCF(mf.x2c(), 4, 4).run() gscan = mc.nuc_grad_method().as_scanner() g1 = gscan(mol)[1] self.assertAlmostEqual(lib.finger(g1), -0.07027493570511917, 7) mcs = mcscf.CASSCF(mf, 4, 4).as_scanner().x2c() e1 = mcs('N 0 0 0; N 0 0 1.201; H 1 1 0; H 1 1 1.2') e2 = mcs('N 0 0 0; N 0 0 1.199; H 1 1 0; H 1 1 1.2') self.assertAlmostEqual(g1[1,2], (e1-e2)/0.002*lib.param.BOHR, 5)
def test_hf(self): with lib.light_speed(2) as c: mf = scf.RHF(cell).sfx2c1e() mf.with_df = df.AFTDF(cell) dm = mf.get_init_guess() h1 = mf.get_hcore() self.assertAlmostEqual(numpy.einsum('ij,ji', dm, h1), -0.2458227312351979 + 0j, 8) kpts = cell.make_kpts([3, 1, 1]) h1 = mf.get_hcore(kpt=kpts[1]) self.assertAlmostEqual(numpy.einsum('ij,ji', dm, h1), -0.04113247191600125 + 0j, 8)
def test_r2(self): with lib.light_speed(10) as c: r1_1 = get_r1(mol1, 0, 2) r1_2 = get_r1(mol2, 0, 2) r2_ref = (r1_1 - r1_2) / 0.0002 * lib.param.BOHR r2 = get_r2(mol, 0, 0, 2, 2) self.assertAlmostEqual(abs(r2 - r2_ref).max(), 0, 7) r1_1 = get_r1(mol1, 1, 2) r1_2 = get_r1(mol2, 1, 2) r2_ref = (r1_1 - r1_2) / 0.0002 * lib.param.BOHR r2 = get_r2(mol, 1, 0, 2, 2) self.assertAlmostEqual(abs(r2 - r2_ref).max(), 0, 7)
def test_x2(self): with lib.light_speed(10) as c: x1_1 = get_x1(mol1, 0) x1_2 = get_x1(mol2, 0) x2_ref = (x1_1[2] - x1_2[2]) / 0.0002 * lib.param.BOHR x2 = get_x2(mol, 0, 0) self.assertAlmostEqual(abs(x2[2, 2] - x2_ref).max(), 0, 7) x1_1 = get_x1(mol1, 1) x1_2 = get_x1(mol2, 1) x2_ref = (x1_1[2] - x1_2[2]) / 0.0002 * lib.param.BOHR x2 = get_x2(mol, 1, 0) self.assertAlmostEqual(abs(x2[2, 2] - x2_ref).max(), 0, 7)
def test_khf(self): with lib.light_speed(2) as c: mf = scf.KRHF(cell).sfx2c1e() mf.with_df = df.AFTDF(cell) mf.kpts = cell.make_kpts([3, 1, 1]) dm = mf.get_init_guess() h1 = mf.get_hcore() self.assertAlmostEqual(numpy.einsum('ij,ji', dm[0], h1[0]), -0.31082970748083477 + 0j, 8) self.assertAlmostEqual(numpy.einsum('ij,ji', dm[1], h1[1]), -0.05200981271862468 + 0j, 8) self.assertAlmostEqual(numpy.einsum('ij,ji', dm[2], h1[2]), -0.05200981271862468 + 0j, 8)
def test_with_x2c_scanner(self): with lib.light_speed(20.): pt = mp.mp2.MP2(mf.x2c()) pt.frozen = [0, 1, 10, 11, 12] gscan = pt.nuc_grad_method().as_scanner().as_scanner() e, g1 = gscan(mol) ps = pt.as_scanner() e1 = ps([[8, (0., 0., 0.)], [1, (0., -0.757, 0.5871)], [1, (0., 0.757, 0.587)]]) e2 = ps([[8, (0., 0., 0.)], [1, (0., -0.757, 0.5869)], [1, (0., 0.757, 0.587)]]) self.assertAlmostEqual(g1[1, 2], (e1 - e2) / 0.0002 * lib.param.BOHR, 5)
def test_hf(self): with lib.light_speed(2) as c: mf = scf.RHF(cell).sfx2c1e() mf.with_df = df.AFTDF(cell) dm = mf.get_init_guess() h1 = mf.get_hcore() self.assertAlmostEqual( numpy.einsum('ij,ji', dm, h1), 2 / 3.7865300454631 * -0.47578184212352159 + 0j, 8) kpts = cell.make_kpts([3, 1, 1]) h1 = mf.get_hcore(kpt=kpts[1]) self.assertAlmostEqual( numpy.einsum('ij,ji', dm, h1), 2 / 3.7865300454631 * -0.09637799091491725 + 0j, 8)
def test_with_x2c_scanner(self): with lib.light_speed(20.): mycc = cc.ccsd.CCSD(mf.x2c()) mycc.frozen = [0, 1, 10, 11, 12] gscan = mycc.nuc_grad_method().as_scanner().as_scanner() e, g1 = gscan(mol) cs = mycc.as_scanner() e1 = cs([[8, (0., 0., 0.)], [1, (0., -0.757, 0.5871)], [1, (0., 0.757, 0.587)]]) e2 = cs([[8, (0., 0., 0.)], [1, (0., -0.757, 0.5869)], [1, (0., 0.757, 0.587)]]) self.assertAlmostEqual(g1[1, 2], (e1 - e2) / 0.0002 * lib.param.BOHR, 5)
def test_R1(self): with lib.light_speed(10) as c: R_1 = get_R(mol1) R_2 = get_R(mol2) R1_ref = (R_1 - R_2) / 0.0002 * lib.param.BOHR R1t = get_r1(mol, 0, 2) self.assertAlmostEqual(abs(R1t - R1_ref).max(), 0, 7) x0 = get_x0(mol) h0, s0 = get_h0_s0(mol) e0, c0 = scipy.linalg.eigh(h0, s0) get_h1_etc = sfx2c1e_grad._gen_first_order_quantities( mol, e0, c0, x0) R1 = get_h1_etc(0)[6][2] self.assertAlmostEqual(abs(R1 - R1t).max(), 0, 9)
def test_x1(self): with lib.light_speed(10) as c: x_1 = get_x0(mol1) x_2 = get_x0(mol2) x1_ref = (x_1 - x_2) / 0.0002 * lib.param.BOHR x1t = get_x1(mol, 0) self.assertAlmostEqual(abs(x1t[2] - x1_ref).max(), 0, 7) x0 = get_x0(mol) h0, s0 = get_h0_s0(mol) e0, c0 = scipy.linalg.eigh(h0, s0) get_h1_etc = sfx2c1e_grad._gen_first_order_quantities( mol, e0, c0, x0) x1 = get_h1_etc(0)[4] self.assertAlmostEqual(abs(x1 - x1t).max(), 0, 9)
def test_dhf_grad_with_ssss_high_cost(self): with lib.light_speed(30): mf = scf.DHF(h2o).run(conv_tol=1e-12) g = mf.nuc_grad_method().kernel() self.assertAlmostEqual(lib.finger(g), 0.0074947016737157545, 7) ms = mf.as_scanner() pmol = h2o.copy() e1 = ms(pmol.set_geom_([["O" , (0. , 0. ,-0.001)], [1 , (0. , -0.757 , 0.587)], [1 , (0. , 0.757 , 0.587)]], unit='Ang')) e2 = ms(pmol.set_geom_([["O" , (0. , 0. , 0.001)], [1 , (0. , -0.757 , 0.587)], [1 , (0. , 0.757 , 0.587)]], unit='Ang')) self.assertAlmostEqual(g[0,2], (e2-e1)/0.002*lib.param.BOHR, 5)
def test_h2(self): with lib.light_speed(10) as c: h1_1, s1_1 = get_h1_s1(mol1, 0) h1_2, s1_2 = get_h1_s1(mol2, 0) h2_ref = (h1_1[2] - h1_2[2]) / 0.0002 * lib.param.BOHR s2_ref = (s1_1[2] - s1_2[2]) / 0.0002 * lib.param.BOHR h2, s2 = get_h2_s2(mol, 0, 0) self.assertAlmostEqual(abs(h2[2, 2] - h2_ref).max(), 0, 7) self.assertAlmostEqual(abs(s2[2, 2] - s2_ref).max(), 0, 7) h1_1, s1_1 = get_h1_s1(mol1, 1) h1_2, s1_2 = get_h1_s1(mol2, 1) h2_ref = (h1_1[2] - h1_2[2]) / 0.0002 * lib.param.BOHR s2_ref = (s1_1[2] - s1_2[2]) / 0.0002 * lib.param.BOHR h2, s2 = get_h2_s2(mol, 1, 0) self.assertAlmostEqual(abs(h2[2, 2] - h2_ref).max(), 0, 7) self.assertAlmostEqual(abs(s2[2, 2] - s2_ref).max(), 0, 7)
def test_dhf_grad_with_ssss_high_cost(self): with lib.light_speed(30): mf = scf.DHF(h2o).run() g = mf.nuc_grad_method().kernel() self.assertAlmostEqual(lib.finger(g), 0.0074940105486053926, 7) ms = mf.as_scanner() pmol = h2o.copy() e1 = ms( pmol.set_geom_([["O", (0., 0., -0.001)], [1, (0., -0.757, 0.587)], [1, (0., 0.757, 0.587)]], unit='Ang')) e2 = ms( pmol.set_geom_([["O", (0., 0., 0.001)], [1, (0., -0.757, 0.587)], [1, (0., 0.757, 0.587)]], unit='Ang')) self.assertAlmostEqual(g[0, 2], (e2 - e1) / 0.002 * lib.param.BOHR, 6)
vj[:,n2c:,n2c:] += vx[0] vk[:,n2c:,:n2c] += vx[1] vx = _vhf.rdirect_bindm('int2e_ip1spsp2_spinor', 's2kl', ('lk->s1ij', 'jk->s1il'), (dmss, dmls), 3, mol._atm, mol._bas, mol._env) * c1**2 vj[:,:n2c,:n2c] += vx[0] vk[:,:n2c,n2c:] += vx[1] return vj, vk if __name__ == "__main__": from pyscf import gto from pyscf import scf from pyscf import lib with lib.light_speed(30): h2o = gto.Mole() h2o.verbose = 0 h2o.output = None#"out_h2o" h2o.atom = [ ["O" , (0. , 0. , 0.)], [1 , (0. , -0.757 , 0.587)], [1 , (0. , 0.757 , 0.587)] ] h2o.basis = {"H": '6-31g', "O": '6-31g',} h2o.build() method = scf.dhf.UHF(h2o).run() g = method.Gradients().kernel() print(g) ms = method.as_scanner()
mol = gto.Mole() mol.basis = 'dzp-dk' mol.atom = ''' O 0.000000 0.000000 0.118351 H 0.000000 0.761187 -0.469725 H 0.000000 -0.761187 -0.469725 ''' mol.charge = 0 mol.spin = 0 mol.symmetry = 1 mol.verbose = 4 mol.nucmod = 1 mol.build() #with lib.light_speed(lib.param.LIGHT_SPEED): with lib.light_speed(5.0): mf = dft.DUKS(mol) mf.grids.level = 3 #mf.grids.prune = None mf.with_ssss = True mf.with_gaunt = False mf.with_breit = False mf.chkfile = name + '.chk' mf.kernel() cc = lib.param.LIGHT_SPEED print mf.mo_occ dm = mf.make_rdm1() coords = mf.grids.coords weights = mf.grids.weights