AnsysSimulation / utils /ansys_utils.py
karthikmn's picture
Update utils/ansys_utils.py
c661a1b verified
from ansys.mapdl.core import launch_mapdl
def generate_apdl_script(dimensions, material, analysis_type, tolerances, forces, acoustic_properties):
"""
Generates an APDL script based on input dimensions, material, and analysis type.
Additional parameters: tolerances, forces, and acoustic properties.
"""
length, width, height = map(float, dimensions.split('x'))
# APDL Script Header
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}
! Apply tolerances (e.g., +/- tolerance for geometry)
! This is a simple representation, actual implementation depends on the use case
! Apply tolerances
etol, {tolerances} ! For example, setting tolerance
! Define element type for {analysis_type} analysis
et, 1, solid185
"""
if analysis_type == "Structural":
apdl_script += f"""
! Apply forces: {forces} N
f, 1, fx, {forces}
! Structural analysis settings
/solu
solve
"""
elif analysis_type == "Thermal":
apdl_script += """
! Apply temperature distribution if needed
temp, 1, 100 ! Example: apply 100 degree Celsius temperature
! Thermal analysis settings
/solu
solve
"""
elif analysis_type == "Acoustic":
apdl_script += f"""
! Acoustic properties: {acoustic_properties}
! Example: absorption rate, speed of sound
/solu
solve
"""
apdl_script += """
/post1
! Output results
prnsol, S, 1 ! Stress results
prnsol, U, 1 ! Displacement results
finish
"""
return apdl_script
def run_simulation(apdl_script, analysis_type):
"""
Runs the ANSYS simulation and returns the results.
This function executes the generated APDL script.
"""
try:
# Start ANSYS MAPDL session
mapdl = launch_mapdl()
# Pass the generated APDL script to ANSYS for execution
mapdl.input_string(apdl_script) # Provide the APDL script
# Extract results based on the analysis type
if analysis_type == "Structural":
results = mapdl.post_processing.nodal_solution
elif analysis_type == "Thermal":
results = mapdl.post_processing.temperature_results
elif analysis_type == "Acoustic":
results = mapdl.post_processing.acoustic_results
# Close the ANSYS session
mapdl.exit()
return f"Simulation for {analysis_type} completed successfully. Results: {results}"
except Exception as e:
return f"Error in ANSYS simulation: {e}"