Spaces:
Sleeping
Sleeping
| import os | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from matplotlib.animation import FuncAnimation | |
| import gradio as gr | |
| # Function to generate buckling frames | |
| def generate_buckling_frame(length=10, amplitude=1, mode=1, frames=100): | |
| """ | |
| Generates the GIF for lateral torsional buckling. | |
| :param length: Length of the beam | |
| :param amplitude: Maximum deflection | |
| :param mode: Buckling mode (1, 2, 3, ...) | |
| :param frames: Number of frames in the animation | |
| :return: Path to the generated GIF | |
| """ | |
| x = np.linspace(0, length, 500) | |
| # Create figure | |
| fig = plt.figure(figsize=(8, 4)) | |
| ax = fig.add_subplot(111, projection='3d') | |
| ax.set_xlim(0, length) | |
| ax.set_ylim(-amplitude, amplitude) | |
| ax.set_zlim(-amplitude, amplitude) | |
| ax.set_xlabel("Length (x)") | |
| ax.set_ylabel("Lateral (y)") | |
| ax.set_zlabel("Torsional (z)") | |
| line, = ax.plot([], [], [], lw=2) | |
| # Update function for the animation | |
| def update(frame): | |
| t = frame / frames | |
| y = amplitude * np.sin(mode * np.pi * x / length) * np.cos(2 * np.pi * t) | |
| z = amplitude * np.sin(mode * np.pi * x / length) * np.sin(2 * np.pi * t) | |
| line.set_data(x, y) | |
| line.set_3d_properties(z) | |
| return line, | |
| # Create animation | |
| ani = FuncAnimation(fig, update, frames=frames, interval=50, blit=False) | |
| # Save as GIF | |
| gif_path = "lateral_torsional_buckling.gif" | |
| ani.save(gif_path, writer="pillow") | |
| plt.close(fig) # Close the figure to free up memory | |
| return gif_path | |
| # Gradio Interface function | |
| def generate_animation(length, amplitude, mode): | |
| gif_path = generate_buckling_frame(length, amplitude, mode) | |
| return gif_path | |
| # Gradio Blocks UI | |
| with gr.Blocks() as app: | |
| gr.Markdown("# Lateral Torsional Buckling Animation") | |
| with gr.Row(): | |
| length_slider = gr.Slider(5, 20, value=10, step=1, label="Beam Length") | |
| amplitude_slider = gr.Slider(0.1, 2, value=1, step=0.1, label="Amplitude") | |
| mode_slider = gr.Slider(1, 5, value=1, step=1, label="Buckling Mode") | |
| generate_btn = gr.Button("Generate Animation") | |
| output = gr.Image(label="Buckling Animation") | |
| generate_btn.click( | |
| generate_animation, | |
| inputs=[length_slider, amplitude_slider, mode_slider], | |
| outputs=output | |
| ) | |
| # Launch the Gradio app | |
| app.launch(server_port=int(os.environ.get("GRADIO_SERVER_PORT", 7860))) | |