#Read a mesh from Gmsh
Load a .msh file (ASCII, format 2.2 or 4.1, first-order tetrahedra) with parseGmsh and use it like any other mesh. What cannot be used is an error that says why.
| Assumes | An ASCII .msh file with first-order tetrahedra; no binary, no hexahedra, prisms, pyramids or second-order elements. |
| Costs | Linear in the size of the file. |
| Uses | parseGmsh |
#The program
Save it as example.mjs next to a package.json with traceprojector installed, and run node example.mjs. It is the file examples/gmsh-mesh.js of the repository, which the test suite runs.
// A mesh from Gmsh: read the .msh file (ASCII, format 2.2 or 4.1, first-order
// tetrahedra) and use it like any other. parseGmsh returns the two arrays the
// operators take, keeps only the vertices the tetrahedra use, and counts the
// boundary triangles and lines Gmsh also writes.
import {readFileSync} from 'node:fs';
import {Locator, Mesh, StableProjector, parseGmsh} from 'traceprojector';
// In Gmsh: Mesh > 3D, then File > Export as a .msh file; choose ASCII.
const file = new URL('_cube.msh', import.meta.url);
const {vertices, tetrahedra, skipped} = parseGmsh(readFileSync(file, 'utf8'));
console.log('vertices', vertices.length, ' tetrahedra', tetrahedra.length);
console.log('skipped', JSON.stringify(skipped));
// The tetrahedra come out positively oriented, so Mesh accepts them.
const mesh = new Mesh(vertices, tetrahedra);
const projector = new StableProjector(vertices, tetrahedra);
const u = (p) => Math.sin(p[0] + p[1]) * Math.exp(p[2]);
const point = [0.3, 0.45, 0.2];
const {tIdx} = new Locator(mesh).findTetrahedron(point);
console.log('u at the point ', u(point).toFixed(4));
console.log('projection at the point', projector.project(u, point, tIdx, 0)
.toFixed(4));
// What cannot be used is an error that says why, never a different mesh.
for (const [what, text] of [
['a binary file', '$MeshFormat\n2.2 1 8\n$EndMeshFormat\n'],
['hexahedra', readFileSync(file, 'utf8').replace(/ 4 2 2 1 /, ' 5 2 2 1 ')],
]) {
try {
parseGmsh(text);
} catch (error) {
console.log(what.padEnd(14), error.constructor.name + ':',
error.message.split(';')[0]);
}
}
#What it prints
vertices 64 tetrahedra 162
skipped {"triangles":108}
u at the point 0.8326
projection at the point 0.8491
a binary file ValidateError: binary .msh files are not supported
hexahedra ValidateError: the file has hexahedra (element type 5)