|
| 1 | +# This file is part of PyCI. |
| 2 | +# |
| 3 | +# PyCI is free software: you can redistribute it and/or modify it under |
| 4 | +# the terms of the GNU General Public License as published by the Free |
| 5 | +# Software Foundation, either version 3 of the License, or (at your |
| 6 | +# option) any later version. |
| 7 | +# |
| 8 | +# PyCI is distributed in the hope that it will be useful, but WITHOUT |
| 9 | +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 11 | +# for more details. |
| 12 | +# |
| 13 | +# You should have received a copy of the GNU General Public License |
| 14 | +# along with PyCI. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +import numpy as np |
| 17 | +import pyci |
| 18 | +from scipy.special import comb |
| 19 | + |
| 20 | +import pytest |
| 21 | +from pyci.test import datafile |
| 22 | +from pyci.fanci import AP1roG |
| 23 | + |
| 24 | +from pyci.fanci.fanpt_wrapper import reduce_to_fock, solve_fanpt |
| 25 | +from pyci.fanpt import FANPTUpdater, FANPTContainerEParam, FANPTContainerEFree |
| 26 | + |
| 27 | +@pytest.mark.parametrize("filename, nocc, expected", [("he_ccpvqz", 1, -2.8868091056425156), |
| 28 | + ("be_ccpvdz", 2, -14.600556820761211), |
| 29 | + ("li2_ccpvdz", 3, -16.862861409549044), |
| 30 | + ("lih_sto6g", 2, -8.963531095653355), |
| 31 | + ("h2_631gdp", 1, -1.869682842154122), |
| 32 | + ("h2o_ccpvdz", 5, -77.96987451399201)]) |
| 33 | +def test_fanpt_e_free(filename, nocc, expected): |
| 34 | + nsteps = 10 |
| 35 | + order = 1 |
| 36 | + |
| 37 | + # Define ham0 and ham1 |
| 38 | + ham1 = pyci.hamiltonian(datafile("{0:s}.fcidump".format(filename))) |
| 39 | + ham0 = pyci.hamiltonian(ham1.ecore, ham1.one_mo, reduce_to_fock(ham1.two_mo)) |
| 40 | + |
| 41 | + # contruct empty fci wave function class instance from # of basis functions and occupation |
| 42 | + wfn0 = pyci.fullci_wfn(ham0.nbasis, nocc, nocc) |
| 43 | + wfn0.add_hartreefock_det() |
| 44 | + |
| 45 | + # initialize sparse matrix operator (hamiltonian into wave function) |
| 46 | + op = pyci.sparse_op(ham0, wfn0) |
| 47 | + |
| 48 | + # solve for the lowest eigenvalue and eigenvector |
| 49 | + e_hf, e_vecs0 = op.solve(n=1, tol=1.0e-8) |
| 50 | + |
| 51 | + # Get params as the solution of the fanci wfn with ham0 (last element will be the energy of the "ideal" system). |
| 52 | + nproj = int(comb(ham1.nbasis, nocc)) |
| 53 | + pyci_wfn = AP1roG(ham1, nocc, nproj=nproj) |
| 54 | + |
| 55 | + params = np.zeros(pyci_wfn.nparam, dtype=pyci.c_double) |
| 56 | + params[-1] = e_hf[0] |
| 57 | + |
| 58 | + fill = 'excitation' |
| 59 | + fanpt_results = solve_fanpt(pyci_wfn, ham0, pyci_wfn.ham, params, |
| 60 | + fill=fill, energy_active=False, resum=False, ref_sd=0, |
| 61 | + final_order=order, lambda_i=0.0, lambda_f=1.0, steps=nsteps, |
| 62 | + solver_kwargs={'mode':'lstsq', 'use_jac':True, 'xtol':1.0e-8, |
| 63 | + 'ftol':1.0e-8, 'gtol':1.0e-5, 'max_nfev':pyci_wfn.nparam, 'verbose':2}) |
| 64 | + |
| 65 | + assert np.allclose(fanpt_results.x[-1], expected) |
| 66 | + |
| 67 | +@pytest.mark.parametrize("filename, nocc, expected", [("he_ccpvqz", 1, -2.8868091056425156), |
| 68 | + ("be_ccpvdz", 2, -14.600556842700215), |
| 69 | + ("li2_ccpvdz", 3, -16.86286984124269), |
| 70 | + ("lih_sto6g", 2, -8.96353109432708), |
| 71 | + ("h2_631gdp", 1, -1.869682842154122), |
| 72 | + ("h2o_ccpvdz", 5, -77.96987516399848)]) |
| 73 | +def test_fanpt_e_param(filename, nocc, expected): |
| 74 | + nsteps = 10 |
| 75 | + order = 1 |
| 76 | + |
| 77 | + # Define ham0 and ham1 |
| 78 | + ham1 = pyci.hamiltonian(datafile("{0:s}.fcidump".format(filename))) |
| 79 | + ham0 = pyci.hamiltonian(ham1.ecore, ham1.one_mo, reduce_to_fock(ham1.two_mo)) |
| 80 | + |
| 81 | + # contruct empty fci wave function class instance from # of basis functions and occupation |
| 82 | + wfn0 = pyci.fullci_wfn(ham0.nbasis, nocc, nocc) |
| 83 | + wfn0.add_hartreefock_det() |
| 84 | + |
| 85 | + # initialize sparse matrix operator (hamiltonian into wave function) |
| 86 | + op = pyci.sparse_op(ham0, wfn0) |
| 87 | + |
| 88 | + # solve for the lowest eigenvalue and eigenvector |
| 89 | + e_hf, e_vecs0 = op.solve(n=1, tol=1.0e-8) |
| 90 | + |
| 91 | + # Get params as the solution of the fanci wfn with ham0 (last element will be the energy of the "ideal" system). |
| 92 | + nproj = int(comb(ham1.nbasis, nocc)) |
| 93 | + pyci_wfn = AP1roG(ham1, nocc, nproj=nproj) |
| 94 | + |
| 95 | + params = np.zeros(pyci_wfn.nparam, dtype=pyci.c_double) |
| 96 | + params[-1] = e_hf[0] |
| 97 | + |
| 98 | + fill = 'excitation' |
| 99 | + fanpt_results = solve_fanpt(pyci_wfn, ham0, pyci_wfn.ham, params, |
| 100 | + fill=fill, energy_active=True, resum=False, ref_sd=0, |
| 101 | + final_order=order, lambda_i=0.0, lambda_f=1.0, steps=nsteps, |
| 102 | + solver_kwargs={'mode':'lstsq', 'use_jac':True, 'xtol':1.0e-8, |
| 103 | + 'ftol':1.0e-8, 'gtol':1.0e-5, 'max_nfev':pyci_wfn.nparam, 'verbose':2}) |
| 104 | + |
| 105 | + assert np.allclose(fanpt_results.x[-1], expected) |
0 commit comments