Salome HOME
61e4bd404a4cfbd34593c525fb2a8019e0ebea68
[tools/medcoupling.git] / doc / tutorial / atestMEDCouplingCube.rst
1
2 .. _python_testMEDCouplingcube_solution:
3
4 3D cube meshing
5 ~~~~~~~~~~~~~~~
6
7 ::
8
9         from MEDCoupling import *
10         from MEDLoader import *
11         import MEDLoaderDataForTest
12
13         from math import *
14
15         # Definition of environnement variables
16         spaceDimension = 3
17         N = 4
18         nbOfNodes = N*N*N
19         nbOfCells = (N-1)*(N-1)*(N-1)
20         nbOfCells2D = (N-1)*(N-1)
21
22         print "1 ********************"
23         # Initialisation of coordinates
24         coordinates = []
25         for k in range(N):
26                 for j in range(N):
27                         for i in range(N):
28                                 coordinates.append(float(i))
29                                 coordinates.append(float(j))
30                                 coordinates.append(float(k))
31                                 
32         print "2 ********************"
33         # Creation of meshing : need following initialisations
34         # => Definition of the mesh dimension
35         # => Definition of number of cells
36         # => Definition of name of meshing
37         mesh=MEDCouplingUMesh.New()
38         mesh.setMeshDimension(3)
39         mesh.allocateCells(nbOfCells+nbOfCells2D)
40         mesh.setName("3Dcube")
41
42         print "3 ********************"
43         # One remark : only one dimension cells by meshing
44         # Construction of volumic meshing
45         # => Definition of connectivity
46         # => Definition of type of cells
47         connectivity = []
48         for k in range(N-1):
49                 for j in range(N-1):
50                         for i in range(N-1):
51                                 inode = N*N*(k+1)+ N*(j+1)+i
52                                 connectivity.append(inode)
53                                 connectivity.append(inode-N)
54                                 connectivity.append(inode-N+1)
55                                 connectivity.append(inode+1)
56                                 connectivity.append(inode-N*N)
57                                 connectivity.append(inode-N*N-N)
58                                 connectivity.append(inode-N*N-N+1)
59                                 connectivity.append(inode-N*N+1)
60         print len(connectivity)
61         print 8*(nbOfCells)
62
63         print "4 ********************"
64         # Adding cells in meshing
65         for i in range(nbOfCells):
66                 mesh.insertNextCell(NORM_HEXA8,8,connectivity[8*i:8*(i+1)])
67                 pass
68
69         print "5 ********************"
70         # Settings of coordinates and verify if it's OK
71         myCoords = DataArrayDouble.New()
72         myCoords.setValues(coordinates,nbOfNodes,3)
73         mesh.setCoords(myCoords)
74         mesh.checkCoherency()
75
76         print "6 ********************"
77         # Extraction of surfacic meshing
78         pt=[0.,0.,0.]
79         vec=[0.,0.,1.]
80         nodes = mesh.findNodesOnPlane(pt,vec,1e-12)
81         mesh2D = mesh.buildFacePartOfMySelfNode(nodes,True)
82         #print mesh2D
83         mesh2D.setName("3Dcube")
84         mesh2D.checkCoherency()
85
86         print "7 ********************"
87         # Creation of field : with following definition
88         # => Definition of the mesh support
89         # => Definition of field name
90         # => Definition of field nature
91         field = MEDCouplingFieldDouble.New(ON_CELLS)
92         field.setMesh(mesh)
93         field.setName("field")
94         field.setNature(Integral)
95
96         # Computing and setting field values
97         myCoords=DataArrayDouble.New()
98         sampleTab=[]
99         bar = mesh.getBarycenterAndOwner()
100         print bar.getNbOfElems()
101         for i in range(nbOfCells):
102                 x = bar.getIJ(i+1,1)
103                 y = bar.getIJ(i+1,2)
104                 z = bar.getIJ(i+1,3)
105                 d = sqrt(x*x+y*y+z*z)
106                 sinus = sin(d)
107                 #f.setValueIJ(i+1,1,sin(d))
108                 sampleTab.append(sinus)
109
110         myCoords.setValues(sampleTab,nbOfCells,1)
111         field.setArray(myCoords)
112
113         fBF = MEDCouplingFieldDouble.New(ON_CELLS)
114         fBF.setMesh(mesh2D)
115         fBF.setName("fieldBottomFace")
116         fBF.setNature(Integral)
117         Cval = 10.
118         myCoords2D=DataArrayDouble.New()
119         sampleTab=[]
120         for i in range(nbOfCells2D):
121                 sampleTab.append(Cval)
122         myCoords2D.setValues(sampleTab,nbOfCells2D,1)
123         fBF.setArray(myCoords2D)
124
125         medFileName = "MEDCoupling_cube3D.med"
126         # For note : True / False in Write* functions
127         # => True : overwriting existing file
128         # => False : add in existing file 
129         meshes=[mesh2D,mesh]
130         MEDLoader.WriteUMeshes(medFileName,meshes,True);
131         MEDLoader.WriteField(medFileName,field,False)
132         MEDLoader.WriteField(medFileName,fBF,False)
133
134
135 ::
136
137         from MEDCoupling import *
138         from MEDLoader import *
139         import MEDLoaderDataForTest
140
141         from math import *
142
143         spaceDim3D = 3
144         MeshDim2D  = 2
145         N = 4
146         NbCell2D = (N-1)*(N-1)
147         NbCell3D = NbCell2D*(N-1)
148         NbNode2D = N*N
149         NbNode3D = NbNode2D*N
150
151         # Creation of a extruded meshing
152         # input : a 2D meshing and a 1D meshing
153         # Creation of 2D meshing
154         coordinates = []
155         for j in range(N):
156                 for i in range(N):
157                         coordinates.append(float(i))
158                         coordinates.append(float(j))
159         Connectivities = [0,4,5,1, 1,5,6,2, 2,6,7,3, 4,8,9,5, 5,9,10,6, 6,10,11,7, 8,12,13,9, 9,13,14,10, 10,14,15,11]
160         myCoords = DataArrayDouble.New()
161         myCoords.setValues(coordinates,NbNode2D,MeshDim2D)
162
163         m1 = MEDCouplingUMesh.New()
164         m1.setMeshDimension(MeshDim2D)
165         m1.allocateCells(NbCell2D)
166         m1.setCoords(myCoords)
167         m1.setName("2D_Support")
168
169         for i in range(NbCell2D):
170                 m1.insertNextCell(NORM_QUAD4,4,Connectivities[4*i:4*(i+1)])
171         m1.changeSpaceDimension(3)
172
173         # Creation of 1D meshing
174         coords = [ 0.0, 1.0, 2.0, 3.0 ]
175         conn   = [ 0,1, 1,2, 2,3 ]
176         m2 = MEDCouplingUMesh.New()
177         m2.setMeshDimension(1)
178         m2.allocateCells(3)
179         m2.insertNextCell(NORM_SEG2,2,conn[0:2])
180         m2.insertNextCell(NORM_SEG2,2,conn[2:4])
181         m2.insertNextCell(NORM_SEG2,2,conn[4:6])
182         myCoords1D=DataArrayDouble.New()
183         myCoords1D.setValues(coords,4,1)
184         m2.setCoords(myCoords1D)
185         m2.changeSpaceDimension(3)
186
187         # Construction of extruded meshing
188         center = [0.,0.,0.]
189         vector = [0.,1.,0.]
190         m2.rotate(center,vector,pi/2.)
191         m3 = m1.buildExtrudedMesh(m2,0)
192         m3.setName("Extrusion")
193
194         # Construction of group : old fashion mode
195         part=[1]
196         meshGroup=m3.buildPartOfMySelf(part,True);
197         meshGroup.setName("meshGroup");
198
199         medFileName = "MEDCoupling_Extrudedcube3D.med"
200         MEDLoader.WriteUMeshesPartition(medFileName,"Extrusion",[m3,meshGroup],True)
201         
202
203 ::
204
205         from MEDCoupling import *
206         from MEDLoader import *
207         import MEDLoaderDataForTest
208
209         from math import *
210
211         spaceDim3D = 3
212         MeshDim2D  = 2
213         N = 4
214         NbCell2D = (N-1)*(N-1)
215         NbCell3D = NbCell2D*(N-1)
216         NbNode2D = N*N
217         NbNode3D = NbNode2D*N
218
219         # Creation of a grid => Structured mesh
220         # Need directions definition
221         mesh=MEDCouplingCMesh.New()
222         coordsX=DataArrayDouble.New()
223         arrX=[ 0., 1., 2., 3. ]
224         coordsX.setValues(arrX,4,1)
225         coordsY=DataArrayDouble.New()
226         arrY=[ 0., 1., 2., 3. ]
227         coordsY.setValues(arrY,4,1)
228         coordsZ=DataArrayDouble.New()
229         arrZ=[ 0., 1., 2., 3. ]
230         coordsZ.setValues(arrZ,4,1)
231         mesh.setCoords(coordsX,coordsY,coordsZ)
232         # Passing structured meshing to unstructured
233         # necessary to save meshing
234         meshU=mesh.buildUnstructured()
235         meshU.setName("Grid")
236
237         # Creation of group : fashion mode
238         # if ids cells are known, this step is not to be made
239         pt=[1]
240         m2 = meshU.buildPartOfMySelf(pt,True);
241         ret,tabIdCells = meshU.areCellsIncludedIn(m2,0)
242         print ret
243         print tabIdCells
244         # Definition of the name group
245         tabIdCells.setName("meshGroup")
246
247         # Passing MEDCoupling to MEDFile
248         fmeshU = MEDFileUMesh.New()
249         fmeshU.setName("Grid")
250         fmeshU.setDescription("IHopeToConvinceLastMEDMEMUsers")
251         myCoords = meshU.getCoords()
252         print myCoords
253         fmeshU.setCoords(myCoords)
254         print "**************************"
255         fmeshU.setMeshAtLevel(0,meshU)
256         print "**************************"
257         fmeshU.setGroupsAtLevel(0,[tabIdCells],False)
258         print "**************************"
259
260         medFileName = "MEDCoupling_Gridcube3D.med"
261         fmeshU.write(medFileName,2)
262