SuriRaja commited on
Commit
f45088a
·
verified ·
1 Parent(s): fb54501

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -0
app.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import matplotlib.pyplot as plt
4
+ from ansys.mapdl.core import launch_mapdl
5
+
6
+ # Function to generate APDL script
7
+ def generate_apdl_script(youngs_modulus, poissons_ratio, length, width, thickness, element_size, pressure):
8
+ script = f"""
9
+ /PREP7
10
+ MP,EX,1,{youngs_modulus}
11
+ MP,PRXY,1,{poissons_ratio}
12
+ BLOCK,0,{length},0,{width},0,{thickness}
13
+ ESIZE,{element_size}
14
+ ET,1,SOLID185
15
+ VMESH,ALL
16
+ NSEL,S,LOC,X,0
17
+ D,ALL,ALL
18
+ ALLSEL,ALL
19
+ NSEL,S,LOC,Z,{thickness}
20
+ SF,ALL,PRES,{pressure}
21
+ ALLSEL,ALL
22
+ /SOLU
23
+ ANTYPE,STATIC
24
+ SOLVE
25
+ FINISH
26
+ /POST1
27
+ SET,1,1
28
+ PRNSOL,U,Z
29
+ PRNSOL,S,EQV
30
+ FINISH
31
+ """
32
+ return script
33
+
34
+ # Function to run simulation and return results
35
+ def run_simulation(apdl_script):
36
+ mapdl = launch_mapdl()
37
+ mapdl.input_strings(apdl_script)
38
+ mapdl.post1()
39
+ mapdl.set(1, 1)
40
+ nodal_displacements = mapdl.post_processing.nodal_displacement('UZ')
41
+ nodal_stress = mapdl.post_processing.nodal_stress(0)
42
+ mapdl.exit()
43
+ return nodal_displacements, nodal_stress
44
+
45
+ # Function to visualize results
46
+ def visualize_results(displacements, stress):
47
+ fig, ax = plt.subplots(2, 1, figsize=(10, 8))
48
+
49
+ ax[0].plot(displacements, label='Nodal Displacement (UZ)')
50
+ ax[0].set_xlabel('Node Number')
51
+ ax[0].set_ylabel('Displacement (mm)')
52
+ ax[0].set_title('Nodal Displacement in Z Direction')
53
+ ax[0].legend()
54
+ ax[0].grid(True)
55
+
56
+ ax[1].plot(stress, label='Nodal Stress (EQV)')
57
+ ax[1].set_xlabel('Node Number')
58
+ ax[1].set_ylabel('Stress (MPa)')
59
+ ax[1].set_title('Equivalent Nodal Stress')
60
+ ax[1].legend()
61
+ ax[1].grid(True)
62
+
63
+ plt.tight_layout()
64
+ plt.show()
65
+
66
+ # Function to evaluate results against design criteria
67
+ def evaluate_results(displacements, stress, displacement_threshold=0.5, stress_threshold=250):
68
+ max_displacement = np.max(np.abs(displacements))
69
+ max_stress = np.max(np.abs(stress))
70
+ displacement_ok = max_displacement <= displacement_threshold
71
+ stress_ok = max_stress <= stress_threshold
72
+ return displacement_ok and stress_ok, max_displacement, max_stress
73
+
74
+ # Gradio interface functions
75
+ def project_requirements(youngs_modulus, poissons_ratio, length, width, thickness, element_size, pressure):
76
+ params = {
77
+ 'youngs_modulus': youngs_modulus,
78
+ 'poissons_ratio': poissons_ratio,
79
+ 'length': length,
80
+ 'width': width,
81
+ 'thickness': thickness,
82
+ 'element_size': element_size,
83
+ 'pressure': pressure
84
+ }
85
+ return params
86
+
87
+ def apdl_program(params):
88
+ apdl_script = generate_apdl_script(**params)
89
+ return apdl_script
90
+
91
+ def simulation_and_visualization(apdl_script):
92
+ displacements, stress = run_simulation(apdl_script)
93
+ visualize_results(displacements, stress)
94
+ design_acceptable, max_disp, max_strs = evaluate_results(displacements, stress)
95
+ result_message = (
96
+ f"Max Displacement: {max_disp:.2f} mm\n"
97
+ f"Max Stress: {max_strs:.2f} MPa\n"
98
+ f"Design {'meets' if design_acceptable else 'does not meet'} all criteria."
99
+ )
100
+ return result_message
101
+
102
+ # Gradio Blocks for multi-tab interface
103
+ with gr.Blocks() as demo:
104
+ with gr.Tabs():
105
+ with gr.TabItem("Project Requirements"):
106
+ youngs_modulus = gr.Number(label="Young's Modulus (MPa)", value=210000)
107
+ poissons_ratio = gr.Number(label="Poisson's Ratio", value=0.3)
108
+ length = gr.Number(label="Length (mm)", value=100)
109
+ width = gr.Number(label="Width (mm)", value=50)
110
+ thickness = gr.Number(label="Thickness (mm)", value=10)
111
+ element_size = gr.Number(label="Element Size (mm)", value=5)
112
+ pressure = gr.Number(label="Applied Pressure (MPa)", value=10)
113
+ params_button = gr.Button("Submit")
114
+ params_output = gr.JSON()
115
+ params_button.click(
116
+ project_requirements,
117
+ inputs=[youngs_modulus, poissons_ratio, length, width, thickness, element_size, pressure],
118
+ outputs=params_output
119
+ )
120
+ with gr.TabItem("APDL Program Generation"):
121
+ apdl_output = gr.Textbox(label="Generated APDL Script", lines=20)
122
+ params_output.change(
123
+ apdl_program,
124
+ inputs=params_output,
125
+ outputs=apdl_output
126
+ )
127
+ with gr.TabItem("Simulation and Visualization"):
128
+ result_output = gr.Textbox(label="Simulation Results", lines=10)
129
+ simulate_button = gr.Button("Run Simulation")
130
+ simulate_button.click(
131
+ simulation_and_visualization,
132
+ inputs=apdl_output,
133
+ outputs=result_output
134
+ )
135
+
136
+ demo.launch()