Dune Core Modules (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
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
48 typedef typename ElementIterator :: Entity Element ;
49 typedef typename Element :: EntitySeed ElementSeed ;
50
51 typedef typename IndexSet::IndexType Index;
52
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 >
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 {
248 // write all cubes to the "cube" block
249 gridout << std::endl << "CUBE" << std::endl;
250
251 // write all simplex elements
252 writeAllElements( elementSeeds, indexSet, cube, vertexIndex, gridout );
253
254 // write end marker for block
255 gridout << "#" << std::endl;
256 }
257 }
258
259 // write all boundaries to the "boundarysegments" block
260#if DUNE_GRID_EXPERIMENTAL_GRID_EXTENSIONS
261 gridout << std::endl << "BOUNDARYSEGMENTS" << std::endl;
262 for( ElementIterator it = gridView_.template begin< 0 >(); it != end; ++it )
263 {
264 const Element& element = *it ;
265 if( !it->hasBoundaryIntersections() )
266 continue;
267
268 const RefElement &refElement = RefElements::general( element.type() );
269
270 const IntersectionIterator iend = gridView_.iend( element ) ;
271 for( IntersectionIterator iit = gridView_.ibegin( element ); iit != iend; ++iit )
272 {
273 if( !iit->boundary() )
274 continue;
275
276 const int boundaryId = iit->boundaryId();
277 if( boundaryId <= 0 )
278 {
279 std::cerr << "Warning: Ignoring nonpositive boundary id: "
280 << boundaryId << "." << std::endl;
281 continue;
282 }
283
284 const int faceNumber = iit->indexInInside();
285 const unsigned int faceSize = refElement.size( faceNumber, 1, dimGrid );
286 std::vector< Index > vertices( faceSize );
287 for( unsigned int i = 0; i < faceSize; ++i )
288 {
289 const int j = refElement.subEntity( faceNumber, 1, i, dimGrid );
290 vertices[ i ] = vertexIndex[ indexSet.subIndex( element, j, dimGrid ) ];
291 }
292 gridout << boundaryId << " " << vertices[ 0 ];
293 for( unsigned int i = 1; i < faceSize; ++i )
294 gridout << " " << vertices[ i ];
295 gridout << std::endl;
296 }
297 }
298 gridout << "#" << std::endl << std::endl;
299#endif // #if DUNE_GRID_EXPERIMENTAL_GRID_EXTENSIONS
300
301 // add additional parameters given by the user
302 gridout << addParams.str() << std::endl;
303
304 gridout << std::endl << "#" << std::endl;
305 }
306
307 template< class GV >
309 write ( std::ostream &gridout) const
310 {
311 // empty vector means no new ordering
312 std::vector< Index > noNewOrdering ;
313 std::stringstream addParams;
314 write( gridout, noNewOrdering, addParams );
315 }
316
317 template< class GV >
319 write ( std::ostream &gridout, const std::stringstream& addParams ) const
320 {
321 // empty vector means no new ordering
322 std::vector< Index > noNewOrdering ;
323 write( gridout, noNewOrdering, addParams );
324 }
325
326 template< class GV >
327 inline void DGFWriter< GV >::write ( const std::string &fileName ) const
328 {
329 std::ofstream gridout( fileName.c_str() );
330 if( gridout )
331 write( gridout );
332 else
333 std::cerr << "Couldn't open file `"<< fileName << "'!"<< std::endl;
334 }
335
336}
337
338#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:61
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:153
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:268
@ cube
Cube element in any nonnegative dimension.
Definition: type.hh:274
@ simplex
Simplicial element in any nonnegative dimension.
Definition: type.hh:273
Base class for exceptions in Dune grid modules.
Definition: exceptions.hh:18
Grid view abstract base class.
Definition: gridview.hh:60
Index Set Interface base class.
Definition: indexidset.hh:76
IndexTypeImp IndexType
The type used for the indices.
Definition: indexidset.hh:90
IndexType size(GeometryType type) const
Return total number of entities of given geometry type in entity set .
Definition: indexidset.hh:220
int indexInInside() const
Local index of codim 1 entity in the inside() entity where intersection is contained in.
Definition: intersection.hh:374
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:279
This class provides access to geometric and topological properties of a reference element.
Definition: referenceelements.hh:354
int size(int c) const
number of subentities of codimension c
Definition: referenceelements.hh:381
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:417
Different resources needed by all grid implementations.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
const IndexSet & indexSet() const
obtain the index set
Definition: gridview.hh:173
Traits::Grid Grid
type of the grid
Definition: gridview.hh:78
const Grid & grid() const
obtain a const reference to the underlying hierarchic grid
Definition: gridview.hh:162
Traits::IntersectionIterator IntersectionIterator
type of the intersection iterator
Definition: gridview.hh:87
IntersectionIterator ibegin(const typename Codim< 0 > ::Entity &entity) const
obtain begin intersection iterator with respect to this view
Definition: gridview.hh:234
Traits::IndexSet IndexSet
type of the index set
Definition: gridview.hh:81
IntersectionIterator iend(const typename Codim< 0 > ::Entity &entity) const
obtain end intersection iterator with respect to this view
Definition: gridview.hh:241
@ dimension
The dimension of the grid.
Definition: gridview.hh:128
Dune namespace.
Definition: alignment.hh:11
Static tag representing a codimension.
Definition: dimension.hh:22
Class providing access to the singletons of the reference elements.
Definition: referenceelements.hh:752
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)