def _sapt_cpscf_solve(cache, jk, rhsA, rhsB, maxiter, conv): """ Solve the SAPT CPHF (or CPKS) equations. """ # Make a preconditioner function P_A = core.Matrix(cache["eps_occ_A"].shape[0], cache["eps_vir_A"].shape[0]) P_A.np[:] = (cache["eps_occ_A"].np.reshape(-1, 1) - cache["eps_vir_A"].np) P_B = core.Matrix(cache["eps_occ_B"].shape[0], cache["eps_vir_B"].shape[0]) P_B.np[:] = (cache["eps_occ_B"].np.reshape(-1, 1) - cache["eps_vir_B"].np) # Preconditioner function def apply_precon(x_vec, act_mask): if act_mask[0]: pA = x_vec[0].clone() pA.apply_denominator(P_A) else: pA = False if act_mask[1]: pB = x_vec[1].clone() pB.apply_denominator(P_B) else: pB = False return [pA, pB] # Hx function def hessian_vec(x_vec, act_mask): if act_mask[0]: xA = cache["wfn_A"].cphf_Hx([x_vec[0]])[0] else: xA = False if act_mask[1]: xB = cache["wfn_B"].cphf_Hx([x_vec[1]])[0] else: xB = False return [xA, xB] # Manipulate the printing sep_size = 51 core.print_out(" " + ("-" * sep_size) + "\n") core.print_out(" " + "SAPT Coupled Induction Solver".center(sep_size) + "\n") core.print_out(" " + ("-" * sep_size) + "\n") core.print_out(" Maxiter = %11d\n" % maxiter) core.print_out(" Convergence = %11.3E\n" % conv) core.print_out(" " + ("-" * sep_size) + "\n") tstart = time.time() core.print_out(" %4s %12s %12s %9s\n" % ("Iter", "(A<-B)", "(B->A)", "Time [s]")) core.print_out(" " + ("-" * sep_size) + "\n") start_resid = [rhsA.sum_of_squares(), rhsB.sum_of_squares()] # print function def pfunc(niter, x_vec, r_vec): if niter == 0: niter = "Guess" else: niter = ("%5d" % niter) # Compute IndAB valA = (r_vec[0].sum_of_squares() / start_resid[0]) ** 0.5 if valA < conv: cA = "*" else: cA = " " # Compute IndBA valB = (r_vec[1].sum_of_squares() / start_resid[1]) ** 0.5 if valB < conv: cB = "*" else: cB = " " core.print_out(" %5s %15.6e%1s %15.6e%1s %9d\n" % (niter, valA, cA, valB, cB, time.time() - tstart)) return [valA, valB] # Compute the solver vecs, resid = solvers.cg_solver( [rhsA, rhsB], hessian_vec, apply_precon, maxiter=maxiter, rcond=conv, printlvl=0, printer=pfunc) core.print_out(" " + ("-" * sep_size) + "\n") return vecs
def _sapt_cpscf_solve(cache, jk, rhsA, rhsB, maxiter, conv, sapt_jk_B=None): """ Solve the SAPT CPHF (or CPKS) equations. """ cache["wfn_A"].set_jk(jk) if sapt_jk_B: cache["wfn_B"].set_jk(sapt_jk_B) else: cache["wfn_B"].set_jk(jk) # Make a preconditioner function P_A = core.Matrix(cache["eps_occ_A"].shape[0], cache["eps_vir_A"].shape[0]) P_A.np[:] = (cache["eps_occ_A"].np.reshape(-1, 1) - cache["eps_vir_A"].np) P_B = core.Matrix(cache["eps_occ_B"].shape[0], cache["eps_vir_B"].shape[0]) P_B.np[:] = (cache["eps_occ_B"].np.reshape(-1, 1) - cache["eps_vir_B"].np) # Preconditioner function def apply_precon(x_vec, act_mask): if act_mask[0]: pA = x_vec[0].clone() pA.apply_denominator(P_A) else: pA = False if act_mask[1]: pB = x_vec[1].clone() pB.apply_denominator(P_B) else: pB = False return [pA, pB] # Hx function def hessian_vec(x_vec, act_mask): if act_mask[0]: xA = cache["wfn_A"].cphf_Hx([x_vec[0]])[0] else: xA = False if act_mask[1]: xB = cache["wfn_B"].cphf_Hx([x_vec[1]])[0] else: xB = False return [xA, xB] # Manipulate the printing sep_size = 51 core.print_out(" " + ("-" * sep_size) + "\n") core.print_out(" " + "SAPT Coupled Induction Solver".center(sep_size) + "\n") core.print_out(" " + ("-" * sep_size) + "\n") core.print_out(" Maxiter = %11d\n" % maxiter) core.print_out(" Convergence = %11.3E\n" % conv) core.print_out(" " + ("-" * sep_size) + "\n") tstart = time.time() core.print_out(" %4s %12s %12s %9s\n" % ("Iter", "(A<-B)", "(B->A)", "Time [s]")) core.print_out(" " + ("-" * sep_size) + "\n") start_resid = [rhsA.sum_of_squares(), rhsB.sum_of_squares()] # print function def pfunc(niter, x_vec, r_vec): if niter == 0: niter = "Guess" else: niter = ("%5d" % niter) # Compute IndAB valA = (r_vec[0].sum_of_squares() / start_resid[0]) ** 0.5 if valA < conv: cA = "*" else: cA = " " # Compute IndBA valB = (r_vec[1].sum_of_squares() / start_resid[1]) ** 0.5 if valB < conv: cB = "*" else: cB = " " core.print_out(" %5s %15.6e%1s %15.6e%1s %9d\n" % (niter, valA, cA, valB, cB, time.time() - tstart)) return [valA, valB] # Compute the solver vecs, resid = solvers.cg_solver( [rhsA, rhsB], hessian_vec, apply_precon, maxiter=maxiter, rcond=conv, printlvl=0, printer=pfunc) core.print_out(" " + ("-" * sep_size) + "\n") return vecs