Index: palm/trunk/SOURCE/lpm_pack_arrays.f90
===================================================================
--- palm/trunk/SOURCE/lpm_pack_arrays.f90	(revision 2417)
+++ palm/trunk/SOURCE/lpm_pack_arrays.f90	(revision 2606)
@@ -25,4 +25,11 @@
 ! -----------------
 ! $Id$
+! Changed particle box locations: center of particle box now coincides 
+! with scalar grid point of same index.
+! Renamed module and subroutines: lpm_pack_arrays_mod -> lpm_pack_and_sort_mod
+! lpm_pack_all_arrays -> lpm_sort_in_subboxes, lpm_pack_arrays -> lpm_pack
+! lpm_sort -> lpm_sort_timeloop_done
+! 
+! 2417 2017-09-06 15:22:27Z suehring
 ! New routine which sorts particles into particles that completed and not 
 ! completed the LES timestep. 
@@ -68,5 +75,5 @@
 !> its timestep. 
 !------------------------------------------------------------------------------!
- MODULE lpm_pack_arrays_mod
+ MODULE lpm_pack_and_sort_mod
  
 
@@ -76,65 +83,74 @@
 
     PRIVATE
-    PUBLIC lpm_pack_all_arrays, lpm_pack_arrays, lpm_sort
-
-    INTERFACE lpm_pack_all_arrays
-       MODULE PROCEDURE lpm_pack_all_arrays
-    END INTERFACE lpm_pack_all_arrays
-
-    INTERFACE lpm_pack_arrays
-       MODULE PROCEDURE lpm_pack_arrays
-    END INTERFACE lpm_pack_arrays
-
-    INTERFACE lpm_sort
-       MODULE PROCEDURE lpm_sort
-    END INTERFACE lpm_sort
+    PUBLIC lpm_sort_in_subboxes, lpm_pack, lpm_sort_timeloop_done
+
+    INTERFACE lpm_sort_in_subboxes
+       MODULE PROCEDURE lpm_sort_in_subboxes
+    END INTERFACE lpm_sort_in_subboxes
+
+    INTERFACE lpm_pack
+       MODULE PROCEDURE lpm_pack
+    END INTERFACE lpm_pack
+
+    INTERFACE lpm_sort_timeloop_done
+       MODULE PROCEDURE lpm_sort_timeloop_done
+    END INTERFACE lpm_sort_timeloop_done
 
 
  CONTAINS
+
+!------------------------------------------------------------------------------!
+! Description:
+! -----------
+!> Routine for the whole processor
+!> Sort all particles into the 8 respective subgrid boxes
+!------------------------------------------------------------------------------!
+    SUBROUTINE lpm_sort_in_subboxes
+
+       USE cpulog,                                                              &
+           ONLY:  cpu_log, log_point_s
+
+       USE indices,                                                             &
+           ONLY:  nxl, nxr, nys, nyn, nzb, nzt
+
+       USE kinds
+
+       USE control_parameters,                                                  &
+           ONLY: dz
+       
+       USE grid_variables,                                                      &
+           ONLY: dx,dy,ddx, ddy
+          
+       IMPLICIT NONE
+
+       INTEGER(iwp) ::  ip !<
+       INTEGER(iwp) ::  jp !<
+       INTEGER(iwp) ::  kp !<
+
+       CALL cpu_log( log_point_s(51), 'lpm_sort_in_subboxes', 'start' )
+       DO  ip = nxl, nxr
+          DO  jp = nys, nyn
+             DO  kp = nzb+1, nzt
+                number_of_particles = prt_count(kp,jp,ip)
+                IF ( number_of_particles <= 0 )  CYCLE
+                particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
+                
+                CALL lpm_pack_and_sort(ip,jp,kp)
+                
+                prt_count(kp,jp,ip) = number_of_particles
+             ENDDO
+          ENDDO
+       ENDDO
+       CALL cpu_log( log_point_s(51), 'lpm_sort_in_subboxes', 'stop' )
+       RETURN
+
+    END SUBROUTINE lpm_sort_in_subboxes
 
 !------------------------------------------------------------------------------!
 ! Description:
 ! ------------
-!> @todo Missing subroutine description.
-!------------------------------------------------------------------------------!
-    SUBROUTINE lpm_pack_all_arrays
-
-       USE cpulog,                                                             &
-           ONLY:  cpu_log, log_point_s
-
-       USE indices,                                                            &
-           ONLY:  nxl, nxr, nys, nyn, nzb, nzt
-
-       USE kinds
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  i !<
-       INTEGER(iwp) ::  j !<
-       INTEGER(iwp) ::  k !<
-
-       CALL cpu_log( log_point_s(51), 'lpm_pack_all_arrays', 'start' )
-       DO  i = nxl, nxr
-          DO  j = nys, nyn
-             DO  k = nzb+1, nzt
-                number_of_particles = prt_count(k,j,i)
-                IF ( number_of_particles <= 0 )  CYCLE
-                particles => grid_particles(k,j,i)%particles(1:number_of_particles)
-                CALL lpm_pack_and_sort(i,j,k)
-                prt_count(k,j,i) = number_of_particles
-             ENDDO
-          ENDDO
-       ENDDO
-       CALL cpu_log( log_point_s(51), 'lpm_pack_all_arrays', 'stop' )
-       RETURN
-
-    END SUBROUTINE lpm_pack_all_arrays
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> @todo Missing subroutine description.
-!------------------------------------------------------------------------------!
-    SUBROUTINE lpm_pack_arrays
+!> Move all particles not marked for deletion to lowest indices (packing)
+!------------------------------------------------------------------------------!
+    SUBROUTINE lpm_pack
 
        USE kinds
