Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,20 @@ To launch Fluent from Python, use the ``launch_fluent`` method:
solver_session = pyfluent.launch_fluent(mode="solver")
solver_session.health_check_service.is_serving

On Windows systems the environment variable ``AWP_ROOT<ver>``, is configured
On Windows systems the environment variable ``AWP_ROOT<ver>`` is configured
when Fluent is installed, where ``<ver>`` is the Fluent release number such as
``232`` for release 2022 R2. PyFluent automatically uses this environment
variable to locate the Fluent installation. On Linux systems configure
``231`` for release 2023 R1. PyFluent automatically uses this environment
variable to locate the latest Fluent installation. On Linux systems configure
``AWP_ROOT<ver>`` to point to the absolute path of an Ansys installation such as
``/apps/ansys_inc/v232``.
``/apps/ansys_inc/v231``.

To use a non-default installation location set ``AWP_ROOT<ver>`` or set the
``PYFLUENT_FLUENT_ROOT`` environment variable to the ``<install
location>/<version>/fluent`` directory, where ``<version>`` is the Fluent
release that you would like to use. For example, ``v232`` uses release 2022 R2.
``PYFLUENT_FLUENT_ROOT`` environment variable to the ``<install location>/<version>/fluent`` directory,
where ``<version>`` is the Fluent release that you would like to use.
For example, ``v231`` uses release 2023 R1.

For information on other ways of specifying the Fluent location for PyFluent,
see `Frequently asked questions <https://fluent.docs.pyansys.com/release/0.12/getting_started/faqs.html>`_.

Basic Usage
~~~~~~~~~~~
Expand Down
6 changes: 4 additions & 2 deletions codegen/allapigen.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import argparse
import os
from pathlib import Path

import datamodelgen
import print_fluent_version
import settingsgen
import tuigen

from ansys.fluent.core.launcher.launcher import get_ansys_version, set_ansys_version
from ansys.fluent.core.launcher.launcher import FluentVersion, get_ansys_version

if __name__ == "__main__":
if not os.getenv("PYFLUENT_LAUNCH_CONTAINER"):
Expand All @@ -26,7 +27,8 @@
args = parser.parse_args()

if args.ansys_version:
set_ansys_version(args.ansys_version)
awp_root = os.environ["AWP_ROOT" + "".join(str(FluentVersion(args.ansys_version)).split("."))[:-1]]
os.environ["PYFLUENT_FLUENT_ROOT"] = Path(awp_root) / "fluent"
if args.fluent_path:
os.environ["PYFLUENT_FLUENT_ROOT"] = args.fluent_path

Expand Down
2 changes: 1 addition & 1 deletion codegen/tuigen.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def _copy_tui_help_xml_file(version: str):
subprocess.run(f"docker container rm {container_name}", shell=is_linux)

