Esempio n. 1
0
def meshToVTK(path, v, f, pointData):
    assert v.shape[1] == f.shape[1] == 3
    # assert c.shape[0] == v.shape[0]

    offsets = np.arange(start=3,
                        stop=3 * f.shape[0] + 1,
                        step=3,
                        dtype=np.int32)

    cell_types = np.empty(f.shape[0], dtype=np.uint8)
    cell_types[:] = VtkTriangle.tid

    # connectivity = np.empty(3 * f.shape[0], dtype=np.int32)
    connectivity = np.ravel(f).astype(np.int32)

    (x, y, z) = (np.ravel(v[:, 0]), np.ravel(v[:, 1]), np.ravel(v[:, 2]))

    w = VtkFile(path, VtkUnstructuredGrid)
    w.openGrid()
    w.openPiece(ncells=f.shape[0], npoints=v.shape[0])

    w.openElement("Points")
    w.addData("points", (x, y, z))
    w.closeElement("Points")
    w.openElement("Cells")
    w.addData("connectivity", connectivity)
    w.addData("offsets", offsets)
    w.addData("types", cell_types)
    w.closeElement("Cells")

    _addDataToFile(w, cellData=None, pointData=pointData)

    w.closePiece()
    w.closeGrid()
    w.appendData((x, y, z))
    w.appendData(connectivity)
    w.appendData(offsets)
    w.appendData(cell_types)

    _appendDataToFile(w, cellData=None, pointData=pointData)

    w.save()
    return w.getFileName()
Esempio n. 2
0
def meshToVTK(path, v, f, pointData):
	assert v.shape[1] == f.shape[1] == 3
	# assert c.shape[0] == v.shape[0]
	
	offsets = np.arange(start = 3, stop = 3 * f.shape[0] + 1,
		step = 3, dtype=np.int32)
	
	cell_types = np.empty(f.shape[0], dtype=np.uint8)
	cell_types[:] = VtkTriangle.tid
	
	# connectivity = np.empty(3 * f.shape[0], dtype=np.int32)
	connectivity = np.ravel(f).astype(np.int32)
	
	(x, y, z) = (np.ravel(v[:, 0]),
		np.ravel(v[:, 1]),
		np.ravel(v[:, 2]))
	
	w = VtkFile(path, VtkUnstructuredGrid)
	w.openGrid()
	w.openPiece(ncells = f.shape[0], npoints = v.shape[0])
	
	w.openElement("Points")
	w.addData("points", (x, y, z))
	w.closeElement("Points")
	w.openElement("Cells")
	w.addData("connectivity", connectivity)
	w.addData("offsets", offsets)
	w.addData("types", cell_types)
	w.closeElement("Cells")
	
	_addDataToFile(w, cellData = None, pointData = pointData)
	
	w.closePiece()
	w.closeGrid()
	w.appendData((x, y, z))
	w.appendData(connectivity)
	w.appendData(offsets)
	w.appendData(cell_types)
	
	_appendDataToFile(w, cellData = None, pointData = pointData)
	
	w.save()
	return w.getFileName()
Esempio n. 3
0
def write_vtp_header(path, prefix, 
                     vertices, connectivity,
                     offsets, nPoints, nPolygons, variable_list, outType, xtime=None): 
    vtkFile = VtkFile("{}/{}".format(path, prefix), VtkPolyData)

    #if xtime is not None:
    #    vtkFile.openElement(str("metadata"))
    #    vtkFile.openElement(str("xtime"))
    #    vtkFile.xml.addText(str(xtime))
    #    vtkFile.closeElement(str("xtime"))
    #    vtkFile.closeElement(str("metadata"))

    vtkFile.openElement(vtkFile.ftype.name)
    vtkFile.openPiece(npoints=nPoints, npolys=nPolygons)

    vtkFile.openElement(str("Points"))
    vtkFile.addData(str("points"), vertices)
    vtkFile.closeElement(str("Points"))

    vtkFile.openElement(str("Polys"))
    vtkFile.addData(str("connectivity"), connectivity)
    vtkFile.addData(str("offsets"), offsets)
    vtkFile.closeElement(str("Polys"))

    vtkFile.openData(str("Point"),
                     scalars='solution')
    for var in variable_list:
        if var in vec_vars:
          vtkFile.addHeader('x'+var, outType, nPoints, 1)
          vtkFile.addHeader('y'+var, outType, nPoints, 1)
          vtkFile.addHeader('z'+var, outType, nPoints, 1)
        else:
          vtkFile.addHeader(var, outType, nPoints, 1)
    vtkFile.closeData(str("Point"))

    vtkFile.closePiece()
    vtkFile.closeElement(vtkFile.ftype.name)

    vtkFile.appendData(vertices)
    vtkFile.appendData(connectivity)
    vtkFile.appendData(offsets)

    return vtkFile  
