subroutine TD_AddComponents
    
    Use CStringConfigurationVariables
    use UnitySignalVariables
    use UnitySignalsModule
    ! Use CStringUpdateVariables
    ! Use CSafetyValveEnumVariables
    ! use UnitySignalsModule
    ! use UnitySignalsModuleVariables
    ! use OperationScenariosModule
    use OperationScenariosModule!, only: Get_ElevatorConnection
    ! use OperationScenariosModule !!CElevator...
    use CHoistingVariables
    use SimulationVariables
    ! use UnitySignalsModule
    ! Use CTdsElevatorModesEnumVariables
    Use TD_DrillStemComponents
    use SimulationVariables !@
    use SimulationVariables !@
    Use TD_StringConnectionData
    
   implicit none
   
   integer :: i , kk , TD_NumOfAddedComponents
   
   
   
   
   
   
   
!====================================================
!                     Add Single
!====================================================
   
   if ( Get_StringUpdate() == STRING_UPDATE_ADD_SINGLE ) then
       kk = 0
       Do i= data%State%TD_String%DrillStemComponentsNumbs , 1 , -1
           if (data%State%TD_DrillStems(i)%ComponentType==3) then
               kk = i
               exit
           end if
       End Do
       ! DrillStems Array:
       TD_NumOfAddedComponents = 1
       Do i= (data%State%TD_String%DrillStemComponentsNumbs+1) , (data%State%TD_String%DrillStemComponentsNumbs+TD_NumOfAddedComponents)
         data%State%TD_DrillStems(i)%ComponentType   = 3
         data%State%TD_DrillStems(i)%Id              = data%State%TD_DrillStems(kk)%Id
         data%State%TD_DrillStems(i)%Od              = data%State%TD_DrillStems(kk)%Od
         data%State%TD_DrillStems(i)%Area            = (pi*((data%State%TD_DrillStems(i)%Od**2)-(data%State%TD_DrillStems(i)%Id**2)))/4.d0
         data%State%TD_DrillStems(i)%RtoolJoint      = data%State%TD_DrillStems(i)%Od*1.3d0/2.d0
         data%State%TD_DrillStems(i)%ToolJointRange  = data%State%TD_String%ToolJointRange
         data%State%TD_DrillStems(i)%Length          = data%State%TD_DrillStems(kk)%LengthIni
         data%State%TD_DrillStems(i)%LengthIni       = data%State%TD_DrillStems(kk)%LengthIni
         data%State%TD_DrillStems(i)%WeightperLength = data%State%TD_DrillStems(kk)%WeightperLength
         data%State%TD_DrillStems(i)%Weight          = data%State%TD_DrillStems(i)%WeightperLength*data%State%TD_DrillStems(i)%Length
         data%State%TD_DrillStems(i)%Density         = data%State%TD_DrillStems(kk)%Density
         data%State%TD_DrillStems(i)%ElasticModule   = data%State%TD_DrillStems(kk)%ElasticModule
       End Do
       data%State%TD_String%DrillStemComponentsNumbs = data%State%TD_String%DrillStemComponentsNumbs+TD_NumOfAddedComponents
       ! DrillStem Array:
       i = data%State%TD_String%StringConfigurationCount+1
       data%State%TD_DrillStem(i)%ComponentType   = 3
       data%State%TD_DrillStem(i)%Numbs           = 1
       data%State%TD_DrillStem(i)%Id              = data%State%TD_DrillStems(kk)%Id   ! [ft]
       data%State%TD_DrillStem(i)%Od              = data%State%TD_DrillStems(kk)%Od   ! [ft]
       data%State%TD_DrillStem(i)%Length          = data%State%TD_DrillStems(kk)%LengthIni
       data%State%TD_DrillStem(i)%WeightperLength = data%State%TD_DrillStems(kk)%WeightperLength
       data%State%TD_DrillStem(i)%TotalLength     = data%State%TD_DrillStem(i)%Numbs*data%State%TD_DrillStem(i)%Length
       data%State%TD_DrillStem(i)%TotalWeight     = data%State%TD_DrillStem(i)%TotalLength*data%State%TD_DrillStem(i)%WeightperLength
       data%State%TD_String%StringConfigurationCount     = data%State%TD_String%StringConfigurationCount+1
       
       
       Call Set_StringUpdate(STRING_UPDATE_NEUTRAL)
   end if
   
   
    
    
    
