2 .. _python_testMEDCouplingcube_solution:
9 from MEDCoupling import *
10 from MEDLoader import *
11 import MEDLoaderDataForTest
15 # Definition of environnement variables
19 nbOfCells = (N-1)*(N-1)*(N-1)
20 nbOfCells2D = (N-1)*(N-1)
22 print "1 ********************"
23 # Initialisation of coordinates
28 coordinates.append(float(i))
29 coordinates.append(float(j))
30 coordinates.append(float(k))
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")
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
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)
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)])
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.checkConsistencyLight()
76 print "6 ********************"
77 # Extraction of surfacic meshing
80 nodes = mesh.findNodesOnPlane(pt,vec,1e-12)
81 mesh2D = mesh.buildFacePartOfMySelfNode(nodes,True)
83 mesh2D.setName("3Dcube")
84 mesh2D.checkConsistencyLight()
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)
93 field.setName("field")
94 field.setNature(ExtensiveMaximum)
96 # Computing and setting field values
97 myCoords=DataArrayDouble.New()
99 bar = mesh.computeCellCenterOfMass()
100 print bar.getNbOfElems()
101 for i in range(nbOfCells):
105 d = sqrt(x*x+y*y+z*z)
107 #f.setValueIJ(i+1,1,sin(d))
108 sampleTab.append(sinus)
110 myCoords.setValues(sampleTab,nbOfCells,1)
111 field.setArray(myCoords)
113 fBF = MEDCouplingFieldDouble.New(ON_CELLS)
115 fBF.setName("fieldBottomFace")
116 fBF.setNature(ExtensiveMaximum)
118 myCoords2D=DataArrayDouble.New()
120 for i in range(nbOfCells2D):
121 sampleTab.append(Cval)
122 myCoords2D.setValues(sampleTab,nbOfCells2D,1)
123 fBF.setArray(myCoords2D)
125 medFileName = "MEDCoupling_cube3D.med"
126 # For note : True / False in Write* functions
127 # => True : overwriting existing file
128 # => False : add in existing file
130 MEDLoader.WriteUMeshes(medFileName,meshes,True);
131 MEDLoader.WriteField(medFileName,field,False)
132 MEDLoader.WriteField(medFileName,fBF,False)
137 from MEDCoupling import *
138 from MEDLoader import *
139 import MEDLoaderDataForTest
146 NbCell2D = (N-1)*(N-1)
147 NbCell3D = NbCell2D*(N-1)
149 NbNode3D = NbNode2D*N
151 # Creation of a extruded meshing
152 # input : a 2D meshing and a 1D meshing
153 # Creation of 2D meshing
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)
163 m1 = MEDCouplingUMesh.New()
164 m1.setMeshDimension(MeshDim2D)
165 m1.allocateCells(NbCell2D)
166 m1.setCoords(myCoords)
167 m1.setName("2D_Support")
169 for i in range(NbCell2D):
170 m1.insertNextCell(NORM_QUAD4,4,Connectivities[4*i:4*(i+1)])
171 m1.changeSpaceDimension(3)
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)
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)
187 # Construction of extruded meshing
190 m2.rotate(center,vector,pi/2.)
191 m3 = m1.buildExtrudedMesh(m2,0)
192 m3.setName("Extrusion")
194 # Construction of group : old fashion mode
196 meshGroup=m3.buildPartOfMySelf(part,True);
197 meshGroup.setName("meshGroup");
199 medFileName = "MEDCoupling_Extrudedcube3D.med"
200 MEDLoader.WriteUMeshesPartition(medFileName,"Extrusion",[m3,meshGroup],True)
205 from MEDCoupling import *
206 from MEDLoader import *
207 import MEDLoaderDataForTest
214 NbCell2D = (N-1)*(N-1)
215 NbCell3D = NbCell2D*(N-1)
217 NbNode3D = NbNode2D*N
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")
237 # Creation of group : fashion mode
238 # if ids cells are known, this step is not to be made
240 m2 = meshU.buildPartOfMySelf(pt,True);
241 ret,tabIdCells = meshU.areCellsIncludedIn(m2,0)
244 # Definition of the name group
245 tabIdCells.setName("meshGroup")
247 # Passing MEDCoupling to MEDFile
248 fmeshU = MEDFileUMesh.New()
249 fmeshU.setName("Grid")
250 fmeshU.setDescription("IHopeToConvinceLastMEDMEMUsers")
251 myCoords = meshU.getCoords()
253 fmeshU.setCoords(myCoords)
254 print "**************************"
255 fmeshU.setMeshAtLevel(0,meshU)
256 print "**************************"
257 fmeshU.setGroupsAtLevel(0,[tabIdCells],False)
258 print "**************************"
260 medFileName = "MEDCoupling_Gridcube3D.med"
261 fmeshU.write(medFileName,2)