Exemplo n.º 1
0
def writeVTK(filename,xdim, ydim, pointData=None, cellData=None):
    """
        Writes data values as a rectilinear or rectangular grid.

        PARAMETERS:
            path: name of the file without extension where data should be saved.
            cellData: dictionary containing arrays with cell centered data.
                      Keys should be the names of the data arrays.
                      Arrays must have the same dimensions in all directions and must contain
                      only scalar data.
            nodeData: dictionary containing arrays with node centered data.
                      Keys should be the names of the data arrays.
                      Arrays must have same dimension in each direction and
                      they should be equal to the dimensions of the cell data plus one and
                      must contain only scalar data.

        RETURNS:
            Full path to saved file.

    """
    nx, ny, nz = xdim, ydim, 0
    lx, ly, lz = xdim/10.0, ydim/10.0, 0
    dx, dy, dz = lx/nx, ly/ny, 0
    ncells = nx * ny
    npoints = (nx + 1) * (ny + 1) * (nz + 1)
    x = np.arange(0, lx + 0.1*dx, dx, dtype='float64')
    y = np.arange(0, ly + 0.1*dy, dy, dtype='float64')
    z = np.arange(0,0, dtype='float64')
    start, end = (0,0,0), (nx, ny, nz)
# Set up object
    w = VtkFile(filename, VtkRectilinearGrid)
#Open XML tags
    w.openGrid(start = start, end = end)
    w.openPiece( start = start, end = end)

# Coordinates of cell vertices
    w.openElement("Coordinates")
    w.addData("x_coordinates", x);
    w.addData("y_coordinates", y);
    w.addData("z_coordinates", z);
    w.closeElement("Coordinates");

# Add data from the dictionary
    #temp = np.random.rand(npoints)
    __addDataToFile(w, cellData, pointData)

#Close XML tags
    w.closePiece()
    w.closeGrid()
#Append Coordinate Data to file in binary form
    w.appendData(x).appendData(y).appendData(z)
#Append Cell and Point Data to file in binary form
    __appendDataToFile(w, cellData, pointData)
    w.save()
    return w.getFileName()
Exemplo n.º 2
0
def triangle_faces_to_VTK(filename, x, y, z, faces, point_data, cell_data):
    vertices = (x, y, z)
    x2 = x * 1.
    y2 = y * 1.
    z2 = z * 1.
    vert2 = (x2, y2, z2)
    w = VtkFile(filename, VtkUnstructuredGrid)
    w.openGrid()
    w.openPiece(npoints=len(x), ncells=len(faces))
    w.openElement("Points")
    w.addData("Points", vertices)
    w.closeElement("Points")

    # Create some temporary arrays to write grid topology.
    ncells = len(faces)
    # Index of last node in each cell.
    offsets = np.arange(start=3, stop=3 * (ncells + 1), step=3, dtype='uint32')
    # Connectivity as unrolled array.
    connectivity = faces.reshape(ncells * 3).astype('int32')
    cell_types = np.ones(ncells, dtype='uint8') * VtkTriangle.tid

    w.openElement("Cells")
    w.addData("connectivity", connectivity)
    w.addData("offsets", offsets)
    w.addData("types", cell_types)
    w.closeElement("Cells")

    _addDataToFile(w, cellData=cell_data, pointData=point_data)

    w.closePiece()
    w.closeGrid()

    w.appendData(vert2)
    w.appendData(connectivity).appendData(offsets).appendData(cell_types)

    _appendDataToFile(w, cellData=cell_data, pointData=point_data)

    w.save()
    return w.getFileName()