Axisymmetric rotor#

This notebook demonstrates the process of running a Workbench service on a local machine to solve both 2D general axisymmetric rotor and 3D rotor models using PyMechanical. It includes steps for uploading project files, executing scripts, downloading results, and displaying output images.

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

ModuleNotFoundError: No module named 'ansys'

Launch the Workbench service on the local machine using specific options. Define the working directory and subdirectories for assets, scripts, and geometry databases (agdb). The launch_workbench function starts a Workbench session with the specified directory.

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

NameError: name 'launch_workbench' is not defined

Upload the project files to the server using the upload_file_from_example_repo method. The files uploaded are axisymmetric_model.agdb, rotor_3d_model.agdb.

[6]:
wb.upload_file_from_example_repo("axisymmetric-rotor/agdb/axisymmetric_model.agdb")
wb.upload_file_from_example_repo("axisymmetric-rotor/agdb/rotor_3d_model.agdb")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[6], line 1
----> 1 wb.upload_file_from_example_repo("axisymmetric-rotor/agdb/axisymmetric_model.agdb")
      2 wb.upload_file_from_example_repo("axisymmetric-rotor/agdb/rotor_3d_model.agdb")

NameError: name 'wb' is not defined

Execute a Workbench script (project.wbjn) to define the project and load the geometry. The log file is set to wb_log_file.log and the name of the system created is stored in sys_name and printed.

[7]:
export_path = 'wb_log_file.log'
wb.set_log_file(export_path)
sys_name = wb.run_script_file(str((assets / "project.wbjn").absolute()), log_level='info')
print(sys_name)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[7], line 2
      1 export_path = 'wb_log_file.log'
----> 2 wb.set_log_file(export_path)
      3 sys_name = wb.run_script_file(str((assets / "project.wbjn").absolute()), log_level='info')
      4 print(sys_name)

NameError: name 'wb' is not defined

Start a PyMechanical server for the system and create a PyMechanical client session to solve the 2D general axisymmetric rotor model. The project directory is printed to verify the connection.

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

NameError: name 'wb' is not defined
[9]:
print(mechanical.project_directory)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[9], line 1
----> 1 print(mechanical.project_directory)

NameError: name 'mechanical' is not defined

Read and execute the script axisymmetric_rotor.py via the PyMechanical client to mesh and solve the 2D general axisymmetric rotor model. The output of the script is printed.

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

NameError: name 'mechanical' is not defined

Specify the Mechanical directory for the Modal Campbell Analysis and fetch the working directory path. Download the solver output file (solve.out) from the server to the client’s current working directory and print its contents.

[11]:
mechanical.run_python_script(f"solve_dir=ExtAPI.DataModel.AnalysisList[2].WorkingDir")
result_solve_dir_server = mechanical.run_python_script(f"solve_dir")
print(f"All solver files are stored on the server at: {result_solve_dir_server}")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[11], line 1
----> 1 mechanical.run_python_script(f"solve_dir=ExtAPI.DataModel.AnalysisList[2].WorkingDir")
      2 result_solve_dir_server = mechanical.run_python_script(f"solve_dir")
      3 print(f"All solver files are stored on the server at: {result_solve_dir_server}")

NameError: name 'mechanical' is not defined
[12]:
solve_out_path = os.path.join(result_solve_dir_server, "solve.out")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[12], line 1
----> 1 solve_out_path = os.path.join(result_solve_dir_server, "solve.out")

NameError: name 'result_solve_dir_server' is not defined
[13]:
def write_file_contents_to_console(path):
    """Write file contents to console."""
    with open(path, "rt") as file:
        for line in file:
            print(line, end="")
[14]:
current_working_directory = os.getcwd()
mechanical.download(solve_out_path, target_dir=current_working_directory)
solve_out_local_path = os.path.join(current_working_directory, "solve.out")
write_file_contents_to_console(solve_out_local_path)
os.remove(solve_out_local_path)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[14], line 2
      1 current_working_directory = os.getcwd()
----> 2 mechanical.download(solve_out_path, target_dir=current_working_directory)
      3 solve_out_local_path = os.path.join(current_working_directory, "solve.out")
      4 write_file_contents_to_console(solve_out_local_path)

