5#ifndef DUNE_GRID_IO_FILE_DGFPARSER_DGFWRITER_HH
6#define DUNE_GRID_IO_FILE_DGFPARSER_DGFWRITER_HH
23#include <dune/common/rangeutilities.hh>
24#include <dune/common/typeutilities.hh>
26#include <dune/geometry/referenceelements.hh>
27#include <dune/geometry/type.hh>
60 typedef typename GridView::template Codim< 0 >::Entity Element;
63 typedef typename Element::EntitySeed ElementSeed;
85 template<
class BoundaryData >
86 void write ( std::ostream &gridout,
const std::vector< Index > &newElemOrder, BoundaryData &&boundaryData,
const std::stringstream &addParams = std::stringstream() )
const;
96 template<
class BoundaryData >
97 void write ( std::ostream &gridout, BoundaryData &&boundaryData,
const std::stringstream &addParams = std::stringstream() )
const;
107 void write ( std::ostream &gridout,
const std::vector< Index > &newElemOrder,
const std::stringstream &addParams = std::stringstream() )
const
109 write( gridout, newElemOrder, [] (
const Intersection &i ) ->
int {
return boundaryId( i ); }, addParams );
119 void write ( std::ostream &gridout,
const std::stringstream &addParams = std::stringstream() )
const
121 write( gridout, [] (
const Intersection &i ) ->
int {
return boundaryId( i ); }, addParams );
130 template<
class... Args >
131 auto write (
const std::string &fileName, Args &&... args )
const
132 -> std::void_t<
decltype( this->
write( std::declval< std::ostream & >(), std::declval< Args >()... ) ) >
134 std::ofstream gridout( fileName );
136 write( gridout, std::forward< Args >( args )... );
138 std::cerr <<
"Couldn't open file `"<< fileName <<
"'!"<< std::endl;
142 auto elementsSeeds (
const std::vector< Index > &newElemOrder )
const
143 -> std::vector< ElementSeed >;
149 -> std::vector< Index >;
151 void writeElement ( std::ostream &gridout,
const std::vector< Index > &dgfIndices,
const Element &element,
const GeometryType &elementType )
const;
153 void writeSimplices ( std::ostream &gridout,
const std::vector< Index > &dgfIndices )
const;
154 void writeSimplices ( std::ostream &gridout,
const std::vector< Index > &dgfIndices,
const std::vector< ElementSeed > &elementSeeds )
const;
156 void writeCubes ( std::ostream &gridout,
const std::vector< Index > &dgfIndices )
const;
157 void writeCubes ( std::ostream &gridout,
const std::vector< Index > &dgfIndices,
const std::vector< ElementSeed > &elementSeeds )
const;
159 template<
class... Args >
160 void writeElements ( std::ostream &gridout,
const std::vector< Index > &dgfIndices,
const Args &... args )
const;
164 static auto boundaryId (
const I &i, PriorityTag< 1 > )
165 -> std::enable_if_t< std::is_convertible< std::decay_t<
decltype( i.impl().boundaryId() ) >,
int >::value,
int >
167 return i.impl().boundaryId();
171 static int boundaryId (
const I &i, PriorityTag< 0 > )
180 static int boundaryId (
const Intersection &,
int bndId ) {
return bndId; }
181 static int boundaryId (
const Intersection &i,
const std::string & ) {
return boundaryId( i ); }
182 static int boundaryId (
const Intersection &i,
const std::pair< int, std::string > &data ) {
return boundrayId( i, data.first ); }
184 static void appendBoundaryData ( std::ostream &gridout,
int ) { gridout << std::endl; }
185 static void appendBoundaryData ( std::ostream &gridout, std::pair< int, std::string > &data ) { appendBoundaryData( gridout, data.second ); }
186 static void appendBoundaryData ( std::ostream &gridout,
const std::string &s ) { gridout <<
" : " << s << std::endl; }
189 template<
class BoundaryData >
190 void writeBoundaries ( std::ostream &gridout,
const std::vector< Index > &dgfIndices, BoundaryData &&boundaryData )
const;
192 void writeBoundaries ( std::ostream &gridout,
const std::vector< Index > &dgfIndices )
const
194 writeBoundaries( gridout, dgfIndices, [] (
const Intersection &i ) ->
int {
return boundaryId( i ); } );
204 -> std::vector< ElementSeed >
206 const IndexSet &indexSet = gridView_.indexSet();
208 const std::size_t orderSize = newElemOrder.size() ;
209 std::vector< ElementSeed > elementSeeds( orderSize );
211 for(
const Element &element : elements( gridView_ ) )
213 assert( newElemOrder[ indexSet.index( element ) ] < orderSize );
214 elementSeeds[ newElemOrder[ indexSet.index( element ) ] ] = element.seed();
225 gridout.setf( std::ios_base::scientific, std::ios_base::floatfield );
226 gridout.precision( 16 );
228 const IndexSet &indexSet = gridView_.indexSet();
231 gridout <<
"DGF" << std::endl;
232 gridout <<
"%" <<
" Elements = " << indexSet.size( 0 ) <<
" | Vertices = " << indexSet.size( dimGrid ) << std::endl;
239 gridout << std::endl <<
"#" << std::endl;
245 -> std::vector< Index >
247 const IndexSet &indexSet = gridView_.indexSet();
249 const Index vxSize = indexSet.size( dimGrid );
250 std::vector< Index > dgfIndices( vxSize, vxSize );
253 gridout << std::endl <<
"VERTEX" << std::endl;
254 Index vertexCount = 0;
255 for(
const Element &element : elements( gridView_ ) )
257 for(
auto i : range( element.subEntities( dimGrid ) ) )
259 const Index vxIndex = indexSet.subIndex( element, i, dimGrid );
260 assert( vxIndex < vxSize );
261 if( dgfIndices[ vxIndex ] == vxSize )
263 dgfIndices[ vxIndex ] = vertexCount++;
264 gridout << element.geometry().corner( i ) << std::endl;
268 gridout <<
"#" << std::endl;
270 if( vertexCount != vxSize )
271 DUNE_THROW(
GridError,
"IndexSet reports wrong number of vertices." );
280 if( element.type() != elementType )
284 const IndexSet &indexSet = gridView_.indexSet();
285 for(
auto i : range( element.subEntities( Element::dimension ) ) )
286 gridout << (i > 0 ?
" " :
"") << dgfIndices[ indexSet.subIndex( element, i, dimGrid ) ];
287 gridout << std::endl;
295 gridout << std::endl <<
"SIMPLEX" << std::endl;
298 for(
const Element &element : elements( gridView_ ) )
299 writeElement( gridout, dgfIndices, element, GeometryTypes::simplex( dimGrid ) );
302 gridout <<
"#" << std::endl;
310 gridout << std::endl <<
"SIMPLEX" << std::endl;
313 for(
const ElementSeed &seed : elementSeeds )
314 writeElement( gridout, dgfIndices, gridView_.grid().entity( seed ), GeometryTypes::simplex( dimGrid ) );
317 gridout <<
"#" << std::endl;
325 gridout << std::endl <<
"CUBE" << std::endl;
328 for(
const Element &element : elements( gridView_ ) )
329 writeElement( gridout, dgfIndices, element, GeometryTypes::cube( dimGrid ) );
332 gridout <<
"#" << std::endl;
337 inline void DGFWriter< GV >::writeCubes ( std::ostream &gridout,
const std::vector< Index > &dgfIndices,
const std::vector< ElementSeed > &elementSeeds )
const
339 const IndexSet &indexSet = gridView_.indexSet();
342 gridout << std::endl <<
"CUBE" << std::endl;
345 for(
const ElementSeed &seed : elementSeeds )
346 writeElement( gridout, dgfIndices, gridView_.grid().entity( seed ), GeometryTypes::cube( dimGrid ) );
349 gridout <<
"#" << std::endl;
354 template<
class... Args >
357 const IndexSet &indexSet = gridView_.indexSet();
359 if( (dimGrid > 1) && (indexSet.size( GeometryTypes::simplex( dimGrid ) ) > 0) )
360 writeSimplices( gridout, dgfIndices, args... );
362 if( indexSet.size( GeometryTypes::cube( dimGrid ) ) > 0 )
363 writeCubes( gridout, dgfIndices, args... );
368 template<
class BoundaryData >
373 const IndexSet &indexSet = gridView_.indexSet();
376 gridout << std::endl <<
"BOUNDARYSEGMENTS" << std::endl;
378 for(
const Element &element : elements( gridView_ ) )
380 if( !element.hasBoundaryIntersections() )
383 const auto &refElement = ReferenceElements< typename Grid::ctype, dimGrid >::general( element.type() );
384 for(
const Intersection &intersection : intersections( gridView_, element ) )
386 if( !intersection.boundary() )
389 const auto data = boundaryData( intersection );
390 const int bndId =
max( boundaryId( intersection, data ), 1 );
392 const int faceNumber = intersection.indexInInside();
393 const unsigned int faceSize = refElement.size( faceNumber, 1, dimGrid );
394 gridout << bndId <<
" ";
395 for(
auto i : range( faceSize ) )
397 const int j = refElement.subEntity( faceNumber, 1, i, dimGrid );
398 gridout <<
" " << dgfIndices[ indexSet.subIndex( element, j, dimGrid ) ];
400 appendBoundaryData( gridout, data );
403 gridout <<
"#" << std::endl;
408 template<
class BoundaryData >
409 inline void DGFWriter< GV >::write ( std::ostream &gridout,
const std::vector< Index > &newElemOrder, BoundaryData &&boundaryData,
const std::stringstream &addParams )
const
411 writeHeader( gridout );
412 auto dgfIndices = writeVertices( gridout );
413 writeElements( gridout, dgfIndices, elementSeeds( newElemOrder ) );
414 writeBoundaries( gridout, dgfIndices, std::forward< BoundaryData >( boundaryData ) );
415 gridout << addParams.str();
416 writeFooter( gridout );
421 template<
class BoundaryData >
422 inline void DGFWriter< GV >::write ( std::ostream &gridout, BoundaryData &&boundaryData,
const std::stringstream &addParams )
const
424 writeHeader( gridout );
425 auto dgfIndices = writeVertices( gridout );
426 writeElements( gridout, dgfIndices );
427 writeBoundaries( gridout, dgfIndices, std::forward< BoundaryData >( boundaryData ) );
428 gridout << addParams.str();
429 writeFooter( gridout );
Traits::Grid Grid
type of the grid
Definition: common/gridview.hh:83
Traits::IndexSet IndexSet
type of the index set
Definition: common/gridview.hh:86
static constexpr int dimension
The dimension of the grid.
Definition: common/gridview.hh:148
Traits::Intersection Intersection
type of the intersection
Definition: common/gridview.hh:89
Include standard header files.
Definition: agrid.hh:60
int max(const DofVectorPointer< int > &dofVector)
Definition: dofvector.hh:337
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:132
Intersection of a mesh entity of codimension 0 ("element") with a "neighboring" element or with the d...
Definition: common/intersection.hh:164
Base class for exceptions in Dune grid modules.
Definition: exceptions.hh:20
IndexTypeImp IndexType
The type used for the indices.
Definition: indexidset.hh:92
write a GridView to a DGF file
Definition: dgfwriter.hh:46
static const int dimGrid
dimension of the grid
Definition: dgfwriter.hh:56
DGFWriter(const GridView &gridView)
constructor
Definition: dgfwriter.hh:72
void write(std::ostream &gridout, const std::stringstream &addParams=std::stringstream()) const
write the GridView into a std::ostream
Definition: dgfwriter.hh:119
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:131
void writeBoundaries(std::ostream &gridout, const std::vector< Index > &dgfIndices, BoundaryData &&boundaryData) const
Definition: dgfwriter.hh:369
static int boundaryId(const Intersection &i)
Definition: dgfwriter.hh:177
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:107
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:409
GridView gridView_
Definition: dgfwriter.hh:198
void writeElement(std::ostream &gridout, const std::vector< Index > &dgfIndices, const Element &element, const GeometryType &elementType) const
Definition: dgfwriter.hh:277
GV GridView
type of grid view
Definition: dgfwriter.hh:51
void writeFooter(std::ostream &gridout) const
Definition: dgfwriter.hh:237
GridView::Grid Grid
type of underlying hierarchical grid
Definition: dgfwriter.hh:53
auto writeVertices(std::ostream &gridout) const -> std::vector< Index >
Definition: dgfwriter.hh:244
void writeElements(std::ostream &gridout, const std::vector< Index > &dgfIndices, const Args &... args) const
Definition: dgfwriter.hh:355
void writeBoundaries(std::ostream &gridout, const std::vector< Index > &dgfIndices) const
Definition: dgfwriter.hh:192
void writeCubes(std::ostream &gridout, const std::vector< Index > &dgfIndices) const
Definition: dgfwriter.hh:322
void writeSimplices(std::ostream &gridout, const std::vector< Index > &dgfIndices) const
Definition: dgfwriter.hh:292
void writeHeader(std::ostream &gridout) const
Definition: dgfwriter.hh:222
auto elementsSeeds(const std::vector< Index > &newElemOrder) const -> std::vector< ElementSeed >
Definition: dgfwriter.hh:203
Different resources needed by all grid implementations.