def test_compute_closest_entity_3d(dim): points = np.array([0.9, 0, 1.135]) mesh = create_unit_cube(MPI.COMM_WORLD, 8, 8, 8) mesh.topology.create_entities(dim) tree = BoundingBoxTree(mesh, dim) num_entities_local = mesh.topology.index_map(dim).size_local + mesh.topology.index_map(dim).num_ghosts entities = np.arange(num_entities_local, dtype=np.int32) midpoint_tree = create_midpoint_tree(mesh, dim, entities) closest_entities = compute_closest_entity(tree, midpoint_tree, mesh, points) # Find which entity is colliding with known closest point on mesh p_c = np.array([0.9, 0, 1]) colliding_entity_bboxes = compute_collisions(tree, p_c) # Refine search by checking for actual collision if the entities are # cells if dim == mesh.topology.dim: colliding_cells = compute_colliding_cells(mesh, colliding_entity_bboxes, p_c) if len(colliding_cells) > 0: assert np.isin(closest_entities[0], colliding_cells) else: if len(colliding_entity_bboxes.links(0)) > 0: assert np.isin(closest_entities[0], colliding_entity_bboxes.links(0))
def test_compute_closest_sub_entity(dim): """Compute distance from subset of cells in a mesh to a point inside the mesh""" ref_distance = 0.31 xc, yc, zc = 0.5, 0.5, 0.5 points = np.array([xc + ref_distance, yc, zc]) mesh = create_unit_cube(MPI.COMM_WORLD, 8, 8, 8) mesh.topology.create_entities(dim) left_entities = locate_entities(mesh, dim, lambda x: x[0] <= xc) tree = BoundingBoxTree(mesh, dim, left_entities) midpoint_tree = create_midpoint_tree(mesh, dim, left_entities) closest_entities = compute_closest_entity(tree, midpoint_tree, mesh, points) # Find which entity is colliding with known closest point on mesh p_c = np.array([xc, yc, zc]) colliding_entity_bboxes = compute_collisions(tree, p_c) # Refine search by checking for actual collision if the entities are # cells if dim == mesh.topology.dim: colliding_cells = compute_colliding_cells(mesh, colliding_entity_bboxes, p_c) if len(colliding_cells) > 0: assert np.isin(closest_entities[0], colliding_cells) else: if len(colliding_entity_bboxes.links(0)) > 0: assert np.isin(closest_entities[0], colliding_entity_bboxes.links(0))
def test_compute_closest_entity_1d(dim): ref_distance = 0.75 N = 16 points = np.array([[-ref_distance, 0, 0], [2 / N, 2 * ref_distance, 0]]) mesh = create_unit_interval(MPI.COMM_WORLD, N) tree = BoundingBoxTree(mesh, dim) num_entities_local = mesh.topology.index_map(dim).size_local + mesh.topology.index_map(dim).num_ghosts entities = np.arange(num_entities_local, dtype=np.int32) midpoint_tree = create_midpoint_tree(mesh, dim, entities) closest_entities = compute_closest_entity(tree, midpoint_tree, mesh, points) # Find which entity is colliding with known closest point on mesh p_c = np.array([[0, 0, 0], [2 / N, 0, 0]]) colliding_entity_bboxes = compute_collisions(tree, p_c) # Refine search by checking for actual collision if the entities are # cells if dim == mesh.topology.dim: colliding_cells = compute_colliding_cells(mesh, colliding_entity_bboxes, p_c) for i in range(points.shape[0]): # If colliding entity is on process if colliding_cells.links(i).size > 0: assert np.isin(closest_entities[i], colliding_cells.links(i)) else: for i in range(points.shape[0]): # Only check closest entity if any bounding box on the # process intersects with the point if colliding_entity_bboxes.links(i).size > 0: assert np.isin(closest_entities[i], colliding_entity_bboxes.links(i))
def test_eval(V, W, Q, mesh): u1 = Function(V) u2 = Function(W) u3 = Function(Q) def e2(x): values = np.empty((3, x.shape[1])) values[0] = x[0] + x[1] + x[2] values[1] = x[0] - x[1] - x[2] values[2] = x[0] + x[1] + x[2] return values def e3(x): values = np.empty((9, x.shape[1])) values[0] = x[0] + x[1] + x[2] values[1] = x[0] - x[1] - x[2] values[2] = x[0] + x[1] + x[2] values[3] = x[0] values[4] = x[1] values[5] = x[2] values[6] = -x[0] values[7] = -x[1] values[8] = -x[2] return values u1.interpolate(lambda x: x[0] + x[1] + x[2]) u2.interpolate(e2) u3.interpolate(e3) x0 = (mesh.geometry.x[0] + mesh.geometry.x[1]) / 2.0 tree = BoundingBoxTree(mesh, mesh.geometry.dim) cell_candidates = compute_collisions(tree, x0) cell = compute_colliding_cells(mesh, cell_candidates, x0) first_cell = cell[0] assert np.allclose(u3.eval(x0, first_cell)[:3], u2.eval(x0, first_cell), rtol=1e-15, atol=1e-15)
def test_padded_bbox(padding): """ Test collision between two meshes separated by a distance of epsilon, and check if padding the mesh creates a possible collision """ eps = 1e-12 x0 = numpy.array([0, 0, 0]) x1 = numpy.array([1, 1, 1 - eps]) mesh_0 = BoxMesh(MPI.COMM_WORLD, [x0, x1], [1, 1, 2], cpp.mesh.CellType.hexahedron) x2 = numpy.array([0, 0, 1 + eps]) x3 = numpy.array([1, 1, 2]) mesh_1 = BoxMesh(MPI.COMM_WORLD, [x2, x3], [1, 1, 2], cpp.mesh.CellType.hexahedron) if padding: pad = eps else: pad = 0 bbox_0 = BoundingBoxTree(mesh_0, mesh_0.topology.dim, padding=pad) bbox_1 = BoundingBoxTree(mesh_1, mesh_1.topology.dim, padding=pad) collisions = compute_collisions(bbox_0, bbox_1) if padding: assert(len(collisions) == 1) # Check that the colliding elements are separated by a distance 2*epsilon element_0 = extract_geometricial_data(mesh_0, mesh_0.topology.dim, [collisions[0][0]])[0] element_1 = extract_geometricial_data(mesh_1, mesh_1.topology.dim, [collisions[0][1]])[0] distance = numpy.linalg.norm(cpp.geometry.compute_distance_gjk(element_0, element_1)) assert(numpy.isclose(distance, 2 * eps)) else: assert(len(collisions) == 0)
def test_compute_collisions_tree_3d(): references = [[ set([18, 19, 20, 21, 22, 23, 42, 43, 44, 45, 46, 47]), set([0, 1, 2, 3, 4, 5, 24, 25, 26, 27, 28, 29]) ], [ set([6, 7, 8, 9, 10, 11, 30, 31, 32, 33, 34, 35]), set([12, 13, 14, 15, 16, 17, 36, 37, 38, 39, 40, 41]) ]] points = [numpy.array([0.52, 0.51, 0.3]), numpy.array([0.9, -0.9, 0.3])] for i, point in enumerate(points): mesh_A = UnitCubeMesh(MPI.COMM_WORLD, 2, 2, 2) mesh_B = UnitCubeMesh(MPI.COMM_WORLD, 2, 2, 2) bgeom = mesh_B.geometry.x bgeom += point tree_A = BoundingBoxTree(mesh_A, mesh_A.topology.dim) tree_B = BoundingBoxTree(mesh_B, mesh_B.topology.dim) entities = compute_collisions(tree_A, tree_B) entities_A = set([q[0] for q in entities]) entities_B = set([q[1] for q in entities]) assert entities_A == references[i][0] assert entities_B == references[i][1]
def test_surface_bbtree(): """Test creation of BBTree on subset of entities(surface cells)""" mesh = create_unit_cube(MPI.COMM_WORLD, 8, 8, 8) sf = _cpp.mesh.exterior_facet_indices(mesh) tdim = mesh.topology.dim f_to_c = mesh.topology.connectivity(tdim - 1, tdim) cells = [f_to_c.links(f)[0] for f in sf] bbtree = BoundingBoxTree(mesh, tdim, cells) # test collision (should not collide with any) p = np.array([0.5, 0.5, 0.5]) assert len(compute_collisions(bbtree, p).array) == 0
def test_compute_collisions_tree_2d(point, cells): mesh_A = UnitSquareMesh(MPI.COMM_WORLD, 4, 4) mesh_B = UnitSquareMesh(MPI.COMM_WORLD, 4, 4) bgeom = mesh_B.geometry.x bgeom += point tree_A = BoundingBoxTree(mesh_A, mesh_A.topology.dim) tree_B = BoundingBoxTree(mesh_B, mesh_B.topology.dim) entities = compute_collisions(tree_A, tree_B) entities_A = set([q[0] for q in entities]) entities_B = set([q[1] for q in entities]) assert entities_A == set(cells[0]) assert entities_B == set(cells[1])
def test_compute_collisions_tree_2d(point): mesh_A = create_unit_square(MPI.COMM_WORLD, 3, 3) mesh_B = create_unit_square(MPI.COMM_WORLD, 5, 5) bgeom = mesh_B.geometry.x bgeom += point tree_A = BoundingBoxTree(mesh_A, mesh_A.topology.dim) tree_B = BoundingBoxTree(mesh_B, mesh_B.topology.dim) entities = compute_collisions(tree_A, tree_B) entities_A = np.sort(np.unique([q[0] for q in entities])) entities_B = np.sort(np.unique([q[1] for q in entities])) cells_A = find_colliding_cells(mesh_A, tree_B.get_bbox(tree_B.num_bboxes - 1)) cells_B = find_colliding_cells(mesh_B, tree_A.get_bbox(tree_A.num_bboxes - 1)) assert np.allclose(entities_A, cells_A) assert np.allclose(entities_B, cells_B)
def test_sub_bbtree(): """Testing point collision with a BoundingBoxTree of sub entitites""" mesh = create_unit_cube(MPI.COMM_WORLD, 4, 4, 4, cell_type=CellType.hexahedron) tdim = mesh.topology.dim fdim = tdim - 1 top_facets = locate_entities_boundary(mesh, fdim, lambda x: np.isclose(x[2], 1)) f_to_c = mesh.topology.connectivity(tdim - 1, tdim) cells = [f_to_c.links(f)[0] for f in top_facets] bbtree = BoundingBoxTree(mesh, tdim, cells) # Compute a BBtree for all processes process_bbtree = bbtree.create_global_tree(mesh.comm) # Find possible ranks for this point point = np.array([0.2, 0.2, 1.0]) ranks = compute_collisions(process_bbtree, point) # Compute local collisions cells = compute_collisions(bbtree, point) if MPI.COMM_WORLD.rank in ranks.array: assert len(cells.links(0)) > 0 else: assert len(cells.links(0)) == 0
def test_compute_collisions_tree_1d(point, cells): mesh_A = UnitIntervalMesh(MPI.COMM_WORLD, 16) mesh_B = UnitIntervalMesh(MPI.COMM_WORLD, 16) bgeom = mesh_B.geometry.x bgeom += point tree_A = BoundingBoxTree(mesh_A, mesh_A.topology.dim) tree_B = BoundingBoxTree(mesh_B, mesh_B.topology.dim) entities = geometry.compute_collisions(tree_A, tree_B) entities_A = set([q[0] for q in entities]) entities_B = set([q[1] for q in entities]) assert entities_A == cells[0] assert entities_B == cells[1]
def test_compute_collisions_tree_1d(point): mesh_A = UnitIntervalMesh(MPI.COMM_WORLD, 16) def locator_A(x): return x[0] >= point[0] # Locate all vertices of mesh A that should collide vertices_A = cpp.mesh.locate_entities(mesh_A, 0, locator_A) mesh_A.topology.create_connectivity(0, mesh_A.topology.dim) v_to_c = mesh_A.topology.connectivity(0, mesh_A.topology.dim) # Find all cells connected to vertex in the collision bounding box cells_A = numpy.sort( numpy.unique( numpy.hstack([v_to_c.links(vertex) for vertex in vertices_A]))) mesh_B = UnitIntervalMesh(MPI.COMM_WORLD, 16) bgeom = mesh_B.geometry.x bgeom += point def locator_B(x): return x[0] <= 1 # Locate all vertices of mesh B that should collide vertices_B = cpp.mesh.locate_entities(mesh_B, 0, locator_B) mesh_B.topology.create_connectivity(0, mesh_B.topology.dim) v_to_c = mesh_B.topology.connectivity(0, mesh_B.topology.dim) # Find all cells connected to vertex in the collision bounding box cells_B = numpy.sort( numpy.unique( numpy.hstack([v_to_c.links(vertex) for vertex in vertices_B]))) # Find colliding entities using bounding box trees tree_A = BoundingBoxTree(mesh_A, mesh_A.topology.dim) tree_B = BoundingBoxTree(mesh_B, mesh_B.topology.dim) entities = compute_collisions(tree_A, tree_B) entities_A = numpy.sort(numpy.unique([q[0] for q in entities])) entities_B = numpy.sort(numpy.unique([q[1] for q in entities])) assert numpy.allclose(entities_A, cells_A) assert numpy.allclose(entities_B, cells_B)
def test_compute_collisions_point_1d(): N = 16 p = np.array([0.3, 0, 0]) mesh = create_unit_interval(MPI.COMM_WORLD, N) dx = 1 / N cell_index = int(p[0] // dx) # Vertices of cell we should collide with vertices = np.array([[dx * cell_index, 0, 0], [dx * (cell_index + 1), 0, 0]]) # Compute collision tdim = mesh.topology.dim tree = BoundingBoxTree(mesh, tdim) entities = compute_collisions(tree, p) assert len(entities.array) == 1 # Get the vertices of the geometry geom_entities = _cpp.mesh.entities_to_geometry(mesh, tdim, entities.array, False)[0] x = mesh.geometry.x cell_vertices = x[geom_entities] # Check that we get the cell with correct vertices assert np.allclose(cell_vertices, vertices)
def test_surface_bbtree_collision(): """Compute collision between two meshes, where only one cell of each mesh are colliding""" tdim = 3 mesh1 = UnitCubeMesh(MPI.COMM_WORLD, 3, 3, 3, cpp.mesh.CellType.hexahedron) mesh2 = UnitCubeMesh(MPI.COMM_WORLD, 3, 3, 3, cpp.mesh.CellType.hexahedron) mesh2.geometry.x[:, :] += numpy.array([0.9, 0.9, 0.9]) sf = cpp.mesh.exterior_facet_indices(mesh1) f_to_c = mesh1.topology.connectivity(tdim - 1, tdim) # Compute unique set of cells (some will be counted multiple times) cells = list(set([f_to_c.links(f)[0] for f in sf])) bbtree1 = BoundingBoxTree(mesh1, tdim, cells) sf = cpp.mesh.exterior_facet_indices(mesh2) f_to_c = mesh2.topology.connectivity(tdim - 1, tdim) cells = list(set([f_to_c.links(f)[0] for f in sf])) bbtree2 = BoundingBoxTree(mesh2, tdim, cells) collisions = compute_collisions(bbtree1, bbtree2) assert len(collisions) == 1
def test_compute_collisions_tree_3d(point): mesh_A = UnitCubeMesh(MPI.COMM_WORLD, 2, 2, 2) mesh_B = UnitCubeMesh(MPI.COMM_WORLD, 2, 2, 2) bgeom = mesh_B.geometry.x bgeom += point tree_A = BoundingBoxTree(mesh_A, mesh_A.topology.dim) tree_B = BoundingBoxTree(mesh_B, mesh_B.topology.dim) entities = compute_collisions(tree_A, tree_B) entities_A = numpy.sort(numpy.unique([q[0] for q in entities])) entities_B = numpy.sort(numpy.unique([q[1] for q in entities])) cells_A = find_colliding_cells(mesh_A, tree_B.get_bbox(tree_B.num_bboxes - 1)) cells_B = find_colliding_cells(mesh_B, tree_A.get_bbox(tree_A.num_bboxes - 1)) assert numpy.allclose(entities_A, cells_A) assert numpy.allclose(entities_B, cells_B)
def test_collision_2nd_order_triangle(): points = np.array([[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [0.65, 0.65], [0.0, 0.5], [0.5, 0.0]]) cells = np.array([[0, 1, 2, 3, 4, 5]]) cell = ufl.Cell("triangle", geometric_dimension=2) domain = ufl.Mesh(ufl.VectorElement("Lagrange", cell, 2)) mesh = create_mesh(MPI.COMM_WORLD, cells, points, domain) # Sample points along an interior line of the domain. The last point # is outside the simplex made by the vertices. sample_points = np.array([[0.1, 0.3, 0.0], [0.2, 0.5, 0.0], [0.6, 0.6, 0.0]]) # Create boundingboxtree tree = geometry.BoundingBoxTree(mesh, mesh.geometry.dim) cell_candidates = geometry.compute_collisions(tree, sample_points) colliding_cells = geometry.compute_colliding_cells(mesh, cell_candidates, sample_points) # Check for collision for i in range(colliding_cells.num_nodes): assert len(colliding_cells.links(i)) == 1 # Check if there is a point on the linear approximation of the # curved facet def line_through_points(p0, p1): return lambda x: (p1[1] - p0[1]) / (p1[0] - p0[0]) * (x - p0[0]) + p0[1 ] line_func = line_through_points(points[2], points[3]) point = np.array([0.2, line_func(0.2), 0]) # Point inside 2nd order geometry, outside linear approximation # Usefull for debugging on a later stage # point = np.array([0.25, 0.89320760, 0]) distance = geometry.squared_distance(mesh, mesh.topology.dim - 1, [2], point) assert np.isclose(distance, 0)
def test_manifold_point_search(): # Simple two-triangle surface in 3d vertices = np.array([[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 = np.array([[0, 1, 2], [0, 1, 3]], dtype=np.int64) domain = ufl.Mesh(ufl.VectorElement("Lagrange", "triangle", 1)) mesh = create_mesh(MPI.COMM_WORLD, cells, vertices, domain) bb = BoundingBoxTree(mesh, mesh.topology.dim) # Find cell colliding with point points = np.array([[0.5, 0.25, 0.75], [0.25, 0.5, 0.75]]) cell_candidates = geometry.compute_collisions(bb, points) colliding_cells = geometry.compute_colliding_cells(mesh, cell_candidates, points) # Extract vertices of cell indices = _cpp.mesh.entities_to_geometry( mesh, mesh.topology.dim, [colliding_cells.links(0)[0], colliding_cells.links(1)[0]], False) cell_vertices = mesh.geometry.x[indices] # Compare vertices with input assert np.allclose(cell_vertices, vertices[cells])
b.ghostUpdate(addv=PETSc.InsertMode.ADD, mode=PETSc.ScatterMode.REVERSE) set_bc(b, [bc]) uc = Function(U) solver = PETSc.KSP().create(A_cond.getComm()) solver.setOperators(A_cond) solver.solve(b, uc.vector) # Pure displacement based formulation a = form(- ufl.inner(sigma_u(u), ufl.grad(v)) * ufl.dx) A = assemble_matrix(a, bcs=[bc]) A.assemble() # Create bounding box for function evaluation bb_tree = geometry.BoundingBoxTree(mesh, 2) # Check against standard table value p = np.array([48.0, 52.0, 0.0], dtype=np.float64) cell_candidates = geometry.compute_collisions(bb_tree, p) cells = geometry.compute_colliding_cells(mesh, cell_candidates, p) uc.x.scatter_forward() if len(cells) > 0: value = uc.eval(p, cells[0]) print(value[1]) assert np.isclose(value[1], 23.95, rtol=1.e-2) # Check the equality of displacement based and mixed condensed global # matrices, i.e. check that condensation is exact assert np.isclose((A - A_cond).norm(), 0.0)