c***********************************************************************
c     ssbox_retbox.f
c     boundary treatment
c     retbox      = final em-boundaries = cleaned version of retbox_mod1

C***********************************************************************

          SUBROUTINE RETBOX
                                                                        
C***********************************************************************
                                                                        
          INCLUDE 'ssbox.inc'                                       
                                                                        
C ******  RETURNS PARTICLES INTO CALCULATION BOX                        
C         WHICH EXTENDS -XL -> +XL                                
C                       -YL -> +YL                                
                                                                        
C         AVAK=2*xl,CVAK=2*yl = BOX-SIZE IN X,Y
C         BVAK=SHEAR IN TANGENTIAL COORDINATE                           
C         VVAK=CORRESPONDING VELOCITY CHANGE                            
c         particle images:  n,m integers (-1,0,1)
c          x_i  =  x + n*avak
c          y_i  =  y + n*bvak + m*cvak
c          vx_i = vx
c          vy_i = vy + n*vvak
                                                                        
C ******  ALSO, KEEPS TRACK OF NUMBER OF CROSSING PARTICLES             
   

          CALL CPU(TIME1)

          AVAK=2.D0*XL                                                  
          CVAK=2.D0*YL                                                  
          BVAK=(KAPPA2-4.D0*OMEGA2)/OMEGA*XL*TIME                       
c------------------------------------------------                       
c         added 27.4.92 |                                               
          bvak=-bvak                                                    
          bvak=bvak-int(bvak/2./yl)*2.*yl                               
          bvak=-bvak                                                    
          if(bvak.gt.yl) bvak=bvak-2.*yl                                
          if(bvak.lt.-yl) bvak=bvak+2.*yl                               
          if(ites.ge.1.or.isis.ge.1) write(idev6,*) 'bvak',bvak
c------------------------------------------------                       
          VVAK=(KAPPA2-4.D0*OMEGA2)/OMEGA*XL                            
                

             DO 10 I=1,LKM
                N=0                                                           
                M=0                                                           
                XX=R(I,1)-XL0                                                 
                IF(XX.GE.0) N=-INT(XX/AVAK+0.5)                               
                IF(XX.LT.0) N=INT(-XX/AVAK+0.5)                               
                R(I,1)=R(I,1)+N*AVAK                                          
                R(I,2)=R(I,2)+N*BVAK                                          
                YY=R(I,2)-YL0                                                 
                IF(YY.GE.0) M=-INT(YY/CVAK+0.5)                               
                IF(YY.LT.0) M=INT(-YY/CVAK+0.5)                               
                R(I,2)=R(I,2)+M*CVAK                                          
                RP(I,2)=RP(I,2)+N*VVAK                                        
                        
                IF(N.GE.1) THEN
                   NHA=NHA+1   
                   ENES=ENES+DENE   
                ENDIF
                IF(N.LE.-1) THEN
                   NHY=NHY+1
                   ENEU=ENEU+DENE
                ENDIF
                IF(M.GE.1) NPA=NPA+1                                          
                IF(M.LE.-1) NPY=NPY+1                                         
                
 10          CONTINUE                                                      

          CALL CPU(TIME2)                                               
          CPU_RET=CPU_RET+TIME2-TIME1                                     
                                                                        
          RETURN                                                        
          END
