Advertisement
Guest User

kernel smoothing in openmp

a guest
Jan 17th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. This code takes a list of positions x,y,z and creates a kernal smoothed mesh using smoothing lengths hsml.
  3. Basically, I want to just to loop through i as fast a possible as every calculation is independent of one another. It just has to populate mesh_out which is shared. kern_result is a function representing the smoothing kernel.
  4.  
  5. !$OMP PARALLEL DO DEFAULT(private) SHARED(mesh_out)
  6. do i = 1,Ntot
  7.     xtmp = x0(i)
  8.     ytmp = y0(i)
  9.     ztmp = z0(i)
  10.     hsmltmp = hsml(i)
  11.  
  12.     if (h.lt.hmin) then
  13.         hsmltmp = hmin
  14.     elseif (h.gt.hmax) then
  15.         hsmltmp = hmax
  16.     end if
  17.  
  18.     Xrel = xtmp-Xc+Lx/2;
  19.     Yrel = ytmp-Yc+Ly/2;
  20.     Zrel = ztmp-Zc+Lz/2;
  21.  
  22.     if (Xrel.gt.-hsmltmp.and.Yrel.gt.-hsmltmp.and.Xrel-Lx.lt.hsmltmp &
  23.       .and.Yrel-Ly.lt.hsmltmp.and.Zrel.gt.-hsmltmp.and.Zrel-Lz.lt.hsmltmp) then
  24.         binned_particles = binned_particles + 1
  25.  
  26.         pixelX = (FLOOR(Xrel/pixelsizeX)+.5)*pixelsizeX
  27.         pixelY = (FLOOR(Yrel/pixelsizeY)+.5)*pixelsizeY
  28.         pixelZ = (FLOOR(Zrel/pixelsizeZ)+.5)*pixelsizeZ
  29.  
  30.         !write(*,*)"pixelXYZ",pixelX,pixelY,pixelZ
  31.  
  32.         nx = INT(CEILING(hsmltmp/pixelsizeX))
  33.         ny = INT(CEILING(hsmltmp/pixelsizeY))
  34.         nz = INT(CEILING(hsmltmp/pixelsizeZ))
  35.         !write(*,*)"Nxyz",nx,ny,nz
  36.         normalization = 0
  37.        
  38.         do dx = -nx,nx
  39.             do dy = -ny,ny
  40.                 do dz = -nz,nz
  41.                     distX = Xrel-(pixelX-REAL(dx)*pixelsizeX)
  42.                     distY = Yrel-(pixelY-REAL(dy)*pixelsizeY)
  43.                     distZ = Zrel-(pixelZ-REAL(dz)*pixelsizeZ)
  44.                     r2 = distX**2+distY**2+distZ**2;
  45.                     if (r2.lt.hsmltmp**2) then
  46.                         kern_result = kernel(hsmltmp,r2)
  47.                         normalization = normalization + kern_result
  48.                         xloc = CEILING(pixelX/pixelsizeX-REAL(dx));
  49.                         yloc = CEILING(pixelY/pixelsizeY-REAL(dy));
  50.                         zloc = CEILING(pixelZ/pixelsizeZ-REAL(dz));
  51.                         !write(*,*)"XYZloc",xloc,yloc,zloc
  52.                         if (xloc.gt.0.and.xloc.le.num_pixelsX.and.yloc.gt.0.and.yloc.le.num_pixelsY.and. &
  53.                              zloc.gt.0.and.zloc.le.num_pixelsZ) then
  54.                             kern_result = kernel(hsmltmp,r2)
  55.                             !$OMP ATOMIC UPDATE
  56.                             mesh_out(INT(zloc),INT(yloc),INT(xloc)) = &
  57.                                     mesh_out(INT(zloc),INT(yloc),INT(xloc)) &
  58.                                                                      + kern_result/normalization
  59.  
  60.                             !write(*,*)"kern",hsmltmp,r2,kern_result  
  61.                         end if
  62.                     end if
  63.                 end do
  64.             end do
  65.         end do
  66.     end if
  67. end do
  68. !$OMP END PARALLEL DO
  69.  
  70.  
  71. RECURSIVE FUNCTION kernel(hdum,rdum) RESULT(kern)
  72. real*8 rdum
  73. real*8 hdum
  74. real*8 kern
  75.  
  76. ! Kernal Coefficients
  77. real*8, parameter :: K1 = 2.546479089470
  78. real*8, parameter :: K2 = 15.278874536822
  79. real*8, parameter :: K3 = 5.092958178941
  80. real*8 u
  81.  
  82. u = sqrt(rdum)/hdum;
  83.  
  84. if (u.lt.0.5) then
  85.     kern = K1+K2*(u-1)*u**2;
  86. else
  87.     kern = K3*(1-u)**3;
  88. end if
  89. END FUNCTION kernel
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement