我想知道如何执行以下操作:
def GetFlux(self, time):
bx = self.GetField("bx", time) * self.wpewce
by = self.GetField("by", time) * self.wpewce
bz = self.GetField("bz", time) * self.wpewce
flux = np.zeros((self.ncells[0]+1,self.ncells[1]+1),"float32", order='FORTRAN')
flux2 = np.zeros((self.ncells[0]+1,self.ncells[1]+1),"float32", order='FORTRAN')
dx = self.dl[0]
dz = self.dl[1]
nx = self.ncells[0]
nz = self.ncells[1]
j = 0
for i in np.arange(1, nx):
flux2[i,0] = flux2[i-1,0] + bz[i-1,0]*dx
flux[1:,0] = flux[0,0] + np.cumsum(bz[:-1,0]*dx)
for j in np.arange(1,nz):
flux2[0,j] = flux2[0,j-1] - bx[0,j-1]*dz
flux[0,1:] = flux[0,0] - np.cumsum(bx[0,:-1]*dz)
for i in np.arange(1,nx):
for j in np.arange(1,nz):
flux2[i,j] = 0.5*(flux2[i-1,j] + bz[i-1,j]*dx) + 0.5*(flux2[i,j-1] - bx[i,j-1]*dz)
return flux2
但是没有两个嵌套循环,这需要很长时间。Bx
,Bz
和flux
是相同大小的数组。
我已经设法用数组索引和 cumsum 替换了前两个单循环,但我不知道如何替换嵌套循环。
任何的想法 ?
谢谢