def test_quadrilateral_dof_ordering(space_type): """Checks that dofs on shared quadrilateral edges match up""" if MPI.COMM_WORLD.rank == 0: # Create a quadrilateral mesh N = 10 temp_points = np.array([[x / 2, y / 2] for x in range(N) for y in range(N)]) order = [i for i, j in enumerate(temp_points)] shuffle(order) points = np.zeros(temp_points.shape) for i, j in enumerate(order): points[j] = temp_points[i] cells = [] for x in range(N - 1): for y in range(N - 1): a = N * y + x cell = [order[i] for i in [a, a + 1, a + N, a + N + 1]] cells.append(cell) # On process 0, input mesh data and distribute to other # processes mesh = Mesh(MPI.COMM_WORLD, CellType.quadrilateral, points, np.array(cells), []) else: # On other processes, accept distributed data mesh = Mesh(MPI.COMM_WORLD, CellType.quadrilateral, np.ndarray((0, 2)), np.ndarray((0, 4)), []) V = FunctionSpace(mesh, space_type) dofmap = V.dofmap edges = {} # Get coordinates of dofs and edges and check that they are the same # for each global dof number X = V.element.dof_reference_coordinates() coord_dofs = mesh.geometry.dofmap x_g = mesh.geometry.x cmap = fem.create_coordinate_map(mesh.ufl_domain()) for cell_n in range(coord_dofs.num_nodes): dofs = dofmap.cell_dofs(cell_n) x_coord_new = np.zeros([4, 2]) for v in range(4): x_coord_new[v] = x_g[coord_dofs.links(cell_n)[v], :2] x = X.copy() cmap.push_forward(x, X, x_coord_new) edge_dofs_local = [] for i in range(4): edge_dofs_local += list(dofmap.dof_layout.entity_dofs(1, i)) edge_dofs = [dofs[i] for i in edge_dofs_local] for i, j in zip(edge_dofs, x[edge_dofs_local]): if i in edges: assert np.allclose(j, edges[i]) else: edges[i] = j
def unit_cell(cell_type, random_order=True): if cell_type == CellType.interval: points = np.array([[0.], [1.]]) if cell_type == CellType.triangle: # Define equilateral triangle with area 1 root = 3**0.25 # 4th root of 3 points = np.array([[0., 0.], [2 / root, 0.], [1 / root, root]]) elif cell_type == CellType.tetrahedron: # Define regular tetrahedron with volume 1 s = 2**0.5 * 3**(1 / 3) # side length points = np.array([[0., 0., 0.], [s, 0., 0.], [s / 2, s * np.sqrt(3) / 2, 0.], [s / 2, s / 2 / np.sqrt(3), s * np.sqrt(2 / 3)]]) elif cell_type == CellType.quadrilateral: # Define unit quadrilateral (area 1) points = np.array([[0., 0.], [1., 0.], [0., 1.], [1., 1.]]) elif cell_type == CellType.hexahedron: # Define unit hexahedron (volume 1) points = np.array([[0., 0., 0.], [1., 0., 0.], [0., 1., 0.], [1., 1., 0.], [0., 0., 1.], [1., 0., 1.], [0., 1., 1.], [1., 1., 1.]]) num_points = len(points) # Randomly number the points and create the mesh order = list(range(num_points)) if random_order: shuffle(order) ordered_points = np.zeros(points.shape) for i, j in enumerate(order): ordered_points[j] = points[i] cells = np.array([order]) mesh = Mesh(MPI.COMM_WORLD, cell_type, ordered_points, cells, []) mesh.topology.create_connectivity_all() return mesh
def test_volume_quadrilateralR3(coordinates): mesh = Mesh(MPI.comm_world, CellType.quadrilateral, numpy.array(coordinates, dtype=numpy.float64), numpy.array([[0, 1, 2, 3]], dtype=numpy.int32), [], cpp.mesh.GhostMode.none) mesh.create_connectivity_all() assert cpp.mesh.volume_entities(mesh, [0], mesh.topology.dim) == 1.0
def test_higher_order_coordinate_map(points, celltype, order): """Computes physical coordinates of a cell, based on the coordinate map.""" cells = np.array([range(len(points))]) mesh = Mesh(MPI.COMM_WORLD, celltype, points, cells, [], degree=order) V = FunctionSpace(mesh, ("Lagrange", 2)) X = V.element.dof_reference_coordinates() coord_dofs = mesh.geometry.dofmap x_g = mesh.geometry.x cmap = fem.create_coordinate_map(mesh.ufl_domain()) x_coord_new = np.zeros([len(points), mesh.geometry.dim]) i = 0 for node in range(len(points)): x_coord_new[i] = x_g[coord_dofs.links(0)[node], :mesh.geometry.dim] i += 1 x = np.zeros(X.shape) cmap.push_forward(x, X, x_coord_new) assert(np.allclose(x[:, 0], X[:, 0])) assert(np.allclose(x[:, 1], 2 * X[:, 1])) if mesh.geometry.dim == 3: assert(np.allclose(x[:, 2], 3 * X[:, 2]))
def test_gmsh_input_quad(order): pygmsh = pytest.importorskip("pygmsh") # Parameterize test if gmsh gets wider support R = 1 res = 0.2 if order == 2 else 0.2 algorithm = 2 if order == 2 else 5 element = "quad{0:d}".format(int((order + 1)**2)) geo = pygmsh.opencascade.Geometry() geo.add_raw_code("Mesh.ElementOrder={0:d};".format(order)) geo.add_ball([0, 0, 0], R, char_length=res) geo.add_raw_code("Recombine Surface {1};") geo.add_raw_code("Mesh.Algorithm = {0:d};".format(algorithm)) msh = pygmsh.generate_mesh(geo, verbose=True, dim=2) if order > 2: # Quads order > 3 have a gmsh specific ordering, and has to be permuted. msh_to_dolfin = np.array([0, 3, 11, 10, 1, 2, 6, 7, 4, 9, 12, 15, 5, 8, 13, 14]) cells = np.zeros(msh.cells_dict[element].shape) for i in range(len(cells)): for j in range(len(msh_to_dolfin)): cells[i, j] = msh.cells_dict[element][i, msh_to_dolfin[j]] else: # XDMF does not support higher order quads cells = permute_cell_ordering(msh.cells_dict[element], permutation_vtk_to_dolfin( CellType.quadrilateral, msh.cells_dict[element].shape[1])) mesh = Mesh(MPI.comm_world, CellType.quadrilateral, msh.points, cells, [], GhostMode.none) surface = assemble_scalar(1 * dx(mesh)) assert MPI.sum(mesh.mpi_comm(), surface) == pytest.approx(4 * np.pi * R * R, rel=1e-5)
def xtest_mesh_order_unchanged_hexahedron(): points = [[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0], [0, 0, 1], [1, 0, 1], [0, 1, 1], [1, 1, 1]] cells = [[0, 1, 2, 3, 4, 5, 6, 7]] mesh = Mesh(MPI.COMM_WORLD, CellType.hexahedron, points, cells, [], cpp.mesh.GhostMode.none) assert (mesh.cells()[0] == cells[0]).all()
def test_higher_order_coordinate_map(points, celltype): """ Computes physical coordinates of a cell, based on the coordinate map. """ cells = np.array([range(len(points))]) mesh = Mesh(MPI.comm_world, celltype, points, cells, [], GhostMode.none) V = FunctionSpace(mesh, ("Lagrange", mesh.degree())) X = V.element.dof_reference_coordinates() coord_dofs = mesh.coordinate_dofs().entity_points() x_g = mesh.geometry.points cmap = fem.create_coordinate_map(mesh.ufl_domain()) x_coord_new = np.zeros([len(points), mesh.geometry.dim]) i = 0 for node in range(len(points)): x_coord_new[i] = x_g[coord_dofs[0, node], :mesh.geometry.dim] i += 1 x = np.zeros(X.shape) cmap.push_forward(x, X, x_coord_new) assert (np.allclose(x[:, 0], X[:, 0])) assert (np.allclose(x[:, 1], 2 * X[:, 1])) if mesh.geometry.dim == 3: assert (np.allclose(x[:, 2], 3 * X[:, 2]))
def two_unit_cells(cell_type, agree=False, random_order=True, return_order=False): if cell_type == CellType.interval: points = np.array([[0.], [1.], [-1.]]) if agree: cells = [[0, 1], [2, 0]] else: cells = [[0, 1], [0, 2]] if cell_type == CellType.triangle: # Define equilateral triangles with area 1 root = 3 ** 0.25 # 4th root of 3 points = np.array([[0., 0.], [2 / root, 0.], [1 / root, root], [1 / root, -root]]) if agree: cells = [[0, 1, 2], [0, 3, 1]] else: cells = [[0, 1, 2], [1, 0, 3]] elif cell_type == CellType.tetrahedron: # Define regular tetrahedra with volume 1 s = 2 ** 0.5 * 3 ** (1 / 3) # side length points = np.array([[0., 0., 0.], [s, 0., 0.], [s / 2, s * np.sqrt(3) / 2, 0.], [s / 2, s / 2 / np.sqrt(3), s * np.sqrt(2 / 3)], [s / 2, s / 2 / np.sqrt(3), -s * np.sqrt(2 / 3)]]) if agree: cells = [[0, 1, 2, 3], [0, 1, 4, 2]] else: cells = [[0, 1, 2, 3], [0, 2, 1, 4]] elif cell_type == CellType.quadrilateral: # Define unit quadrilaterals (area 1) points = np.array([[0., 0.], [1., 0.], [0., 1.], [1., 1.], [0., -1.], [1., -1.]]) if agree: cells = [[0, 1, 2, 3], [4, 5, 0, 1]] else: cells = [[0, 1, 2, 3], [5, 1, 4, 0]] elif cell_type == CellType.hexahedron: # Define unit hexahedra (volume 1) points = np.array([[0., 0., 0.], [1., 0., 0.], [0., 1., 0.], [1., 1., 0.], [0., 0., 1.], [1., 0., 1.], [0., 1., 1.], [1., 1., 1.], [0., 0., -1.], [1., 0., -1.], [0., 1., -1.], [1., 1., -1.]]) if agree: cells = [[0, 1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 0, 1, 2, 3]] else: cells = [[0, 1, 2, 3, 4, 5, 6, 7], [9, 11, 8, 10, 1, 3, 0, 2]] num_points = len(points) # Randomly number the points and create the mesh order = list(range(num_points)) if random_order: shuffle(order) ordered_points = np.zeros(points.shape) for i, j in enumerate(order): ordered_points[j] = points[i] ordered_cells = np.array([[order[i] for i in c] for c in cells]) mesh = Mesh(MPI.COMM_WORLD, cell_type, ordered_points, ordered_cells, []) mesh.topology.create_connectivity_all() if return_order: return mesh, order return mesh
def test_triangle_mesh(): mesh = Mesh( MPI.comm_world, CellType.triangle, numpy.array([[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]], dtype=numpy.float64), numpy.array([[0, 1, 2]], dtype=numpy.int32), [], cpp.mesh.GhostMode.none) assert mesh.num_entities_global(0) == 3 assert mesh.num_entities_global(2) == 1
def test_third_order_tri(): # *---*---*---* 3--11--10--2 # | \ | | \ | # * * * * 8 7 15 13 # | \ | | \ | # * * * * 9 14 6 12 # | \ | | \ | # *---*---*---* 0--4---5---1 for H in (1.0, 2.0): for Z in (0.0, 0.5): L = 1 points = np.array([ [0, 0, 0], [L, 0, 0], [L, H, Z], [0, H, Z], # 0, 1, 2, 3 [L / 3, 0, 0], [2 * L / 3, 0, 0], # 4, 5 [2 * L / 3, H / 3, 0], [L / 3, 2 * H / 3, 0], # 6, 7 [0, 2 * H / 3, 0], [0, H / 3, 0], # 8, 9 [2 * L / 3, H, Z], [L / 3, H, Z], # 10, 11 [L, H / 3, 0], [L, 2 * H / 3, 0], # 12, 13 [L / 3, H / 3, 0], # 14 [2 * L / 3, 2 * H / 3, 0] ]) # 15 cells = np.array([[0, 1, 3, 4, 5, 6, 7, 8, 9, 14], [1, 2, 3, 12, 13, 10, 11, 7, 6, 15]]) cells = permute_cell_ordering( cells, permutation_vtk_to_dolfin(CellType.triangle, cells.shape[1])) mesh = Mesh(MPI.comm_world, CellType.triangle, points, cells, [], GhostMode.none) def e2(x): return x[2] + x[0] * x[1] degree = mesh.degree() # Interpolate function V = FunctionSpace(mesh, ("CG", degree)) u = Function(V) cmap = fem.create_coordinate_map(mesh.ufl_domain()) mesh.geometry.coord_mapping = cmap u.interpolate(e2) intu = assemble_scalar(u * dx(metadata={"quadrature_degree": 40})) intu = MPI.sum(mesh.mpi_comm(), intu) nodes = [0, 9, 8, 3] ref = sympy_scipy(points, nodes, L, H) assert ref == pytest.approx(intu, rel=1e-6)
def test_third_order_quad(L, H, Z): """Test by comparing integration of z+x*y against sympy/scipy integration of a quad element. Z>0 implies curved element. *---------* 3--8--9--2-22-23-17 | | | | | | | 11 14 15 7 26 27 21 | | | | | | | 10 12 13 6 24 25 20 | | | | | *---------* 0--4--5--1-18-19-16 """ points = np.array([[0, 0, 0], [L, 0, 0], [L, H, Z], [0, H, Z], # 0 1 2 3 [L / 3, 0, 0], [2 * L / 3, 0, 0], # 4 5 [L, H / 3, 0], [L, 2 * H / 3, 0], # 6 7 [L / 3, H, Z], [2 * L / 3, H, Z], # 8 9 [0, H / 3, 0], [0, 2 * H / 3, 0], # 10 11 [L / 3, H / 3, 0], [2 * L / 3, H / 3, 0], # 12 13 [L / 3, 2 * H / 3, 0], [2 * L / 3, 2 * H / 3, 0], # 14 15 [2 * L, 0, 0], [2 * L, H, Z], # 16 17 [4 * L / 3, 0, 0], [5 * L / 3, 0, 0], # 18 19 [2 * L, H / 3, 0], [2 * L, 2 * H / 3, 0], # 20 21 [4 * L / 3, H, Z], [5 * L / 3, H, Z], # 22 23 [4 * L / 3, H / 3, 0], [5 * L / 3, H / 3, 0], # 24 25 [4 * L / 3, 2 * H / 3, 0], [5 * L / 3, 2 * H / 3, 0]]) # 26 27 # Change to multiple cells when matthews dof-maps work for quads cells = np.array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], [1, 16, 17, 2, 18, 19, 20, 21, 22, 23, 6, 7, 24, 25, 26, 27]]) cells = permute_cell_ordering(cells, permutation_vtk_to_dolfin(CellType.quadrilateral, cells.shape[1])) mesh = Mesh(MPI.comm_world, CellType.quadrilateral, points, cells, [], GhostMode.none) def e2(x): return x[2] + x[0] * x[1] # Interpolate function V = FunctionSpace(mesh, ("CG", 3)) u = Function(V) cmap = fem.create_coordinate_map(mesh.ufl_domain()) mesh.geometry.coord_mapping = cmap u.interpolate(e2) intu = assemble_scalar(u * dx(mesh)) intu = MPI.sum(mesh.mpi_comm(), intu) nodes = [0, 3, 10, 11] ref = sympy_scipy(points, nodes, 2 * L, H) assert ref == pytest.approx(intu, rel=1e-6)
def test_eval_manifold(): # Simple two-triangle surface in 3d vertices = [(0.0, 0.0, 1.0), (1.0, 1.0, 1.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)] cells = [(0, 1, 2), (0, 1, 3)] mesh = Mesh(MPI.COMM_WORLD, cpp.mesh.CellType.triangle, np.array(vertices, dtype=np.float64), np.array(cells, dtype=np.int32), []) Q = FunctionSpace(mesh, ("CG", 1)) u = Function(Q) u.interpolate(lambda x: x[0] + x[1]) assert np.isclose(u.eval([0.75, 0.25, 0.5], 0)[0], 1.0)
def test_volume_quadrilateral_coplanarity_check_2(scaling): with pytest.raises(RuntimeError) as error: # Unit square cell scaled down by 'scaling' and the first vertex # is distorted so that the vertices are clearly non coplanar mesh = Mesh( MPI.COMM_SELF, CellType.quadrilateral, numpy.array([[1.0, 0.5, 0.6], [0.0, scaling, 0.0], [0.0, 0.0, scaling], [0.0, 1.0, 1.0]], dtype=numpy.float64), numpy.array([[0, 1, 2, 3]], dtype=numpy.int32), []) mesh.topology.create_connectivity_all() cpp.mesh.volume_entities(mesh, [0], mesh.topology.dim) assert "Not coplanar" in str(error.value)
def test_manifold_point_search(): # Simple two-triangle surface in 3d vertices = [(0.0, 0.0, 1.0), (1.0, 1.0, 1.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)] cells = [(0, 1, 2), (0, 1, 3)] mesh = Mesh(MPI.COMM_WORLD, CellType.triangle, numpy.array(vertices, dtype=numpy.float64), numpy.array(cells, dtype=numpy.int32), []) bb = BoundingBoxTree(mesh, mesh.topology.dim) p = numpy.array([0.5, 0.25, 0.75]) assert geometry.compute_first_entity_collision(bb, mesh, p) == 0 p = numpy.array([0.25, 0.5, 0.75]) assert geometry.compute_first_entity_collision(bb, mesh, p) == 1
def test_fourth_order_tri(): L = 1 # *--*--*--*--* 3-21-20-19--2 # | \ | | \ | # * * * * * 10 9 24 23 18 # | \ | | \ | # * * * * * 11 15 8 22 17 # | \ | | \ | # * * * * * 12 13 14 7 16 # | \ | | \ | # *--*--*--*--* 0--4--5--6--1 for H in (1.0, 2.0): for Z in (0.0, 0.5): points = np.array( [[0, 0, 0], [L, 0, 0], [L, H, Z], [0, H, Z], # 0, 1, 2, 3 [L / 4, 0, 0], [L / 2, 0, 0], [3 * L / 4, 0, 0], # 4, 5, 6 [3 / 4 * L, H / 4, Z / 2], [L / 2, H / 2, 0], # 7, 8 [L / 4, 3 * H / 4, 0], [0, 3 * H / 4, 0], # 9, 10 [0, H / 2, 0], [0, H / 4, Z / 2], # 11, 12 [L / 4, H / 4, Z / 2], [L / 2, H / 4, Z / 2], [L / 4, H / 2, 0], # 13, 14, 15 [L, H / 4, Z / 2], [L, H / 2, 0], [L, 3 * H / 4, 0], # 16, 17, 18 [3 * L / 4, H, Z], [L / 2, H, Z], [L / 4, H, Z], # 19, 20, 21 [3 * L / 4, H / 2, 0], [3 * L / 4, 3 * H / 4, 0], # 22, 23 [L / 2, 3 * H / 4, 0]] # 24 ) cells = np.array([[0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], [1, 2, 3, 16, 17, 18, 19, 20, 21, 9, 8, 7, 22, 23, 24]]) cells = permute_cell_ordering(cells, permutation_vtk_to_dolfin(CellType.triangle, cells.shape[1])) mesh = Mesh(MPI.comm_world, CellType.triangle, points, cells, [], GhostMode.none) def e2(x): return x[2] + x[0] * x[1] degree = mesh.degree() # Interpolate function V = FunctionSpace(mesh, ("CG", degree)) u = Function(V) cmap = fem.create_coordinate_map(mesh.ufl_domain()) mesh.geometry.coord_mapping = cmap u.interpolate(e2) intu = assemble_scalar(u * dx(metadata={"quadrature_degree": 50})) intu = MPI.sum(mesh.mpi_comm(), intu) nodes = [0, 3, 10, 11, 12] ref = sympy_scipy(points, nodes, L, H) assert ref == pytest.approx(intu, rel=1e-4)
def test_read_mesh_data(tempdir, tdim, n): filename = os.path.join(tempdir, "mesh.xdmf") mesh = mesh_factory(tdim, n) encoding = XDMFFile.Encoding.HDF5 ghost_mode = cpp.mesh.GhostMode.none with XDMFFile(mesh.mpi_comm(), filename, encoding) as file: file.write(mesh) with XDMFFile(MPI.comm_world, filename) as file: cell_type, points, cells, indices = file.read_mesh_data(MPI.comm_world) mesh2 = Mesh(MPI.comm_world, cell_type, points, cells, indices, ghost_mode) assert (mesh.topology.cell_type == mesh2.topology.cell_type) assert mesh.num_entities_global(0) == mesh2.num_entities_global(0) dim = mesh.topology.dim assert mesh.num_entities_global(dim) == mesh2.num_entities_global(dim)
def test_manifold_point_search(): # Simple two-triangle surface in 3d vertices = [(0.0, 0.0, 1.0), (1.0, 1.0, 1.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0)] cells = [(0, 1, 2), (0, 1, 3)] mesh = Mesh(MPI.COMM_WORLD, CellType.triangle, numpy.array(vertices, dtype=numpy.float64), numpy.array(cells, dtype=numpy.int32), []) bb = BoundingBoxTree(mesh, mesh.topology.dim) p = numpy.array([0.5, 0.25, 0.75]) cell_candidates = geometry.compute_collisions_point(bb, p) cell = cpp.geometry.select_colliding_cells(mesh, cell_candidates, p, 1) assert cell[0] == 0 p = numpy.array([0.25, 0.5, 0.75]) cell_candidates = geometry.compute_collisions_point(bb, p) cell = cpp.geometry.select_colliding_cells(mesh, cell_candidates, p, 1) assert cell[0] == 1
def test_second_order_tri(): # Test second order mesh by computing volume of two cells # *-----*-----* 3----6-----2 # | \ | | \ | # | \ | | \ | # * * * 7 8 5 # | \ | | \ | # | \ | | \ | # *-----*-----* 0----4-----1 for H in (1.0, 2.0): for Z in (0.0, 0.5): L = 1 points = np.array([[0, 0, 0], [L, 0, 0], [L, H, Z], [0, H, Z], [L / 2, 0, 0], [L, H / 2, 0], [L / 2, H, Z], [0, H / 2, 0], [L / 2, H / 2, 0]]) cells = np.array([[0, 1, 3, 4, 8, 7], [1, 2, 3, 5, 6, 8]]) cells = permute_cell_ordering( cells, permutation_vtk_to_dolfin(CellType.triangle, cells.shape[1])) mesh = Mesh(MPI.comm_world, CellType.triangle, points, cells, [], GhostMode.none) def e2(x): return x[2] + x[0] * x[1] degree = mesh.degree() # Interpolate function V = FunctionSpace(mesh, ("CG", degree)) u = Function(V) cmap = fem.create_coordinate_map(mesh.ufl_domain()) mesh.geometry.coord_mapping = cmap u.interpolate(e2) intu = assemble_scalar( u * dx(mesh, metadata={"quadrature_degree": 20})) intu = MPI.sum(mesh.mpi_comm(), intu) nodes = [0, 3, 7] ref = sympy_scipy(points, nodes, L, H) assert ref == pytest.approx(intu, rel=1e-6)
def test_second_order_quad(L, H, Z): """ Test by comparing integration of z+x*y against sympy/scipy integration of a quad element. Z>0 implies curved element. *-----* 3--6--2 | | | | | | 7 8 5 | | | | *-----* 0--4--1 """ points = np.array([[0, 0, 0], [L, 0, 0], [L, H, Z], [0, H, Z], [L / 2, 0, 0], [L, H / 2, 0], [L / 2, H, Z], [0, H / 2, 0], [L / 2, H / 2, 0], [2 * L, 0, 0], [2 * L, H, Z]]) cells = np.array([[0, 1, 2, 3, 4, 5, 6, 7, 8]]) cells = permute_cell_ordering( cells, permutation_vtk_to_dolfin(CellType.quadrilateral, cells.shape[1])) mesh = Mesh(MPI.comm_world, CellType.quadrilateral, points, cells, [], GhostMode.none) def e2(x): return x[2] + x[0] * x[1] # Interpolate function V = FunctionSpace(mesh, ("CG", 2)) u = Function(V) cmap = fem.create_coordinate_map(mesh.ufl_domain()) mesh.geometry.coord_mapping = cmap u.interpolate(e2) intu = assemble_scalar(u * dx(mesh)) intu = MPI.sum(mesh.mpi_comm(), intu) nodes = [0, 3, 7] ref = sympy_scipy(points, nodes, L, H) assert ref == pytest.approx(intu, rel=1e-6)
def test_higher_order_tetra_coordinate_map(order): """ Computes physical coordinates of a cell, based on the coordinate map. """ celltype = CellType.tetrahedron points = np.array([[0, 0, 0], [1, 0, 0], [0, 2, 0], [0, 0, 3], [0, 4 / 3, 1], [0, 2 / 3, 2], [2 / 3, 0, 1], [1 / 3, 0, 2], [2 / 3, 2 / 3, 0], [1 / 3, 4 / 3, 0], [0, 0, 1], [0, 0, 2], [0, 2 / 3, 0], [0, 4 / 3, 0], [1 / 3, 0, 0], [2 / 3, 0, 0], [1 / 3, 2 / 3, 1], [0, 2 / 3, 1], [1 / 3, 0, 1], [1 / 3, 2 / 3, 0]]) if order == 1: points = np.array([points[0, :], points[1, :], points[2, :], points[3, :]]) elif order == 2: points = np.array([points[0, :], points[1, :], points[2, :], points[3, :], [0, 1, 3 / 2], [1 / 2, 0, 3 / 2], [1 / 2, 1, 0], [0, 0, 3 / 2], [0, 1, 0], [1 / 2, 0, 0]]) cells = np.array([range(len(points))]) mesh = Mesh(MPI.COMM_WORLD, celltype, points, cells, [], degree=order) V = FunctionSpace(mesh, ("Lagrange", order)) X = V.element.dof_reference_coordinates() coord_dofs = mesh.geometry.dofmap x_g = mesh.geometry.x cmap = fem.create_coordinate_map(mesh.ufl_domain()) x_coord_new = np.zeros([len(points), mesh.geometry.dim]) i = 0 for node in range(len(points)): x_coord_new[i] = x_g[coord_dofs.links(0)[node], :mesh.geometry.dim] i += 1 x = np.zeros(X.shape) cmap.push_forward(x, X, x_coord_new) assert(np.allclose(x[:, 0], X[:, 0])) assert(np.allclose(x[:, 1], 2 * X[:, 1])) assert(np.allclose(x[:, 2], 3 * X[:, 2]))
def xtest_mesh_order_unchanged_quadrilateral(): points = [[0, 0], [1, 0], [0, 1], [1, 1]] cells = [[0, 1, 2, 3]] mesh = Mesh(MPI.COMM_WORLD, CellType.quadrilateral, points, cells, [], cpp.mesh.GhostMode.none) assert (mesh.cells()[0] == cells[0]).all()
def xtest_mesh_order_unchanged_triangle(): points = [[0, 0], [1, 0], [1, 1]] cells = [[0, 1, 2]] mesh = Mesh(MPI.COMM_WORLD, CellType.triangle, points, cells, [], cpp.mesh.GhostMode.none) assert (mesh.cells()[0] == cells[0]).all()
def test_mesh_construction_pygmsh(): pygmsh = pytest.importorskip("pygmsh") if MPI.COMM_WORLD.rank == 0: geom = pygmsh.opencascade.Geometry() geom.add_ball([0.0, 0.0, 0.0], 1.0, char_length=0.2) pygmsh_mesh = pygmsh.generate_mesh(geom) points, cells = pygmsh_mesh.points, pygmsh_mesh.cells else: points = np.zeros([0, 3]) cells = { "tetra": np.zeros([0, 4], dtype=np.int64), "triangle": np.zeros([0, 3], dtype=np.int64), "line": np.zeros([0, 2], dtype=np.int64) } mesh = Mesh(MPI.COMM_WORLD, dolfinx.cpp.mesh.CellType.tetrahedron, points, cells['tetra'], [], cpp.mesh.GhostMode.none) assert mesh.geometry.degree() == 1 assert mesh.geometry.dim == 3 assert mesh.topology.dim == 3 mesh = Mesh(MPI.COMM_WORLD, dolfinx.cpp.mesh.CellType.triangle, points, cells['triangle'], [], cpp.mesh.GhostMode.none) assert mesh.geometry.degree() == 1 assert mesh.geometry.dim == 3 assert mesh.topology.dim == 2 mesh = Mesh(MPI.COMM_WORLD, dolfinx.cpp.mesh.CellType.interval, points, cells['line'], [], cpp.mesh.GhostMode.none) assert mesh.geometry.degree() == 1 assert mesh.geometry.dim == 3 assert mesh.topology.dim == 1 if MPI.COMM_WORLD.rank == 0: print("Generate mesh") geom = pygmsh.opencascade.Geometry() geom.add_ball([0.0, 0.0, 0.0], 1.0, char_length=0.2) pygmsh_mesh = pygmsh.generate_mesh( geom, extra_gmsh_arguments=['-order', '2']) points, cells = pygmsh_mesh.points, pygmsh_mesh.cells print("End Generate mesh", cells.keys()) else: points = np.zeros([0, 3]) cells = { "tetra10": np.zeros([0, 10], dtype=np.int64), "triangle6": np.zeros([0, 6], dtype=np.int64), "line3": np.zeros([0, 3], dtype=np.int64) } mesh = Mesh(MPI.COMM_WORLD, dolfinx.cpp.mesh.CellType.tetrahedron, points, cells['tetra10'], [], cpp.mesh.GhostMode.none) assert mesh.geometry.degree() == 2 assert mesh.geometry.dim == 3 assert mesh.topology.dim == 3 mesh = Mesh(MPI.COMM_WORLD, dolfinx.cpp.mesh.CellType.triangle, points, cells['triangle6'], [], cpp.mesh.GhostMode.none) assert mesh.geometry.degree() == 2 assert mesh.geometry.dim == 3 assert mesh.topology.dim == 2
def test_triangle_dof_ordering(space_type): """Checks that dofs on shared triangle edges match up""" # Create a triangle mesh if MPI.COMM_WORLD.rank == 0: N = 6 # Create a grid of points [0, 0.5, ..., 9.5]**2, then order them # in a random order temp_points = np.array([[x / 2, y / 2] for x in range(N) for y in range(N)]) order = [i for i, j in enumerate(temp_points)] shuffle(order) points = np.zeros(temp_points.shape) for i, j in enumerate(order): points[j] = temp_points[i] # Make triangle cells using the randomly ordered points cells = [] for x in range(N - 1): for y in range(N - 1): a = N * y + x # Adds two triangle cells: # a+N -- a+N+1 # | / | # | / | # | / | # a --- a+1 for cell in [[a, a + 1, a + N + 1], [a, a + N + 1, a + N]]: cells.append([order[i] for i in cell]) # On process 0, input mesh data and distribute to other # processes mesh = Mesh(MPI.COMM_WORLD, CellType.triangle, points, np.array(cells), []) else: # On other processes, accept distributed data mesh = Mesh(MPI.COMM_WORLD, CellType.triangle, np.ndarray((0, 2)), np.ndarray((0, 3)), []) V = FunctionSpace(mesh, space_type) dofmap = V.dofmap edges = {} # Get coordinates of dofs and edges and check that they are the same # for each global dof number X = V.element.dof_reference_coordinates() coord_dofs = mesh.geometry.dofmap x_g = mesh.geometry.x cmap = fem.create_coordinate_map(mesh.ufl_domain()) for cell_n in range(coord_dofs.num_nodes): dofs = dofmap.cell_dofs(cell_n) x_coord_new = np.zeros([3, 2]) for v in range(3): x_coord_new[v] = x_g[coord_dofs.links(cell_n)[v], :2] x = X.copy() cmap.push_forward(x, X, x_coord_new) edge_dofs_local = [] for i in range(3): edge_dofs_local += list(dofmap.dof_layout.entity_dofs(1, i)) edge_dofs = [dofs[i] for i in edge_dofs_local] for i, j in zip(edge_dofs, x[edge_dofs_local]): if i in edges: assert np.allclose(j, edges[i]) else: edges[i] = j
def test_nth_order_triangle(order): num_nodes = (order + 1) * (order + 2) / 2 cells = np.array([range(int(num_nodes))]) cells = permute_cell_ordering( cells, permutation_vtk_to_dolfin(CellType.triangle, cells.shape[1])) if order == 1: points = np.array([[0.00000, 0.00000, 0.00000], [1.00000, 0.00000, 0.00000], [0.00000, 1.00000, 0.00000]]) elif order == 2: points = np.array([[0.00000, 0.00000, 0.00000], [1.00000, 0.00000, 0.00000], [0.00000, 1.00000, 0.00000], [0.50000, 0.00000, 0.00000], [0.50000, 0.50000, -0.25000], [0.00000, 0.50000, -0.25000]]) elif order == 3: points = np.array([[0.00000, 0.00000, 0.00000], [1.00000, 0.00000, 0.00000], [0.00000, 1.00000, 0.00000], [0.33333, 0.00000, 0.00000], [0.66667, 0.00000, 0.00000], [0.66667, 0.33333, -0.11111], [0.33333, 0.66667, 0.11111], [0.00000, 0.66667, 0.11111], [0.00000, 0.33333, -0.11111], [0.33333, 0.33333, -0.11111]]) elif order == 4: points = np.array([[0.00000, 0.00000, 0.00000], [1.00000, 0.00000, 0.00000], [0.00000, 1.00000, 0.00000], [0.25000, 0.00000, 0.00000], [0.50000, 0.00000, 0.00000], [0.75000, 0.00000, 0.00000], [0.75000, 0.25000, -0.06250], [0.50000, 0.50000, 0.06250], [0.25000, 0.75000, -0.06250], [0.00000, 0.75000, -0.06250], [0.00000, 0.50000, 0.06250], [0.00000, 0.25000, -0.06250], [0.25000, 0.25000, -0.06250], [0.50000, 0.25000, -0.06250], [0.25000, 0.50000, 0.06250]]) elif order == 5: points = np.array([[0.00000, 0.00000, 0.00000], [1.00000, 0.00000, 0.00000], [0.00000, 1.00000, 0.00000], [0.20000, 0.00000, 0.00000], [0.40000, 0.00000, 0.00000], [0.60000, 0.00000, 0.00000], [0.80000, 0.00000, 0.00000], [0.80000, 0.20000, -0.04000], [0.60000, 0.40000, 0.04000], [0.40000, 0.60000, -0.04000], [0.20000, 0.80000, 0.04000], [0.00000, 0.80000, 0.04000], [0.00000, 0.60000, -0.04000], [0.00000, 0.40000, 0.04000], [0.00000, 0.20000, -0.04000], [0.20000, 0.20000, -0.04000], [0.60000, 0.20000, -0.04000], [0.20000, 0.60000, -0.04000], [0.40000, 0.20000, -0.04000], [0.40000, 0.40000, 0.04000], [0.20000, 0.40000, 0.04000]]) elif order == 6: points = np.array([[0.00000, 0.00000, 0.00000], [1.00000, 0.00000, 0.00000], [0.00000, 1.00000, 0.00000], [0.16667, 0.00000, 0.00000], [0.33333, 0.00000, 0.00000], [0.50000, 0.00000, 0.00000], [0.66667, 0.00000, 0.00000], [0.83333, 0.00000, 0.00000], [0.83333, 0.16667, -0.00463], [0.66667, 0.33333, 0.00463], [0.50000, 0.50000, -0.00463], [0.33333, 0.66667, 0.00463], [0.16667, 0.83333, -0.00463], [0.00000, 0.83333, -0.00463], [0.00000, 0.66667, 0.00463], [0.00000, 0.50000, -0.00463], [0.00000, 0.33333, 0.00463], [0.00000, 0.16667, -0.00463], [0.16667, 0.16667, -0.00463], [0.66667, 0.16667, -0.00463], [0.16667, 0.66667, 0.00463], [0.33333, 0.16667, -0.00463], [0.50000, 0.16667, -0.00463], [0.50000, 0.33333, 0.00463], [0.33333, 0.50000, -0.00463], [0.16667, 0.50000, -0.00463], [0.16667, 0.33333, 0.00463], [0.33333, 0.33333, 0.00463]]) elif order == 7: points = np.array([[0.00000, 0.00000, 0.00000], [1.00000, 0.00000, 0.00000], [0.00000, 1.00000, 0.00000], [0.14286, 0.00000, 0.00000], [0.28571, 0.00000, 0.00000], [0.42857, 0.00000, 0.00000], [0.57143, 0.00000, 0.00000], [0.71429, 0.00000, 0.00000], [0.85714, 0.00000, 0.00000], [0.85714, 0.14286, -0.02041], [0.71429, 0.28571, 0.02041], [0.57143, 0.42857, -0.02041], [0.42857, 0.57143, 0.02041], [0.28571, 0.71429, -0.02041], [0.14286, 0.85714, 0.02041], [0.00000, 0.85714, 0.02041], [0.00000, 0.71429, -0.02041], [0.00000, 0.57143, 0.02041], [0.00000, 0.42857, -0.02041], [0.00000, 0.28571, 0.02041], [0.00000, 0.14286, -0.02041], [0.14286, 0.14286, -0.02041], [0.71429, 0.14286, -0.02041], [0.14286, 0.71429, -0.02041], [0.28571, 0.14286, -0.02041], [0.42857, 0.14286, -0.02041], [0.57143, 0.14286, -0.02041], [0.57143, 0.28571, 0.02041], [0.42857, 0.42857, -0.02041], [0.28571, 0.57143, 0.02041], [0.14286, 0.57143, 0.02041], [0.14286, 0.42857, -0.02041], [0.14286, 0.28571, 0.02041], [0.28571, 0.28571, 0.02041], [0.42857, 0.28571, 0.02041], [0.28571, 0.42857, -0.02041]]) # Higher order tests are too slow elif order == 8: points = np.array([[0.00000, 0.00000, 0.00000], [1.00000, 0.00000, 0.00000], [0.00000, 1.00000, 0.00000], [0.12500, 0.00000, 0.00000], [0.25000, 0.00000, 0.00000], [0.37500, 0.00000, 0.00000], [0.50000, 0.00000, 0.00000], [0.62500, 0.00000, 0.00000], [0.75000, 0.00000, 0.00000], [0.87500, 0.00000, 0.00000], [0.87500, 0.12500, -0.00195], [0.75000, 0.25000, 0.00195], [0.62500, 0.37500, -0.00195], [0.50000, 0.50000, 0.00195], [0.37500, 0.62500, -0.00195], [0.25000, 0.75000, 0.00195], [0.12500, 0.87500, -0.00195], [0.00000, 0.87500, -0.00195], [0.00000, 0.75000, 0.00195], [0.00000, 0.62500, -0.00195], [0.00000, 0.50000, 0.00195], [0.00000, 0.37500, -0.00195], [0.00000, 0.25000, 0.00195], [0.00000, 0.12500, -0.00195], [0.12500, 0.12500, -0.00195], [0.75000, 0.12500, -0.00195], [0.12500, 0.75000, 0.00195], [0.25000, 0.12500, -0.00195], [0.37500, 0.12500, -0.00195], [0.50000, 0.12500, -0.00195], [0.62500, 0.12500, -0.00195], [0.62500, 0.25000, 0.00195], [0.50000, 0.37500, -0.00195], [0.37500, 0.50000, 0.00195], [0.25000, 0.62500, -0.00195], [0.12500, 0.62500, -0.00195], [0.12500, 0.50000, 0.00195], [0.12500, 0.37500, -0.00195], [0.12500, 0.25000, 0.00195], [0.25000, 0.25000, 0.00195], [0.50000, 0.25000, 0.00195], [0.25000, 0.50000, 0.00195], [0.37500, 0.25000, 0.00195], [0.37500, 0.37500, -0.00195], [0.25000, 0.37500, -0.00195]]) mesh = Mesh(MPI.comm_world, CellType.triangle, points, cells, [], GhostMode.none) # Find nodes corresponding to y axis nodes = [] for j in range(points.shape[0]): if np.isclose(points[j][0], 0): nodes.append(j) def e2(x): return x[2] + x[0] * x[1] # For solution to be in functionspace V = FunctionSpace(mesh, ("CG", max(2, order))) u = Function(V) cmap = fem.create_coordinate_map(mesh.ufl_domain()) mesh.geometry.coord_mapping = cmap u.interpolate(e2) quad_order = 30 intu = assemble_scalar(u * dx(metadata={"quadrature_degree": quad_order})) intu = MPI.sum(mesh.mpi_comm(), intu) ref = scipy_one_cell(points, nodes) assert ref == pytest.approx(intu, rel=3e-3)
def test_mesh_order_unchanged_tetrahedron(): points = [[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 0, 1]] cells = [[0, 1, 2, 3]] mesh = Mesh(MPI.comm_world, CellType.tetrahedron, points, cells, [], cpp.mesh.GhostMode.none) assert (mesh.cells()[0] == cells[0]).all()
def test_fourth_order_quad(L, H, Z): """Test by comparing integration of z+x*y against sympy/scipy integration of a quad element. Z>0 implies curved element. *---------* 20-21-22-23-24-41--42--43--44 | | | | | | | 15 16 17 18 19 37 38 39 40 | | | | | | | 10 11 12 13 14 33 34 35 36 | | | | | | | 5 6 7 8 9 29 30 31 32 | | | | | *---------* 0--1--2--3--4--25--26--27--28 """ points = np.array([ [0, 0, 0], [L / 4, 0, 0], [L / 2, 0, 0], # 0 1 2 [3 * L / 4, 0, 0], [L, 0, 0], # 3 4 [0, H / 4, -Z / 3], [L / 4, H / 4, -Z / 3], [L / 2, H / 4, -Z / 3], # 5 6 7 [3 * L / 4, H / 4, -Z / 3], [L, H / 4, -Z / 3], # 8 9 [0, H / 2, 0], [L / 4, H / 2, 0], [L / 2, H / 2, 0], # 10 11 12 [3 * L / 4, H / 2, 0], [L, H / 2, 0], # 13 14 [0, (3 / 4) * H, 0], [L / 4, (3 / 4) * H, 0], # 15 16 [L / 2, (3 / 4) * H, 0], [3 * L / 4, (3 / 4) * H, 0], # 17 18 [L, (3 / 4) * H, 0], [0, H, Z], [L / 4, H, Z], # 19 20 21 [L / 2, H, Z], [3 * L / 4, H, Z], [L, H, Z], # 22 23 24 [(5 / 4) * L, 0, 0], [(6 / 4) * L, 0, 0], # 25 26 [(7 / 4) * L, 0, 0], [2 * L, 0, 0], # 27 28 [(5 / 4) * L, H / 4, -Z / 3], [(6 / 4) * L, H / 4, -Z / 3], # 29 30 [(7 / 4) * L, H / 4, -Z / 3], [2 * L, H / 4, -Z / 3], # 31 32 [(5 / 4) * L, H / 2, 0], [(6 / 4) * L, H / 2, 0], # 33 34 [(7 / 4) * L, H / 2, 0], [2 * L, H / 2, 0], # 35 36 [(5 / 4) * L, 3 / 4 * H, 0], # 37 [(6 / 4) * L, 3 / 4 * H, 0], # 38 [(7 / 4) * L, 3 / 4 * H, 0], [2 * L, 3 / 4 * H, 0], # 39 40 [(5 / 4) * L, H, Z], [(6 / 4) * L, H, Z], # 41 42 [(7 / 4) * L, H, Z], [2 * L, H, Z] ]) # 43 44 # VTK ordering cells = np.array([[ 0, 4, 24, 20, 1, 2, 3, 9, 14, 19, 21, 22, 23, 5, 10, 15, 6, 7, 8, 11, 12, 13, 16, 17, 18 ], [ 4, 28, 44, 24, 25, 26, 27, 32, 36, 40, 41, 42, 43, 9, 14, 19, 29, 30, 31, 33, 34, 35, 37, 38, 39 ]]) cells = permute_cell_ordering( cells, permutation_vtk_to_dolfin(CellType.quadrilateral, cells.shape[1])) mesh = Mesh(MPI.comm_world, CellType.quadrilateral, points, cells, [], GhostMode.none) def e2(x): return x[2] + x[0] * x[1] V = FunctionSpace(mesh, ("CG", 4)) u = Function(V) cmap = fem.create_coordinate_map(mesh.ufl_domain()) mesh.geometry.coord_mapping = cmap u.interpolate(e2) intu = assemble_scalar(u * dx(mesh)) intu = MPI.sum(mesh.mpi_comm(), intu) nodes = [0, 5, 10, 15, 20] ref = sympy_scipy(points, nodes, 2 * L, H) assert ref == pytest.approx(intu, rel=1e-5)