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.
 
 
 
 
 
 

1368 lines
81 KiB

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