def test_assign_2D_facets():
    mesh = UnitSquareMesh(MPI.comm_world, 3, 3)
    mesh.create_connectivity(2, 1)
    tdim = mesh.topology.dim
    num_cell_facets = cpp.mesh.cell_num_entities(mesh.topology.cell_type,
                                                 tdim - 1)
    ncells = mesh.num_cells()

    f = MeshValueCollection("int", mesh, 1)
    all_new = True
    for c in range(ncells):
        value = ncells - c
        for i in range(num_cell_facets):
            all_new = all_new and f.set_value(c, i, value + i)

    g = MeshValueCollection("int", mesh, 1)
    g.assign(f)
    assert ncells * 3 == f.size()
    assert ncells * 3 == g.size()
    assert all_new

    for c in range(ncells):
        value = ncells - c
        for i in range(num_cell_facets):
            assert value + i == g.get_value(c, i)
Esempio n. 2
0
def test_save_mesh_value_collection(tempdir, encoding, data_type, cell_type):
    dtype_str, dtype = data_type
    mesh = UnitCubeMesh(MPI.comm_world, 4, 4, 4, cell_type)
    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. 3
0
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_mesh_function_assign_2D_facets():
    mesh = UnitSquareMesh(MPI.comm_world, 3, 3)
    mesh.create_entities(1)
    tdim = mesh.topology.dim
    num_cell_facets = cpp.mesh.cell_num_entities(mesh.topology.cell_type,
                                                 tdim - 1)

    f = MeshFunction("int", mesh, tdim - 1, 25)
    connectivity = mesh.topology.connectivity(tdim, tdim - 1)
    for c in range(mesh.num_cells()):
        facets = connectivity.links(c)
        for i in range(num_cell_facets):
            assert 25 == f.values[facets[i]]

    g = MeshValueCollection("int", mesh, 1)
    g.assign(f)
    assert mesh.num_entities(tdim - 1) == len(f.values)
    assert mesh.num_cells() * 3 == g.size()
    for c in range(mesh.num_cells()):
        for i in range(num_cell_facets):
            assert 25 == g.get_value(c, i)

    f2 = MeshFunction("int", mesh, g, 0)

    connectivity = mesh.topology.connectivity(tdim, tdim - 1)
    for c in range(mesh.num_cells()):
        facets = connectivity.links(c)
        for i in range(num_cell_facets):
            assert f2.values[facets[i]] == g.get_value(c, i)
def test_assign_2D_vertices():
    mesh = UnitSquareMesh(MPI.comm_world, 3, 3)
    mesh.create_connectivity(2, 0)
    ncells = mesh.num_cells()
    num_cell_vertices = cpp.mesh.cell_num_vertices(mesh.cell_type)

    f = MeshValueCollection("int", mesh, 0)
    all_new = True
    for c in range(ncells):
        value = ncells - c
        for i in range(num_cell_vertices):
            all_new = all_new and f.set_value(c, i, value + i)

    g = MeshValueCollection("int", mesh, 0)
    g.assign(f)
    assert ncells * 3 == f.size()
    assert ncells * 3 == g.size()
    assert all_new

    for c in range(ncells):
        value = ncells - c
        for i in range(num_cell_vertices):
            assert value + i == g.get_value(c, i)
def test_mesh_function_assign_2D_vertices():
    mesh = UnitSquareMesh(MPI.comm_world, 3, 3)
    mesh.create_entities(0)
    f = MeshFunction("int", mesh, 0, 25)
    g = MeshValueCollection("int", mesh, 0)
    g.assign(f)
    assert mesh.num_entities(0) == len(f.values)
    assert mesh.num_cells() * 3 == g.size()

    f2 = MeshFunction("int", mesh, g, 0)

    num_cell_vertices = cpp.mesh.cell_num_vertices(mesh.topology.cell_type)
    tdim = mesh.topology.dim
    connectivity = mesh.topology.connectivity(tdim, 0)
    for c in range(mesh.num_cells()):
        vertices = connectivity.links(c)
        for i in range(num_cell_vertices):
            assert 25 == g.get_value(c, i)
            assert f2.values[vertices[i]] == g.get_value(c, i)
Esempio n. 7
0
def test_save_and_read_mesh_value_collection_with_only_one_marked_entity(
        tempdir):
    ndiv = 2
    filename = os.path.join(tempdir, "mesh_value_collection.h5")
    mesh = UnitCubeMesh(MPI.comm_world, ndiv, ndiv, ndiv)
    mvc = MeshValueCollection("size_t", mesh, 3)
    mesh.create_entities(3)
    if MPI.rank(mesh.mpi_comm()) == 0:
        mvc.set_value(0, 1)

    # write to file
    with HDF5File(mesh.mpi_comm(), filename, 'w') as f:
        f.write(mvc, "/mesh_value_collection")

    # read from file
    with HDF5File(mesh.mpi_comm(), filename, 'r') as f:
        mvc = f.read_mvc_size_t(mesh, "/mesh_value_collection")
        assert MPI.sum(mesh.mpi_comm(), mvc.size()) == 1
        if MPI.rank(mesh.mpi_comm()) == 0:
            assert mvc.get_value(0, 0) == 1
