Python coprocessing example: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
(Created page with 'This example is used to demonstrate how the coprocessing library can be used with a python based simulation code. Note that this example requires MPI to be available on your syst…')
 
mNo edit summary
Line 1: Line 1:
This example is used to demonstrate how the coprocessing library can be used with a python based simulation code. Note that this example requires MPI to be available on your system as well as pyMPI to initialize and finalize MPI from the python script. The executable takes in a python coprocessing script and a number of time steps to be run for. Note to remember to set your system environment propery. See [[1]] for details.  
This example is used to demonstrate how the coprocessing library can be used with a python based simulation code. Note that this example requires MPI to be available on your system as well as pyMPI to initialize and finalize MPI from the python script. The executable takes in a python coprocessing script and a number of time steps to be run for. Note to remember to set your system environment properly. See [[1]] for details.  





Revision as of 18:01, 20 August 2010

This example is used to demonstrate how the coprocessing library can be used with a python based simulation code. Note that this example requires MPI to be available on your system as well as pyMPI to initialize and finalize MPI from the python script. The executable takes in a python coprocessing script and a number of time steps to be run for. Note to remember to set your system environment properly. See 1 for details.


Python driver code

<source lang="python"> import sys if len(sys.argv) != 3:

   print "command is 'pyMPI <python driver code> <script name> <number of time steps>'"
   sys.exit(1)

import mpi mpi.initialized() import paraview import paraview.vtk as vtk

  1. initialize and read input parameters

paraview.options.batch = True paraview.options.symmetric = True

def _refHolderMaker(obj):

   def _refHolder(obj2, string):

tmp = obj

   return _refHolder

def coProcess(grid, time, step, scriptname):

   import libvtkCoProcessorPython #on windows do import vtkCoProcessorPython
   try:

cpscript = __import__(scriptname) # this doesn't seem to work properly for me

   except:
       print 'Cannot find ', scriptname, ' -- no coprocessing will be performed.'
       return
   datadescription = libvtkCoProcessorPython.vtkCPDataDescription()
   datadescription.SetTimeData(time, step)
   datadescription.AddInput("input")
   cpscript.RequestDataDescription(datadescription)
   inputdescription = datadescription.GetInputDescriptionByName("input")
   if inputdescription.GetIfGridIsNecessary() == False:
       return
   import paraview.simple
   import paraview.vtk as vtk
   import paraview.numpy_support
   inputdescription.SetGrid(grid)
   cpscript.DoCoProcessing(datadescription)

try:

   numsteps = int(sys.argv[2])

except ValueError:

   print 'the last argument should be a number'
   numsteps = 10


for step in range(numsteps):

   # assume simulatione time is from 0 to 1
   time = step/float(numsteps-1)
   # create the input to the coprocessing library.  normally
   # this will come from the adaptor
   imageData = vtk.vtkImageData()
   imageData.SetOrigin(0, 0, 0)
   imageData.SetSpacing(.1, .1, .1)
   imageData.SetDimensions(10, 12, 12)
   pointArray = vtk.vtkDoubleArray()
   pointArray.SetNumberOfTuples(imageData.GetNumberOfPoints())
   for i in range(imageData.GetNumberOfPoints()):
       pointArray.SetValue(i, i)
   pointArray.SetName("pointData")
   imageData.GetPointData().AddArray(pointArray)
   # "perform" coprocessing.  results are outputted only if
   # the passed in script says we should at time/step
   coProcess(imageData, time, step, sys.argv[1])

mpi.finalized() </source>


Sample coprocessing script

<source lang="python"> try: paraview.simple except: from paraview.simple import * cp_writers = []

def RequestDataDescription(datadescription):

   "Callback to populate the request for current timestep"
   timestep = datadescription.GetTimeStep()
   input_name = 'input'
   if (timestep % 1 == 0) :
       datadescription.GetInputDescriptionByName(input_name).AllFieldsOn()
       datadescription.GetInputDescriptionByName(input_name).GenerateMeshOn()
   else:
       datadescription.GetInputDescriptionByName(input_name).AllFieldsOff()
       datadescription.GetInputDescriptionByName(input_name).GenerateMeshOff()

def DoCoProcessing(datadescription):

   "Callback to do co-processing for current timestep"
   global cp_writers
   cp_writers = []
   timestep = datadescription.GetTimeStep()
   Sphere1 = CreateProducer( datadescription, "input" )
   ImageWriter1 = CreateWriter( XMLPImageDataWriter, "input_grid_%t.pvti", 1 )
   for writer in cp_writers:
       if timestep % writer.cpFrequency == 0:
           writer.FileName = writer.cpFileName.replace("%t", str(timestep))
           writer.UpdatePipeline()
   # explicitly delete the proxies -- we do it this way to avoid problems with prototypes
   tobedeleted = GetProxiesToDelete()
   while len(tobedeleted) > 0:
       Delete(tobedeleted[0])
       tobedeleted = GetProxiesToDelete()

def GetProxiesToDelete():

   iter = servermanager.vtkSMProxyIterator()
   iter.Begin()
   tobedeleted = []
   while not iter.IsAtEnd():
     if iter.GetGroup().find("prototypes") != -1:
        iter.Next()
        continue
     proxy = servermanager._getPyProxy(iter.GetProxy())
     proxygroup = iter.GetGroup()
     iter.Next()
     if proxygroup != 'timekeeper' and proxy != None and proxygroup.find("pq_helper_proxies") == -1 :
         tobedeleted.append(proxy)
   return tobedeleted

def CreateProducer(datadescription, gridname):

 "Creates a producer proxy for the grid"
 if not datadescription.GetInputDescriptionByName(gridname):
   raise RuntimeError, "Simulation input name '%s' does not exist" % gridname
 grid = datadescription.GetInputDescriptionByName(gridname).GetGrid()
 producer = TrivialProducer()
 producer.GetClientSideObject().SetOutput(grid)
 producer.UpdatePipeline()
 return producer

def CreateWriter(proxy_ctor, filename, freq):

   global cp_writers
   writer = proxy_ctor()
   writer.FileName = filename
   writer.add_attribute("cpFrequency", freq)
   writer.add_attribute("cpFileName", filename)
   cp_writers.append(writer)
   return writer

</source>