 

SUBROUTINE CHOKE_LINE
    use SimulationVariables
    USE CAccumulatorVariables
    USE CBopStackVariables
    use CBopControlPanel
    use SimulationVariables
    USE CEquipmentsConstants

    implicit none

    
!=====================================================================
!                 CHOKE LINE 1- BOP CAMERON Type U 5000
!                   START CONDITIONS FOR CHOKE LINE 1
!=====================================================================
    
      data%State%RAM(5)%SuccessionCounter = data%State%RAM(5)%SuccessionCounter + 1    
    
    
       if (data%EquipmentControl%BopControlPanel%ChokeLineValve == -1.0 .and. data%State%BopStackAcc%RigAirMalf==0 .and. data%EquipmentControl%BopControlPanel%AirMasterValve==1 .and. data%State%RamLine%P_ACC>data%State%BopStackAcc%acc_MinPressure) then
           
        if ( data%State%RAM(5)%SuccessionCounter /= data%State%RAM(5)%SuccessionCounterOld+1 ) then
            data%State%RAM(5)%SuccessionCounter = 0   ! also in starup
            data%State%RAM(5)%SuccessionCounterOld = 0   ! also in starup
            return   
        else
            data%State%RAM(5)%SuccessionCounterOld= data%State%RAM(5)%SuccessionCounter        
        endif
        
        
        if ( data%State%RAM(5)%SuccessionCounter < int(2.5/data%State%RamLine%DeltaT_BOP) ) then
            return
        endif
           
           data%State%RAM(5)%First_CloseTimecheck= 1 
           
           
           
           
         if (data%State%BopStackInput%ChokeLineOpenLEDMine == LedOn) then 
             RETURN             
         end if
            data%State%ChokeLine%closed=0
            data%State%RAM(5)%vdis_tot=0
            data%State%RAM(5)%vdis_bottles=0.
            data%State%RAM(5)%fvr_air=0.
            data%State%RAM(5)%vdis_elecp=0.
            data%State%Pumps%Qiter=7
            data%State%RAM(5)%Qzero=70
            data%State%RAM(5)%Q=data%State%RAM(5)%Qzero
            data%State%RAM(5)%flow=70
            data%State%RAM(5)%tol=0.0037
            if (data%State%ChokeLine%finished==1) then
               data%State%ChokeLine%LeverOld= 1.0
           else
               data%State%ChokeLine%LeverOld=data%EquipmentControl%BopControlPanel%ChokeLineValve
           endif
           data%State%ChokeLine%finished=0
            data%State%ChokeLine%IsOpening = .true.
            data%EquipmentControl%BopControlPanel%ChokeLineCloseLED = LedOff
            data%State%BopStackInput%ChokeLineCloseLEDMine = LedOff
            data%EquipmentControl%BopControlPanel%ChokeLineOpenLED = LedOn    !LedBlinking
            data%State%RAM(5)%bop_type = 3
            !AbopChokeLine=196.67
            data%State%ChokeLine%Abop=(data%Configuration%BopStack%ChokeClose*231)/((data%State%ChokeLine%IDBase-data%State%ChokeLine%ODDrillpipe_inBase)/2.)
            data%State%ChokeLine%NeededVolume=data%State%ChokeLine%Abop*(data%State%ChokeLine%IDBase-max(data%State%ChokeLine%ODDrillpipe_in,data%State%ChokeLine%ODDrillpipe_inBase))/(2.*231)  !1.5 galon for each BOP
        endif
       
        if (data%EquipmentControl%BopControlPanel%ChokeLineValve == 1.0 .and. data%State%BopStackAcc%RigAirMalf==0 .and. data%EquipmentControl%BopControlPanel%AirMasterValve==1 .and. data%State%RamLine%P_ACC>data%State%BopStackAcc%acc_MinPressure) then
            
            
            if ( data%State%RAM(5)%SuccessionCounter /= data%State%RAM(5)%SuccessionCounterOld+1 ) then
                data%State%RAM(5)%SuccessionCounter = 0   ! also in starup
                data%State%RAM(5)%SuccessionCounterOld = 0   ! also in starup
                return   
            else
                data%State%RAM(5)%SuccessionCounterOld= data%State%RAM(5)%SuccessionCounter
            endif
        
        
            if ( data%State%RAM(5)%SuccessionCounter < int(2.5/data%State%RamLine%DeltaT_BOP) ) then
                return
            endif
               
               data%State%RAM(5)%First_OpenTimecheck= 1             
            
            
            
         if (data%State%BopStackInput%ChokeLineCloseLEDMine == LedOn) then 
             RETURN             
         end if
            data%State%ChokeLine%closed=0
            data%State%RAM(5)%vdis_tot=0
            data%State%RAM(5)%vdis_bottles=0.
            data%State%RAM(5)%fvr_air=0.
            data%State%RAM(5)%vdis_elecp=0.
            data%State%Pumps%Qiter=7
            data%State%RAM(5)%Qzero=70
            data%State%RAM(5)%Q=data%State%RAM(5)%Qzero
            data%State%RAM(5)%flow=70
            data%State%RAM(5)%tol=0.0037
            
            

                if (data%State%ChokeLine%finished==1) then
                    data%State%ChokeLine%LeverOld= -1.0
                else
                    data%State%ChokeLine%LeverOld=data%EquipmentControl%BopControlPanel%ChokeLineValve
                endif
            data%State%ChokeLine%finished=0
            data%State%ChokeLine%IsClosing = .true.

        
         !if (ChokeLineCloseLED == LedOn) then 
         !    RETURN             
         !end if
         
            data%EquipmentControl%BopControlPanel%ChokeLineCloseLED = LedOff    !new
            data%State%BopStackInput%ChokeLineCloseLEDMine = LedOff    !new             
            
            
            data%EquipmentControl%BopControlPanel%ChokeLineCloseLED = LedOn    !LedBlinking
            data%State%RAM(5)%bop_type = 3
            !AbopChokeLine=196.67
            data%State%ChokeLine%Abop=(data%Configuration%BopStack%ChokeOpen*231)/((data%State%ChokeLine%IDBase-data%State%ChokeLine%ODDrillpipe_inBase)/2.)
            data%State%ChokeLine%NeededVolume=data%State%ChokeLine%Abop*(data%State%ChokeLine%IDBase-max(data%State%ChokeLine%ODDrillpipe_in,data%State%ChokeLine%ODDrillpipe_inBase))/(2.*231)  !1.5 galon for each BOP
        endif

        
