Mesh conversion between MED and OpenFOAM formats : How do you go about it?

Hello everybody!
In this quick tutorial, I’d like to talk about the possible links between the OpenFOAM platform and some tools developed by EDF R&D : Code_Saturne and Salome. Let’s have a look at this! 🙂

Context

About OpenFOAM

OpenFOAM[1][2][3] (Open-source Field Operation And Manipulation) is an open-source CFD software licensed under the GNU General Public License Version 3, with a very large range of features concerning pre-processing, numerical and physics solvers, and post-processing. In particular, this platform contains mesh tools such as blockMesh, snappyHexMesh, or CfMesh in the OpenCFD fork. The most sophisticated of these, snappyHexMesh, is based on an octree volume-to-surface approach, very useful for obtaining a good quality hex-dominant mesh, at the cost of a certain complexity in implementation and some limitations in the case of complex surfaces (snapping problems).

About Code_Saturne, Salome and MED format

Code_Saturne[4] and Salome[5] are open source software developed by EDF R&D. Code_Saturne is dedicated to CFD simulations, whereas Salome is a multi-functional platform including CAD and mesh generation among other things. Salome offers a wide range of mesh algorithms (tetgen, gmsh, hexablock, etc.), but not the possibility to have a semi-automatic generation of hexa-dominant mesh (it’s only possible with a MG commercial licence). In these software, meshes are especially based on the use of the MED file format, which is a specialization of the HDF5 standard.

foam to MED, MED to foam : why?

As mentioned above, as things stand, mesh generation using Openfoam or Salome does not have the same advantages and disadvantages. For example, in some cases, the use of a hex-dominant mesh in Code_Saturne can speed up or improve convergence. On the other hand, the possibility to easily generate a tetra-dominant mesh with a good matching between the surface elements and the geometry can be useful in OpenFOAM.

Unfortunately, there is no official direct way to convert a foam mesh to a format readable by Code_Saturne, and vice-versa. However, the possibility exists. The following sections describe methods I use to do this. Note, however, that this is not necessarily exhaustive!

Prerequisites

This tutorial was written using the following software versions and libraries :

  • OpenFOAM v2412
  • Code_Saturne 8.0
  • Salome 9.14
  • Paraview 5.13
  • MED 4.1.1
  • HDF5 1.10.6
  • CGNS 4.3.0

Foam to MED conversion

Case description

Let’s consider a very simple case, presented in Figure 1, with one inlet, one outlet and two walls. A basic data set in snappyHexMesh allows to obtain the presented mesh in Figure 2 (the mesh relevance is not addressed here), composed of 95% of hexahedra with some prisms and polyhedra.

geometry
Figure 1 - Considered geometry
mesh
Figure 2 - Obtained mesh with SHM

Conversion to MED format for Code_Saturne

The first step is to use Paraview to export the OpenFOAM mesh to .cgns format, which is readable by Salome. To do this, we have to load the mesh in Paraview in the usual way, and then just use File/Save Data by choosing CGNS Files in the available formats (cf. Figure 3).

cgns_export
Figure 3 - Export to cgns format with Paraview

In theory, we should be able to use this mesh in Code_Saturne which can read CGNS format. However, the export generated by Paraview contains multiple bases (a volume base and a surface base) and this is not supported by Code_Saturne. So we have to do a quick manipulation, which can be done with Salome. After opening the platform and selecting the mesh module, we can import the generated cgns mesh. It’s then possible to see the two mentionned bases in the object browser, named “Base_Volume_Elements” and “Base_Surface_Elements”. We just have to build a compound mesh by selecting these two objects, as illustrated in the Figure 4.

salome_windows
Figure 4 - Building of a compound mesh with Salome

We can see the new compound mesh is composed of one volume group and 4 faces groups, as expected. Once that’s done, all that remains to be done is to export the mesh in MED format with File/Export/MED file.

Validation of the MED format mesh

In order to check the consistency of the created mesh in MED format, a good test is a preprocessing step proposed by Code_Saturne, named “mesh quality criteria”. Figure 5 shows some extracts of the mesh analysis respectively done with OpenFOAM (checkMesh utility) and Code_Saturne. We can see following points :

  • Same number of points, internal faces, boundary faces and cells;
  • Relevant groups;
  • Mesh structure coherency is OK.

To finish, a quick simulation test with few iterations allows to confirm the validity of the converted mesh.

mesh quality shm
Figure 5 - Results of mesh analysis for the mesh created with SHM, respectively with OpenFOAM (left) and Code_Saturne (right)

So that’s done, we converted an OpenFOAM mesh in MED format readable by Code_Saturne. Now let’s see how to proceed how to perform the reverse manipulation.

MED to Foam conversion

Case description

Let’s return to our simple case presented in Figure 1. A basic data set in Salome allows to obtain the presented mesh in Figure 6, composed of tetrahedrons with a prismatic boundary layer.

mesh_tetra
Figure 6 - Obtained mesh with Salome

Conversion to OpenFOAM format

The first step is to export the created mesh to UNV format with Salome. To do this, we just have to select the considered mesh and export it with right click/export/UNV file (cf. Figure 7).

Important note : the UNV format doesn’t support pyramidal elements. If such elements are present in the mesh, this manipulation will not work, and the conversion is not possible with this method.

convert_unv
Figure 7 - Export to cgns format with Paraview

Once we have the mesh in UNV format, we can place it in our usual OpenFOAM case directory, and load the OpenFOAM environment. Then, we have to use the following command :

ideasUnvToFoam [options] <unv_file>

Something like Figure 8 should appear :

unvToFoam
Figure 8 - Results of the ideasUnvToFoam utility
update_bc
Figure 9 - Updating of the required boundary condition type

We can see by default, all boundary conditions are set to “patch”. So we have to go in constant/polyMesh/boundary to update the type of boundary conditions which are not patch, as illustrated in Figure 9. The mesh is then ready to be checked.

Validation of the Foam format mesh

In order to check the consistency of the created mesh in FOAM format, Figure 10 shows some extracts of the mesh analysis respectively done with OpenFOAM (checkMesh utility) and Code_Saturne. Once again, we can see following facts :

  • Same number of points, internal faces, boundary faces and cells;
  • Relevant groups;
  • Mesh structure coherency is OK.

As previously, a quick simulation test with few iterations allows to confirm the validity of the converted mesh.

mesh_quality_tetra
Figure 10 - Results of mesh analysis for the mesh created with Salome, respectively with OpenFOAM (left) and Code_Saturne (right)

And that’s all, the conversion process from Salome to Foam format is over!

Some alternatives

The two methods I presented here are the ones I usually use. However and obviously, it’s not the only way to do this. In particular, I would like to mention here some alternatives I know. However, please note that the tools described below are not official, and are or may become obsolete at any time.

Foam to MED conversion : the foamMeshToMED utility

Some GitHub contributors release an utility named foamMeshToMED. The most recent version is at the following link. This is a package to compile in order to include it in the OpenFOAM environment. Despite the name of the package, this tool works well up to the v2312 release (I didn’t use it since this version). It allows a direct conversion from foam to MED format.

Salome to Foam conversion : python script

Concerning the conversion from Salome to Foam format, there is an available python script on GitHub at the following link. I’ve never had the chance to use it myself, and it seems quite old, so I don’t know if it still works.

Thank you for reading this article, and for visiting this website! I hope you found this tutorial useful 🙂