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