subroutine TD_BOPDiamCalculation
    
   Use TD_DrillStemComponents
   use SimulationVariables !@
   use SimulationVariables !@
   use SimulationVariables !@
   Use TD_StringConnectionData
   Use CBopStackVariables
   use SimulationVariables
   use DownHoleModule
   
   
   Integer :: i , j , n , m , TD_Numbs
   Real(8) :: TD_LimitUp , TD_LimitDown , TD_OldFillingValue , TD_AnnTjDiff , TD_AnnularFilling
   Real(8) :: TD_ElToolJoints(2,2)
   
   
   
   
   
   !data%State%TD_String%ToolJointRange = 0.4005d0*3.28   ! [ft]
   
!====================================================
!                  Read BOP Data
!====================================================
   
   data%State%TD_BOP%BOPHeight(5) = data%Configuration%BopStack%AboveAnnularHeight
   data%State%TD_BOP%BOPHeight(1) = data%Configuration%BopStack%AnnularPreventerHeight
   data%State%TD_BOP%BOPHeight(2) = data%Configuration%BopStack%UpperRamHeight
   data%State%TD_BOP%BOPHeight(3) = data%Configuration%BopStack%BlindRamHeight
   data%State%TD_BOP%BOPHeight(6) = data%Configuration%BopStack%KillHeight
   data%State%TD_BOP%BOPHeight(4) = data%Configuration%BopStack%LowerRamHeight
   
   
   data%State%TD_BOP%BOPRamDiam(1) = data%State%ShearRAM%IDAnnularfinal
   data%State%TD_BOP%BOPRamDiam(2) = data%State%ShearRAM%IDPipeRam1final
   data%State%TD_BOP%BOPRamDiam(3) = data%State%ShearRAM%IDshearBopfinal
   data%State%TD_BOP%BOPRamDiam(4) = data%State%ShearRAM%IDPipeRam2final
   
   
   
   
   
   
!====================================================
!         Element Counts in BOPStack Domain
!====================================================
   
   !if (data%State%TD_String%DrillStemComponentsNumbs>5) then
       TD_Numbs = data%State%TD_String%DrillStemComponentsNumbs-7   ! 7 Elements from the Top of DrillStem
   !else
   !    TD_Numbs = 1
   !end if
   
    
    
    
    
    
    
    
    
