Skip to content

Commit 6608afc

Browse files
committed
Refine mesh generation and Gmsh file handling. Clarifying usage in visualization functions
1 parent d2f75f8 commit 6608afc

File tree

3 files changed

+16
-37
lines changed

3 files changed

+16
-37
lines changed

src/mesh/meshGenerationScript.js

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class meshGeneration {
2424
* @param {number} [config.numElementsY=1] - Number of elements along the y-axis (for 1D meshes)
2525
* @param {number} [config.maxY=0] - Maximum y-coordinate of the mesh (for 1D meshes)
2626
* @param {string} [config.meshDimension='2D'] - The dimension of the mesh, either 1D or 2D
27-
* @param {string|File} [config.meshFile=null] - Optional mesh file for predefined meshes (.json or .msh)
27+
* @param {string|File} [config.meshFile=null] - Optional mesh file for predefined meshes (.msh)
2828
* @param {string} [config.elementOrder='linear'] - The order of elements, either 'linear' or 'quadratic'
2929
*/
3030
constructor({
@@ -46,14 +46,20 @@ export class meshGeneration {
4646
}
4747

4848
/**
49-
* Function to generate the mesh based on the dimension or custom mesh file
49+
* Function to generate the mesh based on the dimension or parse a custom mesh file
5050
* @returns {object} The generated mesh containing node coordinates and total nodes
5151
*/
5252
generateMesh() {
5353
if (this.meshFile) {
54-
// If a custom mesh file is provided, read and parse it
55-
const meshData = this.generateMeshFromCustomFile(this.meshFile);
56-
return meshData;
54+
// If a mesh file is provided, check if it's a Gmsh (.msh) file and parse it
55+
if ((typeof this.meshFile === 'string' && this.meshFile.toLowerCase().endsWith('.msh')) ||
56+
(this.meshFile instanceof File && this.meshFile.name.toLowerCase().endsWith('.msh'))) {
57+
return this.generateMeshFromMshFile(this.meshFile);
58+
} else {
59+
const errorMessage = "Only .msh files are supported for mesh import";
60+
errorLog(errorMessage);
61+
throw new Error(errorMessage);
62+
}
5763
} else {
5864
// Validate required geometry parameters based on mesh dimension
5965
if (this.meshDimension === "1D") {
@@ -83,40 +89,13 @@ export class meshGeneration {
8389
}
8490

8591
/**
86-
* Function to parse a custom mesh JSON file and generate the mesh
87-
* @param {string} meshFilePath - Path to the custom mesh file (JSON format)
88-
* @returns {object} Mesh data containing coordinates and connectivity
89-
*/
90-
generateMeshFromCustomFile(meshFilePath) {
91-
const response = fetch(meshFilePath);
92-
const meshData = response.json();
93-
94-
const nodesXCoordinates = [];
95-
const nodesYCoordinates = [];
96-
const { nodes, elements } = meshData;
97-
98-
// Parse the node coordinates
99-
nodes.forEach((node) => {
100-
nodesXCoordinates.push(node.x);
101-
nodesYCoordinates.push(node.y);
102-
});
103-
104-
return {
105-
nodesXCoordinates,
106-
nodesYCoordinates,
107-
totalNodesX: nodesXCoordinates.length,
108-
totalNodesY: nodesYCoordinates.length,
109-
elements,
110-
};
111-
}
112-
113-
/**
114-
* Function to generate a structured mesh based on the msh file
92+
* Function to import a mesh from a Gmsh (.msh) file
93+
* @param {string|File} file - Path or File object to the mesh file (.msh format)
11594
* @returns {object} An object containing the coordinates of nodes,
11695
* total number of nodes, nodal numbering (NOP) array, and boundary elements
11796
*/
11897
async generateMeshFromMshFile(file) {
119-
//for now i have made a parsing of simple quadrilateral .msh file of version 4.1
98+
// Import mesh data from the Gmsh file
12099
const outputMesh = await importGmshQuadTri(file);
121100

122101
return outputMesh;

src/readers/gmshReaderScript.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
/**
1212
* Function to import mesh data from Gmsh format containing quadrilateral and triangular elements
13-
* @param {File} file - The Gmsh file to be parsed
13+
* @param {File} file - The Gmsh file to be parsed (.msh version 4.1)
1414
* @returns {object} The parsed mesh data including node coordinates, element connectivity, and boundary conditions
1515
*/
1616
const importGmshQuadTri = async (file) => {

src/visualization/plotSolutionScript.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function plotSolution(
2525
meshDimension,
2626
plotType,
2727
plotDivId,
28-
showMesh = false // Currently only for rectangular domains
28+
showMesh = false // Only applicable for rectangular domains when using generateMeshFromGeometry
2929
) {
3030
const { nodesXCoordinates, nodesYCoordinates } = nodesCoordinates;
3131

0 commit comments

Comments
 (0)