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 instantiate 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)])
97 # Check mesh consistency:
98 mesh.checkConsistencyLight()
101 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103 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.
105 Definition of 2D mesh
106 ``````````````````````````````````````
112 coordinates.append(...)
114 Connectivities = [...]
115 myCoords = DataArrayDouble.New()
116 myCoords.setValues(coordinates,NbNode2D,MeshDim2D)
118 m1 = MEDCouplingUMesh.New()
119 m1.setMeshDimension(MeshDim2D)
120 m1.allocateCells(NbCell2D)
121 m1.setCoords(myCoords)
122 m1.setName("2D_Support")
124 for i in range(NbCell2D):
125 m1.insertNextCell(NORM_QUAD4,4,Connectivities[4*i:4*(i+1)])
126 m1.changeSpaceDimension(3)
128 Definition of 1D mesh
129 ``````````````````````````````````````
134 m2 = MEDCouplingUMesh.New()
135 m2.setMeshDimension(1)
137 m2.insertNextCell(NORM_SEG2,2,conn[0:2])
138 m2.insertNextCell(NORM_SEG2,2,conn[2:4])
139 m2.insertNextCell(NORM_SEG2,2,conn[4:6])
140 myCoords1D=DataArrayDouble.New()
141 myCoords1D.setValues(coords,4,1)
142 m2.setCoords(myCoords1D)
143 m2.changeSpaceDimension(3)
145 Definition of extruded mesh
146 ``````````````````````````````````````
148 Since 1D meshing isn't well oriented (along 0x vector), we need to imply a transformation on it.
149 Then, we can extrude 2D meshing.
156 m3 = m1.buildExtrudedMesh(m2,0)
157 m3.setName("Extrusion")
160 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
162 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.
166 mesh=MEDCouplingCMesh.New()
167 coordsX=DataArrayDouble.New()
169 coordsX.setValues(arrX,4,1)
170 coordsY=DataArrayDouble.New()
172 coordsY.setValues(arrY,4,1)
173 coordsZ=DataArrayDouble.New()
175 coordsZ.setValues(arrZ,4,1)
176 mesh.setCoords(coordsX,coordsY,coordsZ)
178 Really in order to save this mesh, you need to transform this structured mesh to an unstructerd mesh.
181 meshU=mesh.buildUnstructured()
182 meshU.setName("Grid")
185 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187 A group is a set of cells defining by their id. This set must the input for creating a group.
188 Generally ids cells using in group are known. So you just need put these ids in a DataArray.
191 tabIdCells = DataArrayInt.New()
193 tabIdCells.setValues(IdCells,...)
196 .. note:: It's also possible to retrieve ids cells from a submesh of the principal mesh.
200 ret,tabIdCells = mesh.areCellsIncludedIn(subMesh,0)
203 Once the DataArray is created, some initializations have to be done.
206 # Definition of the name group
207 tabIdCells.setName("meshGroup")
210 In order to add a group on a mesh, you need to transform your unstructured mesh in a file unstructured mesh.
211 Moreover, we need to define:
217 * its number of cells
222 # Passing MEDCoupling to MEDFile
223 fmeshU = MEDFileUMesh.New()
224 fmeshU.setName("Grid")
225 fmeshU.setDescription("IHopeToConvinceLastMEDMEMUsers")
226 myCoords = meshU.getCoords()
227 fmeshU.setCoords(myCoords)
228 fmeshU.setMeshAtLevel(0,meshU)
231 Therefore, you need to define the level (ie. the dimension) of the group.
232 This information is given by a number : 0,-1 or -2.
234 * 0 means the same level at mesh
238 fmeshU.setGroupsAtLevel(0,[tabIdCells],False)
240 Create field on 3D cube
241 ~~~~~~~~~~~~~~~~~~~~~~~
243 First you need to create a CouplingField and initialize some data:
246 * its support (ie mesh)
251 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::
253 # Creation of field : with following definition
254 # => Definition of the mesh support
255 # => Definition of field name
256 # => Definition of field nature
257 field = MEDCouplingFieldDouble.New(ON_CELLS)
259 field.setName("field")
260 field.setNature(ExtensiveMaximum)
262 # Computing and setting field values
263 myCoords=DataArrayDouble.New()
265 bar = mesh.computeCellCenterOfMass()
266 print bar.getNbOfElems()
267 for i in range(nbOfCells):
271 d = sqrt(x*x+y*y+z*z)
273 . sampleTab.append(sinus)
275 myCoords.setValues(sampleTab,nbOfCells,1)
276 field.setArray(myCoords)
279 Saving the mesh in a med file
280 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
285 medFileName = "MEDCoupling_Extrudedcube3D.med"
286 MEDLoader.WriteUMesh(medFileName,meshU,True)
288 .. note:: True / False in Write* functions : True for overwriting existing file and False for adding in existing file
293 In spite of a MEDCoupling mesh has only one dimension, it's possible to genrate a file with multi dimension.
294 Therefore, you need to create as meshes as necessary dimensions.
296 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.
297 The connectivity must respect following figure:
299 .. image:: images/face.jpg
303 # Extraction of surfacic meshing
306 nodes = mesh.findNodesOnPlane(pt,vec,1e-12)
307 mesh2D = mesh.buildFacePartOfMySelfNode(nodes,True)
309 mesh2D.setName("3Dcube")
310 mesh2D.checkConsistencyLight()
312 medFileName = "MEDCoupling_cube3D.med"
314 MEDLoader.WriteUMeshes(medFileName,meshes,True)
320 medFileName = "MEDCoupling_Gridcube3D.med"
321 fmeshU.write(medFileName,2)
323 Saving the fields in the med file
324 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
328 MEDLoader.WriteField(medFileName,field,False)
330 Visualize the mesh with the SMESH module of Salome
331 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
333 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.
335 Visualize the fields with the VISU module of Salome
336 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
338 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.
340 .. image:: images/Field_Cube3D.jpg
345 :ref:`python_testMEDCouplingcube_solution`