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.
 
 
 
 
 
 

1372 lines
88 KiB

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