dune-grid  2.2.1
albertagrid/dgfparser.hh
Go to the documentation of this file.
1 #ifndef DUNE_ALBERTA_DGFPARSER_HH
2 #define DUNE_ALBERTA_DGFPARSER_HH
3 
4 #include <vector>
5 
8 
11 
14 
15 #if HAVE_ALBERTA
16 
17 namespace Dune
18 {
19 
20  // forward declaration
21  // -------------------
22 
23  template < class GridImp, template < class > class IntersectionImp >
24  class Intersection;
25 
26  // DGFGridFactory for AlbertaGrid
27  // ------------------------------
28 
29  template< int dim, int dimworld >
30  struct DGFGridFactory< AlbertaGrid< dim, dimworld > >
31  {
33  const static int dimension = Grid::dimension;
34  typedef MPIHelper::MPICommunicator MPICommunicatorType;
35  typedef typename Grid::template Codim<0>::Entity Element;
36  typedef typename Grid::template Codim<dimension>::Entity Vertex;
38 
39  explicit DGFGridFactory ( std::istream &input,
40  MPICommunicatorType comm = MPIHelper::getCommunicator() );
41  explicit DGFGridFactory ( const std::string &filename,
42  MPICommunicatorType comm = MPIHelper::getCommunicator() );
43 
44  Grid *grid () const
45  {
46  return grid_;
47  }
48 
49  template< class Intersection >
50  bool wasInserted ( const Intersection &intersection ) const
51  {
52  return factory_.wasInserted( intersection );
53  }
54 
55  template< class Intersection >
56  int boundaryId ( const Intersection &intersection ) const
57  {
58  return Grid::getRealImplementation( intersection ).boundaryId();
59  }
60 
61  // return true if boundary paramters found
62  bool haveBoundaryParameters () const
63  {
64  return dgf_.haveBndParameters;
65  }
66 
67  template < class GG, template < class > class II >
69  boundaryParameter ( const Intersection< GG, II > & intersection ) const
70  {
72  typename Intersection::EntityPointer inside = intersection.inside();
73  const typename Intersection::Entity & entity = *inside;
74  const int face = intersection.indexInInside();
75 
76  const GenericReferenceElement< double, dimension > & refElem =
77  GenericReferenceElements< double, dimension >::general( entity.type() );
78  int corners = refElem.size( face, 1, dimension );
79  std :: vector< unsigned int > bound( corners );
80  for( int i=0; i < corners; ++i )
81  {
82  const int k = refElem.subEntity( face, 1, i, dimension );
83  bound[ i ] = factory_.insertionIndex( *entity.template subEntity< dimension >( k ) );
84  }
85 
86  DuneGridFormatParser::facemap_t::key_type key( bound, false );
87  const DuneGridFormatParser::facemap_t::const_iterator pos = dgf_.facemap.find( key );
88  if( pos != dgf_.facemap.end() )
89  return dgf_.facemap.find( key )->second.second;
90  else
92  }
93 
94  template< int codim >
95  int numParameters () const
96  {
97  if( codim == 0 )
98  return dgf_.nofelparams;
99  else if( codim == dimension )
100  return dgf_.nofvtxparams;
101  else
102  return 0;
103  }
104 
105  std::vector< double > &parameter ( const Element &element )
106  {
107  if( numParameters< 0 >() <= 0 )
108  {
109  DUNE_THROW( InvalidStateException,
110  "Calling DGFGridFactory::parameter is only allowed if there are parameters." );
111  }
112  return dgf_.elParams[ factory_.insertionIndex( element ) ];
113  }
114 
115  std::vector< double > &parameter ( const Vertex &vertex )
116  {
117  if( numParameters< dimension >() <= 0 )
118  {
119  DUNE_THROW( InvalidStateException,
120  "Calling DGFGridFactory::parameter is only allowed if there are parameters." );
121  }
122  return dgf_.vtxParams[ factory_.insertionIndex( vertex ) ];
123  }
124 
125  private:
126  bool generate( std::istream &input );
127 
128  Grid *grid_;
129  GridFactory factory_;
131  };
132 
133 
134 
135  // DGFGridInfo for AlbertaGrid
136  // ---------------------------
137 
138  template< int dim, int dimworld >
139  struct DGFGridInfo< AlbertaGrid< dim, dimworld > >
140  {
141  static int refineStepsForHalf ()
142  {
143  return dim;
144  }
145 
146  static double refineWeight ()
147  {
148  return 0.5;
149  }
150  };
151 
152 
153 
154  // Implementation of DGFGridFactory for AlbertaGrid
155  // ------------------------------------------------
156 
157  template< int dim, int dimworld >
158  inline DGFGridFactory< AlbertaGrid< dim, dimworld > >
159  ::DGFGridFactory ( std::istream &input, MPICommunicatorType comm )
160  : dgf_( 0, 1 )
161  {
162  input.clear();
163  input.seekg( 0 );
164  if( !input )
165  DUNE_THROW(DGFException, "Error resetting input stream." );
166  generate( input );
167  }
168 
169 
170  template< int dim, int dimworld >
172  ::DGFGridFactory ( const std::string &filename, MPICommunicatorType comm )
173  : dgf_( 0, 1 )
174  {
175  std::ifstream input( filename.c_str() );
176  if( !input )
177  DUNE_THROW( DGFException, "Macrofile " << filename << " not found." );
178  if( !generate( input ) )
179  grid_ = new AlbertaGrid< dim, dimworld >( filename.c_str() );
180  input.close();
181  }
182 
183 }
184 
185 #endif // #if HAVE_ALBERTA
186 
187 #endif // #ifndef DUNE_ALBERTA_DGFPARSER_HH