Skip to content
1 change: 1 addition & 0 deletions doc/changelog/801.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add windows exe parameter to run dyna
38 changes: 24 additions & 14 deletions src/ansys/dyna/core/run/windows_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"""Windows implementation of LS-DYNA runner."""

import os
from pathlib import Path
import subprocess

from ansys.tools.path import get_latest_ansys_installation
Expand All @@ -43,20 +44,33 @@ def __init__(self, **kwargs):
"""Initialize WindowsRunner."""
super().__init__(**kwargs)
version = kwargs.get("version", None)
self._find_solver(version)
executable = kwargs.get("executable", None)
self._find_solver(version, executable)

def set_input(self, input_file: str, working_directory: str) -> None:
"""Set input file and working directory."""
self.input_file = input_file
self.working_directory = working_directory

def _find_solver(self, version: int) -> None:
def _find_solver(self, version: int, executable: str = None) -> None:
"""Find LS-DYNA solver location."""
if version is not None:
install_loc, _ = _get_unified_install_base_for_version(version)
if executable:
exe_path = Path(executable)
if exe_path.is_file():
self.solver_location = str(exe_path.parent)
self.solver = f'"{str(exe_path)}"'
return
raise FileNotFoundError(f"Specified executable not found: {executable}")
if version:
install_base, _ = _get_unified_install_base_for_version(version)
else:
_, install_loc = get_latest_ansys_installation()
self.solver_location = os.path.join(install_loc, "ansys", "bin", "winx64")
_, install_base = get_latest_ansys_installation()
solver_dir = Path(install_base) / "ansys" / "bin" / "winx64"
solver_exe = solver_dir / self._get_exe_name()
if not solver_exe.is_file():
raise FileNotFoundError(f"LS-DYNA executable not found: {solver_exe}")
self.solver_location = str(solver_dir)
self.solver = f'"{str(solver_exe)}"'

def _get_env_script(self) -> str:
"""Get env script when running using lsrun from workbench."""
Expand All @@ -80,9 +94,6 @@ def _get_exe_name(self) -> str:
}[(self.mpi_option, self.precision)]
return exe_name

def _get_executable(self) -> str:
return os.path.join(self.solver_location, self._get_exe_name())

def _write_runscript(self) -> None:
with open(os.path.join(self.working_directory, self._scriptname), "w") as f:
f.write(self._get_command_line())
Expand All @@ -108,19 +119,18 @@ def run(self) -> None:
def _get_command_line(self) -> str:
"""Get the command line to run LS-DYNA."""
script = f'call "{self._get_env_script()}"'
solver = f'"{self._get_executable()}"'
ncpu = self.ncpu
mem = self.get_memory_string()
input_file = self.input_file
if self.mpi_option == MpiOption.SMP:
command = f"{solver} i={input_file} ncpu={ncpu} memory={mem}"
command = f"{self.solver} i={input_file} ncpu={ncpu} memory={mem}"
elif self.mpi_option == MpiOption.MPP_INTEL_MPI:
# -wdir is used here because sometimes mpiexec does not pass its working directory
# to dyna on windows when run from python subprocess
command = f'mpiexec -wdir "{self.working_directory}" -localonly -np {ncpu} {self.solver} i={input_file} memory={mem}' # noqa:E501
elif self.mpi_option == MpiOption.MPP_MS_MPI:
command = (
f'mpiexec -wdir "{self.working_directory}" -localonly -np {ncpu} {solver} i={input_file} memory={mem}'
f'mpiexec -wdir "{self.working_directory}" -c {ncpu} -aa {self.solver} i={input_file} memory={mem}'
)
elif self.mpi_option == MpiOption.MPP_MS_MPI:
command = f'mpiexec -wdir "{self.working_directory}" -c {ncpu} -aa {solver} i={input_file} memory={mem}'

return f"{script} && {command} > lsrun.out.txt 2>&1"
Loading