Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,88 +1,46 @@
|
|
| 1 |
-
import
|
| 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 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
+
import pickle
|
| 4 |
+
from models.optimizer import optimize_design
|
| 5 |
+
|
| 6 |
+
# Load pre-trained model
|
| 7 |
+
def load_model():
|
| 8 |
+
with open("models/defect_model.pkl", "rb") as file:
|
| 9 |
+
model = pickle.load(file)
|
| 10 |
+
return model
|
| 11 |
+
|
| 12 |
+
# Predict defects
|
| 13 |
+
def predict_defects(model, data):
|
| 14 |
+
predictions = model.predict(data)
|
| 15 |
+
return predictions
|
| 16 |
+
|
| 17 |
+
st.title("Press Tool AI: Defect Prediction and Optimization")
|
| 18 |
+
|
| 19 |
+
# File upload
|
| 20 |
+
uploaded_file = st.file_uploader("Upload Design Parameters (CSV)", type="csv")
|
| 21 |
+
if uploaded_file:
|
| 22 |
+
data = pd.read_csv(uploaded_file)
|
| 23 |
+
st.write("Uploaded Data:")
|
| 24 |
+
st.dataframe(data)
|
| 25 |
+
|
| 26 |
+
# Load AI model
|
| 27 |
+
model = load_model()
|
| 28 |
+
|
| 29 |
+
# Predict defects
|
| 30 |
+
st.subheader("Defect Predictions:")
|
| 31 |
+
predictions = predict_defects(model, data)
|
| 32 |
+
data['Predicted Defects'] = predictions
|
| 33 |
+
st.dataframe(data)
|
| 34 |
+
|
| 35 |
+
# Optimize design
|
| 36 |
+
st.subheader("Optimized Parameters:")
|
| 37 |
+
optimized_data = optimize_design(data)
|
| 38 |
+
st.dataframe(optimized_data)
|
| 39 |
+
|
| 40 |
+
# Download results
|
| 41 |
+
st.download_button(
|
| 42 |
+
label="Download Results",
|
| 43 |
+
data=optimized_data.to_csv(index=False),
|
| 44 |
+
file_name="optimized_design.csv",
|
| 45 |
+
mime="text/csv",
|
| 46 |
+
)
|