subroutine Kick_Expansion    !  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
    real(8) ExpansionVolume

    !write(*,*) 'Kick Expansion'
   
    ExpansionVolume= GasPocketDeltaVol%Array(data%State%MudSystem%NewInfluxNumber - data%State%MudSystem%KickNumber + 1) * 7.48

IF ( data%State%MudSystem%Kickexpansion_DueToMudLost )   ExpansionVolume = ((data%State%MudSystem%Qlost/60.0d0)*data%State%MudSystem%DeltaT_Mudline) 


!============================== kick zire mate bashad ==============================

        if (data%State%MudSystem%Op_KickLoc > 0 .and. data%State%MudSystem%Ann_KickLoc==0) then    ! .and. Op_KickLoc /= Op_MudOrKick%Length ()) then
            
            !write(*,*) 'expansion (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)+ ExpansionVolume

            
            !if (data%State%MUD(4)%Q > 0.) then
            !    
            !    if (abs(ChokeLine_Density%Array(1)-Ann_Density%Last())< DensityMixTol) then
            !        ChokeLine_MudDischarged_Volume%Array(1)= ChokeLine_MudDischarged_Volume%Array(1) + ExpansionVolume
            !    else          
            !        call ChokeLine_Density%AddToFirst (Ann_Density%Last())
            !        call ChokeLine_MudDischarged_Volume%AddToFirst (ExpansionVolume)     ! farz kardam ke hameye hajm ro ba yek density ezafe konim
            !        call ChokeLine_Mud_Forehead_X%AddToFirst (0.0d0)
            !        call ChokeLine_Mud_Forehead_section%AddToFirst (1)
            !        call ChokeLine_Mud_Backhead_X%AddToFirst (0.0d0)
            !        call ChokeLine_Mud_Backhead_section%AddToFirst (1)
            !        call ChokeLine_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
            !        call ChokeLine_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
            !        call ChokeLine_MudOrKick%AddToFirst (Ann_MudOrKick%Last())
            !    endif
            !
            !endif
            
        endif
!========================================================================================           
   
        
        
!============================= foreheade dar fazaye annulus bashad ===========================
            ! agar kick be entehaye annulus reside bashe, expansion ra emaal nemikonim 
        if (data%State%MudSystem%Ann_KickLoc > 0) then ! .and. Ann_KickLoc /= Ann_MudOrKick%Length ()) then
            !write(*,*) 'expansion (2)'

            !if ( sum(Ann_MudDischarged_Volume%Array(1:Ann_KickLoc)) + ExpansionVolume > sum(PipeSection_VolumeCapacity(F_StringIntervalCounts+1:NoPipeSections)) ) then   ! agar khast az mate rad kone
            !    ExpansionVolume= sum(PipeSection_VolumeCapacity(F_StringIntervalCounts+1:NoPipeSections)) - sum(Ann_MudDischarged_Volume%Array(1:Ann_KickLoc))
            !endif        
        
            data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%Ann_KickLoc)= data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%Ann_KickLoc)+ ExpansionVolume
        
            !if (data%State%MUD(4)%Q > 0.) then
            !    
            !    
            !    if (abs(ChokeLine_Density%Array(1)-Ann_Density%Last())< DensityMixTol) then
            !        ChokeLine_MudDischarged_Volume%Array(1)= ChokeLine_MudDischarged_Volume%Array(1) + ExpansionVolume
            !    else 
            !        call ChokeLine_Density%AddToFirst (Ann_Density%Last())
            !        call ChokeLine_MudDischarged_Volume%AddToFirst (ExpansionVolume)     ! farz kardam ke hameye hajm ro ba yek density ezafe konim
            !        call ChokeLine_Mud_Forehead_X%AddToFirst (0.0d0)
            !        call ChokeLine_Mud_Forehead_section%AddToFirst (1)
            !        call ChokeLine_Mud_Backhead_X%AddToFirst (0.0d0)
            !        call ChokeLine_Mud_Backhead_section%AddToFirst (1)
            !        call ChokeLine_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
            !        call ChokeLine_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
            !        call ChokeLine_MudOrKick%AddToFirst (Ann_MudOrKick%Last()) 
            !    endif
            !    
            !endif
            
        endif
!========================================================================================           
        
       
        
        
        
!=============================== foreheade dar choke line  bashad =============================
        
        if (data%State%MudSystem%ChokeLine_KickLoc > 0 .and. data%State%MudSystem%Ann_KickLoc==0) then
        
            data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(data%State%MudSystem%ChokeLine_KickLoc)= data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(data%State%MudSystem%ChokeLine_KickLoc)+ ExpansionVolume
            
        endif
        
        