!====================================================
!                     Add Stand
!====================================================
   
   if ( Get_StringUpdate() == STRING_UPDATE_ADD_STAND ) then
       kk = 0
       Do i= data%State%TD_String%DrillStemComponentsNumbs , 1 , -1
           if (data%State%TD_DrillStems(i)%ComponentType==3) then
               kk = i
               exit
           end if
       End Do
       ! DrillStems Array:
       TD_NumOfAddedComponents = 3
       Do i= (data%State%TD_String%DrillStemComponentsNumbs+1) , (data%State%TD_String%DrillStemComponentsNumbs+TD_NumOfAddedComponents)
         data%State%TD_DrillStems(i)%ComponentType   = 3
         data%State%TD_DrillStems(i)%Id              = data%State%TD_DrillStems(kk)%Id
         data%State%TD_DrillStems(i)%Od              = data%State%TD_DrillStems(kk)%Od
         data%State%TD_DrillStems(i)%Area            = (pi*((data%State%TD_DrillStems(i)%Od**2)-(data%State%TD_DrillStems(i)%Id**2)))/4.0d0
         data%State%TD_DrillStems(i)%RtoolJoint      = data%State%TD_DrillStems(i)%Od*1.30d0/2.0d0
         data%State%TD_DrillStems(i)%ToolJointRange  = data%State%TD_String%ToolJointRange
         data%State%TD_DrillStems(i)%Length          = data%State%TD_DrillStems(kk)%LengthIni
         data%State%TD_DrillStems(i)%LengthIni       = data%State%TD_DrillStems(kk)%LengthIni
         data%State%TD_DrillStems(i)%WeightperLength = data%State%TD_DrillStems(kk)%WeightperLength
         data%State%TD_DrillStems(i)%Weight          = data%State%TD_DrillStems(i)%WeightperLength*data%State%TD_DrillStems(i)%Length
         data%State%TD_DrillStems(i)%Density         = data%State%TD_DrillStems(kk)%Density 
         data%State%TD_DrillStems(i)%ElasticModule   = data%State%TD_DrillStems(kk)%ElasticModule  
       End Do
       data%State%TD_String%DrillStemComponentsNumbs = data%State%TD_String%DrillStemComponentsNumbs+TD_NumOfAddedComponents
       
       ! DrillStem Array:
       i = data%State%TD_String%StringConfigurationCount+1
       data%State%TD_DrillStem(i)%ComponentType   = 3
       data%State%TD_DrillStem(i)%Numbs           = 3
       data%State%TD_DrillStem(i)%Id              = data%State%TD_DrillStems(kk)%Id   ! [ft]
       data%State%TD_DrillStem(i)%Od              = data%State%TD_DrillStems(kk)%Od   ! [ft]
       data%State%TD_DrillStem(i)%Length          = data%State%TD_DrillStems(kk)%LengthIni
       data%State%TD_DrillStem(i)%WeightperLength = data%State%TD_DrillStems(kk)%WeightperLength
       data%State%TD_DrillStem(i)%TotalLength     = data%State%TD_DrillStem(i)%Numbs*data%State%TD_DrillStem(i)%Length
       data%State%TD_DrillStem(i)%TotalWeight     = data%State%TD_DrillStem(i)%TotalLength*data%State%TD_DrillStem(i)%WeightperLength
       data%State%TD_String%StringConfigurationCount     = data%State%TD_String%StringConfigurationCount+1
       
       
       Call Set_StringUpdate(STRING_UPDATE_NEUTRAL)
   end if
   
   
    
    
    
    
