Dune Core Modules (2.9.0)

writeparalleldgf.hh
Go to the documentation of this file.
1#ifndef DUNE_DGFWRITER_HH
2#define DUNE_DGFWRITER_HH
3
9#include <fstream>
10#include <vector>
11
13#include <dune/geometry/referenceelements.hh>
14
24template< class GV >
26{
27 typedef DGFWriter< GV > This;
28
29public:
31 typedef GV GridView;
33 typedef typename GridView::Grid Grid;
34
36 static const int dimGrid = GridView::dimension;
37
38private:
39 typedef typename GridView::IndexSet IndexSet;
40 typedef typename GridView::template Codim< 0 >::Iterator ElementIterator;
41 typedef typename GridView::IntersectionIterator IntersectionIterator;
42 typedef typename GridView::template Codim< dimGrid >::EntityPointer VertexPointer;
43
44 typedef typename ElementIterator :: Entity Element ;
45 typedef typename Element :: EntityPointer ElementPointer;
46 typedef typename Element :: EntitySeed ElementSeed ;
47
48 typedef typename IndexSet::IndexType Index;
49
52
53public:
58 DGFWriter ( const GridView &gridView )
59 : gridView_( gridView )
60 {}
61
66 template <class LoadBalanceHandle>
67 std::string write ( const std::string &fileName, const LoadBalanceHandle &ldb,
68 const int size, const int rank ) const;
69 template <class LoadBalanceHandle>
70 void write ( const std::string &fileName, const LoadBalanceHandle &ldb,
71 const int size ) const;
72
73protected:
74 GridView gridView_;
75
76protected:
78 // helper methods
80 // write one element
81 void writeElement( const Element& element,
82 const IndexSet& indexSet,
83 const Dune::GeometryType& elementType,
84 const std::vector< Index >& vertexIndex,
85 std::ostream &gridout ) const
86 {
87 // if element's type is not the same as the type to write the return
88 if( element.type() != elementType )
89 return ;
90
91 // get vertex numbers of the element
92 const size_t vxSize = element.subEntities( Element::dimension );
93 Index vertices[ vxSize ];
94 for( size_t i = 0; i < vxSize; ++i )
95 vertices[ i ] = vertexIndex[ indexSet.subIndex( element, i, dimGrid ) ];
96
97 gridout << vertices[ 0 ];
98 for( size_t i = 1; i < vxSize; ++i )
99 gridout << " " << vertices[ i ];
100 gridout << std::endl;
101 }
102};
103
104template <class GV>
105template <class LoadBalanceHandle>
106inline std::string DGFWriter< GV >::write ( const std::string &fileName, const LoadBalanceHandle &ldb, const int size, const int rank ) const
107{
108 std::stringstream newName;
109 newName << fileName << "." << size << "." << rank;
110 std::ofstream gridout( newName.str().c_str() );
111 // set the stream to full double precision
112 gridout.setf( std::ios_base::scientific, std::ios_base::floatfield );
113 gridout.precision( 16 );
114
115 const IndexSet &indexSet = gridView_.indexSet();
116
117 // write DGF header
118 gridout << "DGF" << std::endl;
119
120 const Index vxSize = indexSet.size( dimGrid );
121 std::vector< Index > vertexIndex( vxSize, vxSize );
122
123 gridout << "%" << " Elements = " << indexSet.size( 0 ) << " | Vertices = " << vxSize << std::endl;
124
125 // write all vertices into the "vertex" block
126 gridout << std::endl << "VERTEX" << std::endl;
127 Index vertexCount = 0;
128
129 // vector containing entity seed (only needed if new ordering is given)
130 std::vector< ElementSeed > elementSeeds;
131
132 size_t countElements = 0 ;
133 {
134 const ElementIterator end = gridView_.template end< 0 >();
135 ElementIterator it = gridView_.template begin< 0 >();
136 for( ; it != end; ++it)
137 {
138 const Element& element = *it ;
139 if( ldb(element)==rank ) // test if element is assigned to this process
140 {
141 // push element into seed vector
142 elementSeeds.push_back( element.seed() ) ;
143 countElements++;
144
145 // write vertices
146 const int numCorners = element.subEntities( dimGrid );
147 for( int i=0; i<numCorners; ++i )
148 {
149 const Index vxIndex = indexSet.subIndex( element, i, dimGrid );
150 assert( vxIndex < vxSize );
151 if( vertexIndex[ vxIndex ] == vxSize )
152 {
153 vertexIndex[ vxIndex ] = vertexCount++;
154 gridout << element.geometry().corner( i ) << std::endl;
155 }
156 }
157 }
158 }
159 }
160 gridout << "#" << std::endl;
161
162 // type of element to write
164
165 typedef typename std::vector<ElementSeed> :: const_iterator iterator ;
166 const iterator end = elementSeeds.end();
167
168 // only write simplex block if grid view contains simplices
169 if( indexSet.size( simplex ) > 0 )
170 {
171 // write all simplices to the "simplex" block
172 gridout << std::endl << "SIMPLEX" << std::endl;
173
174 // write all simplex elements
175 // perform grid traversal based on new element ordering
176 for( iterator it = elementSeeds.begin(); it != end ; ++ it )
177 {
178 // convert entity seed into entity pointer
179 const ElementPointer ep = gridView_.grid().entityPointer( *it );
180 // write element
181 writeElement( *ep, indexSet, simplex, vertexIndex, gridout );
182 }
183 // write end marker for block
184 gridout << "#" << std::endl;
185 }
186
187 {
188 // cube geometry type
190
191 // only write cube block if grid view contains cubes
192 if( indexSet.size( cube ) > 0 )
193 {
194 // write all cubes to the "cube" block
195 gridout << std::endl << "CUBE" << std::endl;
196 for( iterator it = elementSeeds.begin(); it != end ; ++ it )
197 {
198 // convert entity seed into entity pointer
199 const ElementPointer ep = gridView_.grid().entityPointer( *it );
200 // write element
201 writeElement( *ep, indexSet, cube, vertexIndex, gridout );
202 }
203
204 // write end marker for block
205 gridout << "#" << std::endl;
206 }
207 }
208
209 // write all boundaries to the "boundarysegments" block
210 gridout << std::endl << "BOUNDARYSEGMENTS" << std::endl;
211 for( iterator it = elementSeeds.begin(); it != end ; ++ it )
212 {
213 // convert entity seed into entity pointer
214 const ElementPointer ep = gridView_.grid().entityPointer( *it );
215 const Element& element = *ep;
216 if( !element.hasBoundaryIntersections() )
217 continue;
218
219 const RefElement &refElement = RefElements::general( element.type() );
220
221 const IntersectionIterator iend = gridView_.iend( element ) ;
222 for( IntersectionIterator iit = gridView_.ibegin( element ); iit != iend; ++iit )
223 {
224 if( !iit->boundary() )
225 continue;
226
227 const int boundaryId = iit->boundaryId();
228 if( boundaryId <= 0 )
229 {
230 std::cerr << "Warning: Ignoring nonpositive boundary id: "
231 << boundaryId << "." << std::endl;
232 continue;
233 }
234
235 const int faceNumber = iit->indexInInside();
236 const unsigned int faceSize = refElement.size( faceNumber, 1, dimGrid );
237 std::vector< Index > vertices( faceSize );
238 for( unsigned int i = 0; i < faceSize; ++i )
239 {
240 const int j = refElement.subEntity( faceNumber, 1, i, dimGrid );
241 vertices[ i ] = vertexIndex[ indexSet.subIndex( element, j, dimGrid ) ];
242 }
243 gridout << boundaryId << " " << vertices[ 0 ];
244 for( unsigned int i = 1; i < faceSize; ++i )
245 gridout << " " << vertices[ i ];
246 gridout << std::endl;
247 }
248 }
249 gridout << "#" << std::endl << std::endl;
250
251 gridout << std::endl << "GLOBALVERTEXINDEX" << std::endl;
252 for( iterator it = elementSeeds.begin(); it != end ; ++ it )
253 {
254 // convert entity seed into entity pointer
255 const ElementPointer ep = gridView_.grid().entityPointer( *it );
256 const Element& element = *ep;
257 const int numCorners = element.subEntities( dimGrid );
258 for( int i=0; i<numCorners; ++i )
259 {
260 const Index vxIndex = indexSet.subIndex( element, i, dimGrid );
261 assert( vxIndex < vxSize );
262 if( vertexIndex[ vxIndex ] != vxSize )
263 {
264 vertexIndex[ vxIndex ] = vxSize;
265 gridout << vxIndex << std::endl;
266 }
267 }
268 }
269 gridout << "#" << std::endl << std::endl;
270 return newName.str();
271}
272template <class GV>
273template <class LoadBalanceHandle>
274inline void DGFWriter< GV >::write ( const std::string &fileName, const LoadBalanceHandle &ldb, const int size ) const
275{
276 std::stringstream newName;
277 newName << fileName << "." << size;
278 std::ofstream gridout( newName.str().c_str() );
279 gridout << "DGF" << std::endl;
280 const IndexSet &indexSet = gridView_.indexSet();
281 gridout << "%" << " Elements = " << indexSet.size( 0 ) << " | Vertices = " << indexSet.size(dimGrid) << std::endl;
282 gridout << "ALUPARALLEL" << std::endl;
283 for (int p=0;p<size;++p)
284 {
285 gridout << write(fileName,ldb,size,p) << std::endl;
286 }
287 gridout << "#" << std::endl;
288}
289#endif // #ifndef DUNE_DGFWRITER_HH
write a GridView to a DGF file
Definition: writeparalleldgf.hh:26
GridView::Grid Grid
type of underlying hierarchical grid
Definition: writeparalleldgf.hh:33
GV GridView
type of grid view
Definition: writeparalleldgf.hh:31
static const int dimGrid
dimension of the grid
Definition: writeparalleldgf.hh:36
DGFWriter(const GridView &gridView)
constructor
Definition: writeparalleldgf.hh:58
std::string write(const std::string &fileName, const LoadBalanceHandle &ldb, const int size, const int rank) const
write the GridView to a file
Definition: writeparalleldgf.hh:106
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:125
@ cube
Cube element in any nonnegative dimension.
Definition: type.hh:133
@ simplex
Simplicial element in any nonnegative dimension.
Definition: type.hh:132
Different resources needed by all grid implementations.
IteratorRange<... > vertices(const GV &gv)
Iterates over all vertices (entities with dimension 0) of a GridView.
unspecified-type ReferenceElement
Returns the type of reference element for the argument type T.
Definition: referenceelements.hh:497
constexpr GeometryType cube(unsigned int dim)
Returns a GeometryType representing a hypercube of dimension dim.
Definition: type.hh:472
constexpr GeometryType simplex(unsigned int dim)
Returns a GeometryType representing a simplex of dimension dim.
Definition: type.hh:463
Class providing access to the singletons of the reference elements.
Definition: referenceelements.hh:170
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)