    SUBROUTINE NormalCirculation_StartUp()     ! is called in module FluidFlowMain
    
    USE MudSystemVARIABLES
use SimulationVariables !@@@
    use CTanks
    use SimulationVariables
    USE CMudPropertiesVariables
    Use GeoElements_FluidModule
    use KickVARIABLESModule
    Use CUnityOutputs
    Use CShoeVariables
    use SimulationVariables
    
    implicit none
    
    ! temporary varibales for solving pressure jerks -- 1399-11-09 
    !Pump1BlownInTimeStep = 0
    !Pump2BlownInTimeStep = 0
    !Pump3BlownInTimeStep = 0
    
    !Pump1BlownStarted = .FALSE.
    !Pump2BlownStarted = .FALSE.
    !Pump3BlownStarted = .FALSE.
    
    data%State%MudSystem%Pump1BlownCount = 0
    data%State%MudSystem%Pump2BlownCount = 0
    data%State%MudSystem%Pump3BlownCount = 0
    
    
data%State%MudSystem%DeltaWellCap=0.
data%State%MudSystem%WellCapOld =  0. 
data%State%MudSystem%AnnCapOld=0.
data%State%MudSystem%DeltaAnnCap=0.
    
    
data%State%MPumps%Total_Stroke_Counter_For_Plot = 0.0

        data%State%MudSystem%DeltaT_Mudline=0.1 !second
    
                     Call Set_FlowKellyDisconnect(.false.)
                     Call Set_FlowPipeDisconnect(.false.)
                     
!HZ_ADD= 0.d0
data%State%MudSystem%Flow_timeCounter= 0
data%State%MudSystem%MudSys_timeCounter= 0
data%State%MudSystem%FluidFlowCounter = 0
!========================================================================
!                       MUD CIRCULATION STARTUP
!========================================================================
                     
         data%State%MudSystem%FormationLostPressure= Shoe%LeakOff * Shoe%ShoeDepth  
         data%State%MudSystem%ShoeFractured= .false.
         
        data%State%MudSystem%UGBOSuccessionCounter = 0   ! also in starup
        data%State%MudSystem%UGBOSuccessionCounterOld = 0   ! also in starup
         
                     
                     
        data%State%MudSystem%ChokeLineFlowRate= 0.0
        data%State%MudSystem%StringFlowRate= 0.0
        data%State%MudSystem%AnnulusFlowRate= 0.0
        
        data%State%MudSystem%MudVolume_InjectedFromAnn= 0.D0
        data%State%MudSystem%MudVolume_InjectedToBH= 0.D0
                     
        data%State%MudSystem%DensityMixTol= 0.1     !(ppg)
        data%State%MudSystem%CuttingDensityMixTol= 0.5
        data%State%MudSystem%NewPipeFilling= 1
        data%State%MudSystem%UtubeFilling= 1
        data%State%MudSystem%UtubeEmptyVolume= 0.0
        
        data%State%MudSystem%UtubeMode1Activated= .false.
        data%State%MudSystem%UtubeMode2Activated= .false.
        data%State%MudSystem%UtubePossibility= .false.
        
        
        !KickMigration_2SideBit = .FALSE.
        
        data%State%MudSystem%KickDx= (Reservoir%AutoMigrationRate/3600.)*data%State%MudSystem%DeltaT_Mudline                !AutoMigrationRate (ft/h)= ft per DeltaT_Mudline
        

        data%State%MudSystem%NewInfluxElementCreated= 0
        data%State%MudSystem%NewInfluxNumber= 0
        
        !KickVolumeinAnnulus= 0.0
        data%State%MudSystem%KickDeltaVinAnnulus= 0.0
        KickVARIABLES%GasKickPumpFlowRate= 0.0
        
        data%State%MudSystem%FirstMudSet= 0
        data%State%MudSystem%FirstSetUtube1=0
        data%State%MudSystem%FirstSetUtube2=0
        data%State%MudSystem%SuctionMud=1
        data%State%MudSystem%ImudCount= 1
        imud=1
        data%State%MudSystem%iLoc= 1 ! for Kick

        data%State%MudSystem%Suction_Density_MudSystem= data%Configuration%Mud%ActiveDensity
        data%State%MudSystem%SuctionDensity_Old= data%Configuration%Mud%ActiveDensity       ! initial(ppg)        
        data%State%MudSystem%StringDensity_Old= data%Configuration%Mud%ActiveDensity       ! initial(ppg)
        data%State%MudSystem%AnnulusSuctionDensity_Old= data%Configuration%Mud%ActiveDensity       ! initial(ppg)
        data%State%MudSystem%ChokeLineDensity_Old= data%Configuration%Mud%ActiveDensity       ! initial(ppg)
        
        data%State%MudSystem%TotalAddedVolume= 0.


        data%State%MudSystem%xx=0.


    
    END SUBROUTINE NormalCirculation_StartUp
    
    
   
    
    
    
    
    SUBROUTINE MudSystem_StartUp()
    USE CMudPropertiesVariables
    USE MudSystemVARIABLES
