https://mooseframework.inl.gov
CSGUniverseList.C
Go to the documentation of this file.
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 
16 {
17  // create root universe by default when CSG object is initialized
18  std::string root_name = "ROOT_UNIVERSE";
19  auto root_universe = std::make_unique<CSGUniverse>(root_name, /* is_root = */ true);
20  _root_universe = root_universe.get();
21  _universes.insert(std::make_pair(root_name, std::move(root_universe)));
22 }
23 
25 CSGUniverseList::getUniverse(const std::string & name) const
26 {
27  auto univ = _universes.find(name);
28  if (univ == _universes.end())
29  mooseError("No universe by name " + name + " exists in the geometry.");
30  else
31  return *(univ->second);
32 }
33 
34 std::vector<std::reference_wrapper<const CSGUniverse>>
36 {
37  std::vector<std::reference_wrapper<const CSGUniverse>> univs;
38  for (auto it = _universes.begin(); it != _universes.end(); ++it)
39  univs.push_back(*(it->second.get()));
40  return univs;
41 }
42 
44 CSGUniverseList::addUniverse(const std::string & name)
45 {
46  return addUniverse(std::make_unique<CSGUniverse>(name));
47 }
48 
50 CSGUniverseList::addUniverse(std::unique_ptr<CSGUniverse> universe,
51  const bool ignore_identical_universe)
52 {
53  auto univ_name = universe->getName();
54  if (ignore_identical_universe)
55  // Check that universe already defined in _universes and if so, confirm it matches with input
56  // univ
57  if (auto it = _universes.find(univ_name); it != _universes.end())
58  {
59  if (*universe == *it->second)
60  return *it->second;
61  else
62  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  auto [it, inserted] = _universes.emplace(univ_name, std::move(universe));
71  if (!inserted)
72  mooseError("Universe with name " + univ_name + " already exists in geometry.");
73  return *it->second;
74 }
75 
76 void
77 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  auto prev_name = universe.getName();
82  auto it = _universes.find(prev_name);
83  if (it == _universes.end() || it->second.get() != &universe)
84  mooseError("Universe " + prev_name + " cannot be renamed to " + name +
85  " as it does not exist in this CSGBase instance.");
86 
87  auto existing_univ = std::move(it->second);
88  existing_univ->setName(name);
89  _universes.erase(prev_name);
90  addUniverse(std::move(existing_univ));
91 }
92 
93 bool
95 {
96  const auto all_univs = this->getAllUniverses();
97  const auto other_univs = other.getAllUniverses();
98 
99  // Check that same number of universes are defined in both lists
100  if (all_univs.size() != other_univs.size())
101  return false;
102 
103  // Iterate through each CSGUniverse in list and check equality of universe
104  // with other list
105  for (const auto & univ : all_univs)
106  {
107  const auto & univ_name = univ.get().getName();
108  if (!other.hasUniverse(univ_name))
109  return false;
110  const auto & other_univ = other.getUniverse(univ_name);
111  if (univ.get() != other_univ)
112  return false;
113  }
114  return true;
115 }
116 
117 bool
119 {
120  return !(*this == other);
121 }
122 
123 } // namespace CSG
std::string name(const ElemQuality q)
std::vector< std::reference_wrapper< const CSGUniverse > > getAllUniverses() const
Get all the universes in CSGBase instance.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
const CSGUniverse * _root_universe
root universe for the CSGBase instance
CSGUniverse creates an internal representation of a Constructive Solid Geometry (CSG) universe...
Definition: CSGUniverse.h:27
CSGUniverse & getUniverse(const std::string &name) const
Get a Universe from the list by its name.
const std::string & getName() const
Get the name of the universe.
Definition: CSGUniverse.h:80
CSGUniverseList creates a container for CSGUniverse objects to pass to CSGBase.
CSGUniverse & addUniverse(const std::string &name)
create an empty universe
bool operator==(const CSGUniverseList &other) const
Operator overload for checking if two CSGUniverseList objects are equal.
void renameUniverse(const CSGUniverse &universe, const std::string &name)
rename the specified universe
std::unordered_map< std::string, std::unique_ptr< CSGUniverse > > _universes
Mapping of universe names to pointers of stored universe objects.
bool operator!=(const CSGUniverseList &other) const
Operator overload for checking if two CSGUniverseList objects are not equal.
bool hasUniverse(const std::string &name) const
return whether universe with given name exists in universe list
CSGUniverseList()
Default constructor.