2 .. _python_testMEDCouplingcube_solution:
9 import medcoupling as mc
12 # Definition of environment variables
16 nbOfCells = (N-1)*(N-1)*(N-1)
17 nbOfCells2D = (N-1)*(N-1)
19 print("1 ********************")
20 # Initialisation of coordinates
25 coordinates.append(float(i))
26 coordinates.append(float(j))
27 coordinates.append(float(k))
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")
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
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))
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)])
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()
73 print("6 ********************")
74 # Extraction of surfacic meshing
77 nodes = mesh.findNodesOnPlane(pt,vec,1e-12)
78 mesh2D = mesh.buildFacePartOfMySelfNode(nodes,True)
80 mesh2D.setName("3Dcube")
81 mesh2D.checkConsistencyLight()
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)
90 field.setName("field")
91 field.setNature(ExtensiveMaximum)
93 # Computing and setting field values
94 myCoords=mc.DataArrayDouble.New()
96 bar = mesh.computeCellCenterOfMass()
97 print(bar.getNbOfElems())
98 for i in range(nbOfCells):
102 d = sqrt(x*x+y*y+z*z)
104 #f.setValueIJ(i+1,1,sin(d))
105 sampleTab.append(sinus)
107 myCoords.setValues(sampleTab,nbOfCells,1)
108 field.setArray(myCoords)
110 fBF = mc.MEDCouplingFieldDouble.New(ON_CELLS)
112 fBF.setName("fieldBottomFace")
113 fBF.setNature(ExtensiveMaximum)
115 myCoords2D=mc.DataArrayDouble.New()
117 for i in range(nbOfCells2D):
118 sampleTab.append(Cval)
119 myCoords2D.setValues(sampleTab,nbOfCells2D,1)
120 fBF.setArray(myCoords2D)
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
127 mc.WriteUMeshes(medFileName,meshes,True);
128 mc.WriteField(medFileName,field,False)
129 mc.WriteField(medFileName,fBF,False)
134 import medcoupling as mc
140 NbCell2D = (N-1)*(N-1)
141 NbCell3D = NbCell2D*(N-1)
143 NbNode3D = NbNode2D*N
145 # Creation of a extruded meshing
146 # input : a 2D meshing and a 1D meshing
147 # Creation of 2D meshing
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)
157 m1 = mc.MEDCouplingUMesh.New()
158 m1.setMeshDimension(MeshDim2D)
159 m1.allocateCells(NbCell2D)
160 m1.setCoords(myCoords)
161 m1.setName("2D_Support")
163 for i in range(NbCell2D):
164 m1.insertNextCell(mc.NORM_QUAD4,4,Connectivities[4*i:4*(i+1)])
165 m1.changeSpaceDimension(3)
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)
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)
181 # Construction of extruded meshing
184 m2.rotate(center,vector,pi/2.)
185 m3 = m1.buildExtrudedMesh(m2,0)
186 m3.setName("Extrusion")
188 # Construction of group : old fashion mode
190 meshGroup=m3.buildPartOfMySelf(part,True);
191 meshGroup.setName("meshGroup");
193 medFileName = "MEDCoupling_Extrudedcube3D.med"
194 mc.WriteUMeshesPartition(medFileName,"Extrusion",[m3,meshGroup],True)
199 import medcoupling as mc
205 NbCell2D = (N-1)*(N-1)
206 NbCell3D = NbCell2D*(N-1)
208 NbNode3D = NbNode2D*N
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")
228 # Creation of group : fashion mode
229 # if ids cells are known, this step is not to be made
231 m2 = meshU.buildPartOfMySelf(pt,True);
232 ret,tabIdCells = meshU.areCellsIncludedIn(m2,0)
235 # Definition of the name group
236 tabIdCells.setName("meshGroup")
238 # Passing mc.MEDCoupling to mc.MEDFile
239 fmeshU = mc.MEDFileUMesh.New()
240 fmeshU.setName("Grid")
241 fmeshU.setDescription("IHopeToConvinceLastMEDMEMUsers")
242 myCoords = meshU.getCoords()
244 fmeshU.setCoords(myCoords)
245 print("**************************")
246 fmeshU.setMeshAtLevel(0,meshU)
247 print("**************************")
248 fmeshU.setGroupsAtLevel(0,[tabIdCells],False)
249 print("**************************")
251 medFileName = "MEDCoupling_Gridcube3D.med"
252 fmeshU.write(medFileName,2)