1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 ############################################################################
22 # This file provides utilities to dump content of MEDMEM objects: mesh,
23 # 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 "ELEMENTS:",elems
76 tab2 = tab*(tablevel+1)
77 typeName = theTypeName[type]
80 elems = elems[:showOnly]
82 connectivity = mesh.getConnectivity(MED_NODAL,entity,MED_ALL_ELEMENTS)
83 index = mesh.getConnectivityIndex(MED_NODAL,entity)
84 if debugShowConn: print "CONN:",connectivity,"\nIND:",index
86 types = mesh.getTypes( entity )
89 elemShift += mesh.getNumberOfElements(entity,t)
95 print tab1,typeName,i,":",connectivity[index[elem-1]-1 : index[elem]-1]
97 nbSkip = nbElem - nbShow
99 print tab1,"...",nbSkip,"elements not shown"
104 def _showSupport(support, tablevel,showElems=0):
105 tab1 = tab*(tablevel+0)
106 tab2 = tab*(tablevel+1)
107 tab3 = tab*(tablevel+3)
108 entity = support.getEntity()
109 types = support.getTypes()
110 nbOfTypes = support.getNumberOfTypes()
111 onAll = support.isOnAllElements()
112 print tab1,"-Entity:",theEntityName[entity]
113 print tab1,"-Types :",types
114 print tab1,"-Elements"
116 print tab2,"<< Is on all elements >>"
119 nbOfElmtsOfType = support.getNumberOfElements(type)
120 number = support.getNumber(type)
122 print tab2,"* Type:",theTypeName[type]
123 print tab3,". Nb elements:",nbOfElmtsOfType
124 print tab3,". Numbers:",number[:min(100,nbOfElmtsOfType)],
125 if nbOfElmtsOfType > 100:
126 print "...skip", nbOfElmtsOfType-100
130 if entity != MED_NODE and showElems:
131 print tab3,". Nodal connectivity"
132 _showNodalConnectivity(support.getMesh(),entity,type,number,tablevel+4,showElems)
139 ## Dump i-th family of given entity in mesh.
140 ## Optionally dump nodal connectivity of <showElems> first elements.
141 ## Use showElems=SHOW_ALL to dump connectivity of all elements.
143 def ShowFamily(mesh, entity, i, showElems=0):
144 family = mesh.getFamily(entity,i)
145 familyName = family.getName()
146 familyDescription = family.getDescription()
147 entity = family.getEntity()
148 familyBool = family.isOnAllElements()
149 print "\nFAMILY", i, "on", theEntityName[entity]
150 print tab*1,"-Name :",familyName
151 print tab*1,"-Description:",familyDescription
152 familyIdentifier = family.getIdentifier()
153 nbOfAtt = family.getNumberOfAttributes()
154 print tab*1,"-Identifier :",familyIdentifier
155 print tab*1,"-Attributes"
156 attributesids = family.getAttributesIdentifiers()
157 attributesvals = family.getAttributesValues()
158 for k in range(nbOfAtt):
159 print tab*2,"* Id :",attributesids[k]
160 print tab*2,"* Value :",attributesvals[k]
161 print tab*2,"* Description:",family.getAttributeDescription(k+1)
163 nbOfGrp = family.getNumberOfGroups()
164 print tab*1,"-Nb Of Groups:",nbOfGrp
165 print tab*1,"-Groups:"
166 for k in range(nbOfGrp):
167 print tab*2,k+1,":",family.getGroupName(k+1)
169 _showSupport(family,1,showElems)
172 ## Dump all families in mesh.
173 ## Optionally dump nodal connectivity of <showElems> first elements of each family.
174 ## Use showElems=SHOW_ALL to dump connectivity of all elements.
176 def ShowFamilies(mesh, showElems=0):
177 line = "families in mesh <" + mesh.getName() + ">"
178 print "\n",line,"\n","-"*len(line)
179 for entity in [MED_NODE,MED_CELL,MED_FACE,MED_EDGE]:
180 nbFam = mesh.getNumberOfFamilies(entity)
181 for i in range(nbFam):
182 ShowFamily(mesh,entity,i+1,showElems)
187 ## Optionally dump nodal connectivity of <showElems> first elements of the group.
188 ## Use showElems=SHOW_ALL to dump connectivity of all elements.
190 def ShowGroup(group, showElems):
191 groupName = group.getName()
192 groupDescription = group.getDescription()
193 nbOfFam = group.getNumberOfFamilies()
194 print "\nGROUP on",theEntityName[group.getEntity()]
195 print tab*1,"-Name :",groupName
196 print tab*1,"-Description :",groupDescription
197 print tab*1,"-Nb Of Families:",nbOfFam
198 print tab*1,"-Families"
199 for k in range(nbOfFam):
200 print tab*2,k+1,":",group.getFamily(k+1).getName()
202 _showSupport(group,1,showElems)
205 ## Dump all GROUP's in mesh.
206 ## Optionally dump nodal connectivity of <showElems> first elements of each group.
207 ## Use showElems=SHOW_ALL to dump connectivity of all elements.
209 def ShowGroups(mesh, showElems=0):
210 line = "groups in mesh <" + mesh.getName() + ">"
211 print "\n",line,"\n","-"*len(line)
212 for entity in [MED_NODE,MED_CELL,MED_FACE,MED_EDGE]:
213 nbGrp = mesh.getNumberOfGroups(entity)
215 for j in range(nbGrp):
216 group = mesh.getGroup(entity,j+1)
217 ShowGroup(group,showElems)
222 ## Dump mesh general information.
223 ## Optionally dump node coordinates of first <nodes2Show> nodes.
224 ## <entity2Show> gives number of elements to dump nodal connectivity by
225 ## entities: [<nb CELLs to show>, <nb FACEs>, <nb EDGEs> ].
226 ## Use SHOW_ALL to dump all elements or node coordinates.
228 def ShowMesh(mesh, nodes2Show=0, entity2Show=[0,0,0]):
229 print "---------------------- MESH -------------------------"
230 meshName = mesh.getName()
231 spaceDim = mesh.getSpaceDimension()
232 meshDim = mesh.getMeshDimension()
233 print "The mesh <%s> is a %dD mesh on a %dD geometry" % (meshName,meshDim,spaceDim)
234 nbNodes = mesh.getNumberOfNodes()
235 print "There are",nbNodes,"MED_NODE's"
236 coordSyst = mesh.getCoordinatesSystem()
237 print "The Coordinates :"
240 for isd in range(spaceDim):
241 coordNames.append(mesh.getCoordinateName(isd))
242 coordUnits.append(mesh.getCoordinateUnit(isd))
244 print tab,"system:",coordSyst
245 print tab,"names:", coordNames
246 print tab,"units:", coordUnits
250 coordinates = mesh.getCoordinates(MED_FULL_INTERLACE)
252 maxNbCoord = len( coordinates ) / spaceDim
253 if nbCoord < 0: nbCoord = maxNbCoord
254 else: nbCoord = min( nbCoord, maxNbCoord )
255 for k in range( nbCoord ):
257 print tab*2,k+1,coordinates[n:n+spaceDim]
259 if nbCoord < maxNbCoord: print tab*2,"... %d nodes skipped" % (maxNbCoord-nbCoord)
262 print "The Elements :"
264 for entity in [MED_CELL,MED_FACE,MED_EDGE]:
266 entityName = theEntityName[ entity ]
267 if mesh.getNumberOfElements(entity,MED_ALL_ELEMENTS) < 1: continue
268 nbTypes = mesh.getNumberOfTypes( entity )
270 types = mesh.getTypes( entity )
273 print tab,"%s types:" % entityName
275 nbElemType = mesh.getNumberOfElements(entity,type)
276 print tab*2,"%s: \t %d elements" % ( theTypeName[ type ], nbElemType )
279 if i >= len( entity2Show ): break
280 if not entity2Show[ i ]: continue
281 print tab,"%s nodal connectivity:" % entityName
283 typeName = theTypeName[ type ]
284 nbElemType = mesh.getNumberOfElements(entity,type)
288 number = range (d, nbElemType+d)
289 _showNodalConnectivity(mesh,entity,type,number,2,entity2Show[ i ])
293 print "----------------------Groups, Families-------------------------"
296 for entity in [MED_NODE,MED_CELL,MED_FACE,MED_EDGE]:
297 nbFam = mesh.getNumberOfFamilies(entity)
298 nbGrp = mesh.getNumberOfGroups(entity)
299 nbElem= mesh.getNumberOfElements(entity, MED_ALL_ELEMENTS);
302 if (entity == MED_NODE) :
303 if (nbFam > 0) : print "This mesh has",nbFam,"Node Family(ies)"
304 if (nbGrp > 0) : print "This mesh has",nbGrp,"Node Group(s)"
305 if (nbElem> 0) : print "This mesh has",nbElem,"Node Element(s)"
307 elif (entity == MED_CELL) :
308 if (nbFam > 0) : print "This mesh has",nbFam,"Cell Family(ies)"
309 if (nbGrp > 0) : print "This mesh has",nbGrp,"Cell Group(s)"
310 if (nbElem> 0) : print "This mesh has",nbElem,"Cell Element(s)"
312 elif (entity == MED_FACE) :
313 if (nbFam > 0) : print "This mesh has",nbFam,"Face Family(ies)"
314 if (nbGrp > 0) : print "This mesh has",nbGrp,"Face Group(s)"
315 if (nbElem> 0) : print "This mesh has",nbElem,"Face Element(s)"
317 elif (entity == MED_EDGE) :
318 if (nbFam > 0) : print "This mesh has",nbFam,"Edge Family(ies)"
319 if (nbGrp > 0) : print "This mesh has",nbGrp,"Edge Group(s)"
320 if (nbElem> 0) : print "This mesh has",nbElem,"Edge Element(s)"
323 print "Total nbF", nbF,"nbG",nbG
326 ## Dump all FIELD's in MED.
327 ## Optionally dump <showValues> first values.
328 ## Use showValues=SHOW_ALL to dump all values.
330 def ShowFields( fields, showValues=0 ):
331 nbFields = len(fields)
332 print "---------------------- Fields-------------------------"
333 print "Nb fields", nbFields
334 for (iField, f ) in enumerate( fields ):
337 desc = f.getDescription()
338 itnb = f.getIterationNumber()
340 order = f.getOrderNumber()
341 ftype = f.getValueType()
342 mode = f.getInterlacingType()
343 nbcomp = f.getNumberOfComponents()
344 nbval = f.getNumberOfValues()
345 nbelem = sup.getNumberOfElements(MED_ALL_ELEMENTS)
346 nbOfTypes = sup.getNumberOfTypes()
347 types = sup.getTypes()
348 isOnAll = sup.isOnAllElements()
349 print '\nFIELD',iField
350 print tab*1,'-Name : "%s"' % name
351 print tab*1,'-Description : "%s"' % desc
352 print tab*1,'-IterationNumber : %s' % itnb
353 print tab*1,'-Time : %s' % time
354 print tab*1,'-OrderNumber : %s' % order
355 print tab*1,'-Nb Values : %s' % nbval
356 print tab*1,'-Nb Supp. Elements: %s' % nbelem
357 print tab*1,'-Nb Componenets : %s' % nbcomp
358 print tab*1,'-ValueType : %s' % med_type_champ[ftype]
359 print tab*1,'-Interlace : %s' % medModeSwitch[mode]
360 print tab*1,'-Conponents'
361 for k in range(nbcomp):
363 compName = f.getComponentName(kp1)
364 compDesc = f.getComponentDescription(kp1)
365 compUnit = f.getMEDComponentUnit(kp1)
366 print tab*2,kp1,'*Name : "%s"' % compName
368 print tab*2,' *Description: "%s"' % compDesc
373 print tab*2,' *Unit : "%s"' % compUnit
378 print tab*1,'-MESH : "%s"' % sup.getMeshName()
379 print tab*1,'-SUPPORT : "%s"' % sup.getName()
380 print tab*1,'-On all elements : %s' % bool(isOnAll)
381 print tab*1,'-Types : %s' % types
383 if ftype == MED_REEL64:
384 if mode == MED_FULL_INTERLACE:
385 f = createFieldDoubleFromField(f)
387 f = createFieldDoubleNoInterlaceFromField( f )
390 elif ftype == MED_INT32:
391 if mode == MED_FULL_INTERLACE:
392 f = createFieldIntFromField(f)
394 f = createFieldIntNoInterlaceFromField( f )
398 print tab*1,'<< Unknown field type >>:',ftype
405 hasGauss = f.getGaussPresence()
408 nbGaussByType = f.getNumberOfGaussPoints()
410 for k in range(nbOfTypes):
412 nbOfElmtsOfType = sup.getNumberOfElements(type)
413 if hasGauss: nbGauss = nbGaussByType[ k ]
414 if type == 0: type = MED_POINT1
415 print tab*2,k+1,theTypeName[type],':',nbOfElmtsOfType, 'elements,',\
416 nbGauss,'gauss point(s)'
418 nbOf = sup.getNumberOfElements(MED_ALL_ELEMENTS)
421 elements = sup.getNumber(MED_ALL_ELEMENTS)
425 print tab*1,'-Nb Values :',nbOf
426 #value = f.getValue(MED_FULL_INTERLACE)
427 #print value[0: min( 100, len(value)-1 )]
429 toShow = min( nbOf, showValues )
430 if toShow < 0: toShow = nbOf
431 for I in range( toShow ):
436 if mode == MED_FULL_INTERLACE:
440 for j in range( nbcomp ):
441 for k in range( f.getNbGaussI( i ) ):
442 valueI.append( f.getValueIJK(i,j+1,k+1) )
443 print ' ',i,' - ',valueI #[:nbcomp]
446 print ' ...skip',nbOf - toShow,'values'
451 ## Read all fields in MED
454 nbFields = med.getNumberOfFields()
456 print 'READ FIELDs ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
458 for i in range(nbFields):
459 field_name = med.getFieldName(i)
460 nbOfIt = med.getFieldNumberOfIteration(field_name)
461 print ' The field is',field_name,'with',nbOfIt,'iteration(s)'
462 for j in range(nbOfIt):
463 dtitfield = med.getFieldIteration(field_name,j)
464 dt = dtitfield.getdt()
465 it = dtitfield.getit()
466 field = med.getField(field_name,dt,it)
467 type = field.getValueType()
468 print ' * Iteration:',dt,'Order number:',it,'Type:',type
469 mode = field.getInterlacingType()
470 if type == MED_INT32:
471 if mode == MED_FULL_INTERLACE:
472 fieldint = createFieldIntFromField(field)
474 fieldint = createFieldIntNoInterlaceFromField( field )
475 print ' Reading',fieldint.getName(),'...'
477 elif type == MED_REEL64:
478 if mode == MED_FULL_INTERLACE:
479 f = createFieldDoubleFromField(field)
481 f = createFieldDoubleNoInterlaceFromField( field )
482 print ' Reading',f.getName(),'...'
485 print ' !!!! Bad type of Field !!!!'
487 # Remove a file if it exists
490 if os.access(f, os.F_OK):
493 # Remove a file if it exists