Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://www.mooseframework.org 3 : //* 4 : //* All rights reserved, see COPYRIGHT for full restrictions 5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT 6 : //* 7 : //* Licensed under LGPL 2.1, please see LICENSE for details 8 : //* https://www.gnu.org/licenses/lgpl-2.1.html 9 : 10 : #include "CSGUniverseList.h" 11 : 12 : namespace CSG 13 : { 14 : 15 316 : CSGUniverseList::CSGUniverseList() 16 : { 17 : // create root universe by default when CSG object is initialized 18 316 : std::string root_name = "ROOT_UNIVERSE"; 19 316 : auto root_universe = std::make_unique<CSGUniverse>(root_name, /* is_root = */ true); 20 316 : _root_universe = root_universe.get(); 21 316 : _universes.insert(std::make_pair(root_name, std::move(root_universe))); 22 316 : } 23 : 24 : CSGUniverse & 25 2032 : CSGUniverseList::getUniverse(const std::string & name) const 26 : { 27 2032 : auto univ = _universes.find(name); 28 2032 : if (univ == _universes.end()) 29 6 : mooseError("No universe by name " + name + " exists in the geometry."); 30 : else 31 4060 : return *(univ->second); 32 : } 33 : 34 : std::vector<std::reference_wrapper<const CSGUniverse>> 35 200 : CSGUniverseList::getAllUniverses() const 36 : { 37 200 : std::vector<std::reference_wrapper<const CSGUniverse>> univs; 38 736 : for (auto it = _universes.begin(); it != _universes.end(); ++it) 39 536 : univs.push_back(*(it->second.get())); 40 200 : return univs; 41 0 : } 42 : 43 : CSGUniverse & 44 264 : CSGUniverseList::addUniverse(const std::string & name) 45 : { 46 264 : return addUniverse(std::make_unique<CSGUniverse>(name)); 47 : } 48 : 49 : CSGUniverse & 50 462 : CSGUniverseList::addUniverse(std::unique_ptr<CSGUniverse> universe, 51 : const bool ignore_identical_universe) 52 : { 53 462 : auto univ_name = universe->getName(); 54 462 : if (ignore_identical_universe) 55 : // Check that universe already defined in _universes and if so, confirm it matches with input 56 : // univ 57 14 : if (auto it = _universes.find(univ_name); it != _universes.end()) 58 : { 59 10 : if (*universe == *it->second) 60 8 : return *it->second; 61 : else 62 2 : mooseError("Universe with name ", 63 : univ_name, 64 : " has the same name as an existing universe in CSGBase instance but cannot be " 65 : "discarded as it is not an identical universe."); 66 : } 67 : 68 : // Otherwise, add the universe to the universe list. At this point, we don't expect the universe 69 : // to already be defined in the universe list 70 452 : auto [it, inserted] = _universes.emplace(univ_name, std::move(universe)); 71 452 : if (!inserted) 72 18 : mooseError("Universe with name " + univ_name + " already exists in geometry."); 73 446 : return *it->second; 74 462 : } 75 : 76 : void 77 66 : CSGUniverseList::renameUniverse(const CSGUniverse & universe, const std::string & name) 78 : { 79 : // check that this universe passed in is actually in the same universe that is in the universe 80 : // list 81 66 : auto prev_name = universe.getName(); 82 66 : auto it = _universes.find(prev_name); 83 66 : if (it == _universes.end() || it->second.get() != &universe) 84 10 : mooseError("Universe " + prev_name + " cannot be renamed to " + name + 85 : " as it does not exist in this CSGBase instance."); 86 : 87 64 : auto existing_univ = std::move(it->second); 88 64 : existing_univ->setName(name); 89 64 : _universes.erase(prev_name); 90 66 : addUniverse(std::move(existing_univ)); 91 68 : } 92 : 93 : bool 94 4 : CSGUniverseList::operator==(const CSGUniverseList & other) const 95 : { 96 4 : const auto all_univs = this->getAllUniverses(); 97 4 : const auto other_univs = other.getAllUniverses(); 98 : 99 : // Check that same number of universes are defined in both lists 100 4 : if (all_univs.size() != other_univs.size()) 101 0 : return false; 102 : 103 : // Iterate through each CSGUniverse in list and check equality of universe 104 : // with other list 105 20 : for (const auto & univ : all_univs) 106 : { 107 16 : const auto & univ_name = univ.get().getName(); 108 16 : if (!other.hasUniverse(univ_name)) 109 0 : return false; 110 16 : const auto & other_univ = other.getUniverse(univ_name); 111 16 : if (univ.get() != other_univ) 112 0 : return false; 113 : } 114 4 : return true; 115 4 : } 116 : 117 : bool 118 0 : CSGUniverseList::operator!=(const CSGUniverseList & other) const 119 : { 120 0 : return !(*this == other); 121 : } 122 : 123 : } // namespace CSG