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 and MEDLoader parts of the MED module. We need also mathematical functions, so we import the python math module::
24 from MEDCoupling import *
25 from MEDLoader import *
28 You must define 3 variables for space dimension, number of nodes on each dimension and total number of nodes::
33 NbCell2D = (N-1)*(N-1)
34 NbCell3D = NbCell2D*(N-1)
40 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42 First instanciate a meshing object. Therefore, we need to define :
48 .. note:: All this initialisation are necessary. If one lacks, you will have a segmentation fault!.
52 mesh=MEDCouplingUMesh.New()
53 mesh.setMeshDimension(3)
54 mesh.allocateCells(...)
55 mesh.setName("3Dcube")
57 Definition of nodes coordinates
58 ```````````````````````````````
60 Define the coordinates of the nodes of the 3D cube mesh, and then use the setCoords function to set it::
62 # Initialisation of coordinates
67 coordinates.append(...)
69 myCoords = DataArrayDouble.New()
70 myCoords.setValues(coordinates,nbOfNodes,3)
71 mesh.setCoords(myCoords)
74 Definition of hexahedrons connectivity
75 ``````````````````````````````````````
76 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).
78 .. image:: images/cube.jpg
80 .. warning:: Connectivity elements begin to 0 from (n-1) elements
89 connectivity.append(inode)
90 connectivity.append(...)
92 # Adding cells in meshing
93 for i in range(nbOfCells):
94 mesh.insertNextCell(NORM_HEXA8,8,connectivity[8*i:8*(i+1)])
98 mesh.finishInsertingCells()
102 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
104 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.
106 Definition of 2D mesh
107 ``````````````````````````````````````
113 coordinates.append(...)
115 Connectivities = [...]
116 myCoords = DataArrayDouble.New()
117 myCoords.setValues(coordinates,NbNode2D,MeshDim2D)
119 m1 = MEDCouplingUMesh.New()
120 m1.setMeshDimension(MeshDim2D)
121 m1.allocateCells(NbCell2D)
122 m1.setCoords(myCoords)
123 m1.setName("2D_Support")
125 for i in range(NbCell2D):
126 m1.insertNextCell(NORM_QUAD4,4,Connectivities[4*i:4*(i+1)])
127 m1.finishInsertingCells()
128 m1.changeSpaceDimension(3)
130 Definition of 1D mesh
131 ``````````````````````````````````````
136 m2 = MEDCouplingUMesh.New()
137 m2.setMeshDimension(1)
139 m2.insertNextCell(NORM_SEG2,2,conn[0:2])
140 m2.insertNextCell(NORM_SEG2,2,conn[2:4])
141 m2.insertNextCell(NORM_SEG2,2,conn[4:6])
142 m2.finishInsertingCells()
143 myCoords1D=DataArrayDouble.New()
144 myCoords1D.setValues(coords,4,1)
145 m2.setCoords(myCoords1D)
146 m2.changeSpaceDimension(3)
148 Definition of extruded mesh
149 ``````````````````````````````````````
151 Since 1D meshing isn't well oriented (along 0x vector), we need to imply a transformation on it.
152 Then, we can extrude 2D meshing.
159 m3 = m1.buildExtrudedMesh(m2,0)
160 m3.setName("Extrusion")
163 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
165 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.
169 mesh=MEDCouplingCMesh.New()
170 coordsX=DataArrayDouble.New()
172 coordsX.setValues(arrX,4,1)
173 coordsY=DataArrayDouble.New()
175 coordsY.setValues(arrY,4,1)
176 coordsZ=DataArrayDouble.New()
178 coordsZ.setValues(arrZ,4,1)
179 mesh.setCoords(coordsX,coordsY,coordsZ)
181 Really in order to save this mesh, you need to transform this structured mesh to an unstructerd mesh.
184 meshU=mesh.buildUnstructured()
185 meshU.setName("Grid")
188 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
190 A group is a set of cells defining by their id. This set must the input for creating a group.
191 Generally ids cells using in group are known. So you just need put these ids in a DataArray.
194 tabIdCells = DataArrayInt.New()
196 tabIdCells.setValues(IdCells,...)
199 .. note:: It's also possible to retrieve ids cells from a submesh of the principal mesh.
203 ret,tabIdCells = mesh.areCellsIncludedIn(subMesh,0)
206 Once the DataArray is created, some initializations have to be done.
209 # Definition of the name group
210 tabIdCells.setName("meshGroup")
213 In order to add a group on a mesh, you need to transform your unstructured mesh in a file unstructured mesh.
214 Moreover, we need to define:
220 * its number of cells
225 # Passing MEDCoupling to MEDFile
226 fmeshU = MEDFileUMesh.New()
227 fmeshU.setName("Grid")
228 fmeshU.setDescription("IHopeToConvinceLastMEDMEMUsers")
229 myCoords = meshU.getCoords()
230 fmeshU.setCoords(myCoords)
231 fmeshU.setMeshAtLevel(0,meshU)
234 Therefore, you need to define the level (ie. the dimension) of the group.
235 This information is given by a number : 0,-1 or -2.
237 * 0 means the same level at mesh
241 fmeshU.setGroupsAtLevel(0,[tabIdCells],False)
243 Create field on 3D cube
244 ~~~~~~~~~~~~~~~~~~~~~~~
246 First you need to create a CouplingField and initialize some data:
249 * its support (ie mesh)
254 The field will be a sin function dependant of distance of the barycenter of each cell from origin. So we need to create a barycenter field on the 3D mesh::
256 # Creation of field : with following definition
257 # => Definition of the mesh support
258 # => Definition of field name
259 # => Definition of field nature
260 field = MEDCouplingFieldDouble.New(ON_CELLS)
262 field.setName("field")
263 field.setNature(Integral)
265 # Computing and setting field values
266 myCoords=DataArrayDouble.New()
268 bar = mesh.getBarycenterAndOwner()
269 print bar.getNbOfElems()
270 for i in range(nbOfCells):
274 d = sqrt(x*x+y*y+z*z)
276 . sampleTab.append(sinus)
278 myCoords.setValues(sampleTab,nbOfCells,1)
279 field.setArray(myCoords)
282 Saving the mesh in a med file
283 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
288 medFileName = "MEDCoupling_Extrudedcube3D.med"
289 MEDLoader.WriteUMesh(medFileName,meshU,True)
291 .. note:: True / False in Write* functions : True for overwriting existing file and False for adding in existing file
296 In spite of a MEDCoupling mesh has only one dimension, it's possible to genrate a file with multi dimension.
297 Therefore, you need to create as meshes as necessary dimensions.
299 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.
300 The connectivity must respect following figure:
302 .. image:: images/face.jpg
306 # Extraction of surfacic meshing
309 nodes = mesh.findNodesOnPlane(pt,vec,1e-12)
310 mesh2D = mesh.buildFacePartOfMySelfNode(nodes,True)
312 mesh2D.setName("3Dcube")
313 mesh2D.checkCoherency()
315 medFileName = "MEDCoupling_cube3D.med"
317 MEDLoader.WriteUMeshes(medFileName,meshes,True)
323 medFileName = "MEDCoupling_Gridcube3D.med"
324 fmeshU.write(medFileName,2)
326 Saving the fields in the med file
327 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
331 MEDLoader.WriteField(medFileName,field,False)
333 Visualize the mesh with the SMESH module of Salome
334 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
336 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.
338 Visualize the fields with the VISU module of Salome
339 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
341 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.
343 .. image:: images/Field_Cube3D.jpg
348 :ref:`python_testMEDCouplingcube_solution`