Python numpy.testing.assert_allclose() Examples

The following are 30 code examples of numpy.testing.assert_allclose(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module numpy.testing , or try the search function .
Example #1
Source File: test_rhf_slow.py    From pyscf with Apache License 2.0 6 votes vote down vote up
def test_cplx(self):
        """Tests whether complex conjugation is handled correctly."""
        # Perform mf calculation
        model_rhf = RHF(self.cell)
        model_rhf.kernel()

        # Add random phases
        import numpy
        numpy.random.seed(0)
        p = numpy.exp(2.j * numpy.pi * numpy.random.rand(model_rhf.mo_coeff.shape[1]))
        model_rhf.mo_coeff = model_rhf.mo_coeff * p[numpy.newaxis, :]

        m_ref = PhysERI(model_rhf).tdhf_full_form()

        td_model_rhf = TDRHF(model_rhf)
        assert not td_model_rhf.fast
        td_model_rhf.kernel()
        with self.assertRaises(ValueError):
            td_model_rhf.fast = True
            td_model_rhf.kernel()

        self.assertIsInstance(td_model_rhf.eri, PhysERI4)
        m = td_model_rhf.eri.tdhf_full_form()

        testing.assert_allclose(m, m_ref, atol=1e-14) 
Example #2
Source File: test.py    From pyscf with Apache License 2.0 6 votes vote down vote up
def test_iter_s(self):
        """CCS iterations."""
        e1, t1 = cc.kernel_ground_state_s(self.ccsd)
        # # TODO: MRCC energy is way off expected
        # testing.assert_allclose(self.mf.e_tot + e1, -74.841686696943, atol=1e-4)

        import pyscf.cc
        mf = scf.RHF(self.mol).set(conv_tol = 1e-11).run()
        cc1 = pyscf.cc.UCCSD(mf, frozen=1)
        old_update_amps = cc1.update_amps
        def update_amps(t1, t2, eris):
            t1, t2 = old_update_amps(t1, t2, eris)
            return t1, (t2[0]*0, t2[1]*0, t2[2]*0)
        cc1.update_amps = update_amps
        cc1.kernel()
        testing.assert_allclose(self.mf.e_tot + e1, cc1.e_tot, atol=1e-4) 
Example #3
Source File: test_kproxy_supercell_hf.py    From pyscf with Apache License 2.0 6 votes vote down vote up
def test_class(self):
        """Tests container behavior."""
        model = kproxy_supercell.TDProxy(self.model_krhf, "hf", [self.k, 1, 1], density_fitting_hf)
        model.nroots = self.td_model_krhf.nroots
        assert not model.fast
        model.kernel()
        testing.assert_allclose(model.e, self.td_model_krhf.e, atol=1e-5)
        # Test real
        testing.assert_allclose(model.e.imag, 0, atol=1e-8)

        nocc = nvirt = 4
        testing.assert_equal(model.xy.shape, (len(model.e), 2, self.k, self.k, nocc, nvirt))

        # Test only non-degenerate roots
        d = abs(model.e[1:] - model.e[:-1]) < 1e-8
        d = numpy.logical_or(numpy.concatenate(([False], d)), numpy.concatenate((d, [False])))
        d = numpy.logical_not(d)
        assert_vectors_close(self.td_model_krhf.xy[d], model.xy[d], atol=1e-5) 
Example #4
Source File: test_krhf_slow.py    From pyscf with Apache License 2.0 6 votes vote down vote up
def test_eri_gamma(self):
        """Tests all ERI implementations: with and without symmetries (gamma-point only)."""
        for eri in (ktd.PhysERI, ktd.PhysERI4, ktd.PhysERI8):
            if eri is not ktd.PhysERI8 or self.test8:
                try:
                    e = eri(self.model_krhf)
                    m = e.tdhf_full_form(0)

                    # Test matrix vs ref
                    testing.assert_allclose(m, retrieve_m_khf(e, 0), atol=1e-11)

                    # Test matrix vs pyscf
                    testing.assert_allclose(self.ref_m_gamma, m, atol=1e-12)
                except Exception:
                    print("When testing {} the following exception occurred:".format(eri))
                    raise 
Example #5
Source File: test.py    From pyscf with Apache License 2.0 6 votes vote down vote up
def test_iter_d(self):
        """CCD iterations."""
        e2, t2 = cc.kernel_ground_state_d(self.ccsd)
        # # TODO: MRCC energy is way off expected
        # testing.assert_allclose(self.mf.e_tot + e2, -76.177931897355, atol=1e-4)

        import pyscf.cc
        cc1 = scf.RHF(self.mol).run(conv_tol = 1e-11).apply(pyscf.cc.CCSD)
        cc1.frozen = 1
        old_update_amps = cc1.update_amps
        def update_amps(t1, t2, eris):
            t1, t2 = old_update_amps(t1, t2, eris)
            return t1*0, t2
        cc1.update_amps = update_amps
        cc1.kernel()
        testing.assert_allclose(self.mf.e_tot + e2, cc1.e_tot, atol=1e-4) 
Example #6
Source File: test_kproxy_supercell_hf.py    From pyscf with Apache License 2.0 6 votes vote down vote up
def test_eri(self):
        """Tests ERI."""
        for frozen in (1, [0, 1], [0, -1]):
            try:
                e = kproxy_supercell.PhysERI(self.model_krhf, "hf", [self.k, 1, 1], density_fitting_hf, frozen=frozen)
                m = e.tdhf_full_form()

                ref_e = krhf_slow_supercell.PhysERI4(self.model_krhf, frozen=frozen)
                ref_m = ref_e.tdhf_full_form()

                # Test matrix vs ref
                testing.assert_allclose(m, ref_m, atol=1e-11)

            except Exception:
                print("When testing class with frozen={} the following exception occurred:".format(repr(frozen)))
                raise 
Example #7
Source File: test_rhf_slow.py    From pyscf with Apache License 2.0 6 votes vote down vote up
def test_eri(self):
        """Tests all ERI implementations: with and without symmetries."""
        for eri in (PhysERI, PhysERI4, PhysERI8):
            try:
                e = eri(self.model_rhf)
                m = e.tdhf_full_form()

                # Test matrix vs ref
                testing.assert_allclose(m, retrieve_m_hf(e), atol=1e-14)

                # Test matrix vs pyscf
                testing.assert_allclose(self.ref_m_rhf, m, atol=1e-14)
                vals, vecs = eig(m, nroots=self.td_model_rhf.nroots)
                testing.assert_allclose(vals, self.td_model_rhf.e, atol=1e-5)
            except Exception:
                print("When testing {} the following exception occurred:".format(eri))
                raise 
Example #8
Source File: test_kproxy_hf.py    From pyscf with Apache License 2.0 6 votes vote down vote up
def test_class(self):
        """Tests container behavior."""
        model = kproxy.TDProxy(self.model_krhf, "hf", [self.k, 1, 1], density_fitting_hf)
        model.nroots = self.td_model_krhf.nroots
        assert not model.fast
        model.kernel()
        for i in range(self.k):
            try:
                testing.assert_allclose(model.e[i], self.td_model_krhf.e[i], atol=1e-5)
                # Test real
                testing.assert_allclose(model.e[i].imag, 0, atol=1e-8)

                nocc = nvirt = 4
                testing.assert_equal(model.xy[i].shape, (len(model.e[i]), 2, self.k, nocc, nvirt))

                assert_vectors_close(self.td_model_krhf.xy[i], model.xy[i], atol=1e-5)

            except Exception:
                print("When testing model @k={:d} the following exception occurred:".format(i))
                raise 
Example #9
Source File: test_rhf_slow.py    From pyscf with Apache License 2.0 6 votes vote down vote up
def test_class(self):
        """Tests container behavior."""
        model = TDRHF(self.model_rhf)
        model.nroots = self.td_model_rhf.nroots
        assert model.fast
        e, xy = model.kernel()
        model.fast = False
        model.kernel()
        # Slow vs fast
        testing.assert_allclose(model.e, e)
        assert_vectors_close(model.xy, xy)
        # ... vs ref
        testing.assert_allclose(model.e, self.td_model_rhf.e, atol=1e-12)
        assert_vectors_close(model.xy, self.td_model_rhf.xy, atol=1e-12)
        # Test real
        testing.assert_allclose(model.e.imag, 0, atol=1e-8) 
Example #10
Source File: test_krhf_slow_gamma.py    From pyscf with Apache License 2.0 6 votes vote down vote up
def test_eri(self):
        """Tests all ERI implementations: with and without symmetries."""
        for eri in (ktd.PhysERI, ktd.PhysERI4, ktd.PhysERI8):
            # Note that specific combintation of k-points results in real orbitals and allows testing PhysERI8
            try:
                e = eri(self.model_krhf)
                m = e.tdhf_full_form()

                # Test matrix vs ref
                testing.assert_allclose(m, retrieve_m_hf(e), atol=1e-11)

                # Test matrix vs pyscf
                testing.assert_allclose(self.ref_m, m, atol=1e-5)
            except Exception:
                print("When testing {} the following exception occurred:".format(eri))
                raise 
Example #11
Source File: test_krhf_slow_supercell.py    From pyscf with Apache License 2.0 6 votes vote down vote up
def test_eri(self):
        """Tests all ERI implementations: with and without symmetries."""
        for eri in (ktd.PhysERI, ktd.PhysERI4, ktd.PhysERI8):
            if not eri == ktd.PhysERI8 or self.test8:
                try:
                    e = eri(self.model_krhf)
                    m = e.tdhf_full_form()

                    # Test matrix vs ref
                    testing.assert_allclose(m, retrieve_m_hf(e), atol=1e-11)

                    # Test matrix vs pyscf
                    testing.assert_allclose(self.ref_m, m[numpy.ix_(self.ov_order, self.ov_order)], atol=1e-5)
                except Exception:
                    print("When testing {} the following exception occurred:".format(eri))
                    raise 
Example #12
Source File: test_rhf_slow.py    From pyscf with Apache License 2.0 6 votes vote down vote up
def test_conversion(self):
        """Tests common conversions."""
        e = PhysERI(self.model_rhf)

        full = e.tdhf_full_form()
        a, b = e.tdhf_ab_form()
        mk, k = e.tdhf_mk_form()

        testing.assert_allclose(full, ab2full(a, b))
        testing.assert_allclose(full, mkk2full(mk, k), atol=1e-13)

        _a, _b = full2ab(full)
        testing.assert_allclose(a, _a)
        testing.assert_allclose(b, _b)
        _a, _b = mkk2ab(mk, k)
        testing.assert_allclose(a, _a)
        testing.assert_allclose(b, _b)

        _mk, _k = full2mkk(full)
        testing.assert_allclose(mk, _mk)
        testing.assert_allclose(k, _k)
        _mk, _k = ab2mkk(a, b)
        testing.assert_allclose(mk, _mk)
        testing.assert_allclose(k, _k) 
Example #13
Source File: test_rhf_slow.py    From pyscf with Apache License 2.0 6 votes vote down vote up
def test_class(self):
        """Tests container behavior."""
        model = TDRHF(self.model_rhf)
        model.nroots = self.td_model_rhf.nroots
        assert model.fast
        e, xy = model.kernel()
        model.fast = False
        model.kernel()
        # Slow vs fast
        testing.assert_allclose(model.e, e)
        assert_vectors_close(model.xy, xy)
        # ... vs ref
        testing.assert_allclose(model.e, self.td_model_rhf.e, atol=1e-5)
        assert_vectors_close(model.xy, self.td_model_rhf.xy, atol=1e-2)
        # Test real-valued
        testing.assert_allclose(model.e.imag, 0, atol=1e-8) 
Example #14
Source File: test_gw.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def test_gw(self):
        """Tests container behavior (proxy)."""
        eri = TDRHF_slow(self.model_rks).ao2mo()
        assert eri is not None
        gw_slow = GW_slow(self.td_proxy_model, eri=eri)
        gw_slow.kernel()
        # print(self.gw.mo_energy)

        testing.assert_allclose(gw_slow.mo_energy, self.gw.mo_energy, atol=1e-6) 
Example #15
Source File: test.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def _test_iter_sdt(self):
        """CCSDT iterations."""
        e3, t1, t2, t3 = cc.kernel_ground_state_sdt(self.ccsd)
        testing.assert_allclose(self.mf.e_tot + e3, -74.829163218204, atol=1e-4) 
Example #16
Source File: test_gw.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def test_imds_frozen(self):
        """Tests intermediates: frozen vs non-frozen."""
        td = TDRHF_slow(self.model_rhf, frozen=1)
        td.nroots = self.td_model_rhf_slow.nroots
        td.kernel()

        adjust_td_phase(self.td_model_rhf_slow, td)

        gw_slow_ref = GW_slow(self.td_model_rhf_slow)
        gw_slow_ref.kernel()

        gw_slow_frozen = GW_slow(td)
        gw_slow_frozen.kernel()

        testing.assert_allclose(gw_slow_ref.imds.tdm[..., 1:, 1:], gw_slow_frozen.imds.tdm, atol=1e-4)

        test_energies = numpy.linspace(-2, 3, 300)
        ref_samples = numpy.array(tuple(gw_slow_ref.imds.get_sigma_element(i, 1, 0.01) for i in test_energies))
        frozen_samples = numpy.array(tuple(gw_slow_frozen.imds.get_sigma_element(i, 0, 0.01) for i in test_energies))

        testing.assert_allclose(ref_samples, frozen_samples, atol=5e-4)
        testing.assert_allclose(
            gw_slow_ref.imds.get_rhs(1),
            gw_slow_frozen.imds.get_rhs(0),
            atol=1e-13,
        ) 
Example #17
Source File: test.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def test_iter_sd(self):
        """CCSD iterations."""
        e2, t1, t2 = cc.kernel_ground_state_sd(self.ccsd)
        testing.assert_allclose(self.mf.e_tot + e2, -74.819248718982, atol=1e-4) 
Example #18
Source File: test_gw.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def setUpClass(cls):
        cls.mol = mol = Mole()
        mol.atom = [
            [8, (0., 0., 0.)],
            [1, (0., -0.757, 0.587)],
            [1, (0., 0.757, 0.587)]]

        mol.basis = 'cc-pvdz'
        mol.verbose = 5
        mol.build()

        cls.model_rks = model_rks = RKS(mol)
        # model_rks.xc = 'hf'
        model_rks.kernel()

        cls.td_model_rks = td_model_rks = TDRKS(model_rks)
        td_model_rks.nroots = 4
        td_model_rks.kernel()

        cls.td_proxy_model = td_proxy_model = TDProxy(model_rks, "dft")
        td_proxy_model.nroots = td_model_rks.nroots
        td_proxy_model.kernel()

        testing.assert_allclose(td_model_rks.e, td_proxy_model.e, atol=1e-6)

        cls.gw = gw = GW(model_rks, td_model_rks)
        gw.kernel() 
Example #19
Source File: test_gw.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def test_gw_frozen(self):
        """Tests container behavior (frozen TDHF)."""
        td = TDRHF_slow(self.model_rhf, frozen=1)
        td.nroots = self.td_model_rhf_slow.nroots
        td.kernel()
        adjust_td_phase(self.td_model_rhf_slow, td)

        gw_slow_ref = GW_slow(self.td_model_rhf_slow)
        gw_slow_ref.kernel()
        gw_slow_frozen = GW_slow(td)
        gw_slow_frozen.kernel()

        testing.assert_allclose(gw_slow_frozen.mo_energy, gw_slow_ref.mo_energy[1:], atol=1e-4) 
Example #20
Source File: test_kproxy_supercell_ks.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def test_class(self):
        """Tests container behavior."""
        model = kproxy_supercell.TDProxy(self.model_krks, "dft", [self.k, 1, 1], KRKS)
        model.nroots = self.td_model_rks.nroots
        assert not model.fast
        model.kernel()
        testing.assert_allclose(model.e, self.td_model_rks.e, atol=1e-5)
        if self.k == 2:
            vecs = model.xy.reshape(len(model.xy), -1)[:, self.ov_order]
            # A loose tolerance here because of a low plane-wave cutoff
            assert_vectors_close(vecs, numpy.array(self.td_model_rks.xy).squeeze(), atol=1e-3)
        # Test real
        testing.assert_allclose(model.e.imag, 0, atol=1e-8) 
Example #21
Source File: test.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def test_ip_sd(self):
        """CCSD EOM iterations (IP)."""
        values, vectors = cc.kernel_ip_sd(self.ccsd, self.ccsd.t1, self.ccsd.t2, nroots=self.nroots)

        testing.assert_allclose(values, self.eomip.e, atol=1e-12) 
Example #22
Source File: test.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def test_lambda_sd(self):
        """CCSD lambda iterations."""
        l1, l2 = cc.kernel_lambda_sd(self.ccsd, self.ccsd.t1, self.ccsd.t2)

        testing.assert_allclose(l1, self.ccsd.l1, atol=1e-8)
        testing.assert_allclose(l2, self.ccsd.l2, atol=1e-8) 
Example #23
Source File: test.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def test_iter_sdt(self):
        """CCSDT iterations (there are no triple excitations for a 2-electron system)."""
        e3, t1, t2, t3 = cc.kernel_ground_state_sdt(self.ccsd)

        testing.assert_allclose(e3, self.ccsd.e_corr, atol=1e-8)
        testing.assert_allclose(t1, self.ccsd.t1, atol=1e-8)
        testing.assert_allclose(t2, self.ccsd.t2, atol=1e-7)
        testing.assert_allclose(t3, 0, atol=1e-8) 
Example #24
Source File: test.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def test_iter_sd(self):
        """CCSD iterations."""
        e2, t1, t2 = cc.kernel_ground_state_sd(self.ccsd)

        testing.assert_allclose(e2, self.ccsd.e_corr, atol=1e-8)
        testing.assert_allclose(t1, self.ccsd.t1, atol=1e-8)
        testing.assert_allclose(t2, self.ccsd.t2, atol=1e-7) 
Example #25
Source File: test.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def test_iter_s(self):
        """CCS iterations."""
        e1, t1 = cc.kernel_ground_state_s(self.ccsd)

        testing.assert_allclose(e1, 0, atol=1e-8)
        # TODO: atol=1e-8 does not work
        testing.assert_allclose(t1, 0, atol=1e-6) 
Example #26
Source File: test_proxy.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def test_eri(self):
        """Tests all ERI implementations: with and without symmetries."""
        e = PhysERI(self.model_rhf, "hf")
        m = e.tdhf_full_form()

        # Test matrix vs pyscf
        testing.assert_allclose(self.ref_m, m, atol=1e-14) 
Example #27
Source File: test_proxy.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def test_class(self):
        """Tests container behavior."""
        model = TDProxy(self.model_rks, "dft")
        model.nroots = self.td_model_rks.nroots
        model.kernel()
        testing.assert_allclose(model.e, self.td_model_rks.e, atol=1e-12)
        assert_vectors_close(model.xy, self.td_model_rks.xy, atol=1e-12)
        # Test real
        testing.assert_allclose(model.e.imag, 0, atol=1e-8) 
Example #28
Source File: test_proxy.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def test_eri(self):
        """Tests all ERI implementations: with and without symmetries."""
        e = PhysERI(self.model_rks, "dft")
        m = e.tdhf_full_form()

        # Test matrix vs pyscf
        testing.assert_allclose(self.ref_m, m, atol=1e-14) 
Example #29
Source File: test_common.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def _test_m_complex_support(model, phase=None, **kwargs):
    if phase is None:
        phase = numpy.exp(2.j * numpy.pi * 0.2468) * 1.3579
    m1 = retrieve_m(model, **kwargs)
    m2 = retrieve_m(model, phase=phase, **kwargs)
    testing.assert_allclose(m2, phase * m1, atol=1e-12) 
Example #30
Source File: test_krhf_slow_supercell.py    From pyscf with Apache License 2.0 5 votes vote down vote up
def test_class(self):
        """Tests container behavior."""
        model = ktd.TDRHF(self.model_krhf)
        model.nroots = self.td_model_rhf.nroots
        assert not model.fast
        model.kernel()
        testing.assert_allclose(model.e, self.td_model_rhf.e, atol=1e-5)
        nocc = nvirt = 4
        testing.assert_equal(model.xy.shape, (len(model.e), 2, self.k, self.k, nocc, nvirt))
        vecs = model.xy.reshape(len(model.xy), -1)[:, self.ov_order]
        assert_vectors_close(vecs, numpy.array(self.td_model_rhf.xy).squeeze(), atol=1e-5)
        # Test real
        testing.assert_allclose(model.e.imag, 0, atol=1e-8)