Skip to content

Commit 57e71f4

Browse files
Revathyvenugopal162pre-commit-ci[bot]pyansys-ci-bot
authored
fix: run module subprocess exit properly on failing (#826)
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 cafb914 commit 57e71f4

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

doc/changelog/826.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
run module subprocess exit properly on failing

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

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,20 @@
2222

2323
"""Windows implementation of LS-DYNA runner."""
2424

25+
import logging
2526
import os
2627
from pathlib import Path
2728
import subprocess
29+
import time
2830

2931
from ansys.tools.path import get_latest_ansys_installation
3032
from ansys.tools.path.path import _get_unified_install_base_for_version
3133

3234
from ansys.dyna.core.run.base_runner import BaseRunner
3335
from ansys.dyna.core.run.options import MpiOption, Precision
3436

37+
log = logging.getLogger(__name__)
38+
3539

3640
class WindowsRunner(BaseRunner):
3741
"""Windows implementation to Run LS-DYNA.
@@ -80,6 +84,7 @@ def _get_env_script(self) -> str:
8084
script_name = "lsdynamsvar.bat"
8185
lsprepost = [p for p in os.listdir(self.solver_location) if "lsprepost" in p][0]
8286
env_script_path = os.path.join(self.solver_location, lsprepost, "LS-Run", script_name)
87+
8388
return env_script_path
8489

8590
def _get_exe_name(self) -> str:
@@ -106,15 +111,52 @@ def _scriptname(self) -> str:
106111
def run(self) -> None:
107112
"""Run LS-DYNA."""
108113
self._write_runscript()
109-
subprocess.check_call(
110-
f"cmd /c {self._scriptname}",
111-
shell=False,
112-
universal_newlines=True,
113-
cwd=self.working_directory,
114-
stdin=subprocess.PIPE,
115-
stdout=subprocess.PIPE,
116-
stderr=subprocess.PIPE,
117-
)
114+
script_path = Path(self.working_directory) / self._scriptname
115+
log_file = Path(self.working_directory) / "lsrun.out.txt"
116+
117+
try:
118+
process = subprocess.Popen(
119+
["cmd", "/c", str(script_path)],
120+
cwd=self.working_directory,
121+
stdin=subprocess.DEVNULL,
122+
stdout=subprocess.DEVNULL,
123+
stderr=subprocess.DEVNULL,
124+
universal_newlines=True,
125+
bufsize=1,
126+
)
127+
log.info("LS-DYNA execution started.")
128+
129+
warning_detected = False
130+
131+
while process.poll() is None:
132+
if log_file.exists():
133+
with log_file.open("r", encoding="utf-8", errors="ignore") as f:
134+
content = f.readlines()
135+
for line in content:
136+
if "warning" in line.lower() or "error" in line.lower():
137+
warning_detected = True
138+
time.sleep(2)
139+
140+
process.wait()
141+
if warning_detected:
142+
log.warning("LS-DYNA completed with warnings or errors in the log.")
143+
log.warning(f"Check the log file for details: {log_file}")
144+
145+
if process.returncode != 0:
146+
log.error(f"LS-DYNA run failed with exit code {process.returncode}.")
147+
if log_file.exists():
148+
log.error(f"See log file for details: {log_file}")
149+
raise RuntimeError(f"LS-DYNA failed with exit code {process.returncode}")
150+
151+
log.info("LS-DYNA run completed successfully.")
152+
153+
except subprocess.SubprocessError as e:
154+
msg = f"Subprocess execution failed: {e}"
155+
msg += f"to run LS-DYNA in {self.working_directory} with command: {self._get_command_line()}"
156+
if log_file.exists():
157+
msg += f"\nSee log file at: {log_file}"
158+
log.error(msg)
159+
raise RuntimeError(msg) from e
118160

119161
def _get_command_line(self) -> str:
120162
"""Get the command line to run LS-DYNA."""
@@ -132,5 +174,4 @@ def _get_command_line(self) -> str:
132174
command = (
133175
f'mpiexec -wdir "{self.working_directory}" -c {ncpu} -aa {self.solver} i={input_file} memory={mem}'
134176
)
135-
136177
return f"{script} && {command} > lsrun.out.txt 2>&1"

0 commit comments

Comments
 (0)