def test_assembly_inner_product_2_forms(self): """Test the assembly of 1-forms inner products.""" func_space_lob = FunctionSpace(self.mesh, '2-lobatto', self.p) func_space_gauss = FunctionSpace(self.mesh, '2-gauss', self.p) func_space_extgauss = FunctionSpace(self.mesh, '2-ext_gauss', self.p) basis_lob = BasisForm(func_space_lob) basis_lob.quad_grid = 'gauss' M_lob = inner(basis_lob, basis_lob) basis_gauss = BasisForm(func_space_gauss) basis_gauss.quad_grid = 'lobatto' M_gauss = inner(basis_gauss, basis_gauss) basis_ext_gauss = BasisForm(func_space_extgauss) print(basis_ext_gauss.num_basis) basis_ext_gauss.quad_grid = 'lobatto' M_extgauss = inner(basis_ext_gauss, basis_ext_gauss) M_lob_ass_ref = assemble_slow(self.mesh, M_lob, func_space_lob.dof_map.dof_map, func_space_lob.dof_map.dof_map) M_gauss_ass_ref = assemble_slow(self.mesh, M_gauss, func_space_gauss.dof_map.dof_map, func_space_gauss.dof_map.dof_map) M_extgauss_ass_ref = assemble_slow( self.mesh, M_extgauss, func_space_extgauss.dof_map.dof_map_internal, func_space_extgauss.dof_map.dof_map_internal) M_lob_ass = assemble(M_lob, func_space_lob, func_space_lob).toarray() M_gauss_ass = assemble(M_gauss, func_space_gauss, func_space_gauss).toarray() M_extgauss_ass = assemble(M_extgauss, func_space_extgauss, func_space_extgauss).toarray() npt.assert_array_almost_equal(M_lob_ass_ref, M_lob_ass) npt.assert_array_almost_equal(M_gauss_ass_ref, M_gauss_ass) npt.assert_array_almost_equal(M_extgauss_ass_ref, M_extgauss_ass)
def multiple_element(): mesh = CrazyMesh(2, (5, 7), ((-1, 1), (-1, 1)), curvature=0.3) p = 10, 10 func_space_vort = FunctionSpace(mesh, '0-ext_gauss', (p[0] - 1, p[1] - 1), is_inner=False) func_space_outer_vel = FunctionSpace( mesh, '1-total_ext_gauss', (p[0], p[1]), is_inner=False) # func_space_outer_vel = FunctionSpace( # mesh, '1-gauss', (p[0], p[1]), is_inner=False) func_space_inner_vel = FunctionSpace(mesh, '1-lobatto', p, is_inner=True) func_space_inner_vel.dof_map.continous_dof = True func_space_source = FunctionSpace(mesh, '2-lobatto', p, is_inner=True) basis_vort = BasisForm(func_space_vort) basis_vel_in = BasisForm(func_space_inner_vel) basis_vel_in.quad_grid = 'lobatto' basis_vel_out = BasisForm(func_space_outer_vel) basis_vel_out.quad_grid = 'lobatto' basis_2 = BasisForm(func_space_source) psi = Form(func_space_vort) u_in = Form(func_space_inner_vel) source = Form(func_space_source) source.discretize(ffun) M_1 = inner(basis_vel_in, basis_vel_in) E_21_in = d(func_space_inner_vel) W_02 = basis_2.wedged(basis_vort) W_02_E21 = np.transpose(W_02 @ E_21_in) M_1 = assemble(M_1, (func_space_inner_vel, func_space_inner_vel)) print(np.shape(M_1)) W_02_E21 = assemble(W_02_E21, (func_space_inner_vel, func_space_vort)) print(np.shape(W_02_E21)) W_02 = assemble(W_02, (func_space_source, func_space_vort)) lhs = spr.bmat([[M_1, W_02_E21], [W_02_E21.transpose(), None]]) print(np.shape(lhs)) rhs = np.zeros(np.shape(lhs)[0]) rhs[-func_space_source.num_dof:] = W_02 @ source.cochain solution = spr.linalg.spsolve(lhs.tocsc(), rhs) u_in.cochain = solution[:func_space_inner_vel.num_dof] cochian_psi = np.zeros(func_space_vort.num_dof) cochian_psi[:func_space_vort.num_internal_dof] = solution[-func_space_vort.num_internal_dof:] psi.cochain = cochian_psi xi = eta = np.linspace(-1, 1, 40) u_in.reconstruct(xi, eta) (x, y), u_x, u_y = u_in.export_to_plot() plt.contourf(x, y, u_x) plt.colorbar() plt.title("u_x inner") plt.show() psi.reconstruct(xi, eta) (x, y), psi_value = psi.export_to_plot() plt.contourf(x, y, psi_value) plt.title("psi outer" ) plt.colorbar() plt.show()
def test_basis_input(self): """Test the coboundary operator with some basis as inputs.""" p_x, p_y = 2, 2 func_space_0 = FunctionSpace(self.mesh, '0-lobatto', (p_x, p_y), is_inner=False) func_space_1 = FunctionSpace(self.mesh, '1-lobatto', (p_x, p_y), is_inner=False) func_space_2 = FunctionSpace(self.mesh, '2-lobatto', (p_x, p_y), is_inner=False) basis_0_ref = BasisForm(func_space_0) basis_1_ref = BasisForm(func_space_1) basis_0_ref.quad_grid = 'lobatto' basis_1_ref.quad_grid = 'gauss' basis_2_ref = BasisForm(func_space_2) basis_2_ref.quad_grid = 'lobatto' e_21_ref = d_21_lobatto_outer((p_x, p_y)) e_10_ref = d_10_lobatto_outer((p_x, p_y)) basis_1, e_10 = d(basis_0_ref) basis_1.quad_grid = 'gauss' basis_2, e_21 = d(basis_1_ref) basis_2.quad_grid = 'lobatto' M_1 = inner(basis_1, basis_1) M_1_ref = inner(basis_1_ref, basis_1_ref) npt.assert_array_almost_equal(M_1_ref, M_1) M_2 = inner(basis_2, basis_2) M_2_ref = inner(basis_2_ref, basis_2_ref) npt.assert_array_almost_equal(M_2_ref, M_2) npt.assert_array_equal(e_21_ref, e_21) npt.assert_array_equal(e_10_ref, e_10)
def test_basis_inner(self): """Test inner product with basis functions.""" p_x, p_y = 2, 2 func_space_0 = FunctionSpace(self.mesh, '0-lobatto', (p_x, p_y)) func_space_1 = FunctionSpace(self.mesh, '1-lobatto', (p_x, p_y)) basis_0 = BasisForm(func_space_0) basis_1 = BasisForm(func_space_1) basis_1.quad_grid = 'lobatto' basis_0.quad_grid = 'lobatto' M_1 = inner(basis_1, basis_1) e_10 = d(func_space_0) inner_prod_ref = np.tensordot(e_10, M_1, axes=((0), (0))) # inner_prod = inner(d(basis_0), basis_1) npt.assert_array_almost_equal(inner_prod_ref, inner_prod) # inner_prod_1_ref = np.rollaxis( np.tensordot(M_1, e_10, axes=((0), (0))), 2) inner_prod_1 = inner(basis_1, d(basis_0)) npt.assert_array_almost_equal(inner_prod_1_ref, inner_prod_1) # inner_prod_2_ref = np.tensordot(e_10, np.tensordot(e_10, M_1, axes=((0), (0))), axes=((0), (1))) inner_prod_2 = inner(d(basis_0), d(basis_0)) # npt.assert_array_almost_equal(inner_prod_2_ref, inner_prod_2)
def test_multiple_elements(self): """Test the anysotropic case in multiple element.""" p = (6, 6) is_inner = False crazy_mesh = CrazyMesh(2, (8, 5), ((-1, 1), (-1, 1)), curvature=0.1) func_space_2_lobatto = FunctionSpace(crazy_mesh, '2-lobatto', p, is_inner) func_space_1_lobatto = FunctionSpace(crazy_mesh, '1-lobatto', p, is_inner) func_space_1_lobatto.dof_map.continous_dof = True def diffusion_11(x, y): return 4 * np.ones(np.shape(x)) def diffusion_12(x, y): return 3 * np.ones(np.shape(x)) def diffusion_22(x, y): return 5 * np.ones(np.shape(x)) def source(x, y): return -36 * np.pi ** 2 * np.sin(2 * np.pi * x) * np.sin(2 * np.pi * y) + 24 * np.pi ** 2 * np.cos(2 * np.pi * x) * np.cos(2 * np.pi * y) # mesh function to inject the anisotropic tensor mesh_k = MeshFunction(crazy_mesh) mesh_k.continous_tensor = [diffusion_11, diffusion_12, diffusion_22] # definition of the basis functions basis_1 = BasisForm(func_space_1_lobatto) basis_1.quad_grid = 'gauss' basis_2 = BasisForm(func_space_2_lobatto) basis_2.quad_grid = 'gauss' # solution form phi_2 = Form(func_space_2_lobatto) phi_2.basis.quad_grid = 'gauss' form_source = Form(func_space_2_lobatto) form_source.discretize(source, ('gauss', 30)) # find inner product M_1k = inner(basis_1, basis_1, mesh_k) N_2 = inner(d(basis_1), basis_2) M_2 = inner(basis_2, basis_2) # assemble M_1k = assemble(M_1k, func_space_1_lobatto) N_2 = assemble(N_2, (func_space_1_lobatto, func_space_2_lobatto)) M_2 = assemble(M_2, func_space_2_lobatto) lhs = sparse.bmat([[M_1k, N_2], [N_2.transpose(), None]]).tocsc() rhs_source = (form_source.cochain @ M_2)[:, np.newaxis] rhs_zeros = np.zeros(lhs.shape[0] - np.size(rhs_source))[:, np.newaxis] rhs = np.vstack((rhs_zeros, rhs_source)) solution = sparse.linalg.spsolve(lhs, rhs) phi_2.cochain = solution[-func_space_2_lobatto.num_dof:] # sample the solution xi = eta = np.linspace(-1, 1, 200) phi_2.reconstruct(xi, eta) (x, y), data = phi_2.export_to_plot() plt.contourf(x, y, data) plt.show() print("max value {0} \nmin value {1}" .format(np.max(data), np.min(data))) npt.assert_array_almost_equal(self.solution(x, y), data, decimal=2)
def test_single_element(self): """Test the anysotropic case in a single element.""" dim = 2 elements_layout = (1, 1) bounds_domain = ((-1, 1), (-1, 1)) curvature = 0.1 p = (20, 20) is_inner = False crazy_mesh = CrazyMesh(dim, elements_layout, bounds_domain, curvature) func_space_2_lobatto = FunctionSpace(crazy_mesh, '2-lobatto', p, is_inner) func_space_1_lobatto = FunctionSpace(crazy_mesh, '1-lobatto', p, is_inner) def diffusion_11(x, y): return 4 * np.ones(np.shape(x)) def diffusion_12(x, y): return 3 * np.ones(np.shape(x)) def diffusion_22(x, y): return 5 * np.ones(np.shape(x)) mesh_k = MeshFunction(crazy_mesh) mesh_k.continous_tensor = [diffusion_11, diffusion_12, diffusion_22] basis_1 = BasisForm(func_space_1_lobatto) basis_1.quad_grid = 'gauss' basis_2 = BasisForm(func_space_2_lobatto) basis_2.quad_grid = 'gauss' phi_2 = Form(func_space_2_lobatto) phi_2.basis.quad_grid = 'gauss' M_1k = inner(basis_1, basis_1, mesh_k) N_2 = inner(basis_2, d(basis_1)) def source(x, y): return -36 * np.pi ** 2 * np.sin(2 * np.pi * x) * np.sin(2 * np.pi * y) + 24 * np.pi ** 2 * np.cos(2 * np.pi * x) * np.cos(2 * np.pi * y) form_source = Form(func_space_2_lobatto) form_source.discretize(source) M_2 = inner(basis_2, basis_2) lhs = np.vstack((np.hstack((M_1k[:, :, 0], N_2[:, :, 0])), np.hstack( (np.transpose(N_2[:, :, 0]), np.zeros((np.shape(N_2)[1], np.shape(N_2)[1])))))) rhs = np.zeros((np.shape(lhs)[0], 1)) rhs[-np.shape(M_2)[0]:] = form_source.cochain @ M_2 phi_2.cochain = np.linalg.solve(lhs, rhs)[-phi_2.basis.num_basis:].flatten() xi = eta = np.linspace(-1, 1, 200) phi_2.reconstruct(xi, eta) (x, y), data = phi_2.export_to_plot() print("max value {0} \nmin value {1}" .format(np.max(data), np.min(data))) npt.assert_array_almost_equal(self.solution(x, y), data, decimal=2)
def test_assemble_incidence_matrices(self): p_dual = (self.p[0] - 1, self.p[1] - 1) func_space_lob_0 = FunctionSpace(self.mesh, '0-lobatto', self.p) func_space_extgauss_0 = FunctionSpace(self.mesh, '0-ext_gauss', p_dual) func_space_lob_1 = NextSpace(func_space_lob_0) func_space_extgauss_1 = FunctionSpace(self.mesh, '1-ext_gauss', p_dual) func_space_lob_2 = FunctionSpace(self.mesh, '2-lobatto', self.p) func_space_extgauss_2 = FunctionSpace(self.mesh, '2-ext_gauss', p_dual) e10_lob = d(func_space_lob_0) e21_lob = d(func_space_lob_1) e10_ext = d(func_space_extgauss_0) # e21_ext = d(func_space_extgauss_1) # e10_lob_assembled_ref = assemble_slow( self.mesh, e10_lob, func_space_lob_1.dof_map.dof_map, func_space_lob_0.dof_map.dof_map, mode='replace') e21_lob_assembled_ref = assemble_slow( self.mesh, e21_lob, func_space_lob_2.dof_map.dof_map, func_space_lob_1.dof_map.dof_map, mode='replace') e10_ext_assembled_ref = assemble_slow( self.mesh, e10_ext, func_space_extgauss_1.dof_map.dof_map, func_space_extgauss_0.dof_map.dof_map, mode='replace') # e21_ext_assembled_ref = assemble_slow( # self.mesh, e21_ext, func_space_extgauss_2.dof_map.dof_map, # func_space_extgauss_1.dof_map.dof_map, mode='replace') e10_lob_assembled = assemble(e10_lob, (func_space_lob_1, func_space_lob_0)).toarray() e21_lob_assembled = assemble(e21_lob, (func_space_lob_2, func_space_lob_1)).toarray() e10_ext_assembled = assemble(e10_ext, (func_space_extgauss_1, func_space_extgauss_0)).toarray() # e21_ext_assembled = assemble(e21_ext, func_space_extgauss_2, # func_space_extgauss_1).toarray() npt.assert_array_almost_equal(e10_lob_assembled_ref, e10_lob_assembled) npt.assert_array_almost_equal(e21_lob_assembled_ref, e21_lob_assembled) npt.assert_array_almost_equal(e10_ext_assembled_ref, e10_ext_assembled) # npt.assert_array_almost_equal(e21_ext_assembled_ref, e21_ext_assembled) e10_internal = d(func_space_extgauss_0)[:func_space_extgauss_1.num_internal_local_dof] e10_internal_assembled_ref = assemble_slow( self.mesh, e10_internal, func_space_extgauss_1.dof_map.dof_map_internal, func_space_extgauss_0.dof_map.dof_map) e10_internal_assembled = assemble( e10_internal, (func_space_extgauss_1, func_space_extgauss_0)).toarray() npt.assert_array_almost_equal(e10_internal_assembled_ref, e10_internal_assembled)
def test_discretize_lobatto(self): """Test the discretize method of the 2 - forms.""" my_mesh = CrazyMesh(2, (2, 2), ((-1, 1), (-1, 1)), curvature=0) p_int_0 = 2 for p in range(3, 8): filename = 'discrete_2_form_p' + \ str(p) + '_p_int' + str(p_int_0) + '.dat' directory = os.getcwd() + '/src/tests/test_discrete_2_form/' func_space = FunctionSpace(my_mesh, '2-lobatto', p) form = Form(func_space) form.discretize(paraboloid, ('gauss', p_int_0)) cochain_ref = np.loadtxt(directory + filename, delimiter=',') npt.assert_array_almost_equal(form.cochain_local, cochain_ref, decimal=4) p = 5 for p_int in range(2, 6): filename = 'discrete_2_form_p' + \ str(p) + '_p_int' + str(p_int_0) + '.dat' directory = os.getcwd() + '/src/tests/test_discrete_2_form/' func_space = FunctionSpace(my_mesh, '2-lobatto', p) form = Form(func_space) form.discretize(paraboloid, ('gauss', p_int_0)) cochain_ref = np.loadtxt(directory + filename, delimiter=',') npt.assert_array_almost_equal(form.cochain_local, cochain_ref, decimal=4) p = 6 p_int = 5 filename_1 = 'discrete_2_form_p' + \ str(p) + '_p_int' + str(p_int) + '_el_57.dat' directory = os.getcwd() + '/src/tests/test_discrete_2_form/' mesh_1 = CrazyMesh(2, (5, 7), ((-1, 1), (-1, 1)), curvature=0) func_space = FunctionSpace(mesh_1, '2-lobatto', p) form = Form(func_space) form.discretize(paraboloid, ('gauss', p_int)) cochain_ref_1 = np.loadtxt(directory + filename_1, delimiter=',') npt.assert_array_almost_equal(form.cochain_local, cochain_ref_1) mesh_2 = CrazyMesh(2, (5, 7), ((-1, 1), (-1, 1)), curvature=0.2) filename_2 = 'discrete_2_form_p' + \ str(p) + '_p_int' + str(p_int) + '_el_57_cc2.dat' directory = os.getcwd() + '/src/tests/test_discrete_2_form/' func_space = FunctionSpace(mesh_2, '2-lobatto', p) form = Form(func_space) form.discretize(paraboloid, ('gauss', p_int)) cochain_ref_2 = np.loadtxt(directory + filename_2, delimiter=',') npt.assert_array_almost_equal(form.cochain_local, cochain_ref_2)
def test_project_lobatto(self): directory = 'src/tests/test_form_2/' x_ref = np.loadtxt(directory + 'x_lobatto_n3-2_p20-20_c3_xieta_30.dat', delimiter=',') y_ref = np.loadtxt(directory + 'y_lobatto_n3-2_p20-20_c3_xieta_30.dat', delimiter=',') data_ref = np.loadtxt(directory + 'data_lobatto_n3-2_p20-20_c3_xieta_30.dat', delimiter=',') def ffun(x, y): return np.sin(np.pi * x) * np.sin(np.pi * y) p = (20, 20) n = (3, 2) mesh = CrazyMesh(2, n, ((-1, 1), (-1, 1)), 0.3) xi = eta = np.linspace(-1, 1, 30) func_space = FunctionSpace(mesh, '2-lobatto', p) form_2 = Form(func_space) form_2.discretize(ffun) form_2.reconstruct(xi, eta) (x, y), data = form_2.export_to_plot() npt.assert_array_almost_equal(x_ref, x) npt.assert_array_almost_equal(y_ref, y) npt.assert_array_almost_equal(data_ref, data)
def test_inner(self): list_cases = [ 'p2_n2-2', 'p2_n3-2', 'p5_n1-10', 'p10_n2-2', 'p18_n14-14' ] p = [2, 2, 5, 10, 18] n = [(2, 2), (3, 2), (1, 10), (2, 2), (14, 10)] curvature = [0.2, 0.1, 0.2, 0.2, 0.2] for i, case in enumerate(list_cases[:-1]): print("Test case n : ", i) # basis = basis_forms. # print("theo size ", (p[i] + 1)**2 * # (p[i] + 1)**2 * n[i][0] * n[i][1]) M_0_ref = np.loadtxt( os.getcwd() + '/src/tests/test_M_0/M_0_' + case + '.dat', delimiter=',').reshape((p[i] + 1)**2, n[i][0] * n[i][1], (p[i] + 1)**2) # print(np.shape(M_0_ref)) my_mesh = CrazyMesh(2, n[i], ((-1, 1), (-1, 1)), curvature=curvature[i]) function_space = FunctionSpace(my_mesh, '0-lobatto', p[i]) basis = BasisForm(function_space) basis.quad_grid = 'lobatto' basis_1 = BasisForm(function_space) basis_1.quad_grid = 'lobatto' M_0 = inner(basis, basis_1) for el in range(n[i][0] * n[i][1]): npt.assert_array_almost_equal(M_0_ref[:, el, :], M_0[:, :, el], decimal=4)
def test_visulalization_extended_gauss_e10_inner(self): def pfun(x, y): return np.sin(np.pi * x) * np.sin(np.pi * y) p = (10, 10) n = (20, 20) mesh = CrazyMesh(2, n, ((-1, 1), (-1, 1)), 0.3) func_space_extGau = FunctionSpace(mesh, '0-ext_gauss', p) form_0_extG = Form(func_space_extGau) form_0_extG.discretize(self.pfun) xi = eta = np.linspace(-1, 1, 10) form_0_extG.reconstruct(xi, eta) (x, y), data = form_0_extG.export_to_plot() plt.contourf(x, y, data) plt.title('reduced extended_gauss 0-form') plt.colorbar() plt.show() form_1 = d(form_0_extG) form_1.reconstruct(xi, eta) (x, y), data_dx, data_dy = form_1.export_to_plot() ufun_x, ufun_y = self.ufun() plt.contourf(x, y, data_dx) plt.title('extended_gauss 1-form dx') plt.colorbar() plt.show() # plt.contourf(x, y, data_dy) plt.title('extended_gauss 1-form dy') plt.colorbar() plt.show() print(np.min(data_dy))
def test_visulization_lobato_e10_inner(self): def pfun(x, y): return np.sin(np.pi * x) * np.sin(np.pi * y) p = (20, 20) n = (2, 2) mesh = CrazyMesh(2, n, ((-1, 1), (-1, 1)), 0.2) xi = eta = np.linspace(-1, 1, 100) func_space = FunctionSpace(mesh, '0-lobatto', p) form_0 = Form(func_space) form_0.discretize(pfun) form_0.reconstruct(xi, eta) (x, y), data = form_0.export_to_plot() plt.contourf(x, y, data) plt.title('reduced lobatto 0-form') plt.colorbar() plt.show() form_1 = d(form_0) form_1.reconstruct(xi, eta) (x, y), data_dx, data_dy = form_1.export_to_plot() plt.contourf(x, y, data_dx) plt.title('lobatto 1-form dx') plt.colorbar() # plt.show() plt.contourf(x, y, data_dy) plt.title('lobatto 1-form dy') plt.colorbar() # plt.show() print(np.min(data_dy))
def test_inner(self): list_cases = [ 'p2_n2-2', 'p2_n3-2', 'p5_n1-10', 'p10_n2-2', 'p13_n12-8' ] p = [2, 2, 5, 10, 13] n = [(2, 2), (3, 2), (1, 10), (2, 2), (12, 8)] curvature = [0.1, 0.1, 0.1, 0.1, 0.1] for i, case in enumerate(list_cases[:-1]): print("Test case n : ", i) # basis = basis_forms. # print("theo size ", (p[i] + 1)**2 * # (p[i] + 1)**2 * n[i][0] * n[i][1]) M_2_ref = np.loadtxt( os.getcwd() + '/src/tests/test_M_2/M_2_' + case + '.dat', delimiter=',').reshape((p[i])**2, n[i][0] * n[i][1], (p[i])**2) # print("actual size ", np.size(M_0_ref)) # print(np.shape(M_0_ref)) my_mesh = CrazyMesh(2, n[i], ((-1, 1), (-1, 1)), curvature=curvature[i]) function_space = FunctionSpace(my_mesh, '2-lobatto', p[i]) basis_0 = BasisForm(function_space) basis_0.quad_grid = 'lobatto' basis_1 = BasisForm(function_space) basis_1.quad_grid = 'lobatto' M_2 = inner(basis_0, basis_1) # print("REF ------------------ \n", M_2_ref[:, 0, :]) # print("CALCULATED _----------------\n", M_2[:, :, 0]) for el in range(n[i][0] * n[i][1]): npt.assert_array_almost_equal(M_2_ref[:, el, :], M_2[:, :, el], decimal=7)
def test_local_to_global_cochain(self): # TODO: currently not supported px != py """Test local to global mapping of cochians.""" mesh = CrazyMesh(2, (2, 2), ((-1, 1), (-1, 1)), curvature=0.3) cases = ['2-lobatto', '2-gauss'] p = 2, 3 for form_case in cases: func_space = FunctionSpace(mesh, form_case, p) form_2 = Form(func_space) form_2.discretize(paraboloid)
def test_l2_norm(self): p = (10, 10) n = (10, 10) mesh = CrazyMesh(2, n, ((0, 1), (0, 1)), 0.1) func_space = FunctionSpace(mesh, '1-lobatto', p) form_1 = Form(func_space) form_1.discretize((self.ufun_x, self.ufun_y)) # print("chochain : \n", form_1.chochain) error_global, error_local = form_1.l_2_norm((self.ufun_x, self.ufun_y)) self.assertLess(error_global, 4 * 10**(-12)) self.assertLess(error_local, 4 * 10**(-10))
def test_value_face_basis(self): for p in range(2, 15): function_space = FunctionSpace(self.mesh, '2-lobatto', p) basis = BasisForm(function_space) basis.quad_grid = 'lobatto' ref_funcs = np.loadtxt( os.getcwd() + '/src/tests/test_basis_2_form/basis_2_form_p_' + str(p) + '.dat', delimiter=',') # decimals limited due to the limited precision of the matlab data npt.assert_array_almost_equal(ref_funcs, basis.basis, decimal=1)
def test_ext_gauss_projection(self): p = (10, 10) n = (5, 6) mesh = CrazyMesh(2, n, ((-1, 1), (-1, 1)), 0.3) func_space_extGau = FunctionSpace(mesh, '0-ext_gauss', p) form_0_extG = ExtGaussForm_0(func_space_extGau) form_0_extG.discretize(self.pfun) xi = eta = np.linspace(-1, 1, 20) form_0_extG.reconstruct(xi, eta) (x, y), data = form_0_extG.export_to_plot() npt.assert_array_almost_equal(self.pfun(x, y), data)
def test_lobatto_projection(self): p = (10, 10) n = (5, 6) mesh = CrazyMesh(2, n, ((-1, 1), (-1, 1)), 0.3) func_space = FunctionSpace(mesh, '0-lobatto', p) form_0 = Form(func_space) form_0.discretize(self.pfun) xi = eta = np.linspace(-1, 1, 20) form_0.reconstruct(xi, eta) (x, y), data = form_0.export_to_plot() npt.assert_array_almost_equal(self.pfun(x, y), data)
def test_weighted_inner_continous(self): """Test for weighted inner product.""" mesh = CrazyMesh(2, (2, 2), ((-1, 1), (-1, 1)), curvature=0.2) func_space = FunctionSpace(mesh, '1-lobatto', (3, 4)) basis = BasisForm(func_space) basis.quad_grid = 'gauss' K = MeshFunction(mesh) K.continous_tensor = [diff_tens_11, diff_tens_12, diff_tens_22] M_1_weighted = inner(basis, basis, K) M_1 = inner(basis, basis) npt.assert_array_almost_equal(M_1, M_1_weighted)
def test_discretize_simple(self): """Simple discretization of 0 forms.""" p_s = [(2, 2)] n = (2, 2) for p in p_s: mesh = CrazyMesh(2, n, ((-1, 1), (-1, 1)), 0.0) func_space = FunctionSpace(mesh, '0-lobatto', p) form_0 = Form(func_space) form_0.discretize(self.pfun) # values at x = +- 0.5 and y = +- 0.5 ref_value = np.array((1, -1, -1, 1)) npt.assert_array_almost_equal(ref_value, form_0.cochain_local[4])
def test_func_space_input(self): """Test function space input to coboudary operator. It should return the incidence matrix. """ p_x, p_y = 3, 3 func_space = FunctionSpace(self.mesh, '1-lobatto', (p_x, p_y), is_inner=False) e_21_lobatto_ref = d_21_lobatto_outer((p_x, p_y)) e_21_lobatto = d(func_space) npt.assert_array_almost_equal(e_21_lobatto, e_21_lobatto_ref)
def test_projection_gauss(self): p = (10, 10) n = (5, 6) mesh = CrazyMesh(2, n, ((-1, 1), (-1, 1)), 0.1) func_space = FunctionSpace(mesh, '1-gauss', p) form_1 = Form(func_space) form_1.discretize((self.ufun_x, self.ufun_y)) xi = eta = np.linspace(-1, 1, 40) form_1.reconstruct(xi, eta) (x, y), data_dx, data_dy = form_1.export_to_plot() npt.assert_array_almost_equal(self.ufun_x(x, y), data_dx, decimal=4) npt.assert_array_almost_equal(self.ufun_y(x, y), data_dy, decimal=4)
def test_weighted_metric(self): # TODO: figure out why if the metric tensor is set to ones the result is very different """Compare weighted and unweighted metric terms with K set to identity.""" mesh = CrazyMesh(2, (1, 1), ((-1, 1), (-1, 1)), curvature=0.2) K = MeshFunction(mesh) func_space = FunctionSpace(mesh, '1-lobatto', (3, 3)) basis = BasisForm(func_space) K.continous_tensor = [diff_tens_11, diff_tens_12, diff_tens_22] xi = eta = np.linspace(-1, 1, 5) xi, eta = np.meshgrid(xi, eta) g_11_k, g_12_k, g_22_k = basis.weighted_metric_tensor(xi.ravel('F'), eta.ravel('F'), K) g_11, g_12, g_22 = mesh.metric_tensor(xi.ravel('F'), eta.ravel('F')) npt.assert_array_almost_equal(g_11, g_11_k)
def test_projection_gauss(self): def ffun(x, y): return np.sin(np.pi * x) * np.sin(np.pi * y) p = (20, 20) n = (3, 3) mesh = CrazyMesh(2, n, ((-1, 1), (-1, 1)), 0.3) func_space = FunctionSpace(mesh, '2-ext_gauss', p) f2eg = Form(func_space) f2eg.discretize(ffun) xi = eta = np.linspace(-1, 1, 50) f2eg.reconstruct(xi, eta) (x, y), data = f2eg.export_to_plot() npt.assert_array_almost_equal(ffun(x, y), data)
def test_exceptions(self): p = 2 mesh = CrazyMesh(2, (2, 2), ((-1, 1), (-1, 1))) func_space = FunctionSpace(mesh, '1-lobatto', p) basis = BasisForm(func_space) basis_1 = BasisForm(func_space) quad_cases = [(None, 'gauss'), (None, None), ('gauss', None), ('lobatto', 'gauss')] for case in quad_cases: if None in case: with self.assertRaises(UnboundLocalError): basis.quad_grid = case[0] basis_1.quad_grid = case[1] else: basis.quad_grid = case[0] basis_1.quad_grid = case[1] self.assertRaises(QuadratureError, inner, basis, basis_1)
def test_form_input(self): p_x, p_y = 3, 3 func_space = FunctionSpace(self.mesh, '1-lobatto', (p_x, p_y), is_inner=False) form_1_cochain = np.random.rand((func_space.num_dof)) form_2_cochain_local_ref = d_21_lobatto_outer( (p_x, p_y)) @ cochain_to_local(func_space, form_1_cochain) func_space_2 = NextSpace(func_space) form_2_cochain_ref = cochain_to_global(func_space_2, form_2_cochain_local_ref) form_1 = Form(func_space, form_1_cochain) form_2 = d(form_1) npt.assert_array_almost_equal(form_2_cochain_ref, form_2.cochain)
def test_reduction_visualization_extended_gauss_2form(self): def ffun(x, y): return np.sin(np.pi * x) * np.sin(np.pi * y) p = (20, 20) n = (3, 3) mesh = CrazyMesh(2, n, ((-1, 1), (-1, 1)), 0.3) func_space = FunctionSpace(mesh, '2-ext_gauss', p) f2eg = Form(func_space) f2eg.discretize(ffun) xi = eta = np.linspace(-1, 1, 50) f2eg.reconstruct(xi, eta) (x, y), data = f2eg.export_to_plot() plt.contourf(x, y, data) plt.title('reduced extened_gauss 2-form') plt.colorbar() plt.show()
def test_reconstruct_lobatto(self): """Test the reconstructin of lobatto forms.""" p = (20, 20) n = (3, 2) mesh = CrazyMesh(2, n, ((-1, 1), (-1, 1)), 0.3) xi = eta = np.linspace(-1, 1, 30) func_space = FunctionSpace(mesh, '2-lobatto', p) form_2 = Form(func_space) cochain = np.loadtxt( 'src/tests/test_form_2/cochain_lobatto_n3-2_p20-20_c3.dat', delimiter=',') ref_reconstructed = np.loadtxt( 'src/tests/test_form_2/reconstructed_lobatto_n3-2_p20-20_c3_xieta_30.dat', delimiter=',') form_2.cochain = cochain form_2.reconstruct(xi, eta) npt.assert_array_almost_equal(ref_reconstructed, form_2.reconstructed)
def test_discretize_lobatto(self): def func_x(x, y): return np.ones(np.shape(x)) def func_y(x, y): return np.ones(np.shape(x)) * 2 mesh = CrazyMesh(2, (2, 2), ((-1, 1), (-1, 1)), 0.3) func_space = FunctionSpace(mesh, '1-lobatto', (3, 3)) form_1 = Form(func_space) form_1.discretize((func_x, func_y)) cochain_local_ref = np.loadtxt( 'src/tests/test_discrete_1_form/discrete_1_form_dx_1_dy_2.csv', delimiter=',') npt.assert_array_almost_equal(form_1.cochain_local, cochain_local_ref, decimal=3)
def test_coboundary_lobatto_e10_inner(self): """Test of the coundary 0->1 for lobatto form inner oriented.""" p = (20, 20) n = (6, 6) mesh = CrazyMesh(2, n, ((-1, 1), (-1, 1)), 0.1) xi = eta = np.linspace(-1, 1, 100) func_space = FunctionSpace(mesh, '0-lobatto', p) form_0 = Form(func_space) form_0.discretize(self.pfun) form_1 = d(form_0) form_1.reconstruct(xi, eta) (x, y), data_dx, data_dy = form_1.export_to_plot() ufun_x, ufun_y = self.ufun() npt.assert_array_almost_equal(ufun_x(x, y), data_dx) npt.assert_array_almost_equal(ufun_y(x, y), data_dy)
def solve(n_cells, degree=3, with_plot=False): # Problem w = 3 * np.pi x = Symbol("x") u = sin(w * x) f = -u.diff(x, 2) # As Expr u = Expression(u) f = Expression(f) # Space # element = HermiteElement(degree) poly_set = leg.basis_functions(degree) dof_set = chebyshev_points(degree) element = LagrangeElement(poly_set, dof_set) mesh = IntervalMesh(a=-1, b=1, n_cells=n_cells) V = FunctionSpace(mesh, element) bc = DirichletBC(V, u) # Need mass matrix to intefrate the rhs M = assemble_matrix(V, "mass", get_geom_tensor=None, timer=0) # NOTE We cannot you apply the alpha transform idea because the functions # are mapped with this selective weight on 2nd, 3rd functions. So some rows # of alpha would have to be multiplied by weights which are cell specific. # And then on top of this there would be a dx = J*dy term. Better just to # use the qudrature representations # Mpoly_matrix = leg.mass_matrix(degree) # M_ = assemble_matrix(V, Mpoly_matrix, Mget_geom_tensor, timer=0) # Stiffness matrix for Laplacian A = assemble_matrix(V, "stiffness", get_geom_tensor=None, timer=0) # NOTE the above # Apoly_matrix = leg.stiffness_matrix(degree) # A_ = assemble_matrix(V, Apoly_matrix, Aget_geom_tensor, timer=0) # Interpolant of source fV = V.interpolate(f) # Integrate in L2 to get the vector b = M.dot(fV.vector) # Apply boundary conditions bc.apply(A, b, True) x = spsolve(A, b) # As function uh = Function(V, x) # This is a (slow) way of plotting the high order if with_plot: fig = plt.figure() ax = fig.gca() uV = V.interpolate(u) for cell in Cells(mesh): a, b = cell.vertices[0, 0], cell.vertices[1, 0] x = np.linspace(a, b, 100) y = uh.eval_cell(x, cell) ax.plot(x, y, color=random.choice(["b", "g", "m", "c"])) y = uV.eval_cell(x, cell) ax.plot(x, y, color="r") y = u.eval_cell(x, cell) ax.plot(x, y, color="k") plt.show() # Error norm in CG high order fine_degree = degree + 3 poly_set = leg.basis_functions(fine_degree) dof_set = chebyshev_points(fine_degree) element = LagrangeElement(poly_set, dof_set) V_fine = FunctionSpace(mesh, element) # Interpolate exact solution to fine u_fine = V_fine.interpolate(u) # Interpolate approx solution fine uh_fine = V_fine.interpolate(uh) # Difference vector e = u_fine.vector - uh_fine.vector # L2 if False: Apoly_matrix = leg.mass_matrix(fine_degree) get_geom_tensor = lambda cell: 1.0 / cell.Jac # Need matrix for integration of H10 norm else: Apoly_matrix = leg.stiffness_matrix(fine_degree) get_geom_tensor = lambda cell: cell.Jac A_fine = assemble_matrix(V_fine, Apoly_matrix, get_geom_tensor, timer=0) # Integrate the error e = sqrt(np.sum(e * A_fine.dot(e))) # Mesh size hmin = mesh.hmin() # Add the cond number kappa = np.linalg.cond(A.toarray()) return hmin, e, kappa, A.shape[0]
def solve(n_cells, degree=3, with_plot=False): # Problem x = Symbol('x') vvvv = -0.0625*x**3/pi**3 + 0.0625*x/pi**3 + sin(2*pi*x)/(16*pi**4) u = -vvvv.diff(x, 2) f = sin(2*pi*x) # As Expr u = Expression(u) f = Expression(f) vvvv = Expression(vvvv) # Space element = HermiteElement(degree) mesh = IntervalMesh(a=-1, b=1, n_cells=n_cells) V = FunctionSpace(mesh, element) bc = DirichletBC(V, vvvv) # Need mass matrix to intefrate the rhs M = assemble_matrix(V, 'mass', get_geom_tensor=None, timer=0) # NOTE We cannot you apply the alpha transform idea because the functions # are mapped with this selective weight on 2nd, 3rd functions. So some rows # of alpha would have to be multiplied by weights which are cell specific. # And then on top of this there would be a dx = J*dy term. Better just to # use the qudrature representations # Mpoly_matrix = leg.mass_matrix(degree) # M_ = assemble_matrix(V, Mpoly_matrix, Mget_geom_tensor, timer=0) # Stiffness matrix for Laplacian A = assemble_matrix(V, 'bending', get_geom_tensor=None, timer=0) # NOTE the above # Apoly_matrix = leg.stiffness_matrix(degree) # A_ = assemble_matrix(V, Apoly_matrix, Aget_geom_tensor, timer=0) # Interpolant of source fV = V.interpolate(f) # Integrate in L2 to get the vector b = M.dot(fV.vector) # Apply boundary conditions bc.apply(A, b, True) x = spsolve(A, b) print '>>>>', np.linalg.norm(x - V.interpolate(vvvv).vector) ALaplace = assemble_matrix(V, 'stiffness', get_geom_tensor=None, timer=0) c = ALaplace.dot(x) y = spsolve(M, c) uh = Function(V, y) # This is a (slow) way of plotting the high order if with_plot: fig = plt.figure() ax = fig.gca() uV = V.interpolate(u) for cell in Cells(mesh): a, b = cell.vertices[0, 0], cell.vertices[1, 0] x = np.linspace(a, b, 100) y = uh.eval_cell(x, cell) ax.plot(x, y, color=random.choice(['b', 'g', 'm', 'c'])) y = uV.eval_cell(x, cell) ax.plot(x, y, color='r') y = u.eval_cell(x, cell) ax.plot(x, y, color='k') plt.show() # Error norm fine_degree = degree + 3 element = HermiteElement(fine_degree) V_fine = FunctionSpace(mesh, element) # Interpolate exact solution to fine u_fine = V_fine.interpolate(u) # Interpolate approx solution fine uh_fine = V_fine.interpolate(uh) # Difference vector e = u_fine.vector - uh_fine.vector # Integrate the error A_fine = assemble_matrix(V_fine, 'mass', get_geom_tensor=None, timer=0) e = sqrt(np.sum(e*A_fine.dot(e))) # Mesh size hmin = mesh.hmin() # Add the cond number kappa = np.linalg.cond(A.toarray()) return hmin, e, kappa, A.shape[0]
import numpy as np # CG --------- # Element x = Symbol('x') poly_set = [S(1), x, x**2, x**3] dof_set = np.array([-1, -0.5, 0.5, 1]) element = LagrangeElement(poly_set, dof_set) # Mesh mesh = IntervalMesh(a=-1, b=1, n_cells=2) # Dofmap dofmap = CGDofMap(mesh, element) assert dofmap.dofmap[0] == [0, 1, 2, 3] assert dofmap.dofmap[1] == [1, 4, 5, 6] # Space V = FunctionSpace(mesh, element) assert V.dim == 7 assert dofmap.tabulate_facet_dofs(1) == [1] # Interpolation mesh = IntervalMesh(a=-1, b=1, n_cells=10) V = FunctionSpace(mesh, element) # Function: expr x = Symbol('x') f = Expression(x) f = V.interpolate(f) # As pure function g = V.interpolate(f) assert np.linalg.norm(f.vector - g.vector) < 1E-13 # DG ------ # Dofmap