def test_derivatives(self): np.random.seed(1) las = LASSCF(mf, (4, ), (4, ), spin_sub=(1, )).set(max_cycle_macro=1, ah_level_shift=0).run() ugg = las.get_ugg() ci0_csf = np.random.rand(ugg.ncsf_sub[0][0]) ci0_csf /= np.linalg.norm(ci0_csf) ci0 = ugg.ci_transformers[0][0].vec_csf2det(ci0_csf) las_gorb, las_gci = las.get_grad(mo_coeff=mf.mo_coeff, ci=[[ci0]])[:2] las_grad = np.append(las_gorb, las_gci) las_hess = las.get_hop(ugg=ugg, mo_coeff=mf.mo_coeff, ci=[[ci0]]) self.assertAlmostEqual(lib.fp(las_grad), lib.fp(las_hess.get_grad()), 8) cas_grad, _, cas_hess, _ = newton_casscf.gen_g_hop( mc, mf.mo_coeff, ci0, mc.ao2mo(mf.mo_coeff)) _pack_ci, _unpack_ci = newton_casscf._pack_ci_get_H( mc, mf.mo_coeff, ci0)[-2:] def pack_cas(kappa, ci1): return np.append(mc.pack_uniq_var(kappa), _pack_ci(ci1)) def unpack_cas(x): return mc.unpack_uniq_var(x[:ugg.nvar_orb]), _unpack_ci( x[ugg.nvar_orb:]) def cas2las(y, mode='hx'): yorb, yci = unpack_cas(y) yc = yci[0].ravel().dot(ci0.ravel()) yci[0] -= yc * ci0 yorb *= (0.5 if mode == 'hx' else 1) return ugg.pack(yorb, [yci]) def las2cas(y, mode='x'): yorb, yci = ugg.unpack(y) yc = yci[0][0].ravel().dot(ci0.ravel()) yci[0][0] -= yc * ci0 yorb *= (0.5 if mode == 'x' else 1) return pack_cas(yorb, yci[0]) cas_grad = cas2las(cas_grad) self.assertAlmostEqual(lib.fp(las_grad), lib.fp(cas_grad), 8) x = np.random.rand(ugg.nvar_tot) # orb on input x_las = x.copy() x_las[ugg.nvar_orb:] = 0.0 x_cas = las2cas(x_las, mode='x') hx_las = las_hess._matvec(x_las) hx_cas = cas2las(cas_hess(x_cas), mode='x') self.assertAlmostEqual(lib.fp(hx_las), lib.fp(hx_cas), 8) # CI on input x_las = x.copy() x_las[:ugg.nvar_orb] = 0.0 x_cas = las2cas(x_las, mode='hx') hx_las = las_hess._matvec(x_las) hx_cas = cas2las(cas_hess(x_cas), mode='hx') self.assertAlmostEqual(lib.fp(hx_las), lib.fp(hx_cas), 8)
from mrh.my_pyscf.mcscf.lasscf_testing import LASSCF dr_nn = 2.0 mol = struct(dr_nn, dr_nn, '6-31g', symmetry=False) mol.verbose = lib.logger.DEBUG mol.output = '/dev/null' mol.spin = 0 mol.build() mf = scf.RHF(mol).run() las = LASSCF(mf, (4, 4), ((3, 1), (1, 3)), spin_sub=(3, 3)) las.max_cycle_macro = 1 las.kernel() las.mo_coeff = np.loadtxt('test_lasci_mo.dat') las.ci = [[np.loadtxt('test_lasci_ci0.dat')], [-np.loadtxt('test_lasci_ci1.dat').T]] ugg = las.get_ugg() h_op = las.get_hop(ugg=ugg) np.random.seed(0) x = np.random.rand(ugg.nvar_tot) def tearDownModule(): global mol, mf, las, ugg, h_op, x mol.stdout.close() del mol, mf, las, ugg, h_op, x class KnownValues(unittest.TestCase): def test_grad(self): gorb0, gci0, gx0 = las.get_grad(ugg=ugg) grad0 = np.append(gorb0, gci0)
import os mol = struct(2.0, 2.0, '6-31g', symmetry=False) mol.output = 'sa_lasscf_testing.log' mol.verbose = lib.logger.DEBUG mol.build() mf = scf.RHF(mol).run() mo_coeff = mf.mo_coeff.copy() las = LASSCF(mf, (4, 4), (4, 4), spin_sub=(1, 1)) mo_loc = las.localize_init_guess((list(range(3)), list(range(9, 12))), mo_coeff=mo_coeff) las.state_average_(weights=[0.5, 0.5], spins=[[0, 0], [2, -2]]) las.set(max_cycle_macro=1, max_cycle_micro=1, ah_level_shift=0).kernel() np.random.seed(1) ugg = las.get_ugg() ci0 = [np.random.rand(ncsf) for ncsf in ugg.ncsf_sub.ravel()] ci0 = [c / linalg.norm(c) for c in ci0] x0 = np.concatenate([ np.zeros(ugg.nvar_orb), ] + ci0) _, ci0_sa = ugg.unpack(x0) las.mo_coeff = mo_loc las.ci = ci0_sa hop = las.get_hop(ugg=ugg) ci0_sing = [[ci0_sa[0][0]], [ci0_sa[1][0]]] las_sing = LASSCF(mf, (4, 4), (4, 4), spin_sub=(1, 1)).set(mo_coeff=mo_loc, ci=ci0_sing) las_sing = las_sing.set(ah_level_shift=0, max_cycle_macro=1, max_cycle_micro=1).run()
def test_derivatives(self): np.random.seed(1) las = LASSCF(mf, (4, ), (4, ), spin_sub=(1, )).set(max_cycle_macro=1, ah_level_shift=0) las.state_average_(weights=[0.5, 0.5], charges=[0, 0], spins=[0, 2], smults=[1, 3]).run() ugg = las.get_ugg() ci0_csf = [np.random.rand(ncsf) for ncsf in ugg.ncsf_sub[0]] ci0_csf = [c / np.linalg.norm(c) for c in ci0_csf] ci0 = [ t.vec_csf2det(c) for t, c in zip(ugg.ci_transformers[0], ci0_csf) ] las_gorb, las_gci = las.get_grad(mo_coeff=mf.mo_coeff, ci=[ci0])[:2] las_grad = np.append(las_gorb, las_gci) las_hess = las.get_hop(ugg=ugg, mo_coeff=mf.mo_coeff, ci=[ci0]) self.assertAlmostEqual(lib.fp(las_grad), lib.fp(las_hess.get_grad()), 8) cas_grad, _, cas_hess, _ = newton_casscf.gen_g_hop( mc, mf.mo_coeff, ci0, mc.ao2mo(mf.mo_coeff)) _pack_ci, _unpack_ci = newton_casscf._pack_ci_get_H( mc, mf.mo_coeff, ci0)[-2:] def pack_cas(kappa, ci1): return np.append(mc.pack_uniq_var(kappa), _pack_ci(ci1)) def unpack_cas(x): return mc.unpack_uniq_var(x[:ugg.nvar_orb]), _unpack_ci( x[ugg.nvar_orb:]) def cas2las(y, mode='hx'): yorb, yci = unpack_cas(y) yci = [ 2 * (yc - c * c.ravel().dot(yc.ravel())) for c, yc in zip(ci0, yci) ] yorb *= (0.5 if mode == 'hx' else 1) return ugg.pack(yorb, [yci]) def las2cas(y, mode='x'): yorb, yci = ugg.unpack(y) yci = [ yc - c * c.ravel().dot(yc.ravel()) for c, yc in zip(ci0, yci[0]) ] yorb *= (0.5 if mode == 'x' else 1) return pack_cas(yorb, yci) cas_grad = cas2las(cas_grad) self.assertAlmostEqual(lib.fp(las_grad), lib.fp(cas_grad), 8) x = np.random.rand(ugg.nvar_tot) # orb on input x_las = x.copy() x_las[ugg.nvar_orb:] = 0.0 x_cas = las2cas(x_las, mode='x') hx_las = las_hess._matvec(x_las) hx_cas = cas2las(cas_hess(x_cas), mode='x') self.assertAlmostEqual(lib.fp(hx_las), lib.fp(hx_cas), 8) # CI on input x_las = x.copy() x_las[:ugg.nvar_orb] = 0.0 x_cas = las2cas(x_las, mode='hx') hx_las = las_hess._matvec(x_las) hx_cas = cas2las(cas_hess(x_cas), mode='hx') self.assertAlmostEqual(lib.fp(hx_las), lib.fp(hx_cas), 8)