Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,9 +7,11 @@ from ansys.mapdl.core import launch_mapdl
|
|
| 7 |
from pycalculix import *
|
| 8 |
|
| 9 |
# Simulation Workflow
|
| 10 |
-
def simulation_workflow(
|
|
|
|
|
|
|
| 11 |
print(f"Running simulation for use_case={use_case}, simulator={simulator}")
|
| 12 |
-
|
| 13 |
# Generate APDL script based on user inputs
|
| 14 |
if use_case == "plate":
|
| 15 |
print("Generating APDL script for plate simulation...")
|
|
@@ -17,16 +19,16 @@ def simulation_workflow(use_case, simulator, **kwargs):
|
|
| 17 |
/PREP7
|
| 18 |
MP,EX,1,2E11 ! Elastic modulus
|
| 19 |
MP,PRXY,1,0.3 ! Poisson's ratio
|
| 20 |
-
BLOCK,0,{
|
| 21 |
-
CYLIND,0,{
|
| 22 |
VSUB,ALL ! Subtract the hole from the block
|
| 23 |
ET,1,SOLID185 ! Define element type as SOLID185
|
| 24 |
ESIZE,5 ! Mesh element size
|
| 25 |
VMESH,ALL ! Mesh the entire volume
|
| 26 |
NSEL,S,LOC,Z,0 ! Select bottom face nodes
|
| 27 |
D,ALL,ALL ! Apply constraints on selected nodes
|
| 28 |
-
NSEL,S,LOC,Z,{
|
| 29 |
-
F,ALL,FY,-{
|
| 30 |
/SOLU
|
| 31 |
ANTYPE,STATIC ! Static structural analysis
|
| 32 |
SOLVE
|
|
@@ -41,14 +43,14 @@ PRNSOL,U,SUM ! Print total deformation results
|
|
| 41 |
/PREP7
|
| 42 |
MP,EX,1,2E11 ! Elastic modulus
|
| 43 |
MP,PRXY,1,0.3 ! Poisson's ratio
|
| 44 |
-
BLOCK,0,{
|
| 45 |
ET,1,SOLID185 ! Define element type as SOLID185
|
| 46 |
ESIZE,5 ! Mesh element size
|
| 47 |
VMESH,ALL ! Mesh the entire volume
|
| 48 |
NSEL,S,LOC,X,0 ! Select nodes at one end of the beam
|
| 49 |
D,ALL,ALL ! Apply fixed constraints on one end
|
| 50 |
-
NSEL,S,LOC,X,{
|
| 51 |
-
F,ALL,FY,-{
|
| 52 |
/SOLU
|
| 53 |
ANTYPE,STATIC ! Static structural analysis
|
| 54 |
SOLVE
|
|
@@ -60,11 +62,11 @@ PRNSOL,U,SUM ! Print total deformation results
|
|
| 60 |
else:
|
| 61 |
print("Invalid use case selected.")
|
| 62 |
return "Invalid use case selected.", None, None
|
| 63 |
-
|
| 64 |
# Save APDL script
|
| 65 |
with open("simulation_input.inp", "w") as file:
|
| 66 |
file.write(apdl_script)
|
| 67 |
-
|
| 68 |
print(f"APDL script saved at: simulation_input.inp")
|
| 69 |
|
| 70 |
# Run the selected simulator
|
|
@@ -73,10 +75,10 @@ PRNSOL,U,SUM ! Print total deformation results
|
|
| 73 |
model = Model("pycalculix_simulation")
|
| 74 |
model.set_units("mm")
|
| 75 |
part = Part("block", model)
|
| 76 |
-
part.make_box(0,
|
| 77 |
if use_case == "plate":
|
| 78 |
-
part.cut_cylinder(0,
|
| 79 |
-
model.make_step_static(
|
| 80 |
model.run()
|
| 81 |
stress = model.results().max_stress()
|
| 82 |
deformation = model.results().max_displacement()
|
|
@@ -102,6 +104,22 @@ PRNSOL,U,SUM ! Print total deformation results
|
|
| 102 |
plt.savefig("results_2d.png")
|
| 103 |
plt.close(fig)
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
# 3D Visualization
|
| 106 |
mesh = pv.Box(bounds=(0, kwargs["length"], 0, kwargs["width"], 0, kwargs["thickness"]))
|
| 107 |
plotter = pv.Plotter(off_screen=True)
|
|
@@ -111,27 +129,24 @@ PRNSOL,U,SUM ! Print total deformation results
|
|
| 111 |
return f"Stress: {stress:.2f} MPa, Deformation: {deformation:.2f} mm", "results_2d.png", "results_3d.png"
|
| 112 |
|
| 113 |
# Define Gradio interface
|
| 114 |
-
|
| 115 |
fn=simulation_workflow,
|
| 116 |
inputs=[
|
| 117 |
-
gr.Radio(["plate", "beam"], label="Select Use Case"),
|
| 118 |
-
gr.Dropdown(["PyCalculix", "ANSYS"], label="Select Simulator"),
|
| 119 |
-
gr.Slider(10, 50, step=1, label="Thickness (mm)"),
|
| 120 |
-
gr.Slider(100, 500, step=10, label="Length (mm)"),
|
| 121 |
-
gr.Slider(50, 200, step=10, label="Width (mm)"),
|
| 122 |
-
gr.Slider(5, 25, step=1, label="Hole Diameter (mm)", value=
|
| 123 |
-
gr.Slider(1000, 10000, step=500, label="Force (N)", value=
|
| 124 |
-
gr.Slider(1000, 20000, step=1000, label="Load (N)", value=
|
| 125 |
],
|
| 126 |
outputs=[
|
| 127 |
-
gr.Textbox(label="Simulation Results"),
|
| 128 |
-
gr.Image(label="2D Results Visualization"),
|
| 129 |
-
gr.Image(label="3D Results Visualization")
|
| 130 |
],
|
| 131 |
title="Unified Simulation Tool (PyCalculix and ANSYS)",
|
| 132 |
live=True
|
| 133 |
-
)
|
| 134 |
|
| 135 |
-
# Launch Gradio interface
|
| 136 |
-
print("Launching Gradio interface...")
|
| 137 |
-
interface.launch()
|
|
|
|
| 7 |
from pycalculix import *
|
| 8 |
|
| 9 |
# Simulation Workflow
|
| 10 |
+
def simulation_workflow(
|
| 11 |
+
use_case, simulator, thickness, length, width, hole_diameter, force, load
|
| 12 |
+
):
|
| 13 |
print(f"Running simulation for use_case={use_case}, simulator={simulator}")
|
| 14 |
+
|
| 15 |
# Generate APDL script based on user inputs
|
| 16 |
if use_case == "plate":
|
| 17 |
print("Generating APDL script for plate simulation...")
|
|
|
|
| 19 |
/PREP7
|
| 20 |
MP,EX,1,2E11 ! Elastic modulus
|
| 21 |
MP,PRXY,1,0.3 ! Poisson's ratio
|
| 22 |
+
BLOCK,0,{length},0,{width},0,{thickness} ! Main block geometry
|
| 23 |
+
CYLIND,0,{hole_diameter / 2},0,0,{thickness} ! Circular hole geometry
|
| 24 |
VSUB,ALL ! Subtract the hole from the block
|
| 25 |
ET,1,SOLID185 ! Define element type as SOLID185
|
| 26 |
ESIZE,5 ! Mesh element size
|
| 27 |
VMESH,ALL ! Mesh the entire volume
|
| 28 |
NSEL,S,LOC,Z,0 ! Select bottom face nodes
|
| 29 |
D,ALL,ALL ! Apply constraints on selected nodes
|
| 30 |
+
NSEL,S,LOC,Z,{thickness} ! Select top face nodes
|
| 31 |
+
F,ALL,FY,-{force} ! Apply vertical force
|
| 32 |
/SOLU
|
| 33 |
ANTYPE,STATIC ! Static structural analysis
|
| 34 |
SOLVE
|
|
|
|
| 43 |
/PREP7
|
| 44 |
MP,EX,1,2E11 ! Elastic modulus
|
| 45 |
MP,PRXY,1,0.3 ! Poisson's ratio
|
| 46 |
+
BLOCK,0,{length},0,{width},0,{thickness} ! Beam geometry
|
| 47 |
ET,1,SOLID185 ! Define element type as SOLID185
|
| 48 |
ESIZE,5 ! Mesh element size
|
| 49 |
VMESH,ALL ! Mesh the entire volume
|
| 50 |
NSEL,S,LOC,X,0 ! Select nodes at one end of the beam
|
| 51 |
D,ALL,ALL ! Apply fixed constraints on one end
|
| 52 |
+
NSEL,S,LOC,X,{length} ! Select nodes at the other end
|
| 53 |
+
F,ALL,FY,-{load} ! Apply uniform load
|
| 54 |
/SOLU
|
| 55 |
ANTYPE,STATIC ! Static structural analysis
|
| 56 |
SOLVE
|
|
|
|
| 62 |
else:
|
| 63 |
print("Invalid use case selected.")
|
| 64 |
return "Invalid use case selected.", None, None
|
| 65 |
+
|
| 66 |
# Save APDL script
|
| 67 |
with open("simulation_input.inp", "w") as file:
|
| 68 |
file.write(apdl_script)
|
| 69 |
+
|
| 70 |
print(f"APDL script saved at: simulation_input.inp")
|
| 71 |
|
| 72 |
# Run the selected simulator
|
|
|
|
| 75 |
model = Model("pycalculix_simulation")
|
| 76 |
model.set_units("mm")
|
| 77 |
part = Part("block", model)
|
| 78 |
+
part.make_box(0, length, 0, width, 0, thickness)
|
| 79 |
if use_case == "plate":
|
| 80 |
+
part.cut_cylinder(0, hole_diameter / 2, 0, hole_diameter / 2, thickness)
|
| 81 |
+
model.make_step_static(force if force else load)
|
| 82 |
model.run()
|
| 83 |
stress = model.results().max_stress()
|
| 84 |
deformation = model.results().max_displacement()
|
|
|
|
| 104 |
plt.savefig("results_2d.png")
|
| 105 |
plt.close(fig)
|
| 106 |
|
| 107 |
+
# 3D Visualization
|
| 108 |
+
mesh = pv.Box(bounds=(0, length, 0, width, 0, thickness))
|
| 109 |
+
plotter = pv.Plotter(off_screen=True)
|
| 110 |
+
plotter.add_mesh(mesh, color="white", show_edges=True)
|
| 111 |
+
plotter.screenshot("results_3d.png")
|
| 112 |
+
|
| 113 |
+
return f"Stress: {stress:.2f} MPa, Deformation: {deformation:.2f} mm", "results_2d.png", "results_3d.png"
|
| 114 |
+
|
| 115 |
+
# Visualize results (both 2D and 3D)
|
| 116 |
+
print("Visualizing results...")
|
| 117 |
+
fig, ax = plt.subplots()
|
| 118 |
+
ax.bar(["Stress", "Deformation"], [stress, deformation], color=["red", "blue"])
|
| 119 |
+
ax.set_title(f"Results ({simulator})")
|
| 120 |
+
plt.savefig("results_2d.png")
|
| 121 |
+
plt.close(fig)
|
| 122 |
+
|
| 123 |
# 3D Visualization
|
| 124 |
mesh = pv.Box(bounds=(0, kwargs["length"], 0, kwargs["width"], 0, kwargs["thickness"]))
|
| 125 |
plotter = pv.Plotter(off_screen=True)
|
|
|
|
| 129 |
return f"Stress: {stress:.2f} MPa, Deformation: {deformation:.2f} mm", "results_2d.png", "results_3d.png"
|
| 130 |
|
| 131 |
# Define Gradio interface
|
| 132 |
+
gr.Interface(
|
| 133 |
fn=simulation_workflow,
|
| 134 |
inputs=[
|
| 135 |
+
gr.Radio(["plate", "beam"], label="Select Use Case"),
|
| 136 |
+
gr.Dropdown(["PyCalculix", "ANSYS"], label="Select Simulator"),
|
| 137 |
+
gr.Slider(10, 50, step=1, label="Thickness (mm)"),
|
| 138 |
+
gr.Slider(100, 500, step=10, label="Length (mm)"),
|
| 139 |
+
gr.Slider(50, 200, step=10, label="Width (mm)"),
|
| 140 |
+
gr.Slider(5, 25, step=1, label="Hole Diameter (mm)", value=5), # Default for Plate
|
| 141 |
+
gr.Slider(1000, 10000, step=500, label="Force (N)", value=5000), # Default for Plate
|
| 142 |
+
gr.Slider(1000, 20000, step=1000, label="Load (N)", value=5000) # Default for Beam
|
| 143 |
],
|
| 144 |
outputs=[
|
| 145 |
+
gr.Textbox(label="Simulation Results"),
|
| 146 |
+
gr.Image(label="2D Results Visualization"),
|
| 147 |
+
gr.Image(label="3D Results Visualization")
|
| 148 |
],
|
| 149 |
title="Unified Simulation Tool (PyCalculix and ANSYS)",
|
| 150 |
live=True
|
| 151 |
+
).launch()
|
| 152 |
|
|
|
|
|
|
|
|
|