13#ifndef ALGORITHM_HIERARCHIC_ERROR_ESTIMATOR_HH
14#define ALGORITHM_HIERARCHIC_ERROR_ESTIMATOR_HH
21#include <boost/timer/timer.hpp>
22#include <boost/fusion/include/at_c.hpp>
35 struct InverseOperatorResult;
36 template <
class,
int>
class FieldVector;
41 namespace HierarchicErrorEstimator_Detail{
42 bool biggerThanAbs(
const std::pair<double,int>& x1,
const std::pair<double,int>& x2)
44 return fabs(x1.first)>fabs(x2.first);
47 double add(
const double& x1,
const std::pair<double,int>& x2)
53 template <
class CorrectionVector>
58 template <
class RhsVector>
63 template <
class CorrectionVector>
70 template <
class RhsVector>
77 CorrectionVector
const& cor;
81 template <
class Functional,
class ExtensionVariableSetDescription,
class ExtensionSpace,
class NormFunctional=Functional,
template <
class>
class AdjustRHS=LeaveRHS>
84 static constexpr int yId = 1, uId = 0, pId = 2;
85 static constexpr int noOfVariables = ExtensionVariableSetDescription::noOfVariables;
88 typedef typename Functional::Scalar
Scalar;
89 typedef typename Functional::AnsatzVars::VariableSet
VariableSet;
90 static constexpr int dim = VariableSet::Descriptions::Grid::dimension;
93 typedef typename ExtensionVariableSetDescription::template CoefficientVectorRepresentation<0,2>::type
CoefficientVector02;
94 typedef typename ExtensionVariableSetDescription::template CoefficientVectorRepresentation<0,noOfVariables>::type
CoefficientVector;
95 typedef typename ExtensionVariableSetDescription::GridView::template Codim<0>::Iterator
CellIterator;
103 ExtensionSpace& extensionSpace_,
Scalar fraction=0.7,
bool verbose_=
false)
104 : f(f_), normFunctional(normFunctional_), extensionVariableSetDescription(extensionVariableSetDescription_), extensionSpace(extensionSpace_), assembler(extensionVariableSetDescription.spaces),
105 squaredFraction(fraction*fraction), verbose(verbose_)
110 void operator()(AbstractVector
const& x_, AbstractVector
const& dx_,
int step, AbstractVector
const& lowerOrderRhs)
112 if(verbose) std::cout <<
"ERROR ESTIMATOR: Start." << std::endl;
118 boost::timer::cpu_timer atimer;
120 if(verbose) std::cout <<
"ERROR ESTIMATOR: assembly time: " << boost::timer::format(atimer.elapsed(), 2,
"%t") <<
"s" << std::endl;
124 typedef typename Functional::AnsatzVars::template CoefficientVectorRepresentation<0,2>::type CorrectionVector;
126 Operator op(assembler);
130 std::vector<Scalar> tmpRepVec;
131 IstlInterfaceDetail::toVector(estRhs,tmpRepVec);
133 std::cout <<
"rhs entries:" << std::endl;
135 for(
size_t i=0; i<tmpRepVec.size(); ++i){
136 if(std::isnan(tmpRepVec[i])) std::cout <<
"nan at " << i << std::endl;
138 Scalar val = tmpRepVec[i];
139 if(val < minRhs) minRhs = val;
140 if(val > maxRhs) maxRhs = val;
143 std::cout <<
"max rhs: " << maxRhs << std::endl;
144 std::cout <<
"min rhs: " << minRhs << std::endl;
145 std::cout <<
"sum: " << rhsSum << std::endl;
149 boost::timer::cpu_timer timer;
150 CoefficientVector estSol(ExtensionVariableSetDescription::template CoefficientVectorRepresentation<0,noOfVariables>::init(extensionVariableSetDescription));
155 if(verbose) std::cout <<
"ERROR ESTIMATOR: computation time: " << boost::timer::format(timer.elapsed(), 2,
"%t") <<
"s" << std::endl;
156 typename ExtensionVariableSetDescription::VariableSet mySol(extensionVariableSetDescription);
166 for(
int i=0; i<at_c<yId>(mySol.data).size(); ++i)
168 for(
int j=0; j<at_c<yId>(mySol.data).coefficients()[i].size; ++j)
170 Scalar val = at_c<yId>(mySol.data).coefficients()[i][j];
172 if(val > maxVal) maxVal = val;
173 if(val < minVal) minVal = val;
176 std::cout <<
"y: minimal value: " << minVal << std::endl;
177 std::cout <<
"y: maximal value: " << maxVal << std::endl;
178 std::cout <<
"y: l2-sum: " << sum << std::endl;
179 Scalar tmpMax = maxVal, tmpMin = minVal, tmpSum = sum;
184 for(
int i=0; i<at_c<uId>(mySol.data).size(); ++i)
186 for(
int j=0; j<at_c<uId>(mySol.data).coefficients()[i].size; ++j)
188 Scalar val = at_c<uId>(mySol.data).coefficients()[i][0];
190 if(val > maxVal) maxVal = val;
191 if(val < minVal) minVal = val;
194 std::cout <<
"u: minimal value: " << minVal << std::endl;
195 std::cout <<
"u: maximal value: " << maxVal << std::endl;
196 std::cout <<
"u: l2-sum: " << sum << std::endl;
197 tmpMax =
std::max(tmpMax,maxVal), tmpMin =
std::min(tmpMin,minVal), tmpSum += sum;
202 for(
int i=0; i<at_c<pId>(mySol.data).size(); ++i)
204 for(
int j=0; j<at_c<pId>(mySol.data).coefficients()[i].size; ++j)
206 Scalar val = at_c<pId>(mySol.data).coefficients()[i][j];
208 if(val > maxVal) maxVal = val;
209 if(val < minVal) minVal = val;
212 std::cout <<
"p: minimal value: " << minVal << std::endl;
213 std::cout <<
"p: maximal value: " << maxVal << std::endl;
214 std::cout <<
"p: l2-sum: " << sum << std::endl;
217 std::cout <<
"minimal value: " <<
std::min(tmpMin,minVal) << std::endl;
218 std::cout <<
"maximal value: " <<
std::max(tmpMax,maxVal) << std::endl;
219 std::cout <<
"l2-sum: " << tmpSum << std::endl;
222 auto const& is = extensionSpace.gridManager().grid().leafIndexSet();
223 errorDistribution.clear();
224 errorDistribution.resize(is.size(0),std::make_pair(0.0,0));
225 Scalar errLevel(0.0), minRefine(0.2);
228 EnergyError energyError(normFunctional,x.
get(),mySol);
230 EnergyErrorAssembler eeAssembler(extensionSpace.gridManager(), energyError.getSpaces());
233 typename EnergyError::ErrorVector distError( eeAssembler.rhs() );
234 typename EnergyError::AnsatzVars::VariableSet mde( energyError.getVariableSetDescription() );
239 std::string name =
"errorDistribution_";
240 name += boost::lexical_cast<std::string>(step);
241 writeVTKFile(mde.descriptions.gridView,mde.descriptions,mde,name);
246 CellIterator cend = extensionVariableSetDescription.gridView.template end<0>();
247 for (
CellIterator ci=extensionVariableSetDescription.gridView.template begin<0>(); ci!=cend; ++ci)
248 errorDistribution[is.index(*ci)] = std::make_pair( fabs(boost::fusion::at_c<0>(mde.data).value(*ci,
Dune::FieldVector<Scalar,dim>(0.3))) , is.index(*ci));
258 Scalar bulkCriterionTolerance = squaredFraction*totalErrorSquared;
261 std::cout <<
"ERROR ESTIMATOR: totalErrorSquared: " << totalErrorSquared << std::endl;
262 std::cout <<
"ERROR ESTIMATOR: bulkCriterionTolerance: " << bulkCriterionTolerance << std::endl;
264 Scalar bulkErrorSquared = 0;
266 std::vector<std::pair<Scalar,size_t> > bulkErrorDistribution;
267 auto const& is = extensionSpace.gridManager().grid().leafIndexSet();
268 CellIterator cend = extensionVariableSetDescription.gridView.template end<0>();
270 while(bulkErrorSquared < bulkCriterionTolerance)
272 bulkErrorDistribution.push_back(errorDistribution[bulkErrorDistribution.size()]);
273 bulkErrorSquared += bulkErrorDistribution.back().first;
276 if(verbose) std::cout <<
"ERROR ESTIMATOR: number of candidates for refinement: " << bulkErrorDistribution.size() << std::endl;
306 for (
CellIterator ci=extensionVariableSetDescription.gridView.template begin<0>(); ci!=cend; ++ci)
308 for(
int i=0; i<bulkErrorDistribution.size(); ++i)
309 if(is.index(*ci) == bulkErrorDistribution[i].second)
315 extensionSpace.gridManager().mark(1,*ci);
320 extensionSpace.gridManager().adaptAtOnce();
325 return sqrt(fabs(totalErrorSquared));
330 return extensionSpace.gridManager().grid().size(0);
337 NormFunctional& normFunctional;
338 ExtensionVariableSetDescription& extensionVariableSetDescription;
339 ExtensionSpace& extensionSpace;
343 std::vector<std::pair<double,int> > errorDistribution;
An adaptor presenting a Dune::LinearOperator <domain_type,range_type> interface to a contiguous sub-b...
Mathematical Vector that supports copy-on-write, implements AbstractFunctionSpaceElement.
Implementation const & get() const
Access to the data.
Defines assembly of hierarchically extended problems for defining DLY style error estimators.
HierarchicErrorEstimator< LinearizationAt< Functional >, ExtensionVariableSetDescription, ExtensionVariableSetDescription, HierarchicErrorEstimatorDetail::TakeAllD2< LinearizationAt< Functional > > > ErrorEstimator
Functional::AnsatzVars::VariableSet VariableSet
ExtensionVariableSetDescription::template CoefficientVectorRepresentation< 0, 2 >::type CoefficientVector02
double estimatedAbsoluteError() const final
VariationalFunctionalAssembler< ErrorEstimator > Assembler
Functional::Scalar Scalar
size_t gridSize() const final
virtual ~HierarchicalBasisErrorEstimator()
ExtensionVariableSetDescription::template CoefficientVectorRepresentation< 0, noOfVariables >::type CoefficientVector
HierarchicalBasisErrorEstimator(Functional &f_, NormFunctional &normFunctional_, ExtensionVariableSetDescription &extensionVariableSetDescription_, ExtensionSpace &extensionSpace_, Scalar fraction=0.7, bool verbose_=false)
ExtensionVariableSetDescription::GridView::template Codim< 0 >::Iterator CellIterator
void operator()(AbstractVector const &x_, AbstractVector const &dx_, int step, AbstractVector const &lowerOrderRhs)
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.
@ UMFPACK
UMFPack from SuiteSparse, using 32 bit integer indices.
Dune::FieldVector< T, n > max(Dune::FieldVector< T, n > x, Dune::FieldVector< T, n > const &y)
Componentwise maximum.
Dune::FieldVector< T, n > min(Dune::FieldVector< T, n > x, Dune::FieldVector< T, n > const &y)
Componentwise minimum.
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)
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)
Bridge classes that connect low level FE algorithms to higher level algorithms.
void operator()(RhsVector &rhs)
LeaveRHS(CorrectionVector const &)
void operator()(RhsVector &rhs)
SubstractCorrection(CorrectionVector const &cor_)
Variables and their descriptions.