use SimulationVariables !@@@
    use CDataDisplayConsole
    use SimulationVariables
    USE CHOKEVARIABLES
use SimulationVariables !@
    use SimulationVariables
    USE CBopStackVariables
    use CPumpsVariables
    use CPumps
    use CTanks
    use SimulationVariables
    use KickVARIABLESModule
    implicit none 
   
    
    
    
    
    CALL MUDLINE_LOSS_INPUTS()

!data%State%MPumps%Total_Pump_GPM=10. ! Initial Value
    
    data%State%MUD%Q=0. ! Commented by mahmood
    
    data%State%MudSystem%Q_flow32=0.
    data%State%MudSystem%Q_flow33=0.
    data%State%MudSystem%Q_flow34=0.
    data%State%MudSystem%Q_flow35=0.
    
    data%State%MudSystem%DeltaT_Mudline=0.1 !second
    
    KickVARIABLES%GasKickPumpFlowRate= 0.
    data%State%MudSystem%BellNippleVolume= 0.
    data%State%MudSystem%BellNippleDensity= 0.
    data%State%MudSystem%MudBucketVolume= 0.
    data%State%MudSystem%MudBucketDensity= 0.
    data%State%MudSystem%BellNippleDumpVolume= 0.
    !BellNippleDumpRate= 0.
    !BellNippleToPitsRate= 0.0
    data%State%MudSystem%MudChecked= .true.
    
    data%State%MudSystem%condition32Final= .TRUE.
    data%State%MudSystem%condition33Final= .TRUE.
    data%State%MudSystem%condition34Final= .TRUE.
    
 
data%State%MudSystem%PressureGauge75= 0.0
data%State%MudSystem%PressureGauge76 = 0.0

    
!!====================================================================== 
!!                           TRIP TANK
!!======================================================================
        
    data%State%MudSystem%TripTank_MinVol_Allowded= 50.*42.       !(bbl to gal, initial value)
    data%State%MudSystem%TripTank_MaxVol_Allowded= 50. *42.      !(bbl to gal, initial value)
    
    
data%State%MudSystem%ActiveTankFloorArea= (data%Configuration%Mud%ActiveTotalTankCapacityGal) / (7.48051948*100./12.)     ! (ft^2)  -     Tank Height= 100 inch , 12=inch to ft       7.48051948=gal to ft^3
data%State%MudSystem%TripTankFloorArea= (50.*42.) / (7.48051948*100./12.)     ! (ft^2)   -     50.*42.=Trip Tank Capacity in BBl*42= Gal ,   Tank Height= 100 inch , 12=inch to ft       7.48051948=gal to ft^3



data%State%MudSystem%TripTank_Vol= data%Configuration%Mud%InitialTripTankMudVolumeGal       !(gal)
data%State%MudSystem%TripTank_Dens= 1.
data%EquipmentControl%DataDisplayConsole%TripTankGauge=0.



data%State%MudSystem%ReturnToTrip_Q= 1.
data%State%MudSystem%ActiveToTrip_Q= 1.


