From 3b5c8495e122a8b84e37faf4fc6fa648dad3fd1d Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Sat, 3 Feb 2018 21:19:46 -0500 Subject: [PATCH] Move Cell % universe to C++ --- src/cell.cpp | 42 +++++++++++++++--------------------- src/cell.h | 8 +++++++ src/geometry.F90 | 2 +- src/geometry_header.F90 | 48 +++++++++++++++++++++++++++++++++++++++-- src/hdf5_interface.h | 30 ++++++++++++++++++++++++++ src/input_xml.F90 | 18 ++++------------ src/summary.F90 | 5 ++--- 7 files changed, 108 insertions(+), 45 deletions(-) diff --git a/src/cell.cpp b/src/cell.cpp index 21713471a..2b2943fc4 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -1,6 +1,7 @@ #include "cell.h" #include +#include #include "error.h" #include "hdf5_interface.h" @@ -28,40 +29,31 @@ Cell::Cell(pugi::xml_node cell_node) if (check_for_node(cell_node, "name")) { name = get_node_value(cell_node, "name"); } + + if (check_for_node(cell_node, "universe")) { + universe = stoi(get_node_value(cell_node, "universe")); + } else { + universe = 0; + } } void -Cell::to_hdf5(hid_t group_id) const +//Cell::to_hdf5(hid_t group_id) const +Cell::to_hdf5(hid_t cell_group) const { -/* - std::string group_name {"surface "}; - group_name += std::to_string(id); - - hid_t surf_group = create_group(group_id, group_name); - - switch(bc) { - case BC_TRANSMIT : - write_string(surf_group, "boundary_type", "transmission"); - break; - case BC_VACUUM : - write_string(surf_group, "boundary_type", "vacuum"); - break; - case BC_REFLECT : - write_string(surf_group, "boundary_type", "reflective"); - break; - case BC_PERIODIC : - write_string(surf_group, "boundary_type", "periodic"); - break; - } +// std::string group_name {"surface "}; +// group_name += std::to_string(id); +// +// hid_t surf_group = create_group(group_id, group_name); if (!name.empty()) { - write_string(surf_group, "name", name); + write_string(cell_group, "name", name); } - to_hdf5_inner(surf_group); + //TODO: Lookup universe id in universe_dict + //write_int(cell_group, "universe", universe); - close_group(surf_group); -*/ +// close_group(cell_group); } //============================================================================== diff --git a/src/cell.h b/src/cell.h index 90ca923e3..7aa2d4bb3 100644 --- a/src/cell.h +++ b/src/cell.h @@ -38,6 +38,7 @@ class Cell public: int32_t id; //!< Unique ID std::string name{""}; //!< User-defined name + int32_t universe; //!< Universe # this cell is in explicit Cell(pugi::xml_node cell_node); @@ -58,6 +59,13 @@ extern "C" int32_t cell_id(Cell *c) {return c->id;} extern "C" void cell_set_id(Cell *c, int32_t id) {c->id = id;} +extern "C" int32_t cell_universe(Cell *c) {return c->universe;} + +extern "C" void cell_set_universe(Cell *c, int32_t universe) +{c->universe = universe;} + +extern "C" void cell_to_hdf5(Cell *c, hid_t group) {c->to_hdf5(group);} + //extern "C" void free_memory_cells_c() //{ // delete cells_c; diff --git a/src/geometry.F90 b/src/geometry.F90 index 630c57c04..cdf286c74 100644 --- a/src/geometry.F90 +++ b/src/geometry.F90 @@ -223,7 +223,7 @@ contains if (use_search_cells) then i_cell = search_cells(i) ! check to make sure search cell is in same universe - if (cells(i_cell) % universe /= i_universe) cycle + if (cells(i_cell) % universe() /= i_universe) cycle else i_cell = universes(i_universe) % cells(i) end if diff --git a/src/geometry_header.F90 b/src/geometry_header.F90 index b938dddec..8c900430f 100644 --- a/src/geometry_header.F90 +++ b/src/geometry_header.F90 @@ -1,6 +1,7 @@ module geometry_header use, intrinsic :: ISO_C_BINDING + use hdf5 use algorithm, only: find use constants, only: HALF, TWO, THREE, INFINITY, K_BOLTZMANN, & @@ -35,6 +36,30 @@ module geometry_header type(C_PTR), intent(in), value :: cell_ptr integer(C_INT32_T), intent(in), value :: id end subroutine cell_set_id_c + + function cell_universe_c(cell_ptr) bind(C, name='cell_universe') & + result(universe) + use ISO_C_BINDING + implicit none + type(C_PTR), intent(in), value :: cell_ptr + integer(C_INT32_T) :: universe + end function cell_universe_c + + subroutine cell_set_universe_c(cell_ptr, universe) & + bind(C, name='cell_set_universe') + use ISO_C_BINDING + implicit none + type(C_PTR), intent(in), value :: cell_ptr + integer(C_INT32_T), intent(in), value :: universe + end subroutine cell_set_universe_c + + subroutine cell_to_hdf5_c(cell_ptr, group) bind(C, name='cell_to_hdf5') + use ISO_C_BINDING + use hdf5 + implicit none + type(C_PTR), intent(in), value :: cell_ptr + integer(HID_T), intent(in), value :: group + end subroutine cell_to_hdf5_c end interface !=============================================================================== @@ -150,10 +175,8 @@ module geometry_header type Cell type(C_PTR) :: ptr - character(len=104) :: name = "" ! User-defined name integer :: type ! Type of cell (normal, universe, ! lattice) - integer :: universe ! universe # this cell is in integer :: fill ! universe # filling this cell integer :: instances ! number of instances of this cell in ! the geom @@ -183,6 +206,9 @@ module geometry_header procedure :: id => cell_id procedure :: set_id => cell_set_id + procedure :: universe => cell_universe + procedure :: set_universe => cell_set_universe + procedure :: to_hdf5 => cell_to_hdf5 end type Cell @@ -385,6 +411,24 @@ contains call cell_set_id_c(this % ptr, id) end subroutine cell_set_id + function cell_universe(this) result(universe) + class(Cell), intent(in) :: this + integer(C_INT32_T) :: universe + universe = cell_universe_c(this % ptr) + end function cell_universe + + subroutine cell_set_universe(this, universe) + class(Cell), intent(in) :: this + integer(C_INT32_T), intent(in) :: universe + call cell_set_universe_c(this % ptr, universe) + end subroutine cell_set_universe + + subroutine cell_to_hdf5(this, group) + class(Cell), intent(in) :: this + integer(HID_T), intent(in) :: group + call cell_to_hdf5_c(this % ptr, group) + end subroutine cell_to_hdf5 + !=============================================================================== ! GET_TEMPERATURES returns a list of temperatures that each nuclide/S(a,b) table ! appears at in the model. Later, this list is used to determine the actual diff --git a/src/hdf5_interface.h b/src/hdf5_interface.h index 89dce7ab2..f5a302855 100644 --- a/src/hdf5_interface.h +++ b/src/hdf5_interface.h @@ -43,6 +43,36 @@ close_group(hid_t group_id) } +inline void +write_int(hid_t group_id, char const *name, int32_t buffer) +{ + hid_t dataspace = H5Screate(H5S_SCALAR); + + hid_t dataset = H5Dcreate(group_id, name, H5T_NATIVE_INT32, dataspace, + H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); + + H5Dwrite(dataset, H5T_NATIVE_INT32, H5S_ALL, H5S_ALL, H5P_DEFAULT, &buffer); + + H5Sclose(dataspace); + H5Dclose(dataset); +} + + +//inline void +//write_double(hid_t group_id, char const *name, double buffer) +//{ +// hid_t dataspace = H5Screate(H5S_SCALAR); +// +// hid_t dataset = H5Dcreate(group_id, name, H5T_NATIVE_DOUBLE, dataspace, +// H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); +// +// H5Dwrite(dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, &buffer); +// +// H5Sclose(dataspace); +// H5Dclose(dataset); +//} + + template inline void write_double_1D(hid_t group_id, char const *name, std::array &buffer) diff --git a/src/input_xml.F90 b/src/input_xml.F90 index a9d687400..7679fde2d 100644 --- a/src/input_xml.F90 +++ b/src/input_xml.F90 @@ -1029,16 +1029,6 @@ contains ! Get pointer to i-th cell node node_cell = node_cell_list(i) - ! Copy cell name - if (check_for_node(node_cell, "name")) then - call get_node_value(node_cell, "name", c % name) - end if - - if (check_for_node(node_cell, "universe")) then - call get_node_value(node_cell, "universe", c % universe) - else - c % universe = 0 - end if if (check_for_node(node_cell, "fill")) then call get_node_value(node_cell, "fill", c % fill) if (find(fill_univ_ids, c % fill) == -1) & @@ -1249,7 +1239,7 @@ contains ! For cells, we also need to check if there's a new universe -- ! also for every cell add 1 to the count of cells for the ! specified universe - univ_id = c % universe + univ_id = c % universe() if (.not. cells_in_univ_dict % has(univ_id)) then n_universes = n_universes + 1 n_cells_in_univ = 1 @@ -1623,7 +1613,7 @@ contains do i = 1, n_cells ! Get index in universes array - j = universe_dict % get(cells(i) % universe) + j = universe_dict % get(cells(i) % universe()) ! Set the first zero entry in the universe cells array to the index in the ! global cells array @@ -4406,9 +4396,9 @@ contains ! ADJUST UNIVERSE INDEX FOR EACH CELL associate (c => cells(i)) - id = c % universe + id = c % universe() if (universe_dict % has(id)) then - c % universe = universe_dict % get(id) + call c % set_universe(universe_dict % get(id)) else call fatal_error("Could not find universe " // trim(to_str(id)) & &// " specified on cell " // trim(to_str(c % id()))) diff --git a/src/summary.F90 b/src/summary.F90 index d21b192a3..3aa851424 100644 --- a/src/summary.F90 +++ b/src/summary.F90 @@ -147,11 +147,10 @@ contains c => cells(i) cell_group = create_group(cells_group, "cell " // trim(to_str(c%id()))) - ! Write name for this cell - call write_dataset(cell_group, "name", c%name) + call c % to_hdf5(cell_group) ! Write universe for this cell - call write_dataset(cell_group, "universe", universes(c%universe)%id) + call write_dataset(cell_group, "universe", universes(c%universe())%id) ! Write information on what fills this cell select case (c%type)