// ---------------------------------------------------------------------
//
// Copyright (C) 2019 - 2020 by the deal. II authors
//
// This file is part of the deal. II library. 
//
// The deal. II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version. 
// The full text of the license can be found in the file LICENSE.md at
// the top level directory of deal. II. 
//
// ---------------------------------------------------------------------


// Create a distributed triangulation without multigrid levels and copy it. 

#include <deal. II/base/mpi.h>

#include <deal. II/distributed/fully_distributed_tria.h>
#include <deal. II/distributed/shared_tria.h>
#include <deal. II/distributed/tria.h>

#include <deal. II/fe/fe_q.h>

#include <deal. II/grid/grid_generator.h>
#include <deal. II/grid/grid_out.h>
#include <deal. II/grid/grid_tools.h>
#include <deal. II/grid/tria.h>
#include <deal. II/grid/tria_description.h>

#include "./tests.h"

using namespace dealii;

template <int dim>
void
test(int n_refinements, MPI_Comm comm)
{
  // create pdt
  parallel::distributed::Triangulation<dim> tria_pdt(
    comm,
    dealii::Triangulation<dim>::none,
    parallel::distributed::Triangulation<dim>::construct_multigrid_hierarchy);
  GridGenerator::hyper_cube(tria_pdt);
  tria_pdt.refine_global(n_refinements);

  // create instance of pft
  parallel::fullydistributed::Triangulation<dim> tria_pft(comm);

  // extract relevant information form serial triangulation
  auto construction_data =
    TriangulationDescription::Utilities::create_description_from_triangulation(
      tria_pdt, comm);

  // actually create triangulation
  tria_pft.create_triangulation(construction_data);

  // test triangulation
  FE_Q<dim>       fe(2);
  DoFHandler<dim> dof_handler(tria_pft);
  dof_handler.distribute_dofs(fe);

  // print statistics
  print_statistics(tria_pft);
  print_statistics(dof_handler);
}

int
main(int argc, char *argv[])
{
  Utilities::MPI::MPI_InitFinalize mpi_initialization(argc, argv, 1);
  MPILogInitAll                    all;

  const MPI_Comm comm = MPI_COMM_WORLD;

  {
    deallog.push("2d");
    const int n_refinements = 6;
    test<2>(n_refinements, comm);
    deallog.pop();
  }
  {
    deallog.push("3d");
    const int n_refinements = 4;
    test<3>(n_refinements, comm);
    deallog.pop();
  }
}
