예제 #1
0
 def test_MLE_contributions_to_CR_raises_TypeError(self):
     # A TypeError is raised because analytical scalar product is
     # required while the test function cannot allow that.
     r_grid = Rectangular(r.xl, r.xr, grid=[1, 2, 3])
     test = Function(r_grid.grid, r_grid.values)
     with pytest.raises(TypeError):
         siegerts.MLE_contributions_to_CR(test)
예제 #2
0
 def test_continuum_contributions_to_CR_raises_TypeError(self):
     # A TypeError is raised because analytical scalar product is
     # required while the test function cannot allow that.
     r_grid = Rectangular(r.xl, r.xr, grid=[1, 2, 3])
     test = Function(r_grid.grid, r_grid.values)
     continuum = SWPBasisSet(states=[c_e, c_o, c_e_2, c_o_2])
     with pytest.raises(TypeError):
         continuum.continuum_contributions_to_CR(test)
예제 #3
0
    def _sp_R_3(self, rect):
        r"""
        Parameters
        ----------
        rect: Rectangular
            Rectangular function.

        Returns
        -------
        complex or float
            Value of the analytic scalar product of an eigenstate with a
            rectangular test function in region *III*.
        """
        # Given the parity of the states, the scalar product in region
        # III is closely related to the scalar product in region I.
        k0 = rect.momentum
        h = rect.amplitude
        sp = self._sp_R_1(Rectangular(-rect.xr, -rect.xl, k0=k0, h=h))
        if self.is_even:
            return sp
        else:
            return -sp
예제 #4
0
 def test_MLE_strength_function(self):
     r = Rectangular.from_width_and_center(4.0, 0.2, h=h)
     kgrid = np.arange(0.1, 10.2, 1.0)
     # Bound state
     bnd1_MLE_rf = bnd1.MLE_strength_function(r, kgrid)
     bnd1_MLE_rf_expected = np.array([
         0.005265,
         0.054574,
         0.090293,
         0.109551,
         0.116229,
         0.115406,
         0.110816,
         0.104659,
         0.098098,
         0.091697,
         0.085703,
     ])
     np.testing.assert_array_almost_equal(bnd1_MLE_rf, bnd1_MLE_rf_expected)
     # Resonant state
     res_1 = siegerts.resonants[0]
     r1_MLE_rf = res_1.MLE_strength_function(r, kgrid)
     r1_MLE_rf_expected = np.array([
         -0.004673,
         -0.007866,
         0.003747,
         0.00831,
         0.004756,
         0.003259,
         0.002468,
         0.001983,
         0.001656,
         0.001422,
         0.001245,
     ])
     np.testing.assert_array_almost_equal(r1_MLE_rf, r1_MLE_rf_expected)
