Esempio n. 1
0
def test_save_mesh_value_collection(tempdir, encoding, data_type):
    dtype_str, dtype = data_type
    mesh = UnitCubeMesh(MPI.comm_world, 4, 4, 4)
    tdim = mesh.topology.dim
    meshfn = MeshFunction(dtype_str, mesh, mesh.topology.dim, False)
    meshfn.name = "volume_marker"
    mp = cpp.mesh.midpoints(mesh, tdim, range(mesh.num_entities(tdim)))
    for i in range(mesh.num_cells()):
        if mp[i, 1] > 0.1:
            meshfn.values[i] = 1
        if mp[i, 1] > 0.9:
            meshfn.values[i] = 2

    for mvc_dim in range(0, tdim + 1):
        mvc = MeshValueCollection(dtype_str, mesh, mvc_dim)
        tag = "dim_{}_marker".format(mvc_dim)
        mvc.name = tag
        mesh.create_connectivity(mvc_dim, tdim)
        mp = cpp.mesh.midpoints(mesh, mvc_dim,
                                range(mesh.num_entities(mvc_dim)))
        for e in range(mesh.num_entities(mvc_dim)):
            if (mp[e, 0] > 0.5):
                mvc.set_value(e, dtype(1))

        filename = os.path.join(tempdir, "mvc_{}.xdmf".format(mvc_dim))

        with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as xdmf:
            xdmf.write(meshfn)
            xdmf.write(mvc)

        with XDMFFile(mesh.mpi_comm(), filename) as xdmf:
            read_function = getattr(xdmf, "read_mvc_" + dtype_str)
            mvc = read_function(mesh, tag)
Esempio n. 2
0
def test_cell_iterators():
    "Iterate over cells"
    mesh = UnitCubeMesh(MPI.comm_world, 5, 5, 5)
    for i in range(4):
        mesh.init(3, i)

    # Test connectivity
    cons = [(i, mesh.topology.connectivity(3, i)) for i in range(4)]

    # Test writability
    for i, con in cons:

        def assign(con, i):
            con(i)[0] = 1

        with pytest.raises(Exception):
            assign(con, i)

    n = 0
    for i, c in enumerate(Cells(mesh)):
        n += 1
        for j, con in cons:
            assert numpy.all(con(i) == c.entities(j))

    assert n == mesh.num_cells()
Esempio n. 3
0
def test_save_and_read_mesh_3D(tempdir):
    filename = os.path.join(tempdir, "mesh3d.h5")

    # Write to file
    mesh0 = UnitCubeMesh(MPI.comm_world, 10, 10, 10)
    mesh_file = HDF5File(mesh0.mpi_comm(), filename, "w")
    mesh_file.write(mesh0, "/my_mesh")
    mesh_file.close()

    # Read from file
    mesh_file = HDF5File(mesh0.mpi_comm(), filename, "r")
    mesh1 = mesh_file.read_mesh("/my_mesh", False, cpp.mesh.GhostMode.none)
    mesh_file.close()

    assert mesh0.num_entities_global(0) == mesh1.num_entities_global(0)
    dim = mesh0.topology.dim
    assert mesh0.num_entities_global(dim) == mesh1.num_entities_global(dim)

    # Read from file, and use partition from file
    mesh_file = HDF5File(mesh0.mpi_comm(), filename, "r")
    mesh2 = mesh_file.read_mesh("/my_mesh", True, cpp.mesh.GhostMode.none)
    mesh_file.close()

    assert mesh0.num_cells() == mesh2.num_cells()
    dim = mesh0.topology.dim
    assert mesh0.num_entities_global(dim) == mesh1.num_entities_global(dim)
Esempio n. 4
0
def test_mixed_iterators():
    "Iterate over vertices of cells"

    mesh = UnitCubeMesh(MPI.comm_world, 5, 5, 5)
    n = 0
    for c in Cells(mesh):
        for v in VertexRange(c):
            n += 1
    assert n == 4 * mesh.num_cells()
Esempio n. 5
0
def test_ghost_3d(mode):
    N = 2
    num_cells = N * N * N * 6

    mesh = UnitCubeMesh(MPI.comm_world, N, N, N, ghost_mode=mode)
    if MPI.size(mesh.mpi_comm()) > 1:
        assert MPI.sum(mesh.mpi_comm(), mesh.num_cells()) > num_cells

    assert mesh.num_entities_global(0) == 27
    assert mesh.num_entities_global(3) == num_cells
Esempio n. 6
0
def test_UnitCubeMeshLocal():
    """Create mesh of unit cube."""
    mesh = UnitCubeMesh(MPI.comm_self, 5, 7, 9)
    assert mesh.num_vertices() == 480
    assert mesh.num_cells() == 1890
    assert mesh.geometry.dim == 3