data%State%MudSystem%TripTankPump_Q= .8


data%State%MudSystem%ReturnToTrip_Dens=1.0     ! ppg(lbm/gal)
data%State%MudSystem%ActiveToTrip_Dens=1.0

!!====================================================================== 
!!                           MUD VOLUME TOTALIZER
!!======================================================================

data%State%MudSystem%Mp1Density= 0.0 !(VALVE82)
data%State%MudSystem%Mp2Density= 0.0 !(VALVE83)
data%State%MudSystem%Mp3Density= 0.0 !(VALVE84)


data%State%MudSystem%ReserveTankVolume= data%Configuration%Mud%ReserveMudVolumeGal   ! initial volume  (gal)
data%State%MudSystem%ReserveTankDensity= data%Configuration%Mud%ReserveDensity   ! initial



data%State%MudSystem%CementTankVolumeCalc= data%EquipmentControl%Tank%CementTankVolume          !movaghat--- initial volume  (gal)
data%State%MudSystem%CementTankDensityCalc= data%EquipmentControl%Tank%CementTankDensity       !movaghat--- initial

data%State%MudSystem%PumpsDumpVolume=0.0 
data%State%MudSystem%PumpsDumpFlowRate= 0.0
    


data%State%MudSystem%ActiveTankVolume= data%Configuration%Mud%ActiveMudVolumeGal   ! initial volume  (gal)
data%State%MudSystem%RefrencePitVolume= data%State%MudSystem%ActiveTankVolume/42.    !(bbl)
data%State%MudSystem%RefrencePitVolume_DrillWatch= data%State%MudSystem%ActiveTankVolume/42.    !(bbl)

data%State%MudSystem%MVT_MinVol_Allowded= 0.
data%State%MudSystem%MVT_MaxVol_Allowded= 0.

data%State%MudSystem%MudTank1_vol= data%Configuration%Mud%ActiveMudVolumeGal/3.       ! (gal)
data%State%MudSystem%MudTank2_vol= data%Configuration%Mud%ActiveMudVolumeGal/3.       ! (gal)
data%State%MudSystem%MudTank3_vol= data%Configuration%Mud%ActiveMudVolumeGal/3.       ! (gal)
data%State%MudSystem%ActiveTankSettled= data%Configuration%Mud%ActiveSettledContentsGal ! (gal)
data%State%MudSystem%MudTank4_vol= data%Configuration%Mud%InitialTripTankMudVolumeGal       ! (gal)

data%State%MudSystem%TripTankVolumeCalc= data%Configuration%Mud%InitialTripTankMudVolumeGal   ! initial volume  (gal)
data%State%MudSystem%ActiveTankDensity= data%Configuration%Mud%ActiveDensity       ! initial(ppg)
data%State%MudSystem%TripTankDensityCalc= data%EquipmentControl%Tank%TripTankDensity       ! initial(ppg)

data%State%MudSystem%ChokeManifoldDumpVolume= 0.0

data%State%MudSystem%PitGainLossZero= 0.
data%State%MudSystem%PitGainLossZero_Old= data%State%MudSystem%PitGainLossZero
data%State%MudSystem%MVTCoarseKnob_Old= data%EquipmentControl%DataDisplayConsole%MVTCoarseKnob
data%State%MudSystem%MVTFineKnob_Old= data%EquipmentControl%DataDisplayConsole%MVTFineKnob
data%State%MudSystem%FirstSet_Time= .true.



data%State%MudSystem%PedalMeter= data%Configuration%Mud%PedalFlowMeter    !1600. !(gpm)
data%State%MudSystem%ReturnFlowRate=0.



                  data%State%MudSystem%TotalStrokes1MFFI =0.
                  data%State%MudSystem%TotalStrokes2MFFI =0.

            data%State%MudSystem%TotalStrokesPump1=0.
            data%State%MudSystem%TotalStrokesPump2=0.
            data%State%MudSystem%GraphTotalStrokes=0.
            
            
                  data%State%Choke%TotalStrokes1 =0.
                  data%State%Choke%TotalStrokes2 =0.
            
            
            



    end
    
    
    
    
    
    
    
    
    
    
    
