def test_self_consistent(): #pmDMET mol, mf, mydft, impClusters = test_makemole1() symmetry = None #or [0]*5, takes longer time solverlist = 'CASCI' #['RHF', 'CASCI', 'CASCI', 'CASCI', 'CASCI'] runDMET = dmet.DMET(mf, impClusters, symmetry, orthogonalize_method = 'overlap', schmidt_decomposition_method = 'OED', OEH_type = 'FOCK', SC_CFtype = 'F', solver = solverlist) #runDMET.CAS = [[4,4]] runDMET.SC_canonical = True time1 = time.time() runDMET.self_consistent() time2 = time.time() time_pmDMET = time2 - time1 #QC-DMET myInts = localintegrals.localintegrals( mf, range( mol.nao_nr() ), 'meta_lowdin' ) myInts.TI_OK = False method = 'CASSCF' SCmethod = 'BFGS' #Don't do it self-consistently TI = False theDMET = qc_dmet.dmet( myInts, impClusters, TI, method, SCmethod ) theDMET.impCAS = (4,4) time1 = time.time() theDMET.doselfconsistent() time2 = time.time() time_QCDMET = time2 - time1 return runDMET.Energy_total, time_pmDMET, time_QCDMET
def test_1rdm_response(): mol, mf, impClusters = test_makemole1() #symmetry = [0, 1, 2, 1, 0] #runDMET = dmet.DMET(mf, impClusters, symmetry, orthogonalize_method = 'overlap', schmidt_decomposition_method = 'OED', OEH_type = 'FOCK', SC_CFtype = 'FB', solver = 'RHF') #runDMET.one_shot() umat = np.zeros((mol.nao_nr(), mol.nao_nr())) #QC-DMET myInts = localintegrals.localintegrals(mf, range(mol.nao_nr()), 'meta_lowdin') myInts.TI_OK = True method = 'ED' SCmethod = 'LSTSQ' #Don't do it self-consistently TI = True theDMET = qc_dmet.dmet(myInts, impClusters, TI, method, SCmethod) RDMderivs_rot = theDMET.helper.construct1RDM_response(False, umat, None) Norb = theDMET.helper.locints.Norbs Nterms = theDMET.helper.Nterms numPairs = theDMET.helper.numPairs inH1start = theDMET.helper.H1start inH1row = theDMET.helper.H1row inH1col = theDMET.helper.H1col inH0 = theDMET.helper.locints.loc_rhf_fock() #+ umat RDMderivs_libdmet = libdmet.rhf_response(Norb, Nterms, numPairs, inH1start, inH1row, inH1col, inH0) inH0 = np.array(inH0.reshape((10 * 10)), dtype=ctypes.c_double) assert np.abs((RDMderivs_rot - RDMderivs_libdmet)).sum() < 1e-8 return RDMderivs_rot, RDMderivs_libdmet
def test_costfunction(): #pmDMET mol, mf, impClusters = test_makemole1() symmetry = None #or [0]*5, takes longer time runDMET = dmet.DMET(mf, impClusters, symmetry, orthogonalize_method='meta_lowdin', schmidt_decomposition_method='OED', OEH_type='FOCK', SC_CFtype='FB', solver='RHF') runDMET.one_shot() #QC-DMET myInts = localintegrals.localintegrals(mf, range(mol.nao_nr()), 'meta_lowdin') myInts.TI_OK = False method = 'RHF' SCmethod = 'NONE' #Don't do it self-consistently TI = False theDMET = qc_dmet.dmet(myInts, impClusters, TI, method, SCmethod) theDMET.doselfconsistent() uvec_size = runDMET.uvec.size uvec = np.random.rand(uvec_size) umat = runDMET.uvec2umat(uvec) CF_pmDMET = runDMET.costfunction(uvec) CF_QCDMET = theDMET.costfunction(uvec) CF_deriv_pmDMET = runDMET.costfunction_gradient(uvec) CF_deriv_QCDMET = theDMET.costfunction_derivative(uvec) assert np.isclose((CF_QCDMET - CF_pmDMET).sum(), 0) assert np.isclose((CF_deriv_QCDMET - CF_deriv_pmDMET).sum(), 0)
def test_1RDM_response(): #pmDMET mol, mf, impClusters = test_makemole1() symmetry = [0] * 5 #or 'Translation' runDMET = dmet.DMET(mf, impClusters, symmetry, orthogonalize_method='meta_lowdin', schmidt_decomposition_method='OED', OEH_type='FOCK', SC_CFtype='FB', solver='RHF') #QC-DMET myInts = localintegrals.localintegrals(mf, range(mol.nao_nr()), 'meta_lowdin') myInts.TI_OK = True method = 'ED' SCmethod = 'LSTSQ' #Don't do it self-consistently TI = True theDMET = qc_dmet.dmet(myInts, impClusters, TI, method, SCmethod) uvec_size = runDMET.uvec.size uvec = np.zeros(uvec_size) umat = runDMET.uvec2umat(uvec) RDMderivs_QCDMET = theDMET.helper.construct1RDM_response(False, umat, None) RDMderivs_pDMET = runDMET.construct_1RDM_response(uvec) diff = np.abs((RDMderivs_QCDMET - RDMderivs_pDMET)).sum() assert np.isclose(RDMderivs_QCDMET.size, RDMderivs_pDMET.size) assert np.isclose(diff, 0)
def test_make_H1(): #pmDMET mol, mf, impClusters = test_makemole1() symmetry = [0] * 5 #or 'Translation' runDMET = dmet.DMET(mf, impClusters, symmetry, orthogonalize_method='overlap', schmidt_decomposition_method='OED', OEH_type='FOCK', SC_CFtype='FB', solver='RHF') H1start, H1row, H1col = runDMET.make_H1()[1:] #QC-DMET myInts = localintegrals.localintegrals(mf, range(mol.nao_nr()), 'meta_lowdin') myInts.TI_OK = True method = 'ED' SCmethod = 'LSTSQ' #Don't do it self-consistently TI = True theDMET = qc_dmet.dmet(myInts, impClusters, TI, method, SCmethod) H1start = theDMET.helper.H1start - H1start H1row = theDMET.helper.H1row - H1row H1col = theDMET.helper.H1col - H1col assert H1start.sum() == 0 assert H1row.sum() == 0 assert H1col.sum() == 0
# Order of AO: 3s 2p 1d rotation[ offset+3:offset+6, offset+3:offset+6 ] = ringhelper.p_functions( theta ) rotation[ offset+6:offset+9, offset+6:offset+9 ] = ringhelper.p_functions( theta ) rotation[ offset+9:offset+14, offset+9:offset+14 ] = ringhelper.d_functions( theta ) assert( np.linalg.norm( np.dot( rotation, rotation.T ) - np.eye( rotation.shape[0] ) ) < 1e-6 ) myInts = localintegrals.localintegrals( mf, range( mol.nao_nr() ), localization_type, rotation ) if (( localization_type == 'meta_lowdin' ) or ( localization_type == 'iao' )): myInts.TI_OK = True myInts.molden( 'Be-loc.molden' ) atoms_per_imp = 1 # Impurity size = 1/2/4 Be atoms assert ( nat % atoms_per_imp == 0 ) orbs_per_imp = myInts.Norbs * atoms_per_imp / nat impurityClusters = [] for cluster in range( nat / atoms_per_imp ): impurities = np.zeros( [ myInts.Norbs ], dtype=int ) for orb in range( orbs_per_imp ): impurities[ orbs_per_imp*cluster + orb ] = 1 impurityClusters.append( impurities ) if (( localization_type == 'meta_lowdin' ) or ( localization_type == 'iao' )): isTranslationInvariant = True else: isTranslationInvariant = False # Boys TI is not OK method = 'CC' SCmethod = 'NONE' # NONE or LSTSQ for no self-consistency or least-squares fitting of the u-matrix, respectively theDMET = dmet.dmet( myInts, impurityClusters, isTranslationInvariant, method, SCmethod ) theDMET.doselfconsistent() #theDMET.dump_bath_orbs( 'Be-bathorbs.molden' )
def DMET_wrap(atoms, basis, charge, spin, fragments, fragment_spins, shells, nfreeze, method, thresh, parallel): mol = pyscf.gto.Mole() mol.verbose = 4 mol.output = 'Mole' mol.atom = atoms mol.charge = charge mol.spin = spin mol.basis = basis mol.symmetry = False mol.ecp = None mol.build() from pyscf import scf verbose = False if (parallel): from mpi4py import MPI comm = MPI.COMM_WORLD rank = MPI.COMM_WORLD.Get_rank() size = MPI.COMM_WORLD.Get_size() if (rank == 0): verbose = True else: verbose = True Cf_core, Cf_vale, Cf_virt = orbital_partitioning(mol, fragments, shells, verbose) Cf = RHF_calculation(mol, verbose) iAO_loc, Cf_core, Cf_x = build_iAO_basis(mol, Cf, Cf_core, Cf_vale, nfreeze) Cf_virt = virtual_orbitals(mol, Cf_core, Cf_vale, Cf_virt, iAO_loc) #print("cf core",Cf_core) if (Cf_core is not None): nb = Cf_core.shape[0] FrozenPot = numpy.zeros((nb, nb)) else: nfreeze = 0 nb = mol.nao_nr() FrozenPot = numpy.zeros((nb, nb)) e_core = 0.0 if (nfreeze > 0): import numpy as np from pyscf import scf Hc = mol.intor_symmetric('cint1e_kin_sph') \ + mol.intor_symmetric('cint1e_nuc_sph') dm_core = np.dot(Cf_core, Cf_core.T) * 2 jk_core = scf.hf.get_veff(mol, dm_core) e_core = np.trace(np.dot(Hc, dm_core)) \ + 0.5*np.trace(np.dot(jk_core, dm_core)) FrozenPot = jk_core Cf_core = None mol.nelectron -= 2 * nfreeze print(nfreeze) idx_core = None if (Cf_core is not None): idx_core = atom_to_orb_mapping(mol, Cf_core) idx_vale = atom_to_orb_mapping(mol, Cf_vale) idx_virt = atom_to_orb_mapping(mol, Cf_virt) ximp_at = atom_to_frg_mapping(fragments) print("idx core", idx_core) print("idx_vale", idx_vale) print("idx_virt", idx_virt) print("ximp-at", ximp_at) print("aio_loc", iAO_loc.shape) if (parallel): from mpi4py import MPI comm = MPI.COMM_WORLD rank = MPI.COMM_WORLD.Get_rank() size = MPI.COMM_WORLD.Get_size() assert (len(fragments) == size) Cf_x = comm.bcast(Cf_x, root=0) iAO_loc = comm.bcast(iAO_loc, root=0) idx_vale = comm.bcast(idx_vale, root=0) Cf_core = comm.bcast(Cf_core, root=0) idx_core = comm.bcast(idx_core, root=0) Cf_virt = comm.bcast(Cf_virt, root=0) idx_virt = comm.bcast(idx_virt, root=0) ximp_at = comm.bcast(ximp_at, root=0) import time start = time.time() print("Starting the clock for solver") mf_tot = scf.RHF( mol).density_fit() # this was moved from dfmp2_testing solver mf_tot.with_df._cderi_to_save = 'saved_cderi.h5' # rank-3 decomposition mf_tot.kernel() #print("shape of cderi", mf_tot.with_df._cderi.shape) #TODO: make this ^ conditional, use saved eri dmet_ = dmet.dmet(mol, Cf_x, ximp_at, \ iAO_loc, idx_vale, method=method, thresh=thresh, \ A_core = Cf_core, at_core = idx_core, \ A_virt = Cf_virt, at_virt = idx_virt, \ imp_atx = ximp_at, parallel = parallel, e_core=e_core,\ FrozenPot=FrozenPot, mf_tot = mf_tot) dmet_.eval() done = time.time() elapsed = done - start print("Total Time in Solver", elapsed)
if (True): myInts = localintegrals.localintegrals(mf2, range(mol2.nao_nr()), 'iao') myInts.molden('emft-loc.molden') unit_sizes = None if ((thebasis1 == 'cc-pvdz') and (thebasis2 == 'aug-cc-pvdz')): if (thestructure == 'reactants'): # 1-chlorodecane unit_sizes = np.array([27, 24, 24, 24, 24, 24, 24, 24, 24, 24, 29]) # Cl, 9xCH2, CH3 (272 orbs total) if (thestructure == 'products'): # 1-decanol unit_sizes = np.array([28, 24, 24, 24, 24, 24, 24, 24, 24, 24, 29]) # OH, 9xCH2, CH3 (273 orbs total) assert (np.sum(unit_sizes) == mol2.nao_nr()) for carbons_in_cluster in range(0, 5): # 0, 1, 2, 3, 4 orbs_in_imp = np.sum(unit_sizes[0:carbons_in_cluster + 1]) impurityClusters = [] impurities = np.zeros([mol.nao_nr()], dtype=int) impurities[0:orbs_in_imp] = 1 impurityClusters.append(impurities) theDMET = dmet.dmet(myInts, impurityClusters, isTranslationInvariant=False, method='CC', SCmethod='NONE') theDMET.CC_E_TYPE = 'CASCI' the_energy = theDMET.doselfconsistent() print "###### DMET(", carbons_in_cluster, "C , CCSD ) /", thebasis1, "/", thebasis2, " =", the_energy + ECCSD2
for carbons_in_cluster in cluster_sizes: impurityClusters = [] if ( single_impurity == True ): # Do only 1 impurity at the edge num_orb_in_imp = np.sum( unit_sizes[ 0 : carbons_in_cluster ] ) impurity_orbitals = np.zeros( [ mol.nao_nr() ], dtype=int ) impurity_orbitals[ 0 : num_orb_in_imp ] = 1 impurityClusters.append( impurity_orbitals ) else: # Partition atoms_passed = 0 jump = 0 while ( atoms_passed < len( unit_sizes ) ): num_carb_in_imp = min( carbons_in_cluster, len( unit_sizes ) - atoms_passed ) num_orb_in_imp = np.sum( unit_sizes[ atoms_passed : atoms_passed + num_carb_in_imp ] ) impurity_orbitals = np.zeros( [ mol.nao_nr() ], dtype=int ) impurity_orbitals[ jump : jump + num_orb_in_imp ] = 1 impurityClusters.append( impurity_orbitals ) atoms_passed += num_carb_in_imp jump += num_orb_in_imp theDMET = dmet.dmet( myInts, impurityClusters, isTranslationInvariant=False, method='CC', SCmethod='NONE' ) if ( casci_energy_formula == True ): theDMET.CC_E_TYPE = 'CASCI' if ( one_bath_orb_per_bond == True ): theDMET.BATH_ORBS = 2 * np.ones( [ len(impurityClusters) ], dtype=int ) theDMET.BATH_ORBS[ 0 ] = 1 theDMET.BATH_ORBS[ len(impurityClusters) - 1 ] = 1 the_energy = theDMET.doselfconsistent() print "###### DMET(", carbons_in_cluster,"C , CCSD ) /", thebasis1, "/", thebasis2, " =", the_energy
impurity_orbitals = np.zeros([mol.nao_nr()], dtype=int) num_orb_in_imp = unit_sizes[fragment] if (fragment > 0): impurity_orbitals[jump:jump + num_orb_in_imp] = -1 else: impurity_orbitals[jump:jump + num_orb_in_imp] = 1 impurityClusters.append(impurity_orbitals) jump += num_orb_in_imp isTranslationInvariant = False method = 'CASSCF' SCmethod = 'BFGS' theDMET = dmet.dmet(myInts, impurityClusters, isTranslationInvariant, method, SCmethod, fitImpBath=True, doDET=False) theDMET.impCAS = (2, 2) theDMET.CASlist = [19, 21] theDMET.CC_E_TYPE = 'CASCI' the_energy = theDMET.doselfconsistent() '''X1 = theDMET.ao2loc X2 = theDMET.loc2dmet #PRINT RHF in embedding space for mo in range(10,40): mo_coeff = reduce(np.dot,(X1,X2[:,:40], theDMET.MOmf))[:,mo].reshape(mol.nao_nr()) name = 'MO_nat_casdmet44_' + str(mo) + '.cube' id = mo + 1 mocube.mo(mol, name, id, mo_coeff, nx=60, ny=60, nz=60)'''