dune-grid  2.5.0
dgfwriter.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_DGFWRITER_HH
4 #define DUNE_DGFWRITER_HH
5 
11 #include <fstream>
12 #include <vector>
13 
14 #include <dune/grid/common/grid.hh>
15 #include <dune/geometry/referenceelements.hh>
16 
17 namespace Dune
18 {
19 
29  template< class GV >
30  class DGFWriter
31  {
32  typedef DGFWriter< GV > This;
33 
34  public:
36  typedef GV GridView;
38  typedef typename GridView::Grid Grid;
39 
41  static const int dimGrid = GridView::dimension;
42 
43  private:
44  typedef typename GridView::IndexSet IndexSet;
45  typedef typename GridView::template Codim< 0 >::Iterator ElementIterator;
46  typedef typename GridView::IntersectionIterator IntersectionIterator;
47 
48  typedef typename ElementIterator :: Entity Element ;
49  typedef typename Element :: EntitySeed ElementSeed ;
50 
51  typedef typename IndexSet::IndexType Index;
52 
53  typedef ReferenceElement< typename Grid::ctype, dimGrid > RefElement;
54  typedef ReferenceElements< typename Grid::ctype, dimGrid > RefElements;
55 
56  public:
61  DGFWriter ( const GridView &gridView )
62  : gridView_( gridView )
63  {}
64 
72  void write ( std::ostream &gridout,
73  const std::vector< Index >& newElemOrder,
74  const std::stringstream& addParams = std::stringstream() ) const;
75 
80  void write ( std::ostream &gridout ) const;
81 
88  void write ( std::ostream &gridout,
89  const std::stringstream& addParams ) const;
90 
95  void write ( const std::string &fileName ) const;
96 
97  protected:
98  GridView gridView_;
99 
100  protected:
102  // helper methods
104 
105  // write all elements of type elementType
106  void writeAllElements( const std::vector<ElementSeed>& elementSeeds,
107  const IndexSet& indexSet,
108  const GeometryType& elementType,
109  const std::vector< Index >& vertexIndex,
110  std::ostream &gridout ) const
111  {
112  if (!elementSeeds.empty()) {
113  // perform grid traversal based on new element ordering
114  for (const auto& seed : elementSeeds) {
115  const Element element = gridView_.grid().entity(seed);
116  writeElement(element, indexSet, elementType, vertexIndex, gridout);
117  }
118  }
119  else {
120  // perform default grid traversal
121  for (const auto& element : elements(gridView_))
122  writeElement(element, indexSet, elementType, vertexIndex, gridout);
123  }
124  }
125 
126  // write one element
127  void writeElement( const Element& element,
128  const IndexSet& indexSet,
129  const GeometryType& elementType,
130  const std::vector< Index >& vertexIndex,
131  std::ostream &gridout ) const
132  {
133  // if element's type is not the same as the type to write the return
134  if( element.type() != elementType )
135  return ;
136 
137  // get vertex numbers of the element
138  const size_t vxSize = element.subEntities( Element::dimension );
139  std::vector<Index> vertices(vxSize);
140  for( size_t i = 0; i < vxSize; ++i )
141  vertices[ i ] = vertexIndex[ indexSet.subIndex( element, i, dimGrid ) ];
142 
143  gridout << vertices[ 0 ];
144  for( size_t i = 1; i < vxSize; ++i )
145  gridout << " " << vertices[ i ];
146  gridout << std::endl;
147  }
148  };
149 
150 
151  template< class GV >
152  inline void DGFWriter< GV >::
153  write ( std::ostream &gridout,
154  const std::vector< Index >& newElemOrder,
155  const std::stringstream& addParams ) const
156  {
157  // set the stream to full double precision
158  gridout.setf( std::ios_base::scientific, std::ios_base::floatfield );
159  gridout.precision( 16 );
160 
161  const IndexSet &indexSet = gridView_.indexSet();
162 
163  // vector containing entity seed (only needed if new ordering is given)
164  std::vector< ElementSeed > elementSeeds;
165 
166  // if ordering was provided
167  const size_t orderSize = newElemOrder.size() ;
168  if( orderSize == indexSet.size( 0 ) )
169  {
170  const ElementIterator end = gridView_.template end< 0 >();
171  ElementIterator it = gridView_.template begin< 0 >();
172 
173  if( it != end )
174  {
175  elementSeeds.resize( orderSize, (*it).seed() ) ;
176  size_t countElements = 0 ;
177  for( ; it != end; ++it, ++countElements )
178  {
179  const Element& element = *it ;
180  assert( newElemOrder[ indexSet.index( element ) ] < orderSize );
181  elementSeeds[ newElemOrder[ indexSet.index( element ) ] ] = element.seed();
182  }
183 
184  // make sure that the size of the index set is equal
185  // to the number of counted elements
186  if( countElements != orderSize )
187  DUNE_THROW(InvalidStateException,"DGFWriter::write: IndexSet not consecutive");
188  }
189  }
190 
191  // write DGF header
192  gridout << "DGF" << std::endl;
193 
194  const Index vxSize = indexSet.size( dimGrid );
195  std::vector< Index > vertexIndex( vxSize, vxSize );
196 
197  gridout << "%" << " Elements = " << indexSet.size( 0 ) << " | Vertices = " << vxSize << std::endl;
198 
199  // write all vertices into the "vertex" block
200  gridout << std::endl << "VERTEX" << std::endl;
201  Index vertexCount = 0;
202  const ElementIterator end = gridView_.template end< 0 >();
203  for( ElementIterator it = gridView_.template begin< 0 >(); it != end; ++it )
204  {
205  const Element& element = *it ;
206  const int numCorners = element.subEntities( dimGrid );
207  for( int i=0; i<numCorners; ++i )
208  {
209  const Index vxIndex = indexSet.subIndex( element, i, dimGrid );
210  assert( vxIndex < vxSize );
211  if( vertexIndex[ vxIndex ] == vxSize )
212  {
213  vertexIndex[ vxIndex ] = vertexCount++;
214  gridout << element.geometry().corner( i ) << std::endl;
215  }
216  }
217  }
218  gridout << "#" << std::endl;
219  if( vertexCount != vxSize )
220  DUNE_THROW( GridError, "Index set reports wrong number of vertices." );
221 
222  if( dimGrid > 1 )
223  {
224  // type of element to write
225  GeometryType simplex( GeometryType::simplex, dimGrid );
226 
227  // only write simplex block if grid view contains simplices
228  if( indexSet.size( simplex ) > 0 )
229  {
230  // write all simplices to the "simplex" block
231  gridout << std::endl << "SIMPLEX" << std::endl;
232 
233  // write all simplex elements
234  writeAllElements( elementSeeds, indexSet, simplex, vertexIndex, gridout );
235 
236  // write end marker for block
237  gridout << "#" << std::endl;
238  }
239  }
240 
241  {
242  // cube geometry type
243  GeometryType cube( GeometryType::cube, dimGrid );
244 
245  // only write cube block if grid view contains cubes
246  if( indexSet.size( cube ) > 0 )
247&#