File size: 1,602 Bytes
090629b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# ansys_utils.py

import subprocess
import os

def run_simulation(step_file_path):
    """
    Run ANSYS simulation using the generated STEP file.
    This function assumes you have ANSYS installed and accessible via command line.
    The process involves:
    1. Running ANSYS APDL commands (which should be in a file or string).
    2. Running simulations.
    3. Retrieving results.
    """
    # Here we assume an ANSYS APDL script will be created dynamically
    apdl_script = f"""
    /PREP7
    ! Load the STEP file into ANSYS
    /FREAD, {step_file_path}, STEP
    /SOLU
    ! Apply boundary conditions, loads, etc.
    SOLVE
    /POST1
    ! Get results like displacement, stress, etc.
    """

    # Save the APDL script to a file
    apdl_file = "/tmp/ansys_script.inp"
    with open(apdl_file, "w") as f:
        f.write(apdl_script)

    # Run ANSYS with the APDL script
    try:
        # Replace this command with the actual command for your ANSYS installation
        result = subprocess.run(["ansys", "-b", "-i", apdl_file, "-o", "/tmp/ansys_output.txt"],
                                stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        if result.returncode != 0:
            raise Exception(f"Error running ANSYS: {result.stderr.decode()}")

        # Retrieve and return results from the output file (for example, displacement or stress)
        with open("/tmp/ansys_output.txt", "r") as f:
            simulation_results = f.read()
        
        return simulation_results
    except Exception as e:
        raise Exception(f"Error executing ANSYS simulation: {str(e)}")