saad-sust commited on
Commit
64766a4
Β·
verified Β·
1 Parent(s): 37fb0e2

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -1
app.py CHANGED
@@ -1207,7 +1207,132 @@ def run_sympy(problem: str) -> dict:
1207
  except Exception:
1208
  pass # safe fallback to AI
1209
 
1210
- # ── 10. Matrix / Eigenvalues β€” delegate to AI ────────────────────
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1211
  elif any(k in p for k in ["matrix", "determinant", "eigenvalue",
1212
  "eigenvector", "det("]):
1213
  return {"type": "Matrix", "result": "matrix_detected", "latex": ""}
@@ -1353,6 +1478,18 @@ def ask_ai(problem: str, sympy_info: dict, history: list) -> str:
1353
  "D. NEVER recompute sin(pi), cos(pi) etc β€” sin(pi)=0 exactly, always.\n"
1354
  "E. For Lagrange/Newton interpolation: the polynomial is already given above β€” DO NOT re-expand or re-derive it. Just show the basis polynomials and state the final polynomial from the verified result.\n"
1355
  "F. Your final answer must EXACTLY match the verified result β€” no exceptions.\n\n"
 
 
 
 
 
 
 
 
 
 
 
 
1356
  "=== DIFFERENTIAL GEOMETRY RULES (follow exactly) ===\n"
1357
  "For ANY Differential Geometry question:\n"
1358
  "A. ALWAYS state the definition or theorem FIRST before computing.\n"
 
1207
  except Exception:
1208
  pass # safe fallback to AI
1209
 
