Exemplo n.º 1
0
class TestInterpolate(unittest.TestCase):

    def setUp(self):

        # molecule
        self.mol = Molecule(
            atom='H 0 0 -0.69; H 0 0 0.69',
            unit='bohr',
            calculator='pyscf',
            basis='dzp')

        # wave function
        self.wf = SlaterJastrow(self.mol, kinetic='jacobi',
                                configs='single(2,2)')

        npts = 51
        self.pos = torch.zeros(npts, 6)
        self.pos[:, 2] = torch.linspace(-2, 2, npts)

    def test_ao(self):

        interp_ao = InterpolateAtomicOrbitals(self.wf)
        inter = interp_ao(self.pos)
        ref = self.wf.ao(self.pos)
        delta = (inter - ref).abs().mean()
        assert(delta < 0.1)

    def test_mo_reg(self):

        interp_mo = InterpolateMolecularOrbitals(self.wf)
        inter = interp_mo(self.pos, method='reg')
        ref = self.wf.mo(self.wf.mo_scf(self.wf.ao(self.pos)))
        delta = (inter - ref).abs().mean()
        assert(delta < 0.1)

    def test_mo_irreg(self):

        interp_mo = InterpolateMolecularOrbitals(self.wf)
        inter = interp_mo(self.pos, method='irreg')
        ref = self.wf.mo(self.wf.mo_scf(self.wf.ao(self.pos)))
        delta = (inter - ref).abs().mean()
        assert(delta < 0.1)
Exemplo n.º 2
0
class TestMOvaluesADF(unittest.TestCase):

    def setUp(self):

        # define the molecule
        path_hdf5 = (
            PATH_TEST / 'hdf5/C_adf_dzp.hdf5').absolute().as_posix()
        self.mol = Molecule(load=path_hdf5)

        # define the wave function
        self.wf = SlaterJastrow(self.mol, include_all_mo=True)

        # define the grid points
        self.npts = 21
        pts = get_pts(self.npts)

        self.pos = 10 * torch.ones(self.npts ** 2, self.mol.nelec * 3)
        self.pos[:, :3] = pts
        self.pos = Variable(self.pos)
        self.pos.requires_grad = True

    def test_mo(self):

        movals = self.wf.mo_scf(self.wf.ao(self.pos)).detach().numpy()

        for iorb in range(self.mol.basis.nmo):
            path_cube = PATH_TEST / f'cube/C_MO_%SCF_A%{iorb + 1}.cub'
            fname = path_cube.absolute().as_posix()
            adf_ref_data = np.array(read_cubefile(
                fname)).reshape(self.npts, self.npts)**2
            qmctorch_data = (movals[:, 0, iorb]).reshape(
                self.npts, self.npts)**2

            delta = np.abs(adf_ref_data - qmctorch_data)

            if __PLOT__:
                plt.subplot(1, 3, 1)
                plt.imshow(adf_ref_data)

                plt.subplot(1, 3, 2)
                plt.imshow(qmctorch_data)

                plt.subplot(1, 3, 3)
                plt.imshow(delta)
                plt.show()

            # the 0,0 point is much larger due to num instabilities
            delta = np.sort(delta.flatten())
            delta = delta[:-1]
            assert(delta.mean() < 1E-3)
Exemplo n.º 3
0
class TestAOvaluesADF(unittest.TestCase):
    def setUp(self):

        # define the molecule
        path_hdf5 = (PATH_TEST / 'hdf5/C_adf_dzp.hdf5').absolute().as_posix()
        self.mol = Molecule(load=path_hdf5)

        # define the wave function
        self.wf = SlaterJastrow(self.mol, include_all_mo=True)

        # define the grid points
        self.npts = 21
        pts = get_pts(self.npts)

        self.pos = torch.zeros(self.npts**2, self.mol.nelec * 3)
        self.pos[:, :3] = pts
        self.pos = Variable(self.pos)
        self.pos.requires_grad = True

    def test_ao(self):

        aovals = self.wf.ao(self.pos).detach().numpy()

        for iorb in range(self.mol.basis.nao):

            path_cube = PATH_TEST / f'cube/C_AO_%Basis%AO{iorb}.cub'
            fname = path_cube.absolute().as_posix()
            adf_ref_data = np.array(read_cubefile(fname)).reshape(
                self.npts, self.npts)
            qmctorch_data = (aovals[:, 0, iorb]).reshape(self.npts, self.npts)

            delta = np.abs(adf_ref_data - qmctorch_data)

            if __PLOT__:
                plt.subplot(1, 3, 1)
                plt.imshow(adf_ref_data)

                plt.subplot(1, 3, 2)
                plt.imshow(qmctorch_data)

                plt.subplot(1, 3, 3)
                plt.imshow(delta)
                plt.show()

            assert (delta.mean() < 1E-3)
