dune-grid  2.4.1-rc2
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 >
35  struct DGFGridFactory
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  explicit DGFGridFactory ( std::istream &input,
48  MPICommunicatorType comm = MPIHelper::getCommunicator() ) DUNE_DEPRECATED
49  : macroGrid_( comm )
50  {
51  DUNE_THROW( DGFException, "DGF factories using old MacroGrid implementation"
52  "don't support creation from std::istream." );
53  }
54 
55  explicit DGFGridFactory ( const std::string &filename,
56  MPICommunicatorType comm = MPIHelper::getCommunicator() ) DUNE_DEPRECATED
57  : macroGrid_( filename.c_str(), comm )
58  {
59  grid_ = macroGrid_.template createGrid< Grid >();
60 
61  if( macroGrid_.nofelparams > 0 )
62  {
63  const size_t nofElements = macroGrid_.elements.size();
64  for( size_t i = 0; i < nofElements; ++i )
65  {
66  std::vector< double > coord;
67 
68  DomainType p(0);
69  const size_t nofCorners = macroGrid_.elements[i].size();
70  for (size_t k=0; k<nofCorners; ++k)
71  for (int j=0; j<DomainType::dimension; ++j)
72  p[j]+=macroGrid_.vtx[macroGrid_.elements[i][k]][j];
73  p/=double(nofCorners);
74 
75  elInsertOrder_.insert( std::make_pair( p, i ) );
76  }
77  }
78 
79  if( macroGrid_.nofvtxparams > 0 )
80  {
81  const size_t nofVertices = macroGrid_.vtx.size();
82  for( size_t i = 0; i < nofVertices; ++i )
83  {
84  std::vector< double > coord;
85 
86  DomainType p;
87  for( int k = 0; k < DomainType::dimension; ++k )
88  p[ k ] = macroGrid_.vtx[i][k];
89 
90  vtxInsertOrder_.insert( std::make_pair( p, i ) );
91  }
92  }
93  }
94 
95  Grid *grid()
96  {
97  return grid_;
98  }
99 
100  template <class Intersection>
101  bool wasInserted(const Intersection &intersection) const
102  {
103  return intersection.boundary();
104  }
105 
106  template <class Intersection>
107  int boundaryId(const Intersection &intersection) const
108  {
109  return intersection.boundaryId();
110  }
111 
112  template< int codim >
113  int numParameters () const
114  {
115  if( codim == 0 )
116  return macroGrid_.nofelparams;
117  else if( codim == dimension )
118  return macroGrid_.nofvtxparams;
119  else
120  return 0;
121  }
122 
123  template < class Entity >
124  int numParameters ( const Entity & ) const
125  {
126  return numParameters< Entity::codimension >();
127  }
128 
129  std::vector<double>& parameter(const Element &element)
130  {
131  const typename Element::Geometry &geo = element.geometry();
132  DomainType coord( geo.corner( 0 ) );
133  for( int i = 1; i < geo.corners(); ++i )
134  coord += geo.corner( i );
135  coord /= double( geo.corners() );
136 
137  InsertOrderIterator it = elInsertOrder_.find( coord );
138  if( it != elInsertOrder_.end() )
139  return macroGrid_.elParams[ it->second ];
140  assert(0);
141  return emptyParam;
142  }
143 
144  std::vector<double>& parameter(const Vertex &vertex)
145  {
146  const typename Vertex::Geometry &geo = vertex.geometry();
147  DomainType coord( geo.corner( 0 ) );
148 
149  InsertOrderIterator it = vtxInsertOrder_.find( coord );
150  if( it != vtxInsertOrder_.end() )
151  return macroGrid_.vtxParams[ it->second ];
152  return emptyParam;
153  }
154 
155  // return true if boundary parameters found
157  {
158  return false;
159  }
160 
161  template< class GG, class II >
162  const typename DGFBoundaryParameter::type &
163  boundaryParameter ( const Intersection< GG, II > & intersection ) const
164  {
166  }
167 
168  private:
169  typedef FieldVector<typename Grid::ctype,Grid::dimensionworld> DomainType;
170  struct Compare
171  {
172  bool operator() ( const DomainType &a, const DomainType &b ) const
173  {
174  // returns true, if a < b; c[i] < -eps;
175  const DomainType c = a - b;
176  const double eps = 1e-8;
177 
178  for( int i = 0; i < DomainType::dimension; ++i )
179  {
180  if( c[ i ] <= -eps )
181  return true;
182  if( c[ i ] >= eps )
183  return false;
184  }
185  return false;
186  }
187  };
188  typedef std::map< DomainType, size_t, Compare > InsertOrderMap;
189  typedef typename InsertOrderMap::const_iterator InsertOrderIterator;
190 
191  MacroGrid macroGrid_;
192  Grid *grid_;
193  InsertOrderMap elInsertOrder_;
194  InsertOrderMap vtxInsertOrder_;
195  std::vector<double> emptyParam;
196  };
197 
198 } // end namespace Dune
199 
200 #endif
MPIHelper::MPICommunicator MPICommunicatorType
Definition: dgfgridfactory.hh:39
Definition: macrogrid.hh:19
Grid * grid()
Definition: dgfgridfactory.hh:95
std::vector< double > & parameter(const Element &element)
Definition: dgfgridfactory.hh:129
Intersection of a mesh entities of codimension 0 ("elements") with a "neighboring" element or with th...
Definition: albertagrid/dgfparser.hh:26
const DGFBoundaryParameter::type & boundaryParameter(const Intersection< GG, II > &intersection) const
Definition: dgfgridfactory.hh:163
std::vector< std::vector< unsigned int > > elements
Definition: parser.hh:132
static const int dimension
Definition: dgfgridfactory.hh:38
static const type & defaultValue()
default constructor
Definition: parser.hh:26
int numParameters() const
Definition: dgfgridfactory.hh:113
Wrapper class for entities.
Definition: common/entity.hh:61
std::vector< std::vector< double > > vtxParams
Definition: parser.hh:163
bool boundary() const
Return true if intersection is with interior or exterior boundary (see the cases above) ...
Definition: common/intersection.hh:218
DGFGridFactory(std::istream &input, MPICommunicatorType comm=MPIHelper::getCommunicator())
Definition: dgfgridfactory.hh:47
int numParameters(const Entity &) const
Definition: dgfgridfactory.hh:124
std::string type
type of additional boundary parameters
Definition: parser.hh:23
bool wasInserted(const Intersection &intersection) const
Definition: dgfgridfactory.hh:101
int boundaryId(const Intersection &intersection) const
Definition: dgfgridfactory.hh:107
std::vector< std::vector< double > > elParams
Definition: parser.hh:163
int nofvtxparams
Definition: parser.hh:161
int nofelparams
Definition: parser.hh:161
bool haveBoundaryParameters() const
Definition: dgfgridfactory.hh:156
Definition: common.hh:179
Include standard header files.
Definition: agrid.hh:59
DGFGridFactory(const std::string &filename, MPICommunicatorType comm=MPIHelper::getCommunicator())
Definition: dgfgridfactory.hh:55
std::vector< std::vector< double > > vtx
Definition: parser.hh:123
std::vector< double > & parameter(const Vertex &vertex)
Definition: dgfgridfactory.hh:144
The dimension of the grid.
Definition: common/grid.hh:402
G Grid
Definition: dgfgridfactory.hh:37
exception class for IO errors in the DGF parser
Definition: dgfexception.hh:12