SUBROUTINE MUDLINE_LOSS_INPUTS()
USE MudSystemVARIABLES
use SimulationVariables !@@@
USE CBopStackVariables
use CPumpsVariables
    use CPumps
implicit none
INTEGER I    
    
           
            
!===========================================================================
!                   	MUDLINE MINOR LOSSES INPUT
!===========================================================================        
            
data%State%MudSystem%NO_MudMinors=4       

ALLOCATE (data%State%MudSystem%MudMinors(data%State%MudSystem%NO_MudMinors,4))

!                       ID(INCH)     LF	      CV	    NOTE(BAR)       DESCRIPTION
data%State%MudSystem%MudMinors(1,1)= data%Configuration%Pumps%MudPump1Output
data%State%MudSystem%MudMinors(1,2:4)=    (/1.5*8.,     0.,	    0./)      !elbow   (MLnumber=1,,PumpsToString)
data%State%MudSystem%MudMinors(2,1)= data%Configuration%Pumps%MudPump1Output
data%State%MudSystem%MudMinors(2,2:4)=    (/1.5*6.,     0.,	    0./)      !elbow   (MLnumber=2,,STGaugeToString)  
data%State%MudSystem%MudMinors(3,1:4)=    (/0.,        0.,     0.,	    0./)      !elbow   (MLnumber=3,,WellToPits)
data%State%MudSystem%MudMinors(4,1)= data%Configuration%BopStack%ChokeLineId
data%State%MudSystem%MudMinors(4,2:4)=    (/1.5*7.,     0.,	    0./)      !elbow   (MLnumber=4,,WellToChokeManifold) 


            
ALLOCATE (data%State%MudSystem%MINORDIAMETER_MUDLINE(data%State%MudSystem%NO_MudMinors),data%State%MudSystem%AREAMINOR_MUDLINE(data%State%MudSystem%NO_MudMinors),data%State%MudSystem%LF_MUDLINE(data%State%MudSystem%NO_MudMinors),data%State%MudSystem%CV_MUDLINE(data%State%MudSystem%NO_MudMinors)  &
            ,data%State%MudSystem%NOTE_MUDLINE(data%State%MudSystem%NO_MudMinors))



DO I=1,data%State%MudSystem%NO_MudMinors 
    data%State%MudSystem%MINORDIAMETER_MUDLINE(I)=data%State%MudSystem%MudMinors(I,1)
    data%State%MudSystem%LF_MUDLINE(I)=data%State%MudSystem%MudMinors(I,2)
    data%State%MudSystem%CV_MUDLINE(I)=data%State%MudSystem%MudMinors(I,3)
    data%State%MudSystem%NOTE_MUDLINE(I)=data%State%MudSystem%MudMinors(I,4)
    
    
    data%State%MudSystem%AREAMINOR_MUDLINE(I)=PII*(data%State%MudSystem%MINORDIAMETER_MUDLINE(I)*0.0254)**2/4.   !D(in), AREA(m^2)
ENDDO            
            
!===========================================================================
!                   	MUDLINE PIPNING LOSSES INPUT
!===========================================================================
data%State%MudSystem%NO_PIPINGSMUDLINE=4

ALLOCATE (data%State%MudSystem%PIPINGS_MUDLINE(data%State%MudSystem%NO_PIPINGSMUDLINE,3))

            !               ID(INCH)	L(FEET)	ROUGHNESS(MM)=e     DESCRIPTION	
