subroutine ElementsCreation    !  is called in subroutine Fluid_Flow_Solver
    
        Use GeoElements_FluidModule
        USE CMudPropertiesVariables
        USE MudSystemVARIABLES
use SimulationVariables !@@@
        use SimulationVariables
        USE CHOKEVARIABLES
!use ConfigurationVariables !@
        !use CDataDisplayConsole
   !@ use ConfigurationVariables ,  StandPipePressureDataDisplay=>StandPipePressure
        !use CManifolds
        use SimulationVariables !@
        USE CHOKEVARIABLES
!use ConfigurationVariables !@
        !use CChokeManifoldVariables
    use SimulationVariables
        ! !use CTanks
    !@use ConfigurationVariables, TripTankVolume2 => data%EquipmentControl%DrillingWatch%TripTankVolume, TripTankDensity2 => TripTankDensity
        USE sROP_Other_Variables
        USE sROP_Variables
        use KickVARIABLESModule
        use CError
   
   implicit none
   
   integer jelement, jmud, jsection,ielement,i
   integer jopelement,jopmud,jopsection,iisection,isection,OpSection
   
!===========================================================WELL============================================================
!===========================================================WELL============================================================ 

        if(allocated(data%State%MudSystem%Xstart_PipeSection)) deallocate(data%State%MudSystem%Xstart_PipeSection)
        if(allocated(data%State%MudSystem%Xend_PipeSection)) deallocate(data%State%MudSystem%Xend_PipeSection)
        if(allocated(data%State%MudSystem%PipeSection_VolumeCapacity)) deallocate(data%State%MudSystem%PipeSection_VolumeCapacity)
        if(allocated(data%State%MudSystem%Area_PipeSectionFt)) deallocate(data%State%MudSystem%Area_PipeSectionFt)
        if(allocated(data%State%MudSystem%GeoType)) deallocate(data%State%MudSystem%GeoType)
        if(allocated(data%State%MudSystem%OD_PipeSectionInch)) deallocate(data%State%MudSystem%OD_PipeSectionInch)
        if(allocated(data%State%MudSystem%ID_PipeSectionInch)) deallocate(data%State%MudSystem%ID_PipeSectionInch)
        if(allocated(data%State%MudSystem%Angle_PipeSection)) deallocate(data%State%MudSystem%Angle_PipeSection)
        
        if(allocated(data%State%MudSystem%Xstart_OpSection)) deallocate(data%State%MudSystem%Xstart_OpSection)
        if(allocated(data%State%MudSystem%Xend_OpSection)) deallocate(data%State%MudSystem%Xend_OpSection)
        if(allocated(data%State%MudSystem%OpSection_VolumeCapacity)) deallocate(data%State%MudSystem%OpSection_VolumeCapacity)
        if(allocated(data%State%MudSystem%Area_OpSectionFt)) deallocate(data%State%MudSystem%Area_OpSectionFt)
        if(allocated(data%State%MudSystem%GeoTypeOp)) deallocate(data%State%MudSystem%GeoTypeOp)
        if(allocated(data%State%MudSystem%OD_OpSectionInch)) deallocate(data%State%MudSystem%OD_OpSectionInch)
        if(allocated(data%State%MudSystem%ID_OpSectionInch)) deallocate(data%State%MudSystem%ID_OpSectionInch)
        if(allocated(data%State%MudSystem%Angle_OpSection)) deallocate(data%State%MudSystem%Angle_OpSection)        

        

ALLOCATE (data%State%MudSystem%Xstart_PipeSection(data%State%F_Counts%StringIntervalCounts+data%State%F_Counts%AnnulusIntervalCounts),data%State%MudSystem%Xend_PipeSection(data%State%F_Counts%StringIntervalCounts+data%State%F_Counts%AnnulusIntervalCounts) &
    ,data%State%MudSystem%PipeSection_VolumeCapacity(data%State%F_Counts%StringIntervalCounts+data%State%F_Counts%AnnulusIntervalCounts),data%State%MudSystem%Area_PipeSectionFt(data%State%F_Counts%StringIntervalCounts+data%State%F_Counts%AnnulusIntervalCounts), &
    data%State%MudSystem%GeoType(data%State%F_Counts%StringIntervalCounts+data%State%F_Counts%AnnulusIntervalCounts),data%State%MudSystem%OD_PipeSectionInch(data%State%F_Counts%StringIntervalCounts+data%State%F_Counts%AnnulusIntervalCounts),data%State%MudSystem%ID_PipeSectionInch(data%State%F_Counts%StringIntervalCounts+data%State%F_Counts%AnnulusIntervalCounts))


