以有效方式从Python运行ANSYS Mechanical APDL

问题描述 投票:2回答:1

我有以下代码,该代码编写一个输入文件并使用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外壳上按F5执行它,然后消息出现在外壳上:

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.
python batch-processing ansys
1个回答
0
投票

我不知道为什么您的代码在Python中变慢,但是我建议您使用PyPI中的PyAnsys软件包。它提供了结合Python和APDL的强大基础架构。

© www.soinside.com 2019 - 2024. All rights reserved.