PyMechanical integration#

This example demonstrates how to use PyWorkbench and PyMechanical together to upload geometry, run simulations, and visualize results. It covers launching services, running scripts, and handling files between the client and server.

First, import the necessary modules. We import pathlib for handling filesystem paths, os for interacting with the operating system, and pyvista for visualization. The launch_workbench function from ansys.workbench.core is imported to start a Workbench session, and launch_mechanical from ansys.mechanical.core to start a Mechanical session.

[1]:
import os
import pathlib
import pyvista as pv
from ansys.workbench.core import launch_workbench
from ansys.mechanical.core import launch_mechanical
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 3
      1 import os
      2 import pathlib
----> 3 import pyvista as pv
      4 from ansys.workbench.core import launch_workbench
      5 from ansys.mechanical.core import launch_mechanical

ModuleNotFoundError: No module named 'pyvista'

Define several directories that will be used during the session. workdir is set to the parent directory of the current file. assets, scripts, and agdb are subdirectories within the working directory. The launch_workbench function is called to start a Workbench session with specified directories.

[2]:
workdir = pathlib.Path("__file__").parent
assets = workdir / "assets"
scripts = workdir / "scripts"
agdb = workdir / "agdb"
wb = launch_workbench(client_workdir=str(workdir.absolute()))
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[2], line 5
      3 scripts = workdir / "scripts"
      4 agdb = workdir / "agdb"
----> 5 wb = launch_workbench(client_workdir=str(workdir.absolute()))

NameError: name 'launch_workbench' is not defined

Upload a geometry file (two_pipes.agdb) from the example database to the server using the upload_file_from_example_repo method.

[3]:
wb.upload_file_from_example_repo("pymechanical-integration/agdb/two_pipes.agdb")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[3], line 1
----> 1 wb.upload_file_from_example_repo("pymechanical-integration/agdb/two_pipes.agdb")

NameError: name 'wb' is not defined

Execute a Workbench script (project.wbjn) to create a mechanical system and load the geometry using the run_script_file method. The name of the system created is stored in system_name.

[4]:
system_name = wb.run_script_file(str((assets / "project.wbjn").absolute()))
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[4], line 1
----> 1 system_name = wb.run_script_file(str((assets / "project.wbjn").absolute()))

NameError: name 'wb' is not defined

Start a PyMechanical service for the specified system using the start_mechanical_server method. Create a PyMechanical client connected to this service using launch_mechanical. The project directory is printed to verify the connection.

[5]:
pymech_port = wb.start_mechanical_server(system_name=system_name)
mechanical = launch_mechanical(start_instance=False, ip='localhost', port=pymech_port)
print(mechanical.project_directory)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[5], line 1
----> 1 pymech_port = wb.start_mechanical_server(system_name=system_name)
      2 mechanical = launch_mechanical(start_instance=False, ip='localhost', port=pymech_port)
      3 print(mechanical.project_directory)

NameError: name 'wb' is not defined

Read and execute the script solve.py via the PyMechanical client using run_python_script. This script typically contains commands to mesh and solve the model. The output of the script is printed.

[6]:
with open(scripts / "solve.py") as sf:
    mech_script = sf.read()
print(mechanical.run_python_script(mech_script))
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[6], line 3
      1 with open(scripts / "solve.py") as sf:
      2     mech_script = sf.read()
----> 3 print(mechanical.run_python_script(mech_script))

NameError: name 'mechanical' is not defined

Fetch output files (*solve.out and *deformation.png) from the solver directory to the client’s working directory using the download method.

[7]:
mechanical.download("*solve.out", target_dir=wb.client_workdir)
mechanical.download("*deformation.png", target_dir=wb.client_workdir)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[7], line 1
----> 1 mechanical.download("*solve.out", target_dir=wb.client_workdir)
      2 mechanical.download("*deformation.png", target_dir=wb.client_workdir)

NameError: name 'mechanical' is not defined

Read and print the content of the solver output file (solve.out) to the console.

[8]:
with open(os.path.join(wb.client_workdir, "solve.out"), "r") as f:
    print(f.read())
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[8], line 1
----> 1 with open(os.path.join(wb.client_workdir, "solve.out"), "r") as f:
      2     print(f.read())

NameError: name 'wb' is not defined

Plot the deformation result (deformation.png) using pyvista. A Plotter object is created, and the image is added as a background. The plot is then displayed.

[9]:
pl = pv.Plotter()
pl.add_background_image(os.path.join(wb.client_workdir, "deformation.png"))
pl.show()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[9], line 1
----> 1 pl = pv.Plotter()
      2 pl.add_background_image(os.path.join(wb.client_workdir, "deformation.png"))
      3 pl.show()

NameError: name 'pv' is not defined

Finally, the exit method is called on both the PyMechanical and Workbench clients to gracefully shut down the services, ensuring that all resources are properly released.

[10]:
mechanical.exit()
wb.exit()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[10], line 1
----> 1 mechanical.exit()
      2 wb.exit()

NameError: name 'mechanical' is not defined