Esempio n. 4
0
w.openGrid(start = start, end = end)
w.openPiece( start = start, end = end)

# Point data
#passive = np.zeros(npoints, dtype="float64", order='F')
#w.openData("Point", scalars = "Passive")
#w.addData("Passive", passive)
#w.closeData("Point")

# Cell data
#pressure = np.zeros([nx, ny, nz], dtype="float64", order='F')
if mhd:
  w.openData("Cell", scalars = ("Density","RhoNeutros","GasPressure"), vectors = ("Velocity","Magnetic"))
else:
  w.openData("Cell", scalars = "Density", vectors = ("Velocity"))
w.addData("Density", rrho)
w.addData("Velocity", (vvx,vvy,vvz))
w.addData("GasPressure",ppgas)
if mhd:
  w.addData("Magnetic", (bbx,bby,bbz))
w.addData("RhoNeutros", rrho_n)
w.closeData("Cell")

# 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");

w.closePiece()
Esempio n. 5
0
ncells = nx * ny * nz
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, lz + 0.1 * dz, dz, dtype='float64')
start, end = (0, 0, 0), (nx, ny, nz)

w = VtkFile("./evtk_test", VtkRectilinearGrid)
w.openGrid(start=start, end=end)
w.openPiece(start=start, end=end)

# Point data
temp = np.random.rand(npoints)
vx = vy = vz = np.zeros([nx + 1, ny + 1, nz + 1], dtype="float64", order='F')
w.openData("Point", scalars="Temperature", vectors="Velocity")
w.addData("Temperature", temp)
w.addData("Velocity", (vx, vy, vz))
w.closeData("Point")

# Cell data
pressure = np.zeros([nx, ny, nz], dtype="float64", order='F')
w.openData("Cell", scalars="Pressure")
w.addData("Pressure", pressure)
w.closeData("Cell")

# 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")
Esempio n. 6
0
def _write_vtu_series(grid, coordinates, connectivity, data, filename_base,
                      last_step, is_cell_data):
    steps = last_step + 1 if last_step is not None else len(data)
    fn_tpl = "{}_{:08d}"

    npoints = len(coordinates[0])
    ncells = len(connectivity)

    ref = grid.reference_element
    if ref is ref is referenceelements.triangle:
        points_per_cell = 3
        vtk_el_type = VtkTriangle.tid
    elif ref is referenceelements.square:
        points_per_cell = 4
        vtk_el_type = VtkQuad.tid
    else:
        raise NotImplementedError(
            "vtk output only available for grids with triangle or rectangle reference elments"
        )

    connectivity = connectivity.reshape(-1)
    cell_types = np.empty(ncells, dtype='uint8')
    cell_types[:] = vtk_el_type
    offsets = np.arange(start=points_per_cell,
                        stop=ncells * points_per_cell + 1,
                        step=points_per_cell,
                        dtype='int32')

    group = VtkGroup(filename_base)
    for i in range(steps):
        fn = fn_tpl.format(filename_base, i)
        vtk_data = data[i, :]
        w = VtkFile(fn, VtkUnstructuredGrid)
        w.openGrid()
        w.openPiece(ncells=ncells, npoints=npoints)

        w.openElement("Points")
        w.addData("Coordinates", coordinates)
        w.closeElement("Points")
        w.openElement("Cells")
        w.addData("connectivity", connectivity)
        w.addData("offsets", offsets)
        w.addData("types", cell_types)
        w.closeElement("Cells")
        if is_cell_data:
            _addDataToFile(w, cellData={"Data": vtk_data}, pointData=None)
        else:
            _addDataToFile(w, cellData=None, pointData={"Data": vtk_data})

        w.closePiece()
        w.closeGrid()
        w.appendData(coordinates)
        w.appendData(connectivity).appendData(offsets).appendData(cell_types)
        if is_cell_data:
            _appendDataToFile(w, cellData={"Data": vtk_data}, pointData=None)
        else:
            _appendDataToFile(w, cellData=None, pointData={"Data": vtk_data})

        w.save()
        group.addFile(filepath=fn + '.vtu', sim_time=i)
    group.save()