else:
ansys_version = get_ansys_version()
ansys_version = get_ansys_version() # picking up the file from the latest install location
awp_root = os.environ["AWP_ROOT" + "".join(str(ansys_version).split("."))[:-1]]
xml_source = (
Path(awp_root)
Expand Down
6 changes: 3 additions & 3 deletions doc/source/api/launcher.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
Launching Fluent
================

.. currentmodule:: ansys.fluent.core.launcher
.. currentmodule:: ansys.fluent.core

.. autosummary::
:toctree: _autosummary
launcher.launch_fluent

launch_fluent
19 changes: 19 additions & 0 deletions doc/source/getting_started/faqs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,25 @@ processars and the Fluent GUI:
For additional launch examples, see :ref:`ref_user_guide_launch`. For descriptions of all parameters,
see the :func:`launch_fluent() <ansys.fluent.core.launcher.launcher.launch_fluent>` method.

.. _faqs_fluentloc:

How does PyFluent infer the location to launch Fluent?
------------------------------------------------------
PyFluent infers the Fluent location based on the following information, in increasing order of precedence:


#. ``AWP_ROOT<ver>`` environment variable, which is configured on Windows system
when Fluent is installed, where ``<ver>`` is the Fluent release number such as
``231`` for release 2023 R1. PyFluent automatically uses this environment
variable to locate the latest Fluent installation. On Linux systems configure
``AWP_ROOT<ver>`` to point to the absolute path of an Ansys installation such as
``/apps/ansys_inc/v231``.

#. Value of ``product_version`` parameter passed to :func:`launch_fluent() <ansys.fluent.core.launch_fluent>`.

#. ``PYFLUENT_FLUENT_ROOT`` environment variable, set this to ``<install location>/<ver>/fluent`` directory.


How do you learn how to use PyFluent?
-------------------------------------
Depending on how you prefer to learn, you can use any or all of these methods
Expand Down
12 changes: 7 additions & 5 deletions doc/source/getting_started/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,18 @@ method:
solver = pyfluent.launch_fluent(precision="double", processor_count=2, mode="solver")
solver.health_check_service.is_serving

To locate the Fluent installation, PyFluent automatically uses the ``AWP_ROOT<ver>``
To locate the latest Fluent installation, PyFluent automatically uses the ``AWP_ROOT<ver>``
environment variable, where ``<ver>`` is the three-digit format for the release.
For example, ``AWP_ROOT222`` is the environment variable for the 2022 R2 release.
For example, ``AWP_ROOT231`` is the environment variable for the 2023 R1 release.

On a Windows system, this environment variable is configured when a release is installed.

On a Linux system, you must configure this environment variable to point to the absolute
path of the installed release. For example, for the 2022 R2 release, you would set
the ``AWP_ROOT222`` environment variable to point to an absolute location such as
``C:\Program Files\ANSYS Inc\v222``.
path of the installed release. For example, for the 2023 R1 release, you would set
the ``AWP_ROOT231`` environment variable to point to an absolute location such as
``/apps/ansys_inc/v231``.

For information on other ways of specifying the Fluent location for PyFluent, see :ref:`faqs_fluentloc` in :ref:`faqs`.

Once Fluent is active, you can use the ``solver_session.tui`` interface to send
Fluent TUI commands to PyFluent. For example, this code reads a case file, updates a
Expand Down
2 changes: 0 additions & 2 deletions src/ansys/fluent/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
FluentVersion,
LaunchModes,
launch_fluent,
set_ansys_version,
set_fluent_exe_path,
)
from ansys.fluent.core.session import _BaseSession as Fluent # noqa: F401
from ansys.fluent.core.utils.logging import LOG
Expand Down
40 changes: 2 additions & 38 deletions src/ansys/fluent/core/launcher/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
_THIS_DIR = os.path.dirname(__file__)
_OPTIONS_FILE = os.path.join(_THIS_DIR, "fluent_launcher_options.json")

_FLUENT_EXE_PATH_SET = None # set by set_fluent_path
_ANSYS_VERSION_SET = None # set by set_ansys_version


def _is_windows():
"""Check if the current operating system is windows."""
Expand Down Expand Up @@ -60,38 +57,9 @@ def __str__(self):
return str(self.value)


def set_fluent_exe_path(fluent_exe_path: Union[str, Path]) -> None:
"""Set the Fluent executable path manually.

This supersedes the Fluent path set in the environment variable.
"""
if Path(fluent_exe_path).exists():
global _FLUENT_EXE_PATH_SET
_FLUENT_EXE_PATH_SET = str(fluent_exe_path)
else:
raise RuntimeError(
f"The passed path '{fluent_exe_path}' does not contain a valid Fluent executable file."
)


def set_ansys_version(version: Union[str, float, FluentVersion]) -> None:
"""Set the Fluent version manually.

This method only works if the provided Fluent version is installed
and the environment variables are updated properly. This supersedes
the Fluent path set in the environment variable.
"""
global _ANSYS_VERSION_SET
_ANSYS_VERSION_SET = str(FluentVersion(version))


def get_ansys_version() -> str:
# Look for ANSYS version in the following order:
# 1. value set by set_ansys_version
if _ANSYS_VERSION_SET:
return _ANSYS_VERSION_SET
"""Get the latest ANSYS version from AWP_ROOT environment variables."""

# 2. Search for the latest AWP_ROOT environment variable
for v in FluentVersion:
if "AWP_ROOT" + "".join(str(v).split("."))[:-1] in os.environ:
return str(v)
Expand Down Expand Up @@ -121,11 +89,7 @@ def get_exe_path(fluent_root: Path) -> Path:
if product_version:
return get_exe_path(get_fluent_root(FluentVersion(product_version)))

