"""One execution boundary shared by XML API and native validation tools.""" from __future__ import annotations import os from app.simulation.config import SolveIVPConfig DEFAULT_NUMERIC_ENGINE = "native" def numeric_engine_name(backend: str | None = None) -> str: configured = backend or os.environ.get("SIMULATION_NUMERIC_ENGINE", "") selected = configured.strip().lower() or DEFAULT_NUMERIC_ENGINE if selected == "native-c": return "native" if selected == "native": return selected if selected == "python": raise ValueError("The Python numerical backend has been removed. Use SIMULATION_NUMERIC_ENGINE=native.") raise ValueError(f"Unknown simulation engine: {selected}.") def simulation_config(simulation) -> SolveIVPConfig: # Match the validated native pipe/chamber accuracy. Per-state SI absolute # floors are emitted by native_codegen.tolerances; XML tolerance fields # remain a separate protocol change. return SolveIVPConfig(t_start=simulation.t_start, t_stop=simulation.t_stop, method=simulation.method, rtol=1e-8, max_step=simulation.max_step) def simulate_network(network, simulation, *, progress_callback=None, cancel_check=None, activity_tracker=None, backend=None, raw_series=False): numeric_engine_name(backend) config = simulation_config(simulation) from app.simulation.native_codegen.runner import simulate_native return simulate_native(network, config, sample_step=simulation.sample_step, progress_callback=progress_callback, cancel_check=cancel_check, activity_tracker=activity_tracker, raw_series=raw_series)