NameError: name 'mechanical' is not defined

Specify the Mechanical directory path for the Modal Campbell Analysis and fetch the image directory path. Download an image file (tot_deform_2D.png) from the server to the client’s current working directory and display it using matplotlib.

[15]:
from matplotlib import image as mpimg
from matplotlib import pyplot as plt
[16]:
mechanical.run_python_script(f"image_dir=ExtAPI.DataModel.AnalysisList[2].WorkingDir")
result_image_dir_server = mechanical.run_python_script(f"image_dir")
print(f"Images are stored on the server at: {result_image_dir_server}")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[16], line 1
----> 1 mechanical.run_python_script(f"image_dir=ExtAPI.DataModel.AnalysisList[2].WorkingDir")
      2 result_image_dir_server = mechanical.run_python_script(f"image_dir")
      3 print(f"Images are stored on the server at: {result_image_dir_server}")

NameError: name 'mechanical' is not defined
[17]:
def get_image_path(image_name):
    return os.path.join(result_image_dir_server, image_name)
[18]:
def display_image(path):
    print(f"Printing {path} using matplotlib")
    image1 = mpimg.imread(path)
    plt.figure(figsize=(15, 15))
    plt.axis("off")
    plt.imshow(image1)
    plt.show()
[19]:
image_name = "tot_deform_2D.png"
image_path_server = get_image_path(image_name)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[19], line 2
      1 image_name = "tot_deform_2D.png"
----> 2 image_path_server = get_image_path(image_name)

Cell In[17], line 2, in get_image_path(image_name)
      1 def get_image_path(image_name):
----> 2     return os.path.join(result_image_dir_server, image_name)

NameError: name 'result_image_dir_server' is not defined
[20]:
if image_path_server != "":
    current_working_directory = os.getcwd()

    local_file_path_list = mechanical.download(
        image_path_server, target_dir=current_working_directory
    )
    image_local_path = local_file_path_list[0]
    print(f"Local image path : {image_local_path}")

    display_image(image_local_path)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[20], line 1
----> 1 if image_path_server != "":
      2     current_working_directory = os.getcwd()
      4     local_file_path_list = mechanical.download(
      5         image_path_server, target_dir=current_working_directory
      6     )

NameError: name 'image_path_server' is not defined

Specify the Mechanical directory for the Unbalance Response Analysis and fetch the working directory path. Download the solver output file (solve.out) from the server to the client’s current working directory and print its contents.

[21]:
mechanical.run_python_script(f"solve_dir=ExtAPI.DataModel.AnalysisList[3].WorkingDir")
result_solve_dir_server = mechanical.run_python_script(f"solve_dir")
print(f"All solver files are stored on the server at: {result_solve_dir_server}")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[21], line 1
----> 1 mechanical.run_python_script(f"solve_dir=ExtAPI.DataModel.AnalysisList[3].WorkingDir")
      2 result_solve_dir_server = mechanical.run_python_script(f"solve_dir")
      3 print(f"All solver files are stored on the server at: {result_solve_dir_server}")

NameError: name 'mechanical' is not defined
[22]:
solve_out_path = os.path.join(result_solve_dir_server, "solve.out")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[22], line 1
----> 1 solve_out_path = os.path.join(result_solve_dir_server, "solve.out")

NameError: name 'result_solve_dir_server' is not defined
[23]:
def write_file_contents_to_console(path):
    """Write file contents to console."""
    with open(path, "rt") as file:
        for line in file:
            print(line, end="")
[24]:
current_working_directory = os.getcwd()
mechanical.download(solve_out_path, target_dir=current_working_directory)
solve_out_local_path = os.path.join(current_working_directory, "solve.out")
write_file_contents_to_console(solve_out_local_path)
os.remove(solve_out_local_path)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[24], line 2
      1 current_working_directory = os.getcwd()
----> 2 mechanical.download(solve_out_path, target_dir=current_working_directory)
      3 solve_out_local_path = os.path.join(current_working_directory, "solve.out")
      4 write_file_contents_to_console(solve_out_local_path)

