Back to index

salome-med  6.5.0
med_test_grid.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 #    Python script for testing T5.10 task
00025 # ###########################################
00026 #% Test structured mesh (GRID) reading from file test19.med
00027 #% test19.med can be obtained by running test19_c.out executable
00028 # or locally in MEDMEM directory create_grid
00029 #
00030 from libMEDMEM_Swig import *
00031 
00032 import os
00033 #
00034 #befor running this script, please be sure about the path the file fileName
00035 #
00036 filePath=os.environ["MED_ROOT_DIR"]
00037 filePath=os.path.join(filePath, "share", "salome", "resources", "med")
00038 
00039 medFile = os.path.join(filePath, "test19.med")
00040 print "Read file", medFile
00041 
00042 md = MEDFILEBROWSER(medFile)
00043 
00044 nbMeshes = md.getNumberOfMeshes()
00045 print "Nb meshes:", nbMeshes
00046 if nbMeshes == 0:
00047   raise  RuntimeError, "Can't read med file"
00048 
00049 ##############################
00050 # test "CartGrid"
00051 ##############################
00052 
00053 mesh_name = md.getMeshName(0)
00054 print "Read", mesh_name
00055 grid = GRID(MED_DRIVER,medFile,mesh_name)
00056 
00057 if grid.getIsAGrid() == 0:
00058   raise  RuntimeError, "Mesh 0 is not a grid"
00059 
00060 I = grid.getArrayLength(1)
00061 print "_iArrayLength", I
00062 if I != 4:
00063   raise  RuntimeError, "Error in grid.getArrayLength(1)"
00064 
00065 J = grid.getArrayLength(2)
00066 print "_jArrayLength", J
00067 if J != 4:
00068   raise  RuntimeError, "Error in grid.getArrayLength(2)"
00069 
00070 K = grid.getArrayLength(3)
00071 print "_kArrayLength", K
00072 if K != 0:
00073   raise  RuntimeError, "Error in grid.getArrayLength(3)"
00074 
00075 grid_type = grid.getGridType()
00076 print "grid_type =", grid_type
00077 if grid_type != MED_CARTESIAN:
00078   raise  RuntimeError, "Wrong grid type"
00079 
00080 spaceDim = grid.getSpaceDimension()
00081 print "spaceDim =", spaceDim
00082 if spaceDim != 2:
00083   raise  RuntimeError, "Wrong space dimention"
00084 
00085 meshDim = grid.getMeshDimension()
00086 print "meshDim =", meshDim
00087 if spaceDim != meshDim:
00088   raise  RuntimeError, "Wrong mesh dimention"
00089 
00090 nbNodes = grid.getNumberOfNodes()
00091 print "nbNodes =", nbNodes
00092 if nbNodes != 16:
00093   raise  RuntimeError, "Wrong nb of nodes"
00094 
00095 coordSyst = grid.getCoordinatesSystem()
00096 print "coordSyst =", coordSyst
00097 if coordSyst != "CARTESIAN":
00098   raise  RuntimeError, "Wrong coordinates system"
00099 
00100 mesh = grid.convertInMESH()
00101 coordinates = mesh.getCoordinates(MED_FULL_INTERLACE)
00102 print "coordinates =", coordinates
00103 xy = [ grid.getArrayValue(1, I-1 ) , grid.getArrayValue(2, J-1 ) ]
00104 xy2 = coordinates[(nbNodes-1)*spaceDim:(nbNodes*spaceDim)]
00105 print list(xy)
00106 print xy2
00107 if not list(xy) == list(xy2):
00108   raise  RuntimeError, "Error in grid.getCoordinates(MED_FULL_INTERLACE)"
00109 
00110 nbTypesCell = grid.getNumberOfTypes(MED_CELL)
00111 print "nbTypesCell =", nbTypesCell
00112 if nbTypesCell != 1:
00113   raise  RuntimeError, "Wrong nbTypesCell"
00114 
00115 types = grid.getTypes(MED_CELL)
00116 print "Cell types =", types
00117 if types != [204]:
00118   raise  RuntimeError, "Wrong Cell types"
00119               
00120 nbElemType = grid.getNumberOfElements(MED_CELL,types[0])
00121 print "Nb cells =", nbElemType
00122 if nbElemType != 9:
00123   raise  RuntimeError, "Wrong Nb cells"
00124 
00125 print "getEntityPosition : convert node number to i,j,k"
00126 
00127 n1 = grid.getEntityPosition(MED_NODE,10)
00128 n2 = grid.getEntityPosition(MED_NODE,16)
00129 
00130 if n1 == [1,2]: o1 = "Ok"
00131 else:           o1 = "KO"
00132 if n2 == [3,3]: o2 = "Ok"
00133 else:           o2 = "KO"
00134 
00135 print "Node 10 -> i,j = ", n1, " : ", o1
00136 print "Node 16 -> i,j = ", n2, " : ", o2
00137 
00138 print "getEntityPosition : convert cell number to i,j,k"
00139 
00140 n1 = grid.getEntityPosition(MED_CELL,3)
00141 n2 = grid.getEntityPosition(MED_CELL,4)
00142 
00143 if n1 == [2,0]: o1 = "Ok"
00144 else:           o1 = "KO"
00145 if n2 == [0,1]: o2 = "Ok"
00146 else:           o2 = "KO"
00147 
00148 print "Cell 3 -> i,j = ", n1, " : ", o1
00149 print "Cell 4 -> i,j = ", n2, " : ", o2
00150 
00151 print "getEntityPosition : convert face number to i,j,k"
00152 
00153 n1 = grid.getEntityPosition(MED_FACE,6)
00154 n2 = grid.getEntityPosition(MED_FACE,8)
00155 
00156 if n1 == [1,2,1]: o1 = "Ok"
00157 else:             o1 = "KO"
00158 if n2 == [1,1,2]: o2 = "Ok"
00159 else:             o2 = "KO"
00160 
00161 print "Face 6 -> i,j = ", n1, " : ", o1
00162 print "Face 8 -> i,j = ", n2, " : ", o2
00163 
00164 print "getNodeNumber : convert i,j,k to node number"
00165 
00166 n1 = grid.getNodeNumber(2,1,0)
00167 n2 = grid.getNodeNumber(1,2,0)
00168 
00169 if n1 == 7:  o1 = "Ok"
00170 else:        o1 = "KO"
00171 if n2 == 10: o2 = "Ok"
00172 else:        o2 = "KO"
00173 
00174 print "getNodeNumber 2,1,0 = ", n1, " : ", o1
00175 print "getNodeNumber 1,2,0 = ", n2, " : ", o2
00176 
00177 print "getCellNumber : convert i,j,k to cell number"
00178 
00179 n1 = grid.getCellNumber(1,0,0)
00180 n2 = grid.getCellNumber(2,1,0)
00181 
00182 if n1 == 2: o1 = "Ok"
00183 else:       o1 = "KO"
00184 if n2 == 6: o2 = "Ok"
00185 else:       o2 = "KO"
00186 
00187 print "getCellNumber 1,0,0 = ", n1, " : ", o1
00188 print "getCellNumber 2,1,0 = ", n2, " : ", o2
00189 
00190 print "getFaceNumber : convert i,j,k to face number"
00191 
00192 n1 = grid.getFaceNumber(0,0,2,0)
00193 n2 = grid.getFaceNumber(0,1,1,0)
00194 
00195 if n1 == 7: o1 = "Ok"
00196 else:       o1 = "KO"
00197 if n2 == 5: o2 = "Ok"
00198 else:       o2 = "KO"
00199 
00200 print "getFaceNumber 1,0,2,0 = ", n1, " : ", o1
00201 print "getFaceNumber 1,1,1,0 = ", n2, " : ", o2
00202 
00203 print "Connectivity"
00204 n1 = grid.getNodeNumber(0,0,0)
00205 n2 = grid.getNodeNumber(0,1,0)
00206 n3 = grid.getNodeNumber(1,1,0)
00207 n4 = grid.getNodeNumber(1,0,0)
00208 
00209 conn1 = [n1,n2,n3,n4]
00210 
00211 Connectivity = mesh.getConnectivity(MED_NODAL,MED_CELL,types[0])
00212 print "connectivity =", Connectivity
00213 
00214 print "Cell 1 nodes:", conn1, Connectivity[0:4]
00215 if list(conn1) != list(Connectivity[0:4]):
00216   raise  RuntimeError, "Wrong nodal connectivity"
00217 
00218 
00219 ReverseConnectivity = mesh.getReverseConnectivity(MED_NODAL)
00220 ReverseConnectivityIndex = mesh.getReverseConnectivityIndex(MED_NODAL)
00221 n = 6
00222 beg = ReverseConnectivityIndex[n-1]-1
00223 end = ReverseConnectivityIndex[n]-1
00224 print "Node",n,"rev connectivity =",ReverseConnectivity[beg:end]
00225 if end-beg != 4:
00226   raise  RuntimeError, "Wrong reverse connectivity"
00227 if ReverseConnectivity[beg] != 1:
00228   raise  RuntimeError, "Wrong reverse connectivity"
00229 
00230 edgfecon = mesh.getConnectivity(MED_NODAL,MED_EDGE,MED_ALL_ELEMENTS)
00231 print edgfecon
00232 print len(edgfecon)
00233 
00234 mesh.calculateConnectivity(MED_DESCENDING,MED_CELL)
00235 Connectivity = mesh.getConnectivity(MED_DESCENDING,MED_CELL,MED_ALL_ELEMENTS)
00236 ConnectivityIndex = mesh.getConnectivityIndex(MED_DESCENDING,MED_CELL)
00237 n = 1
00238 beg = ConnectivityIndex[n-1]-1
00239 end = ConnectivityIndex[n]-1
00240 print "Element",n,"descending connectivity -->",Connectivity[beg:end]
00241 if list(Connectivity[beg:end]) != [13, 4, 14, 1]:
00242   raise  RuntimeError, "Wrong descending  connectivity"
00243 
00244 print "getReverseConnectivity(MED_DESCENDING)", mesh.getReverseConnectivity(MED_DESCENDING)
00245 print "grid.getReverseConnectivityIndex(MED_DESCENDING)",mesh.getReverseConnectivityIndex(MED_DESCENDING)
00246 
00247 ##############################
00248 # test "bodyfitted"
00249 ##############################
00250 
00251 mesh_name = md.getMeshName(1)
00252 print "Read", mesh_name
00253 grid = GRID(MED_DRIVER,medFile,mesh_name)
00254 
00255 if grid.getIsAGrid() == 0:
00256   raise  RuntimeError, "Mesh 1 is not a grid"
00257 
00258 mesh = grid.convertInMESH()
00259 
00260 I = grid.getArrayLength(1)
00261 print "_iArrayLength", I
00262 if I != 2:
00263   raise  RuntimeError, "Error in grid.getArrayLength(1)"
00264 
00265 J = grid.getArrayLength(2)
00266 print "_jArrayLength", J
00267 if J != 2:
00268   raise  RuntimeError, "Error in grid.getArrayLength(2)"
00269 
00270 K = grid.getArrayLength(3)
00271 print "_kArrayLength", K
00272 if K != 0:
00273   raise  RuntimeError, "Error in grid.getArrayLength(3)"
00274 
00275 grid_type = grid.getGridType()
00276 print "grid_type =", grid_type
00277 if grid_type != MED_BODY_FITTED:
00278   raise  RuntimeError, "Wrong grid type"
00279 
00280 spaceDim = grid.getSpaceDimension()
00281 print "spaceDim =", spaceDim
00282 if spaceDim != 2:
00283   raise  RuntimeError, "Wrong space dimention"
00284 
00285 meshDim = grid.getMeshDimension()
00286 print "meshDim =", meshDim
00287 if spaceDim != meshDim:
00288   raise  RuntimeError, "Wrong mesh dimention"
00289 
00290 nbNodes = grid.getNumberOfNodes()
00291 print "nbNodes =", nbNodes
00292 if nbNodes != 4:
00293   raise  RuntimeError, "Wrong nb of nodes"
00294 
00295 coordSyst = grid.getCoordinatesSystem()
00296 print "coordSyst =", coordSyst
00297 if coordSyst != "CARTESIAN":
00298   raise  RuntimeError, "Wrong coordinates system"
00299 
00300 nbTypesCell = grid.getNumberOfTypes(MED_CELL)
00301 print "nbTypesCell =", nbTypesCell
00302 if nbTypesCell != 1:
00303   raise  RuntimeError, "Wrong nbTypesCell"
00304 
00305 types = grid.getTypes(MED_CELL)
00306 print "Cell types =", types
00307 if types != [204]:
00308   raise  RuntimeError, "Wrong Cell types"
00309               
00310 nbElemType = grid.getNumberOfElements(MED_CELL,types[0])
00311 print "Nb cells =", nbElemType
00312 if nbElemType != 1:
00313   raise  RuntimeError, "Wrong Nb cells"
00314 
00315 Connectivity = mesh.getConnectivity(MED_NODAL,MED_CELL,types[0])
00316 print "connectivity =", Connectivity
00317 n1 = grid.getNodeNumber(0,0,0)
00318 n2 = grid.getNodeNumber(0,1,0)
00319 n3 = grid.getNodeNumber(1,1,0)
00320 n4 = grid.getNodeNumber(1,0,0)
00321 conn1 = [n1,n2,n3,n4]
00322 print "Cell 1 nodes:", conn1, Connectivity[0:4]
00323 if conn1 != list(Connectivity[0:4]):
00324   raise  RuntimeError, "Wrong nodal connectivity"
00325 
00326 ##############################################
00327 # test "maa1" which in fact is not a pure GRID
00328 ##############################################
00329 
00330 mesh_name = md.getMeshName(2)
00331 
00332 print "Read", mesh_name
00333 mesh = MESH(MED_DRIVER,md.getFileName(),mesh_name)
00334 
00335 if mesh.getIsAGrid() == 0:
00336   print "Mesh ",mesh_name," is not a grid"
00337 
00338 print "END of the Pyhton script ..... Ctrl D to exit"