3dsimulation2.0 / app.py
karthikmn's picture
Update app.py
8b6c956 verified
import gradio as gr
from apdl_generator.apdl_plate import generate_plate_apdl
from apdl_generator.apdl_beam import generate_beam_apdl
from simulators.python_simulation import run_python_simulation
from visualization import visualize_results, visualize_end_product
import os
def simulation_workflow(tool_type, use_case, include_hole, include_force, include_load, thickness, length, width, hole_diameter, force, load, elastic_modulus):
"""
Main simulation workflow that generates multiple formats for APDL, Python code, and simulation results.
"""
# Handle optional parameters
force = force if include_force else 0
load = load if include_load else 0
# Generate APDL script dynamically
if use_case == "plate":
hole_diameter = hole_diameter if include_hole else 0
apdl_path = generate_plate_apdl(thickness, length, width, hole_diameter, force)
elif use_case == "beam":
apdl_path = generate_beam_apdl(length, width, thickness, load)
else:
return "Invalid use case selected.", None, None, None
# Read the generated APDL program
with open(apdl_path, "r") as file:
apdl_program = file.read()
# Run simulation using Python-based solver
stress, deformation = run_python_simulation(apdl_path, use_case, thickness, length, width, force, load, elastic_modulus)
# Explanation for negligible deformation
if deformation < 1e-6:
deformation_note = "\nNote: Deformation is negligible due to the high rigidity of the material."
else:
deformation_note = ""
# Generate 2D simulation visualization
graph_path, _ = visualize_results("Python-Based Solver", length, width, thickness, stress, deformation)
# Generate end-product visualization
product_path = visualize_end_product(use_case, length, width, thickness, deformation)
# Save Python code for simulation
python_code = f"""
import numpy as np
# Define material properties
elastic_modulus = {elastic_modulus}
thickness = {thickness}
length = {length}
width = {width}
# Simulate stress and deformation (simplified example)
def simulate():
stress = elastic_modulus * thickness * width / length # Example formula
deformation = stress / elastic_modulus # Simplified deformation
return stress, deformation
# Run the simulation
stress, deformation = simulate()
print(f"Stress: {stress:.2f} MPa")
print(f"Deformation: {deformation:.6f} mm")
"""
python_file_path = "simulation_code.py"
with open(python_file_path, "w") as f:
f.write(python_code)
# Generate results in different formats
results_text = f"{tool_type} Simulation\nStress: {stress:.2f} MPa\nDeformation: {deformation:.6f} mm{deformation_note}"
# Save results to a .txt file
results_file_path = "simulation_results.txt"
with open(results_file_path, "w") as f:
f.write(results_text)
return (
results_text,
graph_path,
product_path,
apdl_program,
python_file_path, # Provide the path for Python code download
results_file_path # Provide the path for results file download
)
# Define the Gradio interface with a Submit button
inputs = [
gr.Radio(["Punch", "Die"], label="Select Tool Type"),
gr.Radio(["plate", "beam"], label="Select Use Case"),
gr.Checkbox(label="Include Hole for Plate Simulation"), # Checkbox for optional hole
gr.Checkbox(label="Include Force"), # Checkbox for optional force
gr.Checkbox(label="Include Load"), # Checkbox for optional load
gr.Slider(10, 50, step=1, label="Thickness (mm)"),
gr.Slider(100, 500, step=10, label="Length (mm)"),
gr.Slider(50, 200, step=10, label="Width (mm)"),
gr.Slider(5, 25, step=1, label="Hole Diameter (mm)"), # Controlled by "Include Hole"
gr.Slider(1000, 10000, step=500, label="Force (N)"), # Controlled by "Include Force"
gr.Slider(1000, 20000, step=1000, label="Load (N)"), # Controlled by "Include Load"
gr.Slider(5e10, 3e11, step=1e10, label="Elastic Modulus (Pa)") # Slider for Elastic Modulus
]
outputs = [
gr.Textbox(label="Simulation Results"),
gr.Image(label="2D Simulation Visualization"),
gr.Image(label="End Product Visualization"),
gr.Code(language="python", label="Generated APDL Program"), # Python Code Output
gr.File(label="Download Python Simulation Code"), # Allow file download of Python code
gr.File(label="Download Simulation Results") # Allow file download of results
]
# Add a button for submission
submit_button = gr.Button("Run Simulation")
# Create the Gradio interface with the button
interface = gr.Interface(
fn=simulation_workflow,
inputs=inputs,
outputs=outputs,
title="Punch and Die Simulation Tool with End Product Visualization and APDL",
live=False # Disable live updates
)
# Launch the interface
interface.launch()