Skip to content

Commit 1182240

Browse files
germa89akaszynski
andauthored
Fix/Add IP parameter in launcher (#585)
* Removed the default title for plotting in `_plot_point_scalars` to solve issue #576. * Revert "Removed the default title for plotting in `_plot_point_scalars` to solve issue #576." One branch will be created. This reverts commit eefed0e. * Changed documentation to reflect the way you can change the IP of the grpc server. * Added changes to launcher_grpc to create a `mylocal.ip` file which tells Ansys which IP to use to launch the grpc server. I did include an if to avoid execute if the ip is different than default LOCALHOST (127.0.0.1). * Making changes to `launch_mapdl` to pass the argument `ip` to the function `launch_grpc`. * Added procedures to write the `mylocal.ip` file with the desired grpc ip. It seems the process needs quite a bit time before reding the file in (it is not the first thing it does) hence I'm not deleting the file. * Added `test_grpc_custom_ip` to the test grpc unit. Resolve issue #582 * Fixing style. * Answering review. Added Windows description for the commands to launch ansys. * Answering Review. Fixed format in `create_ip_file` and added context manager. * Answering Review. Fixed format in `create_ip_file` and added context manager. Fixed some line spacing and file names. * add skip when mapdl is not locally installed * Update tests/test_grpc.py * Fixing format issues. * Replaced the way IP is found in the output file. Using regex now. * Pushed back in the workflow the `mylocal.ip` file deletion. Now it is deleted when we exit mapdl (`mapdl.exit()`). I removed the traces of the old delete function. * Deleting `_remove_mylocalip_file` Access that file to delete it might be complicated in some environments and it might be prone to errors. Co-authored-by: Alex Kaszynski <[email protected]> * Deleting mention to `_remove_mylocalip_file`. Co-authored-by: Alex Kaszynski <[email protected]> * Added `check_valid_ansys` to skipif. * Fixed `test_grpc_custom_ip` test. * Update tests/test_grpc.py Changes in asserting ip Co-authored-by: Alex Kaszynski <[email protected]> * Fixed name in method. * Re ordering imports. * Format fixing * Format fixing using black. * Format fixing Co-authored-by: Alex Kaszynski <[email protected]>
1 parent f56df53 commit 1182240

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

ansys/mapdl/core/launcher.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,22 @@ def port_in_use(port, host=LOCALHOST):
148148
return True
149149

150150

151+
def create_ip_file(ip, path):
152+
"""Create 'mylocal.ip' file required for ansys to change the IP of the gRPC server."""
153+
154+
file_name = os.path.join(path, "mylocal.ip")
155+
with open(file_name, "w") as f:
156+
f.write(ip)
157+
158+
151159
def launch_grpc(
152160
exec_file="",
153161
jobname="file",
154162
nproc=2,
155163
ram=None,
156164
run_location=None,
157165
port=MAPDL_DEFAULT_PORT,
166+
ip=LOCALHOST,
158167
additional_switches="",
159168
override=True,
160169
timeout=20,
@@ -340,7 +349,12 @@ def launch_grpc(
340349
port += 1
341350
pymapdl._LOCAL_PORTS.append(port)
342351

352+
# setting ip for the grpc server
353+
if ip != LOCALHOST: # Default local ip is 127.0.0.1
354+
create_ip_file(ip, run_location)
355+
343356
cpu_sw = "-np %d" % nproc
357+
344358
if ram:
345359
ram_sw = "-m %d" % int(1024 * ram)
346360
else:
@@ -1005,10 +1019,10 @@ def launch_mapdl(
10051019
)
10061020
elif mode == "grpc":
10071021
port, actual_run_location = launch_grpc(
1008-
port=port, verbose=verbose_mapdl, **start_parm
1022+
port=port, verbose=verbose_mapdl, ip=ip, **start_parm
10091023
)
10101024
mapdl = MapdlGrpc(
1011-
ip=LOCALHOST,
1025+
ip=ip,
10121026
port=port,
10131027
cleanup_on_exit=cleanup_on_exit,
10141028
loglevel=loglevel,

doc/source/getting_started/running_mapdl.rst

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ connect to it. First, launch MAPDL with:
4545
4646
C:/Program Files/ANSYS Inc/v211/ansys/bin/winx64/ANSYS211.exe -grpc
4747
48-
Or on Linux with (assuming a ``/usr/ansys_inc`` install:
48+
Or on Linux with (assuming a ``/usr/ansys_inc`` install):
4949

5050
/usr/ansys_inc/v211/ansys/bin/ansys211 -grpc
5151

@@ -70,6 +70,26 @@ port 50005 with:
7070
7171
/usr/ansys_inc/v211/ansys/bin/ansys211 -port 50005 -grpc
7272
73+
You can configure the IP too. However because of ANSYS limitation to receive
74+
strings from command line, the IP needs to be read from an external file
75+
called ``mylocal.ip``. This file is read automatically.
76+
77+
You can then setup the IP in Windows (Powershell and CMD) with:
78+
79+
.. code::
80+
81+
echo "127.0.0.1" > mylocal.ip
82+
C:/Program Files/ANSYS Inc/v211/ansys/bin/winx64/ANSYS211.exe -grpc
83+
84+
85+
or in Linux with:
86+
87+
.. code::
88+
89+
echo "127.0.0.1" > mylocal.ip
90+
/usr/ansys_inc/v211/ansys/bin/ansys211 -grpc
91+
92+
7393
This server can be connected to either from the same host, or from an
7494
external host. For example, you can connect to a MAPDL service
7595
running locally with:

tests/test_grpc.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
import pytest
55

66
from ansys.mapdl.core import examples
7+
from ansys.mapdl.core.launcher import get_start_instance, check_valid_ansys
8+
from ansys.mapdl.core import launch_mapdl
79

810
PATH = os.path.dirname(os.path.abspath(__file__))
911

1012
# skip entire module unless HAS_GRPC installed or connecting to server
1113
pytestmark = pytest.mark.skip_grpc
1214

15+
skip_launch_mapdl = pytest.mark.skipif(not get_start_instance() and check_valid_ansys(),
16+
reason="Must be able to launch MAPDL locally")
17+
1318

1419
@pytest.fixture(scope="function")
1520
def setup_for_cmatrix(mapdl, cleared):
@@ -149,6 +154,13 @@ def test_download_missing_file(mapdl, tmpdir):
149154
mapdl.download("__notafile__", target)
150155

151156

157+
@skip_launch_mapdl # need to be able to start/stop an instance of MAPDL
158+
def test_grpc_custom_ip():
159+
ip = '127.0.0.2'
160+
mapdl = launch_mapdl(ip=ip)
161+
assert mapdl._ip == ip
162+
163+
152164
def test_cmatrix(mapdl, setup_for_cmatrix):
153165
cap_name = "aaaaa"
154166
output = mapdl.cmatrix(1, "cond", 3, 0, cap_name)

0 commit comments

Comments
 (0)