220 lines
9.1 KiB
Python
220 lines
9.1 KiB
Python
from __future__ import annotations
|
|
|
|
import argparse
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
|
|
def run_command(args: list[str], expect_ok: bool = True) -> subprocess.CompletedProcess[str]:
|
|
result = subprocess.run(args, text=True, capture_output=True)
|
|
if expect_ok and result.returncode != 0:
|
|
raise AssertionError(result.stderr or result.stdout)
|
|
if not expect_ok and result.returncode == 0:
|
|
raise AssertionError("command unexpectedly succeeded")
|
|
return result
|
|
|
|
|
|
def require_field(output: str, field: str) -> None:
|
|
if f"{field}:" not in output:
|
|
raise AssertionError(f"missing CLI field {field}")
|
|
|
|
|
|
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 read_summary(path: Path) -> dict[str, str]:
|
|
metrics: dict[str, str] = {}
|
|
for line in path.read_text(encoding="utf-8").splitlines()[1:]:
|
|
if "," in line:
|
|
name, value = line.split(",", 1)
|
|
metrics[name.strip()] = value.strip()
|
|
return metrics
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--exe", required=True)
|
|
parser.add_argument("--input", required=True)
|
|
parser.add_argument("--bad-input", required=True)
|
|
parser.add_argument("--output-dir", required=True)
|
|
args = parser.parse_args()
|
|
|
|
exe = str(Path(args.exe))
|
|
model = str(Path(args.input))
|
|
bad_model = str(Path(args.bad_input))
|
|
output_dir = str(Path(args.output_dir))
|
|
|
|
version = run_command([exe, "--version"])
|
|
require_field(version.stdout, "version")
|
|
if "0.4.1" not in version.stdout:
|
|
raise AssertionError("CLI version mismatch")
|
|
|
|
inspect = run_command([exe, model])
|
|
for field in (
|
|
"version",
|
|
"input",
|
|
"mode",
|
|
"particles",
|
|
"warnings",
|
|
"time",
|
|
"step",
|
|
"water_particles",
|
|
"water_mass",
|
|
"max_pressure",
|
|
"dtcrit",
|
|
"gas_particles",
|
|
"gas_volume",
|
|
"gas_mean_pressure",
|
|
"gas_max_pressure",
|
|
"gas_mass",
|
|
"gas_momentum_x",
|
|
"gas_min_density",
|
|
"gas_max_density",
|
|
"gas_equivalent_radius",
|
|
"gas_dt_cfl",
|
|
"gas_dt_accel",
|
|
"gas_dtcrit",
|
|
"gas_source_mass",
|
|
"gas_source_energy",
|
|
"explosive_phase_particles",
|
|
"explosive_particles",
|
|
"explosive_reacted_particles",
|
|
"explosive_lambda_min",
|
|
"explosive_lambda_max",
|
|
"explosive_lambda_mean",
|
|
"explosive_released_mass",
|
|
"explosive_released_energy",
|
|
"explosive_detonation_time_min",
|
|
"explosive_detonation_time_max",
|
|
"explosive_initial_mass",
|
|
"explosive_remaining_mass",
|
|
"explosive_release_mass_error",
|
|
"explosive_release_energy_error",
|
|
"explosive_release_momentum_x",
|
|
"explosive_release_momentum_y",
|
|
"explosive_release_momentum_z",
|
|
"status",
|
|
):
|
|
require_field(inspect.stdout, field)
|
|
inspect_fields = read_fields(inspect.stdout)
|
|
if inspect_fields["version"] != "0.4.1":
|
|
raise AssertionError("inspect version field mismatch")
|
|
if Path(inspect_fields["input"]) != Path(model):
|
|
raise AssertionError("inspect input field mismatch")
|
|
if inspect_fields["mode"] != "inspect":
|
|
raise AssertionError("inspect mode not reported")
|
|
if int(inspect_fields["particles"]) != 4:
|
|
raise AssertionError("CLI particle count mismatch")
|
|
if int(inspect_fields["warnings"]) != 0:
|
|
raise AssertionError("CLI warning count mismatch")
|
|
if abs(float(inspect_fields["time"])) > 1.0e-15:
|
|
raise AssertionError("inspect time should be zero")
|
|
if int(inspect_fields["step"]) != 0:
|
|
raise AssertionError("inspect step should be zero")
|
|
if int(inspect_fields["water_particles"]) != 4:
|
|
raise AssertionError("CLI water particle count mismatch")
|
|
if float(inspect_fields["water_mass"]) <= 0.0:
|
|
raise AssertionError("CLI water mass mismatch")
|
|
if float(inspect_fields["max_pressure"]) < 0.0:
|
|
raise AssertionError("CLI max pressure mismatch")
|
|
if float(inspect_fields["dtcrit"]) <= 0.0:
|
|
raise AssertionError("CLI dtcrit mismatch")
|
|
if int(inspect_fields["gas_particles"]) != 0:
|
|
raise AssertionError("CLI water inspect gas particle count mismatch")
|
|
if int(inspect_fields["explosive_particles"]) != 0:
|
|
raise AssertionError("CLI water inspect explosive particle count mismatch")
|
|
if inspect_fields["status"] != "ok":
|
|
raise AssertionError("CLI status field mismatch")
|
|
|
|
gas_model = str(Path(args.input).parent / "gas_ideal_minimal_030.inp")
|
|
gas_inspect = run_command([exe, gas_model])
|
|
gas_fields = read_fields(gas_inspect.stdout)
|
|
if int(gas_fields["gas_particles"]) != 4:
|
|
raise AssertionError("CLI gas particle count mismatch")
|
|
if float(gas_fields["gas_volume"]) <= 0.0:
|
|
raise AssertionError("CLI gas volume mismatch")
|
|
if float(gas_fields["gas_max_pressure"]) <= 0.0:
|
|
raise AssertionError("CLI gas pressure mismatch")
|
|
if float(gas_fields["gas_equivalent_radius"]) <= 0.0:
|
|
raise AssertionError("CLI gas radius mismatch")
|
|
if float(gas_fields["gas_dtcrit"]) <= 0.0:
|
|
raise AssertionError("CLI gas dt mismatch")
|
|
if float(gas_fields["gas_mass"]) <= 0.0:
|
|
raise AssertionError("CLI gas extended mass mismatch")
|
|
if float(gas_fields["gas_source_mass"]) != 0.0:
|
|
raise AssertionError("CLI gas source default mismatch")
|
|
if int(gas_fields["explosive_particles"]) != 0:
|
|
raise AssertionError("CLI gas inspect explosive particle count mismatch")
|
|
|
|
explosive_model = str(Path(args.input).parent / "explosive_conversion_phase_040.inp")
|
|
explosive_run = run_command([exe, explosive_model, "--run"])
|
|
explosive_fields = read_fields(explosive_run.stdout)
|
|
if int(explosive_fields["explosive_phase_particles"]) <= 0:
|
|
raise AssertionError("CLI explosive phase particle count mismatch")
|
|
if int(explosive_fields["explosive_particles"]) <= 0:
|
|
raise AssertionError("CLI explosive tracked particle count mismatch")
|
|
if int(explosive_fields["explosive_reacted_particles"]) <= 0:
|
|
raise AssertionError("CLI explosive reacted particle count mismatch")
|
|
if float(explosive_fields["explosive_lambda_max"]) <= 0.0:
|
|
raise AssertionError("CLI explosive lambda mismatch")
|
|
if float(explosive_fields["explosive_released_energy"]) <= 0.0:
|
|
raise AssertionError("CLI explosive released energy mismatch")
|
|
if float(explosive_fields["explosive_initial_mass"]) <= 0.0:
|
|
raise AssertionError("CLI explosive initial mass mismatch")
|
|
if float(explosive_fields["explosive_release_energy_error"]) != 0.0:
|
|
raise AssertionError("CLI explosive release energy balance mismatch")
|
|
|
|
check = run_command([exe, model, "--check"])
|
|
if "mode: check" not in check.stdout:
|
|
raise AssertionError("check mode not reported")
|
|
|
|
run = run_command([exe, model, "--run", "--output", output_dir])
|
|
run_fields = read_fields(run.stdout)
|
|
if run_fields["mode"] != "run":
|
|
raise AssertionError("run mode not reported")
|
|
if float(run_fields["time"]) <= 0.0:
|
|
raise AssertionError("run mode time did not advance")
|
|
if int(run_fields["step"]) <= 0:
|
|
raise AssertionError("run mode did not advance")
|
|
output_step = int(run_fields["step"])
|
|
output_file = Path(output_dir) / f"sphlite_{output_step:08d}.csv"
|
|
summary_file = Path(output_dir) / f"sphlite_{output_step:08d}_summary.csv"
|
|
if not output_file.exists() or not summary_file.exists():
|
|
raise AssertionError("CLI output files were not written")
|
|
summary = read_summary(summary_file)
|
|
if int(summary["step"]) != output_step:
|
|
raise AssertionError("CLI summary step does not match actual step")
|
|
if abs(float(summary["time"]) - float(run_fields["time"])) > 1.0e-12:
|
|
raise AssertionError("CLI summary time does not match actual time")
|
|
|
|
bad = run_command([exe, bad_model, "--check"], expect_ok=False)
|
|
require_field(bad.stderr, "error_code")
|
|
require_field(bad.stderr, "error_message")
|
|
missing_arg = run_command([exe], expect_ok=False)
|
|
if "usage:" not in missing_arg.stdout:
|
|
raise AssertionError("missing input did not print usage")
|
|
missing_file = run_command([exe, str(Path(output_dir) / "missing.inp")], expect_ok=False)
|
|
require_field(missing_file.stderr, "error_code")
|
|
conflict = run_command([exe, model, "--run", "--check"], expect_ok=False)
|
|
if "usage:" not in conflict.stdout:
|
|
raise AssertionError("conflicting modes did not print usage")
|
|
missing_output = run_command([exe, model, "--run", "--output"], expect_ok=False)
|
|
if "usage:" not in missing_output.stdout:
|
|
raise AssertionError("missing output path did not print usage")
|
|
illegal_output = run_command(
|
|
[exe, model, "--run", "--output", str(Path(output_dir) / "missing-dir" / "nested")],
|
|
expect_ok=False,
|
|
)
|
|
require_field(illegal_output.stderr, "error_code")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|