### Run Example Script Source: https://github.com/pyscience-projects/pyevtk/blob/master/README.md Execute an example script to generate a points.vtu file. The output file will be created in the current directory. ```bash ./tmp/lib/python2.6/site-packages/examples/points.py ``` -------------------------------- ### Build and Install PyEVTK to a Temporary Location Source: https://github.com/pyscience-projects/pyevtk/blob/master/README.md Build and install the package to a temporary location without affecting the global Python site-packages directory. Adjust the Python version in the export command if necessary. ```bash python setup.py build --debug install --prefix=./tmp ``` ```bash export PYTHONPATH=./tmp/lib/python2.6/site-packages/:$PYTHONPATH ``` -------------------------------- ### Group Time-Dependent Files with VtkGroup Source: https://context7.com/pyscience-projects/pyevtk/llms.txt Creates a .pvd file to link multiple time-step VTK files for animation in Paraview. ```python from pyevtk.vtk import VtkGroup from pyevtk.hl import pointsToVTK import numpy as np # Generate time series data for t in range(4): npoints = 100 x = np.random.rand(npoints) + t * 0.1 y = np.random.rand(npoints) z = np.random.rand(npoints) velocity = np.random.rand(npoints) * (t + 1) pointsToVTK( f"./sim{t:04d}", x, y, z, data={"velocity": velocity} ) # Create group file linking all time steps group = VtkGroup("./time_series") group.addFile(filepath="sim0000.vtu", sim_time=0.0) group.addFile(filepath="sim0001.vtu", sim_time=1.0) group.addFile(filepath="sim0002.vtu", sim_time=2.0) group.addFile(filepath="sim0003.vtu", sim_time=3.0) group.save() # Creates: ./time_series.pvd ``` -------------------------------- ### Export Parallel Grid Files Source: https://context7.com/pyscience-projects/pyevtk/llms.txt Combines multiple piece files from distributed computations into a single parallel VTK file. ```python from pyevtk.hl import gridToVTK, writeParallelVTKGrid import numpy as np # Simulate parallel processing: write individual pieces # Process 0: first half of domain x1 = np.linspace(0, 1, 10) y = np.linspace(0, 1, 10) z = np.linspace(0, 1, 10) gridToVTK("piece.0", x1, y, z, start=(0, 0, 0)) # Process 1: second half of domain x2 = np.linspace(1, 2, 20) gridToVTK("piece.1", x2, y, z, start=(9, 0, 0)) # Create parallel file that combines all pieces writeParallelVTKGrid( "combined_mesh", coordsData=((29, 10, 10), np.float64), # (shape, dtype) starts=[(0, 0, 0), (9, 0, 0)], # Start extent of each piece ends=[(9, 9, 9), (28, 9, 9)], # End extent of each piece sources=["piece.0.vtr", "piece.1.vtr"], # Piece file paths ghostlevel=0 ) # Creates: ./combined_mesh.pvtr ``` -------------------------------- ### Export Point Data with pointsToVTK Source: https://context7.com/pyscience-projects/pyevtk/llms.txt Exports point cloud data as an unstructured grid. Use this for particle simulations or scattered data points. ```python from pyevtk.hl import pointsToVTK import numpy as np # Example 1: Random point cloud with multiple data fields npoints = 100 x = np.random.rand(npoints) y = np.random.rand(npoints) z = np.random.rand(npoints) pressure = np.random.rand(npoints) temp = np.random.rand(npoints) filepath = pointsToVTK( "./random_points", x, y, z, data={"temp": temp, "pressure": pressure} ) # Returns: ./random_points.vtu # Example 2: Line of points (e.g., trajectory) x = np.arange(1.0, 10.0, 0.1) y = np.arange(1.0, 10.0, 0.1) z = np.arange(1.0, 10.0, 0.1) pointsToVTK( "./trajectory_points", x, y, z, data={"elevation": z} ) ``` -------------------------------- ### Export Cylinder Data to VTK Source: https://context7.com/pyscience-projects/pyevtk/llms.txt Demonstrates exporting cylinder geometry with associated cell and point data. ```python # Optional data cell_data = {"pressure": np.random.rand(ncells)} point_data = {"temperature": np.random.rand(npoints)} filepath = cylinderToVTK( "./cylinder", x0, y0, z0, z1, radius, nlayers, npilars=npilars, cellData=cell_data, pointData=point_data ) # Returns: ./cylinder.vtu ``` -------------------------------- ### Export Structured Grid to VTK (.vts) Source: https://context7.com/pyscience-projects/pyevtk/llms.txt Use gridToVTK with 3D coordinate arrays to export data on a structured grid. This format supports arbitrarily deformed hexahedral cells. ```python from pyevtk.hl import gridToVTK import numpy as np import random as rnd # Define grid dimensions nx, ny, nz = 6, 6, 2 lx, ly, lz = 1.0, 1.0, 1.0 dx, dy, dz = lx / nx, ly / ny, lz / nz ncells = nx * ny * nz npoints = (nx + 1) * (ny + 1) * (nz + 1) # Base coordinates 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") # Create 3D coordinate arrays with perturbations (non-orthogonal grid) x = np.zeros((nx + 1, ny + 1, nz + 1)) y = np.zeros((nx + 1, ny + 1, nz + 1)) z = np.zeros((nx + 1, ny + 1, nz + 1)) for k in range(nz + 1): for j in range(ny + 1): for i in range(nx + 1): x[i, j, k] = X[i] + (0.5 - rnd.random()) * 0.1 * dx y[i, j, k] = Y[j] + (0.5 - rnd.random()) * 0.1 * dy z[i, j, k] = Z[k] + (0.5 - rnd.random()) * 0.1 * dz # Data arrays pressure = np.random.rand(ncells).reshape((nx, ny, nz)) temp = np.random.rand(npoints).reshape((nx + 1, ny + 1, nz + 1)) # Export structured grid (3D coordinates trigger structured format) filepath = gridToVTK( "./structured_grid", x, y, z, # 3D arrays = structured grid cellData={"pressure": pressure}, pointData={"temp": temp} ) # Returns: ./structured_grid.vts ``` -------------------------------- ### Export Poly-Lines with polyLinesToVTK Source: https://context7.com/pyscience-projects/pyevtk/llms.txt Exports multi-point line segments where each poly-line can contain a variable number of connected points. ```python from pyevtk.hl import polyLinesToVTK import numpy as np # Define points for 2 poly-lines npoints = 7 x = np.zeros(npoints) y = np.zeros(npoints) z = np.zeros(npoints) # First poly-line: 4 points x[0], y[0], z[0] = 0.0, 0.0, 0.0 x[1], y[1], z[1] = 1.0, 1.0, 0.0 x[2], y[2], z[2] = 2.0, 0.0, 0.0 x[3], y[3], z[3] = 3.0, -1.0, 0.0 # Second poly-line: 3 points x[4], y[4], z[4] = 0.0, 0.0, 3.0 x[5], y[5], z[5] = 1.0, 1.0, 3.0 x[6], y[6], z[6] = 2.0, 0.0, 3.0 # Number of points in each poly-line pointsPerLine = np.array([4, 3]) # Point data pressure = np.random.rand(npoints) temp = np.random.rand(npoints) # Cell data (one value per poly-line) velocity = np.array([1.0, 5.0]) filepath = polyLinesToVTK( "./poly_lines", x, y, z, pointsPerLine=pointsPerLine, cellData={"velocity": velocity}, pointData={"temp": temp, "pressure": pressure} ) # Returns: ./poly_lines.vtu ``` -------------------------------- ### Export Cylinder Geometry with cylinderToVTK Source: https://context7.com/pyscience-projects/pyevtk/llms.txt Exports a vertical cylinder as an unstructured grid of quadrilateral cells, discretized into layers and segments. ```python from pyevtk.hl import cylinderToVTK import numpy as np # Cylinder geometry x0, y0 = 0.0, 0.0 # Center position (x, y) z0, z1 = 0.0, 5.0 # Bottom and top z-coordinates radius = 1.0 nlayers = 10 # Number of vertical layers npilars = 24 # Number of segments around circumference (resolution) # Number of cells and points for data arrays ncells = npilars * nlayers npoints = npilars * (nlayers + 1) ``` -------------------------------- ### Reference VTK Cell Type Constants Source: https://context7.com/pyscience-projects/pyevtk/llms.txt Lists available cell type constants for use in unstructured grid generation. ```python from pyevtk.vtk import ( VtkVertex, # tid=1 - Single point VtkPolyVertex, # tid=2 - Multiple points VtkLine, # tid=3 - Line segment (2 points) VtkPolyLine, # tid=4 - Poly-line (n points) VtkTriangle, # tid=5 - Triangle (3 points) VtkTriangleStrip, # tid=6 - Triangle strip VtkPolygon, # tid=7 - Polygon (n points) VtkPixel, # tid=8 - Pixel/Quad (4 points, axis-aligned) VtkQuad, # tid=9 - Quadrilateral (4 points) VtkTetra, # tid=10 - Tetrahedron (4 points) VtkVoxel, # tid=11 - Voxel (8 points, axis-aligned) VtkHexahedron, # tid=12 - Hexahedron (8 points) VtkWedge, # tid=13 - Wedge/Prism (6 points) VtkPyramid, # tid=14 - Pyramid (5 points) VtkQuadraticEdge, # tid=21 - Quadratic edge (3 points) VtkQuadraticTriangle, # tid=22 - Quadratic triangle (6 points) VtkQuadraticQuad, # tid=23 - Quadratic quad (8 points) VtkQuadraticTetra, # tid=24 - Quadratic tetrahedron (10 points) VtkQuadraticHexahedron, # tid=25 - Quadratic hexahedron (20 points) ) # Use .tid attribute to get the cell type ID cell_type = VtkHexahedron.tid # Returns 12 ``` -------------------------------- ### Export Image Data to VTK (.vti) Source: https://context7.com/pyscience-projects/pyevtk/llms.txt Use imageToVTK to export data as a rectangular image grid. Supports scalar and vector data on cell-centered or node-centered locations. Specify origin and spacing for the grid. ```python from pyevtk.hl import imageToVTK import numpy as np # Define grid dimensions nx, ny, nz = 6, 6, 2 ncells = nx * ny * n npoints = (nx + 1) * (ny + 1) * (nz + 1) # Create scalar cell-centered data (pressure at cell centers) pressure = np.random.rand(ncells).reshape((nx, ny, nz), order="C") # Create scalar point-centered data (temperature at nodes) temperature = np.random.rand(npoints).reshape((nx + 1, ny + 1, nz + 1)) # Export with custom origin and spacing filepath = imageToVTK( "./simulation_output", origin=(0.0, 0.0, 0.0), spacing=(0.1, 0.1, 0.5), cellData={"pressure": pressure}, pointData={"temperature": temperature} ) # Returns: absolute path to created file (./simulation_output.vti) # Export vector data (flux as 3-component vector field) fluxx = np.random.rand(ncells).reshape((nx, ny, nz), order="F") fluxy = np.random.rand(ncells).reshape((nx, ny, nz), order="F") fluxz = np.random.rand(ncells).reshape((nx, ny, nz), order="F") flux = (fluxx, fluxy, fluxz) # Tuple of 3 arrays for vector data imageToVTK( "./vector_field", cellData={"flux": flux}, pointData={"temperature": temperature} ) ``` -------------------------------- ### Export Unstructured Mesh with unstructuredGridToVTK Source: https://context7.com/pyscience-projects/pyevtk/llms.txt Exports general unstructured grids with mixed cell types. Requires explicit connectivity, offsets, and cell type arrays. ```python from pyevtk.hl import unstructuredGridToVTK from pyevtk.vtk import VtkTriangle, VtkQuad import numpy as np # Define 6 vertices x = np.array([0.0, 1.0, 2.0, 0.0, 1.0, 2.0]) y = np.array([0.0, 0.0, 0.0, 1.0, 1.0, 1.0]) z = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0]) # Define connectivity (vertex indices for each cell) # Triangle 1: vertices 0, 1, 3 # Triangle 2: vertices 1, 4, 3 # Quad: vertices 1, 2, 5, 4 connectivity = np.array([0, 1, 3, 1, 4, 3, 1, 2, 5, 4], dtype="int32") # Offsets: index of last vertex + 1 for each cell offsets = np.array([3, 6, 10], dtype="int32") # Cell types using VTK constants cell_types = np.array([VtkTriangle.tid, VtkTriangle.tid, VtkQuad.tid], dtype="uint8") # Data arrays point_data = {"node_id": np.array([1, 2, 3, 4, 5, 6])} cell_data = {"cell_id": np.array([1, 2, 3])} field_data = {"simulation_time": np.array([1.0, 2.0])} filepath = unstructuredGridToVTK( "./unstructured_mesh", x, y, z, connectivity=connectivity, offsets=offsets, cell_types=cell_types, cellData=cell_data, pointData=point_data, fieldData=field_data ) # Returns: ./unstructured_mesh.vtu ``` -------------------------------- ### Export Rectilinear Grid to VTK (.vtr) Source: https://context7.com/pyscience-projects/pyevtk/llms.txt Use gridToVTK with 1D coordinate arrays to export data on a rectilinear grid. This format is suitable for axis-aligned grids with non-uniform spacing. ```python from pyevtk.hl import gridToVTK import numpy as np # Define grid dimensions and size nx, ny, nz = 6, 6, 2 lx, ly, lz = 1.0, 1.0, 1.0 dx, dy, dz = lx / nx, ly / ny, lz / nz ncells = nx * ny * nz npoints = (nx + 1) * (ny + 1) * (nz + 1) # Define 1D coordinate arrays (node positions) x = np.arange(0, lx + 0.1 * dx, dx, dtype="float64") # 7 points y = np.arange(0, ly + 0.1 * dy, dy, dtype="float64") # 7 points z = np.arange(0, lz + 0.1 * dz, dz, dtype="float64") # 3 points # Create data arrays pressure = np.random.rand(ncells).reshape((nx, ny, nz)) temp = np.random.rand(npoints).reshape((nx + 1, ny + 1, nz + 1)) # Export rectilinear grid filepath = gridToVTK( "./rectilinear_grid", x, y, z, cellData={"pressure": pressure}, pointData={"temp": temp} ) # Returns: ./rectilinear_grid.vtr ``` -------------------------------- ### Export Line Segments with linesToVTK Source: https://context7.com/pyscience-projects/pyevtk/llms.txt Exports line segments defined by pairs of points. Each pair in the coordinate arrays forms a distinct line. ```python from pyevtk.hl import linesToVTK import numpy as np # Define 2 line segments (4 points total, each pair forms a line) npoints = 4 x = np.zeros(npoints) y = np.zeros(npoints) z = np.zeros(npoints) # First line segment: (0,0,0) to (1,1,1) x[0], y[0], z[0] = 0.0, 0.0, 0.0 x[1], y[1], z[1] = 1.0, 1.0, 1.0 # Second line segment: (0,0,0) to (-1,1,1) x[2], y[2], z[2] = 0.0, 0.0, 0.0 x[3], y[3], z[3] = -1.0, 1.0, 1.0 # Point data (associated with each vertex) pressure = np.random.rand(npoints) temp = np.random.rand(npoints) # Cell data (associated with each line, 2 lines = 2 values) velocity = np.array([1.0, 5.0]) filepath = linesToVTK( "./lines", x, y, z, cellData={"velocity": velocity}, pointData={"temp": temp, "pressure": pressure} ) # Returns: ./lines.vtu ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.