Skip to content

Commit

Permalink
Merge pull request #1234 from nschloe/cellblock-dim
Browse files Browse the repository at this point in the history
add cell_block.dim, many gmsh fixes
  • Loading branch information
nschloe authored Dec 12, 2021
2 parents 184fb02 + 7b8bb57 commit 8ef1548
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 257 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = meshio
version = 5.1.0
version = 5.1.1
author = Nico Schlömer et al.
author_email = [email protected]
description = I/O for many mesh formats
Expand Down
1 change: 0 additions & 1 deletion src/meshio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
xdmf,
)
from .__about__ import __version__
from ._common import topological_dimension
from ._exceptions import ReadError, WriteError
from ._helpers import (
extension_to_filetype,
Expand Down
66 changes: 0 additions & 66 deletions src/meshio/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,72 +83,6 @@
"tetra286": 286,
}

topological_dimension = {
"line": 1,
"triangle": 2,
"quad": 2,
"tetra": 3,
"hexahedron": 3,
"wedge": 3,
"pyramid": 3,
"line3": 1,
"triangle6": 2,
"quad9": 2,
"tetra10": 3,
"hexahedron27": 3,
"wedge18": 3,
"pyramid14": 3,
"vertex": 0,
"quad8": 2,
"hexahedron20": 3,
"triangle10": 2,
"triangle15": 2,
"triangle21": 2,
"line4": 1,
"line5": 1,
"line6": 1,
"tetra20": 3,
"tetra35": 3,
"tetra56": 3,
"quad16": 2,
"quad25": 2,
"quad36": 2,
"triangle28": 2,
"triangle36": 2,
"triangle45": 2,
"triangle55": 2,
"triangle66": 2,
"quad49": 2,
"quad64": 2,
"quad81": 2,
"quad100": 2,
"quad121": 2,
"line7": 1,
"line8": 1,
"line9": 1,
"line10": 1,
"line11": 1,
"tetra84": 3,
"tetra120": 3,
"tetra165": 3,
"tetra220": 3,
"tetra286": 3,
"wedge40": 3,
"wedge75": 3,
"hexahedron64": 3,
"hexahedron125": 3,
"hexahedron216": 3,
"hexahedron343": 3,
"hexahedron512": 3,
"hexahedron729": 3,
"hexahedron1000": 3,
"wedge126": 3,
"wedge196": 3,
"wedge288": 3,
"wedge405": 3,
"wedge550": 3,
}


def cell_data_from_raw(cells, cell_data_raw):
cs = np.cumsum([len(c) for c in cells])[:-1]
Expand Down
85 changes: 82 additions & 3 deletions src/meshio/_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,97 @@

from ._common import num_nodes_per_cell

topological_dimension = {
"line": 1,
"polygon": 2,
"triangle": 2,
"quad": 2,
"tetra": 3,
"hexahedron": 3,
"wedge": 3,
"pyramid": 3,
"line3": 1,
"triangle6": 2,
"quad9": 2,
"tetra10": 3,
"hexahedron27": 3,
"wedge18": 3,
"pyramid14": 3,
"vertex": 0,
"quad8": 2,
"hexahedron20": 3,
"triangle10": 2,
"triangle15": 2,
"triangle21": 2,
"line4": 1,
"line5": 1,
"line6": 1,
"tetra20": 3,
"tetra35": 3,
"tetra56": 3,
"quad16": 2,
"quad25": 2,
"quad36": 2,
"triangle28": 2,
"triangle36": 2,
"triangle45": 2,
"triangle55": 2,
"triangle66": 2,
"quad49": 2,
"quad64": 2,
"quad81": 2,
"quad100": 2,
"quad121": 2,
"line7": 1,
"line8": 1,
"line9": 1,
"line10": 1,
"line11": 1,
"tetra84": 3,
"tetra120": 3,
"tetra165": 3,
"tetra220": 3,
"tetra286": 3,
"wedge40": 3,
"wedge75": 3,
"hexahedron64": 3,
"hexahedron125": 3,
"hexahedron216": 3,
"hexahedron343": 3,
"hexahedron512": 3,
"hexahedron729": 3,
"hexahedron1000": 3,
"wedge126": 3,
"wedge196": 3,
"wedge288": 3,
"wedge405": 3,
"wedge550": 3,
"VTK_LAGRANGE_CURVE": 1,
"VTK_LAGRANGE_TRIANGLE": 2,
"VTK_LAGRANGE_QUADRILATERAL": 2,
"VTK_LAGRANGE_TETRAHEDRON": 3,
"VTK_LAGRANGE_HEXAHEDRON": 3,
"VTK_LAGRANGE_WEDGE": 3,
"VTK_LAGRANGE_PYRAMID": 3,
}


