<commit_before>// This file is part of the dune-grid-multiscale project:
//   http://users.dune-project.org/projects/dune-grid-multiscale
// Copyright holders: <NAME>
// License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)

#ifndef DUNE_GRID_MULTISCALE_PROVIDER_CUBE_HH
#define DUNE_GRID_MULTISCALE_PROVIDER_CUBE_HH

#include <vector>
#include <memory>
#include <type_traits>

#include <dune/common/exceptions.hh>

#include <dune/stuff/common/disable_warnings.hh>
# include <dune/grid/common/mcmgmapper.hh>
# include <dune/grid/io/file/vtk/vtkwriter.hh>
# include <dune/grid/io/file/dgfparser.hh>
# if HAVE_ALUGRID
#   include <dune/grid/alugrid.hh>
# endif
#include <dune/stuff/common/reenable_warnings.hh>

#include <dune/stuff/grid/provider/cube.hh>
#include <dune/stuff/common/logging.hh>
#include <dune/stuff/common/print.hh>
#include <dune/stuff/common/memory.hh>

#include <dune/grid/multiscale/factory/default.hh>

#include "interface.hh"

namespace Dune {
namespace grid {
namespace Multiscale {
namespace Providers {


template< class GridImp >
class Cube
  : public ProviderInterface< GridImp >
{
  typedef ProviderInterface< GridImp >  BaseType;
  typedef Cube< GridImp >               ThisType;
public:
  typedef typename BaseType::GridType   GridType;
  typedef typename BaseType::MsGridType MsGridType;

  static const unsigned int             dimDomain = BaseType::dimDomain;
  typedef typename BaseType::DomainType DomainType;

  static std::string static_id()
  {
    return BaseType::static_id() + ".cube";
  }

  static Stuff::Common::Configuration default_config(const std::string sub_name = "")
  {
    Stuff::Common::Configuration config;
    config["lower_left"] = "[0.0 0.0 0.0]";
    config["upper_right"] = "[1.0 1.0 1.0]";
    config["num_elements"] = "[8 8 8]";
    config["num_partitions"] = "[2 2 2]";
    config["oversampling_layers"] = "0";
    if (sub_name.empty())
      return config;
    else {
      Stuff::Common::Configuration tmp;
      tmp.add(config, sub_name);
      return tmp;
    }
  } // ... createSampleDescription(...)

  static std::unique_ptr< ThisType > create(const Stuff::Common::Configuration config = default_config(),
                                            const std::string sub_name = static_id())
  {
    // get correct config
    const Stuff::Common::Configuration cfg = config.has_sub(sub_name) ? config.sub(sub_name) : config;
    const Stuff::Common::Configuration default_cfg = default_config();
    return Stuff::Common::make_unique< ThisType >(
        cfg.get("lower_left",          default_cfg.get< DomainType >("lower_left"), dimDomain),
        cfg.get("upper_right",         default_cfg.get< DomainType >("upper_right"), dimDomain),
        cfg.get("num_elements",        default_cfg.get< std::vector< unsigned int > >(   "num_elements"), dimDomain),
        cfg.get("num_partitions",      default_cfg.get< std::vector< size_t > >(         "num_partitions"), dimDomain),
        cfg.get("oversampling_layers", default_cfg.get< size_t >(                        "oversampling_layers")));
  } // ... create(...)

  Cube(const DomainType lower_left                    = default_config().get< DomainType >("lower_left"),
       const DomainType upper_right                   = default_config().get< DomainType >("upper_right"),
       const std::vector< unsigned int > num_elements = default_config().get< std::vector< unsigned int >  >("num_elements"),
       const std::vector< size_t > num_partittions    = default_config().get< std::vector< size_t >  >("num_partitions", dimDomain),
       const size_t num_oversampling_layers           = default_config().get< size_t >("oversampling_layers"),
       std::ostream& out                              = DSC_LOG.devnull(),
       const std::string prefix                       = "")
  {
    if (num_partittions.size() < dimDomain)
      DUNE_THROW(Dune::RangeError,
                 "num_partittions has to be at least of size " << dimDomain << " (is "
                 << num_partittions.size() << ")!");
#ifndef DUNE_GRID_MULTISCALE_PROVIDER_CUBE_DISABLE_CHECKS
    for (size_t ii = 0; ii < dimDomain; ++ii) {
      if (num_partittions[ii] > num_elements[ii])
        DUNE_THROW(Dune::RangeError,
                   num_partittions[ii] << " = num_partittions[" << ii << "] has to be smaller than "
                   << "num_elements[" << ii << "] = " << num_elements[ii] << "!)");
    }
#endif // DUNE_GRID_MULTISCALE_PROVIDER_CUBE_DISABLE_CHECKS
    typedef Dune::Stuff::Grid::Providers::Cube< GridType > CubeGridProvider;
    auto grd_ptr = CubeGridProvider(lower_left, upper_right, num_elements).grid_ptr();
#if HAVE_ALUGRID
    if (std::is_same< GridType, ALUConformGrid< 2, 2 > >::value
        || std::is_same< GridType, ALUGrid< 2, 2, simplex, conforming > >::value)
      grd_ptr->globalRefine(1);
#endif // HAVE_ALUGRID
    grid_ = grd_ptr;
    setup(lower_left, upper_right, num_partittions, num_oversampling_layers, out, prefix);
  }

  Cube(const std::shared_ptr< const GridType > grd,
       const DomainType lower_left                 = default_config().get< DomainType >("lower_left"),
       const DomainType upper_right                = default_config().get< DomainType >("upper_right"),
       const std::vector< size_t > num_partittions = default_config().get< std::vector< size_t >  >("num_partitions", dimDomain),
       const size_t num_oversampling_layers        = default_config().get< size_t >("oversampling_layers"),
       std::ostream& out                           = DSC_LOG.devnull(),
       const std::string prefix                    = "")
    : grid_(grd)
  {
    if (num_partittions.size() < dimDomain)
      DUNE_THROW(Dune::RangeError,
                 "num_partittions has to be at least of size " << dimDomain << " (is " << num_partittions.size() << ")!");
    for (size_t ii = 0; ii < dimDomain; ++ii) {
      if (lower_left[ii] >= upper_right[ii])
        DUNE_THROW(Dune::RangeError,
                   lower_left[ii] << " = lower_left[" << ii << "] has to be smaller than upper_right[" << ii
                   << "] = " << upper_right[ii] << "!)");
    }
    setup(lower_left, upper_right, num_partittions, num_oversampling_layers, out, prefix);
  }

  virtual const GridType& grid() const DS_OVERRIDE
  {
    return *grid_;
  }

  std::shared_ptr< const GridType > grid_ptr() const
  {
    return grid_;
  }

  virtual std::shared_ptr< const MsGridType > ms_grid() const DS_OVERRIDE
  {
    return ms_grid_;
  }

private:
  void setup(const DomainType& lower_left,
             const DomainType& upper_right,
             const std::vector< size_t >& num_partitions,
             const size_t num_oversampling_layers,
             std::ostream& out = DSC_LOG.devnull(), const std::string prefix = "")
  {
    typedef Dune::grid::Multiscale::Factory::Default< GridType > MsGridFactoryType;

    const size_t neighbor_recursion_level = Factory::NeighborRecursionLevel< GridType >::compute();
    // prepare
    MsGridFactoryType factory(grid_);
    factory.prepare();
#ifndef NDEBUG
    // debug output
    out << prefix << static_id()<< ":" << std::endl;
    Stuff::Common::print(lower_left, "lower_left", out, prefix);
    Stuff::Common::print(upper_right, "upper_right", out, prefix);
    Stuff::Common::print(num_partitions, "num_partitions", out, prefix);<commit_msg>replace DS_OVERRIDE and DS_FINAL by override and final, respectively<commit_after>// This file is part of the dune-grid-multiscale project:
//   http://users.dune-project.org/projects/dune-grid-multiscale
// Copyright holders: <NAME>
// License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)

#ifndef DUNE_GRID_MULTISCALE_PROVIDER_CUBE_HH
#define DUNE_GRID_MULTISCALE_PROVIDER_CUBE_HH

#include <vector>
#include <memory>
#include <type_traits>

#include <dune/common/exceptions.hh>

#include <dune/stuff/common/disable_warnings.hh>
# include <dune/grid/common/mcmgmapper.hh>
# include <dune/grid/io/file/vtk/vtkwriter.hh>
# include <dune/grid/io/file/dgfparser.hh>
# if HAVE_ALUGRID
#   include <dune/grid/alugrid.hh>
# endif
#include <dune/stuff/common/reenable_warnings.hh>

#include <dune/stuff/grid/provider/cube.hh>
#include <dune/stuff/common/logging.hh>
#include <dune/stuff/common/print.hh>
#include <dune/stuff/common/memory.hh>

#include <dune/grid/multiscale/factory/default.hh>

#include "interface.hh"

namespace Dune {
namespace grid {
namespace Multiscale {
namespace Providers {


template< class GridImp >
class Cube
  : public ProviderInterface< GridImp >
{
  typedef ProviderInterface< GridImp >  BaseType;
  typedef Cube< GridImp >               ThisType;
public:
  typedef typename BaseType::GridType   GridType;
  typedef typename BaseType::MsGridType MsGridType;

  static const unsigned int             dimDomain = BaseType::dimDomain;
  typedef typename BaseType::DomainType DomainType;

  static std::string static_id()
  {
    return BaseType::static_id() + ".cube";
  }

  static Stuff::Common::Configuration default_config(const std::string sub_name = "")
  {
    Stuff::Common::Configuration config;
    config["lower_left"] = "[0.0 0.0 0.0]";
    config["upper_right"] = "[1.0 1.0 1.0]";
    config["num_elements"] = "[8 8 8]";
    config["num_partitions"] = "[2 2 2]";
    config["oversampling_layers"] = "0";
    if (sub_name.empty())
      return config;
    else {
      Stuff::Common::Configuration tmp;
      tmp.add(config, sub_name);
      return tmp;
    }
  } // ... createSampleDescription(...)

  static std::unique_ptr< ThisType > create(const Stuff::Common::Configuration config = default_config(),
                                            const std::string sub_name = static_id())
  {
    // get correct config
    const Stuff::Common::Configuration cfg = config.has_sub(sub_name) ? config.sub(sub_name) : config;
    const Stuff::Common::Configuration default_cfg = default_config();
    return Stuff::Common::make_unique< ThisType >(
        cfg.get("lower_left",          default_cfg.get< DomainType >("lower_left"), dimDomain),
        cfg.get("upper_right",         default_cfg.get< DomainType >("upper_right"), dimDomain),
        cfg.get("num_elements",        default_cfg.get< std::vector< unsigned int > >(   "num_elements"), dimDomain),
        cfg.get("num_partitions",      default_cfg.get< std::vector< size_t > >(         "num_partitions"), dimDomain),
        cfg.get("oversampling_layers", default_cfg.get< size_t >(                        "oversampling_layers")));
  } // ... create(...)

  Cube(const DomainType lower_left                    = default_config().get< DomainType >("lower_left"),
       const DomainType upper_right                   = default_config().get< DomainType >("upper_right"),
       const std::vector< unsigned int > num_elements = default_config().get< std::vector< unsigned int >  >("num_elements"),
       const std::vector< size_t > num_partittions    = default_config().get< std::vector< size_t >  >("num_partitions", dimDomain),
       const size_t num_oversampling_layers           = default_config().get< size_t >("oversampling_layers"),
       std::ostream& out                              = DSC_LOG.devnull(),
       const std::string prefix                       = "")
  {
    if (num_partittions.size() < dimDomain)
      DUNE_THROW(Dune::RangeError,
                 "num_partittions has to be at least of size " << dimDomain << " (is "
                 << num_partittions.size() << ")!");
#ifndef DUNE_GRID_MULTISCALE_PROVIDER_CUBE_DISABLE_CHECKS
    for (size_t ii = 0; ii < dimDomain; ++ii) {
      if (num_partittions[ii] > num_elements[ii])
        DUNE_THROW(Dune::RangeError,
                   num_partittions[ii] << " = num_partittions[" << ii << "] has to be smaller than "
                   << "num_elements[" << ii << "] = " << num_elements[ii] << "!)");
    }
#endif // DUNE_GRID_MULTISCALE_PROVIDER_CUBE_DISABLE_CHECKS
    typedef Dune::Stuff::Grid::Providers::Cube< GridType > CubeGridProvider;
    auto grd_ptr = CubeGridProvider(lower_left, upper_right, num_elements).grid_ptr();
#if HAVE_ALUGRID
    if (std::is_same< GridType, ALUConformGrid< 2, 2 > >::value
        || std::is_same< GridType, ALUGrid< 2, 2, simplex, conforming > >::value)
      grd_ptr->globalRefine(1);
#endif // HAVE_ALUGRID
    grid_ = grd_ptr;
    setup(lower_left, upper_right, num_partittions, num_oversampling_layers, out, prefix);
  }

  Cube(const std::shared_ptr< const GridType > grd,
       const DomainType lower_left                 = default_config().get< DomainType >("lower_left"),
       const DomainType upper_right                = default_config().get< DomainType >("upper_right"),
       const std::vector< size_t > num_partittions = default_config().get< std::vector< size_t >  >("num_partitions", dimDomain),
       const size_t num_oversampling_layers        = default_config().get< size_t >("oversampling_layers"),
       std::ostream& out                           = DSC_LOG.devnull(),
       const std::string prefix                    = "")
    : grid_(grd)
  {
    if (num_partittions.size() < dimDomain)
      DUNE_THROW(Dune::RangeError,
                 "num_partittions has to be at least of size " << dimDomain << " (is " << num_partittions.size() << ")!");
    for (size_t ii = 0; ii < dimDomain; ++ii) {
      if (lower_left[ii] >= upper_right[ii])
        DUNE_THROW(Dune::RangeError,
                   lower_left[ii] << " = lower_left[" << ii << "] has to be smaller than upper_right[" << ii
                   << "] = " << upper_right[ii] << "!)");
    }
    setup(lower_left, upper_right, num_partittions, num_oversampling_layers, out, prefix);
  }

  virtual const GridType& grid() const override
  {
    return *grid_;
  }

  virtual const std::shared_ptr< const MsGridType >& ms_grid() const override
  {
    return ms_grid_;
  }

  std::shared_ptr< const GridType > grid_ptr() const
  {
    return grid_;
  }

private:
  void setup(const DomainType& lower_left,
             const DomainType& upper_right,
             const std::vector< size_t >& num_partitions,
             const size_t num_oversampling_layers,
             std::ostream& out = DSC_LOG.devnull(), const std::string prefix = "")
  {
    typedef Dune::grid::Multiscale::Factory::Default< GridType > MsGridFactoryType;

    const size_t neighbor_recursion_level = Factory::NeighborRecursionLevel< GridType >::compute();
    // prepare
    MsGridFactoryType factory(grid_);
    factory.prepare();
#ifndef NDEBUG
    // debug output
    out << prefix << static_id()<< ":" << std::endl;
    Stuff::Common::print(lower_left, "lower_left", out, prefix);
    Stuff::Common::print(upper_right, "upper_right", out, prefix);
    Stuff::Common::print(num_partitions, "num_partitions", out, prefix);
{'fraction_non_alphanumeric': 0.13531799729364005, 'fraction_numerical': 0.0037889039242219214, 'mean_word_length': 3.220731010850942, 'pattern_counts': {'":': 2, '<': 229, '<?xml version=': 0, '>': 119, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1770693', 'n_tokens_mistral': 4876, 'n_tokens_neox': 4698, 'n_words': 968}