Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,10 @@ That should create a points.vtu file in the current directory.
SUPPORT:
=======

I will continue releasing this package as open source, so it is free to be used in any kind of project. I will also continue providing support for simple questions and making incremental improvements as time allows. However, I also provide contract based support for commercial or research projects interested in this package or in topics related to data analysis and scientific programming with Python, Java, MATLAB/Octave, C/C++ or Fortran. For further details, please contact me to: paulo.herrera.eirl@gmail.com.
I will continue releasing this package as open source, so it is free to be used in any kind of project.
I will also continue providing support for simple questions and making incremental improvements as time
allows. However, I also provide contract based support for commercial or research projects interested
in this package or in topics related to data analysis and scientific programming with Python, Java, MATLAB/Octave, C/C++ or Fortran.
For further details, please contact me to: paulo.herrera.eirl@gmail.com.

**NOTE: PyEVTK moved to GitHub. The new official page is this one (https://github.com/paulo-herrera/PyEVTK)**
53 changes: 53 additions & 0 deletions examples/lines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#! /usr/bin/env python

# ***********************************************************************************
# * Copyright 2010 - 2016 Paulo A. Herrera. All rights reserved. *
# * *
# * Redistribution and use in source and binary forms, with or without *
# * modification, are permitted provided that the following conditions are met: *
# * *
# * 1. Redistributions of source code must retain the above copyright notice, *
# * this list of conditions and the following disclaimer. *
# * *
# * 2. Redistributions in binary form must reproduce the above copyright notice, *
# * this list of conditions and the following disclaimer in the documentation *
# * and/or other materials provided with the distribution. *
# * *
# * THIS SOFTWARE IS PROVIDED BY PAULO A. HERRERA ``AS IS'' AND ANY EXPRESS OR *
# * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF *
# * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO *
# * EVENT SHALL <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, *
# * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
# * BUT NOT LIMITED TO, PROCUREMEN OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
# * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
# * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
# * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
# * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
# ***********************************************************************************

# **************************************************************
# * Example of how to use the high level pointsToVTK function. *
# **************************************************************

from pyevtk.hl import linesToVTK
import numpy as np

# Example 1
npoints = 4
x = np.zeros(npoints)
y = np.zeros(npoints)
z = np.zeros(npoints)
pressure = np.random.rand(npoints)
temp = np.random.rand(npoints)
vel = np.zeros(2)
vel[0] = 1.0
vel[1] = 5.0

x[0], y[0], z[0] = 0.0, 0.0, 0.0
x[1], y[1], z[1] = 1.0, 1.0, 1.0
x[2], y[2], z[2] = 0.0, 0.0, 0.0
x[3], y[3], z[3] = -1.0, 1.0, 1.0

linesToVTK("./lines", x, y, z, cellData = {"vel" : vel}, pointData = {"temp" : temp, "pressure" : pressure})


66 changes: 66 additions & 0 deletions examples/poly_lines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#! /usr/bin/env python

# ***********************************************************************************
# * Copyright 2010 - 2016 Paulo A. Herrera. All rights reserved. *
# * *
# * Redistribution and use in source and binary forms, with or without *
# * modification, are permitted provided that the following conditions are met: *
# * *
# * 1. Redistributions of source code must retain the above copyright notice, *
# * this list of conditions and the following disclaimer. *
# * *
# * 2. Redistributions in binary form must reproduce the above copyright notice, *
# * this list of conditions and the following disclaimer in the documentation *
# * and/or other materials provided with the distribution. *
# * *
# * THIS SOFTWARE IS PROVIDED BY PAULO A. HERRERA ``AS IS'' AND ANY EXPRESS OR *
# * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF *
# * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO *
# * EVENT SHALL <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, *
# * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
# * BUT NOT LIMITED TO, PROCUREMEN OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
# * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
# * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
# * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
# * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
# ***********************************************************************************

# **************************************************************
# * Example of how to use the high level pointsToVTK function. *
# **************************************************************

from pyevtk.hl import polyLinesToVTK
import numpy as np

# Positions of points that define lines
npoints = 7
x = np.zeros(npoints)
y = np.zeros(npoints)
z = np.zeros(npoints)

# First line
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 line
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

# Connectivity of the lines
pointsPerLine = np.zeros(2)
pointsPerLine[0] = 4
pointsPerLine[1] = 3

# Some variables
pressure = np.random.rand(npoints)
temp = np.random.rand(npoints)
vel = np.zeros(6)
vel[0:3] = 1.0
vel[4:6] = 5.0

polyLinesToVTK("./poly_lines", x, y, z, pointsPerLine = pointsPerLine, cellData = {"vel" : vel}, pointData = {"temp" : temp, "pressure" : pressure})


70 changes: 70 additions & 0 deletions examples/unstructured.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#! /usr/bin/env python

# ***********************************************************************************
# * Copyright 2010 - 2017 Paulo A. Herrera. All rights reserved. *
# * *
# * Redistribution and use in source and binary forms, with or without *
# * modification, are permitted provided that the following conditions are met: *
# * *
# * 1. Redistributions of source code must retain the above copyright notice, *
# * this list of conditions and the following disclaimer. *
# * *
# * 2. Redistributions in binary form must reproduce the above copyright notice, *
# * this list of conditions and the following disclaimer in the documentation *
# * and/or other materials provided with the distribution. *
# * *
# * THIS SOFTWARE IS PROVIDED BY PAULO A. HERRERA ``AS IS'' AND ANY EXPRESS OR *
# * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF *
# * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO *
# * EVENT SHALL <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, *
# * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
# * BUT NOT LIMITED TO, PROCUREMEN OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
# * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
# * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
# * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
# * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
# ***********************************************************************************

# ************************************************************************
# * Example of how to use the high level unstructuredGridToVTK function. *
# * This example shows how to export a unstructured grid give its *
# * nodes and topology through a connectivity and offset lists. *
# * Check the VTK file format for details of the unstructured grid. *
# ************************************************************************

from pyevtk.hl import unstructuredGridToVTK
from pyevtk.vtk import VtkTriangle, VtkQuad
import numpy as np

# Define vertices
x = np.zeros(6)
y = np.zeros(6)
z = np.zeros(6)

x[0], y[0], z[0] = 0.0, 0.0, 0.0
x[1], y[1], z[1] = 1.0, 0.0, 0.0
x[2], y[2], z[2] = 2.0, 0.0, 0.0
x[3], y[3], z[3] = 0.0, 1.0, 0.0
x[4], y[4], z[4] = 1.0, 1.0, 0.0
x[5], y[5], z[5] = 2.0, 1.0, 0.0

# Define connectivity or vertices that belongs to each element
conn = np.zeros(10)

conn[0], conn[1], conn[2] = 0, 1, 3 # first triangle
conn[3], conn[4], conn[5] = 1, 4, 3 # second triangle
conn[6], conn[7], conn[8], conn[9] = 1, 2, 5, 4 # rectangle

# Define offset of last vertex of each element
offset = np.zeros(3)
offset[0] = 3
offset[1] = 6
offset[2] = 10

# Define cell types

ctype = np.zeros(3)
ctype[0], ctype[1] = VtkTriangle.tid, VtkTriangle.tid
ctype[2] = VtkQuad.tid

unstructuredGridToVTK("unstructured", x, y, z, connectivity = conn, offsets = offset, cell_types = ctype, cellData = None, pointData = None)
152 changes: 74 additions & 78 deletions pyevtk/hl.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
import numpy as np

# =================================
# Helper functions
# Helper functions
# =================================
def _addDataToFile(vtkFile, cellData, pointData):
# Point data
if pointData != None:
if pointData:
keys = list(pointData.keys())
vtkFile.openData("Point", scalars = keys[0])
for key in keys:
Expand All @@ -45,7 +45,7 @@ def _addDataToFile(vtkFile, cellData, pointData):
vtkFile.closeData("Point")

# Cell data
if cellData != None:
if cellData:
keys = list(cellData.keys())
vtkFile.openData("Cell", scalars = keys[0])
for key in keys:
Expand Down Expand Up @@ -200,7 +200,7 @@ def gridToVTK(path, x, y, z, cellData = None, pointData = None):


# ==============================================================================
def pointsToVTK(path, x, y, z, data):
def pointsToVTK(path, x, y, z, data = None):
"""
Export points and associated data as an unstructured grid.

Expand Down Expand Up @@ -433,88 +433,84 @@ def unstructuredGridToVTK(path, x, y, z, connectivity, offsets, cell_types, cell
return w.getFileName()

# ==============================================================================
def cylindricalToVTK(path, x, y, z, sh, cellData):
def cylinderToVTK(path, x0, y0, z0, z1, radius, nlayers, npilars = 16, cellData=None, pointData=None):
"""
Export points and associated data as an unstructured grid.

A cylindrical mesh connectivity is assumed. That is, the mesh is a
function

f: D --> R^3
(x,y,z)=f(i,j,k)

where D is the cartesian product of graphs between a cycle (C_j)
and two path graphs (P_i and P_k).

D= P_i x C_j x P_k

for further explanation see:
https://en.wikipedia.org/wiki/Cartesian_product_of_graphs
https://en.wikipedia.org/wiki/Path_graph
https://en.wikipedia.org/wiki/Cycle_graph
Export cylinder as VTK unstructured grid.

PARAMETERS:
path: path to file without extension.
x0, yo: center of cylinder.
z0, z1: lower and top elevation of the cylinder.
radius: radius of cylinder.
nlayers: Number of layers in z direction to divide the cylinder.
npilars: Number of points around the diameter of the cylinder.
Higher value gives higher resolution to represent the curved shape.
cellData: dictionary with 1D arrays that store cell data.
Arrays should have number of elements equal to ncells = npilars * nlayers.
pointData: dictionary with 1D arrays that store point data.
Arrays should have number of elements equal to npoints = npilars * (nlayers + 1).


PARAMETERS:
path: name of the file without extension where data should be saved.
x, y, z: 1D arrays with coordinates of the points.
sh: number of cells in each direction
cellData: dictionary with variables associated to each cell.
Keys should be the names of the variable stored in each array.
All arrays must have the same number of elements.

RETURNS:
RETURNS:
Full path to saved file.


NOTE: This function only export vertical shapes for now. However, it should be easy to
rotate the cylinder to represent other orientations.
"""
assert(x.size==y.size==z.size)
s=sh+(1,0,1)
npoints = np.prod(s)
ncells = np.prod(sh)
import math as m


assert(npoints==x.size)

# create some temporary arrays to write grid topology
offsets = np.arange(start = 8, stop = 8*(ncells + 1), step=8, dtype = 'int32') # index of last node in each cell
cell_types = np.empty(ncells, dtype = 'uint8')
cell_types[:] = VtkHexahedron.tid

# create connectivity
connectivity = np.empty(8*ncells, dtype = 'int32')
i=0
# Define x, y coordinates from polar coordinates.
dpi = 2.0 * m.pi / npilars
angles = np.arange(0.0, 2.0 * m.pi, dpi)

for zeta in range(0,sh[2]):
for tita in range(0,sh[1]):
for r in range(0,sh[0]):
for d in ((0,0,0),(1,0,0),(1,1,0),(0,1,0),(0,0,1),(1,0,1),(1,1,1),(0,1,1)):
connectivity[i]=r+d[0]+s[0]*((tita+d[1])%s[1])+s[0]*s[1]*(zeta+d[2])
i+=1
x = radius * np.cos(angles) + x0
y = radius * np.sin(angles) + y0

w = VtkFile(path, VtkUnstructuredGrid)
w.openGrid()
w.openPiece(ncells = ncells, npoints = npoints)

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

# adaptar cellData segun formato!!!

_addDataToFile(w, cellData = cellData, pointData = None)
dz = (z1 - z0) / nlayers
z = np.arange(z0, z1+dz, step = dz)

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

_appendDataToFile(w, cellData = cellData, pointData = None)
npoints = npilars * (nlayers + 1)
ncells = npilars * nlayers

w.save()
return w.getFileName()
xx = np.zeros(npoints)
yy = np.zeros(npoints)
zz = np.zeros(npoints)

ii = 0
for k in range(nlayers + 1):
for p in range(npilars):
xx[ii] = x[p]
yy[ii] = y[p]
zz[ii] = z[k]
ii = ii + 1

# Define connectivity
conn = np.zeros(4 * ncells, dtype = np.int64)
ii = 0
for l in range(nlayers):
for p in range(npilars):
p0 = p
if(p + 1 == npilars):
p1 = 0
else:
p1 = p + 1 # circular loop

n0 = p0 + l * npilars
n1 = p1 + l * npilars
n2 = n0 + npilars
n3 = n1 + npilars

conn[ii + 0] = n0
conn[ii + 1] = n1
conn[ii + 2] = n3
conn[ii + 3] = n2
ii = ii + 4

# Define offsets
offsets = np.zeros(ncells, dtype = np.int64)
for i in range(ncells):
offsets[i] = (i + 1) * 4

# Define cell types
ctype = np.ones(ncells) + VtkPixel.tid

return unstructuredGridToVTK(path, xx, yy, zz, connectivity = conn, offsets = offsets, cell_types = ctype, cellData = cellData, pointData = pointData)
Loading