writeMesh is a MATLAB project for writing MATLAB finite element mesh to inp, bdf, and msh files. writeMesh is a part of Im2mesh package. Tested in MATLAB R2021b.
writeMesh supports mesh with single or multiple phases. Note that phase is also known as part, domain, or physical surface. In finite element modeling of composite materials, each phase in the mesh represents a distinct material component.
writeMesh woks for 2D mesh (triangular and quadrilateral); also works for 3D mesh (tetrahedral and hexahedral).
inpfile (Abaqus)bdffile (Nastran bulk data, compatible with COMSOL)mshfile (Gmsh mesh file format)- For other formats, you can import the generated
mshfile into Gmsh and then export.
9 examples are provided. Examples are live script mlx files (demo01.mlx ~ demo09.mlx). If you find some text in the mlx file is missing, read the html file instead. Examples are also available as html files in the folder "demo_html".
Examples:
- demo01 - Triangular mesh with single phase
- demo02 - Triangular mesh with multiple phases
- demo03 - Quadrilateral mesh
- demo04 - 2D Mesh with quadratic elements
- demo05 - Node sets at the boundary and interface when writing
inpfiles - demo06 - Select formats when writing
inpfiles - demo07 - Tetrahedral mesh
- demo08 - Hexahedral mesh
- demo09 - Set
tolerancewhen searching boundary node set - inp_example1 - Format #1 of
inpfile - inp_example2 - Format #2 of
inpfile - inp_example3 - Format #3 of
inpfile
Functions:
printInp2d( vert, ele, tnum, ele_type, precision, file_name, opt );
printBdf2d( vert, ele, tnum, ele_type, precision, file_name );
printMsh( vert, ele, tnum, conn, precision, file_name );In the above functions, the 1st and 2nd input arguments are required. The other input arguments are optional. The typical usage of function printInp2d is shown below.
printInp2d( vert, ele );
printInp2d( vert, ele, [], [], [], file_name );
printInp2d( vert, ele, [], [], precision, file_name );
printInp2d( vert, ele, [], [], precision, file_name, opt );
printInp2d( vert, ele, tnum, [], precision, file_name );
printInp2d( vert, ele, tnum, ele_type, precision, file_name, opt );The usage of other functions is similar.
Input arguments:
vert: Mesh nodes. It’s a Nn-by-2 matrix, where Nn is the number of nodes in the mesh. Each row ofvertcontains the x, y coordinates for that mesh node.ele: Mesh elements. For linear triangular mesh, it s a Ne-by-3 matrix. For quadratic triangular mesh, it s a Ne-by-6 matrix. For linear quadrilateral mesh, it s a Ne-by-4 matrix. For quadratic quadrilateral mesh, it s a Ne-by-8 matrix. Ne is the number of elements in the mesh. Each row inelecontains the indices of the nodes for that mesh element.tnum: Label of phase, which corresponds to physical surface tag in Gmsh.tnumis a Ne-by-1 array, where Ne is the number of elements. If your mesh has multiple part or multiple phase, you can usetnumfor labelling.tnum(j,1) = k;means the j-th element belongs to the k-th phase. If you mesh has single phase, you can settnumas an empty array.conn: C-by-2 array of constraining edges, where each row defines an edge. You can setconnas an empty array.ele_type: element type. When omitted,ele_typwill be determined based on the size of variableele.precision: number of digits to the right of the decimal point when writing node coordinates. When omitted,precision=8;file_name: file name of exported file, such as'aaa.inp','D:\aaa.inp'. When omitted,file_name='test.suffix';opt- a structure array. It is the extra options forprintInp2d. It stores extra parameter settings forprintInp2d. See demo05, demo06, and comments inprintInp2d.m.
Notes:
-
Function
printInp2dwill automatically search and export node sets at the boundary and interface. We can also define customized node set (see demo05). -
Function
printInp2dworks for triangular and quadrilateral mesh; works for linear and quadratic element. -
Function
printBdf2dworks for linear triangular and quadrilateral element; does not work for quadratic element. -
Function
printMshdoes not work for quadrilateral mesh. -
Function
printMshcan be modified to support quadratic triangular element. I'm not sure whether this is necessary. If you prefermshfile with quadratic triangular element, feel free to let me know. -
I have tested the
mshfile generated by functionprintMshin Gmsh 4.13.1. I didn't see any errors. Themshfile generated by functionprintMshis MSH file format version 4.1. Please refer to the Gmsh manual about details.
Functions:
printInp3d( vert, ele, tnum, ele_type, precision, file_name, opt );
printBdf3d( vert, ele, tnum, ele_type, precision, file_name );In the above functions, the 1st and 2nd input arguments are required. The other input arguments are optional. The input arguments and usage of these functions are similar to their 2D counterparts.
Input arguments:
vert: Mesh nodes. It’s a Nn-by-3 matrix, where Nn is the number of nodes in the mesh. Each row ofvertcontains the x, y, z coordinates for that mesh node.ele: Mesh elements. For linear tetrahedral mesh, it s a Ne-by-4 matrix. For quadratic tetrahedral mesh, it s a Ne-by-10 matrix. For linear hexahedral mesh, it s a Ne-by-8 matrix. For quadratic hexahedral mesh, it s a Ne-by-20 matrix. Ne is the number of elements in the mesh. Each row inelecontains the indices of the nodes for that mesh element.tnum: Label of phase. It is a Ne-by-1 array. If you mesh has single phase, you can settnumas an empty array.
Notes:
- Function
printInp3dwill automatically search and export node sets at the boundary and interface. We can also define customized node set (see demo05). - Function
printInp3dworks for tetrahedral and hexahedral mesh; works for linear and quadratic element. - Function
printBdf3dworks for linear element; does not work for quadratic element. - Function
printMsh3dis under development.
Copyright (C) by Jiexian Ma.
Distributed under the terms of the GNU General Public License (version 3).
The development of writeMesh is nontrivial. If you use writeMesh for your project, please cite Im2mesh package as follows.
Ma, J., & Li, Y. (2025). Im2mesh: A MATLAB/Octave package for generating finite element mesh based on 2D multi-phase image (2.1.5). Zenodo. https://doi.org/10.5281/zenodo.14847059
Great thanks Dr. Yang Lu for providing valuable suggestions.