dune-grid  2.2.0
utility/gridinfo.hh
Go to the documentation of this file.
1 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=8 sw=2 sts=2:
3 
4 #ifndef DUNE_GRID_UTILITY_GRIDINFO_HH
5 #define DUNE_GRID_UTILITY_GRIDINFO_HH
6 
7 #include <algorithm>
8 #include <cstddef>
9 #include <functional>
10 #include <limits>
11 #include <map>
12 #include <ostream>
13 #include <string>
14 #include <vector>
15 
16 #include <dune/common/classname.hh>
17 #include <dune/common/exceptions.hh>
18 #include <dune/common/forloop.hh>
19 #include <dune/common/fvector.hh>
20 #include <dune/geometry/type.hh>
21 #include <dune/geometry/referenceelements.hh>
22 
23 #include <dune/geometry/mockgeometry.hh>
24 
26 
27 namespace Dune {
28 
30  template<class ctype>
31  struct EntityInfo {
33  std::size_t count;
35 
40  ctype volumeMin;
42 
47  ctype volumeMax;
48 
50 
54  ctype volumeSum;
55 
57 
63  count(0), volumeMin(std::numeric_limits<ctype>::infinity()),
64  volumeMax(-std::numeric_limits<ctype>::infinity()), volumeSum(0)
65  { }
66  };
67 
69 
76  public std::binary_function<GeometryType, GeometryType, bool>
77  {
79  inline bool operator()(const GeometryType &a, const GeometryType &b) const
80  {
81  return a.dim() < b.dim() ||
82  (a.dim() == b.dim() && (a.isNone() < b.isNone() ||
83  (a.isNone() == b.isNone() && (a.id() >> 1) < (b.id() >> 1))));
84  // topologyId is set to 0 for None, so no harm im comparing them even if
85  // isNone()==true
86  }
87  };
88 
90 
95  template<class ctype>
96  struct GridViewInfo :
97  public std::map<GeometryType, EntityInfo<ctype>, GridViewInfoGTCompare>
98  {
100  std::string gridName;
102  std::string gridViewName;
104 
108  std::string partitionName;
109 
111 
124  void print(std::ostream &stream, std::string prefix) const {
125  if(!gridName.empty()) {
126  stream << prefix << gridName << ":\n";
127  prefix += " ";
128  }
129  if(!gridViewName.empty()) {
130  stream << prefix << gridViewName << ":\n";
131  prefix += " ";
132  }
133  if(!partitionName.empty()) {
134  stream << prefix << partitionName << ":\n";
135  prefix += " ";
136  }
137 
138  typedef typename GridViewInfo::const_iterator Iterator;
139  std::size_t dim = ~0;
140  const Iterator &end = this->end();
141  for(Iterator it = this->begin(); it != end; ++it) {
142  if(it->first.dim() != dim) {
143  dim = it->first.dim();
144  stream << prefix << "Dim = " << dim << ":\n";
145  }
146  stream << prefix << " " << it->first << ": Count = "
147  << it->second.count << ", Volume range = "
148  << "(" << it->second.volumeMin << ".."
149  << it->second.volumeMax << "), Total volume = "
150  << it->second.volumeSum << "\n";
151  }
152  }
153  };
154 
156 
161  template<class ctype>
162  std::ostream &operator<<(std::ostream &stream,
163  const GridViewInfo<ctype> &info)
164  {
165  info.print(stream, "");
166  return stream;
167  }
168 
169 #ifndef DOXYGEN
170 
171  template<int codim>
172  struct FillGridInfoOperation {
173  template<class Entity, class Mapper, class Visited, class RefElem>
174  static void apply(const Entity &e, const Mapper &mapper, Visited &visited,
175  const typename Entity::Geometry &geo,
176  const RefElem &refelem,
178  {
179  typedef typename Entity::ctype ctype;
180  static const std::size_t dimw = Entity::dimensionworld;
181  static const std::size_t dim = Entity::dimension;
182  std::vector<FieldVector<ctype, dimw> > coords;
183  for(int i = 0; i < refelem.size(codim); ++i) {
184  int index = mapper.map(e, i, codim);
185  if(visited[index])
186  continue;
187  visited[index] = true;
188 
189  GeometryType gt = refelem.type(i, codim);
190  coords.clear();
191  coords.resize( refelem.size(i, codim, dim) );
192  for(std::size_t corner = 0; corner < coords.size(); ++corner)
193  coords[ corner ] = geo.corner( refelem.subEntity( i, codim, corner, dim ) );
194  MockGeometry<ctype, dim-codim, dimw> mygeo(gt, coords);
195 
196  ctype volume = mygeo.volume();
197  EntityInfo<ctype> &ei = gridViewInfo[mygeo.type()];
198  ei.volumeMin = std::min(ei.volumeMin, volume);
199  ei.volumeMax = std::max(ei.volumeMax, volume);
200  ei.volumeSum += volume;
201  }
202  }
203  };
204 
205  template<int dimgrid>
206  struct MCMGNonElementLayout {
207  bool contains(GeometryType gt) const { return gt.dim() < dimgrid; }
208  };
209 #endif // !DOXYGEN
210 
212 
216  template<class GV>
217  void fillGridViewInfoSerial(const GV &gv,
218  GridViewInfo<typename GV::ctype> &gridViewInfo)
219  {
220  typedef typename GV::ctype ctype;
221  static const std::size_t dim = GV::dimension;
222  typedef typename GV::template Codim<0>::Iterator EIterator;
223  typedef typename GV::template Codim<0>::Geometry EGeometry;
224  typedef typename GV::IntersectionIterator IIterator;
225  typedef typename GV::IndexSet IndexSet;
226 
227  typedef typename GridViewInfo<ctype>::iterator InfoIterator;
228 
229  typedef GenericReferenceElements<ctype, dim> RefElems;
230 
232  std::vector<bool> visited(mapper.size(), false);
233 
234  gridViewInfo.gridName = className<typename GV::Grid>();
235  gridViewInfo.gridViewName = className<GV>();
236  gridViewInfo.partitionName = "";
237  gridViewInfo.clear();
238 
239  const EIterator &eend = gv.template end<0>();
240  for(EIterator eit = gv.template begin<0>(); eit != eend; ++eit) {
241  ctype volume = eit->geometry().volume();
242  EntityInfo<ctype> &ei = gridViewInfo[eit->type()];
243  ei.volumeMin = std::min(ei.volumeMin, volume);
244  ei.volumeMax = std::max(ei.volumeMax, volume);
245  ei.volumeSum += volume;
246 
247  if(!eit->type().isNone()) {
248  const EGeometry &geo = eit->geometry();
249  ForLoop<FillGridInfoOperation, 1, dim>::
250  apply(*eit, mapper, visited, geo, RefElems::general(eit->type()),
251  gridViewInfo);
252  }
253  }
254 
255  GeometryType gt;
256  gt.makeNone(dim);
257  if(gridViewInfo.count(gt) > 0) {
258  for(std::size_t codim = 0; codim < dim; ++codim) {
259  gt.makeNone(dim-codim);
260  EntityInfo<ctype> & ei = gridViewInfo[gt];
261  ei.volumeMin = ei.volumeMax = ei.volumeSum =
262  std::numeric_limits<ctype>::quiet_NaN();
263  }
264  gt.makeNone(0);
265  EntityInfo<ctype> & ei = gridViewInfo[gt];
266  ei.volumeMin = ei.volumeMax = ei.volumeSum = 0;
267  }
268 
269  const InfoIterator &end = gridViewInfo.end();
270  const IndexSet &is = gv.indexSet();
271  for(InfoIterator it = gridViewInfo.begin(); it != end; ++it) {
272  it->second.count = is.size(it->first);
273  if(it->second.count == 0)
274  DUNE_THROW(Exception, "Found Entities of geomentry type " <<
275  it->first << " while iterating through the grid, but "
276  "indexSet.size() == 0 for that geometry type");
277  }
278 
279  }
280 
281 } // namespace Dune
282 
283 
284 #endif // DUNE_GRID_UTILITY_GRIDINFO_HH