dune-grid  2.2.1
dgfgeogrid.hh
Go to the documentation of this file.
1 #ifndef DUNE_DGFGEOGRID_HH
2 #define DUNE_DGFGEOGRID_HH
3 
4 #include <dune/common/typetraits.hh>
5 
11 
13 
14 
15 namespace Dune
16 {
17  /************************************************************************
18  * Warning:
19  * Reading DGF files directly into a GeometryGrid is a dirty hack for
20  * two reasons:
21  * 1) The host grid and coordinate function are never deleted (dangling
22  * pointers).
23  * 2) The coordinate function has to provide a default constructor
24  ************************************************************************/
25 
26  // forward declaration
27  // -------------------
28  template < class GridImp, template < class > class IntersectionImp >
29  class Intersection;
30 
31  // DGFCoordFunction
32  // ----------------
33 
34  template< int dimD, int dimR >
36  : public AnalyticalCoordFunction< double, dimD, dimR, DGFCoordFunction< dimD, dimR > >
37  {
40 
41  public:
42  typedef typename Base::DomainVector DomainVector;
43  typedef typename Base::RangeVector RangeVector;
44 
46 
47  DGFCoordFunction ( const Expression *expression )
48  : expression_( expression )
49  {}
50 
51  void evaluate ( const DomainVector &x, RangeVector &y ) const
52  {
53  std::vector< double > vx( dimD );
54  std::vector< double > vy;
55  for( int i = 0; i < dimD; ++i )
56  vx[ i ] = x[ i ];
57  expression_->evaluate( vx, vy );
58  assert( vy.size() == size_t( dimR ) );
59  for( int i = 0; i < dimR; ++i )
60  y[ i ] = vy[ i ];
61  }
62 
63  private:
64  const Expression *expression_;
65  };
66 
67 
68 
69  // DGFCoordFunctionFactory
70  // -----------------------
71 
72  template< class HostGrid, class CoordFunction,
73  bool discrete = GeoGrid::isDiscreteCoordFunctionInterface< typename CoordFunction::Interface >::value >
75 
76 
77  template< class HostGrid, class CoordFunction >
78  struct DGFCoordFunctionFactory< HostGrid, CoordFunction, false >
79  {
80  static CoordFunction *create ( std::istream &input, const HostGrid &hostGrid )
81  {
82  return new CoordFunction;
83  }
84  };
85 
86 
87  template< class HostGrid, class CoordFunction >
88  struct DGFCoordFunctionFactory< HostGrid, CoordFunction, true >
89  {
90  static CoordFunction *create ( std::istream &input, const HostGrid &hostGrid )
91  {
92  return new CoordFunction( hostGrid );
93  }
94  };
95 
96 
97  template< class HostGrid, int dimD, int dimR >
98  struct DGFCoordFunctionFactory< HostGrid, DGFCoordFunction< dimD, dimR >, false >
99  {
101 
102  static CoordFunction *create ( std::istream &input, const HostGrid &hostGrid )
103  {
104  dgf::ProjectionBlock projectionBlock( input, dimR );
105  const typename CoordFunction::Expression *expression = projectionBlock.function( "coordfunction" );
106  if( expression == 0 )
107  DUNE_THROW( DGFException, "no coordfunction specified in DGF file." );
108  return new CoordFunction( expression );
109  }
110  };
111 
112 
113 
114  // DGFGridFactory for GeometryGrid
115  // -------------------------------
116 
117  template< class HostGrid, class CoordFunction, class Allocator >
118  struct DGFGridFactory< GeometryGrid< HostGrid, CoordFunction, Allocator > >
119  {
121 
122  const static int dimension = Grid::dimension;
123  typedef MPIHelper::MPICommunicator MPICommunicator;
124  typedef typename Grid::template Codim<0>::Entity Element;
125  typedef typename Grid::template Codim<dimension>::Entity Vertex;
126 
128 
129  explicit DGFGridFactory ( std::istream &input,
130  MPICommunicator comm = MPIHelper::getCommunicator() )
131  : dgfHostFactory_( input, comm ),
132  grid_( 0 )
133  {
134  HostGrid *hostGrid = dgfHostFactory_.grid();
135  assert( hostGrid != 0 );
136  CoordFunction *coordFunction = CoordFunctionFactory::create( input, *hostGrid );
137  grid_ = new Grid( hostGrid, coordFunction );
138  }
139 
140  explicit DGFGridFactory ( const std::string &filename,
141  MPICommunicator comm = MPIHelper::getCommunicator() )
142  : dgfHostFactory_( filename, comm ),
143  grid_( 0 )
144  {
145  HostGrid *hostGrid = dgfHostFactory_.grid();
146  assert( hostGrid != 0 );
147  std::ifstream input( filename.c_str() );
148  CoordFunction *coordFunction = CoordFunctionFactory::create( input, *hostGrid );
149  grid_ = new Grid( hostGrid, coordFunction );
150  }
151 
152  Grid *grid () const
153  {
154  return grid_;
155  }
156 
157  template< class Intersection >
158  bool wasInserted ( const Intersection &intersection ) const
159  {
160  return dgfHostFactory_.wasInserted( HostGridAccess< Grid >::hostIntersection( intersection ) );
161  }
162 
163  template< class Intersection >
164  int boundaryId ( const Intersection &intersection ) const
165  {
166  return dgfHostFactory_.boundaryId( HostGridAccess< Grid >::hostIntersection( intersection ) );
167  }
168 
169  template< int codim >
170  int numParameters () const
171  {
172  return dgfHostFactory_.template numParameters< codim >();
173  }
174 
175  // return true if boundary parameters found
177  {
178  return dgfHostFactory_.haveBoundaryParameters();
179  }
180 
181  template< class GG, template< class > class II >
182  const typename DGFBoundaryParameter::type &
183  boundaryParameter ( const Dune::Intersection< GG, II > & intersection ) const
184  {
185  return dgfHostFactory_.boundaryParameter( HostGridAccess< Grid >::hostIntersection( intersection ) );
186  }
187 
188  template< class Entity >
189  std::vector< double > &parameter ( const Entity &entity )
190  {
191  return dgfHostFactory_.parameter( HostGridAccess< Grid >::hostEntity( entity ) );
192  }
193 
194  private:
195  DGFGridFactory< HostGrid > dgfHostFactory_;
196  Grid *grid_;
197  };
198 
199 
200 
201  // DGFGridInfo for GeometryGrid
202  // ----------------------------
203 
204  template< class HostGrid, class CoordFunction, class Allocator >
205  struct DGFGridInfo< GeometryGrid< HostGrid, CoordFunction, Allocator > >
206  {
207  static int refineStepsForHalf ()
208  {
210  }
211 
212  static double refineWeight ()
213  {
214  return -1.0;
215  }
216  };
217 
218 }
219 
220 #endif // #ifndef DUNE_DGFGEOGRID_HH