dune-grid  2.2.1
dgfs.hh
Go to the documentation of this file.
1 #ifndef DUNE_DGFS_HH
2 #define DUNE_DGFS_HH
3 
5 #include <dune/grid/sgrid.hh>
6 #include "dgfparser.hh"
7 
8 namespace Dune
9 {
10  // forward declaration
11  // -------------------
12 
13  template< class GridImp, template< class > class IntersectionImp >
14  class Intersection;
15 
16 
17  template< int dim, int dimworld, class ctype >
18  struct DGFGridFactory< SGrid< dim, dimworld, ctype > >
19  {
21 
22  const static int dimension = Grid::dimension;
23 
24  typedef MPIHelper::MPICommunicator MPICommunicatorType;
25 
26  private:
27  typedef FieldVector< double, dimension > Point;
28 
30 
31  public:
32  explicit DGFGridFactory ( std::istream &input,
33  MPICommunicatorType comm = MPIHelper::getCommunicator() )
34  {
35  generate( input, comm );
36  }
37 
38  explicit DGFGridFactory ( const std::string &filename,
39  MPICommunicatorType comm = MPIHelper::getCommunicator() )
40  {
41  std::ifstream input( filename.c_str() );
42  generate( input, comm );
43  }
44 
45  Grid *grid() const
46  {
47  return grid_;
48  }
49 
50  template< class Intersection >
51  bool wasInserted ( const Intersection &intersection ) const
52  {
53  return false;
54  }
55 
56  template< class Intersection >
57  int boundaryId ( const Intersection &intersection ) const
58  {
59  if( boundaryDomainBlock_->isactive() )
60  {
61  std::vector< Point > corners;
62  getCorners( intersection.geometry(), corners );
63  const dgf::DomainData *data = boundaryDomainBlock_->contains( corners );
64  if( data )
65  return data->id();
66  else
67  return intersection.indexInInside();
68  }
69  else
70  return intersection.indexInInside();
71  }
72 
73  template< int codim >
74  int numParameters () const
75  {
76  return 0;
77  }
78 
79  // return true if boundary parameters found
80  bool haveBoundaryParameters () const
81  {
82  return boundaryDomainBlock_->isactive();
83  }
84 
85  template < class GG, template < class > class II >
86  const typename DGFBoundaryParameter::type &
87  boundaryParameter ( const Intersection< GG, II > & intersection ) const
88  {
90  {
91  std::vector< Point > corners;
92  getCorners( intersection.geometry(), corners );
93  const dgf::DomainData *data = boundaryDomainBlock_->contains( corners );
94  if( data )
95  return data->parameter();
96  else
98  }
99  else
101  }
102 
103 
104  template< class Entity >
105  std::vector< double > &parameter ( const Entity &entity )
106  {
107  return emptyParam;
108  }
109 
110  private:
111  void generate( std::istream &gridin, MPICommunicatorType comm );
112 
113  template< class Geometry >
114  static void getCorners ( const Geometry &geometry, std::vector< Point > &corners )
115  {
116  corners.resize( geometry.corners() );
117  for( int i = 0; i < geometry.corners(); ++i )
118  {
119  const typename Geometry::GlobalCoordinate corner = geometry.corner( i );
120  for( int j = 0; j < dimension; ++j )
121  corners[ i ][ j ] = corner[ j ];
122  }
123  }
124 
125  Grid *grid_;
126  dgf::BoundaryDomBlock *boundaryDomainBlock_;
127  std::vector< double > emptyParam;
128  };
129 
130 
131 
132  template< int dim, int dimworld, class ctype >
133  inline void DGFGridFactory< SGrid< dim, dimworld, ctype > >
134  ::generate ( std::istream &gridin, MPICommunicatorType comm )
135  {
136  dgf::IntervalBlock intervalBlock( gridin );
137 
138  if( !intervalBlock.isactive() )
139  DUNE_THROW( DGFException, "SGrid can only be created from an interval block." );
140 
141  if( intervalBlock.numIntervals() != 1 )
142  DUNE_THROW( DGFException, "SGrid can only handle 1 interval block." );
143 
144  if( intervalBlock.dimw() != dim )
145  {
146  DUNE_THROW( DGFException,
147  "Cannot read an interval of dimension " << intervalBlock.dimw()
148  << "into a SGrid< " << dim << ", " << dimworld << " >." );
149  }
150 
151  const dgf::IntervalBlock::Interval &interval = intervalBlock.get( 0 );
152 
153  FieldVector< double, dimension > lower, upper;
154  FieldVector< int, dimension > anz;
155  for( int i = 0; i < dimension; ++i )
156  {
157  lower[ i ] = interval.p[ 0 ][ i ];
158  upper[ i ] = interval.p[ 1 ][ i ];
159  anz[ i ] = interval.n[ i ];
160  }
161 
162  grid_ = new Grid( anz, lower, upper );
163 
164  boundaryDomainBlock_ = new dgf::BoundaryDomBlock( gridin, dimension );
165  }
166 
167 
168 
169  template< int dim, int dimworld, class ctype >
170  struct DGFGridInfo< SGrid< dim, dimworld, ctype > >
171  {
172  static int refineStepsForHalf ()
173  {
174  return 1;
175  }
176 
177  static double refineWeight ()
178  {
179  return 1.0 / double( 1 << dim );
180  }
181  };
182 
183 }
184 #endif // #ifndef DUNE_DGFS_HH