Dune Core Modules (2.3.1)

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
15#include <dune/geometry/referenceelements.hh>
16
17namespace Dune
18{
19
29 template< class GV >
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 typedef typename GridView::template Codim< dimGrid >::EntityPointer VertexPointer;
48
49 typedef typename ElementIterator :: Entity Element ;
50 typedef typename Element :: EntityPointer ElementPointer;
51 typedef typename Element :: EntitySeed ElementSeed ;
52
53 typedef typename IndexSet::IndexType Index;
54
57
58 public:
63 DGFWriter ( const GridView &gridView )
64 : gridView_( gridView )
65 {}
66
74 void write ( std::ostream &gridout,
75 const std::vector< Index >& newElemOrder,
76 const std::stringstream& addParams = std::stringstream() ) const;
77
82 void write ( std::ostream &gridout ) const;
83
90 void write ( std::ostream &gridout,
91 const std::stringstream& addParams ) const;
92
97 void write ( const std::string &fileName ) const;
98
99 protected:
100 GridView gridView_;
101
102 protected:
104 // helper methods
106
107 // write all elements of type elementType
108 void writeAllElements( const std::vector<ElementSeed>& elementSeeds,
109 const IndexSet& indexSet,
110 const GeometryType& elementType,
111 const std::vector< Index >& vertexIndex,
112 std::ostream &gridout ) const
113 {
114 if( elementSeeds.size() > 0 )
115 {
116 // perform grid traversal based on new element ordering
117 typedef typename std::vector<ElementSeed> :: const_iterator iterator ;
118 const iterator end = elementSeeds.end();
119 for( iterator it = elementSeeds.begin(); it != end ; ++ it )
120 {
121 // convert entity seed into entity pointer
122 const ElementPointer ep = gridView_.grid().entityPointer( *it );
123 // write element
124 writeElement( *ep, indexSet, elementType, vertexIndex, gridout );
125 }
126 }
127 else
128 {
129 // perform default grid traversal
130 const ElementIterator end = gridView_.template end< 0 >();
131 for( ElementIterator it = gridView_.template begin< 0 >(); it != end; ++it )
132 {
133 // write element
134 writeElement( *it, indexSet, elementType, vertexIndex, gridout );
135 }
136 }
137 }
138
139 // write one element
140 void writeElement( const Element& element,
141 const IndexSet& indexSet,
142 const GeometryType& elementType,
143 const std::vector< Index >& vertexIndex,
144 std::ostream &gridout ) const
145 {
146 // if element's type is not the same as the type to write the return
147 if( element.type() != elementType )
148 return ;
149
150 // get vertex numbers of the element
151 const size_t vxSize = element.template count< Element::dimension > ();
152 std::vector<Index> vertices(vxSize);
153 for( size_t i = 0; i < vxSize; ++i )
154 vertices[ i ] = vertexIndex[ indexSet.subIndex( element, i, dimGrid ) ];
155
156 gridout << vertices[ 0 ];
157 for( size_t i = 1; i < vxSize; ++i )
158 gridout << " " << vertices[ i ];
159 gridout << std::endl;
160 }
161 };
162
163
164 template< class GV >
166 write ( std::ostream &gridout,
167 const std::vector< Index >& newElemOrder,
168 const std::stringstream& addParams ) const
169 {
170 // set the stream to full double precision
171 gridout.setf( std::ios_base::scientific, std::ios_base::floatfield );
172 gridout.precision( 16 );
173
174 const IndexSet &indexSet = gridView_.indexSet();
175
176 // vector containing entity seed (only needed if new ordering is given)
177 std::vector< ElementSeed > elementSeeds;
178
179 // if ordering was provided
180 const size_t orderSize = newElemOrder.size() ;
181 if( orderSize == indexSet.size( 0 ) )
182 {
183 const ElementIterator end = gridView_.template end< 0 >();
184 ElementIterator it = gridView_.template begin< 0 >();
185
186 if( it != end )
187 {
188 elementSeeds.resize( orderSize, (*it).seed() ) ;
189 size_t countElements = 0 ;
190 for( ; it != end; ++it, ++countElements )
191 {
192 const Element& element = *it ;
193 assert( newElemOrder[ indexSet.index( element ) ] < orderSize );
194 elementSeeds[ newElemOrder[ indexSet.index( element ) ] ] = element.seed();
195 }
196
197 // make sure that the size of the index set is equal
198 // to the number of counted elements
199 if( countElements != orderSize )
200 DUNE_THROW(InvalidStateException,"DGFWriter::write: IndexSet not consecutive");
201 }
202 }
203
204 // write DGF header
205 gridout << "DGF" << std::endl;
206
207 const Index vxSize = indexSet.size( dimGrid );
208 std::vector< Index > vertexIndex( vxSize, vxSize );
209
210 gridout << "%" << " Elements = " << indexSet.size( 0 ) << " | Vertices = " << vxSize << std::endl;
211
212 // write all vertices into the "vertex" block
213 gridout << std::endl << "VERTEX" << std::endl;
214 Index vertexCount = 0;
215 const ElementIterator end = gridView_.template end< 0 >();
216 for( ElementIterator it = gridView_.template begin< 0 >(); it != end; ++it )
217 {
218 const Element& element = *it ;
219 const int numCorners = element.template count< dimGrid > ();
220 for( int i=0; i<numCorners; ++i )
221 {
222 const Index vxIndex = indexSet.subIndex( element, i, dimGrid );
223 assert( vxIndex < vxSize );
224 if( vertexIndex[ vxIndex ] == vxSize )
225 {
226 vertexIndex[ vxIndex ] = vertexCount++;
227 gridout << element.geometry().corner( i ) << std::endl;
228 }
229 }
230 }
231 gridout << "#" << std::endl;
232 if( vertexCount != vxSize )
233 DUNE_THROW( GridError, "Index set reports wrong number of vertices." );
234
235 if( dimGrid > 1 )
236 {
237 // type of element to write
238 GeometryType simplex( GeometryType::simplex, dimGrid );
239
240 // only write simplex block if grid view contains simplices
241 if( indexSet.size( simplex ) > 0 )
242 {
243 // write all simplices to the "simplex" block
244 gridout << std::endl << "SIMPLEX" << std::endl;
245
246 // write all simplex elements
247 writeAllElements( elementSeeds, indexSet, simplex, vertexIndex, gridout );
248
249 // write end marker for block
250 gridout << "#" << std::endl;
251 }
252 }
253
254 {
255 // cube geometry type
256 GeometryType cube( GeometryType::cube, dimGrid );
257
258 // only write cube block if grid view contains cubes
259 if( indexSet.size( cube ) > 0 )
260 {
261 // write all cubes to the "cube" block
262 gridout << std::endl << "CUBE" << std::endl;
263
264 // write all simplex elements
265 writeAllElements( elementSeeds, indexSet, cube, vertexIndex, gridout );
266
267 // write end marker for block
268 gridout << "#" << std::endl;
269 }
270 }
271
272 // write all boundaries to the "boundarysegments" block
273#if DUNE_GRID_EXPERIMENTAL_GRID_EXTENSIONS
274 gridout << std::endl << "BOUNDARYSEGMENTS" << std::endl;
275 for( ElementIterator it = gridView_.template begin< 0 >(); it != end; ++it )
276 {
277 const Element& element = *it ;
278 if( !it->hasBoundaryIntersections() )
279 continue;
280
281 const RefElement &refElement = RefElements::general( element.type() );
282
283 const IntersectionIterator iend = gridView_.iend( element ) ;
284 for( IntersectionIterator iit = gridView_.ibegin( element ); iit != iend; ++iit )
285 {
286 if( !iit->boundary() )
287 continue;
288
289 const int boundaryId = iit->boundaryId();
290 if( boundaryId <= 0 )
291 {
292 std::cerr << "Warning: Ignoring nonpositive boundary id: "
293 << boundaryId << "." << std::endl;
294 continue;
295 }
296
297 const int faceNumber = iit->indexInInside();
298 const unsigned int faceSize = refElement.size( faceNumber, 1, dimGrid );
299 std::vector< Index > vertices( faceSize );
300 for( unsigned int i = 0; i < faceSize; ++i )
301 {
302 const int j = refElement.subEntity( faceNumber, 1, i, dimGrid );
303 vertices[ i ] = vertexIndex[ indexSet.subIndex( element, j, dimGrid ) ];
304 }
305 gridout << boundaryId << " " << vertices[ 0 ];
306 for( unsigned int i = 1; i < faceSize; ++i )
307 gridout << " " << vertices[ i ];
308 gridout << std::endl;
309 }
310 }
311 gridout << "#" << std::endl << std::endl;
312#endif // #if DUNE_GRID_EXPERIMENTAL_GRID_EXTENSIONS
313
314 // add additional parameters given by the user
315 gridout << addParams.str() << std::endl;
316
317 gridout << std::endl << "#" << std::endl;
318 }
319
320 template< class GV >
322 write ( std::ostream &gridout) const
323 {
324 // empty vector means no new ordering
325 std::vector< Index > noNewOrdering ;
326 std::stringstream addParams;
327 write( gridout, noNewOrdering, addParams );
328 }
329
330 template< class GV >
332 write ( std::ostream &gridout, const std::stringstream& addParams ) const
333 {
334 // empty vector means no new ordering
335 std::vector< Index > noNewOrdering ;
336 write( gridout, noNewOrdering, addParams );
337 }
338
339 template< class GV >
340 inline void DGFWriter< GV >::write ( const std::string &fileName ) const
341 {
342 std::ofstream gridout( fileName.c_str() );
343 if( gridout )
344 write( gridout );
345 else
346 std::cerr << "Couldn't open file `"<< fileName << "'!"<< std::endl;
347 }
348
349}
350
351#endif // #ifndef DUNE_DGFWRITER_HH
write a GridView to a DGF file
Definition: dgfwriter.hh:31
static const int dimGrid
dimension of the grid
Definition: dgfwriter.hh:41
DGFWriter(const GridView &gridView)
constructor
Definition: dgfwriter.hh:63
void write(std::ostream &gridout, const std::vector< Index > &newElemOrder, const std::stringstream &addParams=std::stringstream()) const
write the GridView into a std::ostream
Definition: dgfwriter.hh:166
GV GridView
type of grid view
Definition: dgfwriter.hh:36
GridView::Grid Grid
type of underlying hierarchical grid
Definition: dgfwriter.hh:38
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:25
@ cube
Cube element in any nonnegative dimension.
Definition: type.hh:31
@ simplex
Simplicial element in any nonnegative dimension.
Definition: type.hh:30
Base class for exceptions in Dune grid modules.
Definition: exceptions.hh:18
Grid view abstract base class.
Definition: gridview.hh:57
const IndexSet & indexSet() const
obtain the index set
Definition: gridview.hh:158
Traits::Grid Grid
type of the grid
Definition: gridview.hh:67
const Grid & grid() const
obtain a const reference to the underlying hierarchic grid
Definition: gridview.hh:147
Codim< cd >::Iterator end() const
obtain end iterator for this view
Definition: gridview.hh:196
Traits::IntersectionIterator IntersectionIterator
type of the intersection iterator
Definition: gridview.hh:76
IntersectionIterator ibegin(const typename Codim< 0 > ::Entity &entity) const
obtain begin intersection iterator with respect to this view
Definition: gridview.hh:219
Traits::IndexSet IndexSet
type of the index set
Definition: gridview.hh:70
IntersectionIterator iend(const typename Codim< 0 > ::Entity &entity) const
obtain end intersection iterator with respect to this view
Definition: gridview.hh:226
@ dimension
The dimension of the grid.
Definition: gridview.hh:120
Index Set Interface base class.
Definition: indexidset.hh:78
IndexType size(GeometryType type) const
Return total number of entities of given geometry type in entity set .
Definition: indexidset.hh:204
IndexTypeImp IndexType
The type used for the indices.
Definition: indexidset.hh:85
int indexInInside() const
Local index of codim 1 entity in the inside() entity where intersection is contained in.
Definition: intersection.hh:363
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:307
This class provides access to geometric and topological properties of a reference element....
Definition: referenceelements.hh:58
int size(int c) const
number of subentities of codimension c
Definition: referenceelements.hh:86
int subEntity(int i, int c, int ii, int cc) const
obtain number of ii-th subentity with codim cc of (i,c)
Definition: referenceelements.hh:122
Different resources needed by all grid implementations.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:244
Dune namespace.
Definition: alignment.hh:14
Class providing access to the singletons of the reference elements. Special methods are available for...
Definition: referenceelements.hh:563
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 12, 23:30, 2024)