subroutine Kick_Migration    !  is called in subroutine CirculationCodeSelect
    
        Use GeoElements_FluidModule
        USE CMudPropertiesVariables
        USE MudSystemVARIABLES
use SimulationVariables !@@@
        use SimulationVariables
        use SimulationVariables !@
        !use CTanks
    !@use ConfigurationVariables, TripTankVolume2 => data%EquipmentControl%DrillingWatch%TripTankVolume, TripTankDensity2 => TripTankDensity
        USE sROP_Other_Variables
        USE sROP_Variables
        USE CReservoirVariables
        use KickVARIABLESModule

   
   implicit none
   
   integer jelement, jmud, jsection,ielement,i,kickNumber
   integer jopelement,jopmud,jopsection,CuttingValue
   
        
        !data%State%MUD(2)%Q= data%State%MPumps%Total_Pump_GPM
        !data%State%MudSystem%StringFlowRate= data%State%MUD(2)%Q
        !data%State%MudSystem%AnnulusFlowRate= data%State%MUD(2)%Q
        !
        !if (NewPipeFilling == 0) then
        !    data%State%MudSystem%StringFlowRate= 0.
        !    data%State%MudSystem%AnnulusFlowRate= 0.
        !endif  
        
        !StringFlowRateFinal= data%State%MudSystem%StringFlowRate
        !AnnulusFlowRateFinal= data%State%MudSystem%AnnulusFlowRate
        
        
        
        !write(*,*) 'data%State%MUD(2)%Q=====' , data%State%MUD(2)%Q
            !write(*,*) 'Kick Migration,NewInfluxNumber:' , NewInfluxNumber
            
   
   
       !FirstSetKickMigration
            !write(*,*) 'NewInfluxNumber=' , NewInfluxNumber
DO KickNumber= data%State%MudSystem%NewInfluxNumber-KickVARIABLES%NoGasPocket+1 , data%State%MudSystem%NewInfluxNumber
    !write(*,*) 'KickNumber=' , KickNumber
    if (KickVARIABLES%KickFlux .AND. NOT(KickVARIABLES%KickOffBottom) .and. KickNumber == data%State%MudSystem%NewInfluxNumber) cycle
    if ( KickNumber == data%State%MudSystem%Ann_MudOrKick%Last() ) cycle ! when the last element in Annulus is kick, Migration is not called
    !write(*,*) 'Migration will be done for,KickNumber=' ,KickNumber

    !=================== Bottom Hole ENTRANCE(due to Kick) ===================    
    !KickDx= (AutoMigrationRate/60.)*DeltaT_Mudline    !3600 (ft/min)= 6 ft     set in start up
    
    
    data%State%MudSystem%Op_KickLoc= 0
    data%State%MudSystem%Ann_KickLoc= 0
    data%State%MudSystem%ChokeLine_KickLoc= 0
        
    do i = 1, data%State%MudSystem%Op_MudOrKick%Length ()
        if (data%State%MudSystem%Op_MudOrKick%Array(i) == KickNumber) then
            data%State%MudSystem%Op_KickLoc = i
            exit
        endif
    end do
        
    do i = 1, data%State%MudSystem%Ann_MudOrKick%Length ()
        if (data%State%MudSystem%Ann_MudOrKick%Array(i) == KickNumber) then
            data%State%MudSystem%Ann_KickLoc = i
            exit
        endif
    end do  
    
    do i = 1, data%State%MudSystem%ChokeLine_MudOrKick%Length ()
        if (data%State%MudSystem%ChokeLine_MudOrKick%Array(i) == KickNumber) then
            data%State%MudSystem%ChokeLine_KickLoc = i
            exit
        endif
    end do      
        
        !write(*,*) 'Op_KickLoc=' , Op_KickLoc
        !write(*,*) 'Ann_KickLoc=' , Ann_KickLoc
        !!write(*,*) 'ChokeLine_KickLoc=' , ChokeLine_KickLoc
        !
        !
        !write(*,*) 'Op_MudOrKick%Length ()=' , Op_MudOrKick%Length ()
        !
        !
        
        
        
        
!============================== foreheade kick be mate reside bashad *3 ==============================
            
