def ordering_diatomics(mol,C,basis_set): # {{{ ##DZ basis diatomics reordering with frozen 1s if basis_set == '6-31g': orb_type = ['s','pz','px','py'] elif basis_set == 'ccpvdz': orb_type = ['s','pz','dz','px','dxz','py','dyz','dx2-y2','dxy'] else: print("clustering not general yet") exit() ref = np.zeros(C.shape[1]) ## Find dimension of each space dim_orb = [] for orb in orb_type: print("Orb type",orb) idx = 0 for label in mol.ao_labels(): if orb in label: #print(label) idx += 1 ##frozen 1s orbitals if orb == 's': idx -= 2 dim_orb.append(idx) print(idx) new_idx = [] ## Find orbitals corresponding to each orb space for i,orb in enumerate(orb_type): print("Orbital type:",orb) from pyscf import mo_mapping s_pop = mo_mapping.mo_comps(orb, mol, C) #print(s_pop) ref += s_pop cas_list = s_pop.argsort()[-dim_orb[i]:] cas_list = np.sort(cas_list) print('cas_list', np.array(cas_list)) new_idx.extend(cas_list) #print(orb,' population for active space orbitals', s_pop[cas_list]) ao_labels = mol.ao_labels() #idx = mol.search_ao_label(['N.*s']) #for i in idx: # print(i, ao_labels[i]) print(ref) print(new_idx) for label in mol.ao_labels(): print(label) assert(len(new_idx) == len(set(new_idx))) return new_idx
def get_pi_space(mol, mf, cas_norb, cas_nel, local=True, p3=False): # {{{ from pyscf import mcscf, mo_mapping, lo, ao2mo # find the 2pz orbitals using mo_mapping ao_labels = ['C 2pz'] # get the 3pz and 2pz orbitals if p3: ao_labels = ['C 2pz', 'C 3pz'] cas_norb = 2 * cas_norb pop = mo_mapping.mo_comps(ao_labels, mol, mf.mo_coeff) cas_list = np.sort( pop.argsort() [-cas_norb:]) #take the 2z orbitals and resort in MO order print('Population for pz orbitals', pop[cas_list]) mo_occ = np.where(mf.mo_occ > 0)[0] focc_list = list(set(mo_occ) - set(cas_list)) focc = len(focc_list) # localize the active space if local: cl_a = lo.Boys(mol, mf.mo_coeff[:, cas_list]).kernel(verbose=4) C = mf.mo_coeff C[:, cas_list] = cl_a else: C = mf.mo_coeff mo_energy = mf.mo_energy[cas_list] J, K = mf.get_jk() K = K[cas_list, :][:, cas_list] print(K) if mol.symmetry == True: from pyscf import symm mo = symm.symmetrize_orb(mol, C[:, cas_list]) osym = symm.label_orb_symm(mol, mol.irrep_name, mol.symm_orb, mo) #symm.addons.symmetrize_space(mol, mo, s=None, check=True, tol=1e-07) for i in range(len(osym)): print("%4d %8s %16.8f" % (i + 1, osym[i], mo_energy[i])) # reorder the orbitals to get docc,active,vir ordering (Note:sort mo takes orbital ordering from 1) mycas = mcscf.CASCI(mf, cas_norb, cas_nel) C = mycas.sort_mo(cas_list + 1, mo_coeff=C) np.save('C.npy', C) # Get the active space integrals and the frozen core energy h, ecore = mycas.get_h1eff(C) g = ao2mo.kernel(mol, C[:, focc:focc + cas_norb], aosym='s4', compact=False).reshape(4 * ((cas_norb), )) C = C[:, focc:focc + cas_norb] #only carrying the active sapce orbs return h, ecore, g, C
def get_pi_space_local_split(mol, mf, cas_norb, cas_nel): # {{{ from pyscf import mcscf, mo_mapping, lo, ao2mo # find the 2pz orbitals using mo_mapping ao_labels = ['C 2pz'] pop = mo_mapping.mo_comps(ao_labels, mol, mf.mo_coeff) cas_list = np.sort( pop.argsort() [-cas_norb:]) #take the 2z orbitals and resort in MO order print('Population for pz orbitals', pop[cas_list]) mo_occ = np.where(mf.mo_occ > 0)[0] focc_list = list(set(mo_occ) - set(cas_list)) mo_vir = np.where(mf.mo_occ == 0)[0] fvir_list = list(set(mo_vir) - set(cas_list)) focc = len(focc_list) # localize the active space C = mf.mo_coeff ##### New stuff occ_l = cas_list[:cas_norb // 2] occ_v = cas_list[cas_norb // 2:] if 0: boys = lo.Boys(mol, mf.mo_coeff[:, occ_l]) #boys.init_guess = None cl_a = boys.kernel() boys = lo.Boys(mol, mf.mo_coeff[:, occ_v]) #boys.init_guess = None cl_b = boys.kernel() else: cl_a = lo.Boys(mol, mf.mo_coeff[:, occ_l]).kernel(verbose=4) cl_b = lo.Boys(mol, mf.mo_coeff[:, occ_v]).kernel(verbose=4) C[:, occ_l] = cl_a C[:, occ_v] = cl_b # reorder the orbitals to get docc,active,vir ordering (Note:sort mo takes orbital ordering from 1) mycas = mcscf.CASCI(mf, cas_norb, cas_nel) C = mycas.sort_mo(cas_list + 1, mo_coeff=C) # Get the active space integrals and the frozen core energy h, ecore = mycas.get_h1eff(C) g = ao2mo.kernel(mol, C[:, focc:focc + cas_norb], aosym='s4', compact=False).reshape(4 * ((cas_norb), )) C = C[:, focc:focc + cas_norb] #only carrying the active sapce orbs return h, ecore, g, C
H 2.157597486829 1.245660462400 0.000000000000 H 2.157597486829 -1.245660462400 0.000000000000 H -2.157597486829 1.245660462400 0.000000000000 H -2.157597486829 -1.245660462400 0.000000000000''', basis='ccpvdz', symmetry='D2h') mol.build() mf = scf.RHF(mol) mf.kernel() # # Search the active space orbitals based on certain AO components. In this # example, the pi-orbitals from carbon 2pz are considered. # pz_pop = mo_mapping.mo_comps('C 2pz', mol, mf.mo_coeff) cas_list = pz_pop.argsort()[-6:] print('cas_list', cas_list) print('pz population for active space orbitals', pz_pop[cas_list]) mycas = mcscf.CASSCF(mf, 6, 6) # Be careful with the keyword argument "base". By default sort_mo function takes # the 1-based indices. The return indices of .argsort() method above are 0-based cas_orbs = mycas.sort_mo(cas_list, mf.mo_coeff, base=0) mycas.kernel(cas_orbs) # # Localized orbitals are often used as the initial guess of CASSCF method. # When localizing SCF orbitals, it's better to call the localization twice to # obtain the localized orbitals for occupied space and virtual space
osym = symm.label_orb_symm(mol, mol.irrep_name, mol.symm_orb, mo) #symm.addons.symmetrize_space(mol, mo, s=None, check=True, tol=1e-07) for i in range(len(osym)): print("%4d %8s %16.8f"%(i+1,osym[i],mf.mo_energy[i])) mo_occ = mf.mo_occ>0 mo_vir = mf.mo_occ==0 print(mo_occ) print(mo_vir) mo_occ = np.where(mf.mo_occ>0)[0] mo_vir = np.where(mf.mo_occ==0)[0] print(mo_occ) print(mo_vir) from pyscf import mo_mapping s_pop = mo_mapping.mo_comps('C 2pz', mol, mf.mo_coeff) print(s_pop) cas_list = s_pop.argsort()[-cas_norb:] print('cas_list', np.array(cas_list)) print('s population for active space orbitals', s_pop[cas_list]) focc_list = list(set(mo_occ)-set(cas_list)) print(focc_list) fvir_list = list(set(mo_vir)-set(cas_list)) print(fvir_list) def get_eff_for_casci(focc_list,cas_list,h,g): # {{{ cas_dim = len(cas_list)
H 2.157597486829 1.245660462400 0.000000000000 H 2.157597486829 -1.245660462400 0.000000000000 H -2.157597486829 1.245660462400 0.000000000000 H -2.157597486829 -1.245660462400 0.000000000000''', basis='ccpvdz', symmetry='D2h') mol.build() mf = scf.RHF(mol) mf.kernel() # # Search the active space orbitals based on certain AO components. In this # example, the pi-orbitals from carbon 2pz are considered. # pz_pop = mo_mapping.mo_comps('C 2pz', mol, mf.mo_coeff) cas_list = pz_pop.argsort()[-6:] print('cas_list', cas_list) print('pz population for active space orbitals', pz_pop[cas_list]) mycas = mcscf.CASSCF(mf, 6, 6) # Be careful with the keyword argument "base". By default sort_mo function takes # the 1-based indices. The return indices of .argsort() method above are 0-based cas_orbs = mycas.sort_mo(cas_list, mf.mo_coeff, base=0) mycas.kernel(cas_orbs) # # Localized orbitals are often used as the initial guess of CASSCF method. # When localizing SCF orbitals, it's better to call the localization twice to # obtain the localized orbitals for occupied space and virtual space # separately. This split localization scheme can ensure that the initial core