KASKADE 7 development version
errorEstimator2.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2002-2012 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef ALGORITHM_3_ERROR_ESTIMATOR_HH
14#define ALGORITHM_3_ERROR_ESTIMATOR_HH
15
16#include <algorithm>
17#include <iostream>
18#include <utility>
19#include <vector>
20
21#include <boost/timer/timer.hpp>
22#include <boost/fusion/include/at_c.hpp>
23
27#include "fem/variables.hh"
28#include "linalg/direct.hh"
30#include "linalg/tcg.hh"
31#include "utilities/enums.hh"
32
33// forward declarations
34namespace Dune
35{
36 struct InverseOperatorResult;
37 template <class,int> class FieldVector;
38}
39
40namespace Kaskade
41{
42 template <template <class,class,class,bool> class Functional, class VariableSetDescription, class ExtensionVariableSetDescription, class ExtensionSpace,
43 class NormFunctional, class LinearSolverLA, class LinearSolverHA, class LinearSolverLM=LinearSolverLA, class LinearSolverHM=LinearSolverHA,
44 bool lumpM=false>
45 class HierarchicalBasisErrorEstimator2 : public AbstractHierarchicalErrorEstimator
46 {
47 static constexpr int noOfVariables = ExtensionVariableSetDescription::noOfVariables;
48// typedef typename LF::AnsatzVars LVars;
49 static constexpr int yId = 1, uId = 0, pId = 2;
50 public:
51 typedef Functional<VariableSetDescription,VariableSetDescription,VariableSetDescription,lumpM> FLL;
52 typedef Functional<ExtensionVariableSetDescription,VariableSetDescription,ExtensionVariableSetDescription,lumpM> FHL;
53 typedef Functional<VariableSetDescription,ExtensionVariableSetDescription,VariableSetDescription,lumpM> FLH;
54 typedef Functional<ExtensionVariableSetDescription,ExtensionVariableSetDescription,ExtensionVariableSetDescription,lumpM> FHH;
55
56 typedef HierarchicErrorEstimator<LinearizationAt<FLL>,ExtensionVariableSetDescription,ExtensionVariableSetDescription,HierarchicErrorEstimatorDetail::TakeAllD2<LinearizationAt<FLL> > > ErrorEstimator;
58
63
69
74
79
80 typedef typename FLL::Scalar Scalar;
81 static constexpr int dim = VariableSetDescription::Grid::dimension;
84 typedef typename VariableSetDescription::template CoefficientVectorRepresentation<> CVL;
85 typedef typename VariableSetDescription::template CoefficientVectorRepresentation<yId,yId+1> CVLY;
86 typedef typename VariableSetDescription::template CoefficientVectorRepresentation<uId,uId+1> CVLU;
87 typedef typename VariableSetDescription::template CoefficientVectorRepresentation<pId,pId+1> CVLP;
88 typedef typename CVL::type CoefficientVectorL;
89 typedef typename CVLY::type CoefficientVectorLY;
90 typedef typename CVLU::type CoefficientVectorLU;
91 typedef typename CVLP::type CoefficientVectorLP;
92 typedef typename ExtensionVariableSetDescription::template CoefficientVectorRepresentation<0,1>::type CoefficientVectorH01;
93 typedef typename ExtensionVariableSetDescription::template CoefficientVectorRepresentation<0,2>::type CoefficientVectorH02;
94 typedef typename ExtensionVariableSetDescription::template CoefficientVectorRepresentation<2,3> CVH23;
95 typedef typename ExtensionVariableSetDescription::template CoefficientVectorRepresentation<2,3>::type CoefficientVectorH23;
96 typedef typename ExtensionVariableSetDescription::template CoefficientVectorRepresentation<> CVH;
97 typedef typename ExtensionVariableSetDescription::template CoefficientVectorRepresentation<yId,yId+1> CVHY;
98 typedef typename ExtensionVariableSetDescription::template CoefficientVectorRepresentation<uId,uId+1> CVHU;
99 typedef typename ExtensionVariableSetDescription::template CoefficientVectorRepresentation<pId,pId+1> CVHP;
100 typedef typename CVH::type CoefficientVectorH;
101 typedef typename CVHY::type CoefficientVectorHY;
102 typedef typename CVHU::type CoefficientVectorHU;
103 typedef typename CVHP::type CoefficientVectorHP;
104 typedef typename ExtensionVariableSetDescription::GridView::template Codim<0>::Iterator CellIterator;
105
107
108 // HierarchicalBasisErrorEstimator(Functional& f_, LF& lf_, LVars& lvars, ExtensionVariableSetDescription& extensionVariableSetDescription_, ExtensionSpace& extensionSpace_, Scalar fraction=0.7, bool verbose_=false)
109 // : f(f_), lf(lf_), normFunctional(f_), extensionVariableSetDescription(extensionVariableSetDescription_), extensionSpace(extensionSpace_), assembler(extensionVariableSetDescription.spaces),
110 // squaredFraction(fraction*fraction), verbose(verbose_)
111 // {}
112
113 HierarchicalBasisErrorEstimator2(NormFunctional& normFunctional_, VariableSetDescription& variableSetDescription_, ExtensionVariableSetDescription& extensionVariableSetDescription_,
114 ExtensionSpace& extensionSpace_, Scalar fraction=0.7, bool verbose_=false)
115 : /*lf(lf_), lvars(lvars_),*/ normFunctional(normFunctional_), variableSetDescription(variableSetDescription_), extensionVariableSetDescription(extensionVariableSetDescription_),
116 extensionSpace(extensionSpace_), assembler(extensionVariableSetDescription.spaces),
117 squaredFraction(fraction*fraction), verbose(verbose_)
118 {
119 initAssemblers();
120 }
121
123
124 void operator()(AbstractVector const& x_, AbstractVector const& dx_, int step, AbstractVector const&)
125 {
126 using boost::fusion::at_c;
127 if(verbose) std::cout << "ERROR ESTIMATOR: Start." << std::endl;
128 Bridge::Vector<VarSet> const& xl = dynamic_cast<const Bridge::Vector<VarSet>&>(x_);
129 Bridge::Vector<ExtVarSet> xe(extensionVariableSetDescription);
130 assembleAt(xl,xe);
131 Bridge::Vector<VarSet> const& dxl = dynamic_cast<const Bridge::Vector<VarSet>&>(dx_);
132
133 // estimate error in dual variable
134 typename ExtensionVariableSetDescription::VariableSet tmpRep(extensionVariableSetDescription), mySol(extensionVariableSetDescription);
135 typename VariableSetDescription::VariableSet tmpRepL(variableSetDescription);
136
137 All all(*assll,false);
138 Ahh ahh(*asshh,false);
139 Alh alh(*asslh,false);
140
141 Matrix mAll = all.template get<Matrix>();
142 mAll.transpose();
143 Matrix mAhh = ahh.template get<Matrix>();
144 mAhh.transpose();
145 Matrix mAlh = alh.template get<Matrix>();
146 mAlh.transpose();
147
148 tmpRep *= 0;
149 at_c<yId>(tmpRep.data) = at_c<0>(rhs0.data);
150// //MyH1SemiNorm myNorm;
151// //std::cout << "||rhs||=" << myNorm(tmpRep) << std::endl;
152// LinearSolverHA(stateOp,DirectType::MUMPS,MatrixProperties::GENERAL).apply(sol0,rhs0);
153// //directInverseOperator(stateOp,DirectType::MUMPS,MatrixProperties::GENERAL).apply(rhs0,sol0);
154// tmpRep *= 0;
155 at_c<pId>(tmpRep.data) = at_c<0>(sol0.data);
156 at_c<pId>(mySol.data) = at_c<0>(sol0.data);
157 //std::cout << "||sol||=" << myNorm(tmpRep) << std::endl;
158
159 // estimate error in control variable
160 // rhs vectors
161 CoefficientVectorLU rhs1L(asshl->template rhs<uId,uId+1>());
162 CoefficientVectorHU rhs1H(asshh->template rhs<uId,uId+1>());
163 Bhl bhl(*asshl,false);
164 Blh blh(*asslh,false);
165 Bll bll(*assll,false);
166 Bhh bhh(*asshh,false);
167
168 MatrixAsTriplet<Scalar> tmpmat = blh.template get<MatrixAsTriplet<Scalar> >();
169 std::vector<Scalar> tmpx, tmpy;
170 IstlInterfaceDetail::toVector(sol0,tmpy);
171 IstlInterfaceDetail::toVector(rhs1L,tmpx);
172 tmpmat.usmtv(-1.0,tmpy,tmpx);
173 IstlInterfaceDetail::fromVector(tmpx,rhs1L);
174 //std::cout << "rhs1L=" << at_c<0>(rhs1L.data)[0] << std::endl;
175
176 tmpx.clear(); tmpy.clear();
177 IstlInterfaceDetail::toVector(sol0,tmpy);
178 IstlInterfaceDetail::toVector(rhs1H,tmpx);
179 tmpmat = bhh.template get<MatrixAsTriplet<Scalar> >();
180 tmpmat.usmtv(-1.0,tmpy,tmpx);
181 IstlInterfaceDetail::fromVector(tmpx,rhs1H);
182 // bophh.applyscaleadd(-1.0,sol0,rhs1H);
183 //std::cout << "rhs1H=" << at_c<0>(rhs1H.data)[0] << std::endl;
184
185 Muuhh muuhh(*asshh,true);
186 Muull muull(*assll,true);
187 Muulh muulh(*asslh,true);
188 CoefficientVectorHU solhu(CVHU::init(extensionVariableSetDescription));
189 CoefficientVectorLU sollu(CVLU::init(variableSetDescription));
190 LinearSolverLM(muull,DirectType::MUMPS,MatrixProperties::GENERAL).apply(sollu,rhs1L);
191 //directInverseOperator(muull,DirectType::MUMPS,MatrixProperties::GENERAL).apply(rhs1L,sollu);
192 muulh.applyscaleadd(-1.0,sollu,rhs1H);
193 LinearSolverHM(muuhh,DirectType::MUMPS,MatrixProperties::GENERAL).apply(solhu,rhs1H);
194 // directInverseOperator(muuhh,DirectType::MUMPS,MatrixProperties::GENERAL).apply(rhs1H,solhu);
195
196 //std::cout << "sollu=" << at_c<0>(sollu.data)[0] << std::endl;
197 //std::cout << "solhu=" << at_c<0>(solhu.data)[0] << std::endl;
198 at_c<uId>(mySol.data) = at_c<0>(solhu.data);
199
200 // estimate error in state variable
201 CoefficientVectorLY solly(CVLP::init(variableSetDescription));
202 CoefficientVectorHY solhy(CVHP::init(extensionVariableSetDescription));
203 CoefficientVectorLP rhslp(assll->template rhs<pId,pId+1>());
204 CoefficientVectorHP rhshp(asshh->template rhs<pId,pId+1>());
205
206 bll.applyscaleadd(-1.0,sollu,rhslp);
207 blh.applyscaleadd(-1.0,sollu,rhshp);
208 bhl.applyscaleadd(-1.0,solhu,rhslp);
209 bhh.applyscaleadd(-1.0,solhu,rhshp);
210
211 LinearSolverLA(all,DirectType::MUMPS,MatrixProperties::GENERAL).apply(solly,rhslp);
212 //directInverseOperator(all,DirectType::MUMPS,MatrixProperties::GENERAL).apply(rhslp,solly);
213 alh.applyscaleadd(-1.0,solly,rhshp);
214 LinearSolverHA(ahh,DirectType::MUMPS,MatrixProperties::GENERAL).apply(solhy,rhshp);
215 // directInverseOperator(ahh,DirectType::MUMPS,MatrixProperties::GENERAL).apply(rhshp,solhy);
216 at_c<yId>(mySol.data) = at_c<0>(solhy.data);
217
218 // Transfer error indicators to cells.
219 auto const& is = extensionSpace.gridManager().grid().leafIndexSet();
220 errorDistribution.clear();
221 errorDistribution.resize(is.size(0),std::make_pair(0.0,0));
222 Scalar errLevel(0.0), minRefine(0.2);
223
225 EnergyError energyError(normFunctional,xl.get(),mySol);
227 EnergyErrorAssembler eeAssembler(extensionSpace.gridManager(), energyError.getSpaces());
228
229 eeAssembler.assemble(linearization(energyError,xl.get()), EnergyErrorAssembler::RHS);
230 typename EnergyError::ErrorVector distError( eeAssembler.rhs() );
231 typename EnergyError::AnsatzVars::VariableSet mde( energyError.getVariableSetDescription() );
232 mde = distError;
233 //std::cout << "||mde||=" << myNorm(mde) << std::endl;
234
235 if(verbose)
236 {
237 std::string name = "errorDistribution_";
238 name += boost::lexical_cast<std::string>(step);
239 writeVTKFile(mde.descriptions.gridView,mde.descriptions,mde,name);
240 }
241 // Fehler bzgl L_xx : ( sum_i estSol_i * (L_xx * estSol)_i )^{1/2} (nur die x - Komponente, nicht p)
242 // Lokalisierung: einfachste Idee: v_i = estSol_i * (L_xx * estSol)_i
243 // Aufteilen von v_i auf die einzelnen Zellen
244 CellIterator cend = extensionVariableSetDescription.gridView.template end<0>();
245 for (CellIterator ci=extensionVariableSetDescription.gridView.template begin<0>(); ci!=cend; ++ci)
246 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));
247
248 totalErrorSquared = std::accumulate(errorDistribution.begin(), errorDistribution.end(), 0.0, HierarchicErrorEstimator_Detail::add);
249 }
250
252 {
253// int lastIndexForDoubleRefinement = -1;
254 std::sort(errorDistribution.begin(), errorDistribution.end(), HierarchicErrorEstimator_Detail::biggerThanAbs);
255
256 Scalar bulkCriterionTolerance = squaredFraction*totalErrorSquared;
257 if(verbose)
258 {
259 std::cout << "ERROR ESTIMATOR: totalErrorSquared: " << totalErrorSquared << std::endl;
260 std::cout << "ERROR ESTIMATOR: bulkCriterionTolerance: " << bulkCriterionTolerance << std::endl;
261 }
262 Scalar bulkErrorSquared = 0;
263
264 std::vector<std::pair<Scalar,size_t> > bulkErrorDistribution;
265 auto const& is = extensionSpace.gridManager().grid().leafIndexSet();
266 CellIterator cend = extensionVariableSetDescription.gridView.template end<0>();
267
268 while(bulkErrorSquared < bulkCriterionTolerance)
269 {
270 bulkErrorDistribution.push_back(errorDistribution[bulkErrorDistribution.size()]);
271 bulkErrorSquared += bulkErrorDistribution.back().first;
272 }
273
274 if(verbose) std::cout << "ERROR ESTIMATOR: number of candidates for refinement: " << bulkErrorDistribution.size() << std::endl;
275
276// size_t lastIndex = bulkErrorDistribution.size()-1;
277// size_t firstIndex = 0;
278
279 /*while(firstIndex+3 < lastIndex)
280 {
281 if(bulkErrorDistribution.size() > 4)
282 {
283 Scalar firstContribution = bulkErrorDistribution[firstIndex].first;
284 firstContribution *= 15.0/256.0;
285
286 Scalar lastContributions = bulkErrorDistribution[lastIndex--].first;
287 lastContributions += bulkErrorDistribution[lastIndex--].first;
288 lastContributions += bulkErrorDistribution[lastIndex].first;
289 lastContributions *= 15.0/16.0;
290
291 if(lastContributions < firstContribution)
292 {
293 ++lastIndexForDoubleRefinement;
294 ++firstIndex;
295 --lastIndex;
296 bulkErrorDistribution.erase(bulkErrorDistribution.end()-3, bulkErrorDistribution.end());
297 }
298 else break;
299 }
300 }*/
301
302
303 // Refine mesh.
304 for (CellIterator ci=extensionVariableSetDescription.gridView.template begin<0>(); ci!=cend; ++ci) // iterate over cells
305 {
306 for(int i=0; i<bulkErrorDistribution.size(); ++i) // iterate over chosen part of the error distribution
307 if(is.index(*ci) == bulkErrorDistribution[i].second)
308 {
309// if(i <= lastIndexForDoubleRefinement) {
310// extensionSpace.gridManager().mark(2,*ci);
311// }
312// else{
313 extensionSpace.gridManager().mark(1,*ci);
314// }
315 }
316 }
317
318 extensionSpace.gridManager().adaptAtOnce();
319 }
320
321 double estimatedAbsoluteError() const final
322 {
323 return sqrt(fabs(totalErrorSquared));
324 }
325
326 size_t gridSize() const final
327 {
328 return extensionSpace.gridManager().grid().size(0);
329 }
330
331 template <typename... Args>
332 void initFunctionals(const Args&... args)
333 {
334 Fll.reset(new FLL(args...));
335 Flh.reset(new FLH(args...));
336 Fhl.reset(new FHL(args...));
337 Fhh.reset(new FHH(args...));
338 }
339
340 private:
341 void initAssemblers()
342 {
343 assll.reset(new Assll(extensionVariableSetDescription.spaces));
344 asslh.reset(new Asslh(extensionVariableSetDescription.spaces));
345 asshl.reset(new Asshl(extensionVariableSetDescription.spaces));
346 asshh.reset(new Asshh(extensionVariableSetDescription.spaces));
347 }
348
349 void assembleAt(const Bridge::Vector<VarSet>& xl, const Bridge::Vector<ExtVarSet>& xe)
350 {
351 assll->assemble(linearization(*Fll,xl.get()));
352 asslh->assemble(linearization(*Flh,xl.get()));
353 asshl->assemble(linearization(*Fhl,xe.get()));
354 asshh->assemble(linearization(*Fhh,xe.get()));
355 }
356
357 std::unique_ptr<FLL> Fll;
358 std::unique_ptr<FLH> Flh;
359 std::unique_ptr<FHL> Fhl;
360 std::unique_ptr<FHH> Fhh;
361 std::unique_ptr<Assll> assll;
362 std::unique_ptr<Asslh> asslh;
363 std::unique_ptr<Asshl> asshl;
364 std::unique_ptr<Asshh> asshh;
365// LF& lf;
366// LVars& lvars;
367 NormFunctional& normFunctional;
368 VariableSetDescription& variableSetDescription;
369 ExtensionVariableSetDescription& extensionVariableSetDescription;
370 ExtensionSpace& extensionSpace;
371 Assembler assembler;
372 Scalar squaredFraction;
373 Scalar totalErrorSquared;
374 std::vector<std::pair<double,int> > errorDistribution;
375 bool verbose;
376 // AdjustRHS<CoefficientVector> adjustRHS;
377 };
378}
379
380#endif
An adaptor presenting a Dune::LinearOperator <domain_type,range_type> interface to a contiguous sub-b...
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.
Defines assembly of hierarchically extended problems for defining DLY style error estimators.
ExtensionVariableSetDescription::template CoefficientVectorRepresentation< 2, 3 > CVH23
AssembledGalerkinOperator< Asshh, pId, pId+1, yId, yId+1 > Ahh
AssembledGalerkinOperator< Asshl, pId, pId+1, yId, yId+1 > Ahl
void operator()(AbstractVector const &x_, AbstractVector const &dx_, int step, AbstractVector const &)
VariableSetDescription::template CoefficientVectorRepresentation CVL
VariableSetDescription::template CoefficientVectorRepresentation< uId, uId+1 > CVLU
AssembledGalerkinOperator< Asshh, pId, pId+1, uId, uId+1 > Bhh
ExtensionVariableSetDescription::template CoefficientVectorRepresentation< uId, uId+1 > CVHU
AssembledGalerkinOperator< Asshl, pId, pId+1, uId, uId+1 > Bhl
Functional< VariableSetDescription, ExtensionVariableSetDescription, VariableSetDescription, lumpM > FLH
VariableSetDescription::template CoefficientVectorRepresentation< pId, pId+1 > CVLP
void initFunctionals(const Args &... args)
AssembledGalerkinOperator< Asshh, uId, uId+1, uId, uId+1 > Muuhh
VariationalFunctionalAssembler< LinearizationAt< FLH > > Asslh
VariableSet< VariableSetDescription > VarSet
AssembledGalerkinOperator< Asslh, uId, uId+1, uId, uId+1 > Muulh
AssembledGalerkinOperator< Asslh, pId, pId+1, yId, yId+1 > Alh
ExtensionVariableSetDescription::template CoefficientVectorRepresentation CVH
AssembledGalerkinOperator< Asshl, uId, uId+1, uId, uId+1 > Muuhl
ExtensionVariableSetDescription::template CoefficientVectorRepresentation< pId, pId+1 > CVHP
AssembledGalerkinOperator< Asslh, pId, pId+1, uId, uId+1 > Blh
ExtensionVariableSetDescription::GridView::template Codim< 0 >::Iterator CellIterator
AssembledGalerkinOperator< Asshh, yId, yId+1, yId, yId+1 > Myyhh
VariationalFunctionalAssembler< LinearizationAt< FHL > > Asshl
HierarchicErrorEstimator< LinearizationAt< FLL >, ExtensionVariableSetDescription, ExtensionVariableSetDescription, HierarchicErrorEstimatorDetail::TakeAllD2< LinearizationAt< FLL > > > ErrorEstimator
Functional< ExtensionVariableSetDescription, ExtensionVariableSetDescription, ExtensionVariableSetDescription, lumpM > FHH
HierarchicalBasisErrorEstimator2(NormFunctional &normFunctional_, VariableSetDescription &variableSetDescription_, ExtensionVariableSetDescription &extensionVariableSetDescription_, ExtensionSpace &extensionSpace_, Scalar fraction=0.7, bool verbose_=false)
VariationalFunctionalAssembler< ErrorEstimator > Assembler
ExtensionVariableSetDescription::template CoefficientVectorRepresentation< 0, 1 >::type CoefficientVectorH01
AssembledGalerkinOperator< Assll, pId, pId+1, yId, yId+1 > All
ExtensionVariableSetDescription::template CoefficientVectorRepresentation< 2, 3 >::type CoefficientVectorH23
Functional< VariableSetDescription, VariableSetDescription, VariableSetDescription, lumpM > FLL
AssembledGalerkinOperator< Assll, pId, pId+1, uId, uId+1 > Bll
VariationalFunctionalAssembler< LinearizationAt< FHH > > Asshh
VariableSetDescription::template CoefficientVectorRepresentation< yId, yId+1 > CVLY
Functional< ExtensionVariableSetDescription, VariableSetDescription, ExtensionVariableSetDescription, lumpM > FHL
VariationalFunctionalAssembler< LinearizationAt< FLL > > Assll
VariableSet< ExtensionVariableSetDescription > ExtVarSet
ExtensionVariableSetDescription::template CoefficientVectorRepresentation< yId, yId+1 > CVHY
AssembledGalerkinOperator< Assll, uId, uId+1, uId, uId+1 > Muull
ExtensionVariableSetDescription::template CoefficientVectorRepresentation< 0, 2 >::type CoefficientVectorH02
MatrixAsTriplet & transpose()
Transposition.
Definition: triplet.hh:453
void usmtv(Scalar const alpha, X const &x, Y &y) const
Matrix-vector multiplication.
Definition: triplet.hh:296
A class that stores information about a set of variables.
Definition: variables.hh:537
A class for storing a heterogeneous collection of FunctionSpaceElement s.
Definition: variables.hh:341
Error estimation via hierachic FEM.
@ MUMPS
MUMPS sparse solver.
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.