Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ from reportlab.lib.pagesizes import letter
|
|
| 6 |
from reportlab.pdfgen import canvas
|
| 7 |
import gradio as gr
|
| 8 |
import os
|
| 9 |
-
from ansys.mapdl.core import launch_mapdl # For ANSYS integration
|
| 10 |
|
| 11 |
|
| 12 |
# Function for Progressive Die Design
|
|
@@ -43,16 +42,43 @@ def visualize_die(length, width, thickness):
|
|
| 43 |
return f"Error visualizing die: {str(e)}"
|
| 44 |
|
| 45 |
|
| 46 |
-
# Function for
|
| 47 |
-
def stress_analysis(force, die_width, die_height, material_strength):
|
| 48 |
try:
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
ax.bar(["Stress", "Material Strength"], [stress, material_strength])
|
| 54 |
-
ax.set_ylabel("Stress (MPa)")
|
| 55 |
-
ax.set_title("Stress Analysis")
|
| 56 |
plt.close(fig)
|
| 57 |
|
| 58 |
return f"Safety Factor: {round(safety_factor, 2)}", fig
|
|
@@ -60,53 +86,6 @@ def stress_analysis(force, die_width, die_height, material_strength):
|
|
| 60 |
return f"Error in stress analysis: {str(e)}", None
|
| 61 |
|
| 62 |
|
| 63 |
-
# ANSYS Integration for Stress Analysis
|
| 64 |
-
def run_ansys_simulation(force, die_width, die_height, material_strength):
|
| 65 |
-
try:
|
| 66 |
-
# Launch ANSYS MAPDL instance
|
| 67 |
-
mapdl = launch_mapdl()
|
| 68 |
-
mapdl.prep7() # Enter pre-processing module
|
| 69 |
-
|
| 70 |
-
# Define geometry and material properties
|
| 71 |
-
mapdl.rectng(0, die_width, 0, die_height)
|
| 72 |
-
mapdl.mp('EX', 1, material_strength)
|
| 73 |
-
mapdl.et(1, 'PLANE183')
|
| 74 |
-
|
| 75 |
-
# Apply loads
|
| 76 |
-
mapdl.nsel('S', 'LOC', 'X', 0)
|
| 77 |
-
mapdl.d('ALL', 'UX', 0)
|
| 78 |
-
mapdl.f('ALL', 'FY', -force)
|
| 79 |
-
|
| 80 |
-
# Solve
|
| 81 |
-
mapdl.run('/SOLU')
|
| 82 |
-
mapdl.solve()
|
| 83 |
-
mapdl.finish()
|
| 84 |
-
|
| 85 |
-
# Post-processing
|
| 86 |
-
mapdl.post1()
|
| 87 |
-
stress = mapdl.get_value('NODE', 1, 'S', 'EQV') # Retrieve max equivalent stress
|
| 88 |
-
mapdl.exit()
|
| 89 |
-
|
| 90 |
-
return f"Max Stress: {stress:.2f} MPa"
|
| 91 |
-
except Exception as e:
|
| 92 |
-
return f"Error running ANSYS simulation: {str(e)}"
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
# SolidWorks Placeholder for Stress Analysis
|
| 96 |
-
def solidworks_stress_analysis(force, die_width, die_height, material_strength):
|
| 97 |
-
# Replace this function with SolidWorks API or external script
|
| 98 |
-
try:
|
| 99 |
-
output_file = "/path/to/solidworks/output.txt" # Replace with actual path
|
| 100 |
-
if os.path.exists(output_file):
|
| 101 |
-
with open(output_file, "r") as file:
|
| 102 |
-
result = file.read()
|
| 103 |
-
return result.strip()
|
| 104 |
-
else:
|
| 105 |
-
return "SolidWorks simulation output not found."
|
| 106 |
-
except Exception as e:
|
| 107 |
-
return f"Error running SolidWorks simulation: {str(e)}"
|
| 108 |
-
|
| 109 |
-
|
| 110 |
# Function to generate PDF report
|
| 111 |
def generate_pdf_report(data, filename="report.pdf"):
|
| 112 |
try:
|
|
@@ -120,43 +99,12 @@ def generate_pdf_report(data, filename="report.pdf"):
|
|
| 120 |
return f"Error generating report: {str(e)}"
|
| 121 |
|
| 122 |
|
| 123 |
-
# Tool Optimization Function
|
| 124 |
-
def optimize_tool(speed, feed_rate, depth_of_cut, material):
|
| 125 |
-
try:
|
| 126 |
-
tool_life = 1000 / (speed * feed_rate * depth_of_cut)
|
| 127 |
-
recommended_speed = 0.8 * speed
|
| 128 |
-
recommended_feed_rate = 0.9 * feed_rate
|
| 129 |
-
|
| 130 |
-
return {
|
| 131 |
-
"Estimated Tool Life (hrs)": round(tool_life, 2),
|
| 132 |
-
"Recommended Speed (m/min)": round(recommended_speed, 2),
|
| 133 |
-
"Recommended Feed Rate (mm/rev)": round(recommended_feed_rate, 2)
|
| 134 |
-
}
|
| 135 |
-
except Exception as e:
|
| 136 |
-
return {"Error": str(e)}
|
| 137 |
-
|
| 138 |
-
|
| 139 |
# Gradio interface functions
|
| 140 |
-
def stress_analysis_interface(force, die_width, die_height, material_strength,
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
pdf_filename = generate_pdf_report(data)
|
| 146 |
-
return safety_factor, fig, pdf_filename
|
| 147 |
-
|
| 148 |
-
elif simulation_tool == "ANSYS":
|
| 149 |
-
# Run ANSYS-based simulation
|
| 150 |
-
result = run_ansys_simulation(force, die_width, die_height, material_strength)
|
| 151 |
-
return result, None, None
|
| 152 |
-
|
| 153 |
-
elif simulation_tool == "SolidWorks":
|
| 154 |
-
# Run SolidWorks-based simulation
|
| 155 |
-
result = solidworks_stress_analysis(force, die_width, die_height, material_strength)
|
| 156 |
-
return result, None, None
|
| 157 |
-
|
| 158 |
-
else:
|
| 159 |
-
return "Invalid simulation tool selected", None, None
|
| 160 |
|
| 161 |
|
| 162 |
# Create Gradio App
|
|
@@ -181,83 +129,24 @@ with gr.Blocks() as app:
|
|
| 181 |
|
| 182 |
with gr.Tab("Stress Analysis"):
|
| 183 |
gr.Markdown("### Select Simulation Tool and Enter Parameters for Stress Analysis")
|
| 184 |
-
simulation_tool = gr.Dropdown(
|
| 185 |
-
choices=["Python", "ANSYS", "SolidWorks"],
|
| 186 |
-
label="Simulation Tool",
|
| 187 |
-
value="Python",
|
| 188 |
-
)
|
| 189 |
force = gr.Number(label="Force (N)", value=10000)
|
| 190 |
die_width = gr.Number(label="Width (m)", value=0.05)
|
| 191 |
die_height = gr.Number(label="Height (m)", value=0.01)
|
| 192 |
material_strength = gr.Number(label="Material Strength (MPa)", value=250)
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
stress_chart = gr.Plot()
|
| 195 |
-
pdf_file = gr.File(label="Download Report
|
| 196 |
stress_button = gr.Button("Analyze Stress")
|
| 197 |
stress_button.click(
|
| 198 |
stress_analysis_interface,
|
| 199 |
-
inputs=[force, die_width, die_height, material_strength,
|
| 200 |
outputs=[safety_factor_output, stress_chart, pdf_file],
|
| 201 |
)
|
| 202 |
-
# Function for Python-based Stress Analysis with a combined graph
|
| 203 |
-
def stress_analysis(force, die_width, die_height, material_strength):
|
| 204 |
-
try:
|
| 205 |
-
# Calculate parameters
|
| 206 |
-
stress = force / (die_width * die_height) # Stress = Force / Area
|
| 207 |
-
safety_factor = material_strength / stress # Safety Factor = Material Strength / Stress
|
| 208 |
-
|
| 209 |
-
# Data for plotting
|
| 210 |
-
x = ["Stress", "Material Strength", "Safety Factor"]
|
| 211 |
-
y_stress = [stress, material_strength, safety_factor]
|
| 212 |
-
|
| 213 |
-
# Create a combined graph
|
| 214 |
-
fig, ax = plt.subplots()
|
| 215 |
-
ax.plot(x, y_stress, marker='o', label="Simulation Parameters")
|
| 216 |
-
ax.axhline(y=1, color='red', linestyle='--', label="Critical Safety Factor (1)")
|
| 217 |
-
ax.set_ylabel("Value")
|
| 218 |
-
ax.set_title("Stress Analysis Parameters")
|
| 219 |
-
ax.legend()
|
| 220 |
-
plt.tight_layout()
|
| 221 |
-
plt.close(fig)
|
| 222 |
-
|
| 223 |
-
return f"Safety Factor: {round(safety_factor, 2)}", fig
|
| 224 |
-
except Exception as e:
|
| 225 |
-
return f"Error in stress analysis: {str(e)}", None
|
| 226 |
-
def stress_analysis_interface(force, die_width, die_height, material_strength, simulation_tool):
|
| 227 |
-
if simulation_tool == "Python":
|
| 228 |
-
# Python-based stress analysis with combined graph
|
| 229 |
-
safety_factor, fig = stress_analysis(force, die_width, die_height, material_strength)
|
| 230 |
-
data = {"stress": force / (die_width * die_height), "safety_factor": safety_factor}
|
| 231 |
-
pdf_filename = generate_pdf_report(data)
|
| 232 |
-
return safety_factor, fig, pdf_filename
|
| 233 |
-
|
| 234 |
-
elif simulation_tool == "ANSYS":
|
| 235 |
-
# Run ANSYS-based simulation
|
| 236 |
-
result = run_ansys_simulation(force, die_width, die_height, material_strength)
|
| 237 |
-
return result, None, None
|
| 238 |
-
|
| 239 |
-
elif simulation_tool == "SolidWorks":
|
| 240 |
-
# Run SolidWorks-based simulation
|
| 241 |
-
result = solidworks_stress_analysis(force, die_width, die_height, material_strength)
|
| 242 |
-
return result, None, None
|
| 243 |
-
|
| 244 |
-
else:
|
| 245 |
-
return "Invalid simulation tool selected", None, None
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
with gr.Tab("Tool Optimization"):
|
| 249 |
-
gr.Markdown("### Enter Machining Parameters for Tool Optimization")
|
| 250 |
-
speed = gr.Number(label="Cutting Speed (m/min)", value=100)
|
| 251 |
-
feed_rate = gr.Number(label="Feed Rate (mm/rev)", value=0.2)
|
| 252 |
-
depth_of_cut = gr.Number(label="Depth of Cut (mm)", value=1.0)
|
| 253 |
-
material = gr.Dropdown(choices=["Steel", "Aluminum", "Titanium"], label="Material", value="Steel")
|
| 254 |
-
optimization_results = gr.JSON(label="Optimization Results")
|
| 255 |
-
optimize_button = gr.Button("Optimize Tool")
|
| 256 |
-
optimize_button.click(
|
| 257 |
-
optimize_tool,
|
| 258 |
-
inputs=[speed, feed_rate, depth_of_cut, material],
|
| 259 |
-
outputs=optimization_results,
|
| 260 |
-
)
|
| 261 |
|
| 262 |
# Launch the app
|
| 263 |
app.launch()
|
|
|
|
| 6 |
from reportlab.pdfgen import canvas
|
| 7 |
import gradio as gr
|
| 8 |
import os
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
# Function for Progressive Die Design
|
|
|
|
| 42 |
return f"Error visualizing die: {str(e)}"
|
| 43 |
|
| 44 |
|
| 45 |
+
# Function for Stress Analysis (including thermal stress and fatigue strength)
|
| 46 |
+
def stress_analysis(force, die_width, die_height, material_strength, temperature_change=50, alpha=1e-5, elastic_modulus=200000, fatigue_strength=150):
|
| 47 |
try:
|
| 48 |
+
# Mechanical stress
|
| 49 |
+
stress = force / (die_width * die_height) # Stress = Force / Area
|
| 50 |
+
safety_factor = material_strength / stress # Safety Factor = Material Strength / Stress
|
| 51 |
+
|
| 52 |
+
# Thermal stress
|
| 53 |
+
thermal_stress = elastic_modulus * alpha * temperature_change
|
| 54 |
+
|
| 55 |
+
# Fatigue strength
|
| 56 |
+
fatigue_stress = fatigue_strength
|
| 57 |
+
|
| 58 |
+
# Generate data for plotting
|
| 59 |
+
x = np.linspace(1, 100, 100) # Operational range (e.g., % load)
|
| 60 |
+
stress_curve = stress * x / 100 # Simulated stress
|
| 61 |
+
material_strength_curve = np.full_like(x, material_strength) # Constant material strength
|
| 62 |
+
safety_factor_curve = material_strength_curve / stress_curve # Varying safety factor
|
| 63 |
+
thermal_stress_curve = np.full_like(x, thermal_stress) # Constant thermal stress
|
| 64 |
+
fatigue_strength_curve = np.full_like(x, fatigue_stress) # Constant fatigue strength
|
| 65 |
+
|
| 66 |
+
# Create combined graph
|
| 67 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 68 |
+
ax.plot(x, stress_curve, label="Stress (σ)", color="blue")
|
| 69 |
+
ax.plot(x, material_strength_curve, label="Material Strength (σ_y)", color="green")
|
| 70 |
+
ax.plot(x, safety_factor_curve, label="Safety Factor (SF)", color="orange")
|
| 71 |
+
ax.plot(x, thermal_stress_curve, label="Thermal Stress (σ_thermal)", color="purple")
|
| 72 |
+
ax.plot(x, fatigue_strength_curve, label="Fatigue Strength (σ_fatigue)", color="brown")
|
| 73 |
+
ax.axhline(1, color="red", linestyle="--", label="Critical Safety Threshold (SF=1)")
|
| 74 |
+
|
| 75 |
+
ax.set_title("Combined Stress Analysis Parameters")
|
| 76 |
+
ax.set_xlabel("Operational Range (%)")
|
| 77 |
+
ax.set_ylabel("Parameter Value (MPa or Unitless)")
|
| 78 |
+
ax.legend()
|
| 79 |
+
ax.grid()
|
| 80 |
|
| 81 |
+
plt.tight_layout()
|
|
|
|
|
|
|
|
|
|
| 82 |
plt.close(fig)
|
| 83 |
|
| 84 |
return f"Safety Factor: {round(safety_factor, 2)}", fig
|
|
|
|
| 86 |
return f"Error in stress analysis: {str(e)}", None
|
| 87 |
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
# Function to generate PDF report
|
| 90 |
def generate_pdf_report(data, filename="report.pdf"):
|
| 91 |
try:
|
|
|
|
| 99 |
return f"Error generating report: {str(e)}"
|
| 100 |
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
# Gradio interface functions
|
| 103 |
+
def stress_analysis_interface(force, die_width, die_height, material_strength, temperature_change, alpha, elastic_modulus, fatigue_strength):
|
| 104 |
+
safety_factor, fig = stress_analysis(force, die_width, die_height, material_strength, temperature_change, alpha, elastic_modulus, fatigue_strength)
|
| 105 |
+
data = {"stress": force / (die_width * die_height), "safety_factor": safety_factor}
|
| 106 |
+
pdf_filename = generate_pdf_report(data)
|
| 107 |
+
return safety_factor, fig, pdf_filename
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
|
| 110 |
# Create Gradio App
|
|
|
|
| 129 |
|
| 130 |
with gr.Tab("Stress Analysis"):
|
| 131 |
gr.Markdown("### Select Simulation Tool and Enter Parameters for Stress Analysis")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
force = gr.Number(label="Force (N)", value=10000)
|
| 133 |
die_width = gr.Number(label="Width (m)", value=0.05)
|
| 134 |
die_height = gr.Number(label="Height (m)", value=0.01)
|
| 135 |
material_strength = gr.Number(label="Material Strength (MPa)", value=250)
|
| 136 |
+
temperature_change = gr.Number(label="Temperature Change (°C)", value=50)
|
| 137 |
+
alpha = gr.Number(label="Thermal Expansion Coefficient (1/°C)", value=1e-5)
|
| 138 |
+
elastic_modulus = gr.Number(label="Elastic Modulus (MPa)", value=200000)
|
| 139 |
+
fatigue_strength = gr.Number(label="Fatigue Strength (MPa)", value=150)
|
| 140 |
+
|
| 141 |
+
safety_factor_output = gr.Textbox(label="Safety Factor")
|
| 142 |
stress_chart = gr.Plot()
|
| 143 |
+
pdf_file = gr.File(label="Download Report")
|
| 144 |
stress_button = gr.Button("Analyze Stress")
|
| 145 |
stress_button.click(
|
| 146 |
stress_analysis_interface,
|
| 147 |
+
inputs=[force, die_width, die_height, material_strength, temperature_change, alpha, elastic_modulus, fatigue_strength],
|
| 148 |
outputs=[safety_factor_output, stress_chart, pdf_file],
|
| 149 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
# Launch the app
|
| 152 |
app.launch()
|