dune-grid  2.5.1
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 #if DUNE_GRID_EXPERIMENTAL_GRID_EXTENSIONS
110  return intersection.boundaryId();
111 #else
112  return (intersection.boundary()) ? int(intersection.indexInInside()+1) : int(0);
113 #endif
114  }
115 
116  template< int codim >
117  int numParameters () const
118  {
119  if( codim == 0 )
120  return macroGrid_.nofelparams;
121  else if( codim == dimension )
122  return macroGrid_.nofvtxparams;
123  else
124  return 0;
125  }
126 
127  template < class Entity >
128  int numParameters ( const Entity & ) const
129  {
130  return numParameters< Entity::codimension >();
131  }
132 
133  std::vector<double>& parameter(const Element &element)
134  {
135  const typename Element::Geometry &geo = element.geometry();
136  DomainType coord( geo.corner( 0 ) );
137  for( int i = 1; i < geo.corners(); ++i )
138  coord += geo.corner( i );
139  coord /= double( geo.corners() );
140 
141  InsertOrderIterator it = elInsertOrder_.find( coord );
142  if( it != elInsertOrder_.end() )
143  return macroGrid_.elParams[ it->second ];
144  assert(0);
145  return emptyParam;
146  }
147 
148  std::vector<double>& parameter(const Vertex &vertex)
149  {
150  const typename Vertex::Geometry &geo = vertex.geometry();
151  DomainType coord( geo.corner( 0 ) );
152 
153  InsertOrderIterator it = vtxInsertOrder_.find( coord );
154  if( it != vtxInsertOrder_.end() )
155  return macroGrid_.vtxParams[ it->second ];
156  return emptyParam;
157  }
158 
159  // return true if boundary parameters found
161  {
162  return false;
163  }
164 
165  template< class GG, class II >
166  const typename DGFBoundaryParameter::type &
167  boundaryParameter ( const Intersection< GG, II > & intersection ) const
168  {
170  }
171 
172  private:
173  typedef FieldVector<typename Grid::ctype,Grid::dimensionworld> DomainType;
174  struct Compare
175  {
176  bool operator() ( const DomainType &a, const DomainType &b ) const
177  {
178  // returns true, if a < b; c[i] < -eps;
179  const DomainType c = a - b;
180  const double eps = 1e-8;
181 
182  for( int i = 0; i < DomainType::dimension; ++i )
183  {
184  if( c[ i ] <= -eps )
185  return true;
186  if( c[ i ] >= eps )
187  return false;
188  }
189  return false;
190  }
191  };
192  typedef std::map< DomainType, size_t, Compare > InsertOrderMap;
193  typedef typename InsertOrderMap::const_iterator InsertOrderIterator;
194 
195  MacroGrid macroGrid_;
196  Grid *grid_;
197  InsertOrderMap elInsertOrder_;
198  InsertOrderMap vtxInsertOrder_;
199  std::vector<double> emptyParam;
200  };
201 
202 } // end namespace Dune
203 
204 #endif
std::vector< std::vector< double > > vtx
Definition: parser.hh:123
bool boundary() const
Return true if intersection is with interior or exterior boundary (see the cases above) ...
Definition: common/intersection.hh:222
Intersection of a mesh entity of codimension 0 ("element") with a "neighboring" element or with the d...
Definition: albertagrid/dgfparser.hh:26
int indexInInside() const
Local index of codim 1 entity in the inside() entity where intersection is contained in...
Definition: common/intersection.hh:374
Grid * grid()
Definition: dgfgridfactory.hh:95
DGFGridFactory(std::istream &input, MPICommunicatorType comm=MPIHelper::getCommunicator())
Definition: dgfgridfactory.hh:47
MPIHelper::MPICommunicator MPICommunicatorType
Definition: dgfgridfactory.hh:39
DGFGridFactory(const std::string &filename, MPICommunicatorType comm=MPIHelper::getCommunicator())
Definition: dgfgridfactory.hh:55
G Grid
Definition: dgfgridfactory.hh:37
Definition: macrogrid.hh:19
std::vector< double > & parameter(const Element &element)
Definition: dgfgridfactory.hh:133
std::vector< double > & parameter(const Vertex &vertex)
Definition: dgfgridfactory.hh:148
const DGFBoundaryParameter::type & boundaryParameter(const Intersection< GG, II > &intersection) const
Definition: dgfgridfactory.hh:167
std::vector< std::vector< double > > vtxParams
Definition: parser.hh:163
Definition: common.hh:179
std::string type
type of additional boundary parameters
Definition: parser.hh:23
Include standard header files.
Definition: agrid.hh:59
std::vector< std::vector< double > > elParams
Definition: parser.hh:163
exception class for IO errors in the DGF parser
Definition: dgfexception.hh:12
Wrapper class for entities.
Definition: common/entity.hh:64
int numParameters() const
Definition: dgfgridfactory.hh:117
bool haveBoundaryParameters() const
Definition: dgfgridfactory.hh:160
bool wasInserted(const Intersection &intersection) const
Definition: dgfgridfactory.hh:101
std ::vector< std ::vector< unsigned int > > elements
Definition: parser.hh:132
int nofvtxparams
Definition: parser.hh:161
static const int dimension
Definition: dgfgridfactory.hh:38
static const type & defaultValue()
default constructor
Definition: parser.hh:26
The dimension of the grid.
Definition: common/grid.hh:387
int boundaryId(const Intersection &intersection) const
Definition: dgfgridfactory.hh:107
int numParameters(const Entity &) const
Definition: dgfgridfactory.hh:128
int nofelparams
Definition: parser.hh:161