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 "CSGLattice.h" 11 : #include "CSGUtils.h" 12 : 13 : namespace CSG 14 : { 15 : 16 206 : CSGLattice::CSGLattice(const std::string & name, 17 : const std::string & lattice_type, 18 206 : const std::optional<OuterVariant> & outer) 19 618 : : _name(name), _lattice_type(lattice_type), _outer_type("VOID"), _outer_universe(nullptr) 20 : { 21 206 : CSGUtils::checkValidCSGName(name); 22 : // Handle the outer variant if provided 23 206 : if (outer.has_value()) 24 : { 25 32 : std::visit([this](auto && outer_arg) { updateOuter(outer_arg); }, outer.value()); 26 : } 27 206 : } 28 : 29 : void 30 8 : CSGLattice::setUniverseAtIndex(const CSGUniverse & universe, const std::pair<int, int> index) 31 : { 32 16 : std::string base_msg = "Cannot set universe at location (" + std::to_string(index.first) + ", " + 33 24 : std::to_string(index.second) + ") for lattice " + getName() + ". "; 34 8 : if (_universe_map.size() == 0) 35 4 : mooseError(base_msg + "Universe map has not been initialized."); 36 6 : if (!isValidIndex(index)) 37 4 : mooseError(base_msg + "Not a valid location."); 38 4 : _universe_map[index.first][index.second] = universe; 39 8 : } 40 : 41 : bool 42 10 : CSGLattice::hasUniverse(const std::string & name) const 43 : { 44 16 : for (auto list_univ : _universe_map) 45 30 : for (const CSGUniverse & univ : list_univ) 46 24 : if (univ.getName() == name) 47 12 : return true; 48 4 : return false; // no universe with matching name was found 49 : } 50 : 51 : const std::vector<std::vector<std::string>> 52 50 : CSGLattice::getUniverseNameMap() const 53 : { 54 50 : std::vector<std::vector<std::string>> name_map; 55 182 : for (auto & univ_list : _universe_map) 56 : { 57 132 : std::vector<std::string> name_list; 58 492 : for (const CSGUniverse & univ : univ_list) 59 360 : name_list.push_back(univ.getName()); 60 132 : name_map.push_back(name_list); 61 132 : } 62 50 : return name_map; 63 0 : } 64 : 65 : const CSGUniverse & 66 4 : CSGLattice::getUniverseAtIndex(const std::pair<int, int> index) 67 : { 68 4 : if (!isValidIndex(index)) 69 18 : mooseError("Index (" + std::to_string(index.first) + ", " + std::to_string(index.second) + 70 6 : ") is not a valid index for lattice " + getName()); 71 : else 72 2 : return _universe_map[index.first][index.second]; 73 : } 74 : 75 : const std::vector<std::pair<unsigned int, unsigned int>> 76 4 : CSGLattice::getUniverseIndices(const std::string & univ_name) const 77 : { 78 4 : if (!hasUniverse(univ_name)) 79 8 : mooseError("Universe " + univ_name + " does not exist in lattice " + getName()); 80 : 81 2 : std::vector<std::pair<unsigned int, unsigned int>> indices; 82 6 : for (auto i : index_range(_universe_map)) 83 16 : for (auto j : index_range(_universe_map[i])) 84 : { 85 12 : const CSGUniverse & univ = _universe_map[i][j]; 86 12 : if (univ.getName() == univ_name) 87 6 : indices.push_back(std::make_pair(i, j)); 88 : } 89 2 : return indices; 90 0 : } 91 : 92 : const std::vector<std::reference_wrapper<const CSGUniverse>> 93 6 : CSGLattice::getUniqueUniverses() const 94 : { 95 6 : std::vector<std::reference_wrapper<const CSGUniverse>> unique_univs; 96 6 : auto all_univs = getUniverses(); 97 : 98 18 : for (const auto & ulist : all_univs) 99 28 : for (const auto & u : ulist) 100 : { 101 16 : auto it = std::find_if(unique_univs.begin(), 102 : unique_univs.end(), 103 10 : [&u](const auto & ref) { return &ref.get() == &u.get(); }); 104 16 : if (it == unique_univs.end()) 105 8 : unique_univs.push_back(u); 106 : } 107 12 : return unique_univs; 108 6 : } 109 : 110 : const CSGUniverse & 111 106 : CSGLattice::getOuterUniverse() const 112 : { 113 106 : if (getOuterType() != "UNIVERSE") 114 24 : mooseError("Lattice '" + getName() + "' has " + getOuterType() + " outer, not UNIVERSE."); 115 : else 116 102 : return *_outer_universe; 117 : } 118 : 119 : const std::string & 120 20 : CSGLattice::getOuterMaterial() const 121 : { 122 20 : if (getOuterType() != "CSG_MATERIAL") 123 24 : mooseError("Lattice '" + getName() + "' has " + getOuterType() + " outer, not CSG_MATERIAL."); 124 : else 125 16 : return _outer_material; 126 : } 127 : 128 : void 129 42 : CSGLattice::updateOuter(const CSGUniverse & outer_universe) 130 : { 131 42 : _outer_type = "UNIVERSE"; 132 42 : _outer_universe = &outer_universe; 133 42 : _outer_material = ""; 134 42 : } 135 : 136 : void 137 16 : CSGLattice::updateOuter(const std::string & outer_name) 138 : { 139 16 : _outer_type = "CSG_MATERIAL"; 140 16 : _outer_material = outer_name; 141 16 : _outer_universe = nullptr; 142 16 : } 143 : 144 : void 145 6 : CSGLattice::resetOuter() 146 : { 147 6 : _outer_type = "VOID"; 148 6 : _outer_material = ""; 149 6 : _outer_universe = nullptr; 150 6 : } 151 : 152 : bool 153 170 : CSGLattice::operator==(const CSGLattice & other) const 154 : { 155 170 : if (this->getName() != other.getName()) 156 30 : return false; 157 140 : if (this->getType() != other.getType()) 158 0 : return false; 159 140 : if (this->getTransformations() != other.getTransformations()) 160 18 : return false; 161 122 : if (this->getOuterType() != other.getOuterType()) 162 50 : return false; 163 74 : if ((this->getOuterType() == "CSG_MATERIAL") && 164 2 : (this->getOuterMaterial() != other.getOuterMaterial())) 165 2 : return false; 166 84 : if ((this->getOuterType() == "UNIVERSE") && 167 14 : (this->getOuterUniverse() != other.getOuterUniverse())) 168 2 : return false; 169 68 : if (!this->compareAttributes(other)) 170 28 : return false; 171 : 172 40 : const auto & this_univs = this->getUniverses(); 173 40 : const auto & other_univs = other.getUniverses(); 174 40 : if (this_univs.size() != other_univs.size()) 175 0 : return false; 176 64 : for (unsigned int i = 0; i < this_univs.size(); ++i) 177 : { 178 28 : if (this_univs[i].size() != other_univs[i].size()) 179 0 : return false; 180 64 : for (unsigned int j = 0; j < this_univs[i].size(); j++) 181 40 : if (this_univs[i][j].get() != other_univs[i][j].get()) 182 4 : return false; 183 : } 184 : 185 36 : return true; 186 40 : } 187 : 188 : bool 189 134 : CSGLattice::operator!=(const CSGLattice & other) const 190 : { 191 134 : return !(*this == other); 192 : } 193 : 194 : } // namespace CSG