Beispiel #1
0
def test_transform_coeff_with_x_and_r():
    """Test coefficient transform between x and r."""
    coeff = np.array([2, 3, 4])
    ltf = LinearFiniteRTransform(1, 10)  # (-1, 1) -> (r0, rmax)
    inv_tf = InverseRTransform(ltf)  # (r0, rmax) -> (-1, 1)
    x = np.linspace(-1, 1, 20)
    r = ltf.transform(x)
    assert r[0] == 1
    assert r[-1] == 10
    # Transform ODE from [1, 10) to (-1, 1)
    coeff_transform = _transform_ode_from_rtransform(coeff, inv_tf, x)
    derivs_fun = [inv_tf.deriv, inv_tf.deriv2, inv_tf.deriv3]
    coeff_transform_all_pts = _transform_ode_from_derivs(coeff, derivs_fun, r)
    assert_allclose(coeff_transform, coeff_transform_all_pts)
Beispiel #2
0
def test_transformation_of_ode_with_linear_transform():
    """Test transformation of ODE with linear transformation."""
    x = GaussLaguerre(10).points
    # Obtain linear transformation with rmin = 1 and rmax = 10.
    ltf = LinearFiniteRTransform(1, 10)
    # The inverse is x_i = \frac{r_i - r_{min} - R} {r_i - r_{min} + R}
    inv_ltf = InverseRTransform(ltf)
    derivs_fun = [inv_ltf.deriv, inv_ltf.deriv2, inv_ltf.deriv3]
    # Test with 2y + 3y` + 4y``
    coeff = np.array([2, 3, 4])
    coeff_b = _transform_ode_from_derivs(coeff, derivs_fun, x)
    # assert values
    assert_allclose(coeff_b[0], np.ones(len(x)) * coeff[0])
    assert_allclose(coeff_b[1], 1 / 4.5 * coeff[1])
    assert_allclose(coeff_b[2], (1 / 4.5)**2 * coeff[2])
Beispiel #3
0
    def test_solver_ode_bvp_with_tf(self):
        """Test result for high level api solve_ode with fx term."""
        x = np.linspace(-0.999, 0.999, 20)
        btf = BeckeRTransform(0.1, 5)
        r = btf.transform(x)
        ibtf = InverseRTransform(btf)

        def fx(x):
            return 1 / x**2

        coeffs = [-1, 1, 1]
        bd_cond = [(0, 0, 0), (1, 0, 0)]
        # calculate diff equation wt/w tf.
        res = ODE.solve_ode(x, fx, coeffs, bd_cond, ibtf)
        res_ref = ODE.solve_ode(r, fx, coeffs, bd_cond)
        assert_allclose(res(x)[0], res_ref(r)[0], atol=1e-4)
Beispiel #4
0
    def test_solver_ode_coeff_a_f_x_with_tf(self):
        """Test ode with a(x) and f(x) involved."""
        x = np.linspace(-0.999, 0.999, 20)
        btf = BeckeRTransform(0.1, 5)
        r = btf.transform(x)
        ibtf = InverseRTransform(btf)

        def fx(x):
            return 0 * x

        coeffs = [lambda x: x**2, lambda x: 1 / x**2, 0.5]
        bd_cond = [(0, 0, 0), (1, 0, 0)]
        # calculate diff equation wt/w tf.
        res = ODE.solve_ode(x, fx, coeffs, bd_cond, ibtf)
        res_ref = ODE.solve_ode(r, fx, coeffs, bd_cond)
        assert_allclose(res(x)[0], res_ref(r)[0], atol=1e-4)
Beispiel #5
0
 def test_errors_assert(self):
     """Test errors raise."""
     # parameter error
     with self.assertRaises(ValueError):
         BeckeRTransform.find_parameter(np.arange(5), 0.5, 0.1)
     # transform non array type
     with self.assertRaises(TypeError):
         btf = BeckeRTransform(0.1, 1.1)
         btf.transform("dafasdf")
     # inverse init error
     with self.assertRaises(TypeError):
         InverseRTransform(0.5)
     # type error for transform_1d_grid
     with self.assertRaises(TypeError):
         btf = BeckeRTransform(0.1, 1.1)
         btf.transform_1d_grid(np.arange(3))
     with self.assertRaises(ZeroDivisionError):
         btf = BeckeRTransform(0.1, 0)
         itf = InverseRTransform(btf)
         itf._d1(0.5)
     with self.assertRaises(ZeroDivisionError):
         btf = BeckeRTransform(0.1, 0)
         itf = InverseRTransform(btf)
         itf._d1(np.array([0.1, 0.2, 0.3]))
Beispiel #6
0
        atol=1e-4,
    )


