Salome HOME
Fix computation height of isocel triangle with base equal zero : NaN
[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 module. We need also mathematical functions, so we import the python math module::
17
18     import medcoupling as mc
19     from math import *
20
21 Then we must instantiate a meshing object::
22
23         mesh=mc.MEDCouplingUMesh.New()
24         mesh.setMeshDimension(2)
25         mesh.allocateCells(numberOfCells)
26         mesh.setName("MaFleur")
27
28
29 You must define 2 variables for space dimension and total number of nodes::
30
31         numberOfNodes = 25
32         numberOfCells = 12
33         spaceDimension = 2
34
35
36 You must define the coordinates of the nodes of the central hexagon::
37
38         X = [1.,0.5,-0.5,-1.,-0.5,0.5]
39         Y = [0.,sqrt(3.)/2.,sqrt(3.)/2.,0.,-sqrt(3.)/2.,-sqrt(3.)/2.]
40
41 Definition of nodes coordinates
42 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
43
44 Define the coordinates of the origin and the nodes of the 2D central polygon::
45
46         coordinates = []
47         # origin
48         coordinates.append(0.)
49         coordinates.append(0.)
50
51         # central polygon
52         for i in range(6):
53           coordinates.append(X[i])
54           coordinates.append(Y[i])
55
56 Define the coordinates of the extra nodes of the hexagons around the central hexagon::
57
58         # second couron
59         xc = 1.5
60         yc = - sqrt(3.)/2.
61
62         for i in range(6):
63           ...
64
65         myCoords=mc.DataArrayDouble.New()
66         myCoords.setValues(coordinates,numberOfNodes,2)
67         mesh.setCoords(myCoords)
68
69
70 Definition of triangles connectivity
71 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72
73 For each triangle of the mesh, you have to give its connectivity: the list of the nodes which belong to the triangles::
74
75         connectivity = []
76         for i in range(6):
77                 connectivity.append(0)
78                 connectivity.append(...)
79                 connectivity.append(...)
80         for i in range(6):
81                 mesh.insertNextCell(mc.NORM_TRI3,3,connectivity[3*i:3*(i+1)])
82                 pass
83
84
85 Definition of hexagons connectivity
86 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87
88 For each hexagon of the mesh, you have to give its connectivity: the list of the nodes which belong to the hexagons::
89
90         connectivity = []
91         for i in range(6):
92                 start = i%6+1
93                 connectivity.append(...)
94         for i in range(6):
95                 mesh.insertNextCell(mc.NORM_POLYGON,6,connectivity[6*i:6*(i+1)])
96                 pass
97         mesh.checkConsistencyLight()
98
99 Saving the mesh in a med file
100 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
101
102 You have to create a med file with the MED driver::
103
104         medFileName = "MEDCoupling_Fleur.med"
105         mc.WriteUMesh(medFileName,mesh,True)
106
107 Visualize the mesh with SMESH module of Salome
108 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
109
110 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.
111
112 Solution
113 ~~~~~~~~
114
115 :ref:`python_testMEDCoupling2D_solution`