Spaces:
Sleeping
Sleeping
Update utils/ansys_utils.py
Browse files- utils/ansys_utils.py +35 -8
utils/ansys_utils.py
CHANGED
|
@@ -1,10 +1,13 @@
|
|
| 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}
|
|
@@ -14,13 +17,20 @@ def generate_apdl_script(dimensions, material, analysis_type):
|
|
| 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
|
|
@@ -28,14 +38,17 @@ def generate_apdl_script(dimensions, material, analysis_type):
|
|
| 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
|
|
|
|
| 39 |
/solu
|
| 40 |
solve
|
| 41 |
"""
|
|
@@ -43,8 +56,8 @@ def generate_apdl_script(dimensions, material, analysis_type):
|
|
| 43 |
apdl_script += """
|
| 44 |
/post1
|
| 45 |
! Output results
|
| 46 |
-
prnsol, S, 1
|
| 47 |
-
prnsol, U, 1
|
| 48 |
finish
|
| 49 |
"""
|
| 50 |
return apdl_script
|
|
@@ -53,12 +66,26 @@ def generate_apdl_script(dimensions, material, analysis_type):
|
|
| 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 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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}"
|
|
|
|
| 1 |
from ansys.mapdl.core import launch_mapdl
|
| 2 |
|
| 3 |
+
def generate_apdl_script(dimensions, material, analysis_type, tolerances, forces, acoustic_properties):
|
| 4 |
"""
|
| 5 |
Generates an APDL script based on input dimensions, material, and analysis type.
|
| 6 |
+
Additional parameters: tolerances, forces, and acoustic properties.
|
| 7 |
"""
|
| 8 |
length, width, height = map(float, dimensions.split('x'))
|
| 9 |
+
|
| 10 |
+
# APDL Script Header
|
| 11 |
apdl_script = f"""
|
| 12 |
/prep7
|
| 13 |
! Define material properties for {material}
|
|
|
|
| 17 |
|
| 18 |
! Create geometry: Box of size {length}x{width}x{height}
|
| 19 |
block, 0, {length}, 0, {width}, 0, {height}
|
| 20 |
+
|
| 21 |
+
! Apply tolerances (e.g., +/- tolerance for geometry)
|
| 22 |
+
! This is a simple representation, actual implementation depends on the use case
|
| 23 |
+
! Apply tolerances
|
| 24 |
+
etol, {tolerances} ! For example, setting tolerance
|
| 25 |
|
| 26 |
! Define element type for {analysis_type} analysis
|
| 27 |
et, 1, solid185
|
| 28 |
"""
|
| 29 |
|
| 30 |
if analysis_type == "Structural":
|
| 31 |
+
apdl_script += f"""
|
| 32 |
+
! Apply forces: {forces} N
|
| 33 |
+
f, 1, fx, {forces}
|
| 34 |
! Structural analysis settings
|
| 35 |
/solu
|
| 36 |
solve
|
|
|
|
| 38 |
|
| 39 |
elif analysis_type == "Thermal":
|
| 40 |
apdl_script += """
|
| 41 |
+
! Apply temperature distribution if needed
|
| 42 |
+
temp, 1, 100 ! Example: apply 100 degree Celsius temperature
|
| 43 |
! Thermal analysis settings
|
| 44 |
/solu
|
| 45 |
solve
|
| 46 |
"""
|
| 47 |
|
| 48 |
elif analysis_type == "Acoustic":
|
| 49 |
+
apdl_script += f"""
|
| 50 |
+
! Acoustic properties: {acoustic_properties}
|
| 51 |
+
! Example: absorption rate, speed of sound
|
| 52 |
/solu
|
| 53 |
solve
|
| 54 |
"""
|
|
|
|
| 56 |
apdl_script += """
|
| 57 |
/post1
|
| 58 |
! Output results
|
| 59 |
+
prnsol, S, 1 ! Stress results
|
| 60 |
+
prnsol, U, 1 ! Displacement results
|
| 61 |
finish
|
| 62 |
"""
|
| 63 |
return apdl_script
|
|
|
|
| 66 |
def run_simulation(apdl_script, analysis_type):
|
| 67 |
"""
|
| 68 |
Runs the ANSYS simulation and returns the results.
|
| 69 |
+
This function executes the generated APDL script.
|
| 70 |
"""
|
| 71 |
try:
|
| 72 |
+
# Start ANSYS MAPDL session
|
| 73 |
mapdl = launch_mapdl()
|
| 74 |
+
|
| 75 |
+
# Pass the generated APDL script to ANSYS for execution
|
| 76 |
+
mapdl.input_string(apdl_script) # Provide the APDL script
|
| 77 |
+
|
| 78 |
+
# Extract results based on the analysis type
|
| 79 |
+
if analysis_type == "Structural":
|
| 80 |
+
results = mapdl.post_processing.nodal_solution
|
| 81 |
+
elif analysis_type == "Thermal":
|
| 82 |
+
results = mapdl.post_processing.temperature_results
|
| 83 |
+
elif analysis_type == "Acoustic":
|
| 84 |
+
results = mapdl.post_processing.acoustic_results
|
| 85 |
+
|
| 86 |
+
# Close the ANSYS session
|
| 87 |
mapdl.exit()
|
| 88 |
+
|
| 89 |
return f"Simulation for {analysis_type} completed successfully. Results: {results}"
|
| 90 |
except Exception as e:
|
| 91 |
return f"Error in ANSYS simulation: {e}"
|