!====================================================
!                     Add IBOP
!====================================================
   
   !if ( Get_Ibop()==IBOP_INSTALL ) then
   !    data%State%TD_Count%IBOPNewAdd = 1
   !else
   !    data%State%TD_Count%IBOPNewAdd = 0
   !    data%State%TD_Count%IBOPOldAdd = 0
   ! end if
    
   if ( Get_Ibop()==IBOP_INSTALL .and. data%State%TD_Count%IBOPOldAdd==0 ) then
       kk = 0
       Do i= data%State%TD_String%DrillStemComponentsNumbs , 1 , -1
           if (data%State%TD_DrillStems(i)%ComponentType==3) then
               kk = i
               exit
           end if
       End Do
       ! DrillStems Array:
       TD_NumOfAddedComponents = 1
       Do i= (data%State%TD_String%DrillStemComponentsNumbs+1) , (data%State%TD_String%DrillStemComponentsNumbs+TD_NumOfAddedComponents)
         data%State%TD_DrillStems(i)%ComponentType   = 5
         data%State%TD_DrillStems(i)%Id              = data%State%TD_DrillStems(kk)%Id
         data%State%TD_DrillStems(i)%Od              = data%State%TD_DrillStems(kk)%RtoolJoint
         data%State%TD_DrillStems(i)%Area            = (pi*((data%State%TD_DrillStems(i)%Od**2)-(data%State%TD_DrillStems(i)%Id**2)))/4.0d0
         data%State%TD_DrillStems(i)%RtoolJoint      = data%State%TD_DrillStems(kk)%RtoolJoint
         data%State%TD_DrillStems(i)%ToolJointRange  = 0.0d0
         data%State%TD_DrillStems(i)%Length          = data%State%TD_StConn%IBOPLength               ! [ft]
         data%State%TD_DrillStems(i)%LengthIni       = data%State%TD_StConn%IBOPLength               ! [ft]
         data%State%TD_DrillStems(i)%WeightperLength = 55.0d0                      ! [lb/ft]  
         data%State%TD_DrillStems(i)%Weight          = data%State%TD_DrillStems(i)%WeightperLength*data%State%TD_DrillStems(i)%Length
         data%State%TD_DrillStems(i)%Density         = data%State%TD_DrillStems(kk)%Density         !????????????
         data%State%TD_DrillStems(i)%ElasticModule   = data%State%TD_DrillStems(kk)%ElasticModule   !????????????
       End Do
       data%State%TD_String%DrillStemComponentsNumbs = data%State%TD_String%DrillStemComponentsNumbs+TD_NumOfAddedComponents
       
       ! DrillStem Array:
       i = data%State%TD_String%StringConfigurationCount+1
       data%State%TD_DrillStem(i)%ComponentType   = 5     
       data%State%TD_DrillStem(i)%Numbs           = 1
       data%State%TD_DrillStem(i)%Id              = data%State%TD_DrillStems(kk)%Id           ! [ft]
       data%State%TD_DrillStem(i)%Od              = data%State%TD_DrillStems(kk)%RtoolJoint   ! [ft]
       data%State%TD_DrillStem(i)%Length          = 1.540d0                        ! [ft]
       data%State%TD_DrillStem(i)%WeightperLength = 55.0d0                         ! [lb/ft]  
       data%State%TD_DrillStem(i)%TotalLength     = data%State%TD_DrillStem(i)%Numbs*data%State%TD_DrillStem(i)%Length
       data%State%TD_DrillStem(i)%TotalWeight     = data%State%TD_DrillStem(i)%TotalLength*data%State%TD_DrillStem(i)%WeightperLength
       data%State%TD_String%StringConfigurationCount     = data%State%TD_String%StringConfigurationCount+1
       
       
       !data%State%TD_Count%IBOPOldAdd = data%State%TD_Count%IBOPNewAdd
       
       if ( Get_ElevatorConnection() /= ELEVATOR_CONNECTION_STRING .or. (Get_TdsConnectionModes()/=TDS_CONNECTION_NOTHING .and. Get_TdsElevatorModes()/=TDS_ELEVATOR_CONNECTION_STRING) ) then
           data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight+data%State%TD_DrillStem(i)%Length
       end if
       
   end if
   
   if ( Get_Ibop()==IBOP_INSTALL ) then
       data%State%TD_Count%IBOPOldAdd = 1
   else
       data%State%TD_Count%IBOPOldAdd = 0
   end if
   
   
    
    
