Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
'
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|