ALLOCATE (data%State%MudSystem%Xstart_OpSection(data%State%F_Counts%BottomHoleIntervalCounts),data%State%MudSystem%Xend_OpSection(data%State%F_Counts%BottomHoleIntervalCounts) &
    ,data%State%MudSystem%OpSection_VolumeCapacity(data%State%F_Counts%BottomHoleIntervalCounts),data%State%MudSystem%Area_OpSectionFt(data%State%F_Counts%BottomHoleIntervalCounts), &
    data%State%MudSystem%GeoTypeOp(data%State%F_Counts%BottomHoleIntervalCounts),data%State%MudSystem%OD_OpSectionInch(data%State%F_Counts%BottomHoleIntervalCounts),data%State%MudSystem%ID_OpSectionInch(data%State%F_Counts%BottomHoleIntervalCounts))

        data%State%MudSystem%OpSection=0
        data%State%MudSystem%isection=0

  
        DO iisection=1, data%State%F_Counts%IntervalsTotalCounts
            IF (data%State%F_Interval(iisection)%GeoType  == 1) THEN
                data%State%MudSystem%OpSection= data%State%MudSystem%OpSection+1
                data%State%MudSystem%Xstart_OpSection(data%State%MudSystem%OpSection)= (data%State%F_Interval(iisection)%StartDepth)
                data%State%MudSystem%Xend_OpSection(data%State%MudSystem%OpSection)= (data%State%F_Interval(iisection)%EndDepth)
                data%State%MudSystem%Area_OpSectionFt(data%State%MudSystem%OpSection)= PII*((data%State%F_Interval(iisection)%OD/12.0d0)**2-(data%State%F_Interval(iisection)%ID/12.0d0)**2)/4.0d0   !D(in), AREA(ft^2)
                data%State%MudSystem%OD_OpSectionInch(data%State%MudSystem%OpSection)= (data%State%F_Interval(iisection)%OD)
                data%State%MudSystem%ID_OpSectionInch(data%State%MudSystem%OpSection)= (data%State%F_Interval(iisection)%ID)        !REAL(data%State%F_Interval(iisection)%Volume)   
                data%State%MudSystem%GeoTypeOp(data%State%MudSystem%OpSection)= data%State%F_Interval(iisection)%GeoType     ! niaz nist ehtemalan
                !Angle_OpSection(OpSection)= data%State%F_Interval(iisection)%Angle
                !write(*,*) 'iisection=' , iisection
                !write(*,*) 'StartDepth=' , data%State%F_Interval(iisection)%StartDepth
                !write(*,*) 'EndDepth=' , data%State%F_Interval(iisection)%EndDepth
                !write(*,*) 'OD=' , data%State%F_Interval(iisection)%OD
                !write(*,*) 'ID=' , data%State%F_Interval(iisection)%ID
            ELSE
                data%State%MudSystem%isection= data%State%MudSystem%isection+1
                data%State%MudSystem%Xstart_PipeSection(data%State%MudSystem%isection)= (data%State%F_Interval(iisection)%StartDepth)
                !write(*,*) 'data%State%F_Interval(iisection)%StartDepth=' , data%State%F_Interval(iisection)%StartDepth
                data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%isection)= (data%State%F_Interval(iisection)%EndDepth)
                !write(*,*) 'data%State%F_Interval(iisection)%EndDepth=' , data%State%F_Interval(iisection)%EndDepth
                
                data%State%MudSystem%OD_PipeSectionInch(data%State%MudSystem%isection)= (data%State%F_Interval(iisection)%OD) 
                data%State%MudSystem%Area_PipeSectionFt(data%State%MudSystem%isection)= PII*((data%State%F_Interval(iisection)%OD/12.0d0)**2-(data%State%F_Interval(iisection)%ID/12.0d0)**2)/4.0d0   !D(in), AREA(ft^2)
                data%State%MudSystem%ID_PipeSectionInch(data%State%MudSystem%isection)= (data%State%F_Interval(iisection)%ID)
                !PipeSection_VolumeCapacity(isection)= Area_PipeSectionFt(isection)* ABS(Xend_PipeSection(isection)-Xstart_PipeSection(isection))* 7.48051948          !REAL(data%State%F_Interval(iisection)%Volume)      ! (gal)
                data%State%MudSystem%GeoType(data%State%MudSystem%isection)= data%State%F_Interval(iisection)%GeoType
                !Angle_PipeSection(isection)= data%State%F_Interval(iisection)%Angle
            ENDIF
            
        ENDDO


        
        call data%State%MudSystem%Xstart_MudElement%Empty()   
        call data%State%MudSystem%Xstart_MudElement%Add(data%State%MudSystem%Xstart_PipeSection(1))
        
        
        call data%State%MudSystem%Xstart_OpMudElement%Empty()
        call data%State%MudSystem%Xstart_OpMudElement%Add(data%State%MudSystem%Xstart_OpSection(1))
        
        
        call data%State%MudSystem%TVDstart_MudElement%Empty()
        call TVD_Calculator(data%State%MudSystem%Xstart_PipeSection(1),data%State%MudSystem%MudCircVerticalDepth)
        call data%State%MudSystem%TVDstart_MudElement%Add(data%State%MudSystem%MudCircVerticalDepth)
        
        
        call data%State%MudSystem%TVDstart_OpMudElement%Empty()
        call TVD_Calculator(data%State%MudSystem%Xstart_OpSection(1),data%State%MudSystem%MudCircVerticalDepth)
        call data%State%MudSystem%TVDstart_OpMudElement%Add(data%State%MudSystem%MudCircVerticalDepth)        
        
        
        
        data%State%MudSystem%NoPipeSections= data%State%MudSystem%isection        ! sections in string and annulus(GeoType 0 & 2)

        
        DO OpSection= 1,data%State%F_Counts%BottomHoleIntervalCounts
            data%State%MudSystem%OpSection_VolumeCapacity(OpSection)= data%State%MudSystem%Area_OpSectionFt(OpSection)* ABS(data%State%MudSystem%Xend_OpSection(OpSection)-data%State%MudSystem%Xstart_OpSection(OpSection))* 7.48051948d0          !REAL(data%State%F_Interval(iisection)%Volume)
        ENDDO
        data%State%MudSystem%OpSection=OpSection        
        DO isection= 1,data%State%MudSystem%NoPipeSections
            data%State%MudSystem%PipeSection_VolumeCapacity(isection)= data%State%MudSystem%Area_PipeSectionFt(isection)* ABS(data%State%MudSystem%Xend_PipeSection(isection)-data%State%MudSystem%Xstart_PipeSection(isection))* 7.48051948d0          !REAL(data%State%F_Interval(iisection)%Volume)      ! (gal)
        ENDDO
        data%State%MudSystem%isection = isection
        
  
        
        !types:   Mud= 0  Kick=1

