Skip to main content

PyWorkbench Unleashed: Transforming Your Ansys Workbench End-to-End Simulation Experience

vikas.namdeo@ansys.com | 12.09.2024

Introduction

With this blog, we're excited to introduce you to PyWorkbench, a powerful new client library that enhances the Ansys Workbench platform for engineering simulations. By providing seamless automation and integration, PyWorkbench streamlines adoption and delivers customer-centric solutions. Experience an environment that harnesses the power of diverse PyAnsys modules for Ansys applications integrated with Workbench.

With PyWorkbench client library, you can fully leverage the capabilities of the Ansys Workbench, making it simpler to adopt and use for your specific needs. It connects seamlessly with different PyAnsys modules, allowing you to work with a range of Ansys applications.

In this blog post, we will guide you through key concepts and practical strategies for using PyWorkbench. You will learn how to interact with them programmatically, making your engineering simulations more efficient and effective. Let's dive in!

Leverage PyWorkbench to achieve objectives such as:

  • Improved automation, seamless integration with PyAnsys modules, and enhanced user experience for engineering simulations.
  • Streamline your workflows for faster and easier integration.
  • Use Python’s flexibility to manage and customize your simulation processes.

Set up your environment

Before you can use PyAnsys libraries, you must set up your environment. First, ensure that you have legally licensed copies of these Ansys products installed:

Once these Ansys products are installed, set up your environment:

  1. Install Python (if not already installed).

    Ensure that you have Python installed on your system. You can download Python from the official website. The recommended version "as of writing" is Python 3.12.

  2. Create a virtual environment (if not already created).

       python -m venv .venv      
    
  3. Activate the virtual environment.

  • For Windows CMD:

      .venv\Scripts\activate.bat
    
  • For Windows Powershell:

      .venv\Scripts\Activate.ps1
    
  • For Linux:

      source .venv/bin/activate
    

    From this point forward, you must execute all commands within the virtual environment.

  1. Install the libraries.

    Now that your virtual environment is active, you can use pip to install the required PyAnsys libraries. Run the following commands to install the ansys-workbench-core, and ansys-mechanical-core packages:

    python -m pip install ansys-workbench-core
    python -m pip install ansys-mechanical-core
    

Note: For beginners who find the preceding steps challenging, consider using the Ansys Python Manager to install Python and PyAnsys metapackages. This tool also simplifies the process of creating and managing virtual environments.

Create Workflow using PyWorkbench

To establish the workflow, it's necessary to initiate the PyWorkbench server, which can be done using the launch_workbench() method. This procedure initiates a Workbench server on the selected machine and constructs a PyWorkbench client that connects to that server. This system is compatible with Python's interactive mode, enabling real-time script development before executing your code as a program or in batch mode.

from ansys.workbench.core import launch_workbench
wb = launch_workbench(show_gui=True)

When the Workbench is activated, it provides the flexibility to dispatch Journaling commands using either the run_script_string() or run_script_file() methods. These journaling commands can be utilized to construct the connected system or extract data from the archived system. For instance, the subsequent journal script demonstrates how you can leverage these methods to access the archived system within the Material Designer workflow.

wbjn_template = """
import os
import json
import string
import os.path
work_dir = GetServerWorkingDirectory()
arg_ProjectArchive = os.path.join(work_dir, "MatDesigner.wbpz")
# Description="Upzip the archived example project file"
Unarchive(
    ArchivePath=arg_ProjectArchive,
    ProjectPath=GetAbsoluteUserPathName(work_dir + "wbpj\\MatDesigner.wbpj"),
    Overwrite=True)
    """

wb.run_script_string(wbjn_template)

Material Designer Workflow

You have the ability to configure the Workbench command template, enabling you to alter the material properties. Specifically, this will allow you to adjust the Young's modulus of the material, thereby enhancing its functionality.

wbjn_template2 = """designPoint1 = Parameters.GetDesignPoint(Name="0")
parameter1 = Parameters.GetParameter(Name="P1")
designPoint1.SetParameterExpression(
    Parameter=parameter1,
    Expression="{} [Pa]")
backgroundSession1 = UpdateAllDesignPoints(DesignPoints=[designPoint1])
"""

Utilize the following script to instill a fresh value for the Young's Modulus, thereby revolutionizing the project update:

my_command = wbjn_template2.format(1.6e10)
wb.run_script_string(my_command)

Update Design Point with Young's Modulus

In addition, it is possible to retrieve the output values. To do this, the first step is to set up the Workbench script in a way that allows us to inquire about the values of the output parameters.

extract_output = """import json
p = Parameters.GetParameter(Name="P{}")
my_tag = p.DisplayText
wb_script_result = json.dumps(my_tag + ',' + str(p.Value))
"""

Utilize the potent 'extract_output' Workbench script to seamlessly generate updated values in your preferred format. The resultant output will take the form of an organized dictionary, meticulously listing out the material properties as depicted below:

outputs = {}
for p in range(2, 12):
    return_val = wb.run_script_string(extract_output.format(p)).split(',')
    name = return_val[0]
    parameter_val = float(return_val[1])
    outputs[name] = parameter_val
print(outputs)

Output:

{'E1': 3610923.730858071, 'E2': 3610920.439805918, 'E3': 799999810.4495835, 'G12': 898955.9821898732, 'G23': 157509432.0642782, 'G31': 157509432.0642782, 'nu12': 0.9912443197680499, 'nu13': 0.001354096679884023, 'nu23': 0.0013540954457392316, 'rho': 392.499922259344}

You can obtain the Workbench project file by leveraging the download_project_archive() method, which facilitates the archival and download of the project. Finally, call the exit() method on the Workbench client to gracefully shut down the service.

download_project_archive(archive_name)
wb.exit()

Explore PyWorkbench

This serves merely as a rudimentary illustration of what you can accomplish with PyWorkbench. We encourage you to delve deeper into the extensive PyWorkbench documentation for a more comprehensive understanding.

In summary, the PyWorkbench client library allows Method Developers, Engineers, and Researchers to fully utilize Ansys Workbench by integrating it with Python. This library enables you to enhance automation tasks and integrate workflows using various Python features.

Additional Resources

Ansys Workbench User's Guide

PyAnsys documentation