def hess_wuyang(self, x, P_ref, dim, Ne_frag, Ne_env, impJK_sub, bathJK_sub): size = dim * (dim + 1) // 2 umat = tools.vec2mat(x, dim) ops = self.ops subKin = ops["subKin"] subVnuc1 = ops["subVnuc1"] subVnuc2 = ops["subVnuc2"] subVnuc_bound1 = ops["subVnuc_bound1"] subVnuc_bound2 = ops["subVnuc_bound2"] subCoreJK = ops["subCoreJK"] subTEI = ops["subTEI"] subOEI1 = subKin + subVnuc1 + subVnuc_bound1 + impJK_sub + umat subOEI2 = subKin + subVnuc2 + subVnuc_bound2 + subCoreJK + bathJK_sub + umat FRAG_energy, FRAG_1RDM, mo_coeff_frag, mo_energy_frag, mo_occ_frag = qcwrap.pyscf_rhf.scf_oei( subOEI1, dim, Ne_frag, self.smear_sigma) hess_frag = oep_hess(mo_coeff_frag, mo_energy_frag, size, dim, self.Ne_frag // 2, mo_occ_frag, self.smear_sigma) ENV_energy, ENV_1RDM, mo_coeff_env, mo_energy_env, mo_occ_env = qcwrap.pyscf_rhf.scf_oei( subOEI2, dim, Ne_env, self.smear_sigma) hess_env = oep_hess(mo_coeff_env, mo_energy_env, size, dim, self.Ne_env // 2, mo_occ_env, self.smear_sigma) hess = hess_frag + hess_env return hess
def cost_hess_wuyang(self, x, P_ref, dim, Ne_frag, Ne_env, impJK_sub, bathJK_sub, calc_hess=False): size = dim * (dim + 1) // 2 umat = tools.vec2mat(x, dim) print("|umat| = ", np.linalg.norm(umat)) #if(self.params.oep_print >= 3): # print "sum(diag(umat)) = ", np.sum(np.diag(umat)) # tools.MatPrint(umat, 'umat') FRAG_1RDM = np.zeros([dim, dim], dtype=float) ENV_1RDM = np.zeros([dim, dim], dtype=float) FRAG_energy = 0.0 ENV_energy = 0.0 ops = self.ops subKin = ops["subKin"] subVnuc1 = ops["subVnuc1"] subVnuc2 = ops["subVnuc2"] subVnuc_bound1 = ops["subVnuc_bound1"] subVnuc_bound2 = ops["subVnuc_bound2"] subCoreJK = ops["subCoreJK"] subTEI = ops["subTEI"] if (impJK_sub is not None): subOEI1 = subKin + subVnuc1 + subVnuc_bound1 + impJK_sub + umat subOEI2 = subKin + subVnuc2 + subVnuc_bound2 + subCoreJK + bathJK_sub + umat FRAG_energy, FRAG_1RDM, frag_mo_coeff, frag_mo_energy, frag_mo_occ = \ qcwrap.pyscf_rhf.scf_oei( subOEI1, dim, Ne_frag, self.smear_sigma) if (Ne_env > 0): ENV_energy, ENV_1RDM, env_mo_coeff, env_mo_energy, env_mo_occ =\ qcwrap.pyscf_rhf.scf_oei( subOEI2, dim, Ne_env, self.smear_sigma) if (calc_hess): hess_frag = oep_hess(frag_mo_coeff, frag_mo_energy, size, dim, Ne_frag // 2, frag_mo_occ, self.smear_sigma) hess_env = oep_hess(env_mo_coeff, env_mo_energy, size, dim, Ne_env // 2, env_mo_occ, self.smear_sigma) hess = hess_frag + hess_env else: raise Exception("NYI") diffP = FRAG_1RDM + ENV_1RDM - P_ref energy = FRAG_energy + ENV_energy - np.trace(np.dot(P_ref, umat)) grad = tools.mat2vec(diffP, dim) #grad = tools.mat2vec_hchain(diffP, dim) grad = -1.0 * grad print("2-norm (grad), max(grad):") print(np.linalg.norm(grad), ", ", np.amax(np.absolute(grad))) f = -energy print('-W = ', f) #test #f = np.linalg.norm(grad) if (calc_hess): return (f, grad, hess) else: return (f, grad)
def ObjFunc_WuYang(x, v2m, sym_tab, scf_solver, P_ref, dim, use_suborb, nonscf, scf_args_frag, scf_args_env, calc_hess=False): umat = v2m(x, dim, True, sym_tab) print ("|umat| = ", np.linalg.norm(umat)) scf_args_frag.update({'vext_1e':umat}) scf_args_env.update({'vext_1e':umat}) mf_frag = scf_solver(use_suborb, nonscf=nonscf, **scf_args_frag) mf_frag.kernel() #P_frag = mf_frag.make_rdm1() #E_frag = mf_frag.energy_elec(P_frag)[0] P_frag = mf_frag.rdm1 E_frag = mf_frag.elec_energy mf_env = scf_solver(use_suborb, nonscf=nonscf, **scf_args_env) mf_env.kernel() #P_env = mf_env.make_rdm1() #E_env = mf_env.energy_elec(P_env)[0] P_env = mf_env.rdm1 E_env = mf_env.elec_energy P_diff = P_frag + P_env - P_ref W = E_frag + E_env - np.trace(np.dot(P_ref,umat)) grad = v2m(P_diff, dim, False, sym_tab) grad *= -1.0 #l2_f, l2_g = l2_reg(x, dim, v2m, sym_tab, l2_lambda) f = -W #+ l2_f #grad = grad + l2_g print ('-W = ', f) print ("2-norm (grad), max(grad):" ) print (np.linalg.norm(grad), ", ", np.amax(np.absolute(grad))) ''' #test grad eps = 1e-5 size = dim*(dim+1)//2 step = np.zeros([size]) step[0] = eps x_p = x + step x_m = x - step umat_p = v2m(x_p, dim, True, None) scf_args_frag.update({'vext_1e':umat_p}) mf_frag_p = scf_solver(use_suborb, nonscf=nonscf, **scf_args_frag) mf_frag_p.kernel() E_frag_p = mf_frag_p.elec_energy umat_m = v2m(x_m, dim, True, None) scf_args_frag.update({'vext_1e':umat_m}) mf_frag_m = scf_solver(use_suborb, nonscf=nonscf, **scf_args_frag) mf_frag_m.kernel() E_frag_m = mf_frag_m.elec_energy print ("grad_finite: ", (E_frag_p-E_frag_m)/2.0/eps) print ("grad_anal: ", P_frag[0,0]) exit() #end test ''' if calc_hess: from pydmfet.libcpp import oep_hess, symmtrize_hess, fd_hess size = dim*(dim+1)//2 smear_sigma = getattr(mf_frag, 'smear_sigma', 0.0) Ne = int(np.sum(mf_frag.mo_occ)) hess_frag = oep_hess(mf_frag.mo_coeff, mf_frag.mo_energy, size, dim, Ne//2, mf_frag.mo_occ, smear_sigma, sym_tab) #hess_frag = fd_hess(umat,v2m, scf_solver, use_suborb, nonscf, scf_args_frag) smear_sigma = getattr(mf_env, 'smear_sigma', 0.0) Ne = int(np.sum(mf_env.mo_occ)) hess_env = oep_hess(mf_env.mo_coeff, mf_env.mo_energy, size, dim, Ne//2, mf_env.mo_occ, smear_sigma, sym_tab) #hess_env = fd_hess(umat,v2m, scf_solver, use_suborb, nonscf, scf_args_env) hess = -hess_frag - hess_env if sym_tab is not None: hess = symmtrize_hess(hess,sym_tab,size) return f,grad,hess return f, grad
def build_null_space(self, mf_frag, mf_env, Ne_frag, Ne_env, dim, tol=1e-9): size = dim * (dim + 1) // 2 hess_frag = oep_hess(mf_frag.mo_coeff, mf_frag.mo_energy, size, dim, self.Ne_frag // 2, mf_frag.mo_occ, self.smear_sigma) hess_env = oep_hess(mf_env.mo_coeff, mf_env.mo_energy, size, dim, self.Ne_env // 2, mf_env.mo_occ, self.smear_sigma) #u_f, s_f, vh_f = np.linalg.svd(hess_frag) #u_e, s_e, vh_e = np.linalg.svd(hess_env) u_f, s_f, vh_f = mkl_svd(hess_frag) u_e, s_e, vh_e = mkl_svd(hess_env) rankf = tools.rank(s_f, tol) ranke = tools.rank(s_e, tol) print('svd of hess') print(size - rankf, size - ranke) print(s_f[rankf - 4:rankf + 4]) print(s_e[ranke - 4:ranke + 4]) if (rankf >= size or ranke >= size): print('null space not found!') return None x = np.zeros((size)) y = np.zeros((size)) v_f = vh_f.T v_e = vh_e.T #tools.MatPrint(v_f, 'v_f') #tools.MatPrint(v_e, 'v_e') v_fe = np.concatenate((v_f[:, rankf:], -v_e[:, ranke:]), axis=1) v_fe_fort = np.require(v_fe, requirements=['A', 'O', 'W', 'F']) #uu, ss, vvh = np.linalg.svd(v_fe) uu, ss, vvh = mkl_svd(v_fe_fort, 2) rankfe = tools.rank(ss, tol) print('null space shape:', v_fe.shape) print('null space rank:', rankfe) #np.set_printoptions(threshold=np.inf) #print ss if (rankfe >= v_fe.shape[-1]): print('null space not found!') return None print('singular value:', ss[rankfe - 1:rankfe + 1]) vv = vvh[rankfe:, :].T #zero = np.dot(v_fe, vv) #print np.linalg.norm(zero),np.amax(np.absolute(zero)) vint = np.dot(v_f[:, rankf:], vv[:(size - rankf), :]) #vint1 = np.dot(v_e[:,ranke:],vv[(size-rankf):,:]) for i in range(vint.shape[-1]): vint[:, i] = vint[:, i] / np.linalg.norm(vint[:, i]) print('check orthogonality of vint:') zero = np.dot(vint.T, vint) - np.eye(vint.shape[-1]) print(np.linalg.norm(zero), np.amax(np.absolute(zero))) return vint
def ObjFunc_LeastSq(x, v2m, sym_tab, scf_solver, P_ref, dim, use_suborb, nonscf, scf_args_frag, scf_args_env): umat = v2m(x, dim, True, sym_tab) print("|umat| = ", np.linalg.norm(umat)) #tools.MatPrint(umat, "umat") scf_args_frag.update({'vext_1e': umat}) scf_args_env.update({'vext_1e': umat}) mf_frag = scf_solver(use_suborb, nonscf=nonscf, **scf_args_frag) #mf_frag.init_guess = 'hcore' mf_frag.kernel() #P_frag = mf_frag.make_rdm1() #E_frag = mf_frag.energy_elec(P_frag)[0] P_frag = mf_frag.rdm1 E_frag = mf_frag.elec_energy mf_env = scf_solver(use_suborb, nonscf=nonscf, **scf_args_env) #mf_env.init_guess = 'hcore' mf_env.kernel() #P_env = mf_env.make_rdm1() #E_env = mf_env.energy_elec(P_env)[0] P_env = mf_env.rdm1 E_env = mf_env.elec_energy P_diff = P_frag + P_env - P_ref f = v2m(P_diff, dim, False, sym_tab) print("2-norm (grad), max(grad):") print(np.linalg.norm(f), ", ", np.amax(np.absolute(f))) size = dim * (dim + 1) // 2 smear_sigma = getattr(mf_frag, 'smear_sigma', 0.0) Ne = int(np.sum(mf_frag.mo_occ)) hess_frag = oep_hess(mf_frag.mo_coeff, mf_frag.mo_energy, size, dim, Ne // 2, mf_frag.mo_occ, smear_sigma, sym_tab) smear_sigma = getattr(mf_env, 'smear_sigma', 0.0) Ne = int(np.sum(mf_env.mo_occ)) hess_env = oep_hess(mf_env.mo_coeff, mf_env.mo_energy, size, dim, Ne // 2, mf_env.mo_occ, smear_sigma, sym_tab) hess = hess_frag + hess_env ''' #test hess eps = 1e-5 step = np.zeros([size]) step[0] = eps x_p = x + step x_m = x - step umat_p = v2m(x_p, dim, True, None) scf_args_frag.update({'vext_1e':umat_p}) mf_frag_p = scf_solver(use_suborb, nonscf=nonscf, **scf_args_frag) mf_frag_p.init_guess = 'hcore' mf_frag_p.kernel() P_frag_p = mf_frag_p.rdm1 umat_m = v2m(x_m, dim, True, None) scf_args_frag.update({'vext_1e':umat_m}) mf_frag_m = scf_solver(use_suborb, nonscf=nonscf, **scf_args_frag) mf_frag_m.init_guess = 'hcore' mf_frag_m.kernel() P_frag_m = mf_frag_m.rdm1 P_grad = 0.5/eps*(P_frag_p - P_frag_m) P_grad_anl = v2m(hess_frag[:,0], dim, True, None) tools.MatPrint(P_grad,"P_grad finite") tools.MatPrint(P_grad_anl,"P_grad anl") tools.MatPrint((P_grad_anl-P_grad)/P_grad,"P_grad anl-P_grad / P_grad") tools.MatPrint(P_grad*P_grad_anl,"P_grad * P_grad_anl") exit() #end test hess ''' if sym_tab is not None: hess = symmtrize_hess(hess, sym_tab, size) grad = np.dot(f, hess) #print('delP*X') #print(grad) f = v2m(P_diff, dim, False, None) f = 0.5 * np.dot(f, f) print("f = ", f) return f, grad