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.
 
 
 
 
 
 

1371 regels
91 KiB

  1. subroutine TripOut_and_Pump ! is called in subroutine CirculationCodeSelect
  2. Use GeoElements_FluidModule
  3. USE CMudPropertiesVariables
  4. USE MudSystemVARIABLES
  5. use SimulationVariables !@@@
  6. use SimulationVariables
  7. USE CHOKEVARIABLES
  8. !use ConfigurationVariables !@
  9. !use CDataDisplayConsole
  10. !!@ use ConfigurationVariables , StandPipePressureDataDisplay=>StandPipePressure
  11. !use CManifolds
  12. use SimulationVariables !@
  13. USE CHOKEVARIABLES
  14. !use ConfigurationVariables !@
  15. !use CChokeManifoldVariables
  16. use SimulationVariables
  17. !use CTanks
  18. !@use ConfigurationVariables, TripTankVolume2 => data%EquipmentControl%DrillingWatch%TripTankVolume, TripTankDensity2 => TripTankDensity
  19. USE sROP_Other_Variables
  20. USE sROP_Variables
  21. use KickVARIABLESModule
  22. Use CShoeVariables
  23. use CError
  24. implicit none
  25. integer i,ii,AddLocation, iloc_edited, iloc_changedTo2
  26. Real(8) ExcessMudVolume_Remained,SavedDensityForOp
  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(*,*) 'data%State%MUD(2)%Q=====' , data%State%MUD(2)%Q
  32. write(*,*) 'Trip Out'
  33. ! write(*,*) 'check point 1=='
  34. !
  35. !
  36. !
  37. ! do imud=1, Ann_MudDischarged_Volume%Length()
  38. ! write(*,*) 'Ann:', imud, Ann_MudDischarged_Volume%Array(imud), Ann_Density%Array(imud) ,Ann_MudOrKick%Array(imud)
  39. ! enddo
  40. !
  41. ! write(*,*) 'Ann cap=' , sum(PipeSection_VolumeCapacity(F_StringIntervalCounts+1:NoPipeSections))
  42. ! write(*,*) 'Ann mud sum vol=' , sum(Ann_MudDischarged_Volume%Array(:))
  43. !
  44. !
  45. !write(*,*) '==check point 1'
  46. !========================Horizontal PIPE ENTRANCE=================
  47. if (ABS(data%State%MudSystem%SuctionDensity_Old - data%State%MudSystem%Suction_Density_MudSystem) >= data%State%MudSystem%DensityMixTol) then ! new mud is pumped
  48. call data%State%MudSystem%Hz_Density%AddToFirst (data%State%MudSystem%Suction_Density_MudSystem)
  49. call data%State%MudSystem%Hz_MudDischarged_Volume%AddToFirst (0.0d0)
  50. call data%State%MudSystem%Hz_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(1))
  51. call data%State%MudSystem%Hz_Mud_Forehead_section%AddToFirst (1)
  52. call data%State%MudSystem%Hz_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(1))
  53. call data%State%MudSystem%Hz_Mud_Backhead_section%AddToFirst (1)
  54. call data%State%MudSystem%Hz_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
  55. call data%State%MudSystem%Hz_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
  56. call data%State%MudSystem%Hz_MudOrKick%AddToFirst (0)
  57. data%State%MudSystem%SuctionDensity_Old= data%State%MudSystem%Suction_Density_MudSystem
  58. endif
  59. !========================Horizontal PIPE STRING=================
  60. 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)
  61. data%State%MudSystem%total_add = data%State%MudSystem%total_add + ((data%State%MudSystem%StringFlowRate/60.0d0)*data%State%MudSystem%DeltaT_Mudline)
  62. if (data%EquipmentControl%ChokeControlPanel%ChokePanelStrokeResetSwitch == 1) then
  63. data%State%MudSystem%total_add= 0.
  64. endif
  65. !write(*,*) ' total decrease(add to HZ)=' , total_add
  66. !write(*,*) ' add to HZ=' , ((data%State%MudSystem%StringFlowRate/60.0d0)*DeltaT_Mudline)
  67. imud=0
  68. do while (imud < data%State%MudSystem%Hz_Mud_Forehead_X%Length())
  69. imud = imud + 1
  70. if (imud> 1) then
  71. data%State%MudSystem%Hz_Mud_Backhead_X%Array(imud)= data%State%MudSystem%Hz_Mud_Forehead_X%Array(imud-1)
  72. data%State%MudSystem%Hz_Mud_Backhead_section%Array(imud)= data%State%MudSystem%Hz_Mud_Forehead_section%Array(imud-1)
  73. endif
  74. 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))) &
  75. / 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)))
  76. ! +1 for string , -1 for annulus
  77. 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))* &
  78. data%State%MudSystem%Area_PipeSectionFt(data%State%MudSystem%Hz_Mud_Backhead_section%Array(imud)) !(ft^3)
  79. data%State%MudSystem%Hz_EmptyVolume_inBackheadLocation%Array(imud)= data%State%MudSystem%Hz_EmptyVolume_inBackheadLocation%Array(imud)* 7.48051948d0 ! ft^3 to gal
  80. if ( data%State%MudSystem%Hz_MudDischarged_Volume%Array(imud) <= data%State%MudSystem%Hz_EmptyVolume_inBackheadLocation%Array(imud)) then
  81. data%State%MudSystem%Hz_Mud_Forehead_section%Array(imud)= data%State%MudSystem%Hz_Mud_Backhead_section%Array(imud)
  82. 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))
  83. else
  84. data%State%MudSystem%isection= data%State%MudSystem%Hz_Mud_Backhead_section%Array(imud)+1
  85. 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)
  86. do
  87. if (data%State%MudSystem%isection > 1) then ! (horizontal pipe exit)
  88. 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)
  89. data%State%MudSystem%Hz_Mud_Forehead_X%Array(imud)= data%State%MudSystem%Xend_PipeSection(1)
  90. data%State%MudSystem%Hz_Mud_Forehead_section%Array(imud)= 1
  91. if (data%State%MudSystem%Hz_MudDischarged_Volume%Array(imud)<= 0.0d0) then ! imud is completely exited form the string
  92. call RemoveHzMudArrays(imud)
  93. endif
  94. exit
  95. endif
  96. data%State%MudSystem%xx= data%State%MudSystem%Hz_RemainedVolume_in_LastSection%Array(imud)/ data%State%MudSystem%PipeSection_VolumeCapacity(data%State%MudSystem%isection) !(gal)
  97. if (data%State%MudSystem%xx<= 1.0) then
  98. data%State%MudSystem%Hz_Mud_Forehead_section%Array(imud)= data%State%MudSystem%isection
  99. 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)
  100. exit
  101. else
  102. 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)
  103. data%State%MudSystem%isection= data%State%MudSystem%isection+ 1
  104. endif
  105. enddo
  106. endif
  107. enddo
  108. !========================Horizontal PIPE END=================
  109. !========================Utube1 Air Element Removing=================
  110. !if (UtubeMode1Activated== .true.) then ! StringUpdate == .true.
  111. !
  112. ! StringDensity_Old= data%State%MudSystem%St_Density%Array(2)
  113. !
  114. ! UtubeMode1Activated= .false.
  115. !endif
  116. !========================Utube1 Air Element Removing=================
  117. !========================Utube2 Removing from Annulus=================
  118. if (data%State%MudSystem%UtubeMode2Activated== .true.) then ! StringUpdate == .true.
  119. data%State%MudSystem%TotalAddedVolume=0.
  120. if (data%State%MudSystem%Ann_MudOrKick%Last() == 104) then !movaghati. albate age merge anjam shode bashe moshkeli nist
  121. call RemoveAnnulusMudArrays(data%State%MudSystem%Ann_MudOrKick%Length())
  122. endif
  123. data%State%MudSystem%UtubeMode2Activated= .false.
  124. endif
  125. !========================Utube2 Removing from Annulus End=================
  126. !========================New Pipe Filling=================
  127. if (data%State%MudSystem%AddedElementsToString > 0) then ! StringUpdate == .true.
  128. !NoPipeAdded= F_StringIntervalCounts - F_StringIntervalCountsOld
  129. data%State%MudSystem%NewPipeFilling=0
  130. IF (data%State%MudSystem%St_MudOrKick%First() == 104) then
  131. 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
  132. else
  133. call data%State%MudSystem%St_Density%AddToFirst (0.d0)
  134. call data%State%MudSystem%St_MudDischarged_Volume%AddToFirst (sum(data%State%MudSystem%PipeSection_VolumeCapacity(2:1+data%State%MudSystem%AddedElementsToString)))
  135. call data%State%MudSystem%St_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(2))
  136. call data%State%MudSystem%St_Mud_Forehead_section%AddToFirst (2)
  137. call data%State%MudSystem%St_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(2))
  138. call data%State%MudSystem%St_Mud_Backhead_section%AddToFirst (2)
  139. call data%State%MudSystem%St_RemainedVolume_in_LastSection%AddToFirst (0.d0)
  140. call data%State%MudSystem%St_EmptyVolume_inBackheadLocation%AddToFirst (0.d0)
  141. call data%State%MudSystem%St_MudOrKick%AddToFirst (104)
  142. endif
  143. endif
  144. !F_StringIntervalCountsOld= F_StringIntervalCounts
  145. if (data%State%MudSystem%NewPipeFilling == 0) then ! 2= is the first element of string (1= is for Hz pipe)
  146. data%State%MudSystem%LackageMudVolume= data%State%MudSystem%St_MudDischarged_Volume%Array(1) ! = Air element
  147. !write(*,*) 'LackageMudVolume=' , LackageMudVolume
  148. if (ABS(data%State%MudSystem%St_Density%Array(2) - data%State%MudSystem%Hz_Density%Last()) >= data%State%MudSystem%DensityMixTol) then ! new mud is pumped
  149. call data%State%MudSystem%St_Density%AddTo (2,data%State%MudSystem%Hz_Density%Last())
  150. call data%State%MudSystem%St_MudDischarged_Volume%AddTo (2,0.d0)
  151. call data%State%MudSystem%St_Mud_Forehead_X%AddTo (2,data%State%MudSystem%Xstart_PipeSection(2))
  152. call data%State%MudSystem%St_Mud_Forehead_section%AddTo (2 , 2)
  153. call data%State%MudSystem%St_Mud_Backhead_X%AddTo (2,data%State%MudSystem%Xstart_PipeSection(2))
  154. call data%State%MudSystem%St_Mud_Backhead_section%AddTo (2 ,2)
  155. call data%State%MudSystem%St_RemainedVolume_in_LastSection%AddTo (2,0.d0)
  156. call data%State%MudSystem%St_EmptyVolume_inBackheadLocation%AddTo (2,0.d0)
  157. call data%State%MudSystem%St_MudOrKick%AddTo (2,0)
  158. !StringDensity_Old= Hz_Density%Last()
  159. endif
  160. 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)
  161. 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)
  162. !LackageMudVolumeAfterFilling= sum(PipeSection_VolumeCapacity(2:F_StringIntervalCounts)) - sum(St_MudDischarged_Volume%Array(:))
  163. data%State%MudSystem%LackageMudVolumeAfterFilling= data%State%MudSystem%St_MudDischarged_Volume%Array(1) ! last time it should be zero
  164. if (data%State%MudSystem%LackageMudVolumeAfterFilling == 0.) then
  165. data%State%MudSystem%NewPipeFilling= 1
  166. call RemoveStringMudArrays(1)
  167. data%State%MudSystem%St_Mud_Backhead_X%Array(1) = data%State%MudSystem%Xstart_PipeSection(2)
  168. data%State%MudSystem%St_Mud_Backhead_section%Array(1) = 2
  169. endif
  170. endif
  171. !========================New Pipe Filling End=================
  172. if (data%State%MudSystem%NewPipeFilling == 0) then
  173. data%State%MudSystem%StringFlowRate= 0.
  174. data%State%MudSystem%AnnulusFlowRate= 0.
  175. endif
  176. data%State%MudSystem%StringFlowRateFinal= data%State%MudSystem%StringFlowRate
  177. data%State%MudSystem%AnnulusFlowRateFinal= data%State%MudSystem%AnnulusFlowRate
  178. !========================STRING ENTRANCE=================
  179. 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
  180. !if (ABS(StringDensity_Old - Hz_Density%Last()) >= DensityMixTol) then ! new mud is pumped
  181. call data%State%MudSystem%St_Density%AddToFirst (data%State%MudSystem%Hz_Density%Last())
  182. call data%State%MudSystem%St_MudDischarged_Volume%AddToFirst (0.0d0)
  183. call data%State%MudSystem%St_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(2))
  184. call data%State%MudSystem%St_Mud_Forehead_section%AddToFirst (2)
  185. call data%State%MudSystem%St_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(2))
  186. call data%State%MudSystem%St_Mud_Backhead_section%AddToFirst (2)
  187. call data%State%MudSystem%St_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
  188. call data%State%MudSystem%St_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
  189. call data%State%MudSystem%St_MudOrKick%AddToFirst (0)
  190. !StringDensity_Old= Hz_Density%Last()
  191. endif
  192. 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)
  193. !=============== save String Mud data===========
  194. data%State%MudSystem%StMudVolumeSum= 0.d0
  195. !St_MudSaved_Density= 0.d0
  196. data%State%MudSystem%St_Saved_MudDischarged_Volume= 0.d0
  197. !Saved_St_MudOrKick= 0
  198. !Ann_to_Choke_2mud= .false.
  199. do imud=1, data%State%MudSystem%St_MudDischarged_Volume%Length()
  200. data%State%MudSystem%StMudVolumeSum= data%State%MudSystem%StMudVolumeSum + data%State%MudSystem%St_MudDischarged_Volume%Array(imud)
  201. if ( data%State%MudSystem%StMudVolumeSum > sum(data%State%MudSystem%PipeSection_VolumeCapacity(2:data%State%F_Counts%StringIntervalCounts)) ) then
  202. !IF (St_MudOrKick%Array(imud) == 0) THEN
  203. data%State%MudSystem%St_MudSaved_Density = data%State%MudSystem%St_Density%Array(imud)
  204. data%State%MudSystem%St_Saved_MudDischarged_Volume = data%State%MudSystem%StMudVolumeSum - sum(data%State%MudSystem%PipeSection_VolumeCapacity(2:data%State%F_Counts%StringIntervalCounts))
  205. !ELSEIF (St_MudOrKick%Array(imud) > 0 .AND. St_MudOrKick%Array(imud) <100) THEN ! 104= AIR
  206. ! St_Kick_Saved_Volume = StMudVolumeSum - sum(PipeSection_VolumeCapacity(F_StringIntervalCounts+1:NoPipeSections))
  207. ! Saved_St_MudOrKick= St_MudOrKick%Array (imud)
  208. ! St_KickSaved_Density= data%State%MudSystem%St_Density%Array(imud)
  209. !END IF
  210. do ii= imud + 1, data%State%MudSystem%St_MudDischarged_Volume%Length()
  211. !IF (St_MudOrKick%Array(ii) == 0) THEN
  212. 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))
  213. data%State%MudSystem%St_Saved_MudDischarged_Volume = data%State%MudSystem%St_Saved_MudDischarged_Volume + data%State%MudSystem%St_MudDischarged_Volume%Array(ii)
  214. !ELSEIF (St_MudOrKick%Array(imud) > 0 .AND. St_MudOrKick%Array(imud) <100) THEN ! 104= AIR
  215. ! St_Kick_Saved_Volume = St_Kick_Saved_Volume + St_MudDischarged_Volume%Array(ii)
  216. ! Saved_St_MudOrKick= St_MudOrKick%Array (ii)
  217. ! St_KickSaved_Density= data%State%MudSystem%St_Density%Array(ii)
  218. !END IF
  219. enddo
  220. !WRITE (*,*) 'St_Saved_Mud_Volume, St_Kick_Saved_Volume', St_Saved_MudDischarged_Volume, St_Kick_Saved_Volume
  221. exit ! exits do
  222. endif
  223. enddo
  224. data%State%MudSystem%St_Saved_MudDischarged_Volume_Final= data%State%MudSystem%St_Saved_MudDischarged_Volume
  225. IF (data%State%MudSystem%WellHeadIsOpen) data%State%MudSystem%MudVolume_InjectedToBH = data%State%MudSystem%St_Saved_MudDischarged_Volume_Final
  226. !======================================================================
  227. !========================STRING=================
  228. imud=0
  229. do while (imud < data%State%MudSystem%St_Mud_Forehead_X%Length())
  230. imud = imud + 1
  231. if (imud> 1) then
  232. data%State%MudSystem%St_Mud_Backhead_X%Array(imud)= data%State%MudSystem%St_Mud_Forehead_X%Array(imud-1)
  233. data%State%MudSystem%St_Mud_Backhead_section%Array(imud)= data%State%MudSystem%St_Mud_Forehead_section%Array(imud-1)
  234. endif
  235. 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))) &
  236. / 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)))
  237. ! +1 for string , -1 for annulus
  238. 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))* &
  239. data%State%MudSystem%Area_PipeSectionFt(data%State%MudSystem%St_Mud_Backhead_section%Array(imud)) !(ft^3)
  240. data%State%MudSystem%St_EmptyVolume_inBackheadLocation%Array(imud)= data%State%MudSystem%St_EmptyVolume_inBackheadLocation%Array(imud)* 7.48051948d0 ! ft^3 to gal
  241. if ( data%State%MudSystem%St_MudDischarged_Volume%Array(imud) <= data%State%MudSystem%St_EmptyVolume_inBackheadLocation%Array(imud)) then
  242. data%State%MudSystem%St_Mud_Forehead_section%Array(imud)= data%State%MudSystem%St_Mud_Backhead_section%Array(imud)
  243. 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))
  244. ! 7.48 is for gal to ft^3
  245. else
  246. data%State%MudSystem%isection= data%State%MudSystem%St_Mud_Backhead_section%Array(imud)+1
  247. 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)
  248. do
  249. if (data%State%MudSystem%isection > data%State%F_Counts%StringIntervalCounts) then ! last pipe section(string exit)
  250. 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)
  251. data%State%MudSystem%St_Mud_Forehead_X%Array(imud)= data%State%MudSystem%Xend_PipeSection(data%State%F_Counts%StringIntervalCounts)
  252. data%State%MudSystem%St_Mud_Forehead_section%Array(imud)= data%State%F_Counts%StringIntervalCounts
  253. if (data%State%MudSystem%St_MudDischarged_Volume%Array(imud)<= 0.0d0) then ! imud is completely exited form the string
  254. call RemoveStringMudArrays(imud)
  255. endif
  256. exit
  257. endif
  258. data%State%MudSystem%xx= data%State%MudSystem%St_RemainedVolume_in_LastSection%Array(imud)/ data%State%MudSystem%PipeSection_VolumeCapacity(data%State%MudSystem%isection) !(gal)
  259. if (data%State%MudSystem%xx<= 1.0) then
  260. data%State%MudSystem%St_Mud_Forehead_section%Array(imud)= data%State%MudSystem%isection
  261. 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)
  262. exit
  263. else
  264. 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)
  265. data%State%MudSystem%isection= data%State%MudSystem%isection+ 1
  266. endif
  267. enddo
  268. endif
  269. enddo
  270. !========================STRING END=================
  271. !write(*,*) ' a before=='
  272. !
  273. ! do imud=1, Op_MudDischarged_Volume%Length()
  274. ! write(*,*) 'Op:', imud, Op_MudDischarged_Volume%Array(imud), Op_Density%Array(imud) ,Op_MudOrKick%Array(imud)
  275. ! enddo
  276. !
  277. ! do imud=1, Ann_MudDischarged_Volume%Length()
  278. ! write(*,*) 'Ann:', imud, Ann_MudDischarged_Volume%Array(imud), Ann_Density%Array(imud) ,Ann_MudOrKick%Array(imud)
  279. ! enddo
  280. !
  281. ! write(*,*) 'Ann cap=' , sum(PipeSection_VolumeCapacity(F_StringIntervalCounts+1:NoPipeSections))
  282. ! write(*,*) 'Ann mud sum vol=' , sum(Ann_MudDischarged_Volume%Array(:))
  283. !
  284. !
  285. !write(*,*) '==== a before'
  286. iloc_changedTo2 = 0
  287. IF (data%State%MudSystem%Op_MudOrKick%Last() /= 0 .and. data%State%MudSystem%Op_MudOrKick%Last()==data%State%MudSystem%Ann_MudOrKick%First()) then
  288. data%State%MudSystem%iLoc=2 ! it may be 1,2,3 or more, all of them are kick
  289. iloc_changedTo2= 1
  290. endif
  291. iloc_edited= 0
  292. !write(*,*) sum(Op_MudDischarged_Volume%Array(:)) , ((data%State%MudSystem%AnnulusFlowRate/60.d0)*DeltaT_Mudline) , Ann_MudDischarged_Volume%First() , sum(OpSection_VolumeCapacity(1:F_BottomHoleIntervalCounts))
  293. if (data%State%MudSystem%iLoc==2 .and. sum(data%State%MudSystem%Op_MudDischarged_Volume%Array(:))+((data%State%MudSystem%AnnulusFlowRate/60.d0)*data%State%MudSystem%DeltaT_Mudline)+data%State%MudSystem%Ann_MudDischarged_Volume%First() < sum(data%State%MudSystem%OpSection_VolumeCapacity(1:data%State%F_Counts%BottomHoleIntervalCounts)) ) then
  294. data%State%MudSystem%iLoc = 1
  295. iloc_edited = 1
  296. !write(*,*) 'hellooooooo'
  297. endif
  298. !write(*,*) 'ann-cap:' , sum(PipeSection_VolumeCapacity(F_StringIntervalCounts+1 :F_StringIntervalCounts+F_AnnulusIntervalCounts) )
  299. !write(*,*) 'iloc====' , iloc
  300. !MudVolume_InjectedToBH
  301. !=============================Add PumpFlowRate to Bottom Hole ==============================
  302. !if ( data%State%MudSystem%AnnulusFlowRate>0.0 ) then
  303. if ( data%State%MudSystem%MudVolume_InjectedToBH > 0.0 ) then
  304. if (KickVARIABLES%KickOffBottom) then ! (kickOffBottom = F) means kick is next to the bottom hole and usually kick is entering the
  305. AddLocation= data%State%MudSystem%Op_Density%Length()-data%State%MudSystem%iLoc+1+1 ! well, thus pumped mud should be placed above the kick
  306. else
  307. AddLocation= data%State%MudSystem%Op_Density%Length()+1
  308. endif
  309. !write(*,*) 'AddLocation====' , AddLocation
  310. if ( AddLocation== 0) CALL ErrorStop ('AddLocation=0')
  311. if ( ABS(data%State%MudSystem%St_Density%Last() - data%State%MudSystem%Op_Density%Array(AddLocation-1)) >= data%State%MudSystem%DensityMixTol ) then
  312. !write(*,*) 'new pocket**'
  313. !write(*,*) 'data%State%MudSystem%St_Density%Last()=' , data%State%MudSystem%St_Density%Last()
  314. !write(*,*) 'Op_Density%Array(AddLocation-1)=' , Op_Density%Array(AddLocation-1)
  315. call data%State%MudSystem%Op_Density% AddTo (AddLocation,data%State%MudSystem%St_Density%Last())
  316. !call Op_MudDischarged_Volume%AddTo (AddLocation,((data%State%MudSystem%AnnulusFlowRate/60.d0)*DeltaT_Mudline))
  317. call data%State%MudSystem%Op_MudDischarged_Volume%AddTo (AddLocation,data%State%MudSystem%MudVolume_InjectedToBH)
  318. call data%State%MudSystem%Op_Mud_Forehead_X%AddTo (AddLocation,data%State%MudSystem%Xstart_OpSection(1))
  319. call data%State%MudSystem%Op_Mud_Forehead_section%AddTo (AddLocation,1)
  320. call data%State%MudSystem%Op_Mud_Backhead_X%AddTo (AddLocation,data%State%MudSystem%Xstart_OpSection(1))
  321. call data%State%MudSystem%Op_Mud_Backhead_section%AddTo (AddLocation,1)
  322. call data%State%MudSystem%Op_RemainedVolume_in_LastSection%AddTo (AddLocation,0.0d0)
  323. call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%AddTo (AddLocation,0.0d0)
  324. call data%State%MudSystem%Op_MudOrKick%AddTo (AddLocation,0)
  325. else
  326. !write(*,*) 'merge**'
  327. !write(*,*) 'density before=' , Op_Density%Array(AddLocation-1)
  328. !write(*,*) 'data%State%MudSystem%St_Density%Last() for mix=' , data%State%MudSystem%St_Density%Last()
  329. !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))
  330. !Op_MudDischarged_Volume%Array(AddLocation-1)= Op_MudDischarged_Volume%Array(AddLocation-1) + ((data%State%MudSystem%AnnulusFlowRate/60.d0)*DeltaT_Mudline)
  331. 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)
  332. data%State%MudSystem%Op_MudDischarged_Volume%Array(AddLocation-1)= data%State%MudSystem%Op_MudDischarged_Volume%Array(AddLocation-1) + data%State%MudSystem%MudVolume_InjectedToBH
  333. !write(*,*) 'density after=' , Op_Density%Array(AddLocation-1)
  334. endif
  335. endif
  336. !=======================Add PumpFlowRate to Bottom Hole- End ==============================
  337. !write(*,*) 'pump added-before add to ann=='
  338. !
  339. ! do imud=1, Op_MudDischarged_Volume%Length()
  340. ! write(*,*) 'Op:', imud, Op_MudDischarged_Volume%Array(imud), Op_Density%Array(imud) ,Op_MudOrKick%Array(imud)
  341. ! enddo
  342. !
  343. ! do imud=1, Ann_MudDischarged_Volume%Length()
  344. ! write(*,*) 'Ann:', imud, Ann_MudDischarged_Volume%Array(imud), Ann_Density%Array(imud) ,Ann_MudOrKick%Array(imud)
  345. ! enddo
  346. !
  347. ! write(*,*) 'Ann cap=' , sum(PipeSection_VolumeCapacity(F_StringIntervalCounts+1:NoPipeSections))
  348. ! write(*,*) 'Ann mud sum vol=' , sum(Ann_MudDischarged_Volume%Array(:))
  349. !
  350. !
  351. !
  352. !write(*,*) 'pump added====before add to ann'
  353. !=============== save OP Mud data to transfer to the annulus enterance due to tripin or kick
  354. data%State%MudSystem%OpMudVolumeSum= 0.d0
  355. !Op_MudSaved_Density= 0.d0
  356. !Op_KickSaved_Density= 0.d0
  357. data%State%MudSystem%Op_Saved_MudDischarged_Volume= 0.d0
  358. data%State%MudSystem%Op_Kick_Saved_Volume= 0.d0
  359. data%State%MudSystem%Saved_Op_MudOrKick= 0
  360. data%State%MudSystem%Op_NeededVolume_ToFill= 0.d0
  361. do imud=1, data%State%MudSystem%Op_MudDischarged_Volume%Length()
  362. data%State%MudSystem%OpMudVolumeSum= data%State%MudSystem%OpMudVolumeSum + data%State%MudSystem%Op_MudDischarged_Volume%Array(imud)
  363. if ( data%State%MudSystem%OpMudVolumeSum > sum(data%State%MudSystem%OpSection_VolumeCapacity(1:data%State%F_Counts%BottomHoleIntervalCounts)) ) then !1st mode
  364. IF (data%State%MudSystem%Op_MudOrKick%Array(imud) == 0) THEN
  365. data%State%MudSystem%Op_MudSaved_Density = data%State%MudSystem%Op_Density%Array(imud)
  366. data%State%MudSystem%Op_Saved_MudDischarged_Volume = data%State%MudSystem%OpMudVolumeSum - sum(data%State%MudSystem%OpSection_VolumeCapacity(1:data%State%F_Counts%BottomHoleIntervalCounts))
  367. ELSE
  368. data%State%MudSystem%Op_Kick_Saved_Volume = data%State%MudSystem%OpMudVolumeSum - sum(data%State%MudSystem%OpSection_VolumeCapacity(1:data%State%F_Counts%BottomHoleIntervalCounts))
  369. data%State%MudSystem%Saved_Op_MudOrKick= data%State%MudSystem%Op_MudOrKick%Array (imud)
  370. data%State%MudSystem%Op_KickSaved_Density= data%State%MudSystem%Op_Density%Array(imud)
  371. data%State%MudSystem%iLoc= 2
  372. iloc_changedTo2= 2
  373. END IF
  374. do ii= imud + 1, data%State%MudSystem%Op_MudDischarged_Volume%Length()
  375. IF (data%State%MudSystem%Op_MudOrKick%Array(ii) == 0) THEN
  376. 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))
  377. data%State%MudSystem%Op_Saved_MudDischarged_Volume = data%State%MudSystem%Op_Saved_MudDischarged_Volume + data%State%MudSystem%Op_MudDischarged_Volume%Array(ii)
  378. ELSE
  379. data%State%MudSystem%Op_Kick_Saved_Volume = data%State%MudSystem%Op_Kick_Saved_Volume + data%State%MudSystem%Op_MudDischarged_Volume%Array(ii)
  380. data%State%MudSystem%Saved_Op_MudOrKick= data%State%MudSystem%Op_MudOrKick%Array (ii)
  381. data%State%MudSystem%Op_KickSaved_Density= data%State%MudSystem%Op_Density%Array(ii)
  382. data%State%MudSystem%iLoc= 2
  383. iloc_changedTo2= 3
  384. END IF
  385. enddo
  386. exit ! exits do
  387. endif
  388. enddo
  389. if ( sum(data%State%MudSystem%Op_MudDischarged_Volume%Array(:)) < sum(data%State%MudSystem%OpSection_VolumeCapacity(1:data%State%F_Counts%BottomHoleIntervalCounts)) ) then !2nd & 3rd mode
  390. data%State%MudSystem%Op_NeededVolume_ToFill= sum(data%State%MudSystem%OpSection_VolumeCapacity(1:data%State%F_Counts%BottomHoleIntervalCounts)) - sum(data%State%MudSystem%Op_MudDischarged_Volume%Array(:))
  391. endif
  392. !
  393. !write(*,*) 'Op_NeededVolume_ToFill=' , Op_NeededVolume_ToFill
  394. !write(*,*) 'Op_Saved_MudDischarged_Volume=' , Op_Saved_MudDischarged_Volume
  395. !write(*,*) 'Op_Kick_Saved_Volume=' , Op_Kick_Saved_Volume
  396. !
  397. !write(*,*) 'op cap=' , sum(OpSection_VolumeCapacity(1:F_BottomHoleIntervalCounts))
  398. !write(*,*) ' op sum mud=' , sum(Op_MudDischarged_Volume%Array(:))
  399. !======================================================================
  400. !========================Tripping Out- 1st & 3rd Mode====================
  401. if ( (data%State%MudSystem%Op_Kick_Saved_Volume > 0.0 .or. data%State%MudSystem%Op_Saved_MudDischarged_Volume> 0.0) .or. & ! 1st Mode-Pump flow is more than trip out so fluid Level in Annulus Increases
  402. (data%State%MudSystem%Op_NeededVolume_ToFill < ABS(data%State%MudSystem%DeltaVolumeAnnulusCapacity)) ) then !3rd Mode-fluid Level in Annulus Increases
  403. !if ( Op_Kick_Saved_Volume > 0.0 .or. Op_Saved_MudDischarged_Volume> 0.0 ) write(*,*) 'trip out 1st mode'
  404. if ( data%State%MudSystem%Op_NeededVolume_ToFill > 0.0 .and. data%State%MudSystem%Op_NeededVolume_ToFill < ABS(data%State%MudSystem%DeltaVolumeAnnulusCapacity) ) then
  405. ! write(*,*) 'trip out 3rd mode'
  406. data%State%MudSystem%NewVolume= 0.d0 ! for condition iloc=1
  407. SavedDensityForOp= data%State%MudSystem%Ann_Density%Array(1)
  408. ExcessMudVolume_Remained= data%State%MudSystem%Op_NeededVolume_ToFill
  409. imud=1
  410. Do
  411. if(data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud) < ExcessMudVolume_Remained) then
  412. ExcessMudVolume_Remained= ExcessMudVolume_Remained- data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud)
  413. call data%State%MudSystem%Ann_MudDischarged_Volume%Remove (imud)
  414. call data%State%MudSystem%Ann_Mud_Backhead_X%Remove (imud)
  415. call data%State%MudSystem%Ann_Mud_Backhead_section%Remove (imud)
  416. call data%State%MudSystem%Ann_Mud_Forehead_X%Remove (imud)
  417. call data%State%MudSystem%Ann_Mud_Forehead_section%Remove (imud)
  418. call data%State%MudSystem%Ann_Density%Remove (imud)
  419. call data%State%MudSystem%Ann_RemainedVolume_in_LastSection%Remove (imud)
  420. call data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Remove (imud)
  421. call data%State%MudSystem%Ann_MudOrKick%Remove (imud)
  422. elseif(data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud) > ExcessMudVolume_Remained) then
  423. data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud)= data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud)- ExcessMudVolume_Remained
  424. exit
  425. else !(Ann_MudDischarged_Volume%Array(imud) == ExcessMudVolume_Remained)
  426. call data%State%MudSystem%Ann_MudDischarged_Volume%Remove (imud)
  427. call data%State%MudSystem%Ann_Mud_Backhead_X%Remove (imud)
  428. call data%State%MudSystem%Ann_Mud_Backhead_section%Remove (imud)
  429. call data%State%MudSystem%Ann_Mud_Forehead_X%Remove (imud)
  430. call data%State%MudSystem%Ann_Mud_Forehead_section%Remove (imud)
  431. call data%State%MudSystem%Ann_Density%Remove (imud)
  432. call data%State%MudSystem%Ann_RemainedVolume_in_LastSection%Remove (imud)
  433. call data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Remove (imud)
  434. call data%State%MudSystem%Ann_MudOrKick%Remove (imud)
  435. exit
  436. endif
  437. enddo
  438. !write(*,*) 'Op_NeededVolume_ToFill=' ,Op_NeededVolume_ToFill
  439. !write(*,*) 'ABS(DeltaVolumeAnnulusCapacity)=' ,ABS(DeltaVolumeAnnulusCapacity)
  440. !write(*,*) 'Op_MudOrKick%Last()=' ,Op_MudOrKick%Last()
  441. !write(*,*) 'iloc=' ,iloc
  442. !write(*,*) 'iloc_edited=' ,iloc_edited
  443. endif
  444. ! (data%State%MudSystem%AnnulusFlowRate/60.)*DeltaT_Mudline) - DeltaVolumeOp will be added to annulus
  445. !if (iLoc == 1) then
  446. data%State%MudSystem%MudSection= data%State%F_Counts%StringIntervalCounts+1
  447. data%State%MudSystem%BackheadX= data%State%MudSystem%Xstart_PipeSection(data%State%F_Counts%StringIntervalCounts+1)
  448. !elseif (iLoc == 2) then
  449. ! MudSection= Kick_Forehead_section
  450. ! BackheadX= Kick_Forehead_X
  451. !endif
  452. !========================ANNULUS ENTRANCE====================
  453. !if (KickMigration_2SideBit == .FALSE.) then
  454. ! if ( ABS(AnnulusSuctionDensity_Old - data%State%MudSystem%St_Density%Last()) >= DensityMixTol ) then ! new mud is pumped
  455. ! call Ann_Density%AddTo (iLoc,data%State%MudSystem%St_Density%Last())
  456. ! call Ann_MudDischarged_Volume%AddTo (iLoc,0.0d0)
  457. ! call Ann_Mud_Forehead_X%AddTo (iLoc,BackheadX)
  458. ! call Ann_Mud_Forehead_section%AddTo (iLoc,MudSection)
  459. ! call Ann_Mud_Backhead_X%AddTo (iLoc,BackheadX)
  460. ! call Ann_Mud_Backhead_section%AddTo (iLoc,MudSection)
  461. ! call Ann_RemainedVolume_in_LastSection%AddTo (iLoc,0.0d0)
  462. ! call Ann_EmptyVolume_inBackheadLocation%AddTo (iLoc,0.0d0)
  463. ! call Ann_MudOrKick%AddTo (iLoc,0)
  464. ! call Ann_CuttingMud%AddTo (iLoc,0)
  465. !
  466. ! AnnulusSuctionDensity_Old= data%State%MudSystem%St_Density%Last()
  467. !
  468. ! MudIsChanged= .true.
  469. ! endif
  470. !
  471. ! Ann_MudDischarged_Volume%Array(iLoc)= Ann_MudDischarged_Volume%Array(iLoc)+ ((data%State%MudSystem%AnnulusFlowRate/60.0d0)*DeltaT_Mudline) - ((2-iloc)*ABS(DeltaVolumePipe)) !(gal)
  472. !
  473. !endif
  474. 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)- (and add pipe)
  475. data%State%MudSystem%Ann_Mud_Backhead_X%Array(1)= data%State%MudSystem%BackheadX
  476. !iloc=1 : (2-iloc)=1 normal
  477. !iloc=2 : (2-iloc)=0 kick influx or migration is in annulus
  478. !========================Same to Tripping In====================
  479. !write(*,*) 'Op_Kick_Saved_Volume,Op_Saved_MudDischarged_Volume=' , Op_Kick_Saved_Volume,Op_Saved_MudDischarged_Volume
  480. if (data%State%MudSystem%Op_Kick_Saved_Volume > 0.0 .and. data%State%MudSystem%Ann_MudOrKick%First() == 0) then !1st Mode
  481. write(*,*) 'Kick influx enters Annulus'
  482. call data%State%MudSystem%Ann_Density%AddToFirst (data%State%MudSystem%Op_KickSaved_Density)
  483. call data%State%MudSystem%Ann_MudDischarged_Volume%AddToFirst (data%State%MudSystem%Op_Kick_Saved_Volume)
  484. call data%State%MudSystem%Ann_Mud_Forehead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(data%State%F_Counts%StringIntervalCounts+1))
  485. call data%State%MudSystem%Ann_Mud_Forehead_section%AddToFirst (data%State%F_Counts%StringIntervalCounts+1)
  486. call data%State%MudSystem%Ann_Mud_Backhead_X%AddToFirst (data%State%MudSystem%Xstart_PipeSection(data%State%F_Counts%StringIntervalCounts+1))
  487. call data%State%MudSystem%Ann_Mud_Backhead_section%AddToFirst (data%State%F_Counts%StringIntervalCounts+1)
  488. call data%State%MudSystem%Ann_RemainedVolume_in_LastSection%AddToFirst (0.0d0)
  489. call data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%AddToFirst (0.0d0)
  490. call data%State%MudSystem%Ann_MudOrKick%AddToFirst (data%State%MudSystem%Saved_Op_MudOrKick) !<<<<<<<<
  491. call data%State%MudSystem%Ann_CuttingMud%AddToFirst (0)
  492. elseif (data%State%MudSystem%Op_Kick_Saved_Volume > 0.0 .and. data%State%MudSystem%Ann_MudOrKick%First() /= 0) then
  493. data%State%MudSystem%Ann_MudDischarged_Volume%Array(1)= data%State%MudSystem%Ann_MudDischarged_Volume%Array(1) + data%State%MudSystem%Op_Kick_Saved_Volume
  494. endif
  495. if ( data%State%MudSystem%Op_NeededVolume_ToFill > 0.0 .and. (data%State%MudSystem%Op_NeededVolume_ToFill < ABS(data%State%MudSystem%DeltaVolumeAnnulusCapacity)) .and. data%State%MudSystem%Op_MudOrKick%Last() == 0 .and. (data%State%MudSystem%iLoc==2 .or. iloc_edited==1)) then !3rd Mode
  496. !write(*,*) 'checkpoint 0'
  497. !! for avoid kick separation -Op_MudOrKick%Last() == 0: because of pump
  498. data%State%MudSystem%NewVolume= ((data%State%MudSystem%AnnulusFlowRate/60.d0)*data%State%MudSystem%DeltaT_Mudline) ! =volume that should be added to iloc=2 in Ann
  499. call RemoveOpMudArrays(data%State%MudSystem%Op_Density%Length()) ! mud here is removed and then will be added to iloc=2 in Ann in %%1 section
  500. if ( data%State%MudSystem%Ann_MudDischarged_Volume%Array(1) > ((data%State%MudSystem%AnnulusFlowRate/60.d0)*data%State%MudSystem%DeltaT_Mudline) ) then! 1st in Ann = kick ,, we expect: ((data%State%MudSystem%AnnulusFlowRate/60.d0)*DeltaT_Mudline)= OpMudVolLast
  501. 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)
  502. 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) ! kick
  503. else
  504. call RemoveAnnulusMudArrays(1) !kick is removed
  505. data%State%MudSystem%iLoc= 1
  506. 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)
  507. write(*,*) 'little expand'
  508. ! including a little expand
  509. endif
  510. endif
  511. if (data%State%MudSystem%Op_Saved_MudDischarged_Volume> 0.0) then !1st Mode
  512. data%State%MudSystem%NewDensity= data%State%MudSystem%Op_MudSaved_Density
  513. !write(*,*) 'iloc,...' , iloc,((data%State%MudSystem%AnnulusFlowRate/60.d0)*DeltaT_Mudline),Op_Saved_MudDischarged_Volume
  514. if (data%State%MudSystem%iLoc==1) then
  515. !write(*,*) 'checkpoint 1'
  516. data%State%MudSystem%NewVolume= data%State%MudSystem%Op_Saved_MudDischarged_Volume
  517. elseif (real(((data%State%MudSystem%AnnulusFlowRate/60.d0)*data%State%MudSystem%DeltaT_Mudline)) - real(data%State%MudSystem%Op_Saved_MudDischarged_Volume) > 0.d0 ) then ! for avoid kick separation
  518. !write(*,*) 'checkpoint 2'
  519. data%State%MudSystem%NewVolume= ((data%State%MudSystem%AnnulusFlowRate/60.d0)*data%State%MudSystem%DeltaT_Mudline) !- Op_Saved_MudDischarged_Volume
  520. call RemoveOpMudArrays(data%State%MudSystem%Op_Density%Length()) ! mud here is removed and then will be added to iloc=2 in Ann
  521. 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
  522. 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)
  523. 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
  524. else
  525. call RemoveAnnulusMudArrays(1) !kick is removed
  526. data%State%MudSystem%iLoc =1
  527. 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)
  528. write(*,*) 'little expand'
  529. ! including a little expand
  530. endif
  531. else ! iloc==2 , ((data%State%MudSystem%AnnulusFlowRate/60.d0)*DeltaT_Mudline) == Op_Saved_MudDischarged_Volume
  532. !write(*,*) 'checkpoint 3'
  533. data%State%MudSystem%NewVolume= data%State%MudSystem%Op_Saved_MudDischarged_Volume ! it is normal mode
  534. endif
  535. endif
  536. !write(*,*) 'NewVolume=' ,NewVolume
  537. if( data%State%MudSystem%Ann_Density%Length() == 1 .and. data%State%MudSystem%iLoc ==2 ) then
  538. write(*,*) '***errorb****=='
  539. write(*,*) 'iloc_edited=' , iloc_edited
  540. write(*,*) 'iloc_changedTo2=' , iloc_changedTo2
  541. write(*,*) 'Op_Capacity===' , sum(data%State%MudSystem%OpSection_VolumeCapacity(1:data%State%F_Counts%BottomHoleIntervalCounts))
  542. WRITE (*,*) 'Op_Saved_MudDischarged_Volume, Op_Kick_Saved_Volume',data%State%MudSystem%Op_Saved_MudDischarged_Volume, data%State%MudSystem%Op_Kick_Saved_Volume
  543. do imud=1, data%State%MudSystem%Op_MudDischarged_Volume%Length()
  544. 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)
  545. enddo
  546. do imud=1, data%State%MudSystem%Ann_MudDischarged_Volume%Length()
  547. 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)
  548. enddo
  549. write(*,*) '==***errorb****'
  550. endif
  551. 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) & !%%1 section
  552. .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) &
  553. .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
  554. !write(*,*) '%%1 section a)'
  555. 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)
  556. 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
  557. data%State%MudSystem%Ann_Mud_Forehead_X%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%BackheadX
  558. data%State%MudSystem%Ann_Mud_Forehead_section%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%MudSection
  559. data%State%MudSystem%Ann_Mud_Backhead_X%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%BackheadX
  560. data%State%MudSystem%Ann_Mud_Backhead_section%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%MudSection
  561. data%State%MudSystem%Ann_RemainedVolume_in_LastSection%Array(data%State%MudSystem%iLoc)= (0.0d0)
  562. data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Array(data%State%MudSystem%iLoc)= (0.0d0)
  563. else ! 2-Merging conditions are not meeted, so new pocket
  564. !write(*,*) '%%1 section b)'
  565. call data%State%MudSystem%Ann_Density%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%NewDensity)
  566. call data%State%MudSystem%Ann_MudDischarged_Volume%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%NewVolume)
  567. call data%State%MudSystem%Ann_Mud_Forehead_X%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%BackheadX)
  568. call data%State%MudSystem%Ann_Mud_Forehead_section%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%MudSection)
  569. call data%State%MudSystem%Ann_Mud_Backhead_X%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%BackheadX)
  570. call data%State%MudSystem%Ann_Mud_Backhead_section%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%MudSection)
  571. call data%State%MudSystem%Ann_RemainedVolume_in_LastSection%AddTo (data%State%MudSystem%iLoc,0.0d0)
  572. call data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%AddTo (data%State%MudSystem%iLoc,0.0d0)
  573. call data%State%MudSystem%Ann_MudOrKick%AddTo (data%State%MudSystem%iLoc,0)
  574. call data%State%MudSystem%Ann_CuttingMud%AddTo (data%State%MudSystem%iLoc,0)
  575. !write(*,*) 'd) annLength=' , Ann_Density%Length()
  576. endif
  577. !========================Same to Tripping In - End====================
  578. !write(*,*) 'b)Ann_Mud sum=' , sum(Ann_MudDischarged_Volume%Array(:))
  579. !write(*,*) 'no======2'
  580. !
  581. ! do imud=1, Op_MudDischarged_Volume%Length()
  582. ! write(*,*) 'Op:', imud, Op_MudDischarged_Volume%Array(imud), Op_Density%Array(imud) ,Op_MudOrKick%Array(imud)
  583. ! enddo
  584. !
  585. ! do imud=1, Ann_MudDischarged_Volume%Length()
  586. ! write(*,*) 'Ann:', imud, Ann_MudDischarged_Volume%Array(imud), Ann_Density%Array(imud) ,Ann_MudOrKick%Array(imud)
  587. ! enddo
  588. !
  589. !
  590. ! write(*,*) 'Ann cap=' , sum(PipeSection_VolumeCapacity(F_StringIntervalCounts+1:NoPipeSections))
  591. ! write(*,*) 'Ann mud sum vol=' , sum(Ann_MudDischarged_Volume%Array(:))
  592. !
  593. !
  594. !write(*,*) '2======no'
  595. !=============== save Ann Mud data to transfer to the ChokeLine enterance
  596. data%State%MudSystem%AnnMudVolumeSum= 0.d0
  597. !Ann_MudSaved_Density= 0.d0
  598. !Ann_KickSaved_Density= 0.d0
  599. data%State%MudSystem%Ann_Saved_MudDischarged_Volume= 0.d0
  600. data%State%MudSystem%Ann_Kick_Saved_Volume= 0.d0
  601. data%State%MudSystem%Saved_Ann_MudOrKick= 0
  602. data%State%MudSystem%Ann_to_Choke_2mud= .false.
  603. do imud=1, data%State%MudSystem%Ann_MudDischarged_Volume%Length()
  604. data%State%MudSystem%AnnMudVolumeSum= data%State%MudSystem%AnnMudVolumeSum + data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud)
  605. if ( data%State%MudSystem%AnnMudVolumeSum > sum(data%State%MudSystem%PipeSection_VolumeCapacity(data%State%F_Counts%StringIntervalCounts+1:data%State%MudSystem%NoPipeSections)) ) then
  606. IF (data%State%MudSystem%Ann_MudOrKick%Array(imud) == 0) THEN
  607. data%State%MudSystem%Ann_MudSaved_Density = data%State%MudSystem%Ann_Density%Array(imud)
  608. 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))
  609. ELSEIF (data%State%MudSystem%Ann_MudOrKick%Array(imud) > 0 .AND. data%State%MudSystem%Ann_MudOrKick%Array(imud) <100) THEN ! 104= AIR
  610. 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))
  611. data%State%MudSystem%Saved_Ann_MudOrKick= data%State%MudSystem%Ann_MudOrKick%Array (imud)
  612. data%State%MudSystem%Ann_KickSaved_Density= data%State%MudSystem%Ann_Density%Array(imud)
  613. END IF
  614. do ii= imud + 1, data%State%MudSystem%Ann_MudDischarged_Volume%Length()
  615. IF (data%State%MudSystem%Ann_MudOrKick%Array(ii) == 0) THEN
  616. 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))
  617. data%State%MudSystem%Ann_Saved_MudDischarged_Volume = data%State%MudSystem%Ann_Saved_MudDischarged_Volume + data%State%MudSystem%Ann_MudDischarged_Volume%Array(ii)
  618. data%State%MudSystem%Ann_to_Choke_2mud= .true.
  619. ELSEIF (data%State%MudSystem%Ann_MudOrKick%Array(ii) > 0 .AND. data%State%MudSystem%Ann_MudOrKick%Array(ii) <100) THEN ! 104= AIR
  620. data%State%MudSystem%Ann_Kick_Saved_Volume = data%State%MudSystem%Ann_Kick_Saved_Volume + data%State%MudSystem%Ann_MudDischarged_Volume%Array(ii)
  621. data%State%MudSystem%Saved_Ann_MudOrKick= data%State%MudSystem%Ann_MudOrKick%Array (ii)
  622. data%State%MudSystem%Ann_KickSaved_Density= data%State%MudSystem%Ann_Density%Array(ii)
  623. END IF
  624. enddo
  625. exit ! exits do
  626. endif
  627. enddo
  628. data%State%MudSystem%Ann_Saved_MudDischarged_Volume_Final= data%State%MudSystem%Ann_Saved_MudDischarged_Volume
  629. data%State%MudSystem%Ann_Kick_Saved_Volume_Final= data%State%MudSystem%Ann_Kick_Saved_Volume
  630. !write(*,*) 'Ann cap=' , sum(PipeSection_VolumeCapacity(data%State%F_Counts%StringIntervalCounts+1:NoPipeSections))
  631. !write(*,*) 'Ann mud sum vol=' , sum(Ann_MudDischarged_Volume%Array(:))
  632. 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)
  633. !NoGasPocket
  634. !write(*,*) 'Ann_Saved_Mud_Vol,Ann_Kick_Saved_Vol=' , Ann_Saved_MudDischarged_Volume,Ann_Kick_Saved_Volume
  635. !======================================================================
  636. !write(*,*) 'Ann_Saved_Mud=' , Ann_Saved_MudDischarged_Volume
  637. !======================== Annulus ====================
  638. !MudIsChanged= .false.
  639. imud= 0
  640. do while (imud < data%State%MudSystem%Ann_Mud_Forehead_X%Length())
  641. imud = imud + 1
  642. if (imud> 1) then
  643. data%State%MudSystem%Ann_Mud_Backhead_X%Array(imud)= data%State%MudSystem%Ann_Mud_Forehead_X%Array(imud-1)
  644. data%State%MudSystem%Ann_Mud_Backhead_section%Array(imud)= data%State%MudSystem%Ann_Mud_Forehead_section%Array(imud-1)
  645. endif
  646. ! write(*,*) 'imud==' , imud
  647. !write(*,*) '***)Ann_Mud_Backhead_section(imud)= ' , Ann_Mud_Backhead_section%Array(imud), Ann_density%Array(imud)
  648. 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))) &
  649. / 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)))
  650. ! +1 for string , -1 for annulus
  651. 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))* &
  652. data%State%MudSystem%Area_PipeSectionFt(data%State%MudSystem%Ann_Mud_Backhead_section%Array(imud)) !(ft^3)
  653. data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Array(imud)= data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Array(imud)* 7.48051948d0 ! ft^3 to gal
  654. if ( data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud) <= data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Array(imud)) then
  655. data%State%MudSystem%Ann_Mud_Forehead_section%Array(imud)= data%State%MudSystem%Ann_Mud_Backhead_section%Array(imud)
  656. 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))
  657. ! 7.48 is for gal to ft^3
  658. else
  659. data%State%MudSystem%isection= data%State%MudSystem%Ann_Mud_Backhead_section%Array(imud)+1
  660. 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)
  661. do
  662. if (data%State%MudSystem%isection > data%State%MudSystem%NoPipeSections) then ! last pipe section(well exit)
  663. 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)
  664. data%State%MudSystem%Ann_Mud_Forehead_X%Array(imud)= data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%NoPipeSections)
  665. data%State%MudSystem%Ann_Mud_Forehead_section%Array(imud)= data%State%MudSystem%NoPipeSections
  666. if (data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud)<= 0.0d0) then ! imud is completely exited form the well
  667. call RemoveAnnulusMudArrays(imud)
  668. endif
  669. exit
  670. endif
  671. data%State%MudSystem%xx= data%State%MudSystem%Ann_RemainedVolume_in_LastSection%Array(imud)/ data%State%MudSystem%PipeSection_VolumeCapacity(data%State%MudSystem%isection) !(gal)
  672. if (data%State%MudSystem%xx<= 1.0) then
  673. data%State%MudSystem%Ann_Mud_Forehead_section%Array(imud)= data%State%MudSystem%isection
  674. 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)
  675. exit
  676. else
  677. 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)
  678. data%State%MudSystem%isection= data%State%MudSystem%isection+ 1
  679. endif
  680. enddo
  681. endif
  682. enddo
  683. if (data%State%MudSystem%Ann_Mud_Forehead_X%Last() < data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%NoPipeSections)) then
  684. 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
  685. endif
  686. !========================ANNULUS END=================
  687. !*************************************************************************************************************************
  688. !========================Tripping Out- 2nd Mode====================
  689. elseif ( data%State%MudSystem%Op_NeededVolume_ToFill > ABS(data%State%MudSystem%DeltaVolumeAnnulusCapacity) ) then !pump is off or Pump flow is less than trip out so fluid Level in Annulus decreases
  690. !write(*,*) 'trip out 2nd mode'
  691. SavedDensityForOp= data%State%MudSystem%Ann_Density%Array(1)
  692. !========================ANNULUS ENTRANCE====================
  693. ! <<< SIMILAR TO UTUBE 2 >>>
  694. if ( data%State%MudSystem%Ann_Density%Last() /= 0.0 ) then ! new mud is pumped
  695. call data%State%MudSystem%Ann_Density%Add (0.0d0)
  696. call data%State%MudSystem%Ann_MudDischarged_Volume%Add (0.0d0)
  697. call data%State%MudSystem%Ann_Mud_Forehead_X%Add (data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%NoPipeSections))
  698. call data%State%MudSystem%Ann_Mud_Forehead_section%Add (data%State%MudSystem%NoPipeSections)
  699. call data%State%MudSystem%Ann_Mud_Backhead_X%Add (data%State%MudSystem%Xstart_PipeSection(data%State%MudSystem%NoPipeSections))
  700. call data%State%MudSystem%Ann_Mud_Backhead_section%Add (data%State%MudSystem%NoPipeSections)
  701. call data%State%MudSystem%Ann_RemainedVolume_in_LastSection%Add (0.0d0)
  702. call data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Add (0.0d0)
  703. call data%State%MudSystem%Ann_MudOrKick%Add (104)
  704. call data%State%MudSystem%Ann_CuttingMud%Add (0)
  705. !AnnulusSuctionDensity_Old= Hz_Density%Last()
  706. endif
  707. data%State%MudSystem%Ann_Mud_Forehead_section%Array(data%State%MudSystem%Ann_Mud_Forehead_section%Length())= data%State%MudSystem%NoPipeSections !it is needed to be updated for (a condition that one pipe is removed from Annulus due to trip out)- (and add pipe)
  708. 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)
  709. data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%Ann_MudDischarged_Volume%Length())= data%State%MudSystem%Ann_MudDischarged_Volume%Last()+ (data%State%MudSystem%Op_NeededVolume_ToFill - ABS(data%State%MudSystem%DeltaVolumeAnnulusCapacity)) ! Op_NeededVolume_ToFill !ABS(DeltaVolumePipe) - ((data%State%MudSystem%AnnulusFlowRate/60.)*DeltaT_Mudline) !(gal)
  710. !===================================================================
  711. if ( (data%State%MudSystem%iLoc==2 .or. iloc_edited==1) .and. data%State%MudSystem%Op_MudOrKick%Last()==0 ) then ! for avoid kick separation
  712. !write(*,*) 'here mud should be removed from Op last'
  713. if (abs(data%State%MudSystem%Ann_Density%Array(data%State%MudSystem%iLoc)-data%State%MudSystem%Op_Density%Last())< data%State%MudSystem%DensityMixTol) then
  714. 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%Op_Density%Last()*data%State%MudSystem%Op_MudDischarged_Volume%Last())/(data%State%MudSystem%Ann_MudDischarged_Volume%Array(data%State%MudSystem%iLoc)+data%State%MudSystem%Op_MudDischarged_Volume%Last())
  715. 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%Op_MudDischarged_Volume%Last() ! OP_Last is data%State%MUD(effect of pump added mud)
  716. data%State%MudSystem%Ann_Mud_Forehead_X%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%BackheadX
  717. data%State%MudSystem%Ann_Mud_Forehead_section%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%MudSection
  718. data%State%MudSystem%Ann_Mud_Backhead_X%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%BackheadX
  719. data%State%MudSystem%Ann_Mud_Backhead_section%Array(data%State%MudSystem%iLoc)= data%State%MudSystem%MudSection
  720. data%State%MudSystem%Ann_RemainedVolume_in_LastSection%Array(data%State%MudSystem%iLoc)= (0.0d0)
  721. data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Array(data%State%MudSystem%iLoc)= (0.0d0)
  722. !write(*,*) 'merge' ,'Ann_Volume%Array(1)=' , Ann_MudDischarged_Volume%Array(1)
  723. else ! 2-Merging conditions are not meeted, so new pocket
  724. call data%State%MudSystem%Ann_Density%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%Op_Density%Last())
  725. call data%State%MudSystem%Ann_MudDischarged_Volume%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%Op_MudDischarged_Volume%Last())
  726. call data%State%MudSystem%Ann_Mud_Forehead_X%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%BackheadX)
  727. call data%State%MudSystem%Ann_Mud_Forehead_section%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%MudSection)
  728. call data%State%MudSystem%Ann_Mud_Backhead_X%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%BackheadX)
  729. call data%State%MudSystem%Ann_Mud_Backhead_section%AddTo (data%State%MudSystem%iLoc,data%State%MudSystem%MudSection)
  730. call data%State%MudSystem%Ann_RemainedVolume_in_LastSection%AddTo (data%State%MudSystem%iLoc,0.0d0)
  731. call data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%AddTo (data%State%MudSystem%iLoc,0.0d0)
  732. call data%State%MudSystem%Ann_MudOrKick%AddTo (data%State%MudSystem%iLoc,0)
  733. call data%State%MudSystem%Ann_CuttingMud%AddTo (data%State%MudSystem%iLoc,0)
  734. endif
  735. data%State%MudSystem%Op_NeededVolume_ToFill= data%State%MudSystem%Op_NeededVolume_ToFill + data%State%MudSystem%Op_MudDischarged_Volume%Last() ! OP_Last is data%State%MUD(effect of pump added mud)
  736. call RemoveOpMudArrays(data%State%MudSystem%Op_MudOrKick%Length())
  737. endif
  738. !===================================================================
  739. !=============== save Ann Mud data to transfer to the ChokeLine enterance
  740. !AnnMudVolumeSum= 0.d0
  741. !!Ann_MudSaved_Density= 0.d0
  742. !!Ann_KickSaved_Density= 0.d0
  743. data%State%MudSystem%Ann_Saved_MudDischarged_Volume= 0.d0
  744. data%State%MudSystem%Ann_Kick_Saved_Volume= 0.d0
  745. !Saved_Ann_MudOrKick= 0
  746. !Ann_to_Choke_2mud= .false.
  747. !do imud=1, Ann_MudDischarged_Volume%Length()
  748. !
  749. ! AnnMudVolumeSum= AnnMudVolumeSum + Ann_MudDischarged_Volume%Array(imud)
  750. !
  751. ! if ( AnnMudVolumeSum > sum(PipeSection_VolumeCapacity(F_StringIntervalCounts+1:NoPipeSections)) ) then
  752. !
  753. ! IF (Ann_MudOrKick%Array(imud) == 0) THEN
  754. ! Ann_MudSaved_Density = Ann_Density%Array(imud)
  755. ! Ann_Saved_MudDischarged_Volume = AnnMudVolumeSum - sum(PipeSection_VolumeCapacity(F_StringIntervalCounts+1:NoPipeSections))
  756. ! ELSEIF (Ann_MudOrKick%Array(imud) > 0 .AND. Ann_MudOrKick%Array(imud) <100) THEN ! 104= AIR
  757. ! Ann_Kick_Saved_Volume = AnnMudVolumeSum - sum(PipeSection_VolumeCapacity(F_StringIntervalCounts+1:NoPipeSections))
  758. ! Saved_Ann_MudOrKick= Ann_MudOrKick%Array (imud)
  759. ! Ann_KickSaved_Density= Ann_Density%Array(imud)
  760. ! END IF
  761. !
  762. ! do ii= imud + 1, Ann_MudDischarged_Volume%Length()
  763. ! IF (Ann_MudOrKick%Array(ii) == 0) THEN
  764. ! Ann_MudSaved_Density = ((Ann_MudSaved_Density * Ann_Saved_MudDischarged_Volume) + (Ann_Density%Array(ii) * Ann_MudDischarged_Volume%Array(ii))) / (Ann_Saved_MudDischarged_Volume + Ann_MudDischarged_Volume%Array(ii))
  765. ! Ann_Saved_MudDischarged_Volume = Ann_Saved_MudDischarged_Volume + Ann_MudDischarged_Volume%Array(ii)
  766. ! Ann_to_Choke_2mud= .true.
  767. ! ELSEIF (Ann_MudOrKick%Array(ii) > 0 .AND. Ann_MudOrKick%Array(ii) <100) THEN ! 104= AIR
  768. ! Ann_Kick_Saved_Volume = Ann_Kick_Saved_Volume + Ann_MudDischarged_Volume%Array(ii)
  769. ! Saved_Ann_MudOrKick= Ann_MudOrKick%Array (ii)
  770. ! Ann_KickSaved_Density= Ann_Density%Array(ii)
  771. ! END IF
  772. ! enddo
  773. !
  774. ! exit ! exits do
  775. !
  776. ! endif
  777. !
  778. !enddo
  779. ! write(*,*) 'check point 2=='
  780. !
  781. !
  782. !
  783. ! do imud=1, Ann_MudDischarged_Volume%Length()
  784. ! write(*,*) 'Ann:', imud, Ann_MudDischarged_Volume%Array(imud), Ann_Density%Array(imud) ,Ann_MudOrKick%Array(imud)
  785. ! enddo
  786. !
  787. ! write(*,*) 'Ann cap=' , sum(PipeSection_VolumeCapacity(F_StringIntervalCounts+1:NoPipeSections))
  788. ! write(*,*) 'Ann mud sum vol=' , sum(Ann_MudDischarged_Volume%Array(:))
  789. !
  790. !
  791. !write(*,*) '==check point 2'
  792. data%State%MudSystem%Ann_Saved_MudDischarged_Volume_Final= data%State%MudSystem%Ann_Saved_MudDischarged_Volume
  793. data%State%MudSystem%Ann_Kick_Saved_Volume_Final= data%State%MudSystem%Ann_Kick_Saved_Volume
  794. !write(*,*) 'Ann cap=' , sum(PipeSection_VolumeCapacity(F_StringIntervalCounts+1:NoPipeSections))
  795. !write(*,*) 'Ann mud sum vol=' , sum(Ann_MudDischarged_Volume%Array(:))
  796. !write(*,*) 'Ann_Saved_MudDischarged_Volume_Final=' , Ann_Saved_MudDischarged_Volume_Final
  797. 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)
  798. !! NoGasPocket > 0 .AND.
  799. !write(*,*) 'Ann_Saved_Mud_Vol,Ann_Kick_Saved_Vol=' , Ann_Saved_MudDischarged_Volume,Ann_Kick_Saved_Volume
  800. !======================================================================
  801. !========================ANNULUS====================
  802. ! <<< SIMILAR TO UTUBE 2 >>>
  803. !write(*,*) Ann_MudOrKick%Last(), 'DeltaVolumePipe , after volume=' ,ABS(DeltaVolumePipe), Ann_MudDischarged_Volume%Last()
  804. imud= data%State%MudSystem%Ann_Mud_Forehead_X%Length() + 1
  805. do while (imud > 1)
  806. imud = imud - 1
  807. if (imud< data%State%MudSystem%Ann_Mud_Forehead_X%Length()) then
  808. data%State%MudSystem%Ann_Mud_Forehead_X%Array(imud)= data%State%MudSystem%Ann_Mud_Backhead_X%Array(imud+1)
  809. data%State%MudSystem%Ann_Mud_Forehead_section%Array(imud)= data%State%MudSystem%Ann_Mud_Backhead_section%Array(imud+1)
  810. endif
  811. ! <<< Fracture Shoe Lost
  812. IF ( data%State%MudSystem%ShoeLost .and. Shoe%ShoeDepth < data%State%MudSystem%Ann_Mud_Backhead_X%Array(imud) .and. Shoe%ShoeDepth >= data%State%MudSystem%Ann_Mud_Forehead_X%Array(imud) ) then
  813. !write(*,*) 'ShoeLost imud,AnnVolume(imud), VolumeLost:' , imud,Ann_MudDischarged_Volume%Array(imud), (( Qlost/60.0d0)*DeltaT_Mudline)
  814. 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)
  815. if (data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud) < 0.0) then
  816. !write(*,*) 'mud is removed by shoe lost, imud=' , imud
  817. call RemoveAnnulusMudArrays(imud)
  818. imud= imud-1
  819. cycle
  820. endif
  821. data%State%MudSystem%LostInTripOutIsDone= .true.
  822. ENDIF
  823. ! Fracture Shoe Lost >>>
  824. !write(*,*) 'a)imud,Ann_Mud_Forehead_section=',imud,Ann_Mud_Forehead_section%Array(imud)
  825. data%State%MudSystem%DirectionCoef= (data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%Ann_Mud_Forehead_section%Array(imud))-data%State%MudSystem%Xstart_PipeSection(data%State%MudSystem%Ann_Mud_Forehead_section%Array(imud))) &
  826. / ABS(data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%Ann_Mud_Forehead_section%Array(imud))-data%State%MudSystem%Xstart_PipeSection(data%State%MudSystem%Ann_Mud_Forehead_section%Array(imud)))
  827. ! +1 for string , -1 for annulus
  828. !write(*,*) 'b)imud,Forehead_X,Xstart_PipeSection=',imud,Ann_Mud_Forehead_X%Array(imud),Xstart_PipeSection(Ann_Mud_Forehead_section%Array(imud))
  829. data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Array(imud)= data%State%MudSystem%DirectionCoef* (data%State%MudSystem%Ann_Mud_Forehead_X%Array(imud)- data%State%MudSystem%Xstart_PipeSection(data%State%MudSystem%Ann_Mud_Forehead_section%Array(imud)))* &
  830. data%State%MudSystem%Area_PipeSectionFt(data%State%MudSystem%Ann_Mud_Forehead_section%Array(imud)) !(ft^3)
  831. data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Array(imud)= data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Array(imud)* 7.48051948d0 ! ft^3 to gal
  832. if ( data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud) <= data%State%MudSystem%Ann_EmptyVolume_inBackheadLocation%Array(imud)) then
  833. data%State%MudSystem%Ann_Mud_Backhead_section%Array(imud)= data%State%MudSystem%Ann_Mud_Forehead_section%Array(imud)
  834. data%State%MudSystem%Ann_Mud_Backhead_X%Array(imud)= data%State%MudSystem%Ann_Mud_Forehead_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_Forehead_section%Array(imud))
  835. ! 7.48051948 is for gal to ft^3
  836. else
  837. data%State%MudSystem%isection= data%State%MudSystem%Ann_Mud_Forehead_section%Array(imud)-1
  838. 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)
  839. do
  840. if (data%State%MudSystem%isection < data%State%F_Counts%StringIntervalCounts+1) then ! last pipe section(well exit) data%State%F_Counts%StringIntervalCounts+1 is the first section in Annulus
  841. 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)
  842. data%State%MudSystem%Ann_Mud_Backhead_X%Array(imud)= data%State%MudSystem%Xstart_PipeSection(data%State%F_Counts%StringIntervalCounts+1)
  843. data%State%MudSystem%Ann_Mud_Backhead_section%Array(imud)= data%State%F_Counts%StringIntervalCounts+1
  844. if (data%State%MudSystem%Ann_MudDischarged_Volume%Array(imud)<= 0.0d0) then ! imud is completely exited form the well
  845. call RemoveAnnulusMudArrays(imud)
  846. endif
  847. exit
  848. endif
  849. data%State%MudSystem%xx= data%State%MudSystem%Ann_RemainedVolume_in_LastSection%Array(imud)/ data%State%MudSystem%PipeSection_VolumeCapacity(data%State%MudSystem%isection) !(gal)
  850. if (data%State%MudSystem%xx<= 1.0) then
  851. data%State%MudSystem%Ann_Mud_Backhead_section%Array(imud)= data%State%MudSystem%isection
  852. data%State%MudSystem%Ann_Mud_Backhead_X%Array(imud)= (data%State%MudSystem%xx * (data%State%MudSystem%Xstart_PipeSection(data%State%MudSystem%isection)- data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%isection)))+ data%State%MudSystem%Xend_PipeSection(data%State%MudSystem%isection)
  853. exit
  854. else
  855. 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)
  856. data%State%MudSystem%isection= data%State%MudSystem%isection- 1
  857. endif
  858. enddo
  859. endif
  860. enddo
  861. !========================ANNULUS END=================
  862. endif ! end of 1st &3rd & 2nd Mode
  863. !*************************************************************************************************************************
  864. !======================== Bottom Hole Entrance ==========================
  865. !if (iloc == 1) then
  866. if ( data%State%MudSystem%Op_NeededVolume_ToFill > 0.0 ) then ! it is needed for 2nd & 3rd mode
  867. !write(*,*) 'op add for 2nd & 3rd mode done'
  868. if ( ABS(data%State%MudSystem%Op_Density%Last() - SavedDensityForOp ) >= data%State%MudSystem%DensityMixTol) then ! .OR. (Op_MudDischarged_Volume%Last()>42.) ) then ! 1-Merging conditions are not meeted, so new pocket
  869. call data%State%MudSystem%Op_Density%Add (SavedDensityForOp)
  870. call data%State%MudSystem%Op_MudDischarged_Volume%Add (data%State%MudSystem%Op_NeededVolume_ToFill)
  871. call data%State%MudSystem%Op_Mud_Forehead_X%Add (0.0d0)
  872. call data%State%MudSystem%Op_Mud_Forehead_section%Add (1)
  873. call data%State%MudSystem%Op_Mud_Backhead_X%Add (0.0d0)
  874. call data%State%MudSystem%Op_Mud_Backhead_section%Add (1)
  875. call data%State%MudSystem%Op_RemainedVolume_in_LastSection%Add (0.0d0)
  876. call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%Add (0.0d0)
  877. call data%State%MudSystem%Op_MudOrKick%Add (data%State%MudSystem%Ann_MudOrKick%Array(1))
  878. else ! 2-Pockets are Merged
  879. data%State%MudSystem%Op_Density%Array (data%State%MudSystem%Op_Density%Length())= (SavedDensityForOp*data%State%MudSystem%Op_NeededVolume_ToFill+data%State%MudSystem%Op_Density%Last()*data%State%MudSystem%Op_MudDischarged_Volume%Last())/(data%State%MudSystem%Op_MudDischarged_Volume%Last()+data%State%MudSystem%Op_NeededVolume_ToFill)
  880. 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%Op_NeededVolume_ToFill
  881. data%State%MudSystem%Op_RemainedVolume_in_LastSection%Array (data%State%MudSystem%Op_Density%Length())= 0.0
  882. data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%Array (data%State%MudSystem%Op_Density%Length())= 0.0
  883. endif
  884. endif
  885. !============================= Bottom Hole ==============================
  886. imud=0
  887. do while (imud < data%State%MudSystem%Op_Mud_Forehead_X%Length())
  888. imud = imud + 1
  889. if (imud> 1) then
  890. data%State%MudSystem%Op_Mud_Backhead_X%Array(imud)= data%State%MudSystem%Op_Mud_Forehead_X%Array(imud-1)
  891. data%State%MudSystem%Op_Mud_Backhead_section%Array(imud)= data%State%MudSystem%Op_Mud_Forehead_section%Array(imud-1)
  892. endif
  893. 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))) &
  894. / 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)))
  895. ! +1 for string , -1 for annulus
  896. 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))* &
  897. data%State%MudSystem%Area_OpSectionFt(data%State%MudSystem%Op_Mud_Backhead_section%Array(imud)) !(ft^3)
  898. data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%Array(imud)= data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%Array(imud)* 7.48051948d0 ! ft^3 to gal
  899. if ( data%State%MudSystem%Op_MudDischarged_Volume%Array(imud) <= data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%Array(imud)) then
  900. data%State%MudSystem%Op_Mud_Forehead_section%Array(imud)= data%State%MudSystem%Op_Mud_Backhead_section%Array(imud)
  901. 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))
  902. ! 7.48051948 is for gal to ft^3
  903. else
  904. data%State%MudSystem%isection= data%State%MudSystem%Op_Mud_Backhead_section%Array(imud)+1
  905. 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)
  906. do
  907. if (data%State%MudSystem%isection > data%State%F_Counts%BottomHoleIntervalCounts) then ! last pipe section(well exit)
  908. !if( imud==1) KickDeltaVinAnnulus= Op_RemainedVolume_in_LastSection%Array(imud) ! Kick enters Annulus space
  909. 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)
  910. data%State%MudSystem%Op_Mud_Forehead_X%Array(imud)= data%State%MudSystem%Xend_OpSection(data%State%F_Counts%BottomHoleIntervalCounts)
  911. data%State%MudSystem%Op_Mud_Forehead_section%Array(imud)= data%State%F_Counts%BottomHoleIntervalCounts
  912. if (data%State%MudSystem%Op_MudDischarged_Volume%Array(imud)<= 0.0d0) then ! imud is completely exited form the well
  913. call data%State%MudSystem%Op_MudDischarged_Volume%Remove (imud)
  914. call data%State%MudSystem%Op_Mud_Backhead_X%Remove (imud)
  915. call data%State%MudSystem%Op_Mud_Backhead_section%Remove (imud)
  916. call data%State%MudSystem%Op_Mud_Forehead_X%Remove (imud)
  917. call data%State%MudSystem%Op_Mud_Forehead_section%Remove (imud)
  918. call data%State%MudSystem%Op_Density%Remove (imud)
  919. call data%State%MudSystem%Op_RemainedVolume_in_LastSection%Remove (imud)
  920. call data%State%MudSystem%Op_EmptyVolume_inBackheadLocation%Remove (imud)
  921. call data%State%MudSystem%Op_MudOrKick%Remove (imud)
  922. endif
  923. exit
  924. endif
  925. data%State%MudSystem%xx= data%State%MudSystem%Op_RemainedVolume_in_LastSection%Array(imud)/ data%State%MudSystem%OpSection_VolumeCapacity(data%State%MudSystem%isection) !(gal)
  926. if (data%State%MudSystem%xx<= 1.0) then
  927. data%State%MudSystem%Op_Mud_Forehead_section%Array(imud)= data%State%MudSystem%isection
  928. 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)
  929. exit
  930. else
  931. 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)
  932. data%State%MudSystem%isection= data%State%MudSystem%isection+ 1
  933. endif
  934. enddo
  935. endif
  936. enddo
  937. !========================Bottom Hole END=================
  938. ! write(*,*) 'after sorting=='
  939. !!!
  940. ! do imud=1, Op_MudDischarged_Volume%Length()
  941. ! write(*,*) 'Op:', imud, Op_MudDischarged_Volume%Array(imud), Op_Density%Array(imud) ,Op_MudOrKick%Array(imud)
  942. ! enddo
  943. !
  944. ! do imud=1, Ann_MudDischarged_Volume%Length()
  945. ! write(*,*) 'Ann:', imud, Ann_MudDischarged_Volume%Array(imud), Ann_Density%Array(imud) ,Ann_MudOrKick%Array(imud)
  946. ! enddo
  947. !!!
  948. !! write(*,*) 'Ann cap=' , sum(PipeSection_VolumeCapacity(F_StringIntervalCounts+1:NoPipeSections))
  949. !! write(*,*) 'Ann mud sum vol=' , sum(Ann_MudDischarged_Volume%Array(:))
  950. !!!
  951. !!!
  952. !write(*,*) '==after sorting'
  953. !=========================================================
  954. data%State%MudSystem%total_injected = data%State%MudSystem%total_injected + data%State%MudSystem%MudVolume_InjectedFromAnn
  955. if (data%EquipmentControl%ChokeControlPanel%ChokePanelStrokeResetSwitch == 1) then
  956. data%State%MudSystem%total_injected= 0.
  957. endif
  958. !write(*,*) ' data%State%MudSystem%MudVolume_InjectedFromAnn =' , data%State%MudSystem%MudVolume_InjectedFromAnn
  959. !write(*,*) ' total injected-tripout =' , total_injected
  960. !write(*,*) ' injected-tripout =' , data%State%MudSystem%MudVolume_InjectedFromAnn
  961. end subroutine TripOut_and_Pump