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 test_residual_solver(self): from tcc.cc_solvers import residual_diis_solver from tcc.rccsd import RCCSD cc = RCCSD(self.rhf) converged, energy, amps = residual_diis_solver(cc) self.assertEqual(converged, True) self.assertEqual(np.allclose(energy, -0.2133432609672395, 1e-5), True) converged, energy, _ = residual_diis_solver(cc, amps, lam=3, conv_tol_energy=1e-10) self.assertEqual(converged, True) self.assertEqual(np.allclose(energy, -0.2133432609672395, 1e-5), True)
def test_cc_step_diis(): # 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 update_diis_solver from tcc.rccsd import RCCSD cc = RCCSD(rhf) converged1, energy1, _ = residual_diis_solver(cc, conv_tol_energy=1e-10, conv_tol_res=1e-10, max_cycle=100, lam=1) cc._converged = False converged2, energy2, _ = update_diis_solver(cc, conv_tol_energy=1e-10, conv_tol_res=1e-10, beta=0, max_cycle=100)
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 run_ccsd(): basis = BASIS dists = DISTS_REFERENCE.copy() lambdas = LAMBDAS_REFERENCE.copy() def make_mol(dist): from pyscf import gto mol = gto.Mole() mol.atom = [[7, (0., 0., 0.)], [7, (0., 0., dist)]] mol.basis = {'N': basis} mol.build() return mol mols = [make_mol(dist) for dist in dists] # Run all scfs here so we will have same starting points for CC run_scfs(mols, 'reference/{}/scfs_different_dist.p'.format(basis)) with open('reference/{}/scfs_different_dist.p'.format(basis), 'rb') as fp: ref_scfs = pickle.load(fp) from tcc.rccsd_mul import RCCSD_MUL_RI energies = [] amps = None for idxd, (dist, curr_scf) in enumerate(zip(dists, ref_scfs)): tim = time.process_time() rhf = scf.density_fit(scf.RHF(mols[idxd])) rhf.max_cycle = 1 rhf.scf() e_scf, mo_coeff, mo_energy = curr_scf cc = RCCSD_MUL_RI(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-8, max_cycle=500, verbose=logger.NOTE, amps=amps) energies.append(energy + e_scf) elapsed = time.process_time() - tim print('Step {} out of {}, dist = {}, time: {}\n'.format( idxd + 1, len(dists), dist, elapsed)) np.savetxt('reference/{}/RCCSD.txt'.format(basis), np.column_stack((dists, energies)), header='U E_total') # Plot from matplotlib import pyplot as plt fig, ax = plt.subplots() plt.plot(dists, energies) plt.xlabel('$D, \AA$') plt.ylabel('$E$, H') plt.title('Energy in RCCSD (RI)') 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_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 run_cc_ref(workdir): from pyscf.lib import logger print('Running CC') # Restore RHF object with open(workdir + 'rhf_results.p', 'rb') as fp: rhf_results = pickle.load(fp) e_tot, mo_coeff, mo_energy, atom = rhf_results mol = gto.Mole() mol.atom = atom mol.basis = BASIS mol.build() rhf = scf.density_fit(scf.RHF(mol)) rhf.max_cycle = 1 rhf.scf() # Run RCCSD_RI from tcc.rccsd import RCCSD_DIR_RI from tcc.cc_solvers import residual_diis_solver tim = time.process_time() if (not isfile(workdir + 'ccsd_results.p') or not isfile(workdir + 'RCCSD.txt')): cc = RCCSD_DIR_RI(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-8, max_cycle=500, verbose=logger.INFO) ccsd_results = [energy, amps] with open(workdir + 'ccsd_results.p', 'wb') as fp: pickle.dump(ccsd_results, fp) np.savetxt( workdir + 'RCCSD.txt', np.array([energy, ]), header='Energy' ) elapsed = time.process_time() - tim print('Done reference RCCSD, time: {}'.format(elapsed))
def test_cc_hubbard_ri(): # pragma: nocover from pyscf import scf from tcc.hubbard import hubbard_from_scf rhf = hubbard_from_scf(scf.RHF, 22, 22, 3, 'y') rhf.damp = -4.0 rhf.scf() from tcc.cc_solvers import residual_diis_solver from tcc.rccsd_mul import RCCSD_MUL_RI_HUB cc = RCCSD_MUL_RI_HUB(rhf) converged, energy, _ = residual_diis_solver(cc, ndiis=5, conv_tol_res=1e-6, lam=5, max_cycle=100)
def test_rccsd_mul_hub_ri(self): 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() # -3.5 from tcc.cc_solvers import residual_diis_solver from tcc.rccsd_mul import RCCSD_MUL_RI_HUB cc = RCCSD_MUL_RI_HUB(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.93833803927253978, 1e-6), True)
def test_rccsd_mul(self): from tcc.cc_solvers import classic_solver, residual_diis_solver from tcc.rccsd_mul import RCCSD_MUL cc = RCCSD_MUL(self.rhf) converged, energy, amps = classic_solver(cc, conv_tol_amps=1e-8, conv_tol_energy=1e-8) self.assertEqual(converged, True) self.assertEqual(np.allclose(energy, -0.049467456410677929, 1e-5), True) cc._converged = False converged, energy1, _ = residual_diis_solver(cc, amps=amps, conv_tol_energy=1e-10) self.assertEqual(np.allclose(energy, -0.049467456410677929, 1e-5), 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 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()