1210
+ # ── 10. Hydro Mechanics β€” SymPy for computations, AI for theory ──
1211
+ elif any(k in p for k in [
1212
+ "continuity equation", "equation of continuity",
1213
+ "streamline", "stream function", "stream line",
1214
+ "velocity potential", "irrotational", "rotational motion",
1215
+ "lagrangian", "eulerian", "vortex", "vorticity",
1216
+ "path line", "streak line",
1217
+ "bernoulli", "euler's equation", "euler equation of motion",
1218
+ "torricelli", "flow rate", "discharge",
1219
+ "reynolds number", "reynolds",
1220
+ "hydrostatic pressure", "pressure at depth",
1221
+ "hydrostatic", "buoyancy", "archimedes",
1222
+ "laminar flow", "turbulent flow", "viscous flow",
1223
+ "incompressible fluid", "steady flow",
1224
+ "navier-stokes", "navier stokes",
1225
+ "stokes stream function", "complex velocity potential",
1226
+ "source", "sink", "doublet",
1227
+ "milne thomson", "blasius theorem",
1228
+ "dimensional analysis", "buckingham pi",
1229
+ ]):
1230
+ try:
1231
+ x_h, y_h = sp.symbols('x y')
1232
+ g_val = sp.Rational(981, 100) # 9.81
1233
+
1234
+ # ── Continuity: find v2 from A1v1=A2v2 ───────────────
1235
+ if any(k in p for k in ["continuity equation","equation of continuity"]):
1236
+ nums = [float(n) for n in re.findall(r"[-]?\d+\.?\d*", p)]
1237
+ if len(nums) >= 3:
1238
+ A1_v,v1_v,A2_v = nums[0],nums[1],nums[2]
1239
+ v2_v = round(A1_v*v1_v/A2_v, 6)
1240
+ Q_v = round(A1_v*v1_v, 6)
1241
+ return {
1242
+ "type": "ContinuityEq",
1243
+ "result": (f"A1={A1_v}, v1={v1_v}, A2={A2_v}\n"
1244
+ f"v2 = A1*v1/A2 = {v2_v} m/s\n"
1245
+ f"Flow rate Q = A1*v1 = {Q_v} mΒ³/s"),
1246
+ "latex": f"v_2 = {v2_v}\\text{{ m/s}}"
1247
+ }
1248
+
1249
+ # ── Reynolds Number ────────────────────────────────────
1250
+ elif any(k in p for k in ["reynolds number","reynolds"]):
1251
+ nums = [float(n) for n in re.findall(r"[-]?\d+\.?\d*", p)]
1252
+ if len(nums) >= 4:
1253
+ rho_v,v_v,D_v,mu_v = nums[0],nums[1],nums[2],nums[3]
1254
+ Re = round(rho_v*v_v*D_v/mu_v, 2)
1255
+ flow = "Turbulent (Re>4000)" if Re>4000 else ("Transitional (2300<Re<4000)" if Re>2300 else "Laminar (Re<2300)")
1256
+ return {
1257
+ "type": "ReynoldsNumber",
1258
+ "result": f"Re = ρvD/ΞΌ = {rho_v}Γ—{v_v}Γ—{D_v}/{mu_v} = {Re} β†’ {flow}",
1259
+ "latex": f"Re = {Re}"
1260
+ }
1261
+
1262
+ # ── Bernoulli: find P2 ─────────────────────────────────
1263
+ elif "bernoulli" in p:
1264
+ nums = [float(n) for n in re.findall(r"[-]?\d+\.?\d*", p)]
1265
+ if len(nums) >= 5:
1266
+ P1_v,v1_v,h1_v,v2_v,h2_v = nums[0],nums[1],nums[2],nums[3],nums[4]
1267
+ rho_v = 1000 # default water
1268
+ P2_v = round(P1_v + 0.5*rho_v*(v1_v**2-v2_v**2) + rho_v*9.81*(h1_v-h2_v), 4)
1269
+ return {
1270
+ "type": "Bernoulli",
1271
+ "result": (f"P1+½ρv1²+ρgh1 = P2+½ρv2²+ρgh2\n"
1272
+ f"P2 = {P2_v} Pa"),
1273
+ "latex": f"P_2 = {P2_v}\\text{{ Pa}}"
1274
+ }
1275
+
1276
+ # ── Torricelli: v = √(2gh) ─────────────────────────────
1277
+ elif "torricelli" in p:
1278
+ nums = [float(n) for n in re.findall(r"[-]?\d+\.?\d*", p)]
1279
+ if nums:
1280
+ h_v = nums[0]
1281
+ v_torr = round((2*9.81*h_v)**0.5, 6)
1282
+ return {
1283
+ "type": "Torricelli",
1284
+ "result": f"v = √(2gh) = √(2Γ—9.81Γ—{h_v}) = {v_torr} m/s",
1285
+ "latex": f"v = {v_torr}\\text{{ m/s}}"
1286
+ }
1287
+
1288
+ # ── Hydrostatic Pressure ───────────────────────────────
1289
+ elif any(k in p for k in ["hydrostatic pressure","pressure at depth"]):
1290
+ nums = [float(n) for n in re.findall(r"[-]?\d+\.?\d*", p)]
1291
+ if nums:
1292
+ h_v = nums[0]
1293
+ rho_v = 1000
1294
+ P_gauge = round(rho_v*9.81*h_v, 4)
1295
+ P_abs = round(101325 + P_gauge, 4)
1296
+ return {
1297
+ "type": "HydrostaticPressure",
1298
+ "result": (f"At depth h={h_v}m:\n"
1299
+ f"Gauge pressure = ρgh = {P_gauge} Pa\n"
1300
+ f"Absolute pressure = P0+ρgh = {P_abs} Pa"),
1301
+ "latex": f"P = P_0 + \\rho g h = {P_abs}\\text{{ Pa}}"
1302
+ }
1303
+
1304
+ # ── Flow Rate ──────────────────────────────────────────
1305
+ elif any(k in p for k in ["flow rate","discharge"]):
1306
+ nums = [float(n) for n in re.findall(r"[-]?\d+\.?\d*", p)]
1307
+ if len(nums) >= 2:
1308
+ A_v, v_v = nums[0], nums[1]
1309
+ Q_v = round(A_v*v_v, 8)
1310
+ return {
1311
+ "type": "FlowRate",
1312
+ "result": f"Q = AΓ—v = {A_v}Γ—{v_v} = {Q_v} mΒ³/s",
1313
+ "latex": f"Q = {Q_v}\\text{{ mΒ³/s}}"
1314
+ }
1315
+
1316
+ # ── Velocity Potential ─────────────────────────────────
1317
+ elif "velocity potential" in p:
1318
+ m = re.search(r"(?:phi|Ο†|potential)\s*=\s*(.+?)(?:\s|$)", p)
1319
+ if m:
1320
+ raw = clean(m.group(1))
1321
+ phi_expr = parse_expr(raw, transformations=tfms, local_dict={**ld,"x":x_h,"y":y_h})
1322
+ u_comp = sp.diff(phi_expr, x_h)
1323
+ v_comp = sp.diff(phi_expr, y_h)
1324
+ lap = sp.diff(phi_expr,x_h,2) + sp.diff(phi_expr,y_h,2)
1325
+ return {
1326
+ "type": "VelocityPotential",
1327
+ "result": (f"Ο†={str(phi_expr)}, u=βˆ‚Ο†/βˆ‚x={u_comp}, v=βˆ‚Ο†/βˆ‚y={v_comp}\n"
1328
+ f"βˆ‡Β²Ο†={sp.simplify(lap)} (irrotational: {sp.simplify(lap)==0})"),
1329
+ "latex": f"\\nabla^2\\phi = {sp.latex(sp.simplify(lap))}"
1330
+ }
1331
+
1332
+ except Exception:
1333
+ pass # safe fallback to AI
1334
+
1335
+ # ── 11. Matrix / Eigenvalues β€” delegate to AI ────────────────────
1336
  elif any(k in p for k in ["matrix", "determinant", "eigenvalue",
1337
  "eigenvector", "det("]):