if (data%State%MudSystem%Op_KickLoc == data%State%MudSystem%Op_MudOrKick%Length () .and. data%State%MudSystem%Ann_KickLoc==0 ) then
     !write(*,*) '****3'
     
     data%State%MudSystem%iLoc= 2
     
     data%State%MudSystem%KickDv= data%State%MudSystem%Area_OpSectionFt(data%State%MudSystem%Op_Mud_Forehead_section%Array(data%State%MudSystem%Op_KickLoc)) * data%State%MudSystem%KickDx * 7.48051948d0        ! ft^3  to gal

 
        data%State%MudSystem%MinKickDv= min( data%State%MudSystem%KickDv,data%State%MudSystem%Ann_MudDischarged_Volume%Array (1), data%State%MudSystem%Op_MudDischarged_Volume%Last () )
        
        data%State%MudSystem%NewDensity= data%State%MudSystem%Ann_Density%Array (1)
        data%State%MudSystem%NewVolume= data%State%MudSystem%MinKickDv  
        
      if ( data%State%MudSystem%MinKickDv == data%State%MudSystem%KickDv ) then    !eleman bala sari baghi mimund, paeeni(kick) ham baghi mimund
        
                                data%State%MudSystem%Ann_MudDischarged_Volume%Array (1)= data%State%MudSystem%Ann_MudDischarged_Volume%Array (1) - data%State%MudSystem%MinKickDv
                    
                                call data%State%MudSystem%Ann_Density%AddToFirst (data%State%MudSystem%Op_Density%Last())
                                call data%State%MudSystem%Ann_MudDischarged_Volume%AddToFirst (data%State%MudSystem%MinKickDv)
                                call data%State%MudSystem%Ann_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(data%State%F_Counts%StringIntervalCounts+1))
                                call data%State%MudSystem%Ann_Mud_Forehead_section%AddToFirst (data%State%F_Counts%StringIntervalCounts+1)
                                call data%State%MudSystem%Ann_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(data%State%F_Counts%StringIntervalCounts+1))
                                call data%State%MudSystem%Ann_Mud_Backhead_section%AddToFirst (data%State%F_Counts%StringIntervalCounts+1)
                                call data%State%MudSystem%Ann_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
                                call data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0) 
                                call data%State%MudSystem%Ann_MudOrKick%AddToFirst (KickNumber)
                                call data%State%MudSystem%Ann_CuttingMud%AddToFirst (0)
            
                                data%State%MudSystem%Op_MudDischarged_Volume%Array (data%State%MudSystem%Op_KickLoc)= data%State%MudSystem%Op_MudDischarged_Volume%Array (data%State%MudSystem%Op_KickLoc) - data%State%MudSystem%MinKickDv
            
             ! backheade kick zire mate bashad          
             if (data%State%MudSystem%Op_KickLoc > 1) then
                  !if ( Op_Density%Array (Op_KickLoc-1) /= data%State%MudSystem%NewDensity ) then  
              
                        if ( ABS(data%State%MudSystem%Op_Density%Array (data%State%MudSystem%Op_KickLoc-1) - data%State%MudSystem%NewDensity) >= data%State%MudSystem%DensityMixTol ) then
          
          
                            data%State%MudSystem%Old_KickBackHead_X= data%State%MudSystem%Op_Mud_Backhead_X%Array (data%State%MudSystem%Op_KickLoc)
                            data%State%MudSystem%Old_KickBackHead_Section= data%State%MudSystem%Op_Mud_Backhead_section%Array (data%State%MudSystem%Op_KickLoc)
            
                            call data%State%MudSystem%Op_Density%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%NewDensity)
                            call data%State%MudSystem%Op_MudDischarged_Volume%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%NewVolume)
                            call data%State%MudSystem%Op_Mud_Forehead_X%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_X)
                            call data%State%MudSystem%Op_Mud_Forehead_section%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_Section)
                            call data%State%MudSystem%Op_Mud_Backhead_X%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_X)
                            call data%State%MudSystem%Op_Mud_Backhead_section%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_Section)
                            call data%State%MudSystem%Op_RemainedVolume_in_LastSection%AddTo (data%State%MudSystem%Op_KickLoc,0.0d0)
                            call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%AddTo (data%State%MudSystem%Op_KickLoc,0.0d0)
                            call data%State%MudSystem%Op_MudOrKick%AddTo (data%State%MudSystem%Op_KickLoc,0)
        
            
                        else  !Op_Density%Array (imudKick-1) == data%State%MudSystem%NewDensity
          
                            data%State%MudSystem%Op_Density%Array(data%State%MudSystem%Op_KickLoc-1)= (data%State%MudSystem%Op_Density%Array(data%State%MudSystem%Op_KickLoc-1)*data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1)+data%State%MudSystem%NewDensity*data%State%MudSystem%NewVolume)/(data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1)+data%State%MudSystem%NewVolume)
                            data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1)= data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1) + data%State%MudSystem%NewVolume
            
                        endif  
             else       !if Op_KickLoc == 1     (*****Migration Start*****)  *3-1===============================
                !write(*,*) '****3-1'
                 

                        call data%State%MudSystem%Op_Density%AddToFirst (data%State%MudSystem%NewDensity)
                        call data%State%MudSystem%Op_MudDischarged_Volume%AddToFirst (data%State%MudSystem%NewVolume)
                        call data%State%MudSystem%Op_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Old_KickBackHead_X)                                
                        call data%State%MudSystem%Op_Mud_Forehead_section%AddToFirst (data%State%MudSystem%Old_KickBackHead_Section)
                        call data%State%MudSystem%Op_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Old_KickBackHead_X)
                        call data%State%MudSystem%Op_Mud_Backhead_section%AddToFirst (data%State%MudSystem%Old_KickBackHead_Section)
                        call data%State%MudSystem%Op_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
                        call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
                        call data%State%MudSystem%Op_MudOrKick%AddToFirst (0)
                
             endif                                
                                
                      
            
     elseif ( data%State%MudSystem%MinKickDv == data%State%MudSystem%Ann_MudDischarged_Volume%Array (1) ) then  ! eleman bala sari baghi nemimund

                                data%State%MudSystem%Ann_Density%Array(1)= data%State%MudSystem%Op_Density%Last()
                                data%State%MudSystem%Ann_MudOrKick%Array(1)= KickNumber
            
                                data%State%MudSystem%Op_MudDischarged_Volume%Array (data%State%MudSystem%Op_KickLoc)= data%State%MudSystem%Op_MudDischarged_Volume%Array (data%State%MudSystem%Op_KickLoc) - data%State%MudSystem%MinKickDv
            
                                 ! backheade kick zire mate bashad 
                if (data%State%MudSystem%Op_KickLoc > 1) then
                                
                        !if ( Op_Density%Array (Op_KickLoc-1) /= data%State%MudSystem%NewDensity ) then 
                                    
                        if ( ABS(data%State%MudSystem%Op_Density%Array (data%State%MudSystem%Op_KickLoc-1) - data%State%MudSystem%NewDensity) >= data%State%MudSystem%DensityMixTol ) then
          
          
                            data%State%MudSystem%Old_KickBackHead_X= data%State%MudSystem%Op_Mud_Backhead_X%Array (data%State%MudSystem%Op_KickLoc)
                            data%State%MudSystem%Old_KickBackHead_Section= data%State%MudSystem%Op_Mud_Backhead_section%Array (data%State%MudSystem%Op_KickLoc)
            
                            call data%State%MudSystem%Op_Density%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%NewDensity)
                            call data%State%MudSystem%Op_MudDischarged_Volume%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%NewVolume)
                            call data%State%MudSystem%Op_Mud_Forehead_X%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_X)
                            call data%State%MudSystem%Op_Mud_Forehead_section%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_Section)
                            call data%State%MudSystem%Op_Mud_Backhead_X%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_X)
                            call data%State%MudSystem%Op_Mud_Backhead_section%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_Section)
                            call data%State%MudSystem%Op_RemainedVolume_in_LastSection%AddTo (data%State%MudSystem%Op_KickLoc,0.0d0)
                            call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%AddTo (data%State%MudSystem%Op_KickLoc,0.0d0)
                            call data%State%MudSystem%Op_MudOrKick%AddTo (data%State%MudSystem%Op_KickLoc,0)
        
            
                        else  !Op_Density%Array (imudKick-1) == data%State%MudSystem%NewDensity
          
                            data%State%MudSystem%Op_Density%Array(data%State%MudSystem%Op_KickLoc-1)= (data%State%MudSystem%Op_Density%Array(data%State%MudSystem%Op_KickLoc-1)*data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1)+data%State%MudSystem%NewDensity*data%State%MudSystem%NewVolume)/(data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1)+data%State%MudSystem%NewVolume)
                            data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1)= data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1) + data%State%MudSystem%NewVolume
            
                        endif             
            
                else       !if Op_KickLoc == 1     (*****Migration Start*****)  *3-2===============================
                !write(*,*) '****3-2'
                    

                        call data%State%MudSystem%Op_Density%AddToFirst (data%State%MudSystem%NewDensity)
                        call data%State%MudSystem%Op_MudDischarged_Volume%AddToFirst (data%State%MudSystem%NewVolume)
                        call data%State%MudSystem%Op_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Old_KickBackHead_X)                                
                        call data%State%MudSystem%Op_Mud_Forehead_section%AddToFirst (data%State%MudSystem%Old_KickBackHead_Section)
                        call data%State%MudSystem%Op_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Old_KickBackHead_X)
                        call data%State%MudSystem%Op_Mud_Backhead_section%AddToFirst (data%State%MudSystem%Old_KickBackHead_Section)
                        call data%State%MudSystem%Op_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
                        call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
                        call data%State%MudSystem%Op_MudOrKick%AddToFirst (0)
                
             endif             
            
            
 !           
     elseif ( data%State%MudSystem%MinKickDv == data%State%MudSystem%Op_MudDischarged_Volume%Last () ) then  ! eleman balaee baghi mimund, kick hazf mishod
         
                    data%State%MudSystem%Ann_MudDischarged_Volume%Array (1)= data%State%MudSystem%Ann_MudDischarged_Volume%Array (1) - data%State%MudSystem%MinKickDv     
            
                    call data%State%MudSystem%Ann_Density%AddToFirst (data%State%MudSystem%Op_Density%Last())
                    call data%State%MudSystem%Ann_MudDischarged_Volume%AddToFirst (data%State%MudSystem%MinKickDv)
                    call data%State%MudSystem%Ann_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(data%State%F_Counts%StringIntervalCounts+1))
                    call data%State%MudSystem%Ann_Mud_Forehead_section%AddToFirst (data%State%F_Counts%StringIntervalCounts+1)
                    call data%State%MudSystem%Ann_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(data%State%F_Counts%StringIntervalCounts+1))
                    call data%State%MudSystem%Ann_Mud_Backhead_section%AddToFirst (data%State%F_Counts%StringIntervalCounts+1)
                    call data%State%MudSystem%Ann_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
                    call data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0) 
                    call data%State%MudSystem%Ann_MudOrKick%AddToFirst (KickNumber)  
                    call data%State%MudSystem%Ann_CuttingMud%AddToFirst (0)
         
                                
                    data%State%MudSystem%Old_KickBackHead_X= data%State%MudSystem%Op_Mud_Backhead_X%Array (data%State%MudSystem%Op_KickLoc)
                    data%State%MudSystem%Old_KickBackHead_Section= data%State%MudSystem%Op_Mud_Backhead_section%Array (data%State%MudSystem%Op_KickLoc)
                                
                    call data%State%MudSystem%Op_MudDischarged_Volume%Remove (data%State%MudSystem%Op_KickLoc)
                    call data%State%MudSystem%Op_Mud_Backhead_X%Remove (data%State%MudSystem%Op_KickLoc)
                    call data%State%MudSystem%Op_Mud_Backhead_section%Remove (data%State%MudSystem%Op_KickLoc)
                    call data%State%MudSystem%Op_Mud_Forehead_X%Remove (data%State%MudSystem%Op_KickLoc)
                    call data%State%MudSystem%Op_Mud_Forehead_section%Remove (data%State%MudSystem%Op_KickLoc)
                    call data%State%MudSystem%Op_Density%Remove (data%State%MudSystem%Op_KickLoc)
                    call data%State%MudSystem%Op_RemainedVolume_in_LastSection%Remove (data%State%MudSystem%Op_KickLoc)
                    call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%Remove (data%State%MudSystem%Op_KickLoc)   
                    call data%State%MudSystem%Op_MudOrKick%Remove (data%State%MudSystem%Op_KickLoc)   
                                
                                
                        ! backheade kick zire mate bashad 
                if (data%State%MudSystem%Op_KickLoc > 1) then
                    !if ( Op_Density%Array (Op_KickLoc-1) /= data%State%MudSystem%NewDensity ) then 
                                    
                    if ( ABS(data%State%MudSystem%Op_Density%Array (data%State%MudSystem%Op_KickLoc-1) - data%State%MudSystem%NewDensity) >= data%State%MudSystem%DensityMixTol ) then
          
        
            
                        call data%State%MudSystem%Op_Density%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%NewDensity)
                        call data%State%MudSystem%Op_MudDischarged_Volume%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%NewVolume)
                        call data%State%MudSystem%Op_Mud_Forehead_X%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_X)
                        call data%State%MudSystem%Op_Mud_Forehead_section%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_Section)
                        call data%State%MudSystem%Op_Mud_Backhead_X%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_X)
                        call data%State%MudSystem%Op_Mud_Backhead_section%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_Section)
                        call data%State%MudSystem%Op_RemainedVolume_in_LastSection%AddTo (data%State%MudSystem%Op_KickLoc,0.0d0)
                        call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%AddTo (data%State%MudSystem%Op_KickLoc,0.0d0)
                        call data%State%MudSystem%Op_MudOrKick%AddTo (data%State%MudSystem%Op_KickLoc,0)
        
            
                    else  !Op_Density%Array (imudKick-1) == data%State%MudSystem%NewDensity
          
                        data%State%MudSystem%Op_Density%Array(data%State%MudSystem%Op_KickLoc-1)= (data%State%MudSystem%Op_Density%Array(data%State%MudSystem%Op_KickLoc-1)*data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1)+data%State%MudSystem%NewDensity*data%State%MudSystem%NewVolume)/(data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1)+data%State%MudSystem%NewVolume)
                        data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1)= data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1) + data%State%MudSystem%NewVolume
            
                    endif   
                            
             else       !if Op_KickLoc == 1     (*****Migration Start*****)  *3-3===============================
                !write(*,*) '****3-3'

                    call data%State%MudSystem%Op_Density%AddToFirst (data%State%MudSystem%NewDensity)
                    call data%State%MudSystem%Op_MudDischarged_Volume%AddToFirst (data%State%MudSystem%NewVolume)
                    call data%State%MudSystem%Op_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Old_KickBackHead_X)                                
                    call data%State%MudSystem%Op_Mud_Forehead_section%AddToFirst (data%State%MudSystem%Old_KickBackHead_Section)
                    call data%State%MudSystem%Op_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Old_KickBackHead_X)
                    call data%State%MudSystem%Op_Mud_Backhead_section%AddToFirst (data%State%MudSystem%Old_KickBackHead_Section)
                    call data%State%MudSystem%Op_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
                    call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
                    call data%State%MudSystem%Op_MudOrKick%AddToFirst (0)
                
             endif   
                            
                            
                            
            
     endif
     
         
 endif      
 
!========================================================================================         

 
!============================== foreheade kick be mate reside bashad *3 with pump ==============================
            
