3

我有以下代码,它编写一个输入文件并使用 Windows 命令执行 ANSYS Mechanical APDL。我的问题是执行时间要长得多(软件内 15 分钟,从 Python 调用时超过 1 小时)。我需要它更快,因为我改变了尽可能多的“所有”输入参数。

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) 

我通过在 Python shell 上按 F5 来执行它,然后消息出现在 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.
4

1 回答 1

1

我不知道为什么您的代码在 Python 中较慢,但我建议您使用 PyPI 中的 PyAnsys 包。它提供了一个强大的基础设施来结合 Python 和 APDL。

于 2020-05-30T15:11:38.280 回答