<commit_before>#ifndef DUNE_STUFF_GRID_PROVIDER_CUBE_HH
#define DUNE_STUFF_GRID_PROVIDER_CUBE_HH

// system
#include <sstream>
#include <type_traits>
#include <boost/assign/list_of.hpp>

// dune-common
#include <dune/common/parametertree.hh>
#include <dune/common/shared_ptr.hh>
#include <dune/common/exceptions.hh>
#include <dune/common/fvector.hh>

// dune-grid
#include <dune/grid/utility/structuredgridfactory.hh>
#include <dune/grid/yaspgrid.hh>
#include <dune/grid/alugrid.hh>
#include <dune/grid/sgrid.hh>
#include <dune/grid/common/mcmgmapper.hh>
#include <dune/grid/io/file/vtk/vtkwriter.hh>

namespace Dune {
namespace Stuff {
namespace Grid {
namespace Provider {

/**
 *  \brief  Creates a grid of a cube in various dimensions.
 *
 *          Default implementation using the Dune::StructuredGridFactory to create a grid of a cube in 1, 2 or 3
 *          dimensions. Tested with
 *          <ul><li> \c YASPGRID, \c variant 1, dim = 1, 2, 3,
 *          <li> \c SGRID, \c variant 1, dim = 1, 2, 3,
 *          <li> \c ALUGRID_SIMPLEX, \c variant 2, dim = 2, 3,
 *          <li> \c ALUGRID_CONFORM, \c variant 2, dim = 2, 2 and
 *          <li> \c ALUGRID_CUBE, \c variant 1, dim = 2, 3.</ul>
 *  \tparam GridImp
 *          Type of the underlying grid.
 *  \tparam variant
 *          Type of the codim 0 elements:
 *          <ul><li>\c 1: cubes
 *          <li>2: simplices</ul>
 **/
template< typename GridImp, int variant >
class GenericCube
{
public:
  //! Type of the provided grid.
  typedef GridImp GridType;

  //! Dimension of the provided grid.
  static const unsigned int dim = GridType::dimension;

  //! Type of the grids coordinates.
  typedef Dune::FieldVector< typename GridType::ctype, dim > CoordinateType;

  //! Unique identifier: \c stuff.grid.provider.cube
  static const std::string id;

  /**
   *  \brief      Creates a cube.
   *  \param[in]  paramTree
   *              A Dune::ParameterTree containing
   *              <ul><li> the following keys directly or
   *              <li> a subtree named Cube::id, containing the following keys. If a subtree is present, it is always selected. Also it is solely selceted, so that all keys in the supertree are ignored.</ul>
   *              The actual keys are:
   *              <ul><li> \c lowerLeft: \a double that is used as a lower left corner in each dimension.
   *              <li> \c upperRight: \a double that is used as an upper right corner in each dimension.
   *              <li> \c numElements.D \a int to denote the number of elements in direction of dimension D (has to be given for each dimension seperately).
   *              <li> \c level: \a int level of refinement. If given, overrides numElements and creates \f$ 2^level \f$ elements per dimension.
   *              </ul>
   **/
  GenericCube(const Dune::ParameterTree paramTree)
    : lowerLeft_(0.0),
      upperRight_(1.0)
  {
    // select subtree (if necessary)
    Dune::ParameterTree paramTree_ = paramTree;
    if (paramTree.hasSub(id))
      paramTree_ = paramTree.sub(id);
    // get outer bounds
    const double lowerLeft = paramTree_.get("lowerLeft", 0.0);
    const double upperRight = paramTree_.get("upperRight", 1.0);
    assert(lowerLeft < upperRight);
    lowerLeft_ = lowerLeft;
    upperRight_ = upperRight;
    // get number of elements per dim
    if (paramTree.hasKey("level"))
      std::fill(std::begin(numElements_), std::end(numElements_), std::pow(2, paramTree.get("level", 1)));
    else {
      for (unsigned int d = 0; d < dim; ++d) {
        std::stringstream s;
        s << "numElements." << d;
        numElements_[d] = paramTree.get(s.str(), 1);
      }
    }
    buildGrid();
  } // Cube(const Dune::ParameterTree& paramTree)

  /**
   *  \brief      Creates a cube.
   *  \param[in]  lowerLeft
   *              A vector that is used as a lower left corner.
   *  \param[in]  upperRight
   *              A vector that is used as a upper right corner.
   *  \param[in]  level (optional)
   *              Level of refinement (see constructor for details).
   **/
  GenericCube(const CoordinateType& lowerLeft, const CoordinateType& upperRight, const int level = 1)
    : lowerLeft_(lowerLeft),
      upperRight_(upperRight)
  {
    std::fill(numElements_.begin(), numElements_.end(), std::pow(2, level));
    buildGrid();
  }

