def test_identity(self): x = np.linspace(-1, 1, 100) u = x**2 identity = Identity() assert_array_equal(u, identity(u)) twice_id = Coef(2) * Identity() assert_array_equal(2 * u, twice_id(u)) x_id = Coef(x) * Identity() assert_array_equal(x * u, x_id(u))
def test_identity_2d(self): (X, Y), (x, y), _ = grid(2, 100, -1, 1) u = X**2 + Y**2 identity = Identity() assert_array_equal(u, identity(u)) twice_id = Coef(2) * Identity() assert_array_equal(2 * u, twice_id(u)) x_id = Coef(X) * Identity() assert_array_equal(X * u, x_id(u)) dx = x[1] - x[0] d_dx = FinDiff(0, dx) linop = d_dx + 2 * Identity() assert_array_almost_equal(2 * X + 2 * u, linop(u))
def test_1d_oscillator_free_dirichlet(self): n = 300 shape = n, t = np.linspace(0, 5, n) dt = t[1] - t[0] L = FinDiff(0, dt, 2) + Identity() bc = BoundaryConditions(shape) bc[0] = 1 bc[-1] = 2 eq = PDE(L, np.zeros_like(t), bc) u = eq.solve() expected = np.cos(t) - (np.cos(5) - 2) * np.sin(t) / np.sin(5) np.testing.assert_array_almost_equal(expected, u, decimal=4)
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_eigs(self): shape = 8, x = np.linspace(-10, 10, shape[0]) dx = x[1] - x[0] hbar = 1 m = 1 omega = 1 T = Coef(-hbar**2 / (2 * m)) * FinDiff(0, dx, 2) V = Coef(0.5 * omega * x**2) * Identity() H = T + V print("\n", T.matrix(shape).toarray()) print("\n", V.matrix(shape).toarray()) print("\n", H.matrix(shape).toarray()) print(H.matrix(shape).toarray()) vals, vecs = H.eigs(shape) print(vals) print(vecs)