mirror of
https://github.com/K-Dense-AI/scientific-agent-skills.git
synced 2026-12-04 01:24:00 +08:00
134 lines
5.8 KiB
Python
134 lines
5.8 KiB
Python
import hashlib
|
||
import importlib.util
|
||
import json
|
||
from pathlib import Path
|
||
import subprocess
|
||
import sys
|
||
|
||
import pytest
|
||
import skill_contract
|
||
|
||
np = pytest.importorskip("numpy")
|
||
ct = pytest.importorskip("cantera")
|
||
SKILL_ROOT = Path(__file__).resolve().parents[2] / "skills" / "cantera"
|
||
CliHelpTests = skill_contract.cli.help_test_case(SKILL_ROOT)
|
||
spec = importlib.util.spec_from_file_location("cantera_ignition", SKILL_ROOT / "scripts" / "ignition_delay.py")
|
||
module = importlib.util.module_from_spec(spec)
|
||
spec.loader.exec_module(module)
|
||
|
||
|
||
def config():
|
||
return json.loads((SKILL_ROOT / "assets" / "hydrogen-ignition.json").read_text())
|
||
|
||
|
||
@pytest.mark.parametrize("reactor,equilibrium_constraint", [("constant-volume", "UV"), ("constant-pressure", "HP")])
|
||
def test_hydrogen_ignition_conservation_and_independent_equilibrium(reactor, equilibrium_constraint):
|
||
conditions = config()
|
||
conditions["reactor"] = reactor
|
||
history, report = module.simulate(conditions)
|
||
assert report["status"] == "resolved"
|
||
assert report["conservation"]["passed"]
|
||
assert report["conservation"]["element_mass_fraction_absolute_drift"] < 1e-10
|
||
equilibrium = module.load_gas(conditions)
|
||
equilibrium.equilibrate(equilibrium_constraint)
|
||
assert history[-1, 1] == pytest.approx(equilibrium.T, abs=0.02)
|
||
if reactor == "constant-volume":
|
||
assert report["delay_s"] == pytest.approx(0.000313, abs=2e-6)
|
||
assert history[:, 3] == pytest.approx(np.ones(len(history)))
|
||
else:
|
||
assert history[:, 2] == pytest.approx(np.full(len(history), conditions["pressure_pa"]), rel=1e-8)
|
||
|
||
|
||
def test_unreached_ignition_is_not_a_reported_delay():
|
||
conditions = config()
|
||
conditions.update(temperature_k=500, end_time_s=0.001, samples=101)
|
||
_, report = module.simulate(conditions)
|
||
assert report["delay_s"] is None
|
||
assert report["status"] == "temperature-rise-not-reached"
|
||
|
||
|
||
def test_boundary_maximum_is_unresolved():
|
||
times = np.linspace(0, 1, 21)
|
||
report = module.delay_from_history(times, 1000 + 500 * times**3, 200)
|
||
assert report["delay_s"] is None
|
||
assert report["status"] == "derivative-maximum-at-boundary"
|
||
|
||
|
||
@pytest.mark.parametrize("change", [{"pressure_pa": -1}, {"samples": 3},
|
||
{"mole_amounts": {"H2": -1}}, {"mole_amounts": {"H2": 0}},
|
||
{"mole_amounts": {"NOT_A_SPECIES": 1}}, {"rtol": float("nan")}])
|
||
def test_invalid_conditions_rejected(change):
|
||
conditions = config()
|
||
conditions.update(change)
|
||
with pytest.raises(ValueError):
|
||
module.simulate(conditions)
|
||
|
||
|
||
def test_cli_refinement_provenance_and_replay(tmp_path):
|
||
path = SKILL_ROOT / "assets" / "hydrogen-ignition.json"
|
||
command = [sys.executable, str(SKILL_ROOT / "scripts" / "ignition_delay.py"), str(path), str(tmp_path / "run")]
|
||
result = subprocess.run(command, capture_output=True, text=True, timeout=60)
|
||
assert result.returncode == 0, result.stderr
|
||
out = tmp_path / "run"
|
||
report = json.loads((out / "report.json").read_text())
|
||
assert report["numerically_resolved"]
|
||
assert set(report["delay_relative_changes"]) == {"finer_output", "tighter_solver", "longer_horizon"}
|
||
assert all(delta <= 0.01 for delta in report["delay_relative_changes"].values())
|
||
assert report["mechanism"]["snapshot_sha256"] == hashlib.sha256((out / "mechanism.yaml").read_bytes()).hexdigest()
|
||
original = module.load_gas(config())
|
||
replay = ct.Solution(str(out / "mechanism.yaml"), "ohmech")
|
||
np.testing.assert_allclose(replay.forward_rate_constants, original.forward_rate_constants, rtol=1e-12)
|
||
assert len((out / "baseline.csv").read_text().splitlines()) == 3002
|
||
assert subprocess.run(command, capture_output=True, text=True, timeout=60).returncode != 0
|
||
|
||
|
||
def test_simulation_does_not_mutate_initial_solution(monkeypatch):
|
||
conditions = config()
|
||
gas = module.load_gas(conditions)
|
||
initial = gas.state.copy()
|
||
monkeypatch.setattr(module, "load_gas", lambda settings: gas)
|
||
history, _ = module.simulate(conditions)
|
||
np.testing.assert_array_equal(gas.state, initial)
|
||
assert history[-1, 1] > gas.T + 1000
|
||
|
||
|
||
def test_local_imported_mechanism_snapshot_is_independently_replayable(tmp_path):
|
||
source_dir = tmp_path / "input"
|
||
source_dir.mkdir()
|
||
original = module.load_gas(config())
|
||
definitions = source_dir / "definitions.yaml"
|
||
definitions.write_text(original.write_yaml(precision=17), encoding="utf-8")
|
||
source = source_dir / "local.yaml"
|
||
source.write_text("""description: Hydrogen provenance check – imported species and reactions
|
||
phases:
|
||
- name: ohmech
|
||
thermo: ideal-gas
|
||
kinetics: gas
|
||
species:
|
||
- definitions.yaml/species: all
|
||
reactions:
|
||
- definitions.yaml/reactions: all
|
||
""", encoding="utf-8")
|
||
conditions = config()
|
||
conditions["mechanism"] = "local.yaml"
|
||
path = source_dir / "config.json"
|
||
path.write_text(json.dumps(conditions), encoding="utf-8")
|
||
out = tmp_path / "result"
|
||
report = module.run(path, out)
|
||
assert report["numerically_resolved"]
|
||
assert report["mechanism"]["source_sha256"] == hashlib.sha256(source.read_bytes()).hexdigest()
|
||
snapshot = out / "mechanism.yaml"
|
||
assert report["mechanism"]["snapshot_sha256"] == hashlib.sha256(snapshot.read_bytes()).hexdigest()
|
||
assert "provenance check – imported" in snapshot.read_text(encoding="utf-8")
|
||
source.unlink()
|
||
definitions.unlink()
|
||
# A fresh process avoids Cantera's parsed-YAML cache hiding a residual import.
|
||
result = subprocess.run(
|
||
[sys.executable, "-c", "import cantera as ct, json, sys; "
|
||
"gas = ct.Solution(sys.argv[1], 'ohmech'); "
|
||
"print(json.dumps(gas.forward_rate_constants.tolist()))", str(snapshot)],
|
||
capture_output=True, text=True, timeout=30,
|
||
)
|
||
assert result.returncode == 0, result.stderr
|
||
np.testing.assert_allclose(json.loads(result.stdout), original.forward_rate_constants, rtol=1e-12)
|