def test_save_1d_mesh(tempdir, encoding): filename = os.path.join(tempdir, "mf_1D.xdmf") mesh = UnitIntervalMesh(MPI.comm_world, 32) mf = MeshFunction("size_t", mesh, mesh.topology.dim, 0) mf.values[:] = np.arange(mesh.num_entities(1)) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: file.write(mf)
def test_save_1d_tensor(tempdir): mesh = UnitIntervalMesh(MPI.COMM_WORLD, 32) element = ufl.TensorElement("Lagrange", mesh.ufl_cell(), 2, shape=(2, 2)) u = Function(FunctionSpace(mesh, element)) with u.vector.localForm() as loc: loc.set(1.0) filename = os.path.join(tempdir, "u.pvd") with VTKFile(mesh.mpi_comm(), filename, "w") as vtk: vtk.write_function(u, 0.)
def test_save_1d_scalar(tempdir, encoding): filename2 = os.path.join(tempdir, "u1_.xdmf") mesh = UnitIntervalMesh(MPI.comm_world, 32) # FIXME: This randomly hangs in parallel V = FunctionSpace(mesh, ("Lagrange", 2)) u = Function(V) u.vector.set(1.0 + (1j if has_petsc_complex else 0)) with XDMFFile(mesh.mpi_comm(), filename2, encoding=encoding) as file: file.write(u)
def test_save_1d_scalar(tempdir, encoding): filename2 = os.path.join(tempdir, "u1_.xdmf") mesh = UnitIntervalMesh(MPI.COMM_WORLD, 32) V = FunctionSpace(mesh, ("Lagrange", 2)) u = Function(V) u.vector.set(1.0 + (1j if has_petsc_complex else 0)) with XDMFFile(mesh.mpi_comm(), filename2, "w", encoding=encoding) as file: file.write_mesh(mesh) file.write_function(u)
def test_save_and_load_1d_mesh(tempdir, encoding): filename = os.path.join(tempdir, "mesh.xdmf") mesh = UnitIntervalMesh(MPI.comm_world, 32) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: file.write(mesh) with XDMFFile(MPI.comm_world, filename) as file: mesh2 = file.read_mesh(cpp.mesh.GhostMode.none) 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_save_and_load_1d_mesh(tempdir, encoding): filename = os.path.join(tempdir, "mesh.xdmf") mesh = UnitIntervalMesh(MPI.COMM_WORLD, 32) with XDMFFile(mesh.mpi_comm(), filename, "w", encoding=encoding) as file: file.write_mesh(mesh) with XDMFFile(MPI.COMM_WORLD, filename, "r", encoding=encoding) as file: mesh2 = file.read_mesh() assert mesh.topology.index_map(0).size_global == mesh2.topology.index_map(0).size_global assert mesh.topology.index_map(mesh.topology.dim).size_global == mesh2.topology.index_map( mesh.topology.dim).size_global
def test_small_mesh(): mesh3d = UnitCubeMesh(MPI.comm_world, 1, 1, 1) gdim = mesh3d.geometry.dim assert mesh3d.num_entities_global(gdim) == 6 mesh2d = UnitSquareMesh(MPI.comm_world, 1, 1) gdim = mesh2d.geometry.dim assert mesh2d.num_entities_global(gdim) == 2 mesh1d = UnitIntervalMesh(MPI.comm_world, 2) gdim = mesh1d.geometry.dim assert mesh1d.num_entities_global(gdim) == 2
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.points bgeom += point tree_A = BoundingBoxTree(mesh_A, mesh_A.topology.dim) tree_B = BoundingBoxTree(mesh_B, mesh_B.topology.dim) entities_A, entities_B = geometry.compute_collisions_bb(tree_A, tree_B) assert set(entities_A) == cells[0] assert set(entities_B) == cells[1]
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 = 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 mesh_factory(tdim, n): if tdim == 1: return UnitIntervalMesh(MPI.comm_world, n) elif tdim == 2: return UnitSquareMesh(MPI.comm_world, n, n) elif tdim == 3: return UnitCubeMesh(MPI.comm_world, n, n, n)
def test_save_1d_vector(tempfile, file_options): mesh = UnitIntervalMesh(MPI.comm_world, 32) u = Function(VectorFunctionSpace(mesh, ("Lagrange", 2))) u.vector.set(1.0) VTKFile(tempfile + "u.pvd").write(u) for file_option in file_options: VTKFile(tempfile + "u.pvd", file_option).write(u)
def test_compute_entity_collisions_1d(): reference = set([4]) p = numpy.array([0.3, 0.0, 0.0]) mesh = UnitIntervalMesh(MPI.comm_world, 16) tree = BoundingBoxTree(mesh, mesh.topology.dim) entities, _ = geometry.compute_entity_collisions_mesh(tree, mesh, p) assert set(entities) == reference
def mesh_factory(tdim, n): if tdim == 1: return UnitIntervalMesh(MPI.COMM_WORLD, n) elif tdim == 2: return UnitSquareMesh(MPI.COMM_WORLD, n, n) elif tdim == 3: return UnitCubeMesh(MPI.COMM_WORLD, n, n, n)
def mesh_1d(): """Create 1D mesh with degenerate cell""" mesh1d = UnitIntervalMesh(MPI.COMM_WORLD, 4) i1 = np.where((mesh1d.geometry.x == (0.75, 0, 0)).all(axis=1))[0][0] i2 = np.where((mesh1d.geometry.x == (1, 0, 0)).all(axis=1))[0][0] mesh1d.geometry.x[i2] = mesh1d.geometry.x[i1] return mesh1d
def test_save_1d_tensor(tempfile, file_options): mesh = UnitIntervalMesh(MPI.COMM_WORLD, 32) u = Function(TensorFunctionSpace(mesh, ("Lagrange", 2))) u.vector.set(1) VTKFile(tempfile + "u.pvd").write(u) for file_option in file_options: VTKFile(tempfile + "u.pvd", file_option).write(u)
def test_compute_first_entity_collision_1d(): reference = [4] p = numpy.array([0.3, 0, 0]) mesh = UnitIntervalMesh(MPI.comm_world, 16) tree = BoundingBoxTree(mesh, mesh.topology.dim) first = geometry.compute_first_entity_collision(tree, mesh, p) assert first in reference
def test_save_1d_mesh(tempfile, file_options): mesh = UnitIntervalMesh(MPI.COMM_WORLD, 32) VTKFile(tempfile + "mesh.pvd").write(mesh) f = VTKFile(tempfile + "mesh.pvd") f.write(mesh, 0.) f.write(mesh, 1.) for file_option in file_options: VTKFile(tempfile + "mesh.pvd", file_option).write(mesh)
def test_distance_interval(): mesh = UnitIntervalMesh(MPI.COMM_SELF, 1) assert cpp.geometry.squared_distance(mesh, mesh.topology.dim, 0, numpy.array([-1.0, 0, 0 ])) == pytest.approx(1.0) assert cpp.geometry.squared_distance(mesh, mesh.topology.dim, 0, numpy.array([0.5, 0, 0 ])) == pytest.approx(0.0)
def test_compute_first_collision_1d(): reference = {1: [4]} p = numpy.array([0.3, 0, 0]) mesh = UnitIntervalMesh(MPI.comm_world, 16) for dim in range(1, 2): tree = BoundingBoxTree(mesh, dim) first = geometry.compute_first_collision(tree, p) assert first in reference[dim]
def test_compute_collisions_point_1d(): reference = {1: set([4])} p = numpy.array([0.3, 0, 0]) mesh = UnitIntervalMesh(MPI.COMM_WORLD, 16) for dim in range(1, 2): tree = BoundingBoxTree(mesh, mesh.topology.dim) entities = compute_collisions_point(tree, p) assert set(entities) == reference[dim]
def test_save_1d_vector(tempdir): mesh = UnitIntervalMesh(MPI.COMM_WORLD, 32) def f(x): vals = np.zeros((2, x.shape[1])) vals[0] = x[0] vals[1] = 2 * x[0] * x[0] return vals element = ufl.VectorElement("Lagrange", mesh.ufl_cell(), 2, dim=2) u = Function(FunctionSpace(mesh, element)) u.interpolate(f) u.vector.ghostUpdate(addv=PETSc.InsertMode.INSERT, mode=PETSc.ScatterMode.FORWARD) filename = os.path.join(tempdir, "u.pvd") with VTKFile(MPI.COMM_WORLD, filename, "w") as vtk: vtk.write_function(u, 0.)
def test_distance_interval(): mesh = UnitIntervalMesh(MPI.comm_self, 1) cell = MeshEntity(mesh, mesh.topology.dim, 0) assert cpp.geometry.squared_distance(cell, numpy.array([-1.0, 0, 0 ])) == pytest.approx(1.0) assert cpp.geometry.squared_distance(cell, numpy.array([0.5, 0, 0 ])) == pytest.approx(0.0)
def testFacetArea(): references = [(UnitIntervalMesh(MPI.comm_world, 1), 2, 2), (UnitSquareMesh(MPI.comm_world, 1, 1), 4, 4), (UnitCubeMesh(MPI.comm_world, 1, 1, 1), 6, 3)] for mesh, surface, ref_int in references: c0 = ufl.FacetArea(mesh) c1 = dolfinx.FacetArea(mesh) assert (c0) assert (c1)
def test_compute_closest_entity_1d(): reference = (0, 1.0) p = numpy.array([-1.0, 0, 0]) mesh = UnitIntervalMesh(MPI.COMM_WORLD, 16) tree_mid = create_midpoint_tree(mesh) tree = BoundingBoxTree(mesh, mesh.topology.dim) entity, distance = compute_closest_entity(tree, tree_mid, mesh, p) assert entity == reference[0] assert distance[0] == pytest.approx(reference[1], 1.0e-12)
def test_save_1d_meshfunctions(tempfile, mesh_function_types, file_options, type_conv): mesh = UnitIntervalMesh(MPI.comm_world, 32) for d in range(mesh.topology.dim + 1): for t in mesh_function_types: mf = MeshFunction(t, mesh, mesh.topology.dim - d, type_conv[t](1)) VTKFile(tempfile + "mf.pvd").write(mf) f = VTKFile(tempfile + "mf.pvd") f.write(mf, 0.) f.write(mf, 1.)
def test_save_1d_scalar(tempfile, file_options): mesh = UnitIntervalMesh(MPI.COMM_WORLD, 32) u = Function(FunctionSpace(mesh, ("Lagrange", 2))) u.vector.set(1.0) VTKFile(tempfile + "u.pvd").write(u) f = VTKFile(tempfile + "u.pvd") f.write(u, 0.) f.write(u, 1.) for file_option in file_options: VTKFile(tempfile + "u.pvd", file_option).write(u)
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_small_mesh(): mesh3d = UnitCubeMesh(MPI.COMM_WORLD, 1, 1, 1) gdim = mesh3d.geometry.dim assert mesh3d.topology.index_map(gdim).size_global == 6 mesh2d = UnitSquareMesh(MPI.COMM_WORLD, 1, 1) gdim = mesh2d.geometry.dim assert mesh2d.topology.index_map(gdim).size_global == 2 mesh1d = UnitIntervalMesh(MPI.COMM_WORLD, 2) gdim = mesh1d.geometry.dim assert mesh1d.topology.index_map(gdim).size_global == 2
def test_save_1d_scalar(tempdir): mesh = UnitIntervalMesh(MPI.COMM_WORLD, 32) def f(x): return x[0] u = Function(FunctionSpace(mesh, ("CG", 2))) u.interpolate(f) u.vector.ghostUpdate(addv=PETSc.InsertMode.INSERT, mode=PETSc.ScatterMode.FORWARD) filename = os.path.join(tempdir, "u.pvd") with VTKFile(MPI.COMM_WORLD, filename, "w") as vtk: vtk.write_function(u, 0.) vtk.write_function(u, 1.)
def test_compute_closest_entity_1d(dim): ref_distance = 0.75 p = numpy.array([-ref_distance, 0, 0]) mesh = UnitIntervalMesh(MPI.COMM_WORLD, 16) 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, 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)