def test_error_raises(self): """Test proper error raises.""" x = np.linspace(-0.999, 0.999, 20) # r = btf.transform(x) def fx(x): return 1 / x ** 2 coeffs = [-1, -2, 1] bd_cond = [(0, 0, 0), (1, 0, 0), (0, 1, 0), (1, 1, 0)] with self.assertRaises(NotImplementedError): ODE.solve_ode(x, fx, coeffs, bd_cond[3:]) with self.assertRaises(NotImplementedError): ODE.solve_ode(x, fx, coeffs, bd_cond) with self.assertRaises(NotImplementedError): test_coeff = [1, 2, 3, 4, 5] ODE.solve_ode(x, fx, test_coeff, bd_cond) with self.assertRaises(ValueError): test_coeff = [1, 2, 3, 3] tf = BeckeTF(0.1, 1) def fx(x): return x ODE.solve_ode(x, fx, test_coeff, bd_cond[:3], tf)
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 = BeckeTF(0.1, 5) r = btf.transform(x) ibtf = InverseTF(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)
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 = BeckeTF(0.1, 5) r = btf.transform(x) ibtf = InverseTF(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)
def test_solve_ode_bvp(self): """Test result for high level api solve_ode.""" x = np.linspace(0, 2, 10) def fx(x): return 1 if isinstance(x, Number) else np.ones(x.size) coeffs = [0, 0, 1] bd_cond = [[0, 0, 0], [1, 0, 0]] res = ODE.solve_ode(x, fx, coeffs, bd_cond) assert_almost_equal(res(0)[0], 0) assert_almost_equal(res(1)[0], -0.5) assert_almost_equal(res(2)[0], 0) assert_almost_equal(res(0)[1], -1) assert_almost_equal(res(1)[1], 0) assert_almost_equal(res(2)[1], 1)
def solve_poisson_bv(fx, x_range, boundary, m_l=(0, 0), tfm=None): """Solve poisson equation for given function. .. math:: Parameters ---------- fx : Callable Callable function on the right hand side of equation x_range : np.narray(K,) An array with points for numerically solve the ODE The boundary point should be within radial points range boundary : float Boundary value when x to go infinite m_l : tuple, optional m and l value of given spherical harmonics tfm : None, optional Transformation for given x variable Returns ------- scipy.PPline A callable spline for result. """ m_value, l_value = m_l def f_x(r): return fx(r) * -4 * np.pi * r def coeff_0(r): return -l_value * (l_value + 1) / r**2 coeffs = [coeff_0, 0, 1] if l_value == 0 and m_value == 0: bd_cond = [(0, 0, 0), (1, 0, boundary)] else: bd_cond = [(0, 0, 0), (1, 0, 0)] return ODE.solve_ode(x_range, f_x, coeffs, bd_cond, transform=tfm)