#include "openmc/geometry_aux.h" #include // for std::max #include #include #include #include #include "openmc/cell.h" #include "openmc/constants.h" #include "openmc/container_util.h" #include "openmc/dagmc.h" #include "openmc/error.h" #include "openmc/file_utils.h" #include "openmc/geometry.h" #include "openmc/lattice.h" #include "openmc/material.h" #include "openmc/settings.h" #include "openmc/simulation.h" #include "openmc/surface.h" #include "openmc/tallies/filter.h" #include "openmc/tallies/filter_cell_instance.h" #include "openmc/tallies/filter_distribcell.h" namespace openmc { namespace model { std::unordered_map universe_level_counts; } // namespace model void read_geometry_xml() { // Display output message write_message("Reading geometry XML file...", 5); // Check if geometry.xml exists std::string filename = settings::path_input + "geometry.xml"; if (!file_exists(filename)) { fatal_error("Geometry XML file '" + filename + "' does not exist!"); } // Parse settings.xml file pugi::xml_document doc; auto result = doc.load_file(filename.c_str()); if (!result) { fatal_error("Error processing geometry.xml file."); } // Get root element pugi::xml_node root = doc.document_element(); read_geometry_xml(root); } void read_geometry_xml(pugi::xml_node root) { // Read surfaces, cells, lattice std::set> periodic_pairs; std::unordered_map albedo_map; std::unordered_map periodic_sense_map; read_surfaces(root, periodic_pairs, albedo_map, periodic_sense_map); read_cells(root); prepare_boundary_conditions(periodic_pairs, albedo_map, periodic_sense_map); read_lattices(root); // Check to make sure a boundary condition was applied to at least one // surface bool boundary_exists = false; for (const auto& surf : model::surfaces) { if (surf->bc_) { boundary_exists = true; break; } } if (settings::run_mode != RunMode::PLOTTING && settings::run_mode != RunMode::VOLUME && !boundary_exists) { fatal_error("No boundary conditions were applied to any surfaces!"); } // Allocate universes, universe cell arrays, and assign base universe model::root_universe = find_root_universe(); // if the root universe is DAGMC geometry, make sure the model is well-formed check_dagmc_root_univ(); } //============================================================================== void adjust_indices() { // Adjust material/fill idices. for (auto& c : model::cells) { if (c->fill_ != C_NONE) { int32_t id = c->fill_; auto search_univ = model::universe_map.find(id); auto search_lat = model::lattice_map.find(id); if (search_univ != model::universe_map.end()) { c->type_ = Fill::UNIVERSE; c->fill_ = search_univ->second; } else if (search_lat != model::lattice_map.end()) { c->type_ = Fill::LATTICE; c->fill_ = search_lat->second; } else { fatal_error(fmt::format("Specified fill {} on cell {} is neither a " "universe nor a lattice.", id, c->id_)); } } else { c->type_ = Fill::MATERIAL; for (auto& mat_id : c->material_) { if (mat_id != MATERIAL_VOID) { auto search = model::material_map.find(mat_id); if (search == model::material_map.end()) { fatal_error( fmt::format("Could not find material {} specified on cell {}", mat_id, c->id_)); } // Change from ID to index mat_id = search->second; } } } } // Change cell.universe values from IDs to indices. for (auto& c : model::cells) { auto search = model::universe_map.find(c->universe_); if (search != model::universe_map.end()) { c->universe_ = search->second; } else { fatal_error(fmt::format("Could not find universe {} specified on cell {}", c->universe_, c->id_)); } } // Change all lattice universe values from IDs to indices. for (auto& l : model::lattices) { l->adjust_indices(); } } //============================================================================== //! Partition some universes with many z-planes for faster find_cell searches. void partition_universes() { // Iterate over universes with more than 10 cells. (Fewer than 10 is likely // not worth partitioning.) for (const auto& univ : model::universes) { if (univ->cells_.size() > 10) { // Collect the set of surfaces in this universe. std::unordered_set surf_inds; for (auto i_cell : univ->cells_) { for (auto token : model::cells[i_cell]->surfaces()) { surf_inds.insert(std::abs(token) - 1); } } // Partition the universe if there are more than 5 z-planes. (Fewer than // 5 is likely not worth it.) int n_zplanes = 0; for (auto i_surf : surf_inds) { if (dynamic_cast(model::surfaces[i_surf].get())) { ++n_zplanes; if (n_zplanes > 5) { univ->partitioner_ = make_unique(*univ); break; } } } } } } //============================================================================== void assign_temperatures() { for (auto& c : model::cells) { // Ignore non-material cells and cells with defined temperature. if (c->material_.size() == 0) continue; if (c->sqrtkT_.size() > 0) continue; c->sqrtkT_.reserve(c->material_.size()); for (auto i_mat : c->material_) { if (i_mat == MATERIAL_VOID) { // Set void region to 0K. c->sqrtkT_.push_back(0); } else { const auto& mat {model::materials[i_mat]}; c->sqrtkT_.push_back(std::sqrt(K_BOLTZMANN * mat->temperature())); } } } } //============================================================================== void finalize_cell_densities() { for (auto& c : model::cells) { // Convert to density multipliers. if (!c->density_mult_.empty()) { for (int32_t instance = 0; instance < c->density_mult_.size(); ++instance) { c->density_mult_[instance] /= model::materials[c->material(instance)]->density_gpcc(); } } else { c->density_mult_ = {1.0}; } } } //============================================================================== void get_temperatures( vector>& nuc_temps, vector>& thermal_temps) { for (const auto& cell : model::cells) { // Skip non-material cells. if (cell->fill_ != C_NONE) continue; for (int j = 0; j < cell->material_.size(); ++j) { // Skip void materials int i_material = cell->material_[j]; if (i_material == MATERIAL_VOID) continue; // Get temperature(s) of cell (rounding to nearest integer) vector cell_temps; if (cell->sqrtkT_.size() == 1) { double sqrtkT = cell->sqrtkT_[0]; cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN); } else if (cell->sqrtkT_.size() == cell->material_.size()) { double sqrtkT = cell->sqrtkT_[j]; cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN); } else { for (double sqrtkT : cell->sqrtkT_) cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN); } const auto& mat {model::materials[i_material]}; for (const auto& i_nuc : mat->nuclide_) { for (double temperature : cell_temps) { // Add temperature if it hasn't already been added if (!contains(nuc_temps[i_nuc], temperature)) nuc_temps[i_nuc].push_back(temperature); } } for (const auto& table : mat->thermal_tables_) { // Get index in data::thermal_scatt array int i_sab = table.index_table; for (double temperature : cell_temps) { // Add temperature if it hasn't already been added if (!contains(thermal_temps[i_sab], temperature)) thermal_temps[i_sab].push_back(temperature); } } } } // Add temperatures from a temperature field. // We assume that we do not know how geometric cells are impacted by the // temperature field in advance. If we had access to this information, we // could limit the declarations of temperature from the temperature field to // impacted nuclides only. if (settings::temperature_field_on) { vector field_temps; std::unordered_set field_seen; for (auto t : simulation::temperature_field.values()) { if (field_seen.insert(t).second) { field_temps.push_back(t); } } auto add_temperatures = [&field_temps](vector>& lists) { for (auto& temps : lists) { std::unordered_set seen(temps.begin(), temps.end()); for (auto t : field_temps) { if (seen.insert(t).second) { temps.push_back(t); } } } }; add_temperatures(nuc_temps); add_temperatures(thermal_temps); } } //============================================================================== void check_geom_valid_for_delta() { // To ensure that no errors are encountered when applying boundary conditions // in delta tracking, we require that: // i) There is a single cell in the root universe; // ii) The half-space of this root cell only contains boundary surfaces. // This prevents external boundaries from cutting off other surfaces, // potentially yielding erroneous boundary conditions. const auto& root = model::universes[model::root_universe]; if (root->cells_.size() != 1) { fatal_error( "At present, delta tracking requires the root universe to contain a " "single cell. Please reconstruct your geometry with a single cell " "in the root universe."); } const auto root_surfaces = model::cells[root->cells_[0]]->surfaces(); for (auto token : root_surfaces) { const auto s_idx = token < 0 ? (-token) - 1 : token - 1; if (!model::surfaces[s_idx]->bc_) { fatal_error(fmt::format( "Surface {} in the root cell {} does not apply a boundary condition. " "When using delta tracking, all boundary surfaces must be included in " "the region definition of the single cell in the root universe.", model::surfaces[s_idx]->id_, model::cells[root->cells_[0]]->id_)); } } model::root_cell_delta = root->cells_[0]; } //============================================================================== void finalize_geometry() { // Perform some final operations to set up the geometry adjust_indices(); count_universe_instances(); partition_universes(); // Assign temperatures to cells that don't have temperatures already assigned assign_temperatures(); // Check to make sure the geometry is valid for the application of boundary // conditions in delta tracking. if (settings::delta_tracking) { check_geom_valid_for_delta(); } // Determine number of nested coordinate levels in the geometry model::n_coord_levels = maximum_levels(model::root_universe); } //============================================================================== int32_t find_root_universe() { // Find all the universes listed as a cell fill. std::unordered_set fill_univ_ids; for (const auto& c : model::cells) { fill_univ_ids.insert(c->fill_); } // Find all the universes contained in a lattice. for (const auto& lat : model::lattices) { for (auto it = lat->begin(); it != lat->end(); ++it) { fill_univ_ids.insert(*it); } if (lat->outer_ != NO_OUTER_UNIVERSE) { fill_univ_ids.insert(lat->outer_); } } // Figure out which universe is not in the set. This is the root universe. bool root_found {false}; int32_t root_univ; for (int32_t i = 0; i < model::universes.size(); i++) { auto search = fill_univ_ids.find(model::universes[i]->id_); if (search == fill_univ_ids.end()) { if (root_found) { fatal_error("Two or more universes are not used as fill universes, so " "it is not possible to distinguish which one is the root " "universe."); } else { root_found = true; root_univ = i; } } } if (!root_found) fatal_error("Could not find a root universe. Make sure " "there are no circular dependencies in the geometry."); return root_univ; } //============================================================================== void prepare_distribcell(const std::vector* user_distribcells) { write_message("Preparing distributed cell instances...", 5); std::unordered_set distribcells; // start with any cells manually specified via the C++ API if (user_distribcells) { distribcells.insert(user_distribcells->begin(), user_distribcells->end()); } // Find all cells listed in a DistribcellFilter or CellInstanceFilter 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) { const auto& filter_cells = cell_inst_filt->cells(); distribcells.insert(filter_cells.begin(), filter_cells.end()); } } // By default, add material cells to the list of distributed cells if (settings::material_cell_offsets) { for (int64_t i = 0; i < model::cells.size(); ++i) { if (model::cells[i]->type_ == Fill::MATERIAL) distribcells.insert(i); } } // Make sure that the number of materials/temperatures matches the number of // cell instances. for (int i = 0; i < model::cells.size(); i++) { Cell& c {*model::cells[i]}; if (c.material_.size() > 1) { if (c.material_.size() != c.n_instances()) { fatal_error(fmt::format( "Cell {} was specified with {} materials but has {} distributed " "instances. The number of materials must equal one or the number " "of instances.", c.id_, c.material_.size(), c.n_instances())); } } if (c.sqrtkT_.size() > 1) { if (c.sqrtkT_.size() != c.n_instances()) { fatal_error(fmt::format( "Cell {} was specified with {} temperatures but has {} distributed " "instances. The number of temperatures must equal one or the number " "of instances.", c.id_, c.sqrtkT_.size(), c.n_instances())); } } if (c.density_mult_.size() > 1) { if (c.density_mult_.size() != c.n_instances()) { fatal_error(fmt::format("Cell {} was specified with {} density " "multipliers but has {} distributed " "instances. The number of density multipliers " "must equal one or the number " "of instances.", c.id_, c.density_mult_.size(), c.n_instances())); } } } // Search through universes for material cells and assign each one a // distribcell array index according to the containing universe. vector target_univ_ids; for (const auto& u : model::universes) { for (auto idx : u->cells_) { if (distribcells.find(idx) != distribcells.end()) { if (!contains(target_univ_ids, u->id_)) { target_univ_ids.push_back(u->id_); } model::cells[idx]->distribcell_index_ = std::find(target_univ_ids.begin(), target_univ_ids.end(), u->id_) - target_univ_ids.begin(); } } } // Allocate the cell and lattice offset tables. int n_maps = target_univ_ids.size(); for (auto& c : model::cells) { if (c->type_ != Fill::MATERIAL) { c->offset_.resize(n_maps, C_NONE); } } for (auto& lat : model::lattices) { lat->allocate_offset_table(n_maps); } // Fill the cell and lattice offset tables. #pragma omp parallel for for (int map = 0; map < target_univ_ids.size(); map++) { auto target_univ_id = target_univ_ids[map]; std::unordered_map univ_count_memo; for (const auto& univ : model::universes) { int32_t offset = 0; for (int32_t cell_indx : univ->cells_) { Cell& c = *model::cells[cell_indx]; if (c.type_ == Fill::UNIVERSE) { c.offset_[map] = offset; int32_t search_univ = c.fill_; offset += count_universe_instances( search_univ, target_univ_id, univ_count_memo); } else if (c.type_ == Fill::LATTICE) { c.offset_[map] = offset; Lattice& lat = *model::lattices[c.fill_]; offset += lat.fill_offset_table(target_univ_id, map, univ_count_memo); } } } } } //============================================================================== void count_universe_instances() { for (auto& univ : model::universes) { std::unordered_map univ_count_memo; univ->n_instances_ = count_universe_instances( model::root_universe, univ->id_, univ_count_memo); } } //============================================================================== int count_universe_instances(int32_t search_univ, int32_t target_univ_id, std::unordered_map& univ_count_memo) { // If this is the target, it can't contain itself. if (model::universes[search_univ]->id_ == target_univ_id) { return 1; } // If we have already counted the number of instances, reuse that value. auto search = univ_count_memo.find(search_univ); if (search != univ_count_memo.end()) { return search->second; } int count {0}; for (int32_t cell_indx : model::universes[search_univ]->cells_) { Cell& c = *model::cells[cell_indx]; if (c.type_ == Fill::UNIVERSE) { int32_t next_univ = c.fill_; count += count_universe_instances(next_univ, target_univ_id, univ_count_memo); } else if (c.type_ == Fill::LATTICE) { Lattice& lat = *model::lattices[c.fill_]; for (auto it = lat.begin(); it != lat.end(); ++it) { int32_t next_univ = *it; count += count_universe_instances(next_univ, target_univ_id, univ_count_memo); } } } // Remember the number of instances in this universe. univ_count_memo[search_univ] = count; return count; } //============================================================================== std::string distribcell_path_inner(int32_t target_cell, int32_t map, int32_t target_offset, const Universe& search_univ, int32_t offset) { std::stringstream path; path << "u" << search_univ.id_ << "->"; // Check to see if this universe directly contains the target cell. If so, // write to the path and return. for (int32_t cell_indx : search_univ.cells_) { if ((cell_indx == target_cell) && (offset == target_offset)) { Cell& c = *model::cells[cell_indx]; path << "c" << c.id_; return path.str(); } } // The target must be further down the geometry tree and contained in a fill // cell or lattice cell in this universe. Find which cell contains the // target. vector::const_reverse_iterator cell_it { search_univ.cells_.crbegin()}; for (; cell_it != search_univ.cells_.crend(); ++cell_it) { Cell& c = *model::cells[*cell_it]; // Material cells don't contain other cells so ignore them. if (c.type_ != Fill::MATERIAL) { int32_t temp_offset = offset + c.offset_[map]; if (c.type_ == Fill::LATTICE) { Lattice& lat = *model::lattices[c.fill_]; int32_t indx = lat.universes_.size() * map + lat.begin().indx_; temp_offset += lat.offsets_[indx]; } // The desired cell is the first cell that gives an offset smaller or // equal to the target offset. if (temp_offset <= target_offset) break; } } // if we get through the loop without finding an appropriate entry, throw // an error if (cell_it == search_univ.cells_.crend()) { fatal_error( fmt::format("Failed to generate a text label for distribcell with ID {}." "The current label is: '{}'", model::cells[target_cell]->id_, path.str())); } // Add the cell to the path string. Cell& c = *model::cells[*cell_it]; path << "c" << c.id_ << "->"; if (c.type_ == Fill::UNIVERSE) { // Recurse into the fill cell. offset += c.offset_[map]; path << distribcell_path_inner( target_cell, map, target_offset, *model::universes[c.fill_], offset); return path.str(); } else { // Recurse into the lattice cell. Lattice& lat = *model::lattices[c.fill_]; path << "l" << lat.id_; for (ReverseLatticeIter it = lat.rbegin(); it != lat.rend(); ++it) { int32_t indx = lat.universes_.size() * map + it.indx_; int32_t temp_offset = offset + lat.offsets_[indx] + c.offset_[map]; if (temp_offset <= target_offset) { offset = temp_offset; path << "(" << lat.index_to_string(it.indx_) << ")->"; path << distribcell_path_inner( target_cell, map, target_offset, *model::universes[*it], offset); return path.str(); } } throw std::runtime_error {"Error determining distribcell path."}; } } std::string distribcell_path( int32_t target_cell, int32_t map, int32_t target_offset) { auto& root_univ = *model::universes[model::root_universe]; return distribcell_path_inner(target_cell, map, target_offset, root_univ, 0); } //============================================================================== int maximum_levels(int32_t univ) { const auto level_count = model::universe_level_counts.find(univ); if (level_count != model::universe_level_counts.end()) { return level_count->second; } int levels_below {0}; for (int32_t cell_indx : model::universes[univ]->cells_) { Cell& c = *model::cells[cell_indx]; if (c.type_ == Fill::UNIVERSE) { int32_t next_univ = c.fill_; levels_below = std::max(levels_below, maximum_levels(next_univ)); } else if (c.type_ == Fill::LATTICE) { Lattice& lat = *model::lattices[c.fill_]; for (auto it = lat.begin(); it != lat.end(); ++it) { int32_t next_univ = *it; levels_below = std::max(levels_below, maximum_levels(next_univ)); } } } ++levels_below; model::universe_level_counts[univ] = levels_below; return levels_below; } bool is_root_universe(int32_t univ_id) { return model::universe_map[univ_id] == model::root_universe; } //============================================================================== void free_memory_geometry() { model::root_cell_delta = C_NONE; model::cells.clear(); model::cell_map.clear(); model::universes.clear(); model::universe_map.clear(); model::lattices.clear(); model::lattice_map.clear(); model::overlap_check_count.clear(); } } // namespace openmc