1 # Copyright (C) 2005 OPEN CASCADE, CEA, EDF R&D, LEG
2 # PRINCIPIA R&D, EADS CCR, Lip6, BV, CEDRAT
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License.
8 # This library is distributed in the hope that it will be useful
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 ############################################################################
21 # This file provides utilities to dump content of MEDMEM objects: mesh,
22 # field, group, family, nodal connectivity, node coordinates.
24 ############################################################################
26 from libMEDMEM_Swig import *
30 theEntityName = { MED_CELL :"CELL",
34 MED_ALL_ENTITIES:"ALL_ENTITIES" }
36 theTypeName = {MED_NONE :"NONE",
48 MED_TETRA10 :"TETRA10",
50 MED_PENTA15 :"PENTA15",
52 MED_POLYGON :"POLYGON",
53 MED_POLYHEDRA :"POLYHEDRA",
54 MED_ALL_ELEMENTS:"ALL_ELEMENTS"}
56 medModeSwitch = { 0:"FULL_INTERLACE",
58 3:"UNDEFINED_INTERLACE" };
60 med_type_champ = { 6 : "REEL64",
63 0 : "UNDEFINED_TYPE" } ;
73 def _showNodalConnectivity(mesh,entity,type,elems,tablevel,showOnly=SHOW_ALL):
74 if debugShowConn: print "ELEMENS:",elems
76 tab2 = tab*(tablevel+1)
77 typeName = theTypeName[type]
80 elems = elems[:showOnly]
82 if type == MED_POLYGON:
83 connectivity = mesh.getPolygonsConnectivity(MED_NODAL,entity)
84 index = mesh.getPolygonsConnectivityIndex(MED_NODAL,entity)
85 if debugShowConn: print "CONN:",connectivity,"\nIND:",index
86 d = mesh.getNumberOfElements(entity,MED_ALL_ELEMENTS)
89 print tab1,typeName,i,":",connectivity[ index[j-1]-1 : index[j]-1 ]
92 elif type == MED_POLYHEDRA:
93 connectivity = mesh.getPolyhedronConnectivity(MED_NODAL)
94 fIndex = mesh.getPolyhedronFacesIndex()
95 index = mesh.getPolyhedronIndex(MED_NODAL)
96 d = mesh.getNumberOfElements(entity,MED_ALL_ELEMENTS)
97 if debugShowConn: print "CONN:",connectivity,"\nIND:",index,"\nFIND:",fIndex
100 print tab1,typeName, i
101 iF1, iF2 = index[ j-1 ]-1, index[ j ]-1
102 for f in range( iF2 - iF1 ):
103 iN1, iN2 = fIndex[ iF1+f ]-1, fIndex[ iF1+f+1 ]-1
104 print tab2,"Face",(f+1),":",connectivity[ iN1 : iN2 ]
110 types = mesh.getTypes( entity )
113 elemShift += mesh.getNumberOfElements(entity,t)
117 connectivity = mesh.getConnectivity(MED_FULL_INTERLACE, MED_NODAL, entity,MED_ALL_ELEMENTS)
118 index = mesh.getConnectivityIndex(MED_FULL_INTERLACE, entity)
119 if debugShowConn: print "CONN: %s\n INX: %s" % (connectivity,index)
120 nbNodesPerCell = type%100
124 print tab1,typeName,i,":",connectivity[n:n+nbNodesPerCell]
127 nbSkip = nbElem - nbShow
129 print tab1,"...",nbSkip,"elements not shown"
134 def _showSupport(support, tablevel,showElems=0):
135 tab1 = tab*(tablevel+0)
136 tab2 = tab*(tablevel+1)
137 tab3 = tab*(tablevel+3)
138 entity = support.getEntity()
139 types = support.getTypes()
140 nbOfTypes = support.getNumberOfTypes()
141 onAll = support.isOnAllElements()
142 print tab1,"-Entity:",theEntityName[entity]
143 print tab1,"-Types :",types
144 print tab1,"-Elements"
146 print tab2,"<< Is on all elements >>"
149 nbOfElmtsOfType = support.getNumberOfElements(type)
150 number = support.getNumber(type)
152 print tab2,"* Type:",theTypeName[type]
153 print tab3,". Nb elements:",nbOfElmtsOfType
154 print tab3,". Numbers:",number[:min(100,nbOfElmtsOfType)],
155 if nbOfElmtsOfType > 100:
156 print "...skip", nbOfElmtsOfType-100
160 if entity != MED_NODE and showElems:
161 print tab3,". Nodal connectivity"
162 _showNodalConnectivity(support.getMesh(),entity,type,number,tablevel+4,showElems)
169 ## Dump i-th family of given entity in mesh.
170 ## Optionally dump nodal connectivity of <showElems> first elements.
171 ## Use showElems=SHOW_ALL to dump connectivity of all elements.
173 def ShowFamily(mesh, entity, i, showElems=0):
174 family = mesh.getFamily(entity,i)
175 familyName = family.getName()
176 familyDescription = family.getDescription()
177 entity = family.getEntity()
178 familyBool = family.isOnAllElements()
179 print "\nFAMILY", i, "on", theEntityName[entity]
180 print tab*1,"-Name :",familyName
181 print tab*1,"-Description:",familyDescription
182 familyIdentifier = family.getIdentifier()
183 nbOfAtt = family.getNumberOfAttributes()
184 print tab*1,"-Identifier :",familyIdentifier
185 print tab*1,"-Attributes"
186 attributesids = family.getAttributesIdentifiers()
187 attributesvals = family.getAttributesValues()
188 for k in range(nbOfAtt):
189 print tab*2,"* Id :",attributesids[k]
190 print tab*2,"* Value :",attributesvals[k]
191 print tab*2,"* Description:",family.getAttributeDescription(k+1)
193 nbOfGrp = family.getNumberOfGroups()
194 print tab*1,"-Nb Of Groups:",nbOfGrp
195 print tab*1,"-Groups:"
196 for k in range(nbOfGrp):
197 print tab*2,k+1,":",family.getGroupName(k+1)
199 _showSupport(family,1,showElems)
202 ## Dump all families in mesh.
203 ## Optionally dump nodal connectivity of <showElems> first elements of each family.
204 ## Use showElems=SHOW_ALL to dump connectivity of all elements.
206 def ShowFamilies(mesh, showElems=0):
207 line = "families in mesh <" + mesh.getName() + ">"
208 print "\n",line,"\n","-"*len(line)
209 for entity in [MED_NODE,MED_CELL,MED_FACE,MED_EDGE]:
210 nbFam = mesh.getNumberOfFamilies(entity)
211 for i in range(nbFam):
212 ShowFamily(mesh,entity,i+1,showElems)
217 ## Optionally dump nodal connectivity of <showElems> first elements of the group.
218 ## Use showElems=SHOW_ALL to dump connectivity of all elements.
220 def ShowGroup(group, showElems):
221 groupName = group.getName()
222 groupDescription = group.getDescription()
223 nbOfFam = group.getNumberOfFamilies()
224 print "\nGROUP on",theEntityName[group.getEntity()]
225 print tab*1,"-Name :",groupName
226 print tab*1,"-Description :",groupDescription
227 print tab*1,"-Nb Of Families:",nbOfFam
228 print tab*1,"-Families"
229 for k in range(nbOfFam):
230 print tab*2,k+1,":",group.getFamily(k+1).getName()
232 _showSupport(group,1,showElems)
235 ## Dump all GROUP's in mesh.
236 ## Optionally dump nodal connectivity of <showElems> first elements of each group.
237 ## Use showElems=SHOW_ALL to dump connectivity of all elements.
239 def ShowGroups(mesh, showElems=0):
240 line = "groups in mesh <" + mesh.getName() + ">"
241 print "\n",line,"\n","-"*len(line)
242 for entity in [MED_NODE,MED_CELL,MED_FACE,MED_EDGE]:
243 nbGrp = mesh.getNumberOfGroups(entity)
245 for j in range(nbGrp):
246 group = mesh.getGroup(entity,j+1)
247 ShowGroup(group,showElems)
252 ## Dump mesh general information.
253 ## Optionally dump node coordinates of first <nodes2Show> nodes.
254 ## <entity2Show> gives number of elements to dump nodal connectivity by
255 ## entities: [<nb CELLs to show>, <nb FACEs>, <nb EDGEs> ].
256 ## Use SHOW_ALL to dump all elements or node coordinates.
258 def ShowMesh(mesh, nodes2Show=0, entity2Show=[0,0,0]):
259 print "---------------------- MESH -------------------------"
260 meshName = mesh.getName()
261 spaceDim = mesh.getSpaceDimension()
262 meshDim = mesh.getMeshDimension()
263 print "The mesh <%s> is a %dD mesh on a %dD geometry" % (meshName,meshDim,spaceDim)
264 nbNodes = mesh.getNumberOfNodes()
265 print "There are",nbNodes,"MED_NODE's"
266 coordSyst = mesh.getCoordinatesSystem()
267 print "The Coordinates :"
270 for isd in range(spaceDim):
271 coordNames.append(mesh.getCoordinateName(isd))
272 coordUnits.append(mesh.getCoordinateUnit(isd))
274 print tab,"system:",coordSyst
275 print tab,"names:", coordNames
276 print tab,"units:", coordUnits
280 coordinates = mesh.getCoordinates(MED_FULL_INTERLACE)
282 maxNbCoord = len( coordinates ) / spaceDim
283 if nbCoord < 0: nbCoord = maxNbCoord
284 else: nbCoord = min( nbCoord, maxNbCoord )
285 for k in range( nbCoord ):
287 print tab*2,k+1,coordinates[n:n+spaceDim]
289 if nbCoord < maxNbCoord: print tab*2,"... %d nodes skipped" % (maxNbCoord-nbCoord)
292 print "The Elements :"
294 for entity in [MED_CELL,MED_FACE,MED_EDGE]:
296 entityName = theEntityName[ entity ]
297 nbTypes = mesh.getNumberOfTypesWithPoly( entity )
298 if nbTypes == 0 : continue
300 types = mesh.getTypesWithPoly( entity )
303 print tab,"%s types:" % entityName
305 nbElemType = mesh.getNumberOfElementsWithPoly(entity,type)
306 print tab*2,"%s: \t %d elements" % ( theTypeName[ type ], nbElemType )
309 if i >= len( entity2Show ): break
310 if not entity2Show[ i ]: continue
311 print tab,"%s nodal connectivity:" % entityName
313 typeName = theTypeName[ type ]
314 nbElemType = mesh.getNumberOfElementsWithPoly(entity,type)
318 if type==MED_POLYGON or type==MED_POLYHEDRA:
319 d += mesh.getNumberOfElements(entity,MED_ALL_ELEMENTS)
320 number = range (d, nbElemType+d)
321 _showNodalConnectivity(mesh,entity,type,number,2,entity2Show[ i ])
325 print "----------------------Groups, Families-------------------------"
328 for entity in [MED_NODE,MED_CELL,MED_FACE,MED_EDGE]:
329 nbFam = mesh.getNumberOfFamilies(entity)
330 nbGrp = mesh.getNumberOfGroups(entity)
331 nbElem= mesh.getNumberOfElements(entity, MED_ALL_ELEMENTS);
334 if (entity == MED_NODE) :
335 if (nbFam > 0) : print "This mesh has",nbFam,"Node Family(ies)"
336 if (nbGrp > 0) : print "This mesh has",nbGrp,"Node Group(s)"
337 if (nbElem> 0) : print "This mesh has",nbElem,"Node Element(s)"
339 elif (entity == MED_CELL) :
340 if (nbFam > 0) : print "This mesh has",nbFam,"Cell Family(ies)"
341 if (nbGrp > 0) : print "This mesh has",nbGrp,"Cell Group(s)"
342 if (nbElem> 0) : print "This mesh has",nbElem,"Cell Element(s)"
344 elif (entity == MED_FACE) :
345 if (nbFam > 0) : print "This mesh has",nbFam,"Face Family(ies)"
346 if (nbGrp > 0) : print "This mesh has",nbGrp,"Face Group(s)"
347 if (nbElem> 0) : print "This mesh has",nbElem,"Face Element(s)"
349 elif (entity == MED_EDGE) :
350 if (nbFam > 0) : print "This mesh has",nbFam,"Edge Family(ies)"
351 if (nbGrp > 0) : print "This mesh has",nbGrp,"Edge Group(s)"
352 if (nbElem> 0) : print "This mesh has",nbElem,"Edge Element(s)"
355 print "Total nbF", nbF,"nbG",nbG
357 ## Dump all FIELD's in MED.
358 ## Optionally dump <showValues> first values.
359 ## Use showValues=SHOW_ALL to dump all values.
361 def ShowFields( med, showValues=0 ):
362 nbFields = med.getNumberOfFields()
363 print "---------------------- Fields-------------------------"
364 print "Nb fields", nbFields
365 for iField in range( med.getNumberOfFields() ):
366 fName = med.getFieldName( iField )
367 for it in range( med.getFieldNumberOfIteration( fName ) ):
368 dt_it_ = med.getFieldIteration( fName, it )
369 f = med.getField( fName, dt_it_.dt, dt_it_.it )
372 desc = f.getDescription()
373 itnb = f.getIterationNumber()
375 order = f.getOrderNumber()
376 ftype = f.getValueType()
377 mode = f.getInterlacingType()
378 nbcomp = f.getNumberOfComponents()
379 nbval = f.getNumberOfValues()
380 nbelem = sup.getNumberOfElements(MED_ALL_ELEMENTS)
381 nbOfTypes = sup.getNumberOfTypes()
382 types = sup.getTypes()
383 isOnAll = sup.isOnAllElements()
384 print '\nFIELD',iField
385 print tab*1,'-Name : "%s"' % name
386 print tab*1,'-Description : "%s"' % desc
387 print tab*1,'-IterationNumber : %s' % itnb
388 print tab*1,'-Time : %s' % time
389 print tab*1,'-OrderNumber : %s' % order
390 print tab*1,'-Nb Values : %s' % nbval
391 print tab*1,'-Nb Supp. Elements: %s' % nbelem
392 print tab*1,'-Nb Componenets : %s' % nbcomp
393 print tab*1,'-ValueType : %s' % med_type_champ[ftype]
394 print tab*1,'-Interlace : %s' % medModeSwitch[mode]
395 print tab*1,'-Conponents'
396 for k in range(nbcomp):
398 compName = f.getComponentName(kp1)
399 compDesc = f.getComponentDescription(kp1)
400 compUnit = f.getMEDComponentUnit(kp1)
401 print tab*2,kp1,'*Name : "%s"' % compName
403 print tab*2,' *Description: "%s"' % compDesc
407 print tab*2,' *Unit : "%s"' % compUnit
411 print tab*1,'-SUPPORT : "%s"' % sup.getName()
412 print tab*1,'-On all elements : %s' % bool(isOnAll)
413 print tab*1,'-Types : %s' % types
415 if ftype == MED_REEL64:
416 if mode == MED_FULL_INTERLACE:
417 f = createFieldDoubleFromField(f)
419 f = createFieldDoubleNoInterlaceFromField( f )
420 elif ftype == MED_INT32:
421 if mode == MED_FULL_INTERLACE:
422 f = createFieldIntFromField(f)
424 f = createFieldIntNoInterlaceFromField( f )
426 print tab*1,'<< Unknown field type >>:',ftype
433 hasGauss = f.getGaussPresence()
435 nbGaussByType = f.getNumberOfGaussPoints()
436 for k in range(nbOfTypes):
438 nbOfElmtsOfType = sup.getNumberOfElements(type)
439 if hasGauss: nbGauss = nbGaussByType[ k ]
440 if type == 0: type = MED_POINT1
441 print tab*2,k+1,theTypeName[type],':',nbOfElmtsOfType, 'elements,',\
442 nbGauss,'gauss point(s)'
444 nbOf = sup.getNumberOfElements(MED_ALL_ELEMENTS)
447 elements = sup.getNumber(MED_ALL_ELEMENTS)
450 print tab*1,'-Nb Values :',nbOf
451 #value = f.getValue(MED_FULL_INTERLACE)
452 #print value[0: min( 100, len(value)-1 )]
454 toShow = min( nbOf, showValues )
455 if toShow < 0: toShow = nbOf
456 for I in range( toShow ):
461 if mode == MED_FULL_INTERLACE:
465 for j in range( nbcomp ):
466 for k in range( f.getNbGaussI( i ) ):
467 valueI.append( f.getValueIJK(i,j+1,k+1) )
468 print ' ',i,' - ',valueI #[:nbcomp]
471 print ' ...skip',nbOf - toShow,'values'
477 ## Read all fields in MED
480 nbFields = med.getNumberOfFields()
482 print 'READ FIELDs ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
484 for i in range(nbFields):
485 field_name = med.getFieldName(i)
486 nbOfIt = med.getFieldNumberOfIteration(field_name)
487 print ' The field is',field_name,'with',nbOfIt,'iteration(s)'
488 for j in range(nbOfIt):
489 dtitfield = med.getFieldIteration(field_name,j)
490 dt = dtitfield.getdt()
491 it = dtitfield.getit()
492 field = med.getField(field_name,dt,it)
493 type = field.getValueType()
494 print ' * Iteration:',dt,'Order number:',it,'Type:',type
495 mode = field.getInterlacingType()
496 if type == MED_INT32:
497 if mode == MED_FULL_INTERLACE:
498 fieldint = createFieldIntFromField(field)
500 fieldint = createFieldIntNoInterlaceFromField( field )
501 print ' Reading',fieldint.getName(),'...'
503 elif type == MED_REEL64:
504 if mode == MED_FULL_INTERLACE:
505 f = createFieldDoubleFromField(field)
507 f = createFieldDoubleNoInterlaceFromField( field )
508 print ' Reading',f.getName(),'...'
511 print ' !!!! Bad type of Field !!!!'
513 # Remove a file if it exists
516 if os.access(f, os.F_OK):
519 # Remove a file if it exists