download
raw
3.48 kB
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt
from io import BytesIO
import base64
import time
def simple_simulation(mode, nx, delta, with_perturbation):
start_time = time.time()
try:
# Paramètres très simplifiés
L = 500
nstep = min(50, int(1 / delta)) # Très court
time0 = np.linspace(0, delta * nstep, nstep + 1)
ixe = np.linspace(delta, L, nx)
# Simulation ultra-simplifiée
if mode == "Boucle ouverte":
# Signal simple
u = np.zeros((nx, len(time0)))
for i, t in enumerate(time0):
if t > 0.1:
u[:, i] = 0.01 * np.sin(2 * np.pi * 0.5 * t) * np.sin(np.pi * ixe / L)
else:
u[:, i] = 0
title = "Simulation Boucle Ouverte"
else: # PID
# Simulation PID ultra-simple
u = np.zeros((nx, len(time0)))
for i, t in enumerate(time0):
if t > 0.1:
u[:, i] = 0.005 * (1 - np.exp(-2 * t)) * np.sin(np.pi * ixe / L)
else:
u[:, i] = 0
title = "Simulation PID"
# Ajouter bruit si demandé
if with_perturbation:
noise = 0.001 * np.random.randn(nx, len(time0))
u += noise
# Créer un simple plot
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))
# Plot 1: Déformation finale
ax1.plot(ixe, u[:, -1], 'b-', linewidth=2)
ax1.set_title(f'{title} - Déformation finale')
ax1.set_xlabel('Position x (m)')
ax1.set_ylabel('Déplacement u (m)')
ax1.grid(True)
# Plot 2: Évolution au temps
ax2.plot(time0, u[-1, :], 'r-', linewidth=2)
ax2.set_title(f'{title} - Évolution du tip')
ax2.set_xlabel('Temps (s)')
ax2.set_ylabel('Déplacement u_B (m)')
ax2.grid(True)
plt.tight_layout()
# Convertir en image
buf = BytesIO()
fig.savefig(buf, format='png', dpi=100)
buf.seek(0)
img_b64 = base64.b64encode(buf.read()).decode('utf-8')
plt.close(fig)
plot_html = f'<img src="data:image/png;base64,{img_b64}" style="max-width:100%;">'
elapsed = f"Temps de calcul: {time.time() - start_time:.2f}s"
return plot_html, elapsed
except Exception as e:
return f"Erreur: {str(e)}", f"Temps: {time.time() - start_time:.2f}s"
# Interface Gradio simplifiée
with gr.Blocks() as demo:
gr.Markdown("# Simulation Robot Souple - Interface Simplifiée")
with gr.Row():
mode_dropdown = gr.Dropdown(["Boucle ouverte", "PID"], value="Boucle ouverte", label="Mode de simulation")
with gr.Row():
nx_slider = gr.Slider(5, 15, 10, label="Nombre d'éléments (nx)")
delta_slider = gr.Slider(0.01, 0.1, 0.02, label="Pas de temps (δt)")
pert_checkbox = gr.Checkbox(False, label="Avec perturbation")
with gr.Row():
run_btn = gr.Button("Lancer Simulation", variant="primary")
plot_output = gr.HTML()
time_output = gr.Textbox(label="Performance")
run_btn.click(
simple_simulation,
inputs=[mode_dropdown, nx_slider, delta_slider, pert_checkbox],
outputs=[plot_output, time_output]
)
demo.launch()

Xet Storage Details

Size:
3.48 kB
·
Xet hash:
d1817fe0b536fa986e43a952353c9eaa88c33794778a8f5e9d53e4727a0269cd

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.