live.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. r"""This is a module to perform live visualization in the context of Catalyst.
  2. """
  3. from paraview.simple import *
  4. from paraview import servermanager
  5. # -----------------------------------------------------------------------------
  6. def ConnectToCatalyst(ds_host='localhost', ds_port=22222):
  7. """Creates a connection to a catalyst simulation. Example usage:
  8. > Connect("amber") # Connect to a single server at default port
  9. > Connect("amber", 12345) # Connect to a single server at port 12345
  10. return a LiveInsituLink object.
  11. """
  12. displaySession = servermanager.ActiveConnection.Session
  13. insituLink = servermanager.ConnectToCatalyst(ds_host, ds_port)
  14. ProcessServerNotifications()
  15. insituLink.InvokeCommand('Initialize')
  16. SetActiveConnection(servermanager.GetConnectionFromSession(displaySession))
  17. return insituLink
  18. # -----------------------------------------------------------------------------
  19. def ExtractCatalystData(link, name):
  20. ''' Extract data called "name" from simulation managed by the LiveInsituLink link.
  21. Register it in displaySession
  22. '''
  23. displaySession = servermanager.ActiveConnection.Session
  24. proxy = link.CreateExtract('sources', name, 0)
  25. displaySession.GetSessionProxyManager().RegisterProxy('sources', name, proxy)
  26. data = FindSource(name)
  27. return data
  28. # -----------------------------------------------------------------------------
  29. def PauseCatalyst(link, pause = True):
  30. ''' Pause / Unpause the Catalyst simulation managed by the LiveInsituLink link.
  31. '''
  32. currentState = link.GetProperty('SimulationPaused').GetElement(0) == 1
  33. if currentState == pause:
  34. return
  35. link.GetProperty('SimulationPaused').SetElement(0, 1 if pause else 0)
  36. link.UpdateVTKObjects()
  37. if not pause:
  38. link.InvokeCommand('LiveChanged')
  39. # -----------------------------------------------------------------------------
  40. def ProcessServerNotifications():
  41. ''' Processes pending events from server.
  42. '''
  43. pm = servermanager.vtkProcessModule.GetProcessModule()
  44. nam = pm.GetNetworkAccessManager()
  45. while nam.ProcessEvents(1) == 1:
  46. pass