meshRW is a Python module that proposes basic readers and writers for msh (gmsh) and vtk/vtu/pvd (Paraview). It proposes:
- to read basic legacy
gmshmesh files (version: 2.2) - to write mesh files including time series fields to any version of
gmshmesh files and legacy (.vtk) and XML (.vtu, with.pvd) VTK mesh file compatible with Paraview.
Installation via pip install meshRW
Examples of usage could be found in tests files: test_msh.py and test_vtk.py.
meshRW can read msh files only. Notice that no field can be read.
-
For
mshformat (only Legacy version 2):-
Read the file
from meshRW import msh dataMesh = msh.mshReader(filename=<name of the file>, dim=<2 or 3>)Argument
dim(which is optional) could be switched to the value2in order to force to extract nodes coordinates in 2D (z-axis coordinate will be removed). -
Get coordinates of the nodes
nodes = dataMesh.getNodes() -
Get the list of tags number
tags = dataMesh.getTags() -
Get the list of types of elements
tags = dataMesh.getTypes() -
Get the list of elements
elements = dataMesh.getElements(type=<types of elements>, tag=<tags>, dictFormat=<True or False>)The
getElementsproperty
-
meshRW can write msh and vtk files. Basic static and time dependent nodal and elemental fields can be written aswell.
The common syntax for writers is the following
from meshRW import XXXX
XXXX.zzzzWriter(
filename = <name of the file>,
nodes = <array of nodes coordinates>,
title = <title of the file> (optional),
elements = [
{
'connectivity': <name of the file>,
'type': <type (string) of elements (TRI3, TET4...)>,
'physgrp': <list/array of physical groups>,
},...
],
fields = [
{
'data': <array of data>,
'type': <type of data ('nodal' or 'elemental')>,
'dim': <ndimension of data's array>,
'name': <name of the field>,
},
{
'data': <array of data>,
'type': <name of the file>,
'dim': <dimension of data's array>,
'name': <name of the field>,
'nbsteps': <number of steps (transient field for instance)>,
'steps': <list of steps (time steps for instance)>
},...
],
opts = {...} (dictionary of specific options)
)
-
For
mshformat (based on Legacy format, version 2.2 only with classmshand all meshes format provided bygmshusing classmsh2):- for classical legacy file (not use the gmsh's libAPI) could be accessed by choosing
XXXX = mshandzzzz=msh(msh.mshWriter(...)) - for any kind of legacy file (not use the gmsh's libAPI) could be accessed by choosing
XXXX = msh2andzzzz=msh(msh2.mshWriter(...))
- for classical legacy file (not use the gmsh's libAPI) could be accessed by choosing
NB for .msh files:
-
filenamemust contain.mshextension -
optscould be (formsh2only){'version': VV}(VVcould be equal to2, 2.2, 4, 4.1that corresponds togmshmesh files version -MshFileVersion) -
in the case of time series data, all the fields are given by default in the output file (to obtain one field per file, pass option
append = Trueto the writer). -
for
vtkformat (only non-binary legacy)- for classical legacy file (not use the VTK library) could be accessed by choosing
XXXX = vtkandzzzz=vtk(vtk.vtkWriter(...)) - for any kind of legacy file (not use the gmsh's libAPI) could be accessed by choosing
XXXX = vtk2andzzzz=vtk(vtk2.vtkWriter(...))
- for classical legacy file (not use the VTK library) could be accessed by choosing
NB for .vtk|.vtu|.pvd files:
filenamemust contain.vtk,.vtu(recommanded) extensionoptscould be (forvtk2only){'binary': True/False, 'ascii': True/False}(these options force data format in.vtufiles)- in the case of time series fields, the fields are written separated series files with the following format :
basename.NNN.vtu(NNN correspond the number of the step starting at 0). In this case, a Paraview.pvdfile is also generated to declare all the steps and associated mesh files with the time step values.
Developments are based on GMSH API documentation, GMSH tutorials, VTK documentation, Kitware blog posts, VTK discourse.
Copyright 2024 luc.laurent@lecnam.net
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.