Old-style Programmable Filter

First, I know about the NumPy interface. It isn’t doing what I need it to. See the other discussion: Slow Programmable Filter

I’ve written a programmable filter for some analysis I’m doing, but it’s returning odd results. I’m expecting a new CellData field, but I’m getting something that looks like it’s averaged over blocks (in addition to being wrong). Does anyone have any insight?


This is approximately what I would expect to see.

def flatten(input, output):
    output.ShallowCopy(input)
    numCells = output.GetNumberOfCells()
    cellData = input.GetCellData()
    r = cellData.GetArray('radius')
    v = cellData.GetArray('vol')
    d = cellData.GetArray('den')

    def base_bump(t, tau):
        x = (t-tau)**2
        if x > 1:
            out = 0
        else: 
            out = exp(1/(x-1))/0.443993816237631
        return out

    def bump(r, rp, h):
        return base_bump(r/h, rp/h)**(1/h)

    h = 1
    dtype = r.GetDataType()
    out = r.NewInstance()
    out.SetName("denbar")
    for indi in range(0, numCells):
        temp = 0
        for indj in range(0, numCells):
            temp += d.GetValue(indj)*v.GetValue(indj)*bump(
                r.GetValue(indi),r.GetValue(indj),h) 
        out.InsertValue(indi,temp) 
    output.GetCellData().AddArray(out)

input = self.GetInputDataObject(0, 0)
output = self.GetOutputDataObject(0)

if input.IsA("vtkMultiBlockDataSet"):
    output.CopyStructure(input)
    iter = input.NewIterator()
    iter.UnRegister(None)
    iter.InitTraversal()
    while not iter.IsDoneWithTraversal():
        curInput = iter.GetCurrentDataObject()
        curOutput = curInput.NewInstance()
        curOutput.UnRegister(None)
        output.SetDataSet(iter, curOutput)
        flatten(curInput, curOutput)
        iter.GoToNextItem();
else:
  flatten(input, output)

Just confirm that the code is correct, try this: apply Merge Blocks filter after PointDataToCellData and then apply the programmable filter to this Merge Blocks. Are you seeing the same thing?

p.s. a small performance tweak:

...
    out = r.NewInstance()
    out.SetName("denbar")
   # allocate memory at once so each call to `out.InsertValue(...)` 
   # later doesn't cause costly memory resizes
    out.Allocate(numCells)

It seems to be averaging over the whole domain, now. It’s more correct, though. The inner sum is supposed to be carried out over the entire domain, and it wasn’t before.

Unfortunately, it now takes overnight. If I wanted to speed this up (aside from the NumPy interface), I would have to write a VTK plugin, correct?