!=====================================================================    
    
! if (ChokeLineIsOpening .or. ChokeLineIsClosing) then
!         CALL CHOKE_LINE_SUB
! end if    
    
    
    END SUBROUTINE CHOKE_LINE  
    
    
    
    
    
    
    
    
    
    
    
    
SUBROUTINE CHOKE_LINE_SUB  

    use SimulationVariables
    USE CAccumulatorVariables
    USE CBopStackVariables
    use CBopControlPanel
    use SimulationVariables
    USE CEquipmentsConstants
    ! use CSimulationVariables
    implicit none  
    
    
    ! FirstSet= 0
    ! RamsFirstSet= 0
    
    ! loop5: do while (finished_ChokeLine==0)
        
        
             data%State%RAM(5)%SuccessionCounter = data%State%RAM(5)%SuccessionCounter + 1        
        
        
           if (data%EquipmentControl%BopControlPanel%ChokeLineValve == 1.0 .and. data%State%ChokeLine%LeverOld == -1.0 .and. data%State%BopStackAcc%RigAirMalf==0 .and. data%EquipmentControl%BopControlPanel%AirMasterValve==1 .and. data%State%RamLine%P_ACC>data%State%BopStackAcc%acc_MinPressure) then
               
                if ( data%State%RAM(5)%First_CloseTimecheck == 0 ) then 
                    
                    
                    if ( data%State%RAM(5)%SuccessionCounter /= data%State%RAM(5)%SuccessionCounterOld+1 ) then
                        data%State%RAM(5)%SuccessionCounter = 0   ! also in starup
                        data%State%RAM(5)%SuccessionCounterOld = 0   ! also in starup
                        return   
                    else
                        data%State%RAM(5)%SuccessionCounterOld= data%State%RAM(5)%SuccessionCounter        
                    endif
        

        
                    if ( data%State%RAM(5)%SuccessionCounter < int(2.5/data%State%RamLine%DeltaT_BOP) ) then
                        return
                    endif
           
                endif               
               
               
               
            data%State%ChokeLine%closed=0
            data%State%RAM(5)%p_bop=data%State%ShearRAM%PA
            data%State%ChokeLine%LeverOld = data%EquipmentControl%BopControlPanel%ChokeLineValve
            data%EquipmentControl%BopControlPanel%ChokeLineOpenLED = LedOff
            data%State%BopStackInput%ChokeLineOpenLEDMine = LedOff
            data%EquipmentControl%BopControlPanel%ChokeLineCloseLED = LedOn    !LedBlinking
            CALL OpenChokeLine
            data%State%RAM(5)%bop_type = 3
            !AbopChokeLine=196.67
            data%State%ChokeLine%Abop=(data%Configuration%BopStack%ChokeClose*231)/((data%State%ChokeLine%IDBase-data%State%ChokeLine%ODDrillpipe_inBase)/2.)
            data%State%ChokeLine%NeededVolume=data%State%ChokeLine%Abop*(data%State%ChokeLine%ID-max(data%State%ChokeLine%ODDrillpipe_in,data%State%ChokeLine%ODDrillpipe_inBase))/(2.*231)
            
            data%State%RAM(5)%vdis_bottles=0.
            data%State%RAM(5)%fvr_air=0.
            data%State%RAM(5)%vdis_elecp=0.
            data%State%ChokeLine%IsClosing = .true.
            data%State%ChokeLine%IsOpening = .false.
        endif
        
        if (data%EquipmentControl%BopControlPanel%ChokeLineValve == -1.0 .and. data%State%ChokeLine%LeverOld == 1.0 .and. data%State%RamLine%P_ACC>data%State%BopStackAcc%acc_MinPressure .and. data%State%BopStackAcc%RigAirMalf==0 .and. data%EquipmentControl%BopControlPanel%AirMasterValve==1) then 
            
                if ( data%State%RAM(5)%First_OpenTimecheck == 0 ) then

                    if ( data%State%RAM(5)%SuccessionCounter /= data%State%RAM(5)%SuccessionCounterOld+1 ) then
                        data%State%RAM(5)%SuccessionCounter = 0   ! also in starup
                        data%State%RAM(5)%SuccessionCounterOld = 0   ! also in starup
                        return   
                    else
                        data%State%RAM(5)%SuccessionCounterOld= data%State%RAM(5)%SuccessionCounter        
                    endif
        
                    if ( data%State%RAM(5)%SuccessionCounter < int(2.5/data%State%RamLine%DeltaT_BOP) ) then
                        return
                    endif
           
                endif            
            
            
            
            data%State%ChokeLine%closed=0
            data%State%RAM(5)%p_bop=data%State%ShearRAM%PA
            data%State%ChokeLine%LeverOld = data%EquipmentControl%BopControlPanel%ChokeLineValve
            data%EquipmentControl%BopControlPanel%ChokeLineCloseLED = LedOff
            data%State%BopStackInput%ChokeLineCloseLEDMine = LedOff
            data%EquipmentControl%BopControlPanel%ChokeLineOpenLED = LedOn    !LedBlinking
            CALL OpenChokeLine
            data%State%RAM(5)%bop_type = 3
            !AbopChokeLine=196.67
            data%State%ChokeLine%Abop=(data%Configuration%BopStack%ChokeOpen*231)/((data%State%ChokeLine%IDBase-data%State%ChokeLine%ODDrillpipe_inBase)/2.)
            data%State%ChokeLine%NeededVolume=data%State%ChokeLine%Abop*(data%State%ChokeLine%IDBase-data%State%ChokeLine%ID)/(2.*231)
            data%State%RAM(5)%vdis_bottles=0.
            data%State%RAM(5)%fvr_air=0.
            data%State%RAM(5)%vdis_elecp=0.

            data%State%ChokeLine%IsOpening = .true.
            data%State%ChokeLine%IsClosing = .false.
            endif


            data%State%RAM(5)%First_CloseTimecheck = 0
            data%State%RAM(5)%First_OpenTimecheck = 0
 
 
     data%State%RAM(5)%time=data%State%RAM(5)%time+data%State%RamLine%DeltaT_BOP  !overal time (s)

 

