Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,177 +1,116 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
from pycalculix import *
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# Handle optional parameters with default values
|
| 14 |
-
hole_diameter = hole_diameter if hole_diameter is not None else 0
|
| 15 |
-
force = force if force is not None else 0
|
| 16 |
-
load = load if load is not None else 0
|
| 17 |
|
| 18 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
if use_case == "plate":
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
/PREP7
|
| 23 |
-
MP,EX,1,2E11 ! Elastic modulus
|
| 24 |
-
MP,PRXY,1,0.3 ! Poisson's ratio
|
| 25 |
-
BLOCK,0,{length},0,{width},0,{thickness} ! Main block geometry
|
| 26 |
-
CYLIND,0,{hole_diameter / 2},0,0,{thickness} ! Circular hole geometry
|
| 27 |
-
VSUB,ALL ! Subtract the hole from the block
|
| 28 |
-
ET,1,SOLID185 ! Define element type as SOLID185
|
| 29 |
-
ESIZE,5 ! Mesh element size
|
| 30 |
-
VMESH,ALL ! Mesh the entire volume
|
| 31 |
-
NSEL,S,LOC,Z,0 ! Select bottom face nodes
|
| 32 |
-
D,ALL,ALL ! Apply constraints on selected nodes
|
| 33 |
-
NSEL,S,LOC,Z,{thickness} ! Select top face nodes
|
| 34 |
-
F,ALL,FY,-{force} ! Apply vertical force
|
| 35 |
-
/SOLU
|
| 36 |
-
ANTYPE,STATIC ! Static structural analysis
|
| 37 |
-
SOLVE
|
| 38 |
-
/POST1
|
| 39 |
-
PRNSOL,S,EQV ! Print equivalent stress results
|
| 40 |
-
PRNSOL,U,SUM ! Print total deformation results
|
| 41 |
-
/EXIT
|
| 42 |
-
"""
|
| 43 |
elif use_case == "beam":
|
| 44 |
-
|
| 45 |
-
apdl_script = f"""
|
| 46 |
-
/PREP7
|
| 47 |
-
MP,EX,1,2E11 ! Elastic modulus
|
| 48 |
-
MP,PRXY,1,0.3 ! Poisson's ratio
|
| 49 |
-
BLOCK,0,{length},0,{width},0,{thickness} ! Beam geometry
|
| 50 |
-
ET,1,SOLID185 ! Define element type as SOLID185
|
| 51 |
-
ESIZE,5 ! Mesh element size
|
| 52 |
-
VMESH,ALL ! Mesh the entire volume
|
| 53 |
-
NSEL,S,LOC,X,0 ! Select nodes at one end of the beam
|
| 54 |
-
D,ALL,ALL ! Apply fixed constraints on one end
|
| 55 |
-
NSEL,S,LOC,X,{length} ! Select nodes at the other end
|
| 56 |
-
F,ALL,FY,-{load} ! Apply uniform load
|
| 57 |
-
/SOLU
|
| 58 |
-
ANTYPE,STATIC ! Static structural analysis
|
| 59 |
-
SOLVE
|
| 60 |
-
/POST1
|
| 61 |
-
PRNSOL,S,EQV ! Print equivalent stress results
|
| 62 |
-
PRNSOL,U,SUM ! Print total deformation results
|
| 63 |
-
/EXIT
|
| 64 |
-
"""
|
| 65 |
else:
|
| 66 |
-
|
| 67 |
-
return "Invalid use case selected.", None, None
|
| 68 |
-
|
| 69 |
-
# Save APDL script
|
| 70 |
-
with open("simulation_input.inp", "w") as file:
|
| 71 |
-
file.write(apdl_script)
|
| 72 |
-
|
| 73 |
-
print(f"APDL script saved at: simulation_input.inp")
|
| 74 |
-
from time import sleep # Add this at the top to handle sleep
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
sleep(5) # Wait for 5 seconds to ensure results are calculated
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
|
| 83 |
-
max_stress = mapdl.get_value("NODE", 0, "S", "EQV")
|
| 84 |
-
deformation = mapdl.get_value("NODE", 0, "U", "SUM")
|
| 85 |
-
print("Running ANSYS simulation...")
|
| 86 |
-
mapdl = launch_mapdl()
|
| 87 |
-
mapdl.input("simulation_input.inp")
|
| 88 |
-
sleep(5) # Wait for 5 seconds to ensure results are calculated
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
print(f"Error reading ANSYS results: {e}")
|
| 96 |
-
max_stress = deformation = 0 # Default values in case of error
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
#
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
model = Model("pycalculix_simulation")
|
| 106 |
-
model.set_units("mm")
|
| 107 |
-
part = Part("block", model)
|
| 108 |
-
part.make_box(0, length, 0, width, 0, thickness)
|
| 109 |
-
if use_case == "plate":
|
| 110 |
-
part.cut_cylinder(0, hole_diameter / 2, 0, hole_diameter / 2, thickness)
|
| 111 |
-
model.make_step_static(force if force else load)
|
| 112 |
|
| 113 |
-
#
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
except Exception as e:
|
| 119 |
-
print(f"Error reading PyCalculix results: {e}")
|
| 120 |
-
stress = deformation = 0 # Default values in case of error
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
max_stress = mapdl.get_value("NODE", 0, "S", "EQV")
|
| 128 |
-
deformation = mapdl.get_value("NODE", 0, "U", "SUM")
|
| 129 |
-
mapdl.exit()
|
| 130 |
-
stress, deformation = max_stress, deformation
|
| 131 |
-
else:
|
| 132 |
-
print("Invalid simulator selected.")
|
| 133 |
-
return "Invalid simulator selected.", None, None
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
plt.close(fig)
|
| 144 |
|
| 145 |
-
#
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
| 150 |
|
| 151 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
-
# Define Gradio interface
|
| 154 |
interface = gr.Interface(
|
| 155 |
fn=simulation_workflow,
|
| 156 |
inputs=[
|
| 157 |
-
gr.Radio(["
|
| 158 |
-
gr.
|
| 159 |
-
gr.
|
| 160 |
-
gr.
|
| 161 |
-
gr.
|
| 162 |
-
gr.Slider(
|
| 163 |
-
gr.Slider(
|
| 164 |
-
gr.Slider(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
],
|
| 166 |
outputs=[
|
| 167 |
-
gr.Textbox(label="Simulation Results"),
|
| 168 |
-
gr.Image(label="2D
|
| 169 |
-
gr.Image(label="
|
|
|
|
|
|
|
|
|
|
| 170 |
],
|
| 171 |
-
title="
|
| 172 |
live=True
|
| 173 |
)
|
| 174 |
|
| 175 |
# Launch Gradio interface
|
| 176 |
-
print("Launching Gradio interface...")
|
| 177 |
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from apdl_generator.apdl_plate import generate_plate_apdl
|
| 3 |
+
from apdl_generator.apdl_beam import generate_beam_apdl
|
| 4 |
+
from simulators.python_simulation import run_python_simulation
|
| 5 |
+
from visualization import visualize_results, visualize_end_product
|
| 6 |
+
import os
|
|
|
|
| 7 |
|
| 8 |
+
def simulation_workflow(tool_type, use_case, include_hole, include_force, include_load, thickness, length, width, hole_diameter, force, load, elastic_modulus):
|
| 9 |
+
"""
|
| 10 |
+
Main simulation workflow that generates multiple formats for APDL, Python code, and simulation results.
|
| 11 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Handle optional parameters
|
| 14 |
+
force = force if include_force else 0
|
| 15 |
+
load = load if include_load else 0
|
| 16 |
+
|
| 17 |
+
# Generate APDL script dynamically
|
| 18 |
if use_case == "plate":
|
| 19 |
+
hole_diameter = hole_diameter if include_hole else 0
|
| 20 |
+
apdl_path = generate_plate_apdl(thickness, length, width, hole_diameter, force)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
elif use_case == "beam":
|
| 22 |
+
apdl_path = generate_beam_apdl(length, width, thickness, load)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
else:
|
| 24 |
+
return "Invalid use case selected.", None, None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Read the generated APDL program
|
| 27 |
+
with open(apdl_path, "r") as file:
|
| 28 |
+
apdl_program = file.read()
|
|
|
|
| 29 |
|
| 30 |
+
# Run simulation using Python-based solver
|
| 31 |
+
stress, deformation = run_python_simulation(apdl_path, use_case, thickness, length, width, force, load, elastic_modulus)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# Explanation for negligible deformation
|
| 34 |
+
if deformation < 1e-6:
|
| 35 |
+
deformation_note = "\nNote: Deformation is negligible due to the high rigidity of the material."
|
| 36 |
+
else:
|
| 37 |
+
deformation_note = ""
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Generate 2D simulation visualization
|
| 40 |
+
graph_path, _ = visualize_results("Python-Based Solver", length, width, thickness, stress, deformation)
|
| 41 |
+
|
| 42 |
+
# Generate end-product visualization
|
| 43 |
+
product_path = visualize_end_product(use_case, length, width, thickness, deformation)
|
| 44 |
|
| 45 |
+
# Save Python code for simulation
|
| 46 |
+
python_code = f"""
|
| 47 |
+
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# Define material properties
|
| 50 |
+
elastic_modulus = {elastic_modulus}
|
| 51 |
+
thickness = {thickness}
|
| 52 |
+
length = {length}
|
| 53 |
+
width = {width}
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
# Simulate stress and deformation (simplified example)
|
| 56 |
+
def simulate():
|
| 57 |
+
stress = elastic_modulus * thickness * width / length # Example formula
|
| 58 |
+
deformation = stress / elastic_modulus # Simplified deformation
|
| 59 |
+
return stress, deformation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
# Run the simulation
|
| 62 |
+
stress, deformation = simulate()
|
| 63 |
+
print(f"Stress: {stress:.2f} MPa")
|
| 64 |
+
print(f"Deformation: {deformation:.6f} mm")
|
| 65 |
+
"""
|
| 66 |
+
python_file_path = "simulation_code.py"
|
| 67 |
+
with open(python_file_path, "w") as f:
|
| 68 |
+
f.write(python_code)
|
|
|
|
| 69 |
|
| 70 |
+
# Generate results in different formats
|
| 71 |
+
results_text = f"{tool_type} Simulation\nStress: {stress:.2f} MPa\nDeformation: {deformation:.6f} mm{deformation_note}"
|
| 72 |
+
|
| 73 |
+
# Save results to a .txt file
|
| 74 |
+
results_file_path = "simulation_results.txt"
|
| 75 |
+
with open(results_file_path, "w") as f:
|
| 76 |
+
f.write(results_text)
|
| 77 |
|
| 78 |
+
return (
|
| 79 |
+
results_text,
|
| 80 |
+
graph_path,
|
| 81 |
+
product_path,
|
| 82 |
+
apdl_program,
|
| 83 |
+
python_file_path, # Provide the path for Python code download
|
| 84 |
+
results_file_path # Provide the path for results file download
|
| 85 |
+
)
|
| 86 |
|
|
|
|
| 87 |
interface = gr.Interface(
|
| 88 |
fn=simulation_workflow,
|
| 89 |
inputs=[
|
| 90 |
+
gr.Radio(["Punch", "Die"], label="Select Tool Type"),
|
| 91 |
+
gr.Radio(["plate", "beam"], label="Select Use Case"),
|
| 92 |
+
gr.Checkbox(label="Include Hole for Plate Simulation"), # Checkbox for optional hole
|
| 93 |
+
gr.Checkbox(label="Include Force"), # Checkbox for optional force
|
| 94 |
+
gr.Checkbox(label="Include Load"), # Checkbox for optional load
|
| 95 |
+
gr.Slider(10, 50, step=1, label="Thickness (mm)"),
|
| 96 |
+
gr.Slider(100, 500, step=10, label="Length (mm)"),
|
| 97 |
+
gr.Slider(50, 200, step=10, label="Width (mm)"),
|
| 98 |
+
gr.Slider(5, 25, step=1, label="Hole Diameter (mm)"), # Controlled by "Include Hole"
|
| 99 |
+
gr.Slider(1000, 10000, step=500, label="Force (N)"), # Controlled by "Include Force"
|
| 100 |
+
gr.Slider(1000, 20000, step=1000, label="Load (N)"), # Controlled by "Include Load"
|
| 101 |
+
gr.Slider(5e10, 3e11, step=1e10, label="Elastic Modulus (Pa)") # Slider for Elastic Modulus
|
| 102 |
],
|
| 103 |
outputs=[
|
| 104 |
+
gr.Textbox(label="Simulation Results"),
|
| 105 |
+
gr.Image(label="2D Simulation Visualization"),
|
| 106 |
+
gr.Image(label="End Product Visualization"),
|
| 107 |
+
gr.Code(language="python", label="Generated APDL Program"), # Python Code Output
|
| 108 |
+
gr.File(label="Download Python Simulation Code"), # Allow file download of Python code
|
| 109 |
+
gr.File(label="Download Simulation Results") # Allow file download of results
|
| 110 |
],
|
| 111 |
+
title="Punch and Die Simulation Tool with End Product Visualization and APDL",
|
| 112 |
live=True
|
| 113 |
)
|
| 114 |
|
| 115 |
# Launch Gradio interface
|
|
|
|
| 116 |
interface.launch()
|