def test_assign_2D_cells():
    mesh = UnitSquareMesh(MPI.comm_world, 3, 3)
    ncells = mesh.num_cells()
    f = MeshValueCollection("int", mesh, 2)
    all_new = True
    for c in range(ncells):
        value = ncells - c
        all_new = all_new and f.set_value(c, value)
    g = MeshValueCollection("int", mesh, 2)
    g.assign(f)
    assert ncells == f.size()
    assert ncells == g.size()
    assert all_new

    for c in range(ncells):
        value = ncells - c
        assert value, g.get_value(c == 0)

    old_value = g.get_value(0, 0)
    g.set_value(0, 0, old_value + 1)
    assert old_value + 1 == g.get_value(0, 0)
def test_mesh_function_assign_2D_cells():
    mesh = UnitSquareMesh(MPI.comm_world, 3, 3)
    ncells = mesh.num_cells()
    f = MeshFunction("int", mesh, mesh.topology.dim, 0)
    for c in range(ncells):
        f.values[c] = ncells - c

    g = MeshValueCollection("int", mesh, 2)
    g.assign(f)
    assert ncells == len(f.values)
    assert ncells == g.size()

    f2 = MeshFunction("int", mesh, g, 0)

    for c in range(mesh.num_cells()):
        value = ncells - c
        assert value == g.get_value(c, 0)
        assert f2.values[c] == g.get_value(c, 0)

    h = MeshValueCollection("int", mesh, 2)
    global_indices = mesh.topology.index_map(2).global_indices(True)

    ncells_global = mesh.num_entities_global(2)
    for c in range(mesh.num_cells()):
        if global_indices[c] in [5, 8, 10]:
            continue
        value = ncells_global - global_indices[c]
        h.set_value(c, int(value))

    f3 = MeshFunction("int", mesh, h, 0)

    values = f3.values
    values[values > ncells_global] = 0.

    assert MPI.sum(mesh.mpi_comm(), values.sum() * 1.0) == 140.
Esempio n. 10
0
def test_append_and_load_mesh_value_collections(tempdir, encoding, data_type,
                                                cell_type):
    dtype_str, dtype = data_type
    mesh = UnitCubeMesh(MPI.comm_world, 2, 2, 2, cell_type)
    mesh.create_connectivity_all()

    mvc_v = MeshValueCollection(dtype_str, mesh, 0)
    mvc_v.name = "vertices"
    mvc_e = MeshValueCollection(dtype_str, mesh, 1)
    mvc_e.name = "edges"
    mvc_f = MeshValueCollection(dtype_str, mesh, 2)
    mvc_f.name = "facets"
    mvc_c = MeshValueCollection(dtype_str, mesh, 3)
    mvc_c.name = "cells"

    mvcs = [mvc_v, mvc_e, mvc_f, mvc_c]

    filename = os.path.join(tempdir, "appended_mvcs.xdmf")

    with XDMFFile(mesh.mpi_comm(), filename) as xdmf:
        for mvc in mvcs:
            # global_indices = mesh.topology.global_indices(mvc.dim)
            map = mesh.topology.index_map(mvc.dim)
            global_indices = map.global_indices(True)

            for ent in range(mesh.num_entities(mvc.dim)):
                assert (mvc.set_value(ent, global_indices[ent]))
            xdmf.write(mvc)

    mvc_v_in = MeshValueCollection(dtype_str, mesh, 0)
    mvc_e_in = MeshValueCollection(dtype_str, mesh, 1)
    mvc_f_in = MeshValueCollection(dtype_str, mesh, 2)
    mvc_c_in = MeshValueCollection(dtype_str, mesh, 3)

    with XDMFFile(mesh.mpi_comm(), filename) as xdmf:
        read_function = getattr(xdmf, "read_mvc_" + dtype_str)
        mvc_v_in = read_function(mesh, "vertices")
        mvc_e_in = read_function(mesh, "edges")
        mvc_f_in = read_function(mesh, "facets")
        mvc_c_in = read_function(mesh, "cells")

    mvcs_in = [mvc_v_in, mvc_e_in, mvc_f_in, mvc_c_in]

    for (mvc, mvc_in) in zip(mvcs, mvcs_in):
        mf = MeshFunction(dtype_str, mesh, mvc, 0)
        mf_in = MeshFunction(dtype_str, mesh, mvc_in, 0)

        diff = mf_in.values - mf.values
        assert np.all(diff == 0)