def test_copy(self): d = 3 n = np.array([5, 7, 8]) l = np.array([[-2., 3.], [-4., 5.], [-6., 9.]]) GR1 = Grid(d=d, n=n, l=l) GR2 = GR1.copy() GR2.d = 1 GR2.n = np.array([5]) GR2.l = np.array([[-2., 3.]]) self.assertEqual( GR1.d, 3 ) np.testing.assert_array_equal( GR1.n, np.array([5, 7, 8]) ) np.testing.assert_almost_equal( GR1.l, np.array([[-2., 3.], [-4., 5.], [-6., 9.]]) ) self.assertEqual( GR2.d, 1 ) np.testing.assert_array_equal( GR2.n, np.array([5]) ) np.testing.assert_almost_equal( GR2.l, np.array([[-2., 3.]]) )
def test_indm(self): GR = Grid(n=[5, 7, 8]) np.testing.assert_equal(GR.indm(0), np.array([0, 0, 0])) np.testing.assert_equal(GR.indm(1), np.array([1, 0, 0])) np.testing.assert_equal(GR.indm(5), np.array([0, 1, 0])) np.testing.assert_equal(GR.indm(279), np.array([4, 6, 7]))
def test_indf(self): GR = Grid(n=[5, 7, 8]) self.assertEqual(GR.indf([0, 0, 0]), 0) self.assertEqual(GR.indf([1, 0, 0]), 1) self.assertEqual(GR.indf([0, 1, 0]), 5) self.assertEqual(GR.indf([4, 6, 7]), 279)
def test_comp_c(self): GR = Grid(n=[5, 7], l=[ [-4., 3.], [-1., 2.], ], k='c') X = GR.comp([0, 0]) np.testing.assert_almost_equal( X, np.array([[3.], [2.]]) ) X = GR.comp([4, 6]) np.testing.assert_almost_equal( X, np.array([[-4.], [-1.]]) ) X = GR.comp([ [0, 4], [0, 6], ]) np.testing.assert_almost_equal( X, np.array([ [3., -4.], [2., -1.], ]) )
def test_info(self): GR = Grid(n=[5, 7], l=[[-4., 3.], [-1., 2.]]) s = GR.info(is_ret=True) self.assertTrue('Kind : Chebyshev' in s) self.assertTrue('Dimensions : 2' in s) self.assertTrue('Poi 5 | Min -4.000 | Max 3.000 |' in s) self.assertTrue('Poi 7 | Min -1.000 | Max 2.000 |' in s)
def test_copy_err(self): d = 2 n1 = np.array([3, 4]) l1 = np.array([[-2, 4], [-5, 7]]) GR1 = Grid(d, n1, l1, 'u') msg = 'Invalid dimension for number of points (n).' with self.assertRaises(IndexError) as ctx: GR2 = GR1.copy(d=1) self.assertIn(msg, str(ctx.exception))
def test_init_kind(self): d = 2 n = np.array([3, 4]) l = np.array([[-2, 4], [-5, 7]]) GR = Grid(d, n, l, 'u') self.assertEqual(GR.k, 'u') GR = Grid(d, n, l, 'c') self.assertEqual(GR.k, 'c') self.assertRaises(ValueError, lambda: Grid(d, n, l, 'xxx'))
def test_fpe_1d_oup(self): MD = Model.select('fpe_1d_oup') MD.init(s=1., D=0.5, A=1.) SL = Solver(TG=Grid(d=1, n=1000, l=[+0., +8.], k='u'), SG=Grid(d=1, n=101, l=[-6., +6.], k='c'), MD=MD) SL.init() SL.prep() SL.calc(dsbl_print=True) self.assertTrue(SL.hst['err_real'][-1] < 9.29e-06) self.assertTrue(SL.hst['err_stat'][-1] < 9.34e-06)
def test_fpe_3d_drift_zero_tt(self): return # TODO: Remove. MD = Model.select('fpe_3d_drift_zero') MD.init(s=1., D=0.5) SL = Solver(TG=Grid(d=1, n=10, l=[+0., +1.], k='u'), SG=Grid(d=3, n=21, l=[-5., +5.], k='c'), MD=MD, eps=1.E-2, with_tt=True) SL.init() SL.prep() SL.calc(dsbl_print=True) self.assertTrue(SL.hst['err_real'][-1] < 4.48e-03)
def test_copy(self): d = 2 n1 = np.array([3, 4]) l1 = np.array([[-2, 4], [-5, 7]]) n2 = [4, 5] l2 = np.array([[-3, 5], [-6, 8]]) GR1 = Grid(d, n1, l1, 'u') GR2 = GR1.copy(n=n2, l=l2) np.testing.assert_array_equal(GR1.n, n1) np.testing.assert_array_equal(GR2.n, n2) np.testing.assert_array_equal(GR1.l, l1) np.testing.assert_array_equal(GR2.l, l2)
def test_int_3d_tt(self): return # DRAFT GR = Grid(n=[35, 35, 35], l=[-10., 10.]) FN = Func(GR, eps=1.E-6, with_tt=True) FN.init(lambda X: func_for_int(X, 3)).prep().calc() e = np.abs(1. - FN.comp_int()) / np.abs(1.) self.assertTrue(e < 6.0E-7)
def test_init4(self): n = np.array([3]) l = np.array([[-2, 4]]) GR = Grid(n=n, l=l) self.assertEqual(GR.d, 1) np.testing.assert_array_equal(GR.n, np.array([3])) np.testing.assert_almost_equal(GR.l, np.array([[-2., 4.]]))
def test_2ord_elements(self): N = 5 SG = Grid(d=1, n=N + 1, l=[-1., 1.]) D1, D2 = difs(SG, 2) e = D1 @ D1 - D2 self.assertTrue(np.max(np.abs(e)) < 1.E-13)
def test_2d_tt(self): GR = Grid(d=2, n=[28, 30], l=[ [-3., 4.], [-2., 3.], ]) FN = Func(GR, with_tt=True).init(func_2d).prep().calc() self.assertTrue(np.max(FN.test(1000, is_u=True)) < 8.0E-2)
def test_init3(self): n = 3 l = np.array([[-2, 4], [-5, 7]]) GR = Grid(n=n, l=l) self.assertEqual(GR.d, 2) np.testing.assert_array_equal(GR.n, np.array([3, 3])) np.testing.assert_almost_equal(GR.l, np.array([[-2., 4.], [-5., 7.]]))
def test_init2(self): n = [3, 4] l = np.array([-2, 4]) GR = Grid(n=n, l=l) self.assertEqual(GR.d, 2) np.testing.assert_array_equal(GR.n, np.array([3, 4])) np.testing.assert_almost_equal(GR.l, np.array([[-2., 4.], [-2., 4.]]))
def test_1d_ivp(self): TG = Grid(1, 2, [0., 0.0001], k='u') r0 = -2.5 * np.arange(1000).reshape(1, -1) + 0.2 r_real = func_rt_1d(TG.l2, r0) SL = OrdSolver(TG, 'ivp').init(func_f0_1d, with_y0=True) e = np.mean(np.abs((r_real - SL.comp(r0)) / r_real)) self.assertTrue(e < 2.E-06)
def test_init1(self): d = 2 n = 3 l = [-2, 4] GR = Grid(d=d, n=n, l=l) self.assertEqual(GR.d, 2) np.testing.assert_array_equal(GR.n, np.array([3, 3])) np.testing.assert_almost_equal(GR.l, np.array([[-2., 4.], [-2., 4.]]))
def test_3d_tt(self): GR = Grid(d=3, n=[24, 26, 28], l=[ [-3., 4.], [-2., 3.], [-1., 1.], ]) FN = Func(GR, with_tt=True).init(func_3d).prep().calc() self.assertTrue(np.max(FN.test(1000, is_u=True)) < 5.0E-2)
def test_1ord_elements(self): N = 5 SG = Grid(d=1, n=N + 1, l=[-1., 1.]) X = SG.comp()[0, :] D = dif1(SG) e = D[0, 0] - (2. * N**2 + 1.) / 6. self.assertTrue(np.max(np.abs(e)) < 1.E-14) e = D[0, 2] - 2. / (1. - X[2]) self.assertTrue(np.max(np.abs(e)) < 1.E-14) e = D[0, 3] + 2. / (1. - X[3]) self.assertTrue(np.max(np.abs(e)) < 1.E-14) e = D[4, 4] + X[4] / (1. - X[4]**2) / 2. self.assertTrue(np.max(np.abs(e)) < 1.E-14)
def test_init7(self): d = 1 n = [3, 4] l = np.array([[-2, 4]]) msg = 'Invalid dimension for number of points (n).' with self.assertRaises(IndexError) as ctx: GR = Grid(d=d, n=n, l=l) self.assertIn(msg, str(ctx.exception))
def test_init6(self): d = -2 n = np.array([3]) l = np.array([[-2, 4]]) msg = 'Invalid number of dimensions (d).' with self.assertRaises(ValueError) as ctx: GR = Grid(d=d, n=n, l=l) self.assertIn(msg, str(ctx.exception))
def test_2d_eul(self): TG = Grid(1, 2, [0., 0.001], k='u') r0 = np.vstack([ np.arange(100) * 1.1 + 0.2, np.arange(100) * 1.5 + 0.3, ]) r_real = func_rt_2d(TG.l2, r0) SL = OrdSolver(TG, 'eul').init(func_f0_2d, with_y0=True) e = np.mean(np.abs((r_real - SL.comp(r0)) / r_real)) self.assertTrue(e < 1.E-07)
def test_4d_tt(self): GR = Grid(d=4, n=[30, 31, 32, 33], l=[ [-3., 4.], [-2., 3.], [-1., 1.], [-1., 3.], ]) FN = Func(GR, with_tt=True).init(func_4d).prep().calc() self.assertTrue(np.max(FN.test(1000, is_u=True)) < 9.0E-09)
def test_mean(self): n = np.array([8, 10]) l = np.array([ [-4., 3.], [-2., 5.], ]) GR = Grid(2, n, l) np.testing.assert_equal(GR.n0, 9) np.testing.assert_almost_equal(GR.l1, -3.) np.testing.assert_almost_equal(GR.l2, +4.) np.testing.assert_equal(GR.h0, 0.5 * (7./7. + 7./9.))
def test_init(self): d = 3 n = np.array([5, 7, 8]) l = np.array([[-2., 3.], [-4., 5.], [-6., 9.]]) GR = Grid(d=d, n=n, l=l) self.assertEqual( GR.d, 3 ) np.testing.assert_array_equal( GR.n, np.array([5, 7, 8]) ) np.testing.assert_almost_equal( GR.l, np.array([[-2., 3.], [-4., 5.], [-6., 9.]]) )
def test_pars(self): d = 1 n = [11] l = [[-4., 6.]] k = 'u' e = 1.E-10 GR = Grid(d, n, l, k, e) np.testing.assert_equal(GR.d, d) np.testing.assert_array_equal(GR.n, np.array(n)) np.testing.assert_array_equal(GR.l, np.array(l)) self.assertEqual(GR.k, k) self.assertEqual(GR.e, e) np.testing.assert_array_equal(GR.h, [1.]) self.assertEqual(GR.n0, +11) self.assertEqual(GR.l1, -4.) self.assertEqual(GR.l2, +6.) self.assertEqual(GR.h0, +1.)
def test_pars(self): d = 2 n = [11, 5] l = [[-4., 6.], [2., 4.]] k = 'u' e = 1.E-10 GR = Grid(d, n, l, k, e) self.assertEqual(GR.d, d) np.testing.assert_array_equal(GR.n, n) np.testing.assert_array_equal(GR.l, np.array(l)) self.assertEqual(GR.k, k) self.assertEqual(GR.e, e) np.testing.assert_array_equal(GR.h, [1., 0.5]) np.testing.assert_array_equal(GR.n0, 8) self.assertEqual(GR.l1, -1.) self.assertEqual(GR.l2, +5.) self.assertEqual(GR.h0, +0.75)
def test_pars(self): d = 3 n = [8, 7, 11] l = [[-4., 3.], [-1., 2.], [-5., 5.]] k = 'u' e = 1.E-10 GR = Grid(d, n, l, k, e) self.assertEqual(GR.d, d) np.testing.assert_array_equal(GR.n, n) np.testing.assert_array_equal(GR.l, np.array(l)) self.assertEqual(GR.k, k) self.assertEqual(GR.e, e) np.testing.assert_array_equal(GR.h, [1., 0.5, 1. ]) self.assertEqual(GR.n0, 9) self.assertEqual(GR.l1, -3.3333333333333335) self.assertEqual(GR.l2, +3.3333333333333335) self.assertEqual(GR.h0, +2.5 / 3)
def test_is_sym_zero(self): self.assertTrue(Grid(n=5, l=[[-3., 3.]], k='u').is_sym_zero()) self.assertFalse(Grid(n=5, l=[[-4., 3.]], k='u').is_sym_zero()) self.assertFalse(Grid(n=5, l=[[1., 3.]], k='u').is_sym_zero())