class CellBlock:
def __init__(
self,
type: str,
cell_type: str,
data: list | np.ndarray,
tags: list[str] | None = None,
):
self.type = type
self.type = cell_type
self.data = data
if not type.startswith("polyhedron"):

if cell_type.startswith("polyhedron"):
self.dim = 3
else:
self.data = np.asarray(self.data)
self.dim = topological_dimension[cell_type]

self.tags = [] if tags is None else tags

def __repr__(self):
Expand Down
35 changes: 13 additions & 22 deletions src/meshio/gmsh/_gmsh22.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
I/O for Gmsh's msh format, cf.
<http://gmsh.info//doc/texinfo/gmsh.html#File-formats>.
"""
from __future__ import annotations

import logging

import numpy as np
Expand Down Expand Up @@ -128,7 +130,9 @@ def _read_cells(f, cells, point_tags, is_ascii):
_read_cells_ascii(f, cells, cell_tags, total_num_cells)
else:
_read_cells_binary(f, cells, cell_tags, total_num_cells)
cells[:] = _gmsh_to_meshio_order(cells)

# override cells in-place
cells[:] = [(key, _gmsh_to_meshio_order(key, values)) for key, values in cells]

point_tags = np.asarray(point_tags, dtype=np.int32) - 1
remap = -np.ones((np.max(point_tags) + 1,), dtype=np.int32)
Expand Down Expand Up @@ -263,8 +267,6 @@ def write(filename, mesh, float_fmt=".16e", binary=True):
"""Writes msh files, cf.
<http://gmsh.info//doc/texinfo/gmsh.html#MSH-ASCII-file-format>.
"""
cells = _meshio_to_gmsh_order(mesh.cells)

# Filter the point data: gmsh:dim_tags are tags, the rest is actual point data.
point_data = {}
for key, d in mesh.point_data.items():
Expand All @@ -288,7 +290,9 @@ def write(filename, mesh, float_fmt=".16e", binary=True):
logging.warning(
f"Appending zeros to replace the missing {tag[5:]} tag data."
)
tag_data[tag] = [np.zeros(len(x.data), dtype=c_int) for x in mesh.cells]
tag_data[tag] = [
np.zeros(len(cell_block), dtype=c_int) for cell_block in mesh.cells
]

with open(filename, "wb") as fh:
mode_idx = 1 if binary else 0
Expand All @@ -303,7 +307,7 @@ def write(filename, mesh, float_fmt=".16e", binary=True):
_write_physical_names(fh, mesh.field_data)

_write_nodes(fh, mesh.points, float_fmt, binary)
_write_elements(fh, cells, tag_data, binary)
_write_elements(fh, mesh.cells, tag_data, binary)
if mesh.gmsh_periodic is not None:
_write_periodic(fh, mesh.gmsh_periodic, float_fmt)

Expand Down Expand Up @@ -335,31 +339,18 @@ def _write_nodes(fh, points, float_fmt, binary):
fh.write(b"$EndNodes\n")


def _write_elements(fh, cells, tag_data, binary):
def _write_elements(fh, cells: list[CellBlock], tag_data, binary: bool):
# write elements
fh.write(b"$Elements\n")

# count all cells
total_num_cells = 0
for k, cell_block in enumerate(cells):
if isinstance(cell_block, tuple):
node_idcs = cell_block[1]
else:
assert isinstance(cell_block, CellBlock)
node_idcs = cell_block.data

total_num_cells += len(node_idcs)

total_num_cells = sum(len(cell_block) for cell_block in cells)
fh.write(f"{total_num_cells}\n".encode())

consecutive_index = 0
for k, cell_block in enumerate(cells):
if isinstance(cell_block, tuple):
cell_type, node_idcs = cell_block
else:
assert isinstance(cell_block, CellBlock)
cell_type = cell_block.type
node_idcs = cell_block.data
cell_type = cell_block.type
node_idcs = _meshio_to_gmsh_order(cell_type, cell_block.data)

tags = []
for name in ["gmsh:physical", "gmsh:geometrical", "cell_tags"]:
Expand Down
Loading

0 comments on commit 8ef1548

Please sign in to comment.