# 3. value set by set_fluent_exe_path
if _FLUENT_EXE_PATH_SET:
return Path(_FLUENT_EXE_PATH_SET)

# 4. value from get_ansys_version
# 3. the latest ANSYS version from AWP_ROOT environment variables
ansys_version = get_ansys_version()
return get_exe_path(get_fluent_root(FluentVersion(ansys_version)))

Expand Down
15 changes: 1 addition & 14 deletions src/ansys/fluent/core/utils/setup_for_fluent.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
from ansys.fluent.core.launcher.launcher import (
FluentVersion,
launch_fluent,
set_ansys_version,
)
from ansys.fluent.core.launcher.launcher import launch_fluent
from ansys.fluent.core.session_solver import Solver


def setup_for_fluent(*args, **kwargs):
"""Returns global PyConsole objects."""

if kwargs["product_version"] == "22.2.0":
set_ansys_version(FluentVersion.version_22R2)
elif kwargs["product_version"] == "23.1.0":
set_ansys_version(FluentVersion.version_23R1)
elif kwargs["product_version"] == "23.2.0":
set_ansys_version(FluentVersion.version_23R2)

del kwargs["product_version"]

session = launch_fluent(*args, **kwargs)
globals = {}
if "mode" in kwargs.keys() and kwargs["mode"] == "meshing":
Expand Down
77 changes: 0 additions & 77 deletions tests/test_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,6 @@
from ansys.fluent.core.launcher.launcher import get_ansys_version, get_fluent_exe_path


def test_manual_fluent_version_setting():
"""Test case for setting up the Ansys / Fluent version via program"""

old_ansys_version = launcher._ANSYS_VERSION_SET

pyfluent.set_ansys_version("23.1.0")
assert get_ansys_version() == "23.1.0"

pyfluent.set_ansys_version(22.2)
assert get_ansys_version() == "22.2.0"

pyfluent.set_ansys_version(version=pyfluent.FluentVersion.version_23R1)
assert get_ansys_version() == "23.1.0"

# version does not exist
with pytest.raises(RuntimeError):
pyfluent.set_ansys_version(22.1)

# Resets the global variable to its original state
launcher._ANSYS_VERSION_SET = old_ansys_version


def test_manual_fluent_path_setting():
"""Test case for setting up the path to fluent.exe via program"""
with pytest.raises(RuntimeError):
pyfluent.set_fluent_exe_path("X:/dir_1/dir2/xxx.exe")

with pytest.raises(RuntimeError):
pyfluent.set_fluent_exe_path("X:/dir_1/dir2/fluent.bat")


@pytest.mark.skip(reason="Can be used only locally.")
def test_unsuccessful_fluent_connection(with_launching_container):
# start-timeout is intentionally provided to be 2s for the connection to fail
Expand Down Expand Up @@ -120,71 +89,25 @@ def test_get_fluent_exe_path_from_awp_root_232(monkeypatch):
assert get_fluent_exe_path() == expected_path


def test_get_fluent_exe_path_from_set_ansys_version(monkeypatch):
monkeypatch.delenv("PYFLUENT_FLUENT_ROOT", raising=False)
monkeypatch.setenv("AWP_ROOT232", "ansys_inc/v232")
monkeypatch.setenv("AWP_ROOT231", "ansys_inc/v231")
monkeypatch.setenv("AWP_ROOT222", "ansys_inc/v222")
old_ansys_version = launcher._ANSYS_VERSION_SET
pyfluent.set_ansys_version("22.2.0")
if platform.system() == "Windows":
expected_path = Path("ansys_inc/v222/fluent") / "ntbin" / "win64" / "fluent.exe"
else:
expected_path = Path("ansys_inc/v222/fluent") / "bin" / "fluent"
assert get_ansys_version() == "22.2.0"
assert get_fluent_exe_path() == expected_path
launcher._ANSYS_VERSION_SET = old_ansys_version