  /**
   *  \brief      Creates a cube.
   *  \param[in]  lowerLeft
   *              A double that is used as a lower left corner in each dimension.
   *  \param[in]  upperRight
   *              A double that is used as a upper right corner in each dimension.
   *  \param[in]  level (optional)
   *              Level of refinement (see constructor for details).
   **/
  GenericCube(const double lowerLeft, const double upperRight, const int level = 1)
    : lowerLeft_(lowerLeft),
      upperRight_(upperRight)
  {
    std::fill(std::begin(numElements_), std::end(numElements_), std::pow(2, level));
    buildGrid();
  }

  /**
    \brief      Creates a cube. This signature allows to prescribe anisotopic refinement
    \param[in]  lowerLeft
                A double that is used as a lower left corner in each dimension.
    \param[in]  upperRight
                A double that is used as a upper right corner in each dimension.
    \param[in]  elements_per_dim number of elements in each dimension.
                can contain 0 to dim elements (missing dimension are initialized to 1)
    \tparam Coord anything that CoordinateType allows to copy construct from
    \tparam ContainerType some sequence type that functions with std::begin/end
    \tparam T an unsigned integral Type
    **/
  template < class Coord, class ContainerType  >
  GenericCube(const Coord lowerLeft, const Coord upperRight,
       const ContainerType elements_per_dim = boost::assign::list_of<typename ContainerType::value_type>().repeat(GridType::dimensionworld,typename ContainerType::value_type(1)) )
    : lowerLeft_(lowerLeft),
      upperRight_(upperRight)
  {
    static_assert(std::is_unsigned<typename ContainerType::value_type>::value
        && std::is_integral<typename ContainerType::value_type>::value,
        "only unsigned integral number of elements per dimension allowed");
    //base init in case input is shorter
    std::fill(std::begin(numElements_), std::end(numElements_), 1);
    std::copy(std::begin(elements_per_dim), std::end(elements_per_dim), std::begin(numElements_));
    buildGrid();
  }

  /**
    \brief  Provides access to the created grid.<commit_msg>[grid.provider.cube] removed what does not work with gcc-4.4<commit_after>#ifndef DUNE_STUFF_GRID_PROVIDER_CUBE_HH
#define DUNE_STUFF_GRID_PROVIDER_CUBE_HH

// system
#include <sstream>
#include <type_traits>
#include <boost/assign/list_of.hpp>

// dune-common
#include <dune/common/parametertree.hh>
#include <dune/common/shared_ptr.hh>
#include <dune/common/exceptions.hh>
#include <dune/common/fvector.hh>

// dune-grid
#include <dune/grid/utility/structuredgridfactory.hh>
#include <dune/grid/yaspgrid.hh>
#include <dune/grid/alugrid.hh>
#include <dune/grid/sgrid.hh>
#include <dune/grid/common/mcmgmapper.hh>
#include <dune/grid/io/file/vtk/vtkwriter.hh>

namespace Dune {
namespace Stuff {
namespace Grid {
namespace Provider {

/**
 *  \brief  Creates a grid of a cube in various dimensions.
 *
 *          Default implementation using the Dune::StructuredGridFactory to create a grid of a cube in 1, 2 or 3
 *          dimensions. Tested with
 *          <ul><li> \c YASPGRID, \c variant 1, dim = 1, 2, 3,
 *          <li> \c SGRID, \c variant 1, dim = 1, 2, 3,
 *          <li> \c ALUGRID_SIMPLEX, \c variant 2, dim = 2, 3,
 *          <li> \c ALUGRID_CONFORM, \c variant 2, dim = 2, 2 and
 *          <li> \c ALUGRID_CUBE, \c variant 1, dim = 2, 3.</ul>
 *  \tparam GridImp
 *          Type of the underlying grid.
 *  \tparam variant
 *          Type of the codim 0 elements:
 *          <ul><li>\c 1: cubes
 *          <li>2: simplices</ul>
 **/
template< typename GridImp, int variant >
class GenericCube
{
public:
  //! Type of the provided grid.
  typedef GridImp GridType;

  //! Dimension of the provided grid.
  static const unsigned int dim = GridType::dimension;

  //! Type of the grids coordinates.
  typedef Dune::FieldVector< typename GridType::ctype, dim > CoordinateType;

  //! Unique identifier: \c stuff.grid.provider.cube
  static const std::string id;