!====================================================
! Determination of Elements Diameter in BOPStack Domain
!====================================================

   data%State%TD_BOP%BOPDiam = 0.d0
   TD_OldFillingValue = 0.d0
   Do i = data%State%TD_String%DrillStemComponentsNumbs,TD_Numbs,-1
       
       TD_LimitUp   = data%State%TD_DrillStems(i)%TopDepth+data%State%TD_DrillStems(i)%ToolJointRange
       TD_LimitDown = data%State%TD_DrillStems(i)%DownDepth-data%State%TD_DrillStems(i)%ToolJointRange
       TD_ElToolJoints(1,1) = data%State%TD_DrillStems(i)%TopDepth        ! TD_ElToolJoints(i,j) , i=top & down tooljoints of element , j=top & down tooljoints Depth
       TD_ElToolJoints(1,2) = TD_LimitUp
       TD_ElToolJoints(2,1) = TD_LimitDown
       TD_ElToolJoints(2,2) = data%State%TD_DrillStems(i)%DownDepth
       
       
       !===> che meghdar az fazaye annular ba tooljoint por mishavad (for BOP Module)
       Do m = 1,2
           TD_AnnTjDiff = min(TD_ElToolJoints(m,2),(data%State%TD_BOP%BOPHeight(1)+data%State%TD_BOP%BOPThickness))-max(TD_ElToolJoints(m,1),(data%State%TD_BOP%BOPHeight(1)-data%State%TD_BOP%BOPThickness))
           if (TD_AnnTjDiff<0.) then     ! tooljoint is not in the annular range
               TD_AnnTjDiff = 0.d0
           end if
           TD_AnnularFilling  = TD_OldFillingValue+(TD_AnnTjDiff/(data%State%TD_BOP%BOPThickness*2.d0))     ! 0=<TD_AnnularFilling<=1
           TD_OldFillingValue = TD_AnnularFilling
       End Do
       
       
       do j = 1,6
           if ( (data%State%TD_BOP%BOPHeight(j)-data%State%TD_BOP%BOPThickness)>TD_LimitUp .and. (data%State%TD_BOP%BOPHeight(j)+data%State%TD_BOP%BOPThickness)<TD_LimitDown ) then
               data%State%TD_BOP%BOPDiam(j)      = data%State%TD_DrillStems(i)%Od
               data%State%TD_BOP%BOPElementNo(j) = i
               if ( data%State%TD_DrillStems(i)%ComponentType==3 .and. j/=1 .and. data%State%TD_String%DrillStemRotVelocity==0. ) then
                   data%State%TD_BOP%BOPConnectionPossibility(j) = 1
               else if ( j==1 ) then
                   data%State%TD_BOP%BOPConnectionPossibility(j) = 1
               else
                   data%State%TD_BOP%BOPConnectionPossibility(j) = 0
               end if
               !print* , 'T.DP. , B.DP. 1=' , data%State%TD_DrillStems(i)%TopDepth , data%State%TD_DrillStems(i)%DownDepth
               !print* , 'T.R. , B.R. , LimitUp , LimitDown 1=' , (data%State%TD_BOP%BOPHeight(j)-data%State%TD_BOP%BOPThickness) , (data%State%TD_BOP%BOPHeight(j)+data%State%TD_BOP%BOPThickness) , TD_LimitUp , TD_LimitDown
               !print* , 'stringNo , ramsNo , Possibility , BOPDiam 1=' , i , j , data%State%TD_BOP%BOPConnectionPossibility(j) , data%State%TD_BOP%BOPDiam(j)
           else if ( (data%State%TD_BOP%BOPHeight(j)+data%State%TD_BOP%BOPThickness)>=data%State%TD_DrillStems(i)%TopDepth .and. (data%State%TD_BOP%BOPHeight(j)-data%State%TD_BOP%BOPThickness)<=data%State%TD_DrillStems(i)%DownDepth ) then
               data%State%TD_BOP%BOPDiam(j)      = data%State%TD_DrillStems(i)%RtoolJoint*2.d0
               data%State%TD_BOP%BOPElementNo(j) = i
               if ( j==1 ) then
                   data%State%TD_BOP%BOPConnectionPossibility(j) = 1
               else
                   data%State%TD_BOP%BOPConnectionPossibility(j) = 0
               end if
               !print* , 'T.DP. , B.DP. 2=' , data%State%TD_DrillStems(i)%TopDepth , data%State%TD_DrillStems(i)%DownDepth
               !print* , 'T.R. , B.R. , LimitUp , LimitDown 2=' , (data%State%TD_BOP%BOPHeight(j)-data%State%TD_BOP%BOPThickness) , (data%State%TD_BOP%BOPHeight(j)+data%State%TD_BOP%BOPThickness) , TD_LimitUp , TD_LimitDown
               !print* , 'stringNo , ramsNo , Possibility , BOPDiam 2=' , i , j , data%State%TD_BOP%BOPConnectionPossibility(j) , data%State%TD_BOP%BOPDiam(j)
           end if
       end do
       
   End Do
   data%State%TD_BOP%AnnularFillingFinal = TD_AnnularFilling
   
   data%State%TD_BOP%AboveAnnularDiam     = data%State%TD_BOP%BOPDiam(5)
   data%State%TD_BOP%AnnularPreventerDiam = data%State%TD_BOP%BOPDiam(1)
   data%State%TD_BOP%UpperRamDiam         = data%State%TD_BOP%BOPDiam(2)
   data%State%TD_BOP%BlindRamDiam         = data%State%TD_BOP%BOPDiam(3)
   data%State%TD_BOP%KillDiam             = data%State%TD_BOP%BOPDiam(6)
   data%State%TD_BOP%LowerRamDiam         = data%State%TD_BOP%BOPDiam(4)
   
   
    !print* , 'data%State%TD_BOP%BOPElementNo=' , data%State%TD_BOP%BOPElementNo
    !print* , 'data%State%TD_BOP%BOPConnectionPossibility=' , data%State%TD_BOP%BOPConnectionPossibility
    !print* , 'data%State%TD_BOP%BOPDiam=' , data%State%TD_BOP%BOPDiam
   
   
   
   
   !!===> BOP RAMs Condition  ***  data%State%TD_BOP%BOPCondition:  0=open , 1=close
   !do j = 1,4
   !     if ( data%State%TD_BOP%BOPDiam(j)>=data%State%TD_BOP%BOPRamDiam(j) ) then
   !         data%State%TD_BOP%BOPCondition(j) = 1
   !     else
   !         data%State%TD_BOP%BOPCondition(j) = 0
   !     end if
   !end do
   
   
   
   
   
   
   
   
!====================================================
!   String Elements Configuration in BOPStack Domain
!====================================================
   
   !if ( allocated(data%State%TD_BOPElement) ) deallocate (data%State%TD_BOPElement)
   !allocate(data%State%TD_BOPElement())
   n = 0
   data%State%TD_BOPElement%ElementType  = 0.d0
   data%State%TD_BOPElement%ElementStart = 0.d0
   data%State%TD_BOPElement%ElementEnd   = 0.d0
   do k = data%State%TD_String%DrillStemComponentsNumbs,TD_Numbs,-1
       if( ((data%State%TD_DrillStems(k)%TopDepth>=data%State%TD_BOP%BOPHeight(5)).and.(data%State%TD_DrillStems(k)%TopDepth<=data%State%TD_BOP%BOPHeight(4))) .or. ((data%State%TD_DrillStems(k)%DownDepth>=data%State%TD_BOP%BOPHeight(5)).and.(data%State%TD_DrillStems(k)%DownDepth<=data%State%TD_BOP%BOPHeight(4))) ) then
           n = n+1
           data%State%TD_BOPElement(n)%ElementType  = data%State%TD_DrillStems(k)%ComponentType
           data%State%TD_BOPElement(n)%ElementStart = data%State%TD_DrillStems(k)%TopDepth
           data%State%TD_BOPElement(n)%ElementEnd   = data%State%TD_DrillStems(k)%DownDepth
       else if ( data%State%TD_DrillStems(k)%TopDepth<=data%State%TD_BOP%BOPHeight(5) .and. data%State%TD_DrillStems(k)%DownDepth>=data%State%TD_BOP%BOPHeight(4) ) then
           n = n+1
           data%State%TD_BOPElement(n)%ElementType  = data%State%TD_DrillStems(k)%ComponentType
           data%State%TD_BOPElement(n)%ElementStart = data%State%TD_DrillStems(k)%TopDepth
           data%State%TD_BOPElement(n)%ElementEnd   = data%State%TD_DrillStems(k)%DownDepth
       end if
   end do
   
   Call SetBopElements(data%State%TD_BOPElement)
  
   
   
   
end subroutine