def test_get_fluent_exe_path_from_set_fluent_exe_path(monkeypatch):
monkeypatch.delenv("PYFLUENT_FLUENT_ROOT", raising=False)
monkeypatch.setenv("AWP_ROOT232", "ansys_inc/v232")
monkeypatch.setenv("AWP_ROOT231", "ansys_inc/v231")
monkeypatch.setenv("AWP_ROOT222", "ansys_inc/v222")
old_ansys_version = launcher._ANSYS_VERSION_SET
pyfluent.set_ansys_version("22.2.0")
old_fluent_exe_path = launcher._FLUENT_EXE_PATH_SET
monkeypatch.setattr(Path, "exists", lambda self: True)
pyfluent.set_fluent_exe_path("ansys_inc/vNNN/fluent/bin/fluent")
expected_path = Path("ansys_inc/vNNN/fluent/bin/fluent")
assert get_fluent_exe_path() == expected_path
launcher._FLUENT_EXE_PATH_SET = old_fluent_exe_path
launcher._ANSYS_VERSION_SET = old_ansys_version


def test_get_fluent_exe_path_from_product_version_launcher_arg(monkeypatch):
monkeypatch.delenv("PYFLUENT_FLUENT_ROOT", raising=False)
monkeypatch.setenv("AWP_ROOT232", "ansys_inc/v232")
monkeypatch.setenv("AWP_ROOT231", "ansys_inc/v231")
monkeypatch.setenv("AWP_ROOT222", "ansys_inc/v222")
old_ansys_version = launcher._ANSYS_VERSION_SET
pyfluent.set_ansys_version("22.2.0")
old_fluent_exe_path = launcher._FLUENT_EXE_PATH_SET
monkeypatch.setattr(Path, "exists", lambda self: True)
pyfluent.set_fluent_exe_path("ansys_inc/vNNN/fluent/bin/fluent")
if platform.system() == "Windows":
expected_path = Path("ansys_inc/v231/fluent") / "ntbin" / "win64" / "fluent.exe"
else:
expected_path = Path("ansys_inc/v231/fluent") / "bin" / "fluent"
assert get_fluent_exe_path(product_version="23.1.0") == expected_path
launcher._FLUENT_EXE_PATH_SET = old_fluent_exe_path
launcher._ANSYS_VERSION_SET = old_ansys_version


def test_get_fluent_exe_path_from_pyfluent_fluent_root(monkeypatch):
monkeypatch.setenv("PYFLUENT_FLUENT_ROOT", "dev/vNNN/fluent")
monkeypatch.setenv("AWP_ROOT232", "ansys_inc/v232")
monkeypatch.setenv("AWP_ROOT231", "ansys_inc/v231")
monkeypatch.setenv("AWP_ROOT222", "ansys_inc/v222")
old_ansys_version = launcher._ANSYS_VERSION_SET
pyfluent.set_ansys_version("22.2.0")
old_fluent_exe_path = launcher._FLUENT_EXE_PATH_SET
monkeypatch.setattr(Path, "exists", lambda self: True)
pyfluent.set_fluent_exe_path("ansys_inc/vNNN/fluent/bin/fluent")
if platform.system() == "Windows":
expected_path = Path("dev/vNNN/fluent") / "ntbin" / "win64" / "fluent.exe"
else:
expected_path = Path("dev/vNNN/fluent") / "bin" / "fluent"
assert get_fluent_exe_path(product_version="23.1.0") == expected_path
launcher._FLUENT_EXE_PATH_SET = old_fluent_exe_path
launcher._ANSYS_VERSION_SET = old_ansys_version
4 changes: 2 additions & 2 deletions tests/test_launcher_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_launch_remote_instance(monkeypatch, new_solver_session):
monkeypatch.setattr(pypim, "connect", mock_connect)
monkeypatch.setattr(pypim, "is_configured", mock_is_configured)

if os.getenv("PYFLUENT_LAUNCH_CONTAINER") == "1":
if os.getenv("FLUENT_IMAGE_TAG"):
monkeypatch.setattr(launcher, "get_ansys_version", lambda: docker_image_version.get_version())

# Start fluent with launch_fluent
Expand All @@ -50,7 +50,7 @@ def test_launch_remote_instance(monkeypatch, new_solver_session):
# Assert: PyFluent went through the pypim workflow
assert mock_is_configured.called
assert mock_connect.called
if os.getenv("PYFLUENT_LAUNCH_CONTAINER") == "1":
if os.getenv("FLUENT_IMAGE_TAG"):
product_version = docker_image_version.get_version_for_filepath()
else:
product_version = "".join(launcher.get_ansys_version().split("."))[:-1]
Expand Down