Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,32 +4,35 @@ from matplotlib.animation import FuncAnimation
|
|
| 4 |
import tempfile
|
| 5 |
import gradio as gr
|
| 6 |
import plotly.graph_objects as go
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# --- Simulation Core ---
|
| 9 |
def solve_3d_heat_equation(Lx: float, Ly: float, Lz: float,
|
| 10 |
-
t_max: float,
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
"""
|
| 16 |
-
# Spatial grid
|
| 17 |
x = np.linspace(0, Lx, Nx)
|
| 18 |
y = np.linspace(0, Ly, Ny)
|
| 19 |
z = np.linspace(0, Lz, Nz)
|
| 20 |
-
dx, dy, dz = x[1] - x
|
| 21 |
-
|
|
|
|
| 22 |
raise ValueError("Nx, Ny, and Nz must be > 1.")
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
dt =
|
| 26 |
Nt = int(np.ceil(t_max / dt)) + 1
|
|
|
|
| 27 |
rx, ry, rz = Gamma * dt / dx**2, Gamma * dt / dy**2, Gamma * dt / dz**2
|
| 28 |
-
|
| 29 |
-
# Initial condition
|
| 30 |
X, Y, Z = np.meshgrid(x, y, z, indexing='ij')
|
|
|
|
| 31 |
if initial == "gaussian":
|
| 32 |
-
u = np.exp(-((X - Lx/2)**2
|
| 33 |
elif initial == "random":
|
| 34 |
u = np.random.rand(Nx, Ny, Nz)
|
| 35 |
elif initial == "sinusoidal":
|
|
@@ -40,24 +43,23 @@ def solve_3d_heat_equation(Lx: float, Ly: float, Lz: float,
|
|
| 40 |
else:
|
| 41 |
raise ValueError(f"Unknown initial condition: {initial}")
|
| 42 |
|
| 43 |
-
# Storage for solution
|
| 44 |
U = np.zeros((Nt, Nx, Ny, Nz))
|
| 45 |
-
U
|
| 46 |
|
| 47 |
-
# Time-stepping loop
|
| 48 |
for n in range(1, Nt):
|
| 49 |
un = u.copy()
|
| 50 |
-
# Interior update
|
| 51 |
u[1:-1, 1:-1, 1:-1] = (
|
| 52 |
un[1:-1, 1:-1, 1:-1]
|
| 53 |
+ rx * (un[2:, 1:-1, 1:-1] - 2 * un[1:-1, 1:-1, 1:-1] + un[:-2, 1:-1, 1:-1])
|
| 54 |
+ ry * (un[1:-1, 2:, 1:-1] - 2 * un[1:-1, 1:-1, 1:-1] + un[1:-1, :-2, 1:-1])
|
| 55 |
+ rz * (un[1:-1, 1:-1, 2:] - 2 * un[1:-1, 1:-1, 1:-1] + un[1:-1, 1:-1, :-2])
|
| 56 |
)
|
| 57 |
-
|
| 58 |
if bc == "dirichlet":
|
| 59 |
-
u[0, :, :] = u[-1, :, :] =
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
u[0, :, :] = u[1, :, :]
|
| 62 |
u[-1, :, :] = u[-2, :, :]
|
| 63 |
u[:, 0, :] = u[:, 1, :]
|
|
@@ -74,109 +76,125 @@ def solve_3d_heat_equation(Lx: float, Ly: float, Lz: float,
|
|
| 74 |
else:
|
| 75 |
raise ValueError(f"Unknown bc: {bc}")
|
| 76 |
U[n] = u.copy()
|
| 77 |
-
|
| 78 |
return U, dt
|
| 79 |
|
| 80 |
-
# --- Animation Generator ---
|
| 81 |
-
def
|
| 82 |
-
"""
|
| 83 |
-
Create and save a GIF animation from the 3D solution array U at a fixed z-slice.
|
| 84 |
-
"""
|
| 85 |
Nt, Nx, Ny, Nz = U.shape
|
| 86 |
fig, ax = plt.subplots()
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
ax.set_xlabel("x")
|
| 92 |
ax.set_ylabel("y")
|
| 93 |
plt.colorbar(im, ax=ax, label="u")
|
| 94 |
|
| 95 |
def update(frame):
|
| 96 |
-
im.set_data(U[frame, :, :,
|
| 97 |
return [im]
|
| 98 |
|
| 99 |
idx = list(range(0, Nt, frame_skip))
|
| 100 |
-
if idx[-1]
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
ani = FuncAnimation(fig, update, frames=idx, blit=True)
|
| 104 |
|
| 105 |
with tempfile.NamedTemporaryFile(suffix='.gif', delete=False) as tmpfile:
|
| 106 |
-
ani.save(tmpfile.name, writer='pillow', fps=30)
|
| 107 |
gif_path = tmpfile.name
|
| 108 |
|
| 109 |
plt.close(fig)
|
| 110 |
return gif_path
|
| 111 |
|
| 112 |
-
# --- Plotly Figure Generator ---
|
| 113 |
-
def
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
colorscale='viridis'
|
| 124 |
))
|
| 125 |
fig.update_layout(
|
| 126 |
-
title=f"Heat Distribution at t={time_label}
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
| 129 |
)
|
| 130 |
return fig
|
| 131 |
|
| 132 |
-
# --- Simulation Runner ---
|
| 133 |
-
def
|
| 134 |
U, dt = solve_3d_heat_equation(
|
| 135 |
-
Lx=lx, Ly=ly, Lz=lz, t_max=t_max, Gamma=gamma,
|
| 136 |
-
initial=initial, bc=bc
|
| 137 |
)
|
| 138 |
-
Nt = U.shape
|
| 139 |
-
|
| 140 |
-
z_slice = int((z_slice_percent / 100) * (nz - 1))
|
| 141 |
-
# Compute indices for t=0, t/4, 3t/4, t
|
| 142 |
idx0 = 0
|
| 143 |
-
idx1 = round((Nt - 1) / 4)
|
| 144 |
-
idx2 = round(3 * (Nt - 1) / 4)
|
| 145 |
-
idx3 = Nt - 1
|
| 146 |
-
|
| 147 |
u0 = U[idx0]
|
| 148 |
u1 = U[idx1]
|
| 149 |
u2 = U[idx2]
|
| 150 |
u3 = U[idx3]
|
| 151 |
-
|
| 152 |
-
fig0 =
|
| 153 |
-
fig1 =
|
| 154 |
-
fig2 =
|
| 155 |
-
fig3 =
|
| 156 |
-
|
| 157 |
-
gif_path =
|
| 158 |
return gif_path, fig0, fig1, fig2, fig3
|
| 159 |
|
| 160 |
-
# --- Gradio Interface Logic ---
|
| 161 |
-
def
|
| 162 |
nx, ny, nz, frame_skip = int(nx), int(ny), int(nz), int(frame_skip)
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
-
# --- Gradio UI Layout ---
|
| 166 |
with gr.Blocks(theme=gr.themes.Soft(), title="3D Heat Simulator") as demo:
|
| 167 |
-
gr.Markdown("#
|
| 168 |
with gr.Row():
|
| 169 |
with gr.Column(scale=1):
|
| 170 |
gr.Markdown("## Domain & Grid")
|
| 171 |
lx_slider = gr.Slider(0.1, 5.0, 1.0, 0.1, label="Lx")
|
| 172 |
ly_slider = gr.Slider(0.1, 5.0, 1.0, 0.1, label="Ly")
|
| 173 |
-
lz_slider = gr.Slider(0.1, 5.0, 1.0, 0.1, label="Lz")
|
| 174 |
-
nx_slider = gr.Slider(3,
|
| 175 |
-
ny_slider = gr.Slider(3,
|
| 176 |
-
nz_slider = gr.Slider(3,
|
| 177 |
|
| 178 |
gr.Markdown("## Simulation")
|
| 179 |
-
t_slider = gr.Slider(0.01,
|
| 180 |
gamma_slider = gr.Slider(0.001, 1.0, 0.1, 0.001, label="Gamma")
|
| 181 |
|
| 182 |
gr.Markdown("## Conditions")
|
|
@@ -188,32 +206,66 @@ with gr.Blocks(theme=gr.themes.Soft(), title="3D Heat Simulator") as demo:
|
|
| 188 |
)
|
| 189 |
|
| 190 |
gr.Markdown("## Animation")
|
| 191 |
-
frame_skip_slider = gr.Slider(1, 50,
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
run_btn = gr.Button("Run Simulation", variant="primary")
|
| 195 |
with gr.Column(scale=3):
|
| 196 |
-
gif_output = gr.Image(label="Animation")
|
| 197 |
with gr.Row():
|
| 198 |
-
plot1 = gr.Plot(label="t=0")
|
| 199 |
-
plot2 = gr.Plot(label="t=t/4")
|
| 200 |
with gr.Row():
|
| 201 |
-
plot3 = gr.Plot(label="t=3t/4")
|
| 202 |
-
plot4 = gr.Plot(label="t=
|
|
|
|
| 203 |
inputs_list = [lx_slider, ly_slider, lz_slider, t_slider, gamma_slider,
|
| 204 |
-
nx_slider, ny_slider, nz_slider, initial_dropdown, bc_dropdown, frame_skip_slider
|
| 205 |
outputs_list = [gif_output, plot1, plot2, plot3, plot4]
|
| 206 |
-
|
|
|
|
|
|
|
| 207 |
gr.Examples(
|
| 208 |
-
examples=[
|
| 209 |
-
[1.
|
| 210 |
-
[
|
| 211 |
-
[1.0, 1.0, 1.0, 0.2, 0.2, 80, 80, 80, "step", "neumann", 2, 50],
|
| 212 |
-
],
|
| 213 |
inputs=inputs_list,
|
| 214 |
outputs=outputs_list,
|
| 215 |
-
fn=
|
| 216 |
)
|
| 217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
if __name__ == "__main__":
|
| 219 |
demo.launch()
|
|
|
|
| 4 |
import tempfile
|
| 5 |
import gradio as gr
|
| 6 |
import plotly.graph_objects as go
|
| 7 |
+
import base64
|
| 8 |
+
from fastapi import FastAPI
|
| 9 |
+
from pydantic import BaseModel
|
| 10 |
|
| 11 |
+
# --- Simulation Core (3D) ---
|
| 12 |
def solve_3d_heat_equation(Lx: float, Ly: float, Lz: float,
|
| 13 |
+
t_max: float,
|
| 14 |
+
Gamma: float = 0.1,
|
| 15 |
+
Nx: int = 30, Ny: int = 30, Nz: int = 30, # Reduced default for 3D
|
| 16 |
+
initial: str = "gaussian",
|
| 17 |
+
bc: str = "dirichlet"):
|
|
|
|
|
|
|
| 18 |
x = np.linspace(0, Lx, Nx)
|
| 19 |
y = np.linspace(0, Ly, Ny)
|
| 20 |
z = np.linspace(0, Lz, Nz)
|
| 21 |
+
dx, dy, dz = x[1] - x, y[1] - y, z[1] - z
|
| 22 |
+
|
| 23 |
+
if dx == 0 or dy == 0 or dz == 0:
|
| 24 |
raise ValueError("Nx, Ny, and Nz must be > 1.")
|
| 25 |
|
| 26 |
+
# Stability condition for 3D FTCS scheme
|
| 27 |
+
dt = 0.5 / (Gamma * (1/dx**2 + 1/dy**2 + 1/dz**2)) # Adjusted for 3D
|
| 28 |
Nt = int(np.ceil(t_max / dt)) + 1
|
| 29 |
+
|
| 30 |
rx, ry, rz = Gamma * dt / dx**2, Gamma * dt / dy**2, Gamma * dt / dz**2
|
| 31 |
+
|
|
|
|
| 32 |
X, Y, Z = np.meshgrid(x, y, z, indexing='ij')
|
| 33 |
+
|
| 34 |
if initial == "gaussian":
|
| 35 |
+
u = np.exp(-(((X - Lx/2)**2 + (Y - Ly/2)**2 + (Z - Lz/2)**2) / (2*(Lx/10)**2)))
|
| 36 |
elif initial == "random":
|
| 37 |
u = np.random.rand(Nx, Ny, Nz)
|
| 38 |
elif initial == "sinusoidal":
|
|
|
|
| 43 |
else:
|
| 44 |
raise ValueError(f"Unknown initial condition: {initial}")
|
| 45 |
|
|
|
|
| 46 |
U = np.zeros((Nt, Nx, Ny, Nz))
|
| 47 |
+
U = u.copy()
|
| 48 |
|
|
|
|
| 49 |
for n in range(1, Nt):
|
| 50 |
un = u.copy()
|
|
|
|
| 51 |
u[1:-1, 1:-1, 1:-1] = (
|
| 52 |
un[1:-1, 1:-1, 1:-1]
|
| 53 |
+ rx * (un[2:, 1:-1, 1:-1] - 2 * un[1:-1, 1:-1, 1:-1] + un[:-2, 1:-1, 1:-1])
|
| 54 |
+ ry * (un[1:-1, 2:, 1:-1] - 2 * un[1:-1, 1:-1, 1:-1] + un[1:-1, :-2, 1:-1])
|
| 55 |
+ rz * (un[1:-1, 1:-1, 2:] - 2 * un[1:-1, 1:-1, 1:-1] + un[1:-1, 1:-1, :-2])
|
| 56 |
)
|
| 57 |
+
|
| 58 |
if bc == "dirichlet":
|
| 59 |
+
u[0, :, :] = u[-1, :, :] = 0.0
|
| 60 |
+
u[:, 0, :] = u[:, -1, :] = 0.0
|
| 61 |
+
u[:, :, 0] = u[:, :, -1] = 0.0
|
| 62 |
+
elif bc == "neumann": # Zero flux
|
| 63 |
u[0, :, :] = u[1, :, :]
|
| 64 |
u[-1, :, :] = u[-2, :, :]
|
| 65 |
u[:, 0, :] = u[:, 1, :]
|
|
|
|
| 76 |
else:
|
| 77 |
raise ValueError(f"Unknown bc: {bc}")
|
| 78 |
U[n] = u.copy()
|
|
|
|
| 79 |
return U, dt
|
| 80 |
|
| 81 |
+
# --- Animation Generator (3D Slice) ---
|
| 82 |
+
def create_animation_gif_3d_slice(U, Lx, Ly, Lz, initial, bc, Gamma, frame_skip, dt):
|
|
|
|
|
|
|
|
|
|
| 83 |
Nt, Nx, Ny, Nz = U.shape
|
| 84 |
fig, ax = plt.subplots()
|
| 85 |
+
|
| 86 |
+
# We'll animate the central xy-slice
|
| 87 |
+
slice_z_idx = Nz // 2
|
| 88 |
+
z_coord_slice = np.linspace(0, Lz, Nz)[slice_z_idx]
|
| 89 |
+
|
| 90 |
+
data_slice = U[0, :, :, slice_z_idx].T
|
| 91 |
+
|
| 92 |
+
im = ax.imshow(data_slice, cmap='viridis', origin='lower',
|
| 93 |
+
extent=[0, Lx, 0, Ly], vmin=U.min(), vmax=U.max())
|
| 94 |
+
ax.set_title(f"3D Heat Eq (xy-slice at z={z_coord_slice:.2f})\ninit={initial}, bc={bc}, Gamma={Gamma:.2f}")
|
| 95 |
ax.set_xlabel("x")
|
| 96 |
ax.set_ylabel("y")
|
| 97 |
plt.colorbar(im, ax=ax, label="u")
|
| 98 |
|
| 99 |
def update(frame):
|
| 100 |
+
im.set_data(U[frame, :, :, slice_z_idx].T)
|
| 101 |
return [im]
|
| 102 |
|
| 103 |
idx = list(range(0, Nt, frame_skip))
|
| 104 |
+
if not idx or (idx[-1]!= Nt - 1 and Nt > 1) : # Ensure last frame is included if Nt > 1
|
| 105 |
+
if Nt-1 not in idx: idx.append(Nt - 1)
|
| 106 |
+
if not idx and Nt ==1: # Handle case with only one time step
|
| 107 |
+
idx =
|
| 108 |
+
|
| 109 |
|
| 110 |
ani = FuncAnimation(fig, update, frames=idx, blit=True)
|
| 111 |
|
| 112 |
with tempfile.NamedTemporaryFile(suffix='.gif', delete=False) as tmpfile:
|
| 113 |
+
ani.save(tmpfile.name, writer='pillow', fps=max(1, 30 // frame_skip)) # Adjust fps based on skip
|
| 114 |
gif_path = tmpfile.name
|
| 115 |
|
| 116 |
plt.close(fig)
|
| 117 |
return gif_path
|
| 118 |
|
| 119 |
+
# --- Plotly Figure Generator (3D Volume) ---
|
| 120 |
+
def create_plotly_figure_3d(u_3d, Lx, Ly, Lz, time_label):
|
| 121 |
+
Nx, Ny, Nz = u_3d.shape
|
| 122 |
+
x_coords = np.linspace(0, Lx, Nx)
|
| 123 |
+
y_coords = np.linspace(0, Ly, Ny)
|
| 124 |
+
z_coords = np.linspace(0, Lz, Nz)
|
| 125 |
+
|
| 126 |
+
X, Y, Z = np.meshgrid(x_coords, y_coords, z_coords, indexing='ij')
|
| 127 |
+
|
| 128 |
+
fig = go.Figure(data=go.Volume(
|
| 129 |
+
x=X.flatten(),
|
| 130 |
+
y=Y.flatten(),
|
| 131 |
+
z=Z.flatten(),
|
| 132 |
+
value=u_3d.flatten(),
|
| 133 |
+
isomin=u_3d.min(),
|
| 134 |
+
isomax=u_3d.max(),
|
| 135 |
+
opacity=0.1, # needs to be small to see through all surfaces
|
| 136 |
+
surface_count=20, # needs to be a large number for good volume rendering
|
| 137 |
colorscale='viridis'
|
| 138 |
))
|
| 139 |
fig.update_layout(
|
| 140 |
+
title=f"3D Heat Distribution at t={time_label}",
|
| 141 |
+
scene=dict(
|
| 142 |
+
xaxis_title='x',
|
| 143 |
+
yaxis_title='y',
|
| 144 |
+
zaxis_title='z'
|
| 145 |
+
)
|
| 146 |
)
|
| 147 |
return fig
|
| 148 |
|
| 149 |
+
# --- Simulation Runner (Extracted Logic for 3D) ---
|
| 150 |
+
def run_simulation_3d(lx, ly, lz, t_max, gamma, nx, ny, nz, initial, bc, frame_skip):
|
| 151 |
U, dt = solve_3d_heat_equation(
|
| 152 |
+
Lx=lx, Ly=ly, Lz=lz, t_max=t_max, Gamma=gamma,
|
| 153 |
+
Nx=nx, Ny=ny, Nz=nz, initial=initial, bc=bc
|
| 154 |
)
|
| 155 |
+
Nt = U.shape
|
| 156 |
+
|
|
|
|
|
|
|
| 157 |
idx0 = 0
|
| 158 |
+
idx1 = round((Nt - 1) / 4) if Nt > 1 else 0
|
| 159 |
+
idx2 = round(3 * (Nt - 1) / 4) if Nt > 1 else 0
|
| 160 |
+
idx3 = Nt - 1 if Nt > 1 else 0
|
| 161 |
+
|
| 162 |
u0 = U[idx0]
|
| 163 |
u1 = U[idx1]
|
| 164 |
u2 = U[idx2]
|
| 165 |
u3 = U[idx3]
|
| 166 |
+
|
| 167 |
+
fig0 = create_plotly_figure_3d(u0, lx, ly, lz, "0")
|
| 168 |
+
fig1 = create_plotly_figure_3d(u1, lx, ly, lz, f"{idx1*dt:.2f}")
|
| 169 |
+
fig2 = create_plotly_figure_3d(u2, lx, ly, lz, f"{idx2*dt:.2f}")
|
| 170 |
+
fig3 = create_plotly_figure_3d(u3, lx, ly, lz, f"{idx3*dt:.2f}")
|
| 171 |
+
|
| 172 |
+
gif_path = create_animation_gif_3d_slice(U, lx, ly, lz, initial, bc, gamma, frame_skip, dt)
|
| 173 |
return gif_path, fig0, fig1, fig2, fig3
|
| 174 |
|
| 175 |
+
# --- Gradio Interface Logic (3D) ---
|
| 176 |
+
def gradio_interface_3d(lx, ly, lz, t_max, gamma, nx, ny, nz, initial, bc, frame_skip):
|
| 177 |
nx, ny, nz, frame_skip = int(nx), int(ny), int(nz), int(frame_skip)
|
| 178 |
+
gif_path, fig0, fig1, fig2, fig3 = run_simulation_3d(
|
| 179 |
+
lx, ly, lz, t_max, gamma, nx, ny, nz, initial, bc, frame_skip
|
| 180 |
+
)
|
| 181 |
+
return gif_path, fig0, fig1, fig2, fig3
|
| 182 |
|
| 183 |
+
# --- Gradio UI Layout (3D) ---
|
| 184 |
with gr.Blocks(theme=gr.themes.Soft(), title="3D Heat Simulator") as demo:
|
| 185 |
+
gr.Markdown("# 🔥 3D Heat Equation Simulator\nAdjust parameters and run the simulation. Animation shows a central xy-slice.")
|
| 186 |
with gr.Row():
|
| 187 |
with gr.Column(scale=1):
|
| 188 |
gr.Markdown("## Domain & Grid")
|
| 189 |
lx_slider = gr.Slider(0.1, 5.0, 1.0, 0.1, label="Lx")
|
| 190 |
ly_slider = gr.Slider(0.1, 5.0, 1.0, 0.1, label="Ly")
|
| 191 |
+
lz_slider = gr.Slider(0.1, 5.0, 1.0, 0.1, label="Lz (New)") # New
|
| 192 |
+
nx_slider = gr.Slider(3, 100, 30, 1, label="Nx") # Max reduced for 3D
|
| 193 |
+
ny_slider = gr.Slider(3, 100, 30, 1, label="Ny") # Max reduced for 3D
|
| 194 |
+
nz_slider = gr.Slider(3, 100, 30, 1, label="Nz (New)") # New, max reduced
|
| 195 |
|
| 196 |
gr.Markdown("## Simulation")
|
| 197 |
+
t_slider = gr.Slider(0.01, 2.0, 0.2, 0.01, label="t_max") # Max t_max reduced
|
| 198 |
gamma_slider = gr.Slider(0.001, 1.0, 0.1, 0.001, label="Gamma")
|
| 199 |
|
| 200 |
gr.Markdown("## Conditions")
|
|
|
|
| 206 |
)
|
| 207 |
|
| 208 |
gr.Markdown("## Animation")
|
| 209 |
+
frame_skip_slider = gr.Slider(1, 50, 10, 1, label="Frame Skip") # Default skip increased
|
| 210 |
+
run_btn = gr.Button("Run 3D Simulation", variant="primary")
|
| 211 |
+
|
|
|
|
| 212 |
with gr.Column(scale=3):
|
| 213 |
+
gif_output = gr.Image(label="Animation (Central XY Slice)")
|
| 214 |
with gr.Row():
|
| 215 |
+
plot1 = gr.Plot(label="3D Volume at t=0")
|
| 216 |
+
plot2 = gr.Plot(label="3D Volume at t=t/4")
|
| 217 |
with gr.Row():
|
| 218 |
+
plot3 = gr.Plot(label="3D Volume at t=3t/4")
|
| 219 |
+
plot4 = gr.Plot(label="3D Volume at t=t_max")
|
| 220 |
+
|
| 221 |
inputs_list = [lx_slider, ly_slider, lz_slider, t_slider, gamma_slider,
|
| 222 |
+
nx_slider, ny_slider, nz_slider, initial_dropdown, bc_dropdown, frame_skip_slider]
|
| 223 |
outputs_list = [gif_output, plot1, plot2, plot3, plot4]
|
| 224 |
+
|
| 225 |
+
run_btn.click(fn=gradio_interface_3d, inputs=inputs_list, outputs=outputs_list)
|
| 226 |
+
|
| 227 |
gr.Examples(
|
| 228 |
+
examples=[1.0, 1.0, 1.0, 0.2, 0.1, 20, 20, 20, "gaussian", "dirichlet", 10], # Reduced Nx,Ny,Nz for example
|
| 229 |
+
[1.5, 1.0, 0.5, 0.3, 0.05, 25, 20, 15, "sinusoidal", "periodic", 15],
|
| 230 |
+
[1.0, 1.0, 1.0, 0.1, 0.2, 30, 30, 30, "step", "neumann", 5],
|
|
|
|
|
|
|
| 231 |
inputs=inputs_list,
|
| 232 |
outputs=outputs_list,
|
| 233 |
+
fn=gradio_interface_3d
|
| 234 |
)
|
| 235 |
|
| 236 |
+
# --- FastAPI Setup for API Endpoint (3D) ---
|
| 237 |
+
app = FastAPI()
|
| 238 |
+
|
| 239 |
+
# Mount Gradio app to FastAPI
|
| 240 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
| 241 |
+
|
| 242 |
+
# Define the simulation parameters model for 3D
|
| 243 |
+
class SimulationParams3D(BaseModel):
|
| 244 |
+
lx: float
|
| 245 |
+
ly: float
|
| 246 |
+
lz: float # New
|
| 247 |
+
t_max: float
|
| 248 |
+
gamma: float
|
| 249 |
+
nx: int
|
| 250 |
+
ny: int
|
| 251 |
+
nz: int # New
|
| 252 |
+
initial: str
|
| 253 |
+
bc: str
|
| 254 |
+
frame_skip: int
|
| 255 |
+
|
| 256 |
+
# Custom API endpoint to run 3D simulation and return results
|
| 257 |
+
@app.post("/simulate_3d") # Renamed endpoint
|
| 258 |
+
def simulate_3d_api(params: SimulationParams3D):
|
| 259 |
+
gif_path, fig0, fig1, fig2, fig3 = run_simulation_3d(**params.dict())
|
| 260 |
+
with open(gif_path, "rb") as f:
|
| 261 |
+
gif_data = base64.b64encode(f.read()).decode('utf-8')
|
| 262 |
+
return {
|
| 263 |
+
"gif_base64": gif_data,
|
| 264 |
+
"plot0_3d_volume": fig0.to_json(), # Indicate 3D plot
|
| 265 |
+
"plot1_3d_volume": fig1.to_json(),
|
| 266 |
+
"plot2_3d_volume": fig2.to_json(),
|
| 267 |
+
"plot3_3d_volume": fig3.to_json()
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
if __name__ == "__main__":
|
| 271 |
demo.launch()
|