data%State%MudSystem%PIPINGS_MUDLINE(1,1)= data%Configuration%Pumps%MudPump1Output
data%State%MudSystem%PIPINGS_MUDLINE(1,2:3)=		 (/265.,		0.03/)          !(MLnumber=1,,PumpsToString)
data%State%MudSystem%PIPINGS_MUDLINE(2,1)= data%Configuration%Pumps%MudPump1Output
data%State%MudSystem%PIPINGS_MUDLINE(2,2:3)=		 (/100.,       0.03/)          !(MLnumber=2,,STGaugeToString)
data%State%MudSystem%PIPINGS_MUDLINE(3,1:3)=		 (/0.,		0.,          0./)          !(MLnumber=3,,WellToPits)
data%State%MudSystem%PIPINGS_MUDLINE(4,1)= data%Configuration%BopStack%ChokeLineId
data%State%MudSystem%PIPINGS_MUDLINE(4,2)= data%Configuration%BopStack%ChokeLineLength
data%State%MudSystem%PIPINGS_MUDLINE(4,3)= 0.03          !(MLnumber=4,,WellToChokeManifold)

data%State%MudSystem%Area_ChokeLineFt= PII*((data%Configuration%BopStack%ChokeLineId/12.)**2)/4.   !D(in), AREA(ft^2)
data%State%MudSystem%ChokeLine_VolumeCapacity= data%State%MudSystem%Area_ChokeLineFt* data%Configuration%BopStack%ChokeLineLength* 7.48051948      ! (gal)

 ALLOCATE (data%State%MudSystem%DIAM_MUDLINE_INCH(data%State%MudSystem%NO_PIPINGSMUDLINE),  &
            data%State%MudSystem%AREA_MUDLINE(data%State%MudSystem%NO_PIPINGSMUDLINE),data%State%MudSystem%LENGT_MUDLINE(data%State%MudSystem%NO_PIPINGSMUDLINE),data%State%MudSystem%ROUGHNESS_MUDLINE(data%State%MudSystem%NO_PIPINGSMUDLINE),data%State%MudSystem%RELROUGH_MUDLINE(data%State%MudSystem%NO_PIPINGSMUDLINE))


DO I=1,data%State%MudSystem%NO_PIPINGSMUDLINE
    data%State%MudSystem%DIAM_MUDLINE_INCH(I)=data%State%MudSystem%PIPINGS_MUDLINE(I,1)
    data%State%MudSystem%LENGT_MUDLINE(I)=data%State%MudSystem%PIPINGS_MUDLINE(I,2)
    data%State%MudSystem%ROUGHNESS_MUDLINE(I)=data%State%MudSystem%PIPINGS_MUDLINE(I,3)
    
    
    
    data%State%MudSystem%AREA_MUDLINE(I)=PII*(data%State%MudSystem%DIAM_MUDLINE_INCH(I)*0.0254)**2/4   !D(in), AREA(m^2)
    data%State%MudSystem%RELROUGH_MUDLINE(I)=data%State%MudSystem%ROUGHNESS_MUDLINE(I)/(data%State%MudSystem%DIAM_MUDLINE_INCH(I)*25.4)    !e/D
    !DIAM_MUDLINE_MM(I)=DIAM_MUDLINE_MM(I)*.001  !  (m)
    data%State%MudSystem%LENGT_MUDLINE(I)=data%State%MudSystem%LENGT_MUDLINE(I)*.3048      !  (m) 
ENDDO 


!===========================================================================
!                   	MUDLINE STATIC LOSSES INPUT
!===========================================================================  

! Height are in (meter)
data%State%MudSystem%Pumps_Height= 0.
data%State%MudSystem%STpipeGauge_Height= 2. !(m)
data%State%MudSystem%Pits_Height= 1. !(m)
data%State%MudSystem%ChokeManifold_Height= 1.*0.3048  !(ft  to meter)
data%State%MudSystem%WellChokeExit_Height= data%Configuration%BopStack%GroundLevel-data%Configuration%BopStack%KillHeight












        
    END  
    
    

    
    SUBROUTINE MUDLINE_LOSSES(MLnumber) 
    
    USE MudSystemVARIABLES
use SimulationVariables !@@@ 
   implicit none 
    integer  I
    INTEGER MLnumber    
    
    
