From 9f577c0675ff7d90872f36e3f82655cf0bb90a13 Mon Sep 17 00:00:00 2001 From: shenw Date: Fri, 26 Jun 2026 13:14:38 +0800 Subject: [PATCH] Fix clean release verification inputs --- .gitignore | 2 + CMakeLists.txt | 3 + tools/README.md | 5 +- tools/testing/check_examples_layout.py | 74 +++++++ tools/testing/check_perf_contact_pair_omp.py | 99 +++++++++ tools/testing/check_source_size.py | 212 +++++++++++++++++++ tools/testing/run_example_smoke.py | 205 ++++++++++++++++++ tools/testing/test_python_debug.py | 44 ++++ 8 files changed, 642 insertions(+), 2 deletions(-) create mode 100644 tools/testing/check_examples_layout.py create mode 100644 tools/testing/check_perf_contact_pair_omp.py create mode 100644 tools/testing/check_source_size.py create mode 100644 tools/testing/run_example_smoke.py create mode 100644 tools/testing/test_python_debug.py diff --git a/.gitignore b/.gitignore index 8b3ee44..6092828 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,8 @@ CMakeCache.txt cmake_install.cmake CTestTestfile.cmake Testing/ +!tools/testing/ +!tools/testing/** *.mod *.smod *.o diff --git a/CMakeLists.txt b/CMakeLists.txt index 0b4cd78..9cb40a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -624,6 +624,9 @@ add_executable(perf_contact_pair target_link_libraries(perf_contact_pair PRIVATE femlite_core) femlite_apply_fortran_options(perf_contact_pair) +# Some output/restart tests use this source-relative scratch directory. +file(MAKE_DIRECTORY "${CMAKE_SOURCE_DIR}/build-wsl-debug") + add_test(NAME smoke COMMAND test_smoke) set_tests_properties(smoke PROPERTIES WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" diff --git a/tools/README.md b/tools/README.md index 15d1486..0c837d3 100644 --- a/tools/README.md +++ b/tools/README.md @@ -7,12 +7,13 @@ runtime library or command-line solver. - `testing/`: shared Python helpers used by CTest, example smoke tests, source size checks, and other test-authoring workflows. -- `solvers/`: small external-solver helper programs used to exercise file-based - solver adapters and integration paths. Project rules: - Keep production Fortran/C/C++ implementation under `src/`. - Keep test source and test data under `tests/`. - Keep reusable test-authoring utilities under `tools/testing/`. +- Keep benchmark drivers under `tools/benchmarks/`, with inputs and checkers in + the matching `examples//` directory. - Do not place benchmark-only or developer convenience code under `src/`. +- Do not add third-party solver shims or external solver launchers. diff --git a/tools/testing/check_examples_layout.py b/tools/testing/check_examples_layout.py new file mode 100644 index 0000000..5caf85d --- /dev/null +++ b/tools/testing/check_examples_layout.py @@ -0,0 +1,74 @@ +from __future__ import annotations + +import argparse +import re +import sys +from pathlib import Path + + +DOC_SUFFIXES = {".md", ".pdf", ".ppt", ".pptx", ".txt", ".rst"} + + +def parse_args() -> argparse.Namespace: + parser = argparse.ArgumentParser(description="Check FemLite example folder layout.") + parser.add_argument("repo_root") + return parser.parse_args() + + +def main() -> int: + args = parse_args() + repo_root = Path(args.repo_root).resolve() + examples_dir = repo_root / "examples" + violations: list[str] = [] + + if not examples_dir.exists(): + print(f"examples directory does not exist: {examples_dir}") + return 1 + + for folder in sorted(path for path in examples_dir.iterdir() if path.is_dir()): + files = [path for path in folder.iterdir() if path.is_file()] + inputs = sorted(path.name for path in files if path.suffix.lower() == ".inp") + docs = sorted(path.name for path in files if path.suffix.lower() in DOC_SUFFIXES) + run_case = folder / "run_case.bat" + + if docs and not inputs: + violations.append( + f"{folder.relative_to(repo_root)} has documentation but no .inp case" + ) + if inputs and not run_case.exists(): + violations.append( + f"{folder.relative_to(repo_root)} has .inp cases but no run_case.bat" + ) + if run_case.exists(): + text = run_case.read_text(encoding="utf-8", errors="replace").lower() + if "femlite" not in text and "run_example_case.bat" not in text: + violations.append( + f"{run_case.relative_to(repo_root)} does not appear to launch FemLite" + ) + for raw_line in text.splitlines(): + if "run_example_case.bat" not in raw_line: + continue + quoted = re.findall(r'"([^"]+)"', raw_line) + if len(quoted) < 5: + violations.append( + f"{run_case.relative_to(repo_root)} has a malformed helper call" + ) + continue + input_name = quoted[4] + if not (folder / input_name).exists(): + violations.append( + f"{run_case.relative_to(repo_root)} points to missing input {input_name}" + ) + + if violations: + print("Example layout violations:") + for violation in violations: + print(f" {violation}") + return 1 + + print("Example layout passed: documented folders have .inp cases and runnable scripts.") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/testing/check_perf_contact_pair_omp.py b/tools/testing/check_perf_contact_pair_omp.py new file mode 100644 index 0000000..4b39554 --- /dev/null +++ b/tools/testing/check_perf_contact_pair_omp.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python3 +"""Check contact pair performance smoke stats across OpenMP thread counts.""" + +from __future__ import annotations + +import os +import re +import subprocess +import sys +from pathlib import Path + + +METRICS = ( + "active_count", + "search_facets", + "search_masters", + "search_broad", + "search_active", + "bvh_nodes", + "cache_used", +) + + +def run_case(source_root: Path, executable: Path, threads: int) -> dict[str, int]: + env = os.environ.copy() + env["OMP_NUM_THREADS"] = str(threads) + proc = subprocess.run( + [str(executable), "32", "1"], + cwd=source_root, + env=env, + text=True, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + check=False, + ) + if proc.returncode != 0: + print(proc.stdout, end="") + raise SystemExit( + f"perf_contact_pair failed for OMP_NUM_THREADS={threads}: " + f"exit {proc.returncode}" + ) + return parse_metrics(proc.stdout, threads) + + +def parse_metrics(output: str, threads: int) -> dict[str, int]: + values: dict[str, int] = {} + for line in output.splitlines(): + match = re.match(r"\s*([A-Za-z_]+):\s+([-+]?\d+)\s*$", line) + if match and match.group(1) in METRICS: + values[match.group(1)] = int(match.group(2)) + + missing = [metric for metric in METRICS if metric not in values] + if missing: + print(output, end="") + raise SystemExit( + f"missing metrics for OMP_NUM_THREADS={threads}: {', '.join(missing)}" + ) + if values["active_count"] <= 0: + raise SystemExit(f"no active contact for OMP_NUM_THREADS={threads}") + if values["cache_used"] <= 0: + raise SystemExit(f"no BVH cache use for OMP_NUM_THREADS={threads}") + return values + + +def main(argv: list[str]) -> int: + if len(argv) != 3: + print( + "usage: check_perf_contact_pair_omp.py " + "" + ) + return 2 + + source_root = Path(argv[1]).resolve() + executable = Path(argv[2]).resolve() + serial = run_case(source_root, executable, 1) + parallel = run_case(source_root, executable, 4) + + mismatches = [ + metric + for metric in METRICS + if serial[metric] != parallel[metric] + ] + if mismatches: + print("OpenMP contact performance statistics differ:") + for metric in mismatches: + print( + f" {metric}: OMP=1 {serial[metric]}, " + f"OMP=4 {parallel[metric]}" + ) + return 1 + + print("perf_contact_pair OpenMP consistency: PASS") + for metric in METRICS: + print(f" {metric}: {serial[metric]}") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main(sys.argv)) diff --git a/tools/testing/check_source_size.py b/tools/testing/check_source_size.py new file mode 100644 index 0000000..6dd9c83 --- /dev/null +++ b/tools/testing/check_source_size.py @@ -0,0 +1,212 @@ +from __future__ import annotations + +import argparse +import re +import sys +from pathlib import Path + + +SOURCE_SUFFIXES = {".f90", ".c", ".h", ".py"} +FORTRAN_ROUTINE = re.compile( + r"^\s*(?:recursive\s+)?(?:subroutine|function|integer\s+function|" + r"logical\s+function|real\s*\([^)]*\)\s+function)\s+([a-zA-Z_]\w*)", + re.IGNORECASE, +) +PYTHON_FUNCTION = re.compile(r"^(?P\s*)def\s+(?P[a-zA-Z_]\w*)\s*\(") +C_FUNCTION = re.compile( + r"^\s*(?:static\s+)?(?:int|void|double|float|char|long|short|size_t|" + r"bool)\s+([a-zA-Z_]\w*)\s*\([^;]*\)\s*\{?\s*$" +) + + +def parse_args() -> argparse.Namespace: + parser = argparse.ArgumentParser(description="Check FemLite source size limits.") + parser.add_argument("repo_root") + parser.add_argument("--warn-file-lines", type=int, default=1500) + parser.add_argument("--max-file-lines", type=int, default=2000) + parser.add_argument("--warn-routine-lines", type=int, default=100) + parser.add_argument("--max-routine-lines", type=int, default=128) + parser.add_argument("--exceptions", default="") + return parser.parse_args() + + +def source_files(repo_root: Path) -> list[Path]: + files: list[Path] = [] + for folder in ("src", "tests", "tools"): + root = repo_root / folder + if not root.exists(): + continue + for path in root.rglob("*"): + if path.is_file() and path.suffix.lower() in SOURCE_SUFFIXES: + files.append(path) + return sorted(files) + + +def fortran_routines(lines: list[str]) -> list[tuple[str, int, int]]: + starts: list[tuple[str, int]] = [] + for line_no, line in enumerate(lines, start=1): + match = FORTRAN_ROUTINE.match(line) + if match: + starts.append((match.group(1), line_no)) + + routines: list[tuple[str, int, int]] = [] + for index, (name, start) in enumerate(starts): + end = starts[index + 1][1] - 1 if index + 1 < len(starts) else len(lines) + routines.append((name, start, end - start + 1)) + return routines + + +def python_functions(lines: list[str]) -> list[tuple[str, int, int]]: + routines: list[tuple[str, int, int]] = [] + for line_no, line in enumerate(lines, start=1): + match = PYTHON_FUNCTION.match(line) + if not match: + continue + indent = len(match.group("indent")) + end = len(lines) + for next_no in range(line_no + 1, len(lines) + 1): + next_line = lines[next_no - 1] + if len(next_line.strip()) == 0: + continue + next_indent = len(next_line) - len(next_line.lstrip()) + if next_indent <= indent: + end = next_no - 1 + break + routines.append((match.group("name"), line_no, end - line_no + 1)) + return routines + + +def c_functions(lines: list[str]) -> list[tuple[str, int, int]]: + routines: list[tuple[str, int, int]] = [] + line_no = 1 + while line_no <= len(lines): + match = C_FUNCTION.match(lines[line_no - 1]) + if not match: + line_no += 1 + continue + depth = lines[line_no - 1].count("{") - lines[line_no - 1].count("}") + end = line_no + while end < len(lines) and depth > 0: + end += 1 + depth += lines[end - 1].count("{") - lines[end - 1].count("}") + routines.append((match.group(1), line_no, end - line_no + 1)) + line_no = end + 1 + return routines + + +def routines_for(path: Path, lines: list[str]) -> list[tuple[str, int, int]]: + suffix = path.suffix.lower() + if suffix == ".f90": + return fortran_routines(lines) + if suffix == ".py": + return python_functions(lines) + if suffix in {".c", ".h"}: + return c_functions(lines) + return [] + + +def load_exceptions(repo_root: Path, relpath: str) -> dict[tuple[str, str, str], str]: + exceptions: dict[tuple[str, str, str], str] = {} + if not relpath: + return exceptions + + path = repo_root / relpath + if not path.exists(): + return exceptions + for raw_line in path.read_text(encoding="utf-8").splitlines(): + line = raw_line.strip() + if not line or line.startswith("#"): + continue + parts = line.split("|", 3) + if len(parts) != 4: + raise ValueError(f"bad exception line in {path}: {raw_line}") + kind, source_path, name, reason = (part.strip() for part in parts) + if not reason: + raise ValueError(f"missing exception reason in {path}: {raw_line}") + exceptions[(kind, source_path.replace("\\", "/"), name)] = reason + return exceptions + + +def exception_reason( + exceptions: dict[tuple[str, str, str], str], + kind: str, + relative: Path, + name: str = "", +) -> str: + return exceptions.get((kind, relative.as_posix(), name), "") + + +def main() -> int: + args = parse_args() + repo_root = Path(args.repo_root).resolve() + violations: list[str] = [] + warnings: list[str] = [] + exceptions = load_exceptions(repo_root, args.exceptions) + used_exceptions: set[tuple[str, str, str]] = set() + + for path in source_files(repo_root): + lines = path.read_text(encoding="utf-8").splitlines() + relative = path.relative_to(repo_root) + file_reason = exception_reason(exceptions, "file", relative) + if len(lines) > args.max_file_lines: + message = f"{relative}: file has {len(lines)} lines, hard limit is {args.max_file_lines}" + if file_reason: + used_exceptions.add(("file", relative.as_posix(), "")) + warnings.append(f"{message}; exception: {file_reason}") + else: + violations.append(message) + elif len(lines) > args.warn_file_lines: + warnings.append( + f"{relative}: file has {len(lines)} lines, review target is " + f"{args.warn_file_lines}" + ) + for name, start, count in routines_for(path, lines): + routine_reason = exception_reason(exceptions, "routine", relative, name) + if count > args.max_routine_lines: + message = ( + f"{relative}:{start}: {name} has {count} lines, " + f"hard limit is {args.max_routine_lines}" + ) + if routine_reason: + used_exceptions.add(("routine", relative.as_posix(), name)) + warnings.append(f"{message}; exception: {routine_reason}") + else: + violations.append(message) + elif count > args.warn_routine_lines: + warnings.append( + f"{relative}:{start}: {name} has {count} lines, review target " + f"is {args.warn_routine_lines}" + ) + + for kind, source_path, name in sorted(set(exceptions) - used_exceptions): + violations.append( + f"stale source size exception: {kind}|{source_path}|{name} " + "does not match a current hard-threshold item" + ) + + if violations: + print("Source size limit violations:") + for violation in violations: + print(f" {violation}") + if warnings: + print("Source size warnings:") + for warning in warnings: + print(f" {warning}") + return 1 + + print( + "Source size limits passed: " + f"files require review above {args.warn_file_lines} lines and " + f"exceptions above {args.max_file_lines}; " + f"routines target {args.warn_routine_lines} lines and require " + f"exceptions above {args.max_routine_lines}" + ) + if warnings: + print("Source size warnings:") + for warning in warnings: + print(f" {warning}") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/testing/run_example_smoke.py b/tools/testing/run_example_smoke.py new file mode 100644 index 0000000..b3b1b58 --- /dev/null +++ b/tools/testing/run_example_smoke.py @@ -0,0 +1,205 @@ +from __future__ import annotations + +import argparse +import csv +import re +import subprocess +import sys +from pathlib import Path + + +def parse_args() -> argparse.Namespace: + parser = argparse.ArgumentParser(description="Run a lightweight FemLite example smoke test.") + parser.add_argument("repo_root") + parser.add_argument("femlite_exe") + parser.add_argument("build_dir") + parser.add_argument("case_name") + parser.add_argument("input_relpath") + parser.add_argument("mode", choices=("run", "static-linear")) + parser.add_argument("--dynamic-steps", type=int, default=0) + parser.add_argument("--output-interval", type=int, default=0) + parser.add_argument("--require-contact", action="store_true") + parser.add_argument("--require-friction", action="store_true") + parser.add_argument("--probe-node", type=int) + parser.add_argument("--probe-field", choices=("ux", "uy", "uz")) + parser.add_argument("--min-abs-displacement", type=float, default=0.0) + parser.add_argument("--sign", choices=("negative", "positive", "either"), default="either") + parser.add_argument( + "--check-script", + help="Optional repo-relative Python checker called with the example output directory.", + ) + return parser.parse_args() + + +def shortened_dynamic_input(source: Path, destination: Path, steps: int) -> Path: + lines = source.read_text(encoding="utf-8").splitlines() + for index, line in enumerate(lines): + if line.strip().upper().startswith("*DYNAMIC"): + data_index = index + 1 + if data_index >= len(lines): + raise ValueError(f"{source} has *DYNAMIC without a data line") + parts = lines[data_index].split(",") + if not parts: + raise ValueError(f"{source} has an empty *DYNAMIC data line") + dt = float(parts[0]) + lines[data_index] = f"{dt:.10E}, {dt * steps:.10E}" + destination.write_text("\n".join(lines) + "\n", encoding="utf-8") + return destination + raise ValueError(f"{source} does not contain *DYNAMIC") + + +def run_case(args: argparse.Namespace) -> tuple[str, int]: + repo_root = Path(args.repo_root).resolve() + femlite_exe = Path(args.femlite_exe).resolve() + build_dir = Path(args.build_dir).resolve() + source_inp = repo_root / args.input_relpath + case_dir = build_dir / "example_smoke" / args.case_name + case_dir.mkdir(parents=True, exist_ok=True) + + inp = source_inp + expected_steps = 1 if args.mode == "static-linear" else None + if args.dynamic_steps > 0: + inp = shortened_dynamic_input(source_inp, case_dir / source_inp.name, args.dynamic_steps) + expected_steps = args.dynamic_steps + + command = [str(femlite_exe), str(inp)] + if args.mode == "run": + command.append("--run") + elif args.mode == "static-linear": + command.append("--static-linear") + command.extend(["--output-dir", str(case_dir)]) + command.extend(["--output-interval", str(args.output_interval)]) + + completed = subprocess.run( + command, + cwd=repo_root, + text=True, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + ) + print(completed.stdout) + if completed.returncode != 0: + raise RuntimeError(f"FemLite exited with code {completed.returncode}") + + match = re.search(r"steps:\s*(\d+)", completed.stdout) + if not match: + raise RuntimeError("FemLite output did not contain a step count") + steps = int(match.group(1)) + if expected_steps is not None and steps != expected_steps: + raise RuntimeError(f"expected {expected_steps} steps, got {steps}") + return completed.stdout, steps + + +def check_contact_output(build_dir: Path, case_name: str, step: int) -> None: + contact_file = build_dir / "example_smoke" / case_name / f"contact_{step:08d}.csv" + if not contact_file.exists(): + raise RuntimeError(f"missing contact output: {contact_file}") + + with contact_file.open(newline="", encoding="utf-8") as handle: + rows = list(csv.DictReader(handle)) + summary = next((row for row in rows if row.get("kind") == "SUMMARY"), None) + if summary is None: + raise RuntimeError(f"{contact_file} does not contain a SUMMARY row") + active = int(summary["active_count"]) + if active <= 0: + raise RuntimeError(f"{contact_file} did not record active contact") + + +def check_friction_output(build_dir: Path, case_name: str, step: int) -> None: + contact_file = build_dir / "example_smoke" / case_name / f"contact_{step:08d}.csv" + if not contact_file.exists(): + raise RuntimeError(f"missing contact output: {contact_file}") + + with contact_file.open(newline="", encoding="utf-8") as handle: + rows = [row for row in csv.DictReader(handle) if row.get("kind") == "PAIR"] + if not rows: + raise RuntimeError(f"{contact_file} does not contain contact pair rows") + + max_slip = max(float(row["accumulated_slip"]) for row in rows) + max_work = max(float(row["friction_work_total"]) for row in rows) + min_work = min(float(row["friction_work_total"]) for row in rows) + max_reg = max(float(row["friction_regularization"]) for row in rows) + max_ft = max( + max(abs(float(row["ftx"])), abs(float(row["fty"])), abs(float(row["ftz"]))) + for row in rows + ) + if max_reg <= 0.0: + raise RuntimeError(f"{contact_file} did not report friction regularization") + if max_slip <= 0.0 or max_ft <= 0.0: + raise RuntimeError(f"{contact_file} did not record frictional slip") + if min_work < -1.0e-12 or max_work <= 0.0: + raise RuntimeError(f"{contact_file} did not record positive friction work") + + +def check_node_probe(args: argparse.Namespace, step: int) -> None: + if args.probe_node is None: + return + if args.probe_field is None: + raise RuntimeError("--probe-field is required when --probe-node is set") + + build_dir = Path(args.build_dir).resolve() + node_file = build_dir / "example_smoke" / args.case_name / f"nodes_{step:08d}.csv" + if not node_file.exists(): + raise RuntimeError(f"missing node output: {node_file}") + + with node_file.open(newline="", encoding="utf-8") as handle: + rows = list(csv.DictReader(handle)) + row = next( + (item for item in rows if int(item["node_id"]) == args.probe_node), + None, + ) + if row is None: + raise RuntimeError(f"{node_file} does not contain node {args.probe_node}") + + value = float(row[args.probe_field]) + if abs(value) < args.min_abs_displacement: + raise RuntimeError( + f"{args.probe_field} at node {args.probe_node} is too small: {value}" + ) + if args.sign == "negative" and value >= 0.0: + raise RuntimeError( + f"{args.probe_field} at node {args.probe_node} is not negative: {value}" + ) + if args.sign == "positive" and value <= 0.0: + raise RuntimeError( + f"{args.probe_field} at node {args.probe_node} is not positive: {value}" + ) + + +def run_custom_checker(args: argparse.Namespace) -> None: + if not args.check_script: + return + + repo_root = Path(args.repo_root).resolve() + build_dir = Path(args.build_dir).resolve() + case_dir = build_dir / "example_smoke" / args.case_name + check_script = repo_root / args.check_script + if not check_script.exists(): + raise RuntimeError(f"missing check script: {check_script}") + + completed = subprocess.run( + [sys.executable, str(check_script), str(case_dir)], + cwd=repo_root, + text=True, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + ) + print(completed.stdout) + if completed.returncode != 0: + raise RuntimeError(f"{check_script} exited with code {completed.returncode}") + + +def main() -> int: + args = parse_args() + _, steps = run_case(args) + if args.require_contact: + check_contact_output(Path(args.build_dir).resolve(), args.case_name, steps) + if args.require_friction: + check_friction_output(Path(args.build_dir).resolve(), args.case_name, steps) + check_node_probe(args, steps) + run_custom_checker(args) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tools/testing/test_python_debug.py b/tools/testing/test_python_debug.py new file mode 100644 index 0000000..8974c95 --- /dev/null +++ b/tools/testing/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())