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.
 
 
 
 
 
 

908 lines
29 KiB

  1. # 1 "/home/admin/SimulationCore2/Simulator.f90"
  2. module Simulator
  3. use IFPORT
  4. use RedisInterface
  5. use Bop
  6. use PumpsMain
  7. use RopMain
  8. use RotaryTableMain
  9. use DrawworksMain
  10. use FluidFlowMain
  11. use TorqueDragMain
  12. use MudSystemMain
  13. use PipeRams1Main
  14. use PipeRams2Main
  15. use KillLineMain
  16. use ChokeLineMain
  17. use BlindRamsMain
  18. use AnnularMain
  19. use TopDriveMain
  20. use CManifolds
  21. use GeoMain
  22. use ChokeControlMain
  23. use COperationScenariosMain
  24. ! For Json read and write
  25. use CStringConfiguration
  26. use CFormation
  27. use CReservoir
  28. use CShoe
  29. use CAccumulator
  30. use CBopStack
  31. use CHoisting
  32. use CPower
  33. use CPumpsVariables
  34. use CRigSize
  35. use CCasingLinerChoke
  36. use CPathGeneration
  37. use CWellSurveyData
  38. use MudPropertiesModule
  39. use CBitProblems
  40. use CBopProblems
  41. use CChokeProblems
  42. use CDrillStemProblems
  43. use CGaugesProblems
  44. use CHoistingProblems
  45. use CKickProblems
  46. use CLostProblems
  47. use CMudTreatmentProblems
  48. use COtherProblems
  49. use CPumpProblems
  50. use CRotaryProblems
  51. use OperationScenariosModule
  52. use PermissionsModule
  53. use UnitySignalsModule
  54. use CBopControlPanel
  55. use CChokeControlPanel
  56. use CChokeManifold
  57. use CDataDisplayConsole
  58. use CDrillingConsole
  59. use CHook
  60. use CStandPipeManifold
  61. use CTopDrivePanel
  62. use DrillingWatchModule
  63. use CTanks
  64. use UnityModule
  65. use COperationScenariosSettings
  66. use DownHoleModule
  67. use iso_c_binding, only: c_char,c_ptr,c_loc
  68. implicit none
  69. type(json_core):: jsoncore
  70. integer :: simulationStatus,simulationSpeed,msPerStep,simulationEnd,simulationStep
  71. logical::Kelly_ConnectionNothing_in_progress=.false.
  72. character(len=:),allocatable::redisInput,operationScenarioEvent,stateStr
  73. character(kind=c_char,len=:),allocatable,target::redisOutput
  74. type(c_ptr) :: c_string_ptr
  75. character(len=100)::simulationId
  76. enum, bind(c)
  77. enumerator :: PLAY = 1
  78. enumerator :: PAUSE = 2
  79. enumerator :: STOP = 3
  80. enumerator :: PLAY_TO_DETERMINED_TIME = 4
  81. enumerator :: LOAD_STATE = 5
  82. end enum
  83. contains
  84. FUNCTION time_ms()
  85. INTEGER(8) :: time_ms
  86. integer,dimension(8)::timearray
  87. call date_and_time(values=timearray)
  88. time_ms = timearray(8)+timearray(7)*1000 + timearray(6)*60000 + timearray(5)*60000*600000 +timearray(4)*24*60000*60000
  89. RETURN
  90. END FUNCTION
  91. subroutine Simulate(redis_host,redis_port,redis_password, sim_id,stepTime,print_freq)
  92. character(len=*) :: redis_host,redis_password, sim_id
  93. integer::t0,t1,t2,t3,t_read=0,t_write=0,t_exec=0,i,status,redis_port,stepTime,print_freq
  94. integer(8),dimension(12)::t,t_modules
  95. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  96. simulationId = sim_id
  97. do i=1,size(t_modules)
  98. t_modules(i)=0
  99. end do
  100. ! call initSimulation(configFilename)
  101. call initConnection(redis_host,redis_port,redis_password, sim_id,status)
  102. allocate(character(len=30000) :: redisOutput)
  103. if (status<0) then
  104. print *,"Can not init connection to redis."
  105. stop
  106. endif
  107. call read_configuration()
  108. if(simulationStatus==LOAD_STATE) then
  109. call readState()
  110. endif
  111. ! call read_variables()
  112. print *,"connection initialized"
  113. call init_modules()
  114. print *,"modules initialized"
  115. ! call cpu_time(T1)
  116. simulationStep = 1
  117. !$omp parallel sections
  118. !$omp section
  119. call listenToChannel()
  120. !$omp section
  121. do while (.true.)
  122. if(mod(simulationStep,print_freq)==0) then
  123. print_log=.true.
  124. else
  125. print_log=.false.
  126. endif
  127. if(mod(simulationStep,10)==0) call publishMessageToChannel("ack")
  128. ! if(simulationStep>100) exit
  129. t0 = time_ms()
  130. if(print_log) print *,"simulationStep = ",simulationStep
  131. call read_variables()
  132. if(simulationStatus==PLAY_TO_DETERMINED_TIME .and. simulationStep>simulationEnd) exit
  133. if(simulationStatus==STOP) exit
  134. if(simulationStatus==PAUSE) then
  135. if(print_log) print *, "Paused"
  136. call sleepqq(stepTime)
  137. go to 211
  138. endif
  139. t1 = time_ms()
  140. t_read = t_read+t1-t0
  141. ! call logg(4,"read completed")
  142. !! Rafiee, nothing changed
  143. call BopStack_Step()
  144. t(1) = time_ms()
  145. t_modules(1) = t_modules(1)+t(1)-t1
  146. !! Tarmigh, now is rewritten
  147. call Pumps_MainSolver()
  148. t(2) = time_ms()
  149. t_modules(2) = t_modules(2)+t(2)-t(1)
  150. !call Pump2_Step()
  151. !! Rafiee
  152. call ChokeControl_Step()
  153. t(3) = time_ms()
  154. t_modules(3) = t_modules(3)+t(3)-t(2)
  155. !! Tarmigh, now is rewritten
  156. call RotaryTable_Step()
  157. t(4) = time_ms()
  158. t_modules(4) = t_modules(4)+t(4)-t(3)
  159. !! Tarmigh, now is rewritten
  160. call Drawworks_Step()
  161. t(5) = time_ms()
  162. t_modules(5) = t_modules(5)+t(5)-t(4)
  163. !! Rafiee
  164. call MudSystem_Step()
  165. !! Rafiee
  166. call PipeRams1_Step()
  167. call PipeRams2_Step()
  168. !! Rafiee
  169. call KillLine_Step()
  170. t(6) = time_ms()
  171. t_modules(6) = t_modules(6)+t(6)-t(5)
  172. !! Rafiee
  173. call ChokeLine_Step()
  174. t(7) = time_ms()
  175. t_modules(7) = t_modules(7)+t(7)-t(6)
  176. call BlindRams_Step()
  177. t(8) = time_ms()
  178. t_modules(8) = t_modules(8)+t(8)-t(7)
  179. call Annular_Step()
  180. t(9) = time_ms()
  181. t_modules(9) = t_modules(9)+t(9)-t(8)
  182. !!Tarmigh. Step must rewrittem
  183. call TopDrive_Step()
  184. t(10) = time_ms()
  185. t_modules(10) = t_modules(10)+t(10)-t(9)
  186. ! call Geo_Step()
  187. !!Ahmadi
  188. call PathFinding_Step()
  189. t(11) = time_ms()
  190. t_modules(11) = t_modules(11)+t(11)-t(10)
  191. !! Ahmadi
  192. call OperationScenarios_Step()
  193. !! Sheikh
  194. call FluidFlow_Step()
  195. !! Write variables to shared files
  196. t2 = time_ms()
  197. ! stepTime = t2-t1
  198. t_modules(12) = t_modules(12)+t2-t(11)
  199. t_exec = t_exec+t2-t1
  200. ! call date_and_time(values=timearray)
  201. ! t0 = time_ms()
  202. call write_variables()
  203. t3 = time_ms()
  204. if(t3-t0 < stepTime) then
  205. call sleepqq(stepTime-t3+t0)
  206. else
  207. print *,"Simulation step can not be complete in 100 ms. step time=",t3-t0
  208. endif
  209. t_write = t_write+t3-t2
  210. ! print *,"write completed"
  211. ! print *,"t=",t
  212. simulationStep = simulationStep + 1
  213. 211 end do
  214. !$omp end parallel sections
  215. ! call write_variables()
  216. ! call json%print(jsonroot,'test.json')
  217. ! call json%destroy(jsonroot)
  218. do i=1,size(t)
  219. print *,"t_modules(",i,")=",t_modules(i)
  220. end do
  221. print *,"Number of steps =",simulationStep-1
  222. print *,"Read Time (from redis) =",t_read
  223. print *,"Write Time (to redis)=",t_write
  224. print *,"Simulation Time =",t_exec
  225. print *,"Total Time =",t_read+t_write+t_exec
  226. end subroutine Simulate
  227. subroutine write_variables()
  228. use json_module
  229. implicit none
  230. type(json_value),pointer :: jsonroot
  231. character(len=10)::str
  232. type(json_value),pointer :: p
  233. call jsoncore%initialize()
  234. call jsoncore%create_object(jsonroot,'')
  235. call jsoncore%add(jsonroot,'step',simulationStep)
  236. call WarningsToJson(jsonroot)
  237. ! call ProblemsToJson(jsonroot)
  238. call EquipmentsToJson(jsonroot)
  239. ! call StateToJson(jsonroot)
  240. ! some data from state
  241. call jsoncore%create_object(p,'State')
  242. call ManifoldToJson(p)
  243. call notificationsToJson(p)
  244. call UnitySignalsToJson(p)
  245. call jsoncore%add(jsonroot,p)
  246. call jsoncore%print_to_string(jsonroot,redisInput)
  247. if(log_level>4) then
  248. write(str, '(I0)') len(redisInput)
  249. print *,"Writing to redis:"//trim(str)
  250. endif
  251. call setData(redisInput)
  252. ! nullify(redisContent)
  253. ! deallocate(redisContent)
  254. ! call json%destroy(pval)
  255. call jsoncore%destroy(jsonroot)
  256. ! print *,"write ends"
  257. end subroutine
  258. subroutine readState()
  259. ! type(json_value),pointer :: jsonroot,pval
  260. type(json_file) :: jsonfile
  261. type(json_core)::json
  262. integer::len
  263. ! logical::is_found
  264. c_string_ptr = c_loc(redisOutput)
  265. call getData2_C(c_string_ptr,len)
  266. call jsonfile%initialize()
  267. call jsonfile%get_core(json)
  268. call jsonfile%deserialize(redisOutput(1:len))
  269. ! ! call OperationScenarioFromJson(jsonfile)
  270. ! call notificationsFromJson(jsonfile)
  271. ! call permissionsFromJson(jsonfile)
  272. ! call unitySignalsFromJson(jsonfile)
  273. ! call StudentStationFromJson(jsonfile)
  274. ! call BopStackInputFromJson(jsonfile)
  275. ! call BopStackAccFromJson(jsonfile)
  276. ! call RamLineFromJson(jsonfile)
  277. ! call AnnularComputationalFromJson(jsonfile)
  278. ! call AnnularFromJson(jsonfile)
  279. ! call PipeRam1FromJson(jsonfile)
  280. ! call ShearRamFromJson(jsonfile)
  281. ! call PipeRam2FromJson(jsonfile)
  282. ! call ChokeLineFromJson(jsonfile)
  283. ! call KillLineFromJson(jsonfile)
  284. ! call PumpsFromJson(jsonfile)
  285. ! call RAMFromJson(jsonfile)
  286. ! call RAMSFromJson(jsonfile)
  287. ! call ChokeFromJson(jsonfile)
  288. ! call AirDrivenPumpFromJson(jsonfile)
  289. ! call AirPumpLineFromJson(jsonfile)
  290. ! call CHOOKEFromJson(jsonfile)
  291. ! call DrawworksFromJson(jsonfile)
  292. ! call MudSystemFromJson(jsonfile)
  293. ! call MUDFromJson(jsonfile)
  294. ! call MPumpsFromJson(jsonfile)
  295. ! call PUMPFromJson(jsonfile)
  296. ! call RTableFromJson(jsonfile)
  297. ! call TDSFromJson(jsonfile)
  298. ! call GasType(3)FromJson(jsonfile)
  299. ! call PressureDisplayFromJson(jsonfile)
  300. ! call FricPressDropFromJson(jsonfile)
  301. ! call ROP_SpecFromJson(jsonfile)
  302. ! call ROP_BitFromJson(jsonfile)
  303. ! call TDGeoFromJson(jsonfile)
  304. ! call F_String(:)FromJson(jsonfile)
  305. ! call F_CountsFromJson(jsonfile)
  306. ! call F_Interval(:)FromJson(jsonfile)
  307. ! call OD_Annulus(4)FromJson(jsonfile)
  308. ! call TD_DrillStemFromJson(jsonfile)
  309. ! call TD_DrillStemsFromJson(jsonfile)
  310. ! call TD_StringFromJson(jsonfile)
  311. ! call TD_CountFromJson(jsonfile)
  312. ! call G_StringElementFromJson(jsonfile)
  313. ! call TD_VolFromJson(jsonfile)
  314. ! call TD_GeneralFromJson(jsonfile)
  315. ! call TD_BOPFromJson(jsonfile)
  316. ! call TD_BOPElement(4)FromJson(jsonfile)
  317. ! call TD_StConnFromJson(jsonfile)
  318. ! call TD_LoadFromJson(jsonfile)
  319. ! call TD_WellElFromJson(jsonfile)
  320. ! call TD_CasingFromJson(jsonfile)
  321. ! call TD_LinerFromJson(jsonfile)
  322. ! call TD_OpenHoleFromJson(jsonfile)
  323. ! call TD_ROPHoleFromJson(jsonfile)
  324. ! call TD_WellGeneralFromJson(jsonfile)
  325. ! call TD_WellGeo(:)FromJson(jsonfile)
  326. call jsonfile%destroy()
  327. end subroutine
  328. subroutine read_configuration()
  329. type(json_value),pointer :: jsonroot,pval
  330. type(json_file) :: jsonfile
  331. type(json_core)::json
  332. integer::len
  333. ! character(len=:),allocatable::redisOutput
  334. ! call getData3(simulationId,redisOutput)
  335. c_string_ptr = c_loc(redisOutput)
  336. call getData2_C(c_string_ptr,len)
  337. ! print *,len(redisOutput)," bytes read from redis"
  338. ! open(1,file="redisContent.json",status="REPLACE")
  339. ! write(1,"(A)") redisOutput
  340. ! close(1)
  341. call jsonfile%initialize()
  342. call jsonfile%get_core(json)
  343. call jsonfile%deserialize(redisOutput(1:len))
  344. call jsonfile%json_file_get_root(jsonroot)
  345. ! call json%info(jsonvalue, n_children=n_children)
  346. ! print *,"n_children =",n_children
  347. call json%get(jsonroot,'status',pval)
  348. call json%get(pval,simulationStatus)
  349. call json%get(jsonroot,'speed',pval)
  350. call json%get(pval,simulationSpeed)
  351. if(simulationSpeed==0) simulationSpeed = 1
  352. msPerStep = 100/simulationSpeed
  353. call json%get(jsonroot,'endstep',pval)
  354. call json%get(pval,simulationEnd)
  355. call ConfigurationFromJson(jsonfile)
  356. call jsonfile%destroy()
  357. end subroutine
  358. subroutine read_variables()
  359. type(json_value),pointer :: jsonroot,pval
  360. type(json_file) :: jsonfile
  361. type(json_core)::json
  362. integer::stat,leng
  363. logical::found
  364. c_string_ptr = c_loc(redisOutput)
  365. call getData2_C(c_string_ptr,leng)
  366. ! open(1,file="redisContent.json",status="REPLACE")
  367. ! write(1,"(A)") redisOutput
  368. ! close(1)
  369. call jsonfile%initialize()
  370. call jsonfile%get_core(json)
  371. call jsonfile%deserialize(redisOutput(1:leng))
  372. call jsonfile%json_file_get_root(jsonroot)
  373. call jsonfile%get('status',stat,found)
  374. if (stat==0) then
  375. if(print_log) print *,"Status is oddly zero"
  376. return
  377. endif
  378. simulationStatus = stat
  379. ! call jsonfile%get('OperationScenarioEvent',operationScenarioEvent,found)
  380. ! if ( .not. found .and. print_log) print *,"Not found: operationScenarioEvent"
  381. call json%get(jsonroot,'speed',pval)
  382. call json%get(pval,simulationSpeed)
  383. if(simulationSpeed==0) simulationSpeed = 1
  384. msPerStep = 100/simulationSpeed
  385. call json%get(jsonroot,'endstep',pval)
  386. call json%get(pval,simulationEnd)
  387. ! call ProblemsFromJson(jsonfile)
  388. call EquipmentsFromJson(jsonfile)
  389. ! call UnitySignalsFromJson(jsonfile)
  390. call jsonfile%destroy()
  391. end subroutine
  392. subroutine init_modules
  393. print *,"initializing modules"
  394. !Tarmigh
  395. call Pumps_StartUp()
  396. print *,"pump1 initialized"
  397. !call Pump2_Init()
  398. call RotaryTable_Init()
  399. print *,"RT initialized"
  400. call Drawworks_Init()
  401. call TopDrive_Init()
  402. print *,"TD initialized"
  403. !Nothing in init (and step)
  404. ! call Rop_Init()
  405. ! call TorqueDrag_Init()
  406. ! call Geo_Step()
  407. !! Rafiee
  408. call BopStack_Init()
  409. call ChokeControl_Init()
  410. call MudSystem_Init()
  411. ! call PipeRams1_Init()
  412. ! call PipeRams2_Step()
  413. ! call KillLine_Step()
  414. ! call ChokeLine_Step()
  415. ! call BlindRams_Step()
  416. ! call Annular_Step()
  417. !! Sheikh
  418. call FluidFlow_Init()
  419. !! Ahmadi
  420. call PathFinding_Init()
  421. call OperationScenarios_Init()
  422. print *,"Modules are initialized"
  423. ! Calls OSInitialization and that sub only subscribes some notif
  424. end subroutine init_modules
  425. subroutine EquipmentsFromJson(jsonfile)
  426. type(json_value),pointer :: p
  427. type(json_file)::jsonfile
  428. logical::is_found
  429. ! 1. get related root
  430. ! call jsonfile%json_file_get_root(parent)
  431. ! call jsoncore%get(parent,"Equipments",p)
  432. call jsonfile%get("Equipments",p,is_found)
  433. ! call jsoncore%info(p, n_children=n_children)
  434. ! print *,"number of Equipments =",n_children
  435. ! 2. add member of data type to new node
  436. call BopControlPanelFromJson(p)
  437. call ChokeControlPanelFromJson(jsonfile)
  438. call ChokeManifoldFromJson(p)
  439. call DataDisplayConsoleFromJson(p)
  440. call DrillingConsoleFromJson(jsonfile)
  441. call StandPipeManifoldFromJson(p)
  442. call TopDrivePanelFromJson(p)
  443. ! call DrillingWatchFromJson(p)
  444. call TankFromJson(p)
  445. ! call UnityInputsFromJson(jsonfile)
  446. call jsonfile%get('Equipments.HookHeight',data%State%Drawworks%Hook_Height_final,is_found)
  447. if ( .not. is_found ) call logg(4,"Not found: Equipments.HookHeight")
  448. ! if(print_log) print *,"HookHeight=",data%State%Drawworks%Hook_Height_final
  449. ! call jsonfile%get('Equipments.TdsStemIn',data%Equipments%UnityInputs%TdsStemIn,is_found)
  450. ! if ( .not. is_found ) call logg(4,"Not found: Equipments.TdsStemIn")
  451. if (print_log .AND. data%Equipments%UnityInputs%TdsStemIn) print *,"TdsStemIn"
  452. end subroutine
  453. subroutine EquipmentsToJson(parent)
  454. type(json_value),pointer :: parent
  455. type(json_value),pointer :: p
  456. ! 1. create new node
  457. call jsoncore%create_object(p,'Equipments')
  458. ! 2. add member of data type to new node
  459. call BopControlPanelToJson(p)
  460. call ChokeControlPanelToJson(p)
  461. call ChokeManifoldToJson(p)
  462. call DataDisplayConsoleToJson(p)
  463. call DrillingConsoleToJson(p)
  464. call StandPipeManifoldToJson(p)
  465. call TopDrivePanelToJson(p)
  466. call DrillingWatchToJson(p)
  467. call DownHoleToJson(p)
  468. call jsoncore%add(p,"HookVelocity",data%State%Drawworks%HookLinearVelocity_final)
  469. call jsoncore%add(p,"TotalSPM",data%Equipments%MPumps%Total_Pump_SPM)
  470. ! 3. add new node to parent
  471. call jsoncore%add(parent,p)
  472. end subroutine
  473. subroutine WarningsToJson(parent)
  474. type(json_value),pointer :: parent
  475. type(json_value),pointer :: p
  476. ! 1. create new node
  477. call jsoncore%create_object(p,'Warnings')
  478. ! 2. add member of data type to new node
  479. call jsoncore%add(p,"PumpWithKellyDisconnected",data%Warnings%PumpWithKellyDisconnected)
  480. call jsoncore%add(p,"PumpWithTopdriveDisconnected",data%Warnings%PumpWithTopdriveDisconnected)
  481. call jsoncore%add(p,"Pump1PopOffValveBlown",data%Warnings%Pump1PopOffValveBlown)
  482. call jsoncore%add(p,"Pump1Failure",data%Warnings%Pump1Failure)
  483. call jsoncore%add(p,"Pump2PopOffValveBlown",data%Warnings%Pump2PopOffValveBlown)
  484. call jsoncore%add(p,"Pump2Failure",data%Warnings%Pump2Failure)
  485. call jsoncore%add(p,"Pump3PopOffValveBlown",data%Warnings%Pump3PopOffValveBlown)
  486. call jsoncore%add(p,"Pump3Failure",data%Warnings%Pump3Failure)
  487. call jsoncore%add(p,"DrawworksGearsAbuse",data%Warnings%DrawworksGearsAbuse)
  488. call jsoncore%add(p,"RotaryGearsAbuse",data%Warnings%RotaryGearsAbuse)
  489. call jsoncore%add(p,"HoistLineBreak",data%Warnings%HoistLineBreak)
  490. call jsoncore%add(p,"PartedDrillString",data%Warnings%PartedDrillString)
  491. call jsoncore%add(p,"ActiveTankOverflow",data%Warnings%ActiveTankOverflow)
  492. call jsoncore%add(p,"ActiveTankUnderVolume",data%Warnings%ActiveTankUnderVolume)
  493. call jsoncore%add(p,"TripTankOverflow",data%Warnings%TripTankOverflow)
  494. call jsoncore%add(p,"DrillPipeTwistOff",data%Warnings%DrillPipeTwistOff)
  495. call jsoncore%add(p,"DrillPipeParted",data%Warnings%DrillPipeParted)
  496. call jsoncore%add(p,"TripWithSlipsSet",data%Warnings%TripWithSlipsSet)
  497. call jsoncore%add(p,"Blowout",data%Warnings%Blowout)
  498. call jsoncore%add(p,"UndergroundBlowout",data%Warnings%UndergroundBlowout)
  499. call jsoncore%add(p,"MaximumWellDepthExceeded",data%Warnings%MaximumWellDepthExceeded)
  500. call jsoncore%add(p,"CrownCollision",data%Warnings%CrownCollision)
  501. call jsoncore%add(p,"FloorCollision",data%Warnings%FloorCollision)
  502. call jsoncore%add(p,"TopdriveRotaryTableConfilict",data%Warnings%TopdriveRotaryTableConfilict)
  503. ! 3. add new node to parent
  504. call jsoncore%add(parent,p)
  505. end subroutine
  506. subroutine ProblemsToJson(parent)
  507. type(json_value),pointer :: parent
  508. type(json_value),pointer :: p
  509. ! 1. create new node
  510. call jsoncore%create_object(p,'Problems')
  511. ! 2. add member of data type to new node
  512. call BitProblemsToJson(p)
  513. call BopProblemsToJson(p)
  514. call ChokeProblemsToJson(p)
  515. call DrillStemProblemsToJson(p)
  516. call GaugesProblemsToJson(p)
  517. call HoistingProblemsToJson(p)
  518. call KickProblemsToJson(p)
  519. call LostProblemsToJson(p)
  520. call MudTreatmentProblemsToJson(p)
  521. call OtherProblemsToJson(p)
  522. call PumpProblemsToJson(p)
  523. call RotaryProblemsToJson(p)
  524. ! 3. add new node to parent
  525. call jsoncore%add(parent,p)
  526. end subroutine
  527. subroutine writeState() bind(C,name="writeState")
  528. use json_module
  529. implicit none
  530. type(json_value),pointer :: p
  531. type(json_value),pointer :: jsonroot
  532. character(len=10)::str
  533. call jsoncore%initialize()
  534. call jsoncore%create_object(p,'')
  535. call ManifoldToJson(p)
  536. call notificationsToJson(p)
  537. call UnitySignalsToJson(p)
  538. call OperationScenariosToJson(p)
  539. ! call permissionsToJson(p)
  540. ! call StudentStationToJson(p)
  541. ! call BopStackInputToJson(p)
  542. ! call BopStackAccToJson(p)
  543. ! call RamLineToJson(p)
  544. ! call AnnularComputationalToJson(p)
  545. ! call AnnularToJson(p)
  546. ! call PipeRam1ToJson(p)
  547. ! call ShearRamToJson(p)
  548. ! call PipeRam2ToJson(p)
  549. ! call ChokeLineToJson(p)
  550. ! call KillLineToJson(p)
  551. ! call PumpsToJson(p)
  552. ! call RAMToJson(p)
  553. ! call RAMSToJson(p)
  554. ! call ChokeToJson(p)
  555. ! call AirDrivenPumpToJson(p)
  556. ! call AirPumpLineToJson(p)
  557. ! call CHOOKEToJson(p)
  558. ! call DrawworksToJson(p)
  559. ! call MudSystemToJson(p)
  560. ! call MUDToJson(p)
  561. ! call MPumpsToJson(p)
  562. ! call PUMPToJson(p)
  563. ! call RTableToJson(p)
  564. ! call TDSToJson(p)
  565. ! call GasType(3)ToJson(p)
  566. ! call PressureDisplayToJson(p)
  567. ! call FricPressDropToJson(p)
  568. ! call ROP_SpecToJson(p)
  569. ! call ROP_BitToJson(p)
  570. ! call TDGeoToJson(p)
  571. ! call F_String(:)ToJson(p)
  572. ! call F_CountsToJson(p)
  573. ! call F_Interval(:)ToJson(p)
  574. ! call OD_Annulus(4)ToJson(p)
  575. ! call TD_DrillStemToJson(p)
  576. ! call TD_DrillStemsToJson(p)
  577. ! call TD_StringToJson(p)
  578. ! call TD_CountToJson(p)
  579. ! call G_StringElementToJson(p)
  580. ! call TD_VolToJson(p)
  581. ! call TD_GeneralToJson(p)
  582. ! call TD_BOPToJson(p)
  583. ! call TD_BOPElement(4)ToJson(p)
  584. ! call TD_StConnToJson(p)
  585. ! call TD_LoadToJson(p)
  586. ! call TD_WellElToJson(p)
  587. ! call TD_CasingToJson(p)
  588. ! call TD_LinerToJson(p)
  589. ! call TD_OpenHoleToJson(p)
  590. ! call TD_ROPHoleToJson(p)
  591. ! call TD_WellGeneralToJson(p)
  592. ! call TD_WellGeo(:)ToJson(p)
  593. call jsoncore%print_to_string(p,stateStr)
  594. if(log_level>4) then
  595. write(str, '(I0)') len(stateStr)
  596. print *,"Writing to redis:"//trim(stateStr)
  597. endif
  598. call setState(stateStr)
  599. call jsoncore%destroy(jsonroot)
  600. end subroutine
  601. !use this as a template
  602. subroutine notificationsToJson(parent)
  603. type(json_value),pointer :: parent
  604. type(json_value),pointer :: p
  605. ! 1. create new node
  606. call jsoncore%create_object(p,'Notifications')
  607. ! 2. add member of data type to new node
  608. ! 3. add new node to parent
  609. call jsoncore%add(parent,p)
  610. end subroutine
  611. subroutine ConfigurationFromJson(jsonfile)
  612. type(json_file)::jsonfile
  613. type(json_value),pointer :: parent
  614. type(json_value),pointer :: p
  615. ! 1. get related root
  616. call jsonfile%json_file_get_root(parent)
  617. call jsoncore%get(parent,"Configuration",p)
  618. call StringConfigurationFromJson(p)
  619. call FormationFromJson(p)
  620. call ReservoirFromJson(p)
  621. call ShoeFromJson(p)
  622. call AccumulatorFromJson(p)
  623. call BopStackFromJson(p)
  624. call HoistingFromJson(p)
  625. call PowerFromJson(p)
  626. call PumpsFromJson(p)
  627. call RigSizeFromJson(p)
  628. call CasingLinerChokeFromJson(p)
  629. call PathGenerationFromJson(p)
  630. call MudPropertiesFromJson(p)
  631. ! 3. add new node to parent
  632. ! nullify(parent)
  633. end subroutine
  634. subroutine WarningsFromJson(parent)
  635. type(json_value),pointer :: parent
  636. type(json_value),pointer :: p
  637. type(json_value),pointer :: pval
  638. ! 1. get node
  639. call jsoncore%get(parent,'Warnings',p)
  640. ! ! 2. add member of data type to new node
  641. call jsoncore%get(p,'PumpWithKellyDisconnected',pval)
  642. call jsoncore%get(pval,data%Warnings%PumpWithKellyDisconnected)
  643. call jsoncore%get(p,'PumpWithTopdriveDisconnected',pval)
  644. call jsoncore%get(pval,data%Warnings%PumpWithTopdriveDisconnected)
  645. call jsoncore%get(p,'Pump1PopOffValveBlown',pval)
  646. call jsoncore%get(pval,data%Warnings%Pump1PopOffValveBlown)
  647. call jsoncore%get(p,'Pump1Failure',pval)
  648. call jsoncore%get(pval,data%Warnings%Pump1Failure)
  649. call jsoncore%get(p,'Pump2PopOffValveBlown',pval)
  650. call jsoncore%get(pval,data%Warnings%Pump2PopOffValveBlown)
  651. call jsoncore%get(p,'Pump2Failure',pval)
  652. call jsoncore%get(pval,data%Warnings%Pump2Failure)
  653. call jsoncore%get(p,'Pump3PopOffValveBlown',pval)
  654. call jsoncore%get(pval,data%Warnings%Pump3PopOffValveBlown)
  655. call jsoncore%get(p,'Pump3Failure',pval)
  656. call jsoncore%get(pval,data%Warnings%Pump3Failure)
  657. call jsoncore%get(p,'DrawworksGearsAbuse',pval)
  658. call jsoncore%get(pval,data%Warnings%DrawworksGearsAbuse)
  659. call jsoncore%get(p,'RotaryGearsAbuse',pval)
  660. call jsoncore%get(pval,data%Warnings%RotaryGearsAbuse)
  661. call jsoncore%get(p,'HoistLineBreak',pval)
  662. call jsoncore%get(pval,data%Warnings%HoistLineBreak)
  663. call jsoncore%get(p,'PartedDrillString',pval)
  664. call jsoncore%get(pval,data%Warnings%PartedDrillString)
  665. call jsoncore%get(p,'ActiveTankOverflow',pval)
  666. call jsoncore%get(pval,data%Warnings%ActiveTankOverflow)
  667. call jsoncore%get(p,'ActiveTankUnderVolume',pval)
  668. call jsoncore%get(pval,data%Warnings%ActiveTankUnderVolume)
  669. call jsoncore%get(p,'TripTankOverflow',pval)
  670. call jsoncore%get(pval,data%Warnings%TripTankOverflow)
  671. call jsoncore%get(p,'DrillPipeTwistOff',pval)
  672. call jsoncore%get(pval,data%Warnings%DrillPipeTwistOff)
  673. call jsoncore%get(p,'DrillPipeParted',pval)
  674. call jsoncore%get(pval,data%Warnings%DrillPipeParted)
  675. call jsoncore%get(p,'TripWithSlipsSet',pval)
  676. call jsoncore%get(pval,data%Warnings%TripWithSlipsSet)
  677. call jsoncore%get(p,'Blowout',pval)
  678. call jsoncore%get(pval,data%Warnings%Blowout)
  679. call jsoncore%get(p,'UndergroundBlowout',pval)
  680. call jsoncore%get(pval,data%Warnings%UndergroundBlowout)
  681. call jsoncore%get(p,'MaximumWellDepthExceeded',pval)
  682. call jsoncore%get(pval,data%Warnings%MaximumWellDepthExceeded)
  683. call jsoncore%get(p,'CrownCollision',pval)
  684. call jsoncore%get(pval,data%Warnings%CrownCollision)
  685. call jsoncore%get(p,'FloorCollision',pval)
  686. call jsoncore%get(pval,data%Warnings%FloorCollision)
  687. call jsoncore%get(p,'TopdriveRotaryTableConfilict',pval)
  688. call jsoncore%get(pval,data%Warnings%TopdriveRotaryTableConfilict)
  689. end subroutine
  690. subroutine ProblemsFromJson(jsonfile)
  691. type(json_value),pointer :: parent
  692. type(json_value),pointer :: p
  693. type(json_file)::jsonfile
  694. call jsonfile%json_file_get_root(parent)
  695. call jsoncore%get(parent,'Warnings',p)
  696. call BitProblemsToJson(p)
  697. call BopProblemsFromJson(p)
  698. call ChokeProblemsFromJson(p)
  699. call DrillStemProblemsFromJson(p)
  700. call GaugesProblemsFromJson(p)
  701. call HoistingProblemsFromJson(p)
  702. call KickProblemsFromJson(p)
  703. call LostProblemsFromJson(p)
  704. call MudTreatmentProblemsFromJson(p)
  705. call OtherProblemsFromJson(p)
  706. call PumpProblemsFromJson(p)
  707. call RotaryProblemsFromJson(p)
  708. end subroutine
  709. subroutine StateFromJson(jsonfile)
  710. type(json_value),pointer :: p
  711. type(json_file)::jsonfile
  712. logical::is_found
  713. ! 1. get related root
  714. ! call jsonfile%json_file_get_root(parent)
  715. ! call jsoncore%get(parent,"Equipments",p)
  716. call jsonfile%get("Equipments",p,is_found)
  717. ! call jsoncore%info(p, n_children=n_children)
  718. ! print *,"number of Equipments =",n_children
  719. ! 2. add member of data type to new node
  720. call BopControlPanelFromJson(p)
  721. call ChokeControlPanelFromJson(jsonfile)
  722. ! 1. create new node
  723. ! call json%create_object(p,'State')
  724. ! ! call OperationScenarioFromJson(p)
  725. ! call notificationsFromJson(p)
  726. ! call permissionsFromJson(p)
  727. ! call unitySignalsFromJson(p)
  728. ! call StudentStationFromJson(p)
  729. ! call BopStackInputFromJson(p)
  730. ! call BopStackAccFromJson(p)
  731. ! call RamLineFromJson(p)
  732. ! call AnnularComputationalFromJson(p)
  733. ! call AnnularFromJson(p)
  734. ! call PipeRam1FromJson(p)
  735. ! call ShearRamFromJson(p)
  736. ! call PipeRam2FromJson(p)
  737. ! call ChokeLineFromJson(p)
  738. ! call KillLineFromJson(p)
  739. ! call PumpsFromJson(p)
  740. ! call RAMFromJson(p)
  741. ! call RAMSFromJson(p)
  742. ! call ChokeFromJson(p)
  743. ! call AirDrivenPumpFromJson(p)
  744. ! call AirPumpLineFromJson(p)
  745. ! call CHOOKEFromJson(p)
  746. ! call DrawworksFromJson(p)
  747. ! call MudSystemFromJson(p)
  748. ! call MUDFromJson(p)
  749. ! call MPumpsFromJson(p)
  750. ! call PUMPFromJson(p)
  751. ! call RTableFromJson(p)
  752. ! call TDSFromJson(p)
  753. ! call GasType(3)FromJson(p)
  754. ! call PressureDisplayFromJson(p)
  755. ! call FricPressDropFromJson(p)
  756. ! call ROP_SpecFromJson(p)
  757. ! call ROP_BitFromJson(p)
  758. ! call TDGeoFromJson(p)
  759. ! call F_String(:)FromJson(p)
  760. ! call F_CountsFromJson(p)
  761. ! call F_Interval(:)FromJson(p)
  762. ! call OD_Annulus(4)FromJson(p)
  763. ! call TD_DrillStemFromJson(p)
  764. ! call TD_DrillStemsFromJson(p)
  765. ! call TD_StringFromJson(p)
  766. ! call TD_CountFromJson(p)
  767. ! call G_StringElementFromJson(p)
  768. ! call TD_VolFromJson(p)
  769. ! call TD_GeneralFromJson(p)
  770. ! call TD_BOPFromJson(p)
  771. ! call TD_BOPElement(4)FromJson(p)
  772. ! call TD_StConnFromJson(p)
  773. ! call TD_LoadFromJson(p)
  774. ! call TD_WellElFromJson(p)
  775. ! call TD_CasingFromJson(p)
  776. ! call TD_LinerFromJson(p)
  777. ! call TD_OpenHoleFromJson(p)
  778. ! call TD_ROPHoleFromJson(p)
  779. ! call TD_WellGeneralFromJson(p)
  780. ! call TD_WellGeo(:)FromJson(p)
  781. ! 2. add member of data type to new node
  782. ! 3. add new node to parent
  783. ! call jsoncore%add(parent,p)
  784. end subroutine
  785. !use this as a template
  786. subroutine notificationsFromJson(parent)
  787. type(json_value),pointer :: parent
  788. !
  789. ! type(json_value),pointer :: p
  790. ! 1. create new node
  791. ! call json%create_object(p,'Notifications')
  792. ! ! 2. add member of data type to new node
  793. ! ! 3. add new node to parent
  794. ! call json%add(parent,p)
  795. end subroutine
  796. ! subroutine fireOperationScenarioEvent()
  797. ! print *,"fireOperationScenarioEvent starts"
  798. ! if(operationScenarioEvent=='Kelly_ConnectionNothing'.and. .not. Kelly_ConnectionNothing_in_progress) then
  799. ! Kelly_ConnectionNothing_in_progress = .true.
  800. ! print *, "Starting Kelly_ConnectionNothing"
  801. ! call Kelly_ConnectionNothing()
  802. ! print *, "Kelly_ConnectionNothing ends."
  803. ! Kelly_ConnectionNothing_in_progress = .false.
  804. ! endif
  805. ! end subroutine
  806. end module Simulator