KASKADE 7 development version
celldata.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-2011 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 CELLDATA_HH
14#define CELLDATA_HH
15
16#include "fem/lagrangespace.hh"
17
18namespace Kaskade
19{
35 template<class Grd, class T=Dune::FieldVector<double,1> >
37 {
38 public:
39 typedef Grd Grid;
40 typedef T ValueType;
41 typedef typename Grid::template Codim<0>::Entity Cell;
43 typedef std::pair<T, typename Cell::EntityPointer> CellDataPair;
44 typedef std::vector<CellDataPair> CellDataVector;
45 static int const dim = Grid::dimension;
46
47 private:
48
49 struct GreaterFirst
50 {
51 bool operator() (CellDataPair const & p1, CellDataPair const & p2) { return fabs(p1.first) > fabs(p2.first);}
52 };
53
54 public:
56 CellData() : data(), sorted(false), maxed(false), corrEst(false) {};
57
59 explicit CellData(CellDataVector const& data_) : data(data_), sorted(false), maxed(false) {}
60
62 template<class S>
63 explicit CellData(std::vector< std::pair<S, typename Cell::EntityPointer> > const& data_) : sorted(false), maxed(false)
64 {
65 for(int i=0; i<data_.size(); ++i){
66 data.push_back(std::make_pair(data_[i].first,data_[i].second));
67 }
68 }
69
70 template<class Space, class Vector>
71 explicit CellData(Vector const& data_,Space const& ds) : data(), sorted(false), maxed(false)
72 {
73 Space dspace(ds.gridManager(), ds.gridView(), 0);
74 typename Space::Evaluator isfs(dspace);
75 typedef typename Space::GridView::template Codim<0>::Iterator CellIterator;
76 T fv;
77 for (CellIterator ci=dspace.gridView().template begin<0>();
78 ci!=dspace.gridView(). template end<0>(); ++ci)
79 {
80 isfs.moveTo(*ci);
81 fv[0]=T(data_[isfs.globalIndices()[0]]);
82 data.push_back(std::make_pair(fv,ci));
83 }
84 }
85
88 {
89 data = ei.data;
90 sorted=ei.sorted;
91 maxed=false;
92 return *this;
93 }
94
97 {
98 data=ei;
99 sorted=false;
100 maxed=false;
101 return *this;
102 }
103
105 double sum() const
106 {
107 double totalErrorSq=0.0;
108 for(int i=0; i<data.size(); ++i)
109 totalErrorSq+=data[i].first;
110 return totalErrorSq;
111 }
112
114 double abssum() const
115 {
116 double totalErrorSq=0.0;
117 for(int i=0; i<data.size(); ++i)
118 totalErrorSq+=fabs(data[i].first);
119 return totalErrorSq;
120 }
121
123 void sort()
124 {
125 if(!sorted) std::sort(data.begin(),data.end(),GreaterFirst());
126 sorted=true;
127 }
128
130 double maxElement() const
131 {
132 if(maxed) return maxEle;
133 maxed=true;
134 if(sorted)
135 {
136 maxEle=fabs(data[0].first);
137 return maxEle;
138 }
139 maxEle=-1e300;
140 for(int i=0; i<data.size(); i++)
141 maxEle = std::max(maxEle,fabs(data[i].first));
142 return maxEle;
143 }
144
147 {
148 double m=maxElement();
149 for(int i=0; i<data.size(); i++) data[i].first *= 1.0/m;
150 maxEle=1.0;
151 }
152
153 // Routines for reading the data. Designed to allow flexibility in the choice of the container
154
156 typename CellDataVector::const_iterator begin() const {return data.begin();}
157
159 typename CellDataVector::const_iterator end() const {return data.end();}
160
162 typename CellDataVector::iterator begin() {return data.begin();}
163
165 typename CellDataVector::iterator end() {return data.end();}
166
168 template<typename FSElement>
169 void toFunction(FSElement& fse) const
170 {
171 typedef typename FSElement::Space ImageSpace;
172
174 std::vector<typename Grid::ctype> volume(fse.space().degreesOfFreedom(),0.0);
175 *fse = typename ImageSpace::Mapper::ShapeFunctionSet::Scalar(0.0);
176
177 typename ImageSpace::Evaluator isfs(fse.space());
178 for(int i=0; i<data.size(); i++)
179 {
180 typename CellDataPair::second_type ci(data[i].second);
181 isfs.moveTo(*ci);
182 typename Grid::ctype myvolume(ci->geometry().volume());
183 for (int j=0; j<isfs.size(); ++j) volume[isfs.globalIndices()[j]]+=myvolume;
184 localCoefficients.setSize(isfs.size(),1);
185 for (int j=0; j<isfs.size(); ++j)
186 {
187 localCoefficients[j][0]=data[i].first;
188 localCoefficients[j][0] *= myvolume;
189 (*fse)[isfs.globalIndices()[j]]+= localCoefficients[j][0];
190 }
191 }
192 for(int i=0; i<volume.size(); ++i) (*fse)[i]/=volume[i];
193 }
194
195 void setCorrectedEstimate(double est)
196 {
197 corrEst=true;
198 correctedestimate=est;
199 }
200
201 double getCorrectedEstimate() const
202 {
203 return correctedestimate;
204 }
205
206 bool estIsCorrected() const
207 {
208 return corrEst;
209 }
210
211
212 private:
213 CellDataVector data;
214 mutable bool sorted,maxed;
215 mutable T maxEle;
216 bool corrEst;
217 double correctedestimate;
218 };
219
220
222
223 template<class Grid, class T>
225 {
226 double partialErrorSq(0.0), pSum(0.0);
227 ic.sort();
228 double totalErrorSq=ic.abssum();
229 Theta*=Theta;
232 for(iind=ic.begin(); iind != ic.end(); iind++)
233 {
234 typename CellData<Grid,int>::CellDataPair markpair(0,iind->second);
235 if(partialErrorSq <= Theta*totalErrorSq)
236 {
237 markpair.first=1;
238 partialErrorSq += fabs(iind->first);
239 pSum += iind->first;
240 }
241 iv.insert(iv.end(),markpair);
242 }
243 //std::cout << "Expected change:" << sqrt(fabs(pSum)) << " relative:" << sqrt(fabs(1-(ic.sum()-(1-0.25*0.25)*pSum)/ic.sum())) << std::endl;
244 return iv;
245 }
246
248
249 template<class Grid, class T>
250 void deleteAllAboveLevel(CellData<Grid,T>& ic, int maxlevel)
251 {
253 for(iind=ic.begin(); iind != ic.end(); iind++)
254 {
255 if(iind->second->level() > maxlevel) iind->first = 0.0;
256 }
257 }
258
260 // /** To be used together with GridManager::mark(...)*/
261 template<class Grid, class T>
263 {
264 double m=ic.maxElement();
265 Ratio*=Ratio;
268 for(iind=ic.begin(); iind!=ic.end();++iind)
269 {
270 typename CellData<Grid,int>::CellDataPair markpair(0,iind->second);
271 if(fabs(iind->first) >= Ratio*m) markpair.first=1;
272 iv.insert(iv.end(),markpair);
273 }
274 return iv;
275 }
276
278 // /** To be used together with GridManager::mark(...)*/
279 template<class Grid, class T>
280 typename CellData<Grid,int>::CellDataVector markWithinInterval(CellData<Grid,T>& ic, double lower, double upper, int times)
281 {
284 for(iind=ic.begin(); iind!=ic.end();++iind)
285 {
286 typename CellData<Grid,int>::CellDataPair markpair(0,iind->second);
287 if(iind->first>=lower && iind->first <=upper) markpair.first=times;
288 iv.insert(iv.end(),markpair);
289 }
290 return iv;
291 }
292
294 // /** To be used together with GridManager::mark(...)*/
295 template<class Grid>
297 {
300 for(iind=ic.begin(); iind!=ic.end();++iind)
301 {
302 int isWithin=0;
303 typename CellData<Grid,int>::CellDataPair markpair(*iind);
304 Dune::FieldVector<typename Grid::ctype,Grid::dimension> difference=markpair.second->geometry().center()-center;
305 if(difference.two_norm() <= radius) isWithin = 1;
306 markpair.first *= isWithin;
307 iv.insert(iv.end(),markpair);
308 }
309 return iv;
310 }
311
313
315 template<class Indicator>
316 Indicator errorL2(Indicator const& indi)
317 {
318 const int dim(Indicator::dim);
319 typename Indicator::CellDataVector iv;
320
322 for(iind=indi.begin(); iind != indi.end(); iind++)
323 {
325 markpair(iind->first[0]*std::pow(iind->second->geometry().volume(),2.0/dim),iind->second);
326 iv.insert(iv.end(),markpair);
327 }
328 return Indicator(iv);
329 }
330
331
332 template<typename FSElement>
333 void extendMarks(FSElement& fse, FSElement const& fu, int neighboursForMarking)
334 {
335 typedef typename FSElement::Space ImageSpace;
336 typedef typename ImageSpace::Grid Grid;
337 typedef typename Grid::template Codim<0>::Entity Entity;
338
340 *fse = typename ImageSpace::Mapper::ShapeFunctionSet::Scalar(0.0);
341
342 typedef typename ImageSpace::IndexSet::template Codim<0>::template Partition<Dune::All_Partition>::Iterator CellIterator;
343 typename ImageSpace::Evaluator isfs(fse.space());
344 typename FSElement::Space::Evaluator dsfs(fu.space());
345 for (CellIterator ci=fse.space().indexSet().template begin<0,Dune::All_Partition>();
346 ci!=fse.space().indexSet().template end<0,Dune::All_Partition>(); ++ci)
347 {
348 isfs.moveTo(*ci);
349 dsfs.moveTo(*ci);
350
351 std::vector<Dune::FieldVector<typename FSElement::Space::Grid::ctype, FSElement::Space::dim> >const &
352 iNodes(isfs.shapeFunctions().interpolationNodes());
353
354 localCoefficients.setSize(iNodes.size(),1);
355 dsfs.evaluateAt(iNodes[0]);
356 if(fu.value(dsfs) > 0.5)
357 (*fse)[isfs.globalIndices()[0]]= neighboursForMarking;
358 typename Entity::LeafIntersectionIterator faceEnd = ci->ileafend();
359 for (typename Entity::LeafIntersectionIterator face=ci->ileafbegin(); face!=faceEnd; ++face) {
360 if (!face.neighbor())
361 continue; // interior face
362 dsfs.moveTo(*face.outside());
363 dsfs.evaluateAt(iNodes[0]);
364 if(fu.value(dsfs) > 0.5)
365 (*fse)[isfs.globalIndices()[0]]+=1.0;
366 }
367 }
368 for(int i=0; i<(*fse).size(); ++i)
369 if((*fse)[i] >= neighboursForMarking) (*fse)[i]=1.0;
370
371 }
372
373 namespace Bridge
374 {
375
376 template<class ErrorEst, class GridMan>
377 ErrorEst extendMarkings(ErrorEst const& orig, GridMan & gridMan)
378 {
380 typedef typename PWCSpace::template Element<1>::type FSE;
381
382 std::cout << "Before Extension:" << orig.getData().sum() << std::endl;
383
384 PWCSpace pwc(gridMan,gridMan.grid().leafIndexSet(),0);
385 FSE marks(pwc),smark(pwc);
386 std::vector<double> vmark;
387 {
388 orig.getData().toFunction(marks);
389 extendMarks(smark,marks,2);
390 for(int i=0; i<(*smark).size();++i)
391 vmark.push_back((*smark)[i][0]);
392 }
393 ErrorEst extmarks(CellData<typename GridMan::Grid, Dune::FieldVector<double, 1> >(vmark,pwc));
394 std::cout << "After Extension:" << extmarks.getData().sum() << std::endl;
395 return extmarks;
396 }
397 }
398
399/* class AbstractErrorEstimate;
400
401 template<class Grid, class T=Dune::FieldVector<double,1> >
402 class CellDataErrorEstimate : public AbstractErrorEstimate
403 {
404 public:
405 static int const dim = CellData<Grid,T>::dim;
406
407 CellDataErrorEstimate(CellData<Grid,T> cd_) : cd(cd_) {};
408
409 virtual double absoluteError() const {
410 if(cd.estIsCorrected())
411 return cd.getCorrectedEstimate();
412 else
413 return std::sqrt(fabs(cd.abssum())); }
414
415 CellData<Grid,T>& getData() { return cd; }
416 CellData<Grid,T> const& getData() const { return cd; }
417 private:
418 CellData<Grid,T> cd;
419 };*/
420} /* end of namespace Kaskade */
421
422#endif
Class that stores information for each cell of a grid.
Definition: celldata.hh:37
bool estIsCorrected() const
Definition: celldata.hh:206
CellDataVector::const_iterator end() const
Return a const_iterator on data.end()
Definition: celldata.hh:159
CellData(CellDataVector const &data_)
Construct Cell Data from a given CellDataVector.
Definition: celldata.hh:59
void setCorrectedEstimate(double est)
Definition: celldata.hh:195
CellDataVector::iterator end()
Return a const_iterator on data.end()
Definition: celldata.hh:165
CellData< Grid, T > & operator=(const CellDataVector &ei)
Assignment operator between two CellDataVector.
Definition: celldata.hh:96
void normalize()
Scale all entries, such that their maximal value is 1.
Definition: celldata.hh:146
std::pair< T, typename Cell::EntityPointer > CellDataPair
A pair, where the data and an EntityPointer is stored.
Definition: celldata.hh:43
static int const dim
Definition: celldata.hh:45
CellDataVector::iterator begin()
Return a const_iterator on data.begin()
Definition: celldata.hh:162
double abssum() const
Compute the sum over the absolute values of all entries.
Definition: celldata.hh:114
CellData()
Default constructor.
Definition: celldata.hh:56
CellData(std::vector< std::pair< S, typename Cell::EntityPointer > > const &data_)
Construct Cell Data from a given CellDataVector of different scalar type.
Definition: celldata.hh:63
double sum() const
Compute the sum over all entries.
Definition: celldata.hh:105
double maxElement() const
Compute the maximal value of all entries.
Definition: celldata.hh:130
void toFunction(FSElement &fse) const
Create a function space element from *this.
Definition: celldata.hh:169
Grid::template Codim< 0 >::Entity Cell
Definition: celldata.hh:41
void sort()
Sort all entries from large to small.
Definition: celldata.hh:123
CellDataVector::const_iterator begin() const
Return a const_iterator on data.begin()
Definition: celldata.hh:156
CellData< Grid, T > & operator=(const CellData< Grid, T > &ei)
Assignment operator from a CellData object.
Definition: celldata.hh:87
std::vector< CellDataPair > CellDataVector
Definition: celldata.hh:44
CellData(Vector const &data_, Space const &ds)
Definition: celldata.hh:71
double getCorrectedEstimate() const
Definition: celldata.hh:201
A representation of a finite element function space defined over a domain covered by a grid.
Dune::FieldVector< T, n > max(Dune::FieldVector< T, n > x, Dune::FieldVector< T, n > const &y)
Componentwise maximum.
Definition: fixdune.hh:110
Lagrange Finite Elements.
ErrorEst extendMarkings(ErrorEst const &, GridMan &)
Definition: celldata.hh:377
Dune::FieldVector< Scalar, dim > Vector
Indicator errorL2(Indicator const &indi)
Scale error indicators for H^1, such that L_2 indicators are the result.
Definition: celldata.hh:316
void deleteAllAboveLevel(CellData< Grid, T > &ic, int maxlevel)
Delete all error entries of entities which are above a certain level.
Definition: celldata.hh:250
CellData< Grid, int >::CellDataVector markByBulkCriterion(CellData< Grid, T > &ic, double Theta)
Create a CellDataVector, in which the largest entities of *this are marked by 1, using the "bulk crit...
Definition: celldata.hh:224
CellData< Grid, int >::CellDataVector markByMaxCriterion(CellData< Grid, T > &ic, double Ratio)
Create a CellDataVector, in which the largest entities of *this are marked by 1, using the "max crite...
Definition: celldata.hh:262
void extendMarks(FSElement &fse, FSElement const &fu, int neighboursForMarking)
Definition: celldata.hh:333
CellData< Grid, int >::CellDataVector unmarkOutOf(CellData< Grid, int > const &ic, Dune::FieldVector< typename Grid::ctype, Grid::dimension > center, double radius)
Create a CellDataVector, in which the largest entities of *this are marked by 1, using the "max crite...
Definition: celldata.hh:296
CellData< Grid, int >::CellDataVector markWithinInterval(CellData< Grid, T > &ic, double lower, double upper, int times)
Create a CellDataVector, in which the largest entities of *this are marked by 1, using the "max crite...
Definition: celldata.hh:280