def int_with_deg(n_elements, element_deg): msh = simple_line_mesh(n_elements) bf = basis_from_degree(element_deg) apply_to_elements(msh, "basis", bf, non_gen = True) apply_to_elements(msh, "continuous", False, non_gen = True) init_dofs(msh) fnc = lambda x, n: (x[0], x[1]) val = interpolate(fnc, msh) return val
def test_rhs(): msh = rhs_assembler() kernel = TractionKernel(1.0, 0.25) # If we sum across rows, we should get the RHS value for # a function that is 1 everywhere rhs_correct = np.sum(correct_matrix, axis = 0) f = lambda x, n: [1.0, 1.0] apply_coeffs(msh, interpolate(f, msh), "rhs_f") # Make the function look like a basis function. It is one! The only one! rhs = simple_rhs_assemble(msh, lambda e: e.rhs_f, kernel) np.testing.assert_almost_equal(rhs_correct, rhs, 3)
def test_interpolate_normal(): n_elements = 2 element_deg = 0 msh = simple_line_mesh(n_elements) bf = basis_from_degree(element_deg) apply_to_elements(msh, "basis", bf, non_gen = True) apply_to_elements(msh, "continuous", False, non_gen = True) init_dofs(msh) fnc = lambda x, n: (x[0] * n[0], x[1]) val = interpolate(fnc, msh) # All zero! assert((val[:] == 0).all())
def test_interior_point_traction_adjoint(): msh = int_pt_test_setup(50) def section_traction(x, n): if np.abs(x[0]) < np.cos(24 * (np.pi / 50)): x_length = np.sqrt(x[0] ** 2 + x[1] ** 2) return (-x[0] / x_length, -x[1] / x_length) return (0.0, 0.0) traction_coeffs = interpolate(section_traction, msh) apply_coeffs(msh, traction_coeffs, "trac_fnc") kernel = AdjointTractionKernel(1.0, 0.25) pts_normals = (np.array([0.5, 0.0]), np.array([1.0, 0.0])) result = interior_pt(msh, pts_normals, kernel, lambda e: e.trac_fnc) np.testing.assert_almost_equal(result[0], -0.002094,4) np.testing.assert_almost_equal(result[1], 0.0)
def test_mass_matrix_functional(): bf = basis_funcs.basis_from_nodes([0.001, 0.999]) msh = simple_line_mesh(2) q = quadrature.gauss(2) apply_to_elements(msh, "basis", bf, non_gen = True) apply_to_elements(msh, "continuous", False, non_gen = True) init_dofs(msh) fnc_coeffs = interpolate(lambda x,d: [[1,2][x[0] >= 0]] * 2, msh) apply_coeffs(msh, fnc_coeffs, "function_yay") basis_grabber = lambda e: e.function_yay m = mass_matrix_for_rhs(assemble_mass_matrix(msh, q, basis_grabber)) M_exact = [0.5, 0.5, 1.0, 1.0] np.testing.assert_almost_equal(M_exact, m[0:4])
def test_evaluate_solution_on_element(): n_elements = 2 element_deg = 1 msh = simple_line_mesh(n_elements) bf = basis_from_degree(element_deg) apply_to_elements(msh, "basis", bf, non_gen = True) apply_to_elements(msh, "continuous", False, non_gen = True) init_dofs(msh) fnc = lambda x, n: (x[0], x[1]) solution = interpolate(fnc, msh) msh.elements[1].bc = BC("traction", msh.elements[1].basis) u, t = evaluate_solution_on_element(msh.elements[1], 1.0, solution) assert(u[0] == 1.0) assert(u[1] == 0.0)
def test_interpolate_evaluate_hard(): n_elements = 5 # Sixth order elements should exactly interpolate a sixth order polynomial. element_deg = 6 msh = simple_line_mesh(n_elements) bf = basis_from_degree(element_deg) apply_to_elements(msh, "basis", bf, non_gen = True) apply_to_elements(msh, "continuous", False, non_gen = True) init_dofs(msh) fnc = lambda x, n: (x[0] ** 6, 0) solution = interpolate(fnc, msh) apply_to_elements(msh, "bc", BC("traction", msh.elements[0].basis), non_gen = True) x, u, t = evaluate_boundary_solution(msh, solution, 5) assert(x[0][-2] == 0.9) np.testing.assert_almost_equal(u[0][-2], (0.9 ** 6))
def test_realistic_zero_discontinuity(): msh = realistic_assembler(element_deg = 1) k_d = DisplacementKernel(1.0, 0.25) k_t = TractionKernel(1.0, 0.25) G = simple_matrix_assemble(msh, k_d) H = simple_matrix_assemble(msh, k_t) fnc = lambda x, n: (0.0, 1.0) displacements = tools.interpolate(fnc, msh) rhs = np.dot(H, displacements) soln_coeffs = np.linalg.solve(G, rhs) apply_to_elements(msh, "bc", BC("traction", msh.elements[0].basis), non_gen = True) for k in range(msh.n_elements - 1): e_k = msh.elements[k] e_kp1 = msh.elements[k + 1] value_left,t = evaluate_solution_on_element(e_k, 1.0, soln_coeffs) value_right,t = evaluate_solution_on_element(e_kp1, 0.0, soln_coeffs) np.testing.assert_almost_equal(value_left, value_right)
def test_evaluate_boundary_solution_easy(): n_elements = 2 element_deg = 0 msh = simple_line_mesh(n_elements) bf = basis_from_degree(element_deg) apply_to_elements(msh, "basis", bf, non_gen = True) apply_to_elements(msh, "continuous", False, non_gen = True) init_dofs(msh) fnc = lambda x, n: (x[0], x[1]) solution = interpolate(fnc, msh) apply_to_elements(msh, "bc", BC("traction", msh.elements[0].basis), non_gen = True) x, u, t = evaluate_boundary_solution(msh, solution, 11) # Constant basis, so it should be 0.5 everywhere on the element [0,1] assert(x[0][-2] == 0.9) assert(u[0][-2] == 0.5) assert(u[1][-2] == 0.0)