Simulation Core
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1647 lines
101 KiB

  1. # 1 "/home/admin/SimulationCore2/Equipments/MudSystem/Pump_and_Trip_In.f90"
  2. subroutine Pump_and_TripIn ! is called in subroutine CirculationCodeSelect
  3. Use GeoElements_FluidModule
  4. USE CMudPropertiesVariables
  5. USE MudSystemVARIABLES
  6. use SimulationVariables !@@@
  7. use SimulationVariables
  8. USE CHOKEVARIABLES
  9. !use ConfigurationVariables !@
  10. !use CDataDisplayConsole
  11. !@ use ConfigurationVariables , StandPipePressureDataDisplay=>StandPipePressure
  12. !use CManifolds
  13. use SimulationVariables !@
  14. USE CHOKEVARIABLES
  15. !use ConfigurationVariables !@
  16. !use CChokeManifoldVariables
  17. use SimulationVariables
  18. !use CTanks
  19. !@use ConfigurationVariables, TripTankVolume2 => data%Equipments%DrillingWatch%TripTankVolume, TripTankDensity2 => TripTankDensity
  20. USE sROP_Other_Variables
  21. USE sROP_Variables
  22. use KickVARIABLESModule
  23. Use CShoeVariables
  24. use CError
  25. implicit none
  26. integer i,ii,AddLocation
  27. !===========================================================WELL============================================================
  28. !===========================================================WELL============================================================
  29. data%State%MudSystem%StringFlowRate= data%State%MUD(2)%Q
  30. data%State%MudSystem%AnnulusFlowRate= data%State%MUD(2)%Q
  31. !write(*,*) 'Trip In'
  32. !========================Horizontal PIPE ENTRANCE=================
  33. if (ABS(data%State%MudSystem%SuctionDensity_Old - data%State%MudSystem%Suction_Density_MudSystem) >= data%State%MudSystem%DensityMixTol) then ! new mud is pumped
  34. call data%State%MudSystem%Hz_Density%AddToFirst (data%State%MudSystem%Suction_Density_MudSystem)
  35. call data%State%MudSystem%Hz_MudDischarged_Volume%AddToFirst (0.0d0)
  36. call data%State%MudSystem%Hz_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(1))
  37. call data%State%MudSystem%Hz_Mud_Forehead_section%AddToFirst (1)
  38. call data%State%MudSystem%Hz_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(1))
  39. call data%State%MudSystem%Hz_Mud_Backhead_section%AddToFirst (1)
  40. call data%State%MudSystem%Hz_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
  41. call data%State%MudSystem%Hz_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
  42. call data%State%MudSystem%Hz_MudOrKick%AddToFirst (0)
  43. data%State%MudSystem%SuctionDensity_Old= data%State%MudSystem%Suction_Density_MudSystem
  44. endif
  45. !========================Horizontal PIPE STRING=================
  46. data%State%MudSystem%Hz_MudDischarged_Volume%Array(1)= data%State%MudSystem%Hz_MudDischarged_Volume%Array(1)+ ((data%State%MudSystem%StringFlowRate/60.0d0)*data%State%MudSystem%DeltaT_Mudline) !(gal)
  47. data%State%MudSystem%total_add = data%State%MudSystem%total_add + ((data%State%MudSystem%StringFlowRate/60.0d0)*data%State%MudSystem%DeltaT_Mudline)
  48. if (data%Equipments%ChokeControlPanel%ChokePanelStrokeResetSwitch == 1) then
  49. data%State%MudSystem%total_add= 0.
  50. endif
  51. !write(*,*) ' total decrease(add to HZ)=' , total_add
  52. !write(*,*) ' add to HZ=' , ((data%State%MudSystem%StringFlowRate/60.0d0)*DeltaT_Mudline)
  53. imud=0
  54. do while (imud < data%State%MudSystem%Hz_Mud_Forehead_X%Length())
  55. imud = imud + 1
  56. if (imud> 1) then
  57. data%State%MudSystem%Hz_Mud_Backhead_X%Array(imud)= data%State%MudSystem%Hz_Mud_Forehead_X%Array(imud-1)
  58. data%State%MudSystem%Hz_Mud_Backhead_section%Array(imud)= data%State%MudSystem%Hz_Mud_Forehead_section%Array(imud-1)
  59. endif
  60. data%State%MudSystem%DirectionCoef= (data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%Hz_Mud_Backhead_section%Array(imud))-data%State%MudSystem%Xstart_PipeSection(data%State%MudSystem%Hz_Mud_Backhead_section%Array(imud))) &
  61. / ABS(data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%Hz_Mud_Backhead_section%Array(imud))-data%State%MudSystem%Xstart_PipeSection(data%State%MudSystem%Hz_Mud_Backhead_section%Array(imud)))
  62. ! +1 for string , -1 for annulus
  63. data%State%MudSystem%Hz_EmptyVolume_inBackheadLocation%Array(imud)= data%State%MudSystem%DirectionCoef* (data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%Hz_Mud_Backhead_section%Array(imud))- data%State%MudSystem%Hz_Mud_Backhead_X%Array(imud))* &
  64. data%State%MudSystem%Area_PipeSectionFt(data%State%MudSystem%Hz_Mud_Backhead_section%Array(imud)) !(ft^3)
  65. data%State%MudSystem%Hz_EmptyVolume_inBackheadLocation%Array(imud)= data%State%MudSystem%Hz_EmptyVolume_inBackheadLocation%Array(imud)* 7.48051948d0 ! ft^3 to gal
  66. if ( data%State%MudSystem%Hz_MudDischarged_Volume%Array(imud) <= data%State%MudSystem%Hz_EmptyVolume_inBackheadLocation%Array(imud)) then
  67. data%State%MudSystem%Hz_Mud_Forehead_section%Array(imud)= data%State%MudSystem%Hz_Mud_Backhead_section%Array(imud)
  68. data%State%MudSystem%Hz_Mud_Forehead_X%Array(imud)= data%State%MudSystem%Hz_Mud_Backhead_X%Array(imud)+ data%State%MudSystem%DirectionCoef*(data%State%MudSystem%Hz_MudDischarged_Volume%Array(imud)/7.48051948d0)/data%State%MudSystem%Area_PipeSectionFt(data%State%MudSystem%Hz_Mud_Backhead_section%Array(imud))
  69. else
  70. data%State%MudSystem%isection= data%State%MudSystem%Hz_Mud_Backhead_section%Array(imud)+1
  71. data%State%MudSystem%Hz_RemainedVolume_in_LastSection%Array(imud)= data%State%MudSystem%Hz_MudDischarged_Volume%Array(imud)- data%State%MudSystem%Hz_EmptyVolume_inBackheadLocation%Array(imud)
  72. do
  73. if (data%State%MudSystem%isection > 1) then ! (horizontal pipe exit)
  74. data%State%MudSystem%Hz_MudDischarged_Volume%Array(imud)= data%State%MudSystem%Hz_MudDischarged_Volume%Array(imud)- data%State%MudSystem%Hz_RemainedVolume_in_LastSection%Array(imud)
  75. data%State%MudSystem%Hz_Mud_Forehead_X%Array(imud)= data%State%MudSystem%Xend_PipeSection(1)
  76. data%State%MudSystem%Hz_Mud_Forehead_section%Array(imud)= 1
  77. if (data%State%MudSystem%Hz_MudDischarged_Volume%Array(imud)<= 0.0d0) then ! imud is completely exited form the string
  78. call RemoveHzMudArrays(imud)
  79. endif
  80. exit
  81. endif
  82. data%State%MudSystem%xx= data%State%MudSystem%Hz_RemainedVolume_in_LastSection%Array(imud)/ data%State%MudSystem%PipeSection_VolumeCapacity(data%State%MudSystem%isection) !(gal)
  83. if (data%State%MudSystem%xx<= 1.0) then
  84. data%State%MudSystem%Hz_Mud_Forehead_section%Array(imud)= data%State%MudSystem%isection
  85. data%State%MudSystem%Hz_Mud_Forehead_X%Array(imud)= (data%State%MudSystem%xx * (data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%isection)- data%State%MudSystem%Xstart_PipeSection(data%State%MudSystem%isection)))+ data%State%MudSystem%Xstart_PipeSection(data%State%MudSystem%isection)
  86. exit
  87. else
  88. data%State%MudSystem%Hz_RemainedVolume_in_LastSection%Array(imud)= data%State%MudSystem%Hz_RemainedVolume_in_LastSection%Array(imud)- data%State%MudSystem%PipeSection_VolumeCapacity(data%State%MudSystem%isection)
  89. data%State%MudSystem%isection= data%State%MudSystem%isection+ 1
  90. endif
  91. enddo
  92. endif
  93. enddo
  94. !========================Horizontal PIPE END=================
  95. !========================Utube1 Air Element Removing=================
  96. !if (UtubeMode1Activated== .true.) then ! StringUpdate == .true.
  97. !
  98. !
  99. ! !StringDensity_Old=data%State%MudSystem%St_Density%Array(2)
  100. !
  101. ! write(*,*) 'StringDensity_Old=' , StringDensity_Old
  102. !
  103. ! UtubeMode1Activated= .false.
  104. !endif
  105. !========================Utube1 Air Element Removing End=================
  106. !!========================Utube2 Removing from Annulus================= not needed 97.04.26
  107. !
  108. ! if (UtubeMode2Activated== .true.) then ! StringUpdate == .true.
  109. !
  110. ! if (Ann_MudOrKick%Last() == 104) then !movaghati. albate age merge anjam shode bashe moshkeli nist
  111. ! call RemoveAnnulusMudArrays(Ann_MudOrKick%Length())
  112. ! endif
  113. !
  114. ! UtubeMode2Activated= .false.
  115. ! endif
  116. !
  117. !
  118. !!========================Utube2 Removing from Annulus End=================
  119. !========================New Pipe Filling=================
  120. !if (F_StringIntervalCounts > F_StringIntervalCountsOld) then ! StringUpdate == .true.
  121. if (data%State%MudSystem%AddedElementsToString > 0) then ! StringUpdate == .true.
  122. !NoPipeAdded= data%State%F_Counts%StringIntervalCounts - F_StringIntervalCountsOld
  123. data%State%MudSystem%NewPipeFilling=0
  124. IF (data%State%MudSystem%St_MudOrKick%First() == 104) then
  125. data%State%MudSystem%St_MudDischarged_Volume%Array(1) = data%State%MudSystem%St_MudDischarged_Volume%Array(1) + sum(data%State%MudSystem%PipeSection_VolumeCapacity(2:1+data%State%MudSystem%AddedElementsToString)) ! new pipe is filled by air
  126. else
  127. call data%State%MudSystem%St_Density%AddToFirst (0.d0)
  128. call data%State%MudSystem%St_MudDischarged_Volume%AddToFirst (sum(data%State%MudSystem%PipeSection_VolumeCapacity(2:1+data%State%MudSystem%AddedElementsToString)))
  129. call data%State%MudSystem%St_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(2))
  130. call data%State%MudSystem%St_Mud_Forehead_section%AddToFirst (2)
  131. call data%State%MudSystem%St_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(2))
  132. call data%State%MudSystem%St_Mud_Backhead_section%AddToFirst (2)
  133. call data%State%MudSystem%St_RemainedVolume_in_LastSection%AddToFirst (0.d0)
  134. call data%State%MudSystem%St_EmptyVolume_inBackheadLocation%AddToFirst (0.d0)
  135. call data%State%MudSystem%St_MudOrKick%AddToFirst (104)
  136. endif
  137. endif
  138. !F_StringIntervalCountsOld= F_StringIntervalCounts
  139. if (data%State%MudSystem%NewPipeFilling == 0) then ! 2= is the first element of string (1= is for Hz pipe)
  140. data%State%MudSystem%LackageMudVolume= data%State%MudSystem%St_MudDischarged_Volume%Array(1) ! = Air element
  141. write(*,*) 'LackageMudVolume=' , data%State%MudSystem%LackageMudVolume
  142. if (ABS(data%State%MudSystem%St_Density%Array(2) - data%State%MudSystem%Hz_Density%Last()) >= data%State%MudSystem%DensityMixTol) then ! new mud is pumped
  143. call data%State%MudSystem%St_Density%AddTo (2,data%State%MudSystem%Hz_Density%Last())
  144. call data%State%MudSystem%St_MudDischarged_Volume%AddTo (2, 0.d0)
  145. call data%State%MudSystem%St_Mud_Forehead_X%AddTo (2,data%State%MudSystem%Xstart_PipeSection(2))
  146. call data%State%MudSystem%St_Mud_Forehead_section%AddTo (2 , 2)
  147. call data%State%MudSystem%St_Mud_Backhead_X%AddTo (2,data%State%MudSystem%Xstart_PipeSection(2))
  148. call data%State%MudSystem%St_Mud_Backhead_section%AddTo (2 ,2)
  149. call data%State%MudSystem%St_RemainedVolume_in_LastSection%AddTo (2,0.d0)
  150. call data%State%MudSystem%St_EmptyVolume_inBackheadLocation%AddTo (2,0.d0)
  151. call data%State%MudSystem%St_MudOrKick%AddTo (2,0)
  152. !StringDensity_Old= Hz_Density%Last()
  153. endif
  154. data%State%MudSystem%St_MudDischarged_Volume%Array(2)= data%State%MudSystem%St_MudDischarged_Volume%Array(2)+ min( ((data%State%MudSystem%StringFlowRate/60.0d0)*data%State%MudSystem%DeltaT_Mudline), data%State%MudSystem%LackageMudVolume) !(gal)
  155. data%State%MudSystem%St_MudDischarged_Volume%Array(1)= data%State%MudSystem%St_MudDischarged_Volume%Array(1)- min( ((data%State%MudSystem%StringFlowRate/60.0d0)*data%State%MudSystem%DeltaT_Mudline), data%State%MudSystem%LackageMudVolume) ! air(gal)
  156. !LackageMudVolumeAfterFilling= sum(PipeSection_VolumeCapacity(2:F_StringIntervalCounts)) - sum(St_MudDischarged_Volume%Array(:))
  157. data%State%MudSystem%LackageMudVolumeAfterFilling= data%State%MudSystem%St_MudDischarged_Volume%Array(1) ! last time it should be zero
  158. if (data%State%MudSystem%LackageMudVolumeAfterFilling == 0.) then
  159. data%State%MudSystem%NewPipeFilling= 1
  160. call RemoveStringMudArrays(1)
  161. data%State%MudSystem%St_Mud_Backhead_X%Array(1) = data%State%MudSystem%Xstart_PipeSection(2)
  162. data%State%MudSystem%St_Mud_Backhead_section%Array(1) = 2
  163. endif
  164. endif
  165. !========================New Pipe Filling End=================
  166. if (data%State%MudSystem%NewPipeFilling == 0) then
  167. data%State%MudSystem%StringFlowRate= 0.
  168. data%State%MudSystem%AnnulusFlowRate= 0.
  169. endif
  170. data%State%MudSystem%StringFlowRateFinal= data%State%MudSystem%StringFlowRate
  171. data%State%MudSystem%AnnulusFlowRateFinal= data%State%MudSystem%AnnulusFlowRate
  172. !========================STRING ENTRANCE=================
  173. if (data%State%MudSystem%StringFlowRateFinal > 0.0 .and. ABS(data%State%MudSystem%St_Density%First() - data%State%MudSystem%Hz_Density%Last()) >= data%State%MudSystem%DensityMixTol) then ! new mud is pumped
  174. call data%State%MudSystem%St_Density%AddToFirst (data%State%MudSystem%Hz_Density%Last())
  175. call data%State%MudSystem%St_MudDischarged_Volume%AddToFirst (0.0d0)
  176. call data%State%MudSystem%St_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(2))
  177. call data%State%MudSystem%St_Mud_Forehead_section%AddToFirst (2)
  178. call data%State%MudSystem%St_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(2))
  179. call data%State%MudSystem%St_Mud_Backhead_section%AddToFirst (2)
  180. call data%State%MudSystem%St_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
  181. call data%State%MudSystem%St_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
  182. call data%State%MudSystem%St_MudOrKick%AddToFirst (0)
  183. !StringDensity_Old= Hz_Density%Last()
  184. endif
  185. data%State%MudSystem%St_MudDischarged_Volume%Array(1)= data%State%MudSystem%St_MudDischarged_Volume%Array(1)+ ((data%State%MudSystem%StringFlowRate/60.0d0)*data%State%MudSystem%DeltaT_Mudline) !(gal)
  186. !=============== save String Mud data===========
  187. data%State%MudSystem%StMudVolumeSum= 0.d0
  188. !St_MudSaved_Density= 0.d0
  189. data%State%MudSystem%St_Saved_MudDischarged_Volume= 0.d0
  190. !Saved_St_MudOrKick= 0
  191. !Ann_to_Choke_2mud= .false.
  192. do imud=1, data%State%MudSystem%St_MudDischarged_Volume%Length()
  193. data%State%MudSystem%StMudVolumeSum = data%State%MudSystem%StMudVolumeSum + data%State%MudSystem%St_MudDischarged_Volume%Array(imud)
  194. if ( data%State%MudSystem%StMudVolumeSum > sum(data%State%MudSystem%PipeSection_VolumeCapacity(2:data%State%F_Counts%StringIntervalCounts)) ) then
  195. !IF (St_MudOrKick%Array(imud) == 0) THEN
  196. data%State%MudSystem%St_MudSaved_Density =data%State%MudSystem%St_Density%Array(imud)
  197. data%State%MudSystem%St_Saved_MudDischarged_Volume = data%State%MudSystem%StMudVolumeSum - sum(data%State%MudSystem%PipeSection_VolumeCapacity(2:data%State%F_Counts%StringIntervalCounts))
  198. !ELSEIF (St_MudOrKick%Array(imud) > 0 .AND. St_MudOrKick%Array(imud) <100) THEN ! 104= AIR
  199. ! St_Kick_Saved_Volume = StMudVolumeSum - sum(PipeSection_VolumeCapacity(F_StringIntervalCounts+1:NoPipeSections))
  200. ! Saved_St_MudOrKick= St_MudOrKick%Array (imud)
  201. ! St_KickSaved_Density=data%State%MudSystem%St_Density%Array(imud)
  202. !END IF
  203. do ii= imud + 1, data%State%MudSystem%St_MudDischarged_Volume%Length()
  204. !IF (St_MudOrKick%Array(ii) == 0) THEN
  205. data%State%MudSystem%St_MudSaved_Density = ((data%State%MudSystem%St_MudSaved_Density * data%State%MudSystem%St_Saved_MudDischarged_Volume) + (data%State%MudSystem%St_Density%Array(ii) * data%State%MudSystem%St_MudDischarged_Volume%Array(ii))) / (data%State%MudSystem%St_Saved_MudDischarged_Volume + data%State%MudSystem%St_MudDischarged_Volume%Array(ii))
  206. data%State%MudSystem%St_Saved_MudDischarged_Volume = data%State%MudSystem%St_Saved_MudDischarged_Volume + data%State%MudSystem%St_MudDischarged_Volume%Array(ii)
  207. !ELSEIF (St_MudOrKick%Array(imud) > 0 .AND. St_MudOrKick%Array(imud) <100) THEN ! 104= AIR
  208. ! St_Kick_Saved_Volume = St_Kick_Saved_Volume + St_MudDischarged_Volume%Array(ii)
  209. ! Saved_St_MudOrKick= St_MudOrKick%Array (ii)
  210. ! St_KickSaved_Density=data%State%MudSystem%St_Density%Array(ii)
  211. !END IF
  212. enddo
  213. !WRITE (*,*) 'St_Saved_Mud_Volume, St_Kick_Saved_Volume', St_Saved_MudDischarged_Volume, St_Kick_Saved_Volume
  214. exit ! exits do
  215. endif
  216. enddo
  217. data%State%MudSystem%St_Saved_MudDischarged_Volume_Final = data%State%MudSystem%St_Saved_MudDischarged_Volume
  218. IF (data%State%MudSystem%WellHeadIsOpen) data%State%MudSystem%MudVolume_InjectedToBH = data%State%MudSystem%St_Saved_MudDischarged_Volume_Final
  219. !======================================================================
  220. !========================STRING=================
  221. imud=0
  222. do while (imud < data%State%MudSystem%St_Mud_Forehead_X%Length())
  223. imud = imud + 1
  224. if (imud> 1) then
  225. data%State%MudSystem%St_Mud_Backhead_X%Array(imud)= data%State%MudSystem%St_Mud_Forehead_X%Array(imud-1)
  226. data%State%MudSystem%St_Mud_Backhead_section%Array(imud)= data%State%MudSystem%St_Mud_Forehead_section%Array(imud-1)
  227. endif
  228. data%State%MudSystem%DirectionCoef= (data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%St_Mud_Backhead_section%Array(imud))-data%State%MudSystem%Xstart_PipeSection(data%State%MudSystem%St_Mud_Backhead_section%Array(imud))) &
  229. / ABS(data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%St_Mud_Backhead_section%Array(imud))-data%State%MudSystem%Xstart_PipeSection(data%State%MudSystem%St_Mud_Backhead_section%Array(imud)))
  230. ! +1 for string , -1 for annulus
  231. data%State%MudSystem%St_EmptyVolume_inBackheadLocation%Array(imud)= data%State%MudSystem%DirectionCoef* (data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%St_Mud_Backhead_section%Array(imud))- data%State%MudSystem%St_Mud_Backhead_X%Array(imud))* &
  232. data%State%MudSystem%Area_PipeSectionFt(data%State%MudSystem%St_Mud_Backhead_section%Array(imud)) !(ft^3)
  233. data%State%MudSystem%St_EmptyVolume_inBackheadLocation%Array(imud)= data%State%MudSystem%St_EmptyVolume_inBackheadLocation%Array(imud)* 7.48051948d0 ! ft^3 to gal
  234. !write(*,*) 'St_Mud_Backhead_section%Array(1)=' , St_Mud_Backhead_section%Array(1)
  235. !write(*,*) 'Xend_PipeSection(St_Mud_Backhead_section%Array(1))=' , Xend_PipeSection(St_Mud_Backhead_section%Array(1))
  236. !
  237. !write(*,*) 'St_EmptyVolume_inBackheadLocation%Array(1)=' , St_EmptyVolume_inBackheadLocation%Array(1)
  238. !write(*,*) 'St_Mud_Backhead_X%Array(1)=' , St_Mud_Backhead_X%Array(1)
  239. if ( data%State%MudSystem%St_MudDischarged_Volume%Array(imud) <= data%State%MudSystem%St_EmptyVolume_inBackheadLocation%Array(imud)) then
  240. data%State%MudSystem%St_Mud_Forehead_section%Array(imud)= data%State%MudSystem%St_Mud_Backhead_section%Array(imud)
  241. data%State%MudSystem%St_Mud_Forehead_X%Array(imud)= data%State%MudSystem%St_Mud_Backhead_X%Array(imud)+ data%State%MudSystem%DirectionCoef*(data%State%MudSystem%St_MudDischarged_Volume%Array(imud)/7.48051948d0)/data%State%MudSystem%Area_PipeSectionFt(data%State%MudSystem%St_Mud_Backhead_section%Array(imud))
  242. ! 7.48 is for gal to ft^3
  243. else
  244. data%State%MudSystem%isection= data%State%MudSystem%St_Mud_Backhead_section%Array(imud)+1
  245. data%State%MudSystem%St_RemainedVolume_in_LastSection%Array(imud)= data%State%MudSystem%St_MudDischarged_Volume%Array(imud)- data%State%MudSystem%St_EmptyVolume_inBackheadLocation%Array(imud)
  246. do
  247. if (data%State%MudSystem%isection > data%State%F_Counts%StringIntervalCounts) then ! last pipe section(string exit) data%State%F_Counts%StringIntervalCounts includes Horizontal line
  248. data%State%MudSystem%St_MudDischarged_Volume%Array(imud)= data%State%MudSystem%St_MudDischarged_Volume%Array(imud)- data%State%MudSystem%St_RemainedVolume_in_LastSection%Array(imud)
  249. data%State%MudSystem%St_Mud_Forehead_X%Array(imud)= data%State%MudSystem%Xend_PipeSection(data%State%F_Counts%StringIntervalCounts)
  250. data%State%MudSystem%St_Mud_Forehead_section%Array(imud)= data%State%F_Counts%StringIntervalCounts
  251. if (data%State%MudSystem%St_MudDischarged_Volume%Array(imud)<= 0.0d0) then ! imud is completely exited form the string
  252. call RemoveStringMudArrays(imud)
  253. endif
  254. exit
  255. endif
  256. data%State%MudSystem%xx= data%State%MudSystem%St_RemainedVolume_in_LastSection%Array(imud)/ data%State%MudSystem%PipeSection_VolumeCapacity(data%State%MudSystem%isection) !(gal)
  257. if (data%State%MudSystem%xx<= 1.0) then
  258. data%State%MudSystem%St_Mud_Forehead_section%Array(imud)= data%State%MudSystem%isection
  259. data%State%MudSystem%St_Mud_Forehead_X%Array(imud)= (data%State%MudSystem%xx * (data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%isection)- data%State%MudSystem%Xstart_PipeSection(data%State%MudSystem%isection)))+ data%State%MudSystem%Xstart_PipeSection(data%State%MudSystem%isection)
  260. exit
  261. else
  262. data%State%MudSystem%St_RemainedVolume_in_LastSection%Array(imud)= data%State%MudSystem%St_RemainedVolume_in_LastSection%Array(imud)- data%State%MudSystem%PipeSection_VolumeCapacity(data%State%MudSystem%isection)
  263. data%State%MudSystem%isection= data%State%MudSystem%isection+ 1
  264. endif
  265. enddo
  266. endif
  267. enddo
  268. !write(*,*) ' a before=='
  269. !
  270. ! do imud=1, Op_MudDischarged_Volume%Length()
  271. ! write(*,*) 'Op:', imud, Op_MudDischarged_Volume%Array(imud), Op_Density%Array(imud) ,Op_MudOrKick%Array(imud)
  272. ! enddo
  273. !
  274. ! do imud=1, Ann_MudDischarged_Volume%Length()
  275. ! write(*,*) 'Ann:', imud, Ann_MudDischarged_Volume%Array(imud), Ann_Density%Array(imud) ,Ann_MudOrKick%Array(imud)
  276. ! enddo
  277. !
  278. !write(*,*) '==== a before'
  279. !write(*,*) ' iloc (a): ' , iloc
  280. !========================STRING END=================
  281. IF (data%State%MudSystem%Op_MudOrKick%Last() /= 0 .and. data%State%MudSystem%Op_MudOrKick%Last()==data%State%MudSystem%Ann_MudOrKick%First()) data%State%MudSystem%iLoc=2 ! it may be 1,2,3 or more, all of them are kick
  282. !write(*,*) ' iloc (b): ' , iloc
  283. !=============================Add PumpFlowRate to Bottom Hole ==============================
  284. !if ( data%State%MudSystem%AnnulusFlowRate>0.0 ) then
  285. if ( data%State%MudSystem%MudVolume_InjectedToBH > 0.0 ) then
  286. if (KickVARIABLES%KickOffBottom) then ! (kickOffBottom = F) means kick is next to the bottom hole and usually kick is entering the
  287. AddLocation= data%State%MudSystem%Op_Density%Length()-data%State%MudSystem%iLoc+1+1 ! well, thus pumped mud should be placed above the kick
  288. else
  289. AddLocation= data%State%MudSystem%Op_Density%Length()+1
  290. endif
  291. !write(*,*) 'AddLocation====' , AddLocation
  292. if ( AddLocation== 0) CALL ErrorStop ('AddLocation=0')
  293. if ( ABS(data%State%MudSystem%St_Density%Last() - data%State%MudSystem%Op_Density%Array(AddLocation-1)) >= data%State%MudSystem%DensityMixTol ) then
  294. !write(*,*) 'new pocket**'
  295. !write(*,*) data%State%MudSystem%St_Density%Last()=' ,data%State%MudSystem%St_Density%Last()
  296. !write(*,*) 'Op_Density%Array(AddLocation-1)=' , Op_Density%Array(AddLocation-1)
  297. call data%State%MudSystem%Op_Density% AddTo (AddLocation,data%State%MudSystem%St_Density%Last())
  298. !call Op_MudDischarged_Volume%AddTo (AddLocation,((data%State%MudSystem%AnnulusFlowRate/60.d0)*DeltaT_Mudline))
  299. call data%State%MudSystem%Op_MudDischarged_Volume%AddTo (AddLocation,data%State%MudSystem%MudVolume_InjectedToBH)
  300. call data%State%MudSystem%Op_Mud_Forehead_X%AddTo (AddLocation,data%State%MudSystem%Xstart_OpSection(1))
  301. call data%State%MudSystem%Op_Mud_Forehead_section%AddTo (AddLocation,1)
  302. call data%State%MudSystem%Op_Mud_Backhead_X%AddTo (AddLocation,data%State%MudSystem%Xstart_OpSection(1))
  303. call data%State%MudSystem%Op_Mud_Backhead_section%AddTo (AddLocation,1)
  304. call data%State%MudSystem%Op_RemainedVolume_in_LastSection%AddTo (AddLocation,0.0d0)
  305. call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%AddTo (AddLocation,0.0d0)
  306. call data%State%MudSystem%Op_MudOrKick%AddTo (AddLocation,0)
  307. else
  308. !write(*,*) 'merge**'
  309. !write(*,*) 'density before=' , Op_Density%Array(AddLocation-1)
  310. !write(*,*) data%State%MudSystem%St_Density%Last() for mix=' ,data%State%MudSystem%St_Density%Last()
  311. !Op_Density%Array(AddLocation-1)= (Op_Density%Array(AddLocation-1)*Op_MudDischarged_Volume%Array(AddLocation-1)data%State%MudSystem%St_Density%Last()*((data%State%MudSystem%AnnulusFlowRate/60.d0)*DeltaT_Mudline))/(Op_MudDischarged_Volume%Array(AddLocation-1)+((data%State%MudSystem%AnnulusFlowRate/60.d0)*DeltaT_Mudline))
  312. !Op_MudDischarged_Volume%Array(AddLocation-1)= Op_MudDischarged_Volume%Array(AddLocation-1) + ((data%State%MudSystem%AnnulusFlowRate/60.d0)*DeltaT_Mudline)
  313. data%State%MudSystem%Op_Density%Array(AddLocation-1)= (data%State%MudSystem%Op_Density%Array(AddLocation-1)*data%State%MudSystem%Op_MudDischarged_Volume%Array(AddLocation-1)+data%State%MudSystem%St_Density%Last()*data%State%MudSystem%MudVolume_InjectedToBH)/(data%State%MudSystem%Op_MudDischarged_Volume%Array(AddLocation-1)+data%State%MudSystem%MudVolume_InjectedToBH)
  314. data%State%MudSystem%Op_MudDischarged_Volume%Array(AddLocation-1)= data%State%MudSystem%Op_MudDischarged_Volume%Array(AddLocation-1) + data%State%MudSystem%MudVolume_InjectedToBH
  315. !write(*,*) 'density after=' , Op_Density%Array(AddLocation-1)
  316. endif
  317. endif
  318. !=======================Add PumpFlowRate to Bottom Hole- End ==============================
  319. !=============== save OP Mud data to transfer to the annulus enterance due to tripin or kick
  320. data%State%MudSystem%OpMudVolumeSum= 0.d0
  321. !Op_MudSaved_Density= 0.d0
  322. !Op_KickSaved_Density= 0.d0
  323. data%State%MudSystem%Op_Saved_MudDischarged_Volume= 0.d0
  324. data%State%MudSystem%Op_Kick_Saved_Volume= 0.d0
  325. data%State%MudSystem%Saved_Op_MudOrKick= 0
  326. !write(*,*) 'Op_Capacity===' , sum(OpSection_VolumeCapacity(1:F_BottomHoleIntervalCounts))
  327. !write(*,*) 'Op_MudDischarged_Volume%Length()===' , Op_MudDischarged_Volume%Length()
  328. !
  329. do imud=1, data%State%MudSystem%Op_MudDischarged_Volume%Length()
  330. !write(*,*) 'imud, Op_MudDischarged_Volume%Array(imud)=' , imud,Op_MudDischarged_Volume%Array(imud)
  331. data%State%MudSystem%OpMudVolumeSum= data%State%MudSystem%OpMudVolumeSum + data%State%MudSystem%Op_MudDischarged_Volume%Array(imud)
  332. if ( data%State%MudSystem%OpMudVolumeSum > sum(data%State%MudSystem%OpSection_VolumeCapacity(1:data%State%F_Counts%BottomHoleIntervalCounts)) ) then
  333. IF (data%State%MudSystem%Op_MudOrKick%Array(imud) == 0) THEN
  334. data%State%MudSystem%Op_MudSaved_Density = data%State%MudSystem%Op_Density%Array(imud)
  335. data%State%MudSystem%Op_Saved_MudDischarged_Volume = data%State%MudSystem%OpMudVolumeSum - sum(data%State%MudSystem%OpSection_VolumeCapacity(1:data%State%F_Counts%BottomHoleIntervalCounts))
  336. ELSE
  337. data%State%MudSystem%Op_Kick_Saved_Volume = data%State%MudSystem%OpMudVolumeSum - sum(data%State%MudSystem%OpSection_VolumeCapacity(1:data%State%F_Counts%BottomHoleIntervalCounts))
  338. !write(*,*) 'cond 1- Op_MudOrKick%Array (imud),Op_Density%Array(imud):' ,Op_MudOrKick%Array (imud),Op_Density%Array(imud)
  339. data%State%MudSystem%Saved_Op_MudOrKick= data%State%MudSystem%Op_MudOrKick%Array (imud)
  340. data%State%MudSystem%Op_KickSaved_Density= data%State%MudSystem%Op_Density%Array(imud)
  341. data%State%MudSystem%iLoc= 2
  342. END IF
  343. do ii= imud + 1, data%State%MudSystem%Op_MudDischarged_Volume%Length()
  344. IF (data%State%MudSystem%Op_MudOrKick%Array(ii) == 0) THEN
  345. data%State%MudSystem%Op_MudSaved_Density = ((data%State%MudSystem%Op_MudSaved_Density * data%State%MudSystem%Op_Saved_MudDischarged_Volume) + (data%State%MudSystem%Op_Density%Array(ii) * data%State%MudSystem%Op_MudDischarged_Volume%Array(ii))) / (data%State%MudSystem%Op_Saved_MudDischarged_Volume + data%State%MudSystem%Op_MudDischarged_Volume%Array(ii))
  346. data%State%MudSystem%Op_Saved_MudDischarged_Volume = data%State%MudSystem%Op_Saved_MudDischarged_Volume + data%State%MudSystem%Op_MudDischarged_Volume%Array(ii)
  347. ELSE
  348. data%State%MudSystem%Op_Kick_Saved_Volume = data%State%MudSystem%Op_Kick_Saved_Volume + data%State%MudSystem%Op_MudDischarged_Volume%Array(ii)
  349. !write(*,*) 'cond 2- Op_MudOrKick%Array (ii),Op_Density%Array(ii):' ,Op_MudOrKick%Array (ii),Op_Density%Array(ii)
  350. data%State%MudSystem%Saved_Op_MudOrKick= data%State%MudSystem%Op_MudOrKick%Array (ii)
  351. data%State%MudSystem%Op_KickSaved_Density= data%State%MudSystem%Op_Density%Array(ii)
  352. data%State%MudSystem%iLoc= 2
  353. END IF
  354. enddo
  355. exit ! exits do
  356. endif
  357. enddo
  358. !WRITE (*,*) 'Op_Saved_MudDischarged_Volume, Op_Kick_Saved_Volume',Op_Saved_MudDischarged_Volume, Op_Kick_Saved_Volume
  359. !write(*,*) ' iloc (c): ' , iloc
  360. !======================================================================
  361. !======================================================================
  362. !if (iLoc == 1) then
  363. data%State%MudSystem%MudSection= data%State%F_Counts%StringIntervalCounts+1
  364. data%State%MudSystem%BackheadX= data%State%MudSystem%Xstart_PipeSection(data%State%F_Counts%StringIntervalCounts+1)
  365. !elseif (iLoc == 2) then
  366. ! MudSection= Kick_Forehead_section
  367. ! BackheadX= Kick_Forehead_X
  368. !endif
  369. !========================ANNULUS ENTRANCE====================
  370. !if (KickMigration_2SideBit == .FALSE.) then ! because its effect is applied in Migration Code
  371. ! !write(*,*) 'iloc=====' , iLoc bejaye data%State%ROP_Bit%RateOfPenetration ==0. in bude: DeltaVolumeOp == 0.0
  372. ! if (ABS(AnnulusSuctionDensity_OldMudSystem%St_Density%Last()) >= DensityMixTol .OR. (DeltaVolumeOp == 0.0 .and. ABS(Ann_Density%Array(iLoc)data%State%MudSystem%St_Density%Last())>=DensityMixTol .and. data%State%MudSystem%AnnulusFlowRate/=0.0d0) ) then ! new mud is pumped
  373. ! call Ann_Density%AddTo (iLocMudSystem%St_Density%Last())
  374. ! call Ann_MudDischarged_Volume%AddTo (iLoc,0.0d0)
  375. ! call Ann_Mud_Forehead_X%AddTo (iLoc,BackheadX)
  376. ! call Ann_Mud_Forehead_section%AddTo (iLoc,MudSection)
  377. ! call Ann_Mud_Backhead_X%AddTo (iLoc,BackheadX)
  378. ! call Ann_Mud_Backhead_section%AddTo (iLoc,MudSection)
  379. ! call Ann_RemainedVolume_in_LastSection%AddTo (iLoc,0.0d0)
  380. ! call Ann_EmptyVolume_inBackheadLocation%AddTo (iLoc,0.0d0)
  381. ! call Ann_MudOrKick%AddTo (iLoc,0)
  382. ! call Ann_CuttingMud%AddTo (iLoc,0)
  383. ! !write(*,*) 'c) annLength=' , Ann_Density%Length()
  384. !
  385. ! AnnulusSuctionDensity_Old=data%State%MudSystem%St_Density%Last()
  386. !
  387. ! MudIsChanged= .true.
  388. ! endif
  389. !
  390. ! Ann_MudDischarged_Volume%Array(iLoc)= Ann_MudDischarged_Volume%Array(iLoc)+ ((data%State%MudSystem%AnnulusFlowRate/60.d0)*DeltaT_Mudline) !(gal)
  391. !
  392. !endif
  393. data%State%MudSystem%Ann_Mud_Backhead_section%Array(1)= data%State%MudSystem%MudSection !it is needed to be updated for a condition that one pipe is removed from Annulus due to trip out
  394. data%State%MudSystem%Ann_Mud_Backhead_X%Array(1)= data%State%MudSystem%BackheadX
  395. ! write(*,*) 'zero)Ann_Mud sum=' , sum(Ann_MudDischarged_Volume%Array(:))
  396. !
  397. !
  398. !write(*,*) 'pump added-before add to ann=='
  399. !
  400. ! do imud=1, Op_MudDischarged_Volume%Length()
  401. ! write(*,*) 'Op:', imud, Op_MudDischarged_Volume%Array(imud), Op_Density%Array(imud) ,Op_MudOrKick%Array(imud)
  402. ! enddo
  403. !
  404. ! do imud=1, Ann_MudDischarged_Volume%Length()
  405. ! write(*,*) 'Ann:', imud, Ann_MudDischarged_Volume%Array(imud), Ann_Density%Array(imud) ,Ann_MudOrKick%Array(imud)
  406. ! enddo
  407. !
  408. !write(*,*) '====pump added-before add to ann'
  409. !========================Tripping In====================
  410. !write(*,*) 'DeltaVolumeOp=' , DeltaVolumeOp
  411. if (data%State%ROP_Bit%RateOfPenetration==0.) then ! .and. Op_MudOrKick%Last() == 0) then ! trip in mode(loole paeen) Mud
  412. !write(*,*) 'Tripping In'
  413. !write(*,*) 'before' ,'Ann_Volume%Array(1)=' , Ann_MudDischarged_Volume%Array(1)
  414. !if ( MudIsChanged== .true. ) then
  415. ! call RemoveAnnulusMudArrays(iLoc)
  416. !endif
  417. if (data%State%MudSystem%Op_Kick_Saved_Volume > 0.0 .and. data%State%MudSystem%Ann_MudOrKick%First() == 0) then
  418. write(*,*) 'Kick influx enters Annulus'
  419. call data%State%MudSystem%Ann_Density%AddToFirst (data%State%MudSystem%Op_KickSaved_Density)
  420. call data%State%MudSystem%Ann_MudDischarged_Volume%AddToFirst (data%State%MudSystem%Op_Kick_Saved_Volume)
  421. call data%State%MudSystem%Ann_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(data%State%F_Counts%StringIntervalCounts+1))
  422. call data%State%MudSystem%Ann_Mud_Forehead_section%AddToFirst (data%State%F_Counts%StringIntervalCounts+1)
  423. call data%State%MudSystem%Ann_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(data%State%F_Counts%StringIntervalCounts+1))
  424. call data%State%MudSystem%Ann_Mud_Backhead_section%AddToFirst (data%State%F_Counts%StringIntervalCounts+1)
  425. call data%State%MudSystem%Ann_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
  426. call data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
  427. call data%State%MudSystem%Ann_MudOrKick%AddToFirst (data%State%MudSystem%Saved_Op_MudOrKick) !<<<<<<<<
  428. call data%State%MudSystem%Ann_CuttingMud%AddToFirst (0)
  429. elseif (data%State%MudSystem%Op_Kick_Saved_Volume > 0.0 .and. data%State%MudSystem%Ann_MudOrKick%First() /= 0) then
  430. data%State%MudSystem%Ann_MudDischarged_Volume%Array(1)= data%State%MudSystem%Ann_MudDischarged_Volume%Array(1) + data%State%MudSystem%Op_Kick_Saved_Volume
  431. endif
  432. if (data%State%MudSystem%Op_Saved_MudDischarged_Volume> 0.0) then
  433. data%State%MudSystem%NewDensity= data%State%MudSystem%Op_MudSaved_Density
  434. data%State%MudSystem%NewVolume= data%State%MudSystem%Op_Saved_MudDischarged_Volume
  435. !write(*,*) 'NewVolume=' , NewVolume
  436. !write(*,*) 'iloc=' , iloc,'Ann_MudDischarged_Volume%Array(1)=' , Ann_MudDischarged_Volume%Array(1)
  437. if ((data%State%ROP_Bit%RateOfPenetration==0 .and. abs(data%State%MudSystem%Ann_Density%Array(data%State%MudSystem%iLoc)-data%State%MudSystem%NewDensity)< data%State%MudSystem%DensityMixTol) &
  438. .or. (data%State%ROP_Bit%RateOfPenetration>0. .and. data%State%MudSystem%Ann_CuttingMud%Array(data%State%MudSystem%iLoc)==1 .and. abs(data%State%MudSystem%Ann_Density%Array(data%State%MudSystem%iLoc)-data%State%MudSystem%NewDensity)< data%State%MudSystem%CuttingDensityMixTol) &
  439. .or. (data%State%ROP_Bit%RateOfPenetration>0. .and. data%State%MudSystem%Ann_CuttingMud%Array(data%State%MudSystem%iLoc)==0 .and. data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%iLoc) < 42.) ) then ! 1-Pockets are Merged
  440. data%State%MudSystem%Ann_Density%Array(data%State%MudSystem%iLoc)= (data%State%MudSystem%Ann_Density%Array(data%State%MudSystem%iLoc)*data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%iLoc)+data%State%MudSystem%NewDensity*data%State%MudSystem%NewVolume)/(data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%iLoc)+data%State%MudSystem%NewVolume)
  441. data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%iLoc)+data%State%MudSystem%NewVolume
  442. data%State%MudSystem%Ann_Mud_Forehead_X%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%BackheadX
  443. data%State%MudSystem%Ann_Mud_Forehead_section%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%MudSection
  444. data%State%MudSystem%Ann_Mud_Backhead_X%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%BackheadX
  445. data%State%MudSystem%Ann_Mud_Backhead_section%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%MudSection
  446. data%State%MudSystem%Ann_RemainedVolume_in_LastSection%Array(data%State%MudSystem%iLoc)= (0.0d0)
  447. data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Array(data%State%MudSystem%iLoc)= (0.0d0)
  448. !write(*,*) 'merge' ,'Ann_Volume%Array(1)=' , Ann_MudDischarged_Volume%Array(1)
  449. else ! 2-Merging conditions are not meeted, so new pocket
  450. call data%State%MudSystem%Ann_Density%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%NewDensity)
  451. call data%State%MudSystem%Ann_MudDischarged_Volume%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%NewVolume)
  452. call data%State%MudSystem%Ann_Mud_Forehead_X%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%BackheadX)
  453. call data%State%MudSystem%Ann_Mud_Forehead_section%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%MudSection)
  454. call data%State%MudSystem%Ann_Mud_Backhead_X%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%BackheadX)
  455. call data%State%MudSystem%Ann_Mud_Backhead_section%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%MudSection)
  456. call data%State%MudSystem%Ann_RemainedVolume_in_LastSection%AddTo (data%State%MudSystem%iLoc,0.0d0)
  457. call data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%AddTo (data%State%MudSystem%iLoc,0.0d0)
  458. call data%State%MudSystem%Ann_MudOrKick%AddTo (data%State%MudSystem%iLoc,0)
  459. call data%State%MudSystem%Ann_CuttingMud%AddTo (data%State%MudSystem%iLoc,0)
  460. !write(*,*) 'd) annLength=' , Ann_Density%Length()
  461. !write(*,*) 'new' ,'Ann_Volume%Array(1)=' , Ann_MudDischarged_Volume%Array(1)
  462. endif
  463. endif
  464. endif
  465. !========================Tripping In - End====================
  466. !========================Drilling Mode========================
  467. if (data%State%ROP_Bit%RateOfPenetration>0. .and. data%State%MudSystem%DeltaVolumeOp>0.0) then ! trip in mode(loole paeen) DrillingMode== .true.
  468. !write(*,*) 'Drilling Mode'
  469. !if ( MudIsChanged== .true. ) then
  470. ! call RemoveAnnulusMudArrays(iLoc)
  471. !endif
  472. !write(*,*) 'before' ,'Ann_Volume%Array(1)=' , Ann_MudDischarged_Volume%Array(1)
  473. !data%State%MudSystem%NewDensity= data%State%MudSystem%St_Density%Last() * data%State%MudSystem%AnnulusFlowRate + 141.4296E-4*data%State%ROP_Bit%RateOfPenetration*data%State%ROP_Spec%DiameterOfBit**2)/(data%State%MudSystem%AnnulusFlowRate+6.7995E-4*data%State%ROP_Bit%RateOfPenetration*Diameter_of_Bit**2)
  474. data%State%MudSystem%NewDensity=data%State%MudSystem%St_Density%Last()
  475. !NewVolume= ((data%State%MudSystem%AnnulusFlowRate/60.0d0)*DeltaT_Mudline)+DeltaVolumeOp
  476. !!! Density in ppg, flow rate in gpm, ROP in ft/s, bit diameter in inch
  477. do imud=1, data%State%MudSystem%Op_MudDischarged_Volume%Length()
  478. if ( data%State%MudSystem%Op_MudOrKick%Array(imud) == 0 ) then
  479. data%State%MudSystem%Op_Density%Array(imud)= data%State%MudSystem%NewDensity
  480. endif
  481. enddo
  482. if (data%State%MudSystem%Op_Kick_Saved_Volume > 0.0 .and. data%State%MudSystem%Ann_MudOrKick%First() == 0) then
  483. write(*,*) 'Kick influx enters Annulus first time'
  484. !write(*,*) 'Saved_Op_MudOrKick=',Saved_Op_MudOrKick
  485. call data%State%MudSystem%Ann_Density%AddToFirst (data%State%MudSystem%Op_KickSaved_Density)
  486. call data%State%MudSystem%Ann_MudDischarged_Volume%AddToFirst (data%State%MudSystem%Op_Kick_Saved_Volume)
  487. call data%State%MudSystem%Ann_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(data%State%F_Counts%StringIntervalCounts+1))
  488. call data%State%MudSystem%Ann_Mud_Forehead_section%AddToFirst (data%State%F_Counts%StringIntervalCounts+1)
  489. call data%State%MudSystem%Ann_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(data%State%F_Counts%StringIntervalCounts+1))
  490. call data%State%MudSystem%Ann_Mud_Backhead_section%AddToFirst (data%State%F_Counts%StringIntervalCounts+1)
  491. call data%State%MudSystem%Ann_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
  492. call data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
  493. call data%State%MudSystem%Ann_MudOrKick%AddToFirst (data%State%MudSystem%Saved_Op_MudOrKick) !<<<<<<<<
  494. call data%State%MudSystem%Ann_CuttingMud%AddToFirst (0)
  495. elseif (data%State%MudSystem%Op_Kick_Saved_Volume > 0.0 .and. data%State%MudSystem%Ann_MudOrKick%First() /= 0) then
  496. data%State%MudSystem%Ann_MudDischarged_Volume%Array(1)= data%State%MudSystem%Ann_MudDischarged_Volume%Array(1) + data%State%MudSystem%Op_Kick_Saved_Volume
  497. endif
  498. if (data%State%MudSystem%Op_Saved_MudDischarged_Volume> 0.0) then
  499. !write(*,*) 'Op_Saved_Mud added'
  500. data%State%MudSystem%NewDensity= data%State%MudSystem%NewDensity !(drilling density)
  501. data%State%MudSystem%NewVolume= data%State%MudSystem%Op_Saved_MudDischarged_Volume + data%State%MudSystem%DeltaVolumeOp ! (DeltaVolumeOp: for Cuttings Volume)
  502. !write(*,*) 'NewVolume=' , NewVolume
  503. !write(*,*) 'iloc=' , iloc,'Ann_MudDischarged_Volume%Array(1)=' , Ann_MudDischarged_Volume%Array(1)
  504. if ( (data%State%MudSystem%Ann_CuttingMud%Array(data%State%MudSystem%iLoc)==1 .and. abs(data%State%MudSystem%Ann_Density%Array(data%State%MudSystem%iLoc)-data%State%MudSystem%NewDensity)< data%State%MudSystem%CuttingDensityMixTol ) &
  505. .or. (data%State%MudSystem%Ann_CuttingMud%Array(data%State%MudSystem%iLoc)==0 .and. data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%iLoc) < 42.) ) then ! 1-Pockets are Merged
  506. data%State%MudSystem%Ann_Density%Array(data%State%MudSystem%iLoc)= (data%State%MudSystem%Ann_Density%Array(data%State%MudSystem%iLoc)*data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%iLoc)+data%State%MudSystem%NewDensity*data%State%MudSystem%NewVolume)/(data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%iLoc)+data%State%MudSystem%NewVolume)
  507. data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%iLoc)+data%State%MudSystem%NewVolume
  508. data%State%MudSystem%Ann_Mud_Forehead_X%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%BackheadX
  509. data%State%MudSystem%Ann_Mud_Forehead_section%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%MudSection
  510. data%State%MudSystem%Ann_Mud_Backhead_X%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%BackheadX
  511. data%State%MudSystem%Ann_Mud_Backhead_section%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%MudSection
  512. data%State%MudSystem%Ann_RemainedVolume_in_LastSection%Array(data%State%MudSystem%iLoc)= (0.0d0)
  513. data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Array(data%State%MudSystem%iLoc)= (0.0d0)
  514. data%State%MudSystem%Ann_CuttingMud%Array(data%State%MudSystem%iLoc)= 1
  515. !write(*,*) 'merge' ,'Ann_Volume%Array(1)=' , Ann_MudDischarged_Volume%Array(1)
  516. else ! 2-Merging conditions are not meeted, so new pocket
  517. !write(*,*) 'before e) ', iloc, Ann_Density%Array(iLoc),data%State%MudSystem%NewDensity
  518. !write(*,*) 'before e) Ann_MudDischarged_Volume%Array(iLoc)=' , Ann_MudDischarged_Volume%Array(iLoc)
  519. call data%State%MudSystem%Ann_Density%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%NewDensity)
  520. call data%State%MudSystem%Ann_MudDischarged_Volume%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%NewVolume)
  521. call data%State%MudSystem%Ann_Mud_Forehead_X%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%BackheadX)
  522. call data%State%MudSystem%Ann_Mud_Forehead_section%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%MudSection)
  523. call data%State%MudSystem%Ann_Mud_Backhead_X%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%BackheadX)
  524. call data%State%MudSystem%Ann_Mud_Backhead_section%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%MudSection)
  525. call data%State%MudSystem%Ann_RemainedVolume_in_LastSection%AddTo (data%State%MudSystem%iLoc,0.0d0)
  526. call data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%AddTo (data%State%MudSystem%iLoc,0.0d0)
  527. call data%State%MudSystem%Ann_MudOrKick%AddTo (data%State%MudSystem%iLoc,0)
  528. call data%State%MudSystem%Ann_CuttingMud%AddTo (data%State%MudSystem%iLoc,1) ! 1= cutting 0= mud
  529. !write(*,*) 'new' ,'Ann_Volume%Array(1)=' , Ann_MudDischarged_Volume%Array(1)
  530. !write(*,*) 'e) annLength=' , Ann_Density%Length()
  531. endif
  532. endif
  533. endif
  534. !===================================================================
  535. !write(*,*) 'after add to ann=='
  536. !
  537. ! do imud=1, Op_MudDischarged_Volume%Length()
  538. ! write(*,*) 'Op:', imud, Op_MudDischarged_Volume%Array(imud), Op_Density%Array(imud) ,Op_MudOrKick%Array(imud)
  539. ! enddo
  540. !
  541. ! do imud=1, Ann_MudDischarged_Volume%Length()
  542. ! write(*,*) 'Ann:', imud, Ann_MudDischarged_Volume%Array(imud), Ann_Density%Array(imud) ,Ann_MudOrKick%Array(imud)
  543. ! enddo
  544. !
  545. !write(*,*) '==after add to ann'
  546. data%State%MudSystem%NewVolume= ((data%State%MudSystem%AnnulusFlowRate/60.d0)*data%State%MudSystem%DeltaT_Mudline) - data%State%MudSystem%Op_Saved_MudDischarged_Volume
  547. if (data%State%MudSystem%iLoc==2 .and. data%State%MudSystem%Op_MudOrKick%Last()==0 .and. data%State%MudSystem%NewVolume > 0.d0 ) then ! for avoid kick separation
  548. !write(*,*) 'avoid kick separation'
  549. data%State%MudSystem%NewDensity= data%State%MudSystem%Op_MudSaved_Density
  550. call RemoveOpMudArrays(data%State%MudSystem%Op_Density%Length()) ! mud here is removed and then will be added to iloc=2 in Ann
  551. if ( data%State%MudSystem%Ann_MudDischarged_Volume%Array(1) > ((data%State%MudSystem%AnnulusFlowRate/60.d0)*data%State%MudSystem%DeltaT_Mudline)- data%State%MudSystem%Op_Saved_MudDischarged_Volume) then! 1st in Ann = kick
  552. !write(*,*) 'mode1'
  553. data%State%MudSystem%Ann_MudDischarged_Volume%Array(1)= data%State%MudSystem%Ann_MudDischarged_Volume%Array(1) - (((data%State%MudSystem%AnnulusFlowRate/60.d0)*data%State%MudSystem%DeltaT_Mudline) -data%State%MudSystem%Op_Saved_MudDischarged_Volume)
  554. data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_Density%Length())= data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_Density%Length())+ (((data%State%MudSystem%AnnulusFlowRate/60.d0)*data%State%MudSystem%DeltaT_Mudline) - data%State%MudSystem%Op_Saved_MudDischarged_Volume) !kick
  555. else
  556. call RemoveAnnulusMudArrays(1) !kick is removed
  557. data%State%MudSystem%iLoc= 1
  558. data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_Density%Length())= data%State%MudSystem%Op_MudDischarged_Volume%Array(data%State%MudSystem%Op_Density%Length())+ (((data%State%MudSystem%AnnulusFlowRate/60.d0)*data%State%MudSystem%DeltaT_Mudline) - data%State%MudSystem%Op_Saved_MudDischarged_Volume)
  559. !write(*,*) 'mode2'
  560. ! including a little expand
  561. endif
  562. if ((data%State%ROP_Bit%RateOfPenetration==0 .and. abs(data%State%MudSystem%Ann_Density%Array(data%State%MudSystem%iLoc)-data%State%MudSystem%NewDensity)< data%State%MudSystem%DensityMixTol) &
  563. .or. (data%State%ROP_Bit%RateOfPenetration>0. .and. data%State%MudSystem%Ann_CuttingMud%Array(data%State%MudSystem%iLoc)==1 .and. abs(data%State%MudSystem%Ann_Density%Array(data%State%MudSystem%iLoc)-data%State%MudSystem%NewDensity)< data%State%MudSystem%CuttingDensityMixTol) &
  564. .or. (data%State%ROP_Bit%RateOfPenetration>0. .and. data%State%MudSystem%Ann_CuttingMud%Array(data%State%MudSystem%iLoc)==0 .and. data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%iLoc) < 42.) ) then ! 1-Pockets are Merged
  565. data%State%MudSystem%Ann_Density%Array(data%State%MudSystem%iLoc)= (data%State%MudSystem%Ann_Density%Array(data%State%MudSystem%iLoc)*data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%iLoc)+data%State%MudSystem%NewDensity*data%State%MudSystem%NewVolume)/(data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%iLoc)+data%State%MudSystem%NewVolume)
  566. data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%iLoc)+data%State%MudSystem%NewVolume
  567. data%State%MudSystem%Ann_Mud_Forehead_X%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%BackheadX
  568. data%State%MudSystem%Ann_Mud_Forehead_section%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%MudSection
  569. data%State%MudSystem%Ann_Mud_Backhead_X%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%BackheadX
  570. data%State%MudSystem%Ann_Mud_Backhead_section%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%MudSection
  571. data%State%MudSystem%Ann_RemainedVolume_in_LastSection%Array(data%State%MudSystem%iLoc)= (0.0d0)
  572. data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Array(data%State%MudSystem%iLoc)= (0.0d0)
  573. else ! 2-Merging conditions are not meeted, so new pocket
  574. call data%State%MudSystem%Ann_Density%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%NewDensity)
  575. call data%State%MudSystem%Ann_MudDischarged_Volume%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%NewVolume)
  576. call data%State%MudSystem%Ann_Mud_Forehead_X%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%BackheadX)
  577. call data%State%MudSystem%Ann_Mud_Forehead_section%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%MudSection)
  578. call data%State%MudSystem%Ann_Mud_Backhead_X%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%BackheadX)
  579. call data%State%MudSystem%Ann_Mud_Backhead_section%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%MudSection)
  580. call data%State%MudSystem%Ann_RemainedVolume_in_LastSection%AddTo (data%State%MudSystem%iLoc,0.0d0)
  581. call data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%AddTo (data%State%MudSystem%iLoc,0.0d0)
  582. call data%State%MudSystem%Ann_MudOrKick%AddTo (data%State%MudSystem%iLoc,0)
  583. call data%State%MudSystem%Ann_CuttingMud%AddTo (data%State%MudSystem%iLoc,0)
  584. !write(*,*) 'd) annLength=' , Ann_Density%Length()
  585. endif
  586. endif
  587. !===================================================================
  588. if( data%State%MudSystem%Op_MudOrKick%Last() == 1 .and. data%State%MudSystem%Ann_MudOrKick%First() == 0 ) then
  589. write(*,*) '***error2****=='
  590. write(*,*) 'Op_Kick_Saved_Volume,Op_Saved_MudDischarged_Volume=' , data%State%MudSystem%Op_Kick_Saved_Volume,data%State%MudSystem%Op_Saved_MudDischarged_Volume
  591. write(*,*) 'after add to ann=='
  592. do imud=1, data%State%MudSystem%Op_MudDischarged_Volume%Length()
  593. write(*,*) 'Op:', imud, data%State%MudSystem%Op_MudDischarged_Volume%Array(imud), data%State%MudSystem%Op_Density%Array(imud) ,data%State%MudSystem%Op_MudOrKick%Array(imud)
  594. enddo
  595. do imud=1, data%State%MudSystem%Ann_MudDischarged_Volume%Length()
  596. write(*,*) 'Ann:', imud, data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud), data%State%MudSystem%Ann_Density%Array(imud) ,data%State%MudSystem%Ann_MudOrKick%Array(imud)
  597. enddo
  598. write(*,*) '==after add to ann'
  599. write(*,*) 'NewVolume,Op_MudOrKick%Last=' , data%State%MudSystem%NewVolume,data%State%MudSystem%Op_MudOrKick%Last()
  600. write(*,*) '==***error2****'
  601. endif
  602. !=============== save Ann Mud data to transfer to the ChokeLine enterance
  603. data%State%MudSystem%AnnMudVolumeSum= 0.d0
  604. !Ann_MudSaved_Density= 0.d0
  605. !Ann_KickSaved_Density= 0.d0
  606. data%State%MudSystem%Ann_Saved_MudDischarged_Volume= 0.d0
  607. data%State%MudSystem%Ann_Kick_Saved_Volume= 0.d0
  608. data%State%MudSystem%Saved_Ann_MudOrKick= 0
  609. data%State%MudSystem%Ann_to_Choke_2mud= .false.
  610. do imud=1, data%State%MudSystem%Ann_MudDischarged_Volume%Length()
  611. data%State%MudSystem%AnnMudVolumeSum= data%State%MudSystem%AnnMudVolumeSum + data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud)
  612. if ( data%State%MudSystem%AnnMudVolumeSum > sum(data%State%MudSystem%PipeSection_VolumeCapacity(data%State%F_Counts%StringIntervalCounts+1:data%State%MudSystem%NoPipeSections)) ) then
  613. IF (data%State%MudSystem%Ann_MudOrKick%Array(imud) == 0) THEN
  614. data%State%MudSystem%Ann_MudSaved_Density = data%State%MudSystem%Ann_Density%Array(imud)
  615. data%State%MudSystem%Ann_Saved_MudDischarged_Volume = data%State%MudSystem%AnnMudVolumeSum - sum(data%State%MudSystem%PipeSection_VolumeCapacity(data%State%F_Counts%StringIntervalCounts+1:data%State%MudSystem%NoPipeSections))
  616. ELSEIF (data%State%MudSystem%Ann_MudOrKick%Array(imud) > 0 .AND. data%State%MudSystem%Ann_MudOrKick%Array(imud) <100) THEN ! 104= AIR
  617. data%State%MudSystem%Ann_Kick_Saved_Volume = data%State%MudSystem%AnnMudVolumeSum - sum(data%State%MudSystem%PipeSection_VolumeCapacity(data%State%F_Counts%StringIntervalCounts+1:data%State%MudSystem%NoPipeSections))
  618. data%State%MudSystem%Saved_Ann_MudOrKick= data%State%MudSystem%Ann_MudOrKick%Array (imud)
  619. data%State%MudSystem%Ann_KickSaved_Density= data%State%MudSystem%Ann_Density%Array(imud)
  620. END IF
  621. do ii= imud + 1, data%State%MudSystem%Ann_MudDischarged_Volume%Length()
  622. IF (data%State%MudSystem%Ann_MudOrKick%Array(ii) == 0) THEN
  623. data%State%MudSystem%Ann_MudSaved_Density = ((data%State%MudSystem%Ann_MudSaved_Density * data%State%MudSystem%Ann_Saved_MudDischarged_Volume) + (data%State%MudSystem%Ann_Density%Array(ii) * data%State%MudSystem%Ann_MudDischarged_Volume%Array(ii))) / (data%State%MudSystem%Ann_Saved_MudDischarged_Volume + data%State%MudSystem%Ann_MudDischarged_Volume%Array(ii))
  624. data%State%MudSystem%Ann_Saved_MudDischarged_Volume = data%State%MudSystem%Ann_Saved_MudDischarged_Volume + data%State%MudSystem%Ann_MudDischarged_Volume%Array(ii)
  625. data%State%MudSystem%Ann_to_Choke_2mud= .true.
  626. ELSEIF (data%State%MudSystem%Ann_MudOrKick%Array(ii) > 0 .AND. data%State%MudSystem%Ann_MudOrKick%Array(ii) <100) THEN ! 104= AIR
  627. data%State%MudSystem%Ann_Kick_Saved_Volume = data%State%MudSystem%Ann_Kick_Saved_Volume + data%State%MudSystem%Ann_MudDischarged_Volume%Array(ii)
  628. data%State%MudSystem%Saved_Ann_MudOrKick= data%State%MudSystem%Ann_MudOrKick%Array (ii)
  629. data%State%MudSystem%Ann_KickSaved_Density= data%State%MudSystem%Ann_Density%Array(ii)
  630. END IF
  631. enddo
  632. !WRITE (*,*) 'Ann_Saved_Mud_Volume, Ann_Kick_Saved_Volume', Ann_Saved_MudDischarged_Volume, Ann_Kick_Saved_Volume
  633. exit
  634. endif
  635. enddo
  636. data%State%MudSystem%Ann_Saved_MudDischarged_Volume_Final= data%State%MudSystem%Ann_Saved_MudDischarged_Volume !+ Ann_Kick_Saved_Volume
  637. data%State%MudSystem%Ann_Kick_Saved_Volume_Final= data%State%MudSystem%Ann_Kick_Saved_Volume
  638. IF (data%State%MudSystem%WellHeadIsOpen) data%State%MudSystem%MudVolume_InjectedFromAnn = data%State%MudSystem%Ann_Saved_MudDischarged_Volume_Final -((data%State%MudSystem%Qlost/60.0d0)*data%State%MudSystem%DeltaT_Mudline)
  639. !WRITE (*,*) 'data%State%MudSystem%MudVolume_InjectedFromAnn=', data%State%MudSystem%MudVolume_InjectedFromAnn
  640. !======================================================================
  641. !write(*,*) 'c)Ann_Mud sum=' , sum(Ann_MudDischarged_Volume%Array(:))
  642. !write(*,*) 'Ann cap=' , sum(PipeSection_VolumeCapacity(F_StringIntervalCounts+1:NoPipeSections))
  643. !write(*,*) 'Ann_Saved_Mud=' , Ann_Saved_MudDischarged_Volume
  644. data%State%MudSystem%total_injected = data%State%MudSystem%total_injected + data%State%MudSystem%MudVolume_InjectedFromAnn
  645. if (data%Equipments%ChokeControlPanel%ChokePanelStrokeResetSwitch == 1) then
  646. data%State%MudSystem%total_injected= 0.
  647. endif
  648. !write(*,*) ' total injected-tripin =' , total_injected
  649. !write(*,*) 'injected-tripin =' , data%State%MudSystem%MudVolume_InjectedFromAnn
  650. !======================== Annulus ====================
  651. !MudIsChanged= .false.
  652. imud= 0
  653. do while (imud < data%State%MudSystem%Ann_Mud_Forehead_X%Length())
  654. imud = imud + 1
  655. if (imud> 1) then
  656. data%State%MudSystem%Ann_Mud_Backhead_X%Array(imud)= data%State%MudSystem%Ann_Mud_Forehead_X%Array(imud-1)
  657. data%State%MudSystem%Ann_Mud_Backhead_section%Array(imud)= data%State%MudSystem%Ann_Mud_Forehead_section%Array(imud-1)
  658. endif
  659. ! <<< Fracture Shoe Lost
  660. IF ( data%State%MudSystem%ShoeLost .and. data%Configuration%Shoe%ShoeDepth < data%State%MudSystem%Ann_Mud_Backhead_X%Array(imud) .and. data%Configuration%Shoe%ShoeDepth >= data%State%MudSystem%Ann_Mud_Forehead_X%Array(imud) ) then
  661. !write(*,*) 'ShoeLost imud,AnnVolume(imud), VolumeLost:' , imud,Ann_MudDischarged_Volume%Array(imud), (( Qlost/60.0d0)*DeltaT_Mudline)
  662. data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud)= data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud)-((data%State%MudSystem%Qlost/60.0d0)*data%State%MudSystem%DeltaT_Mudline) !(gal)
  663. if (data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud) < 0.0) then
  664. !write(*,*) 'mud is removed by shoe lost, imud=' , imud
  665. call RemoveAnnulusMudArrays(imud)
  666. imud= imud-1
  667. cycle
  668. endif
  669. ENDIF
  670. ! Fracture Shoe Lost >>>
  671. data%State%MudSystem%DirectionCoef= (data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%Ann_Mud_Backhead_section%Array(imud))-data%State%MudSystem%Xstart_PipeSection(data%State%MudSystem%Ann_Mud_Backhead_section%Array(imud))) &
  672. / ABS(data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%Ann_Mud_Backhead_section%Array(imud))-data%State%MudSystem%Xstart_PipeSection(data%State%MudSystem%Ann_Mud_Backhead_section%Array(imud)))
  673. ! +1 for string , -1 for annulus
  674. data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Array(imud)= data%State%MudSystem%DirectionCoef* (data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%Ann_Mud_Backhead_section%Array(imud))- data%State%MudSystem%Ann_Mud_Backhead_X%Array(imud))* &
  675. data%State%MudSystem%Area_PipeSectionFt(data%State%MudSystem%Ann_Mud_Backhead_section%Array(imud)) !(ft^3)
  676. data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Array(imud)= data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Array(imud)* 7.48051948d0 ! ft^3 to gal
  677. if ( data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud) <= data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Array(imud)) then
  678. data%State%MudSystem%Ann_Mud_Forehead_section%Array(imud)= data%State%MudSystem%Ann_Mud_Backhead_section%Array(imud)
  679. data%State%MudSystem%Ann_Mud_Forehead_X%Array(imud)= data%State%MudSystem%Ann_Mud_Backhead_X%Array(imud)+ data%State%MudSystem%DirectionCoef*(data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud)/7.48051948d0)/data%State%MudSystem%Area_PipeSectionFt(data%State%MudSystem%Ann_Mud_Backhead_section%Array(imud))
  680. ! 7.48 is for gal to ft^3
  681. else
  682. data%State%MudSystem%isection= data%State%MudSystem%Ann_Mud_Backhead_section%Array(imud)+1
  683. data%State%MudSystem%Ann_RemainedVolume_in_LastSection%Array(imud)= data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud)- data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Array(imud)
  684. do
  685. if (data%State%MudSystem%isection > data%State%MudSystem%NoPipeSections) then ! last pipe section(well exit)
  686. data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud)= data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud)- data%State%MudSystem%Ann_RemainedVolume_in_LastSection%Array(imud)
  687. data%State%MudSystem%Ann_Mud_Forehead_X%Array(imud)= data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%NoPipeSections)
  688. data%State%MudSystem%Ann_Mud_Forehead_section%Array(imud)= data%State%MudSystem%NoPipeSections
  689. if (data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud)<= 0.0d0) then ! imud is completely exited form the well
  690. !write(*,*) 'remove******'
  691. call RemoveAnnulusMudArrays(imud)
  692. endif
  693. exit
  694. endif
  695. data%State%MudSystem%xx= data%State%MudSystem%Ann_RemainedVolume_in_LastSection%Array(imud)/ data%State%MudSystem%PipeSection_VolumeCapacity(data%State%MudSystem%isection) !(gal)
  696. if (data%State%MudSystem%xx<= 1.0) then
  697. data%State%MudSystem%Ann_Mud_Forehead_section%Array(imud)= data%State%MudSystem%isection
  698. data%State%MudSystem%Ann_Mud_Forehead_X%Array(imud)= (data%State%MudSystem%xx * (data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%isection)- data%State%MudSystem%Xstart_PipeSection(data%State%MudSystem%isection)))+ data%State%MudSystem%Xstart_PipeSection(data%State%MudSystem%isection)
  699. exit
  700. else
  701. data%State%MudSystem%Ann_RemainedVolume_in_LastSection%Array(imud)= data%State%MudSystem%Ann_RemainedVolume_in_LastSection%Array(imud)- data%State%MudSystem%PipeSection_VolumeCapacity(data%State%MudSystem%isection)
  702. data%State%MudSystem%isection= data%State%MudSystem%isection+ 1
  703. endif
  704. enddo
  705. endif
  706. ! write(*,*) 'imud=' , imud
  707. !write(*,*) 'Pinter4 **Ann_Length()=' , Ann_Mud_Forehead_X%Length()
  708. ! write(*,*) 'Ann_Density%Array (imud)=' , Ann_Density%Array (imud)
  709. !
  710. !
  711. !write(*,*) imud,'Ann_Mud_Forehead_X%Array(imud)=' , Ann_Mud_Forehead_X%Array(imud)
  712. !if (Ann_Mud_Forehead_X%Array(imud) < Xend_PipeSection(NoPipeSections)) then
  713. ! Ann_Mud_Forehead_X%Array(imud) = Xend_PipeSection(NoPipeSections) ! for error preventing
  714. !endif
  715. !write(*,*) imud, 'Ann_MudDischarged_Volume%Array(imud)=' , Ann_MudDischarged_Volume%Array(imud), Ann_Density%Array(imud)
  716. enddo
  717. if (data%State%MudSystem%Ann_Mud_Forehead_X%Last() < data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%NoPipeSections)) then
  718. data%State%MudSystem%Ann_Mud_Forehead_X%Array(data%State%MudSystem%Ann_Mud_Forehead_X%Length()) = data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%NoPipeSections) ! for error preventing
  719. endif
  720. !========================ANNULUS END=================
  721. !write(*,*) 'sum(Ann_MudDischarged_Volume%Array())=' , sum(Ann_MudDischarged_Volume%Array(:))
  722. !=========================================================
  723. !write(*,*) 'before======2'
  724. !
  725. ! do imud=1, Op_MudDischarged_Volume%Length()
  726. ! write(*,*) 'Op:', imud, Op_MudDischarged_Volume%Array(imud), Op_Density%Array(imud) ,Op_MudOrKick%Array(imud)
  727. ! enddo
  728. !write(*,*) '2======before'
  729. !========================Bottom Hole=================
  730. imud=0
  731. do while (imud < data%State%MudSystem%Op_Mud_Forehead_X%Length())
  732. imud = imud + 1
  733. if (imud> 1) then
  734. data%State%MudSystem%Op_Mud_Backhead_X%Array(imud)= data%State%MudSystem%Op_Mud_Forehead_X%Array(imud-1)
  735. data%State%MudSystem%Op_Mud_Backhead_section%Array(imud)= data%State%MudSystem%Op_Mud_Forehead_section%Array(imud-1)
  736. endif
  737. !write(*,*) 'imud**=' , imud
  738. data%State%MudSystem%DirectionCoef= (data%State%MudSystem%Xend_OpSection(data%State%MudSystem%Op_Mud_Backhead_section%Array(imud))-data%State%MudSystem%Xstart_OpSection(data%State%MudSystem%Op_Mud_Backhead_section%Array(imud))) &
  739. / ABS(data%State%MudSystem%Xend_OpSection(data%State%MudSystem%Op_Mud_Backhead_section%Array(imud))-data%State%MudSystem%Xstart_OpSection(data%State%MudSystem%Op_Mud_Backhead_section%Array(imud)))
  740. ! +1 for string , -1 for annulus
  741. data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%Array(imud)= data%State%MudSystem%DirectionCoef* (data%State%MudSystem%Xend_OpSection(data%State%MudSystem%Op_Mud_Backhead_section%Array(imud))- data%State%MudSystem%Op_Mud_Backhead_X%Array(imud))* &
  742. data%State%MudSystem%Area_OpSectionFt(data%State%MudSystem%Op_Mud_Backhead_section%Array(imud)) !(ft^3)
  743. data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%Array(imud)= data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%Array(imud)* 7.48051948d0 ! ft^3 to gal
  744. !write(*,*) ' Op_EmptyVolume_inBackheadLocation%Array(1) =' , Op_EmptyVolume_inBackheadLocation%Array(1)
  745. if ( data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%Array(1) < 0.0) CALL ErrorStop1 ('Negative Empty volume')
  746. if ( data%State%MudSystem%Op_MudDischarged_Volume%Array(imud) <= data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%Array(imud)) then
  747. data%State%MudSystem%Op_Mud_Forehead_section%Array(imud)= data%State%MudSystem%Op_Mud_Backhead_section%Array(imud)
  748. data%State%MudSystem%Op_Mud_Forehead_X%Array(imud)= data%State%MudSystem%Op_Mud_Backhead_X%Array(imud)+ data%State%MudSystem%DirectionCoef*(data%State%MudSystem%Op_MudDischarged_Volume%Array(imud)/7.48051948d0)/data%State%MudSystem%Area_OpSectionFt(data%State%MudSystem%Op_Mud_Backhead_section%Array(imud))
  749. ! 7.48 is for gal to ft^3
  750. else
  751. data%State%MudSystem%isection= data%State%MudSystem%Op_Mud_Backhead_section%Array(imud)+1
  752. data%State%MudSystem%Op_RemainedVolume_in_LastSection%Array(imud)= data%State%MudSystem%Op_MudDischarged_Volume%Array(imud)- data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%Array(imud)
  753. do
  754. if (data%State%MudSystem%isection > data%State%F_Counts%BottomHoleIntervalCounts) then ! last pipe section(well exit)
  755. !if( imud==1) KickDeltaVinAnnulus= Op_RemainedVolume_in_LastSection%Array(imud) ! Kick enters Annulus space
  756. data%State%MudSystem%Op_MudDischarged_Volume%Array(imud)= data%State%MudSystem%Op_MudDischarged_Volume%Array(imud)- data%State%MudSystem%Op_RemainedVolume_in_LastSection%Array(imud)
  757. data%State%MudSystem%Op_Mud_Forehead_X%Array(imud)= data%State%MudSystem%Xend_OpSection(data%State%F_Counts%BottomHoleIntervalCounts)
  758. data%State%MudSystem%Op_Mud_Forehead_section%Array(imud)= data%State%F_Counts%BottomHoleIntervalCounts
  759. if (data%State%MudSystem%Op_MudDischarged_Volume%Array(imud)<= 0.0d0) then ! imud is completely exited form the well
  760. call RemoveOpMudArrays(imud)
  761. endif
  762. exit
  763. endif
  764. data%State%MudSystem%xx= data%State%MudSystem%Op_RemainedVolume_in_LastSection%Array(imud)/ data%State%MudSystem%OpSection_VolumeCapacity(data%State%MudSystem%isection) !(gal)
  765. if (data%State%MudSystem%xx<= 1.0) then
  766. data%State%MudSystem%Op_Mud_Forehead_section%Array(imud)= data%State%MudSystem%isection
  767. data%State%MudSystem%Op_Mud_Forehead_X%Array(imud)= (data%State%MudSystem%xx * (data%State%MudSystem%Xend_OpSection(data%State%MudSystem%isection)- data%State%MudSystem%Xstart_OpSection(data%State%MudSystem%isection)))+ data%State%MudSystem%Xstart_OpSection(data%State%MudSystem%isection)
  768. exit
  769. else
  770. data%State%MudSystem%Op_RemainedVolume_in_LastSection%Array(imud)= data%State%MudSystem%Op_RemainedVolume_in_LastSection%Array(imud)- data%State%MudSystem%OpSection_VolumeCapacity(data%State%MudSystem%isection)
  771. data%State%MudSystem%isection= data%State%MudSystem%isection+ 1
  772. endif
  773. enddo
  774. endif
  775. ! for OP remove:
  776. if (data%State%MudSystem%Op_Mud_Forehead_X%Array(imud)== data%State%MudSystem%Xend_OpSection(data%State%F_Counts%BottomHoleIntervalCounts)) then
  777. data%State%MudSystem%totalLength = data%State%MudSystem%Op_MudDischarged_Volume%Length()
  778. do while(imud < data%State%MudSystem%totalLength)
  779. !imud = imud + 1
  780. call RemoveOpMudArrays(data%State%MudSystem%totalLength)
  781. data%State%MudSystem%totalLength = data%State%MudSystem%totalLength - 1
  782. enddo
  783. exit !
  784. endif
  785. !if (Op_Mud_Forehead_X%Array(imud)== Xend_OpSection(data%State%F_Counts%BottomHoleIntervalCounts)) then
  786. ! totalLength = Op_MudDischarged_Volume%Length()
  787. ! do while(imud <= totalLength)
  788. !
  789. ! imud = imud + 1
  790. ! call RemoveOpMudArrays(imud)
  791. ! totalLength = totalLength - 1
  792. !
  793. !
  794. ! enddo
  795. !
  796. ! exit !
  797. !
  798. !endif
  799. enddo
  800. !write(*,*) 'OpSection_VolumeCapacity sum=' , sum(OpSection_VolumeCapacity(:))
  801. !========================Bottom Hole END=================
  802. !write(*,*) 'after sorting=='
  803. !
  804. ! do imud=1, Op_MudDischarged_Volume%Length()
  805. ! write(*,*) 'Op:', imud, Op_MudDischarged_Volume%Array(imud), Op_Density%Array(imud) ,Op_MudOrKick%Array(imud)
  806. ! enddo
  807. !
  808. ! do imud=1, Ann_MudDischarged_Volume%Length()
  809. ! write(*,*) 'Ann:', imud, Ann_MudDischarged_Volume%Array(imud), Ann_Density%Array(imud) ,Ann_MudOrKick%Array(imud)
  810. ! enddo
  811. !
  812. ! !
  813. ! !do imud=1, st_MudDischarged_Volume%Length()
  814. ! ! write(*,*) 'st:', imud, St_MudDischarged_Volume%Array(imud), St_Mud_Backhead_X%Array(imud) ,St_Mud_Forehead_X%Array(imud)
  815. ! !enddo
  816. !
  817. !write(*,*) '==after sorting'
  818. ! write(*,*) 'after sorting st=='
  819. !
  820. ! do imud=1, st_MudDischarged_Volume%Length()
  821. ! write(*,*) 'st-plot:', imud, St_MudDischarged_Volume%Array(imud), St_Mud_Backhead_X%Array(imud) ,St_Mud_Forehead_X%Array(imud)data%State%MudSystem%St_Density%Array(imud)
  822. ! enddo
  823. !
  824. !write(*,*) '==after sorting st'
  825. !write(*,*) '**Ann_Kick_Saved_Final,Mud_InjectedFromAnn' , Ann_Kick_Saved_Volume_Final,data%State%MudSystem%MudVolume_InjectedFromAnn
  826. end subroutine Pump_and_TripIn
  827. subroutine ChokeLineMud ! is called in subroutine CirculationCodeSelect
  828. Use GeoElements_FluidModule
  829. USE CMudPropertiesVariables
  830. USE MudSystemVARIABLES
  831. use SimulationVariables !@@@
  832. use SimulationVariables
  833. USE CHOKEVARIABLES
  834. !use ConfigurationVariables !@
  835. !use CDataDisplayConsole
  836. !@ use ConfigurationVariables , StandPipePressureDataDisplay=>StandPipePressure
  837. !use CManifolds
  838. use SimulationVariables !@
  839. USE CHOKEVARIABLES
  840. !use ConfigurationVariables !@
  841. !use CChokeManifoldVariables
  842. use SimulationVariables
  843. !use CTanks
  844. !@use ConfigurationVariables, TripTankVolume2 => data%Equipments%DrillingWatch%TripTankVolume, TripTankDensity2 => TripTankDensity
  845. USE sROP_Other_Variables
  846. USE sROP_Variables
  847. use KickVARIABLESModule
  848. use PressureDisplayVARIABLESModule
  849. Use CError
  850. Use , intrinsic :: IEEE_Arithmetic
  851. implicit none
  852. integer i,ii,error_occured
  853. error_occured = 0
  854. !write(*,*) 'begining chokeline=='
  855. !write(*,*) 'Ann last:', Ann_MudDischarged_Volume%Last(), Ann_Density%Last() ,Ann_MudOrKick%Last()
  856. !
  857. !do imud=1, ChokeLine_MudDischarged_Volume%Length()
  858. ! write(*,*) 'ChokeLine:', imud, ChokeLine_MudDischarged_Volume%Array(imud), ChokeLine_Density%Array(imud) ,ChokeLine_MudOrKick%Array(imud)
  859. !enddo
  860. !write(*,*) 'Ann_Kick_Saved_Volume_Final,data%State%MudSystem%MudVolume_InjectedFromAnn' , Ann_Kick_Saved_Volume_Final,data%State%MudSystem%MudVolume_InjectedFromAnn
  861. !write(*,*) 'begining chokeline=='
  862. data%State%MudSystem%ChokeLineFlowRate = data%State%MUD(4)%Q
  863. !WRITE (*,*) 'data%State%MUD(4)%Q', data%State%MUD(4)%Q
  864. if (data%State%MudSystem%NewPipeFilling == 0) then ! .or. UtubeFilling==0) then
  865. data%State%MudSystem%ChokeLineFlowRate= 0.
  866. endif
  867. do imud=1, data%State%MudSystem%ChokeLine_MudDischarged_Volume%Length()-2
  868. if ( data%State%MudSystem%ChokeLine_MudOrKick%Array(imud) ==1 .and. data%State%MudSystem%ChokeLine_MudOrKick%Array(imud+1) ==0 .and. data%State%MudSystem%ChokeLine_MudOrKick%Array(imud+2) ==1 ) then
  869. write(*,*) 'error_location is 1'
  870. error_occured = 1
  871. endif
  872. enddo
  873. !
  874. !do imud=1, st_MudDischarged_Volume%Length()
  875. ! write(*,*) 'st:', imud, St_MudDischarged_Volume%Array(imud), St_Mud_Backhead_X%Array(imud) ,St_Mud_Forehead_X%Array(imud)
  876. !enddo
  877. !========================CHOKE LINE ENTRANCE=================
  878. !if ( Ann_Kick_Saved_Volume > 0.0 .and. ( Ann_Saved_MudDischarged_Volume-((Qlost/60.0d0)*DeltaT_Mudline) ) == 0.0 ) then
  879. if ( data%State%MudSystem%Ann_Kick_Saved_Volume > 1.0e-5 .and. ( data%State%MudSystem%MudVolume_InjectedFromAnn ) <= 1.0e-5 ) then
  880. !WRITE (*,*) 'only kick enters to chokeline, Casing pressure = ', PressureGauges(2)
  881. if (data%State%MudSystem%ChokeLine_MudOrKick%First() == 0) then
  882. call data%State%MudSystem%ChokeLine_Density%AddToFirst (data%State%MudSystem%Ann_KickSaved_Density)
  883. call data%State%MudSystem%ChokeLine_MudDischarged_Volume%AddToFirst (0.d0)
  884. call data%State%MudSystem%ChokeLine_Mud_Forehead_X%AddToFirst (0.0d0)
  885. call data%State%MudSystem%ChokeLine_Mud_Forehead_section%AddToFirst (1)
  886. call data%State%MudSystem%ChokeLine_Mud_Backhead_X%AddToFirst (0.0d0)
  887. call data%State%MudSystem%ChokeLine_Mud_Backhead_section%AddToFirst (1)
  888. call data%State%MudSystem%ChokeLine_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
  889. call data%State%MudSystem%ChokeLine_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
  890. call data%State%MudSystem%ChokeLine_MudOrKick%AddToFirst (data%State%MudSystem%Saved_Ann_MudOrKick)
  891. data%State%MudSystem%ChokeLineDensity_Old= data%State%MudSystem%Ann_KickSaved_Density
  892. endif
  893. data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(1)= data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(1)+ data%State%MudSystem%Ann_Kick_Saved_Volume !(gal)
  894. endif
  895. do imud=1, data%State%MudSystem%ChokeLine_MudDischarged_Volume%Length()-2
  896. if ( data%State%MudSystem%ChokeLine_MudOrKick%Array(imud) ==1 .and. data%State%MudSystem%ChokeLine_MudOrKick%Array(imud+1) ==0 .and. data%State%MudSystem%ChokeLine_MudOrKick%Array(imud+2) ==1 ) then
  897. write(*,*) 'error_location is 2'
  898. error_occured = 1
  899. endif
  900. enddo
  901. !if ( Ann_Kick_Saved_Volume == 0.0 .and. ( Ann_Saved_MudDischarged_Volume - ((Qlost/60.0d0)*DeltaT_Mudline) ) > 0.0 ) then
  902. if ( data%State%MudSystem%Ann_Kick_Saved_Volume <= 1.0e-5 .and. data%State%MudSystem%MudVolume_InjectedFromAnn > 1.0e-5 ) then
  903. !WRITE (*,*) 'only mud enters to chokeline'
  904. if ((data%State%MudSystem%Ann_to_Choke_2mud == .false. .and. ABS(data%State%MudSystem%ChokeLineDensity_Old - data%State%MudSystem%Ann_MudSaved_Density) >= data%State%MudSystem%DensityMixTol) .or. data%State%MudSystem%ChokeLine_MudOrKick%First() /= 0) then ! new mud is pumped
  905. call data%State%MudSystem%ChokeLine_Density%AddToFirst (data%State%MudSystem%Ann_MudSaved_Density)
  906. call data%State%MudSystem%ChokeLine_MudDischarged_Volume%AddToFirst (0.0d0)
  907. call data%State%MudSystem%ChokeLine_Mud_Forehead_X%AddToFirst (0.0d0)
  908. call data%State%MudSystem%ChokeLine_Mud_Forehead_section%AddToFirst (1)
  909. call data%State%MudSystem%ChokeLine_Mud_Backhead_X%AddToFirst (0.0d0)
  910. call data%State%MudSystem%ChokeLine_Mud_Backhead_section%AddToFirst (1)
  911. call data%State%MudSystem%ChokeLine_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
  912. call data%State%MudSystem%ChokeLine_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
  913. call data%State%MudSystem%ChokeLine_MudOrKick%AddToFirst (0)
  914. data%State%MudSystem%ChokeLineDensity_Old= data%State%MudSystem%Ann_MudSaved_Density
  915. endif
  916. !ChokeLine_MudDischarged_Volume%Array(1)= ChokeLine_MudDischarged_Volume%Array(1)+ (Ann_Saved_MudDischarged_Volume - ((Qlost/60.0d0)*DeltaT_Mudline) ) !(gal)
  917. data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(1)= data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(1)+ (data%State%MudSystem%MudVolume_InjectedFromAnn) !(gal)
  918. endif
  919. do imud=1, data%State%MudSystem%ChokeLine_MudDischarged_Volume%Length()-2
  920. if ( data%State%MudSystem%ChokeLine_MudOrKick%Array(imud) ==1 .and. data%State%MudSystem%ChokeLine_MudOrKick%Array(imud+1) ==0 .and. data%State%MudSystem%ChokeLine_MudOrKick%Array(imud+2) ==1 ) then
  921. write(*,*) 'error_location is 3'
  922. error_occured = 1
  923. endif
  924. enddo
  925. !if ( Ann_Kick_Saved_Volume > 0.0 .and. (Ann_Saved_MudDischarged_Volume - ((Qlost/60.0d0)*DeltaT_Mudline) ) > 0.0 .and. ChokeLine_MudOrKick%First() /= 0 ) then
  926. if ( data%State%MudSystem%Ann_Kick_Saved_Volume > 1.0e-5 .and. (data%State%MudSystem%MudVolume_InjectedFromAnn) > 1.0e-5 .and. data%State%MudSystem%ChokeLine_MudOrKick%First() /= 0 ) then
  927. WRITE (*,*) 'Kick Enters Choke line Last Time'
  928. data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(1)= data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(1)+ data%State%MudSystem%Ann_Kick_Saved_Volume !(gal)
  929. call data%State%MudSystem%ChokeLine_Density%AddToFirst (data%State%MudSystem%Ann_MudSaved_Density)
  930. !call ChokeLine_MudDischarged_Volume%AddToFirst (Ann_Saved_MudDischarged_Volume - ((Qlost/60.0d0)*DeltaT_Mudline) )
  931. call data%State%MudSystem%ChokeLine_MudDischarged_Volume%AddToFirst (data%State%MudSystem%MudVolume_InjectedFromAnn)
  932. call data%State%MudSystem%ChokeLine_Mud_Forehead_X%AddToFirst (0.0d0)
  933. call data%State%MudSystem%ChokeLine_Mud_Forehead_section%AddToFirst (1)
  934. call data%State%MudSystem%ChokeLine_Mud_Backhead_X%AddToFirst (0.0d0)
  935. call data%State%MudSystem%ChokeLine_Mud_Backhead_section%AddToFirst (1)
  936. call data%State%MudSystem%ChokeLine_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
  937. call data%State%MudSystem%ChokeLine_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
  938. call data%State%MudSystem%ChokeLine_MudOrKick%AddToFirst (0)
  939. data%State%MudSystem%ChokeLineDensity_Old= data%State%MudSystem%Ann_MudSaved_Density
  940. !ELSE if ( Ann_Kick_Saved_Volume > 0.0 .and. ( Ann_Saved_MudDischarged_Volume - ((Qlost/60.0d0)*DeltaT_Mudline) ) > 0.0 .and. ChokeLine_MudOrKick%First() == 0 ) then
  941. ELSE if ( data%State%MudSystem%Ann_Kick_Saved_Volume > 1.0e-5 .and. ( data%State%MudSystem%MudVolume_InjectedFromAnn ) > 1.0e-5 .and. data%State%MudSystem%ChokeLine_MudOrKick%First() == 0 ) then
  942. WRITE (*,*) 'Kick Enters Choke line First Time'
  943. !ChokeLine_MudDischarged_Volume%Array(1)= ChokeLine_MudDischarged_Volume%Array(1)+ ( Ann_Saved_MudDischarged_Volume - ((Qlost/60.0d0)*DeltaT_Mudline) ) !(gal)
  944. data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(1)= data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(1)+ ( data%State%MudSystem%MudVolume_InjectedFromAnn ) !(gal)
  945. call data%State%MudSystem%ChokeLine_Density%AddToFirst (data%State%MudSystem%Ann_KickSaved_Density)
  946. call data%State%MudSystem%ChokeLine_MudDischarged_Volume%AddToFirst (data%State%MudSystem%Ann_Kick_Saved_Volume)
  947. call data%State%MudSystem%ChokeLine_Mud_Forehead_X%AddToFirst (0.0d0)
  948. call data%State%MudSystem%ChokeLine_Mud_Forehead_section%AddToFirst (1)
  949. call data%State%MudSystem%ChokeLine_Mud_Backhead_X%AddToFirst (0.0d0)
  950. call data%State%MudSystem%ChokeLine_Mud_Backhead_section%AddToFirst (1)
  951. call data%State%MudSystem%ChokeLine_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
  952. call data%State%MudSystem%ChokeLine_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
  953. call data%State%MudSystem%ChokeLine_MudOrKick%AddToFirst (data%State%MudSystem%Saved_Ann_MudOrKick)
  954. data%State%MudSystem%ChokeLineDensity_Old= data%State%MudSystem%Ann_KickSaved_Density
  955. endif
  956. do imud=1, data%State%MudSystem%ChokeLine_MudDischarged_Volume%Length()-2
  957. if ( data%State%MudSystem%ChokeLine_MudOrKick%Array(imud) ==1 .and. data%State%MudSystem%ChokeLine_MudOrKick%Array(imud+1) ==0 .and. data%State%MudSystem%ChokeLine_MudOrKick%Array(imud+2) ==1 ) then
  958. write(*,*) 'error_location is 4'
  959. error_occured = 1
  960. endif
  961. enddo
  962. if (error_occured == 1) then
  963. do imud=1, data%State%MudSystem%ChokeLine_MudDischarged_Volume%Length()
  964. write(*,*) 'ChokeLine:', imud, data%State%MudSystem%ChokeLine_Density%Array(imud) ,data%State%MudSystem%ChokeLine_MudOrKick%Array(imud)
  965. enddo
  966. endif
  967. !==========================================================
  968. !
  969. !write(*,*) 'after add chokeline=='
  970. !
  971. ! do imud=1, ChokeLine_MudDischarged_Volume%Length()
  972. ! write(*,*) 'ChokeLine:', imud, ChokeLine_MudDischarged_Volume%Array(imud), ChokeLine_Density%Array(imud) ,ChokeLine_MudOrKick%Array(imud)
  973. ! enddo
  974. !
  975. !write(*,*) 'after add chokeline=='
  976. !
  977. !
  978. !=============== save Choke Mud data==========================
  979. data%State%MudSystem%ChokeMudVolumeSum= 0.d0
  980. !Ann_MudSaved_Density= 0.d0
  981. !Ann_KickSaved_Density= 0.d0
  982. data%State%MudSystem%Choke_Saved_MudDischarged_Volume= 0.d0
  983. data%State%MudSystem%Choke_Kick_Saved_Volume= 0.d0
  984. data%State%MudSystem%Saved_Choke_MudOrKick= 0
  985. do imud=1, data%State%MudSystem%ChokeLine_MudDischarged_Volume%Length()
  986. data%State%MudSystem%ChokeMudVolumeSum= data%State%MudSystem%ChokeMudVolumeSum + data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(imud)
  987. if ( data%State%MudSystem%ChokeMudVolumeSum > data%State%MudSystem%ChokeLine_VolumeCapacity ) then
  988. IF (data%State%MudSystem%ChokeLine_MudOrKick%Array(imud) == 0) THEN
  989. data%State%MudSystem%Choke_MudSaved_Density = data%State%MudSystem%ChokeLine_Density%Array(imud)
  990. data%State%MudSystem%Choke_Saved_MudDischarged_Volume = data%State%MudSystem%ChokeMudVolumeSum - data%State%MudSystem%ChokeLine_VolumeCapacity
  991. ELSEIF (data%State%MudSystem%ChokeLine_MudOrKick%Array(imud) > 0 .AND. data%State%MudSystem%ChokeLine_MudOrKick%Array(imud) <100) THEN ! 104= AIR
  992. data%State%MudSystem%Choke_Kick_Saved_Volume = data%State%MudSystem%ChokeMudVolumeSum - data%State%MudSystem%ChokeLine_VolumeCapacity
  993. data%State%MudSystem%Saved_Choke_MudOrKick= data%State%MudSystem%ChokeLine_MudOrKick%Array (imud)
  994. data%State%MudSystem%Choke_KickSaved_Density= data%State%MudSystem%ChokeLine_Density%Array(imud)
  995. END IF
  996. do ii= imud + 1, data%State%MudSystem%ChokeLine_MudDischarged_Volume%Length()
  997. IF (data%State%MudSystem%ChokeLine_MudOrKick%Array(ii) == 0) THEN
  998. data%State%MudSystem%Choke_MudSaved_Density = ((data%State%MudSystem%Choke_MudSaved_Density * data%State%MudSystem%Choke_Saved_MudDischarged_Volume) + (data%State%MudSystem%ChokeLine_Density%Array(ii) * data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(ii))) / (data%State%MudSystem%Choke_Saved_MudDischarged_Volume + data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(ii))
  999. data%State%MudSystem%Choke_Saved_MudDischarged_Volume = data%State%MudSystem%Choke_Saved_MudDischarged_Volume + data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(ii)
  1000. ELSEIF (data%State%MudSystem%ChokeLine_MudOrKick%Array(ii) > 0 .AND. data%State%MudSystem%ChokeLine_MudOrKick%Array(ii) <100) THEN ! 104= AIR
  1001. data%State%MudSystem%Choke_Kick_Saved_Volume = data%State%MudSystem%Choke_Kick_Saved_Volume + data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(ii)
  1002. data%State%MudSystem%Saved_Choke_MudOrKick= data%State%MudSystem%ChokeLine_MudOrKick%Array (ii)
  1003. data%State%MudSystem%Choke_KickSaved_Density= data%State%MudSystem%ChokeLine_Density%Array(ii)
  1004. END IF
  1005. enddo
  1006. !WRITE (*,*) 'Choke_Saved_Mud_Volume, Choke_Kick_Saved_Volume', Choke_Saved_MudDischarged_Volume, Choke_Kick_Saved_Volume
  1007. exit ! exits do
  1008. endif
  1009. enddo
  1010. data%State%MudSystem%Choke_Saved_MudDischarged_Volume_Final= data%State%MudSystem%Choke_Saved_MudDischarged_Volume !+ Choke_Kick_Saved_Volume
  1011. data%State%MudSystem%Choke_Kick_Saved_Volume_Final= data%State%MudSystem%Choke_Kick_Saved_Volume
  1012. !======================================================================
  1013. !
  1014. !do imud=1, ChokeLine_MudDischarged_Volume%Length()
  1015. ! write(*,*) 'a)ChokeLine:', imud, ChokeLine_MudDischarged_Volume%Array(imud) ,ChokeLine_MudOrKick%Array(imud)
  1016. !enddo
  1017. !write(*,*) 'choke_Mud sum=' , sum(ChokeLine_MudDischarged_Volume%Array(:))
  1018. !write(*,*) 'choke_cap=' , ChokeLine_VolumeCapacity
  1019. !write(*,*) 'Choke_Saved_Mud=' , Choke_Saved_MudDischarged_Volume_Final
  1020. !write(*,*) 'Choke_Saved_Kick=' , Choke_Kick_Saved_Volume_Final
  1021. !========================Choke Line=================
  1022. imud=0
  1023. do while (imud < data%State%MudSystem%ChokeLine_Mud_Forehead_X%Length())
  1024. imud = imud + 1
  1025. if (imud> 1) then
  1026. data%State%MudSystem%ChokeLine_Mud_Backhead_X%Array(imud)= data%State%MudSystem%ChokeLine_Mud_Forehead_X%Array(imud-1)
  1027. data%State%MudSystem%ChokeLine_Mud_Backhead_section%Array(imud)= data%State%MudSystem%ChokeLine_Mud_Forehead_section%Array(imud-1)
  1028. endif
  1029. !DirectionCoef= (Xend_PipeSection(St_Mud_Backhead_section%Array(imud))-Xstart_PipeSection(St_Mud_Backhead_section%Array(imud))) &
  1030. ! / ABS(Xend_PipeSection(St_Mud_Backhead_section%Array(imud))-Xstart_PipeSection(St_Mud_Backhead_section%Array(imud)))
  1031. ! +1 for string , -1 for annulus
  1032. data%State%MudSystem%ChokeLine_EmptyVolume_inBackheadLocation%Array(imud)= (data%Configuration%BopStack%ChokeLineLength- data%State%MudSystem%ChokeLine_Mud_Backhead_X%Array(imud))* data%State%MudSystem%Area_ChokeLineFt !(ft^3)
  1033. data%State%MudSystem%ChokeLine_EmptyVolume_inBackheadLocation%Array(imud)= data%State%MudSystem%ChokeLine_EmptyVolume_inBackheadLocation%Array(imud)* 7.48051948d0 ! ft^3 to gal
  1034. if ( data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(imud) <= data%State%MudSystem%ChokeLine_EmptyVolume_inBackheadLocation%Array(imud)) then
  1035. data%State%MudSystem%ChokeLine_Mud_Forehead_section%Array(imud)= data%State%MudSystem%ChokeLine_Mud_Backhead_section%Array(imud)
  1036. data%State%MudSystem%ChokeLine_Mud_Forehead_X%Array(imud)= data%State%MudSystem%ChokeLine_Mud_Backhead_X%Array(imud)+ (data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(imud)/7.48051948d0)/data%State%MudSystem%Area_ChokeLineFt
  1037. ! 7.48 is for gal to ft^3
  1038. else
  1039. data%State%MudSystem%isection= data%State%MudSystem%ChokeLine_Mud_Backhead_section%Array(imud)+1
  1040. data%State%MudSystem%ChokeLine_RemainedVolume_in_LastSection%Array(imud)= data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(imud)- data%State%MudSystem%ChokeLine_EmptyVolume_inBackheadLocation%Array(imud)
  1041. do
  1042. if (data%State%MudSystem%isection > 1) then ! last pipe section(Chokeline exit)
  1043. data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(imud)= data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(imud)- data%State%MudSystem%ChokeLine_RemainedVolume_in_LastSection%Array(imud)
  1044. data%State%MudSystem%ChokeLine_Mud_Forehead_X%Array(imud)= data%Configuration%BopStack%ChokeLineLength
  1045. data%State%MudSystem%ChokeLine_Mud_Forehead_section%Array(imud)= 1
  1046. if (data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(imud)<= 0.0d0) then ! imud is completely exited form the string
  1047. call data%State%MudSystem%ChokeLine_MudDischarged_Volume%Remove (imud)
  1048. call data%State%MudSystem%ChokeLine_Mud_Backhead_X%Remove (imud)
  1049. call data%State%MudSystem%ChokeLine_Mud_Backhead_section%Remove (imud)
  1050. call data%State%MudSystem%ChokeLine_Mud_Forehead_X%Remove (imud)
  1051. call data%State%MudSystem%ChokeLine_Mud_Forehead_section%Remove (imud)
  1052. call data%State%MudSystem%ChokeLine_Density%Remove (imud)
  1053. call data%State%MudSystem%ChokeLine_RemainedVolume_in_LastSection%Remove (imud)
  1054. call data%State%MudSystem%ChokeLine_EmptyVolume_inBackheadLocation%Remove (imud)
  1055. call data%State%MudSystem%ChokeLine_MudOrKick%Remove (imud)
  1056. endif
  1057. exit
  1058. endif
  1059. data%State%MudSystem%xx= data%State%MudSystem%ChokeLine_RemainedVolume_in_LastSection%Array(imud)/ data%State%MudSystem%ChokeLine_VolumeCapacity !(gal)
  1060. if (data%State%MudSystem%xx<= 1.0) then
  1061. data%State%MudSystem%ChokeLine_Mud_Forehead_section%Array(imud)= data%State%MudSystem%isection
  1062. data%State%MudSystem%ChokeLine_Mud_Forehead_X%Array(imud)= data%State%MudSystem%xx * data%Configuration%BopStack%ChokeLineLength
  1063. exit
  1064. else
  1065. data%State%MudSystem%ChokeLine_RemainedVolume_in_LastSection%Array(imud)= data%State%MudSystem%ChokeLine_RemainedVolume_in_LastSection%Array(imud)- data%State%MudSystem%ChokeLine_VolumeCapacity
  1066. data%State%MudSystem%isection= data%State%MudSystem%isection+ 1
  1067. endif
  1068. enddo
  1069. endif
  1070. enddo
  1071. !========================Choke Line END=================
  1072. !do imud=1, ChokeLine_MudDischarged_Volume%Length()
  1073. ! write(*,*) 'b)ChokeLine:', imud, ChokeLine_MudDischarged_Volume%Array(imud) ,ChokeLine_MudOrKick%Array(imud)
  1074. !enddo
  1075. data%State%MudSystem%ChokeOutletDensity= data%State%MudSystem%ChokeLine_Density%Last() ! used in MudSystem
  1076. do i=1, data%State%MudSystem%ChokeLine_MudOrKick%Length()
  1077. !write(*,555) i,'Choke_Volume(i), type=' ,ChokeLine_MudDischarged_Volume%Array(i),ChokeLine_MudOrKick%Array(i)
  1078. IF (IEEE_Is_NaN(data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(i))) call ErrorStop('NaN in Choke Volume-Plot')
  1079. IF (data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(i)<=0.) call ErrorStop('Choke Volume= <=0' , data%State%MudSystem%ChokeLine_MudDischarged_Volume%Array(i))
  1080. enddo
  1081. 555 FORMAT(I3,5X,A42,(f12.5),5X,I3)
  1082. !write(*,*) 'after sorting chokeline=='
  1083. !IF (ANY(ChokeLine_MudOrKick%Array(:) > 0)) THEN
  1084. ! do imud=1, ChokeLine_MudDischarged_Volume%Length()
  1085. ! write(*,*) 'ChokeLine:', imud, ChokeLine_MudDischarged_Volume%Array(imud), ChokeLine_Density%Array(imud) ,ChokeLine_MudOrKick%Array(imud)
  1086. ! enddo
  1087. !END IF
  1088. !do imud=1, Ann_MudDischarged_Volume%Length()
  1089. ! write(*,*) 'Ann:', imud, Ann_MudDischarged_Volume%Array(imud), Ann_Density%Array(imud) ,Ann_MudOrKick%Array(imud)
  1090. !enddo
  1091. !
  1092. !write(*,*) 'Ann cap=' , sum(PipeSection_VolumeCapacity(F_StringIntervalCounts+1:NoPipeSections))
  1093. ! write(*,*) 'Ann mud sum vol=' , sum(Ann_MudDischarged_Volume%Array(:))
  1094. !write(*,*) '==after sorting chokeline'
  1095. end subroutine ChokeLineMud
  1096. subroutine Choke_GasSound ! is called in subroutine CirculationCodeSelect
  1097. use CSounds
  1098. !Use GeoElements_FluidModule
  1099. !USE CMudPropertiesVariables
  1100. USE MudSystemVARIABLES
  1101. use SimulationVariables !@@@
  1102. !use ConfigurationVariables
  1103. !USE CHOKEVARIABLES
  1104. !use ConfigurationVariables !@
  1105. !!use CDataDisplayConsole
  1106. !@ use ConfigurationVariables , StandPipePressureDataDisplay=>StandPipePressure
  1107. !!use CManifolds
  1108. !use ConfigurationVariables !@
  1109. !USE CHOKEVARIABLES
  1110. !use ConfigurationVariables !@
  1111. !!use CChokeManifoldVariables
  1112. use SimulationVariables
  1113. !use CTanks
  1114. !@use ConfigurationVariables, TripTankVolume2 => TripTankVolume, TripTankDensity2 => TripTankDensity
  1115. !USE sROP_Other_Variables
  1116. !USE sROP_Variables
  1117. !use KickVARIABLESModule
  1118. !use PressureDisplayVARIABLESModule
  1119. !Use CError
  1120. !Use , intrinsic :: IEEE_Arithmetic
  1121. if ( data%State%MudSystem%ChokeLine_MudOrKick%Last() > 0 .AND. data%State%MudSystem%WellToChokeManifoldOpen == .true.) then
  1122. !WellToChokeManifoldWasOpen
  1123. data%State%MudSystem%SoundGasThroughChoke = 100 !100:chon dar adadhaye kamtar az 100 seda ghaat mishavad. eslah shavad.5.8.98 !int (min(ChokeLineFlowRate/2. , 100.))
  1124. print* , 'SoundGasThroughChoke1=', data%State%MudSystem%SoundGasThroughChoke
  1125. !WRITE (*,*) 'WellToChokeManifoldWasOpen-Sound', WellToChokeManifoldWasOpen
  1126. WRITE (*,*) 'WellToChokeManifoldOpen', data%State%MudSystem%WellToChokeManifoldOpen
  1127. else
  1128. data%State%MudSystem%SoundGasThroughChoke = 0
  1129. print* , 'SoundGasThroughChoke2=', data%State%MudSystem%SoundGasThroughChoke
  1130. endif
  1131. !print* , 'SoundGasThroughChoke3=', SoundGasThroughChoke
  1132. call SetSoundGasThroughChoke(data%State%MudSystem%SoundGasThroughChoke)
  1133. end subroutine Choke_GasSound