jithenderchoudary commited on
Commit
6561c7a
·
verified ·
1 Parent(s): ceff7a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -72
app.py CHANGED
@@ -1,74 +1,88 @@
1
- import streamlit as st
2
- import joblib
3
- import pandas as pd
4
  import numpy as np
5
  import matplotlib.pyplot as plt
6
- from models.optimizer import optimize_design
7
- # Load pre-trained model
8
- model = joblib.load('models/defect_model.pkl')
9
- # Page Title
10
- 19/112
11
- st.title("AI-Driven Press Tool Defect Predictor & Optimizer")
12
- # Sidebar: User Input Parameters
13
- st.sidebar.header("Input Parameters")
14
- tool_width = st.sidebar.slider("Tool Width (mm)", min_value=30, max_value=100,
15
- value=50, step=5)
16
- tool_height = st.sidebar.slider("Tool Height (mm)", min_value=10, max_value=50,
17
- value=20, step=2)
18
- material_strength = st.sidebar.slider("Material Strength (MPa)", min_value=200,
19
- max_value=500, value=300, step=20)
20
- press_force = st.sidebar.slider("Press Force (kN)", min_value=50, max_value=300,
21
- value=100, step=10)
22
- # Input Data Frame
23
- input_data = pd.DataFrame({
24
- 'tool_width': [tool_width],
25
- 'tool_height': [tool_height],
26
- 'material_strength': [material_strength],
27
- 'press_force': [press_force]
28
- })
29
- # Predict Defect
30
- defect = model.predict(input_data)[0]
31
- probabilities = model.predict_proba(input_data)[0]
32
- # Optimize Design
33
- optimized_params = optimize_design(tool_width, tool_height, material_strength,
34
- press_force)
35
- # Main Section: Results
36
- st.subheader("Defect Prediction and Optimization Results")
37
- # Prediction Result
38
- st.markdown(f"### Predicted Defect: **{defect}**")
39
- # Visualize Prediction Probabilities
40
- st.markdown("### Prediction Probabilities")
41
- fig, ax = plt.subplots()
42
- labels = model.classes_
43
- ax.bar(labels, probabilities, color='skyblue')
44
- ax.set_ylabel("Probability")
45
- ax.set_title("Defect Prediction Probabilities")
46
- 20/112
47
- Enhancements Added
48
- 1. Bar Chart for Defect Probabilities:
49
- Displays the condence level for each defect prediction.
50
- 2. Real-Time Parameter Adjustment:
51
- Adjust parameters like tool width, height, material strength, and press force using
52
- sliders.
53
- Instantly see how predictions and optimizations are aected.
54
- 3. Line Chart for Parameter Impact:
55
- Visualizes the relationship between tool width and defect probabilities.
56
- Similar visualizations can be added for other parameters.
57
- st.pyplot(fig)
58
- # Optimized Parameters
59
- st.markdown("### Optimized Design Parameters")
60
- st.json(optimized_params)
61
- # Dynamic Visualization: Adjusting Parameters
62
- st.markdown("### Parameter Impact on Predictions")
63
- impact_chart_data = []
64
- for w in range(30, 101, 10):
65
- temp_data = pd.DataFrame({
66
- 'tool_width': [w],
67
- 'tool_height': [tool_height],
68
- 'material_strength': [material_strength],
69
- 'press_force': [press_force]
70
- })
71
- prob = model.predict_proba(temp_data)[0]
72
- impact_chart_data.append((w, prob))
73
- impact_chart_df = pd.DataFrame(impact_chart_data, columns=["Tool Width", *labels])
74
- st.line_chart(impact_chart_df.set_index("Tool Width"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import numpy as np
2
  import matplotlib.pyplot as plt
3
+ from ansys.mapdl.core import launch_mapdl
4
+ # Launch ANSYS MAPDL instance
5
+ mapdl = launch_mapdl()
6
+ # Clear any previous session
7
+ mapdl.clear()
8
+ mapdl.prep7() # Enter preprocessor
9
+ # Function to define and simulate the boring machine attachment
10
+ def simulate_attachment(thickness):
11
+ # Define material properties
12
+ mapdl.mp("EX", 1, 2e11) # Elastic modulus (Pa)
13
+ mapdl.mp("PRXY", 1, 0.3) # Poisson's ratio
14
+ # Create geometry
15
+ mapdl.block(0, 200, 0, 100, 0, thickness) # Rectangular block
16
+ mapdl.cylind(0, 10, 50, 50, 0, thickness) # Cylindrical hole
17
+ mapdl.vsubtract("ALL") # Subtract cylinder from block
18
+ # Define element type and mesh
19
+ mapdl.et(1, "SOLID185") # 3D structural element
20
+ mapdl.esize(5) # Element size
21
+ mapdl.vmesh("ALL") # Mesh all volumes
22
+ # Apply boundary conditions
23
+ mapdl.nsel("S", "LOC", "X", 0) # Select nodes on one end
24
+ 38/112
25
+ mapdl.d("ALL", "ALL") # Fix all degrees of freedom
26
+ mapdl.nsel("S", "LOC", "X", 200) # Select nodes on the other end
27
+ mapdl.f("ALL", "FY", -5000) # Apply force
28
+ # Solve
29
+ mapdl.run("/SOLU") # Enter solution phase
30
+ mapdl.antype("STATIC") # Static analysis
31
+ mapdl.solve()
32
+ mapdl.finish() # Finish solution
33
+ # Post-process
34
+ mapdl.post1()
35
+ max_stress = mapdl.get_value("NODE", 0, "S", "EQV") # Equivalent stress
36
+ max_def = mapdl.get_value("NODE", 0, "U", "SUM") # Total deformation
37
+ # Save plots
38
+ mapdl.post_processing.plot_nodal_solution(
39
+ "S", "EQV", title=f"Stress Distribution (Thickness={thickness} mm)",
40
+ savefig=f"stress_{thickness}.png"
41
+ )
42
+ mapdl.post_processing.plot_nodal_solution(
43
+ "U", "SUM", title=f"Deformation (Thickness={thickness} mm)",
44
+ savefig=f"deformation_{thickness}.png"
45
+ )
46
+ return max_stress, max_def
47
+ # Parameter sweep for different thicknesses
48
+ thickness_values = [20, 25, 30]
49
+ results = []
50
+ for thickness in thickness_values:
51
+ max_stress, max_def = simulate_attachment(thickness)
52
+ results.append({"Thickness (mm)": thickness, "Max Stress (Pa)": max_stress, "Max
53
+ Deformation (mm)": max_def})
54
+ # Visualize results
55
+ import pandas as pd
56
+ results_df = pd.DataFrame(results)
57
+ print(results_df)
58
+ 39/112
59
+ Key Features
60
+ 1. Geometry Creation:
61
+ Python creates the geometry of the attachment, including a rectangular block with a
62
+ cylindrical hole.
63
+ 2. Material Properties:
64
+ Elastic modulus and Poisson's ratio are assigned programmatically.
65
+ 3. Meshing:
66
+ Element type SOLID185 and ne mesh control are applied.
67
+ 4. Boundary Conditions and Loading:
68
+ Fixes one side and applies a distributed load on the opposite side.
69
+ 5. Result Extraction:
70
+ Maximum stress and deformation are retrieved for each thickness.
71
+ Results are saved as plots for stress distribution and deformation.
72
+ 6. Parameter Sweep:
73
+ Thickness is varied (20 mm, 25 mm, 30 mm), and simulations are run for each case.
74
+ # Plot results
75
+ plt.figure(figsize=(10, 5))
76
+ plt.plot(results_df["Thickness (mm)"], results_df["Max Stress (Pa)"], marker="o",
77
+ label="Max Stress")
78
+ plt.plot(results_df["Thickness (mm)"], results_df["Max Deformation (mm)"],
79
+ marker="o", label="Max Deformation")
80
+ plt.xlabel("Thickness (mm)")
81
+ plt.ylabel("Value")
82
+ plt.title("Effect of Thickness on Stress and Deformation")
83
+ plt.legend()
84
+ plt.grid()
85
+ plt.savefig("results_plot.png")
86
+ plt.show()
87
+ # Exit MAPDL
88
+ mapdl.exit()