!=========================================== 
        if (data%State%MudSystem%FirstMudSet==0) then
            call data%State%MudSystem%Hz_MudDischarged_Volume%AddToFirst(data%State%MudSystem%PipeSection_VolumeCapacity(1))  !startup initial
            call data%State%MudSystem%Hz_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(1))
            call data%State%MudSystem%Hz_Mud_Backhead_section%AddToFirst (1)
            call data%State%MudSystem%Hz_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Xend_PipeSection(1))
            call data%State%MudSystem%Hz_Mud_Forehead_section%AddToFirst (1)
            call data%State%MudSystem%Hz_Density%AddToFirst (data%Configuration%Mud%ActiveDensity)       ! initial(ppg)
            call data%State%MudSystem%Hz_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
            call data%State%MudSystem%Hz_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)  
            call data%State%MudSystem%Hz_MudOrKick%AddToFirst (0)  
            
            
            call data%State%MudSystem%St_MudDischarged_Volume%AddToFirst(sum(data%State%MudSystem%PipeSection_VolumeCapacity(2:data%State%F_Counts%StringIntervalCounts)))  !startup initial
            call data%State%MudSystem%St_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(2))
            call data%State%MudSystem%St_Mud_Backhead_section%AddToFirst (2)
            call data%State%MudSystem%St_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Xend_PipeSection(data%State%F_Counts%StringIntervalCounts))
            call data%State%MudSystem%St_Mud_Forehead_section%AddToFirst (data%State%F_Counts%StringIntervalCounts)
            call data%State%MudSystem%St_Density%AddToFirst (data%Configuration%Mud%ActiveDensity)       ! initial(ppg)
            call data%State%MudSystem%St_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
            call data%State%MudSystem%St_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
            call data%State%MudSystem%St_MudOrKick%AddToFirst (0)  
            
                
            call data%State%MudSystem%Ann_MudDischarged_Volume%AddToFirst(sum(data%State%MudSystem%PipeSection_VolumeCapacity(data%State%F_Counts%StringIntervalCounts+1:data%State%MudSystem%NoPipeSections)))
            call data%State%MudSystem%Ann_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(data%State%F_Counts%StringIntervalCounts+1))
            call data%State%MudSystem%Ann_Mud_Backhead_section%AddToFirst (data%State%F_Counts%StringIntervalCounts+1)
            call data%State%MudSystem%Ann_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%NoPipeSections))
            call data%State%MudSystem%Ann_Mud_Forehead_section%AddToFirst (data%State%MudSystem%NoPipeSections)
            call data%State%MudSystem%Ann_Density%AddToFirst (data%Configuration%Mud%ActiveDensity)       ! initial(ppg)
            call data%State%MudSystem%Ann_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
            call data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
            call data%State%MudSystem%Ann_MudOrKick%AddToFirst (0) 
            call data%State%MudSystem%Ann_CuttingMud%AddToFirst (0) 
            
            data%State%MudSystem%OldPosition= data%State%MudSystem%Xend_PipeSection(data%State%F_Counts%StringIntervalCounts)  
            
            data%State%MudSystem%OldAnnulusCapacity= sum(data%State%MudSystem%PipeSection_VolumeCapacity(data%State%F_Counts%StringIntervalCounts+1:data%State%MudSystem%NoPipeSections))
                     
            
            call data%State%MudSystem%ChokeLine_MudDischarged_Volume%AddToFirst(data%State%MudSystem%ChokeLine_VolumeCapacity)
            call data%State%MudSystem%ChokeLine_Mud_Backhead_X%AddToFirst (0.0d0)
            call data%State%MudSystem%ChokeLine_Mud_Backhead_section%AddToFirst (1)
            call data%State%MudSystem%ChokeLine_Mud_Forehead_X%AddToFirst (data%Configuration%BopStack%ChokeLineLength)
            call data%State%MudSystem%ChokeLine_Mud_Forehead_section%AddToFirst (1)
            call data%State%MudSystem%ChokeLine_Density%AddToFirst (data%Configuration%Mud%ActiveDensity)       ! initial(ppg)
            call data%State%MudSystem%ChokeLine_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
            call data%State%MudSystem%ChokeLine_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
            call data%State%MudSystem%ChokeLine_MudOrKick%AddToFirst (0) 
            

            call data%State%MudSystem%Op_MudDischarged_Volume%AddToFirst (sum(data%State%MudSystem%OpSection_VolumeCapacity(1:data%State%F_Counts%BottomHoleIntervalCounts)))
            call data%State%MudSystem%Op_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Xstart_OpSection(1))
            call data%State%MudSystem%Op_Mud_Backhead_section%AddToFirst (1)
            call data%State%MudSystem%Op_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Xend_OpSection(data%State%F_Counts%BottomHoleIntervalCounts))
            call data%State%MudSystem%Op_Mud_Forehead_section%AddToFirst (data%State%F_Counts%BottomHoleIntervalCounts)
            call data%State%MudSystem%Op_Density%AddToFirst (data%Configuration%Mud%ActiveDensity)            
            call data%State%MudSystem%Op_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
            call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)  
            call data%State%MudSystem%Op_MudOrKick%AddToFirst (0)  
            
            !F_StringIntervalCountsOld= F_StringIntervalCounts   ! is used for adding new pipe to string
            data%State%MudSystem%F_StringIntervalCounts_Old= data%State%F_Counts%StringIntervalCounts   ! is used for adding new pipe to string
            
                
            data%State%MudSystem%FirstMudSet= 1     
        endif

        