! if (Op_KickLoc == Op_MudOrKick%Length () .and. Ann_KickLoc==0 .and. data%State%MudSystem%AnnulusFlowRate /= 0.0 ) then
!     write(*,*) '****3 with pump'
!     
!     KickMigration_2SideBit= .true.
!     !iloc= 2
!     
!     !KickDv= Area_OpSectionFt(Op_Mud_Forehead_section%Array(Op_KickLoc)) * KickDx * 7.48051948        ! ft^3  to gal
!     
!!farz mikonam baraye in yek iteration kick az OP hazf nemishavad va hajme aan bishtar az pump flow ast
!
!            KickDv= ((data%State%MudSystem%AnnulusFlowRate/60.0d0)*DeltaT_Mudline)
!        
!            call Ann_Density%AddToFirst (Kick_Density)
!            call Ann_MudDischarged_Volume%AddToFirst (KickDv)
!            call Ann_Mud_Forehead_X%AddToFirst (Xstart_PipeSection(data%State%F_Counts%StringIntervalCounts+1))
!            call Ann_Mud_Forehead_section%AddToFirst (F_StringIntervalCounts+1)
!            call Ann_Mud_Backhead_X%AddToFirst (Xstart_PipeSection(F_StringIntervalCounts+1))
!            call Ann_Mud_Backhead_section%AddToFirst (F_StringIntervalCounts+1)
!            call Ann_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
!            call Ann_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0) 
!            call Ann_MudOrKick%AddToFirst (KickNumber) 
!            call Ann_CuttingMud%AddToFirst (0) 
!            
!            Op_MudDischarged_Volume%Array (Op_KickLoc)= Op_MudDischarged_Volume%Array (Op_KickLoc) - MinKickDv
!            
!        !
!        !if ( ((data%State%MudSystem%AnnulusFlowRate/60.)*DeltaT_Mudline) >= KickDv ) then    ! sorate pump bishtar az kick bashad
!        !    
!        !    KickDv= ((data%State%MudSystem%AnnulusFlowRate/60.)*DeltaT_Mudline)
!            
!            
!
!            ! BackHead:
!            if ( Op_Density%Array (Op_KickLoc-1) /=data%State%MudSystem%St_Density%Last() ) then   
!          
!          
!                Old_KickBackHead_X= Op_Mud_Backhead_X%Array (Op_KickLoc)
!                Old_KickBackHead_Section= Op_Mud_Backhead_section%Array (Op_KickLoc)
!            
!                call Op_Density%AddTo (Op_KickLocMudSystem%St_Density%Last())
!                call Op_MudDischarged_Volume%AddTo (Op_KickLoc,KickDv)
!                call Op_Mud_Forehead_X%AddTo (Op_KickLoc,Old_KickBackHead_X)
!                call Op_Mud_Forehead_section%AddTo (Op_KickLoc,Old_KickBackHead_Section)
!                call Op_Mud_Backhead_X%AddTo (Op_KickLoc,Old_KickBackHead_X)
!                call Op_Mud_Backhead_section%AddTo (Op_KickLoc,Old_KickBackHead_Section)
!                call Op_RemainedVolume_in_LastSection%AddTo (Op_KickLoc,0.0d0)
!                call Op_EmptyVolume_inBackheadLocation%AddTo (Op_KickLoc,0.0d0)
!                call Op_MudOrKick%AddTo (Op_KickLoc,0)
!        
!            
!            else  !Op_Density%Array (imudKick-1) == data%State%MudSystem%NewDensity
!          
!                Op_MudDischarged_Volume%Array (Op_KickLoc-1)= Op_MudDischarged_Volume%Array (Op_KickLoc-1) + KickDv
!            
!            endif         
!        
!
!     
!         
! endif      
 
!========================================================================================  
 
 
!=============================    tamame kick zire mate bashad  *1 ================================
 if ( data%State%MudSystem%Op_KickLoc>0 .and. data%State%MudSystem%Op_KickLoc < data%State%MudSystem%Op_MudOrKick%Length () ) then
     !write(*,*) '****1'
     !iloc= 1
     
     data%State%MudSystem%KickDv= data%State%MudSystem%Area_OpSectionFt(data%State%MudSystem%Op_Mud_Forehead_section%Array(data%State%MudSystem%Op_KickLoc)) * data%State%MudSystem%KickDx * 7.48051948d0        ! ft^3  to gal
     
     
        data%State%MudSystem%Old_KickBackHead_X= data%State%MudSystem%Op_Mud_Backhead_X%Array (data%State%MudSystem%Op_KickLoc)
        data%State%MudSystem%Old_KickBackHead_Section= data%State%MudSystem%Op_Mud_Backhead_section%Array (data%State%MudSystem%Op_KickLoc)     
     
     if ( data%State%MudSystem%KickDv < data%State%MudSystem%Op_MudDischarged_Volume%Array (data%State%MudSystem%Op_KickLoc+1) ) then    !eleman bala sari baghi mimund
         
         !write(*,*) 'cond 11111111111111'
     
            data%State%MudSystem%Op_MudDischarged_Volume%Array (data%State%MudSystem%Op_KickLoc+1)= data%State%MudSystem%Op_MudDischarged_Volume%Array (data%State%MudSystem%Op_KickLoc+1) - data%State%MudSystem%KickDv
        
            data%State%MudSystem%NewDensity= data%State%MudSystem%Op_Density%Array (data%State%MudSystem%Op_KickLoc+1)
            data%State%MudSystem%NewVolume= data%State%MudSystem%KickDv   
            
     else   !KickDv > Op_MudDischarged_Volume%Array (imudKick+1) eleman baghi nemimund
         
        ! write(*,*) 'cond 22222222222222222'
         
         
            data%State%MudSystem%KickDv= data%State%MudSystem%Op_MudDischarged_Volume%Array (data%State%MudSystem%Op_KickLoc+1)
        
            data%State%MudSystem%NewVolume= data%State%MudSystem%KickDv
            data%State%MudSystem%NewDensity= data%State%MudSystem%Op_Density%Array (data%State%MudSystem%Op_KickLoc+1)     
     
   
            
            
            call data%State%MudSystem%Op_MudDischarged_Volume%Remove (data%State%MudSystem%Op_KickLoc+1)
            call data%State%MudSystem%Op_Mud_Backhead_X%Remove (data%State%MudSystem%Op_KickLoc+1)
            call data%State%MudSystem%Op_Mud_Backhead_section%Remove (data%State%MudSystem%Op_KickLoc+1)
            call data%State%MudSystem%Op_Mud_Forehead_X%Remove (data%State%MudSystem%Op_KickLoc+1)
            call data%State%MudSystem%Op_Mud_Forehead_section%Remove (data%State%MudSystem%Op_KickLoc+1)
            call data%State%MudSystem%Op_Density%Remove (data%State%MudSystem%Op_KickLoc+1)
            call data%State%MudSystem%Op_RemainedVolume_in_LastSection%Remove (data%State%MudSystem%Op_KickLoc+1)
            call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%Remove (data%State%MudSystem%Op_KickLoc+1)   
            call data%State%MudSystem%Op_MudOrKick%Remove (data%State%MudSystem%Op_KickLoc+1)     
            
     endif
     
      ! backheade kick zire mate bashad 
     if (data%State%MudSystem%Op_KickLoc > 1) then
          !if ( Op_Density%Array (Op_KickLoc-1) /= data%State%MudSystem%NewDensity ) then  
              
          if ( ABS(data%State%MudSystem%Op_Density%Array (data%State%MudSystem%Op_KickLoc-1) - data%State%MudSystem%NewDensity) >= data%State%MudSystem%DensityMixTol ) then
            

          
          
                call data%State%MudSystem%Op_Density%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%NewDensity)
                call data%State%MudSystem%Op_MudDischarged_Volume%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%NewVolume)
                call data%State%MudSystem%Op_Mud_Forehead_X%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_X)                                
                call data%State%MudSystem%Op_Mud_Forehead_section%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_Section)
                call data%State%MudSystem%Op_Mud_Backhead_X%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_X)
                call data%State%MudSystem%Op_Mud_Backhead_section%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_Section)
                call data%State%MudSystem%Op_RemainedVolume_in_LastSection%AddTo (data%State%MudSystem%Op_KickLoc,0.0d0)
                call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%AddTo (data%State%MudSystem%Op_KickLoc,0.0d0)
                call data%State%MudSystem%Op_MudOrKick%AddTo (data%State%MudSystem%Op_KickLoc,0)
            
          else  !Op_Density%Array (Op_KickLoc-1) == data%State%MudSystem%NewDensity
          
                data%State%MudSystem%Op_Density%Array(data%State%MudSystem%Op_KickLoc-1)= (data%State%MudSystem%Op_Density%Array(data%State%MudSystem%Op_KickLoc-1)*data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1)+data%State%MudSystem%NewDensity*data%State%MudSystem%NewVolume)/(data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1)+data%State%MudSystem%NewVolume)
                data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1)= data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1) + data%State%MudSystem%NewVolume
            
          endif
     else       !if Op_KickLoc == 1     (*****Migration Start*****)  *5-1===============================
     !write(*,*) '****5-1'

                call data%State%MudSystem%Op_Density%AddToFirst (data%State%MudSystem%NewDensity)
                call data%State%MudSystem%Op_MudDischarged_Volume%AddToFirst (data%State%MudSystem%NewVolume)
                call data%State%MudSystem%Op_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Old_KickBackHead_X)                                
                call data%State%MudSystem%Op_Mud_Forehead_section%AddToFirst (data%State%MudSystem%Old_KickBackHead_Section)
                call data%State%MudSystem%Op_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Old_KickBackHead_X)
                call data%State%MudSystem%Op_Mud_Backhead_section%AddToFirst (data%State%MudSystem%Old_KickBackHead_Section)
                call data%State%MudSystem%Op_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
                call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
                call data%State%MudSystem%Op_MudOrKick%AddToFirst (0)
                
     endif
     
     
 endif

!======================================================================================== 
 
            !write(*,*) 'a) density and cutting:' , Ann_Density%Length() , Ann_CuttingMud%Length()
 
 
