Back to index

salome-med  6.5.0
medMeshing_test.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 uses the wrapped C++ class MESHING to buid a mesh from only
00026 # primitive data like coordinates (Pythoin double array) and connectivity (Python
00027 # integer arrays). It is the Python equivalent of the C++ program
00028 # test_MEDMEM_Meshing.cxx in the ../MEDMEM directory of the SALOME distribution
00029 ###################################################################################
00030 #
00031 from libMEDMEM_Swig import *
00032 
00033 # files name to save the generated MESH(ING) in different format
00034 # Med V2.2 and vtk
00035 
00036 med22FileName = "toto22.med"
00037 
00038 vtkFileName = "toto.vtk"
00039 
00040 myMeshing = MESHING()
00041 
00042 myMeshing.setName("meshing")
00043 
00044 # definition of the coordinates
00045 
00046 spaceDimension = 3
00047 
00048 numberOfNodes = 19
00049 
00050 coordinates = [
00051     0.0, 0.0, 0.0  ,
00052     0.0, 0.0, 1.0  ,
00053     2.0, 0.0, 1.0  ,
00054     0.0, 2.0, 1.0  ,
00055     -2.0, 0.0, 1.0 ,
00056     0.0, -2.0, 1.0 ,
00057     1.0, 1.0, 2.0  ,
00058     -1.0, 1.0, 2.0 ,
00059     -1.0, -1.0, 2.0,
00060     1.0, -1.0, 2.0 ,
00061     1.0, 1.0, 3.0  ,
00062     -1.0, 1.0, 3.0 ,
00063     -1.0, -1.0, 3.0,
00064     1.0, -1.0, 3.0 ,
00065     1.0, 1.0, 4.0  ,
00066     -1.0, 1.0, 4.0 ,
00067     -1.0, -1.0, 4.0,
00068     1.0, -1.0, 4.0 ,
00069     0.0, 0.0, 5.0]
00070 
00071 myMeshing.setCoordinates(spaceDimension,numberOfNodes,coordinates,"CARTESIAN",MED_FULL_INTERLACE)
00072 
00073 for i in range(spaceDimension):
00074     unit = "cm      "
00075     if (i == 0):
00076         name = "X       "
00077     elif (i == 1):
00078         name = "Y       "
00079     elif (i == 2):
00080         name = "Z       "
00081 
00082     myMeshing.setCoordinateName(name,i)
00083     myMeshing.setCoordinateUnit(unit,i)
00084 
00085 # definition of connectivities
00086 # cell part
00087 
00088 numberOfTypes = 3
00089 entity = MED_CELL
00090 
00091 types = []
00092 numberOfElements = []
00093 
00094 types.append(MED_TETRA4)
00095 numberOfElements.append(12)
00096 
00097 types.append(MED_PYRA5)
00098 numberOfElements.append(2)
00099 
00100 types.append(MED_HEXA8)
00101 numberOfElements.append(2)
00102 
00103 myMeshing.setNumberOfTypes(numberOfTypes,entity)
00104 myMeshing.setTypes(types,entity)
00105 myMeshing.setNumberOfElements(numberOfElements,entity)
00106 
00107 connectivityTetra =  [1,2,3,6,
00108                       1,2,4,3 ,
00109                       1,2,5,4 ,
00110                       1,2,6,5 ,
00111                       2,7,4,3 ,
00112                       2,8,5,4 ,
00113                       2,9,6,5 ,
00114                       2,10,3,6,
00115                       2,7,3,10,
00116                       2,8,4,7 ,
00117                       2,9,5,8 ,
00118                       2,10,6,9]
00119 
00120 myMeshing.setConnectivity(entity,types[0],connectivityTetra)
00121 
00122 connectivityPyra = [
00123     7,8,9,10,2,
00124     15,18,17,16,19]
00125 
00126 myMeshing.setConnectivity(entity,types[1],connectivityPyra)
00127 
00128 connectivityHexa = [
00129     11,12,13,14,7,8,9,10,
00130     15,16,17,18,11,12,13,14]
00131 
00132 myMeshing.setConnectivity(entity,types[2],connectivityHexa)
00133 
00134 # face part
00135 
00136 numberOfTypes = 2
00137 entity = MED_FACE
00138 
00139 types = []
00140 numberOfElements = []
00141 
00142 types.append(MED_TRIA3)
00143 numberOfElements.append(4)
00144 
00145 types.append(MED_QUAD4)
00146 numberOfElements.append(4)
00147 
00148 myMeshing.setNumberOfTypes(numberOfTypes,entity)
00149 myMeshing.setTypes(types,entity)
00150 myMeshing.setNumberOfElements(numberOfElements,entity)
00151 
00152 connectivityTria = [
00153     1,4,3,
00154     1,5,4,
00155     1,6,5,
00156     1,3,6]
00157 
00158 myMeshing.setConnectivity(entity,types[0],connectivityTria)
00159 
00160 connectivityQuad = [
00161     7,8,9,10   ,
00162     11,12,13,14,
00163     11,7,8,12  ,
00164     12,8,9,13]
00165 
00166 myMeshing.setConnectivity(entity,types[1],connectivityQuad)
00167 
00168 # edge part
00169 
00170 # adding GROUPs
00171 # on Node
00172 
00173 myGroup = GROUP()
00174 myGroup.setName("SomeNodes")
00175 myGroup.setMesh(myMeshing)
00176 myGroup.setEntity(MED_NODE)
00177 myGroup.setNumberOfGeometricType(1)
00178 
00179 myTypes = [MED_NONE]
00180 myGroup.setGeometricType(myTypes)
00181 
00182 myNumberOfElements = [4]
00183 myGroup.setNumberOfElements(myNumberOfElements)
00184 
00185 index = [1,5]
00186 values = [1,4,5,7]
00187 myGroup.setNumber(index,values)
00188 
00189 myMeshing.addGroup(myGroup)
00190 
00191 myGroup = GROUP()
00192 myGroup.setName("OtherNodes")
00193 myGroup.setMesh(myMeshing)
00194 myGroup.setEntity(MED_NODE)
00195 myGroup.setNumberOfGeometricType(1)
00196 
00197 myTypes = [MED_NONE]
00198 myGroup.setGeometricType(myTypes)
00199 
00200 myNumberOfElements = [3]
00201 myGroup.setNumberOfElements(myNumberOfElements)
00202 
00203 index = [1,4]
00204 values = [2,3,6]
00205 myGroup.setNumber(index,values)
00206 
00207 myMeshing.addGroup(myGroup)
00208 
00209 # on Cell
00210 
00211 myGroup = GROUP()
00212 myGroup.setName("SomeCells")
00213 myGroup.setMesh(myMeshing)
00214 myGroup.setEntity(MED_CELL)
00215 myGroup.setNumberOfGeometricType(3)
00216 
00217 myTypes = [MED_TETRA4,MED_PYRA5,MED_HEXA8]
00218 myGroup.setGeometricType(myTypes)
00219 
00220 myNumberOfElements = [4,1,2]
00221 myGroup.setNumberOfElements(myNumberOfElements)
00222 
00223 index = [1,5,6,8]
00224 values = [
00225     2,7,8,12,
00226     13,
00227     15,16
00228     ]
00229 myGroup.setNumber(index,values)
00230 
00231 myMeshing.addGroup(myGroup)
00232 
00233 myGroup = GROUP()
00234 myGroup.setName("OtherCells")
00235 myGroup.setMesh(myMeshing)
00236 myGroup.setEntity(MED_CELL)
00237 myGroup.setNumberOfGeometricType(2)
00238 
00239 myTypes = [MED_TETRA4,MED_PYRA5]
00240 myGroup.setGeometricType(myTypes)
00241 
00242 myNumberOfElements = [4,1]
00243 myGroup.setNumberOfElements(myNumberOfElements)
00244 
00245 index = [1,5,6]
00246 values = [
00247     3,4,5,9,
00248     14
00249     ]
00250 myGroup.setNumber(index,values)
00251 
00252 myMeshing.addGroup(myGroup)
00253 
00254 # on Face
00255 
00256 myGroup = GROUP()
00257 myGroup.setName("SomeFaces")
00258 myGroup.setMesh(myMeshing)
00259 myGroup.setEntity(MED_FACE)
00260 myGroup.setNumberOfGeometricType(2)
00261 
00262 myTypes = [MED_TRIA3,MED_QUAD4]
00263 myGroup.setGeometricType(myTypes)
00264 
00265 myNumberOfElements = [2,3]
00266 myGroup.setNumberOfElements(myNumberOfElements)
00267 
00268 index = [1,3,6]
00269 values = [
00270     2,4,
00271     5,6,8
00272     ]
00273 myGroup.setNumber(index,values)
00274 
00275 myMeshing.addGroup(myGroup)
00276 
00277 myGroup = GROUP()
00278 myGroup.setName("OtherFaces")
00279 myGroup.setMesh(myMeshing)
00280 myGroup.setEntity(MED_FACE)
00281 myGroup.setNumberOfGeometricType(1)
00282 
00283 myTypes = [MED_TRIA3]
00284 myGroup.setGeometricType(myTypes)
00285 
00286 myNumberOfElements = [2]
00287 myGroup.setNumberOfElements(myNumberOfElements)
00288 
00289 index = [1,3]
00290 values = [
00291     1,3
00292     ]
00293 myGroup.setNumber(index,values)
00294 
00295 myMeshing.addGroup(myGroup)
00296 
00297 # saving of the generated mesh in MED and VTK format
00298 
00299 driver = MED_MESH_WRONLY_DRIVER( med22FileName, myMeshing )
00300 myMeshing.write(driver)
00301 
00302 myMeshing.write(VTK_DRIVER,vtkFileName)
00303 
00304 # we build now 8 fields : 4 fields double (integer) :
00305 #                         2 fields on nodes (cells) :
00306 #                         1 scalar (vector)
00307 
00308 supportOnNodes = myMeshing.getSupportOnAll(MED_NODE)
00309 numberOfNodes = supportOnNodes.getNumberOfElements(MED_ALL_ELEMENTS)
00310 
00311 supportOnCells = myMeshing.getSupportOnAll(MED_CELL)
00312 numberOfCells = supportOnCells.getNumberOfElements(MED_ALL_ELEMENTS)
00313 
00314 fieldDoubleScalarOnNodes = FIELDDOUBLE(supportOnNodes,1)
00315 fieldDoubleScalarOnNodes.setName("fieldScalarDoubleNode")
00316 fieldDoubleScalarOnNodes.setIterationNumber(-1)
00317 fieldDoubleScalarOnNodes.setOrderNumber(-1)
00318 fieldDoubleScalarOnNodes.setTime(0.0)
00319 
00320 fieldDoubleScalarOnNodes.setComponentName(1,"Vx")
00321 fieldDoubleScalarOnNodes.setComponentDescription(1,"comp1")
00322 fieldDoubleScalarOnNodes.setMEDComponentUnit(1,"unit1")
00323 
00324 fieldDoubleVectorOnNodes = FIELDDOUBLE(supportOnNodes,spaceDimension)
00325 fieldDoubleVectorOnNodes.setName("fieldVectorDoubleNode")
00326 fieldDoubleVectorOnNodes.setIterationNumber(-1)
00327 fieldDoubleVectorOnNodes.setOrderNumber(-1)
00328 fieldDoubleVectorOnNodes.setTime(0.0)
00329 
00330 fieldDoubleVectorOnNodes.setComponentName(1,"Vx")
00331 fieldDoubleVectorOnNodes.setComponentDescription(1,"comp1")
00332 fieldDoubleVectorOnNodes.setMEDComponentUnit(1,"unit1")
00333 fieldDoubleVectorOnNodes.setComponentName(2,"Vy")
00334 fieldDoubleVectorOnNodes.setComponentDescription(2,"comp2")
00335 fieldDoubleVectorOnNodes.setMEDComponentUnit(2,"unit2")
00336 fieldDoubleVectorOnNodes.setComponentName(3,"Vz")
00337 fieldDoubleVectorOnNodes.setComponentDescription(3,"comp3")
00338 fieldDoubleVectorOnNodes.setMEDComponentUnit(3,"unit3")
00339 
00340 fieldDoubleScalarOnCells = FIELDDOUBLE(supportOnCells,1)
00341 fieldDoubleScalarOnCells.setName("fieldScalarDoubleCell")
00342 fieldDoubleScalarOnCells.setIterationNumber(-1)
00343 fieldDoubleScalarOnCells.setOrderNumber(-1)
00344 fieldDoubleScalarOnCells.setTime(0.0)
00345 
00346 fieldDoubleScalarOnCells.setComponentName(1,"Vx")
00347 fieldDoubleScalarOnCells.setComponentDescription(1,"comp1")
00348 fieldDoubleScalarOnCells.setMEDComponentUnit(1,"unit1")
00349 
00350 fieldDoubleVectorOnCells = FIELDDOUBLE(supportOnCells,spaceDimension)
00351 fieldDoubleVectorOnCells.setName("fieldVectorrDoubleCell")
00352 fieldDoubleVectorOnCells.setIterationNumber(-1)
00353 fieldDoubleVectorOnCells.setOrderNumber(-1)
00354 fieldDoubleVectorOnCells.setTime(0.0)
00355 
00356 fieldDoubleVectorOnCells.setComponentName(1,"Vx")
00357 fieldDoubleVectorOnCells.setComponentDescription(1,"comp1")
00358 fieldDoubleVectorOnCells.setMEDComponentUnit(1,"unit1")
00359 fieldDoubleVectorOnCells.setComponentName(2,"Vy")
00360 fieldDoubleVectorOnCells.setComponentDescription(2,"comp2")
00361 fieldDoubleVectorOnCells.setMEDComponentUnit(2,"unit2")
00362 fieldDoubleVectorOnCells.setComponentName(3,"Vz")
00363 fieldDoubleVectorOnCells.setComponentDescription(3,"comp3")
00364 fieldDoubleVectorOnCells.setMEDComponentUnit(3,"unit3")
00365 
00366 fieldIntScalarOnNodes = FIELDINT(supportOnNodes,1)
00367 fieldIntScalarOnNodes.setName("fieldScalarIntNode")
00368 fieldIntScalarOnNodes.setIterationNumber(-1)
00369 fieldIntScalarOnNodes.setOrderNumber(-1)
00370 fieldIntScalarOnNodes.setTime(0.0)
00371 
00372 fieldIntScalarOnNodes.setComponentName(1,"Vx")
00373 fieldIntScalarOnNodes.setComponentDescription(1,"comp1")
00374 fieldIntScalarOnNodes.setMEDComponentUnit(1,"unit1")
00375 
00376 fieldIntVectorOnNodes = FIELDINT(supportOnNodes,spaceDimension)
00377 fieldIntVectorOnNodes.setName("fieldVectorIntNode")
00378 fieldIntVectorOnNodes.setIterationNumber(-1)
00379 fieldIntVectorOnNodes.setOrderNumber(-1)
00380 fieldIntVectorOnNodes.setTime(0.0)
00381 
00382 fieldIntVectorOnNodes.setComponentName(1,"Vx")
00383 fieldIntVectorOnNodes.setComponentDescription(1,"comp1")
00384 fieldIntVectorOnNodes.setMEDComponentUnit(1,"unit1")
00385 fieldIntVectorOnNodes.setComponentName(2,"Vy")
00386 fieldIntVectorOnNodes.setComponentDescription(2,"comp2")
00387 fieldIntVectorOnNodes.setMEDComponentUnit(2,"unit2")
00388 fieldIntVectorOnNodes.setComponentName(3,"Vz")
00389 fieldIntVectorOnNodes.setComponentDescription(3,"comp3")
00390 fieldIntVectorOnNodes.setMEDComponentUnit(3,"unit3")
00391 
00392 fieldIntScalarOnCells = FIELDINT(supportOnCells,1)
00393 fieldIntScalarOnCells.setName("fieldScalarIntCell")
00394 fieldIntScalarOnCells.setIterationNumber(-1)
00395 fieldIntScalarOnCells.setOrderNumber(-1)
00396 fieldIntScalarOnCells.setTime(0.0)
00397 
00398 fieldIntScalarOnCells.setComponentName(1,"Vx")
00399 fieldIntScalarOnCells.setComponentDescription(1,"comp1")
00400 fieldIntScalarOnCells.setMEDComponentUnit(1,"unit1")
00401 
00402 fieldIntVectorOnCells = FIELDINT(supportOnCells,spaceDimension)
00403 fieldIntVectorOnCells.setName("fieldVectorrIntCell")
00404 fieldIntVectorOnCells.setIterationNumber(-1)
00405 fieldIntVectorOnCells.setOrderNumber(-1)
00406 fieldIntVectorOnCells.setTime(0.0)
00407 
00408 fieldIntVectorOnCells.setComponentName(1,"Vx")
00409 fieldIntVectorOnCells.setComponentDescription(1,"comp1")
00410 fieldIntVectorOnCells.setMEDComponentUnit(1,"unit1")
00411 fieldIntVectorOnCells.setComponentName(2,"Vy")
00412 fieldIntVectorOnCells.setComponentDescription(2,"comp2")
00413 fieldIntVectorOnCells.setMEDComponentUnit(2,"unit2")
00414 fieldIntVectorOnCells.setComponentName(3,"Vz")
00415 fieldIntVectorOnCells.setComponentDescription(3,"comp3")
00416 fieldIntVectorOnCells.setMEDComponentUnit(3,"unit3")
00417 
00418 for i in range(numberOfNodes):
00419     valueInt1 = i+1
00420     valueInt2 = i+2
00421     valueInt3 = i+3
00422     valueDbl1 = valueInt1*0.1
00423     valueDbl2 = valueInt2*0.1
00424     valueDbl3 = valueInt3*0.1
00425     fieldDoubleScalarOnNodes.setValueIJ(i+1,1,valueDbl1)
00426 
00427     fieldIntScalarOnNodes.setValueIJ(i+1,1,valueInt1)
00428 
00429     fieldDoubleVectorOnNodes.setValueIJ(i+1,1,valueDbl1)
00430     fieldDoubleVectorOnNodes.setValueIJ(i+1,2,valueDbl2)
00431     fieldDoubleVectorOnNodes.setValueIJ(i+1,3,valueDbl3)
00432 
00433     fieldIntVectorOnNodes.setValueIJ(i+1,1,valueInt1)
00434     fieldIntVectorOnNodes.setValueIJ(i+1,2,valueInt2)
00435     fieldIntVectorOnNodes.setValueIJ(i+1,3,valueInt3)
00436 
00437 for i in range(numberOfCells):
00438     valueInt1 = i+1
00439     valueInt2 = i+2
00440     valueInt3 = i+3
00441     valueDbl1 = valueInt1*0.1
00442     valueDbl2 = valueInt2*0.1
00443     valueDbl3 = valueInt3*0.1
00444     fieldDoubleScalarOnCells.setValueIJ(i+1,1,valueDbl1)
00445 
00446     fieldIntScalarOnCells.setValueIJ(i+1,1,valueInt1)
00447 
00448     fieldDoubleVectorOnCells.setValueIJ(i+1,1,valueDbl1)
00449     fieldDoubleVectorOnCells.setValueIJ(i+1,2,valueDbl2)
00450     fieldDoubleVectorOnCells.setValueIJ(i+1,3,valueDbl3)
00451 
00452     fieldIntVectorOnCells.setValueIJ(i+1,1,valueInt1)
00453     fieldIntVectorOnCells.setValueIJ(i+1,2,valueInt2)
00454     fieldIntVectorOnCells.setValueIJ(i+1,3,valueInt3)
00455 
00456 driver = MED_FIELDDOUBLE_WRONLY_DRIVER(med22FileName,fieldDoubleScalarOnNodes)
00457 driver.open()
00458 driver.write()
00459 driver.close()
00460 
00461 driver = MED_FIELDINT_WRONLY_DRIVER(med22FileName,fieldIntScalarOnNodes)
00462 fieldIntScalarOnNodes.write(driver)
00463 
00464 idMedV22 = fieldDoubleScalarOnCells.addDriver(MED_DRIVER,med22FileName)
00465 fieldDoubleScalarOnCells.write(idMedV22)
00466 
00467 fieldDoubleVectorOnNodes.write(MED_DRIVER,med22FileName)
00468 fieldIntVectorOnNodes.write   (MED_DRIVER,med22FileName)
00469 fieldIntScalarOnCells.write   (MED_DRIVER,med22FileName)
00470 fieldDoubleVectorOnCells.write(MED_DRIVER,med22FileName)
00471 fieldIntVectorOnCells.write   (MED_DRIVER,med22FileName)
00472 
00473 
00474 idVtk = fieldDoubleScalarOnNodes.addDriver(VTK_DRIVER,vtkFileName,fieldDoubleScalarOnNodes.getName())
00475 fieldDoubleScalarOnNodes.writeAppend(idVtk)
00476 
00477 idVtk = fieldIntScalarOnNodes.addDriver(VTK_DRIVER,vtkFileName,fieldIntScalarOnNodes.getName())
00478 fieldIntScalarOnNodes.writeAppend(idVtk)
00479 
00480 idVtk = fieldDoubleVectorOnNodes.addDriver(VTK_DRIVER,vtkFileName,fieldDoubleVectorOnNodes.getName())
00481 fieldDoubleVectorOnNodes.writeAppend(idVtk)
00482 
00483 idVtk = fieldIntVectorOnNodes.addDriver(VTK_DRIVER,vtkFileName,fieldIntVectorOnNodes.getName())
00484 fieldIntVectorOnNodes.writeAppend(idVtk)
00485 
00486 idVtk = fieldDoubleScalarOnCells.addDriver(VTK_DRIVER,vtkFileName,fieldDoubleScalarOnCells.getName())
00487 fieldDoubleScalarOnCells.writeAppend(idVtk)
00488 
00489 idVtk = fieldIntScalarOnCells.addDriver(VTK_DRIVER,vtkFileName,fieldIntScalarOnCells.getName())
00490 fieldIntScalarOnCells.writeAppend(idVtk)
00491 
00492 idVtk = fieldDoubleVectorOnCells.addDriver(VTK_DRIVER,vtkFileName,fieldDoubleVectorOnCells.getName())
00493 fieldDoubleVectorOnCells.writeAppend(idVtk)
00494 
00495 idVtk = fieldIntVectorOnCells.addDriver(VTK_DRIVER,vtkFileName,fieldIntVectorOnCells.getName())
00496 fieldIntVectorOnCells.writeAppend(idVtk)