13#ifndef EMBEDDED_ERROREST_HH
14#define EMBEDDED_ERROREST_HH
17#include "boost/multi_array.hpp"
28 namespace EmbeddedErrorestimatorDetail
35 template <
class Evaluator>
36 typename Evaluator::Space::Mapper::ShapeFunctionSet::Matrix operator()(Evaluator
const& evaluator)
const
38 typename Evaluator::Space::Mapper::ShapeFunctionSet::Matrix p = evaluator.shapeFunctions().hierarchicProjection();
39 evaluator.combiner().rightTransform(p);
40 evaluator.combiner().leftPseudoInverse(p);
46 template <
class Pairs>
47 struct ApplyProjections
49 ApplyProjections(Pairs
const& data_): data(data_) {}
52 void operator()(Pair
const& var)
const
54 int const sid = std::remove_reference<typename boost::fusion::result_of::value_at_c<Pair,0>::type>::type::spaceIndex;
56 typedef typename boost::fusion::result_of::value_at_c<Pairs,sid>::type DataPair;
57 typename boost::fusion::result_of::value_at_c<DataPair,0>::type evaluator = at_c<0>(at_c<sid>(data));
58 typename boost::fusion::result_of::value_at_c<DataPair,1>::type p = at_c<1>(at_c<sid>(data));
60 typedef typename std::remove_reference<typename boost::fusion::result_of::value_at_c<Pair,1>::type>::type
Function;
62 typename Function::StorageType x(p.N());
64 for (
int i=0; i<p.N(); ++i) {
66 for (
int j=0; j<p.M(); ++j)
67 x[i].
axpy(p[i][j][0][0],f.coefficients()[evaluator.globalIndices()[j]]);
69 for (
int i=0; i<p.N(); ++i) {
70 f.coefficients()[evaluator.globalIndices()[i]] = x[i];
78 template <
class Sequence>
79 ApplyProjections<Sequence> applyProjections(Sequence
const& data)
81 return ApplyProjections<Sequence>(data);
99 template <
class VariableSetDescription>
103 using namespace EmbeddedErrorestimatorDetail;
116 auto evaluators = getEvaluators(f.
descriptions.spaces,
static_cast<SfCache*
>(
nullptr));
121 typedef typename boost::fusion::result_of::as_vector<
122 typename boost::fusion::result_of::transform<Evaluators,GetProjections>::type
124 Projections projections;
130 for (
auto ci=gridView.template begin<0,Dune::All_Partition>();
131 ci!=gridView.template end<0,Dune::All_Partition>(); ++ci) {
138 projections = transform(evaluators,GetProjections());
143 applyProjections(zip(evaluators,projections)));
150 namespace EmbeddedErrorestDetail
158 template <
class IndexSet>
159 CellByCell(IndexSet
const& is): nGroups(is.size(0)) {}
161 size_t operator[](
size_t idx)
const { assert(idx<nGroups);
return idx; }
179 class MaximumCollector
187 MaximumCollector(F
const& f_,
size_t n_)
192 template <
class Cell>
193 int integrationOrder(
Cell const&,
int shapeFunctionOrder)
const
195 return shapeFunctionOrder;
198 void join(MaximumCollector& c)
201 eps = std::move(c.eps);
202 else if (!c.eps.empty())
204 assert(eps.size()==c.eps.size());
205 for (
size_t i=0; i<eps.size(); ++i)
210 template <
class Cell,
class Index,
class LocalPosition,
class Values>
212 double , Values
const& u)
216 eps[idx] =
std::max(eps[idx],
double(f(u)));
219 std::vector<double>
const& epsilon()
const
227 std::vector<double> eps;
252 template <
class VariableSet,
class Gr
idManager>
257 double rhoMin=0.1,
double rhoMax=0.9)
263 timing.
timer().
start(
"project hierarchically");
267 timing.
timer().
stop(
"project hierarchically");
270 auto const n = varDesc.indexSet.size(0);
271 auto f = [&tol](
auto const& us)
275 boost::fusion::for_each(us,[&](
auto const& u)
280 EmbeddedErrorestDetail::MaximumCollector
max(f,n);
285 std::array<size_t,3> counts{0,0,0};
286 for (
auto const& cell: Dune::elements(varDesc.gridView))
288 double e =
max.epsilon()[varDesc.indexSet.index(cell)];
289 gridManager.
mark(e>rhoMax? 1: e<rhoMin? -1: 0, cell);
314 template <
class VariableSetDescription,
class Scaling=IdentityScaling>
328 : gridManager(gridManager_), varDesc(varDesc_), tol(varDesc.noOfVariables,std::make_pair(0.0,0.01)), scaling(scaling_) {}
338 using namespace EmbeddedErrorestDetail;
340 assert(size(err.
data)==size(sol.
data));
347 int const n = sum.
sums.shape()[0];
349 assert(sum.
sums.shape()[1]==2*s);
352 std::vector<double> norm2(s), error2(s);
353 for (
int idx=0; idx<n; ++idx)
354 for (
int j=0; j<s; ++j) {
355 error2[j] += sum.
sums[idx][j];
356 norm2[j] += sum.
sums[idx][j+s];
362 std::cout <<
" ||solution||^2 = " << std::scientific << std::setprecision(3); std::copy(norm2.begin(),norm2.end(),std::ostream_iterator<double>(std::cout,
" "));
363 std::cout << std::endl;
364 std::cout <<
" ||errors||^2 = "; std::copy(error2.begin(),error2.end(),std::ostream_iterator<double>(std::cout,
" "));
365 std::cout << std::resetiosflags( ::std::ios::scientific ) << std::setprecision(6) << std::endl;
369 std::vector<double> errorLimit(s);
370 boost::multi_array<double,2> normalizedErrors(boost::extents[n][s]);
372 for (
int j=0; j<s; ++j)
374 errorLimit[j] =
square(tol[j].first) +
square(tol[j].second)*norm2[j];
375 for (
int i=0; i<n; ++i)
376 normalizedErrors[i][j] = sum.
sums[i][j] / errorLimit[j];
381 auto threshold = marker.
threshold(normalizedErrors);
384 if (threshold.empty())
421 criterion = &criterion_;
434 verbosity = verbosity_;
442 std::vector<std::pair<double,double> > tol;
458 template <
class VariableSetDescription,
class Scaling>
463 std::vector<std::pair<double,double> >
const& tol,
Function is the interface for evaluatable functions .
Embedded error estimation and mesh refinement.
Fixed fraction refinement criterion. Determines the refinement thresholds such that at least the spec...
bool mark(int refCount, typename Grid::template Codim< 0 >::Entity const &cell)
Marks the given cell for refinement or coarsening.
Class that takes care of the transfer of data (of each FunctionSpaceElement) after modification of th...
double two_norm() const
euclidean norm
Base class for refinement criteria.
std::vector< double > threshold(boost::multi_array< double, 2 > const &normalizedErrors) const
Computes the thresholds for refinement.
A scope guard object that automatically closes a timing section on destruction.
Timings & timer()
Returns the used timer.
void stop(std::string const &name)
Stops the timing of given section.
struct Times const * start(std::string const &name)
Starts or continues the timing of given section.
A class that stores information about a set of variables.
GridView const & gridView
The grid view on which the variables live.
IndexSet const & indexSet
index set
static int const noOfVariables
Number of variables in this set.
SpaceList Spaces
type of boost::fusion::vector of FEFunctionSpace s needed
Spaces spaces
A heterogeneous sequence of pointers to (const) spaces involved.
typename Variables_Detail::ImplicitIdVariableDescriptions< VariableList >::type Variables
type of boost::fusion vector of VariableDescription s
SpaceType< Spaces, 0 >::type::Grid Grid
Grid type.
SpaceType< Spaces, 0 >::type::GridView GridView
Grid view type.
A class for storing a heterogeneous collection of FunctionSpaceElement s.
Descriptions const & descriptions
Descriptions of variable set, of type VariableSetDescription (lots of useful infos)
A callable that allows to implement weighted (scaled) norms.
void for_each2(Seq1 const &s1, Seq2 &s2, Func const &func)
std::array< size_t, 3 > uniformEmbeddedErrorEstimation(VariableSet const &u, Dune::FieldVector< double, VariableSet::Descriptions::noOfVariables > const &tol, GridManager &gridManager, double rhoMin=0.1, double rhoMax=0.9)
Embedded error estimation and simultaneous mesh refinement/coarsening.
EmbeddedErrorEstimator< VariableSetDescription, Scaling > Self
Self & setTolerances(std::vector< std::pair< double, double > > const &tol_)
set the tolerances
GridManager< typename VariableSetDescription::Grid > GManager
Self & setCriterion(RefinementCriterion const &criterion_)
set the refinement criterion
Self & setScaling(Scaling const &scaling_)
set the scaling
Self & setVerbosity(int verbosity_)
set the verbosity level
EmbeddedErrorEstimator(GManager &gridManager_, VariableSetDescription const &varDesc_, Scaling const &scaling_=Scaling())
Constructor.
void projectHierarchically(VariableSet< VariableSetDescription > &f)
Projects the given FE function onto the the polynomial ansatz subspace of one order lower....
bool embeddedErrorEstimator(VariableSetDescription const &varDesc, typename VariableSetDescription::VariableSet const &err, typename VariableSetDescription::VariableSet const &sol, Scaling const &scaling, std::vector< std::pair< double, double > > const &tol, GridManager< typename VariableSetDescription::Grid > &gridManager, int verbosity=1)
Embedded error estimation and mesh refinement.
bool estimate(typename VariableSetDescription::VariableSet const &err, typename VariableSetDescription::VariableSet const &sol) const
perform error estimation
typename boost::fusion::result_of::as_vector< typename boost::fusion::result_of::transform< Spaces, GetEvaluatorTypes >::type >::type Evaluators
the heterogeneous sequence type of Evaluators for the given spaces
typename GridView::template Codim< 0 >::Entity Cell
The type of cells (entities of codimension 0) in the grid view.
Dune::FieldVector< typename GridView::ctype, GridView::dimension > LocalPosition
The type of local positions within cells of the grid view.
Dune::FieldVector< T, n > max(Dune::FieldVector< T, n > x, Dune::FieldVector< T, n > const &y)
Componentwise maximum.
void markAndRefine(GridManager &gridManager, GridView const &gridView, boost::multi_array< double, 2 > const &normalizedErrors, std::vector< double > const &threshold)
Refines the grid according to the normalized errors and the given thresholds.
R square(R x)
returns the square of its argument.
void axpy(Dune::BCRSMatrix< Dune::FieldMatrix< Scalar, 1, 1 > > const &P, Dune::BlockVector< Dune::FieldVector< Scalar, n > > const &x, Dune::BlockVector< Dune::FieldVector< Scalar, n > > &y, Scalar alpha=1.0)
Compute . If resetSolution=true computes .
void gridIterate(VariableList const &vars, Functions const &functions, Spaces const &spaces, Collector &f)
A function that supports general iterations over a spatial domain and supports efficient evaluation o...
void scaledTwoNormSquared(Variables const &varDesc, Functions const &f, Spaces const &spaces, Scaling const &scaling, Collector &sum)
Evaluates the square of the scaled -norms of a set of functions.
void moveEvaluatorsToCell(Evaluators &evals, Cell const &cell)
Moves all provided evaluators to the given cell.
A Collector coalescing the information of multi-variable functions on cell groups.
boost::multi_array< double, 2 > sums
Extracts the type of FE space with given index from set of spaces.