Salome HOME
Merge from V6_main 01/04/2013
[modules/med.git] / doc / MEDMEM / MESHINGexample.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23
24 ###################################################################################
25 # This Python script uses the wrapped C++ class MESHING to buid a mesh from only
26 # primitive data like coordinates (Pythoin double array) and connectivity (Python
27 # integer arrays). It is the Python equivalent of the C++ program
28 # test_MEDMEM_Meshing.cxx in the ../MEDMEM directory of the SALOME distribution
29 ###################################################################################
30 #
31 from libMEDMEM_Swig import *
32
33 # files name to save the generated MESH(ING) in different format
34 # Med V2.1 Med V2.2 and vtk
35
36 med21FileName = "toto21.med"
37
38 med22FileName = "toto22.med"
39
40 vtkFileName = "toto.vtk"
41
42 myMeshing = MESHING()
43
44 myMeshing.setName("meshing")
45
46 # definition of the coordinates
47
48 spaceDimension = 3
49
50 numberOfNodes = 19
51
52 coordinates = [
53     0.0, 0.0, 0.0  ,
54     0.0, 0.0, 1.0  ,
55     2.0, 0.0, 1.0  ,
56     0.0, 2.0, 1.0  ,
57     -2.0, 0.0, 1.0 ,
58     0.0, -2.0, 1.0 ,
59     1.0, 1.0, 2.0  ,
60     -1.0, 1.0, 2.0 ,
61     -1.0, -1.0, 2.0,
62     1.0, -1.0, 2.0 ,
63     1.0, 1.0, 3.0  ,
64     -1.0, 1.0, 3.0 ,
65     -1.0, -1.0, 3.0,
66     1.0, -1.0, 3.0 ,
67     1.0, 1.0, 4.0  ,
68     -1.0, 1.0, 4.0 ,
69     -1.0, -1.0, 4.0,
70     1.0, -1.0, 4.0 ,
71     0.0, 0.0, 5.0]
72
73 myMeshing.setCoordinates(spaceDimension,numberOfNodes,coordinates,"CARTESIAN",MED_FULL_INTERLACE)
74
75 for i in range(spaceDimension):
76     unit = "cm      "
77     if (i == 0):
78         name = "X       "
79     elif (i == 1):
80         name = "Y       "
81     elif (i == 2):
82         name = "Z       "
83
84     myMeshing.setCoordinateName(name,i)
85     myMeshing.setCoordinateUnit(unit,i)
86
87 # definition of connectivities
88 # cell part
89
90 numberOfTypes = 3
91 entity = MED_CELL
92
93 types = []
94 numberOfElements = []
95
96 types.append(MED_TETRA4)
97 numberOfElements.append(12)
98
99 types.append(MED_PYRA5)
100 numberOfElements.append(2)
101
102 types.append(MED_HEXA8)
103 numberOfElements.append(2)
104
105 myMeshing.setNumberOfTypes(numberOfTypes,entity)
106 myMeshing.setTypes(types,entity)
107 myMeshing.setNumberOfElements(numberOfElements,entity)
108
109 connectivityTetra = [
110     1,2,3,6 ,
111     1,2,4,3 ,
112     1,2,5,4 ,
113     1,2,6,5 ,
114     2,7,4,3 ,
115     2,8,5,4 ,
116     2,9,6,5 ,
117     2,10,3,6,
118     2,7,3,10,
119     2,8,4,7 ,
120     2,9,5,8 ,
121     2,10,6,9]
122
123 myMeshing.setConnectivity(entity,types[0],connectivityTetra)
124
125 connectivityPyra = [
126     7,8,9,10,2,
127     15,18,17,16,19]
128
129 myMeshing.setConnectivity(entity,types[1],connectivityPyra)
130
131 connectivityHexa = [
132     11,12,13,14,7,8,9,10,
133     15,16,17,18,11,12,13,14]
134
135 myMeshing.setConnectivity(entity,types[2],connectivityPyra)
136
137 # face part
138
139 numberOfTypes = 2
140 entity = MED_FACE
141
142 types = []
143 numberOfElements = []
144
145 types.append(MED_TRIA3)
146 numberOfElements.append(4)
147
148 types.append(MED_QUAD4)
149 numberOfElements.append(4)
150
151 myMeshing.setNumberOfTypes(numberOfTypes,entity)
152 myMeshing.setTypes(types,entity)
153 myMeshing.setNumberOfElements(numberOfElements,entity)
154
155 connectivityTria = [
156     1,4,3,
157     1,5,4,
158     1,6,5,
159     1,3,6]
160
161 myMeshing.setConnectivity(entity,types[0],connectivityPyra)
162
163 connectivityQuad = [
164     7,8,9,10   ,
165     11,12,13,14,
166     11,7,8,12  ,
167     12,8,9,13]
168
169 myMeshing.setConnectivity(entity,types[1],connectivityQuad)
170
171 # edge part
172
173 # adding GROUPs
174 # on Node
175
176 myGroup = GROUP()
177 myGroup.setName("SomeNodes")
178 myGroup.setMesh(myMeshing)
179 myGroup.setEntity(MED_NODE)
180 myGroup.setNumberOfGeometricType(1)
181
182 myTypes = [MED_NONE]
183 myGroup.setGeometricType(myTypes)
184
185 myNumberOfElements = [4]
186 myGroup.setNumberOfElements(myNumberOfElements)
187
188 index = [1,5]
189 values = [1,4,5,7]
190 myGroup.setNumber(index,values)
191
192 myMeshing.addGroup(myGroup)
193
194 myGroup = GROUP()
195 myGroup.setName("OtherNodes")
196 myGroup.setMesh(myMeshing)
197 myGroup.setEntity(MED_NODE)
198 myGroup.setNumberOfGeometricType(1)
199
200 myTypes = [MED_NONE]
201 myGroup.setGeometricType(myTypes)
202
203 myNumberOfElements = [3]
204 myGroup.setNumberOfElements(myNumberOfElements)
205
206 index = [1,4]
207 values = [2,3,6]
208 myGroup.setNumber(index,values)
209
210 myMeshing.addGroup(myGroup)
211
212 # on Cell
213
214 myGroup = GROUP()
215 myGroup.setName("SomeCells")
216 myGroup.setMesh(myMeshing)
217 myGroup.setEntity(MED_CELL)
218 myGroup.setNumberOfGeometricType(3)
219
220 myTypes = [MED_TETRA4,MED_PYRA5,MED_HEXA8]
221 myGroup.setGeometricType(myTypes)
222
223 myNumberOfElements = [4,1,2]
224 myGroup.setNumberOfElements(myNumberOfElements)
225
226 index = [1,5,6,8]
227 values = [
228     2,7,8,12,
229     13,
230     15,16
231     ]
232 myGroup.setNumber(index,values)
233
234 myMeshing.addGroup(myGroup)
235
236 myGroup = GROUP()
237 myGroup.setName("OtherCells")
238 myGroup.setMesh(myMeshing)
239 myGroup.setEntity(MED_CELL)
240 myGroup.setNumberOfGeometricType(2)
241
242 myTypes = [MED_TETRA4,MED_PYRA5]
243 myGroup.setGeometricType(myTypes)
244
245 myNumberOfElements = [4,1]
246 myGroup.setNumberOfElements(myNumberOfElements)
247
248 index = [1,5,6]
249 values = [
250     3,4,5,9,
251     14
252     ]
253 myGroup.setNumber(index,values)
254
255 myMeshing.addGroup(myGroup)
256
257 # on Face
258
259 myGroup = GROUP()
260 myGroup.setName("SomeFaces")
261 myGroup.setMesh(myMeshing)
262 myGroup.setEntity(MED_FACE)
263 myGroup.setNumberOfGeometricType(2)
264
265 myTypes = [MED_TRIA3,MED_QUAD4]
266 myGroup.setGeometricType(myTypes)
267
268 myNumberOfElements = [2,3]
269 myGroup.setNumberOfElements(myNumberOfElements)
270
271 index = [1,3,6]
272 values = [
273     2,4,
274     5,6,8
275     ]
276 myGroup.setNumber(index,values)
277
278 myMeshing.addGroup(myGroup)
279
280 myGroup = GROUP()
281 myGroup.setName("OtherFaces")
282 myGroup.setMesh(myMeshing)
283 myGroup.setEntity(MED_FACE)
284 myGroup.setNumberOfGeometricType(1)
285
286 myTypes = [MED_TRIA3]
287 myGroup.setGeometricType(myTypes)
288
289 myNumberOfElements = [2]
290 myGroup.setNumberOfElements(myNumberOfElements)
291
292 index = [1,3]
293 values = [
294     1,3
295     ]
296 myGroup.setNumber(index,values)
297
298 myMeshing.addGroup(myGroup)
299
300 # saving of the generated mesh in MED and VTK format
301
302 myMeshing.write(MED_DRIVER,med22FileName)
303
304 myMeshing.write(VTK_DRIVER,vtkFileName)
305
306 # we build now 8 fields : 4 fields double (integer) :
307 #                         2 fields on nodes (cells) :
308 #                         1 scalar (vector)
309
310 supportOnNodes = myMeshing.getSupportOnAll(MED_NODE)
311 numberOfNodes = supportOnNodes.getNumberOfElements(MED_ALL_ELEMENTS)
312
313 supportOnCells = myMeshing.getSupportOnAll(MED_CELL)
314 numberOfCells = supportOnCells.getNumberOfElements(MED_ALL_ELEMENTS)
315
316 fieldDoubleScalarOnNodes = FIELDDOUBLE(supportOnNodes,1)
317 fieldDoubleScalarOnNodes.setName("fieldScalarDoubleNode")
318 fieldDoubleScalarOnNodes.setIterationNumber(-1)
319 fieldDoubleScalarOnNodes.setOrderNumber(-1)
320 fieldDoubleScalarOnNodes.setTime(0.0)
321
322 fieldDoubleScalarOnNodes.setComponentName(1,"Vx")
323 fieldDoubleScalarOnNodes.setComponentDescription(1,"comp1")
324 fieldDoubleScalarOnNodes.setMEDComponentUnit(1,"unit1")
325
326 fieldDoubleVectorOnNodes = FIELDDOUBLE(supportOnNodes,spaceDimension)
327 fieldDoubleVectorOnNodes.setName("fieldVectorDoubleNode")
328 fieldDoubleVectorOnNodes.setIterationNumber(-1)
329 fieldDoubleVectorOnNodes.setOrderNumber(-1)
330 fieldDoubleVectorOnNodes.setTime(0.0)
331
332 fieldDoubleVectorOnNodes.setComponentName(1,"Vx")
333 fieldDoubleVectorOnNodes.setComponentDescription(1,"comp1")
334 fieldDoubleVectorOnNodes.setMEDComponentUnit(1,"unit1")
335 fieldDoubleVectorOnNodes.setComponentName(2,"Vy")
336 fieldDoubleVectorOnNodes.setComponentDescription(2,"comp2")
337 fieldDoubleVectorOnNodes.setMEDComponentUnit(2,"unit2")
338 fieldDoubleVectorOnNodes.setComponentName(3,"Vz")
339 fieldDoubleVectorOnNodes.setComponentDescription(3,"comp3")
340 fieldDoubleVectorOnNodes.setMEDComponentUnit(3,"unit3")
341
342 fieldDoubleScalarOnCells = FIELDDOUBLE(supportOnCells,1)
343 fieldDoubleScalarOnCells.setName("fieldScalarDoubleCell")
344 fieldDoubleScalarOnCells.setIterationNumber(-1)
345 fieldDoubleScalarOnCells.setOrderNumber(-1)
346 fieldDoubleScalarOnCells.setTime(0.0)
347
348 fieldDoubleScalarOnCells.setComponentName(1,"Vx")
349 fieldDoubleScalarOnCells.setComponentDescription(1,"comp1")
350 fieldDoubleScalarOnCells.setMEDComponentUnit(1,"unit1")
351
352 fieldDoubleVectorOnCells = FIELDDOUBLE(supportOnCells,spaceDimension)
353 fieldDoubleVectorOnCells.setName("fieldVectorrDoubleCell")
354 fieldDoubleVectorOnCells.setIterationNumber(-1)
355 fieldDoubleVectorOnCells.setOrderNumber(-1)
356 fieldDoubleVectorOnCells.setTime(0.0)
357
358 fieldDoubleVectorOnCells.setComponentName(1,"Vx")
359 fieldDoubleVectorOnCells.setComponentDescription(1,"comp1")
360 fieldDoubleVectorOnCells.setMEDComponentUnit(1,"unit1")
361 fieldDoubleVectorOnCells.setComponentName(2,"Vy")
362 fieldDoubleVectorOnCells.setComponentDescription(2,"comp2")
363 fieldDoubleVectorOnCells.setMEDComponentUnit(2,"unit2")
364 fieldDoubleVectorOnCells.setComponentName(3,"Vz")
365 fieldDoubleVectorOnCells.setComponentDescription(3,"comp3")
366 fieldDoubleVectorOnCells.setMEDComponentUnit(3,"unit3")
367
368 fieldIntScalarOnNodes = FIELDINT(supportOnNodes,1)
369 fieldIntScalarOnNodes.setName("fieldScalarIntNode")
370 fieldIntScalarOnNodes.setIterationNumber(-1)
371 fieldIntScalarOnNodes.setOrderNumber(-1)
372 fieldIntScalarOnNodes.setTime(0.0)
373
374 fieldIntScalarOnNodes.setComponentName(1,"Vx")
375 fieldIntScalarOnNodes.setComponentDescription(1,"comp1")
376 fieldIntScalarOnNodes.setMEDComponentUnit(1,"unit1")
377
378 fieldIntVectorOnNodes = FIELDINT(supportOnNodes,spaceDimension)
379 fieldIntVectorOnNodes.setName("fieldVectorIntNode")
380 fieldIntVectorOnNodes.setIterationNumber(-1)
381 fieldIntVectorOnNodes.setOrderNumber(-1)
382 fieldIntVectorOnNodes.setTime(0.0)
383
384 fieldIntVectorOnNodes.setComponentName(1,"Vx")
385 fieldIntVectorOnNodes.setComponentDescription(1,"comp1")
386 fieldIntVectorOnNodes.setMEDComponentUnit(1,"unit1")
387 fieldIntVectorOnNodes.setComponentName(2,"Vy")
388 fieldIntVectorOnNodes.setComponentDescription(2,"comp2")
389 fieldIntVectorOnNodes.setMEDComponentUnit(2,"unit2")
390 fieldIntVectorOnNodes.setComponentName(3,"Vz")
391 fieldIntVectorOnNodes.setComponentDescription(3,"comp3")
392 fieldIntVectorOnNodes.setMEDComponentUnit(3,"unit3")
393
394 fieldIntScalarOnCells = FIELDINT(supportOnCells,1)
395 fieldIntScalarOnCells.setName("fieldScalarIntCell")
396 fieldIntScalarOnCells.setIterationNumber(-1)
397 fieldIntScalarOnCells.setOrderNumber(-1)
398 fieldIntScalarOnCells.setTime(0.0)
399
400 fieldIntScalarOnCells.setComponentName(1,"Vx")
401 fieldIntScalarOnCells.setComponentDescription(1,"comp1")
402 fieldIntScalarOnCells.setMEDComponentUnit(1,"unit1")
403
404 fieldIntVectorOnCells = FIELDINT(supportOnCells,spaceDimension)
405 fieldIntVectorOnCells.setName("fieldVectorrIntCell")
406 fieldIntVectorOnCells.setIterationNumber(-1)
407 fieldIntVectorOnCells.setOrderNumber(-1)
408 fieldIntVectorOnCells.setTime(0.0)
409
410 fieldIntVectorOnCells.setComponentName(1,"Vx")
411 fieldIntVectorOnCells.setComponentDescription(1,"comp1")
412 fieldIntVectorOnCells.setMEDComponentUnit(1,"unit1")
413 fieldIntVectorOnCells.setComponentName(2,"Vy")
414 fieldIntVectorOnCells.setComponentDescription(2,"comp2")
415 fieldIntVectorOnCells.setMEDComponentUnit(2,"unit2")
416 fieldIntVectorOnCells.setComponentName(3,"Vz")
417 fieldIntVectorOnCells.setComponentDescription(3,"comp3")
418 fieldIntVectorOnCells.setMEDComponentUnit(3,"unit3")
419
420 for i in range(numberOfNodes):
421     valueInt1 = i+1
422     valueInt2 = i+2
423     valueInt3 = i+3
424     valueDbl1 = valueInt1*0.1
425     valueDbl2 = valueInt2*0.1
426     valueDbl3 = valueInt3*0.1
427     fieldDoubleScalarOnNodes.setValueIJ(i+1,1,valueDbl1)
428
429     fieldIntScalarOnNodes.setValueIJ(i+1,1,valueInt1)
430
431     fieldDoubleVectorOnNodes.setValueIJ(i+1,1,valueDbl1)
432     fieldDoubleVectorOnNodes.setValueIJ(i+1,2,valueDbl2)
433     fieldDoubleVectorOnNodes.setValueIJ(i+1,3,valueDbl3)
434
435     fieldIntVectorOnNodes.setValueIJ(i+1,1,valueInt1)
436     fieldIntVectorOnNodes.setValueIJ(i+1,2,valueInt2)
437     fieldIntVectorOnNodes.setValueIJ(i+1,3,valueInt3)
438
439 for i in range(numberOfCells):
440     valueInt1 = i+1
441     valueInt2 = i+2
442     valueInt3 = i+3
443     valueDbl1 = valueInt1*0.1
444     valueDbl2 = valueInt2*0.1
445     valueDbl3 = valueInt3*0.1
446     fieldDoubleScalarOnCells.setValueIJ(i+1,1,valueDbl1)
447
448     fieldIntScalarOnCells.setValueIJ(i+1,1,valueInt1)
449
450     fieldDoubleVectorOnCells.setValueIJ(i+1,1,valueDbl1)
451     fieldDoubleVectorOnCells.setValueIJ(i+1,2,valueDbl2)
452     fieldDoubleVectorOnCells.setValueIJ(i+1,3,valueDbl3)
453
454     fieldIntVectorOnCells.setValueIJ(i+1,1,valueInt1)
455     fieldIntVectorOnCells.setValueIJ(i+1,2,valueInt2)
456     fieldIntVectorOnCells.setValueIJ(i+1,3,valueInt3)
457
458 fieldIntScalarOnNodes.write(MED_DRIVER,med21FileName)
459 fieldDoubleVectorOnNodes.write(MED_DRIVER,med21FileName)
460 fieldIntVectorOnNodes.write(MED_DRIVER,med21FileName)
461 fieldDoubleScalarOnCells.write(MED_DRIVER,med21FileName)
462 fieldIntScalarOnCells.write(MED_DRIVER,med21FileName)
463 fieldDoubleVectorOnCells.write(MED_DRIVER,med21FileName)
464 fieldIntVectorOnCells.write(MED_DRIVER,med21FileName)
465
466
467 idVtk = fieldDoubleScalarOnNodes.addDriver(VTK_DRIVER,vtkFileName,fieldDoubleScalarOnNodes.getName())
468 fieldDoubleScalarOnNodes.writeAppend(idVtk)
469
470 idVtk = fieldIntScalarOnNodes.addDriver(VTK_DRIVER,vtkFileName,fieldIntScalarOnNodes.getName())
471 fieldIntScalarOnNodes.writeAppend(idVtk)
472
473 idVtk = fieldDoubleVectorOnNodes.addDriver(VTK_DRIVER,vtkFileName,fieldDoubleVectorOnNodes.getName())
474 fieldDoubleVectorOnNodes.writeAppend(idVtk)
475
476 idVtk = fieldIntVectorOnNodes.addDriver(VTK_DRIVER,vtkFileName,fieldIntVectorOnNodes.getName())
477 fieldIntVectorOnNodes.writeAppend(idVtk)
478
479 idVtk = fieldDoubleScalarOnCells.addDriver(VTK_DRIVER,vtkFileName,fieldDoubleScalarOnCells.getName())
480 fieldDoubleScalarOnCells.writeAppend(idVtk)
481
482 idVtk = fieldIntScalarOnCells.addDriver(VTK_DRIVER,vtkFileName,fieldIntScalarOnCells.getName())
483 fieldIntScalarOnCells.writeAppend(idVtk)
484
485 idVtk = fieldDoubleVectorOnCells.addDriver(VTK_DRIVER,vtkFileName,fieldDoubleVectorOnCells.getName())
486 fieldDoubleVectorOnCells.writeAppend(idVtk)
487
488 idVtk = fieldIntVectorOnCells.addDriver(VTK_DRIVER,vtkFileName,fieldIntVectorOnCells.getName())
489 fieldIntVectorOnCells.writeAppend(idVtk)