!========================================================================================                  
      
     !write(*,*) 'Expansion======0'
    !    !do imud=1, Ann_MudDischarged_Volume%Length()
    !    !    write(*,*) 'Ann:', imud, Ann_MudDischarged_Volume%Array(imud), Ann_Density%Array(imud) ,Ann_MudOrKick%Array(imud)
    !    !enddo
    !    
    !    do imud=1, Op_MudDischarged_Volume%Length()
    !        write(*,*) 'Op:', imud, Op_MudDischarged_Volume%Array(imud), Op_Density%Array(imud) ,Op_MudOrKick%Array(imud)
    !    enddo
    !write(*,*) '0======expansion'        
        
        
        
    end subroutine Kick_Expansion   
    
    
    
    
    
subroutine Kick_Contraction    !  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
        USE CError

   
   implicit none
   
   integer jelement, jmud, jsection,ielement,i
   integer jopelement,jopmud,jopsection
   real(8) ContractionVolume
   
   
!*********************************************************
 
!       contraction is always with pump flow
   
!*********************************************************
            !write(*,*) 'Kick Contraction'

        !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 (data%State%MudSystem%NewPipeFilling == 0) then
            data%State%MudSystem%StringFlowRate= 0.
            data%State%MudSystem%AnnulusFlowRate= 0.
        endif         
        
        
        

        
        !if (WellHeadIsOpen) then
            ContractionVolume= - GasPocketDeltaVol%Array(data%State%MudSystem%NewInfluxNumber - data%State%MudSystem%KickNumber + 1) * 7.48
        !else
            !ContractionVolume = (data%State%MudSystem%StringFlowRate/60.0d0)*DeltaT_Mudline + DeltaVolumePipe
            if (data%State%MudSystem%KickNumber == 1 .and. data%State%MudSystem%WellHeadIsOpen==.false.)     ContractionVolume = ContractionVolume + (data%State%MudSystem%StringFlowRate/60.0d0)*data%State%MudSystem%DeltaT_Mudline + data%State%MudSystem%DeltaVolumePipe
        !endif
        
        
!**************************************************************************************************************************************************************************        
            
            ! pump mud is added in "pump&TripIn" code
                
            IF (data%State%MudSystem%Op_KickLoc > 0 .and. data%State%MudSystem%Ann_KickLoc == 0) then   ! All of kick is under bit (iloc == 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) - ( ContractionVolume )
                
            ELSE IF (data%State%MudSystem%Op_KickLoc == 0 .AND. data%State%MudSystem%Ann_KickLoc > 0 .AND. data%State%MudSystem%ChokeLine_KickLoc == 0) THEN    ! All of kick is an Annulus (iloc == 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) - ( ContractionVolume )
                
            ELSE IF (data%State%MudSystem%Ann_KickLoc == 0 .AND. data%State%MudSystem%ChokeLine_KickLoc > 0) THEN   ! kick is in chokeline only
                
                data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(data%State%MudSystem%ChokeLine_KickLoc)= data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(data%State%MudSystem%ChokeLine_KickLoc) - ( ContractionVolume )
                
            ELSE IF (data%State%MudSystem%Op_KickLoc > 0 .AND. data%State%MudSystem%Ann_KickLoc > 0) THEN   ! Kick is around bit (iloc==2)
                
                if (data%State%MudSystem%Ann_MudDischarged_Volume%Array(1) > ContractionVolume ) then
                    
                    data%State%MudSystem%Ann_MudDischarged_Volume%Array(1)= data%State%MudSystem%Ann_MudDischarged_Volume%Array(1) - ( ContractionVolume )
                    
                    
                elseif (data%State%MudSystem%Op_MudDischarged_Volume%Last() > ContractionVolume ) then
                    
                    data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_MudDischarged_Volume%Length())= data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_MudDischarged_Volume%Length()) - ( ContractionVolume )
                    
                else
                    Call ErrorStop ('kick contraction error 1')
                endif
                
            ELSE IF (data%State%MudSystem%Ann_KickLoc > 0 .AND. data%State%MudSystem%ChokeLine_KickLoc > 0) THEN
                
                if (data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(1) > ContractionVolume ) then
                    
                    data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(1) = data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(1) - ( ContractionVolume )
                    
                    
                elseif (data%State%MudSystem%Ann_MudDischarged_Volume%Last() > ContractionVolume ) then
                    
                    data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%Ann_MudDischarged_Volume%Length())= data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%Ann_MudDischarged_Volume%Length()) - ( ContractionVolume )
                    
                else
                    Call ErrorStop ('kick contraction error 2')
                endif
                
                

            endif

    ! write(*,*) 'contract======0'
    !!    !do imud=1, Ann_MudDischarged_Volume%Length()
    !!    !    write(*,*) 'Ann:', imud, Ann_MudDischarged_Volume%Array(imud), Ann_Density%Array(imud) ,Ann_MudOrKick%Array(imud)
    !!    !enddo
    !!    
    !    do imud=1, Op_MudDischarged_Volume%Length()
    !        write(*,*) 'Op:', imud, Op_MudDischarged_Volume%Array(imud), Op_Density%Array(imud) ,Op_MudOrKick%Array(imud)
    !    enddo
    !write(*,*) '0======contract'

end subroutine Kick_Contraction   