!====================================================
!       Add Safety Valve & Kelly (OPERATION_DRILL)
!====================================================
   
   if ( data%Configuration%Hoisting%DriveType==1 .and. Get_OperationCondition()==OPERATION_DRILL .and. Get_KellyConnection() == KELLY_CONNECTION_STRING ) then
       data%State%TD_Count%KellyNewAdd = 1
   else
       data%State%TD_Count%KellyNewAdd = 0
       data%State%TD_Count%KellyOldAdd = 0
   end if
   
   if ( data%Configuration%Hoisting%DriveType==1 .and. Get_OperationCondition()==OPERATION_DRILL .and. Get_KellyConnection() == KELLY_CONNECTION_STRING .and. data%State%TD_Count%KellyNewAdd/=data%State%TD_Count%KellyOldAdd ) then
       ! DrillStems Array:
       TD_NumOfAddedComponents = 1
       Do i= (data%State%TD_String%DrillStemComponentsNumbs+1) , (data%State%TD_String%DrillStemComponentsNumbs+TD_NumOfAddedComponents)
         data%State%TD_DrillStems(i)%ComponentType   = 6
         data%State%TD_DrillStems(i)%Id              = data%State%TD_StConn%KellyElementID      ! [ft]
         data%State%TD_DrillStems(i)%Od              = data%State%TD_StConn%KellyElementOD      ! [ft]
         data%State%TD_DrillStems(i)%Area            = (pi*((data%State%TD_DrillStems(i)%Od**2)-(data%State%TD_DrillStems(i)%Id**2)))/4.0d0
         data%State%TD_DrillStems(i)%RtoolJoint      = data%State%TD_DrillStems(i)%Od*1.30d0/2.0d0
         data%State%TD_DrillStems(i)%ToolJointRange  = 0.0d0
         data%State%TD_DrillStems(i)%Length          = data%State%TD_StConn%KellyElementConst   ! [ft]
         data%State%TD_DrillStems(i)%LengthIni       = data%State%TD_StConn%KellyElementConst   ! [ft]
         data%State%TD_DrillStems(i)%WeightperLength = 55.0d0                 ! [lb/ft]  
         data%State%TD_DrillStems(i)%Weight          = data%State%TD_DrillStems(i)%WeightperLength*data%State%TD_DrillStems(i)%Length
         data%State%TD_DrillStems(i)%Density         = 7850.d0*0.06242796d0   ! [kg/m3]*0.06242796=[lb/ft3]
         data%State%TD_DrillStems(i)%ElasticModule   = 200.0d9*0.02088543d0   ! [lb/ft2]        !200GPa=29Mpsi  (steel)
       End Do
       data%State%TD_String%DrillStemComponentsNumbs = data%State%TD_String%DrillStemComponentsNumbs+TD_NumOfAddedComponents
       
       ! DrillStem Array:
       i = data%State%TD_String%StringConfigurationCount+1
       data%State%TD_DrillStem(i)%ComponentType   = 6     
       data%State%TD_DrillStem(i)%Numbs           = 1
       data%State%TD_DrillStem(i)%Id              = 3.0d0/12.d0              ! [ft]
       data%State%TD_DrillStem(i)%Od              = 5.90d0/12.d0             ! [ft]
       data%State%TD_DrillStem(i)%Length          = data%State%TD_StConn%KellyElementConst     ! [ft]
       data%State%TD_DrillStem(i)%WeightperLength = 55.0d0                   ! [lb/ft]
       data%State%TD_DrillStem(i)%TotalLength     = data%State%TD_DrillStem(i)%Numbs*data%State%TD_DrillStem(i)%Length
       data%State%TD_DrillStem(i)%TotalWeight     = data%State%TD_DrillStem(i)%TotalLength*data%State%TD_DrillStem(i)%WeightperLength
       data%State%TD_String%StringConfigurationCount     = data%State%TD_String%StringConfigurationCount+1
       
       
       data%State%TD_Count%KellyOldAdd = data%State%TD_Count%KellyNewAdd
       
   end if
   
   
   
   
   