!===========================  tamame kick balaye mate bashad *2 ================================== 
 
 if ( data%State%MudSystem%Ann_KickLoc > 0 .and.  data%State%MudSystem%Op_KickLoc==0 ) then
     !write(*,*) '****2'
     
     !iloc= 1
     
     data%State%MudSystem%KickDv= data%State%MudSystem%Area_PipeSectionFt(data%State%MudSystem%Ann_Mud_Forehead_section%Array(data%State%MudSystem%Ann_KickLoc)) * data%State%MudSystem%KickDx * 7.48051948d0        ! ft^3  to gal
     
     data%State%MudSystem%NewDensity= data%State%MudSystem%Ann_Density%Array (data%State%MudSystem%Ann_KickLoc+1)
     data%State%MudSystem%NewVolume= data%State%MudSystem%KickDv
     CuttingValue= data%State%MudSystem%Ann_CuttingMud%Array (data%State%MudSystem%Ann_KickLoc+1)
     
     if ( data%State%MudSystem%KickDv < data%State%MudSystem%Ann_MudDischarged_Volume%Array (data%State%MudSystem%Ann_KickLoc+1) ) then    !eleman bala sari baghi mimund
     
            data%State%MudSystem%Ann_MudDischarged_Volume%Array (data%State%MudSystem%Ann_KickLoc+1)= data%State%MudSystem%Ann_MudDischarged_Volume%Array (data%State%MudSystem%Ann_KickLoc+1) - data%State%MudSystem%KickDv
            
     else   !KickDv > Ann_MudDischarged_Volume%Array (imudKick+1) eleman baghi nemimund
         
            data%State%MudSystem%KickDv= data%State%MudSystem%Ann_MudDischarged_Volume%Array (data%State%MudSystem%Ann_KickLoc+1)  
            data%State%MudSystem%NewVolume= data%State%MudSystem%KickDv
     
            call data%State%MudSystem%Ann_MudDischarged_Volume%Remove (data%State%MudSystem%Ann_KickLoc+1)
            call data%State%MudSystem%Ann_Mud_Backhead_X%Remove (data%State%MudSystem%Ann_KickLoc+1)
            call data%State%MudSystem%Ann_Mud_Backhead_section%Remove (data%State%MudSystem%Ann_KickLoc+1)
            call data%State%MudSystem%Ann_Mud_Forehead_X%Remove (data%State%MudSystem%Ann_KickLoc+1)
            call data%State%MudSystem%Ann_Mud_Forehead_section%Remove (data%State%MudSystem%Ann_KickLoc+1)
            call data%State%MudSystem%Ann_Density%Remove (data%State%MudSystem%Ann_KickLoc+1)
            call data%State%MudSystem%Ann_RemainedVolume_in_LastSection%Remove (data%State%MudSystem%Ann_KickLoc+1)
            call data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Remove (data%State%MudSystem%Ann_KickLoc+1) 
            call data%State%MudSystem%Ann_MudOrKick%Remove (data%State%MudSystem%Ann_KickLoc+1) 
            call data%State%MudSystem%Ann_CuttingMud%Remove (data%State%MudSystem%Ann_KickLoc+1) 
            
     endif

     ! backheade kick balaye mate bashad 
     
     if (data%State%MudSystem%Ann_KickLoc > 1) then
          !if ( Ann_Density%Array (Ann_KickLoc-1) /= data%State%MudSystem%NewDensity ) then  
              
          if ( ABS(data%State%MudSystem%Ann_Density%Array (data%State%MudSystem%Ann_KickLoc-1) - data%State%MudSystem%NewDensity) >= data%State%MudSystem%DensityMixTol ) then
    
              data%State%MudSystem%Old_KickBackHead_X= data%State%MudSystem%Ann_Mud_Backhead_X%Array (data%State%MudSystem%Ann_KickLoc)
              data%State%MudSystem%Old_KickBackHead_Section= data%State%MudSystem%Ann_Mud_Backhead_section%Array (data%State%MudSystem%Ann_KickLoc)
          
          
                call data%State%MudSystem%Ann_Density%AddTo (data%State%MudSystem%Ann_KickLoc,data%State%MudSystem%NewDensity)
                call data%State%MudSystem%Ann_MudDischarged_Volume%AddTo (data%State%MudSystem%Ann_KickLoc,data%State%MudSystem%NewVolume)
                call data%State%MudSystem%Ann_Mud_Forehead_X%AddTo (data%State%MudSystem%Ann_KickLoc,data%State%MudSystem%Old_KickBackHead_X)
                call data%State%MudSystem%Ann_Mud_Forehead_section%AddTo (data%State%MudSystem%Ann_KickLoc,data%State%MudSystem%Old_KickBackHead_Section)
                call data%State%MudSystem%Ann_Mud_Backhead_X%AddTo (data%State%MudSystem%Ann_KickLoc,data%State%MudSystem%Old_KickBackHead_X)
                call data%State%MudSystem%Ann_Mud_Backhead_section%AddTo (data%State%MudSystem%Ann_KickLoc,data%State%MudSystem%Old_KickBackHead_Section)
                call data%State%MudSystem%Ann_RemainedVolume_in_LastSection%AddTo (data%State%MudSystem%Ann_KickLoc,0.0d0)
                call data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%AddTo (data%State%MudSystem%Ann_KickLoc,0.0d0)
                call data%State%MudSystem%Ann_MudOrKick%AddTo (data%State%MudSystem%Ann_KickLoc,0)
                call data%State%MudSystem%Ann_CuttingMud%AddTo (data%State%MudSystem%Ann_KickLoc,0)
            
          else  !Op_Density%Array (imudKick-1) == data%State%MudSystem%NewDensity

                data%State%MudSystem%Ann_Density%Array(data%State%MudSystem%Ann_KickLoc-1)= (data%State%MudSystem%Ann_Density%Array(data%State%MudSystem%Ann_KickLoc-1)*data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%Ann_KickLoc-1)+data%State%MudSystem%NewDensity*data%State%MudSystem%NewVolume)/(data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%Ann_KickLoc-1)+data%State%MudSystem%NewVolume)
                data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%Ann_KickLoc-1)= data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%Ann_KickLoc-1) + data%State%MudSystem%NewVolume                
                
            
          endif
          
    else       !if Ann_KickLoc == 1   *6 ===============================
     !write(*,*) '****6'
         
            data%State%MudSystem%Old_KickBackHead_X= data%State%MudSystem%Ann_Mud_Backhead_X%Array (data%State%MudSystem%Ann_KickLoc)
            data%State%MudSystem%Old_KickBackHead_Section= data%State%MudSystem%Ann_Mud_Backhead_section%Array (data%State%MudSystem%Ann_KickLoc)
          
          
            call data%State%MudSystem%Ann_Density%AddToFirst (data%State%MudSystem%NewDensity)
            call data%State%MudSystem%Ann_MudDischarged_Volume%AddToFirst (data%State%MudSystem%NewVolume)
            call data%State%MudSystem%Ann_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Old_KickBackHead_X)                                
            call data%State%MudSystem%Ann_Mud_Forehead_section%AddToFirst (data%State%MudSystem%Old_KickBackHead_Section)
            call data%State%MudSystem%Ann_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Old_KickBackHead_X)
            call data%State%MudSystem%Ann_Mud_Backhead_section%AddToFirst (data%State%MudSystem%Old_KickBackHead_Section)
            call data%State%MudSystem%Ann_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
            call data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
            call data%State%MudSystem%Ann_MudOrKick%AddToFirst (0)
            call data%State%MudSystem%Ann_CuttingMud%AddToFirst (CuttingValue)
                
    endif          
     
 endif     
     
 !======================================================================================== 

 
 
 
 

            
