subroutine CirculationCodeSelect    !  is called in subroutine Fluid_Flow_Solver
    
    use KickVARIABLESModule
    USE MudSystemVARIABLES
use SimulationVariables !@@@
    USE TD_DrillStemComponents
    Use CUnityInputs
    Use CUnityOutputs
    use OperationScenariosModule
    use UTUBEVARSModule
    use sROP_Variables
    use PressureDisplayVARIABLESModule
    
    

    implicit none
    
    
    Integer i,KickNumber
    
    !NewInfluxNumber = NoGasPocket
    
    data%State%MudSystem%Flow_timeCounter= data%State%MudSystem%Flow_timeCounter+1
    
            !if (ChokePanelStrokeResetSwitch == 1) then
            !    Flow_timeCounter= 0
            !endif     
    
    
    !write(*,*) 'Flow_timeCounter' , Flow_timeCounter
    
!===========================Shoe Lost=============================== 

           call ShoeLostSub
           
!===================================================================    
    
      
            data%State%MudSystem%iLoc= 1 ! will be changed in KickFlux and Migration or Pump and TripIn (save OP Mud data)
            !KickMigration_2SideBit= .false.
            Call Set_FlowPipeDisconnect(.false.)
            Call Set_FlowKellyDisconnect(.false.)
  
            call ElementsCreation
            

            
            
            if (data%State%MUD(8)%Q > 0.0)     call FillingWell_By_BellNipple        ! Filling Well Through BellNipple ( Path j11 )
            !if (data%State%MUD(10)%Q > 0.0)     call FillingWell_By_Pumps        ! Filling Well Through Pumps ( Path j19 )
            
                !write(*,*) 'TD_RemoveVolume,Get_JointConnectionPossible=' , TD_RemoveVolume,Get_JointConnectionPossible()
            
            if (data%State%TD_Vol%RemoveVolume > 0.)  call DisconnectingPipe     !! .and. Get_JointConnectionPossible() == .false.) call DisconnectingPipe
            
            
            IF (KickVARIABLES%KickFlux .AND. NOT(KickVARIABLES%KickOffBottom)) THEN 
                call Kick_Influx
            endif
            
            
            
            
            IF ( data%State%MudSystem%NewInfluxNumber > 0 ) THEN
                call Kick_Migration
            endif
            
!   ============================ must be after migration ==============================            

        DO KickNumber= data%State%MudSystem%NewInfluxNumber-KickVARIABLES%NoGasPocket+1 , data%State%MudSystem%NewInfluxNumber
            ! FINDING NEW KICK LOCATIONS:
               data%State%MudSystem%Ann_KickLoc= 0
               data%State%MudSystem%Op_KickLoc= 0
               data%State%MudSystem%ChokeLine_KickLoc= 0
               
            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%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%ChokeLine_MudOrKick%Length ()
                if (data%State%MudSystem%ChokeLine_MudOrKick%Array(i) == KickNumber) then
                    data%State%MudSystem%ChokeLine_KickLoc = i
                    exit
                endif
            end do                
            
            !   ============================ must be after migration-end ===========================            
            
            IF (ALLOCATED(GasPocketWeight%Array) .and. KickNumber == data%State%MudSystem%NewInfluxNumber .AND. NOT(KickVARIABLES%KickOffBottom) .AND. data%State%MudSystem%WellHeadIsOpen) THEN

                cycle
                
            ELSE IF (ALLOCATED(GasPocketWeight%Array)) THEN
                
                if (((GasPocketDeltaVol%Array(data%State%MudSystem%NewInfluxNumber - KickNumber + 1) > 0.0 .AND. data%State%MudSystem%WellHeadIsOpen) .or. data%State%MudSystem%Kickexpansion_DueToMudLost) )   call Kick_Expansion
                    
                if ((GasPocketDeltaVol%Array(data%State%MudSystem%NewInfluxNumber - KickNumber + 1) < 0.0 ) .OR. data%State%MudSystem%WellHeadIsOpen == .FALSE.)   CALL Kick_Contraction

            ENDIF
        ENDDO       
        data%State%MudSystem%KickNumber = KickNumber       
        data%State%MudSystem%LostInTripOutIsDone= .false.  
        
        if( data%State%MudSystem%DeltaVolumeOp >= 0.0 .and. Get_KellyConnection()==KELLY_CONNECTION_STRING) then    
            !write(*,*) 'DeltaVolumeOp=' , DeltaVolumeOp
            call Pump_and_TripIn
        elseif (data%State%MudSystem%DeltaVolumeOp < 0.0) then
            ! when we have Utube and tripping out simultaneously, it uses "TripOut_and_Pump" subroutine, and then Utube code is done
            ! "Utube" and "Pump_and_TripIn" subroutines, not to be used simultaneously because "Utube" code supports trip in
            call TripOut_and_Pump
        endif
        
        data%State%MudSystem%WellOutletDensity= data%State%MudSystem%Ann_Density%Last()   !    (ppg) used in MudSystem    
            
            
            
        if (data%State%MUD(4)%Q > 0.) then          !     ( j4 > 0 )   ! THIS CIRCULATION CODE IS JUST FOR LINE J4, AND NOT NEEDED FOR LINE J18
            call ChokeLineMud
        endif
            
            
        call Choke_GasSound
            
        !WRITE(*,*) 'CIRCU-Ann_Saved_MudDischarged_Volume' , Ann_Saved_MudDischarged_Volume
            
        !****Utube is called in Plot Subroutine****
        
        Call Instructor_CirculationMud_Edit
            
      
                
        call PlotFinalMudElements
        
        data%State%MudSystem%MudChecked= .true.
        data%State%MudSystem%UtubePossibility= .true.
        !WRITE(*,*) '***********************************************************************'     
    
end subroutine CirculationCodeSelect    