bridge.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. r"""
  2. This module can be used in Python-based apps (or simulations) to simplify the
  3. code that such apps need to incorporate into their code to use Catalyst.
  4. """
  5. # note, here we deliberately don't import paraview.servermanager and the ilk
  6. # since we don't want to initialize ParaView yet.
  7. import paraview
  8. from paraview.modules.vtkPVCatalyst import vtkCPProcessor, vtkCPDataDescription
  9. from paraview.modules.vtkPVPythonCatalyst import vtkCPPythonScriptV2Pipeline, vtkCPPythonScriptPipeline, vtkCPPythonPipeline
  10. from paraview.modules.vtkRemotingCore import vtkProcessModule
  11. def _sanity_check():
  12. pm = vtkProcessModule.GetProcessModule()
  13. if pm and pm.GetPartitionId() == 0:
  14. paraview.print_warning(\
  15. "Warning: ParaView has been initialized before `initialize` is called")
  16. coprocessor = None
  17. def initialize():
  18. # if ParaView is already initialized, some of the options we set here don't
  19. # get used, hence it's a good thing to warn about it. It's not an error,
  20. # but something that users (and developers) should realize.
  21. # Note, this happens when pvpython/pvbatch is used to execute the Python scripts.
  22. # Once we unity ParaView initialization in these executables and standard
  23. # Python interpreter, this will not be an issue.
  24. _sanity_check()
  25. # initialize ParaView's ServerManager. To do that, we set some
  26. # variables that control how the ParaView will get initialized
  27. # and then simply `import paraview.servermanager`
  28. paraview.options.batch = True
  29. paraview.options.symmetric = True
  30. global coprocessor
  31. coprocessor = vtkCPProcessor()
  32. if not coprocessor.Initialize():
  33. raise RuntimeError("Failed to initialize Catalyst")
  34. def add_pipeline_legacy(scriptname):
  35. global coprocessor
  36. assert coprocessor is not None
  37. pipeline = vtkCPPythonScriptPipeline()
  38. if not pipeline.Initialize(scriptname):
  39. raise RuntimeError("Initialization failed!")
  40. coprocessor.AddPipeline(pipeline)
  41. def add_pipeline_v2(path):
  42. import os.path
  43. global coprocessor
  44. assert coprocessor is not None
  45. pipeline = vtkCPPythonScriptV2Pipeline()
  46. if not pipeline.Initialize(path):
  47. raise RuntimeError("Initialization failed!")
  48. coprocessor.AddPipeline(pipeline)
  49. def add_pipeline(filename, version=0):
  50. import os.path
  51. # if version is specified, use that.
  52. if int(version) == 1:
  53. add_pipeline_legacy(filename)
  54. elif int(version) == 2:
  55. add_pipeline_v2(filename)
  56. elif int(version) == 0:
  57. # if not, try to determine version using the filename.
  58. version = vtkCPPythonPipeline.DetectScriptVersion(filename)
  59. if version == 0:
  60. # default to version 1.0
  61. version = 1
  62. return add_pipeline(filename, version)
  63. else:
  64. raise RuntimeError("Invalid version '%d'" % version)
  65. def coprocess(time, timestep, dataset, name="input", wholeExtent=None):
  66. global coprocessor
  67. dataDesc = vtkCPDataDescription()
  68. dataDesc.AddInput(name)
  69. dataDesc.SetTimeData(time, timestep)
  70. idd = dataDesc.GetInputDescriptionByName(name)
  71. if wholeExtent is not None:
  72. idd.SetWholeExtent(wholeExtent)
  73. if not coprocessor.RequestDataDescription(dataDesc):
  74. # nothing to do.
  75. return False
  76. idd.SetGrid(dataset)
  77. coprocessor.CoProcess(dataDesc)
  78. def finalize():
  79. global coprocessor
  80. coprocessor.Finalize()