Esempio n. 7
0
    new_dir = 'paraview'
new_data = new_dir + '/data'
os.system('mkdir -p ' + new_dir)
os.system('mkdir -p ' + new_data)

#Add to write file.
for step in times:
    filename = new_data + '/' + str(step)
    w = VtkFile(filename, VtkStructuredGrid)  #evtk_test0

    w.openGrid(start=start, end=end)
    w.openPiece(start=start, end=end)

    w.openData("Point", scalars=scalar_vars_string, vectors="Velocity")
    for key in scalar_vars:
        w.addData(str(key), np.array(f[key][step]))
    vx = np.array(f['u'][step])
    vy = np.array(f['v'][step])
    vz = np.array(f['w'][step])
    w.addData("Velocity", (vx, vy, vz))
    w.closeData("Point")

    # Coordinates of cell vertices
    w.openElement("Points")
    w.addData("points", (X, Y, Z))
    w.closeElement("Points")

    w.closePiece()
    w.closeGrid()

    # Need to modify parameters
Esempio n. 8
0
start, end = (1, 1, 1), (nx, ny, nz)  #Modify 0->1
if (len(sys.argv) == 2):
    new_dir = sys.argv[1]
else:
    new_dir = 'paraview'
new_data = new_dir + '/data'
os.system('mkdir -p ' + new_dir)
os.system('mkdir -p ' + new_data)
for step in times:
    filename = new_data + '/' + str(step)
    w = VtkFile(filename, VtkStructuredGrid)  #evtk_test0
    w.openGrid(start=start, end=end)
    w.openPiece(start=start, end=end)
    w.openData('Point', scalars=scalar_vars_string, vectors='Velocity')
    for key in scalar_vars:
        w.addData(str(key), np.array(f[key][step]))
    vx = np.array(f['u'][step])
    vy = np.array(f['v'][step])
    vz = np.array(f['w'][step])
    w.addData('Velocity', (vx, vy, vz))
    w.closeData('Point')
    w.openElement('Points')
    w.addData('points', (X, Y, Z))
    w.closeElement('Points')
    w.closePiece()
    w.closeGrid()
    for key in scalar_vars:
        w.appendData(data=np.array(f[key][step]))
    w.appendData(data=(vx, vy, vz))
    w.appendData((X, Y, Z))
    w.save()
Esempio n. 9
0
def write_vtp_header(path,
                     prefix,
                     active_var_index,
                     var_indices,
                     variable_list,
                     all_dim_vals,
                     vertices,
                     connectivity,
                     offsets,
                     nPoints,
                     nPolygons,
                     outType,
                     cellData=True,
                     pointData=False,
                     xtime=None):  # {{{
    vtkFile = VtkFile("{}/{}".format(path, prefix), VtkPolyData)

    if xtime is not None:
        vtkFile.openElement(str("metadata"))
        vtkFile.openElement(str("xtime"))
        vtkFile.xml.addText(str(xtime))
        vtkFile.closeElement(str("xtime"))
        vtkFile.closeElement(str("metadata"))

    vtkFile.openElement(vtkFile.ftype.name)
    vtkFile.openPiece(npoints=nPoints, npolys=nPolygons)

    vtkFile.openElement(str("Points"))
    vtkFile.addData(str("points"), vertices)
    vtkFile.closeElement(str("Points"))

    vtkFile.openElement(str("Polys"))
    vtkFile.addData(str("connectivity"), connectivity)
    vtkFile.addData(str("offsets"), offsets)
    vtkFile.closeElement(str("Polys"))

    if (cellData):
        vtkFile.openData(
            str("Cell"),
            scalars=[str(var) for var in variable_list[active_var_index]])
        for iVar in var_indices:
            var_name = variable_list[iVar]
            (out_var_names, dim_list) = \
                get_hyperslab_name_and_dims(var_name, all_dim_vals[var_name])
            for out_var_name in out_var_names:
                vtkFile.addHeader(str(out_var_name), outType, nPolygons, 1)
        vtkFile.closeData(str("Cell"))
    if (pointData):
        vtkFile.openData(
            str("Point"),
            scalars=[str(var) for var in variable_list[active_var_index]])
        for iVar in var_indices:
            var_name = variable_list[iVar]
            (out_var_names, dim_list) = \
                get_hyperslab_name_and_dims(var_name, all_dim_vals[var_name])
            for out_var_name in out_var_names:
                vtkFile.addHeader(str(out_var_name), outType, nPoints, 1)
        vtkFile.closeData(str("Point"))

    vtkFile.closePiece()
    vtkFile.closeElement(vtkFile.ftype.name)

    vtkFile.appendData(vertices)
    vtkFile.appendData(connectivity)
    vtkFile.appendData(offsets)

    return vtkFile  # }}}
