launch_mechanical#

ansys.mechanical.core.launch_mechanical(allow_input=True, exec_file=None, batch=True, loglevel='ERROR', log_file=False, log_mechanical=None, additional_switches=None, additional_envs=None, start_timeout=120, port=None, ip=None, start_instance=None, verbose_mechanical=False, clear_on_connect=False, cleanup_on_exit=True, version=None, keep_connection_alive=True)#

Start Mechanical locally.

Parameters:
allow_input: bool, optional

Whether to allow user input when discovering the path to the Mechanical executable file.

exec_filestr, optional

Path for the Mechanical executable file. The default is None, in which case the cached location is used. If PyPIM is configured and this parameter is set to None, PyPIM launches Mechanical using its version parameter.

batchbool, optional

Whether to launch Mechanical in batch mode. The default is True. When False, Mechanical launches in UI mode.

loglevelstr, optional

Level of messages to print to the console. Options are:

  • "WARNING": Prints only Ansys warning messages.

  • "ERROR": Prints only Ansys error messages.

  • "INFO": Prints all Ansys messages.

The default is WARNING.

log_filebool, optional

Whether to copy the messages to a file named logs.log, which is located where the Python script is executed. The default is False.

log_mechanicalstr, optional

Path to the output file on the local disk to write every script command to. The default is None. However, you might set "log_mechanical='pymechanical_log.txt'" to write all commands that are sent to Mechanical via PyMechanical to this file. You can then use these commands to run a script within Mechanical without PyMechanical.

additional_switcheslist, optional

Additional switches for Mechanical. The default is None.

additional_envsdictionary, optional

Dictionary of additional environment variables to pass. The default is None.

start_timeoutfloat, optional

Maximum allowable time in seconds to connect to the Mechanical server. The default is 120.

portint, optional

Port to launch the Mechanical gRPC server on. The default is None, in which case 10000 is used. The final port is the first port available after (or including) this port. You can override the default behavior of this parameter with the PYMECHANICAL_PORT=<VALID PORT> environment variable.

ipstr, optional

IP address to use only when start_instance is False. The default is None, in which case "127.0.0.1" is used. If you provide an IP address, start_instance is set to False. A host name can be provided as an alternative to an IP address.

start_instancebool, optional

Whether to launch and connect to a new Mechanical instance. The default is None, in which case an attempt is made to connect to an existing Mechanical instance at the given ip and port parameters, which have defaults of "127.0.0.1" and 10000 respectively. When True, a local instance of Mechanical is launched. You can override the default behavior of this parameter with the PYMECHANICAL_START_INSTANCE=FALSE environment variable.

verbose_mechanicalbool, optional

Whether to enable printing of all output when launching and running a Mechanical instance. The default is False. This parameter should be set to True for debugging only as output can be tracked within PyMechanical.

clear_on_connectbool, optional

When start_instance is False, whether to clear the environment when connecting to Mechanical. The default is False. When True, a fresh environment is provided when you connect to Mechanical.

cleanup_on_exitbool, optional

Whether to exit Mechanical when Python exits. The default is True. When False, Mechanical is not exited when the garbage for this Mechanical instance is collected.

versionstr, optional

Mechanical version to run in the three-digit format. For example, "231" for 2023 R1. The default is None, in which case the server runs the latest installed version. If PyPIM is configured and exce_file=None, PyPIM launches Mechanical using its version parameter.

keep_connection_alivebool, optional

Whether to keep the gRPC connection alive by running a background thread and making dummy calls for remote connections. The default is True.

Returns:
ansys.mechanical.core.mechanical.Mechanical

Instance of Mechanical.

Return type:

Mechanical

Notes

If the environment is configured to use PyPIM and start_instance=True, then starting the instance is delegated to PyPIM. In this case, most of the preceding parameters are ignored because the server-side configuration is used.

Examples

Launch Mechanical.

>>> from ansys.mechanical.core import launch_mechanical
>>> mech = launch_mechanical()

Launch Mechanical using a specified executable file.

>>> exec_file_path = 'C:/Program Files/ANSYS Inc/v231/aisol/bin/win64/AnsysWBU.exe'
>>> mech = launch_mechanical(exec_file_path)

Connect to an existing Mechanical instance at IP address 192.168.1.30 on port 50001.

>>> mech = launch_mechanical(start_instance=False, ip='192.168.1.30', port=50001)