Skip to content

Commit 1e341df

Browse files
committed
create a importing a simple gmsh file v4.1 for quadrilateral mesh file
1 parent 85fa913 commit 1e341df

2 files changed

Lines changed: 171 additions & 0 deletions

File tree

src/mesh/meshGenerationScript.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
/**
1212
* Class to handle the generation of structured finite element meshes
1313
*/
14+
15+
16+
import { importGmsh } from '../readers/gmshQuadReader';
17+
18+
1419
export class meshGeneration {
1520
/**
1621
* Constructor to initialize the meshGeneration class
@@ -84,6 +89,19 @@ export class meshGeneration {
8489
};
8590
}
8691

92+
/**
93+
* Generate a structured mesh based on the msh file
94+
* @returns {object} An object containing the coordinates of nodes,
95+
* total number of nodes, nodal numbering (NOP) array, and boundary elements
96+
*/
97+
async generateMeshFromMshFile(file){
98+
99+
//for now i have made a parsing of simple quadrilateral .msh file of version 4.1
100+
const outputMesh = await importGmsh(file)
101+
102+
return outputMesh
103+
}
104+
87105
/**
88106
* Generate a structured mesh based on the geometry configuration
89107
* @returns {object} An object containing the coordinates of nodes,

src/readers/gmshQuadReader.js

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
const importGmsh = async (file) => {
2+
let newFin = {
3+
nodesXCoordinates: [],
4+
nodesYCoordinates: [],
5+
nodalNumbering: [],
6+
boundaryElements: [],
7+
gmshV: 0,
8+
ascii: false,
9+
fltBytes: 8,
10+
totalNodesX:0,
11+
totalNodesY:0
12+
};
13+
let textre = await file.text();
14+
textre = textre
15+
.split("\n")
16+
.map((eachLine) => eachLine.trim())
17+
.filter((eachLine) => eachLine != "" && eachLine != " ");
18+
19+
let inNodesSections = false;
20+
21+
let inElementsSecitons = false;
22+
23+
let inMeshSections = false;
24+
25+
let lineNumber = 0;
26+
27+
for (let line of textre) {
28+
if (line == "$MeshFormat") {
29+
inMeshSections = true;
30+
continue;
31+
}
32+
if (line == "$EndMeshFormat") {
33+
lineNumber = 0;
34+
inMeshSections = false;
35+
continue;
36+
}
37+
if (line == "$Nodes") {
38+
inNodesSections = true;
39+
continue;
40+
}
41+
if (line == "$EndNodes") {
42+
lineNumber = 0;
43+
inNodesSections = false;
44+
continue;
45+
}
46+
if (line == "$Elements") {
47+
inElementsSecitons = true;
48+
continue;
49+
}
50+
if (line == "$EndElements") {
51+
lineNumber = 0;
52+
53+
inElementsSecitons = false;
54+
continue;
55+
}
56+
lineNumber = lineNumber + 1;
57+
if (inMeshSections) {
58+
let temp = line.split(" ");
59+
let gmshVersion = parseFloat(temp[0]);
60+
let asciiOr = temp[1] === "0" ? true : false;
61+
let fltBytesOr = temp[2];
62+
63+
newFin.gmshV = gmshVersion;
64+
newFin.ascii = asciiOr;
65+
newFin.fltBytes = fltBytesOr;
66+
}
67+
let temp = line.split(" ");
68+
if (inNodesSections) {
69+
if (temp.length === 3) {
70+
newFin.totalNodesX += 1;
71+
newFin.totalNodesY += 1;
72+
newFin.nodesXCoordinates = [
73+
...newFin.nodesXCoordinates,
74+
parseFloat(temp[0]),
75+
];
76+
newFin.nodesYCoordinates = [
77+
...newFin.nodesYCoordinates,
78+
parseFloat(temp[1]),
79+
];
80+
}
81+
}
82+
83+
if (inElementsSecitons) {
84+
if (temp.length === 5) {
85+
newFin.nodalNumbering = [
86+
...newFin.nodalNumbering,
87+
temp.slice(1).map((num) => parseInt(num, 10)),
88+
];
89+
}
90+
}
91+
}
92+
93+
94+
// const taichiRunner = new TaichiRunner();
95+
// await taichiRunner.init();
96+
97+
// await taichiRunner.addToScope({ newFin, tempBoundary });
98+
99+
// const useKernel = await taichiRunner.createKernel(() => {
100+
const edgeCount = {};
101+
for (let i = 0; i < newFin.nodalNumbering.length; i++) {
102+
const element = newFin.nodalNumbering[i];
103+
const edges = [
104+
[element[0], element[1]],
105+
[element[1], element[2]],
106+
[element[2], element[3]],
107+
[element[3], element[0]],
108+
];
109+
edges.forEach((edge) => {
110+
const key =
111+
edge[0] < edge[1]
112+
? `${edge[0]}-${edge[1]}`
113+
: `${edge[1]}-${edge[0]}`;
114+
edgeCount[key] = (edgeCount[key] || 0) + 1;
115+
});
116+
}
117+
118+
newFin.boundaryElements = [];
119+
for (let i = 0; i < newFin.nodalNumbering.length; i++) {
120+
const element = newFin.nodalNumbering[i];
121+
const edges = [
122+
[element[0], element[1]],
123+
[element[1], element[2]],
124+
[element[2], element[3]],
125+
[element[3], element[0]],
126+
];
127+
for (let j = 0; j < edges.length; j++) {
128+
const edge = edges[j];
129+
const key =
130+
edge[0] < edge[1]
131+
? `${edge[0]}-${edge[1]}`
132+
: `${edge[1]}-${edge[0]}`;
133+
if (edgeCount[key] === 1) {
134+
if (!newFin.boundaryElements[i]) {
135+
newFin.boundaryElements[i] = [];
136+
}
137+
newFin.boundaryElements[i].push([i, j]);
138+
}
139+
}
140+
}
141+
142+
newFin.boundaryElements = newFin.boundaryElements.filter((each) => each);
143+
144+
// });
145+
146+
// if (useKernel) {
147+
// await taichiRunner.runKernel(useKernel);
148+
// }
149+
150+
return newFin;
151+
};
152+
153+
export { importGmsh };

0 commit comments

Comments
 (0)