AnsysSimulation / utils /ansys_utils.py
karthikmn's picture
Create utils/ansys_utils.py
b56773f verified
raw
history blame
1.67 kB
from ansys.mapdl.core import launch_mapdl
def generate_apdl_script(dimensions, material, analysis_type):
"""
Generates an APDL script based on input dimensions, material, and analysis type.
"""
length, width, height = map(float, dimensions.split('x'))
apdl_script = f"""
/prep7
! Define material properties for {material}
mp, ex, 1, 210e3
mp, nuxy, 1, 0.3
mp, dens, 1, 7.85e-9
! Create geometry: Box of size {length}x{width}x{height}
block, 0, {length}, 0, {width}, 0, {height}
! Define element type for {analysis_type} analysis
et, 1, solid185
"""
if analysis_type == "Structural":
apdl_script += """
! Structural analysis settings
/solu
solve
"""
elif analysis_type == "Thermal":
apdl_script += """
! Thermal analysis settings
/solu
solve
"""
elif analysis_type == "Acoustic":
apdl_script += """
! Acoustic analysis settings
/solu
solve
"""
apdl_script += """
/post1
! Output results
prnsol, S, 1
prnsol, U, 1
finish
"""
return apdl_script
def run_simulation(apdl_script, analysis_type):
"""
Runs the ANSYS simulation and returns the results.
"""
try:
mapdl = launch_mapdl()
mapdl.input_string(apdl_script) # Pass the APDL script to the ANSYS solver
results = mapdl.post_processing.nodal_solution
mapdl.exit()
return f"Simulation for {analysis_type} completed successfully. Results: {results}"
except Exception as e:
return f"Error in ANSYS simulation: {e}"