diff --git a/include/openmc/tallies/filter_cell_instance.h b/include/openmc/tallies/filter_cell_instance.h index a2a204db6..a3fbad181 100644 --- a/include/openmc/tallies/filter_cell_instance.h +++ b/include/openmc/tallies/filter_cell_instance.h @@ -59,7 +59,7 @@ private: std::unordered_map map_; //! Level in the geometry to check for the cell instance - int32_t geom_level_ {-1}; + int32_t geom_level_ {C_NONE}; }; } // namespace openmc diff --git a/src/geometry.cpp b/src/geometry.cpp index 563e5467b..7eb731660 100644 --- a/src/geometry.cpp +++ b/src/geometry.cpp @@ -67,8 +67,8 @@ bool check_cell_overlap(Particle& p, bool error) int cell_instance_at_level(const Particle& p, int level) { // throw error if the requested level is too deep for the geometry - if (level > p.n_coord()) { - fatal_error(fmt::format("Cell instance at level {} requested, but only {} levels exist in the model.", level, p.n_coord())); + if (level > model::n_coord_levels) { + fatal_error(fmt::format("Cell instance at level {} requested, but only {} levels exist in the geometry.", level, p.n_coord())); } // determine the cell instance diff --git a/src/geometry_aux.cpp b/src/geometry_aux.cpp index af6ebac9f..9d3062901 100644 --- a/src/geometry_aux.cpp +++ b/src/geometry_aux.cpp @@ -319,9 +319,13 @@ prepare_distribcell() std::unordered_set distribcells; for (auto& filt : model::tally_filters) { auto* distrib_filt = dynamic_cast(filt.get()); + auto* cell_inst_filt = dynamic_cast(filt.get()); if (distrib_filt) { distribcells.insert(distrib_filt->cell()); } + if (cell_inst_filt) { + for (const auto& c_inst : cell_inst_filt->cell_instances()) distribcells.insert(c_inst.index_cell); + } } // By default, add material cells to the list of distributed cells diff --git a/src/tallies/filter_cell_instance.cpp b/src/tallies/filter_cell_instance.cpp index c5a88dc7a..ba70b06bb 100644 --- a/src/tallies/filter_cell_instance.cpp +++ b/src/tallies/filter_cell_instance.cpp @@ -56,8 +56,8 @@ CellInstanceFilter::set_cell_instances(gsl::span instances) const auto& c {model::cells[x.index_cell]}; if (c->type_ != Fill::MATERIAL && geom_level_ < 0) { throw std::invalid_argument{fmt::format( - "Cell {} is not filled with a material. Only material cells can be " - "used in a cell instance filter.", c->id_)}; + "Cell {} is not filled with a material. A geometry level must be specified to" + "use cells filled with a universe or lattice.", c->id_)}; } cell_instances_.push_back(x); map_[x] = cell_instances_.size() - 1; @@ -82,7 +82,7 @@ CellInstanceFilter::get_all_bins(const Particle& p, TallyEstimator estimator, if (geom_level_ >= 0) { // if the particle has fewer levels than the cell we're looking for, // return no bins - if (p.coord(geom_level_).cell == C_NONE) return; + if (p.n_coord() - 1 < geom_level_ || p.coord(geom_level_).cell == C_NONE) return; // otherwise use the cell at the requested level // and compute the cell instance for this particle's position