Esempio n. 10
0
def gridToVTK_with_start(path, x, y, z, cellData=None, pointData=None, fieldData=None, start=None):
    """
    Write data values as a rectilinear or rectangular grid.
    Parameters
    ----------
    path : str
        name of the file without extension where data should be saved.
    x : array-like
        x coordinate axis.
    y : array-like
        y coordinate axis.
    z : array-like
        z coordinate axis.
    cellData : dict, optional
        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.
    pointData : dict, optional
        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.
    fieldData : dict, optional
        dictionary with variables associated with the field.
        Keys should be the names of the variable stored in each array.
    start : tuple, optional
        start position of data extent.
    Returns
    -------
    str
        Full path to saved file.
    Notes
    -----
    coordinates of the nodes of the grid. They can be 1D or 3D depending if
    the grid should be saved as a rectilinear or logically structured grid,
    respectively.
    Arrays should contain coordinates of the nodes of the grid.
    If arrays are 1D, then the grid should be Cartesian,
    i.e. faces in all cells are orthogonal.
    If arrays are 3D, then the grid should be logically structured
    with hexahedral cells.
    In both cases the arrays dimensions should be
    equal to the number of nodes of the grid.
    """

    import numpy as np
    from pyevtk.vtk import VtkFile, VtkStructuredGrid
    
    # Extract dimensions
    if start is None:
        start = (0, 0, 0)
    s = x.shape
    end = (start[0]+s[0]-1, start[1]+s[1]-1, start[2]+s[2]-1)

    w = VtkFile(path, VtkStructuredGrid)
    w.openGrid(start=start, end=end)
    w.openPiece(start=start, end=end)
    w.openElement("Points")
    w.addData("coordinates", (x, y, z))
    w.closeElement("Points")
    # Point data
    if pointData:
        keys = list(pointData.keys())
        w.openData("Point", scalars=keys[0])
        for key in keys:
            data = pointData[key]
            w.addData(key, data)
        w.closeData("Point")
    # Cell data
    if cellData:
        keys = list(cellData.keys())
        w.openData("Cell", scalars=keys[0])
        for key in keys:
            data = cellData[key]
            w.addData(key, data)
        w.closeData("Cell")
    # Field data
    # https://www.visitusers.org/index.php?title=Time_and_Cycle_in_VTK_files#XML_VTK_files
    if fieldData:
        keys = list(fieldData.keys())
        w.openData("Field")  # no attributes in FieldData
        for key in keys:
            data = fieldData[key]
            w.addData(key, data)
        w.closeData("Field")
    w.closePiece()
    w.closeGrid()
    # Write coordinates
    w.appendData((x, y, z))
    # Write data
    if pointData is not None:
        keys = list(pointData.keys())
        for key in keys:
            data = pointData[key]
            w.appendData(data)
    if cellData is not None:
        keys = list(cellData.keys())
        for key in keys:
            data = cellData[key]
            w.appendData(data)
    if fieldData is not None:
        keys = list(fieldData.keys())
        for key in keys:
            data = fieldData[key]
            w.appendData(data)
    w.save()
    return w.getFileName()