1212 * Class to handle the generation of structured finite element meshes
1313 */
1414
15- import { importGmshQuadTri } from '../readers/gmshQuadReader.js' ;
15+ import { importGmshQuadTri } from "../readers/gmshQuadReader.js" ;
16+ import { errorLog } from "../utilities/loggingScript.js" ;
1617
1718export class meshGeneration {
1819 /**
1920 * Constructor to initialize the meshGeneration class
2021 * @param {object } config - Configuration object for the mesh
21- * @param {number } config.numElementsX - Number of elements along the x-axis
22- * @param {number } config.maxX - Maximum x-coordinate of the mesh
23- * @param {number } [config.numElementsY=1] - Number of elements along the y-axis (default is 1 for 1D meshes)
24- * @param {number } [config.maxY=0] - Maximum y-coordinate of the mesh (default is 0 for 1D meshes)
25- * @param {string } [config.meshDimension='2D'] - The dimension of the mesh, either 1D or 2D (default is 2D)
26- * @param {string } [config.meshFile=null] - Optional mesh file (JSON) for predefined meshes
27- * @param {string } [config.elementOrder='linear'] - The order of elements, either 'linear' or 'quadratic' (default is 'linear')
22+ * @param {number } [ config.numElementsX] - Number of elements along the x-axis (required for geometry-based mesh)
23+ * @param {number } [ config.maxX] - Maximum x-coordinate of the mesh (required for geometry-based mesh)
24+ * @param {number } [config.numElementsY=1] - Number of elements along the y-axis (for 1D meshes)
25+ * @param {number } [config.maxY=0] - Maximum y-coordinate of the mesh (for 1D meshes)
26+ * @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)
28+ * @param {string } [config.elementOrder='linear'] - The order of elements, either 'linear' or 'quadratic'
2829 */
2930 constructor ( {
30- numElementsX,
31- maxX,
32- numElementsY = 1 ,
33- maxY = 0 ,
34- meshDimension = "2D" ,
31+ numElementsX = null ,
32+ maxX = null ,
33+ numElementsY = null ,
34+ maxY = null ,
35+ meshDimension = null ,
3536 meshFile = null ,
3637 elementOrder = "linear" ,
3738 } ) {
@@ -54,6 +55,23 @@ export class meshGeneration {
5455 const meshData = this . generateMeshFromCustomFile ( this . meshFile ) ;
5556 return meshData ;
5657 } else {
58+ // Validate required geometry parameters based on mesh dimension
59+ if ( this . meshDimension === "1D" ) {
60+ if ( this . numElementsX === null || this . maxX === null ) {
61+ const errorMessage =
62+ "numElementsX and maxX are required parameters when generating a 1D mesh from geometry" ;
63+ errorLog ( errorMessage ) ;
64+ throw new Error ( errorMessage ) ;
65+ }
66+ } else if ( this . meshDimension === "2D" ) {
67+ if ( this . numElementsX === null || this . maxX === null || this . numElementsY === null || this . maxY === null ) {
68+ const errorMessage =
69+ "numElementsX, maxX, numElementsY, and maxY are required parameters when generating a 2D mesh from geometry" ;
70+ errorLog ( errorMessage ) ;
71+ throw new Error ( errorMessage ) ;
72+ }
73+ }
74+
5775 // Generate mesh based on dimension
5876 return this . generateMeshFromGeometry ( ) ;
5977 }
@@ -87,17 +105,16 @@ export class meshGeneration {
87105 } ;
88106 }
89107
90- /**
108+ /**
91109 * Generate a structured mesh based on the msh file
92110 * @returns {object } An object containing the coordinates of nodes,
93111 * total number of nodes, nodal numbering (NOP) array, and boundary elements
94112 */
95- async generateMeshFromMshFile ( file ) {
96-
113+ async generateMeshFromMshFile ( file ) {
97114 //for now i have made a parsing of simple quadrilateral .msh file of version 4.1
98115 const outputMesh = await importGmshQuadTri ( file ) ;
99116
100- return outputMesh
117+ return outputMesh ;
101118 }
102119
103120 /**
@@ -133,9 +150,9 @@ export class meshGeneration {
133150 // Generate nodal numbering (NOP) array
134151 const nodalNumbering = this . generateNodalNumbering (
135152 this . numElementsX ,
136- null , // numElementsY (not used in 1D)
153+ null , // numElementsY (not used in 1D)
137154 totalNodesX ,
138- null , // totalNodesY (not used in 1D)
155+ null , // totalNodesY (not used in 1D)
139156 this . elementOrder
140157 ) ;
141158 // Find boundary elements
@@ -159,15 +176,15 @@ export class meshGeneration {
159176 nodesYCoordinates [ 0 ] = yStart ;
160177 for ( let nodeIndexY = 1 ; nodeIndexY < totalNodesY ; nodeIndexY ++ ) {
161178 nodesXCoordinates [ nodeIndexY ] = nodesXCoordinates [ 0 ] ;
162- nodesYCoordinates [ nodeIndexY ] = nodesYCoordinates [ 0 ] + ( nodeIndexY * deltaY ) ;
179+ nodesYCoordinates [ nodeIndexY ] = nodesYCoordinates [ 0 ] + nodeIndexY * deltaY ;
163180 }
164181 for ( let nodeIndexX = 1 ; nodeIndexX < totalNodesX ; nodeIndexX ++ ) {
165182 const nnode = nodeIndexX * totalNodesY ;
166- nodesXCoordinates [ nnode ] = nodesXCoordinates [ 0 ] + ( nodeIndexX * deltaX ) ;
183+ nodesXCoordinates [ nnode ] = nodesXCoordinates [ 0 ] + nodeIndexX * deltaX ;
167184 nodesYCoordinates [ nnode ] = nodesYCoordinates [ 0 ] ;
168185 for ( let nodeIndexY = 1 ; nodeIndexY < totalNodesY ; nodeIndexY ++ ) {
169186 nodesXCoordinates [ nnode + nodeIndexY ] = nodesXCoordinates [ nnode ] ;
170- nodesYCoordinates [ nnode + nodeIndexY ] = nodesYCoordinates [ nnode ] + ( nodeIndexY * deltaY ) ;
187+ nodesYCoordinates [ nnode + nodeIndexY ] = nodesYCoordinates [ nnode ] + nodeIndexY * deltaY ;
171188 }
172189 }
173190 } else if ( this . elementOrder === "quadratic" ) {
@@ -242,7 +259,7 @@ export class meshGeneration {
242259 if ( this . meshDimension === "1D" ) {
243260 // Left boundary (element 0, side 0)
244261 boundaryElements [ 0 ] . push ( [ 0 , 0 ] ) ;
245-
262+
246263 // Right boundary (last element, side 1)
247264 boundaryElements [ 1 ] . push ( [ this . numElementsX - 1 , 1 ] ) ;
248265 } else if ( this . meshDimension === "2D" ) {
0 commit comments