Skip to content

Commit

Permalink
First git commit. Version 1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-herrera committed Mar 13, 2019
0 parents commit 28def4c
Show file tree
Hide file tree
Showing 19 changed files with 1,885 additions and 0 deletions.
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
***********************************************************************************
* Copyright 2010 - 2019 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. *
***********************************************************************************
14 changes: 14 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
README.txt
setup.py
LICENSE
src/__init__.py
src/hl.py
src/vtk.py
src/xml.py
src/examples/__init__.py
src/examples/group.py
src/examples/image.py
src/examples/lowlevel.py
src/examples/points.py
src/examples/rectilinear.py
src/examples/structured.py
80 changes: 80 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
INTRODUCTION:
=============

EVTK (Export VTK) package allows exporting data to binary VTK files for
visualization and data analysis with any of the visualization packages that
support VTK files, e.g. Paraview, VisIt and Mayavi. EVTK does not depend on any
external library (e.g. VTK), so it is easy to install in different systems.

Since version 0.9 the package is composed only of a set of pure Python files, hence
it is straightforwrd to install and run in any system where Python is installed.
EVTK provides low and high level interfaces. While the low level interface
can be used to export data that is stored in any type of container, the high
level functions make easy to export data stored in Numpy arrays.

INSTALLATION:
=============

Go to the source directory and type:
python setup.py install

DOCUMENTATION:
==============

This file together with the included examples in the examples directory in the
source tree provide enough information to start using the package.

DESIGN GUIDELINES:
==================

The design of the package considered the following objectives:

1. Self-contained. The package does not require any external library with
the exception of Numpy, which is becoming a standard package in many Python
installations.

2. Flexibility. It is possible to use EVTK to export data stored in any
container and in any of the grid formats supported by VTK by using the low level
interface.

3. Easy of use. The high level interface makes very easy to export data stored
in Numpy arrays. The high level interface provides functions to export most of
the grids supported by VTK: image data, rectilinear and structured grids. It
also includes a function to export point sets and associated data that can be
used to export results from particle and meshless numerical simulations.

4. Performance. The aim of the package is to be used as a part of
post-processing tools. Thus, good performance is important to handle the results
of large simulations. However, latest versions give priority to ease of installation
and use over performance.

REQUIREMENTS:
=============

- Numpy. Tested with Numpy 1.8.0 to 1.13.3.

It is compatible with both Python 2 (2.7+) and Python 3 (3.3+). Since version 0.9 it is only compatible
with VTK 6.0 and newer versions.

DEVELOPER NOTES:
================

It is useful to build and install the package to a temporary location without
touching the global python site-packages directory while developing. To do
this, while in the root directory, one can type:

1. python setup.py build --debug install --prefix=./tmp
2. export PYTHONPATH=./tmp/lib/python2.7/site-packages/:$PYTHONPATH

NOTE: you may have to change the Python version depending of the installed
version on your system.

To test the package one can run some of the examples, e.g.:
./tmp/lib/python2.7/site-packages/examples/points.py

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: [email protected].
50 changes: 50 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# ***********************************************************************************
# * 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. *
# ***********************************************************************************

# CHECK THIS PATCH
#try:
# from setuptools import setup
#except ImportError:
from distutils.core import setup

import numpy as np

def readme(fname):
with open(fname, 'r') as f:
return f.read()

setup(
name = 'evtk',
version = '1.1.1',
description = 'Export data as binary VTK files',
long_description = readme('README.txt'),
author = 'Paulo Herrera',
author_email = '[email protected]',
url = 'https://bitbucket.org/pauloh/pyevtk',
packages = ['evtk'],
package_dir = {'evtk' : 'src'},
package_data = {'evtk' : ['LICENSE', 'examples/*.py']}
)

19 changes: 19 additions & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# *************************************************************************
# * Copyright 2010 - 2016 Paulo Herrera *
# * *
# * This file is part of EVTK. *
# * *
# * EVTK is free software: you can redistribute it and/or modify *
# * it under the terms of the GNU General Public License as published by *
# * the Free Software Foundation, either version 3 of the License, or *
# * (at your option) any later version. *
# * *
# * EVTK is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU General Public License for more details. *
# * *
# * You should have received a copy of the GNU General Public License *
# * along with EVTK. If not, see <http://www.gnu.org/licenses/>. *
# *************************************************************************

