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 "CSGCellList.h" 11 : 12 : namespace CSG 13 : { 14 : 15 316 : CSGCellList::CSGCellList() {} 16 : 17 : CSGCell & 18 735 : CSGCellList::addCell(std::unique_ptr<CSGCell> cell, const bool ignore_identical_cell) 19 : { 20 735 : auto cell_name = cell->getName(); 21 735 : if (ignore_identical_cell) 22 : // Check that cell already defined in _cells and if so, confirm it matches with input cell 23 16 : if (auto it = _cells.find(cell_name); it != _cells.end()) 24 : { 25 12 : if (*cell == *it->second) 26 10 : return *it->second; 27 : else 28 2 : mooseError("Cell with name ", 29 : cell_name, 30 : " has the same name as an existing cell in CSGBase instance but cannot be " 31 : "discarded as it is not an identical cell."); 32 : } 33 : 34 : // Otherwise, add the cell to the cell list. At this point, we don't expect the cell 35 : // to already be defined in the cell list 36 723 : auto [it, inserted] = _cells.emplace(cell_name, std::move(cell)); 37 723 : if (!inserted) 38 24 : mooseError("Cell with name " + cell_name + " already exists in geometry."); 39 : 40 715 : return *it->second; 41 735 : } 42 : 43 : CSGCell & 44 827 : CSGCellList::getCell(const std::string & name) const 45 : { 46 827 : if (_cells.find(name) == _cells.end()) 47 6 : mooseError("No cell by name " + name + " exists in the geometry."); 48 : else 49 825 : return *(_cells.find(name)->second); 50 : } 51 : 52 : CSGCell & 53 100 : CSGCellList::addVoidCell(const std::string & name, const CSGRegion & region) 54 : { 55 100 : return addCell(std::make_unique<CSGCell>(name, region)); 56 : } 57 : 58 : CSGCell & 59 141 : CSGCellList::addMaterialCell(const std::string & name, 60 : const std::string & mat_name, 61 : const CSGRegion & region) 62 : { 63 141 : return addCell(std::make_unique<CSGCell>(name, mat_name, region)); 64 : } 65 : 66 : CSGCell & 67 46 : CSGCellList::addUniverseCell(const std::string & name, 68 : const CSGUniverse & univ, 69 : const CSGRegion & region) 70 : { 71 46 : return addCell(std::make_unique<CSGCell>(name, &univ, region)); 72 : } 73 : 74 : CSGCell & 75 76 : CSGCellList::addLatticeCell(const std::string & name, 76 : const CSGLattice & lattice, 77 : const CSGRegion & region) 78 : { 79 76 : return addCell(std::make_unique<CSGCell>(name, &lattice, region)); 80 : } 81 : 82 : std::vector<std::reference_wrapper<const CSGCell>> 83 216 : CSGCellList::getAllCells() const 84 : { 85 216 : std::vector<std::reference_wrapper<const CSGCell>> cells; 86 828 : for (auto it = _cells.begin(); it != _cells.end(); ++it) 87 612 : cells.push_back(*(it->second)); 88 216 : return cells; 89 0 : } 90 : 91 : void 92 124 : CSGCellList::renameCell(const CSGCell & cell, const std::string & name) 93 : { 94 : // check that this cell passed in is actually in the same cell that is in the cell list 95 124 : auto prev_name = cell.getName(); 96 124 : auto it = _cells.find(prev_name); 97 124 : if (it == _cells.end() || it->second.get() != &cell) 98 10 : mooseError("Cell " + prev_name + " cannot be renamed to " + name + 99 : " as it does not exist in this CSGBase instance."); 100 : 101 122 : auto existing_cell = std::move(_cells.find(prev_name)->second); 102 122 : existing_cell->setName(name); 103 122 : _cells.erase(prev_name); 104 124 : addCell(std::move(existing_cell)); 105 126 : } 106 : 107 : bool 108 6 : CSGCellList::operator==(const CSGCellList & other) const 109 : { 110 6 : const auto all_cells = this->getAllCells(); 111 6 : const auto other_cells = other.getAllCells(); 112 : 113 : // Check that same number of cells are defined in both lists 114 6 : if (all_cells.size() != other_cells.size()) 115 0 : return false; 116 : 117 : // Iterate through each CSGCell in list and check equality of each cell 118 : // with other list 119 22 : for (const auto & cell : all_cells) 120 : { 121 18 : const auto & cell_name = cell.get().getName(); 122 18 : if (!other.hasCell(cell_name)) 123 2 : return false; 124 18 : const auto & other_cell = other.getCell(cell_name); 125 18 : if (cell.get() != other_cell) 126 2 : return false; 127 : } 128 4 : return true; 129 6 : } 130 : 131 : bool 132 0 : CSGCellList::operator!=(const CSGCellList & other) const 133 : { 134 0 : return !(*this == other); 135 : } 136 : 137 : } // namespace CSG