Using python salome libraries from a python IDE

Hello, I’m currently using Windows 10 and trying to create a script to automate meshing and simulation tasks. The main goal is to move nodes based on the stress results, this involves multiples iterations calling salome (to edit the mesh) and aster_study (to run the FEA) from my python IDE (Spyder) using 'Popen" from “subprocess” library.

Ideally, I would like to use salome python libraries by just typing: “import salome”, “import GEOM”, “import SMESH” to edit a mesh, but I don’t have access to these libraries in my python instance - in Spyder IDE. In this scenario I would be able to execute ‘.MoveNode()’ function from my IDE.

The current solution is working but with manual iterations:

path_meshing = 'run_salome.bat -t --pinter'
mesh_creation = Popen(path_meshing, shell = True, cwd=r"\sm-2019-w64-1.2")

This command opens a 1st terminal (CMD) and then call a 2nd terminal (Python) where I can run python commands (and execute my node movements) manually. The problem is that I lose the connection of the 2nd terminal from python IDE, I just have acess to the 1st terminal (CMD).

In conclusion, I have to apply the “nodeMove()” input mannualy because I don’t have acess to the 2nd terminal from python IDE.

Note: I’ve also tried using

path_meshing = 'run_salome.bat -t --pinter MyAutomation.py'
mesh_creation = Popen(path_meshing, shell = True, cwd=r"\sm-2019-w64-1.2")

But the main problem here is that I have to execute this command several times (and also because the ‘MyAutomation.py’ needs to be updated each iteration).
Besides that, the time to open salome each iterations consumes almost 8 seconds. For an example, I’m running basic tests and the salome is consuming almost 80% of the total time (1000s) in just 100 iterations :frowning: .

This currently solution is too slow and also creates multiples terminal windows (making it impossible to use the machine while running the script). Because I’ve to reopen a new instance of salome each time I want to move nodes in my mesh…

I hope someone will be able to help me, thank you!

Hello,

First of all, you do not need an IDE for running scripts that use SALOME modules. While I might understand having spyder can give you better debugging features, I find it useless.

I have tried installing Spyder directly inside the SALOME environment via CMD but it requires so many packages that it essentially borked SALOME. Unless Spyder has a way of running python scripts with modified environments I do not think you will be able to mix the two easily.

One way is to just run the python script via CMD (of course before you need to load SALOME environment) or use a simple editor (I use Sublime Text to run scripts with SALOME environment). IDE’s usually bring too many things and are hard to make them compatible with other products with equally intense environment.

Regards,
Gregor

1 Like

Hi,
Instead of Spyder, you can consider using the Visual Code IDE which can be run from SALOME by selecting in the main menu bar: Tools - Plugins .
See these discussions which might help to setup things:
Visual Studio Code in SALOME

Regarding the usage of Spyder with SALOME, as stressed by Gregor, this can be tricky and risky.
Of course, you can still install Spyder within SALOME at your own risk with the three following commands in Windows:

call run_salome.bat context
python -m pip install --upgrade pip
python -m pip install spyder

Then edit file: SALOME-9.8.0\W64\Python\lib\site-packages\spyder\requirements.py
and at line 46, replace:

    actual_ver = qtpy.QT_VERSION

with:

   actual_ver = '5.12.3'

then simply do:

python -c “import spyder; from spyder.app import start; start.main()”

In principle, you should then be able to use Spyder but again better use Visual Code.

1 Like

Hi Gregor,

I wonder if you can let me know how to set up the SALOME environment to run the python script via the Sublime Text. I am having troubles importing the salome python libraries.

Hello,

essentially, for Windows, you have to do the following:

  1. Open CMD

  2. In CMD go to SALOME directory.

  3. Run env_launch.bat
    Simply type env_launch.bat and press enter

  4. Run sublime text from the same CMD. This means you have to write the full path to the sublime text binary:
    For instance:
    >"c:\Program Files\Sublime Text\sublime_text.exe"
    With this the SALOME environment will be visible to Sublime Text.

  5. Now you also have to add a new Python build (Tools → Build System → New Build System), so that sublime text will launch the SALOME python interpreter instead of system python (causing “fatal cannot import site” errors)

    In the bellow build example, change the path to the python exe accordingly. I named my SALOME python build as SALOME_PYTHON

