jithenderchoudary commited on
Commit
93a1e91
·
verified ·
1 Parent(s): 8218aa6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +151 -1
app.py CHANGED
@@ -5,7 +5,157 @@ import pandas as pd
5
  import pickle
6
 
7
  # Ensure the project root is in the Python path
8
- sys.path.append(os.path.dirname(os.path.abspath(__file__)))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  # Import optimization logic
11
  from models.optimizer import optimize_design
 
5
  import pickle
6
 
7
  # Ensure the project root is in the Python path
8
+ sys.path.append(os.paimport gradio as gr
9
+ import numpy as np
10
+ import pandas as pd
11
+ from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
12
+ from joblib import dump, load
13
+ from ansys.mapdl.core import launch_mapdl
14
+ import matplotlib.pyplot as plt
15
+ import os
16
+
17
+ # ========== Train AI Models ==========
18
+ def train_models():
19
+ # Synthetic training data
20
+ data = {
21
+ "Thickness": [10, 15, 20, 25],
22
+ "Hole_Diameter": [5, 10, 15, 20],
23
+ "Force": [5000, 7000, 10000, 12000],
24
+ "Max_Stress": [300, 250, 200, 150],
25
+ "Max_Deformation": [0.5, 0.4, 0.3, 0.2],
26
+ "Pass_Fail": [1, 1, 0, 0], # 1: Pass, 0: Fail
27
+ }
28
+ df = pd.DataFrame(data)
29
+ X = df[["Thickness", "Hole_Diameter", "Force"]]
30
+
31
+ # Train regression models
32
+ stress_model = RandomForestRegressor().fit(X, df["Max_Stress"])
33
+ deformation_model = RandomForestRegressor().fit(X, df["Max_Deformation"])
34
+
35
+ # Train classification model
36
+ pass_fail_model = RandomForestClassifier().fit(X, df["Pass_Fail"])
37
+
38
+ # Save models
39
+ dump(stress_model, "stress_model.pkl")
40
+ dump(deformation_model, "deformation_model.pkl")
41
+ dump(pass_fail_model, "pass_fail_model.pkl")
42
+
43
+ # Train the models
44
+ train_models()
45
+
46
+ # Load models
47
+ stress_model = load("stress_model.pkl")
48
+ deformation_model = load("deformation_model.pkl")
49
+ pass_fail_model = load("pass_fail_model.pkl")
50
+
51
+ # ========== ANSYS Simulation ==========
52
+ def run_ansys_simulation(thickness, hole_diameter, force):
53
+ mapdl = launch_mapdl()
54
+ mapdl.clear()
55
+ mapdl.prep7()
56
+
57
+ # Material properties
58
+ mapdl.mp("EX", 1, 2e11)
59
+ mapdl.mp("PRXY", 1, 0.3)
60
+
61
+ # Geometry
62
+ mapdl.block(0, 100, 0, 50, 0, thickness)
63
+ mapdl.cylind(0, hole_diameter / 2, 50, 25, 0, thickness)
64
+ mapdl.vsubtract("ALL")
65
+
66
+ # Meshing
67
+ mapdl.et(1, "SOLID185")
68
+ mapdl.esize(5)
69
+ mapdl.vmesh("ALL")
70
+
71
+ # Boundary conditions and force
72
+ mapdl.nsel("S", "LOC", "X", 0)
73
+ mapdl.d("ALL", "ALL")
74
+ mapdl.nsel("S", "LOC", "X", 100)
75
+ mapdl.f("ALL", "FY", -force)
76
+
77
+ # Solve
78
+ mapdl.run("/SOLU")
79
+ mapdl.antype("STATIC")
80
+ mapdl.solve()
81
+ mapdl.finish()
82
+
83
+ # Post-process
84
+ mapdl.post1()
85
+ max_stress = mapdl.get_value("NODE", 0, "S", "EQV")
86
+ max_deformation = mapdl.get_value("NODE", 0, "U", "SUM")
87
+ mapdl.exit()
88
+
89
+ return max_stress, max_deformation
90
+
91
+ # ========== AI and Simulation Workflow ==========
92
+ def ai_and_simulation_workflow(thickness, hole_diameter, force):
93
+ # AI pre-screening
94
+ pre_screen = pass_fail_model.predict([[thickness, hole_diameter, force]])[0]
95
+ if pre_screen == 0:
96
+ return {
97
+ "status": "Fail",
98
+ "message": "AI predicts failure. Please adjust parameters.",
99
+ "stress": None,
100
+ "deformation": None,
101
+ }
102
+
103
+ # Run ANSYS simulation
104
+ max_stress, max_deformation = run_ansys_simulation(thickness, hole_diameter, force)
105
+
106
+ # Validate results with AI
107
+ validation = "Pass" if max_stress < 250 and max_deformation < 0.3 else "Fail"
108
+
109
+ # Generate output
110
+ return {
111
+ "status": validation,
112
+ "message": f"Design {validation}.",
113
+ "stress": max_stress,
114
+ "deformation": max_deformation,
115
+ }
116
+
117
+ # ========== UI ==========
118
+ def visualize_results(results):
119
+ fig, ax = plt.subplots()
120
+ labels = ["Stress", "Deformation"]
121
+ values = [results["stress"], results["deformation"]]
122
+ ax.bar(labels, values, color=["#FFA07A", "#20B2AA"])
123
+ ax.set_title("Simulation Results")
124
+ plt.tight_layout()
125
+ image_path = "results.png"
126
+ plt.savefig(image_path)
127
+ plt.close(fig)
128
+ return image_path
129
+
130
+ def run_ui(thickness, hole_diameter, force):
131
+ results = ai_and_simulation_workflow(thickness, hole_diameter, force)
132
+ if results["status"] == "Fail":
133
+ return results["message"], None
134
+ else:
135
+ image_path = visualize_results(results)
136
+ return results["message"], image_path
137
+
138
+ # Gradio Interface
139
+ interface = gr.Interface(
140
+ fn=run_ui,
141
+ inputs=[
142
+ gr.Slider(10, 50, step=1, label="Thickness (mm)"),
143
+ gr.Slider(5, 25, step=1, label="Hole Diameter (mm)"),
144
+ gr.Slider(1000, 15000, step=500, label="Force (N)"),
145
+ ],
146
+ outputs=[
147
+ gr.Textbox(label="Simulation Status"),
148
+ gr.Image(label="Simulation Visualization"),
149
+ ],
150
+ title="AI-Driven ANSYS Design Validation",
151
+ description="Interactive tool for design validation using AI and ANSYS.",
152
+ theme="default",
153
+ live=True,
154
+ )
155
+
156
+ # Launch the interface
157
+ interface.launch()
158
+ th.dirname(os.path.abspath(__file__)))
159
 
160
  # Import optimization logic
161
  from models.optimizer import optimize_design