def test_create_domain_1d(self): domain = dg_domain.Domain(extents=[(0, 2)], num_elements=2, num_points=3) self.assertEqual(domain.dim, 1) self.assertEqual(len(domain.elements), 2) self.assertEqual(domain.get_total_num_points(), 6)
def test_set_data(self): domain = dg_domain.Domain(extents=[(0, 1), (0, 2)], num_elements=2, num_points=3) def scalar_field(x, amplitude): return amplitude * np.sqrt(x[0]**2 + x[1]**2) def vector_field(x, amplitude): return amplitude * x domain.set_data(scalar_field, 'u', amplitude=2) npt.assert_almost_equal(domain.indexed_elements[(0, 0)].u[:, 0], [0, 0.5, 1]) npt.assert_almost_equal(domain.indexed_elements[(0, 0)].u[0, :], [0, 1, 2]) domain.set_data(vector_field, 'v', amplitude=2) npt.assert_almost_equal(domain.indexed_elements[(0, 0)].v[0, :, 0], [0, 0.5, 1]) npt.assert_almost_equal(domain.indexed_elements[(0, 0)].v[1, 0, :], [0, 1, 2]) domain.set_data(domain.get_data('u'), 'u2') npt.assert_equal(domain.get_data('u2'), domain.get_data('u')) domain.set_data(domain.get_data('v'), 'v2', 1) npt.assert_equal(domain.get_data('v2'), domain.get_data('v')) domain.set_data(domain.get_data(['u', 'v']), ['u3', 'v3'], [0, 1]) npt.assert_equal(domain.get_data('u3'), domain.get_data('u')) npt.assert_equal(domain.get_data('v3'), domain.get_data('v'))
def test_interpolate_1d(self): domain = dg_domain.Domain(extents=[(-1, 1)], num_elements=1, num_points=3) e = list(domain.elements)[0] x = e.inertial_coords[0] u = x**2 + 2. * x + 3. target_points = lgl_points(4) u_interp = dg_operators.interpolate_to(u, e, [target_points]) x_target = inertial_coords([target_points], e.extents)[0] u_expected = x_target**2 + 2. * x_target + 3. npt.assert_allclose(u_interp, u_expected)
def test_interpolate_2d(self): domain = dg_domain.Domain(extents=2 * [(-1, 1)], num_elements=1, num_points=3) e = list(domain.elements)[0] x, y = e.inertial_coords u = x**2 * y**2 + 2. * x + 3. * y + 4. e_target = dg_domain.Element(e.extents, num_points=[4, 4], quadrature=e.quadrature) u_interp = dg_operators.interpolate_to(u, e, e_target.collocation_points) x_target, y_target = e_target.inertial_coords u_expected = x_target**2 * y_target**2 + 2. * x_target + 3. * y_target + 4. npt.assert_allclose(u_interp, u_expected)
def test_mass_matrix(self): """Tests that the mass matrix is M_ij = integrate(l_i * l_j, -1, 1)""" domain = dg_domain.Domain(extents=[(-1, 1)], num_elements=1, num_points=3) e = list(domain.elements)[0] M = dg_operators.mass_matrix(e, 0) def m(x, i, j): return lagrange_polynomial(e.collocation_points[0], i)(x) * lagrange_polynomial( e.collocation_points[0], j)(x) M_expected = np.zeros((e.num_points[0], e.num_points[0])) for i in range(e.num_points[0]): for j in range(e.num_points[0]): M_expected[i, j] = scipy.integrate.quad(m, -1, 1, args=(i, j))[0] npt.assert_allclose(M, M_expected, rtol=100 * self.eps)