13#ifndef GOAL_ORIENTED_ERROR_ESTIMATOR_HH
14#define GOAL_ORIENTED_ERROR_ESTIMATOR_HH
21#include <boost/fusion/include/at_c.hpp>
22#include <boost/mpl/at.hpp>
34 bool biggerThanAbs(
const std::pair<double,int>& x1,
const std::pair<double,int>& x2)
36 return fabs(x1.first)>fabs(x2.first);
39 double add(
const double& x1,
const std::pair<double,int>& x2)
44 template <
template <
class,
class>
class TemplateFunctional,
class OriginalVariableSetDescription,
class ExtensionVariableSetDescription,
class ExtensionSpace>
47 static constexpr int noOfVariables = ExtensionVariableSetDescription::noOfVariables;
50 typedef TemplateFunctional<OriginalVariableSetDescription,OriginalVariableSetDescription> F_hh;
51 typedef TemplateFunctional<OriginalVariableSetDescription,ExtensionVariableSetDescription> F_he;
52 typedef TemplateFunctional<ExtensionVariableSetDescription,OriginalVariableSetDescription> F_eh;
53 typedef TemplateFunctional<ExtensionVariableSetDescription,ExtensionVariableSetDescription> F_ee;
55 static constexpr int yIdx = F_hh::yIdx;
56 static constexpr int uIdx = F_hh::uIdx;
57 static constexpr int pIdx = F_hh::lIdx;
65 typedef typename OriginalVariableSetDescription::VariableSet VariableSet;
66 typedef typename ExtensionVariableSetDescription::VariableSet ExtensionVariableSet;
68 typedef typename ExtensionVariableSetDescription::GridView::template Codim<0>::Iterator CellIterator;
71 typedef typename F_hh::Scalar
Scalar;
72 static constexpr int dim = VariableSet::Descriptions::Grid::dimension;
79 typedef typename OriginalVariableSetDescription::template CoefficientVectorRepresentation<>::type
Vector;
80 typedef typename OriginalVariableSetDescription::template CoefficientVectorRepresentation<>
VectorCreator;
81 typedef typename OriginalVariableSetDescription::template CoefficientVectorRepresentation<uIdx,uIdx+1>::type
UVector;
82 typedef typename OriginalVariableSetDescription::template CoefficientVectorRepresentation<yIdx,yIdx+1>::type
YVector;
83 typedef typename OriginalVariableSetDescription::template CoefficientVectorRepresentation<uIdx,uIdx+1>
UVectorCreator;
84 typedef typename OriginalVariableSetDescription::template CoefficientVectorRepresentation<yIdx,yIdx+1>
YVectorCreator;
87 typedef typename ExtensionVariableSetDescription::template CoefficientVectorRepresentation<>::type
ExtensionVector;
88 typedef typename ExtensionVariableSetDescription::template CoefficientVectorRepresentation<uIdx,uIdx+1>::type
UExtensionVector;
89 typedef typename ExtensionVariableSetDescription::template CoefficientVectorRepresentation<yIdx,yIdx+1>::type
YExtensionVector;
91 typedef typename ExtensionVariableSetDescription::template CoefficientVectorRepresentation<uIdx,uIdx+1>
UExtensionVectorCreator;
92 typedef typename ExtensionVariableSetDescription::template CoefficientVectorRepresentation<yIdx,yIdx+1>
YExtensionVectorCreator;
94 GoalOrientedErrorEstimator(ExtensionVariableSetDescription& extensionVariableSetDescription_, ExtensionSpace& extensionSpace_,
Scalar fraction_=0.8,
size_t maxNoOfCells_=5000,
bool onlyLowerTriangle_=
false)
95 : extensionVariableSetDescription(extensionVariableSetDescription_), extensionSpace(extensionSpace_), extensionAssembler(extensionVariableSetDescription.spaces),
96 fraction(fraction_), maxNoOfCells(maxNoOfCells_), onlyLowerTriangle(onlyLowerTriangle_)
101 void operator()(AbstractVector
const& x_, AbstractVector
const& dx_,
int step)
103 if(extensionSpace.gridManager().grid().size(0) > maxNoOfCells)
105 std::cout <<
"Skipping error estimation." << std::endl;
109 std::cout <<
"Starting error estimation." << std::endl;
121 std::cout <<
"computing residual in extension space." << std::endl;
127 ExtensionVector dx_e(ExtensionVectorCreator::init(extensionVariableSetDescription));
132 Assembler_ee assembler_ee(extensionVariableSetDescription.spaces);
133 Assembler_eh assembler_eh(extensionVariableSetDescription.spaces);
134 Assembler_he assembler_he(extensionVariableSetDescription.spaces);
136 assembler_hh.
assemble(linearization(*f_hh,x.
get()));
137 assembler_he.
assemble(linearization(*f_he,x.
get()));
138 ExtensionVariableSet x_e(extensionVariableSetDescription);
140 assembler_eh.
assemble(linearization(*f_eh,x_e));
141 assembler_ee.
assemble(linearization(*f_ee,x_e));
144 std::cout <<
"computing weight functions 1" << std::endl;
147 YVector tmpRhs_h(YVectorCreator::init(x.
get().descriptions));
148 YExtensionVector tmpRhs_e(YExtensionVectorCreator::init(extensionVariableSetDescription));
150 UVector du_h( at_c<uIdx>(dx.
get().data).coefficients() );
159 B_hh.
apply(du_h, tmpRhs_h);
162 B_he.
apply(du_h, tmpRhs_e);
172 YVector wy_h(YVectorCreator::init(x.
get().descriptions));
174 YExtensionVector wy_e(YExtensionVectorCreator::init(extensionVariableSetDescription));
179 ds_hh.
apply(tmpRhs_h, wy_h);
185 std::cout <<
"computing weight functions 2" << std::endl;
193 H_hh.
apply(wy_h,tmpRhs_h);
196 H_he.
apply(wy_h,tmpRhs_e);
202 YVector wp_h(YVectorCreator::init(x.
get().descriptions));
203 YExtensionVector wp_e(YExtensionVectorCreator::init(extensionVariableSetDescription));
212 Scalar estimatedError = 0.5 * ( at_c<0>(wy_e.data)*at_c<yIdx>(rhs_e.data)
213 + at_c<0>(du_e.data)*at_c<uIdx>(rhs_e.data)
214 + at_c<0>(wp_e.data)*at_c<pIdx>(rhs_e.data) );
216 std::cout <<
"estimated error: " << estimatedError << std::endl;
219 CellIterator cend = extensionVariableSetDescription.gridView.template end<0>();
220 std::vector<size_t> neighBoringCells(extensionVariableSetDescription.gridView.size(
dim-1),0);
221 for(CellIterator ci = extensionVariableSetDescription.gridView.template begin<0>(); ci!=cend; ++ci)
224 for(
int edgeIdInEntity=0; edgeIdInEntity<3; ++edgeIdInEntity)
226 int id = extensionVariableSetDescription.gridView.indexSet().subIndex(*ci, edgeIdInEntity,
dim-1);
227 if(
id < neighBoringCells.size())
228 neighBoringCells[id]++;
234 typename ExtensionVariableSetDescription::VariableSet mySol(extensionVariableSetDescription);
236 std::vector<Scalar> edgeErrorIndicators(at_c<yIdx>(dx_e.data).N());
237 std::cout <<
"edgeErrorIndicators: " << edgeErrorIndicators.size() << std::endl;
238 std::cout <<
"mySol: " << boost::fusion::at_c<1>(mySol.data).coefficients().N() << std::endl;
239 for(
size_t i=0; i<edgeErrorIndicators.size(); ++i)
241 edgeErrorIndicators[i] = 0.5 * ( at_c<0>(wy_e.data)[i]*at_c<yIdx>(rhs_e.data)[i] + at_c<0>(du_e.data)[0]*at_c<uIdx>(rhs_e.data)[0] + at_c<0>(wp_e.data)[i]*at_c<pIdx>(rhs_e.data)[i] );
242 boost::fusion::at_c<1>(mySol.data).coefficients()[i][0] = edgeErrorIndicators[i];
243 edgeErrorIndicators[i] /= neighBoringCells[i];
246 errorDistribution.clear();
247 errorDistribution.resize(extensionVariableSetDescription.gridView.size(0),std::make_pair(0.0,0));
248 auto const& is = extensionSpace.gridManager().grid().leafIndexSet();
261 for (CellIterator ci=extensionVariableSetDescription.gridView.template begin<0>(); ci!=cend; ++ci)
264 size_t index = is.index(*ci);
265 errorDistribution[index] = std::make_pair(0,index);
266 size_t nEdges = (
dim==2) ? 3 : 6;
267 for(
int edgeIdInEntity=0; edgeIdInEntity<nEdges; ++edgeIdInEntity)
269 typedef typename ExtensionVariableSetDescription::GridView::Grid Grid;
271 size_t globalEdgeId = Dune::GenericReferenceElements<typename Grid::ctype,Grid::dimension>::simplex().subEntity(edgeIdInEntity,Grid::dimension-1,0,Grid::dimension);
273 errorDistribution[index].first += edgeErrorIndicators[globalEdgeId];
277 totalError = std::accumulate(errorDistribution.begin(), errorDistribution.end(), 0.0,
add);
350 template <
typename... Args>
353 f_hh.reset(
new F_hh(args...));
354 f_eh.reset(
new F_eh(args...));
355 f_he.reset(
new F_he(args...));
356 f_ee.reset(
new F_ee(args...));
363 if(extensionSpace.gridManager().grid().size(0) > maxNoOfCells)
365 std::cout <<
"ERROR ESTIMATOR: Grid has more than " << maxNoOfCells <<
" cells -> No more refinement." << std::endl;
369 std::sort(errorDistribution.begin(), errorDistribution.end(),
biggerThanAbs);
371 double errLevel = errorDistribution[3*errorDistribution.size()/4].first;
372 auto const& is = extensionSpace.gridManager().grid().leafIndexSet();
374 CellIterator cend = extensionVariableSetDescription.gridView.template end<0>();
375 for (CellIterator ci=extensionVariableSetDescription.gridView.template begin<0>(); ci!=cend; ++ci)
376 if(errorDistribution[is.index(*ci)].first > errLevel) extensionSpace.gridManager().mark(1,*ci);
405 extensionSpace.gridManager().adaptAtOnce();
410 return fabs(totalError);
415 return extensionSpace.gridManager().grid().size(0);
420 ExtensionVariableSetDescription& extensionVariableSetDescription;
421 ExtensionSpace& extensionSpace;
426 bool onlyLowerTriangle;
427 std::vector<std::pair<double,size_t> > errorDistribution;
429 std::unique_ptr<F_hh> f_hh;
430 std::unique_ptr<F_eh> f_eh;
431 std::unique_ptr<F_he> f_he;
432 std::unique_ptr<F_ee> f_ee;
An adaptor presenting a Dune::LinearOperator <domain_type,range_type> interface to a contiguous sub-b...
virtual void apply(Domain const &x, Range &b) const
compute
virtual void applyscaleadd(Scalar alpha, Domain const &x, Range &b) const
Compute .
Mathematical Vector that supports copy-on-write, implements AbstractFunctionSpaceElement.
Implementation const & get() const
Access to the data.
VariationalFunctionalAssembler< ErrorEstimator > ExtensionAssembler
double estimatedAbsoluteError() const
~GoalOrientedErrorEstimator()
ExtensionVariableSetDescription::template CoefficientVectorRepresentation< uIdx, uIdx+1 > UExtensionVectorCreator
ExtensionVariableSetDescription::template CoefficientVectorRepresentation ::type ExtensionVector
bool nearMinimizer() const
OriginalVariableSetDescription::template CoefficientVectorRepresentation ::type Vector
OriginalVariableSetDescription::template CoefficientVectorRepresentation VectorCreator
OriginalVariableSetDescription::template CoefficientVectorRepresentation< yIdx, yIdx+1 > YVectorCreator
ExtensionVariableSetDescription::template CoefficientVectorRepresentation< uIdx, uIdx+1 >::type UExtensionVector
ExtensionVariableSetDescription::template CoefficientVectorRepresentation ExtensionVectorCreator
ExtensionVariableSetDescription::template CoefficientVectorRepresentation< yIdx, yIdx+1 > YExtensionVectorCreator
size_t gridSize() const final
OriginalVariableSetDescription::template CoefficientVectorRepresentation< yIdx, yIdx+1 >::type YVector
OriginalVariableSetDescription::template CoefficientVectorRepresentation< uIdx, uIdx+1 > UVectorCreator
void operator()(AbstractVector const &x_, AbstractVector const &dx_, int step)
ExtensionVariableSetDescription::template CoefficientVectorRepresentation< yIdx, yIdx+1 >::type YExtensionVector
GoalOrientedErrorEstimator(ExtensionVariableSetDescription &extensionVariableSetDescription_, ExtensionSpace &extensionSpace_, Scalar fraction_=0.8, size_t maxNoOfCells_=5000, bool onlyLowerTriangle_=false)
HierarchicErrorEstimator< LinearizationAt< F_hh >, ExtensionVariableSetDescription, ExtensionVariableSetDescription, HierarchicErrorEstimatorDetail::TakeAllD2< LinearizationAt< F_hh > > > ErrorEstimator
OriginalVariableSetDescription::template CoefficientVectorRepresentation< uIdx, uIdx+1 >::type UVector
void initializeFunctionals(Args... args)
Defines assembly of hierarchically extended problems for defining DLY style error estimators.
Dune::LinearOperator interface for inverse operators.
virtual void apply(Domain const &x, Range &y) const
Proxy class for the linearization of a nonlinear functional.
A class for assembling Galerkin representation matrices and right hand sides for variational function...
TestVariableSetDescription::template CoefficientVectorRepresentation< first, last >::type rhs() const
Returns a contiguous subrange of the rhs coefficient vectors.
void assemble(F const &f, unsigned int flags=Assembler::EVERYTHING, int nThreads=0, bool verbose=false)
Assembly without block filter or cell filter.
Error estimation via hierachic FEM.
DirectType
Available direct solvers for linear equation systems.
@ MUMPS
MUMPS sparse solver.
@ UMFPACK
UMFPack from SuiteSparse, using 32 bit integer indices.
MatrixProperties
Characterizations of sparse matrix properties.
InverseLinearOperator< DirectSolver< typename AssembledGalerkinOperator< GOP, firstRow, lastRow, firstCol, lastCol >::Domain, typename AssembledGalerkinOperator< GOP, firstRow, lastRow, firstCol, lastCol >::Range > > directInverseOperator(AssembledGalerkinOperator< GOP, firstRow, lastRow, firstCol, lastCol > const &A, DirectType directType, MatrixProperties properties)
bool biggerThanAbs(const std::pair< double, int > &x1, const std::pair< double, int > &x2)
double add(const double &x1, const std::pair< double, int > &x2)
Bridge classes that connect low level FE algorithms to higher level algorithms.
Variables and their descriptions.