NameError: name 'mechanical' is not defined

Start a PyMechanical server for the 3D rotor model system and create a PyMechanical client session. The project directory is printed to verify the connection.

[25]:
server_port = wb.start_mechanical_server(system_name=sys_name[0])
mechanical = launch_mechanical(start_instance=False, ip='localhost', port=server_port)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[25], line 1
----> 1 server_port = wb.start_mechanical_server(system_name=sys_name[0])
      2 mechanical = launch_mechanical(start_instance=False, ip='localhost', port=server_port)

NameError: name 'wb' is not defined
[26]:
print(mechanical.project_directory)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[26], line 1
----> 1 print(mechanical.project_directory)

NameError: name 'mechanical' is not defined

Read and execute the script rotor_3d.py via the PyMechanical client to mesh and solve the 3D rotor model. The output of the script is printed.

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

NameError: name 'mechanical' is not defined

Specify the Mechanical directory for the Modal Campbell Analysis and fetch the working directory path. Download the solver output file (solve.out) from the server to the client’s current working directory and print its contents.

[28]:
mechanical.run_python_script(f"solve_dir=ExtAPI.DataModel.AnalysisList[2].WorkingDir")
result_solve_dir_server = mechanical.run_python_script(f"solve_dir")
print(f"All solver files are stored on the server at: {result_solve_dir_server}")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[28], line 1
----> 1 mechanical.run_python_script(f"solve_dir=ExtAPI.DataModel.AnalysisList[2].WorkingDir")
      2 result_solve_dir_server = mechanical.run_python_script(f"solve_dir")
      3 print(f"All solver files are stored on the server at: {result_solve_dir_server}")

NameError: name 'mechanical' is not defined
[29]:
solve_out_path = os.path.join(result_solve_dir_server, "solve.out")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[29], line 1
----> 1 solve_out_path = os.path.join(result_solve_dir_server, "solve.out")

NameError: name 'result_solve_dir_server' is not defined
[30]:
def write_file_contents_to_console(path):
    """Write file contents to console."""
    with open(path, "rt") as file:
        for line in file:
            print(line, end="")
[31]:
current_working_directory = os.getcwd()
mechanical.download(solve_out_path, target_dir=current_working_directory)
solve_out_local_path = os.path.join(current_working_directory, "solve.out")
write_file_contents_to_console(solve_out_local_path)
os.remove(solve_out_local_path)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[31], line 2
      1 current_working_directory = os.getcwd()
----> 2 mechanical.download(solve_out_path, target_dir=current_working_directory)
      3 solve_out_local_path = os.path.join(current_working_directory, "solve.out")
      4 write_file_contents_to_console(solve_out_local_path)

NameError: name 'mechanical' is not defined

Specify the Mechanical directory path for the Modal Campbell Analysis and fetch the image directory path. Download an image file (tot_deform_3D.png) from the server to the client’s current working directory and display it using matplotlib.

[32]:
from matplotlib import image as mpimg
from matplotlib import pyplot as plt
[33]:
mechanical.run_python_script(f"image_dir=ExtAPI.DataModel.AnalysisList[2].WorkingDir")
result_image_dir_server = mechanical.run_python_script(f"image_dir")
print(f"Images are stored on the server at: {result_image_dir_server}")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[33], line 1
----> 1 mechanical.run_python_script(f"image_dir=ExtAPI.DataModel.AnalysisList[2].WorkingDir")
      2 result_image_dir_server = mechanical.run_python_script(f"image_dir")
      3 print(f"Images are stored on the server at: {result_image_dir_server}")

NameError: name 'mechanical' is not defined
[34]:
def get_image_path(image_name):
    return os.path.join(result_image_dir_server, image_name)
[35]:
def display_image(path):
    print(f"Printing {path} using matplotlib")
    image1 = mpimg.imread(path)
    plt.figure(figsize=(15, 15))
    plt.axis("off")
    plt.imshow(image1)
    plt.show()
[36]:
image_name = "tot_deform_3D.png"
image_path_server = get_image_path(image_name)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[36], line 2
      1 image_name = "tot_deform_3D.png"
----> 2 image_path_server = get_image_path(image_name)

