17 #include <deal.II/base/template_constraints.h> 18 #include <deal.II/lac/vector.h> 19 #include <deal.II/lac/block_vector_base.h> 20 #include <deal.II/lac/block_vector.h> 21 #include <deal.II/lac/petsc_vector.h> 22 #include <deal.II/lac/petsc_block_vector.h> 23 #include <deal.II/lac/trilinos_vector.h> 24 #include <deal.II/lac/trilinos_block_vector.h> 26 #include <deal.II/grid/grid_refinement.h> 27 #include <deal.II/grid/tria_accessor.h> 28 #include <deal.II/grid/tria_iterator.h> 29 #include <deal.II/grid/tria.h> 37 DEAL_II_NAMESPACE_OPEN
44 template <
typename number>
49 return *std::max_element(criteria.
begin(), criteria.
end());
53 template <
typename number>
58 return *std::min_element(criteria.
begin(), criteria.
end());
63 DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
65 #ifdef DEAL_II_WITH_PETSC 74 #ifndef PETSC_USE_COMPLEX 75 for (
unsigned int i=0; i<criteria.
size(); ++i)
76 m = std::max (m, criteria(i));
78 Assert(
false,
ExcMessage(
"The GridRefinement functions should only get real-valued vectors of refinement indicators." 79 " Using these functions with complex-valued PETSc vectors does not make sense."))
92 PetscScalar m = criteria(0);
93 #ifndef PETSC_USE_COMPLEX 94 for (
unsigned int i=1; i<criteria.
size(); ++i)
95 m = std::min (m, criteria(i));
97 Assert(
false,
ExcMessage(
"The GridRefinement functions should only get real-valued vectors of refinement indicators." 98 " Using these functions with complex-valued PETSc vectors does not make sense."))
105 #ifdef DEAL_II_WITH_TRILINOS 110 TrilinosScalar m = 0;
120 TrilinosScalar m = 0;
126 DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
131 template <
typename VectorType>
132 typename constraint_and_return_value<!IsBlockVector<VectorType>::value,
133 typename VectorType::value_type>::type
134 min_element (
const VectorType &criteria)
136 return internal::min_element (criteria);
140 template <
typename VectorType>
141 typename constraint_and_return_value<!IsBlockVector<VectorType>::value,
142 typename VectorType::value_type>::type
143 max_element (
const VectorType &criteria)
145 return internal::max_element (criteria);
149 template <
typename VectorType>
150 typename constraint_and_return_value<IsBlockVector<VectorType>::value,
151 typename VectorType::value_type>::type
152 min_element (
const VectorType &criteria)
154 typename VectorType::value_type t = internal::min_element(criteria.block(0));
155 for (
unsigned int b=1;
b<criteria.n_blocks(); ++
b)
156 t = std::min (t, internal::min_element(criteria.block(b)));
162 template <
typename VectorType>
163 typename constraint_and_return_value<IsBlockVector<VectorType>::value,
164 typename VectorType::value_type>::type
165 max_element (
const VectorType &criteria)
167 typename VectorType::value_type t = internal::max_element(criteria.block(0));
168 for (
unsigned int b=1;
b<criteria.n_blocks(); ++
b)
169 t = std::max (t, internal::max_element(criteria.block(b)));
185 template <
class VectorType>
186 void qsort_index (
const VectorType &a,
187 std::vector<unsigned int> &ind,
192 typename VectorType::value_type v;
206 while ((a(ind[i])>v) && (i<r));
211 while ((a(ind[j])<v) && (j>0));
214 std::swap (ind[i], ind[j]);
216 std::swap (ind[i], ind[r]);
219 qsort_index(a,ind,l,i-1);
220 qsort_index(a,ind,i+1,r);
227 template <
int dim,
class VectorType,
int spacedim>
229 const VectorType &criteria,
230 const double threshold,
231 const unsigned int max_to_mark)
240 if (criteria.all_zero())
243 const unsigned int n_cells = criteria.size();
247 double new_threshold=threshold;
251 if (new_threshold==0)
253 new_threshold = criteria(0);
254 for (
unsigned int index=1; index<n_cells; ++index)
255 if (criteria(index)>0
256 && (criteria(index)<new_threshold))
257 new_threshold=criteria(index);
260 unsigned int marked=0;
262 cell != tria.
end(); ++cell)
263 if (std::fabs(criteria(cell->active_cell_index())) >= new_threshold)
268 cell->set_refine_flag();
274 template <
int dim,
class VectorType,
int spacedim>
276 const VectorType &criteria,
277 const double threshold)
284 cell != tria.
end(); ++cell)
285 if (std::fabs(criteria(cell->active_cell_index())) <= threshold)
286 if (!cell->refine_flag_set())
287 cell->set_coarsen_flag();
291 std::pair<double, double>
293 const unsigned int max_n_cells,
294 const double top_fraction,
295 const double bottom_fraction)
303 double refine_cells = current_n_cells * top_fraction;
304 double coarsen_cells = current_n_cells * bottom_fraction;
309 std::pair<double, double> adjusted_fractions(top_fraction, bottom_fraction);
313 if (current_n_cells >= max_n_cells)
325 adjusted_fractions.first = 0;
326 coarsen_cells = (current_n_cells - max_n_cells) /
327 cell_decrease_on_coarsen;
328 adjusted_fractions.second = std::min(coarsen_cells/current_n_cells, 1.0);
343 else if (static_cast<unsigned int>
345 + refine_cells * cell_increase_on_refine
346 - coarsen_cells * cell_decrease_on_coarsen)
361 (max_n_cells - current_n_cells)
363 (refine_cells * cell_increase_on_refine
364 - coarsen_cells * cell_decrease_on_coarsen);
366 adjusted_fractions.first = alpha * top_fraction;
367 adjusted_fractions.second = alpha * bottom_fraction;
369 return (adjusted_fractions);
372 template <
int dim,
class VectorType,
int spacedim>
375 const VectorType &criteria,
376 const double top_fraction,
377 const double bottom_fraction,
378 const unsigned int max_n_cells)
387 const std::pair<double, double> adjusted_fractions =
388 adjust_refine_and_coarsen_number_fraction<dim> (criteria.size(),
393 const int refine_cells =
static_cast<int>(adjusted_fractions.first * criteria.size());
394 const int coarsen_cells =
static_cast<int>(adjusted_fractions.second * criteria.size());
396 if (refine_cells || coarsen_cells)
401 std::nth_element (tmp.
begin(), tmp.
begin()+refine_cells,
403 std::greater<double>());
404 refine (tria, criteria, *(tmp.
begin() + refine_cells));
409 std::nth_element (tmp.
begin(), tmp.
begin()+tmp.
size()-coarsen_cells,
411 std::greater<double>());
413 *(tmp.
begin() + tmp.
size() - coarsen_cells));
420 template <
int dim,
typename VectorType,
int spacedim>
423 const VectorType &criteria,
424 const double top_fraction,
425 const double bottom_fraction,
426 const unsigned int max_n_cells)
441 const double total_error = tmp.
l1_norm();
445 std::sort (tmp.begin(), tmp.end(), std::greater<double>());
451 (
sum<top_fraction*total_error) && (pp!=(tmp.end()-1));
454 double top_threshold = ( pp != tmp.
begin () ?
460 (
sum<bottom_fraction*total_error) && (qq!=tmp.
begin());
463 double bottom_threshold = ( qq != (tmp.end()-1) ?
480 const unsigned int refine_cells = pp - tmp.
begin(),
481 coarsen_cells = tmp.end() - qq;
483 if (static_cast<unsigned int>
492 refine_and_coarsen_fixed_number (tria,
494 1.*refine_cells/criteria.size(),
495 1.*coarsen_cells/criteria.size(),
533 if ((top_threshold == max_element(criteria)) &&
535 top_threshold *= 0.999;
537 if (bottom_threshold>=top_threshold)
538 bottom_threshold = 0.999*top_threshold;
541 if (top_threshold < max_element(criteria))
542 refine (tria, criteria, top_threshold, pp - tmp.
begin());
544 if (bottom_threshold > min_element(criteria))
545 coarsen (tria, criteria, bottom_threshold);
550 template <
int dim,
typename VectorType,
int spacedim>
553 const VectorType &criteria,
554 const unsigned int order)
562 std::vector<unsigned int> tmp(criteria.size());
563 for (
unsigned int i=0; i<criteria.size(); ++i)
566 qsort_index (criteria, tmp, 0, criteria.size()-1);
568 double expected_error_reduction = 0;
569 const double original_error = criteria.l1_norm();
571 const unsigned int N = criteria.size();
574 double min_cost = std::numeric_limits<double>::max();
575 unsigned int min_arg = 0;
577 for (
unsigned int M = 0; M<criteria.size(); ++M)
579 expected_error_reduction += (1-std::pow(2.,-1.*order)) * criteria(tmp[M]);
581 const double cost = std::pow(((std::pow(2.,dim)-1)*(1+M)+N),
583 (original_error-expected_error_reduction);
584 if (cost <= min_cost)
591 refine (tria, criteria, criteria(tmp[min_arg]));
596 #include "grid_refinement.inst" 598 DEAL_II_NAMESPACE_CLOSE
unsigned int n_active_cells() const
void refine_and_coarsen_fixed_number(Triangulation< dim, spacedim > &triangulation, const VectorType &criteria, const double top_fraction_of_cells, const double bottom_fraction_of_cells, const unsigned int max_n_cells=std::numeric_limits< unsigned int >::max())
static const unsigned int invalid_unsigned_int
real_type l1_norm() const
active_cell_iterator begin_active(const unsigned int level=0) const
void refine_and_coarsen_fixed_fraction(Triangulation< dim, spacedim > &tria, const VectorType &criteria, const double top_fraction, const double bottom_fraction, const unsigned int max_n_cells=std::numeric_limits< unsigned int >::max())
cell_iterator end() const
static ::ExceptionBase & ExcMessage(std::string arg1)
static ::ExceptionBase & ExcNegativeCriteria()
#define Assert(cond, exc)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
void coarsen(Triangulation< dim, spacedim > &tria, const VectorType &criteria, const double threshold)
SymmetricTensor< rank, dim, Number > sum(const SymmetricTensor< rank, dim, Number > &local, const MPI_Comm &mpi_communicator)
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)
const Epetra_MultiVector & trilinos_vector() const
std::pair< double, double > adjust_refine_and_coarsen_number_fraction(const unsigned int current_n_cells, const unsigned int max_n_cells, const double top_fraction_of_cells, const double bottom_fraction_of_cells)
static ::ExceptionBase & ExcInvalidParameterValue()
void refine(Triangulation< dim, spacedim > &tria, const VectorType &criteria, const double threshold, const unsigned int max_to_mark=numbers::invalid_unsigned_int)
void refine_and_coarsen_optimize(Triangulation< dim, spacedim > &tria, const VectorType &criteria, const unsigned int order=2)
Tensor< 2, dim, Number > l(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)