!===============================PIPE LOSS===================================  
 data%State%MUD(MLnumber)%Re_MUDline=data%State%MUD(MLnumber)%Q*6.30902e-5*data%State%MudSystem%DIAM_MUDLINE_INCH(MLnumber)*0.0254/(data%State%MudSystem%AREA_MUDLINE(MLnumber)*data%State%MUD(MLnumber)%nu)    !<<<<<< nu: DOROST SHAVAD.ALAN DAR STARTUP SET SHODE
!write(*,*) 'data%State%MUD(MLnumber)%Re_MUDline=' , data%State%MUD(MLnumber)%Re_MUDline
!   Q*6.30902e-5   for (gpm) to (m^3/sec)
     if ( data%State%MUD(MLnumber)%Re_MUDline<Re_cr) then
          data%State%MUD(MLnumber)%fric=64/ data%State%MUD(MLnumber)%Re_MUDline
     else

         data%State%MUD(MLnumber)%fric=1/(-1.8*log10((data%State%MudSystem%RELROUGH_MUDLINE(MLnumber)/3.7)**1.11+6.9/ data%State%MUD(MLnumber)%Re_MUDline))**2
     endif

 data%State%MUD(MLnumber)%fricloss=((data%State%MUD(MLnumber)%fric*(wdens*data%State%MUD(MLnumber)%Mud_SG*data%State%MudSystem%LENGT_MUDLINE(MLnumber)*(data%State%MUD(MLnumber)%Q*6.30902e-5/data%State%MudSystem%AREA_MUDLINE(MLnumber))**2))/(2*data%State%MudSystem%DIAM_MUDLINE_INCH(MLnumber)*0.0254))/6895


!==============================MINOR LOSS===================================  

    if (data%State%MudSystem%LF_MUDLINE(MLnumber)/=0) then
         data%State%MUD(MLnumber)%minlosspa_MUDLINE=data%State%MudSystem%LF_MUDLINE(MLnumber)*wdens*data%State%MUD(MLnumber)%Mud_SG*(data%State%MUD(MLnumber)%Q*6.30902e-5/data%State%MudSystem%AREAMINOR_MUDLINE(MLnumber))**2/2  !(Pa)
         data%State%MUD(MLnumber)%minloss_MUDLINE= data%State%MUD(MLnumber)%minlosspa_MUDLINE/6895 !(psi)
    elseif (data%State%MudSystem%CV_MUDLINE(MLnumber)/=0) then
         data%State%MUD(MLnumber)%minlosspa_MUDLINE=1000*data%State%MUD(MLnumber)%Mud_SG*((11.7*data%State%MUD(MLnumber)%Q*6.30902e-5*3600)/(data%State%MudSystem%CV_MUDLINE(MLnumber)))**2 !(pa)
         data%State%MUD(MLnumber)%minloss_MUDLINE= data%State%MUD(MLnumber)%minlosspa_MUDLINE/6895 !(psi)
    else
         data%State%MUD(MLnumber)%minlosspa_MUDLINE=data%State%MudSystem%NOTE_MUDLINE(MLnumber)*1e5 !(pa)
         data%State%MUD(MLnumber)%minloss_MUDLINE= data%State%MUD(MLnumber)%minlosspa_MUDLINE/6895 !(psi)
    endif

    
!==========================STATIC & KINETIC LOSS=============================  

data%State%MudSystem%String_Height= 50.*0.3048          !<<<<<<<<<<<<<<< (foot) to (meter). az khanom tarmigh       
data%State%MUD(1)%static_loss=(data%State%MudSystem%String_Height- data%State%MudSystem%Pumps_Height)*0.0    !(MLnumber=1,,PumpsToString)
data%State%MUD(2)%static_loss=(data%State%MudSystem%String_Height- data%State%MudSystem%STpipeGauge_Height)*data%State%MUD(2)%Mud_SG*wdens*gravity/6895    ! (psi)        (MLnumber=2,,STGaugeToString)
data%State%MUD(3)%static_loss=0.    !(MLnumber=1,,WellToPits)
data%State%MUD(4)%static_loss=(data%State%MudSystem%ChokeManifold_Height- data%State%MudSystem%WellChokeExit_Height)*data%State%MUD(4)%Mud_SG*wdens*gravity/6895    !(MLnumber=4,,WellToChokeManifold)



