def test_near_evaluations(R, mesh): # Test that we allow point evaluation that are slightly outside u0 = Function(R) u0.vector.set(1.0) offset = 0.99 * np.finfo(float).eps bb_tree = geometry.BoundingBoxTree(mesh, mesh.geometry.dim) a = mesh.geometry.x[0] cell_candidates = geometry.compute_collisions_point(bb_tree, a) cells = geometry.select_colliding_cells(mesh, cell_candidates, a, 1) a_shift_x = np.array([a[0] - offset, a[1], a[2]]) cell_candidates = geometry.compute_collisions_point(bb_tree, a_shift_x) cells_shift_x = geometry.select_colliding_cells(mesh, cell_candidates, a_shift_x, 1) assert u0.eval(a, cells)[0] == pytest.approx( u0.eval(a_shift_x, cells_shift_x)[0]) a_shift_xyz = np.array([ a[0] - offset / math.sqrt(3), a[1] - offset / math.sqrt(3), a[2] - offset / math.sqrt(3) ]) cell_candidates = geometry.compute_collisions_point(bb_tree, a) cells_shift_xyz = geometry.select_colliding_cells(mesh, cell_candidates, a_shift_xyz, 1) assert u0.eval(a, cells)[0] == pytest.approx( u0.eval(a_shift_xyz, cells_shift_xyz)[0])
def test_midpoint_tree(N): """ Test that midpoint tree speed up compute_closest_entity """ mesh = UnitCubeMesh(MPI.COMM_WORLD, N, N, N) mesh.topology.create_entities(mesh.topology.dim) left_cells = locate_entities(mesh, mesh.topology.dim, lambda x: x[0] <= 0.4) tree = BoundingBoxTree(mesh, mesh.topology.dim, left_cells) midpoint_tree = create_midpoint_tree(mesh, mesh.topology.dim, left_cells) p = numpy.array([1 / 3, 2 / 3, 2]) # Find entity closest to point in two steps # 1. Find closest midpoint using midpoint tree entity_m, distance_m = compute_closest_entity(midpoint_tree, p, mesh) # 2. Refine search by using exact distance query entity, distance = compute_closest_entity(tree, p, mesh, R=distance_m) # Find entity closest to point in one step e_r, d_r = compute_closest_entity(tree, p, mesh) assert entity == e_r assert distance == d_r if len(left_cells) > 0: assert distance < distance_m else: assert distance == -1 p_c = numpy.array([1 / 3, 2 / 3, 1]) entities = compute_collisions_point(tree, p_c) entities = select_colliding_cells(mesh, entities, p_c, len(entities)) if len(entities) > 0: assert numpy.isin(e_r, entities)
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 p = numpy.array([0.5 + ref_distance, 0.5, 0.5]) mesh = UnitCubeMesh(MPI.COMM_WORLD, 8, 8, 8) mesh.topology.create_entities(dim) left_entities = locate_entities(mesh, dim, lambda x: x[0] <= 0.5) tree = BoundingBoxTree(mesh, dim, left_entities) entity, distance = compute_closest_entity(tree, p, mesh) min_distance = MPI.COMM_WORLD.allreduce(distance, op=MPI.MIN) assert min_distance == pytest.approx(ref_distance, 1.0e-12) # Find which entity is colliding with known closest point on mesh p_c = numpy.array([0.5, 0.5, 0.5]) entities = compute_collisions_point(tree, p_c) # Refine search by checking for actual collision if the entities are # cells if dim == mesh.topology.dim: entities = select_colliding_cells(mesh, entities, p_c, len(entities)) if len(entities) > 0: assert numpy.isin(entity, entities)
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) for point in sample_points: cell_candidates = geometry.compute_collisions_point(tree, point) colliding_cell = geometry.select_colliding_cells( mesh, cell_candidates, point, 1) assert len(colliding_cell) == 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 = cpp.geometry.squared_distance(mesh, mesh.topology.dim - 1, 2, point) assert np.isclose(distance, 0)
def test_compute_closest_entity_2d(dim): p = numpy.array([-1.0, -0.01, 0.0]) mesh = UnitSquareMesh(MPI.COMM_WORLD, 15, 15) tree = BoundingBoxTree(mesh, dim) entity, distance = compute_closest_entity(tree, p, mesh) min_distance = MPI.COMM_WORLD.allreduce(distance, op=MPI.MIN) ref_distance = numpy.sqrt(p[0]**2 + p[1]**2) assert min_distance == pytest.approx(ref_distance, 1.0e-12) # Find which entity is colliding with known closest point on mesh p_c = numpy.array([0, 0, 0]) entities = compute_collisions_point(tree, p_c) # Refine search by checking for actual collision if the entities are # cells # NOTE: Could be done for all entities if we generalize # select_colliding_cells to select_colliding_entities if dim == mesh.topology.dim: entities = select_colliding_cells(mesh, entities, p_c, len(entities)) if len(entities) > 0: assert numpy.isin(entity, entities)
def test_compute_closest_entity_3d(dim): ref_distance = 0.135 p = numpy.array([0.9, 0, 1 + ref_distance]) mesh = UnitCubeMesh(MPI.COMM_WORLD, 8, 8, 8) mesh.topology.create_entities(dim) tree = BoundingBoxTree(mesh, dim) entity, distance = compute_closest_entity(tree, p, mesh) min_distance = MPI.COMM_WORLD.allreduce(distance, op=MPI.MIN) assert min_distance == pytest.approx(ref_distance, 1.0e-12) # Find which entity is colliding with known closest point on mesh p_c = numpy.array([0.9, 0, 1]) entities = compute_collisions_point(tree, p_c) # Refine search by checking for actual collision if the entities are # cells # NOTE: Could be done for all entities if we generalize # select_colliding_cells to select_colliding_entities if dim == mesh.topology.dim: entities = select_colliding_cells(mesh, entities, p_c, len(entities)) if len(entities) > 0: assert numpy.isin(entity, entities)
else: df.value = 0.0 fecoda.main.solve_displ_system(J[0], F[0], intern_var0, intern_var1, expr_compiled, w0, w1, bcs_displ, df, dt, t, k) fecoda.main.post_update(expr_compiled, intern_var0, intern_var1, w0, w1) fecoda.main.copyout_state(w0, w1, intern_var0, intern_var1) force.value += df.value bb_tree = BoundingBoxTree(mesh, 3) p = numpy.array([0.0, 0.0, 0.3], dtype=numpy.float64) cell_candidates = compute_collisions_point(bb_tree, p) cell = select_colliding_cells(mesh, cell_candidates, p, 1) interpolate(ufl.sym(ufl.grad(w1["displ"])), strain) if len(cell) > 0: value = strain.eval(p, cell) value = value[-1] else: value = None values = comm.gather(value, root=0) if rank == 0: value = [x for x in values if x is not None][0] compl = value * -1.0e+6 / args.sigma log["compl"].append(compl) log["times"].append(float(t.value) - tp - fecoda.mps.t_begin)