Cell In[34], line 2, in get_image_path(image_name)
      1 def get_image_path(image_name):
----> 2     return os.path.join(result_image_dir_server, image_name)

NameError: name 'result_image_dir_server' is not defined
[37]:
if image_path_server != "":
    current_working_directory = os.getcwd()

    local_file_path_list = mechanical.download(
        image_path_server, target_dir=current_working_directory
    )
    image_local_path = local_file_path_list[0]
    print(f"Local image path : {image_local_path}")

    display_image(image_local_path)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[37], line 1
----> 1 if image_path_server != "":
      2     current_working_directory = os.getcwd()
      4     local_file_path_list = mechanical.download(
      5         image_path_server, target_dir=current_working_directory
      6     )

NameError: name 'image_path_server' is not defined

Specify the Mechanical directory for the Unbalance Response Analysis and fetch the working directory path. Download the solver output file (solve.out) from the server to the client’s current working directory and print its contents.

[38]:
mechanical.run_python_script(f"solve_dir=ExtAPI.DataModel.AnalysisList[3].WorkingDir")
result_solve_dir_server = mechanical.run_python_script(f"solve_dir")
print(f"All solver files are stored on the server at: {result_solve_dir_server}")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[38], line 1
----> 1 mechanical.run_python_script(f"solve_dir=ExtAPI.DataModel.AnalysisList[3].WorkingDir")
      2 result_solve_dir_server = mechanical.run_python_script(f"solve_dir")
      3 print(f"All solver files are stored on the server at: {result_solve_dir_server}")

NameError: name 'mechanical' is not defined
[39]:
solve_out_path = os.path.join(result_solve_dir_server, "solve.out")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[39], line 1
----> 1 solve_out_path = os.path.join(result_solve_dir_server, "solve.out")

NameError: name 'result_solve_dir_server' is not defined
[40]:
def write_file_contents_to_console(path):
    """Write file contents to console."""
    with open(path, "rt") as file:
        for line in file:
            print(line, end="")
[41]:
current_working_directory = os.getcwd()
mechanical.download(solve_out_path, target_dir=current_working_directory)
solve_out_local_path = os.path.join(current_working_directory, "solve.out")
write_file_contents_to_console(solve_out_local_path)
os.remove(solve_out_local_path)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[41], line 2
      1 current_working_directory = os.getcwd()
----> 2 mechanical.download(solve_out_path, target_dir=current_working_directory)
      3 solve_out_local_path = os.path.join(current_working_directory, "solve.out")
      4 write_file_contents_to_console(solve_out_local_path)

NameError: name 'mechanical' is not defined

Download all the files from the server to the current working directory for the 3D rotor model. Verify the source path for the directory and copy all files from the server to the client.

[42]:
import shutil
import glob
[43]:
current_working_directory = os.getcwd()
target_dir2 = current_working_directory
print(f"Files to be copied from server path at: {target_dir2}")
print(f"All the solver files are stored on the server at: {result_solve_dir_server}")
Files to be copied from server path at: C:\Users\ansys\actions-runner\_work\pyworkbench-examples\pyworkbench-examples\pyworkbench-examples\doc\source\examples\axisymmetric-rotor
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[43], line 4
      2 target_dir2 = current_working_directory
      3 print(f"Files to be copied from server path at: {target_dir2}")
----> 4 print(f"All the solver files are stored on the server at: {result_solve_dir_server}")

NameError: name 'result_solve_dir_server' is not defined
[44]:
source_dir = result_solve_dir_server
destination_dir = target_dir2
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[44], line 1
----> 1 source_dir = result_solve_dir_server
      2 destination_dir = target_dir2

NameError: name 'result_solve_dir_server' is not defined
[45]:
for file in glob.glob(source_dir + '/*'):
    shutil.copy(file, destination_dir)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[45], line 1
----> 1 for file in glob.glob(source_dir + '/*'):
      2     shutil.copy(file, destination_dir)

NameError: name 'source_dir' is not defined

Finally, call the exit method on both the PyMechanical and Workbench clients to gracefully shut down the services.

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

NameError: name 'mechanical' is not defined