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.
 
 
 
 
 
 

1627 lines
80 KiB

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