Spaces:
Sleeping
Sleeping
Create utils/ansys_utils.py
Browse files- utils/ansys_utils.py +64 -0
utils/ansys_utils.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ansys.mapdl.core import launch_mapdl
|
| 2 |
+
|
| 3 |
+
def generate_apdl_script(dimensions, material, analysis_type):
|
| 4 |
+
"""
|
| 5 |
+
Generates an APDL script based on input dimensions, material, and analysis type.
|
| 6 |
+
"""
|
| 7 |
+
length, width, height = map(float, dimensions.split('x'))
|
| 8 |
+
apdl_script = f"""
|
| 9 |
+
/prep7
|
| 10 |
+
! Define material properties for {material}
|
| 11 |
+
mp, ex, 1, 210e3
|
| 12 |
+
mp, nuxy, 1, 0.3
|
| 13 |
+
mp, dens, 1, 7.85e-9
|
| 14 |
+
|
| 15 |
+
! Create geometry: Box of size {length}x{width}x{height}
|
| 16 |
+
block, 0, {length}, 0, {width}, 0, {height}
|
| 17 |
+
|
| 18 |
+
! Define element type for {analysis_type} analysis
|
| 19 |
+
et, 1, solid185
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
if analysis_type == "Structural":
|
| 23 |
+
apdl_script += """
|
| 24 |
+
! Structural analysis settings
|
| 25 |
+
/solu
|
| 26 |
+
solve
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
elif analysis_type == "Thermal":
|
| 30 |
+
apdl_script += """
|
| 31 |
+
! Thermal analysis settings
|
| 32 |
+
/solu
|
| 33 |
+
solve
|
| 34 |
+
"""
|
| 35 |
+
|
| 36 |
+
elif analysis_type == "Acoustic":
|
| 37 |
+
apdl_script += """
|
| 38 |
+
! Acoustic analysis settings
|
| 39 |
+
/solu
|
| 40 |
+
solve
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
apdl_script += """
|
| 44 |
+
/post1
|
| 45 |
+
! Output results
|
| 46 |
+
prnsol, S, 1
|
| 47 |
+
prnsol, U, 1
|
| 48 |
+
finish
|
| 49 |
+
"""
|
| 50 |
+
return apdl_script
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def run_simulation(apdl_script, analysis_type):
|
| 54 |
+
"""
|
| 55 |
+
Runs the ANSYS simulation and returns the results.
|
| 56 |
+
"""
|
| 57 |
+
try:
|
| 58 |
+
mapdl = launch_mapdl()
|
| 59 |
+
mapdl.input_string(apdl_script) # Pass the APDL script to the ANSYS solver
|
| 60 |
+
results = mapdl.post_processing.nodal_solution
|
| 61 |
+
mapdl.exit()
|
| 62 |
+
return f"Simulation for {analysis_type} completed successfully. Results: {results}"
|
| 63 |
+
except Exception as e:
|
| 64 |
+
return f"Error in ANSYS simulation: {e}"
|