def test_save_and_read_mesh_value_collection(tempdir): ndiv = 2 filename = os.path.join(tempdir, "mesh_value_collection.h5") mesh = UnitCubeMesh(MPI.comm_world, ndiv, ndiv, ndiv) # write to file with HDF5File(mesh.mpi_comm(), filename, 'w') as f: for dim in range(mesh.topology.dim): mvc = MeshValueCollection("size_t", mesh, dim) mesh.create_entities(dim) mp = cpp.mesh.midpoints(mesh, dim, range(mesh.num_entities(dim))) for e in range(mesh.num_entities(dim)): # this can be easily computed to the check the value val = int(ndiv * mp[e].sum()) + 1 mvc.set_value(e, val) f.write(mvc, "/mesh_value_collection_{}".format(dim)) # read from file with HDF5File(mesh.mpi_comm(), filename, 'r') as f: for dim in range(mesh.topology.dim): mvc = f.read_mvc_size_t(mesh, "/mesh_value_collection_{}".format(dim)) mp = cpp.mesh.midpoints(mesh, dim, range(mesh.num_entities(dim))) # check the values for (cell, lidx), val in mvc.values().items(): eidx = MeshEntity(mesh, mesh.topology.dim, cell).entities(dim)[lidx] mid = mp[eidx] assert val == int(ndiv * mid.sum()) + 1
def test_tabulate_all_coordinates(mesh_factory): func, args = mesh_factory mesh = func(*args) V = FunctionSpace(mesh, ("Lagrange", 1)) W0 = FiniteElement("Lagrange", mesh.ufl_cell(), 1) W1 = VectorElement("Lagrange", mesh.ufl_cell(), 1) W = FunctionSpace(mesh, W0 * W1) D = mesh.geometry.dim V_dofmap = V.dofmap W_dofmap = W.dofmap all_coords_V = V.tabulate_dof_coordinates() all_coords_W = W.tabulate_dof_coordinates() local_size_V = V_dofmap().index_map.size_local * V_dofmap().index_map.block_size local_size_W = W_dofmap().index_map.size_local * W_dofmap().index_map.block_size all_coords_V = all_coords_V.reshape(local_size_V, D) all_coords_W = all_coords_W.reshape(local_size_W, D) checked_V = [False] * local_size_V checked_W = [False] * local_size_W # Check that all coordinates are within the cell it should be map = mesh.topology.index_map(mesh.topology.dim) num_cells = map.size_local + map.num_ghosts for c in range(num_cells): cell = MeshEntity(mesh, mesh.topology.dim, c) dofs_V = V_dofmap.cell_dofs(c) for di in dofs_V: if di >= local_size_V: continue assert cell.contains(all_coords_V[di]) checked_V[di] = True dofs_W = W_dofmap.cell_dofs(c) for di in dofs_W: if di >= local_size_W: continue assert cell.contains(all_coords_W[di]) checked_W[di] = True # Assert that all dofs have been checked by the above assert all(checked_V) assert all(checked_W)
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 test_distance_tetrahedron(): mesh = UnitCubeMesh(MPI.comm_self, 1, 1, 1) cell = MeshEntity(mesh, mesh.topology.dim, 5) assert cpp.geometry.squared_distance(cell, numpy.array([-1.0, -1.0, -1.0 ])) == pytest.approx(3.0) assert cpp.geometry.squared_distance(cell, numpy.array([-1.0, 0.5, 0.5 ])) == pytest.approx(1.0) assert cpp.geometry.squared_distance(cell, numpy.array([0.5, 0.5, 0.5 ])) == pytest.approx(0.0)
def test_distance_triangle(): mesh = UnitSquareMesh(MPI.comm_self, 1, 1) cell = MeshEntity(mesh, mesh.topology.dim, 1) assert cpp.geometry.squared_distance(cell, numpy.array([-1.0, -1.0, 0.0 ])) == pytest.approx(2.0) assert cpp.geometry.squared_distance(cell, numpy.array([-1.0, 0.5, 0.0 ])) == pytest.approx(1.0) assert cpp.geometry.squared_distance(cell, numpy.array([0.5, 0.5, 0.0 ])) == pytest.approx(0.0)
def test_tabulate_coord_periodic(mesh_factory): def periodic_boundary(x): return x[0] < np.finfo(float).eps func, args = mesh_factory mesh = func(*args) V = FiniteElement("Lagrange", mesh.ufl_cell(), 1) Q = VectorElement("Lagrange", mesh.ufl_cell(), 1) W = V * Q V = FunctionSpace(mesh, V, constrained_domain=periodic_boundary) W = FunctionSpace(mesh, W, constrained_domain=periodic_boundary) L0 = W.sub(0) L1 = W.sub(1) L01 = L1.sub(0) L11 = L1.sub(1) sdim = V.element.space_dimension() coord0 = np.zeros((sdim, 2), dtype="d") coord1 = np.zeros((sdim, 2), dtype="d") coord2 = np.zeros((sdim, 2), dtype="d") coord3 = np.zeros((sdim, 2), dtype="d") map = mesh.topology.index_map(mesh.topology.dim) num_cells = map.size_local + map.num_ghosts for i in range(num_cells): cell = MeshEntity(mesh, mesh.topology.dim, i) coord0 = V.element.tabulate_dof_coordinates(cell) coord1 = L0.element.tabulate_dof_coordinates(cell) coord2 = L01.element.tabulate_dof_coordinates(cell) coord3 = L11.element.tabulate_dof_coordinates(cell) coord4 = L1.element.tabulate_dof_coordinates(cell) assert (coord0 == coord1).all() assert (coord0 == coord2).all() assert (coord0 == coord3).all() assert (coord4[:sdim] == coord0).all() assert (coord4[sdim:] == coord0).all()
def test_mesh_topology_against_fiat(mesh_factory, ghost_mode=cpp.mesh.GhostMode.none): """Test that mesh cells have topology matching to FIAT reference cell they were created from. """ func, args = mesh_factory xfail_ghosted_quads_hexes(func, ghost_mode) mesh = func(*args) if not is_simplex(mesh.topology.cell_type): return # Create FIAT cell cell_name = cpp.mesh.to_string(mesh.topology.cell_type) fiat_cell = FIAT.ufc_cell(cell_name) # Initialize all mesh entities and connectivities mesh.topology.create_connectivity_all() map = mesh.topology.index_map(mesh.topology.dim) num_cells = map.size_local + map.num_ghosts for i in range(num_cells): cell = MeshEntity(mesh, mesh.topology.dim, i) # Get mesh-global (MPI-local) indices of cell vertices vertex_global_indices = cell.entities(0) # Loop over all dimensions of reference cell topology for d, d_topology in fiat_cell.get_topology().items(): # Get entities of dimension d on the cell entities = cell.entities(d) if len(entities) == 0: # Fixup for highest dimension entities = (i, ) # Loop over all entities of fixed dimension d for entity_index, entity_topology in d_topology.items(): # Check that entity vertices map to cell vertices in correct order entity = MeshEntity(mesh, d, entities[entity_index]) vertices_dolfin = np.sort(entity.entities(0)) vertices_fiat = np.sort( vertex_global_indices[np.array(entity_topology)]) assert all(vertices_fiat == vertices_dolfin)
def c5(mesh3d): # Regular tetrahedron with edge sqrt(2) return MeshEntity(mesh3d, mesh3d.topology.dim, 5)
def c1(mesh3d): # Degenerate cell return MeshEntity(mesh3d, mesh3d.topology.dim, 1)
def c0(mesh3d): """Original tetrahedron from UnitCubeMesh(MPI.COMM_WORLD, 1, 1, 1)""" return MeshEntity(mesh3d, mesh3d.topology.dim, 0)
def c0(mesh3d): """Original tetrahedron from UnitCubeMesh(MPI.comm_world, 1, 1, 1)""" return MeshEntity(mesh3d, mesh3d.topology.dim, 0)