Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,6 @@ import traceback
|
|
| 9 |
# --- FIX for CSV Field Size Limit ---
|
| 10 |
# Increase the CSV field size limit to handle large Plotly JSON objects that are
|
| 11 |
# generated when Gradio's Examples component tries to log the output.
|
| 12 |
-
# This is the most robust way to set the limit to the maximum supported by the system.
|
| 13 |
max_int = sys.maxsize
|
| 14 |
while True:
|
| 15 |
try:
|
|
@@ -30,9 +29,9 @@ def solve_and_plot_interactive(Lx: float,
|
|
| 30 |
initial: str = "gaussian",
|
| 31 |
bc: str = "dirichlet"):
|
| 32 |
"""
|
| 33 |
-
Solves the 2D heat equation and returns an interactive Plotly figure.
|
| 34 |
"""
|
| 35 |
-
# --- 1. Simulation Setup ---
|
| 36 |
x = np.linspace(0, Lx, Nx)
|
| 37 |
y = np.linspace(0, Ly, Ny)
|
| 38 |
dx, dy = x[1] - x[0], y[1] - y[0]
|
|
@@ -52,7 +51,7 @@ def solve_and_plot_interactive(Lx: float,
|
|
| 52 |
elif initial == "step":
|
| 53 |
u = np.where((X < Lx/2) & (Y < Ly/2), 1.0, 0.0)
|
| 54 |
|
| 55 |
-
# --- 2. Solve the Heat Equation ---
|
| 56 |
time_indices = np.linspace(0, Nt - 1, M, dtype=int)
|
| 57 |
U_slider = np.zeros((M, Nx, Ny))
|
| 58 |
store_idx = 0
|
|
@@ -79,6 +78,15 @@ def solve_and_plot_interactive(Lx: float,
|
|
| 79 |
store_idx += 1
|
| 80 |
|
| 81 |
# --- 3. Create a Plotly Figure for Web ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
points_2d = np.vstack([X.ravel(), Y.ravel()]).T
|
| 83 |
tri = Delaunay(points_2d)
|
| 84 |
|
|
@@ -93,12 +101,17 @@ def solve_and_plot_interactive(Lx: float,
|
|
| 93 |
i=tri.simplices[:, 0], j=tri.simplices[:, 1], k=tri.simplices[:, 2],
|
| 94 |
intensity=z_data,
|
| 95 |
colorscale='Viridis',
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
name=f'Time: {time_value:.2f}s',
|
| 97 |
showscale=True if i == 0 else False,
|
| 98 |
visible=(i == 0)
|
| 99 |
)
|
| 100 |
)
|
| 101 |
|
|
|
|
| 102 |
steps = []
|
| 103 |
for i in range(len(fig.data)):
|
| 104 |
time_value = (time_indices[i] / (Nt-1)) * t_max if Nt > 1 else 0
|
|
@@ -112,16 +125,26 @@ def solve_and_plot_interactive(Lx: float,
|
|
| 112 |
|
| 113 |
sliders = [dict(active=0, currentvalue={"prefix": "Time: "}, pad={"t": 50}, steps=steps)]
|
| 114 |
|
|
|
|
| 115 |
fig.update_layout(
|
| 116 |
title=f'2D Heat Eq — init={initial}, bc={bc}',
|
| 117 |
-
scene=dict(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
sliders=sliders
|
| 119 |
)
|
|
|
|
| 120 |
|
| 121 |
return fig
|
| 122 |
|
| 123 |
|
| 124 |
-
# --- Gradio Interface Function with Error Handling ---
|
| 125 |
def gradio_interface(lx, ly, t_max, m_steps, gamma, nx, ny, initial, bc):
|
| 126 |
try:
|
| 127 |
nx, ny, m_steps = int(nx), int(ny), int(m_steps)
|
|
@@ -139,7 +162,7 @@ def gradio_interface(lx, ly, t_max, m_steps, gamma, nx, ny, initial, bc):
|
|
| 139 |
)
|
| 140 |
return error_fig
|
| 141 |
|
| 142 |
-
# --- Gradio UI Definition ---
|
| 143 |
with gr.Blocks(theme=gr.themes.Soft(), title="2D Heat Simulator") as demo:
|
| 144 |
gr.Markdown("# ♨️ 2D Heat Equation Simulator")
|
| 145 |
gr.Markdown("Adjust parameters and click 'Run' to generate an interactive plot directly in your browser.")
|
|
|
|
| 9 |
# --- FIX for CSV Field Size Limit ---
|
| 10 |
# Increase the CSV field size limit to handle large Plotly JSON objects that are
|
| 11 |
# generated when Gradio's Examples component tries to log the output.
|
|
|
|
| 12 |
max_int = sys.maxsize
|
| 13 |
while True:
|
| 14 |
try:
|
|
|
|
| 29 |
initial: str = "gaussian",
|
| 30 |
bc: str = "dirichlet"):
|
| 31 |
"""
|
| 32 |
+
Solves the 2D heat equation and returns an interactive Plotly figure with a fixed axis range.
|
| 33 |
"""
|
| 34 |
+
# --- 1. Simulation Setup (No changes here) ---
|
| 35 |
x = np.linspace(0, Lx, Nx)
|
| 36 |
y = np.linspace(0, Ly, Ny)
|
| 37 |
dx, dy = x[1] - x[0], y[1] - y[0]
|
|
|
|
| 51 |
elif initial == "step":
|
| 52 |
u = np.where((X < Lx/2) & (Y < Ly/2), 1.0, 0.0)
|
| 53 |
|
| 54 |
+
# --- 2. Solve the Heat Equation (No changes here) ---
|
| 55 |
time_indices = np.linspace(0, Nt - 1, M, dtype=int)
|
| 56 |
U_slider = np.zeros((M, Nx, Ny))
|
| 57 |
store_idx = 0
|
|
|
|
| 78 |
store_idx += 1
|
| 79 |
|
| 80 |
# --- 3. Create a Plotly Figure for Web ---
|
| 81 |
+
|
| 82 |
+
# --- FIX: Calculate the global min/max temperature for a fixed Z-axis range ---
|
| 83 |
+
z_min = U_slider.min()
|
| 84 |
+
z_max = U_slider.max()
|
| 85 |
+
# Add a tiny buffer if the data is completely flat to avoid a display error
|
| 86 |
+
if z_max == z_min:
|
| 87 |
+
z_max += 1e-9
|
| 88 |
+
# --------------------------------------------------------------------------------
|
| 89 |
+
|
| 90 |
points_2d = np.vstack([X.ravel(), Y.ravel()]).T
|
| 91 |
tri = Delaunay(points_2d)
|
| 92 |
|
|
|
|
| 101 |
i=tri.simplices[:, 0], j=tri.simplices[:, 1], k=tri.simplices[:, 2],
|
| 102 |
intensity=z_data,
|
| 103 |
colorscale='Viridis',
|
| 104 |
+
# --- FIX: Set the intensity range to the global min/max for a consistent color bar ---
|
| 105 |
+
cmin=z_min,
|
| 106 |
+
cmax=z_max,
|
| 107 |
+
# -------------------------------------------------------------------------------------
|
| 108 |
name=f'Time: {time_value:.2f}s',
|
| 109 |
showscale=True if i == 0 else False,
|
| 110 |
visible=(i == 0)
|
| 111 |
)
|
| 112 |
)
|
| 113 |
|
| 114 |
+
# Slider creation logic is unchanged
|
| 115 |
steps = []
|
| 116 |
for i in range(len(fig.data)):
|
| 117 |
time_value = (time_indices[i] / (Nt-1)) * t_max if Nt > 1 else 0
|
|
|
|
| 125 |
|
| 126 |
sliders = [dict(active=0, currentvalue={"prefix": "Time: "}, pad={"t": 50}, steps=steps)]
|
| 127 |
|
| 128 |
+
# --- FIX: Update the layout to use the fixed axis ranges ---
|
| 129 |
fig.update_layout(
|
| 130 |
title=f'2D Heat Eq — init={initial}, bc={bc}',
|
| 131 |
+
scene=dict(
|
| 132 |
+
xaxis_title='X',
|
| 133 |
+
yaxis_title='Y',
|
| 134 |
+
zaxis_title='Temperature',
|
| 135 |
+
# Set the axis ranges to be fixed across all animation frames
|
| 136 |
+
xaxis=dict(range=[0, Lx]),
|
| 137 |
+
yaxis=dict(range=[0, Ly]),
|
| 138 |
+
zaxis=dict(range=[z_min, z_max]),
|
| 139 |
+
),
|
| 140 |
sliders=sliders
|
| 141 |
)
|
| 142 |
+
# -------------------------------------------------------------
|
| 143 |
|
| 144 |
return fig
|
| 145 |
|
| 146 |
|
| 147 |
+
# --- Gradio Interface Function with Error Handling (No changes here) ---
|
| 148 |
def gradio_interface(lx, ly, t_max, m_steps, gamma, nx, ny, initial, bc):
|
| 149 |
try:
|
| 150 |
nx, ny, m_steps = int(nx), int(ny), int(m_steps)
|
|
|
|
| 162 |
)
|
| 163 |
return error_fig
|
| 164 |
|
| 165 |
+
# --- Gradio UI Definition (No changes here) ---
|
| 166 |
with gr.Blocks(theme=gr.themes.Soft(), title="2D Heat Simulator") as demo:
|
| 167 |
gr.Markdown("# ♨️ 2D Heat Equation Simulator")
|
| 168 |
gr.Markdown("Adjust parameters and click 'Run' to generate an interactive plot directly in your browser.")
|