How to use the meshio._mesh.CellBlock function in meshio

To help you get started, we’ve selected a few meshio examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nschloe / meshio / meshio / mdpa / _mdpa.py View on Github external
if t is None:
            t = inverse_num_nodes_per_cell[num_nodes_per_elem]

        if len(cells) == 0 or t != cells[-1].type:
            cells.append(CellBlock(t, []))
        # Subtract one to account for the fact that python indices are 0-based.
        cells[-1].data.append(numpy.array(data[-num_nodes_per_elem:]) - 1)

        # Using the property id as tag
        if t not in cell_tags:
            cell_tags[t] = []
        cell_tags[t].append([data[1]])

    # convert to numpy arrays
    for k, c in enumerate(cells):
        cells[k] = CellBlock(c.type, numpy.array(c.data, dtype=int))

    # Cannot convert cell_tags[key] to numpy array: There may be a
    # different number of tags for each cell.

    if line.strip() not in ["End Elements", "End Conditions"]:
        raise ReadError()
github nschloe / meshio / meshio / gmsh / _gmsh40.py View on Github external
"msh4 requires 3D points, but 2D points given. "
            "Appending 0 third component."
        )
        mesh.points = numpy.column_stack(
            [mesh.points[:, 0], mesh.points[:, 1], numpy.zeros(mesh.points.shape[0])]
        )

    if binary:
        for k, (key, value) in enumerate(mesh.cells):
            if value.dtype != c_int:
                logging.warning(
                    "Binary Gmsh needs c_int (typically numpy.int32) integers "
                    "(got %s). Converting.",
                    value.dtype,
                )
                mesh.cells[k] = CellBlock(key, numpy.array(value, dtype=c_int))

    cells = _meshio_to_gmsh_order(mesh.cells)

    with open(filename, "wb") as fh:
        mode_idx = 1 if binary else 0
        size_of_double = 8
        fh.write(
            "$MeshFormat\n4.0 {} {}\n".format(mode_idx, size_of_double).encode("utf-8")
        )
        if binary:
            numpy.array([1], dtype=c_int).tofile(fh)
            fh.write(b"\n")
        fh.write(b"$EndMeshFormat\n")

        if mesh.field_data:
            _write_physical_names(fh, mesh.field_data)
github nschloe / meshio / meshio / gmsh / _gmsh41.py View on Github external
if mesh.points.shape[1] == 2:
        logging.warning(
            "msh4 requires 3D points, but 2D points given. "
            "Appending 0 third component."
        )
        mesh.points = numpy.column_stack(
            [mesh.points[:, 0], mesh.points[:, 1], numpy.zeros(mesh.points.shape[0])]
        )

    if binary:
        for k, (key, value) in enumerate(mesh.cells):
            if value.dtype != c_int:
                logging.warning(
                    "Binary Gmsh needs c_size_t (got %s). Converting.", value.dtype
                )
                mesh.cells[k] = CellBlock(key, value.astype(c_size_t))

    cells = _meshio_to_gmsh_order(mesh.cells)

    with open(filename, "wb") as fh:
        file_type = 1 if binary else 0
        data_size = c_size_t.itemsize
        fh.write(b"$MeshFormat\n")
        fh.write("4.1 {} {}\n".format(file_type, data_size).encode("utf-8"))
        if binary:
            numpy.array([1], dtype=c_int).tofile(fh)
            fh.write(b"\n")
        fh.write(b"$EndMeshFormat\n")

        if mesh.field_data:
            _write_physical_names(fh, mesh.field_data)
github nschloe / meshio / meshio / ply / _ply.py View on Github external
cell_data[name] = []
                i += n + 1

        i = 0
        for name, dtype in zip(cell_data_names, cell_dtypes):
            n = int(data[i])
            dtype = ply_to_numpy_dtype[dtype[1]]
            data = [dtype(data[j]) for j in range(i + 1, i + n + 1)]
            if name == "vertex_indices":
                polygons[n].append(data)
            else:
                cell_data[name].append(data)
            i += n + 1

    cells = [
        CellBlock(cell_type_from_count(n), numpy.array(data))
        for (n, data) in polygons.items()
    ]

    return Mesh(verts, cells, point_data=point_data, cell_data=cell_data)
github nschloe / meshio / meshio / neuroglancer / _neuroglancer.py View on Github external
raise ReadError("The precomputed mesh data is too short")
    flat_vertices = np.frombuffer(buf, " num_vertices):
        raise ReadError("The mesh references nonexistent vertices")

    return Mesh(vertices, [CellBlock("triangle", triangles)])
github nschloe / meshio / meshio / stl / _stl.py View on Github external
def data_from_facets(facets):
    # Now, all facets contain the point coordinate. Try to identify individual
    # points and build the data arrays.
    pts = numpy.concatenate(facets)

    # TODO equip `unique()` with a tolerance
    # Use return_index so we can use sort on `idx` such that the order is
    # preserved; see .
    _, idx, inv = numpy.unique(pts, axis=0, return_index=True, return_inverse=True)
    k = numpy.argsort(idx)
    points = pts[idx[k]]
    inv_k = numpy.argsort(k)
    cells = [CellBlock("triangle", inv_k[inv].reshape(-1, 3))]
    return points, cells
github nschloe / meshio / meshio / nastran / _nastran.py View on Github external
if cell_type == "tetra":
                cell_type = "tetra10"
            elif cell_type == "pyramid":
                cell_type = "pyramid13"
            elif cell_type == "wedge":
                cell_type = "wedge15"
            elif cell_type == "hexahedron":
                cell_type = "hexahedron20"

        if len(cells) > 0 and cells[-1].type == cell_type:
            cells[-1].data.append(cell)
            cells_id[-1].append(cell_id)
            if cell_ref is not None:
                cell_refs[-1].append(cell_ref)
        else:
            cells.append(CellBlock(cell_type, [cell]))
            cells_id.append([cell_id])
            if cell_ref is not None:
                cell_refs.append([cell_ref])
github nschloe / meshio / meshio / nastran / _nastran.py View on Github external
keyword_prev = keyword

        # CellBlock card continuation for 2nd order CTETRA, CPYRA, CPENTA, CHEXA elements
        elif keyword[0] == "+":
            assert cell is not None
            cell.extend(chunks[1:])

    # Add the last cell
    add_cell(cell, cell_type, cell_ref, keyword_prev)

    # Convert to numpy arrays
    points = numpy.array(points)
    points_id = numpy.array(points_id, dtype=int)
    for k, (c, cid) in enumerate(zip(cells, cells_id)):
        cells[k] = CellBlock(c.type, numpy.array(c.data, dtype=int))
        cells_id[k] = numpy.array(cid, dtype=int)

    # Convert to natural point ordering
    # https://stackoverflow.com/questions/16992713/translate-every-element-in-numpy-array-according-to-key
    points_id_dict = dict(zip(points_id, numpy.arange(len(points), dtype=int)))
    points_id_get = numpy.vectorize(points_id_dict.__getitem__)
    for k, c in enumerate(cells):
        cells[k] = CellBlock(c.type, points_id_get(c.data))

    # Construct the mesh object
    mesh = Mesh(points, cells)
    mesh.points_id = points_id
    mesh.cells_id = cells_id
    if len(point_refs) > 0:
        mesh.point_data["nastran:ref"] = numpy.array(point_refs)
    if len(cell_refs) > 0: