 SUBROUTINE DEALLOCATE_ARRAYS()
use SimulationVariables
implicit none
!===========================================================================
!                   	RAMLINE MINOR LOSSES INPUT
!===========================================================================
 DEALLOCATE (data%State%BopStackInput%MINORS1,data%State%RamLine%MINORDIAMETER_RAMLINE,data%State%RamLine%AREAMINOR_RAMLINE  &
            ,data%State%RamLine%LF_RAMLINE,data%State%RamLine%CV_RAMLINE,data%State%RamLine%NOTE_RAMLINE,data%State%RAMS%minlosspa,data%State%RAMS%minloss)
!===========================================================================
!                   	RAMLINE PIPNING LOSSES INPUT
!===========================================================================
 DEALLOCATE (data%State%BopStackInput%PIPINGS_RAMLINE,data%State%RamLine%DIAM_RAMLINE_INCH,  &
            data%State%RamLine%AREA_RAMLINE,data%State%RamLine%LENGT_RAMLINE,data%State%RamLine%ROUGHNESS_RAMLINE,data%State%RamLine%RELROUGH_RAMLINE   &
            ,data%State%RAMS%Re_ramline,data%State%RAMS%fric,data%State%RAMS%fricloss)
!===========================================================================
!                   	ANNULAR MINOR LOSSES INPUT
!===========================================================================
 DEALLOCATE (data%State%BopStackInput%MINORS_ANNULAR,data%State%AnnularComputational%MINORDIAMETER_ANNULARLINE,data%State%AnnularComputational%AREAMINOR_ANNULARLINE  &
            ,data%State%AnnularComputational%LF_ANNULARLINE,data%State%AnnularComputational%CV_ANNULARLINE,data%State%AnnularComputational%NOTE_ANNULARLINE,data%State%AnnularComputational%minlosspa_ANNULAR,data%State%AnnularComputational%minloss_ANNULAR)
!===========================================================================
!                   	ANNULAR PIPNING LOSSES INPUT
!=========================================================================== 
 DEALLOCATE (data%State%BopStackInput%PIPINGS_ANNULAR,data%State%AnnularComputational%DIAM_ANNULARLINE_INCH,data%State%AnnularComputational%AREA_ANNULARLINE, &
            data%State%AnnularComputational%LENGT_ANNULARLINE,data%State%AnnularComputational%ROUGHNESS_ANNULARLINE,data%State%AnnularComputational%RELROUGH_ANNULARLINE   &
            ,data%State%AnnularComputational%Re_ANNULARline,data%State%AnnularComputational%fricANNULAR,data%State%AnnularComputational%friclossANNULAR)
!===========================================================================
!                   	AIR PUMP LOSSES INPUT
!===========================================================================  
DEALLOCATE (data%State%BopStackInput%PIPINGS_AIRPUMP,data%State%RamLine%DIAM_AIR_INCH,  &
            data%State%RamLine%Re_air,data%State%RamLine%AREA_AIR,data%State%RamLine%LENGT_AIR,data%State%RamLine%ROUGHNESS_AIRPLINE,data%State%RamLine%REL_ROUGHAIR,  &
           data%State%RamLine%fric_air,data%State%RamLine%fricloss_air)
   !================================================================ 
DEALLOCATE (data%State%BopStackInput%MINORS_AIRPUMP,data%State%RamLine%MINORDIAM_AIR_INCH,  &
            data%State%RamLine%MINORAREA_AIR,data%State%RamLine%LF_AIR,data%State%RamLine%CV_AIR,data%State%RamLine%NOTE_AIR &
            ,data%State%RamLine%minlosspa_air,data%State%RamLine%minloss_air)  
 
!===========================================================================
!                   	DELAY ARRAYS
!===========================================================================  
call data%State%AnnularComputational%Pannular_regDelay%Empty()



    END







    
    
    
    
SUBROUTINE LOSS_INPUTS()
use SimulationVariables
implicit none
INTEGER I

!===========================================================================
!                   	RAMLINE MINOR LOSSES INPUT
!===========================================================================
data%State%RamLine%NO_MINORSRAMLINE=34

ALLOCATE (data%State%BopStackInput%MINORS1(data%State%RamLine%NO_MINORSRAMLINE,4))

    !               ID(INCH)     LF	      CV	    NOTE(BAR)   DESCRIPTION
data%State%BopStackInput%MINORS1(1,1:4)=     (/2.,        2.,      0.,	    0./)      !Acc.tee
data%State%BopStackInput%MINORS1(2,1:4)=     (/2.,        0.9,     0.,	    0./)      !Avg.acc.tee
data%State%BopStackInput%MINORS1(3,1:4)=     (/2.,        0.9,     0.,	    0./)      !Avg.acc.tee
data%State%BopStackInput%MINORS1(4,1:4)=     (/2.,        0.9,     0.,	    0./)      !Avg.acc.tee
data%State%BopStackInput%MINORS1(5,1:4)=     (/2.,        0.9,     0.,	    0./)      !tee
data%State%BopStackInput%MINORS1(6,1:4)=     (/2.,        2.,      0.,	    0./)      !tee
data%State%BopStackInput%MINORS1(7,1:4)=     (/2.,        0.,      105.,	    0./)      !valve
data%State%BopStackInput%MINORS1(8,1:4)=     (/2.,        0.9,     0.,	    0./)      !tee
data%State%BopStackInput%MINORS1(9,1:4)=     (/2.,        0.,      105.,	    0./)      !valve
data%State%BopStackInput%MINORS1(10,1:4)=    (/2.,        0.42,    0.,	    0./)      !elbow
data%State%BopStackInput%MINORS1(11,1:4)=    (/2.,        0.42,    0.,	    0./)      !elbow
data%State%BopStackInput%MINORS1(12,1:4)=    (/2.,        0.8,     0.,	    0./)      !unionA
data%State%BopStackInput%MINORS1(13,1:4)=    (/2.,        0.8,     0.,	    0./)      !unionA
data%State%BopStackInput%MINORS1(14,1:4)=    (/2.,        1.5,     0.,	    0./)      !elbow
data%State%BopStackInput%MINORS1(15,1:4)=    (/2.,        0.,      425.,	    0./)      !valve
data%State%BopStackInput%MINORS1(16,1:4)=    (/2.,        2.,      0.,	    0./)      !tee
data%State%BopStackInput%MINORS1(17,1:4)=    (/0.75,      0.,      1.5,	    0./)      !REGULATOR
data%State%BopStackInput%MINORS1(18,1:4)=    (/1.,        2.,      0.,	    0./)      !tee
data%State%BopStackInput%MINORS1(19,1:4)=    (/1.,        1.5,     0.,	    0./)      !elbow
data%State%BopStackInput%MINORS1(20,1:4)=    (/1.,        0.42,    0.,	    0./)      !elbow
data%State%BopStackInput%MINORS1(21,1:4)=    (/1.,        0.42,    0.,	    0./)      !elbow
data%State%BopStackInput%MINORS1(22,1:4)=    (/1.,        1.5,     0.,	    0./)      !elbow
data%State%BopStackInput%MINORS1(23,1:4)=    (/1.,        0.,      105.,     0./)      !valve
data%State%BopStackInput%MINORS1(24,1:4)=    (/1.,        0.9,     0.,	    0./)      !tee
data%State%BopStackInput%MINORS1(25,1:4)=    (/1.,        0.,      0.,	    0.5/)     !FT
data%State%BopStackInput%MINORS1(26,1:4)=    (/1.,        0.,      0.,       3.4/)     !filter
data%State%BopStackInput%MINORS1(27,1:4)=    (/1.,        0.,      105.,	    0./)      !valve
data%State%BopStackInput%MINORS1(28,1:4)=    (/1.,        0.9,     0.,	    0./)      !tee
data%State%BopStackInput%MINORS1(29,1:4)=    (/1.,        1.5,     0.,	    0./)      !elbow
data%State%BopStackInput%MINORS1(30,1:4)=    (/1.,        1.5,     0.,	    0./)      !elbow
data%State%BopStackInput%MINORS1(31,1:4)=    (/1.,        0.,      9.2,	    0./)      !valve
data%State%BopStackInput%MINORS1(32,1:4)=    (/1.,        0.8,     0.,	    0./)      !unionA
data%State%BopStackInput%MINORS1(33,1:4)=    (/1.,        0.8,     0.,	    0./)      !unionA
data%State%BopStackInput%MINORS1(34,1:4)=    (/0.75,      0.35,     0.,	    0./)      !contraction


ALLOCATE (data%State%RamLine%MINORDIAMETER_RAMLINE(data%State%RamLine%NO_MINORSRAMLINE),data%State%RamLine%AREAMINOR_RAMLINE(data%State%RamLine%NO_MINORSRAMLINE),data%State%RamLine%LF_RAMLINE(data%State%RamLine%NO_MINORSRAMLINE),data%State%RamLine%CV_RAMLINE(data%State%RamLine%NO_MINORSRAMLINE)  &
            ,data%State%RamLine%NOTE_RAMLINE(data%State%RamLine%NO_MINORSRAMLINE),data%State%RAMS%minlosspa(6,data%State%RamLine%NO_MINORSRAMLINE),data%State%RAMS%minloss(6,data%State%RamLine%NO_MINORSRAMLINE))



DO I=1,data%State%RamLine%NO_MINORSRAMLINE
    data%State%RamLine%MINORDIAMETER_RAMLINE(I)=data%State%BopStackInput%MINORS1(I,1)
    data%State%RamLine%LF_RAMLINE(I)=data%State%BopStackInput%MINORS1(I,2)
    data%State%RamLine%CV_RAMLINE(I)=data%State%BopStackInput%MINORS1(I,3)
    data%State%RamLine%NOTE_RAMLINE(I)=data%State%BopStackInput%MINORS1(I,4)
    
    
    data%State%RamLine%AREAMINOR_RAMLINE(I)=PI*(data%State%RamLine%MINORDIAMETER_RAMLINE(I)*0.0254)**2/4.   !D(in), AREA(m)
ENDDO
    

!===========================================================================
!                   	RAMLINE PIPNING LOSSES INPUT
!===========================================================================
data%State%RamLine%NO_PIPINGSRAMLINE=15

ALLOCATE (data%State%BopStackInput%PIPINGS_RAMLINE(data%State%RamLine%NO_PIPINGSRAMLINE,3))

            !               ID(INCH)	L(MM)	ROUGHNESS(MM)=e  DESCRIPTION		
