def test_1d_linear_combination(self): x = np.sqrt(np.linspace(0, 1, 10)) d1 = Coef(x) * FinDiff(0, x, acc=4) d2 = Coef(x**2) * FinDiff(0, x, 2, acc=4) assert_array_almost_equal(3 * x**3, d1(x**3)) assert_array_almost_equal(6 * x**3, d2(x**3)) assert_array_almost_equal(9 * x**3, (d1 + d2)(x**3))
def test_matrix_1d(self): x = np.linspace(0, 6, 7) d2_dx2 = FinDiff(0, x[1]-x[0], 2) u = x**2 mat = d2_dx2.matrix(u.shape) np.testing.assert_array_almost_equal(2*np.ones_like(x), mat.dot(u.reshape(-1)))
def test_3d_linear_combination(self): x, y, z = [np.sqrt(np.linspace(0, 1, 10))] * 3 X, Y, Z = np.meshgrid(x, y, z, indexing='ij') d1 = Coef(X) * FinDiff(0, x, acc=4) d2 = Coef(Y) * FinDiff(1, y, acc=4) assert_array_almost_equal(3 * X ** 3, d1(X**3 + Y**3 + Z**3)) assert_array_almost_equal(3 * Y ** 3, d2(X**3 + Y**3 + Z**3)) assert_array_almost_equal(3 * (X**3 + Y**3), (d1 + d2)(X**3 + Y**3 + Z**3))
def test_matrix_2d_mixed_nonuni(self): x, y = [np.linspace(0, 5, 6), np.linspace(0, 6, 7)] X, Y = np.meshgrid(x, y, indexing='ij') d2_dxdy = FinDiff((0, x), (1, y)) u = X**2 * Y**2 mat = d2_dxdy.matrix(u.shape) expected = d2_dxdy(u).reshape(-1) actual = mat.dot(u.reshape(-1)) np.testing.assert_array_almost_equal(expected, actual)
def test_matrix_3d_nonuni_performance(self): x = y = z = np.linspace(0, 4, 30) X, Y, Z = np.meshgrid(x, y, z, indexing='ij') laplace = FinDiff(0, x, 2) + FinDiff(1, y, 2) + FinDiff(2, z, 2) u = X**2 + Y**2 + Z**2 mat = laplace.matrix(u.shape) np.testing.assert_array_almost_equal(6 * np.ones_like(X).reshape(-1), mat.dot(u.reshape(-1)))
def test_local_stencil_operator_with_coef(self): x = np.linspace(0, 10, 101) y = np.linspace(0, 10, 101) X, Y = np.meshgrid(x, y, indexing='ij') u = X * Y dx = x[1] - x[0] dy = y[1] - y[0] d1x = Coef(2) * FinDiff(0, dx) * FinDiff(1, dy) stencil1 = d1x.stencil(u.shape) du_dx = stencil1.apply_all(u) np.testing.assert_array_almost_equal(2 * np.ones_like(X), du_dx)
def test_local_stencil_operator_mixed_partials(self): x = np.linspace(0, 10, 101) y = np.linspace(0, 10, 101) X, Y = np.meshgrid(x, y, indexing='ij') u = X * Y dx = x[1] - x[0] dy = y[1] - y[0] d1x = FinDiff((0, dx), (1, dy)) stencil1 = d1x.stencil(u.shape) du_dx = stencil1.apply_all(u) np.testing.assert_array_almost_equal(np.ones_like(X), du_dx)
def test_local_stencil_operator_addition(self): n = 100 (X, Y), _, (dx, dy) = grid(2, n, -1, 1) u = X**3 + Y**3 d = FinDiff(0, dx, 2) + FinDiff(1, dy, 2) expected = d(u) stl = d.stencil(u.shape) actual = stl.apply_all(u) np.testing.assert_array_almost_equal(expected, actual)
def test_plus(self): (X, Y), _, h = grid(2, 50, 0, 1) u = X**2 + Y**2 d_dx = FinDiff(0, h[0]) d_dy = FinDiff(1, h[1]) d = d_dx + d_dy u1 = d(u) u1_ex = 2 * X + 2 * Y assert_array_almost_equal(u1, u1_ex)
def test_partial_diff(self): nx = 100 x = np.linspace(0, np.pi, nx) u = np.sin(x) ux_ex = np.cos(x) fd = FinDiff(0, x[1] - x[0], 1) fd.set_accuracy(4) ux = fd(u) assert_array_almost_equal(ux, ux_ex, decimal=5) ny = 100 y = np.linspace(0, np.pi, ny) X, Y = np.meshgrid(x, y, indexing='ij') u = np.sin(X) * np.sin(Y) uxy_ex = np.cos(X) * np.cos(Y) fd = FinDiff((0, x[1] - x[0], 1), (1, y[1] - y[0], 1)) fd.set_accuracy(4) uxy = fd(u) assert_array_almost_equal(uxy, uxy_ex, decimal=5)
def test_local_stencil_single_axis_center_2d_compared_with_findiff(self): n = 70 (X, Y), _, (dx, dy) = grid(2, n, -1, 1) u = X**3 * Y**3 d4_dx2dy2 = FinDiff(1, dx, 2) expected = d4_dx2dy2(u) stl = d4_dx2dy2.stencil(u.shape) actual = stl.apply_all(u) np.testing.assert_array_almost_equal(expected, actual)
def test_1d_different_accs(self): x = np.r_[np.arange(0, 4, 0.05), np.arange(4, 10, 1)] f = np.exp(-x**2) d_dx = FinDiff(0, x, acc=4) f_x = d_dx(f) assert_array_almost_equal(-2 * x * np.exp(-x**2), f_x, decimal=4) # same, but this time with default acc x = np.linspace(0, 1, 100) f = np.exp(-x**2) d_dx = FinDiff(0, x) f_x = d_dx(f) assert_array_almost_equal(-2 * x * np.exp(-x**2), f_x, decimal=4)
def test_3d_different_accs(self): x = np.r_[np.arange(0, 4, 0.05), np.arange(4, 10, 1)] y = np.r_[np.arange(0, 4, 0.05), np.arange(4, 10, 1)] z = np.r_[np.arange(0, 4.5, 0.05), np.arange(4.5, 10, 1)] X, Y, Z = np.meshgrid(x, y, z, indexing='ij') f = np.exp(-X**2 - Y**2 - Z**2) d_dy = FinDiff(1, y, acc=4) fy = d_dy(f) fye = -2 * Y * np.exp(-X**2 - Y**2 - Z**2) assert_array_almost_equal(fy, fye, decimal=4) d_dy = FinDiff(1, y, acc=6) fy = d_dy(f) assert_array_almost_equal(fy, fye, decimal=4)
def test_several_tuples_as_args(self): x, y, z = [np.sqrt(np.linspace(0, 1, 10))] * 3 X, Y, Z = np.meshgrid(x, y, z, indexing='ij') d1 = FinDiff((0, x), (1, y), acc=4) assert_array_almost_equal(d1(X * Y), np.ones_like(X))
def test_1d_different_accs(self): x = np.r_[np.arange(0, 4, 0.05), np.arange(4, 10, 1)] f = np.exp(-x**2) d_dx = FinDiff(0, x, acc=4) f_x = d_dx(f) assert_array_almost_equal(-2 * x * np.exp(-x**2), f_x, decimal=4)
def test_matrix_2d(self): thr = np.get_printoptions()["threshold"] lw = np.get_printoptions()["linewidth"] np.set_printoptions(threshold=np.inf) np.set_printoptions(linewidth=500) x, y = [np.linspace(0, 5, 6)] * 2 X, Y = np.meshgrid(x, y, indexing='ij') laplace = FinDiff(0, x[1]-x[0], 2) + FinDiff(0, y[1]-y[0], 2) u = X**2 + Y**2 mat = laplace.matrix(u.shape) print(mat.toarray()) np.testing.assert_array_almost_equal(4 * np.ones_like(X).reshape(-1), mat.dot(u.reshape(-1))) np.set_printoptions(threshold=thr) np.set_printoptions(linewidth=lw)
def test_various_input_formats(self): def f(x): return x**3 def df_dx(x): return 6*x x_nu = np.sqrt(np.linspace(0, 1, 10)) f_nu = f(x_nu) self.assertRaises(Exception, lambda: FinDiff(0, 2, coords=[x_nu], acc=2)) d_dx_B = FinDiff(0, x_nu, 2, acc=4) df_dx_nu_B = d_dx_B(f_nu) assert_array_almost_equal(df_dx(x_nu), df_dx_nu_B)
def test_minus(self): (X, Y), _, h = grid(2, 50, 0, 1) u = X**2 + Y**2 #import pdb #pdb.set_trace() d_dx = FinDiff(0, h[0]) d_dy = FinDiff(1, h[1]) d = d_dx - d_dy u1 = d(u) u1_ex = 2 * X - 2 * Y assert_array_almost_equal(u1, u1_ex)
def test_1d_neumann_hom(self): nx = 11 shape = (nx, ) x = np.linspace(0, 1, nx) dx = x[1] - x[0] L = FinDiff(0, dx, 2) bc = BoundaryConditions(shape) bc[0] = 1 bc[-1] = FinDiff(0, dx, 1), 2 pde = PDE(L, np.zeros_like(x), bc) u = pde.solve() expected = 2 * x + 1 np.testing.assert_array_almost_equal(expected, u)
def test_mixed_partials(self): (X, Y, Z), _, (dx, dy, dz) = grid(3, 50, 0, 1) u = X**2 * Y**2 * Z**2 d3_dxdydz = FinDiff((0, dx), (1, dy), (2, dz)) diffed = d3_dxdydz(u) assert_array_almost_equal(8 * X * Y * Z, diffed)
def test_1d_oscillator_driv_neumann(self): n = 200 shape = n, t = np.linspace(0, 1, n) dt = t[1] - t[0] L = FinDiff(0, dt, 2) - FinDiff(0, dt) + Identity() f = -3 * np.exp(-t) * np.cos(t) + 2 * np.exp(-t) * np.sin(t) expected = np.exp(-t) * np.sin(t) bc = BoundaryConditions(shape) bc[0] = FinDiff(0, dt), 1 bc[-1] = expected[-1] eq = PDE(L, f, bc) u = eq.solve() np.testing.assert_array_almost_equal(expected, u, decimal=4)
def test_spac(self): (X, Y), _, (dx, dy) = grid(2, 100, -1, 1) u = X**2 + Y**2 d_dx = FinDiff(0, dx) d_dy = FinDiff(1, dy) assert_array_almost_equal(2 * X, d_dx(u)) assert_array_almost_equal(2 * Y, d_dy(u)) d_dx = FinDiff(0, dx) d_dy = FinDiff(1, dy) u = X * Y d2_dxdy = d_dx * d_dy assert_array_almost_equal(np.ones_like(u), d2_dxdy(u))
def test_partial_diff_1d(self): nx = 11 x = np.linspace(0, 1, nx) u = x**3 ux_ex = 3 * x**2 fd = FinDiff(0, x[1] - x[0], 1) ux = fd(u, acc=4) assert_array_almost_equal(ux, ux_ex, decimal=5)
def test_2d_inhom_var_coefs_dirichlet_all(self): shape = (41, 50) (x, y), (dx, dy), (X, Y) = make_grid(shape, edges=[(-1, 1), (-1, 1)]) expected = X**3 + Y**3 + X * Y + 1 L = Coef(3 * X) * FinDiff(0, dx, 2) + Coef(2 * Y) * FinDiff( (0, dx, 1), (1, dy, 1)) + FinDiff(1, dy, 2) f = 18 * X**2 + 8 * Y bc = BoundaryConditions(shape) bc[0, :] = expected bc[-1, :] = expected bc[:, 0] = expected bc[:, -1] = expected pde = PDE(L, f, bc) actual = pde.solve() np.testing.assert_array_almost_equal(expected, actual, decimal=4)
def test_local_stencil_single_axis_center_1d(self): x = np.linspace(0, 1, 50) dx = x[1] - x[0] u = x**3 d2_dx2 = FinDiff((0, dx, 2)) stl = d2_dx2.stencil(u.shape) idx = 5 actual = stl.apply(u, idx) d2u_dx2 = d2_dx2(u) expected = d2u_dx2[idx] self.assertAlmostEqual(expected, actual) actual = stl.apply_all(u) expected = d2u_dx2 np.testing.assert_array_almost_equal(expected, actual)
def test_multiply(self): (X, Y), _, h = grid(2, 5, 0, 1) u = X**2 + Y**2 d2_dx2 = FinDiff(0, h[0], 2) d = Coef(X) * d2_dx2 u1 = d(u) assert_array_almost_equal(u1, 2 * X)
def test_matrix_1d_coeffs_nonuni(self): shape = 11, x = np.linspace(0, 10, 11) L = Coef(x) * FinDiff(0, x, 2) u = np.random.rand(*shape).reshape(-1) actual = L.matrix(shape).dot(u) expected = L(u).reshape(-1) np.testing.assert_array_almost_equal(expected, actual)
def test_multiply_operators(self): (X, Y), _, h = grid(2, 50, 0, 1) u = X**2 + Y**2 d_dx = FinDiff(0, h[0]) d2_dx2_test = d_dx * d_dx uxx = d2_dx2_test(u) assert_array_almost_equal(uxx, np.ones_like(X) * 2)
def test_2d_dirichlet_inhom(self): shape = (11, 11) x, y = np.linspace(0, 1, shape[0]), np.linspace(0, 1, shape[1]) dx, dy = x[1] - x[0], y[1] - y[0] X, Y = np.meshgrid(x, y, indexing='ij') L = FinDiff(0, dx, 2) + FinDiff(1, dy, 2) expected = X**3 + Y**3 + 1 f = 6 * X + 6 * Y bc = BoundaryConditions(shape) bc[0, :] = expected bc[-1, :] = expected bc[:, 0] = expected bc[:, -1] = expected pde = PDE(L, f, bc) u = pde.solve() np.testing.assert_array_almost_equal(expected, u)
def test_mixed_equation__with_coeffs_2d(self): shape = (41, 51) x, y = np.linspace(0, 1, shape[0]), np.linspace(0, 1, shape[1]) dx, dy = x[1] - x[0], y[1] - y[0] X, Y = np.meshgrid(x, y, indexing='ij') L = FinDiff(0, dx, 2) + Coef(X * Y) * FinDiff( (0, dx, 1), (1, dy, 1)) + FinDiff(1, dy, 2) expected = X**3 + Y**3 + 1 f = 6 * (X + Y) bc = BoundaryConditions(shape) bc[0, :] = expected bc[-1, :] = expected bc[:, 0] = expected bc[:, -1] = expected pde = PDE(L, f, bc) u = pde.solve() np.testing.assert_array_almost_equal(expected, u, decimal=4)