3#ifndef DUNE_GRID_IO_FILE_DGFPARSER_DGFWRITER_HH
4#define DUNE_GRID_IO_FILE_DGFPARSER_DGFWRITER_HH
21#include <dune/common/rangeutilities.hh>
22#include <dune/common/typeutilities.hh>
24#include <dune/geometry/referenceelements.hh>
25#include <dune/geometry/type.hh>
58 typedef typename GridView::template Codim< 0 >::Entity Element;
61 typedef typename Element::EntitySeed ElementSeed;
83 template<
class BoundaryData >
84 void write ( std::ostream &gridout,
const std::vector< Index > &newElemOrder, BoundaryData &&boundaryData,
const std::stringstream &addParams = std::stringstream() )
const;
94 template<
class BoundaryData >
95 void write ( std::ostream &gridout, BoundaryData &&boundaryData,
const std::stringstream &addParams = std::stringstream() )
const;
105 void write ( std::ostream &gridout,
const std::vector< Index > &newElemOrder,
const std::stringstream &addParams = std::stringstream() )
const
107 write( gridout, newElemOrder, [] (
const Intersection &i ) ->
int {
return boundaryId( i ); }, addParams );
117 void write ( std::ostream &gridout,
const std::stringstream &addParams = std::stringstream() )
const
119 write( gridout, [] (
const Intersection &i ) ->
int {
return boundaryId( i ); }, addParams );
128 template<
class... Args >
129 auto write (
const std::string &fileName, Args &&... args )
const
130 -> std::void_t<
decltype( this->
write( std::declval< std::ostream & >(), std::declval< Args >()... ) ) >
132 std::ofstream gridout( fileName );
134 write( gridout, std::forward< Args >( args )... );
136 std::cerr <<
"Couldn't open file `"<< fileName <<
"'!"<< std::endl;
140 auto elementsSeeds (
const std::vector< Index > &newElemOrder )
const
141 -> std::vector< ElementSeed >;
147 -> std::vector< Index >;
149 void writeElement ( std::ostream &gridout,
const std::vector< Index > &dgfIndices,
const Element &element,
const GeometryType &elementType )
const;
151 void writeSimplices ( std::ostream &gridout,
const std::vector< Index > &dgfIndices )
const;
152 void writeSimplices ( std::ostream &gridout,
const std::vector< Index > &dgfIndices,
const std::vector< ElementSeed > &elementSeeds )
const;
154 void writeCubes ( std::ostream &gridout,
const std::vector< Index > &dgfIndices )
const;
155 void writeCubes ( std::ostream &gridout,
const std::vector< Index > &dgfIndices,
const std::vector< ElementSeed > &elementSeeds )
const;
157 template<
class... Args >
158 void writeElements ( std::ostream &gridout,
const std::vector< Index > &dgfIndices,
const Args &... args )
const;
162 static auto boundaryId (
const I &i, PriorityTag< 1 > )
163 -> std::enable_if_t< std::is_convertible< std::decay_t<
decltype( i.impl().boundaryId() ) >,
int >::value,
int >
165 return i.impl().boundaryId();
169 static int boundaryId (
const I &i, PriorityTag< 0 > )
178 static int boundaryId (
const Intersection &,
int bndId ) {
return bndId; }
179 static int boundaryId (
const Intersection &i,
const std::string & ) {
return boundaryId( i ); }
180 static int boundaryId (
const Intersection &i,
const std::pair< int, std::string > &data ) {
return boundrayId( i, data.first ); }
182 static void appendBoundaryData ( std::ostream &gridout,
int ) { gridout << std::endl; }
183 static void appendBoundaryData ( std::ostream &gridout, std::pair< int, std::string > &data ) { appendBoundaryData( gridout, data.second ); }
184 static void appendBoundaryData ( std::ostream &gridout,
const std::string &s ) { gridout <<
" : " << s << std::endl; }
187 template<
class BoundaryData >
188 void writeBoundaries ( std::ostream &gridout,
const std::vector< Index > &dgfIndices, BoundaryData &&boundaryData )
const;
190 void writeBoundaries ( std::ostream &gridout,
const std::vector< Index > &dgfIndices )
const
192 writeBoundaries( gridout, dgfIndices, [] (
const Intersection &i ) ->
int {
return boundaryId( i ); } );
202 -> std::vector< ElementSeed >
204 const IndexSet &indexSet = gridView_.indexSet();
206 const std::size_t orderSize = newElemOrder.size() ;
207 std::vector< ElementSeed > elementSeeds( orderSize );
209 for(
const Element &element : elements( gridView_ ) )
211 assert( newElemOrder[ indexSet.index( element ) ] < orderSize );
212 elementSeeds[ newElemOrder[ indexSet.index( element ) ] ] = element.seed();
223 gridout.setf( std::ios_base::scientific, std::ios_base::floatfield );
224 gridout.precision( 16 );
226 const IndexSet &indexSet = gridView_.indexSet();
229 gridout <<
"DGF" << std::endl;
230 gridout <<
"%" <<
" Elements = " << indexSet.size( 0 ) <<
" | Vertices = " << indexSet.size( dimGrid ) << std::endl;
237 gridout << std::endl <<
"#" << std::endl;
243 -> std::vector< Index >
245 const IndexSet &indexSet = gridView_.indexSet();
247 const Index vxSize = indexSet.size( dimGrid );
248 std::vector< Index > dgfIndices( vxSize, vxSize );
251 gridout << std::endl <<
"VERTEX" << std::endl;
252 Index vertexCount = 0;
253 for(
const Element &element : elements( gridView_ ) )
255 for(
auto i : range( element.subEntities( dimGrid ) ) )
257 const Index vxIndex = indexSet.subIndex( element, i, dimGrid );
258 assert( vxIndex < vxSize );
259 if( dgfIndices[ vxIndex ] == vxSize )
261 dgfIndices[ vxIndex ] = vertexCount++;
262 gridout << element.geometry().corner( i ) << std::endl;
266 gridout <<
"#" << std::endl;
268 if( vertexCount != vxSize )
269 DUNE_THROW(
GridError,
"IndexSet reports wrong number of vertices." );
275 inline void DGFWriter< GV >::writeElement ( std::ostream &gridout,
const std::vector< Index > &dgfIndices,
const Element &element,
const GeometryType &elementType )
const
278 if( element.type() != elementType )
282 const IndexSet &indexSet = gridView_.indexSet();
283 for(
auto i : range( element.subEntities( Element::dimension ) ) )
284 gridout << (i > 0 ?
" " :
"") << dgfIndices[ indexSet.subIndex( element, i, dimGrid ) ];
285 gridout << std::endl;
293 gridout << std::endl <<
"SIMPLEX" << std::endl;
296 for(
const Element &element : elements( gridView_ ) )
297 writeElement( gridout, dgfIndices, element, GeometryTypes::simplex( dimGrid ) );
300 gridout <<
"#" << std::endl;
308 gridout << std::endl <<
"SIMPLEX" << std::endl;
311 for(
const ElementSeed &seed : elementSeeds )
312 writeElement( gridout, dgfIndices, gridView_.grid().entity( seed ), GeometryTypes::simplex( dimGrid ) );
315 gridout <<
"#" << std::endl;
323 gridout << std::endl <<
"CUBE" << std::endl;
326 for(
const Element &element : elements( gridView_ ) )
327 writeElement( gridout, dgfIndices, element, GeometryTypes::cube( dimGrid ) );
330 gridout <<
"#" << std::endl;
335 inline void DGFWriter< GV >::writeCubes ( std::ostream &gridout,
const std::vector< Index > &dgfIndices,
const std::vector< ElementSeed > &elementSeeds )
const
337 const IndexSet &indexSet = gridView_.indexSet();
340 gridout << std::endl <<
"CUBE" << std::endl;
343 for(
const ElementSeed &seed : elementSeeds )
344 writeElement( gridout, dgfIndices, gridView_.grid().entity( seed ), GeometryTypes::cube( dimGrid ) );
347 gridout <<
"#" << std::endl;
352 template<
class... Args >
355 const IndexSet &indexSet = gridView_.indexSet();
357 if( (dimGrid > 1) && (indexSet.size( GeometryTypes::simplex( dimGrid ) ) > 0) )
358 writeSimplices( gridout, dgfIndices, args... );
360 if( indexSet.size( GeometryTypes::cube( dimGrid ) ) > 0 )
361 writeCubes( gridout, dgfIndices, args... );
366 template<
class BoundaryData >
371 const IndexSet &indexSet = gridView_.indexSet();
374 gridout << std::endl <<
"BOUNDARYSEGMENTS" << std::endl;
376 for(
const Element &element : elements( gridView_ ) )
378 if( !element.hasBoundaryIntersections() )
381 const auto &refElement = ReferenceElements< typename Grid::ctype, dimGrid >::general( element.type() );
382 for(
const Intersection &intersection : intersections( gridView_, element ) )
384 if( !intersection.boundary() )
387 const auto data = boundaryData( intersection );
388 const int bndId = max( boundaryId( intersection, data ), 1 );
390 const int faceNumber = intersection.indexInInside();
391 const unsigned int faceSize = refElement.size( faceNumber, 1, dimGrid );
392 gridout << bndId <<
" ";
393 for(
auto i : range( faceSize ) )
395 const int j = refElement.subEntity( faceNumber, 1, i, dimGrid );
396 gridout <<
" " << dgfIndices[ indexSet.subIndex( element, j, dimGrid ) ];
398 appendBoundaryData( gridout, data );
401 gridout <<
"#" << std::endl;
406 template<
class BoundaryData >
407 inline void DGFWriter< GV >::write ( std::ostream &gridout,
const std::vector< Index > &newElemOrder, BoundaryData &&boundaryData,
const std::stringstream &addParams )
const
409 writeHeader( gridout );
410 auto dgfIndices = writeVertices( gridout );
411 writeElements( gridout, dgfIndices, elementSeeds( newElemOrder ) );
412 writeBoundaries( gridout, dgfIndices, std::forward< BoundaryData >( boundaryData ) );
413 gridout << addParams.str();
414 writeFooter( gridout );
419 template<
class BoundaryData >
420 inline void DGFWriter< GV >::write ( std::ostream &gridout, BoundaryData &&boundaryData,
const std::stringstream &addParams )
const
422 writeHeader( gridout );
423 auto dgfIndices = writeVertices( gridout );
424 writeElements( gridout, dgfIndices );
425 writeBoundaries( gridout, dgfIndices, std::forward< BoundaryData >( boundaryData ) );
426 gridout << addParams.str();
427 writeFooter( gridout );
Traits::Grid Grid
type of the grid
Definition: common/gridview.hh:80
Traits::IndexSet IndexSet
type of the index set
Definition: common/gridview.hh:83
Traits::Intersection Intersection
type of the intersection
Definition: common/gridview.hh:86
@ dimension
The dimension of the grid.
Definition: common/gridview.hh:130
Include standard header files.
Definition: agrid.hh:58
Intersection of a mesh entity of codimension 0 ("element") with a "neighboring" element or with the d...
Definition: common/intersection.hh:162
Base class for exceptions in Dune grid modules.
Definition: exceptions.hh:18
IndexTypeImp IndexType
The type used for the indices.
Definition: indexidset.hh:90
write a GridView to a DGF file
Definition: dgfwriter.hh:44
static const int dimGrid
dimension of the grid
Definition: dgfwriter.hh:54
DGFWriter(const GridView &gridView)
constructor
Definition: dgfwriter.hh:70
void write(std::ostream &gridout, const std::stringstream &addParams=std::stringstream()) const
write the GridView into a std::ostream
Definition: dgfwriter.hh:117
auto write(const std::string &fileName, Args &&... args) const -> std::void_t< decltype(this->write(std::declval< std::ostream & >(), std::declval< Args >()...)) >
write the GridView to a file
Definition: dgfwriter.hh:129
void writeBoundaries(std::ostream &gridout, const std::vector< Index > &dgfIndices, BoundaryData &&boundaryData) const
Definition: dgfwriter.hh:367
static int boundaryId(const Intersection &i)
Definition: dgfwriter.hh:175
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:105
void write(std::ostream &gridout, const std::vector< Index > &newElemOrder, BoundaryData &&boundaryData, const std::stringstream &addParams=std::stringstream()) const
write the GridView into a std::ostream
Definition: dgfwriter.hh:407
GridView gridView_
Definition: dgfwriter.hh:196
void writeElement(std::ostream &gridout, const std::vector< Index > &dgfIndices, const Element &element, const GeometryType &elementType) const
Definition: dgfwriter.hh:275
GV GridView
type of grid view
Definition: dgfwriter.hh:49
void writeFooter(std::ostream &gridout) const
Definition: dgfwriter.hh:235
GridView::Grid Grid
type of underlying hierarchical grid
Definition: dgfwriter.hh:51
auto writeVertices(std::ostream &gridout) const -> std::vector< Index >
Definition: dgfwriter.hh:242
void writeElements(std::ostream &gridout, const std::vector< Index > &dgfIndices, const Args &... args) const
Definition: dgfwriter.hh:353
void writeBoundaries(std::ostream &gridout, const std::vector< Index > &dgfIndices) const
Definition: dgfwriter.hh:190
void writeCubes(std::ostream &gridout, const std::vector< Index > &dgfIndices) const
Definition: dgfwriter.hh:320
void writeSimplices(std::ostream &gridout, const std::vector< Index > &dgfIndices) const
Definition: dgfwriter.hh:290
void writeHeader(std::ostream &gridout) const
Definition: dgfwriter.hh:220
auto elementsSeeds(const std::vector< Index > &newElemOrder) const -> std::vector< ElementSeed >
Definition: dgfwriter.hh:201
Different resources needed by all grid implementations.