pyne/pyne — pull request #238 by erelson (2014-01-06 05:33:46+00:00)
Title: Unstructured mesh element volume calculation methods

This pull request extends the mesh element volume calculation abilities of `mesh.py`. Specifically, unstructured tet and hex elements are now supported. Additionally, a commit of pep8/whitespace fixes is included as well.

The additions are four methods:

- `get_tet_volume`
- `_calc_tet_volume`
- `get_hex_volume`
- `get_elem_volume`

Since both the get_tet and get_hex methods are actually calculating the volumes of tetrahedra, they share _calc_tet_volume. (Any planar faceted hexahedra can be decomposed into 5 tets) The get_elem method uses the number of vertices to determine the voxel type, and then calls the correct method.

As I write this, I'm wondering if maybe I should combine the three get methods into one... thoughts? The ability to specifically get a tet volume would be lost, and the added cost of an extra if statement... negligible.

Both the tet and hex methods are taken from MOAB's tools/measure.cpp. The tet method is also that listed on Wikipedia. Another hex method is found in an example on the pytaps site.

Since I had two hex methods, I did some speed testing with tests of the form:

```python
filename = os.path.join(os.path.dirname(__file__),
"files_mesh_test/grid543.h5m")
mesh = Mesh(mesh_file=filename)
for _ in xrange(100):
vols = list()
for __, __, ve in mesh:
vols.append(mesh.get_hex_volume2(ve))
assert_almost_equal(np.mean(vols), 51.3333, places=4)
```

Results of speedtests like the above for the three hexahedra volume methods::

test_mesh.test_hex_volume_pytaps ... ok
Ran 1 test in 0.598s
OK
----------------------------------------------------------------------
test_mesh.test_hex_volume_moab ... ok
Ran 1 test in 0.324s
OK
----------------------------------------------------------------------
test_mesh.test_sc_hex_volume ... ok
Ran 1 test in 0.260s
OK

So the moab method appears to be superior, and is the one I put in the pull request. Testing the structured hex volume method (using structured_iterate_hex_volumes instead of iterating through the mesh directly) indicates that structured mesh elements' volumes are unsurprisingly even quicker to determine.

I got lazy, sort of, with the tests. I reuse existing mesh files in tests/files_mesh_test. They do a whole bunch of calculations rather than just a few, so they take up several tens of milliseconds to run (about doubles the runtime of test_mesh.py), and test_elem_volume re-runs both of these... If desired, I'll do something lighter-weight instead. Tests compare [mean/min/max] with those reported by the `mbsize` tool.

I'm fairly convinced now that I should combine the three methods into one, as I mention above... I'll do that in a day or two unless others suggest otherwise. In the meantime, anything I should change?