Spaces:
Sleeping
Sleeping
Create stress_analysis.py
Browse files- stress_analysis.py +49 -0
stress_analysis.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
|
| 4 |
+
def stress_analysis(force, die_width, die_height, material_strength, youngs_modulus, poisson_ratio, graph_type):
|
| 5 |
+
try:
|
| 6 |
+
if die_width <= 0 or die_height <= 0:
|
| 7 |
+
return "Width and height must be greater than zero.", None
|
| 8 |
+
|
| 9 |
+
stress = force / (die_width * die_height)
|
| 10 |
+
safety_factor = material_strength / stress
|
| 11 |
+
strain = stress / youngs_modulus
|
| 12 |
+
lateral_strain = poisson_ratio * strain
|
| 13 |
+
|
| 14 |
+
fig = plt.figure(figsize=(8, 6))
|
| 15 |
+
|
| 16 |
+
if graph_type == "3D Stress Visualization":
|
| 17 |
+
ax = fig.add_subplot(111, projection='3d')
|
| 18 |
+
x = np.linspace(0, die_width, 50)
|
| 19 |
+
y = np.linspace(0, die_height, 50)
|
| 20 |
+
X, Y = np.meshgrid(x, y)
|
| 21 |
+
Z = stress * (1 - (X / die_width) ** 2 - (Y / die_height) ** 2)
|
| 22 |
+
|
| 23 |
+
surf = ax.plot_surface(X, Y, Z, cmap='viridis')
|
| 24 |
+
ax.set_xlabel('Width (m)')
|
| 25 |
+
ax.set_ylabel('Height (m)')
|
| 26 |
+
ax.set_zlabel('Stress (MPa)')
|
| 27 |
+
fig.colorbar(surf, shrink=0.5, aspect=5)
|
| 28 |
+
ax.set_title("3D Stress Visualization")
|
| 29 |
+
|
| 30 |
+
else:
|
| 31 |
+
ax = fig.add_subplot(111)
|
| 32 |
+
categories = ["Stress (MPa)", "Material Strength (MPa)", "Strain", "Lateral Strain"]
|
| 33 |
+
values = [stress, material_strength, strain, lateral_strain]
|
| 34 |
+
|
| 35 |
+
if graph_type == "Bar Plot":
|
| 36 |
+
ax.bar(categories, values, color='blue')
|
| 37 |
+
ax.set_title("Stress Analysis - Bar Plot")
|
| 38 |
+
|
| 39 |
+
elif graph_type == "Line Plot":
|
| 40 |
+
ax.plot(categories, values, marker='o', linestyle='-', color='green')
|
| 41 |
+
ax.set_title("Stress Analysis - Line Plot")
|
| 42 |
+
|
| 43 |
+
plt.tight_layout()
|
| 44 |
+
plt.close(fig)
|
| 45 |
+
|
| 46 |
+
return f"Safety Factor: {round(safety_factor, 2)}", fig
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return f"Error in stress analysis: {str(e)}", None
|