Add coupling output restart and APIs
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def run_command(args: list[str]) -> subprocess.CompletedProcess[str]:
|
||||
result = subprocess.run(args, text=True, capture_output=True)
|
||||
if result.returncode != 0:
|
||||
raise AssertionError(result.stderr or result.stdout)
|
||||
return result
|
||||
|
||||
|
||||
def read_fields(output: str) -> dict[str, str]:
|
||||
fields: dict[str, str] = {}
|
||||
for line in output.splitlines():
|
||||
if ":" in line:
|
||||
name, value = line.split(":", 1)
|
||||
fields[name.strip()] = value.strip()
|
||||
return fields
|
||||
|
||||
|
||||
def require_field(fields: dict[str, str], field: str) -> None:
|
||||
if field not in fields:
|
||||
raise AssertionError(f"missing coupling CLI field {field}")
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--exe", required=True)
|
||||
parser.add_argument("--input", required=True)
|
||||
args = parser.parse_args()
|
||||
|
||||
exe = str(Path(args.exe))
|
||||
model = str(Path(args.input))
|
||||
|
||||
fields = read_fields(run_command([exe, model]).stdout)
|
||||
for field in (
|
||||
"coupling_regions",
|
||||
"coupling_region_1_name",
|
||||
"coupling_region_1_export_mode",
|
||||
"coupling_region_1_import_mode",
|
||||
"coupling_region_1_enabled",
|
||||
"coupling_region_1_facets",
|
||||
"coupling_region_2_name",
|
||||
"coupling_region_2_export_mode",
|
||||
"coupling_region_2_import_mode",
|
||||
"coupling_region_2_particles",
|
||||
"coupling_force_x",
|
||||
"coupling_force_y",
|
||||
"coupling_force_z",
|
||||
"coupling_impulse_x",
|
||||
"coupling_impulse_y",
|
||||
"coupling_impulse_z",
|
||||
"coupling_work",
|
||||
"coupling_energy_exchange",
|
||||
"coupling_nonfinite_imports",
|
||||
"coupling_stale_loads",
|
||||
):
|
||||
require_field(fields, field)
|
||||
|
||||
if int(fields["coupling_regions"]) != 2:
|
||||
raise AssertionError("coupling CLI region count mismatch")
|
||||
if fields["coupling_region_1_name"] != "SHELL_FACETS":
|
||||
raise AssertionError("coupling CLI region 1 name mismatch")
|
||||
if int(fields["coupling_region_1_export_mode"]) != 2:
|
||||
raise AssertionError("coupling CLI facet export mode mismatch")
|
||||
if int(fields["coupling_region_1_import_mode"]) != 2:
|
||||
raise AssertionError("coupling CLI facet import mode mismatch")
|
||||
if int(fields["coupling_region_1_enabled"]) != 1:
|
||||
raise AssertionError("coupling CLI enabled flag mismatch")
|
||||
if int(fields["coupling_region_1_facets"]) != 3:
|
||||
raise AssertionError("coupling CLI facet count mismatch")
|
||||
if fields["coupling_region_2_name"] != "SHELL_BOTH":
|
||||
raise AssertionError("coupling CLI region 2 name mismatch")
|
||||
if int(fields["coupling_region_2_export_mode"]) != 3:
|
||||
raise AssertionError("coupling CLI both export mode mismatch")
|
||||
if int(fields["coupling_region_2_import_mode"]) != 1:
|
||||
raise AssertionError("coupling CLI force import mode mismatch")
|
||||
if int(fields["coupling_region_2_particles"]) != 3:
|
||||
raise AssertionError("coupling CLI particle count mismatch")
|
||||
if int(fields["coupling_nonfinite_imports"]) != 0:
|
||||
raise AssertionError("coupling CLI nonfinite count mismatch")
|
||||
if int(fields["coupling_stale_loads"]) != 0:
|
||||
raise AssertionError("coupling CLI stale count mismatch")
|
||||
|
||||
check_fields = read_fields(run_command([exe, model, "--check"]).stdout)
|
||||
if check_fields["mode"] != "check":
|
||||
raise AssertionError("coupling CLI check mode mismatch")
|
||||
if int(check_fields["coupling_regions"]) != 2:
|
||||
raise AssertionError("coupling CLI check region count mismatch")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
@@ -0,0 +1,76 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
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_EXPORT_BOTH,
|
||||
COUPLING_IMPORT_FORCE,
|
||||
PHASE_SHELL,
|
||||
SPHLite,
|
||||
)
|
||||
|
||||
wrapper_source = (root / "python" / "sphlite_debug.py").read_text(encoding="utf-8")
|
||||
if "sphlite_coupling_get_region_count" not in wrapper_source:
|
||||
raise AssertionError("Python wrapper is missing coupling region query")
|
||||
if "sphlite_coupling_apply_particle_forces" not in wrapper_source:
|
||||
raise AssertionError("Python wrapper is missing coupling force import")
|
||||
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)
|
||||
if summary["coupling_regions"] != 2:
|
||||
raise AssertionError("coupling inspect region count mismatch")
|
||||
region_info = summary["coupling_region_info"]
|
||||
if region_info[1]["name"] != "SHELL_BOTH":
|
||||
raise AssertionError("coupling inspect region name mismatch")
|
||||
if region_info[1]["export_mode"] != COUPLING_EXPORT_BOTH:
|
||||
raise AssertionError("coupling inspect export mode mismatch")
|
||||
if region_info[1]["import_mode"] != COUPLING_IMPORT_FORCE:
|
||||
raise AssertionError("coupling inspect import mode mismatch")
|
||||
if summary["coupling_nonfinite_imports"] != 0:
|
||||
raise AssertionError("coupling inspect nonfinite count mismatch")
|
||||
|
||||
export = solver.coupling_particle_export(2)
|
||||
if len(export["ext_ids"]) != 3 or export["ext_ids"][0] != 1:
|
||||
raise AssertionError("coupling Python particle export mismatch")
|
||||
if export["phases"][0] != PHASE_SHELL:
|
||||
raise AssertionError("coupling Python particle phase mismatch")
|
||||
if len(export["scalar_values"]) != 3 * 7:
|
||||
raise AssertionError("coupling Python scalar export shape mismatch")
|
||||
if len(export["vector_values"]) != 3 * 3 * 3:
|
||||
raise AssertionError("coupling Python vector export shape mismatch")
|
||||
|
||||
solver.coupling_apply_particle_forces(2, [1], [2.0, 0.0, 0.0])
|
||||
diagnostics = solver.coupling_diagnostics()
|
||||
if diagnostics["coupling_imported_forces"] != 1:
|
||||
raise AssertionError("coupling Python diagnostics import count mismatch")
|
||||
if len(diagnostics["coupling_force"]) != 3:
|
||||
raise AssertionError("coupling Python diagnostics force shape mismatch")
|
||||
try:
|
||||
solver.coupling_apply_particle_forces(2, [1], [1.0, 0.0])
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError("coupling Python force shape error did not raise")
|
||||
finally:
|
||||
solver.finalize()
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user