1#ifndef DUNE_FEM_ALUGRIDWRITER_HH
2#define DUNE_FEM_ALUGRIDWRITER_HH
14#include <dune/alugrid/3d/topology.hh>
22 template <
class Gr
idPartType>
23 class GlobalConsecutiveIndexSet
25 typedef typename GridPartType :: GridType GridType;
26 typedef typename GridType :: Traits :: LocalIdSet IdSetType;
27 typedef typename IdSetType :: IdType IdType;
29 enum { dimension = GridType :: dimension };
31 const GridPartType& gridPart_;
32 const GridType& grid_;
33 const IdSetType& idSet_;
35 typedef typename GridType :: template
Codim< 0 > :: Geometry :: GlobalCoordinate CoordinateType;
36 typedef typename GridType :: template
Codim< 0 > :: Entity EntityType;
46 Vertex(
const CoordinateType& vx,
49 : vx_( vx ), id_( id ), index_( index )
51 const CoordinateType& vx()
const {
return vx_ ; }
52 const IdType& id ()
const {
return id_; }
53 const int index ()
const { assert(index_ >= 0);
return index_; }
54 void setIndex(
const int index) { index_ = index; }
56 typedef Vertex VertexType;
58 typedef std::map< IdType, VertexType > IndexMapType;
59 typedef typename IndexMapType :: iterator IndexMapIteratorType;
60 mutable IndexMapType indices_;
64 public CommDataHandleIF<
67 const IdSetType& idSet_;
69 IndexMapType& indices_;
71 explicit DataHandle(
const IdSetType& idSet,
73 IndexMapType& indices )
81 bool contains (
int dim,
int codim )
const
87 bool fixedSize (
int dim,
int codim )
const
93 template<
class Entity >
94 size_t size (
const Entity &entity )
const
100 template<
class MessageBuffer,
class Entity >
101 void gather ( MessageBuffer &buffer,
102 const Entity &entity )
const
105 buffer.write( myRank_ );
106 IndexMapIteratorType it = indices_.find( idSet_.id( entity ) );
107 int index = ( it == indices_.end() ) ? -1 : (*it).second.index();
108 buffer.write( index );
112 template<
class MessageBuffer,
class Entity >
113 void scatter ( MessageBuffer &buffer,
114 const Entity &entity,
115 const size_t dataSize )
121 buffer.read( index );
122 if( myRank_ > rank && index > -1 )
124 const IdType
id = idSet_.id( entity );
125 IndexMapIteratorType it = indices_.find(
id );
126 if( it == indices_.end() )
128 VertexType vx( entity.geometry().corner(0),
id, index );
135 explicit GlobalConsecutiveIndexSet(
const GridPartType& gridPart,
136 const bool useIds =
false )
137 : gridPart_( gridPart ),
138 grid_( gridPart.grid() ),
139 idSet_( grid_.localIdSet() ),
142 const int pSize = grid_.comm().size();
143 const int myRank = grid_.comm().rank();
145 for(
int p = 0; p<pSize; ++p )
153 const IteratorType endit = gridPart_.template end< dimension > ();
154 for( IteratorType it = gridPart_.template begin< dimension > ();
157 const typename IteratorType::Entity &entity = *it;
158 const IdType
id = idSet_.id( entity );
159 if( indices_.find(
id ) == indices_.end() )
161 VertexType vx( entity.geometry().corner(0),
id, index );
162 indices_[ id ] = vx ;
169 typedef typename GridPartType :: template
Codim< 0 > :: IteratorType
171 const IteratorType endit = gridPart_.template end< 0 > ();
172 for( IteratorType it = gridPart_.template begin< 0 > ();
175 const EntityType& entity = *it ;
176 const int count = entity.subEntities( dimension );
177 for(
int i = 0; i<count; ++i)
179 auto vertex = entity.template subEntity< dimension > ( i );
180 const int id = gridPart_.indexSet().index(
vertex );
181 if( indices_.find(
id ) == indices_.end() )
183 VertexType vx(
vertex.geometry().corner(0),
id, -1);
184 indices_[ id ] = vx ;
192 IndexMapIteratorType end = indices_.end();
193 for( IndexMapIteratorType it = indices_.begin(); it != end; ++it, ++myindex)
195 (*it).second.setIndex( myindex );
203 grid_.comm().broadcast(&index, 1, p );
206 if( grid_.comm().size() > 1 )
208 DataHandle dataHandle( idSet_, myRank, indices_ );
209 gridPart_.communicate( dataHandle,
216 void writeCoordinates ( std::ostream& out )
const
218 out << indices_.size() << std::endl;
220 out << std::scientific;
221 IndexMapIteratorType end = indices_.end();
222 for( IndexMapIteratorType it = indices_.begin(); it != end; ++it)
224 out << (*it).second.vx() << std::endl;
228 void writeIndices ( std::ostream& out )
const
230 IndexMapIteratorType end = indices_.end();
231 for( IndexMapIteratorType it = indices_.begin(); it != end; ++it)
233 out << (*it).second.id() <<
" -1" << std::endl;
237 template <
class EntityType>
238 int index (
const EntityType& entity )
const
242 IndexMapIteratorType it = indices_.find(
244 idSet_.id( entity ) :
245 gridPart_.indexSet().index( entity ) );
246 assert( it != indices_.end() );
247 return (*it).second.index();
252 return indices_.size();
256 template <
class Gr
idPartType,
class IndexSetType >
259 const GridPartType& gridPart_;
261 typedef typename GridPartType :: GridType GridType;
263 const IndexSetType& indexSet_;
265 enum { dimension = GridType :: dimension };
267 typedef typename GridType :: template
Codim< 0 > :: Entity Entity;
269 ALUGridWriter(
const GridPartType& gridPart,
270 const IndexSetType& indexSet )
271 : gridPart_( gridPart ),
272 indexSet_( indexSet )
276 void write(
const std::string& filename,
const int rank )
const
278 typedef typename GridPartType :: template
Codim< 0 > :: IteratorType
280 const IteratorType endit = gridPart_.template end< 0 > ();
281 IteratorType it = gridPart_.template begin< 0 > ();
282 if( it == endit )
return;
284 const Entity &entity = *it;
285 bool hexahedra = entity.type().isHexahedron();
286 if( ! hexahedra && ! entity.type().isTetrahedron() )
288 DUNE_THROW(InvalidStateException,
"Wrong geometry type");
292 for(; it != endit; ++it )
297 std::stringstream filestr;
298 filestr << filename <<
"." << rank;
300 std::ofstream file ( filestr.str().c_str() );
303 std::cerr <<
"ERROR: couldn't open file " << filestr.str() << std::endl;
308 file.setf (std::ios::fixed, std::ios::floatfield) ;
310 const char* header = ( hexahedra ) ?
"!Hexahedra" :
"!Tetrahedra";
313 file <<
" ( noVertices = " << indexSet_.size();
314 file <<
" | noElements = " << noElements <<
" )" << std :: endl;
317 indexSet_.writeCoordinates( file );
319 file << noElements << std::endl;
322 typedef ElementTopologyMapping< hexa > ElementTopo;
323 writeElements< ElementTopo >( file );
324 writeBoundaries< ElementTopo >( file );
328 typedef ElementTopologyMapping< tetra > ElementTopo;
329 writeElements< ElementTopo >( file );
330 writeBoundaries< ElementTopo >( file );
334 indexSet_.writeIndices( file );
337 int getIndex(
const Entity& entity,
const int i )
const
339 return indexSet_.subIndex( entity, i, dimension );
342 template <
class ElementTopo>
343 void writeElements( std::ostream& out )
const
345 typedef typename GridPartType :: template
Codim< 0 > :: IteratorType IteratorType;
346 const IteratorType endit = gridPart_.template end< 0 > ();
347 for(IteratorType it = gridPart_.template begin< 0 > ();
350 const Entity& entity = *it ;
351 const int nVx = entity.subEntities( dimension );
353 out << getIndex( entity, ElementTopo::dune2aluVertex( 0 ) );
354 for(
int i=1; i<nVx; ++i)
356 out <<
" " << getIndex( entity, ElementTopo::dune2aluVertex( i ));
362 template <
class ElementTopo>
363 void writeBoundaries( std::ostream& out )
const
365 typedef typename GridPartType :: template
Codim< 0 > :: IteratorType IteratorType;
366 typedef typename GridPartType :: IntersectionIteratorType IntersectionIteratorType;
367 typedef typename GridType :: template
Codim< 0 > :: Entity Entity;
368 const IteratorType endit = gridPart_.template end< 0 > ();
370 for(IteratorType it = gridPart_.template begin< 0 > ();
373 const Entity& entity = *it ;
374 const IntersectionIteratorType endnit = gridPart_.iend( entity );
375 for( IntersectionIteratorType nit = gridPart_.ibegin( entity );
376 nit != endnit ; ++nit )
378 typedef typename IntersectionIteratorType :: Intersection Intersection;
379 const Intersection& inter = *nit;
380 if( inter.boundary() )
382 else if( inter.neighbor() &&
388 out << bndFaces << std::endl;
390 for(IteratorType it = gridPart_.template begin< 0 > ();
393 const Entity& entity = *it ;
394 typedef typename GridType :: ctype coordType;
397 const IntersectionIteratorType endnit = gridPart_.iend( entity );
398 for( IntersectionIteratorType nit = gridPart_.ibegin( entity );
399 nit != endnit ; ++nit )
401 typedef typename IntersectionIteratorType :: Intersection Intersection;
402 const Intersection& inter = *nit;
404 if( inter.boundary() )
406 bndId = inter.boundaryId();
408 else if( inter.neighbor() &&
416 out << -bndId <<
" ";
417 const int duneFace = inter.indexInInside();
418 const int vxNr = refElem.size( duneFace, 1, dimension );
421 std::vector< int > vertices( vxNr );
422 const int aluFace = ElementTopo :: generic2aluFace( duneFace );
423 for(
int i=0; i<vxNr; ++i)
425 const int j = ElementTopo :: faceVertex( aluFace, i );
426 const int k = ElementTopo :: alu2genericVertex( j );
427 out <<
" " << indexSet_.subIndex( entity, k, dimension );
436 static void dumpMacroGrid(
const GridPartType& gridPart,
437 const IndexSetType& indexSet,
438 const std::string& filename,
441 const int rank = ( p < 0 ? gridPart.comm().rank() : p);
442 ALUGridWriter< GridPartType, IndexSetType > writer ( gridPart, indexSet );
443 writer.write( filename, rank );
static constexpr int codimension
Know your own codimension.
Definition: entity.hh:106
Different resources needed by all grid implementations.
Describes the parallel communication interface class for MessageBuffers and DataHandles.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
constexpr GeometryType vertex
GeometryType representing a vertex.
Definition: type.hh:492
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
constexpr std::bool_constant<((II==value)||...)> contains(std::integer_sequence< T, II... >, std::integral_constant< T, value >)
Checks whether or not a given sequence contains a value.
Definition: integersequence.hh:137
static const ReferenceElement & general(const GeometryType &type)
get general reference elements
Definition: referenceelements.hh:156
std::size_t fixedSize
The number of data items per index if it is fixed, 0 otherwise.
Definition: variablesizecommunicator.hh:264