Spaces:
Sleeping
Sleeping
Update ansys_simulation.py
Browse files- ansys_simulation.py +32 -49
ansys_simulation.py
CHANGED
|
@@ -1,55 +1,38 @@
|
|
| 1 |
-
|
| 2 |
-
from ai_models import load_models
|
| 3 |
-
from ansys_simulation import run_ansys_simulation
|
| 4 |
-
from visualization import generate_graph
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
|
| 8 |
-
stress_model, deformation_model, pass_fail_model = load_models()
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
# AI Pre-Screening
|
| 12 |
try:
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
return f"Error with AI model prediction: {str(e)}", None
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
# Generate Visualization
|
| 27 |
-
try:
|
| 28 |
-
graph_path = generate_graph(thickness, hole_diameter, force, max_stress, max_deformation)
|
| 29 |
-
if not os.path.exists(graph_path):
|
| 30 |
-
return f"Error: Graph path '{graph_path}' does not exist.", None
|
| 31 |
except Exception as e:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
return f"Pass: Stress={max_stress} Pa, Deformation={max_deformation} mm", graph_path
|
| 35 |
-
|
| 36 |
-
# Gradio UI
|
| 37 |
-
interface = gr.Interface(
|
| 38 |
-
fn=ai_and_simulation_workflow,
|
| 39 |
-
inputs=[
|
| 40 |
-
gr.Slider(10, 50, step=1, label="Thickness (mm)"),
|
| 41 |
-
gr.Slider(5, 25, step=1, label="Hole Diameter (mm)"),
|
| 42 |
-
gr.Slider(1000, 15000, step=500, label="Force (N)")
|
| 43 |
-
],
|
| 44 |
-
outputs=[
|
| 45 |
-
gr.Textbox(label="Simulation Status"),
|
| 46 |
-
gr.Image(label="Dynamic Results Visualization")
|
| 47 |
-
],
|
| 48 |
-
title="AI-Driven ANSYS Design Validator",
|
| 49 |
-
description="Dynamic tool for optimizing and validating press tool designs.",
|
| 50 |
-
theme="default",
|
| 51 |
-
live=True
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
if __name__ == "__main__":
|
| 55 |
-
interface.launch()
|
|
|
|
| 1 |
+
# ansys_simulation.py
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
import ansys.mapdl.core as pymapdl
|
|
|
|
| 4 |
|
| 5 |
+
def run_ansys_simulation(thickness, hole_diameter, force):
|
|
|
|
| 6 |
try:
|
| 7 |
+
# Launch ANSYS Mapdl instance
|
| 8 |
+
mapdl = pymapdl.launch_mapdl()
|
|
|
|
| 9 |
|
| 10 |
+
# Set the parameters for the simulation
|
| 11 |
+
print(f"Running ANSYS simulation with Thickness: {thickness}, Hole Diameter: {hole_diameter}, Force: {force}")
|
| 12 |
+
|
| 13 |
+
# Define some simple simulation commands for the sake of example
|
| 14 |
+
mapdl.clear() # Clear any previous simulations or settings
|
| 15 |
+
mapdl.prep7() # Switch to preprocessor module
|
| 16 |
+
|
| 17 |
+
# Example of setting up a material, geometry, and boundary conditions
|
| 18 |
+
mapdl.et(1, 183) # Define element type (e.g., solid)
|
| 19 |
+
mapdl.keyopt(1, 3, 3) # Some element option (example)
|
| 20 |
+
|
| 21 |
+
# Setup the geometry (creating a simple solid object based on input parameters)
|
| 22 |
+
mapdl.blk(0, thickness, 0, hole_diameter, 0, force) # Example block based on input
|
| 23 |
+
|
| 24 |
+
# Solve the problem
|
| 25 |
+
mapdl.solve()
|
| 26 |
+
|
| 27 |
+
# Get results
|
| 28 |
+
result = mapdl.result
|
| 29 |
+
max_stress = result.stress().max() # Get maximum stress
|
| 30 |
+
max_deformation = result.deformation().max() # Get maximum deformation
|
| 31 |
+
|
| 32 |
+
print(f"Max Stress: {max_stress} Pa, Max Deformation: {max_deformation} mm")
|
| 33 |
+
|
| 34 |
+
return max_stress, max_deformation
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
+
print(f"Error during ANSYS simulation: {str(e)}")
|
| 38 |
+
raise # Re-raise the error to be handled by the calling function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|