
SUBROUTINE BOP_StartUp()
use SimulationVariables
USE CAccumulatorVariables
USE CBopStackVariables
use CBopControlPanel
    use SimulationVariables
USE CEquipmentsConstants
implicit none   

integer i
    
    
!UpperRamsFailureMalf=0   
!AnnularFailureMalf=0    
!LowerRamsFailureMalf=0
!MiddleRamsFailureMalf=0
!UpperRamsLeakMalf=0
!LowerRamsLeakMalf=0
!MiddleRamsLeakMalf=0
!AnnularLeakMalf=0
!AccPupmsFailMalf=0
!AirSupplyPressureGauge=0
!======================= SETTING VARIABLES

data%State%BopStackInput%BOP_timeCounter= 0

!============== FOR MANIFOLD VALVES CODE=====================
CALL OpenAnnular
CALL OpenUpperRams
CALL OpenMiddleRams
CALL CloseKillLine
CALL CloseChokeLine
CALL OpenLowerRams

data%State%Annular%Annular_Situation_forTD= 0       ! open - for TD code
data%State%RamLine%ShearBop_Situation_forTD= 0       ! open - for TD code
data%State%PipeRam1%Situation_forTD= 0       ! open - for TD code
data%State%PipeRam2%Situation_forTD= 0       ! open - for TD code
!======================================================================

CALL LOSS_INPUTS()
!
!OPEN(50,FILE='AIRPUMP_OUTPUTS.DAT')
!OPEN(60,FILE='RAMS_OUTPUTS.DAT')
!======================================================================
!                            GET INPUTS  
!======================================================================

data%State%RAM%SuccessionCounter = 0
data%State%RAM%SuccessionCounterOld = 0

data%State%RAM%First_CloseTimecheck= 0
data%State%RAM%First_OpenTimecheck= 0



data%State%BopStackAcc%Cumulative_AirVolume= 0.0


data%State%BopStackAcc%BOTTLE_CAPACITY=10 !(GALON)
data%State%BopStackAcc%NOBOTTLES=data%Configuration%Accumulator%NumberOfBottels







!fvr_tot=40      !(GALON)  IN CHARGED POSITION
data%State%BopStackAcc%PRAMS_REGSET=1500       !RamsReglatorSet       !=1500 DEFAULT regulator set pressure (PSI)
data%State%BopStackAcc%acc_ChargedPressure=3000   !charged(PSI)
data%State%BopStackAcc%acc_MinPressure=data%Configuration%Accumulator%AccumulatorMinimumOperatingPressure        !1200      !discharged(PSI)
!acc_precharge=1000


data%State%BopStackAcc%FVR_TOT=(-2451*(data%State%BopStackAcc%acc_ChargedPressure**(-0.8202))+8.435)*data%State%BopStackAcc%NOBOTTLES    ! IT IS WRRITEN FOR PRECHARGE 1000 PSI FROM ITS CURVE

!write(*,*) 'fvr_tot=',fvr_tot

!ELECTRIC_PUMPON=2800
data%State%Pumps%ELECTRIC_ON=data%Configuration%Accumulator%StartPressure
!ELECTRIC_PUMPOFF=acc_ChargedPressure    !=3000 psi
data%State%Pumps%ELECTRIC_OFF=data%Configuration%Accumulator%StopPressure
!QELECTRIC_PUMP=12   !(gpm)
data%State%Pumps%QELECTRIC=data%Configuration%Accumulator%ElectricPumpOutput



!AIR_PUMPON=2600
data%State%Pumps%AIR_ON=data%Configuration%Accumulator%StartPressure2
!AIR_PUMPOFF=2900
data%State%Pumps%AIR_OFF=data%Configuration%Accumulator%StopPressure2



data%State%RAM%tol=0.0037  !%=(2700-2600)/2700
data%State%RAM%tolzero=data%State%RAM%tol

data%State%Annular%tolAnnular=0.0018   !=(2900-2895)/2900
data%State%Annular%tolzeroAnnular=data%State%Annular%tolAnnular
!======================OTHER INPUTS(CONSTANTS)===========================
data%State%ShearRAM%PA=300      !(PSI)
data%State%ShearRAM%P_SHEAR=1200  !(PSI)
!p_shear=2423.1  !(PSI)
data%State%ShearRAM%PB=data%State%ShearRAM%P_SHEAR-data%State%ShearRAM%PA
data%State%AnnularComputational%pa_annular=100 !(psi)

  


