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.

Pump_and_Trip_In.f90 79 KiB

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