 

SUBROUTINE KILL_LINE
    use SimulationVariables
    USE CAccumulatorVariables
    USE CBopStackVariables
    use CBopControlPanel
    use SimulationVariables
    USE CEquipmentsConstants
    ! use CSimulationVariables
    
    implicit none
     
    
!=====================================================================
!                 KILL LINE 1- BOP CAMERON Type U 5000
!                   START CONDITIONS FOR KILL LINE 1
!=====================================================================  
    
      data%State%RAM(6)%SuccessionCounter = data%State%RAM(6)%SuccessionCounter + 1 
      
      
       if (data%EquipmentControl%BopControlPanel%KillLineValve == -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(6)%SuccessionCounter /= data%State%RAM(6)%SuccessionCounterOld+1 ) then
            data%State%RAM(6)%SuccessionCounter = 0   ! also in starup
            data%State%RAM(6)%SuccessionCounterOld = 0   ! also in starup
            return   
        else
            data%State%RAM(6)%SuccessionCounterOld= data%State%RAM(6)%SuccessionCounter        
        endif
        
        
        if ( data%State%RAM(6)%SuccessionCounter < int(2.5/data%State%RamLine%DeltaT_BOP) ) then
            return
        endif
           
           data%State%RAM(6)%First_CloseTimecheck= 1              
           
           
           
         if (data%State%BopStackInput%KillLineOpenLedMine == LedOn) then 
             RETURN            
         end if
            data%State%KillLine%closed=0
            data%State%RAM(6)%vdis_tot=0
            data%State%RAM(6)%vdis_bottles=0.
            data%State%RAM(6)%fvr_air=0.
            data%State%RAM(6)%vdis_elecp=0.
            data%State%Pumps%Qiter=7
            data%State%RAM(6)%Qzero=70
            data%State%RAM(6)%Q=data%State%RAM(6)%Qzero
            data%State%RAM(6)%flow=70
            data%State%RAM(6)%tol=0.0037
            if (data%State%KillLine%finished==1) then
               data%State%KillLine%LeverOld= 1.0
           else
               data%State%KillLine%LeverOld=data%EquipmentControl%BopControlPanel%KillLineValve
           endif
           data%State%KillLine%finished=0
            data%State%KillLine%IsOpening = .true.
            data%EquipmentControl%BopControlPanel%KillLineCloseLED = LedOff
            data%State%BopStackInput%KillLineCloseLedMine = LedOff
            data%EquipmentControl%BopControlPanel%KillLineOpenLED = LedOn    !LedBlinking
            data%State%RAM(6)%bop_type = 3
            !AbopKillLine=196.67
            data%State%KillLine%Abop=(data%Configuration%BopStack%KillClose*231)/((data%State%KillLine%IDBase-data%State%KillLine%ODDrillpipe_inBase)/2.)
            data%State%KillLine%NeededVolume=data%State%KillLine%Abop*(data%State%KillLine%IDBase-max(data%State%KillLine%ODDrillpipe_in,data%State%KillLine%ODDrillpipe_inBase))/(2.*231)  !1.5 galon for each BOP
        endif
       
        if (data%EquipmentControl%BopControlPanel%KillLineValve == 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(6)%SuccessionCounter /= data%State%RAM(6)%SuccessionCounterOld+1 ) then
                data%State%RAM(6)%SuccessionCounter = 0   ! also in starup
                data%State%RAM(6)%SuccessionCounterOld = 0   ! also in starup
                return   
            else
                data%State%RAM(6)%SuccessionCounterOld= data%State%RAM(6)%SuccessionCounter
            endif
        
        
            if ( data%State%RAM(6)%SuccessionCounter < int(2.5/data%State%RamLine%DeltaT_BOP) ) then
                return
            endif
               
               data%State%RAM(6)%First_OpenTimecheck= 1             
            
            
            
         if (data%State%BopStackInput%KillLineCloseLedMine == LedOn) then 
             RETURN            
         end if
            data%State%KillLine%closed=0
            data%State%RAM(6)%vdis_tot=0
            data%State%RAM(6)%vdis_bottles=0.
            data%State%RAM(6)%fvr_air=0.
            data%State%RAM(6)%vdis_elecp=0.
            data%State%Pumps%Qiter=7
            data%State%RAM(6)%Qzero=70
            data%State%RAM(6)%Q=data%State%RAM(6)%Qzero
            data%State%RAM(6)%flow=70
            data%State%RAM(6)%tol=0.0037
            
            

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

        
         !if (KillLineCloseLed == LedOn) then 
         !    RETURN            
         !end if
         
            data%EquipmentControl%BopControlPanel%KillLineCloseLED = LedOff    !new
            data%State%BopStackInput%KillLineCloseLedMine = LedOff    !new             
            
            data%EquipmentControl%BopControlPanel%KillLineCloseLED = LedOn    !LedBlinking
            data%State%RAM(6)%bop_type = 3
            !AbopKillLine=196.67
            data%State%KillLine%Abop=(data%Configuration%BopStack%KillOpen*231)/((data%State%KillLine%IDBase-data%State%KillLine%ODDrillpipe_inBase)/2.)
            data%State%KillLine%NeededVolume=data%State%KillLine%Abop*(data%State%KillLine%IDBase-max(data%State%KillLine%ODDrillpipe_in,data%State%KillLine%ODDrillpipe_inBase))/(2.*231)  !1.5 galon for each BOP
        endif
        
!==========================================================================      
    
if (data%State%KillLine%IsOpening .or. data%State%KillLine%IsClosing) then
        CALL KILL_LINE_SUB
