Ansys / app.py
jithenderchoudary's picture
Update app.py
f4bd790 verified
raw
history blame
4.49 kB
import gradio as gr
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
from joblib import dump, load
from ansys.mapdl.core import launch_mapdl
import matplotlib.pyplot as plt
import os
python -c "import ansys.mapdl.core"
# ========== Train AI Models ==========
def train_models():
# Synthetic training data
data = {
"Thickness": [10, 15, 20, 25],
"Hole_Diameter": [5, 10, 15, 20],
"Force": [5000, 7000, 10000, 12000],
"Max_Stress": [300, 250, 200, 150],
"Max_Deformation": [0.5, 0.4, 0.3, 0.2],
"Pass_Fail": [1, 1, 0, 0], # 1: Pass, 0: Fail
}
df = pd.DataFrame(data)
X = df[["Thickness", "Hole_Diameter", "Force"]]
# Train regression models
stress_model = RandomForestRegressor().fit(X, df["Max_Stress"])
deformation_model = RandomForestRegressor().fit(X, df["Max_Deformation"])
# Train classification model
pass_fail_model = RandomForestClassifier().fit(X, df["Pass_Fail"])
# Save models
dump(stress_model, "stress_model.pkl")
dump(deformation_model, "deformation_model.pkl")
dump(pass_fail_model, "pass_fail_model.pkl")
# Train the models
train_models()
# Load models
stress_model = load("stress_model.pkl")
deformation_model = load("deformation_model.pkl")
pass_fail_model = load("pass_fail_model.pkl")
# ========== ANSYS Simulation ==========
def run_ansys_simulation(thickness, hole_diameter, force):
mapdl = launch_mapdl()
mapdl.clear()
mapdl.prep7()
# Material properties
mapdl.mp("EX", 1, 2e11)
mapdl.mp("PRXY", 1, 0.3)
# Geometry
mapdl.block(0, 100, 0, 50, 0, thickness)
mapdl.cylind(0, hole_diameter / 2, 50, 25, 0, thickness)
mapdl.vsubtract("ALL")
# Meshing
mapdl.et(1, "SOLID185")
mapdl.esize(5)
mapdl.vmesh("ALL")
# Boundary conditions and force
mapdl.nsel("S", "LOC", "X", 0)
mapdl.d("ALL", "ALL")
mapdl.nsel("S", "LOC", "X", 100)
mapdl.f("ALL", "FY", -force)
# Solve
mapdl.run("/SOLU")
mapdl.antype("STATIC")
mapdl.solve()
mapdl.finish()
# Post-process
mapdl.post1()
max_stress = mapdl.get_value("NODE", 0, "S", "EQV")
max_deformation = mapdl.get_value("NODE", 0, "U", "SUM")
mapdl.exit()
return max_stress, max_deformation
# ========== AI and Simulation Workflow ==========
def ai_and_simulation_workflow(thickness, hole_diameter, force):
# AI pre-screening
pre_screen = pass_fail_model.predict([[thickness, hole_diameter, force]])[0]
if pre_screen == 0:
return {
"status": "Fail",
"message": "AI predicts failure. Please adjust parameters.",
"stress": None,
"deformation": None,
}
# Run ANSYS simulation
max_stress, max_deformation = run_ansys_simulation(thickness, hole_diameter, force)
# Validate results with AI
validation = "Pass" if max_stress < 250 and max_deformation < 0.3 else "Fail"
# Generate output
return {
"status": validation,
"message": f"Design {validation}.",
"stress": max_stress,
"deformation": max_deformation,
}
# ========== UI ==========
def visualize_results(results):
fig, ax = plt.subplots()
labels = ["Stress", "Deformation"]
values = [results["stress"], results["deformation"]]
ax.bar(labels, values, color=["#FFA07A", "#20B2AA"])
ax.set_title("Simulation Results")
plt.tight_layout()
image_path = "results.png"
plt.savefig(image_path)
plt.close(fig)
return image_path
def run_ui(thickness, hole_diameter, force):
results = ai_and_simulation_workflow(thickness, hole_diameter, force)
if results["status"] == "Fail":
return results["message"], None
else:
image_path = visualize_results(results)
return results["message"], image_path
# Gradio Interface
interface = gr.Interface(
fn=run_ui,
inputs=[
gr.Slider(10, 50, step=1, label="Thickness (mm)"),
gr.Slider(5, 25, step=1, label="Hole Diameter (mm)"),
gr.Slider(1000, 15000, step=500, label="Force (N)"),
],
outputs=[
gr.Textbox(label="Simulation Status"),
gr.Image(label="Simulation Visualization"),
],
title="AI-Driven ANSYS Design Validation",
description="Interactive tool for design validation using AI and ANSYS.",
theme="default",
live=True,
)
# Launch the interface
interface.launch()