Back to index

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