 
   
module ChokeModule
    USE CHOKEVARIABLES
use SimulationVariables !@ 
    !use CDataDisplayConsole
    ! !@use ConfigurationVariables, CasingPressureDataDisplay=>CasingPressure!, StandPipePressureDataDisplay=>StandPipePressure
    USE CChokeProblemsVariables
    !use CChokeControlPanelVariables
    ! !@use ConfigurationVariables, StandPipePressureChoke=>StandPipePressure
    USE MudSystemVARIABLES
use SimulationVariables !@@@
    use CChokeManifold
    use SimulationVariables
    
    contains
    
    ! function CHOKE_MainBody1 result(CHNUMBER)
    !     implicit none
    !     INTEGER CHNUMBER
    !     IF(data%EquipmentControl%ChokeControlPanel%ChokeSelectorSwitch== 1) THEN
    !         data%EquipmentControl%ChokeControlPanel%Choke1LED=1
    !         data%EquipmentControl%ChokeControlPanel%Choke2LED=0
    !         CHNUMBER=1
    !     ELSE
    !         data%EquipmentControl%ChokeControlPanel%Choke2LED=1
    !         data%EquipmentControl%ChokeControlPanel%Choke1LED=0
    !         CHNUMBER=2
    !     ENDIF
    ! end function CHOKE_MainBody1
        ! CALL PIPE_RAMS_CHOKE(CHNUMBER)

    ! subroutine Choke_innerLoop
    !     ! do while (ABS(data%EquipmentControl%ChokeControlPanel%ChokeControlLever)==1.0 .AND. data%EquipmentControl%ChokeControlPanel%ChokePanelRigAirSwitch == 1 .AND. data%State%CHOOKE(CHNUMBER)%FailMalf==0 .AND. data%State%AirDrivenPump%ChokeAirFail==0)
    !     if (data%EquipmentControl%ChokeControlPanel%ChokeControlLever == 1.0) then
    !         data%State%CHOOKE(CHNUMBER)%ChokeIsClosing = .true.
    !         data%State%CHOOKE(CHNUMBER)%ChokeIsOpening = .false.
    !     endif
    !     if (data%EquipmentControl%ChokeControlPanel%ChokeControlLever == -1.0) then 
    !         data%State%CHOOKE(CHNUMBER)%ChokeIsOpening = .true.
    !         data%State%CHOOKE(CHNUMBER)%ChokeIsClosing = .false.
    !     endif
    !     data%State%AirPumpLine%TIME=data%State%AirPumpLine%TIME+data%State%AirPumpLine%DeltaT_Choke  !overal time (s)
    !     call airpump_code_CHOKE(CHNUMBER)
    !     ! call sleepqq(100)
    !     ! if (IsStopped == .true.) return  
    !     ! enddo
    !     end if
    ! end subroutine Choke_innerLoop
    
    subroutine CHOKE_MainBody2
        IF (data%EquipmentControl%ChokeControlPanel%Choke1LED==1) THEN
            data%EquipmentControl%ChokeControlPanel%ChokePosition= (1 - data%State%Choke%GaugeChokePositionMailf) * data%State%CHOOKE(1)%PercentClose*10       ! display monitor
        ELSE       ! Choke2LED==1
            data%EquipmentControl%ChokeControlPanel%ChokePosition= (1 - data%State%Choke%GaugeChokePositionMailf) * data%State%CHOOKE(2)%PercentClose*10       ! display monitor
        ENDIF
        ! =================== calculating Area
        ! AreaChoke=0.5
        ! .91 >> 9 percent clearance
        data%State%CHOOKE(1)%AreaChoke=0.01334635-(0.01334635* MIN(data%State%CHOOKE(1)%PercentClose / 0.91 , 1.0)) !ft^2     0.01334635 is ChokeAreaFullyOpen (ft^2)
        data%State%CHOOKE(2)%AreaChoke=0.01334635-(0.01334635* MIN(data%State%CHOOKE(2)%PercentClose/ 0.91 , 1.0)) !ft^2
        !write(*,*) 'data%State%CHOOKE(1)%AreaChoke= ' , data%State%CHOOKE(1)%AreaChoke
        data%State%Choke%HydraulicChoke1WashoutCoef= data%State%Choke%HydraulicChoke1WashoutCoef * data%State%CHOOKE(1)%WashoutMalf    
        data%State%Choke%HydraulicChoke1WashoutCoef= MIN( 0.5 , data%State%Choke%HydraulicChoke1WashoutCoef+ data%State%CHOOKE(1)%WashoutMalf*(0.5/(180.0/data%State%AirPumpLine%DeltaT_Choke)) ) ! 0.5 = maximum washout coef , 180.0 sec = 3 min duration time
        data%State%Choke%HydraulicChoke2WashoutCoef= data%State%Choke%HydraulicChoke2WashoutCoef * data%State%CHOOKE(2)%WashoutMalf    
        data%State%Choke%HydraulicChoke2WashoutCoef= MIN( 0.5 , data%State%Choke%HydraulicChoke2WashoutCoef+ data%State%CHOOKE(2)%WashoutMalf*(0.5/(180.0/data%State%AirPumpLine%DeltaT_Choke)) ) ! 0.5 = maximum washout coef , 180.0 sec = 3 min duration time
        !write(*,*) 'HydraulicChoke1WashoutCoef=' , HydraulicChoke1WashoutCoef        
        IF (data%State%CHOOKE(1)%PlugMalf == 1) THEN
            data%State%Choke%Present_HydraulicChoke1Plug= data%State%Choke%Present_HydraulicChoke1Plug * data%State%CHOOKE(1)%PlugMalf 
            ! integer: HydraulicChoke1PluggedPercent,HydraulicChoke1PluggedPercent_Old,PlugTimeCounter,ChokePlugTimeDelay
            ! real: Present_HydraulicChoke1Plug,DeltaPlug1Percent
            !write(*,*) 'HydraulicChoke1PluggedPercent=' , HydraulicChoke1PluggedPercent
            if ( (data%problems%ChokeProblems%HydraulicChoke1PluggedPercent - data%State%Choke%HydraulicChoke1PluggedPercent_Old) /= 0) then
                data%State%Choke%DeltaPlug1Percent = (REAL(data%problems%ChokeProblems%HydraulicChoke1PluggedPercent)/100.) - data%State%Choke%Present_HydraulicChoke1Plug
                data%State%Choke%Plug1TimeCounter = 0
                !write(*,*) 'DeltaPlug1Percent, Present_HydraulicChoke1Plug  =  ' ,DeltaPlug1Percent , Present_HydraulicChoke1Plug 
            endif
            data%State%Choke%Plug1TimeCounter= data%State%Choke%Plug1TimeCounter + 1
            data%State%Choke%HydraulicChoke1PluggedPercent_Old= data%problems%ChokeProblems%HydraulicChoke1PluggedPercent
            if (data%State%Choke%Plug1TimeCounter <= data%State%Choke%ChokePlugTimeDelay) then    !ChokePlugTimeDelay=1800        
                data%State%Choke%Present_HydraulicChoke1Plug = data%State%Choke%Present_HydraulicChoke1Plug + data%State%CHOOKE(1)%PlugMalf* ((data%State%Choke%DeltaPlug1Percent / real(data%State%Choke%ChokePlugTimeDelay)))     ! real(ChokePlugTimeDelay)= 600.0
            endif

            !write(*,*) 'Present_HydraulicChoke1Plug=' , Present_HydraulicChoke1Plug
        ENDIF
            
        IF (data%State%CHOOKE(2)%PlugMalf == 1) THEN 
            data%State%Choke%Present_HydraulicChoke2Plug= data%State%Choke%Present_HydraulicChoke2Plug * data%State%CHOOKE(2)%PlugMalf 
            ! integer: HydraulicChoke2PluggedPercent,HydraulicChoke2PluggedPercent_Old,PlugTimeCounter,ChokePlugTimeDelay
            ! real: Present_HydraulicChoke2Plug,DeltaPlug2Percent
            if ( (data%problems%ChokeProblems%HydraulicChoke2PluggedPercent - data%State%Choke%HydraulicChoke2PluggedPercent_Old) /= 0 ) then
                data%State%Choke%DeltaPlug2Percent = (REAL(data%problems%ChokeProblems%HydraulicChoke2PluggedPercent)/100.) - data%State%Choke%Present_HydraulicChoke2Plug
                data%State%Choke%Plug2TimeCounter = 0
            endif    
            data%State%Choke%Plug2TimeCounter= data%State%Choke%Plug2TimeCounter + 1
            data%State%Choke%HydraulicChoke2PluggedPercent_Old= data%problems%ChokeProblems%HydraulicChoke2PluggedPercent
            if (data%State%Choke%Plug2TimeCounter <= data%State%Choke%ChokePlugTimeDelay) then    !ChokePlugTimeDelay=1800
                data%State%Choke%Present_HydraulicChoke2Plug = data%State%Choke%Present_HydraulicChoke2Plug + data%State%CHOOKE(2)%PlugMalf *((data%State%Choke%DeltaPlug2Percent / real(data%State%Choke%ChokePlugTimeDelay)))     ! real(ChokePlugTimeDelay)= 1800.0
            endif        
        ENDIF
        ! fully open area is 123/64 in^2 = 0.01334635 ft^2
        data%State%CHOOKE(1)%AreaChoke=data%State%CHOOKE(1)%AreaChoke+(data%State%CHOOKE(1)%WashoutMalf*data%State%Choke%HydraulicChoke1WashoutCoef*data%State%Choke%ChokeAreaFullyOpen/144.0)      ! Initialised in Choke Startup
        
        !write(*,*) 'data%State%CHOOKE(1)%WashoutMalf , data%State%CHOOKE(1)%AreaChoke='  ,data%State%CHOOKE(1)%WashoutMalf , data%State%CHOOKE(1)%AreaChoke
        
        data%State%CHOOKE(1)%AreaChoke=data%State%CHOOKE(1)%AreaChoke-(data%State%CHOOKE(1)%PlugMalf* data%State%Choke%Present_HydraulicChoke1Plug *data%State%CHOOKE(1)%AreaChoke)         ! Initialised in Choke Startup
        
        data%State%CHOOKE(2)%AreaChoke=data%State%CHOOKE(2)%AreaChoke+(data%State%CHOOKE(2)%WashoutMalf*data%State%Choke%HydraulicChoke2WashoutCoef*data%State%Choke%ChokeAreaFullyOpen/144.0)      ! Initialised in Choke Startup
        data%State%CHOOKE(2)%AreaChoke=data%State%CHOOKE(2)%AreaChoke-(data%State%CHOOKE(2)%PlugMalf* data%State%Choke%Present_HydraulicChoke2Plug *data%State%CHOOKE(2)%AreaChoke)         ! Initialised in Choke Startup
        !write(*,*) 'data%State%CHOOKE(2)%WashoutMalf , data%State%CHOOKE(2)%AreaChoke='  ,data%State%CHOOKE(2)%WashoutMalf , data%State%CHOOKE(2)%AreaChoke
        
        
        data%State%CHOOKE(1)%AreaChokeFinal= data%State%CHOOKE(1)%AreaChoke
        data%State%CHOOKE(2)%AreaChokeFinal= data%State%CHOOKE(2)%AreaChoke 
    
        ! 144: ft^2 to in^2
        CALL SetHydraulicChock1(100 - nint((data%State%CHOOKE(1)%AreaChokeFinal/(data%State%Choke%ChokeAreaFullyOpen/144.))*100))     ! for manifold valve    
        CALL SetHydraulicChock2(100 - nint((data%State%CHOOKE(2)%AreaChokeFinal/(data%State%Choke%ChokeAreaFullyOpen/144.))*100))     ! for manifold valve     
    
        !write(*,*) 'data%State%CHOOKE(1)%PercentClose=' , data%State%CHOOKE(1)%PercentClose     ! close percent 0 to 100
        !write(*,*) 'ChokePosition=' , ChokePosition     ! close position 0 to 10 for display
        !write(*,*) 'data%State%CHOOKE(1)%AreaChokeFinal=' , data%State%CHOOKE(1)%AreaChokeFinal       ! Open Area (in^2) with clearance
        !write(*,*) '(ChokeAreaFullyOpen/144.)=' , (ChokeAreaFullyOpen/144.)
        !write(*,*) 'valve value=' , 100 - nint((data%State%CHOOKE(1)%AreaChokeFinal/(ChokeAreaFullyOpen/144.))*100)        ! percent close 0 to 100 with clearance
        !write(*,*) 'Valve(33)%Status=' , Valve(33)%Status       ! T : open , F: close    
    ! =================== calculating Area   
    
    !AreaChoke= data%State%CHOOKE(1)%AreaChoke   !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< badan moteghayer 1 ya 2 shavad
    
    ! write(*,*)    'AreaChoke:',AreaChoke

    !===========================================================================
    !                        	MUD PUMP STROKES
    !                   & TOTAL STROKE- CHOKE CONTROL PANEL
    !=========================================================================== 
    
    !************************* IN MUD SYSTEM MODULE*************************    
 end subroutine CHOKE_MainBody2

end module ChokeModule