Script for converting a mesh from Salome-Platform to OpenFOAM
Hi all,
I'd like to share a python script that converts a Salome mesh to OpenFOAM. It's really easy to use. Just create the geometry and mesh in salome, select the mesh you want to export and go to file-> load script, select salomeToOpenFOAM and the script will convert your mesh to NameOfMesh/constant/polyMesh. You can download it from https://github.com/nicolasedh/salomeToOpenFOAM. I've included a couple of sample mesh in the sample*py files. I've been searching the forums for a way to convert a mesh from Salome-Platform to OpenFOAM but I haven't found an easy way. Most frequently people seem to suggest that you save the mesh in unv-format and then use ideasUnvToFoam. The downside is that the unv format doesn't support pyramids. Another option I've found is to save the mesh in gmsh-format and then use one specific version of gmsh to save the mesh in another specific version then finally use gmshToFoam. Then there is pythonFlu, which I suppose can do the same thing but is a bit overkill for my purposes. Anyways I hope you find it useful. As people tend to point out, this script is in no way endorsed by anyone but me nor am I affiliated with anyone but myself... Happy foaming Nicolas |
Hi,
I've just added support for internal patches, i.e. baffles or inter-region patches. The script has been tested with Salome 7.2 and OpenFOAM 2.2.x. Although I see no reason why it shouldn't work on other version of OpenFOAM. Included in the git repository are several sample scripts that creates a mesh using salome and exports it to OpenFOAM. For instance there is sampleMultiRegionPipeWithViscous.py which creates a pipe with a spherical solid inside. Both the pipe and the solid are meshed using layers. The mesh is exported to openfoam with cellZones for each of the regions. Just run Code:
splitMeshRegions -cellZones -overwrite Let me know if you find something that doesn't work or have any ideas for improvement. Best Nicolas |
Thanks a lot!
Hi Nicolas, just these days I was looking for some tool like that! I definitely will give it a try (also with older versions of OpenFOAM) with some mean meshes I am building in Salome. Of course I will inform you about any problems - as well as about tested capabilities! ;-) Thank you very much for that tool already now! Cheers, Bernhard
|
Hi Nicholas,
thanks a lot for your effort. I tried your script, but I ran into following error, can you tell me what does it mean? Code:
|
Hi Michele,
Thanks for posting back the problem. All I can see is that there is an issue when you write the boundary file. I haven't seen that particular error before. Would it be possible for you to upload a python-dump file so I can try it my self? I haven't tested the script on all possible combinations of meshes so it could be a bug I haven't seen. Otherwise you could try to set debug=2 on line 51 in salomeToOpenFOAM and see if you can find the error. Best Nicolas |
1 Attachment(s)
Hi Nicolas,
sorry for late answer, but I was on a trip. Using your script, I manually perform these steps: 1 - import step file of the geometry (a simple one in this case); 2 - create group of faces and volumes; 3 - mesh the geometry (I used Netgen 3D-2D-1D); 4 - select mesh1 and load the script. Is this a right workflow? Anyway, I tried to raise debug level up to 2, here's the message: Code:
p, li { white-space: pre-wrap; } >>> execfile(r"/home/geeko/Documents/salome/salomeToOpenFOAM.py") I tryed to raise debug level up to 3, here the response (I summarized it): Code:
Let me know waht you need.. Regards |
Hi,
Yes either the stl or the hdf would be necessary. Basically every thing I need to recreate your steps. The work flow is OK although I haven't tested to mesh from an existing stl. Netgen should work fine. It isn't necessary to create a group of volumes if you only have one region. I'll try to recreate your steps tonight when I get home. Best regards Nicolas |
1 Attachment(s)
Hi,
enclosed you can find the step file, as you can see is a simple model that I'm using to understand how to set up properly a basic problem concerning internal faces, before going deeper into hard ones. The model is made of 3 cylinders. For example: try to think about the one in the middle as a generic axial fan, where I want to set static pressure BC between suction and delivery. the other two cylinders are generic volume of air: one is on suction line, other is on discharge line. As mentioned bove, I performed meshing using a generic scheme as Netgen 3D-2D-1D with theta hypotesis. Only paramter impose is max size. Hope it can be of help. |
1 Attachment(s)
Here's the hdf file.
It's quitea a rough mesh, but upload size limit dosen't allow large file (97kb ?!?). Regards |
Hi,
Thank you for the files. The good news is that I've found the issue. It might take some time to fix though. For some reason salome reports that your baffles are external faces which confuses the script. I'll have a look at it. In the mean time, it is possible to export internal baffles. Have a look at sampleMultiRegionPipeWithViscous.py. It might work if you create one group of volumes for each cylinder. /Nicolas |
Hi Nicolas,
sampleMultiRegionPipeWithViscosity is what I was looking for: indeed salomeToOpenFoam worked properly with your example. The only difference I found between your sample and mine is that I have pairs of faces connecting each other (see inletFan with wall for example), and only one solid region. In your sample you have several solid region. Michele |
solved (?)
1 Attachment(s)
Hi,
I build the step file building 3 different solids (region) according to the 3 different cylinders and defining group for faces internal patches, walls, ecc... I also created a grouup concerning 3 solid regions called "internal". I ran SalomeToOpenFOAM.py script and worked properly. Attached the screenshot of paraview. So if you have to define internal baffles, you have to define the volume containing them as well. I don't understand if this limitation comes with the OpenFOAM scripts or other: I mean, why is it not possible to have only one solid region with internal faces? Michele |
1 Attachment(s)
Hi,
I'm glad to hear you got it working! Quote:
I think our first task would be to figure out how to create a proper baffle in Salome. Then figure out how to export it. Quote:
For the script to work it needs to be able to distinguish between groups that are regular patches and groups that are internal. Currently the script uses a salome filter to get a list of all faces that lie on the exterior of the domain (SMESH.FT_FreeFaces). If a group of faces has faces that aren't in the list of exterior faces then the group is considered to be a baffle. This should work as long as we can create a proper baffle in Salome. Let me know if you have any success in creating a proper baffle in Salome. After all there are scenarios where one might model a baffle that doesn't lie on the interface between two volumes. /Nicolas |
Hi NIcolas,
well, I "played" a bit after have accomplished to make the script run properly. First: Apart to make the script works, I'm facing some normal usage problem of meshing with Salome; so I'm focusing to understand how to set up a good mesh (viscous layer,ecc...) even if I had the same feeling that something between the mesh doesn't work right! I think I have to "play" a little more before making any conclusion. Second: Meanwhile I would like to set up a case with OpenFOAM and be able to run it (example: fan inside flow domain, conjugate heattransfer solid-gas, ecc...) So my tasks are two, and they require me a lot of time. Anyway what you made is a good starting point for me! Quote:
Let's say that for the moment for me is okay, and I'm going to use your script to generate the mesh I need. I think it's better if I take note of some other bugs i'll face before reporting you. Thank you by now! Michele |
Good luck Michele!
OpenFOAM can seem daunting at first but it is worth the time spent on it. You learn a lot. So good luck with your efforts! For others reading this thread I created a new sample mesh that uses a baffle. The script will export the mesh and it runs. checkMesh will report "multiply connected (shared edge)" and write all the points on the baffle to a set nonManifoldPoints. However, the mesh will run ok. Just do Code:
git pull 1.) Create the fluid domain in (GEOM module) 2.) Create your baffle (in GEOM module) 3.) Use "partion" with the fluid domain as object and the baffle as tool (in GEOM module) 4.) Mesh and create groups from geometry. Be sure to include the baffle as a separate group. Export the mesh to OpenFOAM. /Nicolas |
Hey Nicolas,
this script is exactly what I need right now! Thank you so much for that, I was really having troubles getting a geometry containing pyramids converted. Btw: At first I was using Salome-MECA (I think 2013), and there your script failed due to not finding the smesh module. But using Salome 7.3.0 solved that issue. Thanks again, amazing work! |
Quote:
Thanks for the feedback! It's good to hear the code works for 7.3.0 as well. I haven't tested Salome-MECA and don't know anything about it. Maybe the smesh model isn't included in MECA or maybe something needs to be added to the python path. If you come up with a solution I'm happy to include the changes. /Nicolas |
Hi Nicolas!
Thank you for posting it. Look like it is exactly what i need. Hope I will figure out how to ue your script. |
I can confirm that the script works for 7.3.0 the same way as in 7.2.0.
@skuznet: The use of the script is a rather simple thing: Within Salome go to the Menu "File" -> "Load Script" and simply chose salomeToOpenFOAM.py from where you saved it. That is all! In the console window of Salome you will see that it first counts different things, then checks the boundaries, and finally the different files are written. In the end it even provides you with the time it took to convert the mesh! In my case, conversion of 80'000 cells takes approximately 20 seconds, 1 million cells takes around 250 to 300 seconds (if I remember correctly), depending on the mesh structure. The computer I am using is a Core i7 vPro, so don't wonder if it takes some time on not-so-new hardware. Nevertheless: It is a great tool and up to now it converted every mesh flawlessly! Thanks again, Nicolas! |
Your welcome Bernhard, it nice to hear that the script is used!
Just be sure to have the mesh you wish to export selected before you run the script. It's also possible to run salomeToOpenFOAM from the python console inside salome. Check out the sample*py scripts that are included. They create different types of meshes and export them. The last 5 lines or so shows how to use the script from the command line. (I've only tested them with Salome 7.2.0). In order to run the script from the command line type the following: Code:
#if you you don't have the script in the current working dir Happy foaming! Nicolas |
Hi Nicolas,
I'm facing one more problem with the script; and I can't understand if there's a problem with python: Code:
can you help me? I'm using Salome version 7.3.0 thanks |
Dear Michele,
though not being Nicolas, maybe I have an idea on the reason of the problem. To me the output looks as if you were calling the script from the top of your root directory. Obviously, your system would not let you as a normal user write data into / . Does the problem occur also if you start Salome (if you call the script from within Salome) or the script (if you call it directly from the shell) from your user home folder? A short side remark to Nicolas nevertheless: Would it be painfully difficult to add a routine asking for the place where the folder should be put? The last time I was using that awesome script it put the folder into the directory from which Salome was called... Cheers, Bernhard |
Quote:
The error messeage (last two lines) would suggest that you don't have write permission where you wan't to store the mesh. Also note one of the first lines Quote:
To change the path, open the python interpreter and type (you can ignore the lines starting with #. Code:
import os Best of luck Nicolas |
Hi Bernhard,
the problem to me arise from within Salome. I perform: select the mesh --> File --> load script --> select salomeToOpenFoam.py (in home/michele/Documents/salomeToOpenFoam.py Never used the call within the shell. Anyway I fear there's something wrong within python 2.7.3, but I don't know how to check. |
Well before you start looking for errors within your Python installation (that definitely would be out of my league, as well), just give it a try if it works when starting Salome from your home folder!
I remember that the location of the script does NOT matter for the position where the mesh folder goes, but the location from which Salome is started definitely does. Try: cd $HOME ; runSalome (or whatever command you use for starting Salome) Let us know if it works, please! |
Bernard, you're right!.
Quote:
Code:
/home/michele/salome/apply_v7_3_0 when I type Code:
import os |
Nicolas was right, too! I just was quicker this one time! ;-)
My opinion: Basically when using applications you never miss root privileges - if they are required, the software is set up wrongly. I strongly suggest not to try anything to breach the root-barrier. It just makes your system more vulnerable. Now comes the part of speculation on my side: Maybe it is possible to change the icon on the desktop in a way it changes to your home directory and starts Salome only after that? How to do that, I would have to experiment, unfortunately. For changing the script or the working directory called from salomeToOpenFOAM.py, I refer to Nicolas. HE is the man concerning the magic script and python. ;-) Cheers, Bernhard |
Hi Bernhard!
I guess you posted while I was still typing =). Michele, as Berhard says you should not need root privileges. It sounds like you've opened a separate "terminal". What I meant was to use a the python console inside salome. Code:
os.getcwd() Code:
os.chdir("/home/YOURuserNAME/somepath/polyMesh") Or you can start salome from another directory by providing the full path to runAppli. Code:
/home/michele/salome/apply_v7_3_0/runAppli Nicolas |
Sorry,
didn't think about it, but now it works well! typing inside Salome solved the problem as well. Thanks a lot again both of you, Nicolas and Bernhard! |
Hi OpenFOAM users,
Do the meshes at the inter-region patches to OpenFOAM have to be conforming? Thanks. - ffan Quote:
|
Hi Nicolas,
I discovered your contribution while I was searching this Forum for other issues, but I think it can be very useful. A.s.a.p. I will test it, using Salomé 7.4 and oF 2.3. I will write you back my feelings. Ciao Piero |
SalomeToOpenFOAM.py with Salome 7.4 and OF 2.3.
Quote:
But in case you bump into problems, please do post it here! In the worst case you trip a heisenbug, in the best case you help some other users not to repeat avoidable mistakes. ;-) |
just a stupid correction
1 Attachment(s)
Hi Nicolas,
I'm sending to you a a very small correction I made, because the out directory was not the correct one. For example, if mesh name is Mesh_1, outdir was "(cwd)/Mesh_1" instead of "(cwd)/Mesh_1/constant/polyMesh". Attached you will find source code, with my correction well signaled (-pici-). Thank you for you attention. Bye Piero |
Thanks!
Quote:
Best Regards Nicolas |
Python scripting
Quote:
I'm really a beginner :o in Python, so I will be obliged if you would explain how is 'different' the manner you used. Many thanks . . . Piero |
Quote:
Code:
if __name__ == "__main__": Now I moved all the code under "__main__" to it's own function so the variables aren't global and only exits in the namespace of that function. /Nicolas |
Hi Nicolas,
Thank you very much. I am using the salomeToOpenFoam.py script in Salome 7.4.0. Your creative work has helped us a lot. Also, your explanations are lucid. Yours Anil Kunwar |
Error while trying the script
Hello,
while I've tried the script I got an error message: >>> execfile(r"/home/user/salomeToOpenFOAM-master/salomeToOpenFOAM.py") Traceback (most recent call last): File "<input>", line 1, in <module> File "/home/user/salomeToOpenFOAM-master/salomeToOpenFOAM.py", line 46, in <module> from salome.smesh import smeshBuilder ImportError: cannot import name smeshBuilder >>> and te script won't continue. My salome-version is 6 / 2013.1 , 32 bit, linux. I would like to know, how to get the script running. Many thanks in advance, Ralf |
Hi Ralph,
I've only tried the scripts with salome versions 7.2,7.3 and 7.4. Most likely you need to install a more recent version of Salome. Best Regards Nicolas |
All times are GMT -4. The time now is 11:04. |