优化仿真求解性能并修复流量闭合问题(初版)

This commit is contained in:
ljz committed 2026-08-16 17:46:05 +08:00
1 parent 57b459bc72
commit 5332a788f3
55 files changed
+8973 -549

No files matched your search

+37 -9
View File
@@ -1,6 +1,7 @@
from __future__ import annotations
from collections.abc import Callable, Iterator, Mapping
from collections.abc import AsyncIterator, Callable, Iterator, Mapping
from contextlib import asynccontextmanager
import csv
from dataclasses import dataclass
from datetime import datetime, timezone
@@ -22,6 +23,7 @@ from fastapi.responses import FileResponse, HTMLResponse, StreamingResponse
from pydantic import BaseModel, ConfigDict, Field, ValidationError
from app.simulation.performance import performance_span, profile_phase, profile_run
from app.simulation.property_cache import property_cache_run
from app.system_xml import (
SystemXmlDocument,
SystemXmlValidationReport,
@@ -35,7 +37,19 @@ if TYPE_CHECKING:
from app.simulation.systems.network import SimulationNetwork
app = FastAPI(title="System Simulation ReactFlow App")
@asynccontextmanager
async def _app_lifespan(application: FastAPI) -> AsyncIterator[None]:
from app.simulation.warmup import warm_up_simulation_runtime
application.state.simulation_warmup = warm_up_simulation_runtime().as_dict()
yield
app = FastAPI(
title="System Simulation ReactFlow App",
lifespan=_app_lifespan,
)
FRONTEND_DIST_DIR = Path(__file__).resolve().parent.parent / "frontend" / "dist"
PROJECT_STORAGE_DIR = Path(__file__).parent / "data" / "reactflow-projects"
SYSTEM_XML_SCHEMA_VERSION = "3"
@@ -668,14 +682,25 @@ def run_system_xml_simulation(
progress_callback: SimulationProgressEmitter | None = None,
cancel_check: Callable[[], bool] | None = None,
) -> dict[str, object]:
with profile_run() as trace:
result = _run_system_xml_simulation_profiled(
xml_bytes,
progress_callback,
cancel_check,
)
with property_cache_run() as property_cache:
with profile_run() as trace:
result = _run_system_xml_simulation_profiled(
xml_bytes,
progress_callback,
cancel_check,
)
performance = trace.snapshot()
if performance.get("mode") == "audit" and property_cache is not None:
cache_info = property_cache.info()
performance["propertyCache"] = {
"hits": cache_info.hits,
"misses": cache_info.misses,
"maxEntriesPerCache": cache_info.max_entries_per_cache,
"cacheCount": cache_info.cache_count,
"currentEntries": cache_info.current_entries,
"evictions": cache_info.evictions,
}
if performance.get("mode") != "off":
diagnostics = dict(result.get("diagnostics", {}))
diagnostics["performance"] = performance
@@ -766,6 +791,9 @@ def _run_system_xml_simulation_profiled(
},
) from exc
except AlgebraicSolveError as exc:
algebraic_diagnostics = exc.diagnostics.as_dict()
algebraic_diagnostics["scopeKind"] = exc.scope_kind
algebraic_diagnostics["scopeComponents"] = list(exc.scope_components)
raise HTTPException(
status_code=422,
detail={
@@ -778,7 +806,7 @@ def _run_system_xml_simulation_profiled(
"message": str(exc),
}
],
"diagnostics": exc.diagnostics.as_dict(),
"diagnostics": algebraic_diagnostics,
},
) from exc
except StreamSolveError as exc: