【问题标题】:parallelizing python finite element analysis并行化python有限元分析
【发布时间】:2020-05-06 00:32:08
【问题描述】:

我正在尝试并行化下面涉及有限元分析的 python 函数。特别是,我试图使函数中的 for 循环并行运行。我已经在 Matlab 中使用 parfor 完成了它,我正在尝试在 python 中做同样的事情。

def assemble(u):
K=np.zeros((ndof,ndof)) # initializing global stiffness matrix
Fint=np.zeros(ndof)
Fext=np.zeros(ndof)
for iel in range(tne):
    elnodes=elems[iel,:] # nodes of the local elements
    xcel=nodes[elnodes,0] # x-coordinates for the local elements
    ycel=nodes[elnodes,1] # y-coordinates for the local elements
    zcel=nodes[elnodes,2] # z-coordinates for the local elements
    dof=np.array([3*elnodes[0],3*elnodes[0]+1,3*elnodes[0]+2,3*elnodes[1],\
                  3*elnodes[1]+1,3*elnodes[1]+2,3*elnodes[2],3*elnodes[2]+1,\
                  3*elnodes[2]+2,3*elnodes[3],3*elnodes[3]+1,3*elnodes[3]+2,\
                  3*elnodes[4],3*elnodes[4]+1,3*elnodes[4]+2,3*elnodes[5],\
                  3*elnodes[5]+1,3*elnodes[5]+2,3*elnodes[6],3*elnodes[6]+1,\
                  3*elnodes[6]+2,3*elnodes[7],3*elnodes[7]+1,3*elnodes[7]+2]).flatten()
    u_el=u[dof]
    strain,stress=SS(xcel,ycel,zcel,u_el)
    ESM,Fint_e,Fext_e=Elem_KF(xcel,ycel,zcel,strain,stress)
    K[np.ix_(dof,dof)]+=ESM
    Fint[dof]+=Fint_e
    Fext[dof]+=Fext_e
R=Fext-Fint
return K,Fint,Fext,R

我已经尝试过了,但它对我不起作用:

def assemble(u):
dof2    = np.zeros((tne,24))
Fint_e2 = np.zeros((tne,24))
Fext_e2 = np.zeros((tne,24))
ppp = Pool(2)
for iel in range(tne):
    Fint_e2[iel,:], Fext_e2[iel,:], dof2[iel,:] = ppp.apply(workers,args=(iel,u,))
ppp.close()
ppp.join()
return Fint_e2, Fext_e2, dof2

def workers(iel, u):
print(iel)
elnodes=elems[iel,:].long() # nodes of the local elements
xcel=nodes[elnodes,0] # x-coordinates for the local elements
ycel=nodes[elnodes,1] # y-coordinates for the local elements
zcel=nodes[elnodes,2] # z-coordinates for the local elements
dof=np.array([3*elnodes[0],3*elnodes[0]+1,3*elnodes[0]+2,\
                  3*elnodes[1],3*elnodes[1]+1,3*elnodes[1]+2,\
                  3*elnodes[2],3*elnodes[2]+1,3*elnodes[2]+2,\
                  3*elnodes[3],3*elnodes[3]+1,3*elnodes[3]+2,\
                  3*elnodes[4],3*elnodes[4]+1,3*elnodes[4]+2,\
                  3*elnodes[5],3*elnodes[5]+1,3*elnodes[5]+2,\
                  3*elnodes[6],3*elnodes[6]+1,3*elnodes[6]+2,\
                  3*elnodes[7],3*elnodes[7]+1,3*elnodes[7]+2]).flatten()
u_el=u[dof]
strain,stress = Stress_Strain(xcel,ycel,zcel,u_el)
Fint_e,Fext_e = KEL(xcel,ycel,zcel,strain,stress)
return Fint_e, Fext_e, dof

任何帮助将不胜感激。谢谢!

【问题讨论】:

  • 这能回答你的问题吗? stackoverflow.com/questions/4682429/parfor-for-python 那里有一些不错的答案。
  • python 中的循环不如 matlab 快 - 实际上慢得多。您将有更好的时间尝试矢量化您的代码(如果可能的话),而不是尝试使用 multiprocessingconcurrent.futures 让它快速运行 - 因为您将为跨进程酸洗的性能损失付出代价并且没有由于 GIL,CPU 受限的线程速度提高

标签: python pool multiprocess finite-element-analysis


【解决方案1】:

您可以使用Pools,同时使用多个进程。

from multiprocessing import Pool

p = Pool(n_threads)

for iel in range(tne):
   #function will calculate all the parameters you have inside the loop
   results = p.apply(function,args=(x,y,z,etc,))  

#wait for all threads to return their value
p.close()
p.join()

【讨论】:

  • 嗨拉米罗,感谢您的回复。我按照您的建议尝试了一些方法,但它对我不起作用。请在下面查看我的答案。
猜你喜欢
  • 2016-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 2022-01-15
  • 1970-01-01
  • 2023-03-25
相关资源
最近更新 更多