! data%State%RAM(RNUMBER)%kinetic_loss1=data%State%MUD(MLnumber)%Mud_SG*data%State%MUD(MLnumber)%Mud_Density*(data%State%RAM(RNUMBER)%Q*6.30902e-5/((1/4.)*pi*(.72*0.254e-1)**2))**2/(2*6895) !(psi)

 
 
!============================TOTAL LOSS=======================================  
 data%State%MUD(MLnumber)%total_loss= data%State%MUD(MLnumber)%fricloss+ data%State%MUD(MLnumber)%minloss_MUDLINE+ data%State%MUD(MLnumber)%static_loss!+ data%State%RAM(RNUMBER)%kinetic_loss1 !(psi)

    
    END
    
    
    
    
    
    
   SUBROUTINE DEALLOCATE_ARRAYS_MudSystem()
USE MudSystemVARIABLES
use SimulationVariables !@@@
implicit none
!===========================================================================
!                   	RAMLINE MINOR LOSSES INPUT
!===========================================================================
 if (allocated(data%State%MudSystem%MudMinors)) DEALLOCATE (data%State%MudSystem%MudMinors)
!===========================================================================
!                   	RAMLINE PIPNING LOSSES INPUT
!===========================================================================
 if (allocated(data%State%MudSystem%MINORDIAMETER_MUDLINE)) DEALLOCATE (data%State%MudSystem%MINORDIAMETER_MUDLINE)
 if (allocated(data%State%MudSystem%AREAMINOR_MUDLINE)) DEALLOCATE (data%State%MudSystem%AREAMINOR_MUDLINE)
 if (allocated(data%State%MudSystem%LF_MUDLINE)) DEALLOCATE (data%State%MudSystem%LF_MUDLINE)
 if (allocated(data%State%MudSystem%CV_MUDLINE)) DEALLOCATE (data%State%MudSystem%CV_MUDLINE)
 if (allocated(data%State%MudSystem%NOTE_MUDLINE)) DEALLOCATE (data%State%MudSystem%NOTE_MUDLINE)
 
!===========================================================================
!                   	ANNULAR MINOR LOSSES INPUT
!===========================================================================
 if (allocated(data%State%MudSystem%PIPINGS_MUDLINE)) DEALLOCATE (data%State%MudSystem%PIPINGS_MUDLINE)
!===========================================================================
!                   	ANNULAR PIPNING LOSSES INPUT
!===========================================================================  
 if (allocated(data%State%MudSystem%DIAM_MUDLINE_INCH)) DEALLOCATE (data%State%MudSystem%DIAM_MUDLINE_INCH)
 if (allocated(data%State%MudSystem%AREA_MUDLINE)) DEALLOCATE (data%State%MudSystem%AREA_MUDLINE)
 if (allocated(data%State%MudSystem%LENGT_MUDLINE)) DEALLOCATE (data%State%MudSystem%LENGT_MUDLINE)
 if (allocated(data%State%MudSystem%ROUGHNESS_MUDLINE)) DEALLOCATE (data%State%MudSystem%ROUGHNESS_MUDLINE)
 if (allocated(data%State%MudSystem%RELROUGH_MUDLINE)) DEALLOCATE (data%State%MudSystem%RELROUGH_MUDLINE)
 
    END  
    
    
    
    
    
    
    subroutine AddDynamicArray(array, value)
        implicit none
        REAL, allocatable, intent(inout) :: array(:)
        REAL, intent(in) :: value
        REAL, allocatable :: tempArr(:)
        integer :: i, isize
        
        !if(allocated(array)) then
        !    isize = size(array)
        !    allocate(tempArr(isize+1))
        !    do i=1,isize          
        !    tempArr(i) = array(i)
        !    end do
        !    tempArr(isize+1) = value
        !    deallocate(array)
        !    call move_alloc(tempArr, array)
        !else
        !    allocate(array(1))
        !    array(1) = value
        !end if
            
    end subroutine