Optislang#

class ansys.optislang.core.optislang.Optislang(host: str | None = None, port: int | None = None, executable: str | Path | None = None, project_path: str | Path | None = None, batch: bool = True, service: bool = False, port_range: Tuple[int, int] | None = None, no_run: bool | None = None, no_save: bool = False, force: bool = True, reset: bool = False, auto_relocate: bool = False, listener_id: str | None = None, multi_listener: Iterable[Tuple[str, int, str | None]] | None = None, ini_timeout: int | float = 60, name: str | None = None, password: str | None = None, loglevel: str | None = None, shutdown_on_finished: bool = True, env_vars: Mapping[str, str] | None = None, import_project_properties_file: str | Path | None = None, export_project_properties_file: str | Path | None = None, import_placeholders_file: str | Path | None = None, export_placeholders_file: str | Path | None = None, output_file: str | Path | None = None, dump_project_state: str | Path | None = None, opx_project_definition_file: str | Path | None = None, additional_args: Iterable[str] | None = None)#

Connects to the optiSLang app and provides an API to control it.

For remote connection, it is assumed that optiSLang is already running on a remote (or local) host as a server. In this case, the host and port must be specified. Parameters related to the execution of the new optiSLang server are ignored.

To run optiSLang locally, both the host and port parameters must be None, which are their defaults. Other parameters can be optionally specified.

Parameters:
hostOptional[str], optional

IPv4/v6 address or domain name on which optiSLang is running as a server. The default is None.

portOptional[int], optional

Port on which optiSLang is running as a server. The default is None.

executableOptional[Union[str, pathlib.Path]], optional

Path to the optiSLang executable file to execute on a the local host. The default is None. This parameter is ignored when host and port parameters are specified.

project_pathOptional[Union[str, pathlib.Path]], optional

Path to the optiSLang project file that a new local optiSLang server is to use. The default is None. This parameter is ignored when host and port parameters are specified. Here is how this parameter is used:

  • If the project file exists, it is opened.

  • If the project file does not exist, a project is created in the specified path.

  • If the path is None, a new project is created in the temporary directory.

batchbool, optional

Determines whether to start optiSLang server in batch mode. Defaults to True.

..note:: Cannot be used in combination with service mode.

service: bool, optional

Determines whether to start optiSLang server in service mode. If True, batch argument is set to False. Defaults to False.

..note:: Cannot be used in combination with batch mode.

port_rangeTuple[int, int], optional

Defines the port range for optiSLang server. Defaults to None.

no_runOptional[bool], optional

Determines whether not to run the specified project when started in batch mode. Defaults to None.

Note

Only supported in batch mode.

no_savebool, optional

Whether to save the specified project after all other actions are completed. The default is False. This parameter is ignored when host and port parameters are specified.

Note

Only supported in batch mode.

forcebool, optional

Determines whether to force opening/processing specified project when started in batch mode even if issues occur. Defaults to True.

Note

Only supported in batch mode.

resetbool, optional

Determines whether to reset specified project after load. Defaults to False.

Note

Only supported in batch mode.

auto_relocatebool, optional

Determines whether to automatically relocate missing file paths. Defaults to False.

Note

Only supported in batch mode.

listener_idOptional[str], optional

Specific unique ID for the TCP listener. Defaults to None.

multi_listenerIterable[Tuple[str, int, Optional[str]]], optional

Multiple remote listeners (plain TCP/IP based) to be registered at optiSLang server. Each listener is a combination of host, port and (optionally) listener ID. Defaults to None.

ini_timeoutfloat, optional

Time in seconds to connect to the optiSLang server. The default is 20.

nameOptional[str], optional

ID of the optiSLang instance. Defaults to None.

passwordOptional[str], optional

Server password. The default is None. This parameter is used when communication with the server requires that the request contain a password. Defaults to None.

loglevelOptional[str], optional