data%State%Annular%IDAnnularBase=13.625        !(inch)
data%State%Annular%IDAnnular=data%State%Annular%IDAnnularBase
data%State%Annular%ODDrillpipe_inAnnularBase=5.        ! so 18 gal is for complete closing of annular
data%State%Annular%ODDrillpipe_inAnnular=5.     ! initial
data%State%AnnularComputational%AnnularMovingPressure=360. !(psi)
data%State%ShearRAM%IDAnnularFinal= data%State%Annular%IDAnnular


data%State%ShearRAM%IDshearBopBase=13.625        !(inch)
data%State%ShearRAM%IDshearBop=data%State%ShearRAM%IDshearBopBase
data%State%ShearRAM%ODDrillpipe_inShearRamBase=5     !initial
data%State%ShearRAM%ODDrillpipe_inShearRam=5     !initial
data%State%ShearRAM%IDshearBopFinal= data%State%ShearRAM%IDshearBop

data%State%PipeRam1%IDBase=13.625        !(inch)
data%State%PipeRam1%ID=data%State%PipeRam1%IDBase
data%State%PipeRam1%ODDrillpipe_inBase=5     !initial
data%State%PipeRam1%ODDrillpipe_in=5     !initial
data%State%ShearRAM%IDPipeRam1Final= data%State%PipeRam1%ID


data%State%PipeRam2%ID=data%State%PipeRam1%IDBase        !(inch)
data%State%PipeRam2%ODDrillpipe_inBase=5     !initial
data%State%PipeRam2%ODDrillpipe_in=5     !initial
data%State%ShearRAM%IDPipeRam2Final= data%State%PipeRam2%ID

data%State%ChokeLine%IDBase=8.6        !(inch)
data%State%ChokeLine%ID=data%State%ChokeLine%IDBase
data%State%ChokeLine%ODDrillpipe_inBase=5
data%State%ChokeLine%ODDrillpipe_in=5

data%State%KillLine%IDBase=8.6        !(inch)
data%State%KillLine%ID=data%State%KillLine%IDBase
data%State%KillLine%ODDrillpipe_inBase=5
data%State%KillLine%ODDrillpipe_in=5


!va=4   !(liter)
!vb=8  !(liter)

!cv=2;  !flow coefficinet of regulator

data%State%ShearRAM%RAM_COURSE=320.2   !milimeter
data%State%ShearRAM%H_REGRAM=0 !(m)<<<<<<<<<<<<<<<<<<<<<<<<
data%State%ShearRAM%H_ShearRamBop=(data%Configuration%BopStack%GroundLevel-data%Configuration%BopStack%BlindRamHeight)*0.3048   ! foot to meter
data%State%PipeRam1%H=(data%Configuration%BopStack%GroundLevel-data%Configuration%BopStack%UpperRamHeight)*0.3048   ! foot to meter
data%State%PipeRam2%H_Bop=(data%Configuration%BopStack%GroundLevel-data%Configuration%BopStack%LowerRamHeight)*0.3048   ! foot to meter
data%State%Annular%H_AnnularBop=(data%Configuration%BopStack%GroundLevel-data%Configuration%BopStack%AnnularPreventerHeight)*0.3048   ! foot to meter
data%State%ChokeLine%H_Bop=(data%Configuration%BopStack%GroundLevel-data%Configuration%BopStack%KillHeight)*0.3048   ! foot to meter
data%State%KillLine%H_Bop=(data%Configuration%BopStack%GroundLevel-data%Configuration%BopStack%KillHeight)*0.3048   ! foot to meter


data%State%RamLine%P_ACC=data%State%BopStackAcc%acc_ChargedPressure
data%State%RAM%vdis_bottles=0   !initial discharged volume
data%State%RamLine%FVR=data%State%BopStackAcc%FVR_TOT
data%State%RAMS%minloss=0.

!======================AIRPUMP INPUTS(CONSTANTS)===========================

data%State%RAM%FVR_AIR=0
data%State%Pumps%P_AIRP=0

