Connect with Ansys

Ansys provides its API to perform operations remotely. We will show how to setup connection.

What do we want to achieve?

Ansys provides its API to perform operations remotely. This action is made possible by running a gRPC server. We will describe how to set up such a connection and run a straightforward connection using a Python library created for this purpose.

With this solution, it is possible to repeatedly execute the same program, automate repetitive tasks and execute simulations without turning on the user interface.

Another essential aspect is segregation. We can execute our program on another (potentially weaker) machine. We are also no longer limited by location - the only concern is bandwidth, which is not a critical problem these days.

Pre-instalations

Before we start, let us check what we need:

APDL Software

Obviously, we need Ansys software with APDL (Ansys Parametric Design Language). It needs to be installed with licensing.

More on what is APDL?
Programing language library
image/svg+xml

We will be using Python programming language. Nowadays, many operating systems come preinstalled with Python or offers ready-to-go installers.

When using Windows (your local machine - it might be different than machine running ANSYS) and not yet have Python installed, consider using Conda (wrapper for running Python).

Please install the package manager pip

Command line / shell

Using the command line is an essential skill for running scripts or programs.

If you do not know yet how to open your system console, read some help pages/articles:

Windows Terminal
Linux Command Line
MacOS Terminal

Start gRPC server

We need to start a process which will serve APDL execution on the machine with ANSYS.

To do so, locate where ANSYS mapdl program is located and run the following command in console:

[path to ANSYS executable] -grpc -port 50052

In the example, for Windows, the installation location can be: C:/Program Files/ANSYS Inc/vXXX (XXX - your version), so the command would look like:

C:/Program Files/ANSYS Inc/vXXX/ansys/bin/winx64/ANSYSXXX.exe -grpc -port 50052

After this action, you should see the following text output in your command line:

Start GRPC Server
##############################
### START GRPC SERVER      ###
##############################

Server Executable   : MapdlGrpc Server
Server listening on : 0.0.0.0:50052

This means that you have successfully started the gRPC server.

Remarks

❗ This gRPC server will take a slot of your license when executing.

❗ Might occur error where port (50052) is already taken. In that situation, you need to close a process which uses that port. It might be a different instance of the gRPC server. If it is another program which uses that port, you can assign a different one (change number 50052 to another one). Note: 50052 is the default port.

❗ Your gRPC server is running on the same machine but it might not be accessible from the other one, there might be a couple of reasons for that, most common is that the firewall blocks traffic on a particular port. We will be covering such issues in different articles.

❗ In further part, when your apdl session will be closed, gRPC server might be closed as well. We will cover that issue in a different article.

Connecting using Python

Before writing the actual program, let us ensure that we have the necessary libraries installed on our machine. Open a console and type:

pip install ansys-mapdl-core

This action will install a package which allows you to connect and remotely run apdl commands using Python language/commands.

Now, open the text editor and create the file run_ansys.py (if you wish, you can name it differently; just remember to end with .py extension and use the same name in the following console commands) and type/paste the following content:

from ansys.mapdl.core import Mapdl mapdl = Mapdl("grpc_machine_address", port=50052) print(mqpdl)

where grpc_machine_address is the network address of the machine running gRPC server. If you are running it locally (same machine), use 127.0.0.1; if in local network, use local ip address. More advanced networking we will cover in different articles.

When the file is in place, open the console, get to the file location (move your current location in the terminal to the file one) and run the command:

python run_ansys.py

what should result in the following output:

Product:             ANSYS Mechanical Enterprise
MAPDL Version:       RELEASE  20XX RX           BUILD XX.X
PyMAPDL Version:     Version: X.XX.X

If you get this, you know that you have successfully connected the gRPC server and Python program. That opens many possibilities to automate your work or enable execution in a remote environment.

Now, it is time to learn how to execute apdl commands via Python -- coming soon on totalsym.com. If you have some issues, contact us via emial: [email protected]

Remarks

❗ Possible errors might be connected to networking issues. We are covering them in different article

❗ There are a couple more settings in initiating comnection which might be helpful. All of them are covered in API documentation on ansys help articles: API Reference