Logging level. The options are:

  • CRITICAL: Log errors that are fatal for the app.

  • ERROR: Log errors that are fatal for some operation, but not for the app.

  • WARNING: Log some oddities or potential problems.

  • INFO: Log some useful information that the program works as expected.

  • DEBUG: Log all information for use in debugging.

Defaults to None.

shutdown_on_finished: bool, optional

Whether to shut down when execution is finished and no listeners are registered. The default is True. This parameter is ignored when host and port parameters are specified.

Note

Only supported in batch mode.

env_varsOptional[Mapping[str, str]], optional

Additional environmental variables (key and value) for the optiSLang server process. Defaults to None.

import_project_properties_fileOptional[Union[str, pathlib.Path]], optional

Optional path to a project properties file to import. Defaults to None.

export_project_properties_fileOptional[Union[str, pathlib.Path]], optional

Optional path to a project properties file to export. Defaults to None.

Note

Only supported in batch mode.

import_placeholders_fileOptional[Union[str, pathlib.Path]], optional

Optional path to a placeholders file to import. Defaults to None.

export_placeholders_fileOptional[Union[str, pathlib.Path]], optional

Optional path to a placeholders file to export. Defaults to None.

Note

Only supported in batch mode.

output_fileOptional[Union[str, pathlib.Path]], optional

Optional path to an output file for writing project run results to. Defaults to None.

Note

Only supported in batch mode.

dump_project_stateOptional[Union[str, pathlib.Path]], optional

Optional path to a project state dump file to export. If a relative path is provided, it is considered to be relative to the project working directory. Defaults to None.

Note

Only supported in batch mode.

opx_project_definition_fileOptional[Union[str, pathlib.Path]], optional

Optional path to an OPX project definition file. Defaults to None.

Note

Only supported in batch mode.

additional_argsOptional[Iterable[str]], optional

Additional command line arguments used for execution of the optiSLang server process. Defaults to None.

Raises:
RuntimeError

Raised when the connection to the optiSLang server cannot be established before the specified timeout.

Examples

Start and connect to the local optiSLang server, get the version of optiSLang in use, print the version, and shut down the server.

>>> from ansys.optislang.core import Optislang
>>> osl = Optislang()
>>> osl_version = osl.osl_version_string
>>> print(osl_version)
>>> osl.dispose()

Methods

Optislang.dispose()

Close all local threads and unregister listeners.

Optislang.get_osl_server()

Get the currently used instance of the OslServer.

Optislang.get_osl_version()

Get the optiSLang version in use as a tuple.

Optislang.get_osl_version_string()

Get the optiSLang version in use as a string.

Optislang.get_timeout()

Get the timeout value for executing commands.

Optislang.get_working_dir()

Get the path to the optiSLang project's working directory.

Optislang.new()

Create and open a new project.

Optislang.open(file_path[, force, restore, ...])

Open a project.

Optislang.reset()

Reset the project.

Optislang.run_python_file(file_path[, args])

Read a Python script from a file, load it in a project context, and run it.

Optislang.run_python_script(script[, args])

Load a Python script in a project context and run it.

Optislang.save()

Save changes to the project data and settings.

Optislang.save_as(file_path[, force, ...])

Save and open the project at a new location.

Optislang.save_copy(file_path)

Save a copy of the project to a specified location..

Optislang.set_timeout([timeout])

Set the timeout value for the executing commands.

Optislang.shutdown([force])

Shut down the optiSLang server.

Optislang.start([wait_for_started, ...])

Start project execution.

Optislang.stop([wait_for_finished])

Stop project execution.

Attributes

Optislang.application

Instance of the Application class.

Optislang.has_active_project

Whether a project is loaded.

Optislang.log

Instance logger.

Optislang.name

Unique ID of the optiSLang instance.

Optislang.osl_server

Get the currently used instance of the OslServer.

Optislang.osl_version

Version of used optiSLang.

Optislang.osl_version_string

Version of used optiSLang.

Optislang.project

Instance of the Project class.

Optislang.timeout

Get the timeout value for executing commands.