|
| 1 | +/* Copyright (c) 2016 - 2024, the adamantine authors. |
| 2 | + * |
| 3 | + * This file is subject to the Modified BSD License and may not be distributed |
| 4 | + * without copyright and license information. Please refer to the file LICENSE |
| 5 | + * for the text and further information on this license. |
| 6 | + */ |
| 7 | + |
| 8 | +#define BOOST_TEST_MODULE ClosestSourcePointAdaptation |
| 9 | + |
| 10 | +#include "../source/ClosestSourcePointAdaptation.hh" |
| 11 | + |
| 12 | +#include <deal.II/base/quadrature_lib.h> |
| 13 | +#include <deal.II/distributed/cell_data_transfer.templates.h> |
| 14 | +#include <deal.II/distributed/tria.h> |
| 15 | +#include <deal.II/fe/fe_nothing.h> |
| 16 | +#include <deal.II/fe/fe_values.h> |
| 17 | +#include <deal.II/grid/grid_generator.h> |
| 18 | + |
| 19 | +#include <random> |
| 20 | +#include <string> |
| 21 | + |
| 22 | +#include "main.cc" |
| 23 | + |
| 24 | +template <int dim, int spacedim> |
| 25 | +void test() |
| 26 | +{ |
| 27 | + dealii::parallel::distributed::Triangulation<dim, spacedim> tria( |
| 28 | + MPI_COMM_WORLD); |
| 29 | + |
| 30 | + dealii::GridGenerator::subdivided_hyper_cube(tria, 2); |
| 31 | + tria.refine_global(3); |
| 32 | + |
| 33 | + dealii::QGauss<dim> quadrature(2); |
| 34 | + const unsigned int n_q_points = quadrature.size(); |
| 35 | + const unsigned int n_active_cells = tria.n_global_active_cells(); |
| 36 | + |
| 37 | + std::vector<std::vector<double>> cell_quad_point_values( |
| 38 | + n_active_cells, |
| 39 | + std::vector<double>(n_q_points, |
| 40 | + std::numeric_limits<double>::signaling_NaN())); |
| 41 | + |
| 42 | + std::random_device rnd_device; |
| 43 | + std::mt19937 mersenne_engine{rnd_device()}; |
| 44 | + std::uniform_real_distribution<double> dist{1., 2.}; |
| 45 | + |
| 46 | + for (auto &cell : tria.active_cell_iterators()) |
| 47 | + if (cell->is_locally_owned()) |
| 48 | + { |
| 49 | + cell->set_refine_flag(); |
| 50 | + std::generate(cell_quad_point_values[cell->active_cell_index()].begin(), |
| 51 | + cell_quad_point_values[cell->active_cell_index()].end(), |
| 52 | + [&]() { return dist(mersenne_engine); }); |
| 53 | + } |
| 54 | + |
| 55 | + adamantine::ClosestQuadPointAdaptation<dim, spacedim, double> |
| 56 | + closest_quad_point_adaptation(quadrature); |
| 57 | + dealii::parallel::distributed::CellDataTransfer< |
| 58 | + dim, spacedim, std::vector<std::vector<double>>> |
| 59 | + cell_data_transfer( |
| 60 | + tria, false, |
| 61 | + [&](const typename dealii::Triangulation<dim, spacedim>::cell_iterator |
| 62 | + &parent, |
| 63 | + const std::vector<double> parent_values) { |
| 64 | + return closest_quad_point_adaptation.coarse_to_fine(parent, |
| 65 | + parent_values); |
| 66 | + }, |
| 67 | + [&](const typename dealii::Triangulation<dim, spacedim>::cell_iterator |
| 68 | + &parent, |
| 69 | + const std::vector<std::vector<double>> child_values) { |
| 70 | + return closest_quad_point_adaptation.fine_to_coarse(parent, |
| 71 | + child_values); |
| 72 | + }); |
| 73 | + |
| 74 | + cell_data_transfer.prepare_for_coarsening_and_refinement( |
| 75 | + cell_quad_point_values); |
| 76 | + tria.execute_coarsening_and_refinement(); |
| 77 | + |
| 78 | + const unsigned int n_active_cells_new = tria.n_global_active_cells(); |
| 79 | + |
| 80 | + std::vector<std::vector<double>> cell_quad_point_values_new( |
| 81 | + n_active_cells_new, |
| 82 | + std::vector<double>(n_q_points, |
| 83 | + std::numeric_limits<double>::signaling_NaN())); |
| 84 | + cell_data_transfer.unpack(cell_quad_point_values_new); |
| 85 | + |
| 86 | + for (auto &cell : tria.active_cell_iterators()) |
| 87 | + if (cell->is_locally_owned()) |
| 88 | + { |
| 89 | + cell->set_coarsen_flag(); |
| 90 | + for (unsigned int i = 0; i < n_q_points; ++i) |
| 91 | + { |
| 92 | + const double quad_point_value = |
| 93 | + cell_quad_point_values_new[cell->active_cell_index()][i]; |
| 94 | + BOOST_TEST(quad_point_value >= 1.); |
| 95 | + BOOST_TEST(quad_point_value <= 2.); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + cell_data_transfer.prepare_for_coarsening_and_refinement( |
| 100 | + cell_quad_point_values_new); |
| 101 | + tria.execute_coarsening_and_refinement(); |
| 102 | + |
| 103 | + BOOST_TEST(tria.n_global_active_cells() == n_active_cells); |
| 104 | + std::vector<std::vector<double>> cell_quad_point_values_final( |
| 105 | + n_active_cells, |
| 106 | + std::vector<double>(n_q_points, |
| 107 | + std::numeric_limits<double>::signaling_NaN())); |
| 108 | + |
| 109 | + cell_data_transfer.unpack(cell_quad_point_values_final); |
| 110 | + |
| 111 | + for (auto &cell : tria.active_cell_iterators()) |
| 112 | + if (cell->is_locally_owned()) |
| 113 | + { |
| 114 | + cell->set_refine_flag(); |
| 115 | + for (unsigned int i = 0; i < n_q_points; ++i) |
| 116 | + { |
| 117 | + BOOST_TEST(cell_quad_point_values_final[cell->active_cell_index()][i] == |
| 118 | + cell_quad_point_values[cell->active_cell_index()][i]); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +BOOST_AUTO_TEST_CASE(closest_source_point_adaptation) |
| 124 | +{ |
| 125 | + test<2, 2>(); |
| 126 | + test<3, 3>(); |
| 127 | +} |
0 commit comments