예제 #5
0
class TestSWPContinuum:
    def test_init_raises_WavenumberError(self):
        # Due to a complex wavenumber
        with pytest.raises(WavenumberError):
            SWPContinuum(1j, "o", pot)

    @pytest.mark.parametrize(
        "value, expected",
        [
            (c_e == SWPContinuum(k, "e", pot, grid=xgrid), True),
            (c_e == c_o, False),
            (c_e == Rectangular(-2.0, 2.0), False),
            (c_e != 1, True),
        ],
    )
    def test_eq(self, value, expected):
        assert value == expected

    @pytest.mark.parametrize("wrong_parity", [1, "toto", None])
    def test_parity_raises_ParityError(self, wrong_parity):
        # All other value than the correct parity (here, 'e') raise a
        # ParityError.
        with pytest.raises(ParityError):
            SWPContinuum(1, wrong_parity, pot)

    @pytest.mark.parametrize(
        "value, expected",
        [
            (c_e.is_even, True),
            (c_o.is_even, False),
            (c_e.is_odd, False),
            (c_o.is_odd, True),
        ],
    )
    def test_is_parity(self, value, expected):
        assert value == expected

    @pytest.mark.parametrize(
        "state, expected",
        [(c_e_with_grid, c_e_expected), (c_o_with_grid, c_o_expected)],
    )
    def test_compute_wavefunction(self, state, expected):
        np.testing.assert_array_almost_equal(state.values, expected)

    def test_compute_wavefunction_raises_ValueError(self):
        # wrong grid: complex grid
        cplx_grid = 0.0j * np.zeros(len(xgrid))
        cplx_grid[0] = 1.0j  # Now it will raise an error
        with pytest.raises(ValueError):
            SWPContinuum(k, "e", pot, grid=cplx_grid)

    @pytest.mark.parametrize(
        "value, expected",
        [
            (c_e.scal_prod(r1), -0.90635923273823826 - 1.0929535715333105j),
            (c_o.scal_prod(r1), 0.0j),
            (c_e.scal_prod(r1p), 8.1518896862942469 + 0.90334965979254944j),
            (c_o.scal_prod(r1p), 0.044058371125370144 - 0.091660394131671374j),
            (c_e.scal_prod(r1m), -0.62073133482877063 + 8.1782662389604024j),
            (c_o.scal_prod(r1m),
             -0.044058371125370144 + 0.091660394131671374j),
            (c_e.scal_prod(r2p), -0.46402741449617413 - 0.53088816154466156j),
            (c_o.scal_prod(r2p), 0.30059494527010172 - 0.58538480308246721j),
            (c_e.scal_prod(r2m), -0.43585286304346693 - 0.55425261681753712j),
            (c_o.scal_prod(r2m), -0.26937227999135543 + 0.60039259120519894j),
            (c_e.scal_prod(g1), -0.072629271280681904 - 0.087581632731056958j),
            (c_o.scal_prod(g1), 0.0j),
            (c_e.scal_prod(g2), -0.044201378011213856 - 0.053301221214571481j),
            (c_o.scal_prod(g2), -0.077013178199935931 - 0.037017898720824186j),
        ],
    )
    def test_analytical_scal_prod(self, value, expected):
        decimal = 14
        np.testing.assert_almost_equal(value, expected, decimal=decimal)

    @pytest.mark.parametrize(
        "value, expected",
        [
            (c_e_na.scal_prod(r_na_1),
             0.26626637332669501 + 0.32108326720237679j),
            (c_o_na.scal_prod(r_na_1), 0.0j),
            (c_e_na.scal_prod(r_na_k0_1),
             -0.52973455183891471 - 0.63879226854430993j),
            (c_e_na.scal_prod(r_na_3),
             -0.49218097140147571 - 0.59350744285884549j),
            (c_o_na.scal_prod(r_na_3),
             1.5309260929446813 + 0.7358697353142144j),
            (c_e_na.scal_prod(r_na_k0_3),
             0.0052031824473088156 - 0.94598903180912952j),
            (c_e_na.scal_prod(g_na_1),
             -0.22338282414061131 - 0.26937117937892679j),
            (c_o_na.scal_prod(g_na_1), 0.0j),
            (c_e_na.scal_prod(g_na_k0_1),
             -0.22341030109728754 - 0.26940431308226365j),
            (c_e_na.scal_prod(g_na_2),
             -0.16205020291442584 - 0.19541186501508359j),
            (c_o_na.scal_prod(g_na_2),
             0.0017661284228762 + 0.0008489243609226155j),
            (c_e_na.scal_prod(g_na_k0_2),
             -0.16248453420586526 - 0.19555059652396745j),
        ],
    )
    def test_numerical_scal_prod(self, value, expected):
        decimal = 14
        np.testing.assert_almost_equal(value, expected, decimal=decimal)

    def test_scal_prod_raises_TypeError(self):
        # Scalar product not implemented for SWPSiegert,
        # even if it is known that it is zero for bound state.(TODO?)
        with pytest.raises(TypeError):
            c_e.scal_prod(bnd1)
예제 #6
0
c_o_expected = np.array([
    -0.466284 + 0.224128j,
    -0.422628 + 0.203144j,
    0.038320 - 0.018419j,
    -0.148014 + 0.071146j,
    0.000000 + 0.0j,
    0.148014 - 0.071146j,
    -0.038320 + 0.018419j,
    0.422628 - 0.203144j,
    0.466284 - 0.224128j,
])
c_e_na = SWPContinuum(k, "e", pot, grid=xgrid, analytic=False)
c_o_na = SWPContinuum(k, "o", pot, grid=xgrid, analytic=False)

