How to configure connection to ANSYS gRPC on remote machine
Pre assumptions
ANSYS, with its gRPC and Python library, is a powerful tool for the automating mechanical analysis. However, the correct setup of a fully remote environment can be tricky, especially when ensuring that the ANSYS server and calling machines are close enough to each other.
This instruction will show how to cope with most issues and should be a comprehensive guide for people less experienced with setting up internet/network communications.
We assume that you are already familiar with our article on how to establish a gRPC connection. Here, we will only cover common issues you may encounter when establishing the connection.
Check out what your situation is, and depending on it, read and apply proper instructions.
Script execution and gRPC server on the same machine
You are running ANSYS gRPC server and python code on the same machine under same address (localhost). Nothing to do - it should work out of the box. Reading the article should be sufficient for you.
If you are wondering if you are doing it right, then the answer is yes. Running Python apdl being controlled by the script is automation. There is no particular need to do it on a remote machine unless you do not want to share resources (CPU, RAM, disk, etc.).
Script execution and gRPC server in the same network
It means that you are interconnected using the same device, assuming it is the router. Each device has an assigned local network number/identifier.
This configuration is beneficial for workplaces when the machine is shared among a couple of people. Another exciting case is an incompatible operating system. gRPC server requires installed ANSYS, which is bound to specific operating systems, but the execution code can be run on any other system which has the support of Python.
In 99% of cases, on the gRPC machine, traffic will be blocked for ports used by ANSYS. In the previous article, you were able to set up the connection port. It was 50052 by default.
To allow other computers/machines to connect with to your server, you need to open these ports for incoming and outgoing traffic. The configuration depends on your system, as ANSYS can be run on Windows or Unix/Ubuntu/CentOS. See these two instructions:windows, unix. Configuration: TCP / specific port 50052 (or different if the port was altered when running the grPC server)
On Ubuntu, just run the following command in the console (it will ask for the administrator password).
sudo ufw allow 50052/tcp
Local network configuration
In that case, the gRPC machine and code-executing machine are in the same network. In other words, they are connected to the same routing device, and they have similar local addresses.
Every device in the local network has an identifying address. The most common type of addresation is IPv4 (Internet Protocol version 4), which consists of 4 numbers (range 0-255) separated with commas. If we know that address, we can connect to the proper machine.
How to obtain. an IP address?
Use the command line (console) of your machine. On Windows, type:
ipconfig
You should get a list of interfaces. Finding the correct one depends on your connection type, i.e., the Wireless Adapter is for Wifi and the LAN Adapter when using a cable connection. The most common is a situation where the address starts with 192.168...
On Unix/Mac OS, use the command:
ifconfig
Another option is to log into your router (if you have access) and check the connected devices: see what IP is assigned to the gRPC server machine.
Connect with gRPC
Once you have obtained the IP address of the gRPC server (you need to know the address of the machine running ANSYS, not yours), you can update the python connection code, i.e., if IP address is 192.168.1.11, edit this line:
mapdl = Mapdl("192.168.1.11", port=50052)
Within that code, you should gain access to the gRPC server (remember to add firewall rules).
Note: The machine's IP can change on restart.
This situation can occur if the IP address of the device changes. It happens because of a commonly used mechanism called DHCP, which dynamically assigns IP addresses, so on reconnect, your router may change the address.
Solution: Routers have a function to assign static IPs. We will not show you how to do so because it differs from router to router and brand to brand, but please use the manual (search for: "static IP assign").
Internet configuration
In this case, the machine and the code are not on the same network. Only available connections can be made over the internet network.
Every device on the internet also has an IPv4 address (see previous section). The problem is that the internet has grown so fast that addresses no longer point to individual machines, but to whole subnetworks. This approach is a solution to an insufficient number of IP addresses to the number of connected devices.
To perform such a connection, your router (subnetwork) needs to have an IP called "public". This address will be recognisable on all internet pointing to your network. You need to contact your ISP (Internet Service Provider) to get a public IP. It is an extra paid feature (in many cases).
Port Forwarding
Obtaining a public IP address is one of many required steps. Your router, which has a recognizable IP on the internet, must redirect incoming traffic to your machine. This is where port forwarding is necessary to set up. 99% of all routers support port forwarding. As with static IP, configuration differs per model and brand of device. Please search in the manual for "port forwarding".
You need to set up that source port 50052 is redirected to target port 50052 on your local machine using TCP (pass the machine's local IP address; in the previous section, it is explained how to retrieve it). Note that you might need to set a static IP address for your machine in the local network (see previous section) to always point to the same device - some routers allow you to tell by MAC address - it is another type of address, can be obtained as well with commands ipconfig/ifconifg, this address should never change on your local network.
Let's analyze how it will work. Your updated code to this version:
mapdl = Mapdl("A.B.C.D", port=50052)
where A.B.C.D is a public IP. When executing the code traffic through the internet network, it will reach your router, asking to send traffic on port 50052. We have set up a rule saying that incoming traffic on port 50052 will be redirected to a local machine with the address X.Y.W.Z. Hence, your request will be delivered to the gRPC server.
This part would be the whole configuration for the internet connection, but there is a common obstacle to overcome. Most ISPs offer public IP, but in a dynamic form. In other words, the addresses are recognizable over the internet, but can often change. To overcome this, you need to configure a DDNS service (Dynamic Domain Name Server).
DDNS
DNS is a service which allows the translation of human-readable addresses to IP addresses on the internet. For example, when using the name totalsym.com, this service translates it to the server which handles displaying this page. DDNS is a service which does the same, but it can be updated quite fast anytime ip changes. If you have a dynamic public IP, instead of updating your IP in code on each shift, you could use human-readable form. Most routers support ddns (search for ddns in the manual). As always, the configuration depends on the model and brand. To set up DDNS, you will be obligated to open an account in one of the providers, such as noip or dyndns.
Once you obtained a DDNS name, such as totalsym.ddnstotalsym.com, you can use that address in code like this:
Please take note that the last version is not 100% safe. Anyone can gain access to your gRPC server. You would need to set up particular measures to better secure servers. This will be explained in different articles.
If you have issues or questions, please contact us via email: [email protected].