Simulation Core
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

1367 lignes
69 KiB

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