Files
SPHLite/tools/testing/test_coupling_python_081.py
2026-07-04 11:43:00 +08:00

106 lines
4.4 KiB
Python

from __future__ import annotations
import argparse
import math
import sys
from pathlib import Path
def close_to(actual: float, expected: float) -> bool:
return math.isclose(actual, expected, rel_tol=1.0e-8, abs_tol=1.0e-10)
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--library", required=True)
parser.add_argument("--input", required=True)
args = parser.parse_args()
root = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(root / "Python"))
from sphlite_debug import ( # noqa: PLC0415
COUPLING_CONTACT_STATE_IMPORT,
COUPLING_DISTRIBUTION_AREA,
COUPLING_DISTRIBUTION_MASS,
COUPLING_DISTRIBUTION_UNIFORM,
COUPLING_LOAD_PRESSURE,
COUPLING_LOAD_TRACTION,
SPHLite,
)
wrapper_source = (root / "Python" / "sphlite_debug.py").read_text(encoding="utf-8")
for symbol in (
"sphlite_coupling_apply_facet_pressures",
"sphlite_coupling_apply_facet_tractions",
"sphlite_coupling_apply_facet_resultants",
"sphlite_coupling_apply_contact_state",
"sphlite_coupling_get_contact_diagnostics",
):
if symbol not in wrapper_source:
raise AssertionError(f"Python wrapper is missing {symbol}")
if "sphft_" in wrapper_source:
raise AssertionError("Python wrapper directly references Fortran symbols")
solver = SPHLite(args.library)
solver.initialize()
try:
summary = solver.inspect(args.input)
pressure_region = summary["coupling_region_info"][0]
traction_region = summary["coupling_region_info"][1]
if pressure_region["load_mode"] != COUPLING_LOAD_PRESSURE:
raise AssertionError("Python region pressure load mode mismatch")
if pressure_region["distribution_mode"] != COUPLING_DISTRIBUTION_AREA:
raise AssertionError("Python region pressure distribution mismatch")
if pressure_region["contact_state_mode"] != COUPLING_CONTACT_STATE_IMPORT:
raise AssertionError("Python region contact state mismatch")
if traction_region["load_mode"] != COUPLING_LOAD_TRACTION:
raise AssertionError("Python region traction load mode mismatch")
if traction_region["distribution_mode"] != COUPLING_DISTRIBUTION_MASS:
raise AssertionError("Python region traction distribution mismatch")
solver.coupling_apply_facet_resultants(
1, [1], [1.0, 2.0, 3.0], COUPLING_DISTRIBUTION_UNIFORM, 101
)
balance = solver.coupling_load_balance()
if not close_to(balance["coupling_force_balance_error"], 0.0):
raise AssertionError("Python load balance force error mismatch")
solver.coupling_apply_facet_pressures(1, [1], [13.0], COUPLING_DISTRIBUTION_AREA, 102)
solver.coupling_apply_facet_tractions(
2, [1], [0.0, 4.0, 0.0], COUPLING_DISTRIBUTION_MASS, 102
)
solver.coupling_apply_contact_state(
1, [1], [0.0, 0.0, 1.0], [2.0e-4], [5.0e-3], [8.0], 102
)
contact = solver.coupling_contact_diagnostics()
if contact["coupling_active_contacts"] != 1:
raise AssertionError("Python active contact count mismatch")
if not close_to(contact["coupling_max_penetration"], 5.0e-3):
raise AssertionError("Python max penetration mismatch")
diagnostics = solver.coupling_diagnostics()
if diagnostics["coupling_active_contacts"] != 1:
raise AssertionError("Python diagnostics contact count mismatch")
if not close_to(diagnostics["coupling_max_pressure"], 13.0):
raise AssertionError("Python diagnostics pressure mismatch")
if not close_to(diagnostics["coupling_max_traction"], 4.0):
raise AssertionError("Python diagnostics traction mismatch")
try:
solver.coupling_apply_facet_tractions(2, [1], [1.0], COUPLING_DISTRIBUTION_MASS, 103)
except ValueError:
pass
else:
raise AssertionError("Python traction shape error did not raise")
try:
solver.coupling_apply_contact_state(1, [1], [0.0], [0.0], [0.0], [0.0], 103)
except ValueError:
pass
else:
raise AssertionError("Python contact shape error did not raise")
finally:
solver.finalize()
return 0
if __name__ == "__main__":
raise SystemExit(main())