Giter VIP home page Giter VIP logo

meshio's Introduction

meshio

I/O for mesh files.

PyPi Version Anaconda Cloud Packaging status PyPI pyversions DOI GitHub stars Downloads

Discord

gh-actions codecov LGTM Code style: black

There are various mesh formats available for representing unstructured meshes. meshio can read and write all of the following and smoothly converts between them:

Abaqus (.inp), ANSYS msh (.msh), AVS-UCD (.avs), CGNS (.cgns), DOLFIN XML (.xml), Exodus (.e, .exo), FLAC3D (.f3grid), H5M (.h5m), Kratos/MDPA (.mdpa), Medit (.mesh, .meshb), MED/Salome (.med), Nastran (bulk data, .bdf, .fem, .nas), Netgen (.vol, .vol.gz), Neuroglancer precomputed format, Gmsh (format versions 2.2, 4.0, and 4.1, .msh), OBJ (.obj), OFF (.off), PERMAS (.post, .post.gz, .dato, .dato.gz), PLY (.ply), STL (.stl), Tecplot .dat, TetGen .node/.ele, SVG (2D output only) (.svg), SU2 (.su2), UGRID (.ugrid), VTK (.vtk), VTU (.vtu), WKT (TIN) (.wkt), XDMF (.xdmf, .xmf).