101 changes: 101 additions & 0 deletions src/evtk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# ***********************************************************************************
# * 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. *
# ***********************************************************************************

import struct
import numpy as np
import sys

# Map numpy dtype to struct format
np_to_struct = { 'int8' : 'b',
'uint8' : 'B',
'int16' : 'h',
'uint16' : 'H',
'int32' : 'i',
'uint32' : 'I',
'int64' : 'q',
'uint64' : 'Q',
'float32' : 'f',
'float64' : 'd' }

def _get_byte_order_char():
# Check format in https://docs.python.org/3.5/library/struct.html
if sys.byteorder == "little":
return '<'
else:
return '>'

# ================================
# Python interface
# ================================
def writeBlockSize(stream, block_size):
fmt = _get_byte_order_char() + 'Q' # Write size as unsigned long long == 64 bits unsigned integer
stream.write(struct.pack(fmt, block_size))

def writeArrayToFile(stream, data):
#stream.flush() # this should not be necessary
assert (data.ndim == 1 or data.ndim == 3)
fmt = _get_byte_order_char() + str(data.size) + np_to_struct[data.dtype.name] # > for big endian

# Check if array is contiguous
assert (data.flags['C_CONTIGUOUS'] or data.flags['F_CONTIGUOUS'])

# NOTE: VTK expects data in FORTRAN order
# This is only needed when a multidimensional array has C-layout
dd = np.ravel(data, order='F')

bin = struct.pack(fmt, *dd)
stream.write(bin)

# ==============================================================================
def writeArraysToFile(stream, x, y, z):
# Check if arrays have same shape and data type
assert ( x.size == y.size == z.size ), "Different array sizes."
assert ( x.dtype.itemsize == y.dtype.itemsize == z.dtype.itemsize ), "Different item sizes."

nitems = x.size
itemsize = x.dtype.itemsize

fmt = _get_byte_order_char() + str(1) + np_to_struct[x.dtype.name] # > for big endian

# Check if arrays are contiguous
assert (x.flags['C_CONTIGUOUS'] or x.flags['F_CONTIGUOUS'])
assert (y.flags['C_CONTIGUOUS'] or y.flags['F_CONTIGUOUS'])
assert (z.flags['C_CONTIGUOUS'] or z.flags['F_CONTIGUOUS'])


# NOTE: VTK expects data in FORTRAN order
# This is only needed when a multidimensional array has C-layout
xx = np.ravel(x, order='F')
yy = np.ravel(y, order='F')
zz = np.ravel(z, order='F')

# eliminate this loop by creating a composed array.
for i in range(nitems):
bx = struct.pack(fmt, xx[i])
by = struct.pack(fmt, yy[i])
bz = struct.pack(fmt, zz[i])
stream.write(bx)
stream.write(by)
stream.write(bz)
Empty file added src/examples/__init__.py
Empty file.
40 changes: 40 additions & 0 deletions src/examples/group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#! /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 create a VTK group to visualize time *
# * dependent data. *
# **************************************************************

from evtk.vtk import VtkGroup

g = VtkGroup("./group")
g.addFile(filepath = "sim0000.vtu", sim_time = 0.0)
g.addFile(filepath = "sim0001.vtu", sim_time = 1.0)
g.addFile(filepath = "sim0002.vtu", sim_time = 2.0)
g.addFile(filepath = "sim0003.vtu", sim_time = 3.0)
g.save()
45 changes: 45 additions & 0 deletions src/examples/image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#! /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 imageToVTK function. *
# **************************************************************

from evtk.hl import imageToVTK
import numpy as np

# Dimensions
nx, ny, nz = 6, 6, 2
ncells = nx * ny * nz
npoints = (nx + 1) * (ny + 1) * (nz + 1)

# Variables
pressure = np.random.rand(ncells).reshape( (nx, ny, nz), order = 'C')
temp = np.random.rand(npoints).reshape( (nx + 1, ny + 1, nz + 1))

imageToVTK("./image", cellData = {"pressure" : pressure}, pointData = {"temp" : temp} )

Loading

0 comments on commit 28def4c

Please sign in to comment.