8 The meshing class of the SALOME MED module allows user to create a mesh from scratch.
9 In this example we propose to build a mesh on a 3D cube by three methods (classical method, method by extrusion and grid method). Each cell of the mesh must be a hexaedron.
10 We see also how creating a group.
11 Then we create a field on all the 3D cube.
12 Each result will be saved in a med file, and then visualized with the SMESH module of Salome.
14 In spite of a mesh in MEDCoupling has only one dimension, it's possible to save some meshes with different dimension in one med file. We will see this method.
16 .. image:: images/Mesh_cube3D.jpg
19 Beginning of implementation
20 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
22 To implement this exercice we use the python language script and import the medcoupling module. We need also mathematical functions, so we import the python math module::
24 import medcoupling as mc
27 You must define 3 variables for space dimension, number of nodes on each dimension and total number of nodes::
32 NbCell2D = (N-1)*(N-1)
33 NbCell3D = NbCell2D*(N-1)
39 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41 First instantiate a meshing object. Therefore, we need to define :
47 .. note:: All this initialisation is necessary. If one is missing, you'll have a segmentation fault!.
51 mesh = mc.MEDCouplingUMesh.New()
52 mesh.setMeshDimension(3)
53 mesh.allocateCells(...)
54 mesh.setName("3Dcube")
56 Definition of nodes coordinates
57 ```````````````````````````````
59 Define the coordinates of the nodes of the 3D cube mesh, and then use the setCoords function to set it::
61 # Initialisation of coordinates
66 coordinates.append(...)
68 myCoords = mc.DataArrayDouble.New()
69 myCoords.setValues(coordinates,nbOfNodes,3)
70 mesh.setCoords(myCoords)
73 Definition of hexahedrons connectivity
74 ``````````````````````````````````````
75 For each hexahedron of the mesh, you have to give its connectivity: the list of the nodes which belong to the hexahedron. The order of the nodes in the connectivity array must respect the MEDCoupling format (see the following figure).
77 .. image:: images/cube.jpg
79 .. warning:: Connectivity elements begin to 0 from (n-1) elements
88 connectivity.append(inode)
89 connectivity.append(...)
91 # Adding cells in meshing
92 for i in range(nbOfCells):
93 mesh.insertNextCell(mc.NORM_HEXA8,8,connectivity[8*i:8*(i+1)])
96 # Check mesh consistency:
97 mesh.checkConsistencyLight()
100 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102 In order to create a extruded mesh, we need one 2D mesh and one 1D mesh which define the vector of extrusion and the number of steps.
104 Definition of 2D mesh
105 ``````````````````````````````````````
111 coordinates.append(...)
113 Connectivities = [...]
114 myCoords = mc.DataArrayDouble.New()
115 myCoords.setValues(coordinates,NbNode2D,MeshDim2D)
117 m1 = mc.MEDCouplingUMesh.New()
118 m1.setMeshDimension(MeshDim2D)
119 m1.allocateCells(NbCell2D)
120 m1.setCoords(myCoords)
121 m1.setName("2D_Support")
123 for i in range(NbCell2D):
124 m1.insertNextCell(mc.NORM_QUAD4,4,Connectivities[4*i:4*(i+1)])
125 m1.changeSpaceDimension(3)
127 Definition of 1D mesh
128 ``````````````````````````````````````
133 m2 = mc.MEDCouplingUMesh.New()
134 m2.setMeshDimension(1)
136 m2.insertNextCell(mc.NORM_SEG2,2,conn[0:2])
137 m2.insertNextCell(mc.NORM_SEG2,2,conn[2:4])
138 m2.insertNextCell(mc.NORM_SEG2,2,conn[4:6])
139 myCoords1D=mc.DataArrayDouble.New()
140 myCoords1D.setValues(coords,4,1)
141 m2.setCoords(myCoords1D)
142 m2.changeSpaceDimension(3)
144 Definition of extruded mesh
145 ``````````````````````````````````````
147 Since 1D meshing isn't well oriented (along 0x vector), we need to imply a transformation on it.
148 Then, we can extrude 2D meshing.
155 m3 = m1.buildExtrudedMesh(m2,0)
156 m3.setName("Extrusion")
159 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
161 it's the easiest way to create a grid since you have no connectivity to set. They will be automatically setting. Incrementation of ids will be made first along Ox axis, then along Oy axis and finally along Oz axis.
165 mesh=mc.MEDCouplingCMesh.New()
166 coordsX=mc.DataArrayDouble.New()
168 coordsX.setValues(arrX,4,1)
169 coordsY=mc.DataArrayDouble.New()
171 coordsY.setValues(arrY,4,1)
172 coordsZ=mc.DataArrayDouble.New()
174 coordsZ.setValues(arrZ,4,1)
175 mesh.setCoords(coordsX,coordsY,coordsZ)
177 Really in order to save this mesh, you need to transform this structured mesh to an unstructerd mesh.
180 meshU=mesh.buildUnstructured()
181 meshU.setName("Grid")
184 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
186 A group is a set of cells defining by their id. This set must the input for creating a group.
187 Generally ids cells using in group are known. So you just need put these ids in a DataArray.
190 tabIdCells = mc.DataArrayInt.New()
192 tabIdCells.setValues(IdCells,...)
195 .. note:: It's also possible to retrieve ids cells from a submesh of the principal mesh.
199 ret,tabIdCells = mesh.areCellsIncludedIn(subMesh,0)
202 Once the DataArray is created, some initializations have to be done.
205 # Definition of the name group
206 tabIdCells.setName("meshGroup")
209 In order to add a group on a mesh, you need to transform your unstructured mesh in a file unstructured mesh.
210 Moreover, we need to define:
216 * its number of cells
221 # Passing MEDCoupling to MEDFile
222 fmeshU = mc.MEDFileUMesh.New()
223 fmeshU.setName("Grid")
224 fmeshU.setDescription("IHopeToConvinceLastMEDMEMUsers")
225 myCoords = meshU.getCoords()
226 fmeshU.setCoords(myCoords)
227 fmeshU.setMeshAtLevel(0,meshU)
230 Therefore, you need to define the level (ie. the dimension) of the group.
231 This information is given by a number : 0,-1 or -2.
233 * 0 means the same level at mesh
237 fmeshU.setGroupsAtLevel(0,[tabIdCells],False)
239 Create field on 3D cube
240 ~~~~~~~~~~~~~~~~~~~~~~~
242 First you need to create a CouplingField and initialize some data:
245 * its support (ie mesh)
250 The field will be a sin function dependent of distance of the barycenter of each cell from origin. So we need to create a barycenter field on the 3D mesh::
252 # Creation of field : with following definition
253 # => Definition of the mesh support
254 # => Definition of field name
255 # => Definition of field nature
256 field = mc.MEDCouplingFieldDouble.New(ON_CELLS)
258 field.setName("field")
259 field.setNature(ExtensiveMaximum)
261 # Computing and setting field values
262 myCoords=mc.DataArrayDouble.New()
264 bar = mesh.computeCellCenterOfMass()
265 print(bar.getNbOfElems())
266 for i in range(nbOfCells):
270 d = sqrt(x*x+y*y+z*z)
272 . sampleTab.append(sinus)
274 myCoords.setValues(sampleTab,nbOfCells,1)
275 field.setArray(myCoords)
278 Saving the mesh in a med file
279 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
284 medFileName = "MEDCoupling_Extrudedcube3D.med"
285 mc.WriteUMesh(medFileName,meshU,True)
287 .. note:: True / False in Write* functions : True for overwriting existing file and False for adding in existing file
292 In spite of a MEDCoupling mesh has only one dimension, it's possible to generate a file with multi dimension.
293 Therefore, you need to create as meshes as necessary dimensions.
295 You have to give the connectivity of the faces on the bottom face of the 3D cube: the list of the nodes which belong to the face.
296 The connectivity must respect following figure:
298 .. image:: images/face.jpg
302 # Extraction of surfacic meshing
305 nodes = mesh.findNodesOnPlane(pt,vec,1e-12)
306 mesh2D = mesh.buildFacePartOfMySelfNode(nodes,True)
308 mesh2D.setName("3Dcube")
309 mesh2D.checkConsistencyLight()
311 medFileName = "MEDCoupling_cube3D.med"
313 mc.WriteUMeshes(medFileName,meshes,True)
319 medFileName = "MEDCoupling_Gridcube3D.med"
320 fmeshU.write(medFileName,2)
322 Saving the fields in the med file
323 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
327 mc.WriteField(medFileName,field,False)
329 Visualize the mesh with the SMESH module of Salome
330 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
332 Launch Salome platform, then select SMESH module and import your MED file. First You can see the number of elements of your mesh. For that, select your mesh in the object browser, set select Mesh menu and "Advanced Mesh Info" option. Verify the number of faces and the number of hexahedrons. To visualize your mesh: click right bottom on your mesh and select "Show" option. You can also visualize your groups. Select one group, click right bottom on your group and select "Show only" option.
334 Visualize the fields with the VISU module of Salome
335 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
337 Launch Salome platform, then select VISU module and import your MED file. You can see in the object browser the 2 fields you have created. Then you have to create a scalar map on each field to visualize them.
339 .. image:: images/Field_Cube3D.jpg
344 :ref:`python_testMEDCouplingcube_solution`