Skip to content

Commit c55fc46

Browse files
authored
Merge pull request #12 from xylar/update_from_hererra_pyevtk
Bring in 3 commits from PyEVTK BitBucket repo and 1 from paulo-herrera/PyEVTK on github
2 parents 11a1e70 + 979e2f9 commit c55fc46

File tree

7 files changed

+277
-82
lines changed

7 files changed

+277
-82
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,10 @@ That should create a points.vtu file in the current directory.
104104
SUPPORT:
105105
=======
106106

107-
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.
107+
I will continue releasing this package as open source, so it is free to be used in any kind of project.
108+
I will also continue providing support for simple questions and making incremental improvements as time
109+
allows. However, I also provide contract based support for commercial or research projects interested
110+
in this package or in topics related to data analysis and scientific programming with Python, Java, MATLAB/Octave, C/C++ or Fortran.
111+
For further details, please contact me to: paulo.herrera.eirl@gmail.com.
112+
113+
**NOTE: PyEVTK moved to GitHub. The new official page is this one (https://github.com/paulo-herrera/PyEVTK)**

examples/lines.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#! /usr/bin/env python
2+
3+
# ***********************************************************************************
4+
# * Copyright 2010 - 2016 Paulo A. Herrera. All rights reserved. *
5+
# * *
6+
# * Redistribution and use in source and binary forms, with or without *
7+
# * modification, are permitted provided that the following conditions are met: *
8+
# * *
9+
# * 1. Redistributions of source code must retain the above copyright notice, *
10+
# * this list of conditions and the following disclaimer. *
11+
# * *
12+
# * 2. Redistributions in binary form must reproduce the above copyright notice, *
13+
# * this list of conditions and the following disclaimer in the documentation *
14+
# * and/or other materials provided with the distribution. *
15+
# * *
16+
# * THIS SOFTWARE IS PROVIDED BY PAULO A. HERRERA ``AS IS'' AND ANY EXPRESS OR *
17+
# * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF *
18+
# * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO *
19+
# * EVENT SHALL <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, *
20+
# * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
21+
# * BUT NOT LIMITED TO, PROCUREMEN OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
22+
# * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
23+
# * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
24+
# * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
25+
# * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
26+
# ***********************************************************************************
27+
28+
# **************************************************************
29+
# * Example of how to use the high level pointsToVTK function. *
30+
# **************************************************************
31+
32+
from pyevtk.hl import linesToVTK
33+
import numpy as np
34+
35+
# Example 1
36+
npoints = 4
37+
x = np.zeros(npoints)
38+
y = np.zeros(npoints)
39+
z = np.zeros(npoints)
40+
pressure = np.random.rand(npoints)
41+
temp = np.random.rand(npoints)
42+
vel = np.zeros(2)
43+
vel[0] = 1.0
44+
vel[1] = 5.0
45+
46+
x[0], y[0], z[0] = 0.0, 0.0, 0.0
47+
x[1], y[1], z[1] = 1.0, 1.0, 1.0
48+
x[2], y[2], z[2] = 0.0, 0.0, 0.0
49+
x[3], y[3], z[3] = -1.0, 1.0, 1.0
50+
51+
linesToVTK("./lines", x, y, z, cellData = {"vel" : vel}, pointData = {"temp" : temp, "pressure" : pressure})
52+
53+

examples/poly_lines.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#! /usr/bin/env python
2+
3+
# ***********************************************************************************
4+
# * Copyright 2010 - 2016 Paulo A. Herrera. All rights reserved. *
5+
# * *
6+
# * Redistribution and use in source and binary forms, with or without *
7+
# * modification, are permitted provided that the following conditions are met: *
8+
# * *
9+
# * 1. Redistributions of source code must retain the above copyright notice, *
10+
# * this list of conditions and the following disclaimer. *
11+
# * *
12+
# * 2. Redistributions in binary form must reproduce the above copyright notice, *
13+
# * this list of conditions and the following disclaimer in the documentation *
14+
# * and/or other materials provided with the distribution. *
15+
# * *
16+
# * THIS SOFTWARE IS PROVIDED BY PAULO A. HERRERA ``AS IS'' AND ANY EXPRESS OR *
17+
# * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF *
18+
# * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO *
19+
# * EVENT SHALL <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, *
20+
# * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
21+
# * BUT NOT LIMITED TO, PROCUREMEN OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
22+
# * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
23+
# * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
24+
# * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
25+
# * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
26+
# ***********************************************************************************
27+
28+
# **************************************************************
29+
# * Example of how to use the high level pointsToVTK function. *
30+
# **************************************************************
31+
32+
from pyevtk.hl import polyLinesToVTK
33+
import numpy as np
34+
35+
# Positions of points that define lines
36+
npoints = 7
37+
x = np.zeros(npoints)
38+
y = np.zeros(npoints)
39+
z = np.zeros(npoints)
40+
41+
# First line
42+
x[0], y[0], z[0] = 0.0, 0.0, 0.0
43+
x[1], y[1], z[1] = 1.0, 1.0, 0.0
44+
x[2], y[2], z[2] = 2.0, 0.0, 0.0
45+
x[3], y[3], z[3] = 3.0, -1.0, 0.0
46+
47+
# Second line
48+
x[4], y[4], z[4] = 0.0, 0.0, 3.0
49+
x[5], y[5], z[5] = 1.0, 1.0, 3.0
50+
x[6], y[6], z[6] = 2.0, 0.0, 3.0
51+
52+
# Connectivity of the lines
53+
pointsPerLine = np.zeros(2)
54+
pointsPerLine[0] = 4
55+
pointsPerLine[1] = 3
56+
57+
# Some variables
58+
pressure = np.random.rand(npoints)
59+
temp = np.random.rand(npoints)
60+
vel = np.zeros(6)
61+
vel[0:3] = 1.0
62+
vel[4:6] = 5.0
63+
64+
polyLinesToVTK("./poly_lines", x, y, z, pointsPerLine = pointsPerLine, cellData = {"vel" : vel}, pointData = {"temp" : temp, "pressure" : pressure})
65+
66+

examples/unstructured.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#! /usr/bin/env python
2+
3+
# ***********************************************************************************
4+
# * Copyright 2010 - 2017 Paulo A. Herrera. All rights reserved. *
5+
# * *
6+
# * Redistribution and use in source and binary forms, with or without *
7+
# * modification, are permitted provided that the following conditions are met: *
8+
# * *
9+
# * 1. Redistributions of source code must retain the above copyright notice, *
10+
# * this list of conditions and the following disclaimer. *
11+
# * *
12+
# * 2. Redistributions in binary form must reproduce the above copyright notice, *
13+
# * this list of conditions and the following disclaimer in the documentation *
14+
# * and/or other materials provided with the distribution. *
15+
# * *
16+
# * THIS SOFTWARE IS PROVIDED BY PAULO A. HERRERA ``AS IS'' AND ANY EXPRESS OR *
17+
# * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF *
18+
# * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO *
19+
# * EVENT SHALL <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, *
20+
# * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
21+
# * BUT NOT LIMITED TO, PROCUREMEN OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
22+
# * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
23+
# * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
24+
# * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
25+
# * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
26+
# ***********************************************************************************
27+
28+
# ************************************************************************
29+
# * Example of how to use the high level unstructuredGridToVTK function. *
30+
# * This example shows how to export a unstructured grid give its *
31+
# * nodes and topology through a connectivity and offset lists. *
32+
# * Check the VTK file format for details of the unstructured grid. *
33+
# ************************************************************************
34+
35+
from pyevtk.hl import unstructuredGridToVTK
36+
from pyevtk.vtk import VtkTriangle, VtkQuad
37+
import numpy as np
38+
39+
# Define vertices
40+
x = np.zeros(6)
41+
y = np.zeros(6)
42+
z = np.zeros(6)
43+
44+
x[0], y[0], z[0] = 0.0, 0.0, 0.0
45+
x[1], y[1], z[1] = 1.0, 0.0, 0.0
46+
x[2], y[2], z[2] = 2.0, 0.0, 0.0
47+
x[3], y[3], z[3] = 0.0, 1.0, 0.0
48+
x[4], y[4], z[4] = 1.0, 1.0, 0.0
49+
x[5], y[5], z[5] = 2.0, 1.0, 0.0
50+
51+
# Define connectivity or vertices that belongs to each element
52+
conn = np.zeros(10)
53+
54+
conn[0], conn[1], conn[2] = 0, 1, 3 # first triangle
55+
conn[3], conn[4], conn[5] = 1, 4, 3 # second triangle
56+
conn[6], conn[7], conn[8], conn[9] = 1, 2, 5, 4 # rectangle
57+
58+
# Define offset of last vertex of each element
59+
offset = np.zeros(3)
60+
offset[0] = 3
61+
offset[1] = 6
62+
offset[2] = 10
63+
64+
# Define cell types
65+
66+
ctype = np.zeros(3)
67+
ctype[0], ctype[1] = VtkTriangle.tid, VtkTriangle.tid
68+
ctype[2] = VtkQuad.tid
69+
70+
unstructuredGridToVTK("unstructured", x, y, z, connectivity = conn, offsets = offset, cell_types = ctype, cellData = None, pointData = None)

pyevtk/hl.py

Lines changed: 74 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
import numpy as np
3333

3434
# =================================
35-
# Helper functions
35+
# Helper functions
3636
# =================================
3737
def _addDataToFile(vtkFile, cellData, pointData):
3838
# Point data
39-
if pointData != None:
39+
if pointData:
4040
keys = list(pointData.keys())
4141
vtkFile.openData("Point", scalars = keys[0])
4242
for key in keys:
@@ -45,7 +45,7 @@ def _addDataToFile(vtkFile, cellData, pointData):
4545
vtkFile.closeData("Point")
4646

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

201201

202202
# ==============================================================================
203-
def pointsToVTK(path, x, y, z, data):
203+
def pointsToVTK(path, x, y, z, data = None):
204204
"""
205205
Export points and associated data as an unstructured grid.
206206
@@ -433,88 +433,84 @@ def unstructuredGridToVTK(path, x, y, z, connectivity, offsets, cell_types, cell
433433
return w.getFileName()
434434

435435
# ==============================================================================
436-
def cylindricalToVTK(path, x, y, z, sh, cellData):
436+
def cylinderToVTK(path, x0, y0, z0, z1, radius, nlayers, npilars = 16, cellData=None, pointData=None):
437437
"""
438-
Export points and associated data as an unstructured grid.
439-
440-
A cylindrical mesh connectivity is assumed. That is, the mesh is a
441-
function
442-
443-
f: D --> R^3
444-
(x,y,z)=f(i,j,k)
445-
446-
where D is the cartesian product of graphs between a cycle (C_j)
447-
and two path graphs (P_i and P_k).
448-
449-
D= P_i x C_j x P_k
450-
451-
for further explanation see:
452-
https://en.wikipedia.org/wiki/Cartesian_product_of_graphs
453-
https://en.wikipedia.org/wiki/Path_graph
454-
https://en.wikipedia.org/wiki/Cycle_graph
438+
Export cylinder as VTK unstructured grid.
439+
440+
PARAMETERS:
441+
path: path to file without extension.
442+
x0, yo: center of cylinder.
443+
z0, z1: lower and top elevation of the cylinder.
444+
radius: radius of cylinder.
445+
nlayers: Number of layers in z direction to divide the cylinder.
446+
npilars: Number of points around the diameter of the cylinder.
447+
Higher value gives higher resolution to represent the curved shape.
448+
cellData: dictionary with 1D arrays that store cell data.
449+
Arrays should have number of elements equal to ncells = npilars * nlayers.
450+
pointData: dictionary with 1D arrays that store point data.
451+
Arrays should have number of elements equal to npoints = npilars * (nlayers + 1).
455452
456-
457-
PARAMETERS:
458-
path: name of the file without extension where data should be saved.
459-
x, y, z: 1D arrays with coordinates of the points.
460-
sh: number of cells in each direction
461-
cellData: dictionary with variables associated to each cell.
462-
Keys should be the names of the variable stored in each array.
463-
All arrays must have the same number of elements.
464-
465-
RETURNS:
453+
RETURNS:
466454
Full path to saved file.
467-
455+
456+
NOTE: This function only export vertical shapes for now. However, it should be easy to
457+
rotate the cylinder to represent other orientations.
468458
"""
469-
assert(x.size==y.size==z.size)
470-
s=sh+(1,0,1)
471-
npoints = np.prod(s)
472-
ncells = np.prod(sh)
459+
import math as m
473460

474-
475-
assert(npoints==x.size)
476-
477-
# create some temporary arrays to write grid topology
478-
offsets = np.arange(start = 8, stop = 8*(ncells + 1), step=8, dtype = 'int32') # index of last node in each cell
479-
cell_types = np.empty(ncells, dtype = 'uint8')
480-
cell_types[:] = VtkHexahedron.tid
481-
482-
# create connectivity
483-
connectivity = np.empty(8*ncells, dtype = 'int32')
484-
i=0
461+
# Define x, y coordinates from polar coordinates.
462+
dpi = 2.0 * m.pi / npilars
463+
angles = np.arange(0.0, 2.0 * m.pi, dpi)
485464

486-
for zeta in range(0,sh[2]):
487-
for tita in range(0,sh[1]):
488-
for r in range(0,sh[0]):
489-
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)):
490-
connectivity[i]=r+d[0]+s[0]*((tita+d[1])%s[1])+s[0]*s[1]*(zeta+d[2])
491-
i+=1
465+
x = radius * np.cos(angles) + x0
466+
y = radius * np.sin(angles) + y0
492467

493-
w = VtkFile(path, VtkUnstructuredGrid)
494-
w.openGrid()
495-
w.openPiece(ncells = ncells, npoints = npoints)
496-
497-
w.openElement("Points")
498-
w.addData("points", (x,y,z))
499-
w.closeElement("Points")
500-
w.openElement("Cells")
501-
w.addData("connectivity", connectivity)
502-
w.addData("offsets", offsets)
503-
w.addData("types", cell_types)
504-
w.closeElement("Cells")
505-
506-
# adaptar cellData segun formato!!!
507-
508-
_addDataToFile(w, cellData = cellData, pointData = None)
468+
dz = (z1 - z0) / nlayers
469+
z = np.arange(z0, z1+dz, step = dz)
509470

510-
w.closePiece()
511-
w.closeGrid()
512-
w.appendData( (x,y,z) )
513-
w.appendData(connectivity).appendData(offsets).appendData(cell_types)
514-
515-
_appendDataToFile(w, cellData = cellData, pointData = None)
471+
npoints = npilars * (nlayers + 1)
472+
ncells = npilars * nlayers
516473

517-
w.save()
518-
return w.getFileName()
474+
xx = np.zeros(npoints)
475+
yy = np.zeros(npoints)
476+
zz = np.zeros(npoints)
519477

478+
ii = 0
479+
for k in range(nlayers + 1):
480+
for p in range(npilars):
481+
xx[ii] = x[p]
482+
yy[ii] = y[p]
483+
zz[ii] = z[k]
484+
ii = ii + 1
485+
486+
# Define connectivity
487+
conn = np.zeros(4 * ncells, dtype = np.int64)
488+
ii = 0
489+
for l in range(nlayers):
490+
for p in range(npilars):
491+
p0 = p
492+
if(p + 1 == npilars):
493+
p1 = 0
494+
else:
495+
p1 = p + 1 # circular loop
496+
497+
n0 = p0 + l * npilars
498+
n1 = p1 + l * npilars
499+
n2 = n0 + npilars
500+
n3 = n1 + npilars
501+
502+
conn[ii + 0] = n0
503+
conn[ii + 1] = n1
504+
conn[ii + 2] = n3
505+
conn[ii + 3] = n2
506+
ii = ii + 4
507+
508+
# Define offsets
509+
offsets = np.zeros(ncells, dtype = np.int64)
510+
for i in range(ncells):
511+
offsets[i] = (i + 1) * 4
520512

513+
# Define cell types
514+
ctype = np.ones(ncells) + VtkPixel.tid
515+
516+
return unstructuredGridToVTK(path, xx, yy, zz, connectivity = conn, offsets = offsets, cell_types = ctype, cellData = cellData, pointData = pointData)

0 commit comments

Comments
 (0)