!===================================================
!                        BOP 
!===================================================
if (data%State%ChokeLine%closed==0) then    !bop closing
     call bop_code(4,data%State%ChokeLine%H_Bop,5)   !ramtype=4     5=RNUMBER
endif    !bop is closing
!================================================================
if (data%State%ChokeLine%closed==1) then
    data%State%RAM(5)%Q=0
    !p_bop=pram_reg
    data%State%RAM(5)%p_bop=data%State%ShearRAM%PA
    data%State%RAMS%minloss(5,17)=0.       !RNUMBER=5
endif

data%State%RAM(5)%timecounter_ram=data%State%RAM(5)%timecounter_ram+1





!        MiddleRamsStatus = IDshearBop
!        UpperRamsStatus = IDPipeRam1
!        LowerRamsStatus = IDPipeRam2
!        AnnularStatus = IDAnnular
!        AccumulatorPressureGauge = p_acc
!        ManifoldPressureGauge= pram_reg
!        AnnularPressureGauge=Pannular_reg
!
!        
!
!         WRITE(60,60) data%State%RAM(5)%time,data%State%RAM(5)%Q,data%State%RAM(5)%vdis_tot,p_acc, &
!         pram_reg,Pannular_reg,data%State%RAM(5)%p_bop,IDshearBop, &
!         IDPipeRam1,IDPipeRam2,IDAnnular
!60       FORMAT(11(f18.5))
         
         
        !   call sleepqq(100)
    if (data%State%ChokeLine%closed==1) then
    !   if ((UpperRamsValve==1. .and. UpperRamsFailureMalf==0) .or. (UpperRamsValve==-1.0 .and. UpperRamsFailureMalf==0) .or. (MiddleRamsValve==1. .and. MiddleRamsFailureMalf==0) .or. (MiddleRamsValve==-1.0 .and. MiddleRamsFailureMalf==0) .or. (LowerRamsValve==1. .and. LowerRamsFailureMalf==0) .or. (LowerRamsValve==-1.0 .and. LowerRamsFailureMalf==0) .or. (AnnularValve==1. .and. AnnularFailureMalf==0) .or. (AnnularValve==-1.0 .and. AnnularFailureMalf==0) .or. KillLineValve==1. .or. KillLineValve==-1.0) then
            data%State%ChokeLine%finished=1
    !   endif
endif          
          
        !   if (IsStopped == .true.) return 

    ! end do  loop5   !while finished_ChokeLine==0
    
END SUBROUTINE CHOKE_LINE_SUB