@pytest.mark.parametrize(
    "transform, fx, coeffs, bd_cond",
    [
        # Test with ode -y + y` + y``=1/x^2
        [
            BeckeRTransform(1.0, 5.0),
            fx_complicated_example3,
            [-1, 1, 1],
            [(0, 0, 3), (1, 0, 3)],
        ],
        [
            InverseRTransform(BeckeRTransform(1.0, 5.0)),
            fx_complicated_example3,
            [-1, 1, 1],
            [(0, 0, 3), (1, 0, 3)],
        ],
        [
            BeckeRTransform(1.0, 5.0),
            fx_complicated_example3,
            [-1, 1, 1],
            [(0, 0, 3), (1, 0, 3)],
        ],
        # Test one with boundary conditions on the derivatives
        [
            SqTF(1, 3),
            fx_complicated_example,
            np.random.uniform(-100, 100, (4, )),
Beispiel #7
0
    def test_poisson_solve(self):
        """Test the poisson solve function."""
        oned = GaussChebyshev(30)
        oned = GaussChebyshev(50)
        btf = BeckeRTransform(1e-7, 1.5)
        rad = btf.transform_1d_grid(oned)
        l_max = 7
        atgrid = AtomGrid(rad, degrees=[l_max])
        value_array = self.helper_func_gauss(atgrid.points)
        p_0 = atgrid.integrate(value_array)

        # test density sum up to np.pi**(3 / 2)
        assert_allclose(p_0, np.pi**1.5, atol=1e-4)
        sph_coor = atgrid.convert_cart_to_sph()[:, 1:3]
        spls_mt = Poisson._proj_sph_value(
            atgrid.rgrid,
            sph_coor,
            l_max // 2,
            value_array,
            atgrid.weights,
            atgrid.indices,
        )

        # test splines project fit gauss function well

        def gauss(r):
            return np.exp(-(r**2))

        for _ in range(20):
            coors = np.random.rand(10, 3)
            r = np.linalg.norm(coors, axis=-1)
            spl_0_0 = spls_mt[0, 0]
            interp_v = spl_0_0(r)
            ref_v = gauss(r) * np.sqrt(4 * np.pi)
            # 0.28209479 is the value in spherical harmonic Z_0_0
            assert_allclose(interp_v, ref_v, atol=1e-3)
        ibtf = InverseRTransform(btf)
        linsp = np.linspace(-1, 0.99, 50)
        bound = p_0 * np.sqrt(4 * np.pi)
        res_bv = Poisson.solve_poisson_bv(spls_mt[0, 0],
                                          linsp,
                                          bound,
                                          tfm=ibtf)

        near_rg_pts = np.array([1e-2, 0.1, 0.2, 0.3, 0.5, 0.7, 1.0, 1.2])
        near_tf_pts = ibtf.transform(near_rg_pts)
        long_rg_pts = np.array([2, 3, 4, 5, 6, 7, 8, 9, 10])
        long_tf_pts = ibtf.transform(long_rg_pts)
        short_res = res_bv(near_tf_pts)[0] / near_rg_pts / (2 * np.sqrt(np.pi))
        long_res = res_bv(long_tf_pts)[0] / long_rg_pts / (2 * np.sqrt(np.pi))
        # ref are calculated with mathemetical
        # integrate[exp[-x^2 - y^2 - z^2] / sqrt[(x - a)^2 + y^2 +z^2], range]
        ref_short_res = [
            6.28286,  # 0.01
            6.26219,  # 0.1
            6.20029,  # 0.2
            6.09956,  # 0.3
            5.79652,  # 0.5
            5.3916,  # 0.7
            4.69236,  # 1.0
            4.22403,  # 1.2
        ]
        ref_long_res = [
            2.77108,  # 2
            1.85601,  # 3
            1.39203,  # 4
            1.11362,  # 5
            0.92802,  # 6
            0.79544,  # 7
            0.69601,  # 8
            0.61867,  # 9
            0.55680,  # 10
        ]
        assert_allclose(short_res, ref_short_res, atol=5e-4)
        assert_allclose(long_res, ref_long_res, atol=5e-4)
        # solve same poisson equation with gauss directly
        gauss_pts = btf.transform(linsp)
        res_gs = Poisson.solve_poisson_bv(gauss, gauss_pts, p_0)
        gs_int_short = res_gs(near_rg_pts)[0] / near_rg_pts
        gs_int_long = res_gs(long_rg_pts)[0] / long_rg_pts
        assert_allclose(gs_int_short, ref_short_res, 5e-4)
        assert_allclose(gs_int_long, ref_long_res, 5e-4)
Beispiel #8
0
 def test_handymod_inverse(self):
     """Test inverse transform basic function."""
     btf = HandyModRTransform(0.1, 10.0, 2)
     inv = InverseRTransform(btf)
     new_array = inv.transform(btf.transform(self.array))
     assert_allclose(new_array, self.array)
Beispiel #9
0
 def test_knowles_inverse(self):
     """Test inverse transform basic function."""
     btf = KnowlesRTransform(0.1, 1.1, 2)
     inv = InverseRTransform(btf)
     new_array = inv.transform(btf.transform(self.array))
     assert_allclose(new_array, self.array)
Beispiel #10
0
 def test_multiexp_inverse(self):
     """Test inverse transform basic function."""
     btf = MultiExpRTransform(0.1, 1.1)
     inv = InverseRTransform(btf)
     new_array = inv.transform(btf.transform(self.array))
     assert_allclose(new_array, self.array)
Beispiel #11
0
 def test_becke_inverse_inverse(self):
     """Test inverse of inverse of Becke transformation."""
     btf = BeckeRTransform(0.1, 1.1)
     inv = InverseRTransform(btf)
     inv_inv = inv.inverse(inv.transform(self.array))
     assert_allclose(inv_inv, self.array, atol=1e-7)
Beispiel #12
0
 def test_becke_inverse_deriv(self):
     """Test inverse transformation derivatives with finite diff."""
     btf = BeckeRTransform(0.1, 1.1)
     inv = InverseRTransform(btf)
     self._deriv_finite_diff(0, 20, inv)