Launching OpenFOAM commands from FreeCAD script

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
barakk
Posts: 7
Joined: Mon Nov 22, 2021 5:13 pm

Launching OpenFOAM commands from FreeCAD script

Post by barakk »

Hi all,

I have a bash script that executes several OpenFOAM (openfoam.org) commands:

Code: Select all

#!/bin/sh
touch test.txt
source /opt/openfoam9/etc/bashrc
blockMesh > log
snappyHexMesh -overwrite >> log
Now I'm trying to launch this script from FreeCAD using a python script with the method

Code: Select all

os.system('myscript.sh')
While the bash scrip (myscript.sh) works fine when launched from the bash shell, when launched from FreeCAD, the OpenFOAM commands it includes seem to be ignored.
I know that my script does get launched from the FreeCAD script because the

Code: Select all

touch test.txt
command works.
Trying to overcome this problem, I added to my bash script a line that has to be included in the .bashrc file for OpenFOAM to work:

Code: Select all

source /opt/openfoam9/etc/bashrc
with no success.
I believe that it has to do with the OpenFOAM environment not properly loading when called from FreeCAD...
Any clues?

Thank,
Barak K
User avatar
onekk
Veteran
Posts: 6206
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Launching OpenFOAM commands from FreeCAD script

Post by onekk »

Or maybe some different shell used.

you are using "source" and maybe some other commands, that maybe are interpreted correctly by internal shell, that use maybe a symlink to bash or dash when invoked using "/bin/sh"

But maybe internal Python interpreter use a different shell, (or is invoking the shell with some parameter that modify things)

Or maybe Python interpreter has no read access to some commands paths, so the command is not executed due to access rights.

You could try to copy the script and substitute invocation of blockMesh and snappyHexMesh with their complete path.

Hoping it will help.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
barakk
Posts: 7
Joined: Mon Nov 22, 2021 5:13 pm

Re: Launching OpenFOAM commands from FreeCAD script

Post by barakk »

Thanks for the advice, Carlo!
I will surely give it a try and come back to update.
barakk
Posts: 7
Joined: Mon Nov 22, 2021 5:13 pm

Re: Launching OpenFOAM commands from FreeCAD script

Post by barakk »

I used the full path, but it still doesn't work.

Thanks anyway.
User avatar
onekk
Veteran
Posts: 6206
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Launching OpenFOAM commands from FreeCAD script

Post by onekk »

You could try to use this writing to launch a command, it is working with python 3.5 and up.

Code: Select all

import subprocess
   
subprocess.run('date +%a', shell=True)
maybe read Python official documentation about subprocess

Hope it helps

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
barakk
Posts: 7
Joined: Mon Nov 22, 2021 5:13 pm

Re: Launching OpenFOAM commands from FreeCAD script

Post by barakk »

Hi again,

I finally resolved the problem.
The solution consists of two parts:
1) using the non-snap FreeCad installation (It's not clear to me why it doesn't work in the snap version),
2) calling the OpenFOAM app (e.g. checkMesh) using subprocess() rather than os.system() - as already suggested by Carlo D.

Thanks and FYI,
Barak
User avatar
onekk
Veteran
Posts: 6206
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Launching OpenFOAM commands from FreeCAD script

Post by onekk »

probably snap version is more isolated from system settings.
os in the code is the os that python see.

Maybe trying to guess what is the path for executables used by os...

maybe some bin directory is lacking in the default path.

Snaps are made to isolate FC from the system so you have not blame it for doing what is supposed to do.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply