From 1d78df26eb4256535ddeada1dc1eb5317ee578e7 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Mon, 11 Mar 2019 10:14:17 -0500 Subject: [PATCH 01/32] Generalizing slice operation. Using templates to specialize data returned. --- include/openmc/plot.h | 12 ++- src/plot.cpp | 171 ++++++++++++++++++++++++++---------------- 2 files changed, 117 insertions(+), 66 deletions(-) diff --git a/include/openmc/plot.h b/include/openmc/plot.h index 8a9e4e74dc..63246b2653 100644 --- a/include/openmc/plot.h +++ b/include/openmc/plot.h @@ -53,6 +53,7 @@ struct RGBColor { typedef xt::xtensor ImageData; typedef xt::xtensor IdData; +typedef xt::xtensor PropertyData; enum class PlotType { slice = 1, @@ -73,8 +74,17 @@ enum class PlotColorBy { //=============================================================================== // Plot class //=============================================================================== -struct PlotBase { +class PlotBase { + public: + IdData get_id_map() const; + PropertyData get_property_map() const; + + private: + template + D generate_data() const; + // Members + public: Position origin_; //!< Plot origin in geometry Position width_; //!< Plot width in geometry PlotBasis basis_; //!< Plot basis (XY/XZ/YZ) diff --git a/src/plot.cpp b/src/plot.cpp index 729b2db486..95f7acad76 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -29,6 +29,34 @@ namespace openmc { const RGBColor WHITE {255, 255, 255}; constexpr int PLOT_LEVEL_LOWEST {-1}; //!< lower bound on plot universe level constexpr int NOT_FOUND {-1}; + +struct id_setter { + void operator()(const Particle& p, IdData& ids, int y, int x, int level) { + Cell* c = model::cells[p.coord_[level].cell].get(); + ids(y,x,0) = c->id_; + if (c->type_ == FILL_UNIVERSE || p.material_ == MATERIAL_VOID) { + ids(y,x,1) = NOT_FOUND; + } else { + Material* m = model::materials[p.material_].get(); + ids(y,x,1) = m->id_; + } + } +}; + +struct property_setter { + void operator()(const Particle& p, PropertyData& props, int y, int x, int level) { + Cell* c = model::cells[p.coord_[level].cell].get(); + props(y,x,0) = (p.sqrtkT_ * p.sqrtkT_) * K_BOLTZMANN; + if (c->type_ == FILL_UNIVERSE || p.material_ == MATERIAL_VOID) { + props(y,x, 1) = NOT_FOUND; + } else { + Material* m = model::materials[p.material_].get(); + props(y,x,1) = m->density_gpcc_; + } + } +}; + + //============================================================================== // Global variables //============================================================================== @@ -637,6 +665,82 @@ Plot::Plot(pugi::xml_node plot_node) set_mask(plot_node); } // End Plot constructor +template +D PlotBase::generate_data() const { + + size_t width = pixels_[0]; + size_t height = pixels_[1]; + + // get pixel size + double in_pixel = (width_[0])/static_cast(width); + double out_pixel = (width_[1])/static_cast(height); + + // size data array + D data({height, width, 2}, NOT_FOUND); + + // setup basis indices and initial position centered on pixel + int in_i, out_i; + Position xyz = origin_; + switch(basis_) { + case PlotBasis::xy : + in_i = 0; + out_i = 1; + break; + case PlotBasis::xz : + in_i = 0; + out_i = 2; + break; + case PlotBasis::yz : + in_i = 1; + out_i = 2; + break; + } + + // set initial position + xyz[in_i] = origin_[in_i] - width_[0] / 2. + in_pixel / 2.; + xyz[out_i] = origin_[out_i] + width_[1] / 2. - out_pixel / 2.; + + // arbitrary direction + Direction dir = {0.5, 0.5, 0.5}; + +#pragma omp parallel +{ + Particle p; + p.r() = xyz; + p.u() = dir; + p.coord_[0].universe = model::root_universe; + int level = level_; + int j{}; + + #pragma omp for + for (int y = 0; y < height; y++) { + p.r()[out_i] = xyz[out_i] - out_pixel * y; + for (int x = 0; x < width; x++) { + p.r()[in_i] = xyz[in_i] + in_pixel * x; + p.n_coord_ = 1; + // local variables + bool found_cell = find_cell(&p, 0); + j = p.n_coord_ - 1; + if (level >=0) {j = level + 1;} + if (found_cell) { + setter()(p, data, y, x, j); + Cell* c = model::cells[p.coord_[j].cell].get(); + } + } // inner for + } // outer for + } // omp parallel + + return data; +} + +IdData PlotBase::get_id_map() const { + return generate_data(); +} + +PropertyData PlotBase::get_property_map() const { + return generate_data(); +} + //============================================================================== // POSITION_RGB computes the red/green/blue values for a given plot with the // current particle's position @@ -963,73 +1067,10 @@ extern "C" int openmc_id_map(const void* plot, int32_t* data_out) return OPENMC_E_INVALID_ARGUMENT; } - size_t width = plt->pixels_[0]; - size_t height = plt->pixels_[1]; - - // get pixel size - double in_pixel = (plt->width_[0])/static_cast(width); - double out_pixel = (plt->width_[1])/static_cast(height); - - // size data array - IdData data({height, width, 2}, NOT_FOUND); - - // setup basis indices and initial position centered on pixel - int in_i, out_i; - Position xyz = plt->origin_; - switch(plt->basis_) { - case PlotBasis::xy : - in_i = 0; - out_i = 1; - break; - case PlotBasis::xz : - in_i = 0; - out_i = 2; - break; - case PlotBasis::yz : - in_i = 1; - out_i = 2; - break; - } - - // set initial position - xyz[in_i] = plt->origin_[in_i] - plt->width_[0] / 2. + in_pixel / 2.; - xyz[out_i] = plt->origin_[out_i] + plt->width_[1] / 2. - out_pixel / 2.; - - // arbitrary direction - Direction dir = {0.5, 0.5, 0.5}; - - #pragma omp parallel - { - Particle p; - p.r() = xyz; - p.u() = dir; - p.coord_[0].universe = model::root_universe; - int level = plt->level_; - int j{}; - - #pragma omp for - for (int y = 0; y < height; y++) { - p.r()[out_i] = xyz[out_i] - out_pixel * y; - for (int x = 0; x < width; x++) { - p.r()[in_i] = xyz[in_i] + in_pixel * x; - p.n_coord_ = 1; - // local variables - bool found_cell = find_cell(&p, 0); - j = p.n_coord_ - 1; - if (level >=0) {j = level + 1;} - if (found_cell) { - Cell* c = model::cells[p.coord_[j].cell].get(); - data(y,x,0) = c->id_; - if (c->type_ != FILL_UNIVERSE && p.material_ != MATERIAL_VOID) { - data(y,x,1) = model::materials[p.material_]->id_; - } - } - } // inner for - } // outer for - } // omp parallel + auto ids = plt->get_id_map(); // write id data to array - std::copy(data.begin(), data.end(), data_out); + std::copy(ids.begin(), ids.end(), data_out); return 0; } From b9a57b02d453324b70024aa78681c12a98c2d2dc Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Mon, 11 Mar 2019 10:55:54 -0500 Subject: [PATCH 02/32] Exposing properties via capi and Python interface. --- include/openmc/capi.h | 3 ++- openmc/capi/plot.py | 23 +++++++++++++++++++++++ src/plot.cpp | 20 +++++++++++++++++++- tests/unit_tests/test_capi.py | 21 +++++++++++++++++++++ 4 files changed, 65 insertions(+), 2 deletions(-) diff --git a/include/openmc/capi.h b/include/openmc/capi.h index 5d07edb212..5e74446f1a 100644 --- a/include/openmc/capi.h +++ b/include/openmc/capi.h @@ -70,7 +70,8 @@ extern "C" { int openmc_next_batch(int* status); int openmc_nuclide_name(int index, const char** name); int openmc_plot_geometry(); - int openmc_id_map(const void* slice, int32_t* data_out); + int openmc_id_map(const void* slice, int32_t *data_out); + int openmc_property_map(void* slice, double *data_out); int openmc_reset(); int openmc_run(); void openmc_set_seed(int64_t new_seed); diff --git a/openmc/capi/plot.py b/openmc/capi/plot.py index 78fcef912c..e8d1c06b4c 100644 --- a/openmc/capi/plot.py +++ b/openmc/capi/plot.py @@ -218,3 +218,26 @@ def id_map(plot): _dll.openmc_id_map(POINTER(_PlotBase)(plot), img_data.ctypes.data_as(POINTER(c_int32))) return img_data + + +def property_map(plot): + """ + Generate a 2-D map of (cell_temperature, material_density). Used for + in-memory image generation. + + Parameters + ---------- + plot : An openmc.capi.plot._PlotBase object describing the slice of the + model to be generated + + Returns + ------- + property_map : a NumPy array with shape (vertical pixels, horizontal pixels, 2) + of OpenMC property ids with dtype float + + """ + prop_data = np.zeros((plot.vRes, plot.hRes, 2), + dtype=np.dtype('float')) + _dll.openmc_property_map(POINTER(_PlotBase)(plot), + prop_data.ctypes.data_as(POINTER(c_double))) + return prop_data diff --git a/src/plot.cpp b/src/plot.cpp index 95f7acad76..5a35ceb40c 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -46,7 +46,7 @@ struct id_setter { struct property_setter { void operator()(const Particle& p, PropertyData& props, int y, int x, int level) { Cell* c = model::cells[p.coord_[level].cell].get(); - props(y,x,0) = (p.sqrtkT_ * p.sqrtkT_) * K_BOLTZMANN; + props(y,x,0) = (p.sqrtkT_ * p.sqrtkT_) / K_BOLTZMANN; if (c->type_ == FILL_UNIVERSE || p.material_ == MATERIAL_VOID) { props(y,x, 1) = NOT_FOUND; } else { @@ -1075,4 +1075,22 @@ extern "C" int openmc_id_map(const void* plot, int32_t* data_out) return 0; } +extern "C" int openmc_property_map(void* plot, double* data_out) { + + auto plt = reinterpret_cast(plot); + if (!plt) { + set_errmsg("Invalid slice pointer passed to openmc_id_map"); + return OPENMC_E_INVALID_ARGUMENT; + } + + PropertyData data = plt->get_property_map(); + + // write id data to array + size_t i = 0; + for (auto v : data) { data_out[i++] = v; } + + return 0; +} + + } // namespace openmc diff --git a/tests/unit_tests/test_capi.py b/tests/unit_tests/test_capi.py index 97d18531bd..1d2fb2ecba 100644 --- a/tests/unit_tests/test_capi.py +++ b/tests/unit_tests/test_capi.py @@ -419,3 +419,24 @@ def test_id_map(capi_init): ids = openmc.capi.plot.id_map(s) assert np.array_equal(expected_ids, ids) + +def test_property_map(capi_init): + expected_properties = np.array( + [[(293.6, 0.740582), (293.6, 6.55), (293.6, 0.740582)], + [ (293.6, 6.55), (293.6, 10.29769), (293.6, 6.55)], + [(293.6, 0.740582), (293.6, 6.55), (293.6, 0.740582)]], dtype='float') + + # create a plot object + s = openmc.capi.plot._PlotBase() + s.width = 1.26 + s.height = 1.26 + s.vRes = 3 + s.hRes = 3 + s.origin = (0.0, 0.0, 0.0) + s.basis = 'xy' + s.level = -1 + + properties = openmc.capi.plot.property_map(s) + print(properties) + print(expected_properties) + assert np.allclose(expected_properties, properties, atol=1e-04) From cdaad6ad5461f3d2bf2ed7f24d5fd327946733ef Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Tue, 12 Mar 2019 14:41:02 -0500 Subject: [PATCH 03/32] Making pointer for property_map const and fixing property_map tests. --- include/openmc/capi.h | 2 +- openmc/capi/plot.py | 2 +- src/plot.cpp | 9 ++++----- tests/unit_tests/test_capi.py | 6 ++---- 4 files changed, 8 insertions(+), 11 deletions(-) diff --git a/include/openmc/capi.h b/include/openmc/capi.h index 5e74446f1a..617153b6c6 100644 --- a/include/openmc/capi.h +++ b/include/openmc/capi.h @@ -71,7 +71,7 @@ extern "C" { int openmc_nuclide_name(int index, const char** name); int openmc_plot_geometry(); int openmc_id_map(const void* slice, int32_t *data_out); - int openmc_property_map(void* slice, double *data_out); + int openmc_property_map(const void* slice, double *data_out); int openmc_reset(); int openmc_run(); void openmc_set_seed(int64_t new_seed); diff --git a/openmc/capi/plot.py b/openmc/capi/plot.py index e8d1c06b4c..c950f278bc 100644 --- a/openmc/capi/plot.py +++ b/openmc/capi/plot.py @@ -236,7 +236,7 @@ def property_map(plot): of OpenMC property ids with dtype float """ - prop_data = np.zeros((plot.vRes, plot.hRes, 2), + prop_data = np.zeros((plot.v_res, plot.h_res, 2), dtype=np.dtype('float')) _dll.openmc_property_map(POINTER(_PlotBase)(plot), prop_data.ctypes.data_as(POINTER(c_double))) diff --git a/src/plot.cpp b/src/plot.cpp index 5a35ceb40c..45782b802d 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -1075,19 +1075,18 @@ extern "C" int openmc_id_map(const void* plot, int32_t* data_out) return 0; } -extern "C" int openmc_property_map(void* plot, double* data_out) { +extern "C" int openmc_property_map(const void* plot, double* data_out) { - auto plt = reinterpret_cast(plot); + auto plt = reinterpret_cast(plot); if (!plt) { set_errmsg("Invalid slice pointer passed to openmc_id_map"); return OPENMC_E_INVALID_ARGUMENT; } - PropertyData data = plt->get_property_map(); + PropertyData props = plt->get_property_map(); // write id data to array - size_t i = 0; - for (auto v : data) { data_out[i++] = v; } + std::copy(props.begin(), props.end(), data_out); return 0; } diff --git a/tests/unit_tests/test_capi.py b/tests/unit_tests/test_capi.py index 1d2fb2ecba..14dac21fc8 100644 --- a/tests/unit_tests/test_capi.py +++ b/tests/unit_tests/test_capi.py @@ -430,13 +430,11 @@ def test_property_map(capi_init): s = openmc.capi.plot._PlotBase() s.width = 1.26 s.height = 1.26 - s.vRes = 3 - s.hRes = 3 + s.v_res = 3 + s.h_res = 3 s.origin = (0.0, 0.0, 0.0) s.basis = 'xy' s.level = -1 properties = openmc.capi.plot.property_map(s) - print(properties) - print(expected_properties) assert np.allclose(expected_properties, properties, atol=1e-04) From 49ee1c4048dc1efc42e2b72a464ecb25d1dc162e Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Tue, 12 Mar 2019 17:37:27 -0500 Subject: [PATCH 04/32] Making datasets structs with set_value methods instead of templating setters. --- include/openmc/plot.h | 27 ++++++++++++++++--- src/plot.cpp | 63 ++++++++++++++++++++++--------------------- 2 files changed, 56 insertions(+), 34 deletions(-) diff --git a/include/openmc/plot.h b/include/openmc/plot.h index 63246b2653..b879dbe369 100644 --- a/include/openmc/plot.h +++ b/include/openmc/plot.h @@ -51,9 +51,30 @@ struct RGBColor { uint8_t red, green, blue; }; + typedef xt::xtensor ImageData; -typedef xt::xtensor IdData; -typedef xt::xtensor PropertyData; + +struct IdData { + // Constructor + IdData(int h_res, int v_res); + + // Methods + void set_value(int y, int x, const Particle& p, int level); + + // Members + xt::xtensor data; +}; + +struct PropertyData { + // Constructor + PropertyData(int h_res, int v_res); + + // Methods + void set_value(int y, int x, const Particle& p, int level); + + // Members + xt::xtensor data; +}; enum class PlotType { slice = 1, @@ -80,7 +101,7 @@ class PlotBase { PropertyData get_property_map() const; private: - template + template D generate_data() const; // Members diff --git a/src/plot.cpp b/src/plot.cpp index 45782b802d..cbdae30d55 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -28,34 +28,35 @@ namespace openmc { const RGBColor WHITE {255, 255, 255}; constexpr int PLOT_LEVEL_LOWEST {-1}; //!< lower bound on plot universe level -constexpr int NOT_FOUND {-1}; +constexpr int32_t NOT_FOUND {-1}; -struct id_setter { - void operator()(const Particle& p, IdData& ids, int y, int x, int level) { - Cell* c = model::cells[p.coord_[level].cell].get(); - ids(y,x,0) = c->id_; - if (c->type_ == FILL_UNIVERSE || p.material_ == MATERIAL_VOID) { - ids(y,x,1) = NOT_FOUND; - } else { - Material* m = model::materials[p.material_].get(); - ids(y,x,1) = m->id_; - } +IdData::IdData(int h_res, int v_res) { + data = xt::xtensor({v_res, h_res, 2}, NOT_FOUND); +} + +void +IdData::set_value(int y, int x, const Particle& p, int level) { + Cell* c = model::cells[p.coord_[level].cell].get(); + data(y,x,0) = c->id_; + if (c->type_ != FILL_UNIVERSE && p.material_ != MATERIAL_VOID) { + Material* m = model::materials[p.material_].get(); + data(y,x,1) = m->id_; } -}; +} -struct property_setter { - void operator()(const Particle& p, PropertyData& props, int y, int x, int level) { - Cell* c = model::cells[p.coord_[level].cell].get(); - props(y,x,0) = (p.sqrtkT_ * p.sqrtkT_) / K_BOLTZMANN; - if (c->type_ == FILL_UNIVERSE || p.material_ == MATERIAL_VOID) { - props(y,x, 1) = NOT_FOUND; - } else { - Material* m = model::materials[p.material_].get(); - props(y,x,1) = m->density_gpcc_; - } +PropertyData::PropertyData(int h_res, int v_res) { + data = xt::xtensor({v_res, h_res, 2}, NOT_FOUND); +} + +void +PropertyData::set_value(int y, int x, const Particle& p, int level) { + Cell* c = model::cells[p.coord_[level].cell].get(); + data(y,x,0) = (p.sqrtkT_ * p.sqrtkT_) / K_BOLTZMANN; + if (c->type_ != FILL_UNIVERSE && p.material_ != MATERIAL_VOID) { + Material* m = model::materials[p.material_].get(); + data(y,x,1) = m->density_gpcc_; } -}; - +} //============================================================================== // Global variables @@ -665,7 +666,7 @@ Plot::Plot(pugi::xml_node plot_node) set_mask(plot_node); } // End Plot constructor -template +template D PlotBase::generate_data() const { size_t width = pixels_[0]; @@ -676,7 +677,7 @@ D PlotBase::generate_data() const { double out_pixel = (width_[1])/static_cast(height); // size data array - D data({height, width, 2}, NOT_FOUND); + D data(width, height); // setup basis indices and initial position centered on pixel int in_i, out_i; @@ -723,7 +724,7 @@ D PlotBase::generate_data() const { j = p.n_coord_ - 1; if (level >=0) {j = level + 1;} if (found_cell) { - setter()(p, data, y, x, j); + data.set_value(y, x, p, j); Cell* c = model::cells[p.coord_[j].cell].get(); } } // inner for @@ -734,11 +735,11 @@ D PlotBase::generate_data() const { } IdData PlotBase::get_id_map() const { - return generate_data(); + return generate_data(); } PropertyData PlotBase::get_property_map() const { - return generate_data(); + return generate_data(); } //============================================================================== @@ -1070,7 +1071,7 @@ extern "C" int openmc_id_map(const void* plot, int32_t* data_out) auto ids = plt->get_id_map(); // write id data to array - std::copy(ids.begin(), ids.end(), data_out); + std::copy(ids.data.begin(), ids.data.end(), data_out); return 0; } @@ -1086,7 +1087,7 @@ extern "C" int openmc_property_map(const void* plot, double* data_out) { PropertyData props = plt->get_property_map(); // write id data to array - std::copy(props.begin(), props.end(), data_out); + std::copy(props.data.begin(), props.data.end(), data_out); return 0; } From eb95c91a2dbe1c8375b72cdc74259259275edc2c Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Tue, 12 Mar 2019 18:15:04 -0500 Subject: [PATCH 05/32] Replacing plot loop in create_ppm with new get_id_map method. --- src/plot.cpp | 104 ++++++++++++++++++++++++++++----------------------- 1 file changed, 58 insertions(+), 46 deletions(-) diff --git a/src/plot.cpp b/src/plot.cpp index cbdae30d55..ccd96fde4a 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -129,57 +129,69 @@ void create_ppm(Plot pl) size_t width = pl.pixels_[0]; size_t height = pl.pixels_[1]; - double in_pixel = (pl.width_[0])/static_cast(width); - double out_pixel = (pl.width_[1])/static_cast(height); + // double in_pixel = (pl.width_[0])/static_cast(width); + // double out_pixel = (pl.width_[1])/static_cast(height); - ImageData data; - data.resize({width, height}); + ImageData data({width, height}, pl.not_found_); - int in_i, out_i; - Position r; - switch(pl.basis_) { - case PlotBasis::xy : - in_i = 0; - out_i = 1; - r.x = pl.origin_[0] - pl.width_[0] / 2.; - r.y = pl.origin_[1] + pl.width_[1] / 2.; - r.z = pl.origin_[2]; - break; - case PlotBasis::xz : - in_i = 0; - out_i = 2; - r.x = pl.origin_[0] - pl.width_[0] / 2.; - r.y = pl.origin_[1]; - r.z = pl.origin_[2] + pl.width_[1] / 2.; - break; - case PlotBasis::yz : - in_i = 1; - out_i = 2; - r.x = pl.origin_[0]; - r.y = pl.origin_[1] - pl.width_[0] / 2.; - r.z = pl.origin_[2] + pl.width_[1] / 2.; - break; - } +// int in_i, out_i; +// Position r; +// switch(pl.basis_) { +// case PlotBasis::xy : +// in_i = 0; +// out_i = 1; +// r.x = pl.origin_[0] - pl.width_[0] / 2.; +// r.y = pl.origin_[1] + pl.width_[1] / 2.; +// r.z = pl.origin_[2]; +// break; +// case PlotBasis::xz : +// in_i = 0; +// out_i = 2; +// r.x = pl.origin_[0] - pl.width_[0] / 2.; +// r.y = pl.origin_[1]; +// r.z = pl.origin_[2] + pl.width_[1] / 2.; +// break; +// case PlotBasis::yz : +// in_i = 1; +// out_i = 2; +// r.x = pl.origin_[0]; +// r.y = pl.origin_[1] - pl.width_[0] / 2.; +// r.z = pl.origin_[2] + pl.width_[1] / 2.; +// break; +// } - Direction u {0.7071, 0.7071, 0.0}; +// Direction u {0.5, 0.5, 0.5}; - #pragma omp parallel - { - Particle p; - p.r() = r; - p.u() = u; - p.coord_[0].universe = model::root_universe; +// #pragma omp parallel +// { +// Particle p; +// p.r() = r; +// p.u() = u; +// p.coord_[0].universe = model::root_universe; - #pragma omp for - for (int y = 0; y < height; y++) { - p.r()[out_i] = r[out_i] - out_pixel * y; - for (int x = 0; x < width; x++) { - // local variables - RGBColor rgb; - int id; - p.r()[in_i] = r[in_i] + in_pixel * x; - position_rgb(p, pl, rgb, id); - data(x,y) = rgb; +// #pragma omp for +// for (int y = 0; y < height; y++) { +// p.r()[out_i] = r[out_i] - out_pixel * y; +// for (int x = 0; x < width; x++) { +// // local variables +// RGBColor rgb; +// int id; +// p.r()[in_i] = r[in_i] + in_pixel * x; +// position_rgb(p, pl, rgb, id); +// data(x,y) = rgb; +// } +// } +// } + + auto ids = pl.get_id_map(); + + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + RGBColor color; + if (PlotColorBy::cells == pl.color_by_) { + data(x,y) = pl.colors_[model::cell_map[ids.data(y,x,0)]]; + } else if (PlotColorBy::mats == pl.color_by_) { + data(x,y) = pl.colors_[model::material_map[ids.data(y,x,1)]]; } } } From 4c50cbcda8686d43da6c9b19909077ee3d97eba9 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Tue, 12 Mar 2019 20:35:21 -0500 Subject: [PATCH 06/32] Removing original loop from create_ppm. --- src/plot.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/plot.cpp b/src/plot.cpp index ccd96fde4a..da3bdf549f 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -4,6 +4,8 @@ #include #include +#include "xtensor/xview.hpp" + #include "openmc/cell.h" #include "openmc/constants.h" #include "openmc/file_utils.h" @@ -132,7 +134,8 @@ void create_ppm(Plot pl) // double in_pixel = (pl.width_[0])/static_cast(width); // double out_pixel = (pl.width_[1])/static_cast(height); - ImageData data({width, height}, pl.not_found_); + ImageData data({width, height}); + xt::view(data, xt::all(), xt::all()) = pl.not_found_; // int in_i, out_i; // Position r; From 09258c5b79c6d66d1735a824dcdb5cfc670008c9 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 13 Mar 2019 15:19:01 -0500 Subject: [PATCH 07/32] Plots working as before using the single loop. --- include/openmc/plot.h | 4 +-- src/plot.cpp | 76 ++++++++++--------------------------------- 2 files changed, 19 insertions(+), 61 deletions(-) diff --git a/include/openmc/plot.h b/include/openmc/plot.h index b879dbe369..54035fd858 100644 --- a/include/openmc/plot.h +++ b/include/openmc/plot.h @@ -88,8 +88,8 @@ enum class PlotBasis { }; enum class PlotColorBy { - cells = 1, - mats = 2 + cells = 0, + mats = 1 }; //=============================================================================== diff --git a/src/plot.cpp b/src/plot.cpp index da3bdf549f..5e960b8d2e 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -30,6 +30,7 @@ namespace openmc { const RGBColor WHITE {255, 255, 255}; constexpr int PLOT_LEVEL_LOWEST {-1}; //!< lower bound on plot universe level +constexpr int32_t VOID {-2}; constexpr int32_t NOT_FOUND {-1}; IdData::IdData(int h_res, int v_res) { @@ -40,7 +41,10 @@ void IdData::set_value(int y, int x, const Particle& p, int level) { Cell* c = model::cells[p.coord_[level].cell].get(); data(y,x,0) = c->id_; - if (c->type_ != FILL_UNIVERSE && p.material_ != MATERIAL_VOID) { + if (p.material_ == MATERIAL_VOID) { + data(y,x,1) = VOID; + return; + } else if (c->type_ != FILL_UNIVERSE) { Material* m = model::materials[p.material_].get(); data(y,x,1) = m->id_; } @@ -131,73 +135,27 @@ void create_ppm(Plot pl) size_t width = pl.pixels_[0]; size_t height = pl.pixels_[1]; - // double in_pixel = (pl.width_[0])/static_cast(width); - // double out_pixel = (pl.width_[1])/static_cast(height); - ImageData data({width, height}); xt::view(data, xt::all(), xt::all()) = pl.not_found_; -// int in_i, out_i; -// Position r; -// switch(pl.basis_) { -// case PlotBasis::xy : -// in_i = 0; -// out_i = 1; -// r.x = pl.origin_[0] - pl.width_[0] / 2.; -// r.y = pl.origin_[1] + pl.width_[1] / 2.; -// r.z = pl.origin_[2]; -// break; -// case PlotBasis::xz : -// in_i = 0; -// out_i = 2; -// r.x = pl.origin_[0] - pl.width_[0] / 2.; -// r.y = pl.origin_[1]; -// r.z = pl.origin_[2] + pl.width_[1] / 2.; -// break; -// case PlotBasis::yz : -// in_i = 1; -// out_i = 2; -// r.x = pl.origin_[0]; -// r.y = pl.origin_[1] - pl.width_[0] / 2.; -// r.z = pl.origin_[2] + pl.width_[1] / 2.; -// break; -// } - -// Direction u {0.5, 0.5, 0.5}; - -// #pragma omp parallel -// { -// Particle p; -// p.r() = r; -// p.u() = u; -// p.coord_[0].universe = model::root_universe; - -// #pragma omp for -// for (int y = 0; y < height; y++) { -// p.r()[out_i] = r[out_i] - out_pixel * y; -// for (int x = 0; x < width; x++) { -// // local variables -// RGBColor rgb; -// int id; -// p.r()[in_i] = r[in_i] + in_pixel * x; -// position_rgb(p, pl, rgb, id); -// data(x,y) = rgb; -// } -// } -// } - auto ids = pl.get_id_map(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { - RGBColor color; + auto id = ids.data(y, x, pl.color_by_); + // no setting needed if not found + if (id == NOT_FOUND) { continue; } if (PlotColorBy::cells == pl.color_by_) { - data(x,y) = pl.colors_[model::cell_map[ids.data(y,x,0)]]; + data(x,y) = pl.colors_[model::cell_map[id]]; } else if (PlotColorBy::mats == pl.color_by_) { - data(x,y) = pl.colors_[model::material_map[ids.data(y,x,1)]]; - } - } - } + if (id == VOID) { + data(x,y) = WHITE; + continue; + } + data(x,y) = pl.colors_[model::material_map[id]]; + } // color_by if-else + } // x for loop + } // y for loop // draw mesh lines if present if (pl.index_meshlines_mesh_ >= 0) {draw_mesh_lines(pl, data);} From 898d1cfb7e1fcedfd55a71a6ed791fed0072faf4 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 13 Mar 2019 15:33:30 -0500 Subject: [PATCH 08/32] Using xtensor initializer constructor. --- src/plot.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/plot.cpp b/src/plot.cpp index 5e960b8d2e..b0731dedef 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -135,11 +135,12 @@ void create_ppm(Plot pl) size_t width = pl.pixels_[0]; size_t height = pl.pixels_[1]; - ImageData data({width, height}); - xt::view(data, xt::all(), xt::all()) = pl.not_found_; + ImageData data({width, height}, pl.not_found_); + // generate ids for the plot auto ids = pl.get_id_map(); + // assign colors for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { auto id = ids.data(y, x, pl.color_by_); From 82229019af0944e7adc5113da4b1374da1fd1afd Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 13 Mar 2019 18:23:24 -0500 Subject: [PATCH 09/32] Updating plotting results now that locations are centered on pixels. --- tests/regression_tests/plot/results_true.dat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/regression_tests/plot/results_true.dat b/tests/regression_tests/plot/results_true.dat index 7f8fcc6a82..b3db3f33bd 100644 --- a/tests/regression_tests/plot/results_true.dat +++ b/tests/regression_tests/plot/results_true.dat @@ -1 +1 @@ -30f435795c755b6fb28565a33c68e95415e5c09bca59f5549a6cbd2dd165bac9d90f5a599e12a5041cb927698abde562b7bac14175441c57b5c0dccd00544506 \ No newline at end of file +42839208bdd8c8db16f9eba5c755b422dd1e49a755cc4d75284f31a3c086353f0eb6bd9abed8c29816dffe988d1e1e4f0dfae5fb8ffc03521294c8262d919061 \ No newline at end of file From 68cb23a6c837f781067aa7cef57f7abad795dcee Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Sat, 16 Mar 2019 17:17:12 -0500 Subject: [PATCH 10/32] Updating property_map documentation. --- openmc/capi/plot.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/openmc/capi/plot.py b/openmc/capi/plot.py index c950f278bc..b686373b8f 100644 --- a/openmc/capi/plot.py +++ b/openmc/capi/plot.py @@ -227,13 +227,14 @@ def property_map(plot): Parameters ---------- - plot : An openmc.capi.plot._PlotBase object describing the slice of the - model to be generated + plot : openmc.capi.plot._PlotBase + Object describing the slice of the model to be generated Returns ------- - property_map : a NumPy array with shape (vertical pixels, horizontal pixels, 2) - of OpenMC property ids with dtype float + property_map : numpy.ndarray + a NumPy array with shape (vertical pixels, horizontal pixels, 2) of + OpenMC property ids with dtype float """ prop_data = np.zeros((plot.v_res, plot.h_res, 2), From c8843d1572ed59ce0af6c064ad4dce637baeea0b Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Sat, 16 Mar 2019 17:17:40 -0500 Subject: [PATCH 11/32] Decrementing NOT_FOUND to avoid a clash with MATERIAL_VOID. --- src/plot.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/plot.cpp b/src/plot.cpp index b0731dedef..e1bc37657e 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -30,8 +30,7 @@ namespace openmc { const RGBColor WHITE {255, 255, 255}; constexpr int PLOT_LEVEL_LOWEST {-1}; //!< lower bound on plot universe level -constexpr int32_t VOID {-2}; -constexpr int32_t NOT_FOUND {-1}; +constexpr int32_t NOT_FOUND {-2}; IdData::IdData(int h_res, int v_res) { data = xt::xtensor({v_res, h_res, 2}, NOT_FOUND); @@ -42,7 +41,7 @@ IdData::set_value(int y, int x, const Particle& p, int level) { Cell* c = model::cells[p.coord_[level].cell].get(); data(y,x,0) = c->id_; if (p.material_ == MATERIAL_VOID) { - data(y,x,1) = VOID; + data(y,x,1) = MATERIAL_VOID; return; } else if (c->type_ != FILL_UNIVERSE) { Material* m = model::materials[p.material_].get(); @@ -149,7 +148,7 @@ void create_ppm(Plot pl) if (PlotColorBy::cells == pl.color_by_) { data(x,y) = pl.colors_[model::cell_map[id]]; } else if (PlotColorBy::mats == pl.color_by_) { - if (id == VOID) { + if (id == MATERIAL_VOID) { data(x,y) = WHITE; continue; } @@ -750,7 +749,7 @@ void position_rgb(Particle p, Plot pl, RGBColor& rgb, int& id) } else if (p.material_ == MATERIAL_VOID) { // By default, color void cells white rgb = WHITE; - id = NOT_FOUND; + id = MATERIAL_VOID; } else { rgb = pl.colors_[p.material_]; id = model::materials[p.material_]->id_; @@ -1058,7 +1057,7 @@ extern "C" int openmc_property_map(const void* plot, double* data_out) { return OPENMC_E_INVALID_ARGUMENT; } - PropertyData props = plt->get_property_map(); + auto props = plt->get_property_map(); // write id data to array std::copy(props.data.begin(), props.data.end(), data_out); From 3ab8680d5eba1d754f20adc5067e19d22992e32a Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Sat, 16 Mar 2019 21:36:58 -0500 Subject: [PATCH 12/32] Updates to voxel plotting, need to investigate differences. --- include/openmc/plot.h | 2 ++ src/plot.cpp | 36 +++++++++++++-------------- tests/regression_tests/plot/plots.xml | 2 +- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/include/openmc/plot.h b/include/openmc/plot.h index 54035fd858..a3b5ee4d81 100644 --- a/include/openmc/plot.h +++ b/include/openmc/plot.h @@ -98,6 +98,8 @@ enum class PlotColorBy { class PlotBase { public: IdData get_id_map() const; + xt::xtensor get_cell_ids() const; + PropertyData get_property_map() const; private: diff --git a/src/plot.cpp b/src/plot.cpp index e1bc37657e..76af0da975 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -711,6 +711,11 @@ IdData PlotBase::get_id_map() const { return generate_data(); } +xt::xtensor PlotBase::get_cell_ids() const { + auto ids = get_id_map(); + return xt::flip(xt::view(ids.data, xt::all(), xt::all(), 0), 0); +} + PropertyData PlotBase::get_property_map() const { return generate_data(); } @@ -965,30 +970,23 @@ void create_voxel(Plot pl) int id; 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 x = 0; x < pl.pixels_[0]; x++) { - // get voxel color - position_rgb(p, pl, rgb, id); - // write to plot data - data[y][x] = id; - // advance particle in x direction - p.r().x += vox[0]; - } - // advance particle in y direction - p.r().y += vox[1]; - p.r().x = ll[0]; - } - // advance particle in z direction - p.r().z += vox[2]; - p.r().y = ll[1]; - p.r().x = ll[0]; + PlotBase pltbase; + pltbase.width_ = pl.width_; + pltbase.origin_ = pl.origin_; + pltbase.basis_ = PlotBasis::xy; + pltbase.pixels_ = pl.pixels_; + pltbase.level_ = pl.level_; + + pl.origin_.z += ll.z + z * vox[2]; + + auto data = pltbase.get_cell_ids(); + // Write to HDF5 dataset - voxel_write_slice(z, dspace, dset, memspace, &(data[0])); + voxel_write_slice(z, dspace, dset, memspace, &(data(0,0))); } voxel_finalize(dspace, dset, memspace); file_close(file_id); - } void diff --git a/tests/regression_tests/plot/plots.xml b/tests/regression_tests/plot/plots.xml index ecfe69125b..078666e759 100644 --- a/tests/regression_tests/plot/plots.xml +++ b/tests/regression_tests/plot/plots.xml @@ -24,7 +24,7 @@ - 100 100 10 + 500 500 500 0. 0. 0. 20 20 10 From d55f46f3744551d4f5bc33661edcb8ad050f6118 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Sun, 17 Mar 2019 09:46:47 -0500 Subject: [PATCH 13/32] Correcting origin location. --- src/plot.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plot.cpp b/src/plot.cpp index 76af0da975..b8c16fefe8 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -977,7 +977,7 @@ void create_voxel(Plot pl) pltbase.pixels_ = pl.pixels_; pltbase.level_ = pl.level_; - pl.origin_.z += ll.z + z * vox[2]; + pl.origin_.z = pl.origin_.z + z * vox[2]; auto data = pltbase.get_cell_ids(); From 506ecb9ee103be4a8d76179c068496d213c269c6 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Sun, 17 Mar 2019 11:24:03 -0500 Subject: [PATCH 14/32] Reverting change to plots.xml. Updating z coordinate location. --- src/plot.cpp | 2 +- tests/regression_tests/plot/plots.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plot.cpp b/src/plot.cpp index b8c16fefe8..9fd26f8a1f 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -977,7 +977,7 @@ void create_voxel(Plot pl) pltbase.pixels_ = pl.pixels_; pltbase.level_ = pl.level_; - pl.origin_.z = pl.origin_.z + z * vox[2]; + pltbase.origin_.z = ll.z + z * vox[2]; auto data = pltbase.get_cell_ids(); diff --git a/tests/regression_tests/plot/plots.xml b/tests/regression_tests/plot/plots.xml index 078666e759..ecfe69125b 100644 --- a/tests/regression_tests/plot/plots.xml +++ b/tests/regression_tests/plot/plots.xml @@ -24,7 +24,7 @@ - 500 500 500 + 100 100 10 0. 0. 0. 20 20 10 From 0270a4fe93bf8e289ffbb6578cbfebc6b6c2b527 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Sun, 17 Mar 2019 11:36:17 -0500 Subject: [PATCH 15/32] Cleaning out unused variables in create_voxel. --- src/plot.cpp | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/src/plot.cpp b/src/plot.cpp index 9fd26f8a1f..585c8f43c7 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -919,13 +919,6 @@ void create_voxel(Plot pl) // initial particle position Position ll = pl.origin_ - pl.width_ / 2.; - // allocate and initialize particle - Direction u {0.7071, 0.7071, 0.0}; - Particle p; - p.r() = ll; - p.u() = u; - p.coord_[0].universe = model::root_universe; - // Open binary plot file for writing std::ofstream of; std::string fname = std::string(pl.path_plot_); @@ -957,28 +950,22 @@ void create_voxel(Plot pl) hid_t dspace, dset, memspace; voxel_init(file_id, &(dims[0]), &dspace, &dset, &memspace); - // move to center of voxels - ll.x += vox[0] / 2.; - ll.y += vox[1] / 2.; - ll.z += vox[2] / 2.; - - int data[pl.pixels_[1]][pl.pixels_[0]]; + PlotBase pltbase; + pltbase.width_ = pl.width_; + pltbase.origin_ = pl.origin_; + pltbase.basis_ = PlotBasis::xy; + pltbase.pixels_ = pl.pixels_; + pltbase.level_ = -1; // all universes for voxel files ProgressBar pb; - - RGBColor rgb; - int id; for (int z = 0; z < pl.pixels_[2]; z++) { + // update progress bar pb.set_value(100.*(double)z/(double)(pl.pixels_[2]-1)); - PlotBase pltbase; - pltbase.width_ = pl.width_; - pltbase.origin_ = pl.origin_; - pltbase.basis_ = PlotBasis::xy; - pltbase.pixels_ = pl.pixels_; - pltbase.level_ = pl.level_; + // update z coordinate pltbase.origin_.z = ll.z + z * vox[2]; + // generate ids using plotbase auto data = pltbase.get_cell_ids(); // Write to HDF5 dataset From e4ab6db1a9f06c8ff58d7af1178d730eac31dd29 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Sun, 17 Mar 2019 11:47:56 -0500 Subject: [PATCH 16/32] Updating plot/plot_voxel test results after change to NOT_FOUND = -2. --- 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 b3db3f33bd..18251e8d0c 100644 --- a/tests/regression_tests/plot/results_true.dat +++ b/tests/regression_tests/plot/results_true.dat @@ -1 +1 @@ -42839208bdd8c8db16f9eba5c755b422dd1e49a755cc4d75284f31a3c086353f0eb6bd9abed8c29816dffe988d1e1e4f0dfae5fb8ffc03521294c8262d919061 \ No newline at end of file +6b3eb36488b995b42233e6b7c0164cf6c92a1823b3c91985b97fd076f86c0aad93fbdb74e70026f5f12c61a6aee52a09588171a7fd839511ee7d9efc3952beb2 \ 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 8016217847..52222000f6 100644 --- a/tests/regression_tests/plot_voxel/results_true.dat +++ b/tests/regression_tests/plot_voxel/results_true.dat @@ -1 +1 @@ -76274390783bf0fd54a63e3ee141e072e797a564935a16f22a97788bc051e33fd7160dd311f0c50e45b0b498701f6735a37fabacbf9f36903381d9e18759a684 \ No newline at end of file +ff596b17d2cd33f964a952279b824292272ab73f57ded94812286e6bce12b5c54852d5ccd1dd137fa6312101568c06510cd89f822469d6c904a17f96259e4d03 \ No newline at end of file From 9c48ee69a4dcce9983473a1e16e5cfe5f71a33a9 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Sun, 17 Mar 2019 12:27:14 -0500 Subject: [PATCH 17/32] Removing now-unused function. --- include/openmc/plot.h | 2 -- src/plot.cpp | 13 +++++-------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/include/openmc/plot.h b/include/openmc/plot.h index a3b5ee4d81..54035fd858 100644 --- a/include/openmc/plot.h +++ b/include/openmc/plot.h @@ -98,8 +98,6 @@ enum class PlotColorBy { class PlotBase { public: IdData get_id_map() const; - xt::xtensor get_cell_ids() const; - PropertyData get_property_map() const; private: diff --git a/src/plot.cpp b/src/plot.cpp index 585c8f43c7..b61973d0e5 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -711,11 +711,6 @@ IdData PlotBase::get_id_map() const { return generate_data(); } -xt::xtensor PlotBase::get_cell_ids() const { - auto ids = get_id_map(); - return xt::flip(xt::view(ids.data, xt::all(), xt::all(), 0), 0); -} - PropertyData PlotBase::get_property_map() const { return generate_data(); } @@ -909,7 +904,6 @@ void draw_mesh_lines(Plot pl, ImageData& data) void create_voxel(Plot pl) { - // compute voxel widths in each direction std::array vox; vox[0] = pl.width_[0]/(double)pl.pixels_[0]; @@ -966,10 +960,13 @@ void create_voxel(Plot pl) pltbase.origin_.z = ll.z + z * vox[2]; // generate ids using plotbase - auto data = pltbase.get_cell_ids(); + IdData ids = pltbase.get_id_map(); + + // select only cell ID data and flip the y-axis + xt::xtensor data1 = xt::flip(xt::view(ids.data, xt::all(), xt::all(), 0), 0); // Write to HDF5 dataset - voxel_write_slice(z, dspace, dset, memspace, &(data(0,0))); + voxel_write_slice(z, dspace, dset, memspace, &(data1(0,0))); } voxel_finalize(dspace, dset, memspace); From 45dd3ecc0601cd23f80de8ced9020eed9808db54 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Sun, 17 Mar 2019 12:28:19 -0500 Subject: [PATCH 18/32] Updating some documentation. --- src/plot.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/plot.cpp b/src/plot.cpp index b61973d0e5..42634d3581 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -893,11 +893,11 @@ void draw_mesh_lines(Plot pl, ImageData& data) // CREATE_VOXEL outputs a binary file that can be input into silomesh for 3D // geometry visualization. It works the same way as create_ppm by dragging a // particle across the geometry for the specified number of voxels. The first 3 -// int(4)'s in the binary are the number of x, y, and z voxels. The next 3 -// real(8)'s are the widths of the voxels in the x, y, and z directions. The -// next 3 real(8)'s are the x, y, and z coordinates of the lower left -// point. Finally the binary is filled with entries of four int(4)'s each. Each -// 'row' in the binary contains four int(4)'s: 3 for x,y,z position and 1 for +// int's in the binary are the number of x, y, and z voxels. The next 3 +// double's are the widths of the voxels in the x, y, and z directions. The +// next 3 double's are the x, y, and z coordinates of the lower left +// point. Finally the binary is filled with entries of four int's each. Each +// 'row' in the binary contains four int's: 3 for x,y,z position and 1 for // cell or material id. For 1 million voxels this produces a file of // approximately 15MB. // ============================================================================= From e696374a914e83ff9894ff633bb520c4c2bb9d12 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Sun, 17 Mar 2019 19:26:08 -0500 Subject: [PATCH 19/32] Renaming the data attribute. --- include/openmc/plot.h | 4 ++-- src/plot.cpp | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/include/openmc/plot.h b/include/openmc/plot.h index 54035fd858..96c2f37ad2 100644 --- a/include/openmc/plot.h +++ b/include/openmc/plot.h @@ -62,7 +62,7 @@ struct IdData { void set_value(int y, int x, const Particle& p, int level); // Members - xt::xtensor data; + xt::xtensor data_; }; struct PropertyData { @@ -73,7 +73,7 @@ struct PropertyData { void set_value(int y, int x, const Particle& p, int level); // Members - xt::xtensor data; + xt::xtensor data_; }; enum class PlotType { diff --git a/src/plot.cpp b/src/plot.cpp index 42634d3581..202e8e99a5 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -33,33 +33,33 @@ constexpr int PLOT_LEVEL_LOWEST {-1}; //!< lower bound on plot universe level constexpr int32_t NOT_FOUND {-2}; IdData::IdData(int h_res, int v_res) { - data = xt::xtensor({v_res, h_res, 2}, NOT_FOUND); + data_ = xt::xtensor({v_res, h_res, 2}, NOT_FOUND); } void IdData::set_value(int y, int x, const Particle& p, int level) { Cell* c = model::cells[p.coord_[level].cell].get(); - data(y,x,0) = c->id_; + data_(y,x,0) = c->id_; if (p.material_ == MATERIAL_VOID) { - data(y,x,1) = MATERIAL_VOID; + data_(y,x,1) = MATERIAL_VOID; return; } else if (c->type_ != FILL_UNIVERSE) { Material* m = model::materials[p.material_].get(); - data(y,x,1) = m->id_; + data_(y,x,1) = m->id_; } } PropertyData::PropertyData(int h_res, int v_res) { - data = xt::xtensor({v_res, h_res, 2}, NOT_FOUND); + data_ = xt::xtensor({v_res, h_res, 2}, NOT_FOUND); } void PropertyData::set_value(int y, int x, const Particle& p, int level) { Cell* c = model::cells[p.coord_[level].cell].get(); - data(y,x,0) = (p.sqrtkT_ * p.sqrtkT_) / K_BOLTZMANN; + data_(y,x,0) = (p.sqrtkT_ * p.sqrtkT_) / K_BOLTZMANN; if (c->type_ != FILL_UNIVERSE && p.material_ != MATERIAL_VOID) { Material* m = model::materials[p.material_].get(); - data(y,x,1) = m->density_gpcc_; + data_(y,x,1) = m->density_gpcc_; } } @@ -142,7 +142,7 @@ void create_ppm(Plot pl) // assign colors for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { - auto id = ids.data(y, x, pl.color_by_); + auto id = ids.data_(y, x, pl.color_by_); // no setting needed if not found if (id == NOT_FOUND) { continue; } if (PlotColorBy::cells == pl.color_by_) { @@ -963,7 +963,7 @@ void create_voxel(Plot pl) IdData ids = pltbase.get_id_map(); // select only cell ID data and flip the y-axis - xt::xtensor data1 = xt::flip(xt::view(ids.data, xt::all(), xt::all(), 0), 0); + xt::xtensor data1 = xt::flip(xt::view(ids.data_, xt::all(), xt::all(), 0), 0); // Write to HDF5 dataset voxel_write_slice(z, dspace, dset, memspace, &(data1(0,0))); @@ -1026,7 +1026,7 @@ extern "C" int openmc_id_map(const void* plot, int32_t* data_out) auto ids = plt->get_id_map(); // write id data to array - std::copy(ids.data.begin(), ids.data.end(), data_out); + std::copy(ids.data_.begin(), ids.data_.end(), data_out); return 0; } @@ -1042,7 +1042,7 @@ extern "C" int openmc_property_map(const void* plot, double* data_out) { auto props = plt->get_property_map(); // write id data to array - std::copy(props.data.begin(), props.data.end(), data_out); + std::copy(props.data_.begin(), props.data_.end(), data_out); return 0; } From 2c598129dad2c58a75b48feb18dd96d51eab94eb Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Sun, 17 Mar 2019 19:27:47 -0500 Subject: [PATCH 20/32] Removing position_rgb function. --- include/openmc/plot.h | 7 ------- src/plot.cpp | 47 ------------------------------------------- 2 files changed, 54 deletions(-) diff --git a/include/openmc/plot.h b/include/openmc/plot.h index 96c2f37ad2..28e6734ebb 100644 --- a/include/openmc/plot.h +++ b/include/openmc/plot.h @@ -162,13 +162,6 @@ void draw_mesh_lines(Plot pl, ImageData& data); //! \param[out] image data associated with the plot object void output_ppm(Plot pl, const ImageData& data); -//! Get the rgb color for a given particle position in a plot -//! \param[in] particle with position for current pixel -//! \param[in] plot object -//! \param[out] rgb color -//! \param[out] cell or material id for particle position -void position_rgb(Particle p, Plot pl, RGBColor& rgb, int& id); - //! Initialize a voxel file //! \param[in] id of an open hdf5 file //! \param[in] dimensions of the voxel file (dx, dy, dz) diff --git a/src/plot.cpp b/src/plot.cpp index 202e8e99a5..edb50ed096 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -715,53 +715,6 @@ PropertyData PlotBase::get_property_map() const { return generate_data(); } -//============================================================================== -// POSITION_RGB computes the red/green/blue values for a given plot with the -// current particle's position -//============================================================================== - - -void position_rgb(Particle p, Plot pl, RGBColor& rgb, int& id) -{ - p.n_coord_ = 1; - - bool found_cell = find_cell(&p, 0); - - int j = p.n_coord_ - 1; - - if (settings::check_overlaps) {check_cell_overlap(&p);} - - // Set coordinate level if specified - if (pl.level_ >= 0) {j = pl.level_ + 1;} - - if (!found_cell) { - // If no cell, revert to default color - rgb = pl.not_found_; - id = NOT_FOUND; - } else { - if (PlotColorBy::mats == pl.color_by_) { - // Assign color based on material - const auto& c = model::cells[p.coord_[j].cell]; - if (c->type_ == FILL_UNIVERSE) { - // If we stopped on a middle universe level, treat as if not found - rgb = pl.not_found_; - id = NOT_FOUND; - } else if (p.material_ == MATERIAL_VOID) { - // By default, color void cells white - rgb = WHITE; - id = MATERIAL_VOID; - } else { - rgb = pl.colors_[p.material_]; - id = model::materials[p.material_]->id_; - } - } else if (PlotColorBy::cells == pl.color_by_) { - // Assign color based on cell - rgb = pl.colors_[p.coord_[j].cell]; - id = model::cells[p.coord_[j].cell]->id_; - } - } // endif found_cell -} - //============================================================================== // OUTPUT_PPM writes out a previously generated image to a PPM file //============================================================================== From 076dca84a26362d7d3f9385dfe01afd6dbe2c072 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Sun, 17 Mar 2019 19:30:46 -0500 Subject: [PATCH 21/32] Cleaning up some numpy docstring formatting. --- openmc/capi/plot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openmc/capi/plot.py b/openmc/capi/plot.py index b686373b8f..edb2e42a3d 100644 --- a/openmc/capi/plot.py +++ b/openmc/capi/plot.py @@ -223,7 +223,7 @@ def id_map(plot): def property_map(plot): """ Generate a 2-D map of (cell_temperature, material_density). Used for - in-memory image generation. + in-memory image generation. Parameters ---------- @@ -233,7 +233,7 @@ def property_map(plot): Returns ------- property_map : numpy.ndarray - a NumPy array with shape (vertical pixels, horizontal pixels, 2) of + A NumPy array with shape (vertical pixels, horizontal pixels, 2) of OpenMC property ids with dtype float """ From 2d144e21acebccb563db72d3e6021fbc38b53d86 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Sun, 17 Mar 2019 19:36:34 -0500 Subject: [PATCH 22/32] Updating/adding some docstrings. --- include/openmc/plot.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/include/openmc/plot.h b/include/openmc/plot.h index 28e6734ebb..08797c33ba 100644 --- a/include/openmc/plot.h +++ b/include/openmc/plot.h @@ -51,10 +51,10 @@ struct RGBColor { uint8_t red, green, blue; }; - typedef xt::xtensor ImageData; -struct IdData { +struct IdData +{ // Constructor IdData(int h_res, int v_res); @@ -62,10 +62,11 @@ struct IdData { void set_value(int y, int x, const Particle& p, int level); // Members - xt::xtensor data_; + xt::xtensor data_; //!< 2D array of cell & material ids }; -struct PropertyData { +struct PropertyData +{ // Constructor PropertyData(int h_res, int v_res); @@ -73,7 +74,7 @@ struct PropertyData { void set_value(int y, int x, const Particle& p, int level); // Members - xt::xtensor data_; + xt::xtensor data_; //!< 2D array of temperature & density data }; enum class PlotType { @@ -95,7 +96,9 @@ enum class PlotColorBy { //=============================================================================== // Plot class //=============================================================================== -class PlotBase { +class PlotBase +{ + // Methods public: IdData get_id_map() const; PropertyData get_property_map() const; From 44654e775923a78168847aac1b7ba0aaa02e4abd Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Mon, 18 Mar 2019 09:17:30 -0500 Subject: [PATCH 23/32] A couple capi.plot module fixes and an added test. --- openmc/capi/plot.py | 3 +-- tests/unit_tests/test_capi.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/openmc/capi/plot.py b/openmc/capi/plot.py index edb2e42a3d..7e429be6c8 100644 --- a/openmc/capi/plot.py +++ b/openmc/capi/plot.py @@ -1,7 +1,6 @@ from ctypes import c_int, c_int32, c_double, Structure, POINTER from . import _dll -from .core import _DLLGlobal from .error import _error_handler import numpy as np @@ -31,7 +30,7 @@ class _Position(Structure): elif idx == 2: return self.z else: - raise IndexError("{} index is invalid for _Position".format(key)) + raise IndexError("{} index is invalid for _Position".format(idx)) def __setitem__(self, idx, val): if idx == 0: diff --git a/tests/unit_tests/test_capi.py b/tests/unit_tests/test_capi.py index 14dac21fc8..dc3812fe0d 100644 --- a/tests/unit_tests/test_capi.py +++ b/tests/unit_tests/test_capi.py @@ -438,3 +438,26 @@ def test_property_map(capi_init): properties = openmc.capi.plot.property_map(s) assert np.allclose(expected_properties, properties, atol=1e-04) + + +def test_position(capi_init): + + pos = openmc.capi.plot._Position(1.0, 1.0, 1.0) + + assert pos[0] == 1.0 + assert pos[1] == 1.0 + assert pos[2] == 1.0 + + pos[0] = 1.3 + pos[1] = 1.3 + pos[2] = 1.3 + + assert pos[0] == 1.3 + assert pos[1] == 1.3 + assert pos[2] == 1.3 + + with pytest.raises(IndexError) as e: + pos[3] = 1.3 + + with pytest.raises(IndexError) as e: + pos[-1] = 1.3 From 2b1d2917f74e37907abe40a5d2b575be843a55d5 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Mon, 18 Mar 2019 09:48:21 -0500 Subject: [PATCH 24/32] Addressing some comments from @promano in the PR. --- include/openmc/plot.h | 6 ++---- openmc/capi/plot.py | 16 +++++++++------- src/plot.cpp | 12 ++++++------ 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/include/openmc/plot.h b/include/openmc/plot.h index 08797c33ba..f6f8c7ea70 100644 --- a/include/openmc/plot.h +++ b/include/openmc/plot.h @@ -53,8 +53,7 @@ struct RGBColor { typedef xt::xtensor ImageData; -struct IdData -{ +struct IdData { // Constructor IdData(int h_res, int v_res); @@ -65,8 +64,7 @@ struct IdData xt::xtensor data_; //!< 2D array of cell & material ids }; -struct PropertyData -{ +struct PropertyData { // Constructor PropertyData(int h_res, int v_res); diff --git a/openmc/capi/plot.py b/openmc/capi/plot.py index 7e429be6c8..f9873f4bd9 100644 --- a/openmc/capi/plot.py +++ b/openmc/capi/plot.py @@ -214,14 +214,18 @@ def id_map(plot): """ img_data = np.zeros((plot.v_res, plot.h_res, 2), dtype=np.dtype('int32')) - _dll.openmc_id_map(POINTER(_PlotBase)(plot), - img_data.ctypes.data_as(POINTER(c_int32))) + _dll.openmc_id_map(plot, img_data.ctypes.data_as(POINTER(c_int32))) return img_data +_dll.openmc_id_map.argtypes = [POINTER(_PlotBase), POINTER(c_double)] +_dll.openmc_id_map.restype = c_int +_dll.openmc_id_map.errcheck = _error_handler + + def property_map(plot): """ - Generate a 2-D map of (cell_temperature, material_density). Used for + Generate a 2-D map of cell temperature and material density. Used for in-memory image generation. Parameters @@ -236,8 +240,6 @@ def property_map(plot): OpenMC property ids with dtype float """ - prop_data = np.zeros((plot.v_res, plot.h_res, 2), - dtype=np.dtype('float')) - _dll.openmc_property_map(POINTER(_PlotBase)(plot), - prop_data.ctypes.data_as(POINTER(c_double))) + prop_data = np.zeros((plot.v_res, plot.h_res, 2)) + _dll.openmc_property_map(plot, prop_data.ctypes.data_as(POINTER(c_double))) return prop_data diff --git a/src/plot.cpp b/src/plot.cpp index edb50ed096..9b5f5c3bca 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -32,9 +32,9 @@ const RGBColor WHITE {255, 255, 255}; constexpr int PLOT_LEVEL_LOWEST {-1}; //!< lower bound on plot universe level constexpr int32_t NOT_FOUND {-2}; -IdData::IdData(int h_res, int v_res) { - data_ = xt::xtensor({v_res, h_res, 2}, NOT_FOUND); -} +IdData::IdData(int h_res, int v_res) + : data_({v_res, h_res, 2}, NOT_FOUND) +{ } void IdData::set_value(int y, int x, const Particle& p, int level) { @@ -49,9 +49,9 @@ IdData::set_value(int y, int x, const Particle& p, int level) { } } -PropertyData::PropertyData(int h_res, int v_res) { - data_ = xt::xtensor({v_res, h_res, 2}, NOT_FOUND); -} +PropertyData::PropertyData(int h_res, int v_res) + : data_({v_res, h_res, 2}, NOT_FOUND) +{ } void PropertyData::set_value(int y, int x, const Particle& p, int level) { From 805c336597822d8c5281c5c19824ef796d12aa30 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Mon, 18 Mar 2019 10:07:18 -0500 Subject: [PATCH 25/32] Updating template parameter, moving generate_dat def to header, renaming generate_data->gen_map and removing additional indirection. --- include/openmc/plot.h | 78 +++++++++++++++++++++++++++++++++++---- src/plot.cpp | 85 ++----------------------------------------- 2 files changed, 75 insertions(+), 88 deletions(-) diff --git a/include/openmc/plot.h b/include/openmc/plot.h index f6f8c7ea70..2e56c96587 100644 --- a/include/openmc/plot.h +++ b/include/openmc/plot.h @@ -10,6 +10,8 @@ #include "hdf5.h" #include "openmc/position.h" #include "openmc/constants.h" +#include "openmc/cell.h" +#include "openmc/geometry.h" #include "openmc/particle.h" #include "openmc/xml_interface.h" @@ -96,14 +98,8 @@ enum class PlotColorBy { //=============================================================================== class PlotBase { - // Methods public: - IdData get_id_map() const; - PropertyData get_property_map() const; - - private: - template - D generate_data() const; + template T get_map() const; // Members public: @@ -114,6 +110,74 @@ class PlotBase int level_; //!< Plot universe level }; +template +T PlotBase::get_map() const { + + size_t width = pixels_[0]; + size_t height = pixels_[1]; + + // get pixel size + double in_pixel = (width_[0])/static_cast(width); + double out_pixel = (width_[1])/static_cast(height); + + // size data array + T data(width, height); + + // setup basis indices and initial position centered on pixel + int in_i, out_i; + Position xyz = origin_; + switch(basis_) { + case PlotBasis::xy : + in_i = 0; + out_i = 1; + break; + case PlotBasis::xz : + in_i = 0; + out_i = 2; + break; + case PlotBasis::yz : + in_i = 1; + out_i = 2; + break; + } + + // set initial position + xyz[in_i] = origin_[in_i] - width_[0] / 2. + in_pixel / 2.; + xyz[out_i] = origin_[out_i] + width_[1] / 2. - out_pixel / 2.; + + // arbitrary direction + Direction dir = {0.5, 0.5, 0.5}; + + #pragma omp parallel + { + Particle p; + p.r() = xyz; + p.u() = dir; + p.coord_[0].universe = model::root_universe; + int level = level_; + int j{}; + + #pragma omp for + for (int y = 0; y < height; y++) { + p.r()[out_i] = xyz[out_i] - out_pixel * y; + for (int x = 0; x < width; x++) { + p.r()[in_i] = xyz[in_i] + in_pixel * x; + p.n_coord_ = 1; + // local variables + bool found_cell = find_cell(&p, 0); + j = p.n_coord_ - 1; + if (level >=0) {j = level + 1;} + if (found_cell) { + data.set_value(y, x, p, j); + Cell* c = model::cells[p.coord_[j].cell].get(); + } + } // inner for + } // outer for + } // omp parallel + + return data; +} + class Plot : public PlotBase { diff --git a/src/plot.cpp b/src/plot.cpp index 9b5f5c3bca..0d43d1b8d9 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -6,7 +6,6 @@ #include "xtensor/xview.hpp" -#include "openmc/cell.h" #include "openmc/constants.h" #include "openmc/file_utils.h" #include "openmc/geometry.h" @@ -137,7 +136,7 @@ void create_ppm(Plot pl) ImageData data({width, height}, pl.not_found_); // generate ids for the plot - auto ids = pl.get_id_map(); + auto ids = pl.get_map(); // assign colors for (int y = 0; y < height; y++) { @@ -639,82 +638,6 @@ Plot::Plot(pugi::xml_node plot_node) set_mask(plot_node); } // End Plot constructor -template -D PlotBase::generate_data() const { - - size_t width = pixels_[0]; - size_t height = pixels_[1]; - - // get pixel size - double in_pixel = (width_[0])/static_cast(width); - double out_pixel = (width_[1])/static_cast(height); - - // size data array - D data(width, height); - - // setup basis indices and initial position centered on pixel - int in_i, out_i; - Position xyz = origin_; - switch(basis_) { - case PlotBasis::xy : - in_i = 0; - out_i = 1; - break; - case PlotBasis::xz : - in_i = 0; - out_i = 2; - break; - case PlotBasis::yz : - in_i = 1; - out_i = 2; - break; - } - - // set initial position - xyz[in_i] = origin_[in_i] - width_[0] / 2. + in_pixel / 2.; - xyz[out_i] = origin_[out_i] + width_[1] / 2. - out_pixel / 2.; - - // arbitrary direction - Direction dir = {0.5, 0.5, 0.5}; - -#pragma omp parallel -{ - Particle p; - p.r() = xyz; - p.u() = dir; - p.coord_[0].universe = model::root_universe; - int level = level_; - int j{}; - - #pragma omp for - for (int y = 0; y < height; y++) { - p.r()[out_i] = xyz[out_i] - out_pixel * y; - for (int x = 0; x < width; x++) { - p.r()[in_i] = xyz[in_i] + in_pixel * x; - p.n_coord_ = 1; - // local variables - bool found_cell = find_cell(&p, 0); - j = p.n_coord_ - 1; - if (level >=0) {j = level + 1;} - if (found_cell) { - data.set_value(y, x, p, j); - Cell* c = model::cells[p.coord_[j].cell].get(); - } - } // inner for - } // outer for - } // omp parallel - - return data; -} - -IdData PlotBase::get_id_map() const { - return generate_data(); -} - -PropertyData PlotBase::get_property_map() const { - return generate_data(); -} - //============================================================================== // OUTPUT_PPM writes out a previously generated image to a PPM file //============================================================================== @@ -913,7 +836,7 @@ void create_voxel(Plot pl) pltbase.origin_.z = ll.z + z * vox[2]; // generate ids using plotbase - IdData ids = pltbase.get_id_map(); + IdData ids = pltbase.get_map(); // select only cell ID data and flip the y-axis xt::xtensor data1 = xt::flip(xt::view(ids.data_, xt::all(), xt::all(), 0), 0); @@ -976,7 +899,7 @@ extern "C" int openmc_id_map(const void* plot, int32_t* data_out) return OPENMC_E_INVALID_ARGUMENT; } - auto ids = plt->get_id_map(); + auto ids = plt->get_map(); // write id data to array std::copy(ids.data_.begin(), ids.data_.end(), data_out); @@ -992,7 +915,7 @@ extern "C" int openmc_property_map(const void* plot, double* data_out) { return OPENMC_E_INVALID_ARGUMENT; } - auto props = plt->get_property_map(); + auto props = plt->get_map(); // write id data to array std::copy(props.data_.begin(), props.data_.end(), data_out); From 378268bb37b6ec3a6aa49a0ca8a2ff0ba3c270cf Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Mon, 18 Mar 2019 10:09:28 -0500 Subject: [PATCH 26/32] Lassoing stars. --- include/openmc/capi.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/openmc/capi.h b/include/openmc/capi.h index 617153b6c6..8815d243ab 100644 --- a/include/openmc/capi.h +++ b/include/openmc/capi.h @@ -70,8 +70,8 @@ extern "C" { int openmc_next_batch(int* status); int openmc_nuclide_name(int index, const char** name); int openmc_plot_geometry(); - int openmc_id_map(const void* slice, int32_t *data_out); - int openmc_property_map(const void* slice, double *data_out); + int openmc_id_map(const void* slice, int32_t* data_out); + int openmc_property_map(const void* slice, double* data_out); int openmc_reset(); int openmc_run(); void openmc_set_seed(int64_t new_seed); From 190c8aa71241de2ee48d5f1feb7514590198a087 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Mon, 18 Mar 2019 10:30:45 -0500 Subject: [PATCH 27/32] Fixing dll definitions for openmc_property_map. --- openmc/capi/plot.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/openmc/capi/plot.py b/openmc/capi/plot.py index f9873f4bd9..44c7ad3830 100644 --- a/openmc/capi/plot.py +++ b/openmc/capi/plot.py @@ -218,9 +218,9 @@ def id_map(plot): return img_data -_dll.openmc_id_map.argtypes = [POINTER(_PlotBase), POINTER(c_double)] -_dll.openmc_id_map.restype = c_int -_dll.openmc_id_map.errcheck = _error_handler +_dll.openmc_property_map.argtypes = [POINTER(_PlotBase), POINTER(c_double)] +_dll.openmc_property_map.restype = c_int +_dll.openmc_property_map.errcheck = _error_handler def property_map(plot): From 656df47365fca097daf88cb6237cd3d8bd1a7cc7 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Mon, 18 Mar 2019 14:16:56 -0500 Subject: [PATCH 28/32] Addressing a couple style issues. --- include/openmc/plot.h | 10 ++++------ openmc/capi/plot.py | 4 ++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/include/openmc/plot.h b/include/openmc/plot.h index 2e56c96587..91ac9ec207 100644 --- a/include/openmc/plot.h +++ b/include/openmc/plot.h @@ -96,13 +96,12 @@ enum class PlotColorBy { //=============================================================================== // Plot class //=============================================================================== -class PlotBase -{ - public: +class PlotBase { +public: template T get_map() const; // Members - public: +public: Position origin_; //!< Plot origin in geometry Position width_; //!< Plot width in geometry PlotBasis basis_; //!< Plot basis (XY/XZ/YZ) @@ -178,8 +177,7 @@ T PlotBase::get_map() const { return data; } -class Plot : public PlotBase -{ +class Plot : public PlotBase { public: // Constructor diff --git a/openmc/capi/plot.py b/openmc/capi/plot.py index 44c7ad3830..766ff95dcc 100644 --- a/openmc/capi/plot.py +++ b/openmc/capi/plot.py @@ -197,7 +197,7 @@ _dll.openmc_id_map.errcheck = _error_handler def id_map(plot): """ - Generate a 2-D map of (cell_id, material_id). Used for in-memory image + Generate a 2-D map of cell and material IDs. Used for in-memory image generation. Parameters @@ -225,7 +225,7 @@ _dll.openmc_property_map.errcheck = _error_handler def property_map(plot): """ - Generate a 2-D map of cell temperature and material density. Used for + Generate a 2-D map of cell temperatures and material densities. Used for in-memory image generation. Parameters From 2e2ec4a4121b1fa12754b9f3393b804710161597 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Mon, 18 Mar 2019 14:47:44 -0500 Subject: [PATCH 29/32] Addressing narrowing warning. --- include/openmc/plot.h | 6 +++--- src/plot.cpp | 8 +++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/include/openmc/plot.h b/include/openmc/plot.h index 91ac9ec207..12f5cfab9f 100644 --- a/include/openmc/plot.h +++ b/include/openmc/plot.h @@ -57,7 +57,7 @@ typedef xt::xtensor ImageData; struct IdData { // Constructor - IdData(int h_res, int v_res); + IdData(size_t h_res, size_t v_res); // Methods void set_value(int y, int x, const Particle& p, int level); @@ -68,7 +68,7 @@ struct IdData { struct PropertyData { // Constructor - PropertyData(int h_res, int v_res); + PropertyData(size_t h_res, size_t v_res); // Methods void set_value(int y, int x, const Particle& p, int level); @@ -105,7 +105,7 @@ public: Position origin_; //!< Plot origin in geometry Position width_; //!< Plot width in geometry PlotBasis basis_; //!< Plot basis (XY/XZ/YZ) - std::array pixels_; //!< Plot size in pixels + std::array pixels_; //!< Plot size in pixels int level_; //!< Plot universe level }; diff --git a/src/plot.cpp b/src/plot.cpp index 0d43d1b8d9..ba769d611b 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -31,7 +31,7 @@ const RGBColor WHITE {255, 255, 255}; constexpr int PLOT_LEVEL_LOWEST {-1}; //!< lower bound on plot universe level constexpr int32_t NOT_FOUND {-2}; -IdData::IdData(int h_res, int v_res) +IdData::IdData(size_t h_res, size_t v_res) : data_({v_res, h_res, 2}, NOT_FOUND) { } @@ -48,7 +48,7 @@ IdData::set_value(int y, int x, const Particle& p, int level) { } } -PropertyData::PropertyData(int h_res, int v_res) +PropertyData::PropertyData(size_t h_res, size_t v_res) : data_({v_res, h_res, 2}, NOT_FOUND) { } @@ -807,7 +807,9 @@ void create_voxel(Plot pl) // Write current date and time write_attribute(file_id, "date_and_time", time_stamp().c_str()); hsize_t three = 3; - write_attribute(file_id, "num_voxels", pl.pixels_); + std::array pixels; + std::copy(pl.pixels_.begin(), pl.pixels_.end(), pixels.begin()); + write_attribute(file_id, "num_voxels", pixels); write_attribute(file_id, "voxel_width", vox); write_attribute(file_id, "lower_left", ll); From f6192dfc9e2037a2621f5bffc5f00b92f1020256 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Mon, 18 Mar 2019 15:14:47 -0500 Subject: [PATCH 30/32] Updates to mirror changes in pixels_ type in plot.cpp. --- openmc/capi/plot.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/openmc/capi/plot.py b/openmc/capi/plot.py index 766ff95dcc..9ff398f5c1 100644 --- a/openmc/capi/plot.py +++ b/openmc/capi/plot.py @@ -1,4 +1,4 @@ -from ctypes import c_int, c_int32, c_double, Structure, POINTER +from ctypes import c_int, c_size_t, c_int32, c_double, Structure, POINTER from . import _dll from .error import _error_handler @@ -57,7 +57,7 @@ class _PlotBase(Structure): The width of the plot along the x, y, and z axes, respectively basis_ : c_int The axes basis of the plot view. - pixels_ : c_int[3] + pixels_ : c_size_t[3] The resolution of the plot in the horizontal and vertical dimensions level_ : c_int The universe level for the plot view @@ -73,9 +73,9 @@ class _PlotBase(Structure): basis : string One of {'xy', 'xz', 'yz'} indicating the horizontal and vertical axes of the plot. - h_res : float + h_res : int The horizontal resolution of the plot in pixels - v_res : float + v_res : int The vertical resolution of the plot in pixels level : int The universe level for the plot (default: -1 -> all universes shown) @@ -83,7 +83,7 @@ class _PlotBase(Structure): _fields_ = [('origin_', _Position), ('width_', _Position), ('basis_', c_int), - ('pixels_', 3*c_int), + ('pixels_', 3*c_size_t), ('level_', c_int)] def __init__(self): From abd417c5f6835a28d11bd9950f112f3a854d052d Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Mon, 18 Mar 2019 15:38:25 -0500 Subject: [PATCH 31/32] Updating other signatures to match new pixels_ type. --- include/openmc/plot.h | 4 ++-- src/plot.cpp | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/openmc/plot.h b/include/openmc/plot.h index 12f5cfab9f..73bcd1d361 100644 --- a/include/openmc/plot.h +++ b/include/openmc/plot.h @@ -60,7 +60,7 @@ struct IdData { IdData(size_t h_res, size_t v_res); // Methods - void set_value(int y, int x, const Particle& p, int level); + void set_value(size_t y, size_t x, const Particle& p, int level); // Members xt::xtensor data_; //!< 2D array of cell & material ids @@ -71,7 +71,7 @@ struct PropertyData { PropertyData(size_t h_res, size_t v_res); // Methods - void set_value(int y, int x, const Particle& p, int level); + void set_value(size_t y, size_t x, const Particle& p, int level); // Members xt::xtensor data_; //!< 2D array of temperature & density data diff --git a/src/plot.cpp b/src/plot.cpp index ba769d611b..02fb42d76b 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -36,7 +36,7 @@ IdData::IdData(size_t h_res, size_t v_res) { } void -IdData::set_value(int y, int x, const Particle& p, int level) { +IdData::set_value(size_t y, size_t x, const Particle& p, int level) { Cell* c = model::cells[p.coord_[level].cell].get(); data_(y,x,0) = c->id_; if (p.material_ == MATERIAL_VOID) { @@ -53,7 +53,7 @@ PropertyData::PropertyData(size_t h_res, size_t v_res) { } void -PropertyData::set_value(int y, int x, const Particle& p, int level) { +PropertyData::set_value(size_t y, size_t x, const Particle& p, int level) { Cell* c = model::cells[p.coord_[level].cell].get(); data_(y,x,0) = (p.sqrtkT_ * p.sqrtkT_) / K_BOLTZMANN; if (c->type_ != FILL_UNIVERSE && p.material_ != MATERIAL_VOID) { @@ -139,8 +139,8 @@ void create_ppm(Plot pl) auto ids = pl.get_map(); // assign colors - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { + for (size_t y = 0; y < height; y++) { + for (size_t x = 0; x < width; x++) { auto id = ids.data_(y, x, pl.color_by_); // no setting needed if not found if (id == NOT_FOUND) { continue; } From 43914d42049724f0841ff5818fda005f475a1ecd Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Tue, 19 Mar 2019 08:41:11 -0500 Subject: [PATCH 32/32] Re-correcting direction and updating _Position tests. --- include/openmc/plot.h | 2 +- tests/unit_tests/test_capi.py | 20 +++++--------------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/include/openmc/plot.h b/include/openmc/plot.h index 73bcd1d361..2752add9cf 100644 --- a/include/openmc/plot.h +++ b/include/openmc/plot.h @@ -145,7 +145,7 @@ T PlotBase::get_map() const { xyz[out_i] = origin_[out_i] + width_[1] / 2. - out_pixel / 2.; // arbitrary direction - Direction dir = {0.5, 0.5, 0.5}; + Direction dir = {0.7071, 0.7071, 0.0}; #pragma omp parallel { diff --git a/tests/unit_tests/test_capi.py b/tests/unit_tests/test_capi.py index dc3812fe0d..be88072d9f 100644 --- a/tests/unit_tests/test_capi.py +++ b/tests/unit_tests/test_capi.py @@ -442,22 +442,12 @@ def test_property_map(capi_init): def test_position(capi_init): - pos = openmc.capi.plot._Position(1.0, 1.0, 1.0) + pos = openmc.capi.plot._Position(1.0, 2.0, 3.0) - assert pos[0] == 1.0 - assert pos[1] == 1.0 - assert pos[2] == 1.0 + assert tuple(pos) == (1.0, 2.0, 3.0) pos[0] = 1.3 - pos[1] = 1.3 - pos[2] = 1.3 + pos[1] = 2.3 + pos[2] = 3.3 - assert pos[0] == 1.3 - assert pos[1] == 1.3 - assert pos[2] == 1.3 - - with pytest.raises(IndexError) as e: - pos[3] = 1.3 - - with pytest.raises(IndexError) as e: - pos[-1] = 1.3 + assert tuple(pos) == (1.3, 2.3, 3.3)