!============================== kick 2 tarafe mate bashad *4 ==============================
 
 if ( data%State%MudSystem%Ann_KickLoc > 0 .and.  data%State%MudSystem%Op_KickLoc > 0 ) then 
     !write(*,*) '****4'
 
     data%State%MudSystem%iLoc= 2
 
     data%State%MudSystem%KickDv= data%State%MudSystem%Area_PipeSectionFt(data%State%MudSystem%Ann_Mud_Forehead_section%Array(data%State%MudSystem%Ann_KickLoc)) * data%State%MudSystem%KickDx * 7.48051948d0        ! ft^3  to gal
     
     
        data%State%MudSystem%MinKickDv= min( data%State%MudSystem%KickDv,data%State%MudSystem%Ann_MudDischarged_Volume%Array (data%State%MudSystem%Ann_KickLoc+1), data%State%MudSystem%Op_MudDischarged_Volume%Last () )            ! Ann_KickLoc+1=2   Op_MudDischarged_Volume%Last ()=kick

        data%State%MudSystem%NewDensity= data%State%MudSystem%Ann_Density%Array (data%State%MudSystem%Ann_KickLoc+1)
        data%State%MudSystem%NewVolume= data%State%MudSystem%MinKickDv  
     
      if ( data%State%MudSystem%MinKickDv == data%State%MudSystem%KickDv ) then    !eleman bala sari baghi mimund, paeeni(kick) dar OP ham baghi mimund
     !write(*,*) '****4----1'

     
                        data%State%MudSystem%Ann_MudDischarged_Volume%Array (data%State%MudSystem%Ann_KickLoc+1)= data%State%MudSystem%Ann_MudDischarged_Volume%Array (data%State%MudSystem%Ann_KickLoc+1) - data%State%MudSystem%MinKickDv
            
                        data%State%MudSystem%Ann_MudDischarged_Volume%Array (data%State%MudSystem%Ann_KickLoc)= data%State%MudSystem%Ann_MudDischarged_Volume%Array (data%State%MudSystem%Ann_KickLoc) + data%State%MudSystem%MinKickDv       ! Ann_KickLoc= 1
            
                        data%State%MudSystem%Op_MudDischarged_Volume%Array (data%State%MudSystem%Op_KickLoc)= data%State%MudSystem%Op_MudDischarged_Volume%Array (data%State%MudSystem%Op_KickLoc) - data%State%MudSystem%MinKickDv       ! Op_KickLoc= last
            
            
                            ! backheade kick zire mate bashad 
                        if ( data%State%MudSystem%Op_KickLoc>1) then
                            
                                !if ( Op_Density%Array (Op_KickLoc-1) /= data%State%MudSystem%NewDensity ) then   
          
                                if ( ABS(data%State%MudSystem%Op_Density%Array (data%State%MudSystem%Op_KickLoc-1) - data%State%MudSystem%NewDensity) >= data%State%MudSystem%DensityMixTol ) then
                    
                                data%State%MudSystem%Old_KickBackHead_X= data%State%MudSystem%Op_Mud_Backhead_X%Array (data%State%MudSystem%Op_KickLoc)
                                data%State%MudSystem%Old_KickBackHead_Section= data%State%MudSystem%Op_Mud_Backhead_section%Array (data%State%MudSystem%Op_KickLoc)
            
                                call data%State%MudSystem%Op_Density%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%NewDensity)
                                call data%State%MudSystem%Op_MudDischarged_Volume%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%NewVolume)
                                call data%State%MudSystem%Op_Mud_Forehead_X%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_X)
                                call data%State%MudSystem%Op_Mud_Forehead_section%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_Section)
                                call data%State%MudSystem%Op_Mud_Backhead_X%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_X)
                                call data%State%MudSystem%Op_Mud_Backhead_section%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_Section)
                                call data%State%MudSystem%Op_RemainedVolume_in_LastSection%AddTo (data%State%MudSystem%Op_KickLoc,0.0d0)
                                call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%AddTo (data%State%MudSystem%Op_KickLoc,0.0d0)
                                call data%State%MudSystem%Op_MudOrKick%AddTo (data%State%MudSystem%Op_KickLoc,0)
        
            
                            else  ! merge

                                data%State%MudSystem%Op_Density%Array(data%State%MudSystem%Op_KickLoc-1)= (data%State%MudSystem%Op_Density%Array(data%State%MudSystem%Op_KickLoc-1)*data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1)+data%State%MudSystem%NewDensity*data%State%MudSystem%NewVolume)/(data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1)+data%State%MudSystem%NewVolume)
                                data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1)= data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1) + data%State%MudSystem%NewVolume

                            endif 
                            
                        else       !if Op_KickLoc == 1     (*****Migration Start*****)  *5-2===============================
     !write(*,*) '****5-2'
         
                                data%State%MudSystem%Old_KickBackHead_X= data%State%MudSystem%Op_Mud_Backhead_X%Array (data%State%MudSystem%Op_KickLoc)
                                data%State%MudSystem%Old_KickBackHead_Section= data%State%MudSystem%Op_Mud_Backhead_section%Array (data%State%MudSystem%Op_KickLoc)
          
          
                                call data%State%MudSystem%Op_Density%AddToFirst (data%State%MudSystem%NewDensity)
                                call data%State%MudSystem%Op_MudDischarged_Volume%AddToFirst (data%State%MudSystem%NewVolume)
                                call data%State%MudSystem%Op_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Old_KickBackHead_X)                                
                                call data%State%MudSystem%Op_Mud_Forehead_section%AddToFirst (data%State%MudSystem%Old_KickBackHead_Section)
                                call data%State%MudSystem%Op_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Old_KickBackHead_X)
                                call data%State%MudSystem%Op_Mud_Backhead_section%AddToFirst (data%State%MudSystem%Old_KickBackHead_Section)
                                call data%State%MudSystem%Op_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
                                call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
                                call data%State%MudSystem%Op_MudOrKick%AddToFirst (0)
                
                        endif
                            
                       
           
      elseif ( data%State%MudSystem%MinKickDv == data%State%MudSystem%Ann_MudDischarged_Volume%Array (data%State%MudSystem%Ann_KickLoc+1) ) then  ! eleman bala sari baghi nemimund            
     !write(*,*) '****4----2'
            
                        call data%State%MudSystem%Ann_MudDischarged_Volume%Remove (data%State%MudSystem%Ann_KickLoc+1)
                        call data%State%MudSystem%Ann_Mud_Backhead_X%Remove (data%State%MudSystem%Ann_KickLoc+1)
                        call data%State%MudSystem%Ann_Mud_Backhead_section%Remove (data%State%MudSystem%Ann_KickLoc+1)
                        call data%State%MudSystem%Ann_Mud_Forehead_X%Remove (data%State%MudSystem%Ann_KickLoc+1)
                        call data%State%MudSystem%Ann_Mud_Forehead_section%Remove (data%State%MudSystem%Ann_KickLoc+1)
                        call data%State%MudSystem%Ann_Density%Remove (data%State%MudSystem%Ann_KickLoc+1)
                        call data%State%MudSystem%Ann_RemainedVolume_in_LastSection%Remove (data%State%MudSystem%Ann_KickLoc+1)
                        call data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Remove (data%State%MudSystem%Ann_KickLoc+1) 
                        call data%State%MudSystem%Ann_MudOrKick%Remove (data%State%MudSystem%Ann_KickLoc+1) 
                        call data%State%MudSystem%Ann_CuttingMud%Remove (data%State%MudSystem%Ann_KickLoc+1) 
            
                        data%State%MudSystem%Ann_MudDischarged_Volume%Array (data%State%MudSystem%Ann_KickLoc)= data%State%MudSystem%Ann_MudDischarged_Volume%Array (data%State%MudSystem%Ann_KickLoc) + data%State%MudSystem%MinKickDv       ! Ann_KickLoc= 1
            
                        data%State%MudSystem%Op_MudDischarged_Volume%Array (data%State%MudSystem%Op_KickLoc)= data%State%MudSystem%Op_MudDischarged_Volume%Array (data%State%MudSystem%Op_KickLoc) - data%State%MudSystem%MinKickDv       ! Op_KickLoc= last
            
                            ! backheade kick zire mate bashad 
                        if (data%State%MudSystem%Op_KickLoc > 1) then
                            
                            !if ( Op_Density%Array (Op_KickLoc-1) /= data%State%MudSystem%NewDensity ) then 
                                
                            if ( ABS(data%State%MudSystem%Op_Density%Array (data%State%MudSystem%Op_KickLoc-1) - data%State%MudSystem%NewDensity) >= data%State%MudSystem%DensityMixTol ) then
          
          
                                data%State%MudSystem%Old_KickBackHead_X= data%State%MudSystem%Op_Mud_Backhead_X%Array (data%State%MudSystem%Op_KickLoc)
                                data%State%MudSystem%Old_KickBackHead_Section= data%State%MudSystem%Op_Mud_Backhead_section%Array (data%State%MudSystem%Op_KickLoc)
            
                                call data%State%MudSystem%Op_Density%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%NewDensity)
                                call data%State%MudSystem%Op_MudDischarged_Volume%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%NewVolume)
                                call data%State%MudSystem%Op_Mud_Forehead_X%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_X)
                                call data%State%MudSystem%Op_Mud_Forehead_section%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_Section)
                                call data%State%MudSystem%Op_Mud_Backhead_X%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_X)
                                call data%State%MudSystem%Op_Mud_Backhead_section%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_Section)
                                call data%State%MudSystem%Op_RemainedVolume_in_LastSection%AddTo (data%State%MudSystem%Op_KickLoc,0.0d0)
                                call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%AddTo (data%State%MudSystem%Op_KickLoc,0.0d0)
                                call data%State%MudSystem%Op_MudOrKick%AddTo (data%State%MudSystem%Op_KickLoc,0)
        
            
                            else  !Op_Density%Array (imudKick-1) == data%State%MudSystem%NewDensity
                                data%State%MudSystem%Op_Density%Array(data%State%MudSystem%Op_KickLoc-1)= (data%State%MudSystem%Op_Density%Array(data%State%MudSystem%Op_KickLoc-1)*data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1)+data%State%MudSystem%NewDensity*data%State%MudSystem%NewVolume)/(data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1)+data%State%MudSystem%NewVolume)
                                data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1)= data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1) + data%State%MudSystem%NewVolume
            
                            endif  
                            
                        else       !if Op_KickLoc == 1     (*****Migration Start*****)  *5-2===============================
         
                                data%State%MudSystem%Old_KickBackHead_X= data%State%MudSystem%Op_Mud_Backhead_X%Array (data%State%MudSystem%Op_KickLoc)
                                data%State%MudSystem%Old_KickBackHead_Section= data%State%MudSystem%Op_Mud_Backhead_section%Array (data%State%MudSystem%Op_KickLoc)
          
          
                                call data%State%MudSystem%Op_Density%AddToFirst (data%State%MudSystem%NewDensity)
                                call data%State%MudSystem%Op_MudDischarged_Volume%AddToFirst (data%State%MudSystem%NewVolume)
                                call data%State%MudSystem%Op_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Old_KickBackHead_X)                                
                                call data%State%MudSystem%Op_Mud_Forehead_section%AddToFirst (data%State%MudSystem%Old_KickBackHead_Section)
                                call data%State%MudSystem%Op_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Old_KickBackHead_X)
                                call data%State%MudSystem%Op_Mud_Backhead_section%AddToFirst (data%State%MudSystem%Old_KickBackHead_Section)
                                call data%State%MudSystem%Op_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
                                call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
                                call data%State%MudSystem%Op_MudOrKick%AddToFirst (0)
                
                        endif                            
                            
                        
     elseif ( data%State%MudSystem%MinKickDv == data%State%MudSystem%Op_MudDischarged_Volume%Last () ) then  ! eleman balaee baghi mimund, kick az OP kamel kharej mishod          
     !write(*,*) '****4----3'
     
            
            
                        data%State%MudSystem%Ann_MudDischarged_Volume%Array (data%State%MudSystem%Ann_KickLoc+1)= data%State%MudSystem%Ann_MudDischarged_Volume%Array (data%State%MudSystem%Ann_KickLoc+1) - data%State%MudSystem%MinKickDv
            
                        data%State%MudSystem%Ann_MudDischarged_Volume%Array (data%State%MudSystem%Ann_KickLoc)= data%State%MudSystem%Ann_MudDischarged_Volume%Array (data%State%MudSystem%Ann_KickLoc) + data%State%MudSystem%MinKickDv       ! Ann_KickLoc= 1

          
          
                    data%State%MudSystem%Old_KickBackHead_X= data%State%MudSystem%Op_Mud_Backhead_X%Array (data%State%MudSystem%Op_KickLoc)
                    data%State%MudSystem%Old_KickBackHead_Section= data%State%MudSystem%Op_Mud_Backhead_section%Array (data%State%MudSystem%Op_KickLoc)   
                                
                                
                        call data%State%MudSystem%Op_MudDischarged_Volume%Remove (data%State%MudSystem%Op_KickLoc)        ! Op_KickLoc= last
                        call data%State%MudSystem%Op_Mud_Backhead_X%Remove (data%State%MudSystem%Op_KickLoc)
                        call data%State%MudSystem%Op_Mud_Backhead_section%Remove (data%State%MudSystem%Op_KickLoc)
                        call data%State%MudSystem%Op_Mud_Forehead_X%Remove (data%State%MudSystem%Op_KickLoc)
                        call data%State%MudSystem%Op_Mud_Forehead_section%Remove (data%State%MudSystem%Op_KickLoc)
                        call data%State%MudSystem%Op_Density%Remove (data%State%MudSystem%Op_KickLoc)
                        call data%State%MudSystem%Op_RemainedVolume_in_LastSection%Remove (data%State%MudSystem%Op_KickLoc)
                        call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%Remove (data%State%MudSystem%Op_KickLoc)   
                        call data%State%MudSystem%Op_MudOrKick%Remove (data%State%MudSystem%Op_KickLoc)    
          
                             ! backheade kick zire mate bashad 
                        if (data%State%MudSystem%Op_KickLoc > 1) then
                            
                            !if ( Op_Density%Array (Op_KickLoc-1) /= data%State%MudSystem%NewDensity ) then   
                            
                            if ( ABS(data%State%MudSystem%Op_Density%Array (data%State%MudSystem%Op_KickLoc-1) - data%State%MudSystem%NewDensity) >= data%State%MudSystem%DensityMixTol ) then
          

                                call data%State%MudSystem%Op_Density%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%NewDensity)
                                call data%State%MudSystem%Op_MudDischarged_Volume%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%NewVolume)
                                call data%State%MudSystem%Op_Mud_Forehead_X%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_X)
                                call data%State%MudSystem%Op_Mud_Forehead_section%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_Section)
                                call data%State%MudSystem%Op_Mud_Backhead_X%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_X)
                                call data%State%MudSystem%Op_Mud_Backhead_section%AddTo (data%State%MudSystem%Op_KickLoc,data%State%MudSystem%Old_KickBackHead_Section)
                                call data%State%MudSystem%Op_RemainedVolume_in_LastSection%AddTo (data%State%MudSystem%Op_KickLoc,0.0d0)
                                call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%AddTo (data%State%MudSystem%Op_KickLoc,0.0d0)
                                call data%State%MudSystem%Op_MudOrKick%AddTo (data%State%MudSystem%Op_KickLoc,0)
   
                            else  !Op_Density%Array (imudKick-1) == data%State%MudSystem%NewDensity
                                data%State%MudSystem%Op_Density%Array(data%State%MudSystem%Op_KickLoc-1)= (data%State%MudSystem%Op_Density%Array(data%State%MudSystem%Op_KickLoc-1)*data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1)+data%State%MudSystem%NewDensity*data%State%MudSystem%NewVolume)/(data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1)+data%State%MudSystem%NewVolume)
                                data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1)= data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_KickLoc-1) + data%State%MudSystem%NewVolume
            
                            endif    
                            
                        else       !if Op_KickLoc == 1     (*****Migration Start*****)  *5-2===============================

                                call data%State%MudSystem%Op_Density%AddToFirst (data%State%MudSystem%NewDensity)
                                call data%State%MudSystem%Op_MudDischarged_Volume%AddToFirst (data%State%MudSystem%NewVolume)
                                call data%State%MudSystem%Op_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Old_KickBackHead_X)                                
                                call data%State%MudSystem%Op_Mud_Forehead_section%AddToFirst (data%State%MudSystem%Old_KickBackHead_Section)
                                call data%State%MudSystem%Op_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Old_KickBackHead_X)
                                call data%State%MudSystem%Op_Mud_Backhead_section%AddToFirst (data%State%MudSystem%Old_KickBackHead_Section)
                                call data%State%MudSystem%Op_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
                                call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
                                call data%State%MudSystem%Op_MudOrKick%AddToFirst (0)
                
                        endif                            
                                
            data%State%MudSystem%iLoc= 1        ! ok
                        
     endif

     
 endif
 
