user8636507
user8636507

Reputation:

Run ANSYS Mechanical APDL from Python in an efficient manner

I have the following code, which writes an Input file and executes ANSYS Mechanical APDL using a Windows command. My issue is the execution time is much longer (15 minutes inside the software, upwards of 1 hour when calling from Python). I need it to be faster because I am varying as many as 'all' the input parameters.

def RunAPDL(E,t,w,p,aa,bb,lz,alpha,delta):

    ansyspath = r'C:\Program Files\ANSYS.Inc\v181\ansys\bin\winx64\MAPDL.exe'
    directory = r'C:\Users\Erik\Documents\ANSYS'
    jobname = 'file'
    memory = '4096'
    reserve = '1024'
    inputfile = r'C:\Users\Erik\Documents\ANSYS\ShellBucklingInput.inp'
    outputfile = r'C:\Users\Erik\Documents\ANSYS\OutputFile.txt'
    resultsfile = r'C:\Users\Erik\Documents\ANSYS\ShellBuckling.csv'

    # Start time count
    start = time.clock()

    # Write input file
    input_parameters = ('/NERR,200,10000,,OFF,0 \n'
                        'pi = acos(-1) \n'
                        'E = {:6.0f}     ! N/mm2 Young\'s modulus\n'
                        't = {:4.2f}       ! mm thickness\n'
                        'w = {:3.2f}       ! Poisson\'s ratio\n'
                        'p = {:7.2f}    ! N/mm2 external pressure\n'
                        'aa = {:6.2f}   ! mm horizontal radius at u = 0\n'
                        'bb = {:6.2f}   ! mm vertical radius\n'
                        'lz = {:6.2f}   ! mm model height\n'
                        'lu = 2*asin(lz/2/bb) !mm \n'
                        'lv = 2*pi      ! model perimeter at u = 0 \n'
                        'nu = 2*NINT(ABS(aa)/SQRT(ABS(aa)*t)) \n'
                        'nv = 3*nu      ! number of elements along v-axis \n'
                        'alpha = {:4.2f}   ! ratio of lu that is not loaded by p \n'
                        'delta = {:4.2f}*t ! prescribed imperfection magnitude \n'
                        '*ULIB,ShellBucklingLibrary,mac \n'
                        '*USE,ShellBuckling,pi,E,t,w,p,aa,bb,lz,lu,nu,nv,alpha,delta \n'
                        '/CLEAR'
                        ).format(E,t,w,p,aa,bb,lz,alpha,delta)
    with open(inputfile,'w') as f:
        f.write(input_parameters)

    # Call ANSYS
    callstring = ('\"{}\" -p aa_t_a -dir \"{}\" -j \"{}\" -s read'
                  ' -m {} -db {} -t -d win32 -b -i \"{}\" -o \"{}\"'
                  ).format(ansyspath,directory,jobname,memory,reserve,inputfile,outputfile)
    print('Invoking ANSYS with', callstring)
    proc = subprocess.Popen(callstring).wait()

    # Update pressure field for next analysis
    with open(resultsfile,'r') as f:
        lambdaS = float(list(csv.reader(f))[-1][16])
    p = 1.2*lambdaS*p
    print('Updated pressure is',p,' N/mm2.')

    # Stop time count
    stop = time.clock()
    print('Elapsed time is ',stop-start,' seconds.')

    return(p) 

I execute it by pressing F5 on the Python shell, then messages appear in the shell:

Invoking ANSYS with "C:\Program Files\ANSYS 
Inc\v181\ansys\bin\winx64\MAPDL.exe" -p aa_t_a -dir 
"C:\Users\Erik\Documents\ANSYS" -j "file" -s read -m 4096 -db 1024 -t -d 
win32 -b -i "C:\Users\Erik\Documents\ANSYS\ShellBucklingInput.inp" -o 
"C:\Users\Erik\Documents\ANSYS\OutputFile.txt"
Updated pressure is -0.0046478399999999994  N/mm2.
Elapsed time is  4016.59094467131  seconds.

Upvotes: 3

Views: 4438

Answers (1)

Christos Sevastiadis
Christos Sevastiadis

Reputation: 161

I don't know why your code is slower in Python but I suggest you to use PyAnsys package from PyPI. It offers a robust infrastructure to combine Python and APDL.

Upvotes: 2

Related Questions