!====================================================
!          Add Safety Valve (OPERATION_TRIP)
!====================================================
   
   !if ( Get_OperationCondition()==OPERATION_TRIP .and. Get_SafetyValve()==SAFETY_VALVE_INSTALL ) then
   !    data%State%TD_Count%SafetyValveNewAdd = 1
   !else
   !    data%State%TD_Count%SafetyValveNewAdd = 0
   !    data%State%TD_Count%SafetyValveOldAdd = 0
   !end if
   
   if ( data%State%TD_StConn%KellyDriveTypeMode/=0 .and. data%State%TD_StConn%OldOperationCondition/=0 .and. Get_SafetyValve()==SAFETY_VALVE_INSTALL .and. data%State%TD_Count%SafetyValveOldAdd==0 ) then
       kk = 0
       Do i= data%State%TD_String%DrillStemComponentsNumbs , 1 , -1
           if (data%State%TD_DrillStems(i)%ComponentType==3) then
               kk = i
               exit
           end if
       End Do
       ! DrillStems Array:
       TD_NumOfAddedComponents = 1
       Do i= (data%State%TD_String%DrillStemComponentsNumbs+1) , (data%State%TD_String%DrillStemComponentsNumbs+TD_NumOfAddedComponents)
         data%State%TD_DrillStems(i)%ComponentType   = 7
         data%State%TD_DrillStems(i)%Id              = data%State%TD_DrillStems(kk)%Id
         data%State%TD_DrillStems(i)%Od              = data%State%TD_DrillStems(kk)%RtoolJoint
         data%State%TD_DrillStems(i)%Area            = (pi*((data%State%TD_DrillStems(i)%Od**2)-(data%State%TD_DrillStems(i)%Id**2)))/4.0d0
         data%State%TD_DrillStems(i)%RtoolJoint      = data%State%TD_DrillStems(kk)%RtoolJoint
         data%State%TD_DrillStems(i)%ToolJointRange  = 0.0d0
         data%State%TD_DrillStems(i)%Length          = data%State%TD_StConn%SafetyValveLength        ! [ft]
         data%State%TD_DrillStems(i)%LengthIni       = data%State%TD_StConn%SafetyValveLength        ! [ft]
         data%State%TD_DrillStems(i)%WeightperLength = 55.0d0                      ! [lb/ft]
         data%State%TD_DrillStems(i)%Weight          = data%State%TD_DrillStems(i)%WeightperLength*data%State%TD_DrillStems(i)%Length
         data%State%TD_DrillStems(i)%Density         = data%State%TD_DrillStems(kk)%Density         !????????????
         data%State%TD_DrillStems(i)%ElasticModule   = data%State%TD_DrillStems(kk)%ElasticModule   !????????????
       End Do
       data%State%TD_String%DrillStemComponentsNumbs = data%State%TD_String%DrillStemComponentsNumbs+TD_NumOfAddedComponents
       
       ! DrillStem Array:
       i = data%State%TD_String%StringConfigurationCount+1
       data%State%TD_DrillStem(i)%ComponentType   = 7
       data%State%TD_DrillStem(i)%Numbs           = 1
       data%State%TD_DrillStem(i)%Id              = data%State%TD_DrillStems(kk)%Id           ! [ft]
       data%State%TD_DrillStem(i)%Od              = data%State%TD_DrillStems(kk)%RtoolJoint   ! [ft]
       data%State%TD_DrillStem(i)%Length          = 1.540d0                        ! [ft]
       data%State%TD_DrillStem(i)%WeightperLength = 55.0d0                         ! [lb/ft]  
       data%State%TD_DrillStem(i)%TotalLength     = data%State%TD_DrillStem(i)%Numbs*data%State%TD_DrillStem(i)%Length
       data%State%TD_DrillStem(i)%TotalWeight     = data%State%TD_DrillStem(i)%TotalLength*data%State%TD_DrillStem(i)%WeightperLength
       data%State%TD_String%StringConfigurationCount     = data%State%TD_String%StringConfigurationCount+1
       
       
       !data%State%TD_Count%SafetyValveOldAdd = data%State%TD_Count%SafetyValveNewAdd
       
       if ( Get_ElevatorConnection() /= ELEVATOR_CONNECTION_STRING .or. (Get_TdsConnectionModes()/=TDS_CONNECTION_NOTHING .and. Get_TdsElevatorModes()/=TDS_ELEVATOR_CONNECTION_STRING) ) then
           data%State%TD_StConn%ConnectionHeight = data%State%TD_StConn%ConnectionHeight+data%State%TD_DrillStem(i)%Length
       end if
       
   end if
   
   if ( Get_SafetyValve()==SAFETY_VALVE_INSTALL ) then
       data%State%TD_Count%SafetyValveOldAdd = 1
   else
       data%State%TD_Count%SafetyValveOldAdd = 0
   end if
   
   

end subroutine