!===================== Trip Detection ================  
        
        !DeltaVolumeOp > 0 : Trip in
        !DeltaVolumeOp < 0 : Trip out


        data%State%MudSystem%DeltaVolumeOp= ((data%State%MudSystem%Xend_PipeSection(data%State%F_Counts%StringIntervalCounts)-data%State%MudSystem%OldPosition)*PII*((data%State%MudSystem%OD_PipeSectionInch(data%State%F_Counts%StringIntervalCounts+1)/12.0d0)**2)/4.0d0)* 7.48051948d0! ft^3  to gal    !   D(in)
        data%State%MudSystem%DeltaVolumeOp = INT(data%State%MudSystem%DeltaVolumeOp * 100000.d0) / 100000.d0
        
        data%State%MudSystem%DeltaVolumePipe= ((data%State%MudSystem%Xend_PipeSection(data%State%F_Counts%StringIntervalCounts)-data%State%MudSystem%OldPosition)*PII*((data%State%MudSystem%ID_PipeSectionInch(data%State%F_Counts%StringIntervalCounts+data%State%F_Counts%AnnulusIntervalCounts)/12.0d0)**2)/4.0d0)* 7.48051948d0! ft^3  to gal
        data%State%MudSystem%DeltaVolumePipe = INT(data%State%MudSystem%DeltaVolumePipe * 100000.d0) / 100000.d0
        
        !DeltaVolumeAnnulusCapacity= ((Xend_PipeSection(data%State%F_Counts%StringIntervalCounts)-OldPosition))*Area_PipeSectionFt(NoPipeSections)* 7.48051948d0! ft^3  to gal
        KickVARIABLES%DrillStringSpeed = (data%State%MudSystem%Xend_PipeSection(data%State%F_Counts%StringIntervalCounts)-data%State%MudSystem%OldPosition) / 0.1
        
        data%State%MudSystem%DeltaVolumeAnnulusCapacity= sum(data%State%MudSystem%PipeSection_VolumeCapacity(data%State%F_Counts%StringIntervalCounts+1:data%State%MudSystem%NoPipeSections)) - data%State%MudSystem%OldAnnulusCapacity
        
        
        !write(*,*) 'DeltaVolumeAnnulusCapacity= ' , DeltaVolumeAnnulusCapacity
        
        !write(*,*) 'DeltaVolumePipe=' , DeltaVolumePipe
        !write(*,*) 'DeltaVolumeOp=' , DeltaVolumeOp
        !
        !
        !write(*,*) 'Bit here=' , Xend_PipeSection(data%State%F_Counts%StringIntervalCounts)
        
        
        
         
        data%State%MudSystem%OldAnnulusCapacity= sum(data%State%MudSystem%PipeSection_VolumeCapacity(data%State%F_Counts%StringIntervalCounts+1:data%State%MudSystem%NoPipeSections))

        data%State%MudSystem%OldPosition= data%State%MudSystem%Xend_PipeSection(data%State%F_Counts%StringIntervalCounts)
                
        ! Needed for trip in or out:
        if (data%State%MudSystem%Hz_Mud_Backhead_X%Length() == 0) then
            
                    CALL ErrorStop('Hz_Mud_Backhead_X Length is 0')
       endif
        
        
        data%State%MudSystem%Hz_Mud_Backhead_X%Array(1)= data%State%MudSystem%Xstart_PipeSection(1)
        data%State%MudSystem%Hz_Mud_Backhead_section%Array(1)= 1
        
        data%State%MudSystem%AddedElementsToString = data%State%F_Counts%StringIntervalCounts - data%State%MudSystem%F_StringIntervalCounts_Old
        data%State%MudSystem%St_Mud_Backhead_X%Array(1)= data%State%MudSystem%Xstart_PipeSection(2)
        data%State%MudSystem%St_Mud_Backhead_section%Array(1)= 2
        data%State%MudSystem%Ann_Mud_Backhead_X%Array(1)= data%State%MudSystem%Xstart_PipeSection(data%State%F_Counts%StringIntervalCounts+1)
        data%State%MudSystem%Ann_Mud_Backhead_section%Array(1)= data%State%F_Counts%StringIntervalCounts+1
        data%State%MudSystem%Op_Mud_Backhead_X%Array(1)= data%State%MudSystem%Xstart_OpSection(1)
        data%State%MudSystem%Op_Mud_Backhead_section%Array(1)= 1
        data%State%MudSystem%ChokeLine_Mud_Backhead_X%Array(1)= 0.
        data%State%MudSystem%ChokeLine_Mud_Backhead_section%Array(1)= 1
           
        
        data%State%MudSystem%F_StringIntervalCounts_Old= data%State%F_Counts%StringIntervalCounts
!write(*,*) 'Xstart_PipeSection(2)' , Xstart_PipeSection(2)
!write(*,*) 'Xend_PipeSection(1)' , Xend_PipeSection(1)

        
!===================================================
        
                        !
                        !DeltaWellCap= sum(PipeSection_VolumeCapacity(F_StringIntervalCounts+1:NoPipeSections)) + sum(OpSection_VolumeCapacity(1:F_BottomHoleIntervalCounts)) - WellCapOld
                        !WellCapOld= sum(PipeSection_VolumeCapacity(F_StringIntervalCounts+1:NoPipeSections)) + sum(OpSection_VolumeCapacity(1:F_BottomHoleIntervalCounts))
                        !write(*,*) 'DeltaWellCap=' , DeltaWellCap
                        !
                        !
                        !
                        !DeltaAnnCap= sum(PipeSection_VolumeCapacity(F_StringIntervalCounts+1:NoPipeSections))  - AnnCapOld
                        !AnnCapOld= sum(PipeSection_VolumeCapacity(F_StringIntervalCounts+1:NoPipeSections))
                        !write(*,*) 'DeltaAnnCap=' , DeltaAnnCap
  
    end subroutine ElementsCreation
    
    
    
    