1338
  return {"type": "Matrix", "result": "matrix_detected", "latex": ""}
 
1478
  "D. NEVER recompute sin(pi), cos(pi) etc β€” sin(pi)=0 exactly, always.\n"
1479
  "E. For Lagrange/Newton interpolation: the polynomial is already given above β€” DO NOT re-expand or re-derive it. Just show the basis polynomials and state the final polynomial from the verified result.\n"
1480
  "F. Your final answer must EXACTLY match the verified result β€” no exceptions.\n\n"
1481
+ "=== HYDRO MECHANICS RULES (follow exactly) ===\n"
1482
+ "For ANY Hydro Mechanics/Fluid Mechanics question:\n"
1483
+ "A. Always state whether fluid is ideal/viscous, compressible/incompressible first.\n"
1484
+ "B. Continuity equation: A1v1 = A2v2 (incompressible), βˆ‚Ο/βˆ‚t + βˆ‡Β·(ρv) = 0 (general).\n"
1485
+ "C. Bernoulli: P + ½ρv² + ρgh = constant (along streamline, ideal fluid).\n"
1486
+ "D. Reynolds: Re=ρvD/ΞΌ β€” Re<2300 laminar, Re>4000 turbulent.\n"
1487
+ "E. Euler equations: ρ(Dv/Dt) = -βˆ‡P + ρg (inviscid flow).\n"
1488
+ "F. Navier-Stokes: ρ(Dv/Dt) = -βˆ‡P + ΞΌβˆ‡Β²v + ρg (viscous flow).\n"
1489
+ "G. For proofs: state assumptions β†’ derive step by step β†’ state limitations.\n"
1490
+ "H. Always give physical interpretation of result.\n"
1491
+ "I. Torricelli theorem: v=√(2gh) β€” derived from Bernoulli.\n"
1492
+ "J. Stream function ψ: u=βˆ‚Οˆ/βˆ‚y, v=-βˆ‚Οˆ/βˆ‚x. Velocity potential Ο†: u=βˆ‚Ο†/βˆ‚x, v=βˆ‚Ο†/βˆ‚y.\n\n"
1493
  "=== DIFFERENTIAL GEOMETRY RULES (follow exactly) ===\n"
1494
  "For ANY Differential Geometry question:\n"
1495
  "A. ALWAYS state the definition or theorem FIRST before computing.\n"