!======================================================================================== 
          
          
          
!============================== kick 2 tarafe mate bashad *4 with pump ==============================

 !
 !if ( Ann_KickLoc > 0 .and.  Op_KickLoc > 0 .and. data%State%MudSystem%AnnulusFlowRate /= 0.0 ) then 
 !    write(*,*) '****4 with pump'
 !    
 !    KickMigration_2SideBit= .true.
 !
 !    !iloc= 2
 !
 !   KickDv= Area_PipeSectionFt(Ann_Mud_Forehead_section%Array(Ann_KickLoc)) * KickDx * 7.48051948d0        ! ft^3  to gal
 !   !MinKickDv= min( KickDv,Ann_MudDischarged_Volume%Array (Ann_KickLoc+1), Op_MudDischarged_Volume%Last () )            ! Ann_KickLoc+1=2   Op_MudDischarged_Volume%Last ()=kick
 !    
 !    
 !       !MinKickDv= min( KickDv,Ann_MudDischarged_Volume%Array (Ann_KickLoc+1), Op_MudDischarged_Volume%Last () )            ! Ann_KickLoc+1=2   Op_MudDischarged_Volume%Last ()=kick
 !       !
 !       !data%State%MudSystem%NewDensity= Ann_Density%Array (Ann_KickLoc+1)
 !       !NewVolume= MinKickDv  
 !       
 !       
 !       if ( ((data%State%MudSystem%AnnulusFlowRate/60.0d0)*DeltaT_Mudline) >= KickDv ) then    ! sorate pump bishtar az kick bashad---tu in halat aslan kari be elemane balaiye kick tuye Ann nadarim
 !           !WRITE(*,*) '*****sorate pump bishtar az kick*******'
 !           MinKickDv= ((data%State%MudSystem%AnnulusFlowRate/60.0d0)*DeltaT_Mudline)  ! dar asl maxKickDv ast     
 !       
 !           
 !           
 !           
 !           
 !           
 !           
 !           
 !               if ( Op_MudDischarged_Volume%Last () > MinKickDv ) then    !eleman paeeni(kick) dar OP  baghi mimund
 !           
 !                   Ann_MudDischarged_Volume%Array (Ann_KickLoc)= Ann_MudDischarged_Volume%Array (Ann_KickLoc) + MinKickDv       ! Ann_KickLoc= 1
 !           
 !                   Op_MudDischarged_Volume%Array (Op_KickLoc)= Op_MudDischarged_Volume%Array (Op_KickLoc) - MinKickDv       ! Op_KickLoc= last        
 !       
 !                   Old_KickBackHead_X= Op_Mud_Backhead_X%Array (Op_KickLoc)
 !                   Old_KickBackHead_Section= Op_Mud_Backhead_section%Array (Op_KickLoc)
 !                              ! backheade kick zire mate bashad 
 !                               if ( Op_KickLoc>1) then
 !                           
 !                                           if ( Op_Density%Array (Op_KickLoc-1) /=data%State%MudSystem%St_Density%Last() ) then   
 !         
 !         
 !                                               Old_KickBackHead_X= Op_Mud_Backhead_X%Array (Op_KickLoc)
 !                                               Old_KickBackHead_Section= Op_Mud_Backhead_section%Array (Op_KickLoc)
 !           
 !                                               call Op_Density%AddTo (Op_KickLocMudSystem%St_Density%Last())
 !                                               call Op_MudDischarged_Volume%AddTo (Op_KickLoc,MinKickDv)
 !                                               call Op_Mud_Forehead_X%AddTo (Op_KickLoc,Old_KickBackHead_X)
 !                                               call Op_Mud_Forehead_section%AddTo (Op_KickLoc,Old_KickBackHead_Section)
 !                                               call Op_Mud_Backhead_X%AddTo (Op_KickLoc,Old_KickBackHead_X)
 !                                               call Op_Mud_Backhead_section%AddTo (Op_KickLoc,Old_KickBackHead_Section)
 !                                               call Op_RemainedVolume_in_LastSection%AddTo (Op_KickLoc,0.0d0)
 !                                               call Op_EmptyVolume_inBackheadLocation%AddTo (Op_KickLoc,0.0d0)
 !                                               call Op_MudOrKick%AddTo (Op_KickLoc,0)
 !       
 !           
 !                                           else  !Op_Density%Array (imudKick-1) == data%State%MudSystem%NewDensity
 !         
 !                                               Op_MudDischarged_Volume%Array (Op_KickLoc-1)= Op_MudDischarged_Volume%Array (Op_KickLoc-1) + NewVolume
 !           
 !                                           endif 
 !                           
 !                               else       !if Op_KickLoc == 1     (*****Migration Start*****)  *5-2===============================
 !                   write(*,*) '****5-2 with pump'        
 !       
 !       
 !                                       !Old_KickBackHead_X= Op_Mud_Backhead_X%Array (Op_KickLoc)
 !                                       !Old_KickBackHead_Section= Op_Mud_Backhead_section%Array (Op_KickLoc)
 !         
 !         
 !                                       call Op_Density%AddToFirst data%State%MudSystem%St_Density%Last())
 !                                       call Op_MudDischarged_Volume%AddToFirst (MinKickDv)
 !                                       call Op_Mud_Forehead_X%AddToFirst (Old_KickBackHead_X)                                
 !                                       call Op_Mud_Forehead_section%AddToFirst (Old_KickBackHead_Section)
 !                                       call Op_Mud_Backhead_X%AddToFirst (Old_KickBackHead_X)
 !                                       call Op_Mud_Backhead_section%AddToFirst (Old_KickBackHead_Section)
 !                                       call Op_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
 !                                       call Op_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
 !                                       call Op_MudOrKick%AddToFirst (0)
 !               
 !                               endif        
 !       
 !               elseif ( Op_MudDischarged_Volume%Last () <= MinKickDv ) then    !eleman paeeni(kick) dar OP  baghi nemimund yani kick az OP kamel kharej mishod
 !           
 !                   MinKickDv= Op_MudDischarged_Volume%Last ()      
 !       
 !      
 !               write(*,*) '****4----3 with pump a'
 !           
 !
 !                       Ann_MudDischarged_Volume%Array (Ann_KickLoc+1)= Ann_MudDischarged_Volume%Array (Ann_KickLoc+1) + ((data%State%MudSystem%AnnulusFlowRate/60.0d0)*DeltaT_Mudline)-MinKickDv
 !           
 !                       Ann_MudDischarged_Volume%Array (Ann_KickLoc)= Ann_MudDischarged_Volume%Array (Ann_KickLoc) + MinKickDv       ! Ann_KickLoc= 1
 !           
 !                       call Op_MudDischarged_Volume%Remove (Op_KickLoc)        ! Op_KickLoc= last
 !                       call Op_Mud_Backhead_X%Remove (Op_KickLoc)
 !                       call Op_Mud_Backhead_section%Remove (Op_KickLoc)
 !                       call Op_Mud_Forehead_X%Remove (Op_KickLoc)
 !                       call Op_Mud_Forehead_section%Remove (Op_KickLoc)
 !                       call Op_Density%Remove (Op_KickLoc)
 !                       call Op_RemainedVolume_in_LastSection%Remove (Op_KickLoc)
 !                       call Op_EmptyVolume_inBackheadLocation%Remove (Op_KickLoc)   
 !                       call Op_MudOrKick%Remove (Op_KickLoc)            
 !       
 !
 !                                    ! backheade kick zire mate bashad 
 !                               if (Op_KickLoc > 1) then
 !                           
 !                                   if ( Op_Density%Array (Op_KickLoc-1) /=data%State%MudSystem%St_Density%Last() ) then   
 !         
 !         
 !                                       !Old_KickBackHead_X= Op_Mud_Backhead_X%Array (Op_KickLoc)
 !                                       !Old_KickBackHead_Section= Op_Mud_Backhead_section%Array (Op_KickLoc)
 !           
 !                                       call Op_Density%AddTo (Op_KickLocMudSystem%St_Density%Last())
 !                                       call Op_MudDischarged_Volume%AddTo (Op_KickLoc,MinKickDv)
 !                                       call Op_Mud_Forehead_X%AddTo (Op_KickLoc,Old_KickBackHead_X)
 !                                       call Op_Mud_Forehead_section%AddTo (Op_KickLoc,Old_KickBackHead_Section)
 !                                       call Op_Mud_Backhead_X%AddTo (Op_KickLoc,Old_KickBackHead_X)
 !                                       call Op_Mud_Backhead_section%AddTo (Op_KickLoc,Old_KickBackHead_Section)
 !                                       call Op_RemainedVolume_in_LastSection%AddTo (Op_KickLoc,0.0d0)
 !                                       call Op_EmptyVolume_inBackheadLocation%AddTo (Op_KickLoc,0.0d0)
 !                                       call Op_MudOrKick%AddTo (Op_KickLoc,0)
 !       
 !
 !           
 !                                   else  !Op_Density%Array (imudKick-1) == data%State%MudSystem%NewDensity
 !
 !         
 !                                       Op_MudDischarged_Volume%Array (Op_KickLoc-1)= Op_MudDischarged_Volume%Array (Op_KickLoc-1) + MinKickDv
 !           
 !                                   endif    
 !                           
 !                               else       !if Op_KickLoc == 1     (*****Migration Start*****)  *5-2===============================
 !
 !        
 !                                       Old_KickBackHead_X= Op_Mud_Backhead_X%Array (Op_KickLoc)
 !                                       Old_KickBackHead_Section= Op_Mud_Backhead_section%Array (Op_KickLoc)
 !         
 !         
 !                                       call Op_Density%AddToFirst data%State%MudSystem%St_Density%Last())
 !                                       call Op_MudDischarged_Volume%AddToFirst (MinKickDv)
 !                                       call Op_Mud_Forehead_X%AddToFirst (Old_KickBackHead_X)                                
 !                                       call Op_Mud_Forehead_section%AddToFirst (Old_KickBackHead_Section)
 !                                       call Op_Mud_Backhead_X%AddToFirst (Old_KickBackHead_X)
 !                                       call Op_Mud_Backhead_section%AddToFirst (Old_KickBackHead_Section)
 !                                       call Op_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
 !                                       call Op_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
 !                                       call Op_MudOrKick%AddToFirst (0)
 !               
 !                               endif   
 !
 !                       
 !               endif       ! ende 2 halat ke kick tuye Op baghi bemune ya namune- dar halati ke  ((data%State%MudSystem%AnnulusFlowRate/60.)*DeltaT_Mudline) >= KickDv )   ! sorate pump bishtar az kick bashad
 !       
 !       
 !       
 !       
 !       else    !if(  ((data%State%MudSystem%AnnulusFlowRate/60.)*DeltaT_Mudline) < KickDv ) then        ! sorate pump kamtar az kick bashad
 !           !WRITE(*,*) '*****sorate pump kamtar az kick*******'
 !    
 !      
 !           
 !           
 !           MinKickDv= min( KickDv,Ann_MudDischarged_Volume%Array (Ann_KickLoc+1), Op_MudDischarged_Volume%Last () )            ! Ann_KickLoc+1=2   Op_MudDischarged_Volume%Last ()=kick
 !
 !           
 !           
 !        !write(*,*) 'MinKickDv=' , MinKickDv
 !
 !    
 !         if ( MinKickDv == KickDv ) then    !eleman bala sari baghi mimund, paeeni(kick) dar OP ham baghi mimund
 !        !write(*,*) '****4----1 with pump'
 !        !write(*,*) data%State%MudSystem%St_Density%Last()=' ,data%State%MudSystem%St_Density%Last()
 !        !write(*,*) '((data%State%MudSystem%AnnulusFlowRate/60.)*DeltaT_Mudline)=' , ((data%State%MudSystem%AnnulusFlowRate/60.)*DeltaT_Mudline)
 !        !write(*,*) 'Ann_Density%Array(Ann_KickLoc+1)=' , Ann_Density%Array(Ann_KickLoc+1)
 !        !write(*,*) '(MinKickDv-((data%State%MudSystem%AnnulusFlowRate/60.)*DeltaT_Mudline))=' , (MinKickDv-((data%State%MudSystem%AnnulusFlowRate/60.)*DeltaT_Mudline))
 !        
 !
 !                       data%State%MudSystem%NewDensity= data%State%MudSystem%St_Density%Last()*((data%State%MudSystem%AnnulusFlowRate/60.0d0)*DeltaT_Mudline) + Ann_Density%Array(Ann_KickLoc+1)*(MinKickDv-((data%State%MudSystem%AnnulusFlowRate/60.0d0)*DeltaT_Mudline))) &
 !                                       / (((data%State%MudSystem%AnnulusFlowRate/60.0d0)*DeltaT_Mudline) + (MinKickDv-((data%State%MudSystem%AnnulusFlowRate/60.0d0)*DeltaT_Mudline)))
 !                       NewVolume= MinKickDv
 !                       
 !                       
 !                       Ann_MudDischarged_Volume%Array (Ann_KickLoc+1)= Ann_MudDischarged_Volume%Array (Ann_KickLoc+1) - (MinKickDv-((data%State%MudSystem%AnnulusFlowRate/60.0d0)*DeltaT_Mudline))
 !           
 !                       Ann_MudDischarged_Volume%Array (Ann_KickLoc)= Ann_MudDischarged_Volume%Array (Ann_KickLoc) + MinKickDv       ! Ann_KickLoc= 1
 !           
 !                       Op_MudDischarged_Volume%Array (Op_KickLoc)= Op_MudDischarged_Volume%Array (Op_KickLoc) - MinKickDv       ! Op_KickLoc= last
 !           
 !           
 !                           ! backheade kick zire mate bashad 
 !                       if ( Op_KickLoc>1) then
 !        
 !                           
 !                           
 !                           if ( ABS(Op_Density%Array (Op_KickLoc-1) - data%State%MudSystem%NewDensity) > DensityMixTol) then ! .OR. (Op_MudDischarged_Volume%Array (Op_KickLoc-1)>42.) ) then   
 !         
 !
 !         
 !                               Old_KickBackHead_X= Op_Mud_Backhead_X%Array (Op_KickLoc)
 !                               Old_KickBackHead_Section= Op_Mud_Backhead_section%Array (Op_KickLoc)
 !           
 !                               call Op_Density%AddTo (Op_KickLoc,data%State%MudSystem%NewDensity)
 !                               call Op_MudDischarged_Volume%AddTo (Op_KickLoc,NewVolume)
 !                               call Op_Mud_Forehead_X%AddTo (Op_KickLoc,Old_KickBackHead_X)
 !                               call Op_Mud_Forehead_section%AddTo (Op_KickLoc,Old_KickBackHead_Section)
 !                               call Op_Mud_Backhead_X%AddTo (Op_KickLoc,Old_KickBackHead_X)
 !                               call Op_Mud_Backhead_section%AddTo (Op_KickLoc,Old_KickBackHead_Section)
 !                               call Op_RemainedVolume_in_LastSection%AddTo (Op_KickLoc,0.0d0)
 !                               call Op_EmptyVolume_inBackheadLocation%AddTo (Op_KickLoc,0.0d0)
 !                               call Op_MudOrKick%AddTo (Op_KickLoc,0)
 !       
 !           
 !                           else  !Merge Condition
 !         
 !                               Op_MudDischarged_Volume%Array (Op_KickLoc-1)= Op_MudDischarged_Volume%Array (Op_KickLoc-1) + NewVolume
 !                               Op_Density%Array (Op_KickLoc-1)= (Op_MudDischarged_Volume%Array (Op_KickLoc-1)*Op_Density%Array (Op_KickLoc-1)+NewVolume*data%State%MudSystem%NewDensity) / &
 !                                                                   (Op_MudDischarged_Volume%Array (Op_KickLoc-1)+NewVolume)
 !                               
 !           
 !                           endif 
 !                           
 !                       else       !if Op_KickLoc == 1     (*****Migration Start*****)  *5-2===============================
 !       write(*,*) '****5-2 with pump'
 !        
 !                               Old_KickBackHead_X= Op_Mud_Backhead_X%Array (Op_KickLoc)
 !                               Old_KickBackHead_Section= Op_Mud_Backhead_section%Array (Op_KickLoc)
 !         
 !         
 !                               call Op_Density%AddToFirst (data%State%MudSystem%NewDensity)
 !                               call Op_MudDischarged_Volume%AddToFirst (NewVolume)
 !                               call Op_Mud_Forehead_X%AddToFirst (Old_KickBackHead_X)                                
 !                               call Op_Mud_Forehead_section%AddToFirst (Old_KickBackHead_Section)
 !                               call Op_Mud_Backhead_X%AddToFirst (Old_KickBackHead_X)
 !                               call Op_Mud_Backhead_section%AddToFirst (Old_KickBackHead_Section)
 !                               call Op_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
 !                               call Op_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
 !                               call Op_MudOrKick%AddToFirst (0)
 !               
 !                       endif
 !                           
 !                      
 !          
 !       elseif ( MinKickDv == Ann_MudDischarged_Volume%Array (Ann_KickLoc+1) ) then  ! eleman bala sari baghi nemimund            
 !       write(*,*) '****4----2 with pump'
 !       
 !                       data%State%MudSystem%NewDensity= data%State%MudSystem%St_Density%Last()*((data%State%MudSystem%AnnulusFlowRate/60.0d0)*DeltaT_Mudline) + Ann_Density%Array(Ann_KickLoc+1)*(MinKickDv-((data%State%MudSystem%AnnulusFlowRate/60.0d0)*DeltaT_Mudline))) &
 !                                       / (((data%State%MudSystem%AnnulusFlowRate/60.0d0)*DeltaT_Mudline) + Ann_MudDischarged_Volume%Array (Ann_KickLoc+1))
 !                       NewVolume= MinKickDv
 !                       
 !                       call RemoveAnnulusMudArrays(Ann_KickLoc+1) 
 !           
 !                       Ann_MudDischarged_Volume%Array (Ann_KickLoc)= Ann_MudDischarged_Volume%Array (Ann_KickLoc) + MinKickDv       ! Ann_KickLoc= 1
 !           
 !                       Op_MudDischarged_Volume%Array (Op_KickLoc)= Op_MudDischarged_Volume%Array (Op_KickLoc) - MinKickDv       ! Op_KickLoc= last
 !           
 !                           ! backheade kick zire mate bashad 
 !                       if (Op_KickLoc > 1) then
 !                           
 !                           if ( ABS(Op_Density%Array (Op_KickLoc-1) - data%State%MudSystem%NewDensity) > DensityMixTol) then ! .OR. (Op_MudDischarged_Volume%Array (Op_KickLoc-1)>42.) ) then   
 !         
 !                               !
 !                               Old_KickBackHead_X= Op_Mud_Backhead_X%Array (Op_KickLoc)
 !                               Old_KickBackHead_Section= Op_Mud_Backhead_section%Array (Op_KickLoc)
 !           
 !                               call Op_Density%AddTo (Op_KickLoc,data%State%MudSystem%NewDensity)
 !                               call Op_MudDischarged_Volume%AddTo (Op_KickLoc,NewVolume)
 !                               call Op_Mud_Forehead_X%AddTo (Op_KickLoc,Old_KickBackHead_X)
 !                               call Op_Mud_Forehead_section%AddTo (Op_KickLoc,Old_KickBackHead_Section)
 !                               call Op_Mud_Backhead_X%AddTo (Op_KickLoc,Old_KickBackHead_X)
 !                               call Op_Mud_Backhead_section%AddTo (Op_KickLoc,Old_KickBackHead_Section)
 !                               call Op_RemainedVolume_in_LastSection%AddTo (Op_KickLoc,0.0d0)
 !                               call Op_EmptyVolume_inBackheadLocation%AddTo (Op_KickLoc,0.0d0)
 !                               call Op_MudOrKick%AddTo (Op_KickLoc,0)
 !       
 !           
 !                           else  !Merge Condition
 !         
 !                               Op_MudDischarged_Volume%Array (Op_KickLoc-1)= Op_MudDischarged_Volume%Array (Op_KickLoc-1) + NewVolume
 !                               Op_Density%Array (Op_KickLoc-1)= (Op_MudDischarged_Volume%Array (Op_KickLoc-1)*Op_Density%Array (Op_KickLoc-1)+NewVolume*data%State%MudSystem%NewDensity) / &
 !                                                                   (Op_MudDischarged_Volume%Array (Op_KickLoc-1)+NewVolume)
 !                               
 !                               
 !           
 !                           endif  
 !                           
 !                       else       !if Op_KickLoc == 1     (*****Migration Start*****)  *5-2===============================
 !        
 !                               Old_KickBackHead_X= Op_Mud_Backhead_X%Array (Op_KickLoc)
 !                               Old_KickBackHead_Section= Op_Mud_Backhead_section%Array (Op_KickLoc)
 !         
 !         
 !                               call Op_Density%AddToFirst (data%State%MudSystem%NewDensity)
 !                               call Op_MudDischarged_Volume%AddToFirst (NewVolume)
 !                               call Op_Mud_Forehead_X%AddToFirst (Old_KickBackHead_X)                                
 !                               call Op_Mud_Forehead_section%AddToFirst (Old_KickBackHead_Section)
 !                               call Op_Mud_Backhead_X%AddToFirst (Old_KickBackHead_X)
 !                               call Op_Mud_Backhead_section%AddToFirst (Old_KickBackHead_Section)
 !                               call Op_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
 !                               call Op_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
 !                               call Op_MudOrKick%AddToFirst (0)
 !               
 !                       endif                            
 !                           
 !                       
 !        elseif ( MinKickDv == Op_MudDischarged_Volume%Last () ) then  ! eleman balaee baghi mimund, kick az OP kamel kharej mishod          
 !        write(*,*) '****4----3 with pump b'
 !        
 !                       data%State%MudSystem%NewDensity=data%State%MudSystem%St_Density%Last()
 !                       NewVolume= MinKickDv
 !           
 !                       
 !                       Old_KickBackHead_X= Op_Mud_Backhead_X%Array (Op_KickLoc)
 !                       Old_KickBackHead_Section= Op_Mud_Backhead_section%Array (Op_KickLoc)             
 !                       
 !           
 !                       !Ann_MudDischarged_Volume%Array (Ann_KickLoc+1)= Ann_MudDischarged_Volume%Array (Ann_KickLoc+1) - MinKickDv farz kardam dast be elemane balaee nazanam
 !           
 !                       Ann_MudDischarged_Volume%Array (Ann_KickLoc)= Ann_MudDischarged_Volume%Array (Ann_KickLoc) + MinKickDv       ! Ann_KickLoc= 1
 !    !write(*,*) 'pointer 1'        
 !                       call Op_MudDischarged_Volume%Remove (Op_KickLoc)        ! Op_KickLoc= last
 !                       call Op_Mud_Backhead_X%Remove (Op_KickLoc)
 !                       call Op_Mud_Backhead_section%Remove (Op_KickLoc)
 !                       call Op_Mud_Forehead_X%Remove (Op_KickLoc)
 !                       call Op_Mud_Forehead_section%Remove (Op_KickLoc)
 !                       call Op_Density%Remove (Op_KickLoc)
 !                       call Op_RemainedVolume_in_LastSection%Remove (Op_KickLoc)
 !                       call Op_EmptyVolume_inBackheadLocation%Remove (Op_KickLoc)   
 !                       call Op_MudOrKick%Remove (Op_KickLoc)    
 !         
 !    !write(*,*) 'pointer 2'        
 !         
 !                            ! backheade kick zire mate bashad 
 !                       if (Op_KickLoc > 1) then
 !                           
 !                           if ( ABS(Op_Density%Array (Op_KickLoc-1) - data%State%MudSystem%NewDensity) > DensityMixTol) then ! .OR. (Op_MudDischarged_Volume%Array (Op_KickLoc-1)>42.) ) then   
 !    !write(*,*) 'pointer 3'        
 !         
 !         
 !
 !           
 !                               call Op_Density%AddTo (Op_KickLoc,data%State%MudSystem%NewDensity)
 !                               call Op_MudDischarged_Volume%AddTo (Op_KickLoc,NewVolume)
 !                               call Op_Mud_Forehead_X%AddTo (Op_KickLoc,Old_KickBackHead_X)
 !                               call Op_Mud_Forehead_section%AddTo (Op_KickLoc,Old_KickBackHead_Section)
 !                               call Op_Mud_Backhead_X%AddTo (Op_KickLoc,Old_KickBackHead_X)
 !                               call Op_Mud_Backhead_section%AddTo (Op_KickLoc,Old_KickBackHead_Section)
 !                               call Op_RemainedVolume_in_LastSection%AddTo (Op_KickLoc,0.0d0)
 !                               call Op_EmptyVolume_inBackheadLocation%AddTo (Op_KickLoc,0.0d0)
 !                               call Op_MudOrKick%AddTo (Op_KickLoc,0)
 !       
 !    !write(*,*) 'pointer 4'        
 !           
 !                           else  !Merge Condition
 !         
 !                               Op_MudDischarged_Volume%Array (Op_KickLoc-1)= Op_MudDischarged_Volume%Array (Op_KickLoc-1) + NewVolume
 !                               Op_Density%Array (Op_KickLoc-1)= (Op_MudDischarged_Volume%Array (Op_KickLoc-1)*Op_Density%Array (Op_KickLoc-1)+NewVolume*data%State%MudSystem%NewDensity) / &
 !                                                                   (Op_MudDischarged_Volume%Array (Op_KickLoc-1)+NewVolume)
 !           
 !                           endif    
 !                           
 !                       else       !if Op_KickLoc == 1     (*****Migration Start*****)  *5-2===============================
 !    !write(*,*) 'pointer 5'        
 !        
 !
 !    !write(*,*) 'pointer 6'        
 !         
 !         
 !                               call Op_Density%AddToFirst (data%State%MudSystem%NewDensity)
 !                               call Op_MudDischarged_Volume%AddToFirst (NewVolume)
 !                               call Op_Mud_Forehead_X%AddToFirst (Old_KickBackHead_X)                                
 !                               call Op_Mud_Forehead_section%AddToFirst (Old_KickBackHead_Section)
 !                               call Op_Mud_Backhead_X%AddToFirst (Old_KickBackHead_X)
 !                               call Op_Mud_Backhead_section%AddToFirst (Old_KickBackHead_Section)
 !                               call Op_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
 !                               call Op_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
 !                               call Op_MudOrKick%AddToFirst (0)
 !    !write(*,*) 'pointer 7'        
 !               
 !                       endif                            
 !                               
 !                       
 !       endif
 !           
 !    
 !           
 !       endif        !(  ((data%State%MudSystem%AnnulusFlowRate/60.)*DeltaT_Mudline) < KickDv )        ! sorate pump kamtar az kick bashad
 !       
 !
 !
 !                       
 !    endif

!========================================================================================           
   
ENDDO   ! KickNumber= 1, NewInfluxNumber
data%State%MudSystem%KickNumber = kickNumber        
        
     !write(*,*)    'c)Ann_MudDischarged_Volume%Array(:)=' , sum(Ann_MudDischarged_Volume%Array(:))
        
        
             
    end subroutine Kick_Migration             