# Test functions
r1 = Rectangular(-10.0, 10.0, h=h)
r1p = Rectangular(-10.0, 10.0, k0=1.0, h=2.0)
r1m = Rectangular(-10.0, 10.0, k0=-1.0, h=2.0)
r2 = Rectangular(-1.8, 2.2, h=h)
r2p = Rectangular(-1.8, 2.2, h=h, k0=qq)
r2m = Rectangular(-1.8, 2.2, h=h, k0=-qq)
g1 = Gaussian(0.25, 0.0, h=h)
g2 = Gaussian(0.25, 0.2, h=h)
r_na_1 = Rectangular(-2.0, 2.0, h=h, grid=xgrid)
r_na_k0_1 = Rectangular(-2.0, 2.0, k0=2.0, h=h, grid=xgrid)
r_na_2 = Rectangular(0.5, 4.5, h=h, grid=xgrid)
r_na_k0_2 = Rectangular(0.5, 4.5, k0=2.0, h=h, grid=xgrid)
r_na_3 = Rectangular.from_width_and_center(4.0, 2.5, h=2.0, grid=xgrid)
r_na_k0_3 = Rectangular.from_width_and_center(4.0,
                                              2.5,
                                              k0=2.0,
예제 #7
0
 def test_split(self):
     # Rectangular function spreading over all regions
     xl = -10
     xr = 5
     r_large = Rectangular(xl, xr)
     l0 = 5
     V0 = 10
     pot = SWPotential(l0, V0)
     r_I, r_II, r_III = r_large.split(pot)
     assert r_I == Rectangular(xl, -l0 / 2)
     assert r_II == Rectangular(-l0 / 2, l0 / 2)
     assert r_III == Rectangular(l0 / 2, xr)
     # Rectangular function spreading over region I
     r_regI = Rectangular(xl, -l0)
     r_I, r_II, r_III = r_regI.split(pot)
     assert r_I == r_regI
     assert r_II is None
     assert r_III is None
     # Rectangular function spreading over region II and III
     r_regII_to_III = Rectangular(0, xr)
     r_I, r_II, r_III = r_regII_to_III.split(pot)
     assert r_I is None
     assert r_II == Rectangular(0, l0 / 2)
     assert r_III == Rectangular(l0 / 2, xr)
     # Rectangular function spreading over region II
     r_regII = Rectangular(-l0 / 4, l0 / 4)
     r_I, r_II, r_III = r_regII.split(pot)
     assert r_I is None
     assert r_II == r_regII
     assert r_III is None
예제 #8
0
 def test_conjugate(self):
     assert r_momentum.conjugate() == Rectangular(xc - a / 2.0,
                                                  xc + a / 2,
                                                  k0=-k0,
                                                  h=h)
예제 #9
0
class TestRectangular:
    @pytest.mark.parametrize(
        "to_evaluate",
        [
            "Rectangular(1, 2, h=0)",  # zero height
            "Rectangular(2, -2, h=0)",  # zero height, width < 0
        ],
    )
    def test_init_raises_ValueError(self, to_evaluate):
        with pytest.raises(ValueError):
            eval(to_evaluate)

    def test_eq(self):
        assert r_grid == r

    @pytest.mark.parametrize(
        "to_compare",
        [
            Rectangular.from_center_and_width(xc, a + 2.0, h=h),
            Rectangular.from_center_and_width(xc + 1.0, a, h=h),
            Rectangular.from_center_and_width(xc, a),
            wf_even,
        ],
    )
    def test_not_eq(self, to_compare):
        assert r != to_compare

    def test_is_odd(self):
        assert not r.is_odd

    def test_norm(self):
        assert r.norm() == r_momentum.norm()

    def test_conjugate(self):
        assert r_momentum.conjugate() == Rectangular(xc - a / 2.0,
                                                     xc + a / 2,
                                                     k0=-k0,
                                                     h=h)

    def test_split(self):
        # Rectangular function spreading over all regions
        xl = -10
        xr = 5
        r_large = Rectangular(xl, xr)
        l0 = 5
        V0 = 10
        pot = SWPotential(l0, V0)
        r_I, r_II, r_III = r_large.split(pot)
        assert r_I == Rectangular(xl, -l0 / 2)
        assert r_II == Rectangular(-l0 / 2, l0 / 2)
        assert r_III == Rectangular(l0 / 2, xr)
        # Rectangular function spreading over region I
        r_regI = Rectangular(xl, -l0)
        r_I, r_II, r_III = r_regI.split(pot)
        assert r_I == r_regI
        assert r_II is None
        assert r_III is None
        # Rectangular function spreading over region II and III
        r_regII_to_III = Rectangular(0, xr)
        r_I, r_II, r_III = r_regII_to_III.split(pot)
        assert r_I is None
        assert r_II == Rectangular(0, l0 / 2)
        assert r_III == Rectangular(l0 / 2, xr)
        # Rectangular function spreading over region II
        r_regII = Rectangular(-l0 / 4, l0 / 4)
        r_I, r_II, r_III = r_regII.split(pot)
        assert r_I is None
        assert r_II == r_regII
        assert r_III is None

    def test_split_raises_TypeError(self):
        with pytest.raises(TypeError):
            r.split(r)
예제 #10
0
g_momentum = Gaussian(sigma, xc, h=h, k0=k0)
g_grid_momentum = Gaussian(sigma, xc, h=h, k0=k0, grid=xgrid)
gauss_momentum_expected = np.array([
    -3.68527303e-15 - 2.50587932e-14j,
    2.92467480e-08 + 8.51098476e-09j,
    -4.38546014e-04 + 5.07757908e-04j,
    -1.12638700e-01 - 2.46120050e-01j,
    2.00000000e00 + 0.00000000e00j,
    -1.12638700e-01 + 2.46120050e-01j,
    -4.38546014e-04 - 5.07757908e-04j,
    2.92467480e-08 - 8.51098476e-09j,
    -3.68527303e-15 + 2.50587932e-14j,
])
# Variables for the tests of Rectangular
a = 2.0
r = Rectangular(-a / 2.0, a / 2.0, h=h)
r_grid = Rectangular(-a / 2.0, a / 2.0, h=h, grid=xgrid)
r_grid_expected = np.array([0.0, 0.0, 0.0, 2.0, 2.0, 2.0, 0.0, 0.0, 0.0],
                           dtype=complex)
r_momentum = Rectangular(-a / 2.0, a / 2.0, k0=k0, h=h)
r_grid_momentum = Rectangular(-a / 2.0, a / 2.0, k0=k0, h=h, grid=xgrid)
r_grid_momentum_expected = np.array(
    [
        0.0j,
        0.0j,
        0.0j,
        -0.83229367 - 1.81859485j,
        2.0 + 0.0j,
        -0.83229367 + 1.81859485j,
        0.0j,
        0.0j,
예제 #11
0
    Rectangular,
    BasisSet,
    Eigenstate,
    UniformCoordMap,
    ErfKGCoordMap,
    Hamiltonian,
)

# Numerical continuum BasisSet generated by:
xgrid1 = np.linspace(-6.5, 6.5, 31)
l = np.sqrt(2) * np.pi
V0 = 10
pot1 = SWPotential(l, V0, grid=xgrid1)
cm = UniformCoordMap(0)
basis = Hamiltonian(pot1, cm).solve()
r_num = Rectangular.from_center_and_width(0.25, 2.0, grid=xgrid1)
# Numerical Siegert BasisSet generated by:
cm_sieg = ErfKGCoordMap(0.4, 5.5, 5.5)
basis_sieg = Hamiltonian(pot1, cm_sieg).solve(max_virial=0.15)


class TestBasisSet:
    def test_init_from_one_state(self):
        BasisSet(states=basis[0])

    def test_init_raises_ValueError(self):
        with pytest.raises(ValueError):
            BasisSet(states=1)

    def test_potential(self):
        assert basis.potential == pot1
예제 #12
0
res = siegerts.resonants
ares = siegerts.antiresonants
cont = siegerts.continuum
pot = siegerts.potential
l = pot.width
xgrid = np.linspace(-l / 2, l / 2, 5)
siegerts_grid = SWPBasisSet.from_file(filename, nres=5, grid=xgrid)

# Analytical continuum SWPBasisSet
k = 1.0
k_2 = 2.0
c_e = SWPContinuum(k, "e", pot, grid=xgrid)
c_o = SWPContinuum(k, "o", pot, grid=xgrid)
c_e_2 = SWPContinuum(k_2, "e", pot, grid=xgrid)
c_o_2 = SWPContinuum(k_2, "o", pot, grid=xgrid)
r = Rectangular.from_center_and_width(0.25, 2.0)
r_even = Rectangular(-1.0, 1.0)
bnds_grid = siegerts_grid.bounds
exact_grid = siegerts_grid.bounds + [c_e, c_o, c_e_2, c_o_2]
kgrid = np.arange(0.1, 5.2, 1.0)
time_grid = [0.0, 1.0]


class TestSWPBasisSet:
    def test_init_raises_ValueError_not_SWPEigenstate(self):
        with pytest.raises(ValueError):
            SWPBasisSet(states=[Eigenstate([-1, 0, 1], [2, 1, 0], 1.0)])

    def test_init_raises_ValueError_different_potentials(self):
        bnd1 = bnds[0]
        bnd2 = bnds[1]