Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDMEM_SWIG / med_opsupp_test.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2012  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 is testing the merge and the intersection of
26 # several SUPPORTs
27 ############################################################################
28 #
29 from libMEDMEM_Swig import *
30 from random import *
31 import os
32 #
33 #befor running this script, please be sure about the path the file fileName
34 #
35 filePath=os.environ["MED_ROOT_DIR"]
36 filePath=os.path.join(filePath, "share", "salome", "resources", "med")
37
38 medFile = os.path.join( filePath, "pointe.med" )
39
40 def print_ord(i):
41     if i == 0:
42         return 'first'
43     elif i == 1:
44         return 'second'
45     elif i == 2:
46         return 'third'
47     else:
48         return `i`+'th'
49
50 md = MEDFILEBROWSER(medFile)
51 nbMeshes = md.getNumberOfMeshes()
52
53 print "The med file", medFile, "contains", nbMeshes, "mesh(es)"
54
55 mesh_name = md.getMeshName(0)
56 mesh = MESH(MED_DRIVER,medFile,mesh_name)
57 spaceDim = mesh.getSpaceDimension()
58 meshDim = mesh.getMeshDimension()
59 nbNodes = mesh.getNumberOfNodes()
60 print "The mesh",mesh_name,"is a",spaceDim,"D mesh on a",meshDim,"D geometry and has",nbNodes,"Nodes"
61
62 print ""
63 print "Mesh Families analysis:"
64 print ""
65
66 for entity in [MED_NODE,MED_CELL,MED_FACE,MED_EDGE]:
67     nbFam = mesh.getNumberOfFamilies(entity)
68     if (entity == MED_NODE) & (nbFam > 0):
69         print "This mesh has",nbFam,"Node Family(ies)"
70     elif (entity == MED_NODE) & (nbFam == 0):
71         print "This mesh has no Node Family(ies)"
72     elif (entity == MED_CELL) & (nbFam > 0):
73         print "This mesh has",nbFam,"Cell Family(ies)"
74     elif (entity == MED_CELL) & (nbFam == 0):
75         print "This mesh has no Cell Family(ies)"
76     elif (entity == MED_FACE) & (nbFam > 0):
77         print "This mesh has",nbFam,"Face Family(ies)"
78     elif (entity == MED_FACE) & (nbFam == 0):
79         print "This mesh has no Face Family(ies)"
80     elif (entity == MED_EDGE) & (nbFam > 0):
81         print "This mesh has",nbFam,"Edge Family(ies)"
82     elif (entity == MED_EDGE) & (nbFam == 0):
83         print "This mesh has no Edge Family(ies)"
84     print ""
85     
86     if nbFam > 0:
87         for j in range(nbFam):
88             family = mesh.getFamily(entity,j+1)
89             familyName = family.getName()
90             familyDescription = family.getDescription()
91             familyEntity = family.getEntity()
92             familyBool = family.isOnAllElements()
93             print "  -Name:",familyName
94             print "  -Description:",familyDescription
95             print "  -Entity:",familyEntity
96             familyIdentifier = family.getIdentifier()
97             nbOfAtt = family.getNumberOfAttributes()
98             print "  -Identifier:",familyIdentifier
99             print "  -Number Of Attributes:",nbOfAtt
100             attributesids = family.getAttributesIdentifiers()
101             attributesvals = family.getAttributesValues()
102             for k in range(nbOfAtt):
103                 print "    * Attributes:",attributesids[k],":",attributesvals[k],",",family.getAttributeDescription(k+1)
104             nbOfGrp = family.getNumberOfGroups()
105             print "  -Number Of Groups:",nbOfGrp
106             for k in range(nbOfGrp):
107                 print "    * Group:",family.getGroupName(k+1)
108             print "  -Entities list:"
109             if (familyBool):
110                 print "  -Is on all entities"
111             else:
112                 nbOfTypes = family.getNumberOfTypes()
113                 types = family.getTypes()
114                 print "  -Number Of Types:",nbOfTypes
115                 for k in range(nbOfTypes):
116                     type = types[k]
117                     nbOfElmtsOfType = family.getNumberOfElements(type)
118                     number = family.getNumber(type)
119                     print "    * Type",type
120                     print "    * Number",number[0:nbOfElmtsOfType]
121                 print ""
122
123 print "we are going to do some operation on Node families"
124 print "by definition families have no intersection"
125 print ""
126 entity = MED_NODE
127 familyNode1 = mesh.getFamily(entity,1)
128 familyNode2 = mesh.getFamily(entity,2)
129 familyNode3 = mesh.getFamily(entity,3)
130 familyNode4 = mesh.getFamily(entity,4)
131
132 listOfSupports = []
133 listOfSupports.append(familyNode1)
134 listOfSupports.append(familyNode2)
135
136 support = mesh.mergeSupports(listOfSupports)
137
138 supportName = support.getName()
139 supportDescription = support.getDescription()
140 supportMeshname = support.getMesh().getName()
141 supportEntity = support.getEntity()
142 supportBool = support.isOnAllElements()
143 print "  -Name:",supportName
144 print "  -Description:",supportDescription
145 print "  -Entity:",supportEntity
146 if (supportBool):
147     print "  -Is on all entities"
148 else:
149     nbOfTypes = support.getNumberOfTypes()
150     types = support.getTypes()
151     print "  -Number Of Types:",nbOfTypes
152     for k in range(nbOfTypes):
153         type = types[k]
154         nbOfElmtsOfType = support.getNumberOfElements(type)
155         number = support.getNumber(type)
156         print "    * Type",type
157         print "    * Number",number[0:nbOfElmtsOfType]
158     print ""
159
160 support = mesh.intersectSupports(listOfSupports)
161
162 supportName = support.getName()
163 supportDescription = support.getDescription()
164 supportMeshname = support.getMesh().getName()
165 supportEntity = support.getEntity()
166 supportBool = support.isOnAllElements()
167 print "  -Name:",supportName
168 print "  -Description:",supportDescription
169 print "  -Entity:",supportEntity
170 if (supportBool):
171     print "  -Is on all entities"
172 else:
173     nbOfTypes = support.getNumberOfTypes()
174     types = support.getTypes()
175     print "  -Number Of Types:",nbOfTypes
176     for k in range(nbOfTypes):
177         type = types[k]
178         nbOfElmtsOfType = support.getNumberOfElements(type)
179         number = support.getNumber(type)
180         print "    * Type",type
181         print "    * Number",number[0:nbOfElmtsOfType]
182     print ""
183
184 listOfSupports.append(familyNode3)
185
186 support = mesh.mergeSupports(listOfSupports)
187
188 supportName = support.getName()
189 supportDescription = support.getDescription()
190 supportMeshname = support.getMesh().getName()
191 supportEntity = support.getEntity()
192 supportBool = support.isOnAllElements()
193 print "  -Name:",supportName
194 print "  -Description:",supportDescription
195 print "  -Entity:",supportEntity
196 if (supportBool):
197     print "  -Is on all entities"
198 else:
199     nbOfTypes = support.getNumberOfTypes()
200     types = support.getTypes()
201     print "  -Number Of Types:",nbOfTypes
202     for k in range(nbOfTypes):
203         type = types[k]
204         nbOfElmtsOfType = support.getNumberOfElements(type)
205         number = support.getNumber(type)
206         print "    * Type",type
207         print "    * Number",number[0:nbOfElmtsOfType]
208     print ""
209
210 print "we are going to do some operation on Cell families"
211 print "by definition families have no intersection"
212 print ""
213 entity = MED_CELL
214 familyCell1 = mesh.getFamily(entity,1)
215 familyCell2 = mesh.getFamily(entity,2)
216 familyCell3 = mesh.getFamily(entity,3)
217
218 listOfSupports = []
219 listOfSupports.append(familyCell1)
220 listOfSupports.append(familyCell2)
221
222 support = mesh.mergeSupports(listOfSupports)
223
224 supportName = support.getName()
225 supportDescription = support.getDescription()
226 supportMeshname = support.getMesh().getName()
227 supportEntity = support.getEntity()
228 supportBool = support.isOnAllElements()
229 print "  -Name:",supportName
230 print "  -Description:",supportDescription
231 print "  -Entity:",supportEntity
232 if (supportBool):
233     print "  -Is on all entities"
234 else:
235     nbOfTypes = support.getNumberOfTypes()
236     types = support.getTypes()
237     print "  -Number Of Types:",nbOfTypes
238     for k in range(nbOfTypes):
239         type = types[k]
240         nbOfElmtsOfType = support.getNumberOfElements(type)
241         number = support.getNumber(type)
242         print "    * Type",type
243         print "    * Number",number[0:nbOfElmtsOfType]
244     print ""
245
246 support = mesh.intersectSupports(listOfSupports)
247
248 supportName = support.getName()
249 supportDescription = support.getDescription()
250 supportMeshname = support.getMesh().getName()
251 supportEntity = support.getEntity()
252 supportBool = support.isOnAllElements()
253 print "  -Name:",supportName
254 print "  -Description:",supportDescription
255 print "  -Entity:",supportEntity
256 if (supportBool):
257     print "  -Is on all entities"
258 else:
259     nbOfTypes = support.getNumberOfTypes()
260     types = support.getTypes()
261     print "  -Number Of Types:",nbOfTypes
262     for k in range(nbOfTypes):
263         type = types[k]
264         nbOfElmtsOfType = support.getNumberOfElements(type)
265         number = support.getNumber(type)
266         print "    * Type",type
267         print "    * Number",number[0:nbOfElmtsOfType]
268     print ""
269
270 listOfSupports.append(familyCell3)
271
272 support = mesh.mergeSupports(listOfSupports)
273
274 supportName = support.getName()
275 supportDescription = support.getDescription()
276 supportMeshname = support.getMesh().getName()
277 supportEntity = support.getEntity()
278 supportBool = support.isOnAllElements()
279 print "  -Name:",supportName
280 print "  -Description:",supportDescription
281 print "  -Entity:",supportEntity
282 if (supportBool):
283     print "  -Is on all entities"
284 else:
285     nbOfTypes = support.getNumberOfTypes()
286     types = support.getTypes()
287     print "  -Number Of Types:",nbOfTypes
288     for k in range(nbOfTypes):
289         type = types[k]
290         nbOfElmtsOfType = support.getNumberOfElements(type)
291         number = support.getNumber(type)
292         print "    * Type",type
293         print "    * Number",number[0:nbOfElmtsOfType]
294     print ""
295
296 print "Let's now do an example of union and intersection of two supports with a non empty intersection"
297 print ""
298 support1 = SUPPORT()
299 support1.setMesh(mesh)
300 support1.setName("Partial_Support_1")
301 support1Description = "Partial Support 1"
302 support1NbOfTypes = 3
303 support1TotNbElm = 8
304 support1GeoType = [MED_TETRA4,MED_HEXA8,MED_PYRA5]
305 support1NbOfElm = [4,2,2]
306 support1NbValue = [1,2,3,4,15,16,13,14]
307
308 support1.setpartial(support1Description,support1NbOfTypes,support1TotNbElm,
309                     support1GeoType,support1NbOfElm,support1NbValue)
310
311 supportName = support1.getName()
312 supportDescription = support1.getDescription()
313 supportMeshname = support1.getMesh().getName()
314 supportEntity = support1.getEntity()
315 supportBool = support1.isOnAllElements()
316 print "  -Name:",supportName
317 print "  -Description:",supportDescription
318 print "  -Entity:",supportEntity
319 if (supportBool):
320     print "  -Is on all entities"
321 else:
322     nbOfTypes = support1.getNumberOfTypes()
323     types = support1.getTypes()
324     print "  -Number Of Types:",nbOfTypes
325     for k in range(nbOfTypes):
326         type = types[k]
327         nbOfElmtsOfType = support1.getNumberOfElements(type)
328         number = support1.getNumber(type)
329         print "    * Type",type
330         print "    * Number",number[0:nbOfElmtsOfType]
331     print ""
332
333 support2 = SUPPORT()
334 support2.setMesh(mesh)
335 support2.setName("Partial_Support_2")
336 support2Description = "Partial Support 2"
337 support2NbOfTypes = 3
338 support2TotNbElm = 8
339 support2GeoType = [MED_TETRA4,MED_HEXA8,MED_PYRA5]
340 support2NbOfElm = [6,1,1]
341 support2NbValue = [3,4,9,10,11,12,16,14]
342
343 support2.setpartial(support2Description,support2NbOfTypes,support2TotNbElm,
344                     support2GeoType,support2NbOfElm,support2NbValue)
345
346 supportName = support2.getName()
347 supportDescription = support2.getDescription()
348 supportMeshname = support2.getMesh().getName()
349 supportEntity = support2.getEntity()
350 supportBool = support2.isOnAllElements()
351 print "  -Name:",supportName
352 print "  -Description:",supportDescription
353 print "  -Entity:",supportEntity
354 if (supportBool):
355     print "  -Is on all entities"
356 else:
357     nbOfTypes = support2.getNumberOfTypes()
358     types = support2.getTypes()
359     print "  -Number Of Types:",nbOfTypes
360     for k in range(nbOfTypes):
361         type = types[k]
362         nbOfElmtsOfType = support2.getNumberOfElements(type)
363         number = support2.getNumber(type)
364         print "    * Type",type
365         print "    * Number",number[0:nbOfElmtsOfType]
366     print ""
367
368 listOfSupports = []
369 listOfSupports.append(support1)
370 listOfSupports.append(support2)
371
372 print "The union"
373 print ""
374
375 support = mesh.mergeSupports(listOfSupports)
376
377 supportName = support.getName()
378 supportDescription = support.getDescription()
379 supportMeshname = support.getMesh().getName()
380 supportEntity = support.getEntity()
381 supportBool = support.isOnAllElements()
382 print "  -Name:",supportName
383 print "  -Description:",supportDescription
384 print "  -Entity:",supportEntity
385 if (supportBool):
386     print "  -Is on all entities"
387 else:
388     nbOfTypes = support.getNumberOfTypes()
389     types = support.getTypes()
390     print "  -Number Of Types:",nbOfTypes
391     for k in range(nbOfTypes):
392         type = types[k]
393         nbOfElmtsOfType = support.getNumberOfElements(type)
394         number = support.getNumber(type)
395         print "    * Type",type
396         print "    * Number",number[0:nbOfElmtsOfType]
397     print ""
398
399 print "The intersection"
400 print ""
401
402 support = mesh.intersectSupports(listOfSupports)
403
404 supportName = support.getName()
405 supportDescription = support.getDescription()
406 supportMeshname = support.getMesh().getName()
407 supportEntity = support.getEntity()
408 supportBool = support.isOnAllElements()
409 print "  -Name:",supportName
410 print "  -Description:",supportDescription
411 print "  -Entity:",supportEntity
412 if (supportBool):
413     print "  -Is on all entities"
414 else:
415     nbOfTypes = support.getNumberOfTypes()
416     types = support.getTypes()
417     print "  -Number Of Types:",nbOfTypes
418     for k in range(nbOfTypes):
419         type = types[k]
420         nbOfElmtsOfType = support.getNumberOfElements(type)
421         number = support.getNumber(type)
422         print "    * Type",type
423         print "    * Number",number[0:nbOfElmtsOfType]
424     print ""
425
426 print "END of the Pyhton script ..... Ctrl D to exit"