From 5bbf3f1ffee598904acb3d54997fda6b5927292b Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Tue, 26 Feb 2019 14:01:09 -0600 Subject: [PATCH 1/7] Using numpy array to transfer data rather than loops and additional double structure. --- scripts/openmc-voxel-to-vtk | 24 ++++++++++----------- tests/regression_tests/plot_voxel/plots.xml | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/scripts/openmc-voxel-to-vtk b/scripts/openmc-voxel-to-vtk index c0d8e9a14..88172bb4c 100755 --- a/scripts/openmc-voxel-to-vtk +++ b/scripts/openmc-voxel-to-vtk @@ -8,6 +8,7 @@ import numpy as np import h5py import vtk +from vtk.util import numpy_support def main(): # Process command line arguments @@ -22,26 +23,25 @@ def main(): dimension = fh.attrs['num_voxels'] width = fh.attrs['voxel_width'] lower_left = fh.attrs['lower_left'] - voxel_data = fh['data'].value nx, ny, nz = dimension upper_right = lower_left + width*dimension - + grid = vtk.vtkImageData() grid.SetDimensions(nx+1, ny+1, nz+1) grid.SetOrigin(*lower_left) grid.SetSpacing(*width) - - data = vtk.vtkDoubleArray() + + # transpose data from OpenMC ordering (zyx) to VTK ordering (xyz) + # and flatten to 1-D array + h5data = fh['data'].value.T.flatten() + data = vtk.vtkIntArray() data.SetName("id") - data.SetNumberOfTuples(nx*ny*nz) - for x in range(nx): - sys.stdout.write(" {}%\r".format(int(x/nx*100))) - sys.stdout.flush() - for y in range(ny): - for z in range(nz): - i = z*nx*ny + y*nx + x - data.SetValue(i, voxel_data[x, y, z]) + # pass h5data as a "hint" that no copy/allocation is necessary + data.SetArray(h5data, h5dara.size, True) + # set the data array + data.array = h5data + # add data to image grid grid.GetCellData().AddArray(data) writer = vtk.vtkXMLImageDataWriter() diff --git a/tests/regression_tests/plot_voxel/plots.xml b/tests/regression_tests/plot_voxel/plots.xml index 833329b42..e57992c2a 100644 --- a/tests/regression_tests/plot_voxel/plots.xml +++ b/tests/regression_tests/plot_voxel/plots.xml @@ -2,7 +2,7 @@ - 50 50 10 + 500 500 100 0. 0. 0. 20 20 10 From 3175a3be1d7d7a2f9c2400916cdb34ebffb5f709 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Tue, 26 Feb 2019 18:48:29 -0600 Subject: [PATCH 2/7] Resetting plots.xml. Adding some output to new script. --- scripts/openmc-voxel-to-vtk | 10 ++++++---- tests/regression_tests/plot_voxel/plots.xml | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/scripts/openmc-voxel-to-vtk b/scripts/openmc-voxel-to-vtk index 88172bb4c..c7d60258a 100755 --- a/scripts/openmc-voxel-to-vtk +++ b/scripts/openmc-voxel-to-vtk @@ -8,8 +8,6 @@ import numpy as np import h5py import vtk -from vtk.util import numpy_support - def main(): # Process command line arguments parser = ArgumentParser() @@ -34,11 +32,14 @@ def main(): # transpose data from OpenMC ordering (zyx) to VTK ordering (xyz) # and flatten to 1-D array - h5data = fh['data'].value.T.flatten() + print("Reading and translating data...") + h5data = fh['data'][...] + h5data = h5data.T.copy() + data = vtk.vtkIntArray() data.SetName("id") # pass h5data as a "hint" that no copy/allocation is necessary - data.SetArray(h5data, h5dara.size, True) + data.SetArray(h5data, h5data.size, True) # set the data array data.array = h5data # add data to image grid @@ -52,6 +53,7 @@ def main(): if not args.output.endswith(".vti"): args.output += ".vti" writer.SetFileName(args.output) + print("Writing VTK file {}...".format(args.output)) writer.Write() if __name__ == '__main__': diff --git a/tests/regression_tests/plot_voxel/plots.xml b/tests/regression_tests/plot_voxel/plots.xml index e57992c2a..833329b42 100644 --- a/tests/regression_tests/plot_voxel/plots.xml +++ b/tests/regression_tests/plot_voxel/plots.xml @@ -2,7 +2,7 @@ - 500 500 100 + 50 50 10 0. 0. 0. 20 20 10 From 16fac7cc071c0faafbf950dc9f0a036cd0465db1 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Tue, 26 Feb 2019 20:45:11 -0600 Subject: [PATCH 3/7] Removing data set line. --- scripts/openmc-voxel-to-vtk | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/scripts/openmc-voxel-to-vtk b/scripts/openmc-voxel-to-vtk index c7d60258a..c43f60b68 100755 --- a/scripts/openmc-voxel-to-vtk +++ b/scripts/openmc-voxel-to-vtk @@ -38,10 +38,8 @@ def main(): data = vtk.vtkIntArray() data.SetName("id") - # pass h5data as a "hint" that no copy/allocation is necessary + # set the array using the h5data array data.SetArray(h5data, h5data.size, True) - # set the data array - data.array = h5data # add data to image grid grid.GetCellData().AddArray(data) From 14f9d04ad78c2d548a38655c766b603cb86a251a Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 27 Feb 2019 17:11:16 -0600 Subject: [PATCH 4/7] Changing zyx ordering of voxel files to xyz and updating the voxel-to-vtk script. Adding check for version number in the script to avoid incorrect conversion of older voxel files. --- scripts/openmc-voxel-to-vtk | 15 ++++++++++++++- src/plot.cpp | 28 ++++++++++++++-------------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/scripts/openmc-voxel-to-vtk b/scripts/openmc-voxel-to-vtk index c43f60b68..afac8984f 100755 --- a/scripts/openmc-voxel-to-vtk +++ b/scripts/openmc-voxel-to-vtk @@ -8,6 +8,9 @@ import numpy as np import h5py import vtk +_min_version = (0,10,0) + + def main(): # Process command line arguments parser = ArgumentParser() @@ -18,6 +21,17 @@ def main(): # Read data from voxel file fh = h5py.File(args.voxel_file, 'r') + + # check version + version = tuple(fh.attrs['openmc_version']) + if version < _min_version: + old_version = ".".join(map(str,version)) + min_version = ".".join(map(str,_min_version)) + err_msg = "This voxel file was generated using OpenMC " \ + "version {}. Please re-generate using "\ + "version {} or higher.".format(old_version, min_version) + raise ValueError(err_msg) + dimension = fh.attrs['num_voxels'] width = fh.attrs['voxel_width'] lower_left = fh.attrs['lower_left'] @@ -34,7 +48,6 @@ def main(): # and flatten to 1-D array print("Reading and translating data...") h5data = fh['data'][...] - h5data = h5data.T.copy() data = vtk.vtkIntArray() data.SetName("id") diff --git a/src/plot.cpp b/src/plot.cpp index ec486ae27..0fb418848 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -884,9 +884,9 @@ void create_voxel(Plot pl) // Create dataset for voxel data -- note that the dimensions are reversed // since we want the order in the file to be z, y, x hsize_t dims[3]; - dims[0] = pl.pixels_[0]; + dims[0] = pl.pixels_[2]; dims[1] = pl.pixels_[1]; - dims[2] = pl.pixels_[2]; + dims[2] = pl.pixels_[0]; hid_t dspace, dset, memspace; voxel_init(file_id, &(dims[0]), &dspace, &dset, &memspace); @@ -895,33 +895,33 @@ void create_voxel(Plot pl) ll[1] = ll[1] + vox[1] / 2.; ll[2] = ll[2] + vox[2] / 2.; - int data[pl.pixels_[1]][pl.pixels_[2]]; + int data[pl.pixels_[1]][pl.pixels_[0]]; ProgressBar pb; RGBColor rgb; int id; - for (int x = 0; x < pl.pixels_[0]; x++) { - pb.set_value(100.*(double)x/(double)(pl.pixels_[0]-1)); + for (int z = 0; z < pl.pixels_[2]; z++) { + pb.set_value(100.*(double)z/(double)(pl.pixels_[2]-1)); for (int y = 0; y < pl.pixels_[1]; y++) { - for (int z = 0; z < pl.pixels_[2]; z++) { + for (int x = 0; x < pl.pixels_[0]; x++) { // get voxel color position_rgb(p, pl, rgb, id); // write to plot data - data[y][z] = id; - // advance particle in z direction - p.coord[0].xyz[2] = p.coord[0].xyz[2] + vox[2]; + data[y][x] = id; + // advance particle in x direction + p.coord[0].xyz[0] = p.coord[0].xyz[0] + vox[0]; } // advance particle in y direction p.coord[0].xyz[1] = p.coord[0].xyz[1] + vox[1]; - p.coord[0].xyz[2] = ll[2]; + p.coord[0].xyz[0] = ll[0]; } - // advance particle in x direction - p.coord[0].xyz[0] = p.coord[0].xyz[0] + vox[0]; + // advance particle in z direction + p.coord[0].xyz[2] = p.coord[0].xyz[2] + vox[2]; p.coord[0].xyz[1] = ll[1]; - p.coord[0].xyz[2] = ll[2]; + p.coord[0].xyz[0] = ll[0]; // Write to HDF5 dataset - voxel_write_slice(x, dspace, dset, memspace, &(data[0])); + voxel_write_slice(z, dspace, dset, memspace, &(data[0])); } voxel_finalize(dspace, dset, memspace); From 125812c487da0e56a8bc6f0751462696404fcf4c Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 27 Feb 2019 18:09:59 -0600 Subject: [PATCH 5/7] Updating voxel version and using that for a check rather than the OpenMC version. --- include/openmc/constants.h | 2 +- scripts/openmc-voxel-to-vtk | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/include/openmc/constants.h b/include/openmc/constants.h index 916f7693b..5a4c16911 100644 --- a/include/openmc/constants.h +++ b/include/openmc/constants.h @@ -33,7 +33,7 @@ constexpr std::array VERSION_PARTICLE_RESTART {2, 0}; constexpr std::array VERSION_TRACK {2, 0}; constexpr std::array VERSION_SUMMARY {6, 0}; constexpr std::array VERSION_VOLUME {1, 0}; -constexpr std::array VERSION_VOXEL {1, 0}; +constexpr std::array VERSION_VOXEL {1, 1}; constexpr std::array VERSION_MGXS_LIBRARY {1, 0}; constexpr char VERSION_MULTIPOLE[] {"v0.2"}; diff --git a/scripts/openmc-voxel-to-vtk b/scripts/openmc-voxel-to-vtk index afac8984f..699f5a015 100755 --- a/scripts/openmc-voxel-to-vtk +++ b/scripts/openmc-voxel-to-vtk @@ -8,7 +8,7 @@ import numpy as np import h5py import vtk -_min_version = (0,10,0) +_min_version = (1,1) def main(): @@ -23,13 +23,14 @@ def main(): fh = h5py.File(args.voxel_file, 'r') # check version - version = tuple(fh.attrs['openmc_version']) + version = tuple(fh.attrs['version']) if version < _min_version: old_version = ".".join(map(str,version)) min_version = ".".join(map(str,_min_version)) - err_msg = "This voxel file was generated using OpenMC " \ - "version {}. Please re-generate using "\ - "version {} or higher.".format(old_version, min_version) + err_msg = "This voxel file's version is {}. This script " \ + "only supports voxel files with version {} or " \ + "higher. Please generate a new voxel file using " \ + "a newer version of OpenMC.".format(old_version, min_version) raise ValueError(err_msg) dimension = fh.attrs['num_voxels'] From a3b93a0e8f193c35eb6709ff378180dbf3df2e0b Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 27 Feb 2019 18:11:33 -0600 Subject: [PATCH 6/7] Updating related test result files. --- tests/regression_tests/plot/results_true.dat | 2 +- tests/regression_tests/plot_voxel/results_true.dat | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/regression_tests/plot/results_true.dat b/tests/regression_tests/plot/results_true.dat index ba8a49226..7f8fcc6a8 100644 --- a/tests/regression_tests/plot/results_true.dat +++ b/tests/regression_tests/plot/results_true.dat @@ -1 +1 @@ -01ecda0f3820a49c8a41d8dc47d1e5c58767a04301621c2437231fcc04401ddea47b67d0529ca56a32d4d97b4f1416a2e0b6120d3bdc87d74a7e9889758a8808 \ No newline at end of file +30f435795c755b6fb28565a33c68e95415e5c09bca59f5549a6cbd2dd165bac9d90f5a599e12a5041cb927698abde562b7bac14175441c57b5c0dccd00544506 \ No newline at end of file diff --git a/tests/regression_tests/plot_voxel/results_true.dat b/tests/regression_tests/plot_voxel/results_true.dat index 41193c9bc..801621784 100644 --- a/tests/regression_tests/plot_voxel/results_true.dat +++ b/tests/regression_tests/plot_voxel/results_true.dat @@ -1 +1 @@ -20a0c3598bb698efa4bba65c5e55d71f4385e5b1bcf0067964e45001c12f5c0a729935a96409c760dbd3ec439ae6c676fce77d6e578b41f8f3e0ac68c9277acb \ No newline at end of file +76274390783bf0fd54a63e3ee141e072e797a564935a16f22a97788bc051e33fd7160dd311f0c50e45b0b498701f6735a37fabacbf9f36903381d9e18759a684 \ No newline at end of file From 04526b0f075c13ee990b4cba704f1d105ed4732f Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Thu, 28 Feb 2019 09:05:47 -0600 Subject: [PATCH 7/7] Bumping major version of voxel file rather than minor. --- include/openmc/constants.h | 2 +- scripts/openmc-voxel-to-vtk | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/openmc/constants.h b/include/openmc/constants.h index 5a4c16911..067e6431a 100644 --- a/include/openmc/constants.h +++ b/include/openmc/constants.h @@ -33,7 +33,7 @@ constexpr std::array VERSION_PARTICLE_RESTART {2, 0}; constexpr std::array VERSION_TRACK {2, 0}; constexpr std::array VERSION_SUMMARY {6, 0}; constexpr std::array VERSION_VOLUME {1, 0}; -constexpr std::array VERSION_VOXEL {1, 1}; +constexpr std::array VERSION_VOXEL {2, 0}; constexpr std::array VERSION_MGXS_LIBRARY {1, 0}; constexpr char VERSION_MULTIPOLE[] {"v0.2"}; diff --git a/scripts/openmc-voxel-to-vtk b/scripts/openmc-voxel-to-vtk index 699f5a015..c7f5ffa1a 100755 --- a/scripts/openmc-voxel-to-vtk +++ b/scripts/openmc-voxel-to-vtk @@ -8,7 +8,7 @@ import numpy as np import h5py import vtk -_min_version = (1,1) +_min_version = (2,0) def main():