(Here's a little survey on which formats are actually used.)

Install with one of

pip install meshio[all]
conda install -c conda-forge meshio

([all] pulls in all optional dependencies. By default, meshio only uses numpy.) You can then use the command-line tool

meshio convert    input.msh output.vtk   # convert between two formats

meshio info       input.xdmf             # show some info about the mesh

meshio compress   input.vtu              # compress the mesh file
meshio decompress input.vtu              # decompress the mesh file

meshio binary     input.msh              # convert to binary format
meshio ascii      input.msh              # convert to ASCII format

with any of the supported formats.

In Python, simply do

import meshio

mesh = meshio.read(
    filename,  # string, os.PathLike, or a buffer/open file
    # file_format="stl",  # optional if filename is a path; inferred from extension
    # see meshio-convert -h for all possible formats
)
# mesh.points, mesh.cells, mesh.cells_dict, ...

# mesh.vtk.read() is also possible

to read a mesh. To write, do

import meshio

# two triangles and one quad
points = [
    [0.0, 0.0],
    [1.0, 0.0],
    [0.0, 1.0],
    [1.0, 1.0],
    [2.0, 0.0],
    [2.0, 1.0],
]
cells = [
    ("triangle", [[0, 1, 2], [1, 3, 2]]),
    ("quad", [[1, 4, 5, 3]]),
]

mesh = meshio.Mesh(
    points,
    cells,
    # Optionally provide extra data on points, cells, etc.
    point_data={"T": [0.3, -1.2, 0.5, 0.7, 0.0, -3.0]},
    # Each item in cell data must match the cells array
    cell_data={"a": [[0.1, 0.2], [0.4]]},
)
mesh.write(
    "foo.vtk",  # str, os.PathLike, or buffer/open file
    # file_format="vtk",  # optional if first argument is a path; inferred from extension
)

# Alternative with the same options
meshio.write_points_cells("foo.vtk", points, cells)

For both input and output, you can optionally specify the exact file_format (in case you would like to enforce ASCII over binary VTK, for example).

Time series

The XDMF format supports time series with a shared mesh. You can write times series data using meshio with

with meshio.xdmf.TimeSeriesWriter(filename) as writer:
    writer.write_points_cells(points, cells)
    for t in [0.0, 0.1, 0.21]:
        writer.write_data(t, point_data={"phi": data})

and read it with

with meshio.xdmf.TimeSeriesReader(filename) as reader:
    points, cells = reader.read_points_cells()
    for k in range(reader.num_steps):
        t, point_data, cell_data = reader.read_data(k)

ParaView plugin

gmsh paraview

*A Gmsh file opened with ParaView.*

If you have downloaded a binary version of ParaView, you may proceed as follows.

  • Install meshio for the Python major version that ParaView uses (check pvpython --version)
  • Open ParaView
  • Find the file paraview-meshio-plugin.py of your meshio installation (on Linux: ~/.local/share/paraview-5.9/plugins/) and load it under Tools / Manage Plugins / Load New
  • Optional: Activate Auto Load

You can now open all meshio-supported files in ParaView.

Performance comparison

The comparisons here are for a triangular mesh with about 900k points and 1.8M triangles. The red lines mark the size of the mesh in memory.

File sizes

file size

I/O speed

performance

Maximum memory usage

memory usage

Installation

meshio is available from the Python Package Index, so simply run

pip install meshio

to install.

Additional dependencies (netcdf4, h5py) are required for some of the output formats and can be pulled in by

pip install meshio[all]

You can also install meshio from Anaconda:

conda install -c conda-forge meshio

Testing

To run the meshio unit tests, check out this repository and type

tox

License

meshio is published under the MIT license.

meshio's People

Contributors

blacheref avatar blechta avatar bwoodsend avatar cbcoutinho avatar christost avatar clbarnes avatar clemoreau avatar dalcinl avatar dbeurle avatar ffilotto avatar gdmcbain avatar iitrabhi avatar jesusbill avatar keileg avatar keurfonluu avatar krande avatar leoschwarz avatar loumalouomega avatar mhochsteger avatar michejansen avatar moiserousseau avatar mrambausek avatar muellerseb avatar nilswagner avatar nschloe avatar pre-commit-ci[bot] avatar s1291 avatar spectre5 avatar tianyikillua avatar vlukes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

meshio's Issues

remove large python loops from i/o

The loops in the PERMAS (and perhaps other) read/write routines are all in Python, e.g., here. This makes the I/O really slow. A better approach would be to reduce this to just one write(date) command with an appropriate numpy.array as data.

MED conversion inverts cells

It seems that meshio inverts cells when converting from/to MED.

Here I attach an example. I first converted .msh to .med using meshio, then converted this by gmsh to .msh, and finally converted this back to .med by meshio.

The problem is obvious even from comparing the two gmsh-ascii files doublet-tet.1.msh and doublet-tet.3-gmsh-med-meshio-msh.msh.

setup.py fails to run with Python 3 and a non-unicode locale

The Debian builders do not have a unicode locale set, and therefore your setup.py fails to run with the system python3 interpreter:

>>> python3 setup.py clean
running clean
>>> LC_ALL=C python3 setup.py clean
Traceback (most recent call last):
  File "setup.py", line 11, in <module>
    exec(f.read(), about)
  File "/usr/lib/python3.5/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 73: ordinal not in range(128)

It works fine with Python 2.

ANSYS Fluent .msh files

Hello,

Does the meshio support ANSYS Fluent .msh files? The documentation says that it does, but it looks like it only supports .gmsh files by the in the _init.py.

Thank You,

Vitaly

error when using voropy with python 3.5

hi!

when using this command: mesh, point_data, field_data = voropy.reader.read(filename)
i'm getting this error

mesh, point_data, field_data = voropy.reader.read(filename)

  File "C:\Users\imène\Anaconda3\lib\site-packages\voropy\reader.py", line 64, in read
    if len(cells_nodes[0]) == 3:

KeyError: 0`

type of file is VTU and version of python is 3.5

can u help me please ?

best regards

Vtk file can't be read if there is only mesh points (no associate data)

I have a structured mesh as a vtk file that contains only mesh points and there is no associated data in that file besides the nodes. Using meshio.read('mesh.vtk') gives the following error that ties to line 123 in vtk_io.py because it seeks GetData() in a file that does not have any:

# Explicitly extract points, cells, point data, field data
    points = numpy.copy(numpy_support.vtk_to_numpy(
            vtk_mesh.GetPoints().GetData()
            ))

Any suggestion? Could you include the capability to read just the mesh file? I was interested in converting vtk mesh to gmsh...so, if you think there will be further problems during conversion because of missing associated data then I would be interested to know. Thanks.

vtk io with python3

When loading a vtk file using python3, there is an iteritems() call in vtk_io.py

points, cells, point_data, cell_data, field_data = meshio.read('output_95.vtu')
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.5/site-packages/meshio/init.py", line 83, in read
return vtk_io.read('vtu', filename)
File "/usr/local/lib/python3.5/site-packages/meshio/vtk_io.py", line 96, in read
)
File "/usr/local/lib/python3.5/site-packages/meshio/vtk_io.py", line 54, in _read_cells
#for vtk_type, meshio_type in vtk_to_meshio_type.iteritems():
AttributeError: 'dict' object has no attribute 'iteritems'

Maybe we could use the future library to make the code compatible with python2 and python3?

Manpage for meshio-convert

@nschloe, I wrote a manpage for meshio-convert for the meshio-tools Debian package. I'd be happy to forward it upstream if you are interested. It is written in Markdown and converted using pandoc.

Please let me know.

pip install in jupyter

I'm getting the following problem with python3 and pip install on jupyter (specifically on notebooks.azure.com):

!pip install meshio

Collecting meshio
  Using cached meshio-1.7.4.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-build-564tyt8x/meshio/setup.py", line 11, in <module>
        exec(f.read(), about)
      File "/home/nbcommon/anaconda3_410/lib/python3.5/encodings/ascii.py", line 26, in decode
        return codecs.ascii_decode(input, self.errors)[0]
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 71: ordinal not in range(128)
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-564tyt8x/meshio/

Clearly some kind of UTF-8 issue?

only support for contiguous arrays

I have created my own build recipe using anaconda, I am using vtk 8.0 python bindings on latest tagged version 1.8.10

When I convert this mesh from gmsh format to vtu I get contiguous array error.

meshio-convert  DSI020CALb.msh DSI020CALb.vtu
Number of points: 2116098
Elements:
  Number of triangles: 1410720
  Number of tetras: 11285760
Cell data: geometrical, physical
Traceback (most recent call last):
  File "/home/ksansom/anaconda3/envs/meshio/bin/meshio-convert", line 4, in <module>
    __import__('pkg_resources').run_script('meshio==1.8.10', 'meshio-convert')
  File "/home/ksansom/anaconda3/envs/meshio/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg/pkg_resources/__init__.py", line 744, in run_script
  File "/home/ksansom/anaconda3/envs/meshio/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg/pkg_resources/__init__.py", line 1506, in run_script
  File "/home/ksansom/anaconda3/envs/meshio/lib/python3.6/site-packages/meshio-1.8.10-py3.6.egg/EGG-INFO/scripts/meshio-convert", line 130, in <module>
  File "/home/ksansom/anaconda3/envs/meshio/lib/python3.6/site-packages/meshio-1.8.10-py3.6.egg/EGG-INFO/scripts/meshio-convert", line 70, in _main
  File "/home/ksansom/anaconda3/envs/meshio/lib/python3.6/site-packages/meshio-1.8.10-py3.6.egg/meshio/helpers.py", line 187, in write
  File "/home/ksansom/anaconda3/envs/meshio/lib/python3.6/site-packages/meshio-1.8.10-py3.6.egg/meshio/vtk_io.py", line 236, in write
  File "/home/ksansom/anaconda3/envs/meshio/lib/python3.6/site-packages/meshio-1.8.10-py3.6.egg/meshio/vtk_io.py", line 335, in _generate_vtk_mesh
  File "/home/ksansom/anaconda3/envs/meshio/lib/python3.6/site-packages/vtk/util/numpy_support.py", line 132, in numpy_to_vtk
    assert z.flags.contiguous, 'Only contiguous arrays are supported.'
AssertionError: Only contiguous arrays are supported.

same kind of error when is use this command

./meshio-convert.py --input-format gmsh-binary --output-format vtu-binary DSI020CALb.msh DSI020CALb.vtu
Number of points: 2116098
Elements:
  Number of triangles: 1410720
  Number of tetras: 11285760
Cell data: geometrical, physical
Traceback (most recent call last):
  File "./meshio-convert.py", line 130, in <module>
    _main()
  File "./meshio-convert.py", line 70, in _main
    field_data=field_data
  File "/home/ksansom/anaconda3/envs/meshio/lib/python3.6/site-packages/meshio-1.8.10-py3.6.egg/meshio/helpers.py", line 187, in write
  File "/home/ksansom/anaconda3/envs/meshio/lib/python3.6/site-packages/meshio-1.8.10-py3.6.egg/meshio/vtk_io.py", line 236, in write
  File "/home/ksansom/anaconda3/envs/meshio/lib/python3.6/site-packages/meshio-1.8.10-py3.6.egg/meshio/vtk_io.py", line 335, in _generate_vtk_mesh
  File "/home/ksansom/anaconda3/envs/meshio/lib/python3.6/site-packages/vtk/util/numpy_support.py", line 132, in numpy_to_vtk
    assert z.flags.contiguous, 'Only contiguous arrays are supported.
AssertionError: Only contiguous arrays are supported.

Does not read geometrical entity numbers...

Hi,

It seems there is a typo which makes meshio to read physical entity number only....
At line #245 in gmsh_io.py

   output_cell_data[key]['geometrical'] = cell_data[key][:, 0]

should be

   output_cell_data[key]['geometrical'] = cell_data[key][:, 1]

Thank you.

VTK reader - AttributeError: 'NoneType' object has no attribute 'IsA'

annulus-20.exo.zip
We currently need an easy way to convert exodus files to the MED (Salome) format. One possibility we have come across is to convert exodus to gmsh using your tool meshio and then convert gmsh to med using GMSH. But we have been not successful yet with the meshio part, see the error below. I attach the exodus file (distributed with PETSc); it's nevertheless the same with any other exodus file I tried. It's also the case for both Python 2.7.13 and 3.6.2.

Do you have any idea, what's the issue?

Thank you for this nice contribution to the community!

$ meshio-convert --input-format exodus --output-format gmsh-ascii annulus-20.exo bla.msh
Traceback (most recent call last):
  File "/opt/miniconda3/envs/p2/bin/meshio-convert", line 133, in <module>
    _main()
  File "/opt/miniconda3/envs/p2/bin/meshio-convert", line 16, in _main
    meshio.read(args.infile, file_format=args.input_format)
  File "/opt/miniconda3/envs/p2/lib/python2.7/site-packages/meshio/helpers.py", line 114, in read
    out = vtk_io.read('exodus', filename)
  File "/opt/miniconda3/envs/p2/lib/python2.7/site-packages/meshio/vtk_io.py", line 119, in read
    vtk_mesh = _read_exodusii_mesh(reader)
  File "/opt/miniconda3/envs/p2/lib/python2.7/site-packages/meshio/vtk_io.py", line 204, in _read_exodusii_mesh
    if sub_block.IsA('vtkUnstructuredGrid'):
AttributeError: 'NoneType' object has no attribute 'IsA'

treat gmsh's physical groups

The Gmsh data format offers several of data fields, e.g., ElementData, NodeData etc. On top of that, there a "Physical Groups", effectively tagging entities. It's not quite clear yet where this information fits in with other mesh formats.

Reading tags

Is there any way currently to read in tags from the mesh file? for example: when reading the following triangle from a .msh file

13137 2 2 0 30 9328 13513 16999

I need to be able to read the second tag, ie the fact it belongs to object 30
Can't find a way to do this using meshio, so i'm having to read the file twice to get this information.
Cheers.

AssertionError: More than one / no 'vtkUnstructuredGrid' found!

Some Exodus files fail to read with the following traceback:

Traceback (most recent call last):
  File "/opt/miniconda3/envs/meshio/bin/meshio-convert", line 133, in <module>
    _main()
  File "/opt/miniconda3/envs/meshio/bin/meshio-convert", line 16, in _main
    meshio.read(args.infile, file_format=args.input_format)
  File "/opt/miniconda3/envs/meshio/lib/python3.6/site-packages/meshio/helpers.py", line 114, in read
    out = vtk_io.read('exodus', filename)
  File "/opt/miniconda3/envs/meshio/lib/python3.6/site-packages/meshio/vtk_io.py", line 119, in read
    vtk_mesh = _read_exodusii_mesh(reader)
  File "/opt/miniconda3/envs/meshio/lib/python3.6/site-packages/meshio/vtk_io.py", line 208, in _read_exodusii_mesh
    assert len(vtk_mesh) == 1, 'More than one \'vtkUnstructuredGrid\' found!'
AssertionError: More than one 'vtkUnstructuredGrid' found!

(sample file to reproduce: sevenside.exo.zip)

or

Traceback (most recent call last):
  File "/opt/miniconda3/envs/meshio/bin/meshio-convert", line 133, in <module>
    _main()
  File "/opt/miniconda3/envs/meshio/bin/meshio-convert", line 16, in _main
    meshio.read(args.infile, file_format=args.input_format)
  File "/opt/miniconda3/envs/meshio/lib/python3.6/site-packages/meshio/helpers.py", line 114, in read
    out = vtk_io.read('exodus', filename)
  File "/opt/miniconda3/envs/meshio/lib/python3.6/site-packages/meshio/vtk_io.py", line 119, in read
    vtk_mesh = _read_exodusii_mesh(reader)
  File "/opt/miniconda3/envs/meshio/lib/python3.6/site-packages/meshio/vtk_io.py", line 207, in _read_exodusii_mesh
    assert vtk_mesh, 'No \'vtkUnstructuredGrid\' found!'
AssertionError: No 'vtkUnstructuredGrid' found!

(sample file to reproduce: hex_2_2_2.exo.zip)

meshio-convert gmsh to dolfin-xml missing files

The files *_facet_region.xml and *_obstacle_physical_region.xml are not created by meshio-convert. This means, that the information about the boundaries is lost during the conversion, right?

converting xml to msh

Hello, I'm tying to convert the attached file with the command

meshio-convert mesh.xml mesh.msh
mesh.xml.zip

and I get the following error

File "src/lxml/lxml.etree.pyx", line 2452, in lxml.etree._Attrib.__getitem__ (src/lxml/lxml.etree.c:68665)
KeyError: 'z'

Add lxml to install_requires in setup.py

If converting from gmsh to dolfin-xml works without a running installation of dolfin, the required package lxml should be added to install_requires in setup.py.

AssertionError

Hi,
get the below error message on ubuntu 14.04. I get that kind of error message as well on python console. However the file is https://www.dropbox.com/sh/i259x4vmf7e8fjt/AADFpEbb7RWPwqUtraw5exeVa (in 'angle' directory)
readable by paraview. It is binary.

thats how I installed:
1637 apt-get install python-h5py
1638 pip install meshio
1639 apt-get install python-pyvtk
1640 apt-get install python-numpy
1641 apt-get install python-vtk
1642 history

Test:

meshio-convert /tmp/elmermesh/angle/case0001.vtu input.msh
Traceback (most recent call last):
  File "/usr/local/bin/meshio-convert", line 64, in <module>
    _main()
  File "/usr/local/bin/meshio-convert", line 16, in _main
    meshio.read(args.infile, timestep=args.timesteps)
  File "/usr/local/lib/python2.7/dist-packages/meshio/__init__.py", line 40, in read
    return vtk_io.read('vtu', filename)
  File "/usr/local/lib/python2.7/dist-packages/meshio/vtk_io.py", line 35, in read
    cells_nodes = _read_cells_nodes(vtk_mesh)
  File "/usr/local/lib/python2.7/dist-packages/meshio/vtk_io.py", line 129, in _read_cells_nodes
    assert all(array[::num_nodes_per_cell+1] == num_nodes_per_cell)
AssertionError

Btw:
"Requirements

MeshIO depends on

h5py,
NumPy, and
VTK."
VTK link points to a false url...

Maybe you can help. Tia

mix of `readline` and `next` causes `ValueError`

when using the gmsh reader I encounter many errors of this type

ValueError: Mixing iteration and read methods would lose data

See this gist for a test that fails on my configuration

https://gist.github.com/capitalaslash/cf6d5d14735e79b4e501b28a6696cf4f

the key factor is the use of physical tags in the mesh.
the error is caused by the use of islice, e.g. gmsh_io.py:111

for k, line in enumerate(islice(f, num_phys_names)):
    line = line.decode('utf-8')

since islice is implemented with next, see

https://docs.python.org/2/library/itertools.html#itertools.islice

(at least in python2).
a possible solution is to replace those loops with something like

for k in range(num_phys_names):
    line = f.readline().decode('utf-8')

that ensures a uniform use of readline everywhere.
I don't know if this would affect performance significantly.
would you accept a patch along this way?

Memory Error

Hello,

I get a "Memory Error" when converting an ansys .msh to a vtu-binary.

In fact, it's a 3d volume mesh, but the code output says: "number of triangles."

Is it possible that it's recognizing the wrong kind of mesh?

Thank You,

Vitaly

VTK dependency

(py)VTK is a pretty heavy-weight dependency, and at least for Ubuntu the package is Python 2 only.

Would it be possible to avoid the VTK dependency and still convert to VTK/XDMF? It would enhance portability.

Use XDMF output with ASCII only

I just tried to output to xdmf, but got:

HDF5-DIAG: Error detected in HDF5 (1.8.16) thread 140140583401216:
  #000: ../../../src/H5T.c line 1671 in H5Tcopy(): not a datatype or dataset
    major: Invalid arguments to routine
    minor: Inappropriate type
XDMF Error in /build/vtk6-YpT4yb/vtk6-6.2.0+dfsg1/ThirdParty/xdmf2/vtkxdmf2/libsrc/XdmfDataDesc.cxx line 492 (Error Creating Data Type)
HDF5-DIAG: Error detected in HDF5 (1.8.16) thread 140140583401216:
  #000: ../../../src/H5T.c line 1671 in H5Tcopy(): not a datatype or dataset
    major: Invalid arguments to routine

and a lot more error messages - is it possible to switch to ASCII only XDMF (i.e. pure XML). Although it may be less efficient in storage, it is less likely to cause a crash from conflicting library versions etc.

Gmsh: Support for PhysicalNames section

I would like to add support for writing $PhysicalNames sections:

$PhysicalNames
number-of-names
physical-dimension physical-number "physical-name"
…
$EndPhysicalNames

However, the read() function returns field_data missing the physical-dimension information. I'm not sure what to do here regarding backward compatibility. Right now field_data is a dict {"physical-name": physical-number, ...}.

  • We can either make a backward incompatible change to output {"physical-name": (physical-dimension, physical-number), ...}.

  • Or append some sort or private-looking special key, let say __gmsh@physical_dimension mapping to a dict {"physical-name": physical-dimension, ...}.

Just take a decision according to your preference, and I'll contribute the PR.

PS: My use case is high order meshes. I generate high order meshes with Gmsh, then I use meshio to read it, extract the low-order mesh (i.e just the topology or skeleton of the mesh) and write it back to disk alongside with the high-order coordinates of each cell. Then I can feed a high-order DG-like code with these two pieces of information. However, right now I have no easy way to write back the $PhysicalNames section, as the read() function does not return the full info.

Support for higher-order elements

Any plans to support higher-order elements? I tried converting a 10-node tetrahedron from gmsh to XDMF, and got the error

vtk_type = meshio_to_vtk_type[meshio_type]
KeyError: 'tetra10'

Fluent .msh: support binary, and elements -> faces -> nodes mapping

Unfortunately I can't share the whole Fluent .msh file, but I can share the beginning at least:

(0 machine configuration:)
(4 (60 0 0 1 2 4 4 4 8 4 8))
(0 grid written by ANSYS Meshing
   nodes:       (10 (id start end type) (x y z ...))
              faces:       (13 (id start end type elemType)
                (v-0 v-1 .. v-n right-cell left-cell ...))
   cells:       (12 (id start end type elemtype))
   parent-face: (59 (start end parent child) (nchilds child0 child1 ...))
)
(2 3)
(10 (0 1 5406dd 0))
(13 (0 1 2202aa7 0))
(12 (0 1 ece71a 0))
(3010 (3 1 430529 1 3)
(<9b>ĎDŘHřČ?5QÁ^K.SÚżBÉEÔe<82>Ĺżü VęGŢČ?Qn^Yum_Úż6Ś<8f>^]Ţ<9c>Ĺż\<81>Ú˘b]É?^[D<88>x.^^ÚżÜç<80>T<8a>^DĹż^_Oţă/$É?x=ůŐ<9a>3ÚżšłßB^DKĹżtň#rÎ^RÉ?

There are two issues:

  • it uses a binary format (that's easy to fix, one just has to read it properly and convert)
  • it doesn't save elements -> nodes mapping, but rather elements -> faces -> nodes mapping, which makes it actually quite technical to convert into the usual elements -> nodes mapping, so that one can convert to other formats.

Discarding unused cell types crashes with Python 3

When converting a 3D mesh to dolfin XML format, the following traceback appears.

Cell data: physical, geometrical
Traceback (most recent call last):
  File "/usr/local/bin/meshio-convert", line 130, in <module>
    _main()
  File "/usr/local/bin/meshio-convert", line 70, in _main
    field_data=field_data
  File "/usr/local/lib/python3.5/dist-packages/meshio/helpers.py", line 170, in write
    dolfin_io.write(filename, points, cells, cell_data=cell_data)
  File "/usr/local/lib/python3.5/dist-packages/meshio/dolfin_io.py", line 245, in write
    _write_mesh(filename, points, cell_type, cells)
  File "/usr/local/lib/python3.5/dist-packages/meshio/dolfin_io.py", line 136, in _write_mesh
    discarded_cells.remove(cell_type)
AttributeError: 'dict_keys' object has no attribute 'remove'

With the Python 2 version, it works fine. The crash happens when the message WARNING:root:DOLFIN XML can only handle one cell type at a time. Using tetra, discarding triangle. should be printed instead.

Exodus converted to empty mesh

Conversion of the attached file results into an empty mesh.

It seems this holds for arbitrary output format.
meshio-convert --output-format vtk-ascii blockcylinder-50.exo blockcylinder-50.vtk
even fails with
ValueError: need at least one array to concatenate

Testing gmsh using tempfiles

Hi Nico,

I'm writing some gmsh tests for a model I'm developing, and I thought you may be interested as well (w.r.t meshio anyway). I want to have some example models built into the testing, and instead of carrying around .msh files, I'm using tempfile.NameTemplateFiles They work slightly better than text buffers, and are deleted after use automatically. This is a nice alternative to having .msh files lying around for me since I'm constantly writing over them - I thought you might be interested. This works on both py2 and py3 on my machine

Example:

import meshio
import tempfile

gmsh_buffer = '''\
$MeshFormat
2.2 0 8
$EndMeshFormat
$PhysicalNames
5
1 1 "lower"
1 2 "right"
1 3 "upper"
1 4 "left"
2 5 "domain"
$EndPhysicalNames
$Nodes
9
1 0 0 0
2 1 0 0
3 1 1 0
4 0 1 0
5 0.499999999998694 0 0
6 1 0.499999999998694 0
7 0.5000000000020591 1 0
8 0 0.5000000000020591 0
9 0.5000000000003766 0.5000000000003766 0
$EndNodes
$Elements
12
1 1 2 1 1 1 5
2 1 2 1 1 5 2
3 1 2 2 2 2 6
4 1 2 2 2 6 3
5 1 2 3 3 3 7
6 1 2 3 3 7 4
7 1 2 4 4 4 8
8 1 2 4 4 8 1
9 3 2 5 1 1 5 9 8
10 3 2 5 1 8 9 7 4
11 3 2 5 1 5 2 6 9
12 3 2 5 1 9 6 3 7
$EndElements
'''

# Remember to remove indentation if the multi-line string is in a function
import textwrap
gmsh_buffer = textwrap.dedent(gmsh_buffer)


with tempfile.NamedTemporaryFile(suffix='.msh') as temp:
    temp.write(gmsh_buffer.encode('utf-8'))
    print(temp.name)
    temp.flush()
    points, cells, point_data, cell_data, field_data = meshio.read(temp.name)

(do all the things)

Cheers,
Chris

More mesh formats

Implement more mesh formats, e.g.,

  • Dolfin's XML,
  • METIS (.gra)
  • Scotch (.grf),
  • diffpack (.grid),
  • Abaqus (.inp)

AttributeError: SetInput

meshio-convert screw.msh screw.vtu
Traceback (most recent call last):
File "/home/nils/local/bin/meshio-convert", line 64, in
_main()
File "/home/nils/local/bin/meshio-convert", line 25, in _main
field_data=field_data
File "/home/nils/local/lib/python2.7/site-packages/meshio/init.py", line 84, in write
field_data=field_data
File "/home/nils/local/lib/python2.7/site-packages/meshio/vtk_io.py", line 204, in write
writer.SetInput(vtk_mesh)
AttributeError: SetInput

Gmsh: only mesh, no data?

Although you have point_data and cell_data information, seems like you didn't really take care of that in your code (Mostly I mean Gmsh format). If you don't have plans to add this feature. I will do this myself. Basicly I want to transform between our own FEM format/.msh format/abaqus .inp format/VTK format.

One problem is that VTK can't handle element node data as is in the .msh format. My work-around is to add repeated points on shared nodes. But visualization tools such as Paraview can't handle it very well. Those filters tends to merge coincident points and average those points. Actually I found your project when searching for better solutions. Any suggestions would be appreciated.

Converting a structured mesh with nodes and cells to vtk?

Hi Nico,

May I know how to convert a structured mesh with an array of cells and nodes to a vtk file?
Let's say I have a mesh with 30x30x20 cells, so the arrays of cells and nodes are given as follows:

np.size(cellsarray) = (18000, 3)
np.size(nodesarray) = (20181, 3)

Thanks.

Exodus reading error: KeyError: 'coordz'

I get the following error with the attached simple 2D Exodus mesh:

Traceback (most recent call last):
  File "/opt/miniconda3/envs/meshio/bin/meshio-convert", line 133, in <module>
    _main()
  File "/opt/miniconda3/envs/meshio/bin/meshio-convert", line 16, in _main
    meshio.read(args.infile, file_format=args.input_format)
  File "/opt/miniconda3/envs/meshio/lib/python3.6/site-packages/meshio/helpers.py", line 121, in read
    out = exodus_io.read(filename)
  File "/opt/miniconda3/envs/meshio/lib/python3.6/site-packages/meshio/exodus_io.py", line 72, in read
    nc.variables['coordz'][:],
KeyError: 'coordz'

GMSH: Add quad16 and hexahedron64

The patch is trivial, if you prefer a PR, let me know and I'll submit one with the diff below.

diff --git a/meshio/gmsh_io.py b/meshio/gmsh_io.py
index fc53da2..36aa8a8 100644
--- a/meshio/gmsh_io.py
+++ b/meshio/gmsh_io.py
@@ -30,6 +30,7 @@ num_nodes_per_cell = {
     'pyramid14': 14,
     'line4': 4,
     'quad16': 16,
+    'hexahedron64': 64,
     }
 
 # Translate meshio types to gmsh codes
@@ -52,6 +53,7 @@ _gmsh_to_meshio_type = {
         14: 'pyramid14',
         26: 'line4',
         36: 'quad16',
+        92: 'hexahedron64',
         }
 _meshio_to_gmsh_type = {v: k for k, v in _gmsh_to_meshio_type.items()}

converting .msh to .vtu gives error

Converting .msh to .vtu gives an abort. it looks like it was able to read the mesh but not write it to file?

meshio-convert mesh.msh mesh.vtu
Number of points: 540930
Elements:
Number of triangles: 406420
Number of tetras: 2058564
Cell data: physical, geometrical
Fatal Python error: PyThreadState_Get: no current thread

Abort trap: 6

Segmentation fault when converting GMSH to XDMF

Hi,
I just tried your tool for converting a simple gmsh mesh to the XDMF format (for fenics). I get a segfault. Converting to, for example, VTK works though.
Any ideas?

Can I expect the converter to handle gmsh's physical groups?

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.