Skip to content

Commit 75784d7

Browse files
Revathyvenugopal162pre-commit-ci[bot]pyansys-ci-bot
authored
fix: executables handling and add mock test (#869)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent 1d682d3 commit 75784d7

File tree

4 files changed

+95
-21
lines changed

4 files changed

+95
-21
lines changed

doc/changelog/869.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Executables handling and add mock test

src/ansys/dyna/core/run/linux_runner.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# SOFTWARE.
2222

2323
import os
24+
from typing import Optional
2425

2526
from ansys.tools.path import get_dyna_path, get_latest_ansys_installation
2627
from ansys.tools.path.path import _get_unified_install_base_for_version
@@ -44,25 +45,29 @@ def set_input(self, input_file: str, working_directory: str) -> None:
4445
self.input_file = input_file
4546
self.working_directory = working_directory
4647

47-
def _find_solver(self, version: int, executable: str) -> None:
48-
atp_dyna_path = get_dyna_path(find=False, allow_input=False)
49-
if executable is not None:
50-
# User passed in executable directly. Use that.
51-
if os.path.isfile(executable):
52-
self.solver = executable
53-
elif atp_dyna_path is not None:
54-
# User stored dyna solver in ansys-tools-path, use that
48+
def _find_solver(self, version: Optional[int], executable: Optional[str]) -> None:
49+
"""Determine the appropriate LS-DYNA solver executable path."""
50+
51+
if executable:
52+
# Use user-provided executable path
53+
if not os.path.isfile(executable):
54+
raise FileNotFoundError(f"LS-DYNA executable not found at: {executable}")
55+
self.solver = executable
56+
return
57+
58+
# Check if solver is available from ansys-tools-path
59+
atp_dyna_path = get_dyna_path(find=True, allow_input=False)
60+
if atp_dyna_path:
5561
self.solver = atp_dyna_path
56-
elif version is not None:
57-
# User passed in the version, compute the path to the dyna solver from the
58-
# unified installation
59-
# of that version
62+
return
63+
64+
# Resolve from specified version or fallback to latest
65+
if version:
6066
install_loc, _ = _get_unified_install_base_for_version(version)
61-
self.solver = os.path.join(install_loc, "ansys", "bin", "linx64", self._get_exe_name())
6267
else:
63-
# User passed nothing, find the dyna solver from the latest unified installation
64-
install_loc, _ = get_latest_ansys_installation()
65-
self.solver = os.path.join(install_loc, "ansys", "bin", "linx64", self._get_exe_name())
68+
_, install_loc = get_latest_ansys_installation()
69+
70+
self.solver = os.path.join(install_loc, "ansys", "bin", "linx64", self._get_exe_name())
6671

6772
def _get_exe_name(self) -> str:
6873
exe_name = {

src/ansys/dyna/core/run/windows_runner.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,24 @@ def _find_solver(self, version: int, executable: str = None) -> None:
6262
"""Find LS-DYNA solver location."""
6363
if executable:
6464
exe_path = Path(executable)
65-
if exe_path.is_file():
66-
self.solver_location = str(exe_path.parent)
67-
self.solver = f'"{str(exe_path)}"'
68-
return
69-
raise FileNotFoundError(f"Specified executable not found: {executable}")
65+
if not exe_path.is_file():
66+
raise FileNotFoundError(f"Specified LS-DYNA executable not found: {executable}")
67+
68+
self.solver_location = str(exe_path.parent)
69+
self.solver = f'"{exe_path}"' # Proper quoting for Windows paths with spaces
70+
return
71+
7072
if version:
7173
install_base, _ = _get_unified_install_base_for_version(version)
7274
else:
7375
_, install_base = get_latest_ansys_installation()
76+
7477
solver_dir = Path(install_base) / "ansys" / "bin" / "winx64"
7578
solver_exe = solver_dir / self._get_exe_name()
79+
7680
if not solver_exe.is_file():
7781
raise FileNotFoundError(f"LS-DYNA executable not found: {solver_exe}")
82+
7883
self.solver_location = str(solver_dir)
7984
self.solver = f'"{str(solver_exe)}"'
8085

tests/test_localrun.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import pytest
2+
import os
3+
from pathlib import Path
4+
from ansys.dyna.core.run.linux_runner import LinuxRunner
5+
6+
7+
@pytest.fixture
8+
def instance():
9+
# Set default precision and mpi_option to avoid _get_exe_name() KeyError
10+
runner = LinuxRunner()
11+
runner.mpi_option = runner.mpi_option.SMP
12+
runner.precision = runner.precision.DOUBLE
13+
return runner
14+
15+
16+
def test_find_solver_with_valid_executable(monkeypatch):
17+
fake_executable = "/fake/path/lsdyna_sp.e"
18+
monkeypatch.setattr(os.path, "isfile", lambda path: path == fake_executable)
19+
20+
instance = LinuxRunner(executable=fake_executable)
21+
assert instance.solver == fake_executable
22+
23+
24+
def test_find_solver_with_invalid_executable(monkeypatch):
25+
fake_executable = "/invalid/path/lsdyna_sp.e"
26+
monkeypatch.setattr(os.path, "isfile", lambda path: False)
27+
28+
with pytest.raises(FileNotFoundError, match="LS-DYNA executable not found"):
29+
LinuxRunner(executable=fake_executable)
30+
31+
32+
def test_find_solver_with_version(monkeypatch):
33+
monkeypatch.setattr("ansys.tools.path.path._read_config_file", lambda: {})
34+
monkeypatch.setattr("ansys.tools.path.get_dyna_path", lambda *args, **kwargs: None)
35+
monkeypatch.setattr("ansys.tools.path.path._get_unified_install_base_for_version",
36+
lambda version, supported_versions=None: ("/opt/ansys/v231", None))
37+
monkeypatch.setattr(os.path, "isfile", lambda path: True)
38+
39+
instance = LinuxRunner(version=231)
40+
41+
expected_exe = "ansys/bin/linx64/lsdyna_dp.e"
42+
assert instance.solver == expected_exe
43+
44+
45+
def test_find_solver_latest_install(monkeypatch):
46+
monkeypatch.setattr("ansys.tools.path.path._read_config_file", lambda: {})
47+
monkeypatch.setattr("ansys.tools.path.get_dyna_path", lambda *args, **kwargs: None)
48+
monkeypatch.setattr("ansys.tools.path.path.get_available_ansys_installations",
49+
lambda: {251: '/usr/ansys_inc/v251'})
50+
51+
monkeypatch.setattr(
52+
"ansys.tools.path.path.get_latest_ansys_installation",
53+
lambda: (241, "/opt/ansys/v241")
54+
)
55+
56+
monkeypatch.setattr(os.path, "isfile", lambda path: True)
57+
58+
instance = LinuxRunner()
59+
60+
expected_exe = "/usr/ansys_inc/v251/ansys/bin/linx64/lsdyna_dp.e"
61+
assert instance.solver == expected_exe
62+
63+

0 commit comments

Comments
 (0)