From CAELinuxWiki
Revision as of 21:30, 26 October 2010 by JMB (Talk | contribs) (Add: Salome forum URL)

Jump to: navigation, search

Python script to create a CFD mesh of Hexahedral & Tetrahedral elements (Topic under construction)

This short tutorial is based largely on contributions of others in the Salome forum. It is an elegant example of utilizing a python script to create a well structured hexahedral mesh around a tetrahedral mesh all in 3D. The script is posted at:, but is reproduced here with extensive embedded comments for the python novice. You will also need the test.brep file contained in which should be saved to any convenient place such as /tmp as used for the demonstration here.


Download the zip file from the Salome forum or here: (it was just renamed here for clarity, but the contents are the same as of the Salome forum. Extract the BREP file inside it to the /tmp subdirectory.

cd /tmp
unzip <pathname>/

Copy and paste the script below to any text editor (gedit, kedit, nedit, etc) then save it to any convenient location (say ~/ i.e. your home directory or even /tmp again)

Start Salome and open a new study in the GEOM module. Use File -> Load Script (or [Ctrl-T] and select the script from where you had saved it. Salome will process the script and when you switch to the MESH module's VTK display, you will see a mesh as seen below:

the geometry: Salome-TUI-HexaTetra-Mesh1.png

and the mesh: Salome-TUI-HexaTetra-Mesh2.png

The script:

   from smesh import *
   # Setup the current study called 'myStudy'
   # 1.  Set the full path and name of the BREP file and
   # 2.  Import it into Salome, 
   # 3.  then publish it so it will show up in the Object Browser window pane
   brep_file = "\/tmp\/test.brep"
   # *** Remove the 2 "\" above. Without it this website's Wiki page does not work!
   test_brep_1 = geompy.ImportBREP(brep_file)
   # 4.  Explode the 3rd sub-solid of the object 'test_brep_1'
   # 5.  and publish it in the Object Browser window pane as a child of 'test_brep_1'
   inner_box = geompy.SubShapeAllSorted(test_brep_1, geompy.ShapeType["SOLID"])[3]
   geompy.addToStudyInFather( test_brep_1, inner_box, "inner box" )
   # 6.  Explode all the sub-faces of the object test_brep_1
   ff = geompy.SubShapeAllSorted(test_brep_1, geompy.ShapeType["FACE"])
   # 7.  Select specific faces which will be needed for local mesh
   #     hypothesis using a 'for' loop
   inbox_tria_faces = [ ff[i] for i in [6,13,15,19,20,21]]
   # 8.  Make a compound using the list 'inbox_tria_faces'
   # 9.  and publish it in the Object Browser window pane as a child of 'test_brep_1'
   inbox_tria_faces = geompy.MakeCompound(inbox_tria_faces)
   geompy.addToStudyInFather( test_brep_1, inbox_tria_faces, "inbox_tria_faces" )
   # 10. Explode all the sub-edges of the object 'test_brep_1'
   ee = geompy.SubShapeAllSorted(test_brep_1, geompy.ShapeType["EDGE"])
   # 11. Select specific edges which will be needed for local mesh
   #     hypothesis using a 'for' loop
   inbox_edges = [ ee[i] for i in [12,13,14,15,24,25,26,27,28,29,30,31]]
   # 12. Make a compound using the list inbox_edges
   # 13. and publish it in the Object Browser window pane as a child of 'test_brep_1'
   inbox_edges = geompy.MakeCompound(inbox_edges)
   geompy.addToStudyInFather( test_brep_1, inbox_edges, "inbox_edges" )
   # 14. Define a mesh called 'Mesh'
   # 15. Set the global 1D hypothesis to 5 segments per edge
   # 16. Set the local 1D hypothesis to 15 segments per edge for 
   #     all edges in the list 'inbox_edges'
   # 17. Set the global algorithm as quadrangle
   # 18. Set the local 2D algorithm as triangles for
   #     the faces in the list 'inbox_tria_faces
   # 19. Set the global 3D algorithm to hexahedron
   # 20. Set the local 3D algorithm to tetrahedron for
   #     the solid 'inner_box'
   # 21. Finally compute the mesh !
   mesh = Mesh(test_brep_1)


This page was made possible by the help of S. Michael and the topic raised by vaina, who provided the BREP and STEP model. Thank you.

The script was tested and functional on Salome 5.1.4 under Ubuntu Jaunty 9.04 64bit installed from binaries.

Disclaimer: This page is still evolving and omissions or errors are a regrettable part of such a process. So user beware! -JMB

A new page