simplified maskParticles which lead to 5x speedup

This commit is contained in:
Michael Krayer 2020-04-19 18:15:58 +02:00
parent 2f81bc2507
commit fcca37ad93
1 changed files with 20 additions and 23 deletions

View File

@ -1092,6 +1092,10 @@ class ibmppp:
def maskParticles(self,key,reconstruct=False,cval=np.nan,padding=0.0): def maskParticles(self,key,reconstruct=False,cval=np.nan,padding=0.0):
'''Fills grid points which lie inside of solid phase with values.''' '''Fills grid points which lie inside of solid phase with values.'''
# Shortcut to local grid
xg = self.localGrid[key][0]
yg = self.localGrid[key][1]
zg = self.localGrid[key][2]
# Loop through local particles # Loop through local particles
for ipart in range(0,self.__npartl): for ipart in range(0,self.__npartl):
# Slice a box from the field around the particle # Slice a box from the field around the particle
@ -1120,29 +1124,22 @@ class ibmppp:
coeff_roty = 0.0 coeff_roty = 0.0
coeff_rotz = 0.0 coeff_rotz = 0.0
# Get bounding box of particle # Get bounding box of particle
idx_x = np.nonzero((self.localGrid[key][0]>=xp-rp) & (self.localGrid[key][0]<=xp+rp))[0] idx_x = np.nonzero((xg>=xp-rp) & (xg<=xp+rp))[0]
idx_y = np.nonzero((self.localGrid[key][1]>=yp-rp) & (self.localGrid[key][1]<=yp+rp))[0] idx_y = np.nonzero((yg>=yp-rp) & (yg<=yp+rp))[0]
idx_z = np.nonzero((self.localGrid[key][2]>=zp-rp) & (self.localGrid[key][2]<=zp+rp))[0] idx_z = np.nonzero((zg>=zp-rp) & (zg<=zp+rp))[0]
# Slice the bounding box # Triple for loop
sli_x = slice(idx_x[0],idx_x[-1]+1) for ii in range(idx_x[0],idx_x[-1]+1):
sli_y = slice(idx_y[0],idx_y[-1]+1) Dx = xg[ii]-xp
sli_z = slice(idx_z[0],idx_z[-1]+1) for jj in range(idx_y[0],idx_y[-1]+1):
# Construct a grid of the subarray Dy = yg[jj]-yp
xg,yg,zg = np.meshgrid(self.localGrid[key][0][sli_x],self.localGrid[key][1][sli_y],self.localGrid[key][2][sli_z],indexing='ij',copy=True) for kk in range(idx_z[0],idx_z[-1]+1):
# Iterate through subarray and mask field Dz = zg[kk]-zp
it = np.nditer((self.field[key][sli_x,sli_y,sli_z],xg,yg,zg), isInside = Dx*Dx+Dy*Dy+Dz*Dz <= rp*rp
op_flags=[['writeonly'],['readonly'],['readonly'],['readonly']] if isInside:
) if reconstruct:
while not it.finished: self.field[key][ii,jj,kk] = coeff_lin + coeff_rotx*Dx + coeff_roty*Dy + coeff_rotz*Dz
xx,yy,zz = it[1:4] else:
Dx,Dy,Dz = xx-xp, yy-yp, zz-zp self.field[key][ii,jj,kk] = cval
isInside = Dx**2+Dy**2+Dz**2 <= rp**2
if isInside:
if reconstruct:
it[0] = coeff_lin + coeff_rotx*Dx + coeff_roty*Dy + coeff_rotz*Dz
else:
it[0] = cval
it.iternext()
def shiftField(self,key,shift,subsequent=True): def shiftField(self,key,shift,subsequent=True):
'''Shifts a field by dx/2 in specified direction as in-place operation. The new values are determined by linear interpolation.''' '''Shifts a field by dx/2 in specified direction as in-place operation. The new values are determined by linear interpolation.'''