From 2ce911977a40592a855e0080acc5128dbae07772 Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Thu, 11 Oct 2018 20:37:07 -0400 Subject: [PATCH] Finish moving CellFilter implementation to C++ --- include/openmc/tallies/tally_filter_cell.h | 4 +- src/tallies/tally_filter.cpp | 7 +++ src/tallies/tally_filter_cell.F90 | 69 ++++------------------ 3 files changed, 20 insertions(+), 60 deletions(-) diff --git a/include/openmc/tallies/tally_filter_cell.h b/include/openmc/tallies/tally_filter_cell.h index 0d56752c7..972fafddb 100644 --- a/include/openmc/tallies/tally_filter_cell.h +++ b/include/openmc/tallies/tally_filter_cell.h @@ -22,6 +22,7 @@ public: from_xml(pugi::xml_node node) override { cells_ = get_node_array(node, "bins"); + n_bins_ = cells_.size(); } virtual void @@ -61,7 +62,7 @@ public: to_statepoint(hid_t filter_group) const override { write_dataset(filter_group, "type", "cell"); - write_dataset(filter_group, "n_bins", cells_.size()); + write_dataset(filter_group, "n_bins", n_bins_); std::vector cell_ids; for (auto c : cells_) cell_ids.push_back(cells[c]->id_); write_dataset(filter_group, "bins", cell_ids); @@ -73,7 +74,6 @@ public: return "Cell " + std::to_string(cells[cells_[bin-1]]->id_); } -protected: std::vector cells_; std::unordered_map map_; }; diff --git a/src/tallies/tally_filter.cpp b/src/tallies/tally_filter.cpp index 49f5b97d1..7df15e02f 100644 --- a/src/tallies/tally_filter.cpp +++ b/src/tallies/tally_filter.cpp @@ -118,6 +118,13 @@ extern "C" { int filter_n_bins(TallyFilter* filt) {return filt->n_bins_;} + void + cell_filter_get_bins(CellFilter* filt, int32_t** cells, int32_t* n) + { + *cells = filt->cells_.data(); + *n = filt->cells_.size(); + } + int mesh_filter_get_mesh(MeshFilter* filt) {return filt->mesh_;} void diff --git a/src/tallies/tally_filter_cell.F90 b/src/tallies/tally_filter_cell.F90 index e02fef583..f4201ffb7 100644 --- a/src/tallies/tally_filter_cell.F90 +++ b/src/tallies/tally_filter_cell.F90 @@ -2,15 +2,8 @@ module tally_filter_cell use, intrinsic :: ISO_C_BINDING - use constants, only: ONE, MAX_LINE_LEN - use dict_header, only: EMPTY - use error, only: fatal_error - use hdf5_interface - use geometry_header - use particle_header, only: Particle - use string, only: to_str + use error use tally_filter_header - use xml_interface implicit none private @@ -21,58 +14,10 @@ module tally_filter_cell !=============================================================================== type, public, extends(CppTallyFilter) :: CellFilter - integer(C_INT32_T), allocatable :: cells(:) - type(DictIntInt) :: map - contains - procedure :: from_xml - procedure :: initialize => initialize_cell end type CellFilter contains - subroutine from_xml(this, node) - class(CellFilter), intent(inout) :: this - type(XMLNode), intent(in) :: node - - integer :: n - - call this % from_xml_cpp_inner(node) - - ! Determine how many bins were given - n = node_word_count(node, "bins") - - ! Allocate and store bins - this % n_bins = n - allocate(this % cells(n)) - call get_node_array(node, "bins", this % cells) - end subroutine from_xml - - subroutine initialize_cell(this) - class(CellFilter), intent(inout) :: this - - integer :: i, id - integer :: val - - call this % initialize_cpp_inner() - - ! Convert ids to indices. - do i = 1, this % n_bins - id = this % cells(i) - val = cell_dict % get(id) - if (val /= EMPTY) then - this % cells(i) = val - else - call fatal_error("Could not find cell " // trim(to_str(id)) & - &// " specified on tally filter.") - end if - end do - - ! Generate mapping from cell indices to filter bins. - do i = 1, this % n_bins - call this % map % set(this % cells(i), i) - end do - end subroutine initialize_cell - !=============================================================================== ! C API FUNCTIONS !=============================================================================== @@ -84,12 +29,20 @@ contains integer(C_INT32_T), intent(out) :: n integer(C_INT) :: err + interface + subroutine cell_filter_get_bins(filt, cells, n) bind(C) + import C_PTR, C_INT + type(C_PTR), intent(in), value :: filt + type(C_PTR), intent(out) :: cells + integer(C_INT), intent(out) :: n + end subroutine cell_filter_get_bins + end interface + err = verify_filter(index) if (err == 0) then select type (f => filters(index) % obj) type is (CellFilter) - cells = C_LOC(f % cells) - n = size(f % cells) + call cell_filter_get_bins(f % ptr, cells, n) class default err = E_INVALID_TYPE call set_errmsg("Tried to get cells from a non-cell filter.")