@@ -173,43 +189,38 @@
        number_of_particles = nn
 
-    END SUBROUTINE lpm_pack_arrays
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> @todo Missing subroutine description.
-!------------------------------------------------------------------------------!
+    END SUBROUTINE lpm_pack 
+
     SUBROUTINE lpm_pack_and_sort (ip,jp,kp)
 
-      USE control_parameters,                                                  &
-          ONLY: dz
-
-      USE kinds
-
-      USE grid_variables,                                                      &
-          ONLY: ddx, ddy
-
-      IMPLICIT NONE
-
-      INTEGER(iwp), INTENT(IN) :: ip
-      INTEGER(iwp), INTENT(IN) :: jp
-      INTEGER(iwp), INTENT(IN) :: kp
-
-      INTEGER(iwp)             :: i
-      INTEGER(iwp)             :: is
-      INTEGER(iwp)             :: j
-      INTEGER(iwp)             :: k
-      INTEGER(iwp)             :: n
-      INTEGER(iwp)             :: nn
-      INTEGER(iwp)             :: m
-      INTEGER(iwp)             :: sort_index
-
-      INTEGER(iwp), DIMENSION(0:7) :: sort_count
-
-      TYPE(particle_type), DIMENSION(number_of_particles,0:7) :: sort_particles
-
+       USE control_parameters,                                                 &
+           ONLY: dz
+
+       USE kinds
+
+       USE grid_variables,                                                     &
+           ONLY: dx,dy,ddx, ddy
+
+       IMPLICIT NONE
+
+       INTEGER(iwp), INTENT(IN) :: ip
+       INTEGER(iwp), INTENT(IN) :: jp
+       INTEGER(iwp), INTENT(IN) :: kp
+
+       INTEGER(iwp)             :: i
+       INTEGER(iwp)             :: is
+       INTEGER(iwp)             :: j
+       INTEGER(iwp)             :: k
+       INTEGER(iwp)             :: n
+       INTEGER(iwp)             :: nn
+       INTEGER(iwp)             :: m
+       INTEGER(iwp)             :: sort_index
+
+       INTEGER(iwp), DIMENSION(0:7) :: sort_count
+
+       TYPE(particle_type), DIMENSION(number_of_particles,0:7) :: sort_particles
+      
        nn = 0
        sort_count = 0
-
+                
        DO  n = 1, number_of_particles
           sort_index = 0
@@ -217,11 +228,18 @@
           IF ( particles(n)%particle_mask )  THEN
              nn = nn + 1
-             i = particles(n)%x * ddx
-             j = particles(n)%y * ddy
-             k = ( particles(n)%z + 0.5_wp * dz ) / dz + offset_ocean_nzt
-             IF ( i == ip )  sort_index = sort_index+4
-             IF ( j == jp )  sort_index = sort_index+2
-             IF ( k == kp )  sort_index = sort_index+1
-             sort_count(sort_index) = sort_count(sort_index)+1
+!
+!--          Sorting particles with a binary scheme
+!--          sort_index=111_2=7_10 -> particle at the left,south,bottom subgridbox
+!--          sort_index=000_2=0_10 -> particle at the right,north,top subgridbox
+!--          For this the center of the gridbox is calculated 
+             i = (particles(n)%x + 0.5_wp * dx) * ddx
+             j = (particles(n)%y + 0.5_wp * dy) * ddy
+             k = ( particles(n)%z+ 0.5_wp *dz ) / dz+1 + offset_ocean_nzt
+                      
+             IF ( i == ip )  sort_index = sort_index + 4 
+             IF ( j == jp )  sort_index = sort_index + 2
+             IF ( k == kp )  sort_index = sort_index + 1
+                      
+             sort_count(sort_index) = sort_count(sort_index) + 1
              m = sort_count(sort_index)
              sort_particles(m,sort_index) = particles(n)
@@ -236,15 +254,14 @@
           grid_particles(kp,jp,ip)%start_index(is) = nn + 1
           DO n = 1,sort_count(is)
-             nn = nn+1
+             nn = nn + 1
              particles(nn) = sort_particles(n,is)
-          ENDDO
-          grid_particles(kp,jp,ip)%end_index(is) = nn
-       ENDDO
-
-       number_of_particles = nn
-       RETURN
-
+             ENDDO
+             grid_particles(kp,jp,ip)%end_index(is) = nn
+       ENDDO
+
+       number_of_particles = nn    
+                
     END SUBROUTINE lpm_pack_and_sort
-
+                
 !------------------------------------------------------------------------------!
 ! Description:
@@ -254,5 +271,5 @@
 !> complete the LES timestep. 
 !------------------------------------------------------------------------------!
-    SUBROUTINE lpm_sort
+    SUBROUTINE lpm_sort_timeloop_done
 
        USE control_parameters,                                                 &
@@ -342,6 +359,6 @@
        ENDDO
 
-    END SUBROUTINE lpm_sort
-
-
- END module lpm_pack_arrays_mod
+    END SUBROUTINE lpm_sort_timeloop_done
+
+
+ END module lpm_pack_and_sort_mod