Exemplo n.º 4
0
class TestAOvaluesPyscf(unittest.TestCase):
    def setUp(self):

        # define the molecule
        at = 'C 0 0 0'
        basis = 'dzp'
        self.mol = Molecule(atom=at,
                            calculator='pyscf',
                            basis=basis,
                            unit='bohr')

        self.m = gto.M(atom=at, basis=basis, unit='bohr')

        # define the wave function
        self.wf = SlaterJastrow(self.mol)

        self.pos = torch.zeros(100, self.mol.nelec * 3)

        self.pos[:, 0] = torch.linspace(-5, 5, 100)
        self.pos[:, 1] = torch.linspace(-5, 5, 100)
        self.pos[:, 2] = torch.linspace(-5, 5, 100)

        self.pos = Variable(self.pos)
        self.pos.requires_grad = True

        self.x = self.pos[:, 0].detach().numpy()

    def test_ao(self):

        nzlm = np.linalg.norm(self.m.cart2sph_coeff(), axis=1)

        aovals = self.wf.ao(self.pos).detach().numpy() / nzlm
        aovals_ref = self.m.eval_ao('GTOval_cart',
                                    self.pos.detach().numpy()[:, :3])

        for iorb in range(self.mol.basis.nao):

            if __PLOT__:

                plt.plot(self.x, aovals[:, 0, iorb])
                plt.plot(self.x, aovals_ref[:, iorb])
                plt.show()

            assert np.allclose(aovals[:, 0, iorb], aovals_ref[:, iorb])

    def test_ao_deriv(self):

        nzlm = np.linalg.norm(self.m.cart2sph_coeff(), axis=1)

        daovals = self.wf.ao(self.pos, derivative=1).detach().numpy() / nzlm

        daovals_ref = self.m.eval_gto('GTOval_ip_cart',
                                      self.pos.detach().numpy()[:, :3])
        daovals_ref = daovals_ref.sum(0)

        for iorb in range(self.mol.basis.nao):

            if __PLOT__:
                plt.plot(self.x, daovals[:, 0, iorb])
                plt.plot(self.x, daovals_ref[:, iorb])
                plt.show()

            assert np.allclose(daovals[:, 0, iorb], daovals_ref[:, iorb])
Exemplo n.º 5
0
class TestAOderivativesPyscf(unittest.TestCase):
    def setUp(self):

        torch.manual_seed(101)
        np.random.seed(101)

        # define the molecule
        at = 'Li 0 0 0; H 0 0 1'
        basis = 'dzp'
        self.mol = Molecule(atom=at,
                            calculator='pyscf',
                            basis=basis,
                            unit='bohr')

        self.m = gto.M(atom=at, basis=basis, unit='bohr')

        # define the wave function
        self.wf = SlaterJastrow(self.mol, include_all_mo=True)

        # define the grid points
        npts = 11
        self.pos = torch.rand(npts, self.mol.nelec * 3)
        self.pos = Variable(self.pos)
        self.pos.requires_grad = True

    def test_ao_deriv(self):

        ao = self.wf.ao(self.pos)
        dao = self.wf.ao(self.pos, derivative=1)

        dao_grad = grad(ao, self.pos, grad_outputs=torch.ones_like(ao))[0]

        gradcheck(self.wf.ao, self.pos)
        assert (torch.allclose(dao.sum(), dao_grad.sum()))

    def test_ao_grad_sum(self):

        ao = self.wf.ao(self.pos)
        dao_sum = self.wf.ao(self.pos, derivative=1, sum_grad=True)
        dao = self.wf.ao(self.pos, derivative=1, sum_grad=False)

        assert (torch.allclose(dao_sum, dao.sum(-1)))

    def test_ao_hess(self):

        ao = self.wf.ao(self.pos)
        d2ao = self.wf.ao(self.pos, derivative=2)
        d2ao_grad = hess(ao, self.pos)
        assert (torch.allclose(d2ao.sum(), d2ao_grad.sum()))

    def test_ao_hess_sum(self):

        ao = self.wf.ao(self.pos)
        d2ao_sum = self.wf.ao(self.pos, derivative=2, sum_hess=True)
        d2ao = self.wf.ao(self.pos, derivative=2, sum_hess=False)
        assert (torch.allclose(d2ao_sum, d2ao.sum(-1)))

    def test_ao_mixed_der(self):
        ao = self.wf.ao(self.pos)
        d2ao = self.wf.ao(self.pos, derivative=3)
        d2ao_auto = hess_mixed_terms(ao, self.pos)

        assert (torch.allclose(d2ao.sum(), d2ao_auto.sum()))

    def test_ao_all(self):
        ao = self.wf.ao(self.pos)
        dao = self.wf.ao(self.pos, derivative=1, sum_grad=False)
        d2ao = self.wf.ao(self.pos, derivative=2)
        ao_all, dao_all, d2ao_all = self.wf.ao(self.pos, derivative=[0, 1, 2])

        assert (torch.allclose(ao, ao_all))
        assert (torch.allclose(dao, dao_all))
        assert (torch.allclose(d2ao, d2ao_all))
Exemplo n.º 6
0
class TestCompareSlaterJastrowOrbitalDependentBackFlow(unittest.TestCase):
    def setUp(self):

        torch.manual_seed(101)
        np.random.seed(101)

        set_torch_double_precision()

        # molecule
        mol = Molecule(atom='Li 0 0 0; H 0 0 3.015',
                       unit='bohr',
                       calculator='pyscf',
                       basis='sto-3g',
                       redo_scf=True)

        self.wf = SlaterJastrowBackFlow(mol,
                                        kinetic='jacobi',
                                        include_all_mo=True,
                                        configs='single_double(2,2)',
                                        backflow_kernel=BackFlowKernelInverse,
                                        orbital_dependent_backflow=True)

        for ker in self.wf.ao.backflow_trans.backflow_kernel.orbital_dependent_kernel:
            ker.weight.data *= 0

        self.wf_ref = SlaterJastrow(mol,
                                    kinetic='jacobi',
                                    include_all_mo=True,
                                    configs='single_double(2,2)')

        self.random_fc_weight = torch.rand(self.wf.fc.weight.shape)
        self.wf.fc.weight.data = self.random_fc_weight
        self.wf_ref.fc.weight.data = self.random_fc_weight

        self.nbatch = 5
        self.pos = torch.Tensor(np.random.rand(self.nbatch, self.wf.nelec * 3))
        self.pos.requires_grad = True

    def test_forward(self):
        """Check that backflow give same results as normal SlaterJastrow."""
        wf_val = self.wf(self.pos)
        wf_val_ref = self.wf_ref(self.pos)

        assert (torch.allclose(wf_val, wf_val_ref))

    def test_jacobian_mo(self):
        """Check that backflow give same results as normal SlaterJastrow."""

        dmo = self.wf.pos2mo(self.pos, derivative=1)
        dmo_ref = self.wf_ref.pos2mo(self.pos, derivative=1)
        assert (torch.allclose(dmo.sum(0), dmo_ref))

    def test_hess_mo(self):
        """Check that backflow give same results as normal SlaterJastrow."""
        d2ao = self.wf.ao(self.pos, derivative=2, sum_hess=False)
        d2val = self.wf.ao2mo(d2ao)

        d2ao_ref = self.wf_ref.ao(self.pos, derivative=2, sum_hess=True)
        d2val_ref = self.wf_ref.ao2mo(d2ao_ref)
        assert (torch.allclose(d2val_ref, d2val.sum(0)))

    def test_grad_wf(self):
        pass

    def test_local_energy(self):

        self.wf.kinetic_energy = self.wf.kinetic_energy_jacobi
        eloc_jac = self.wf.local_energy(self.pos)

        self.wf_ref.kinetic_energy = self.wf_ref.kinetic_energy_jacobi
        eloc_jac_ref = self.wf_ref.local_energy(self.pos)

        assert torch.allclose(eloc_jac_ref.data,
                              eloc_jac.data,
                              rtol=1E-4,
                              atol=1E-4)

    def test_kinetic_energy(self):

        ejac_ref = self.wf_ref.kinetic_energy_jacobi(self.pos)
        ejac = self.wf.kinetic_energy_jacobi(self.pos)

        assert torch.allclose(ejac_ref.data, ejac.data, rtol=1E-4, atol=1E-4)
Exemplo n.º 7
0
class TestAOderivativesADF(unittest.TestCase):
    def setUp(self):

        # define the molecule
        path_hdf5 = PATH_TEST / 'hdf5/C_adf_dzp.hdf5'
        self.mol = Molecule(load=path_hdf5)

        # define the wave function
        self.wf = SlaterJastrow(self.mol, include_all_mo=True)

        # define the grid points
        npts = 11
        self.pos = torch.rand(npts, self.mol.nelec * 3)
        self.pos = Variable(self.pos)
        self.pos.requires_grad = True

    def test_ao_deriv(self):

        ao = self.wf.ao(self.pos)
        dao = self.wf.ao(self.pos, derivative=1)
        dao_grad = grad(ao, self.pos, grad_outputs=torch.ones_like(ao))[0]

        gradcheck(self.wf.ao, self.pos)
        assert (torch.allclose(dao.sum(), dao_grad.sum()))

    def test_ao_grad_sum(self):

        ao = self.wf.ao(self.pos)
        dao_sum = self.wf.ao(self.pos, derivative=1, sum_grad=True)
        dao = self.wf.ao(self.pos, derivative=1, sum_grad=False)

        assert (torch.allclose(dao_sum, dao.sum(-1)))

    def test_ao_hess(self):

        ao = self.wf.ao(self.pos)
        d2ao = self.wf.ao(self.pos, derivative=2)
        d2ao_grad = hess(ao, self.pos)
        assert (torch.allclose(d2ao.sum(), d2ao_grad.sum()))

    def test_ao_hess_sum(self):

        ao = self.wf.ao(self.pos)
        d2ao_sum = self.wf.ao(self.pos, derivative=2, sum_hess=True)
        d2ao = self.wf.ao(self.pos, derivative=2, sum_hess=False)
        assert (torch.allclose(d2ao_sum, d2ao.sum(-1)))

    def test_ao_mixed_der(self):
        ao = self.wf.ao(self.pos)
        d2ao = self.wf.ao(self.pos, derivative=3)
        d2ao_auto = hess_mixed_terms(ao, self.pos)
        assert (torch.allclose(d2ao.sum(), d2ao_auto.sum()))