def test_cc_unitary(): # pragma: nocover from pyscf import gto from pyscf import scf mol = gto.Mole() mol.atom = [[8, (0., 0., 0.)], [1, (0., -0.757, 0.587)], [1, (0., 0.757, 0.587)]] mol.basis = { 'H': 'sto-3g', 'O': 'sto-3g', } mol.build() rhf = scf.RHF(mol) rhf.scf() # -76.0267656731 from tcc.cc_solvers import residual_diis_solver from tcc.cc_solvers import classic_solver from tcc.rccsd import RCCSD_UNIT, RCCSD cc1 = RCCSD_UNIT(rhf) cc2 = RCCSD(rhf) converged1, energy2, _ = residual_diis_solver(cc1, ndiis=5, conv_tol_energy=-1, conv_tol_res=1e-10) converged2, energy2, _ = residual_diis_solver(cc2, ndiis=5, conv_tol_energy=-1, conv_tol_res=1e-10)
def run_ccsd(): us = U_VALUES_REFERENCE.copy() from tcc.cc_solvers import residual_diis_solver energies = [] amps = None for idxu, u in enumerate(us): rhf = hubbard_from_scf(scf.RHF, N, N, u, USE_PBC) rhf.scf() scf_energy = rhf.e_tot cc = RCCSD_UNIT(rhf) converged, energy, amps = residual_diis_solver(cc, conv_tol_energy=1e-6, lam=15, amps=amps, max_cycle=2000) energies.append(scf_energy + energy) np.savetxt('reference/{}-site/RCCSD.txt'.format(N), np.column_stack((us, energies)), header='U E_total') # Plot from matplotlib import pyplot as plt fig, ax = plt.subplots() plt.plot(us, energies) plt.xlabel('$U$') plt.ylabel('$E$, H') plt.title('Energy behavior for different ranks') fig.show()
def test_cc_hubbard(self): from tcc.cc_solvers import residual_diis_solver from tcc.rccsd import RCCSD_UNIT cc = RCCSD_UNIT(self.rhf) converged, energy, _ = residual_diis_solver(cc, ndiis=5, conv_tol_res=1e-6, lam=5, max_cycle=100) self.assertEqual(np.allclose(energy, -0.93834495800469087, 1e-6), True)
def test_update_diis_solver(): from tcc.hubbard import hubbard_from_scf from pyscf import scf N = 6 U = 4 USE_PBC = 'y' rhf = hubbard_from_scf(scf.RHF, N, N, U, USE_PBC) rhf.scf() # -76.0267656731 from tcc.rccsd import RCCSD_UNIT from tcc.rccsd_cpd import (RCCSD_nCPD_LS_T_HUB) cc1 = RCCSD_UNIT(rhf) cc2 = RCCSD_nCPD_LS_T_HUB(rhf, rankt={'t2': 30}) from tcc.cc_solvers import (residual_diis_solver, update_diis_solver, classic_solver) cc1._converged = False converged1, energy1, amps1 = residual_diis_solver(cc1, conv_tol_energy=1e-8, lam=3, max_cycle=100) import pickle with open('test_amps.p', 'rb') as fp: ampsi = pickle.load(fp) cc2._converged = False converged2, energy2, amps2 = update_diis_solver(cc2, conv_tol_energy=1e-8, conv_tol_res=1e-8, beta=0.666, max_cycle=100, amps=ampsi) cc2._converged = False converged2, energy2, amps2 = classic_solver(cc2, conv_tol_energy=1e-8, conv_tol_res=1e-8, lam=3, max_cycle=100)
def test_cc_hubbard(): # pragma: nocover from pyscf import gto from pyscf import scf from tcc.hubbard import hubbard_from_scf rhf = hubbard_from_scf(scf.RHF, 6, 6, 3, 'y') rhf.damp = -4.0 rhf.scf() from tcc.cc_solvers import root_solver, residual_diis_solver from tcc.rccsd import RCCSD_UNIT cc = RCCSD_UNIT(rhf) converged, energy, _ = root_solver(cc, conv_tol=1e-6)
def calc_solutions_diff_r_eq_cpd(): """ Plot error of RCCSD-CPD vs rank for weak corellation """ rankst = RANKS_T.copy() # Run RHF calculations rhf = hubbard_from_scf(scf.RHF, N, N, U, 'y') rhf.damp = -4.0 rhf.scf() rhf_results = tuple([rhf.e_tot, rhf.mo_coeff, rhf.mo_energy]) with open('calculated/{}-site/amps_and_scf_eq/rhf_results_u_{}.p'.format(N, U), 'wb') as fp: pickle.dump(rhf_results, fp) # Run reference calculation cc_ref = RCCSD_UNIT(rhf) _, energy_ref, amps_ref = root_solver(cc_ref, conv_tol=1e-10) cc_results = tuple([energy_ref, amps_ref]) with open('calculated/{}-site/amps_and_scf_eq/cc_results_u_{}.p'.format(N, U), 'wb') as fp: pickle.dump(cc_results, fp) all_amps = [] for idx, rank in enumerate(rankst): tim = time.process_time() cc = RCCSD_nCPD_LS_T_HUB(rhf, rankt={'t2': rank}) converged, energy, amps = classic_solver( cc, lam=1.8, conv_tol_energy=1e-14, conv_tol_amps=1e-10, max_cycle=40000, verbose=logger.NOTE) if not converged: Warning( 'Warning: N = {}, U = {} ' 'Rank = {} did not converge'.format(N, U, rank) ) all_amps.append(tuple([energy, rank, amps])) elapsed = time.process_time() - tim print('Step {} out of {}, rank = {}, time: {}'.format( idx + 1, len(rankst), rank, elapsed )) with open('calculated/{}-site/amps_and_scf_eq/energy_rank_amps_u_{}.p'.format(N, U), 'wb') as fp: pickle.dump(all_amps, fp)
def test_rccsd_unit(self): from tcc.cc_solvers import classic_solver, residual_diis_solver from tcc.rccsd import RCCSD, RCCSD_UNIT cc1 = RCCSD(self.rhf) cc2 = RCCSD_UNIT(self.rhf) converged1, energy1, amps = classic_solver(cc1) converged2, energy2, _ = classic_solver(cc2) self.assertEqual(converged1, converged2) self.assertEqual(np.allclose(energy1, -0.2133432609672395, 1e-5), True) self.assertEqual(np.allclose(energy1, energy2, 1e-5), True) converged1, energy1, _ = residual_diis_solver(cc1, amps=amps, conv_tol_energy=1e-10) converged2, energy2, _ = residual_diis_solver(cc2, amps=amps, conv_tol_energy=1e-10) self.assertEqual(np.allclose(energy1, -0.2133432609672395, 1e-5), True) self.assertEqual(np.allclose(energy1, energy2, 1e-5), True)
def calc_err_vs_r_cpd(): """ Plot error of RCCSD-CPD vs rank for weak corellation """ # Set up parameters of the script N = 14 U = 2 rankst = np.round(N**np.linspace(0.2, 1.8, num=10)).astype('int') # Run RHF calculations from pyscf import scf from tcc.hubbard import hubbard_from_scf rhf = hubbard_from_scf(scf.RHF, N, N, U, 'y') rhf.damp = -4.0 rhf.scf() from tcc.cc_solvers import (classic_solver, root_solver) from tcc.rccsd import RCCSD_UNIT from tcc.rccsd_cpd import RCCSD_CPD_LS_T_HUB from tensorly.decomposition import parafac # Run reference calculation cc_ref = RCCSD_UNIT(rhf) _, energy_ref, amps_ref = root_solver(cc_ref, conv_tol=1e-10) energies = [] deltas = [] for idx, rank in enumerate(rankst): tim = time.process_time() cc = RCCSD_CPD_LS_T_HUB(rhf, rankt=rank) xs = parafac( amps_ref.t2, rank, tol=1e-14 ) amps_guess = cc.types.AMPLITUDES_TYPE( amps_ref.t1, *xs ) converged, energy, amps = classic_solver( cc, lam=1.8, conv_tol_energy=1e-14, conv_tol_amps=1e-10, max_cycle=10000, amps=amps_guess, verbose=logger.NOTE) if not converged: Warning( 'Warning: N = {}, U = {} ' 'Rank = {} did not converge'.format(N, U, rank) ) energies.append(energy) deltas.append(energy - energy_ref) elapsed = time.process_time() - tim print('Step {} out of {}, rank = {}, time: {}'.format( idx + 1, len(rankst), rank, elapsed )) results = np.column_stack((rankst, energies, deltas)) np.savetxt( 'calculated/{}-site/err_vs_rank.txt'.format(N), results, header='Rank Energy Delta', fmt=('%i', '%e', '%e') ) rankst, energies, deltas = np.loadtxt( 'calculated/{}-site/err_vs_rank.txt'.format(N), unpack=True) # Plot from matplotlib import pyplot as plt plt.plot(np.log(rankst)/np.log(N), np.log10(np.abs(deltas))) plt.xlabel('log$_N(R)$') plt.ylabel('log($\Delta$)') plt.title('Error dependence on rank in weak correlation regime') plt.show()
def calc_t2_norm_vs_u_rccsd(): """ Calculate norms of T2 in conventional RCCSD """ us = U_VALUES.copy() lambdas = LAMBDAS.copy() with open('calculated/{}-site/scfs_different_u_t1.p'.format(N), 'rb') as fp: ref_scfs = pickle.load(fp) t1_norms = [] energies = [] t2_norms = [] # timb = time.process_time() solutions = [] amps = None for idxu, (u, curr_scf) in enumerate(zip(us, ref_scfs)): rhf = hubbard_from_scf(scf.RHF, N, N, u, 'y') rhf.max_cycle = 1 rhf.scf() e_scf, mo_coeff, mo_energy = curr_scf cc = RCCSD_UNIT(rhf, mo_coeff=mo_coeff, mo_energy=mo_energy) converged, energy, amps = residual_diis_solver(cc, conv_tol_energy=1e-9, conv_tol_res=1e-9, max_cycle=10000, verbose=logger.NOTE, amps=None, lam=14) norms = amps.map(np.linalg.norm) t1_norms.append(norms.t1) t2_norms.append(norms.t2) energies.append(energy) solutions.append([u, curr_scf, amps]) print('Step {} out of {}'.format(idxu + 1, len(us))) t1_norms = np.column_stack((us, t1_norms)) t2_norms = np.column_stack((us, t2_norms)) np.savetxt('calculated/{}-site/t1_norm_vs_u_rccsd.txt'.format(N), t1_norms, header='U |t1|') np.savetxt('calculated/{}-site/t2_norm_vs_u_rccsd.txt'.format(N), t2_norms, header='U |t2|') with open('calculated/{}-site/amps_and_scf_rccsd.p'.format(N), 'wb') as fp: pickle.dump(solutions, fp) us, *t2_l = np.loadtxt( 'calculated/{}-site/t2_norm_vs_u_rccsd.txt'.format(N), unpack=True) # Plot from matplotlib import pyplot as plt fig, ax = plt.subplots() plt.plot(us, t2_l[0]) plt.xlabel('$U / t$') plt.ylabel('$||T^{2}||$') plt.title('$T^{2}$ norm behavior for different ranks') fig.show()