From e8678a7582e8f1ba840abff881978da4f6cabeae Mon Sep 17 00:00:00 2001 From: shenw Date: Mon, 22 Jun 2026 23:59:15 +0800 Subject: [PATCH] Add Python debug runner --- CMakeLists.txt | 14 ++++++ Python/femlite_debug.py | 71 ++++++++++++++++++++++++++++++ tests/scripts/test_python_debug.py | 44 ++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 Python/femlite_debug.py create mode 100644 tests/scripts/test_python_debug.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b5f324..ad7f7c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ FortranCInterface_HEADER( "${CMAKE_BINARY_DIR}/femlite_fortran_mangle.h" MACRO_NAMESPACE "FC_" ) +find_package(Python3 COMPONENTS Interpreter) set(CMAKE_Fortran_STANDARD 95) set(CMAKE_Fortran_STANDARD_REQUIRED ON) @@ -347,6 +348,19 @@ set_tests_properties(c_api PROPERTIES WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" ) +if(Python3_Interpreter_FOUND) + add_test(NAME python_debug + COMMAND "${Python3_EXECUTABLE}" + "${CMAKE_SOURCE_DIR}/tests/scripts/test_python_debug.py" + "${CMAKE_SOURCE_DIR}" + "$" + "${CMAKE_BINARY_DIR}" + ) + set_tests_properties(python_debug PROPERTIES + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + ) +endif() + add_test(NAME implicit_inp COMMAND test_implicit_inp) set_tests_properties(implicit_inp PROPERTIES WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" diff --git a/Python/femlite_debug.py b/Python/femlite_debug.py new file mode 100644 index 0000000..10b762c --- /dev/null +++ b/Python/femlite_debug.py @@ -0,0 +1,71 @@ +from __future__ import annotations + +from dataclasses import dataclass +from pathlib import Path +import re +import subprocess +from typing import Iterable + + +_FIELD_RE = re.compile(r"^\s*([A-Za-z][A-Za-z0-9 _-]*):\s*(.+?)\s*$") + + +@dataclass +class FemLiteResult: + args: list[str] + returncode: int + stdout: str + stderr: str + + def check(self) -> "FemLiteResult": + if self.returncode != 0: + raise RuntimeError( + "FemLite command failed with code " + f"{self.returncode}: {' '.join(self.args)}\n{self.stdout}\n{self.stderr}" + ) + return self + + def fields(self) -> dict[str, str]: + values: dict[str, str] = {} + for line in self.stdout.splitlines(): + match = _FIELD_RE.match(line) + if match: + key = match.group(1).strip().lower().replace(" ", "_") + values[key] = match.group(2).strip() + return values + + def int_field(self, name: str) -> int: + return int(self.fields()[name]) + + def float_field(self, name: str) -> float: + return float(self.fields()[name]) + + +class FemLiteRunner: + def __init__(self, executable: str | Path): + self.executable = Path(executable) + + def inspect_model(self, inp_file: str | Path) -> FemLiteResult: + return self._run([str(inp_file)]) + + def run_explicit(self, inp_file: str | Path) -> FemLiteResult: + return self._run([str(inp_file), "--run"]) + + def solve_implicit_linear(self, inp_file: str | Path) -> FemLiteResult: + return self._run([str(inp_file), "--implicit-linear"]) + + def _run(self, args: Iterable[str]) -> FemLiteResult: + command = [str(self.executable)] + command.extend(args) + completed = subprocess.run( + command, + check=False, + text=True, + capture_output=True, + ) + return FemLiteResult( + args=command, + returncode=completed.returncode, + stdout=completed.stdout, + stderr=completed.stderr, + ) diff --git a/tests/scripts/test_python_debug.py b/tests/scripts/test_python_debug.py new file mode 100644 index 0000000..8974c95 --- /dev/null +++ b/tests/scripts/test_python_debug.py @@ -0,0 +1,44 @@ +from pathlib import Path +import sys + + +def main() -> int: + if len(sys.argv) != 4: + print("usage: test_python_debug.py ") + return 2 + + repo_root = Path(sys.argv[1]).resolve() + femlite_exe = Path(sys.argv[2]).resolve() + build_dir = Path(sys.argv[3]).resolve() + + sys.path.insert(0, str(repo_root / "Python")) + from femlite_debug import FemLiteRunner + + runner = FemLiteRunner(femlite_exe) + + summary = runner.inspect_model(repo_root / "examples" / "c3d4_one_element.inp") + summary.check() + fields = summary.fields() + if int(fields["nodes"]) != 4: + return 10 + if int(fields["elements"]) != 1: + return 11 + if int(fields["dofs"]) != 12: + return 12 + + solved = runner.solve_implicit_linear( + repo_root / "tests" / "data" / "c3d4_implicit_pull.inp" + ) + solved.check() + if solved.int_field("steps") != 1: + return 20 + if solved.float_field("residual") > 1.0e-8: + return 21 + + marker = build_dir / "python_debug_checked.txt" + marker.write_text("python debug interface ok\n", encoding="utf-8") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main())