Back to index

salome-med  6.5.0
med_test3.py
Go to the documentation of this file.
00001 #  -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00003 #
00004 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00005 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00006 #
00007 # This library is free software; you can redistribute it and/or
00008 # modify it under the terms of the GNU Lesser General Public
00009 # License as published by the Free Software Foundation; either
00010 # version 2.1 of the License.
00011 #
00012 # This library is distributed in the hope that it will be useful,
00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 # Lesser General Public License for more details.
00016 #
00017 # You should have received a copy of the GNU Lesser General Public
00018 # License along with this library; if not, write to the Free Software
00019 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020 #
00021 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00022 #
00023 
00024 ###################################################################################
00025 # This Python script is parsing a MED file using MED Memory from SALOME platform:
00026 # It tests the setValue, setRow and setColum functions on MED fields
00027 ###################################################################################
00028 #
00029 from libMEDMEM_Swig import *
00030 from random import *
00031 import os
00032 #
00033 #befor running this script, please be sure about the path the file fileName
00034 #
00035 filePath=os.environ["MED_ROOT_DIR"]
00036 filePath=os.path.join(filePath, "share", "salome", "resources", "med")
00037 
00038 medFile = os.path.join(filePath, "carre_en_quad4_seg2.med")
00039 #medFile = os.path.join(filePath, "cube_hexa8_quad4.med")
00040 
00041 def print_ord(i):
00042     if i == 0:
00043         return 'first'
00044     elif i == 1:
00045         return 'second'
00046     elif i == 2:
00047         return 'third'
00048     else:
00049         return `i`+'th'
00050 
00051 md = MEDFILEBROWSER(medFile)
00052 
00053 nbMeshes = md.getNumberOfMeshes()
00054 
00055 nbFields = md.getNumberOfFields()
00056 
00057 print "The med file", medFile, "contains", nbMeshes, "mesh(es) and", nbFields, "field(s)"
00058 
00059 if (nbMeshes>0):
00060     print "Mesh(es) Name(s) is(are) "
00061 
00062     for i in range(nbMeshes):
00063         mesh_name = md.getMeshName(i)
00064         print "   - ",mesh_name
00065 
00066 if (nbFields>0):
00067     print "Field(s) Name(s) is(are) "
00068 
00069     for i in range(nbFields):
00070         field_name = md.getFieldName(i)
00071         print "   - ",field_name
00072 
00073 print ""
00074 
00075 if (nbMeshes>0):
00076     print "Mesh(es) Analysis "
00077     for i in range(nbMeshes):
00078         mesh_name = md.getMeshName(i)
00079         mesh = MESH(MED_DRIVER,md.getFileName(),mesh_name)
00080         spaceDim = mesh.getSpaceDimension()
00081         for entity in [MED_NODE,MED_CELL,MED_FACE,MED_EDGE]:
00082             nbFam = mesh.getNumberOfFamilies(entity)
00083             if (entity == MED_NODE) & (nbFam > 0):
00084                 print "This mesh has",nbFam,"Node Family(ies)"
00085             elif (entity == MED_CELL) & (nbFam > 0):
00086                 print "This mesh has",nbFam,"Cell Family(ies)"
00087             elif (entity == MED_FACE) & (nbFam > 0):
00088                 print "This mesh has",nbFam,"Face Family(ies)"
00089             elif (entity == MED_EDGE) & (nbFam > 0):
00090                 print "This mesh has",nbFam,"Edge Family(ies)"
00091 
00092             if nbFam > 0:
00093                 for j in range(nbFam):
00094                     print ""
00095                     family = mesh.getFamily(entity,j+1)
00096                     familyName = family.getName()
00097                     familyDescription = family.getDescription()
00098                     familyEntity = family.getEntity()
00099                     familyBool = family.isOnAllElements()
00100                     print "  -Name:",familyName
00101                     print "  -Description:",familyDescription
00102                     print "  -Entity:",familyEntity
00103                     familyIdentifier = family.getIdentifier()
00104                     nbOfAtt = family.getNumberOfAttributes()
00105                     print "  -Identifier:",familyIdentifier
00106                     print "  -Number Of Attributes:",nbOfAtt
00107                     attributesids = family.getAttributesIdentifiers()
00108                     attributesvals = family.getAttributesValues()
00109                     for k in range(nbOfAtt):
00110                         print "    * Attributes:",attributesids[k],":",attributesvals[k],",",family.getAttributeDescription(k+1)
00111                     nbOfGrp = family.getNumberOfGroups()
00112                     print "  -Number Of Groups:",nbOfGrp
00113                     for k in range(nbOfGrp):
00114                         print "    * Group:",family.getGroupName(k+1)
00115                     print "  -Entities list:"
00116                     if (familyBool):
00117                         print "  -Is on all entities"
00118                     else:
00119                         nbOfTypes = family.getNumberOfTypes()
00120                         types = family.getTypes()
00121                         print "  -Number Of Types:",nbOfTypes
00122                         for k in range(nbOfTypes):
00123                             type = types[k]
00124                             nbOfElmtsOfType = family.getNumberOfElements(type)
00125                             number = family.getNumber(type)
00126                             print "    * Type",type
00127                             print "    * Number",number[0:nbOfElmtsOfType]
00128                         print ""
00129                         numberFamily = family.getNumber(MED_ALL_ELEMENTS)
00130                         print "    * Getting an Integer Field on the family ",familyName
00131                         fieldFamilyIntg = FIELDINT(family,spaceDim)
00132                         fieldFamilyIntg.setIterationNumber(0)
00133                         fieldFamilyIntg.setOrderNumber(0)
00134                         fieldFamilyIntg.setTime(0.0)
00135                         for kcomp in range(spaceDim):
00136                             if kcomp == 0:
00137                                 fieldCompName = "comp1"
00138                                 fieldCompDesc = "desc1"
00139                                 fieldCompUnit = "unit1"
00140                             if kcomp == 1:
00141                                 fieldCompName = "comp2"
00142                                 fieldCompDesc = "desc2"
00143                                 fieldCompUnit = "unit2"
00144                             if kcomp == 2:
00145                                 fieldCompName = "comp2"
00146                                 fieldCompDesc = "desc2"
00147                                 fieldCompUnit = "unit2"
00148 
00149                             kcomp1 = kcomp+1
00150                             fieldFamilyIntg.setComponentName(kcomp1,fieldCompName)
00151                             fieldFamilyIntg.setComponentDescription(kcomp1,fieldCompDesc)
00152                             fieldFamilyIntg.setMEDComponentUnit(kcomp1,fieldCompUnit)
00153                         fieldFamilyName = "Integer Field on "+familyName
00154                         fieldFamilyIntg.setName(fieldFamilyName)
00155                         field_name = fieldFamilyIntg.getName()
00156                         type_field = fieldFamilyIntg.getValueType()
00157                         nbOfComp = fieldFamilyIntg.getNumberOfComponents()
00158                         print "      The field",field_name,"is with the type",type_field
00159                         print "      It has",nbOfComp,"Component(s)"
00160                         for kcomp in range(nbOfComp):
00161                             kcomp1 = kcomp+1
00162                             compName = fieldFamilyIntg.getComponentName(kcomp1)
00163                             compDesc = fieldFamilyIntg.getComponentDescription(kcomp1)
00164                             compUnit = fieldFamilyIntg.getMEDComponentUnit(kcomp1)
00165                             print "      * Component:",kcomp1
00166                             print "          Name:",compName
00167                             print "          Description:",compDesc
00168                             print "          Unit:",compUnit
00169 
00170                         nbOf = fieldFamilyIntg.getSupport().getNumberOfElements(MED_ALL_ELEMENTS)
00171                         print "      Values:",nbOf
00172                         print "      Randomly set (via setValue) and get (via getValue) to check with the same mode ..!"
00173                         value = []
00174                         for k in range(nbOf*nbOfComp):
00175                             value.append(randint(0,100))
00176                         print "      the mode is full interlace"
00177                         mode = MED_FULL_INTERLACE
00178                         fieldFamilyIntg.setValue(value)
00179                         valueverif = fieldFamilyIntg.getValue()
00180                         for k in range(nbOf*nbOfComp):
00181                             print "      Set/Get Entry * ",value[k]," / ",valueverif[k]
00182                         print ""
00183                         value = []
00184                         for k in range(nbOf*nbOfComp):
00185                             value.append(randint(0,100))
00186                         print "      the mode is no interlace"
00187                         mode = MED_NO_INTERLACE
00188                         fieldFamilyIntg.setValue(value)
00189                         valueverif = fieldFamilyIntg.getValue()
00190                         for k in range(nbOf*nbOfComp):
00191                             print "      Set/Get Entry * ",value[k]," / ",valueverif[k]
00192                         print ""
00193                         print "      Randomly set (via setValue) and get (via getValue) to check with different mode ..!"
00194                         value = []
00195                         for k in range(nbOf*nbOfComp):
00196                             value.append(randint(0,100))
00197                         print "      the mode for set is full interlace and no interlace for get"
00198                         mode_full = MED_FULL_INTERLACE
00199                         mode_no = MED_NO_INTERLACE
00200                         fieldFamilyIntg.setValue(value)
00201                         valueverif = fieldFamilyIntg.getValue()
00202                         for k in range(nbOf*nbOfComp):
00203                             print "      Set/Get Entry * ",value[k]," / ",valueverif[k]
00204                         print ""
00205                         value = []
00206                         for k in range(nbOf*nbOfComp):
00207                             value.append(randint(0,100))
00208                         print "      the mode for set is no interlace and full interlace for get"
00209                         fieldFamilyIntg.setValue(value)
00210                         valueverif = fieldFamilyIntg.getValue()
00211                         for k in range(nbOf*nbOfComp):
00212                             print "      Set/Get Entry * ",value[k]," / ",valueverif[k]
00213                         print ""
00214                         print "      Randomly set (via setRow/setColumn) and get (via getRow/getColumn) to check with the same mode ..!"
00215                         value = []
00216                         for k in range(nbOf*nbOfComp):
00217                             value.append(randint(0,100))
00218                         mode = MED_FULL_INTERLACE
00219                         print "      the mode is full interlace ", mode
00220                         print "      the field interlacing mode ",fieldFamilyIntg.getInterlacingType()
00221                         for k in range(nbOf):
00222                             valInd = numberFamily[k]
00223                             fieldFamilyIntg.setRow(valInd,value[k*nbOfComp:(k+1)*nbOfComp])
00224                         valueverif = []
00225                         for k in range(nbOf):
00226                             valInd = numberFamily[k]
00227                             valueverif.extend(fieldFamilyIntg.getRow(valInd))
00228                         for k in range(nbOf*nbOfComp):
00229                             print "      Set/Get Entry * ",value[k]," / ",valueverif[k]
00230                         print ""
00231                         value = []
00232                         for k in range(nbOf*nbOfComp):
00233                             value.append(randint(0,100))
00234                         mode = MED_NO_INTERLACE
00235                         print "      the mode is no interlace ", mode
00236                         print "      the field interlacing mode ",fieldFamilyIntg.getInterlacingType()
00237                         for k in range(nbOf):
00238                             valInd = numberFamily[k]
00239                             fieldFamilyIntg.setRow(valInd,value[k*nbOfComp:(k+1)*nbOfComp])
00240                         fieldFamilyIntgOtherMode = createFieldIntConvertNoInterlace(fieldFamilyIntg)
00241                         valueverif = []
00242                         for k in range(nbOfComp):
00243                             valueverif.extend(fieldFamilyIntgOtherMode.getColumn(k+1))
00244                         for k in range(nbOf*nbOfComp):
00245                             print "      Set/Get Entry * ",value[k]," / ",valueverif[k]
00246                         print ""
00247 print "END of the Pyhton script ..... Ctrl D to exit"