data%State%BopStackInput%PIPINGS_RAMLINE(1,1:3)=		 (/2.,		1035.,		0.03/)      !Avg.acc.distance
data%State%BopStackInput%PIPINGS_RAMLINE(2,1:3)=		 (/2.,		730.,       0.03/)      !Acc.end.horizontal
data%State%BopStackInput%PIPINGS_RAMLINE(3,1:3)=		 (/2.,		2000.,		0.03/)      !Acc.end.vertical
data%State%BopStackInput%PIPINGS_RAMLINE(4,1:3)=      (/2.,		6000.,		0.05/)      !Hyd.hose
data%State%BopStackInput%PIPINGS_RAMLINE(5,1:3)=      (/2.,		2370.,		0.03/)      !Corner.vertical1
data%State%BopStackInput%PIPINGS_RAMLINE(6,1:3)=      (/2.,		210.,       0.03/)      !Add.from.bend
data%State%BopStackInput%PIPINGS_RAMLINE(7,1:3)=      (/1.,		780.,       0.03/)      !Corner.horizontal1
data%State%BopStackInput%PIPINGS_RAMLINE(8,1:3)=      (/1.,		780.,       0.03/)      !Corner.horizontal2
data%State%BopStackInput%PIPINGS_RAMLINE(9,1:3)=      (/1.,		750.,       0.03/)      !Extra.length.back
data%State%BopStackInput%PIPINGS_RAMLINE(10,1:3)=     (/1.,		800.,       0.03/)      !Corner.horizontal3
data%State%BopStackInput%PIPINGS_RAMLINE(11,1:3)=     (/1.,		1650.,		0.03/)      !Corner.vertical2
data%State%BopStackInput%PIPINGS_RAMLINE(12,1:3)=     (/1.,		340.,       0.03/)      !12.Valves.horizontal
data%State%BopStackInput%PIPINGS_RAMLINE(13,1:3)=     (/1.,		1650.,		0.03/)      !Valves.vertical
data%State%BopStackInput%PIPINGS_RAMLINE(14,1:3)=     (/1.,		31000.,		10./)       !Hyd.hose
data%State%BopStackInput%PIPINGS_RAMLINE(15,1:3)=     (/1.,		526.,       0.03/)      !Add.from.bend



 ALLOCATE (data%State%RamLine%DIAM_RAMLINE_INCH(data%State%RamLine%NO_PIPINGSRAMLINE),  &
            data%State%RamLine%AREA_RAMLINE(data%State%RamLine%NO_PIPINGSRAMLINE),data%State%RamLine%LENGT_RAMLINE(data%State%RamLine%NO_PIPINGSRAMLINE),data%State%RamLine%ROUGHNESS_RAMLINE(data%State%RamLine%NO_PIPINGSRAMLINE),data%State%RamLine%RELROUGH_RAMLINE(data%State%RamLine%NO_PIPINGSRAMLINE)   &
            ,data%State%RAMS%Re_ramline(6,data%State%RamLine%NO_PIPINGSRAMLINE),data%State%RAMS%fric(6,data%State%RamLine%NO_PIPINGSRAMLINE),data%State%RAMS%fricloss(6,data%State%RamLine%NO_PIPINGSRAMLINE))

DO I=1,data%State%RamLine%NO_PIPINGSRAMLINE
    data%State%RamLine%DIAM_RAMLINE_INCH(I)=data%State%BopStackInput%PIPINGS_RAMLINE(I,1)
    data%State%RamLine%LENGT_RAMLINE(I)=data%State%BopStackInput%PIPINGS_RAMLINE(I,2)
    data%State%RamLine%ROUGHNESS_RAMLINE(I)=data%State%BopStackInput%PIPINGS_RAMLINE(I,3)
    
    
    
    data%State%RamLine%AREA_RAMLINE(I)=PI*(data%State%RamLine%DIAM_RAMLINE_INCH(I)*0.0254)**2/4   !D(in), AREA(m)
    data%State%RamLine%RELROUGH_RAMLINE(I)=data%State%RamLine%ROUGHNESS_RAMLINE(I)/(data%State%RamLine%DIAM_RAMLINE_INCH(I)*25.4)    !e/D
    !DIAM_RAMLINE_MM(I)=DIAM_RAMLINE_MM(I)*.001  !  (m)
    data%State%RamLine%LENGT_RAMLINE(I)=data%State%RamLine%LENGT_RAMLINE(I)*.001      !  (m) 
ENDDO
  


!===========================================================================
!                   	ANNULAR PREVENTER MINOR LOSSES INPUT
!===========================================================================
data%State%AnnularComputational%NO_MinorsAnnularLine=29

ALLOCATE (data%State%BopStackInput%MINORS_ANNULAR(data%State%AnnularComputational%NO_MinorsAnnularLine,4))

    !			            ID(INCH)	LF	    CV	NOTE(BAR)     DESCRIPTION
data%State%BopStackInput%MINORS_ANNULAR(1,1:4)=      (/2.,       2.,	    0.,	    0./)      !Acc.tee
data%State%BopStackInput%MINORS_ANNULAR(2,1:4)=      (/2.,       0.9,	0.,	    0./)      !Avg.acc.tee
data%State%BopStackInput%MINORS_ANNULAR(3,1:4)=      (/2.,       0.9,	0.,	    0./)      !Avg.acc.tee
data%State%BopStackInput%MINORS_ANNULAR(4,1:4)=      (/2.,       0.9,	0.,	    0./)      !Avg.acc.tee	
data%State%BopStackInput%MINORS_ANNULAR(5,1:4)=      (/2.,       0.9,	0.,	    0./)      !tee
data%State%BopStackInput%MINORS_ANNULAR(6,1:4)=      (/2.,       2.,	    0.,	    0./)      !tee	
data%State%BopStackInput%MINORS_ANNULAR(7,1:4)=      (/2.,       0.,	    105.,	0./)      !valve
data%State%BopStackInput%MINORS_ANNULAR(8,1:4)=      (/2.,       0.9,	0.,	    0./)      !tee
data%State%BopStackInput%MINORS_ANNULAR(9,1:4)=      (/2.,       0.,	    105.,	0./)      !valve
data%State%BopStackInput%MINORS_ANNULAR(10,1:4)=     (/2.,       0.42,	0.,	    0./)      !elbow
data%State%BopStackInput%MINORS_ANNULAR(11,1:4)=     (/2.,       0.42,	0.,	    0./)      !elbow
data%State%BopStackInput%MINORS_ANNULAR(12,1:4)=     (/2.,       0.8,	0.,	    0./)      !unionA
data%State%BopStackInput%MINORS_ANNULAR(13,1:4)=     (/2.,       0.8,	0.,	    0./)      !unionA
data%State%BopStackInput%MINORS_ANNULAR(14,1:4)=     (/2.,       1.5,	0.,	    0./)      !elbow	
data%State%BopStackInput%MINORS_ANNULAR(15,1:4)=     (/2.,       0.,	    425.,	0./)      !valve		
data%State%BopStackInput%MINORS_ANNULAR(16,1:4)=     (/2.,       2.,	    0.,	    0./)      !tee
data%State%BopStackInput%MINORS_ANNULAR(17,1:4)=     (/0.75,     0.,	    1.5,	0./)      !REGULATOR		
data%State%BopStackInput%MINORS_ANNULAR(18,1:4)=     (/1.,       2.,	    0.,	    0./)      !tee	
data%State%BopStackInput%MINORS_ANNULAR(19,1:4)=     (/1.,       1.5,	0.,	    0./)      !elbow	
data%State%BopStackInput%MINORS_ANNULAR(20,1:4)=     (/1.,       0.42,	0.,	    0./)      !elbow	
data%State%BopStackInput%MINORS_ANNULAR(21,1:4)=     (/1.,       0.42,	0.,	    0./)      !elbow
data%State%BopStackInput%MINORS_ANNULAR(22,1:4)=     (/1.,       1.5,	0.,	    0./)      !elbow	
data%State%BopStackInput%MINORS_ANNULAR(23,1:4)=     (/1.,       0.,	    3.2,	0./)      !valve
data%State%BopStackInput%MINORS_ANNULAR(24,1:4)=     (/1.,       2.,	    0.,	    0./)      !tee
data%State%BopStackInput%MINORS_ANNULAR(25,1:4)=     (/1.,       1.5,	0.,	    0./)      !elbow
data%State%BopStackInput%MINORS_ANNULAR(26,1:4)=     (/1.,       0.42,	0.,	    0./)      !elbow
data%State%BopStackInput%MINORS_ANNULAR(27,1:4)=     (/1.,       0.42,	0.,	    0./)      !elbow
data%State%BopStackInput%MINORS_ANNULAR(28,1:4)=     (/1.,       1.5,	0.,	    0./)      !elbow
data%State%BopStackInput%MINORS_ANNULAR(29,1:4)=     (/1.,       0.,	    3.2,	0./)      !valve



 ALLOCATE (data%State%AnnularComputational%MINORDIAMETER_ANNULARLINE(data%State%AnnularComputational%NO_MinorsAnnularLine),data%State%AnnularComputational%AREAMINOR_ANNULARLINE(data%State%AnnularComputational%NO_MinorsAnnularLine),data%State%AnnularComputational%LF_ANNULARLINE(data%State%AnnularComputational%NO_MinorsAnnularLine)  &
            ,data%State%AnnularComputational%CV_ANNULARLINE(data%State%AnnularComputational%NO_MinorsAnnularLine),data%State%AnnularComputational%NOTE_ANNULARLINE(data%State%AnnularComputational%NO_MinorsAnnularLine),data%State%AnnularComputational%minlosspa_ANNULAR(data%State%AnnularComputational%NO_MinorsAnnularLine),data%State%AnnularComputational%minloss_ANNULAR(data%State%AnnularComputational%NO_MinorsAnnularLine))



DO I=1,data%State%AnnularComputational%NO_MinorsAnnularLine
    data%State%AnnularComputational%MINORDIAMETER_ANNULARLINE(I)=data%State%BopStackInput%MINORS_ANNULAR(I,1)
    data%State%AnnularComputational%LF_ANNULARLINE(I)=data%State%BopStackInput%MINORS_ANNULAR(I,2)
    data%State%AnnularComputational%CV_ANNULARLINE(I)=data%State%BopStackInput%MINORS_ANNULAR(I,3)
    data%State%AnnularComputational%NOTE_ANNULARLINE(I)=data%State%BopStackInput%MINORS_ANNULAR(I,4)
    
    data%State%AnnularComputational%AREAMINOR_ANNULARLINE(I)=PI*(data%State%AnnularComputational%MINORDIAMETER_ANNULARLINE(I)*0.0254)**2/4.   !D(in), AREA(m)
ENDDO
   

!===========================================================================
!                   ANNULAR PREVENTER PIPNING LOSSES INPUT
!===========================================================================
data%State%AnnularComputational%NO_PipingsAnnularLine=10

ALLOCATE (data%State%BopStackInput%PIPINGS_ANNULAR(data%State%AnnularComputational%NO_PipingsAnnularLine,3))

                !            ID(INCH)   L(MM)	ROUGHNESS(MM)=e		 DESCRIPTION			
