Replace Python numerical kernels with native C execution

This commit is contained in:
ljz committed 2026-09-10 01:12:18 +08:00
1 parent 48da6be21c
commit 3b38f73fe0
227 files changed
+16801 -75499

No files matched your search

+15 -7
View File
@@ -461,16 +461,16 @@ type SimulationResult = {
final: Record<string, number>;
series: Record<string, number[]>;
diagnostics: {
pressureFlow: {
pressureFlow?: {
solveCount: number;
maxScaledResidual: number;
maxEvaluationsPerSolve: number;
};
stream: {
stream?: {
maxIterationsPerSolve: number;
};
stateCount: number;
sampleCount: number;
sampleCount?: number;
};
};
@@ -1259,7 +1259,7 @@ export function observeSimulationActivityWatchdog(
},
};
}
const SUPPORTED_SOLVER_METHODS = ["BDF", "Radau", "LSODA", "RK45", "RK23", "DOP853"];
const SUPPORTED_SOLVER_METHODS = ["BDF", "RK45"];
const simulationConfigLabels: Record<SimulationNumericKey, string> = {
t_start: "起始时间",
t_stop: "结束时间",
@@ -7374,7 +7374,7 @@ function FlowWorkbench() {
) => {
const resultStatus = result.status ?? (result.success ? "completed" : "failed");
const sampleCount = Math.min(
result.diagnostics.sampleCount,
result.diagnostics.sampleCount ?? result.series.time?.length ?? 0,
result.series.time?.length ?? 0,
);
const hasUsableSamples = sampleCount >= 2;
@@ -7403,6 +7403,11 @@ function FlowWorkbench() {
failed: `仿真异常终止,已保留计算至 ${simulatedUntil} s 的 ${sampleCount} 个采样点`,
} as const;
const statusMessage = statusMessages[resultStatus];
const maxScaledResidual = result.diagnostics.pressureFlow?.maxScaledResidual;
const residualMessage =
typeof maxScaledResidual === "number" && Number.isFinite(maxScaledResidual)
? `;最大方程残差 ${formatNumber(maxScaledResidual)}`
: "";
if (resultStatus === "completed") {
setSimulationProgress({
@@ -7415,7 +7420,7 @@ function FlowWorkbench() {
});
appendConsoleEntry(
"success",
`${statusMessage},可在“结果”页签查看;最大方程残差 ${formatNumber(result.diagnostics.pressureFlow.maxScaledResidual)}`,
`${statusMessage},可在“结果”页签查看${residualMessage}`,
);
} else {
setSimulationProgress((current) => ({
@@ -7444,7 +7449,10 @@ function FlowWorkbench() {
id: `simulation-${Date.now()}`,
createdAt: new Date().toISOString(),
project: cloneValue(project),
result: cloneValue(result),
result: cloneValue({
...result,
diagnostics: { ...result.diagnostics, sampleCount },
}),
};
setResultSnapshot(nextSnapshot);
storeResultSnapshot(nextSnapshot);
+2 -2
View File
@@ -146,12 +146,12 @@ type ResultPayload = {
stateTransitionTimes?: number[];
}>;
};
pressureFlow: {
pressureFlow?: {
solveCount: number;
maxScaledResidual: number;
maxEvaluationsPerSolve: number;
};
stream: {
stream?: {
maxIterationsPerSolve: number;
};
stateCount: number;
+42
View File
@@ -0,0 +1,42 @@
import { expect, test } from "@playwright/test";
import { expandSimulationConsole, prepareApp, resultSnapshot, wideProject } from "./fixtures";
for (const diagnosticsMode of ["native", "legacy-native", "python"] as const) {
test(`completed ${diagnosticsMode} results can be published and restored`, async ({ page }) => {
await prepareApp(page);
const diagnostics = diagnosticsMode === "python"
? resultSnapshot.result.diagnostics
: { backend: "native-c", stateCount: 1,
...(diagnosticsMode === "native" ? { sampleCount: 3 } : {}) };
await page.route("**/api/system-xml/simulate-stream", route => route.fulfill({
contentType: "application/x-ndjson",
body: JSON.stringify({ event: "result", progress: 100, phase: "completed",
result: { ...resultSnapshot.result, diagnostics } }) + "\n",
}));
await page.goto("/");
await page.locator('input[type="file"]').setInputFiles({
name: "native-result.json", mimeType: "application/json",
buffer: Buffer.from(JSON.stringify({ ...wideProject, edges: [...wideProject.edges, {
id: "close-test-loop", source: "generic_sensor_4", target: "generic_sensor_1",
sourceHandle: "port_b", targetHandle: "port_a", data: { isContactEdge: false },
}] })),
});
await expect(page.getByRole("textbox", { name: "工程", exact: true })).toHaveValue(wideProject.name);
await page.getByRole("button", { name: "运行仿真", exact: true }).click();
await expandSimulationConsole(page);
const consolePanel = page.getByRole("complementary", { name: "仿真控制台", exact: true });
await expect(consolePanel).toContainText("仿真完成,已生成新的结果,共 3 个采样点");
await expect(consolePanel).not.toContainText("仿真失败");
if (diagnosticsMode === "python") {
await expect(consolePanel).toContainText("最大方程残差 0");
} else {
await expect(consolePanel).not.toContainText("最大方程残差");
}
const saved = await page.evaluate(() => JSON.parse(sessionStorage.getItem("system-simulation-flow:latest-result") ?? "null"));
expect(saved.result.diagnostics.sampleCount).toBe(3);
expect(saved.result.series).toEqual(resultSnapshot.result.series);
await page.reload();
await page.getByRole("tab", { name: "结果", exact: true }).click();
await expect(page.getByText("3 个采样点", { exact: false }).first()).toBeVisible();
});
}