Program Listing for File mesh.hxx

Return to documentation for file (mesh_handle/mesh.hxx)

/*
  This file is part of t8code.
  t8code is a C library to manage a collection (a forest) of multiple
  connected adaptive space-trees of general element classes in parallel.

  Copyright (C) 2025 the developers

  t8code is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  t8code is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with t8code; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#ifndef T8_MESH_HXX
#define T8_MESH_HXX

#include <t8.h>
#include <t8_forest/t8_forest_general.h>
#include "element.hxx"
#include <iterator>
#include <memory>
#include <vector>

namespace t8_mesh_handle
{

template <class TMeshElement = element<>>
class mesh {
 public:
  friend TMeshElement;
  using mesh_const_iterator =
    typename std::vector<TMeshElement>::const_iterator;
  mesh (t8_forest_t input_forest): m_forest (input_forest)
  {
    update_elements ();
  }

  t8_locidx_t
  get_num_local_elements () const
  {
    return t8_forest_get_local_num_leaf_elements (m_forest);
  }

  mesh_const_iterator
  begin () const
  {
    return m_elements.cbegin ();
  }

  mesh_const_iterator
  end () const
  {
    return m_elements.cend ();
  }

  const TMeshElement&
  operator[] (t8_locidx_t local_index) const
  {
    return m_elements[local_index];
  }

  t8_forest_t
  get_forest () const
  {
    return m_forest;
  }

  void
  set_forest (t8_forest_t input_forest)
  {
    m_forest = input_forest;
    update_elements ();
  }

 private:
  void
  update_elements ()
  {
    // Clear the element vector if already created.
    m_elements.clear ();
    m_elements.reserve (get_num_local_elements ());
    // Iterate through forest elements and fill the element vector with newly created mesh elements.
    for (t8_locidx_t itree = 0; itree < t8_forest_get_num_local_trees (m_forest); ++itree) {
      const t8_locidx_t num_elems = t8_forest_get_tree_num_leaf_elements (m_forest, itree);
      for (t8_locidx_t ielem = 0; ielem < num_elems; ++ielem) {
        m_elements.emplace_back (this, itree, ielem);
      }
    }
  }

  t8_forest_t m_forest;
  std::vector<TMeshElement> m_elements;
};

}  // namespace t8_mesh_handle
#endif /* !T8_MESH_HXX */