From f23028531631d06752b7fdd45e02d600c378ae0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A2=E4=BA=AC=E6=B3=BD?= Date: Thu, 4 Jun 2026 05:39:42 +0000 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86csv=E8=BE=93?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cryo_tank/main.py | 3 ++- src/cryo_tank/output.py | 23 ++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/cryo_tank/main.py b/src/cryo_tank/main.py index c64e31e..0a353fb 100644 --- a/src/cryo_tank/main.py +++ b/src/cryo_tank/main.py @@ -21,7 +21,7 @@ from cryo_tank.tank_model import CryoTank from cryo_tank.heat_leak import MLIHeatLeak from cryo_tank.solver import run from cryo_tank.output import ( - save_history, plot_temperatures, plot_liquid_level, + save_history, save_history_csv, plot_temperatures, plot_liquid_level, plot_he_flow, plot_heat_fluxes, plot_pressure, ) @@ -67,6 +67,7 @@ def main(): # --- Output --- save_history(history, os.path.join(OUTPUT_DIR, "cryo_tank_history.npz")) + save_history_csv(history, os.path.join(OUTPUT_DIR, "cryo_tank_history.csv")) plot_temperatures(history, os.path.join(OUTPUT_DIR, "cryo_tank_temperatures.png")) plot_liquid_level(history, os.path.join(OUTPUT_DIR, "cryo_tank_level.png")) plot_he_flow(history, os.path.join(OUTPUT_DIR, "cryo_tank_he_flow.png")) diff --git a/src/cryo_tank/output.py b/src/cryo_tank/output.py index 58030a1..c22b948 100644 --- a/src/cryo_tank/output.py +++ b/src/cryo_tank/output.py @@ -1,7 +1,7 @@ # src/cryo_tank/output.py """ Output helpers for the cryogenic tank simulation. -Generates PNG plots and NPZ data files. +Generates PNG plots, NPZ data files, and CSV tables. """ import os @@ -19,6 +19,27 @@ def save_history(history, path): np.savez_compressed(path, **history) +def save_history_csv(history, path): + """Save all history time series to a CSV file.""" + dirname = os.path.dirname(path) + if dirname: + os.makedirs(dirname, exist_ok=True) + + field_names = list(history.keys()) + columns = [np.asarray(history[name]) for name in field_names] + n_rows = len(columns[0]) if columns else 0 + + for name, values in zip(field_names, columns): + if values.ndim != 1: + raise ValueError(f"CSV history field must be 1D: {name}") + if len(values) != n_rows: + raise ValueError(f"CSV history field length mismatch: {name}") + + data = np.column_stack(columns) if columns else np.empty((0, 0)) + header = ",".join(field_names) + np.savetxt(path, data, delimiter=",", header=header, comments="") + + def plot_temperatures(history, path): """Plot T_liq and T_ull vs time.""" fig, ax = plt.subplots(figsize=(10, 5))