Files
SystemSimulationApp/app/simulation/backends.py
T

38 lines
1.5 KiB
Python

"""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:
# Preserve the existing XML execution accuracy. The XML
# protocol's future tolerance fields are a separate compatibility change.
return SolveIVPConfig(t_start=simulation.t_start, t_stop=simulation.t_stop,
method=simulation.method, rtol=1e-6, max_step=simulation.max_step)
def simulate_network(network, simulation, *, progress_callback=None,
cancel_check=None, activity_tracker=None, backend=None):
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)