def test_jk_charge_constraint(self): numpy.random.seed(12) nao = mol.nao_nr() dm = numpy.random.random((nao, nao)) dm = dm + dm.T vj0, vk0 = scf.RHF(mol).get_jk(mol, dm) ej0 = numpy.einsum('ij,ij->', vj0, dm) ek0 = numpy.einsum('ij,ij->', vk0, dm) jkdf = mdf.MDF(mol) jkdf.charge_constraint = False jkdf.auxbasis = 'weigend' jkdf.gs = (10, ) * 3 mf = mdf_jk.density_fit(scf.RHF(mol), with_df=jkdf) vj1, vk1 = mf.get_jk(mol, dm) self.assertAlmostEqual( numpy.einsum('ij,ij->', vj1, dm) - ej0, 0.0003166388141693232, 9) self.assertAlmostEqual( numpy.einsum('ij,ij->', vk1, dm) - ek0, 0.0005799101835037845, 9) jkdf = mdf.MDF(mol) jkdf.charge_constraint = False jkdf.metric = 'T' jkdf.auxbasis = 'weigend' jkdf.gs = (10, ) * 3 mf = mdf_jk.density_fit(scf.RHF(mol), with_df=jkdf) vj1, vk1 = mf.get_jk(mol, dm) self.assertAlmostEqual( numpy.einsum('ij,ij->', vj1, dm) - ej0, 0.12360821384800857, 9) self.assertAlmostEqual( numpy.einsum('ij,ij->', vk1, dm) - ek0, 0.4701984522179572, 9) jkdf = mdf.MDF(mol) jkdf.charge_constraint = False jkdf.metric = 'J' jkdf.auxbasis = 'weigend' jkdf.gs = (10, ) * 3 mf = mdf_jk.density_fit(scf.RHF(mol), with_df=jkdf) vj1, vk1 = mf.get_jk(mol, dm) self.assertAlmostEqual( numpy.einsum('ij,ij->', vj1, dm) - ej0, -6.3466650786381251e-5, 9) self.assertAlmostEqual( numpy.einsum('ij,ij->', vk1, dm) - ek0, -4.5425207332527862e-5, 9)
def test_jk_metric(self): numpy.random.seed(12) nao = mol.nao_nr() dm = numpy.random.random((nao, nao)) dm = dm + dm.T vj0, vk0 = scf.RHF(mol).get_jk(mol, dm) ej0 = numpy.einsum('ij,ij->', vj0, dm) ek0 = numpy.einsum('ij,ij->', vk0, dm) jkdf = mdf.MDF(mol) jkdf.metric = 'S' jkdf.auxbasis = 'weigend' jkdf.gs = (10, ) * 3 mf = mdf_jk.density_fit(scf.RHF(mol), with_df=jkdf) vj1, vk1 = mf.get_jk(mol, dm) self.assertAlmostEqual( numpy.einsum('ij,ij->', vj1, dm) - ej0, 9.5034429456575253e-5, 9) self.assertAlmostEqual( numpy.einsum('ij,ij->', vk1, dm) - ek0, -4.759826947520196e-5, 9) jkdf = mdf.MDF(mol) jkdf.metric = 'T' jkdf.auxbasis = 'weigend' jkdf.gs = (10, ) * 3 mf = mdf_jk.density_fit(scf.RHF(mol), with_df=jkdf) vj1, vk1 = mf.get_jk(mol, dm) self.assertAlmostEqual( numpy.einsum('ij,ij->', vj1, dm) - ej0, 0.005304754655668375, 9) self.assertAlmostEqual( numpy.einsum('ij,ij->', vk1, dm) - ek0, 0.076335586801803856, 9) jkdf = mdf.MDF(mol) jkdf.metric = 'J' jkdf.auxbasis = 'weigend' jkdf.gs = (10, ) * 3 mf = mdf_jk.density_fit(scf.RHF(mol), with_df=jkdf) vj1, vk1 = mf.get_jk(mol, dm) self.assertAlmostEqual( numpy.einsum('ij,ij->', vj1, dm) - ej0, -6.56455759013852e-5, 9) self.assertAlmostEqual( numpy.einsum('ij,ij->', vk1, dm) - ek0, -4.965601597461955e-5, 9)
def density_fit(mf, auxbasis=None, gs=(10,10,10), with_df=None): '''Generte density-fitting SCF object Args: auxbasis : str or basis dict Same format to the input attribute mol.basis. The default basis 'weigend+etb' means weigend-coulomb-fit basis for light elements and even-tempered basis for heavy elements. gs : tuple number of grids in each (+)direction with_df : MDF object ''' mf_class = mf.__class__ if with_df is None: with_df = mdf.MDF(mf.mol) with_df.max_memory = mf.max_memory with_df.stdout = mf.stdout with_df.verbose = mf.verbose with_df.auxbasis = auxbasis with_df.gs = gs class XDFHF(mf_class): def __init__(self): self.__dict__.update(mf.__dict__) self.with_df = with_df self._keys = self._keys.union(['with_df']) def get_jk(self, mol=None, dm=None, hermi=1): if self.with_df: return get_jk(self.with_df, mol, dm, hermi, self.opt, True, True) else: return mf_class.get_jk(self, mol, dm, hermi) def get_j(self, mol=None, dm=None, hermi=1): if self.with_df: return get_jk(self.with_df, mol, dm, hermi, self.opt, True, False)[0] else: return mf_class.get_j(self, mol, dm, hermi) def get_k(self, mol=None, dm=None, hermi=1): if self.with_df: return get_jk(self.with_df, mol, dm, hermi, self.opt, False, True)[1] else: return mf_class.get_k(self, mol, dm, hermi) return XDFHF()
#mf0.max_cycle = 0 mf0.kernel() dm1 = mf0.make_rdm1() #dm1 = numpy.random.random(dm.shape) #dm1 = dm1 + dm1.T #auxbasis = {'C': gto.expand_etbs([[0, 10, .5, 2.], [1, 7, .3, 2.]])} auxbasis = 'weigend' #auxbasis = 'sto3g' #auxbasis = 'tzp' #auxbasis = {'Fe': gto.mole.uncontract(gto.basis.load('tzp', 'Fe'))} #auxbasis = mdf._default_basis(mol, addons.aug_etb_for_dfbasis(mol, beta=2.2, start_at=0)) #auxbasis = addons.aug_etb_for_dfbasis(mol, beta=2.2, start_at=0) auxbasis = None #auxbasis = addons.aug_etb_for_dfbasis(mol, beta=2.2, start_at=0) #auxbasis = mdf._default_basis(mol) #mf = scf.density_fit(scf.RHF(mol), auxbasis) #mf = with_poisson_(scf.RHF(mol), auxbasis, gs=(50,302)) mf = mdf.MDF(mol).set(auxbasis=auxbasis, gs=(5,)*3).update_mf(scf.RHF(mol)) vj0, vk0 = mf0.get_jk(mol, dm) vj1, vk1 = mf.get_jk(mol, dm) print('J', numpy.einsum('ij,ij->', vj1, dm1), 'ref=2495.9843578661084', 'exact', numpy.einsum('ij,ij->', vj0, dm1)) print('K', numpy.einsum('ij,ij->', vk1, dm1), 'ref=426.19988812673307', 'exact', numpy.einsum('ij,ij->', vk0, dm1)) print('J-K', numpy.einsum('ij,ij->', vj1-vk1*.5, dm1), 'ref=2282.8844138027462', 'exact', numpy.einsum('ij,ij->', vj0-vk0*.5, dm1)) exit() print(mf.scf())