dune-grid  2.2.0
dgfoned.hh
Go to the documentation of this file.
1 #ifndef DUNE_GRID_FILE_IO_DGFPARSER_DGFONED_HH
2 #define DUNE_GRID_FILE_IO_DGFPARSER_DGFONED_HH
3 
4 //- C++ includes
5 #include <algorithm>
6 #include <fstream>
7 #include <iostream>
8 #include <istream>
9 #include <vector>
10 
11 //- dune-common includes
12 #include <dune/common/exceptions.hh>
13 
14 //- dune-grid includes
16 #include <dune/grid/onedgrid.hh>
17 
18 //- local includes
19 #include "dgfparser.hh"
20 
21 
22 namespace
23 {
24  // helper method used below
25  double getfirst ( std::vector< double > v )
26  {
27  return v[ 0 ];
28  }
29 } // end anonymous namespace
30 
31 
32 
33 namespace Dune
34 {
35 
36  // DGFGridInfo
37  // -----------
38 
39  template< >
40  struct DGFGridInfo< OneDGrid >
41  {
42  static int refineStepsForHalf ()
43  {
44  return 1;
45  }
46 
47  static double refineWeight ()
48  {
49  return 0.5;
50  }
51  };
52 
53 
54 
55  // DGFGridFactory< OneDGrid >
56  // --------------------------
57 
58  template< >
60  {
62  typedef OneDGrid Grid;
64  const static int dimension = Grid::dimension;
66  typedef MPIHelper::MPICommunicator MPICommunicatorType;
67 
69  explicit DGFGridFactory ( std::istream &input,
70  MPICommunicatorType comm = MPIHelper::getCommunicator() )
71  : grid_( 0 ),
72  emptyParameters_( 0 )
73  {
74  generate( input, comm );
75  }
76 
78  explicit DGFGridFactory ( const std::string &filename,
79  MPICommunicatorType comm = MPIHelper::getCommunicator() )
80  : grid_( 0 ),
81  emptyParameters_( 0 )
82  {
83  std::ifstream input( filename.c_str() );
84  generate( input, comm );
85  }
86 
88  Grid *grid () const
89  {
90  return grid_;
91  }
92 
94  template < class GG, template< class > class II >
95  bool wasInserted ( const Dune::Intersection< GG, II > &intersection ) const
96  {
97  return false;
98  }
99 
100  template < class GG, template< class > class II >
101  int boundaryId ( const Dune::Intersection< GG, II > &intersection ) const
102  {
103  // OneDGrid returns boundary segment index;
104  // we return the index as the method boundaryId is deprecated
105  return intersection.boundarySegmentIndex();
106  }
107 
109  template< class Entity >
110  int numParameters ( const Entity & ) const
111  {
112  return 0;
113  }
114 
116  template< int codim >
117  int numParameters () const
118  {
119  return 0;
120  }
121 
122  template< class Entity >
123  std::vector< double >& parameter ( const Entity &entity )
124  {
125  return parameter< Entity::codimension >( entity );
126  }
127 
129  template< int codim >
130  std::vector< double > &parameter ( const typename Grid::Codim< codim >::Entity &element )
131  {
132  return emptyParameters_;
133  }
134 
137  {
138  return false;
139  }
140 
142  template < class GG, template< class > class II >
144  {
146  }
147 
148  private:
149  // generate grid
150  void generate ( std::istream &input, MPICommunicatorType comm );
151 
152  Grid *grid_;
153  std::vector< double > emptyParameters_;
154  };
155 
156 
157 
158  // Implementation of DGFGridFactory< OneDGrid >
159  // --------------------------------------------
160 
161  inline void DGFGridFactory< OneDGrid >::generate ( std::istream &input, MPICommunicatorType comm )
162  {
163  // try to open interval block
164  dgf::IntervalBlock intervalBlock( input );
165 
166  // try to open vertex block
167  int dimensionworld = Grid::dimensionworld;
168  dgf::VertexBlock vertexBlock( input, dimensionworld );
169 
170  // check at least one block is active
171  if( !( vertexBlock.isactive() || intervalBlock.isactive() ))
172  DUNE_THROW( DGFException, "No readable block found" );
173 
174  std::vector< std::vector< double > > vertices;
175 
176  // read vertices first
177  if( vertexBlock.isactive() )
178  {
179  int nparameter = 0;
180  std::vector< std::vector< double > > parameter;
181  vertexBlock.get( vertices, parameter, nparameter );
182 
183  if( nparameter > 0 )
184  std::cerr << "Warning: vertex parameters will be ignored" << std::endl;
185  }
186 
187  // get vertices from interval block
188  if ( intervalBlock.isactive() )
189  {
190  if( intervalBlock.dimw() != dimensionworld )
191  {
192  DUNE_THROW( DGFException, "Error: wrong coordinate dimension in interval block \
193  (got " << intervalBlock.dimw() << ", expected " << dimensionworld << ")" );
194  }
195 
196  int nintervals = intervalBlock.numIntervals();
197  for( int i = 0; i < nintervals; ++i )
198  intervalBlock.getVtx( i, vertices );
199  }
200 
201  // copy to vector of doubles
202  std::vector< double > vtx( vertices.size() );
203  transform( vertices.begin(), vertices.end(), vtx.begin(), getfirst );
204 
205  // remove duplicates
206  std::sort( vtx.begin(), vtx.end() );
207  std::vector< double >::iterator it = std::unique( vtx.begin(), vtx.end() );
208  vtx.erase( it, vtx.end() );
209  if( vertices.size() != vtx.size() )
210  std::cerr << "Warning: removed duplicate vertices" << std::endl;
211 
212  // create grid
213  grid_ = new OneDGrid( vtx );
214  }
215 
216 } // end namespace Dune
217 
218 #endif // #ifndef DUNE_GRID_FILE_IO_DGFPARSER_DGFONED_HH