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.

Trip_Out_andPump.f90 69 KiB

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