Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
from calculator import (
|
| 7 |
+
deflection_simply_supported_point_load,
|
| 8 |
+
deflection_simply_supported_udl,
|
| 9 |
+
deflection_cantilever_point_load,
|
| 10 |
+
deflection_cantilever_udl,
|
| 11 |
+
generate_deflection_curve,
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
plt.switch_backend("Agg") # safe for non-interactive environments (Spaces)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def plot_deflection(x, y, L):
|
| 18 |
+
fig, ax = plt.subplots(figsize=(8, 3))
|
| 19 |
+
ax.plot(x, y)
|
| 20 |
+
ax.axhline(0, linewidth=0.8)
|
| 21 |
+
ax.set_xlabel("x (m)")
|
| 22 |
+
ax.set_ylabel("Deflection (m)")
|
| 23 |
+
ax.set_title("Beam Deflection Curve")
|
| 24 |
+
ax.grid(True)
|
| 25 |
+
ax.set_xlim(0, L)
|
| 26 |
+
fig.tight_layout()
|
| 27 |
+
buf = BytesIO()
|
| 28 |
+
fig.savefig(buf, format="png")
|
| 29 |
+
buf.seek(0)
|
| 30 |
+
plt.close(fig)
|
| 31 |
+
return buf
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def run(beam_type, L, load_type, load_value, load_pos, E, I, num_points):
|
| 35 |
+
# convert inputs
|
| 36 |
+
L = float(L)
|
| 37 |
+
E = float(E)
|
| 38 |
+
I = float(I)
|
| 39 |
+
load_value = float(load_value)
|
| 40 |
+
num_points = int(num_points)
|
| 41 |
+
|
| 42 |
+
# For point load use load_pos (a). For UDL, load_pos ignored.
|
| 43 |
+
a = float(load_pos) if load_type == "Point Load" else None
|
| 44 |
+
|
| 45 |
+
x, y = generate_deflection_curve(
|
| 46 |
+
beam_type=beam_type,
|
| 47 |
+
load_type=load_type,
|
| 48 |
+
L=L,
|
| 49 |
+
load_value=load_value,
|
| 50 |
+
a=a,
|
| 51 |
+
E=E,
|
| 52 |
+
I=I,
|
| 53 |
+
num=num_points,
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# find maximum deflection magnitude and its x location
|
| 57 |
+
# deflections are returned as signed values (downwards negative)
|
| 58 |
+
abs_y = np.abs(y)
|
| 59 |
+
idx = np.argmax(abs_y)
|
| 60 |
+
max_defl = float(y[idx])
|
| 61 |
+
max_x = float(x[idx])
|
| 62 |
+
|
| 63 |
+
buf = plot_deflection(x, y, L)
|
| 64 |
+
|
| 65 |
+
# Return nicely formatted outputs
|
| 66 |
+
return (
|
| 67 |
+
f"{max_defl:.6e} m",
|
| 68 |
+
f"{max_x:.4f} m",
|
| 69 |
+
buf,
|
| 70 |
+
f"x sample: {np.round(x[:6], 6).tolist()} ...",
|
| 71 |
+
f"y sample: {np.round(y[:6], 9).tolist()} ...",
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
title = "Beam Deflection Calculator"
|
| 76 |
+
desc = "Calculate beam deflections for common beam/load cases and view the deflection curve."
|
| 77 |
+
|
| 78 |
+
with gr.Blocks() as demo:
|
| 79 |
+
gr.Markdown(f"# {title}\n\n{desc}")
|
| 80 |
+
|
| 81 |
+
with gr.Row():
|
| 82 |
+
with gr.Column():
|
| 83 |
+
beam_type = gr.Radio(
|
| 84 |
+
["Simply Supported", "Cantilever"],
|
| 85 |
+
label="Beam Type",
|
| 86 |
+
value="Simply Supported",
|
| 87 |
+
)
|
| 88 |
+
L = gr.Number(label="Length L (m)", value=1.0)
|
| 89 |
+
load_type = gr.Radio(
|
| 90 |
+
["Point Load", "Uniformly Distributed Load"],
|
| 91 |
+
label="Load Type",
|
| 92 |
+
value="Point Load",
|
| 93 |
+
)
|
| 94 |
+
load_value = gr.Number(label="Load (N or N/m)", value=100.0)
|
| 95 |
+
load_pos = gr.Number(
|
| 96 |
+
label="Load position a (m) — for point load (from left / fixed end)",
|
| 97 |
+
value=0.5,
|
| 98 |
+
)
|
| 99 |
+
E = gr.Number(label="Elastic modulus E (Pa)", value=2.1e11)
|
| 100 |
+
I = gr.Number(label="Moment of inertia I (m^4)", value=1e-6)
|
| 101 |
+
num_points = gr.Slider(50, 2000, value=400, step=10, label="Points for curve")
|
| 102 |
+
run_btn = gr.Button("Calculate")
|
| 103 |
+
|
| 104 |
+
with gr.Column():
|
| 105 |
+
max_defl_out = gr.Textbox(label="Max deflection", interactive=False)
|
| 106 |
+
max_x_out = gr.Textbox(label="Location of max deflection", interactive=False)
|
| 107 |
+
plot_out = gr.Image(label="Deflection curve")
|
| 108 |
+
x_sample = gr.Textbox(label="x sample", interactive=False)
|
| 109 |
+
y_sample = gr.Textbox(label="y sample", interactive=False)
|
| 110 |
+
|
| 111 |
+
run_btn.click(
|
| 112 |
+
run,
|
| 113 |
+
inputs=[beam_type, L, load_type, load_value, load_pos, E, I, num_points],
|
| 114 |
+
outputs=[max_defl_out, max_x_out, plot_out, x_sample, y_sample],
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
if __name__ == "__main__":
|
| 118 |
+
demo.launch()
|