data%State%RamLine%BA1=1003; data%State%RamLine%BA2=.03375; data%State%RamLine%BA3=4.014; data%State%RamLine%BA4=.2458
data%State%RamLine%BBA1 =31.8; data%State%RamLine%BBA2 =-725.7 ; data%State%RamLine%BBA3 =4154

data%State%Pumps%Qiter=7  !(gpm)

! Q=0.0003585; true


data%State%RamLine%DeltaT_BOP=0.1 !second
data%State%Pumps%TOL_AIR=.08

data%State%Pumps%alpha_Qair=0
data%State%Pumps%alpha_timeair=0
data%State%Pumps%alpha_paccair=data%State%RamLine%P_ACC
data%State%Pumps%alpha_pairp=data%State%RamLine%P_ACC
data%State%Pumps%alpha_diffpair=0
data%State%Pumps%alpha_lossesair=0
data%State%Pumps%alpha_fvrair=0

data%State%RamLine%counter_airp=1

!======================BOP INPUTS(CONSTANTS)===========================

    if (data%Configuration%Accumulator%PrechargePressure == 1400.) then
        data%State%RamLine%B1=1396; data%State%RamLine%B2=0.17; data%State%RamLine%B3=3.873; data%State%RamLine%B4=1.101
    elseif (data%Configuration%Accumulator%PrechargePressure == 2000.) then
        data%State%RamLine%B1=1980; data%State%RamLine%B2=0.1237; data%State%RamLine%B3=15.69; data%State%RamLine%B4=1.029
    elseif (data%Configuration%Accumulator%PrechargePressure == 600.) then
        data%State%RamLine%B1=591.9; data%State%RamLine%B2=0.1968; data%State%RamLine%B3=2.887; data%State%RamLine%B4=0.9757
    else !(PrechargePressure == 1000.) then  ! this is for precharge=1000 psi
        data%State%RamLine%B1=993.7; data%State%RamLine%B2=0.164; data%State%RamLine%B3=5.492; data%State%RamLine%B4=0.9796 
    endif
    
data%State%BopStackAcc%ByPassOld= 1.0

data%State%RAM%p_bop=data%State%ShearRAM%PA
data%State%AnnularComputational%p_annular=data%State%AnnularComputational%pa_annular
!Q=0.0055;  !initial flow rate (m^3/s)
data%State%RAM%flow=60  !(gpm) initial value
data%State%RAM%Qzero=70    !for DP code, increasing Q after shear
data%State%RAM%vdis_tot=0
data%State%RamLine%AIRP_SWITCH=0 !off position
data%State%RamLine%ELECP_SWITCH=0  !off position
data%State%RamLine%ShearBop_closed=1
data%State%PipeRam1%closed=1
data%State%PipeRam2%closed=1
data%State%ChokeLine%closed=1
data%State%KillLine%closed=1
data%State%Annular%Annular_closed=1
data%State%RamLine%FINISHED_shear=0
data%State%PipeRam1%finished=0
data%State%PipeRam2%finished=0
data%State%ChokeLine%finished=0
data%State%KillLine%finished=0
data%State%Annular%finished_Annular=0
data%State%Pumps%DELTAV_ELECP=0
data%State%RAM%vdis_elecp=0
!==================================================================
data%State%RAM%timecounter_ram=0
data%State%RAM%Q=0
data%State%BopStackAcc%pram_reg=data%State%BopStackAcc%PRAMS_REGSET   !psi       !RamsReglatorSet
data%State%Annular%Pannular_reg=min(data%EquipmentControl%BopControlPanel%AnnularRegulatorSetControl,1700.)




data%EquipmentControl%BopControlPanel%MiddleRamsStatus= data%State%ShearRAM%IDshearBop
data%EquipmentControl%BopControlPanel%UpperRamsStatus= data%State%PipeRam1%ID
data%EquipmentControl%BopControlPanel%LowerRamsStatus= data%State%PipeRam2%ID
data%EquipmentControl%BopControlPanel%AnnularStatus = data%State%Annular%IDAnnular


data%EquipmentControl%BopControlPanel%AccumulatorPressureGauge = data%State%RamLine%P_ACC


data%State%RAM%time=0

