OpenFOAM related export options (eMesh, fms, polyMesh)

I think many people would highly appreciate built-in export methods such as:

  • Edge mesh as “eMesh” file. (also obj)
  • Mesh entity as “fms” (cfMesh format)
  • 3D mesh as polyMesh folder. (OpenFOAM mesh)

There are already scripts for those but not so convinient.
At least “eMesh” exporter might be a cost efficient addition.

(If there are such options available in current versions that I don’t know of, I’m sorry in advance and please educate me)

here you have for exporting of
fms External OpenFOAM python script not running after update to Salome 9.9 - #10 by cbourcier
eMesh, have a look at how it looks it is quite simple to do a script for exporting it. last time I needed I did a script couldn’t find it right now but it is super easy/simple for creating it.
polyMesh, GitHub - EastEriq/salomeToOpenFOAM: A python script that outputs a Salome mesh to OpenFOAM

currently there is no build in export.
for polyMesh also IF YOU DONT HAVE PYRAMIDS:

  1. remove 1D groups of your mesh in salome
  2. export in UNV format
  3. use build in ideasUnvToFoam (this step will fail if you have 1D groups before exporting it)
    I have experience other times that ideasUnvToFoam fails, never bothered to look why. but remove everything that are not patches info and you should be ok

also, salome now is on github. you are more than welcome to create your tools and ask for a merge for so this such tools are ‘build-in’ for other future versions

1 Like

Dear Franco,

Thank you for your reply. I am aware of these solutions (work arounds) and I was using all these for a while now. But I still I don’t think these methods are very convenient, and I think people can benefit from clean built-in export options. Let me elaborate:

  • To export a feature edge mesh, I need to load a script, write command lines in correct syntax, select certain items while doing these.
  • If I generate geometry in Shaper, the mesh object has an id starting with 0:1:4:* (instead of 0:1:2:*) and it doesn’t act as a mesh for scripts that export mesh to polyMesh. I found a work around by sheer luck.
  • PolyMesh exporting scripts work with pyramids too btw, where unv-export & ideasToFoam fails as you also mentioned.
  • I must also mention that, I didn’t enjoy spending time and effort to search if and how I can export such mesh items and if there are better ways to do so, considering it is something so regular as exporting the mesh.

I thought that not all improvement suggestions have to be to add something that Salome can’t do, but also to improve usage experience. Personally, as a Salome user, I would be happy if exporting mesh becomes more reliable and robust.

About participating in Salome improvements, I’m not that experienced in Salome or Python to be honest. But thank you for reminding me that there is such an option, I would like to give some efforts in the future.

Cheers.

I know, but the polyMesh exporting script is WAY slower than the ideas workflow. specially for big meshes.

1 Like

Hi,
there was script posted in the old forum for exporting a group of edges into an eMesh file.

I have slightly modified it to be compatible with Python-3.

# Original source by Saint Michael
#   January 13. 2015
#   http://www.salome-platform.org/forum/forum_10/714480261

Here’s the code. Have fun.

# Known to work:
#   Salome 8.2 & OpenFOAM-4.0 (snappyHexMesh)

# name of eMesh file
eMeshFile = "/tmp/test.eMesh"

# get a selected mesh group of edges

import salome, SMESH
from salome.gui import helper

segGroupSO = helper.getSObjectSelected()[0]
if not segGroupSO:
    raise RuntimeError("Select a mesh group of edges to write!")

segGroup = segGroupSO.GetObject()
if not segGroup:
    meshComp = salome.myStudy.FindObjectByPath("/Mesh").GetObject()
    if not meshComp:
        raise RuntimeError("Activate Mesh module!")
    else:
        raise RuntimeError("Select a mesh group of edges to write!")
if not hasattr( segGroup, "GetType" ):
    raise RuntimeError("Select a mesh group!")
if segGroup.GetType() != SMESH.EDGE:
    raise RuntimeError("Select a group of edges!")

# write eMesh file

# points(
# (0 0 0) // point 1
# (1 0 0) // point 2
# (1 1 0) // point 3
# );
# edges(
# (1 2) // edge between points 1 and 2
# (2 3) // edge between points 2 and 3
# );

n2p = {} # used to keep track of points to avoid duplication of points in the final eMesh file
points = []
edges  = []
mesh = segGroup.GetMesh()
# loop over all edges
for seg in segGroup.GetIDs():
    edgePoints = []
    # loop over all nodes of current edge
    for n in mesh.GetElemNodes( seg ):
        curPointCoord = mesh.GetNodeXYZ(n)
        # has point n already been added to points
        if list(n2p.values()).__contains__(curPointCoord):
            pass
        else:
            points.append( curPointCoord )
            n2p[n] = curPointCoord
        # end if
        edgePoints.append( points.index(curPointCoord) )
        pass
    edges.append( edgePoints )
    pass

if not points:
    raise RuntimeError("No segments in the group %s" % segGroup.GetName())

outFile = open( eMeshFile, "w" )

outFile.write( "/*--------------------------------*- C++ -*----------------------------------*\\\n" )
outFile.write( "| =========                 |                                                 |\n" )
outFile.write( "| \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox           |\n" )
outFile.write( "|  \\    /   O peration     | Version:  4.1                                   |\n" )
outFile.write( "|   \\  /    A nd           | Web:      www.OpenFOAM.org                      |\n" )
outFile.write( "|    \\/     M anipulation  |                                                 |\n" )
outFile.write( "\*---------------------------------------------------------------------------*/\n" )
outFile.write( "FoamFile\n" )
outFile.write( "{\n" )
outFile.write( "    version     2.0;\n" )
outFile.write( "    format      ascii;\n" )
outFile.write( "    class       IOobject;\n" )
outFile.write( "    location    "";\n" )
outFile.write( "    object      test.eMesh;\n" )
outFile.write( "}\n" )
outFile.write( "// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //\n" )



outFile.write( "// points\n" )
outFile.write( "%s(\n" %(points.__len__()) )

for xyz in points:
    outFile.write( "(%s %s %s)\n" %( xyz[0], xyz[1], xyz[2]))
outFile.write( ")\n" )


outFile.write( "// edges\n" )
outFile.write( "%s(\n" %(edges.__len__()) )

for pp in edges:
    outFile.write( "(%s %s)\n" %( pp[0], pp[1] ))
outFile.write( ")\n" )

print("%s written"%eMeshFile)
2 Likes

Thank you for sharing the code.

I truly appreciate all the comments and suggestions to help with exporting meshes. Please don’t take me wrong; your input is invaluable.

However, my main concern in raising this issue wasn’t about being unable to export these mesh items. Rather, it was to highlight that relying on external scripts for something as fundamental as exporting a mesh is not very convenient.

That said, this is just my personal opinion. If it’s taken into consideration, that’s fantastic! If not, the methods mentioned here and elsewhere still work. They may not be ideal, but they’re a good alternative.

Cheers!