Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://mooseframework.inl.gov 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 : #pragma once 11 : 12 : // MOOSE includes 13 : #include "MooseError.h" 14 : 15 : // libMesh includes 16 : #include "libmesh/elem.h" 17 : #include "libmesh/elem_range.h" 18 : #include "libmesh/mesh_base.h" 19 : 20 : /** 21 : * Helper for setting up a contiguous index for a given range of elements that are 22 : * known by this processor. 23 : * 24 : * The contiguous index is useful for indexing into alternate data structures for 25 : * each element without the need for a map. 26 : * 27 : * The index is stored on each elem for quick access. 28 : */ 29 : class ElemIndexHelper 30 : { 31 : public: 32 : /** 33 : * Constructor. 34 : * 35 : * @param mesh The mesh that contains the elements that are to be index into. 36 : * @param extra_elem_integer_name A name for the extra element integer that will store the index. 37 : * 38 : * Make sure to call initialize() after construction! 39 : */ 40 : ElemIndexHelper(libMesh::MeshBase & mesh, const std::string & extra_elem_integer_name); 41 : 42 : /** 43 : * Initializes the indices in a contiguous manner for the given element range 44 : */ 45 : void initialize(const libMesh::SimpleRange<libMesh::MeshBase::element_iterator> elems); 46 : 47 : /** 48 : * Whether or not the element \p elem has an index set for it using this object. 49 : */ 50 : bool hasIndex(const libMesh::Elem * elem) const 51 : { 52 : mooseAssert(elem, "Null elem"); 53 : mooseAssert(_initialized, "Not initialized"); 54 : mooseAssert(_mesh.query_elem_ptr(elem->id()), "Not an elem of the mesh"); 55 : return elem->get_extra_integer(_extra_integer) != libMesh::DofObject::invalid_id; 56 : } 57 : 58 : /** 59 : * Get the index associated with the element \p elem. 60 : */ 61 : libMesh::dof_id_type getIndex(const libMesh::Elem * elem) const 62 : { 63 : mooseAssert(hasIndex(elem), "Elem not in indexed range"); 64 913145 : return elem->get_extra_integer(_extra_integer); 65 : } 66 : 67 : /** 68 : * Gets the maximum index generated using this object. 69 : * 70 : * Useful for initializing data structures that will be indexed 71 : * using the indices provided by this object. 72 : */ 73 5519 : libMesh::dof_id_type maxIndex() const { return _max_index; } 74 : 75 : private: 76 : // The mesh 77 : libMesh::MeshBase & _mesh; 78 : /// The extra elem integer that stores the index 79 : unsigned int _extra_integer; 80 : /// Whether or not this object is initialized 81 : bool _initialized; 82 : /// The max index generated 83 : libMesh::dof_id_type _max_index; 84 : };