dune-grid  2.8.0
dgfgridfactory.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_DGF_GRIDFACTORY_HH
4 #define DUNE_DGF_GRIDFACTORY_HH
5 
6 #include <iostream>
7 #include <string>
8 #include <vector>
9 #include <map>
10 #include <assert.h>
11 
12 #include <dune/common/parallel/mpihelper.hh>
15 
18 
19 
20 namespace Dune
21 {
22 
23  // External Forward Declarations
24  // -----------------------------
25 
26  template < class GridImp, class IntersectionImp >
27  class Intersection;
28 
29 
30 
31  // DGFGridFactory
32  // --------------
33 
34  template < class G >
36  {
37  typedef G Grid;
38  const static int dimension = Grid::dimension;
39  typedef MPIHelper::MPICommunicator MPICommunicatorType;
40 
41  private:
42  typedef typename Grid::template Codim< 0 >::Entity Element;
43 
44  typedef typename Grid::template Codim< dimension >::Entity Vertex;
45 
46  public:
47 
48  explicit DGFGridFactory ( const std::string &filename,
49  MPICommunicatorType comm = MPIHelper::getCommunicator() )
50  : macroGrid_( filename.c_str(), comm )
51  {
52  grid_ = macroGrid_.template createGrid< Grid >();
53 
54  if( macroGrid_.nofelparams > 0 )
55  {
56  const size_t nofElements = macroGrid_.elements.size();
57  for( size_t i = 0; i < nofElements; ++i )
58  {
59  std::vector< double > coord;
60 
61  DomainType p(0);
62  const size_t nofCorners = macroGrid_.elements[i].size();
63  for (size_t k=0; k<nofCorners; ++k)
64  for (int j=0; j<DomainType::dimension; ++j)
65  p[j]+=macroGrid_.vtx[macroGrid_.elements[i][k]][j];
66  p/=double(nofCorners);
67 
68  elInsertOrder_.insert( std::make_pair( p, i ) );
69  }
70  }
71 
72  if( macroGrid_.nofvtxparams > 0 )
73  {
74  const size_t nofVertices = macroGrid_.vtx.size();
75  for( size_t i = 0; i < nofVertices; ++i )
76  {
77  std::vector< double > coord;
78 
79  DomainType p;
80  for( int k = 0; k < DomainType::dimension; ++k )
81  p[ k ] = macroGrid_.vtx[i][k];
82 
83  vtxInsertOrder_.insert( std::make_pair( p, i ) );
84  }
85  }
86  }
87 
89  {
90  return grid_;
91  }
92 
93  template <class Intersection>
94  bool wasInserted(const Intersection &intersection) const
95  {
96  return intersection.boundary();
97  }
98 
99  template <class Intersection>
100  int boundaryId(const Intersection &intersection) const
101  {
102  return (intersection.boundary()) ? int(intersection.indexInInside()+1) : int(0);
103  }
104 
105  template< int codim >
106  int numParameters () const
107  {
108  if( codim == 0 )
109  return macroGrid_.nofelparams;
110  else if( codim == dimension )
111  return macroGrid_.nofvtxparams;
112  else
113  return 0;
114  }
115 
116  template < class Entity >
117  int numParameters ( const Entity & ) const
118  {
119  return numParameters< Entity::codimension >();
120  }
121 
122  std::vector<double>& parameter(const Element &element)
123  {
124  const typename Element::Geometry &geo = element.geometry();
125  DomainType coord( geo.corner( 0 ) );
126  for( int i = 1; i < geo.corners(); ++i )
127  coord += geo.corner( i );
128  coord /= double( geo.corners() );
129 
130  InsertOrderIterator it = elInsertOrder_.find( coord );
131  if( it != elInsertOrder_.end() )
132  return macroGrid_.elParams[ it->second ];
133  assert(0);
134  return emptyParam;
135  }
136 
137  std::vector<double>& parameter(const Vertex &vertex)
138  {
139  const typename Vertex::Geometry &geo = vertex.geometry();
140  DomainType coord( geo.corner( 0 ) );
141 
142  InsertOrderIterator it = vtxInsertOrder_.find( coord );
143  if( it != vtxInsertOrder_.end() )
144  return macroGrid_.vtxParams[ it->second ];
145  return emptyParam;
146  }
147 
148  // return true if boundary parameters found
150  {
151  return false;
152  }
153 
154  template< class GG, class II >
155  const typename DGFBoundaryParameter::type &
156  boundaryParameter ( const Intersection< GG, II > & intersection ) const
157  {
159  }
160 
161  private:
162  typedef FieldVector<typename Grid::ctype,Grid::dimensionworld> DomainType;
163  struct Compare
164  {
165  bool operator() ( const DomainType &a, const DomainType &b ) const
166  {
167  // returns true, if a < b; c[i] < -eps;
168  const DomainType c = a - b;
169  const double eps = 1e-8;
170 
171  for( int i = 0; i < DomainType::dimension; ++i )
172  {
173  if( c[ i ] <= -eps )
174  return true;
175  if( c[ i ] >= eps )
176  return false;
177  }
178  return false;
179  }
180  };
181  typedef std::map< DomainType, size_t, Compare > InsertOrderMap;
182  typedef typename InsertOrderMap::const_iterator InsertOrderIterator;
183 
184  MacroGrid macroGrid_;
185  Grid *grid_;
186  InsertOrderMap elInsertOrder_;
187  InsertOrderMap vtxInsertOrder_;
188  std::vector<double> emptyParam;
189  };
190 
191 } // end namespace Dune
192 
193 #endif
Include standard header files.
Definition: agrid.hh:58
@ vertex
Definition: common.hh:131
Definition: dgfgridfactory.hh:36
int numParameters() const
Definition: dgfgridfactory.hh:106
const DGFBoundaryParameter::type & boundaryParameter(const Intersection< GG, II > &intersection) const
Definition: dgfgridfactory.hh:156
int boundaryId(const Intersection &intersection) const
Definition: dgfgridfactory.hh:100
MPIHelper::MPICommunicator MPICommunicatorType
Definition: dgfgridfactory.hh:39
int numParameters(const Entity &) const
Definition: dgfgridfactory.hh:117
bool wasInserted(const Intersection &intersection) const
Definition: dgfgridfactory.hh:94
DGFGridFactory(const std::string &filename, MPICommunicatorType comm=MPIHelper::getCommunicator())
Definition: dgfgridfactory.hh:48
std::vector< double > & parameter(const Vertex &vertex)
Definition: dgfgridfactory.hh:137
std::vector< double > & parameter(const Element &element)
Definition: dgfgridfactory.hh:122
G Grid
Definition: dgfgridfactory.hh:37
Grid * grid()
Definition: dgfgridfactory.hh:88
static const int dimension
Definition: dgfgridfactory.hh:38
bool haveBoundaryParameters() const
Definition: dgfgridfactory.hh:149
Intersection of a mesh entity of codimension 0 ("element") with a "neighboring" element or with the d...
Definition: common/intersection.hh:162
bool boundary() const
Return true if intersection is with interior or exterior boundary (see the cases above)
Definition: common/intersection.hh:214
int indexInInside() const
Local index of codim 1 entity in the inside() entity where intersection is contained in.
Definition: common/intersection.hh:344
Wrapper class for entities.
Definition: common/entity.hh:64
@ dimension
The dimension of the grid.
Definition: common/grid.hh:386
static const type & defaultValue()
default constructor
Definition: parser.hh:26
std::string type
type of additional boundary parameters
Definition: parser.hh:23
int nofvtxparams
Definition: parser.hh:161
std::vector< std::vector< double > > vtxParams
Definition: parser.hh:163
int nofelparams
Definition: parser.hh:161
std::vector< std::vector< double > > elParams
Definition: parser.hh:163
std::vector< std::vector< double > > vtx
Definition: parser.hh:123
std ::vector< std ::vector< unsigned int > > elements
Definition: parser.hh:132