# 4-particle density matrix is explicitly computed. # Ref: S Guo, JCTC, ASAP # ################################################## # # Call mol.build function to redirect the output to another file. # mol.build(output='dmrg_nevpt2_slow.out') mc = mcscf.CASCI(m, 4, 4) # # Use DMRGCI as the active space solver. DMRGCI solver allows us to handle # ~ 50 active orbitals. # mc.fcisolver = dmrgscf.DMRGCI(mol, maxM=200) mc.fcisolver.nroots = 2 # # Passing mc_orb to CASCI kernel function so that the CASCI calculation is # carried out with the given orbitals. More examples refers to # pyscf/mcscf/62-make_init_guess # mc.kernel(mc_orb) # # The default DMRG-SC-NEVPT2 implementation is based on the 4-particle density matrix. # dmrg_nevpt_e1 = mrpt.NEVPT(mc, root=0).kernel() dmrg_nevpt_e2 = mrpt.NEVPT(mc, root=1).kernel() ##################################################
def test_mc2step_4o4e(self): mc = mcscf.CASSCF(m, 4, 4) mc.fcisolver = dmrgscf.DMRGCI(mol) emc = mc.mc2step()[0] self.assertAlmostEqual(emc, -108.913786407955, 7)
def kernel(self): mol = self.mol output = mol.output fpre = output[:output.rfind('_')] nobt, nocc = self.nobt, self.nocc # lowTri = [0]*nobt # for i in range(1, nobt): lowTri[i] = lowTri[i-1]+i # pub.lowTri=lowTri; gvb.rgvb.i_lowTri=lowTri pub.lT0 = pub.lowTri0(nobt) nc = self.nc nt, np = self.nt, self.np thd, ncyc, xE = self.thd, self.ncyc, self.xE # mo integral and repressive matrix print('get mo integral and repressive matrix') datfile = F'{fpre}_gvb_init.dat' fchkfile = F'{datfile}.fchk' from pyscf import scf, ao2mo hcore = scf.hf.get_hcore(mol) eri = mol.intor('int2e', aosym='s8') #onp=18; gvbfile=F'{fpre}_gvb{onp}.out' # reload CI #with open(gvbfile, 'r') as input: # linel=input.readlines() # linel=linel[pub.iis('GVB CI:\n', linel)+1:] # linel=linel[:pub.iis('\n', linel)] # from re import findall # for p in range(self.np): # p1=-p-1 # sl = findall(r"[-+]?\d+\.?\d*[eE]?[-+]?\d*", linel[p1].strip('\r\n')) # self.ci[p1] = [float(sl[1]), float(sl[3])] # print(F'{self.p[p1, 0] + 1:2d}({self.ci[p1, 0]:6f}) <-> {self.p[p1, 1] + 1:2d}({self.ci[p1, 1]:6f})') #from gvb.rgvb import load_mo # reload mo #load_mo(self, F'{fpre}_gvb{onp}.fchk') obts = self.mo_coeff self.h = reduce(numpy.dot, (obts.T, hcore, obts)) self.g = ao2mo.kernel(eri, obts) from bccc.rep import rep # from bcpt2 import gvbpt2 # ael_pt2, mcl_pt2 = gvbpt2.gvb_bcpt2(mol, datfile, fchkfile, fixed_core=nc) # print(F'mcl_pt2.shape = {mcl_pt2.shape}') nbs = 16 rl = [None] * nocc # print( 'mcl = ') for P in range(nocc): # ael[P] = numpy.zeros(nbs) # if nc<=P: # ael[P][:15] = ael_pt2[P-nc] # ael[P][:] = ael[P][[4, 5, 6, 7, 8, 9, 15, 0, 1, 2, 3, 10, 11, 12, 13, 14]] # # mc = numpy.zeros(shape=(nbs, nbs)) # # mc[:15, :15] = mcl_pt2[P]; mc[15][15] = 1. # # mc[:,:] = mc[[4, 5, 6, 7, 8, 9, 15, 0, 1, 2, 3, 10, 11, 12, 13, 14], :] # # mc[:,:] = mc[:, [4, 6, 7, 5, 8, 9, 15, 0, 1, 2, 3, 10, 11, 12, 13, 14]] # # mcl[P] = mc # # rl[P] = mrfeqd(mc) mc1 = numpy.identity(nbs) if nc <= P: # self.ci[P] = mcl_pt2[P-nc][4][4:6] # mc1[0,0:2] = [mcl_pt2[P-nc][4,4], mcl_pt2[P-nc][4,5]] mc1[0, 0:2] = self.ci[P] mc1[1, 0:2] = [-mc1[0, 1], mc1[0, 0]] a = 1 / math.sqrt(2.) mc1[2, 2:4] = [a, -a] mc1[3, 2:4] = [a, a] # print(F'P = {P}') # print(F' 0: {mc1[0][:4]}') # print(F' 1: {mc1[1][:4]}') # print(F' 2: {mc1[2][:4]}') # print(F' 3: {mc1[3][:4]}') # print(F' 7: {mc1[7][7:9]}') # print(F' 8: {mc1[8][7:9]}') # print(F' 9: {mc1[9][9:11]}') # print(F' 10: {mc1[10][9:11]}') # print(F' 11: {mc1[11][11:13]}') # print(F' 12: {mc1[12][11:13]}') # print(F' 13: {mc1[13][13:15]}') # print(F' 14: {mc1[14][13:15]}') rl[P] = rep(mc1) print('\n\n\n\n') # check GVB energy print('check GVB energy') from bcpt2.read_pair import read_eng_dat E_gvb, e_nuc = read_eng_dat(datfile) print(F'e_nuc = {e_nuc}') print(F'E(GVB) = {E_gvb:.10f} (.dat)') mf = self.ref mf.h_mo, mf.g_mo = self.h, self.g gvb.rgvb.lT0 = pub.lT0 mf.e_ele = mf.energy_elec() E_gvb = mf.e_nuc + mf.e_ele print(F'E(GVB) = {E_gvb:.10f} (Ref.)') from bccc.hm.hm_ import hm_ h0d = hm_(rl, self.h, self.g, self.p, nc)[()] # h0d = hm_(rl, self)[()] E_gvb = self.e_nuc + h0d[()] print(F'E(GVB) = {E_gvb:.10f}') print('\n\n\n\n') # BCPT2 print('check GVB-BCPT2 energy') from bcpt2 import gvbpt2 ael_pt2, E_bcpt2 = gvbpt2.gvb_bcpt2(mol, datfile, fchkfile, fixed_core=nc, fixed_virt=self.nvir - self.np) ael = [None] * nocc for P in range(nocc): ael[P] = numpy.zeros(nbs) if nc <= P: ael[P][:15] = ael_pt2[P - nc] ael[P][:] = ael[P][[ 4, 5, 6, 7, 8, 9, 15, 0, 1, 2, 3, 10, 11, 12, 13, 14 ]] print(F'E(GVB-BCPT2) = {E_bcpt2:.10f} (Ref.)') from bccc.pub import bcpt2 corr_bcpt2 = bcpt2(ael, h0d) print(F'corr(GVB-BCPT2) = {corr_bcpt2}') E_bcpt2 = E_gvb + corr_bcpt2 print(F'E(GVB-BCPT2) = {E_bcpt2}') print('\n\n\n\n') # check FCI/MCSCF/DMRG print('check FCI/MCSCF/DMRG', flush=True) # from bccc.hm1.hm import hm # print('From hm1') #from bccc.hm.hm import hm #t1 = datetime.datetime.now() #mh = hm(rl, self.h, self.g, self.p, self.nt, nc) #t2 = datetime.datetime.now() #print(F'calculate H matrix {t2-t1}') #nu=len(mh); id={u: i for i, u in enumerate(mh.keys())} #mH = numpy.zeros(shape=(nu, nu)) #for bra,i in id.items(): # # print(F'bra = {bra}') # for ket,j in id.items(): # mH[i,j] = mh[bra].get(ket, 0.0) # # if abs(mH[i,j])>1.0e-6: print(F' ket = {ket}: {mH[i,j]}') #eig,v = numpy.linalg.eigh(mH) ## print(F'eig = {eig}') #E_fci = self.e_nuc+eig[0] #print(F'E(FCI) = {E_fci}') hf = scf.RHF(self.mol) hf.max_cycle = 1 hf.kernel() # gvb.rgvb.load_mo(hf, fchkfile) # hf.mo_coeff[:,nc:nocc+np] = self.mo_coeff[:, list(range(nc, 2*nocc-nc, 2))+list(range(2*nocc-1-nc, nc, -2))] hf.mo_coeff = self.mo_coeff # FCI # from pyscf import fci # mc = fci.FCI(hf) # mc.kernel() # CASCI from pyscf import mcscf mc = mcscf.CASCI(hf, 2 * np, 2 * np, ncore=nc) #mc.fcisolver.nroots = nu mc.fcisolver.memory = 30 # GB #e_fci,eig_fci,*_ = mc.kernel() ## print(F'eig_fci = {eig_fci}') #E_fci = e_fci[0] #print(F'E(FCI) = {E_fci} (Ref.)') # CASSCF # mc = mcscf.CASSCF(hf, 2*np, 2*np, ncore=nc) mc.fix_spin_(ss=0.0) mc.fcisolver.level_shift = 0.2 mc.fcisolver.pspace_size = 1200 if np > 6: # DMRG from pyscf import dmrgscf mc.fcisolver = dmrgscf.DMRGCI(mol, maxM=1000) mc.fcisolver.block_extra_keyword = ['memory, 30, g'] mc.kernel() #err = [abs(a-b) for a,b in zip(eig, eig_fci)] #emax = max(err) #print(F'max error = {emax}') #emean = sum(err)/nu #print(F'mean error = {emean}') print('\n\n\n\n') # initilize t print('initialize t') # from bccc.init import init # t0 = init(nocc) from bccc.hm.hm import hm t1 = datetime.datetime.now() mh = hm(rl, self.h, self.g, self.p, self.nt, nc) # mh = hm(rl, self) t2 = datetime.datetime.now() print(F'calculate H matrix {t2-t1}') from bccc.pub import bclcc corr_bclcc, t0 = bclcc(nocc, mh, nt, nc) print(F'corr(GVB-BCLCC{nt}) = {corr_bclcc}') E_bclcc = E_gvb + corr_bclcc print(F'E(GVB-BCLCC{nt}) = {E_bclcc}') import gc del mh gc.collect() #t0 = ddict(float) #finit='C4H10_4_6-31g_bccc2b.out' #print(F'finit = {finit}') #with open(finit, 'r') as input: # linel=input.readlines() # linel=linel[pub.iis('t(final) =\n', linel)+1:] # linel=linel[:pub.iis('\n', linel)] # from re import findall # for line in linel: # sl = findall(r"[-+]?\d+\.?\d*[eE]?[-+]?\d*", line.strip('\r\n')) # t0[tuple((int(sl[i]), int(sl[i+1])) for i in range(0,len(sl)-1,2))] = float(sl[-1]) #print('t(initial) =') #for u,tu in t0.items(): # if abs(tu)>1.0e-6: print(F'u,tu = {u},{tu}') print('\n\n\n\n') # GVB-BCCC print('calculate GVB-BCCC energy') # from bccc.ta1.ta import ta # print('From ta1') from bccc.iter import iter # corr_bccc,t = ta(t0, mh, self.p, nt, nc) corr_bccc, t = iter(t0, rl, self.h, self.g, self.p, nt, nc, thd, ncyc, xE) print(F'corr(GVB-BCCC{nt}) = {corr_bccc}') E_bccc = E_gvb + corr_bccc print(F'E(GVB-BCCC{nt}) = {E_bccc}') print('t(final) =') for u, tu in t.items(): if abs(tu) > 1.0e-6: print(F'u,tu = {u},{tu}') print('\n\n\n\n')
['H', (1.435682, 2.309097, 0.0)], ['H', (1.435682, -0.163698, 0.0)], ['H', (-0.705820, -1.400095, 0.0)], ['H', (-2.847323, -0.163698, 0.0)], ['H', (-2.847323, 2.309097, 0.0)]], basis=basisName, output=outputName, max_memory=16000) # Hartree-Fock calculation mf = scf.RHF(mol) mf.kernel() mf.analyze() # Get active space for CASSCF calculation n_orb_active_space, nelec_act_space, new_all_orbs = avas.kernel(mf, 'C 2pz') # CASSCF mc = dmrgscf.DMRGSCF(mf, n_orb_active_space, nelec_act_space) mc.state_specific_(state=1) mc.fcisolver.maxM = 500 mc.kernel(new_all_orbs) mc_orbs = mc.mo_coeff # CASCI mc = mcscf.CASCI(mf, n_orb_active_space, nelec_act_space) mc.fcisolver = dmrgscf.DMRGCI(mol) mc.fcisolver.maxM = 1200 mc.fcisolver.nroots = 2 mc.kernel(mc_orbs) # NEVPT2 correction mps_nevpt_e2 = mrpt.NEVPT(mc, root=1).compress_approx(maxM=1200).kernel() mc.analyze() os.system('date > endTime')
def CAS(self, CAS, CAS_MO, Orbital_optimization = False, solver = 'FCI'): ''' CASSCF with FCI or DMRG solver from BLOCK or CheMPS2 ''' Norb = self.Norb Nimp = self.Nimp FOCK = self.FOCK.copy() if (self.chempot != 0.0): for orb in range(Nimp): FOCK[orb, orb] -= self.chempot mol = gto.Mole() mol.build(verbose = 0) mol.atom.append(('C', (0, 0, 0))) mol.nelectron = self.Nel mol.incore_anyway = True mf = scf.RHF( mol ) mf.get_hcore = lambda *args: FOCK mf.get_ovlp = lambda *args: np.eye(Norb) mf._eri = ao2mo.restore(8, self.TEI, Norb) mf.scf(self.DMguess) DMloc = np.dot(np.dot(mf.mo_coeff, np.diag(mf.mo_occ)), mf.mo_coeff.T) if ( mf.converged == False ): mf = rhf_newtonraphson.solve( mf, dm_guess=DMloc) DMloc = np.dot(np.dot(mf.mo_coeff, np.diag(mf.mo_occ)), mf.mo_coeff.T) if CAS == None: CAS_nelec = self.Nel CAS_norb = Norb CAS = 'full' else: CAS_nelec = CAS[0] CAS_norb = CAS[1] print(" Active space: ", CAS) # Replace FCI solver by DMRG solver in CheMPS2 or BLOCK if Orbital_optimization == True: mc = mcscf.CASSCF(mf, CAS_norb, CAS_nelec) else: mc = mcscf.CASCI(mf, CAS_norb, CAS_nelec) if solver == 'CheMPS2': mc.fcisolver = dmrgscf.CheMPS2(mol) elif solver == 'Block': mc.fcisolver = dmrgscf.DMRGCI(mol) if CAS_MO is not None: print(" Active space MOs: ", CAS_MO) mo = mc.sort_mo(CAS_MO) ECAS = mc.kernel(mo)[0] else: ECAS = mc.kernel()[0] ###### Get RDM1 + RDM2 ##### CAS_norb = mc.ncas core_norb = mc.ncore CAS_nelec = mc.nelecas core_MO = mc.mo_coeff[:,:core_norb] CAS_MO = mc.mo_coeff[:,core_norb:core_norb+CAS_norb] casdm1 = mc.fcisolver.make_rdm12(mc.ci, CAS_norb, CAS_nelec)[0] #in CAS space # Transform the casdm1 (in CAS space) to casdm1ortho (orthonormal space). casdm1ortho = np.einsum('ap,pq->aq', CAS_MO, casdm1) casdm1ortho = np.einsum('bq,aq->ab', CAS_MO, casdm1ortho) coredm1 = np.dot(core_MO, core_MO.T) * 2 #in localized space RDM1 = coredm1 + casdm1ortho casdm2 = mc.fcisolver.make_rdm12(mc.ci, CAS_norb, CAS_nelec)[1] #in CAS space # Transform the casdm2 (in CAS space) to casdm2ortho (orthonormal space). casdm2ortho = np.einsum('ap,pqrs->aqrs', CAS_MO, casdm2) casdm2ortho = np.einsum('bq,aqrs->abrs', CAS_MO, casdm2ortho) casdm2ortho = np.einsum('cr,abrs->abcs', CAS_MO, casdm2ortho) casdm2ortho = np.einsum('ds,abcs->abcd', CAS_MO, casdm2ortho) coredm2 = np.zeros([Norb, Norb, Norb, Norb]) #in AO coredm2 += np.einsum('pq,rs-> pqrs',coredm1,coredm1) coredm2 -= 0.5*np.einsum('ps,rq-> pqrs',coredm1,coredm1) effdm2 = np.zeros([Norb, Norb, Norb, Norb]) #in AO effdm2 += 2*np.einsum('pq,rs-> pqrs',casdm1ortho,coredm1) effdm2 -= np.einsum('ps,rq-> pqrs',casdm1ortho,coredm1) RDM2 = coredm2 + casdm2ortho + effdm2 ImpurityEnergy = 0.50 * np.einsum('ij,ij->', RDM1[:Nimp,:], FOCK[:Nimp,:] + self.OEI[:Nimp,:]) \ + 0.125 * np.einsum('ijkl,ijkl->', RDM2[:Nimp,:,:,:], self.TEI[:Nimp,:,:,:]) \ + 0.125 * np.einsum('ijkl,ijkl->', RDM2[:,:Nimp,:,:], self.TEI[:,:Nimp,:,:]) \ + 0.125 * np.einsum('ijkl,ijkl->', RDM2[:,:,:Nimp,:], self.TEI[:,:,:Nimp,:]) \ + 0.125 * np.einsum('ijkl,ijkl->', RDM2[:,:,:,:Nimp], self.TEI[:,:,:,:Nimp]) return (ImpurityEnergy, ECAS, RDM1)
def kernel(self): mol = self.mol output = mol.output fpre = output[:output.rfind('_')] nobt, nocc = self.nobt, self.nocc pub.lT0 = pub.lowTri0(nobt) nc = self.nc nt, np = self.nt, self.np thd, ncyc, xE = self.thd, self.ncyc, self.xE # mo integral and repressive matrix print('get mo integral and repressive matrix') datfile = F'{fpre}_gvb_init.dat' fchkfile = F'{datfile}.fchk' from pyscf import scf, ao2mo hcore = scf.hf.get_hcore(mol) eri = mol.intor('int2e', aosym='s8') obts = self.mo_coeff[:, :nobt] self.h = reduce(numpy.dot, (obts.T, hcore, obts)) self.g = ao2mo.kernel(eri, obts) from bccc.rep import rep r = [None] * nocc for P in range(nocc): mc = numpy.identity(16) if nc <= P: mc[0, 0:2] = self.ci[P] mc[1, 0:2] = [-mc[0, 1], mc[0, 0]] a = 1 / math.sqrt(2.) mc[2, 2:4] = [a, -a] mc[3, 2:4] = [a, a] r[P] = rep(mc) self.r = r print('\n\n\n\n') # check GVB energy print('check GVB energy') from bcpt2.read_pair import read_eng_dat E_gvb, e_nuc = read_eng_dat(datfile) print(F'e_nuc = {e_nuc}') print(F'E(GVB) = {E_gvb:.10f} (.dat)') mf = self.ref mf.h_mo, mf.g_mo = self.h, self.g gvb.rgvb.lT0 = pub.lT0 mf.e_ele = mf.energy_elec() E_gvb = mf.e_nuc + mf.e_ele print(F'E(GVB) = {E_gvb:.10f} (Ref.)') from bccc.hm.hm_ import hm_ h0d = hm_(r, self.h, self.g, self.p, nc)[()] # h0d = hm_(self)[()] E_gvb = self.e_nuc + h0d[()] print(F'E(GVB) = {E_gvb:.10f}') print('\n\n\n\n') # BCPT2 print('check GVB-BCPT2 energy') from bcpt2 import gvbpt2 ael_pt2, E_bcpt2 = gvbpt2.gvb_bcpt2(mol, datfile, fchkfile, fixed_core=nc, fixed_virt=self.nvir - self.np) ael = [None] * nocc for P in range(nocc): ael[P] = numpy.zeros(16) if nc <= P: ael[P][:15] = ael_pt2[P - nc] ael[P][:] = ael[P][[ 4, 5, 6, 7, 8, 9, 15, 0, 1, 2, 3, 10, 11, 12, 13, 14 ]] print(F'E(GVB-BCPT2) = {E_bcpt2:.10f} (Ref.)') from bccc.pub import bcpt2 corr_bcpt2 = bcpt2(ael, h0d) print(F'corr(GVB-BCPT2) = {corr_bcpt2}') E_bcpt2 = E_gvb + corr_bcpt2 print(F'E(GVB-BCPT2) = {E_bcpt2}') print('\n\n\n\n') # check FCI/MCSCF/DMRG print('check FCI/MCSCF/DMRG', flush=True) hf = scf.RHF(self.mol) hf.max_cycle = 1 hf.kernel() hf.mo_coeff = self.mo_coeff from pyscf import mcscf mc = mcscf.CASCI(hf, 2 * np, 2 * np, ncore=nc) #mc.fcisolver.nroots = nu mc.fcisolver.memory = 30 # GB mc.fix_spin_(ss=0.0) #mc.fcisolver.level_shift = 0.2 #mc.fcisolver.pspace_size = 1200 if np > 6: # DMRG from pyscf import dmrgscf mc.fcisolver = dmrgscf.DMRGCI(mol, maxM=1000) mc.fcisolver.block_extra_keyword = ['memory, 30, g'] #mc.kernel() print('\n\n\n\n') # initilize t print('initialize t') t = self.t if t is None: print('t0 from GVB-LBCCC') from bccc.hm.hm import hm t1 = datetime.datetime.now() mh = hm(r, self.h, self.g, self.p, nt, nc) # mh = hm(self) t2 = datetime.datetime.now() print(F'calculate H matrix {t2-t1}') from bccc.pub import bclcc corr_bclcc, t = bclcc(nocc, mh, nt, nc) print(F'corr(GVB-BCLCC{nt}) = {corr_bclcc}') E_bclcc = E_gvb + corr_bclcc print(F'E(GVB-BCLCC{nt}) = {E_bclcc}') import gc del mh gc.collect() else: finit = t # initial t0 file print(F't0 from {finit}') t = ddict(float) with open(finit, 'r') as input: linel = input.readlines() linel = linel[pub.iis('t(final) =\n', linel) + 1:] linel = linel[:pub.iis('\n', linel)] from re import findall for line in linel: sl = findall(r"[-+]?\d+\.?\d*[eE]?[-+]?\d*", line.strip('\r\n')) t[tuple((int(sl[i]), int(sl[i + 1])) for i in range(0, len(sl) - 1, 2))] = float(sl[-1]) print('\n\n\n\n') # GVB-BCCC print('calculate GVB-BCCC energy') # from bccc.ta1.ta import ta # print('From ta1') from bccc.iter import iter # corr_bccc,t = ta(t, mh, self.p, nt, nc) corr_bccc, t = iter(t, r, self.h, self.g, self.p, nt, nc, thd, ncyc, xE) print(F'corr(GVB-BCCC{nt}) = {corr_bccc}') E_bccc = E_gvb + corr_bccc print(F'E(GVB-BCCC{nt}) = {E_bccc}') print('t(final) =') for u, tu in t.items(): if abs(tu) > 1.0e-6: print(F'u,tu = {u},{tu}') #print('t symmetry =') #ul = list(t.keys()); ul_,nu_ = [],len(t) #for i,u in enumerate(ul): # if u not in ul_: # tu=t[u] # print(F'{u}: {tu}') # ul_.append(u) # tu_=abs(tu) # for v in ul[i+1:]: # if v not in ul_: # tv=t[v] # if abs(tu_-abs(tv))<1.0e-7: # print(F'{v}: {tv}') # ul_.append(v) # print() print('\n\n\n\n')