  /**
   *  \brief      Creates a cube.
   *  \param[in]  paramTree
   *              A Dune::ParameterTree containing
   *              <ul><li> the following keys directly or
   *              <li> a subtree named Cube::id, containing the following keys. If a subtree is present, it is always selected. Also it is solely selceted, so that all keys in the supertree are ignored.</ul>
   *              The actual keys are:
   *              <ul><li> \c lowerLeft: \a double that is used as a lower left corner in each dimension.
   *              <li> \c upperRight: \a double that is used as an upper right corner in each dimension.
   *              <li> \c numElements.D \a int to denote the number of elements in direction of dimension D (has to be given for each dimension seperately).
   *              <li> \c level: \a int level of refinement. If given, overrides numElements and creates \f$ 2^level \f$ elements per dimension.
   *              </ul>
   **/
  GenericCube(const Dune::ParameterTree paramTree)
    : lowerLeft_(0.0),
      upperRight_(1.0)
  {
    // select subtree (if necessary)
    Dune::ParameterTree paramTree_ = paramTree;
    if (paramTree.hasSub(id))
      paramTree_ = paramTree.sub(id);
    // get outer bounds
    const double lowerLeft = paramTree_.get("lowerLeft", 0.0);
    const double upperRight = paramTree_.get("upperRight", 1.0);
    assert(lowerLeft < upperRight);
    lowerLeft_ = lowerLeft;
    upperRight_ = upperRight;
    // get number of elements per dim
    if (paramTree.hasKey("level"))
      std::fill(numElements_.begin(), numElements_.end(), std::pow(2, paramTree.get("level", 1)));
    else {
      for (unsigned int d = 0; d < dim; ++d) {
        std::stringstream s;
        s << "numElements." << d;
        numElements_[d] = paramTree.get(s.str(), 1);
      }
    }
    buildGrid();
  } // Cube(const Dune::ParameterTree& paramTree)

  /**
   *  \brief      Creates a cube.
   *  \param[in]  lowerLeft
   *              A vector that is used as a lower left corner.
   *  \param[in]  upperRight
   *              A vector that is used as a upper right corner.
   *  \param[in]  level (optional)
   *              Level of refinement (see constructor for details).
   **/
  GenericCube(const CoordinateType& lowerLeft, const CoordinateType& upperRight, const int level = 1)
    : lowerLeft_(lowerLeft),
      upperRight_(upperRight)
  {
    std::fill(numElements_.begin(), numElements_.end(), std::pow(2, level));
    buildGrid();
  }

  /**
   *  \brief      Creates a cube.
   *  \param[in]  lowerLeft
   *              A double that is used as a lower left corner in each dimension.
   *  \param[in]  upperRight
   *              A double that is used as a upper right corner in each dimension.
   *  \param[in]  level (optional)
   *              Level of refinement (see constructor for details).
   **/
  GenericCube(const double lowerLeft, const double upperRight, const int level = 1)
    : lowerLeft_(lowerLeft),
      upperRight_(upperRight)
  {
    std::fill(numElements_.begin(), numElements_.end(), std::pow(2, level));
    buildGrid();
  }

  /**
    \brief      Creates a cube. This signature allows to prescribe anisotopic refinement
    \param[in]  lowerLeft
                A double that is used as a lower left corner in each dimension.
    \param[in]  upperRight
                A double that is used as a upper right corner in each dimension.
    \param[in]  elements_per_dim number of elements in each dimension.
                can contain 0 to dim elements (missing dimension are initialized to 1)
    \tparam Coord anything that CoordinateType allows to copy construct from
    \tparam ContainerType some sequence type that functions with std::begin/end
    \tparam T an unsigned integral Type
    **/
  template < class Coord, class ContainerType  >
  GenericCube(const Coord lowerLeft, const Coord upperRight,
       const ContainerType elements_per_dim = boost::assign::list_of<typename ContainerType::value_type>().repeat(GridType::dimensionworld,typename ContainerType::value_type(1)) )
    : lowerLeft_(lowerLeft),
      upperRight_(upperRight)
  {
    static_assert(std::is_unsigned<typename ContainerType::value_type>::value
        && std::is_integral<typename ContainerType::value_type>::value,
        "only unsigned integral number of elements per dimension allowed");
    //base init in case input is shorter
    std::fill(numElements_.begin(), numElements_.end(), 1);
    std::copy(elements_per_dim.begin(), elements_per_dim.end(), numElements_.begin());
    buildGrid();
  }

  /**
    \brief  Provides access to the created grid.
{'fraction_non_alphanumeric': 0.10945541132191412, 'fraction_numerical': 0.006912973346647208, 'mean_word_length': 2.7837837837837838, 'pattern_counts': {'":': 0, '<': 95, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '796790', 'n_tokens_mistral': 4090, 'n_tokens_neox': 3951, 'n_words': 1280}