Spaces:
Build error
Build error
| import gradio as gr | |
| import subprocess | |
| from apdl_generator.apdl_plate import generate_plate_apdl | |
| from apdl_generator.apdl_beam import generate_beam_apdl | |
| from simulators.pycalculix_simulation import run_pycalculix_simulation | |
| from simulators.ansys_simulation import run_ansys_simulation | |
| from visualization import visualize_results | |
| def simulation_workflow(use_case, simulator, **kwargs): | |
| print(f"Running simulation for use_case={use_case}, simulator={simulator}") | |
| # Generate APDL script based on user inputs | |
| if use_case == "plate": | |
| print("Generating APDL script for plate simulation...") | |
| apdl_path = generate_plate_apdl(kwargs["thickness"], kwargs["length"], kwargs["width"], kwargs["hole_diameter"], kwargs["force"]) | |
| elif use_case == "beam": | |
| print("Generating APDL script for beam simulation...") | |
| apdl_path = generate_beam_apdl(kwargs["length"], kwargs["width"], kwargs["thickness"], kwargs["load"]) | |
| else: | |
| print("Invalid use case selected.") | |
| return "Invalid use case selected.", None, None | |
| print(f"APDL script generated at: {apdl_path}") | |
| # Run the selected simulator | |
| if simulator == "PyCalculix": | |
| print("Running PyCalculix simulation...") | |
| stress, deformation = run_pycalculix_simulation(use_case, **kwargs) | |
| elif simulator == "ANSYS": | |
| print("Running ANSYS simulation...") | |
| stress, deformation = run_ansys_simulation(apdl_path) | |
| else: | |
| print("Invalid simulator selected.") | |
| return "Invalid simulator selected.", None, None | |
| print(f"Simulation results - Stress: {stress}, Deformation: {deformation}") | |
| # Visualize results (both 2D and 3D) | |
| print("Visualizing results...") | |
| graph_path, three_d_path = visualize_results(simulator, kwargs["length"], kwargs["width"], kwargs["thickness"], stress, deformation) | |
| print(f"Visualization paths - 2D: {graph_path}, 3D: {three_d_path}") | |
| return f"Stress: {stress:.2f} MPa, Deformation: {deformation:.2f} mm", graph_path, three_d_path | |
| # Define Gradio interface | |
| interface = gr.Interface( | |
| fn=simulation_workflow, | |
| inputs=[ | |
| gr.Radio(["plate", "beam"], label="Select Use Case"), # Choose plate or beam simulation | |
| gr.Dropdown(["PyCalculix", "ANSYS"], label="Select Simulator"), # Choose simulator | |
| gr.Slider(10, 50, step=1, label="Thickness (mm)"), # Input: Thickness | |
| gr.Slider(100, 500, step=10, label="Length (mm)"), # Input: Length | |
| gr.Slider(50, 200, step=10, label="Width (mm)"), # Input: Width | |
| gr.Slider(5, 25, step=1, label="Hole Diameter (mm)", optional=True), # Input: Hole Diameter (for plate) | |
| gr.Slider(1000, 10000, step=500, label="Force (N)", optional=True), # Input: Force (for plate) | |
| gr.Slider(1000, 20000, step=1000, label="Load (N)", optional=True) # Input: Load (for beam) | |
| ], | |
| outputs=[ | |
| gr.Textbox(label="Simulation Results"), # Output: Simulation text results | |
| gr.Image(label="2D Results Visualization"), # Output: 2D plot (stress and deformation) | |
| gr.Image(label="3D Results Visualization") # Output: 3D plot (stress distribution) | |
| ], | |
| title="Unified Simulation Tool (PyCalculix and ANSYS)", | |
| live=True | |
| ) | |
| # Launch Gradio interface | |
| print("Launching Gradio interface...") | |
| interface.launch() | |