{
“cmd”: [“f:/SALOME-9.8.0/W64/Python/python.exe”, “-u”, “$file”],
“file_regex”: “[1]File "(…?)", line ([0-9]*)”,
“selector”: “source.python”
}

  1. Now select the new python build in the menu Tools → Build System → SALOME_PYTHON
  2. Write a new python script, try to import salome and it should work. Happy coding

I must say in Linux it’s much easier, just source the env_launch.sh and run sublime text and that’s it. Additionally if you install LSP and LSP-pyright, you can enjoy type hinting and code completion for functions (although sometimes it takes a while for pyright to analyze everything).

Best regards,
Gregor


  1. ↩︎

2 Likes

Hi, Gregor

Thank you very much for your kind and detailed answer for my question!!!

SuJong

Hi Gregor,

I have following questions. I followed up your instruction, but I am still having some trobules. I am working on this with Salome 9.7.0 and sublime text 3.

(1) I wonder if this works only for the SALOME 9.8.0 or 9.7.0 will be fine?
(2) When I printed the system path on the CMD and on sublime text 3, the results were different. It seems that the salome environment was not fully loaded in the sublime text 3. Am I missing something and do I have to repeat Steps 1-4 everytime to use the salome environment?
(3) Due to (2), I can import salome library on CMD but failed on the sublime text 3.
(4) When I call “salome.salome_init()” I got this runtime error as follows:

import salome
salome.salome_init()
th. 00000018EC1AFC80 - Trace e:\s\salome-9.7.0\sources\kernel\src\namingservice\salome_namingservice.cxx [1410] : CORBA::SystemException: unable to contact the naming service
salome.salome_init(): Naming Service Unreacheable
============================================
May be there is no running SALOME session
salome.salome_init() is intended to be used
within an already running session
============================================
Traceback (most recent call last):
File “”, line 1, in
File “C:\SALOME-9.7.0\W64\KERNEL\lib\python3.6\site-packages\salome\salome_init_.py”, line 180, in salome_init
salome_init_with_session(path, embedded)
File “C:\SALOME-9.7.0\W64\KERNEL\lib\python3.6\site-packages\salome\salome_init_.py”, line 234, in salome_init_with_session
orb, lcc, naming_service, cm, esm, dsm, modulcat = salome_kernel_init()
File “C:\SALOME-9.7.0\W64\KERNEL\lib\python3.6\site-packages\salome\salome_kernel.py”, line 69, in salome_kernel_init
lcc = LifeCycleCORBA(orb)
File “C:\SALOME-9.7.0\W64\KERNEL\bin\salome\LifeCycleCORBA.py”, line 36, in init
SALOME_LifeCycleCORBA.init(self)
File “C:\SALOME-9.7.0\W64\KERNEL\lib\python3.6\site-packages\salome\libSALOME_LifeCycleCORBA.py”, line 175, in init
this = _libSALOME_LifeCycleCORBA.new_SALOME_LifeCycleCORBA(ns)
RuntimeError: Naming Service Unreacheable

Although this error does not happen when I call this function again, I think this error was not resolved because I got another error when I create new geomBuilder obejct.

salome.salome_init()
import GEOM
from salome.geom import geomBuilder
import math
import SALOMEDS
geompy = geomBuilder.New()
Traceback (most recent call last):
File “”, line 1, in
File “C:\SALOME-9.7.0\W64\GEOM\lib\python3.6\site-packages\salome\salome\geom\geomBuilder.py”, line 14079, in New
geom = geomBuilder()
File “C:\SALOME-9.7.0\W64\GEOM\lib\python3.6\site-packages\salome\salome\geom\geomBuilder.py”, line 708, in new
geom = lcc.FindOrLoadComponent( “FactoryServer”, “GEOM” )
AttributeError: ‘NoneType’ object has no attribute ‘FindOrLoadComponent’

I appreciate if if you can give me any comments on the troubles I am having!

Best,

SuJong

Hello SuJong,

sorry for the late reply, I have tried to run the script the same way as you would run SALOME in TUI mode salome -t name_of_python_script.py (so that you could use the SMESH and GEOM modules). While this works in Linux, it I have yet to figure out how to run it on windows.