!         WRITE(60,10) '  Overal Time','Q Ram Line','Vdis Ram Line','P Accumulator','Preg Ram Line','Preg Annular','   P BOP','ID Shear Ram', &
!             'ID Pipe Ram1','ID Pipe Ram2','ID Annular'
!10       FORMAT(11(A18))
!
!
!
!         WRITE(60,60) data%State%RAM(2)%time,data%State%RAM(2)%Q,data%State%RAM(2)%vdis_tot,p_acc, &
!         pram_reg,Pannular_reg,data%State%RAM(2)%p_bop,IDshearBop, &
!         IDPipeRam1,IDPipeRam2,IDAnnular
!60       FORMAT(11(f18.5))


      data%EquipmentControl%BopControlPanel%MiddleRamsOpenLED = LedOn
      data%State%BopStackInput%MiddleRamsOpenLEDMine = LedOn
      data%EquipmentControl%BopControlPanel%MiddleRamsCloseLED = LedOff
      data%State%BopStackInput%MiddleRamsCloseLEDMine = LedOff
         data%State%RamLine%ShearRamIsOpening = .false.
         data%State%RamLine%ShearRamIsClosing = .false.

      data%EquipmentControl%BopControlPanel%UpperRamsOpenLED = LedOn
      data%State%BopStackInput%UpperRamsOpenLEDMine = LedOn
      data%EquipmentControl%BopControlPanel%UpperRamsCloseLED = LedOff
      data%State%BopStackInput%UpperRamsCloseLEDMine = LedOff
         data%State%PipeRam1%IsOpening = .false.
         data%State%PipeRam1%IsClosing = .false.         
         
      data%EquipmentControl%BopControlPanel%LowerRamsOpenLED = LedOn
      data%State%BopStackInput%LowerRamsOpenLEDMine = LedOn
      data%EquipmentControl%BopControlPanel%LowerRamsCloseLED = LedOff
      data%State%BopStackInput%LowerRamsCloseLEDMine = LedOff
         data%State%PipeRam2%IsOpening = .false.
         data%State%PipeRam2%IsClosing = .false.  
         
      data%EquipmentControl%BopControlPanel%ChokeLineOpenLED = LedOff
      data%State%BopStackInput%ChokeLineOpenLEDMine = LedOff
      data%EquipmentControl%BopControlPanel%ChokeLineCloseLED = LedOn
      data%State%BopStackInput%ChokeLineCloseLEDMine = LedOn
         data%State%ChokeLine%IsOpening = .false.
         data%State%ChokeLine%IsClosing = .false. 
         
         
      data%EquipmentControl%BopControlPanel%KillLineOpenLED = LedOff
      data%State%BopStackInput%KillLineOpenLedMine = LedOff
      data%EquipmentControl%BopControlPanel%KillLineCloseLED = LedOn
      data%State%BopStackInput%KillLineCloseLedMine = LedOn
         data%State%KillLine%IsOpening = .false.
         data%State%KillLine%IsClosing = .false.         
         
         
      data%EquipmentControl%BopControlPanel%AnnularOpenLED = LedOn
      data%State%BopStackInput%AnnularOpenLedMine = LedOn
      
      data%EquipmentControl%BopControlPanel%AnnularCloseLED = LedOff
      data%State%BopStackInput%AnnularCloseLedMine = LedOff
      
         data%State%Annular%AnnularIsOpening = .false.
         data%State%Annular%AnnularIsClosing = .false.  
           
         
         data%State%Annular%FirstSet=1
         data%State%AnnularComputational%RamsFirstSet=1
         data%EquipmentControl%BopControlPanel%ManifoldPressureGauge=data%State%BopStackAcc%PRAMS_REGSET        !RamsReglatorSet         
         data%EquipmentControl%BopControlPanel%AnnularPressureGauge= (1 - data%State%Annular%AnnularPressureGaugeMalf) * min(data%EquipmentControl%BopControlPanel%AnnularRegulatorSetControl,1700.)
         
         
data%State%AnnularComputational%PannularTimeStepDelay = int(1./data%State%RamLine%DeltaT_BOP)       ! 1/0.1 : for 1 sec delay in AnnRegulator shot time

    DO i = 1 , data%State%AnnularComputational%PannularTimeStepDelay
        CALL data%State%AnnularComputational%Pannular_regDelay%AddToFirst(data%EquipmentControl%BopControlPanel%AnnularPressureGauge)
    END DO         
         
         
         
    end
    