Salome HOME
3f0764385d495c5e1b638665b2b4159fda5b0b90
[tools/medcoupling.git] / doc / tutorial / medcoupling_2Dpolygon.rst
1
2 Meshing a 2D geometry with polygons
3 -----------------------------------
4
5 Objective
6 ~~~~~~~~~
7
8 In this example we propose to build a mesh on a 2D geometry. We first create triangles in the center of the geometry and then hexagons around them. The result will be saved in a med file, and then visualized with the SMESH module of Salome.
9
10 .. image:: images/Mesh_polygons2D.jpg
11
12
13 Beginning of implementation
14 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
15
16 To implement this exercice we use the python language script and import the MEDCoupling and MEDLoader parts of the MED module::
17
18         from MEDCoupling import *
19         from MEDLoader import *
20
21         from math import *
22
23 Then we must instanciate a meshing object::
24
25         mesh=MEDCouplingUMesh.New()
26         mesh.setMeshDimension(2)
27         mesh.allocateCells(numberOfCells)
28         mesh.setName("MaFleur")
29
30
31 You must define 2 variables for space dimension and total number of nodes::
32
33         numberOfNodes = 25
34         numberOfCells = 12
35         spaceDimension = 2
36
37
38 You must define the coordinates of the nodes of the central hexagon::
39
40         X = [1.,0.5,-0.5,-1.,-0.5,0.5]
41         Y = [0.,sqrt(3.)/2.,sqrt(3.)/2.,0.,-sqrt(3.)/2.,-sqrt(3.)/2.]
42
43 Definition of nodes coordinates
44 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
45
46 Define the coordinates of the origin and the nodes of the 2D central polygon::
47
48         coordinates = []
49         # origin
50         coordinates.append(0.)
51         coordinates.append(0.)
52
53         # central polygon
54         for i in range(6):
55           coordinates.append(X[i])
56           coordinates.append(Y[i])
57
58 Define the coordinates of the extra nodes of the hexagons around the central hexagon::
59
60         # second couron
61         xc = 1.5
62         yc = - sqrt(3.)/2.
63
64         for i in range(6):
65           ...
66
67         myCoords=DataArrayDouble.New()
68         myCoords.setValues(coordinates,numberOfNodes,2)
69         mesh.setCoords(myCoords)
70
71
72 Definition of triangles connectivity
73 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74
75 For each triangle of the mesh, you have to give its connectivity: the list of the nodes which belong to the triangles::
76
77         connectivity = []
78         for i in range(6):
79                 connectivity.append(0)
80                 connectivity.append(...)
81                 connectivity.append(...)
82         for i in range(6):
83                 mesh.insertNextCell(NORM_TRI3,3,connectivity[3*i:3*(i+1)])
84                 pass
85
86
87 Definition of hexagons connectivity
88 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89
90 For each hexagon of the mesh, you have to give its connectivity: the list of the nodes which belong to the hexagons::
91
92         connectivity = []
93         for i in range(6):
94                 start = i%6+1
95                 connectivity.append(...)
96         for i in range(6):
97                 mesh.insertNextCell(NORM_POLYGON,6,connectivity[6*i:6*(i+1)])
98                 pass
99         mesh.checkCoherency()
100
101 Saving the mesh in a med file
102 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103
104 You have to create a med file with the MED driver::
105
106         medFileName = "MEDCoupling_Fleur.med"
107         MEDLoader.WriteUMesh(medFileName,mesh,True)
108
109 Visualize the mesh with SMESH module of Salome
110 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
111
112 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 triangles and the number of hexagons. To visualize your mesh: click right bottom on your mesh and select "Show" option.
113
114 Solution
115 ~~~~~~~~
116
117 :ref:`python_testMEDCoupling2D_solution`