dune-grid  2.2.1
dgfgridfactory.hh
Go to the documentation of this file.
1 #ifndef DUNE_DGF_GRIDFACTORY_HH
2 #define DUNE_DGF_GRIDFACTORY_HH
3 
4 #include <iostream>
5 #include <string>
6 #include <vector>
7 #include <map>
8 #include <assert.h>
9 
10 #include <dune/common/mpihelper.hh>
13 
16 
17 
18 namespace Dune
19 {
20 
21  // forward declaration
22  template < class GridImp, template < class > class IntersectionImp >
23  class Intersection;
24 
25  template < class G >
26  struct DGFGridFactory
27  {
28  typedef G Grid;
29  const static int dimension = Grid::dimension;
30  typedef MPIHelper::MPICommunicator MPICommunicatorType;
31 
32  private:
33  typedef typename Grid::template Codim< 0 >::Entity Element;
34 
35  typedef typename Grid::template Codim< dimension >::Entity Vertex;
36 
37  public:
38  explicit DGFGridFactory ( std::istream &input,
39  MPICommunicatorType comm = MPIHelper::getCommunicator() ) DUNE_DEPRECATED
40  : macroGrid_( comm )
41  {
42  DUNE_THROW( DGFException, "DGF factories using old MacroGrid implementation"
43  "don't support creation from std::istream." );
44  }
45 
46  explicit DGFGridFactory ( const std::string &filename,
47  MPICommunicatorType comm = MPIHelper::getCommunicator() ) DUNE_DEPRECATED
48  : macroGrid_( filename.c_str(), comm )
49  {
50  grid_ = macroGrid_.template createGrid< Grid >();
51 
52  if( macroGrid_.nofelparams > 0 )
53  {
54  const size_t nofElements = macroGrid_.elements.size();
55  for( size_t i = 0; i < nofElements; ++i )
56  {
57  std::vector< double > coord;
58 
59  DomainType p(0);
60  const size_t nofCorners = macroGrid_.elements[i].size();
61  for (size_t k=0;k<nofCorners;++k)
62  for (int j=0;j<DomainType::dimension;++j)
63  p[j]+=macroGrid_.vtx[macroGrid_.elements[i][k]][j];
64  p/=double(nofCorners);
65 
66  elInsertOrder_.insert( std::make_pair( p, i ) );
67  }
68  }
69 
70  if( macroGrid_.nofvtxparams > 0 )
71  {
72  const size_t nofVertices = macroGrid_.vtx.size();
73  for( size_t i = 0; i < nofVertices; ++i )
74  {
75  std::vector< double > coord;
76 
77  DomainType p;
78  for( int k = 0; k < DomainType::dimension; ++k )
79  p[ k ] = macroGrid_.vtx[i][k];
80 
81  vtxInsertOrder_.insert( std::make_pair( p, i ) );
82  }
83  }
84  }
85 
87  {
88  return grid_;
89  }
90 
91  template <class Intersection>
92  bool wasInserted(const Intersection &intersection) const
93  {
94  return intersection.boundary();
95  }
96 
97  template <class Intersection>
98  int boundaryId(const Intersection &intersection) const
99  {
100  return intersection.boundaryId();
101  }
102 
103  template< int codim >
104  int numParameters () const
105  {
106  if( codim == 0 )
107  return macroGrid_.nofelparams;
108  else if( codim == dimension )
109  return macroGrid_.nofvtxparams;
110  else
111  return 0;
112  }
113 
114  template < class Entity >
115  int numParameters ( const Entity & ) const
116  {
117  return numParameters< Entity::codimension >();
118  }
119 
120  std::vector<double>& parameter(const Element &element)
121  {
122  const typename Element::Geometry &geo = element.geometry();
123  DomainType coord( geo.corner( 0 ) );
124  for( int i = 1; i < geo.corners(); ++i )
125  coord += geo.corner( i );
126  coord /= double( geo.corners() );
127 
128  InsertOrderIterator it = elInsertOrder_.find( coord );
129  if( it != elInsertOrder_.end() )
130  return macroGrid_.elParams[ it->second ];
131  assert(0);
132  return emptyParam;
133  }
134 
135  std::vector<double>& parameter(const Vertex &vertex)
136  {
137  const typename Vertex::Geometry &geo = vertex.geometry();
138  DomainType coord( geo.corner( 0 ) );
139 
140  InsertOrderIterator it = vtxInsertOrder_.find( coord );
141  if( it != vtxInsertOrder_.end() )
142  return macroGrid_.vtxParams[ it->second ];
143  return emptyParam;
144  }
145 
146  // return true if boundary parameters found
148  {
149  return false;
150  }
151 
152  template < class GG, template < class > class II >
153  const typename DGFBoundaryParameter::type &
154  boundaryParameter ( const Intersection< GG, II > & intersection ) const
155  {
157  }
158 
159  private:
160  typedef FieldVector<typename Grid::ctype,Grid::dimensionworld> DomainType;
161  struct Compare
162  {
163  bool operator() ( const DomainType &a, const DomainType &b ) const
164  {
165  // returns true, if a < b; c[i] < -eps;
166  const DomainType c = a - b;
167  const double eps = 1e-8;
168 
169  for( int i = 0; i < DomainType::dimension; ++i )
170  {
171  if( c[ i ] <= -eps )
172  return true;
173  if( c[ i ] >= eps )
174  return false;
175  }
176  return false;
177  }
178  };
179  typedef std::map< DomainType, size_t, Compare > InsertOrderMap;
180  typedef typename InsertOrderMap::const_iterator InsertOrderIterator;
181 
182  MacroGrid macroGrid_;
183  Grid *grid_;
184  InsertOrderMap elInsertOrder_;
185  InsertOrderMap vtxInsertOrder_;
186  std::vector<double> emptyParam;
187  };
188 
189 } // end namespace Dune
190 
191 #endif