data%State%BopStackInput%PIPINGS_ANNULAR(1,1:3)=		 (/2.,		1035.,		0.03/)		     !Avg.acc.distance				
data%State%BopStackInput%PIPINGS_ANNULAR(2,1:3)=		 (/2.,		730.,		0.03/)		     !Acc.endhorizontal				
data%State%BopStackInput%PIPINGS_ANNULAR(3,1:3)=		 (/2.,		2000.,		0.03/)		     !Acc.endvertical				
data%State%BopStackInput%PIPINGS_ANNULAR(4,1:3)=		 (/2.,		6000.,		0.03/)		     !Hyd.hose				
data%State%BopStackInput%PIPINGS_ANNULAR(5,1:3)=		 (/2.,		2370.,		0.03/)		     !Corner.vertical1				
data%State%BopStackInput%PIPINGS_ANNULAR(6,1:3)=		 (/2.,		210.,		0.03/)		     !Add.frombend				
data%State%BopStackInput%PIPINGS_ANNULAR(7,1:3)=		 (/2.,		1000.,		0.03/)		     !manifold				
data%State%BopStackInput%PIPINGS_ANNULAR(8,1:3)=		 (/1.,		46000.,		0.03/)		     !pipe				
data%State%BopStackInput%PIPINGS_ANNULAR(9,1:3)=		 (/2.,		1000.,		0.03/)		     !manifold				
data%State%BopStackInput%PIPINGS_ANNULAR(10,1:3)=     (/1.,		46000.,		0.03/)           !pipe




 ALLOCATE (data%State%AnnularComputational%DIAM_ANNULARLINE_INCH(data%State%AnnularComputational%NO_PipingsAnnularLine),data%State%AnnularComputational%AREA_ANNULARLINE(data%State%AnnularComputational%NO_PipingsAnnularLine),data%State%AnnularComputational%LENGT_ANNULARLINE(data%State%AnnularComputational%NO_PipingsAnnularLine)  &
            ,data%State%AnnularComputational%ROUGHNESS_ANNULARLINE(data%State%AnnularComputational%NO_PipingsAnnularLine),data%State%AnnularComputational%RELROUGH_ANNULARLINE(data%State%AnnularComputational%NO_PipingsAnnularLine)   &
            ,data%State%AnnularComputational%Re_ANNULARline(data%State%AnnularComputational%NO_PipingsAnnularLine),data%State%AnnularComputational%fricANNULAR(data%State%AnnularComputational%NO_PipingsAnnularLine),data%State%AnnularComputational%friclossANNULAR(data%State%AnnularComputational%NO_PipingsAnnularLine))


DO I=1,data%State%AnnularComputational%NO_PipingsAnnularLine
    data%State%AnnularComputational%DIAM_ANNULARLINE_INCH(I)=data%State%BopStackInput%PIPINGS_ANNULAR(I,1)
    data%State%AnnularComputational%LENGT_ANNULARLINE(I)=data%State%BopStackInput%PIPINGS_ANNULAR(I,2)
    data%State%AnnularComputational%ROUGHNESS_ANNULARLINE(I)=data%State%BopStackInput%PIPINGS_ANNULAR(I,3)
    
    
    data%State%AnnularComputational%AREA_ANNULARLINE(I)=PI*(data%State%AnnularComputational%DIAM_ANNULARLINE_INCH(I)*0.0254)**2/4.   !D(in), AREA(m)
    data%State%AnnularComputational%RELROUGH_ANNULARLINE(I)=data%State%AnnularComputational%ROUGHNESS_ANNULARLINE(I)/(data%State%AnnularComputational%DIAM_ANNULARLINE_INCH(I)*25.4)
    !DIAM_ANNULARLINE_MM(I)=DIAM_ANNULARLINE_MM(I)*.001  !  (m)
    data%State%AnnularComputational%LENGT_ANNULARLINE(I)=data%State%AnnularComputational%LENGT_ANNULARLINE(I)*.001      !  (m) 
    ENDDO


!===========================================================================
!                   	AIR PUMP LOSSES INPUT
!===========================================================================  
data%State%RamLine%NO_PIPINGS_AIRPLINE=1

ALLOCATE (data%State%BopStackInput%PIPINGS_AIRPUMP(data%State%RamLine%NO_PIPINGS_AIRPLINE,3))

                !			ID(INCH)    L(MM)	  ROUGHNESS(MM)=e   DESCRIPTION	
data%State%BopStackInput%PIPINGS_AIRPUMP(1,1:3)=		 (/2.,      10000.,   0.03/)            !Avg.acc.distance
 


  
  ALLOCATE (data%State%RamLine%DIAM_AIR_INCH(data%State%RamLine%NO_PIPINGS_AIRPLINE),data%State%RamLine%Re_air(data%State%RamLine%NO_PIPINGS_AIRPLINE),data%State%RamLine%AREA_AIR(data%State%RamLine%NO_PIPINGS_AIRPLINE),  &
            data%State%RamLine%LENGT_AIR(data%State%RamLine%NO_PIPINGS_AIRPLINE),data%State%RamLine%ROUGHNESS_AIRPLINE(data%State%RamLine%NO_PIPINGS_AIRPLINE),data%State%RamLine%REL_ROUGHAIR(data%State%RamLine%NO_PIPINGS_AIRPLINE),  &
           data%State%RamLine%fric_air(data%State%RamLine%NO_PIPINGS_AIRPLINE),data%State%RamLine%fricloss_air(data%State%RamLine%NO_PIPINGS_AIRPLINE))



DO I=1,data%State%RamLine%NO_PIPINGS_AIRPLINE
    data%State%RamLine%DIAM_AIR_INCH(I)=data%State%BopStackInput%PIPINGS_AIRPUMP(I,1)
    data%State%RamLine%LENGT_AIR(I)=data%State%BopStackInput%PIPINGS_AIRPUMP(I,2)
    data%State%RamLine%ROUGHNESS_AIRPLINE(I)=data%State%BopStackInput%PIPINGS_AIRPUMP(I,3)
    
    
    data%State%RamLine%AREA_AIR(I)=PI*(data%State%RamLine%DIAM_AIR_INCH(I)*0.0254)**2/4   !D(in), AREA(m)
    data%State%RamLine%REL_ROUGHAIR(I)=data%State%RamLine%ROUGHNESS_AIRPLINE(I)/(data%State%RamLine%DIAM_AIR_INCH(I)*25.4)
    !DIAM_RAMLINE_MM(I)=DIAM_RAMLINE_MM(I)*.001  !  (m)
    data%State%RamLine%LENGT_AIR(I)=data%State%RamLine%LENGT_AIR(I)*.001      !  (m) 
    ENDDO


    
   !================================================================ 
data%State%RamLine%NO_MINORS_AIRPLINE=6

ALLOCATE (data%State%BopStackInput%MINORS_AIRPUMP(data%State%RamLine%NO_MINORS_AIRPLINE,4))

                    !		ID(INCH)		LF	    CV	NOTE(BAR)      DESCRIPTION	
data%State%BopStackInput%MINORS_AIRPUMP(1,1:4)=      (/2.,			10.,	0.,	    0./)       !Acc.tee	
data%State%BopStackInput%MINORS_AIRPUMP(2,1:4)=      (/2.,			11.,	0.,	    0./)       !elbow	
data%State%BopStackInput%MINORS_AIRPUMP(3,1:4)=      (/1.,			0.,	    0.,	    3.4/)      !filter
data%State%BopStackInput%MINORS_AIRPUMP(4,1:4)=      (/2.,			0.,	    105.,	0./)       !valve	
data%State%BopStackInput%MINORS_AIRPUMP(5,1:4)=      (/1.,			0.,	    9.2,	0./)       !valve	
data%State%BopStackInput%MINORS_AIRPUMP(6,1:4)=      (/2.,			6.4,	0.,	    0./)       !unionA  
  

    
  ALLOCATE (data%State%RamLine%MINORDIAM_AIR_INCH(data%State%RamLine%NO_MINORS_AIRPLINE),data%State%RamLine%MINORAREA_AIR(data%State%RamLine%NO_MINORS_AIRPLINE),  &
            data%State%RamLine%LF_AIR(data%State%RamLine%NO_MINORS_AIRPLINE),data%State%RamLine%CV_AIR(data%State%RamLine%NO_MINORS_AIRPLINE),data%State%RamLine%NOTE_AIR(data%State%RamLine%NO_MINORS_AIRPLINE) &
            ,data%State%RamLine%minlosspa_air(data%State%RamLine%NO_MINORS_AIRPLINE),data%State%RamLine%minloss_air(data%State%RamLine%NO_MINORS_AIRPLINE))   
 

 
DO I=1,data%State%RamLine%NO_MINORS_AIRPLINE
    data%State%RamLine%MINORDIAM_AIR_INCH(I)=data%State%BopStackInput%MINORS_AIRPUMP(I,1)
    data%State%RamLine%LF_AIR(I)=data%State%BopStackInput%MINORS_AIRPUMP(I,2)
    data%State%RamLine%CV_AIR(I)=data%State%BopStackInput%MINORS_AIRPUMP(I,3)
    data%State%RamLine%NOTE_AIR(I)=data%State%BopStackInput%MINORS_AIRPUMP(I,4)
    
    
    data%State%RamLine%MINORAREA_AIR(I)=PI*(data%State%RamLine%MINORDIAM_AIR_INCH(I)*0.0254)**2/4.   !D(in), AREA(m)
ENDDO    
    

 
    END
    
SUBROUTINE pumps_charge_bottle()
use SimulationVariables
USE CAccumulatorVariables
USE CBopStackVariables
use CBopControlPanel
    use SimulationVariables
USE CEquipmentsConstants
! use CSimulationVariables
implicit none

    !Pannular_regset=min(AnnularRegulatorSetControl,1700.)  ! for changing its set conditions instantaneously
!write(*,*) 'pumps_charge_bottle'
    
    if(data%EquipmentControl%BopControlPanel%ByePassValve == -1.0) then
        data%State%BopStackAcc%ByPassOld= -1.0
    elseif(data%EquipmentControl%BopControlPanel%ByePassValve == 1.0) then
        data%State%BopStackAcc%ByPassOld= 1.0
    endif

    
