george2cool36 commited on
Commit
7324519
·
verified ·
1 Parent(s): 79f14b2

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +212 -0
app.py ADDED
@@ -0,0 +1,212 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # =========================
2
+ # Column Buckling Calculator — Euler Elastic (Rectangular Section)
3
+ # =========================
4
+
5
+ import math
6
+ import gradio as gr
7
+ import pandas as pd
8
+
9
+ SCOPE_MD = """
10
+ ### Scope & Assumptions
11
+ - **Problem:** Axially compressed **prismatic column** (rectangular cross-section), **Euler elastic buckling**.
12
+ - **Outputs:** Governing critical load \(P_{cr}\), governing axis, slenderness \(λ\), factor of safety vs. applied load \(P\), verdict.
13
+ - **Method:** Euler buckling (linear-elastic, small deflection), **no inelastic (Johnson)**, **no eccentricity**, **no imperfections**.
14
+ - **Section:** Rectangle (width \(b\), height \(h\)); checks both axes and picks the **weaker axis** (smaller \(P_{cr}\)).
15
+ - **End conditions:** Choose \(K\): Fixed–Fixed (0.5), Fixed–Pinned (0.7), Pinned–Pinned (1.0), Fixed–Free (2.0).
16
+ - **Units:** SI (m, N, GPa, MPa). Input \(P\) in kN. Results show \(P_{cr}\) in **kN**.
17
+
18
+ ### Valid Ranges (hard checks)
19
+ - 0.1 < L ≤ 20 m
20
+ - 0 < P ≤ 5*10^6 N
21
+ - 1 ≤ E ≤ 400 GPa
22
+ - 10 ≤ Sy ≤ 3000 MPa (for context only; not used in Euler (P_{cr}\)
23
+ - 0.005 < b ≤ 2 m
24
+ - 0.005 < h ≤ 2 m
25
+ """
26
+
27
+ # ----- Validation -----
28
+ def _validate_inputs(L_m, P_kN, E_GPa, Sy_MPa, b_m, h_m):
29
+ errs = []
30
+ def in_range(name, val, lo, hi):
31
+ if not (lo < val <= hi):
32
+ errs.append(f"{name} must be in ({lo}, {hi}] (got {val}).")
33
+ in_range("Length L [m]", L_m, 0.1, 20.0)
34
+ in_range("Load P [kN]", P_kN, 0.0, 5000.0) # 5e6 N
35
+ in_range("Elastic modulus E [GPa]", E_GPa, 1.0, 400.0)
36
+ in_range("Yield strength Sy [MPa]", Sy_MPa, 10.0, 3000.0)
37
+ in_range("Width b [m]", b_m, 0.005, 2.0)
38
+ in_range("Height h [m]", h_m, 0.005, 2.0)
39
+ if errs:
40
+ raise ValueError("\n".join(errs))
41
+
42
+ # ----- Core Math -----
43
+ def euler_buckling_rect(L_m, P_kN, E_GPa, Sy_MPa, b_m, h_m, K):
44
+ """
45
+ Euler elastic buckling for a rectangular column.
46
+ Checks both principal axes and selects the governing (smaller Pcr).
47
+ """
48
+ _validate_inputs(L_m, P_kN, E_GPa, Sy_MPa, b_m, h_m)
49
+
50
+ # SI conversions
51
+ P_applied_N = float(P_kN) * 1e3
52
+ E_Pa = float(E_GPa) * 1e9
53
+
54
+ # Section properties
55
+ A = b_m * h_m
56
+ Ix = b_m * (h_m**3) / 12.0
57
+ Iy = h_m * (b_m**3) / 12.0
58
+ rx = (Ix / A) ** 0.5
59
+ ry = (Iy / A) ** 0.5
60
+
61
+ KL = K * L_m
62
+ Pcr_x = (math.pi**2) * E_Pa * Ix / (KL**2)
63
+ Pcr_y = (math.pi**2) * E_Pa * Iy / (KL**2)
64
+
65
+ # Governing axis (smaller Pcr)
66
+ if Pcr_x <= Pcr_y:
67
+ axis = "x (buckles about the weak direction of Ix → bending about h)"
68
+ Pcr = Pcr_x
69
+ r_govern = rx
70
+ I_govern = Ix
71
+ else:
72
+ axis = "y (buckles about the weak direction of Iy → bending about b)"
73
+ Pcr = Pcr_y
74
+ r_govern = ry
75
+ I_govern = Iy
76
+
77
+ slenderness = KL / r_govern if r_govern > 0 else math.inf
78
+ fos = Pcr / P_applied_N if P_applied_N > 0 else math.inf
79
+ ok = P_applied_N <= Pcr
80
+
81
+ # Pretty print helpers
82
+ def _fmt(x, d=6):
83
+ try:
84
+ return f"{x:.{d}g}"
85
+ except Exception:
86
+ return str(x)
87
+
88
+ steps_md = "\n".join([
89
+ "## Show the math (Euler elastic buckling)",
90
+ f"L = {_fmt(L_m)} m, K = {_fmt(K)}, KL = {K} * {L_m} = {KL:.6g} m",
91
+ f"E = {_fmt(E_GPa)} GPa, P = {_fmt(P_kN)} kN (= {P_applied_N:.6g} N)",
92
+ f"b = {_fmt(b_m)} m, h = {_fmt(h_m)} m",
93
+ "",
94
+ "Area and moments of inertia:",
95
+ f"A = b * h = {b_m} * {h_m} = {A:.6e} m^2",
96
+ f"Ix = b * h^3 / 12 = {b_m} * {h_m}^3 / 12 = {Ix:.6e} m^4",
97
+ f"Iy = h * b^3 / 12 = {h_m} * {b_m}^3 / 12 = {Iy:.6e} m^4",
98
+ f"rx = sqrt(Ix / A) = sqrt({Ix:.6e} / {A:.6e}) = {rx:.6e} m",
99
+ f"ry = sqrt(Iy / A) = sqrt({Iy:.6e} / {A:.6e}) = {ry:.6e} m",
100
+ "",
101
+ "Euler critical loads:",
102
+ "Pcr = π^2 * E * I / (K*L)^2",
103
+ f"Pcr_x = (π^2) * ({E_GPa} * 10^9) * ({Ix:.6e}) / ({K} * {L_m})^2 = {Pcr_x:.6e} N",
104
+ f"Pcr_y = (π^2) * ({E_GPa} * 10^9) * ({Iy:.6e}) / ({K} * {L_m})^2 = {Pcr_y:.6e} N",
105
+ f"Governing axis: {axis}",
106
+ f"Pcr(governing) = {Pcr:.6e} N = {Pcr/1e3:.3f} kN",
107
+ "",
108
+ "Slenderness (governing axis):",
109
+ f"λ = (K*L) / r_governing = {KL:.6g} / {r_govern:.6e} = {slenderness:.2f}",
110
+ "",
111
+ "Check vs applied load:",
112
+ f"FoS_buckling = Pcr / P = {Pcr:.6e} / {P_applied_N:.6e} = {fos:.3f}",
113
+ f"Verdict: {'OK (no buckling at P)' if ok else 'NOT OK (buckles at P)'}"
114
+ ])
115
+
116
+ results = {
117
+ "A_m2": A,
118
+ "Ix_m4": Ix,
119
+ "Iy_m4": Iy,
120
+ "rx_m": rx,
121
+ "ry_m": ry,
122
+ "Pcr_x_N": Pcr_x,
123
+ "Pcr_y_N": Pcr_y,
124
+ "Pcr_governing_N": Pcr,
125
+ "P_applied_N": P_applied_N,
126
+ "FoS_buckling": fos,
127
+ "governing_axis": axis,
128
+ "slenderness_governing": slenderness,
129
+ "ok": bool(ok),
130
+ }
131
+ verdict = {
132
+ "message": "OK: no Euler buckling at the applied load" if ok else "NOT OK: Euler buckling likely at the applied load",
133
+ "governing_axis": axis
134
+ }
135
+ return results, verdict, steps_md
136
+
137
+ # ----- Gradio glue -----
138
+ END_CONDITIONS = {
139
+ "Fixed–Fixed (K=0.5)": 0.5,
140
+ "Fixed–Pinned (K=0.7)": 0.7,
141
+ "Pinned–Pinned (K=1.0)": 1.0,
142
+ "Fixed–Free / Cantilever (K=2.0)": 2.0,
143
+ }
144
+
145
+ def run_once(L_m, P_kN, E_GPa, Sy_MPa, b_m, h_m, end_condition):
146
+ try:
147
+ K = END_CONDITIONS[end_condition]
148
+ res, ver, steps = euler_buckling_rect(
149
+ float(L_m), float(P_kN), float(E_GPa), float(Sy_MPa),
150
+ float(b_m), float(h_m), float(K)
151
+ )
152
+ df = pd.DataFrame([{
153
+ "Pcr_x [kN]": round(res["Pcr_x_N"]/1e3, 3),
154
+ "Pcr_y [kN]": round(res["Pcr_y_N"]/1e3, 3),
155
+ "Pcr (governing) [kN]": round(res["Pcr_governing_N"]/1e3, 3),
156
+ "Applied P [kN]": round(res["P_applied_N"]/1e3, 3),
157
+ "FoS_buckling [-]": round(res["FoS_buckling"], 3),
158
+ "Slenderness (λ)": round(res["slenderness_governing"], 2),
159
+ "Governing axis": res["governing_axis"],
160
+ "Verdict": ver["message"],
161
+ }])
162
+ explain = (
163
+ f"Column buckles about {res['governing_axis']}: "
164
+ f"Pcr={res['Pcr_governing_N']/1e3:.2f} kN vs P={res['P_applied_N']/1e3:.2f} kN "
165
+ f"(FoS={res['FoS_buckling']:.2f}) → {ver['message']}."
166
+ )
167
+ return df, explain, steps, ""
168
+ except Exception as e:
169
+ return pd.DataFrame(), "", "", f"Input error:\n{e}"
170
+
171
+ with gr.Blocks(title="Column Buckling — Euler Elastic") as demo:
172
+ gr.Markdown("# Column Buckling Calculator — Euler Elastic (Rectangular Section)")
173
+ gr.Markdown(SCOPE_MD)
174
+
175
+ with gr.Row():
176
+ with gr.Column():
177
+ gr.Markdown("### Geometry & Material")
178
+ L_m = gr.Number(value=3.0, label="Length L [m]")
179
+ b_m = gr.Number(value=0.06, label="Width b [m]")
180
+ h_m = gr.Number(value=0.10, label="Height h [m]")
181
+ E_GPa = gr.Number(value=200., label="Elastic modulus E [GPa]")
182
+ Sy_MPa= gr.Number(value=250., label="Yield strength Sy [MPa] (context)")
183
+ with gr.Column():
184
+ gr.Markdown("### Load & End Condition")
185
+ P_kN = gr.Number(value=200.0, label="Applied load P [kN]")
186
+ end_condition = gr.Radio(
187
+ list(END_CONDITIONS.keys()),
188
+ value="Pinned–Pinned (K=1.0)",
189
+ label="End conditions (effective-length factor K)"
190
+ )
191
+
192
+ run_btn = gr.Button("Compute")
193
+
194
+ gr.Markdown("### Results")
195
+ results_df = gr.Dataframe(label="Numerical results", interactive=False)
196
+
197
+ gr.Markdown("### Explain the result")
198
+ explain_md = gr.Markdown()
199
+
200
+ gr.Markdown("### Show the math")
201
+ steps_md = gr.Markdown()
202
+
203
+ err_box = gr.Textbox(label="Errors", interactive=False)
204
+
205
+ run_btn.click(
206
+ fn=run_once,
207
+ inputs=[L_m, P_kN, E_GPa, Sy_MPa, b_m, h_m, end_condition],
208
+ outputs=[results_df, explain_md, steps_md, err_box]
209
+ )
210
+
211
+ if __name__ == "__main__":
212
+ demo.launch(debug=False)