end if    
    
    
    END SUBROUTINE KILL_LINE
    
    
    
    
    
    
    
    
    
    
    
SUBROUTINE KILL_LINE_SUB   
    
    use SimulationVariables
    USE CAccumulatorVariables
    USE CBopStackVariables
    use CBopControlPanel
    use SimulationVariables
    USE CEquipmentsConstants
    ! use CSimulationVariables

    implicit none
    
    
    data%State%Annular%FirstSet= 0
    data%State%AnnularComputational%RamsFirstSet= 0
    
    ! loop6: do while (finished_KillLine==0)
        
        
            data%State%RAM(6)%SuccessionCounter = data%State%RAM(6)%SuccessionCounter + 1
       
        
        
           if (data%EquipmentControl%BopControlPanel%KillLineValve == 1.0 .and. data%State%KillLine%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(6)%First_CloseTimecheck == 0 ) then 
                    
                    
                    if ( data%State%RAM(6)%SuccessionCounter /= data%State%RAM(6)%SuccessionCounterOld+1 ) then
                        data%State%RAM(6)%SuccessionCounter = 0   ! also in starup
                        data%State%RAM(6)%SuccessionCounterOld = 0   ! also in starup
                        return   
                    else
                        data%State%RAM(6)%SuccessionCounterOld= data%State%RAM(6)%SuccessionCounter        
                    endif
        

        
                    if ( data%State%RAM(6)%SuccessionCounter < int(2.5/data%State%RamLine%DeltaT_BOP) ) then
                        return
                    endif
           
                endif               
               
               
               
            data%State%KillLine%closed=0
            data%State%RAM(6)%p_bop=data%State%ShearRAM%PA
            data%State%KillLine%LeverOld = data%EquipmentControl%BopControlPanel%KillLineValve
            data%EquipmentControl%BopControlPanel%KillLineOpenLED = LedOff
            data%State%BopStackInput%KillLineOpenLedMine = LedOff
            data%EquipmentControl%BopControlPanel%KillLineCloseLED = LedOn    !LedBlinking
            CALL OpenKillLine
            data%State%RAM(6)%bop_type = 3
            !AbopKillLine=196.67
            data%State%KillLine%Abop=(data%Configuration%BopStack%KillClose*231)/((data%State%KillLine%IDBase-data%State%KillLine%ODDrillpipe_inBase)/2.)
            data%State%KillLine%NeededVolume=data%State%KillLine%Abop*(data%State%KillLine%ID-max(data%State%KillLine%ODDrillpipe_in,data%State%KillLine%ODDrillpipe_inBase))/(2.*231)
            
            data%State%RAM(6)%vdis_bottles=0.
            data%State%RAM(6)%fvr_air=0.
            data%State%RAM(6)%vdis_elecp=0.
            data%State%KillLine%IsClosing = .true.
            data%State%KillLine%IsOpening = .false.
        endif
        
        if (data%EquipmentControl%BopControlPanel%KillLineValve == -1.0 .and. data%State%KillLine%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(6)%First_OpenTimecheck == 0 ) then

                    if ( data%State%RAM(6)%SuccessionCounter /= data%State%RAM(6)%SuccessionCounterOld+1 ) then
                        data%State%RAM(6)%SuccessionCounter = 0   ! also in starup
                        data%State%RAM(6)%SuccessionCounterOld = 0   ! also in starup
                        return   
                    else
                        data%State%RAM(6)%SuccessionCounterOld= data%State%RAM(6)%SuccessionCounter        
                    endif
        
                    if ( data%State%RAM(6)%SuccessionCounter < int(2.5/data%State%RamLine%DeltaT_BOP) ) then
                        return
                    endif
           
                endif            
            
            
            data%State%KillLine%closed=0
            data%State%RAM(6)%p_bop=data%State%ShearRAM%PA
            data%State%KillLine%LeverOld = data%EquipmentControl%BopControlPanel%KillLineValve
            data%EquipmentControl%BopControlPanel%KillLineCloseLED = LedOff
            data%State%BopStackInput%KillLineCloseLedMine = LedOff
            data%EquipmentControl%BopControlPanel%KillLineOpenLED = LedOn    !LedBlinking
            CALL OpenKillLine
            data%State%RAM(6)%bop_type = 3
            !AbopKillLine=196.67
            data%State%KillLine%Abop=(data%Configuration%BopStack%KillOpen*231)/((data%State%KillLine%IDBase-data%State%KillLine%ODDrillpipe_inBase)/2.)
            data%State%KillLine%NeededVolume=data%State%KillLine%Abop*(data%State%KillLine%IDBase-data%State%KillLine%ID)/(2.*231)
            data%State%RAM(6)%vdis_bottles=0.
            data%State%RAM(6)%fvr_air=0.
            data%State%RAM(6)%vdis_elecp=0.

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

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

 

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

data%State%RAM(6)%timecounter_ram=data%State%RAM(6)%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(6)%time,data%State%RAM(6)%Q,data%State%RAM(6)%vdis_tot,p_acc, &
!         pram_reg,Pannular_reg,data%State%RAM(6)%p_bop,IDshearBop, &
!         IDPipeRam1,IDPipeRam2,IDAnnular
!60       FORMAT(11(f18.5))
         
         
        !   call sleepqq(100)
          
if (data%State%KillLine%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. ChokeLineValve==1. .or. ChokeLineValve==-1.0) then
        data%State%KillLine%finished=1
 !   endif
endif          
          
        !   if (IsStopped == .true.) return 
          
    ! end do  loop6   !while finished_KillLine==0
    
END SUBROUTINE KILL_LINE_SUB 