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
Example below on how to start gRPC from Ansys Mechanical scripting window (or apply this to a button or ACT for easier access)
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")
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.
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")
Workbench operates similar
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")
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.
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.