File size: 4,883 Bytes
788b960
e952dbf
 
 
 
 
788b960
c9c0542
e952dbf
 
 
47a31ec
e952dbf
 
 
 
 
788b960
e952dbf
c9c0542
788b960
c9c0542
788b960
e952dbf
341dd47
e952dbf
 
 
341dd47
e952dbf
c9c0542
dcbde3d
e952dbf
 
 
 
 
341dd47
e952dbf
c9c0542
e952dbf
 
c9c0542
788b960
e952dbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
788b960
e952dbf
c9c0542
f66ba62
e952dbf
 
 
 
788b960
e952dbf
 
 
 
 
 
 
 
788b960
f66ba62
 
 
 
 
 
 
 
 
 
 
 
 
c9c0542
f66ba62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
788b960
 
f66ba62
 
e952dbf
f66ba62
788b960
 
f66ba62
788b960
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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()