def get_vhf(mol, dm, *args, **kwargs): """This function needs to be defined here as it depends on eri2c and eri3c, which are computed above. This is a bit a la 'por la pata del blumer' but currently I don't have a better solution because the density lives only in the namespace inside the function. """ naux = eri2c.shape[0] nao = mol.nao_nr() rho = np.einsum('ijp,ij->p', eri3c, dm) rho = np.linalg.solve(eri2c, rho) jmat = np.einsum('p,ijp->ij', rho, eri3c) kpj = np.einsum('ijp,jk->ikp', eri3c, dm) pik = np.linalg.solve(eri2c, kpj.reshape(-1, naux).T) kmat = np.einsum('pik,kjp->ij', pik.reshape(naux, nao, nao), eri3c) # Printing the .den file print >> den_file, len(mol.atom) print >> den_file, 'Molecule', sys.argv[1] # iini = 0 for i in mol.atom: bas_len = sum(normalization.df_basis_dict[auxmol.basis][i.split()[0]]) print >> den_file, i.strip('\n'), ' ', print >> den_file, " ".join("%16.12f" % x for x in rho[iini:iini + bas_len]) #print >>den_file, " ".join("%16.12f" % x for x in rho[iini:iini+bas_len[i.split()[0]]]) iini += bas_len rho_normalized = rho * normalization.normalize_df(auxmol)[0] # Compute the Hellmann Feynman Forces from the DF expansion. print '\nHellmann-Feynman Forces from DF:-----------------------' print ' x y z' for atom_id in range(mol.natm): print atom_id, mol.atom_symbol(atom_id), for axis in [0, 1, 2]: norm_aux = fdf.OneGaussian_from_Overlap(auxmol) #*np.sqrt(np.pi*4) hf = fdf.HellmannFeynman_df(auxmol, atom_id, axis) #*np.sqrt(np.pi*4) Coul = grad.grad_nuc(mol)[atom_id][axis] print '%16.9f' % (hf.dot(rho_normalized) + Coul), print '' print '-------------------------------------------------------' print '\nNorm = %12.6f' % (norm_aux.dot(rho_normalized)) #print '\nRHO =', rho_normalized # Save the density in numpy arrat .npy file #rhosave=open('rho.np','w'); np.save(rhosave,rho); rhosave.close() return jmat - kmat * .5
def _get_hellmann_feynman_mo(mol,density_matrix): """Hellmann-Feynman Forces from the MO.""" _hf_forces_mo=np.ones([mol.natm,3]) for i in range(mol.natm): F_en_0=mo_integrals.hellmann_feynman_mo(mol,i)/2. F_nn=grad.grad_nuc(mol) F_en=np.einsum('ij,kji->k',density_matrix,F_en_0)*2. # 2*\Sum P_ij*Phi_i*Phi_j _hf_forces_mo[i]=F_en+F_nn[i] return -_hf_forces_mo
def _get_hellmann_feynman_df(rho_normalized,mol,auxmol): """Compute the Hellmann Feynman Forces from the DF expansion. """ _hf_forces_df = np.ones([mol.natm,3]) for atom_id in range(mol.natm): hf=df_integrals.hellmann_feynman_df(auxmol,atom_id) coul=grad.grad_nuc(mol)[atom_id]#[axis] _hf_forces_df[atom_id]=np.dot(rho_normalized,hf)+coul return -_hf_forces_df
) mf = scf.RHF(mol) mf.conv_tol = 1e-14 ehf = mf.scf() mycc = ccsd.CCSD(mf) mycc.conv_tol = 1e-10 mycc.conv_tol_normt = 1e-10 ecc, t1, t2 = mycc.kernel() eris = mycc.ao2mo() e3ref = ccsd_t.kernel(mycc, eris, t1, t2) print ehf+ecc+e3ref eris = mycc.ao2mo(mf.mo_coeff) conv, l1, l2 = ccsd_t_lambda.kernel(mycc, eris, t1, t2) g1 = kernel(mycc, t1, t2, l1, l2, eris=eris, mf_grad=grad.RHF(mf)) print(g1 + grad.grad_nuc(mol)) #O 0.0000000000 0.0000000000 -0.0112045345 #H 0.0000000000 0.0234464201 0.0056022672 #H 0.0000000000 -0.0234464201 0.0056022672 mol = gto.M( verbose = 0, atom = ''' H -1.90779510 0.92319522 0.08700656 H -1.08388168 -1.61405643 -0.07315086 H 2.02822318 -0.61402169 0.09396693 H 0.96345360 1.30488291 -0.10782263 ''', unit='bohr', basis = '631g')
print('-----------------------------------') mol = gto.M(verbose=0, atom=[["O", (0., 0., 0.)], [1, (0., -0.757, 0.587)], [1, (0., 0.757, 0.587)]], basis='631g') mf = scf.RHF(mol) ehf = mf.scf() mycc = ccsd.CCSD(mf) mycc.conv_tol = 1e-10 mycc.conv_tol_normt = 1e-10 ecc, t1, t2 = mycc.kernel() l1, l2 = mycc.solve_lambda() g1 = kernel(mycc, t1, t2, l1, l2, mf_grad=grad.RHF(mf)) print('gcc') print(g1 + grad.grad_nuc(mol)) #[[ 0 0 1.00950925e-02] # [ 0 2.28063426e-02 -5.04754623e-03] # [ 0 -2.28063426e-02 -5.04754623e-03]] lib.parameters.BOHR = 1 r = 1.76 #.748 mol = gto.M(verbose=0, atom='''H 0 0 0; H 0 0 %f''' % r, basis='631g') mf = scf.RHF(mol) mf.conv_tol = 1e-14 ehf0 = mf.scf() ghf = grad.RHF(mf).grad() mycc = ccsd.CCSD(mf) mycc.conv_tol = 1e-10 mycc.conv_tol_normt = 1e-10 ecc, t1, t2 = mycc.kernel()
print '\nReference Forces:--------------------------------------' print ' x y z' for i in range(mol.natm): print i, mol.atom_symbol(i), print '%16.9f %16.9f %16.9f' % (hartree_fock_force[i, 0], hartree_fock_force[i, 1], hartree_fock_force[i, 2]) print '-------------------------------------------------------' P = mf.from_chk() print '\nHellmann-Feynman Forces from MO:-----------------------' print ' x y z' for i in range(mol.natm): F_en_0 = fdf.HellmannFeynman(mol, i) / 2. F_nn = grad.grad_nuc(mol) F_en = np.einsum('ij,kji->k', P, F_en_0) * 2. # 2*\Sum P_ij*Phi_i*Phi_j Ft = F_en + F_nn[i] print i, mol.atom_symbol(i), print '%16.9f %16.9f %16.9f' % (Ft[0], Ft[1], Ft[2]) print '-------------------------------------------------------' mf.get_veff = get_vhf # This substitutes the function 'get_veff' in mf by the # implementation of density fitting 'get_vhf' which prints the d_l coefficients. # This is done here after the SCF was solved. e_fit = mf.energy_tot() # Run this only to get the density basis coefficients. # Here the function 'get_vhf' is called and give access to the coefficients # prints the d_l coefficients.