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)
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_2d_inhom_var_coefs_with_identity_all_dirichlet(self): shape = (5, 5) (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) + Coef(5*X*Y) * Identity() L = Coef(5 * X * Y) * FinDiff(0, dx, 2) #Identity() #f = 18 * X**2 + 8*Y + 5*X*Y*expected mat = L.matrix(shape) print(mat) 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)