detail.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. r"""
  2. Internal functions not intended for use by users. This may change without
  3. notice.
  4. """
  5. from .. import logger
  6. from ..modules.vtkPVInSitu import vtkInSituPipelinePython
  7. from ..modules.vtkPVPythonCatalyst import vtkCPPythonScriptV2Helper
  8. def _get_active_helper():
  9. return vtkCPPythonScriptV2Helper.GetActiveInstance()
  10. def _get_active_data_description():
  11. if not IsInsitu():
  12. return None
  13. helper = _get_active_helper()
  14. return helper.GetDataDescription()
  15. def _transform_registration_name(name):
  16. from . import get_args
  17. import re, argparse
  18. regex = re.compile(r"^\${args\.([\w.-]+)}$")
  19. m = regex.match(name)
  20. if not m:
  21. return name
  22. argname = m.group(1)
  23. parser = argparse.ArgumentParser()
  24. parser.add_argument("--%s" % argname, dest=argname)
  25. parser.add_argument("-%s" % argname, dest=argname)
  26. result = parser.parse_known_args(get_args())[0]
  27. val = getattr(result, argname)
  28. return val if val else name
  29. def IsInsitu():
  30. """Returns True if executing in an insitu environment, else false"""
  31. helper = _get_active_helper()
  32. return (helper is not None)
  33. def IsLegacyCatalystAdaptor():
  34. """Returns True if the active execution environment is from within a legacy
  35. Catalyst adaptor implementation that uses vtkCPProcessor etc. manually,
  36. instead of the Conduit-based in situ API"""
  37. return _get_active_data_description() is not None
  38. if IsInsitu() and not IsLegacyCatalystAdaptor():
  39. from ..modules.vtkPVInSitu import vtkInSituInitializationHelper
  40. def IsCatalystInSituAPI():
  41. """Returns True if the active execution environment is from within
  42. an implementation of the Conduit-based Catalyst In Situ API."""
  43. return IsInsitu() and not IsLegacyCatalystAdaptor() and vtkInSituInitializationHelper.IsInitialized()
  44. def IsInsituInput(name):
  45. if not name or not IsInsitu():
  46. return False
  47. name = _transform_registration_name(name)
  48. dataDesc = _get_active_data_description()
  49. if IsLegacyCatalystAdaptor():
  50. # Legacy Catalyst
  51. if dataDesc.GetInputDescriptionByName(name) is not None:
  52. return True
  53. elif IsCatalystInSituAPI() and (vtkInSituInitializationHelper.GetProducer(name) is not None):
  54. # Catalyst 2.0
  55. return True
  56. return False
  57. def RegisterExtractor(extractor):
  58. """Keeps track of extractors created inside a specific Catalyst
  59. script. This is useful to ensure we only update the extractors for that
  60. current script when multiple scripts are being executed in the same run.
  61. """
  62. assert IsInsitu()
  63. _get_active_helper().RegisterExtractor(extractor.SMProxy)
  64. def RegisterView(view):
  65. """Keeps track of views created inside a specific Catalyst
  66. script. This is useful to ensure we only update the views for the
  67. current script when multiple scripts are being executed in the same run.
  68. """
  69. assert IsInsitu()
  70. _get_active_helper().RegisterView(view.SMProxy)
  71. if IsCatalystInSituAPI():
  72. view.ViewTime = vtkInSituInitializationHelper.GetTime()
  73. else:
  74. view.ViewTime = _get_active_data_description().GetTime()
  75. def CreateProducer(name):
  76. assert IsInsituInput(name)
  77. from . import log_level
  78. from .. import log
  79. originalname = name
  80. name = _transform_registration_name(name)
  81. log(log_level(), "creating producer for simulation input named '%s' (original-name=%s)" \
  82. % (name, originalname))
  83. if IsCatalystInSituAPI():
  84. # Catalyst 2.0
  85. from paraview import servermanager
  86. producer = servermanager._getPyProxy(vtkInSituInitializationHelper.GetProducer(name))
  87. # since state file may have arbitrary properties being specified
  88. # on the original source, we ensure we ignore them
  89. producer.IgnoreUnknownSetRequests = True
  90. return producer
  91. # Legacy Catalyst
  92. from paraview import servermanager
  93. helper = _get_active_helper()
  94. producer = helper.GetTrivialProducer(name)
  95. producer = servermanager._getPyProxy(producer)
  96. # since state file may have arbitrary properties being specified
  97. # on the original source, we ensure we ignore them
  98. producer.IgnoreUnknownSetRequests = True
  99. return producer
  100. def InitializePythonEnvironment():
  101. """
  102. This is called in vtkCPPythonPipeline to initialize the Python
  103. environment to be more suitable for Catalyst in situ execution.
  104. """
  105. from . import log_level
  106. from .. import log
  107. log(log_level(), "initializing Python environment for in situ")
  108. # disable writing bytecode, we don't want to generate pyc files
  109. # when running in situ.
  110. log(log_level(), "disable byte-code generation")
  111. import sys
  112. sys.dont_write_bytecode = True
  113. # ensure we can import ParaView servermanager and necessary modules
  114. log(log_level(), "import paraview.servermanager")
  115. from paraview import servermanager
  116. # import Python wrapping for vtkPVCatalyst module
  117. log(log_level(), "import paraview.modules.vtkPVCatalyst")
  118. from paraview.modules import vtkPVCatalyst
  119. def RegisterPackageFromZip(zipfilename, packagename=None):
  120. from . import importers
  121. zipfilename = _mpi_exchange_if_needed(zipfilename)
  122. return importers.add_file(zipfilename, packagename)
  123. def RegisterPackageFromDir(path):
  124. import os.path
  125. from . import importers
  126. packagename = os.path.basename(path)
  127. init_py = os.path.join(path, "__init__.py")
  128. return importers.add_file(init_py, packagename)
  129. def RegisterModuleFromFile(filename):
  130. from . import importers
  131. return importers.add_file(filename)
  132. _temp_directory = None
  133. def _mpi_exchange_if_needed(filename):
  134. global _temp_directory
  135. from ..modules.vtkRemotingCore import vtkProcessModule
  136. pm = vtkProcessModule.GetProcessModule()
  137. if pm.GetNumberOfLocalPartitions() <= 1:
  138. return filename
  139. from mpi4py import MPI
  140. from vtkmodules.vtkParallelMPI4Py import vtkMPI4PyCommunicator
  141. comm = vtkMPI4PyCommunicator.ConvertToPython(pm.GetGlobalController().GetCommunicator())
  142. if comm.Get_rank() == 0:
  143. with open(filename, 'rb') as f:
  144. data = f.read()
  145. else:
  146. data = None
  147. data = comm.bcast(data, root=0)
  148. if comm.Get_rank() == 0:
  149. return filename
  150. else:
  151. if not _temp_directory:
  152. import tempfile, os.path
  153. # we hook the temp-dir to the module so it lasts for the livetime of
  154. # the interpreter and gets cleaned up on exit.
  155. _temp_directory = tempfile.TemporaryDirectory()
  156. filename = os.path.join(_temp_directory.name, os.path.basename(filename))
  157. with open(filename, "wb") as f:
  158. f.write(data)
  159. return filename