!=====================================================================
!                       ACCUMULATOR
!=====for a 10 gal bottle,precharge=1000psig curve BOSCH-isotherm=====
!for charging bottles by the pump
!((((((((IN OUTER LOOP))))))
! ba1=1003; ba2=.03375; ba3=4.014; ba4=.2458;
if (data%State%RamLine%AIRP_SWITCH==0) data%State%Pumps%DELTAV_AIR=0
data%State%RamLine%FVR=data%State%RamLine%FVR+data%State%Pumps%DELTAV_AIR+data%State%Pumps%DELTAV_ELECP

data%State%RamLine%pacc_before=data%State%RamLine%P_ACC
data%State%RamLine%P_ACC=data%State%RamLine%B1*exp(data%State%RamLine%B2*data%State%RamLine%FVR/data%State%BopStackAcc%NOBOTTLES)+data%State%RamLine%B3*exp(data%State%RamLine%B4*data%State%RamLine%FVR/data%State%BopStackAcc%NOBOTTLES) ! adiabatic(psig)<<<< 8=no. of bottles

!=====================================================================


    if(data%State%BopStackAcc%ByPassOld == 1.0) then

        if (data%State%BopStackAcc%pram_reg<data%State%BopStackAcc%PRAMS_REGSET) then
            !pram_reg=pram_reg+(p_acc-pacc_before)
            data%State%BopStackAcc%PressureDifference= (data%State%RamLine%P_ACC-data%State%RamLine%pacc_before)
        else
            !pram_reg=PRAMS_REGSET
            data%State%BopStackAcc%PressureDifference= data%State%BopStackAcc%PRAMS_REGSET - data%State%BopStackAcc%pram_reg
        endif
        
        
         !old=pram_reg
         !difference= new-old
         if (ABS(data%State%BopStackAcc%PressureDifference) > BaseDifferenceP) then
             data%State%BopStackAcc%pram_reg= data%State%BopStackAcc%pram_reg + (data%State%BopStackAcc%PressureDifference/PressureDifferenceSteps)      ! PressureDifferenceSteps = 20.
         else
             
                if (data%State%BopStackAcc%pram_reg<data%State%BopStackAcc%PRAMS_REGSET) then
                    data%State%BopStackAcc%pram_reg=data%State%BopStackAcc%pram_reg+(data%State%RamLine%P_ACC-data%State%RamLine%pacc_before)
                else
                    data%State%BopStackAcc%pram_reg=data%State%BopStackAcc%PRAMS_REGSET
                endif
        
         endif        
        
        
        
        

    else        !if(ByPassOld == -1.0) then


         !old=pram_reg
         !new=p_acc- MAXVAL(data%State%RAM%loss_before)
         !difference= new-old
         data%State%BopStackAcc%PressureDifference= data%State%RamLine%P_ACC- MAXVAL(data%State%RAM%loss_before) - data%State%BopStackAcc%pram_reg
         if (ABS(data%State%BopStackAcc%PressureDifference) > BaseDifferenceP) then
             data%State%BopStackAcc%pram_reg= data%State%BopStackAcc%pram_reg + (data%State%BopStackAcc%PressureDifference/PressureDifferenceSteps)      ! PressureDifferenceSteps = 20.
         else
            data%State%BopStackAcc%pram_reg= data%State%RamLine%P_ACC- MAXVAL(data%State%RAM%loss_before)
         endif         

    endif


    !Pannular_reg= min(p_acc,Pannular_regset)
    

    end
    
    
SUBROUTINE airpump_code()    
use SimulationVariables
use CSounds
implicit none
INTEGER I



data%State%Pumps%QAIR=data%State%Pumps%Qiter+.1   !(gpm)  maximum flow for the start
data%State%RamLine%diffp_air=-10
data%State%RamLine%losses_air=10

!===================================================================
!                       AIR OPERATED PUMP
!=================for air consumption at 8 bar======================
do while (data%State%RamLine%diffp_air<0)
 data%State%Pumps%QAIR=data%State%Pumps%QAIR-.1
! Qup=QAIR_PUMP;
! bba1 =31.8; bba2 =-725.7 ; bba3 =4154;
 data%State%Pumps%P_AIRP=data%State%RamLine%BBA1*data%State%Pumps%QAIR**2+data%State%RamLine%BBA2*data%State%Pumps%QAIR+data%State%RamLine%BBA3 !(psig)
 data%State%RamLine%kinetic_air=sg*wdens*(data%State%Pumps%QAIR*6.30902e-5/((1/4.)*pi*(2*0.254e-1)**2))**2/(2*6895) !(psi)


 data%State%RamLine%diffp_air=data%State%Pumps%P_AIRP+data%State%RamLine%kinetic_air-data%State%RamLine%P_ACC

end do    !returns Qup


 do while (abs((data%State%RamLine%diffp_air-data%State%RamLine%losses_air)/data%State%RamLine%diffp_air)>data%State%Pumps%TOL_AIR) !finding correct QAIR_pump for 1 timecounter_ram

if (data%State%RamLine%diffp_air-data%State%RamLine%losses_air>0) then
    data%State%Pumps%QAIR=data%State%Pumps%QAIR+.01
else
    data%State%Pumps%QAIR=data%State%Pumps%QAIR-.01
endif

!===================================================================
!                       AIR OPERATED PUMP
!                   Maximator - Model: GX (35)
!=================for air consumption at 8 bar======================

 data%State%Pumps%P_AIRP=data%State%RamLine%BBA1*data%State%Pumps%QAIR**2+data%State%RamLine%BBA2*data%State%Pumps%QAIR+data%State%RamLine%BBA3 !(psig)
 data%State%RamLine%kinetic_air=sg*wdens*(data%State%Pumps%QAIR*6.30902e-005/((1/4.)*pi*(2*0.254e-1)**2))**2/(2*6895) !(psi)

 data%State%RamLine%diffp_air=data%State%Pumps%P_AIRP+data%State%RamLine%kinetic_air-data%State%RamLine%P_ACC


!===========================LOSSES====================================
 do i=1,data%State%RamLine%NO_PIPINGS_AIRPLINE
data%State%RamLine%Re_air(i)=data%State%Pumps%QAIR*6.30902e-005*data%State%RamLine%DIAM_AIR_INCH(I)*0.0254/(data%State%RamLine%AREA_AIR(i)*nu)
enddo

do i=1,data%State%RamLine%NO_PIPINGS_AIRPLINE
     if (data%State%RamLine%Re_air(i)<Re_cr) then
         data%State%RamLine%fric_air(i)=64/data%State%RamLine%Re_air(i)
     else
        data%State%RamLine%fric_air(i)=1/(-1.8*log10((data%State%RamLine%REL_ROUGHAIR(i)/3.7)**1.11+6.9/data%State%RamLine%Re_air(i)))**2
     endif
     
data%State%RamLine%fricloss_air(i)=((data%State%RamLine%fric_air(i)*(wdens*sg*data%State%RamLine%LENGT_AIR(i)*(data%State%Pumps%QAIR*6.30902e-005/data%State%RamLine%AREA_AIR(i))**2))/(2*data%State%RamLine%DIAM_AIR_INCH(I)*0.0254))/6895

 enddo




data%State%RamLine%pipe_loss1air=sum(data%State%RamLine%fricloss_air)  !loss before regulator(psi)

do i=1,data%State%RamLine%NO_MINORS_AIRPLINE
    if (data%State%RamLine%LF_AIR(i)/=0) then
        data%State%RamLine%minlosspa_air(i)=data%State%RamLine%LF_AIR(i)*wdens*sg*(data%State%Pumps%QAIR*6.30902e-005/data%State%RamLine%MINORAREA_AIR(i))**2/2  !(Pa)
        data%State%RamLine%minloss_air(i)=data%State%RamLine%minlosspa_air(i)/6895 !(psi)
    elseif (data%State%RamLine%CV_AIR(i)/=0) then
        data%State%RamLine%minlosspa_air(i)=1000*sg*((11.7*data%State%Pumps%QAIR*6.30902e-005*3600)/(data%State%RamLine%CV_AIR(i)))**2 !(pa)
        data%State%RamLine%minloss_air(i)=data%State%RamLine%minlosspa_air(i)/6895 !(psi)
    else
        data%State%RamLine%minlosspa_air(i)=data%State%RamLine%NOTE_AIR(i)*1e5 !(pa)
        data%State%RamLine%minloss_air(i)=data%State%RamLine%minlosspa_air(i)/6895 !(psi)
    endif
enddo

        
data%State%RamLine%minor_loss1air=sum(data%State%RamLine%minloss_air)  !loss before regulator(psi)

data%State%RamLine%static_loss1air=0

data%State%RamLine%losses_air=data%State%RamLine%pipe_loss1air+data%State%RamLine%minor_loss1air+data%State%RamLine%static_loss1air !(psi)
!=========================================================================



 end do  !returns correct QAIR_PUMP for 1 counter

data%State%RamLine%counter_airp=data%State%RamLine%counter_airp+1 
   data%State%Pumps%Qiter=data%State%Pumps%QAIR
   
   data%State%BopStackAcc%Cumulative_AirVolume= data%State%BopStackAcc%Cumulative_AirVolume + ((data%State%Pumps%QAIR * data%State%RamLine%DeltaT_BOP / 60.0 ) / (6.71*0.004329004) )        ! =strokes
   !write(*,*) 'Cumulative_AirVolume=' , Cumulative_AirVolume
   
   if ( data%State%BopStackAcc%Cumulative_AirVolume > 1.0 ) then
       data%State%BopStackAcc%SoundKoomeyAirPump= 120.0
       data%State%BopStackAcc%Cumulative_AirVolume= data%State%BopStackAcc%Cumulative_AirVolume - 1.0
   else
       data%State%BopStackAcc%SoundKoomeyAirPump= 0.0
   endif
   
   
   !SoundKoomeyAirPump= int ( (QAIR_PUMP/ (6.71*0.004329004))  )   !  6.71: Volume per stroke (in^3)  , 0.004329004: in^3 to gal
   !  2.0 : two air pumps   60:seconds
   !write(*,*) 'sedaaaa=' ,(QAIR_PUMP/ (6.71*0.004329004)) *DeltaT_BOP/(60.0*2.0) 
   call SetSoundKoomeyAirPump(data%State%BopStackAcc%SoundKoomeyAirPump)
   

data%State%Pumps%DELTAV_AIR=data%State%Pumps%QAIR*data%State%RamLine%DeltaT_BOP/60.  !(galon) delta_t=1sec , Q(gpm)

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


!((((((((IN OUTER LOOP))))))
!fvr_air=fvr_air+deltav_air  !=fvr_after (galon) for next timecounter_ram


!========================================================

         !WRITE(50,50) QAIR_PUMP,p_airp, &
             !p_acc,diffp_air,losses_air,deltav_air
    50     FORMAT(6(f15.5))

    
    end
    
    
    SUBROUTINE bop_code(ramtype,H_RAMBOP,RNUMBER) 
    
    use SimulationVariables
    USE CAccumulatorVariables
    use CBopControlPanel
    use SimulationVariables
    USE CEquipmentsConstants
    ! use CSimulationVariables
    use SimulationVariables !@
    use CUnityOutputs
   implicit none 
    integer ramtype, I
    real H_RAMBOP
    INTEGER RNUMBER
   
    !Pannular_regset=min(AnnularRegulatorSetControl,1700.)  ! for changing its set conditions instantaneously
    
!====================================================
!       BOP back pressure IN PRESENCE OF DP
!====================================================
if (data%State%RAM(RNUMBER)%bop_type==2) then
     data%State%RAM(RNUMBER)%checkp= data%State%RAM(RNUMBER)%p_bop
if ( data%State%RAM(RNUMBER)%vdis_tot<data%State%ShearRAM%VA) then
     data%State%RAM(RNUMBER)%p_bop=data%State%ShearRAM%PA
elseif (data%State%ShearRAM%VA<= data%State%RAM(RNUMBER)%vdis_tot .and.  data%State%RAM(RNUMBER)%vdis_tot<=data%State%ShearRAM%VB) then
     data%State%RAM(RNUMBER)%p_bop=data%State%ShearRAM%PA+data%State%ShearRAM%PB*(( data%State%RAM(RNUMBER)%vdis_tot-data%State%ShearRAM%VA)/(data%State%ShearRAM%VB-data%State%ShearRAM%VA))
else
     data%State%RAM(RNUMBER)%p_bop=data%State%ShearRAM%PA
endif
 
if ( data%State%RAM(RNUMBER)%checkp/=data%State%ShearRAM%PA .and.  data%State%RAM(RNUMBER)%p_bop==data%State%ShearRAM%PA) then
    data%State%RAM(RNUMBER)%Q=data%State%RAM(RNUMBER)%Qzero
else
 data%State%RAM(RNUMBER)%Q=data%State%RAM(RNUMBER)%flow
endif

    endif    
    
!====================================================
!       BOP back pressure without DP
!====================================================    
if (data%State%RAM(RNUMBER)%bop_type==3) then 
     data%State%RAM(RNUMBER)%p_bop=data%State%ShearRAM%PA
! Q=flow    
    endif 
    
      
    
 data%State%RAM(RNUMBER)%clock=0
!======================Losses============================
 data%State%RAM(RNUMBER)%loss_after=0 !initial value
 data%State%RAM(RNUMBER)%diffp_ram=1000  !initial value
 data%State%RAM(RNUMBER)%loss_before=0

!Q=0.0055;  %initial flow rate (m^3/s)

 data%State%RAM(RNUMBER)%Q=data%State%RAM(RNUMBER)%flow


do while (abs( data%State%RAM(RNUMBER)%diffp_ram- data%State%RAM(RNUMBER)%loss_after)/ data%State%RAM(RNUMBER)%diffp_ram>data%State%RAM(RNUMBER)%tol)
    if (data%State%RAM(RNUMBER)%Bottles_Charged_MalfActive==.true.) exit
! while abs( data%State%RAM(RNUMBER)%diffp_ram- data%State%RAM(RNUMBER)%loss_after)>10
 data%State%RAM(RNUMBER)%clock= data%State%RAM(RNUMBER)%clock+1

if ( data%State%RAM(RNUMBER)%clock>20) then
!    tclock=clock
    data%State%RAM(RNUMBER)%tol= data%State%RAM(RNUMBER)%TOLZERO+(floor( data%State%RAM(RNUMBER)%clock/10)-1)*.001
endif
 !      if (clock==1) continue
       
    if ( data%State%RAM(RNUMBER)%clock/=1 .and.  data%State%RAM(RNUMBER)%loss_after> data%State%RAM(RNUMBER)%diffp_ram) then
        data%State%RAM(RNUMBER)%Q=data%State%RAM(RNUMBER)%Q-.1
    elseif ( data%State%RAM(RNUMBER)%clock/=1 .and.  data%State%RAM(RNUMBER)%loss_after<= data%State%RAM(RNUMBER)%diffp_ram) then
        data%State%RAM(RNUMBER)%Q=data%State%RAM(RNUMBER)%Q+.01

    endif

!====================Before Regulator=========================
!pipe_loss1=3.2;  % should be replaced by formula
do i=1,data%State%RamLine%NO_PIPINGSRAMLINE
 data%State%RAMS%Re_ramline(RNUMBER,i)=data%State%RAM(RNUMBER)%Q*6.30902e-5*data%State%RamLine%DIAM_RAMLINE_INCH(i)*0.0254/(data%State%RamLine%AREA_RAMLINE(i)*nu)

enddo


do i=1,data%State%RamLine%NO_PIPINGSRAMLINE
     if ( data%State%RAMS%Re_ramline(RNUMBER,i)<Re_cr) then
          data%State%RAMS%fric(RNUMBER,i)=64/ data%State%RAMS%Re_ramline(RNUMBER,i)
     else

         data%State%RAMS%fric(RNUMBER,i)=1/(-1.8*log10((data%State%RamLine%RELROUGH_RAMLINE(i)/3.7)**1.11+6.9/ data%State%RAMS%Re_ramline(RNUMBER,i)))**2
     endif

 data%State%RAMS%fricloss(RNUMBER,i)=(( data%State%RAMS%fric(RNUMBER,i)*(wdens*sg*data%State%RamLine%LENGT_RAMLINE(i)*(data%State%RAM(RNUMBER)%Q*6.30902e-5/data%State%RamLine%AREA_RAMLINE(i))**2))/(2*data%State%RamLine%DIAM_RAMLINE_INCH(i)*0.0254))/6895

enddo
data%State%RAM(RNUMBER)%pipe_loss1=sum( data%State%RAMS%fricloss(RNUMBER,1:6))  !loss before regulator(psi)

!   Q*6.30902e-5   for (gpm) to (m^3/sec)
do i=1,data%State%RamLine%NO_MINORSRAMLINE
    if (data%State%RamLine%LF_RAMLINE(i)/=0) then
         data%State%RAMS%minlosspa(RNUMBER,i)=data%State%RamLine%LF_RAMLINE(i)*wdens*sg*(data%State%RAM(RNUMBER)%Q*6.30902e-5/data%State%RamLine%AREAMINOR_RAMLINE(i))**2/2  !(Pa)
         data%State%RAMS%minloss(RNUMBER,i)= data%State%RAMS%minlosspa(RNUMBER,i)/6895 !(psi)
    elseif (data%State%RamLine%CV_RAMLINE(i)/=0) then
         data%State%RAMS%minlosspa(RNUMBER,i)=1000*sg*((11.7*data%State%RAM(RNUMBER)%Q*6.30902e-5*3600)/(data%State%RamLine%CV_RAMLINE(i)))**2 !(pa)
         data%State%RAMS%minloss(RNUMBER,i)= data%State%RAMS%minlosspa(RNUMBER,i)/6895 !(psi)
    else
         data%State%RAMS%minlosspa(RNUMBER,i)=data%State%RamLine%NOTE_RAMLINE(i)*1e5 !(pa)
         data%State%RAMS%minloss(RNUMBER,i)= data%State%RAMS%minlosspa(RNUMBER,i)/6895 !(psi)
    endif
enddo

 data%State%RAMS%minloss(RNUMBER,17)= 5.*data%State%RAM(RNUMBER)%Q       ! <==== regulator loss (psig)
        
 data%State%RAM(RNUMBER)%minor_loss1=sum( data%State%RAMS%minloss(RNUMBER,1:16))  !loss before regulator(psi)

 data%State%RAM(RNUMBER)%static_loss1=0

 data%State%RAM(RNUMBER)%kinetic_loss1=sg*wdens*(data%State%RAM(RNUMBER)%Q*6.30902e-5/((1/4.)*pi*(.72*0.254e-1)**2))**2/(2*6895) !(psi)
!.72 for regulator diameter)

 data%State%RAM(RNUMBER)%loss_before= data%State%RAM(RNUMBER)%pipe_loss1+ data%State%RAM(RNUMBER)%minor_loss1+ data%State%RAM(RNUMBER)%static_loss1+ data%State%RAM(RNUMBER)%kinetic_loss1 !(psi)
!===================after Regulator======================
! all in (psi)
 data%State%RAM(RNUMBER)%pipe_loss2=sum( data%State%RAMS%fricloss(RNUMBER,7:data%State%RamLine%NO_PIPINGSRAMLINE))  !7=after regulator<<<<<<<
 data%State%RAM(RNUMBER)%minor_loss2=sum( data%State%RAMS%minloss(RNUMBER,17:data%State%RamLine%NO_MINORSRAMLINE))  !17=after regulator<<<<<<<

 data%State%RAM(RNUMBER)%deltah=H_RAMBOP-data%State%ShearRAM%H_REGRAM
! data%State%RAM(RNUMBER)%static_loss2pa= data%State%RAM(RNUMBER)%deltah*sg*wdens*gravity; %(pa)
 data%State%RAM(RNUMBER)%static_loss2= data%State%RAM(RNUMBER)%deltah*sg*wdens*gravity/6895 !(psi)
 data%State%RAM(RNUMBER)%kinetic_loss2=0

 data%State%RAM(RNUMBER)%loss_after= data%State%RAM(RNUMBER)%pipe_loss2+ data%State%RAM(RNUMBER)%minor_loss2+ data%State%RAM(RNUMBER)%static_loss2 !(psi)
!=======================================================

    if(data%EquipmentControl%BopControlPanel%ByePassValve == -1.0) then
        data%State%BopStackAcc%ByPassOld= -1.0
    elseif(data%EquipmentControl%BopControlPanel%ByePassValve == 1.0) then
        data%State%BopStackAcc%ByPassOld= 1.0
    endif
 !   write(*,*) 'ByPassOld=', ByPassOld
 

!==========================p_regulator======================
if (data%State%BopStackAcc%PRAMS_REGSET<data%State%RamLine%P_ACC- data%State%RAM(RNUMBER)%loss_before) then
    
    if(data%State%BopStackAcc%ByPassOld == 1.0)  then  
         !old=pram_reg
         !new=PRAMS_REGSET- sum(data%State%RAMS%minloss(2:6,17))     ! 2:6 = RNUMBER
         !difference= new-old
         data%State%BopStackAcc%PressureDifference= data%State%BopStackAcc%PRAMS_REGSET- sum(data%State%RAMS%minloss(2:6,17)) - data%State%BopStackAcc%pram_reg
         if (ABS(data%State%BopStackAcc%PressureDifference) > BaseDifferenceP) then
             data%State%BopStackAcc%pram_reg= data%State%BopStackAcc%pram_reg + (data%State%BopStackAcc%PressureDifference/PressureDifferenceSteps)      ! PressureDifferenceSteps = 20.
         else
            data%State%BopStackAcc%pram_reg= data%State%BopStackAcc%PRAMS_REGSET- sum(data%State%RAMS%minloss(2:6,17))     ! 2:6 = RNUMBER
         endif
        !pram_reg=PRAMS_REGSET- sum(data%State%RAMS%minloss(2:6,17))     ! 2:6 = RNUMBER
    else    !(ByPassOld == -1.0) 
         !old=pram_reg
         !new=p_acc- data%State%RAM(RNUMBER)%loss_before
         !difference= new-old
         data%State%BopStackAcc%PressureDifference= data%State%RamLine%P_ACC- data%State%RAM(RNUMBER)%loss_before - data%State%BopStackAcc%pram_reg
         if (ABS(data%State%BopStackAcc%PressureDifference) > BaseDifferenceP) then
             data%State%BopStackAcc%pram_reg= data%State%BopStackAcc%pram_reg + (data%State%BopStackAcc%PressureDifference/PressureDifferenceSteps)      ! PressureDifferenceSteps = 20.
         else
            data%State%BopStackAcc%pram_reg= data%State%RamLine%P_ACC- data%State%RAM(RNUMBER)%loss_before
         endif    
    
    endif
        !pram_reg=p_acc- data%State%RAM(RNUMBER)%loss_before

else
    data%State%BopStackAcc%pram_reg=data%State%RamLine%P_ACC- data%State%RAM(RNUMBER)%loss_before- sum(data%State%RAMS%minloss(2:6,17))     ! 2:6 = RNUMBER
endif
!===========================================================


!====================Flow Rate calculation==================
 data%State%RAM(RNUMBER)%diffp_ram=data%State%BopStackAcc%pram_reg+data%State%RAMS%minloss(RNUMBER,17)- data%State%RAM(RNUMBER)%p_bop
    !write(*,*) 'Q calculation'


end do  ! returns correct Q for one timecounter_ram
!write(*,*)'data%State%RAM(RNUMBER)%Q= ' , data%State%RAM(RNUMBER)%Q

!write(*,*) 'UpperRamsLeakMalf , Bottles_Charged_MalfActive=' , UpperRamsLeakMalf , Bottles_Charged_MalfActive
if (data%State%RAM(RNUMBER)%Bottles_Charged_MalfActive==.true.) then
    data%State%RAM(RNUMBER)%Q= 5.0 ! gpm
    !write(*,*) 'malffffffffff'
endif

data%State%RAM(RNUMBER)%flow=data%State%RAM(RNUMBER)%Q



!====================p_accumulator for next iterate==================
! deltav=Q*1*1000;   %delta_t=1sec (lit)
! fvr=fvr-deltav;  %=fvr_after (lit)
! p_acc=0.001178*fvr^3+0.02019*fvr^2+16.1*fvr+2379; %(psia)
! vdis_bottles=fvr_tot-fvr;   %from the start***needed for  p_bop calculating (lit)

!========for a 10 gal bottle,precharge=1000psig curve BOSCH======
data%State%RAM(RNUMBER)%deltav_bottles=data%State%RAM(RNUMBER)%Q*data%State%RamLine%DeltaT_BOP/60.   !(galon) delta_t=1sec , Q(gpm)
data%State%RamLine%FVR=data%State%RamLine%FVR- data%State%RAM(RNUMBER)%deltav_bottles  !=fvr_after (galon)
! b1=993.7; b2=0.164; b3=5.492; b4=0.9796;
 data%State%RAM(RNUMBER)%p_acccheck=data%State%RamLine%P_ACC
data%State%RamLine%P_ACC=data%State%RamLine%B1*exp(data%State%RamLine%B2*data%State%RamLine%FVR/data%State%BopStackAcc%NOBOTTLES)+data%State%RamLine%B3*exp(data%State%RamLine%B4*data%State%RamLine%FVR/data%State%BopStackAcc%NOBOTTLES) !(psig)<<<< 8=no. of bottles
!formula is derived for fvr (galon)
!vdis_bottles=fvr_tot-fvr
 data%State%RAM(RNUMBER)%vdis_bottles= data%State%RAM(RNUMBER)%vdis_bottles+ data%State%RAM(RNUMBER)%deltav_bottles

 data%State%RamLine%NoActiveRmas=6.-real(data%State%Annular%Annular_closed+data%State%RamLine%ShearBop_closed+data%State%PipeRam1%closed+data%State%PipeRam2%closed+data%State%ChokeLine%closed+data%State%KillLine%closed)
 
 data%State%RAM(RNUMBER)%vdis_elecp= data%State%RAM(RNUMBER)%vdis_elecp+(data%State%Pumps%DELTAV_ELECP/data%State%RamLine%NoActiveRmas)  !overal discharged
  data%State%RAM(RNUMBER)%fvr_air= data%State%RAM(RNUMBER)%fvr_air+(data%State%Pumps%DELTAV_AIR/data%State%RamLine%NoActiveRmas)  !=fvr_after (galon) for next timecounter_ram
!write(*,*) 'deltav_air , NoActiveRmas' , deltav_air , NoActiveRmas

 data%State%RAM(RNUMBER)%vdis_tot= data%State%RAM(RNUMBER)%vdis_bottles+ data%State%RAM(RNUMBER)%fvr_air+ data%State%RAM(RNUMBER)%vdis_elecp      !overal
      !  write(*,*)  data%State%RAM(RNUMBER)%vdis_tot, vdis_bottles, data%State%RAM(RNUMBER)%fvr_air, vdis_elecp      !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

if (ramtype==1) then    !for shear ram
     if (data%State%RamLine%ShearRamIsClosing) then
        data%State%ShearRAM%IDshearBop=(2.*(data%State%ShearRAM%NeededVolumeShearRams- data%State%RAM(RNUMBER)%vdis_tot)*231./data%State%ShearRAM%AbopShearRam) +data%State%ShearRAM%ShearIsNotAllowed*data%State%ShearRAM%ODDrillpipe_inShearRam
        !write(*,*) 'stop now' !, IDshearBop, NeededVolumeShearRams, data%State%RAM(RNUMBER)%vdis_tot, AbopShearRam
     endif
     
     if (data%State%RamLine%ShearRamIsOpening) then
        data%State%ShearRAM%IDshearBop=data%State%ShearRAM%IDshearBopBase-2.*(data%State%ShearRAM%NeededVolumeShearRams- data%State%RAM(RNUMBER)%vdis_tot)*231./data%State%ShearRAM%AbopShearRam    
     endif
     

        
      if ( data%State%RAM(RNUMBER)%vdis_tot>data%State%ShearRAM%NeededVolumeShearRams) then

             data%State%RamLine%ShearBop_closed=1
             !ShearBop_closed_withPossibility= ShearBop_closed * TD_BOPConnectionPossibility(3)
             
             data%State%BopStackAcc%pram_reg=data%State%BopStackAcc%pram_reg+data%State%RAMS%minloss(RNUMBER,17)
             data%State%RamLine%P_ACC= data%State%RAM(RNUMBER)%p_acccheck

           if (data%State%RamLine%ShearRamIsClosing) then
              data%State%ShearRAM%IDshearBop=0. + data%State%ShearRAM%ShearIsNotAllowed*data%State%ShearRAM%ODDrillpipe_inShearRam      
              data%EquipmentControl%BopControlPanel%MiddleRamsCloseLED = LedOn
              data%State%BopStackInput%MiddleRamsCloseLEDMine = LedOn
              data%EquipmentControl%BopControlPanel%MiddleRamsOpenLED = LedOff
              data%State%BopStackInput%MiddleRamsOpenLEDMine = LedOff
              if (data%State%TD_BOP%BOPConnectionPossibility(3) == 1 .and. data%State%ShearRAM%ShearIsNotAllowed==0) then
                  CALL CloseMiddleRams
                  data%State%RamLine%ShearBop_Situation_forTD= 1       ! closed - for TD code
              endif
          endif
     
          if (data%State%RamLine%ShearRamIsOpening) then
             data%State%ShearRAM%IDshearBop=data%State%ShearRAM%IDshearBopBase
             data%EquipmentControl%BopControlPanel%MiddleRamsOpenLED = LedOn
             data%State%BopStackInput%MiddleRamsOpenLEDMine = LedOn
             data%EquipmentControl%BopControlPanel%MiddleRamsCloseLED = LedOff
             data%State%BopStackInput%MiddleRamsCloseLEDMine = LedOff
             CALL OpenMiddleRams
             data%State%RamLine%ShearBop_Situation_forTD= 0       ! open - for TD code
         endif
    
      endif  
      
      data%State%ShearRAM%IDshearBopFinal= data%State%ShearRAM%IDshearBop       ! for output data
     
endif

if (ramtype==2) then    !for pipe ram1
     if (data%State%PipeRam1%IsClosing) then
        data%State%PipeRam1%ID=(2.*(data%State%PipeRam1%NeededVolume- data%State%RAM(RNUMBER)%vdis_tot)*231./data%State%PipeRam1%A)+max(data%State%PipeRam1%ODDrillpipe_in,data%State%PipeRam1%ODDrillpipe_inBase)   
     endif
     
     if (data%State%PipeRam1%IsOpening) then
        data%State%PipeRam1%ID=data%State%PipeRam1%IDBase-2.*(data%State%PipeRam1%NeededVolume- data%State%RAM(RNUMBER)%vdis_tot)*231./data%State%PipeRam1%A    
     endif

     if ( data%State%RAM(RNUMBER)%vdis_tot>data%State%PipeRam1%NeededVolume) then

              data%State%PipeRam1%closed=1
              !PipeRam1_Situation_forTD= PipeRam1_closed * TD_BOPConnectionPossibility(2)
              data%State%BopStackAcc%pram_reg=data%State%BopStackAcc%pram_reg+data%State%RAMS%minloss(RNUMBER,17)
              data%State%RamLine%P_ACC= data%State%RAM(RNUMBER)%p_acccheck

           if (data%State%PipeRam1%IsClosing) then
              data%State%PipeRam1%ID=max(data%State%PipeRam1%ODDrillpipe_in,data%State%PipeRam1%ODDrillpipe_inBase)       
              data%EquipmentControl%BopControlPanel%UpperRamsCloseLED = LedOn
              data%State%BopStackInput%UpperRamsCloseLEDMine = LedOn
              data%EquipmentControl%BopControlPanel%UpperRamsOpenLED = LedOff
              data%State%BopStackInput%UpperRamsOpenLEDMine = LedOff
              if (data%State%TD_BOP%BOPConnectionPossibility(2) == 1) then
                  CALL CloseUpperRams       ! for C code
                  call Set_BlowoutFromAnnular(.true.) 

 
                  data%State%PipeRam1%Situation_forTD= 1       ! closed - for TD code
              endif
          endif
     
          if (data%State%PipeRam1%IsOpening) then
              data%State%PipeRam1%ID=data%State%PipeRam1%IDBase
              data%EquipmentControl%BopControlPanel%UpperRamsOpenLED = LedOn
              data%State%BopStackInput%UpperRamsOpenLEDMine = LedOn
              data%EquipmentControl%BopControlPanel%UpperRamsCloseLED = LedOff
              data%State%BopStackInput%UpperRamsCloseLEDMine = LedOff
              Call OpenUpperRams       ! for C code
              data%State%PipeRam1%Situation_forTD= 0       ! open - for TD code
         endif
    
     endif 
     
     data%State%ShearRAM%IDPipeRam1Final= data%State%PipeRam1%ID       ! for output data

endif


if (ramtype==3) then    !for pipe ram2
     if (data%State%PipeRam2%IsClosing) then
        data%State%PipeRam2%ID=(2.*(data%State%PipeRam2%NeededVolume- data%State%RAM(RNUMBER)%vdis_tot)*231./data%State%PipeRam1%A)+max(data%State%PipeRam2%ODDrillpipe_in,data%State%PipeRam1%ODDrillpipe_inBase)   
     endif
     
     if (data%State%PipeRam2%IsOpening) then
        data%State%PipeRam2%ID=data%State%PipeRam1%IDBase-2.*(data%State%PipeRam2%NeededVolume- data%State%RAM(RNUMBER)%vdis_tot)*231./data%State%PipeRam1%A    
     endif

     if ( data%State%RAM(RNUMBER)%vdis_tot>data%State%PipeRam2%NeededVolume) then

              data%State%PipeRam2%closed=1
              !PipeRam2_closed_withPossibility= PipeRam2_closed * TD_BOPConnectionPossibility(4)
              data%State%BopStackAcc%pram_reg=data%State%BopStackAcc%pram_reg+data%State%RAMS%minloss(RNUMBER,17)
              data%State%RamLine%P_ACC= data%State%RAM(RNUMBER)%p_acccheck

           if (data%State%PipeRam2%IsClosing) then
              data%State%PipeRam2%ID=max(data%State%PipeRam2%ODDrillpipe_in,data%State%PipeRam1%ODDrillpipe_inBase)       
              data%EquipmentControl%BopControlPanel%LowerRamsCloseLED = LedOn
              data%State%BopStackInput%LowerRamsCloseLEDMine = LedOn
              data%EquipmentControl%BopControlPanel%LowerRamsOpenLED = LedOff
              data%State%BopStackInput%LowerRamsOpenLEDMine = LedOff
              if (data%State%TD_BOP%BOPConnectionPossibility(4) == 1) then
                  CALL CloseLowerRams
                  data%State%PipeRam2%Situation_forTD= 1       ! closed - for TD code
              endif
          endif
     
          if (data%State%PipeRam2%IsOpening) then
              data%State%PipeRam2%ID=data%State%PipeRam1%IDBase
              data%EquipmentControl%BopControlPanel%LowerRamsOpenLED = LedOn
              data%State%BopStackInput%LowerRamsOpenLEDMine = LedOn
              data%EquipmentControl%BopControlPanel%LowerRamsCloseLED = LedOff
              data%State%BopStackInput%LowerRamsCloseLEDMine = LedOff
              CALL OpenLowerRams
              data%State%PipeRam2%Situation_forTD= 0       ! open - for TD code
         endif
    
     endif  
     
     data%State%ShearRAM%IDPipeRam2Final= data%State%PipeRam2%ID       ! for output data

endif


if (ramtype==4) then    !for Choke Line
     if (data%State%ChokeLine%IsClosing) then
        data%State%ChokeLine%ID=(2.*(data%State%ChokeLine%NeededVolume- data%State%RAM(RNUMBER)%vdis_tot)*231./data%State%ChokeLine%Abop)+max(data%State%ChokeLine%ODDrillpipe_in,data%State%ChokeLine%ODDrillpipe_inBase)  
     endif
     
     if (data%State%ChokeLine%IsOpening) then
        data%State%ChokeLine%ID=data%State%ChokeLine%IDBase-2.*(data%State%ChokeLine%NeededVolume- data%State%RAM(RNUMBER)%vdis_tot)*231./data%State%ChokeLine%Abop    
     endif

     if ( data%State%RAM(RNUMBER)%vdis_tot>data%State%ChokeLine%NeededVolume) then

              data%State%ChokeLine%closed=1
              data%State%BopStackAcc%pram_reg=data%State%BopStackAcc%pram_reg+data%State%RAMS%minloss(RNUMBER,17)
              data%State%RamLine%P_ACC= data%State%RAM(RNUMBER)%p_acccheck

           if (data%State%ChokeLine%IsClosing) then
              data%State%ChokeLine%ID=max(data%State%ChokeLine%ODDrillpipe_in,data%State%ChokeLine%ODDrillpipe_inBase)      
              data%EquipmentControl%BopControlPanel%ChokeLineCloseLED = LedOn
              data%State%BopStackInput%ChokeLineCloseLEDMine = LedOn
              data%EquipmentControl%BopControlPanel%ChokeLineOpenLED = LedOff
              data%State%BopStackInput%ChokeLineOpenLEDMine = LedOff
              CALL CloseChokeLine
          endif
     
          if (data%State%ChokeLine%IsOpening) then
              data%State%ChokeLine%ID=data%State%ChokeLine%IDBase
              data%EquipmentControl%BopControlPanel%ChokeLineOpenLED = LedOn
              data%State%BopStackInput%ChokeLineOpenLEDMine = LedOn
              data%EquipmentControl%BopControlPanel%ChokeLineCloseLED = LedOff
              data%State%BopStackInput%ChokeLineCloseLEDMine = LedOff
              CALL OpenChokeLine
         endif
    
     endif  

endif



if (ramtype==5) then    !for Kill Line
     if (data%State%KillLine%IsClosing) then
        data%State%KillLine%ID=(2.*(data%State%KillLine%NeededVolume- data%State%RAM(RNUMBER)%vdis_tot)*231./data%State%KillLine%Abop)+max(data%State%KillLine%ODDrillpipe_in,data%State%KillLine%ODDrillpipe_inBase)  
     endif
     
     if (data%State%KillLine%IsOpening) then
        data%State%KillLine%ID=data%State%KillLine%IDBase-2.*(data%State%KillLine%NeededVolume- data%State%RAM(RNUMBER)%vdis_tot)*231./data%State%KillLine%Abop    
     endif

     if ( data%State%RAM(RNUMBER)%vdis_tot>data%State%KillLine%NeededVolume) then

              data%State%KillLine%closed=1
              data%State%BopStackAcc%pram_reg=data%State%BopStackAcc%pram_reg+data%State%RAMS%minloss(RNUMBER,17)
              data%State%RamLine%P_ACC= data%State%RAM(RNUMBER)%p_acccheck

           if (data%State%KillLine%IsClosing) then
              data%State%KillLine%ID=max(data%State%KillLine%ODDrillpipe_in,data%State%KillLine%ODDrillpipe_inBase)      
              data%EquipmentControl%BopControlPanel%KillLineCloseLED = LedOn
              data%State%BopStackInput%KillLineCloseLedMine = LedOn
              data%EquipmentControl%BopControlPanel%KillLineOpenLED = LedOff
              data%State%BopStackInput%KillLineOpenLedMine = LedOff
              CALL CloseKillLine
          endif
     
          if (data%State%KillLine%IsOpening) then
              data%State%KillLine%ID=data%State%KillLine%IDBase
              data%EquipmentControl%BopControlPanel%KillLineOpenLED = LedOn
              data%State%BopStackInput%KillLineOpenLedMine = LedOn
              data%EquipmentControl%BopControlPanel%KillLineCloseLED = LedOff
              data%State%BopStackInput%KillLineCloseLedMine = LedOff
              CALL OpenKillLine
         endif
    
     endif  

endif




!if (ramtype==4) then    !for annular
!     if (AnnularIsClosing) then
!        IDAnnular=((NeededVolumeAnnular-vdis_tot)*231./AbopAnnular)+ODDrillpipe_inAnnular   
!     endif
!     
!     if (AnnularIsOpening) then
!        IDAnnular=IDAnnularBase-(NeededVolumeAnnular-vdis_tot)*231./AbopAnnular
!     endif
!
!     if (vdis_tot>NeededVolumeAnnular) then
!
!              Annular_closed=1
!              p_acc= data%State%RAM(RNUMBER)%p_acccheck
!
!           if (AnnularIsClosing) then
!              IDAnnular=ODDrillpipe_inAnnular       
!              AnnularCloseLed = LedOn
!              AnnularOpenLed = LedOff
!          endif
!     
!          if (AnnularIsOpening) then
!              IDAnnular=IDAnnularBase
!              AnnularOpenLed = LedOn
!              AnnularCloseLed = LedOff        
!         endif
!    
!     endif  
!
!endif


    end
    
    
   
    SUBROUTINE bop_codeAnnular(RNUMBER) 
    
    use SimulationVariables 
    use CBopControlPanel
    use SimulationVariables
    USE CEquipmentsConstants
    use SimulationVariables !@
implicit none
INTEGER RNUMBER, I


    !Pannular_regset=min(AnnularRegulatorSetControl,1700.)  ! for changing its set conditions instantaneously
    
!====================================================
!       BOP back pressure without DP
!====================================================    
!if (bop_type==3) then 
    !p_annular=510.725-(30.145*IDAnnular)
    data%State%AnnularComputational%p_annular=448-(19.7*data%State%Annular%IDAnnular)
!    Q=flow    
!    endif 
    
      
    
 data%State%RAM(RNUMBER)%clock=0
!======================Losses============================
 data%State%RAM(RNUMBER)%loss_after=0 !initial value
 data%State%RAM(RNUMBER)%diffp_ram=1000  !initial value
 data%State%RAM(RNUMBER)%loss_before=0

!Q=0.0055;  %initial flow rate (m^3/s)

 data%State%RAM(RNUMBER)%Q=data%State%RAM(RNUMBER)%flow

!write(*,*) 'Q1=' , Q
!write(*,*) 'tol=' , tol
do while (abs( data%State%RAM(RNUMBER)%diffp_ram- data%State%RAM(RNUMBER)%loss_after)/ data%State%RAM(RNUMBER)%diffp_ram>data%State%Annular%tolAnnular)
    if (data%State%RAM(RNUMBER)%Bottles_Charged_MalfActive==.true.) exit
! while abs( data%State%RAM(RNUMBER)%diffp_ram- data%State%RAM(RNUMBER)%loss_after)>10
 data%State%RAM(RNUMBER)%clock= data%State%RAM(RNUMBER)%clock+1

if ( data%State%RAM(RNUMBER)%clock>20) then
!    tclock=clock
    data%State%Annular%tolAnnular=data%State%Annular%tolzeroAnnular+(floor( data%State%RAM(RNUMBER)%clock/10)-1)*.001
endif
 !      if (clock==1) continue
       
    if ( data%State%RAM(RNUMBER)%clock/=1 .and.  data%State%RAM(RNUMBER)%loss_after> data%State%RAM(RNUMBER)%diffp_ram) then
        data%State%RAM(RNUMBER)%Q=data%State%RAM(RNUMBER)%Q-.1
    elseif ( data%State%RAM(RNUMBER)%clock/=1 .and.  data%State%RAM(RNUMBER)%loss_after<= data%State%RAM(RNUMBER)%diffp_ram) then
        data%State%RAM(RNUMBER)%Q=data%State%RAM(RNUMBER)%Q+.01

    endif

!====================Before Regulator=========================
do i=1,data%State%AnnularComputational%NO_PipingsAnnularLine
data%State%AnnularComputational%Re_ANNULARline(i)=data%State%RAM(RNUMBER)%Q*6.30902e-5*data%State%AnnularComputational%DIAM_ANNULARLINE_INCH(i)*0.0254/(data%State%AnnularComputational%AREA_ANNULARLINE(i)*nu)

enddo



do i=1,data%State%AnnularComputational%NO_PipingsAnnularLine
     if (data%State%AnnularComputational%Re_ANNULARline(i)<Re_cr) then
         data%State%AnnularComputational%fricANNULAR(i)=64/data%State%AnnularComputational%Re_ANNULARline(i)
     else

        data%State%AnnularComputational%fricANNULAR(i)=1/(-1.8*log10((data%State%AnnularComputational%RELROUGH_ANNULARLINE(i)/3.7)**1.11+6.9/data%State%AnnularComputational%Re_ANNULARline(i)))**2
     endif

data%State%AnnularComputational%friclossANNULAR(i)=((data%State%AnnularComputational%fricANNULAR(i)*(wdens*sg*data%State%AnnularComputational%LENGT_ANNULARLINE(i)*(data%State%RAM(RNUMBER)%Q*6.30902e-5/data%State%AnnularComputational%AREA_ANNULARLINE(i))**2))/(2*data%State%AnnularComputational%DIAM_ANNULARLINE_INCH(i)*0.0254))/6895

enddo




 data%State%RAM(RNUMBER)%pipe_loss1=sum(data%State%AnnularComputational%friclossANNULAR(1:6))  !loss before regulator(psi)


do i=1,data%State%AnnularComputational%NO_MinorsAnnularLine
    if (data%State%AnnularComputational%LF_ANNULARLINE(i)/=0) then
        data%State%AnnularComputational%minlosspa_ANNULAR(i)=data%State%AnnularComputational%LF_ANNULARLINE(i)*wdens*sg*(data%State%RAM(RNUMBER)%Q*6.30902e-5/data%State%AnnularComputational%AREAMINOR_ANNULARLINE(i))**2/2  !(Pa)
        data%State%AnnularComputational%minloss_ANNULAR(i)=data%State%AnnularComputational%minlosspa_ANNULAR(i)/6895 !(psi)
    elseif (data%State%AnnularComputational%CV_ANNULARLINE(i)/=0) then
        data%State%AnnularComputational%minlosspa_ANNULAR(i)=1000*sg*((11.7*data%State%RAM(RNUMBER)%Q*6.30902e-5*3600)/(data%State%AnnularComputational%CV_ANNULARLINE(i)))**2 !(pa)
        data%State%AnnularComputational%minloss_ANNULAR(i)=data%State%AnnularComputational%minlosspa_ANNULAR(i)/6895 !(psi)
    else
        data%State%AnnularComputational%minlosspa_ANNULAR(i)=data%State%AnnularComputational%NOTE_ANNULARLINE(i)*1e5 !(pa)
        data%State%AnnularComputational%minloss_ANNULAR(i)=data%State%AnnularComputational%minlosspa_ANNULAR(i)/6895 !(psi)
    endif
enddo

data%State%AnnularComputational%minloss_ANNULAR(17)= 3.4*data%State%RAM(RNUMBER)%Q       ! <==== regulator loss (psig)
        
 data%State%RAM(RNUMBER)%minor_loss1=sum(data%State%AnnularComputational%minloss_ANNULAR(1:16))  !loss before regulator(psi)

 data%State%RAM(RNUMBER)%static_loss1=0

 data%State%RAM(RNUMBER)%kinetic_loss1=sg*wdens*(data%State%RAM(RNUMBER)%Q*6.30902e-5/((1/4.)*pi*(.72*0.254e-1)**2))**2/(2*6895) !(psi)
!.72 for regulator diameter)

 data%State%RAM(RNUMBER)%loss_before= data%State%RAM(RNUMBER)%pipe_loss1+ data%State%RAM(RNUMBER)%minor_loss1+ data%State%RAM(RNUMBER)%static_loss1+ data%State%RAM(RNUMBER)%kinetic_loss1 !(psi)
!===================after Regulator======================
! all in (psi)
 data%State%RAM(RNUMBER)%pipe_loss2=sum(data%State%AnnularComputational%friclossANNULAR(7:data%State%AnnularComputational%NO_PipingsAnnularLine))  !7=after regulator<<<<<<<
 data%State%RAM(RNUMBER)%minor_loss2=sum(data%State%AnnularComputational%minloss_ANNULAR(17:data%State%AnnularComputational%NO_MinorsAnnularLine))  !17=after regulator<<<<<<<

 data%State%RAM(RNUMBER)%deltah=data%State%Annular%H_AnnularBop-data%State%ShearRAM%H_REGRAM
! data%State%RAM(RNUMBER)%static_loss2pa= data%State%RAM(RNUMBER)%deltah*sg*wdens*gravity; %(pa)
 data%State%RAM(RNUMBER)%static_loss2= data%State%RAM(RNUMBER)%deltah*sg*wdens*gravity/6895 !(psi)
 data%State%RAM(RNUMBER)%kinetic_loss2=0

 data%State%RAM(RNUMBER)%loss_after= data%State%RAM(RNUMBER)%pipe_loss2+ data%State%RAM(RNUMBER)%minor_loss2+ data%State%RAM(RNUMBER)%static_loss2 !(psi)
!=======================================================



!==========================p_regulator======================
if (data%State%Annular%Pannular_regset<data%State%RamLine%P_ACC- data%State%RAM(RNUMBER)%loss_before) then
    
            !!new= Pannular_regset-minloss_ANNULAR(17) 
            ! 
            !CALL Pannular_regDelay%AddToFirst( Pannular_regset-minloss_ANNULAR(17) )
            !CALL Pannular_regDelay%Remove(Pannular_regDelay%Length())
            !Pannular_reg= Pannular_regDelay%Last()
            
            data%State%Annular%Pannular_reg= data%State%Annular%Pannular_regset-data%State%AnnularComputational%minloss_ANNULAR(17)     

    ! data%State%RAM(RNUMBER)%loss_after= data%State%RAM(RNUMBER)%loss_after-minloss(17);   %minloss(17)=regulator loss
else
    
            !!new= p_acc- data%State%RAM(RNUMBER)%loss_before-minloss_ANNULAR(17)
            ! 
            !CALL Pannular_regDelay%AddToFirst( p_acc- data%State%RAM(RNUMBER)%loss_before-minloss_ANNULAR(17) )
            !CALL Pannular_regDelay%Remove(Pannular_regDelay%Length())
            !Pannular_reg= Pannular_regDelay%Last()
            
            data%State%Annular%Pannular_reg= data%State%RamLine%P_ACC- data%State%RAM(RNUMBER)%loss_before-data%State%AnnularComputational%minloss_ANNULAR(17)    

endif
!===========================================================


!====================Flow Rate calculation==================
 data%State%RAM(RNUMBER)%diffp_ram=data%State%Annular%Pannular_reg+data%State%AnnularComputational%minloss_ANNULAR(17)-data%State%AnnularComputational%p_annular





end do  ! returns correct Q for one timecounter_ram

if (data%State%RAM(RNUMBER)%Bottles_Charged_MalfActive==.true.) then
    data%State%RAM(RNUMBER)%Q= 5.0 ! gpm
    !write(*,*) 'malffffffffff'
endif

data%State%RAM(RNUMBER)%flow=data%State%RAM(RNUMBER)%Q
!write(*,*) 'minloss_ANNULAR(17)=' , minloss_ANNULAR(17)
!write(*,*) '%loss_after=' ,data%State%RAM(RNUMBER)%loss_after
!====================p_accumulator for next iterate==================
! deltav=Q*1*1000;   %delta_t=1sec (lit)
! fvr=fvr-deltav;  %=fvr_after (lit)
! p_acc=0.001178*fvr^3+0.02019*fvr^2+16.1*fvr+2379; %(psia)
! vdis_bottles=fvr_tot-fvr;   %from the start***needed for p_BOP calculating (lit)

!========for a 10 gal bottle,precharge=1000psig curve BOSCH======
 data%State%RAM(RNUMBER)%deltav_bottles=data%State%RAM(RNUMBER)%Q*data%State%RamLine%DeltaT_BOP/60.   !(galon) delta_t=1sec , Q(gpm)
data%State%RamLine%FVR=data%State%RamLine%FVR- data%State%RAM(RNUMBER)%deltav_bottles  !=fvr_after (galon)
! b1=993.7; b2=0.164; b3=5.492; b4=0.9796;
 data%State%RAM(RNUMBER)%p_acccheck=data%State%RamLine%P_ACC
data%State%RamLine%P_ACC=data%State%RamLine%B1*exp(data%State%RamLine%B2*data%State%RamLine%FVR/data%State%BopStackAcc%NOBOTTLES)+data%State%RamLine%B3*exp(data%State%RamLine%B4*data%State%RamLine%FVR/data%State%BopStackAcc%NOBOTTLES) !(psig)<<<< 8=no. of bottles
!formula is derived for fvr (galon)
!vdis_bottles=fvr_tot-fvr
 data%State%RAM(RNUMBER)%vdis_bottles= data%State%RAM(RNUMBER)%vdis_bottles+ data%State%RAM(RNUMBER)%deltav_bottles

 data%State%RamLine%NoActiveRmas=6.-real(data%State%Annular%Annular_closed+data%State%RamLine%ShearBop_closed+data%State%PipeRam1%closed+data%State%PipeRam2%closed+data%State%ChokeLine%closed+data%State%KillLine%closed)
 data%State%RAM(RNUMBER)%vdis_elecp= data%State%RAM(RNUMBER)%vdis_elecp+(data%State%Pumps%DELTAV_ELECP/data%State%RamLine%NoActiveRmas)  !overal discharged
  data%State%RAM(RNUMBER)%fvr_air= data%State%RAM(RNUMBER)%fvr_air+(data%State%Pumps%DELTAV_AIR/data%State%RamLine%NoActiveRmas)  !=fvr_after (galon) for next timecounter_ram


 data%State%RAM(RNUMBER)%vdis_tot= data%State%RAM(RNUMBER)%vdis_bottles+ data%State%RAM(RNUMBER)%fvr_air+ data%State%RAM(RNUMBER)%vdis_elecp      !overal
        !write(*,*)  'data%State%RAM(RNUMBER)%vdis_tot=' , data%State%RAM(RNUMBER)%vdis_tot!, vdis_bottles,fvr_air, vdis_elecp      !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

        !write(*,*) 'a)IDAnnular=' , IDAnnular
        !    WRITE(*,*) 'a)NeededVolumeAnnular=' , NeededVolumeAnnular
        !    
        !    write(*,*) 'AbopAnnular= ' , AbopAnnular
        !    write(*,*) 'max=' , max(ODDrillpipe_inAnnular,ODDrillpipe_inAnnularBase)  

!if (ramtype==4) then    !for annular
     if (data%State%Annular%AnnularIsClosing) then
        data%State%Annular%IDAnnular=(2.*(data%State%Annular%NeededVolumeAnnular- data%State%RAM(RNUMBER)%vdis_tot)*231./data%State%Annular%AbopAnnular)+max(data%State%Annular%ODDrillpipe_inAnnular,data%State%Annular%ODDrillpipe_inAnnularBase)  
        !write(*,*) 'b)IDAnnular=' , IDAnnular
     endif
     
     if (data%State%Annular%AnnularIsOpening) then
        data%State%Annular%IDAnnular=data%State%Annular%IDAnnularBase-2.*(data%State%Annular%NeededVolumeAnnular- data%State%RAM(RNUMBER)%vdis_tot)*231./data%State%Annular%AbopAnnular
     endif

     if ( data%State%RAM(RNUMBER)%vdis_tot>data%State%Annular%NeededVolumeAnnular) then

              data%State%Annular%Annular_closed=1
              !Annular_closed_withPossibility= Annular_closed * TD_BOPConnectionPossibility(1)
              !pram_reg=pram_reg+minloss_ANNULAR(17)
            data%State%Annular%Pannular_reg= data%State%Annular%Pannular_reg+data%State%AnnularComputational%minloss_ANNULAR(17)               

              data%State%RamLine%P_ACC= data%State%RAM(RNUMBER)%p_acccheck
           if (data%State%Annular%AnnularIsClosing) then
               
              data%State%Annular%IDAnnular=max(data%State%Annular%ODDrillpipe_inAnnular,data%State%Annular%ODDrillpipe_inAnnularBase)      
              data%EquipmentControl%BopControlPanel%AnnularCloseLED = LedOn
              data%State%BopStackInput%AnnularCloseLedMine = LedOn
              
              data%EquipmentControl%BopControlPanel%AnnularOpenLED = LedOff
              data%State%BopStackInput%AnnularOpenLedMine = LedOff
              
              if (data%State%TD_BOP%BOPConnectionPossibility(1) == 1) then
                  CALL CloseAnnular
                  data%State%Annular%Annular_Situation_forTD= 1       ! closed - for TD code
              endif
           endif
           
           
     
          if (data%State%Annular%AnnularIsOpening) then
              
              data%State%Annular%IDAnnular=data%State%Annular%IDAnnularBase
              data%EquipmentControl%BopControlPanel%AnnularOpenLED = LedOn
              data%State%BopStackInput%AnnularOpenLedMine = LedOn
              
              data%EquipmentControl%BopControlPanel%AnnularCloseLED = LedOff
              data%State%BopStackInput%AnnularCloseLedMine = LedOff
              
              CALL OpenAnnular
              data%State%Annular%Annular_Situation_forTD= 0       ! open - for TD code
         endif
    
     endif 
     
        data%State%ShearRAM%IDAnnularFinal= data%State%Annular%IDAnnular       ! for output data
!endif


    end    