Maybe Nabil knows more, or maybe the use of Visual Code IDE is better in this way as it is better supported.

Sorry, my use of SALOME is primarily MEDCoupling as I do the geometry and mesh handling beforehand.

Best regards,
Gregor

First of all, thank you very much @gregor.simic and @NabilG for the support!

Last week I resumed my research and I finally managed to run the scripts by installing Visual Studio inside SALOME (also, it’s much better than Spyder IDE, thanks again!).

------------------------------------------ New Question ------------------------------------------

I will take advantage of your knowledge and ask one more question:

Since my last post I was able to upgrade my script, and the main problem now is 'time to solve the FEA". Because for an optimization problem I have created a function (f) that returns the maximum value of Maximum Principal Stress of all nodes given an input mesh, so I have this function:

f(mesh) = max([sigma_3_node1, sigma_3_node2, sigma_3_node3, …, sigma_3_nodeN)

And then a choose N node positions as variables (it’s 2d, so each node has (x_N,y_N) values).

Now, I need to calculate the gradient of this f function, but every f evaluation requires 1 FEA analysis (4s within the current mesh). And in order to calculate each term of the gradient (using finite difference numerical approximation i.e. d(f)/d(x) = f((x+h) + f(x))/h), I need at least 2 f evaluations (i.e. 2 FEA analysis or 8 seconds running in serial).

Can anyone help me to reduce this 4s analysis is just a mesh with 3k nodes? Or know another way to calculate this gradient efficiently?

------------------------------------------ EXAMPLE ------------------------------------------

So, in order to optimize 10 nodes, each node has (x,y) coordinates. So now my f function has 20 variables = (x_1,y_1,x_2,y_2,…,x_10,y_10). In this specific case I’ll need 21 FEA analysis - because the term f(x) is common in all gradient therms just o calculate ONE gradient.

Each FEA analysis take 4s and I managed to run 4x FEA in parallel (using my QuadCore CPU). So, theoretically a gradient of 20 variables needs 21 FEA and will take 20 seconds:
→ Start → 4 FEA (4 seconds)-> 4 FEA (4 seconds)-> 4 FEA (4 seconds)-> 4 FEA (4 seconds)-> 1 FEA (4 seconds) → End. But in real life, takes ~51 seconds. Much faster than the old script version, that used FEA in serial and for this same gradient it was need more than 100s per gradient. But still a lot of time.

Hi Joao,

Not sure if it’s too late now. Nevertheless, I feel like adding something here that you might find useful…

I don’t really know what your single FEA is like: linear or non-linear, static or some dynamics involved, etc. I mean, you can of course try different solvers, compromising the mesh density, etc. However regardless, there’s a certain limit, and a single FEA will simply take that time, and there’s nothing to do with that, if you’ve already tried what I just mentioned. And if you’re using a gradient-descent-like algorithm, than for a single step in your design space (i.e., to produce one new design of your structure) you’ll have to calculate a gradient of the objective function ‘f’ in the current design point, that is to calculate ‘f’ N+1 times, N being the number of design variables. Also, bear in mind that you usually may have to account for the restrictions on your state variable (strains, stresses, displacement, velocities, natural frequencies, etc.). This usually needs to be addressed, but can be done through various means.

Usually, commercial codes try to keep and reuse the information about the points in design space. That information is coming anyway, because you’re calculating the gradient. Then, they use that info to approximate the objective function and construct so-called a meta-model. Then, say, you perform 3 legit FE simulations, and results of those simulations are at the same time also used for the construction and update of the meta-model, and then 4-th is just obtained from that meta-model alone, which is lighting fast. This way you reduce amount of simulations needed, but at the same time, if your meta-model produces quite different outcomes compared to the real FEA at a particular point in the design space, then you’re at risk of getting wrong gradient and wrong further direction from that design.

Also, note that with your number of design parameters (20), this becomes a tough issue with gradient-based methods, in my opinion. Maybe, you may want to check out the derivative-free optimization methods, that don’t use any gradients at all (Derivative-free optimization - Wikipedia). Artificial bee colony (ABC) and particle swamp optimization (PSO) algorithms are very popular, for example, and not so difficult to implement in the code.

Hopefully, this may be useful. Feel free to ask for details, if needed.

Cheers,
Roman