1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
// ---------------------------------------------------------------------
//
// Copyright (C) 2000 - 2018 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 at
// the top level of the deal.II distribution.
//
// ---------------------------------------------------------------------
#include <deal.II/base/template_constraints.h>
#include <deal.II/lac/vector.h>
#include <deal.II/lac/block_vector_base.h>
#include <deal.II/lac/block_vector.h>
#include <deal.II/lac/trilinos_vector.h>
#include <deal.II/lac/trilinos_parallel_block_vector.h>
#include <deal.II/grid/grid_refinement.h>
#include <deal.II/grid/tria_accessor.h>
#include <deal.II/grid/tria_iterator.h>
#include <deal.II/grid/tria.h>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <functional>
#include <fstream>
DEAL_II_NAMESPACE_OPEN
template <int dim, typename Number, int spacedim>
void GridRefinement::refine (Triangulation<dim,spacedim> &tria,
const Vector<Number> &criteria,
const double threshold,
const unsigned int max_to_mark)
{
Assert (criteria.size() == tria.n_active_cells(),
ExcDimensionMismatch(criteria.size(), tria.n_active_cells()));
Assert (criteria.is_non_negative (), ExcNegativeCriteria());
// when all indicators are zero we
// do not need to refine but only
// to coarsen
if (criteria.all_zero())
return;
const unsigned int n_cells = criteria.size();
//TODO: This is undocumented, looks fishy and seems unnecessary
double new_threshold=threshold;
// when threshold==0 find the
// smallest value in criteria
// greater 0
if (new_threshold==0)
{
new_threshold = criteria(0);
for (unsigned int index=1; index<n_cells; ++index)
if (criteria(index)>0
&& (criteria(index)<new_threshold))
new_threshold=criteria(index);
}
unsigned int marked=0;
for (typename Triangulation<dim,spacedim>::active_cell_iterator cell = tria.begin_active();
cell != tria.end(); ++cell)
if (std::fabs(criteria(cell->active_cell_index())) >= new_threshold)
{
if (max_to_mark!=numbers::invalid_unsigned_int && marked>=max_to_mark)
break;
++marked;
cell->set_refine_flag();
}
}
template <int dim, typename Number, int spacedim>
void GridRefinement::coarsen (Triangulation<dim,spacedim> &tria,
const Vector<Number> &criteria,
const double threshold)
{
Assert (criteria.size() == tria.n_active_cells(),
ExcDimensionMismatch(criteria.size(), tria.n_active_cells()));
Assert (criteria.is_non_negative (), ExcNegativeCriteria());
for (typename Triangulation<dim,spacedim>::active_cell_iterator cell = tria.begin_active();
cell != tria.end(); ++cell)
if (std::fabs(criteria(cell->active_cell_index())) <= threshold)
if (!cell->refine_flag_set())
cell->set_coarsen_flag();
}
template <int dim>
std::pair<double, double>
GridRefinement::adjust_refine_and_coarsen_number_fraction (const unsigned int current_n_cells,
const unsigned int max_n_cells,
const double top_fraction,
const double bottom_fraction)
{
Assert (top_fraction>=0, ExcInvalidParameterValue());
Assert (top_fraction<=1, ExcInvalidParameterValue());
Assert (bottom_fraction>=0, ExcInvalidParameterValue());
Assert (bottom_fraction<=1, ExcInvalidParameterValue());
Assert (top_fraction+bottom_fraction <= 1, ExcInvalidParameterValue());
double refine_cells = current_n_cells * top_fraction;
double coarsen_cells = current_n_cells * bottom_fraction;
const double cell_increase_on_refine = GeometryInfo<dim>::max_children_per_cell - 1.0;
const double cell_decrease_on_coarsen = 1.0 - 1.0/GeometryInfo<dim>::max_children_per_cell;
std::pair<double, double> adjusted_fractions(top_fraction, bottom_fraction);
// first we have to see whether we
// currently already exceed the target
// number of cells
if (current_n_cells >= max_n_cells)
{
// if yes, then we need to stop
// refining cells and instead try to
// only coarsen as many as it would
// take to get to the target
// as we have no information on cells
// being refined isotropically or
// anisotropically, assume isotropic
// refinement here, though that may
// result in a worse approximation
adjusted_fractions.first = 0;
coarsen_cells = (current_n_cells - max_n_cells) /
cell_decrease_on_coarsen;
adjusted_fractions.second = std::min(coarsen_cells/current_n_cells, 1.0);
}
// otherwise, see if we would exceed the
// maximum desired number of cells with the
// number of cells that are likely going to
// result from refinement. here, each cell
// to be refined is replaced by
// C=GeometryInfo<dim>::max_children_per_cell
// new cells, i.e. there will be C-1 more
// cells than before. similarly, C cells
// will be replaced by 1
// again, this is true for isotropically
// refined cells. we take this as an
// approximation of a mixed refinement.
else if (static_cast<unsigned int>
(current_n_cells
+ refine_cells * cell_increase_on_refine
- coarsen_cells * cell_decrease_on_coarsen)
>
max_n_cells)
{
// we have to adjust the
// fractions. assume we want
// alpha*refine_fraction and
// alpha*coarsen_fraction as new
// fractions and the resulting number
// of cells to be equal to
// max_n_cells. this leads to the
// following equation for alpha
const double alpha
=
1. *
(max_n_cells - current_n_cells)
/
(refine_cells * cell_increase_on_refine
- coarsen_cells * cell_decrease_on_coarsen);
adjusted_fractions.first = alpha * top_fraction;
adjusted_fractions.second = alpha * bottom_fraction;
}
return (adjusted_fractions);
}
template <int dim, typename Number, int spacedim>
void
GridRefinement::refine_and_coarsen_fixed_number (Triangulation<dim,spacedim> &tria,
const Vector<Number> &criteria,
const double top_fraction,
const double bottom_fraction,
const unsigned int max_n_cells)
{
// correct number of cells is
// checked in @p{refine}
Assert ((top_fraction>=0) && (top_fraction<=1), ExcInvalidParameterValue());
Assert ((bottom_fraction>=0) && (bottom_fraction<=1), ExcInvalidParameterValue());
Assert (top_fraction+bottom_fraction <= 1, ExcInvalidParameterValue());
Assert (criteria.is_non_negative (), ExcNegativeCriteria());
const std::pair<double, double> adjusted_fractions =
adjust_refine_and_coarsen_number_fraction<dim> (criteria.size(),
max_n_cells,
top_fraction,
bottom_fraction);
const int refine_cells = static_cast<int>(adjusted_fractions.first * criteria.size());
const int coarsen_cells = static_cast<int>(adjusted_fractions.second * criteria.size());
if (refine_cells || coarsen_cells)
{
Vector<Number> tmp (criteria);
if (refine_cells)
{
if (static_cast<size_t>(refine_cells) == criteria.size())
refine (tria, criteria, -std::numeric_limits<double>::max());
else
{
std::nth_element (tmp.begin(), tmp.begin()+refine_cells,
tmp.end(),
std::greater<double>());
refine (tria, criteria, *(tmp.begin() + refine_cells));
}
}
if (coarsen_cells)
{
if (static_cast<size_t>(coarsen_cells) == criteria.size())
coarsen (tria, criteria, std::numeric_limits<double>::max());
else
{
std::nth_element (tmp.begin(), tmp.begin()+tmp.size()-coarsen_cells,
tmp.end(),
std::greater<double>());
coarsen (tria, criteria,
*(tmp.begin() + tmp.size() - coarsen_cells));
}
}
}
}
template <int dim, typename Number, int spacedim>
void
GridRefinement::refine_and_coarsen_fixed_fraction (Triangulation<dim,spacedim> &tria,
const Vector<Number> &criteria,
const double top_fraction,
const double bottom_fraction,
const unsigned int max_n_cells)
{
// correct number of cells is
// checked in @p{refine}
Assert ((top_fraction>=0) && (top_fraction<=1), ExcInvalidParameterValue());
Assert ((bottom_fraction>=0) && (bottom_fraction<=1), ExcInvalidParameterValue());
Assert (top_fraction+bottom_fraction <= 1, ExcInvalidParameterValue());
Assert (criteria.is_non_negative (), ExcNegativeCriteria());
// let tmp be the cellwise square of the
// error, which is what we have to sum
// up and compare with
// @p{fraction_of_error*total_error}.
Vector<Number> tmp;
tmp = criteria;
const double total_error = tmp.l1_norm();
// sort the largest criteria to the
// beginning of the vector
std::sort (tmp.begin(), tmp.end(), std::greater<double>());
// compute thresholds
typename Vector<Number>::const_iterator pp=tmp.begin();
for (double sum=0;
(sum<top_fraction*total_error) && (pp!=(tmp.end()-1));
++pp)
sum += *pp;
double top_threshold = ( pp != tmp.begin () ?
(*pp+*(pp-1))/2 :
*pp );
typename Vector<Number>::const_iterator qq=(tmp.end()-1);
for (double sum=0;
(sum<bottom_fraction*total_error) && (qq!=tmp.begin());
--qq)
sum += *qq;
double bottom_threshold = ( qq != (tmp.end()-1) ?
(*qq + *(qq+1))/2 :
0);
// we now have an idea how many cells we
// are going to refine and coarsen. we use
// this information to see whether we are
// over the limit and if so use a function
// that knows how to deal with this
// situation
// note, that at this point, we have no
// information about anisotropically refined
// cells, thus use the situation of purely
// isotropic refinement as guess for a mixed
// refinemnt as well.
{
const unsigned int refine_cells = pp - tmp.begin(),
coarsen_cells = tmp.end() - qq;
if (static_cast<unsigned int>
(tria.n_active_cells()
+ refine_cells * (GeometryInfo<dim>::max_children_per_cell - 1)
- (coarsen_cells *
(GeometryInfo<dim>::max_children_per_cell - 1) /
GeometryInfo<dim>::max_children_per_cell))
>
max_n_cells)
{
refine_and_coarsen_fixed_number (tria,
criteria,
1.*refine_cells/criteria.size(),
1.*coarsen_cells/criteria.size(),
max_n_cells);
return;
}
}
// in some rare cases it may happen that
// both thresholds are the same (e.g. if
// there are many cells with the same
// error indicator). That would mean that
// all cells will be flagged for
// refinement or coarsening, but some will
// be flagged for both, namely those for
// which the indicator equals the
// thresholds. This is forbidden, however.
//
// In some rare cases with very few cells
// we also could get integer round off
// errors and get problems with
// the top and bottom fractions.
//
// In these case we arbitrarily reduce the
// bottom threshold by one permille below
// the top threshold
//
// Finally, in some cases
// (especially involving symmetric
// solutions) there are many cells
// with the same error indicator
// values. if there are many with
// indicator equal to the top
// threshold, no refinement will
// take place below; to avoid this
// case, we also lower the top
// threshold if it equals the
// largest indicator and the
// top_fraction!=1
const auto minmax_element = std::minmax_element(criteria.begin(), criteria.end());
if ((top_threshold == *minmax_element.second) && (top_fraction != 1))
top_threshold *= 0.999;
if (bottom_threshold>=top_threshold)
bottom_threshold = 0.999*top_threshold;
// actually flag cells
if (top_threshold < *minmax_element.second)
refine (tria, criteria, top_threshold, pp - tmp.begin());
if (bottom_threshold > *minmax_element.first)
coarsen (tria, criteria, bottom_threshold);
}
template <int dim, typename Number, int spacedim>
void
GridRefinement::refine_and_coarsen_optimize (Triangulation<dim,spacedim> &tria,
const Vector<Number> &criteria,
const unsigned int order)
{
Assert (criteria.size() == tria.n_active_cells(),
ExcDimensionMismatch(criteria.size(), tria.n_active_cells()));
Assert (criteria.is_non_negative (), ExcNegativeCriteria());
// get a decreasing order on the error indicator
std::vector<unsigned int> cell_indices(criteria.size());
std::iota(cell_indices.begin(), cell_indices.end(), 0u);
std::sort(cell_indices.begin(), cell_indices.end(),
[&criteria](const unsigned int left,
const unsigned int right)
{
return criteria[left] > criteria[right];
});
double expected_error_reduction = 0;
const double original_error = criteria.l1_norm();
const std::size_t N = criteria.size();
// minimize the cost functional discussed in the documentation
double min_cost = std::numeric_limits<double>::max();
std::size_t min_arg = 0;
for (std::size_t M = 0; M<criteria.size(); ++M)
{
expected_error_reduction += (1-std::pow(2.,-1.*order)) * criteria(cell_indices[M]);
const double cost = std::pow(((std::pow(2.,dim)-1)*(1+M)+N),
(double)order/dim) *
(original_error-expected_error_reduction);
if (cost <= min_cost)
{
min_cost = cost;
min_arg = M;
}
}
refine (tria, criteria, criteria(cell_indices[min_arg]));
}
// explicit instantiations
#include "grid_refinement.inst"
DEAL_II_NAMESPACE_CLOSE
|