Issue #14 new

Vector Data Support

Anonymous created an issue

This is not really a bug, but I don't know of any other way to contact you other than this issue tracker...

I am a big fan of this package. I use it to translate data into vtk format. I am wondering if there are any plans to support vector data in the future? If not I might consider adding this feature myself, since it would be very useful to me.

Thanks!

Comments (5)

  1. Chris Geroux

    it is basically already there except for the high level function _requiresLargeVTKFileSize. I have replaced lines 108-112:

        if cellData <> None:
            keys = cellData.keys()
            for key in keys:
                data = cellData[key]
                sum = sum + data.size*data.dtype.itemsize
    

    with:

        if cellData <> None:
            tupleType=type(())
            keys = cellData.keys()
            for key in keys:
                data = cellData[key]
                if type(data)==tupleType:
                    sum = sum + data[0].size*data[0].dtype.itemsize
                    sum = sum + data[1].size*data[1].dtype.itemsize
                    sum = sum + data[2].size*data[2].dtype.itemsize
                else:
                    sum = sum + data.size*data.dtype.itemsize
    

    and added to structured.py example

    #make a vector
    velocityx = np.random.rand(ncells).reshape( (nx, ny, nz))
    velocityy = np.random.rand(ncells).reshape( (nx, ny, nz))
    velocityz = np.random.rand(ncells).reshape( (nx, ny, nz))
    velocity=(velocityx,velocityy,velocityz)
    

    and changed

    gridToVTK("./structured", x, y, z, cellData = {"pressure" : pressure}, pointData = {"temp" : temp})
    

    to

    gridToVTK("./structured", x, y, z, cellData = {"pressure" : pressure,"velocity":velocity}, pointData = {"temp" : temp})
    

    I quickly tested it out the resulting output using visit and it seems to work.

  2. Stephen Hamilton

    I ran across this, and I had a similar issue, but I needed to do a vector output for vti files. I made this change to hl.py and it appears to work for me.

    Original: Lines 104-108:

    elif pointData != None:
            keys = list(pointData.keys())
            data = pointData[keys[0]]
            end = data.shape
            end = (end[0] - 1, end[1] - 1, end[2] - 1)
    

    Updated:

    elif pointData != None:
            keys = list(pointData.keys())
            data = pointData[keys[0]]
            if hasattr(data, 'shape'):
                end = data.shape
                end = (end[0] - 1, end[1] - 1, end[2] - 1)
            #Added for vector support...
            elif (data[0].ndim == 3 and data[1].ndim == 3 and data[0].ndim == 3):                        
                keys = list(pointData.keys())
                data = pointData[keys[0]]
                end = data[0].shape
                end = (end[0] - 1, end[1] - 1, end[2] - 1)
    

    I'm not 100% sure this is the correct way to do it, but I was able to get vectors in my VTK file, and it appears they are correct.

  3. chichilalescu

    The code already works fine with vector data. Assume your numpy vector has the shape (3, n, n, n), and is called v. Then your call should be as follows:

    gridToVTK("./structured", x, y, z, pointData = {"v" : (v[0], v[1], v[2])})
    

    Paraview will happily treat v as a vector afterwards.

    It should work identically if the array's shape is (3, n) (for unstructured grids or whatever), and for cellData. I just wasted an hour trying my own hack, and then realized I didn't need to hack anything...

    EDIT: I notice that if I'd read Chris's comment, I wouldn't have wasted my time...

  4. Log in to comment