Here are some tips and examples for how to run these applications with PyAnsys and utilize AI to run the applications from any platform.
AI needs to do 2 things to control the Ansys applications:
1. Create/Modify python code
2. Execute python code
With those 2 abilities alone, you can run Ansys tools, and AI is really, really good at those 2 things.
Additionally, AI is good at assessing if what it did was successful and following up on any clean up tasks, summarizing what happened, exploring what if situations, or taking it further in any direction you prefer/allow.
There are 2 main ways/modes to have AI work and the application and the PyAnsys module/mode determine which one to use.
- PyAnsys is a pass through to run the application API commands
- PyAnsys python code directly runs that process
Pass Through:
For the pass though mode you (or an AI agent helper) will generate application API code. This code is then "sent" as is to the application and you get the text output as a result.
- Mechanical -> Iron python mechanical APIs
- Workbench -> Iron python workbench APIs
- MAPDL -> APDL design language commands
- DPF
- Discovery
1. > Mechanical
Example below on how to start gRPC from Ansys Mechanical scripting window (or apply this to a button or ACT for easier access)
AppAPI=Ansys.ACT.Mechanical.MechanicalAPI.Instance.ApplicationAPI
AppAPI.GrpcServer.Start(port, bWnuaSupportNeeded=True)
Once port is open, note the number and use it in the snippet below:
#Example of how to connect to an open mechanical GUI instance and send API commands from PyMechanical Remote Mode
from ansys.mechanical.core import launch_mechanical
#gRPC port number/id. Update this for each new connection to route to right mechanical session.
port_number = 0
# Connect to the running instance
mechanical = launch_mechanical(start_instance=False, port=port_number, cleanup_on_exit=False)
#output in text form of the script execution result
raw_output = mechanical.run_python_script("Your Code Here or read from a file")
It is useful to know this is for Remote Model of PyMechanical, and there is Embedded Mode. Embedded does not allow the UI to be open, but has pythonic access to the mechanical objects more directly. Depending on the use case you can choose, or dynamically switch between the two when needed. AI it also really good at dynamically presenting information in custom built UI, charts, graphs, tools. Embedded mode can be used with other tools+AI on top to still provide UI access to the data, but not the Mechanical GUI open live.
2. > MAPDL
MAPDL is similar, but you cannot start gRPC with a running session, you need to launch it with gRPC, so "connect" is "launch" here. FYI You can connect to it again, but just cannot connect to a GUI session that is open like you can for Mechanical.
There is not commands to run in MAPDL as you are starting it fresh with gRPC. All of this happens with PyAnsys (PyMAPDL) scripts.
import ansys.mapdl.core as pymapdl
mapdl = pymapdl.launch_mapdl(version=MAPDL_VERSION, run_location=str(run_folder),override=True)
output = mapdl.run("Your Code Here or read from a file")
3. > Workbench
Workbench operates similar to mechanical, but the APIs are slightly different. Same ideas, different APIs per application.
Scripting snippet to run within Workbench scripting console or batch
def start_gRPC():
port = GetServerPort()
return port if port else StartServer()
port = start_gRPC()
print(port)
This is then run from PyAnsys (PyWorkbench specifically) to connect to it. Obviously dynamically update the port id to match the port opened from the application.
from ansys.workbench.core import launch_workbench, connect_workbench
#launch with your own local paths and version preference
wb = connect_workbench(port=port_number, security="your preference here")
raw_output = wb.run_script_string("Your Code Here or read from a file")
4. > Discovery
See this post for how to connect to Discovery
Direct Code:
For direct code interaction see almost any PyDPF example. This is because DPF does not have an application to control, it is the application, doing the processing directly.
Another example is PyMechanical Embedded mode. In this mode the GUI is not open, and you are dealing with python objects directly for mechanical.
Example of DPF code being run in PyAnsys code directly on data. No other application and no gRPC connection step.
from ansys.dpf import core as dpf
# 1. Define the data source directly using your result file (.rst)
path = r"C:\Path\to\your\file.rst"
data_sources = dpf.DataSources(path)
# 2. Instantiate the stress operator
# Use dpf.operators.result.stress() for the full stress tensor
stress_op = dpf.operators.result.stress()
stress_op.inputs.data_sources.connect(data_sources)
# Optional: Instantiate an equivalent (von Mises) operator to convert the tensor
# or use dpf.operators.result.stress_von_mises()
von_mises_op = dpf.operators.result.stress_von_mises()
von_mises_op.inputs.data_sources.connect(data_sources)
# 3. Evaluate the workflow and extract the Fields Container
stress_fc = von_mises_op.eval()
AI and How Works:
You can setup AI to operate in many different ways depending on your preferences. Set how you want AI to work in an agent.md file and/or skills. But one example that works is to setup the AI so it generates the application API code. For example MAPDL or iron python Mechanical API scripts in a file. You then have a standard PyAnsys file that is your "pass through" module. This is the module you actually run or have the AI run it for you in a python environment that has PyAnsys installed. So for example:
Prompt: "Read this .csv file and apply the pressures to the listed named selections"
AI Action (Mechanical)
- Read the .csv and understand formatting.
- Create an iron python API script to parse the .csv file, and API code to create pressure objects for each line assigning the values and scoping to named selections.
- Run the standard PyMechanical module with minor update to the right gRPC port number and dynamically created iron python script file
AI Action (MAPDL domain):
- Read the .csv and understand formatting.
- It realizes perhaps parsing the .csv file with MAPDL isn't the best option and decides to parse the information with python directly
- From the parsed data it generates a text file of MAPDL commands including the SFE or SF commands applying them to APDL components. It will know "components" are named selections in APDL, and also will likely check if the the component is a nodal or elemental component and apply the right SF or SFE command accordingly.
- Run the standard PyMAPDL module with minor update to the right gRPC port number and dynamically created MAPDL script file.
This is just one way to set it up. Another very obvious thing to do is use the available MCP servers for these applications. How and what you do depends on the level you want to actually see the code before actions are performed, what autonomy you want to give an AI agent, and what type of workflow you are doing across applications. It is up to you, but for any setup the PyAnsys ecosystem provides a communication path for AI to control the Ansys tools, running them autonomously and making choices along the way that are dynamic and flexible.