3#ifndef DUNE_DGF_GRIDPTR_HH
4#define DUNE_DGF_GRIDPTR_HH
20#include <dune/grid/common/gridenums.hh>
22#include <dune/grid/common/intersection.hh>
23#include <dune/grid/common/partitionset.hh>
24#include <dune/grid/common/rangegenerators.hh>
26#include <dune/grid/io/file/dgfparser/dgfexception.hh>
27#include <dune/grid/io/file/dgfparser/entitykey.hh>
28#include <dune/grid/io/file/dgfparser/parser.hh>
37 struct DGFGridFactory;
39 template<
class Gr
idImp,
class IntersectionImp >
59 template<
class Gr
idType >
62 class mygrid_ptr :
public std::shared_ptr< GridType >
64 typedef std::shared_ptr< GridType > base_t ;
71 if( use_count() == 1 )
75 if( grd )
delete grd ;
79 void assignObj(
const mygrid_ptr& other )
82 base_t :: operator = ( other );
86 using base_t :: swap ;
87 using base_t :: use_count ;
90 mygrid_ptr() : base_t( ( GridType * ) 0, emptydeleter_t() ) {}
92 mygrid_ptr(
const mygrid_ptr& other ) { assignObj( other ); }
94 explicit mygrid_ptr( GridType* grd ) : base_t( grd, emptydeleter_t() ) {}
97 ~mygrid_ptr() { removeObj(); }
100 mygrid_ptr&
operator = (
const mygrid_ptr& other )
109 GridType* grd = this->get();
110 base_t ptr(( GridType * ) 0, emptydeleter_t() );
117 static const int dimension = GridType::dimension;
120 explicit GridPtr (
const std::string &filename,
130 haveBndParam_( false )
132 DGFGridFactory< GridType > dgfFactory( filename, comm );
133 initialize( dgfFactory );
147 haveBndParam_( false )
149 DGFGridFactory< GridType > dgfFactory( input, comm );
150 initialize( dgfFactory );
163 haveBndParam_( false )
176 haveBndParam_( false )
181 : gridPtr_(org.gridPtr_),
182 elParam_(org.elParam_),
183 vtxParam_(org.vtxParam_),
184 bndParam_(org.bndParam_),
186 emptyParam_( org.emptyParam_ ),
187 nofElParam_(org.nofElParam_),
188 nofVtxParam_(org.nofVtxParam_),
189 haveBndParam_(org.haveBndParam_)
195 gridPtr_ = org.gridPtr_;
196 elParam_ = org.elParam_;
197 vtxParam_ = org.vtxParam_;
198 bndParam_ = org.bndParam_;
200 emptyParam_ = org.emptyParam_;
202 nofElParam_ = org.nofElParam_;
203 nofVtxParam_ = org.nofVtxParam_;
204 haveBndParam_ = org.haveBndParam_;
211 gridPtr_ = mygrid_ptr( grd );
216 emptyParam_.resize(0);
220 haveBndParam_ =
false;
231 return gridPtr_.operator -> ();
241 return gridPtr_.operator -> ();
245 GridType*
release () {
return gridPtr_.release(); }
250 case 0 :
return nofElParam_;
break;
251 case GridType::dimension :
return nofVtxParam_;
break;
257 template <
class Entity>
264 template<
class Gr
idImp,
class IntersectionImp >
271 template <
class Entity>
274 typedef typename GridType::LevelGridView
GridView;
275 GridView gridView = gridPtr_->levelGridView( 0 );
279 if( nofElParam_ > 0 )
281 assert( (
unsigned int)gridView.
indexSet().index( entity ) < elParam_.size() );
282 return elParam_[ gridView.
indexSet().index( entity ) ];
285 case GridType::dimension :
286 if( nofVtxParam_ > 0 )
288 assert( (
unsigned int)gridView.
indexSet().index( entity ) < vtxParam_.size() );
289 return vtxParam_[ gridView.
indexSet().index( entity ) ];
297 template<
class Gr
idImp,
class IntersectionImp >
301 if ( !haveBndParam_ )
309 if( gridPtr_->comm().size() > 1 )
311 DataHandle dh(*
this);
318 if( gridPtr_->comm().size() > 1 )
320 DataHandle dh(*
this);
321 gridPtr_->loadBalance( dh.interface() );
327 template<
class Range >
328 static bool isEmpty ( Range &&range )
330 return range.begin() == range.end();
333 void initialize ( DGFGridFactory< GridType > &dgfFactory )
335 gridPtr_ = mygrid_ptr( dgfFactory.grid() );
337 const auto gridView = gridPtr_->levelGridView( 0 );
338 const auto &indexSet = gridView.indexSet();
340 nofElParam_ = dgfFactory.template numParameters< 0 >();
341 nofVtxParam_ = dgfFactory.template numParameters< dimension >();
342 haveBndParam_ = dgfFactory.haveBoundaryParameters();
344 std::array< int, 3 > nofParams = {{ nofElParam_, nofVtxParam_,
static_cast< int >( haveBndParam_ ) }};
345 gridView.comm().max( nofParams.data(), nofParams.size() );
350 nofElParam_ = nofParams[ 0 ];
351 nofVtxParam_ = nofParams[ 1 ];
355 haveBndParam_ =
static_cast< bool >( nofParams[ 2 ] );
357 if( (nofElParam_ != nofParams[ 0 ]) || (nofVtxParam_ != nofParams[ 1 ]) )
358 DUNE_THROW( DGFException,
"Number of parameters differs between processes" );
360 elParam_.resize( nofElParam_ > 0 ? indexSet.size( 0 ) : 0 );
361 vtxParam_.resize( nofVtxParam_ > 0 ? indexSet.size( dimension ) : 0 );
363 bndId_.resize( indexSet.size( 1 ) );
365 bndParam_.resize( gridPtr_->numBoundarySegments() );
369 if( nofElParam_ > 0 )
371 std::swap( elParam_[ indexSet.index( element ) ], dgfFactory.parameter( element ) );
372 assert( elParam_[ indexSet.index( element ) ].size() ==
static_cast< std::size_t
>( nofElParam_ ) );
375 if( nofVtxParam_ > 0 )
377 for(
unsigned int v = 0, n = element.subEntities( dimension ); v < n; ++v )
379 const auto index = indexSet.subIndex( element, v, dimension );
380 if( vtxParam_[ index ].empty() )
381 std::swap( vtxParam_[ index ], dgfFactory.parameter( element.template subEntity< dimension >( v ) ) );
382 assert( vtxParam_[ index ].size() ==
static_cast< std::size_t
>( nofVtxParam_ ) );
386 if( element.hasBoundaryIntersections() )
388 for(
const auto &intersection : intersections( gridView, element ) )
391 if( !intersection.boundary() || intersection.type().isNone() )
394 const auto k = indexSet.subIndex( element, intersection.indexInInside(), 1 );
395 bndId_[ k ] = dgfFactory.boundaryId( intersection );
397 bndParam_[ intersection.boundarySegmentIndex() ] = dgfFactory.boundaryParameter( intersection );
403 template <
class Entity>
404 std::vector< double > ¶ms (
const Entity &entity )
406 const auto gridView = gridPtr_->levelGridView( 0 );
410 if( nofElParam_ > 0 ) {
411 if ( gridView.indexSet().index( entity ) >= elParam_.size() )
412 elParam_.resize( gridView.indexSet().index( entity ) );
413 return elParam_[ gridView.indexSet().index( entity ) ];
416 case GridType::dimension :
417 if( nofVtxParam_ > 0 ) {
418 if ( gridView.indexSet().index( entity ) >= vtxParam_.size() )
419 vtxParam_.resize( gridView.indexSet().index( entity ) );
420 return vtxParam_[ gridView.indexSet().index( entity ) ];
427 void setNofParams(
int cdim,
int nofP )
430 case 0 : nofElParam_ = nofP;
break;
431 case GridType::dimension : nofVtxParam_ = nofP;
break;
436 :
public CommDataHandleIF< DataHandle, char >
438 explicit DataHandle (
GridPtr &gridPtr )
439 : gridPtr_( gridPtr ), idSet_( gridPtr->localIdSet() )
441 const auto gridView = gridPtr_->levelGridView( 0 );
442 const auto &indexSet = gridView.indexSet();
446 if( gridPtr_.nofElParam_ > 0 )
447 std::swap( gridPtr_.elParam_[ indexSet.index( element ) ], elData_[ idSet_.id( element ) ] );
449 if( gridPtr_.nofVtxParam_ > 0 )
451 for(
unsigned int v = 0, n = element.subEntities( dimension ); v < n; ++v )
453 const auto index = indexSet.subIndex( element, v, dimension );
454 if ( !gridPtr_.vtxParam_[ index ].empty() )
455 std::swap( gridPtr_.vtxParam_[ index ], vtxData_[ idSet_.subId( element, v, dimension ) ] );
459 if( element.hasBoundaryIntersections() )
461 for(
const auto &intersection : intersections( gridView, element ) )
464 if( !intersection.boundary() || intersection.type().isNone() )
467 const int i = intersection.indexInInside();
468 auto &bndData = bndData_[ idSet_.subId( element, i, 1 ) ];
469 bndData.first = gridPtr_.bndId_[ indexSet.subIndex( element, i, 1 ) ];
470 if( gridPtr_.haveBndParam_ )
471 std::swap( bndData.second, gridPtr_.bndParam_[ intersection.boundarySegmentIndex() ] );
477 DataHandle (
const DataHandle & ) =
delete;
478 DataHandle ( DataHandle && ) =
delete;
482 const auto gridView = gridPtr_->levelGridView( 0 );
483 const auto &indexSet = gridView.indexSet();
485 if( gridPtr_.nofElParam_ > 0 )
486 gridPtr_.elParam_.resize( indexSet.size( 0 ) );
487 if( gridPtr_.nofVtxParam_ > 0 )
488 gridPtr_.vtxParam_.resize( indexSet.size( dimension ) );
492 if( gridPtr_.nofElParam_ > 0 )
494 std::swap( gridPtr_.elParam_[ indexSet.index( element ) ], elData_[ idSet_.id( element ) ] );
495 assert( gridPtr_.elParam_[ indexSet.index( element ) ].size() ==
static_cast< std::size_t
>( gridPtr_.nofElParam_ ) );
498 if( gridPtr_.nofVtxParam_ > 0 )
500 for(
unsigned int v = 0; v < element.subEntities( dimension ); ++v )
502 const auto index = indexSet.subIndex( element, v, dimension );
503 if( gridPtr_.vtxParam_[ index ].empty() )
504 std::swap( gridPtr_.vtxParam_[ index ], vtxData_[ idSet_.subId( element, v, dimension ) ] );
505 assert( gridPtr_.vtxParam_[ index ].size() ==
static_cast< std::size_t
>( gridPtr_.nofVtxParam_ ) );
509 if( element.hasBoundaryIntersections() )
511 for(
const auto &intersection : intersections( gridView, element ) )
514 if( !intersection.boundary() || intersection.type().isNone() )
517 const int i = intersection.indexInInside();
518 auto &bndData = bndData_[ idSet_.subId( element, i, 1 ) ];
519 gridPtr_.bndId_[ indexSet.subIndex( element, i, 1 ) ] = bndData.first;
520 if( gridPtr_.haveBndParam_ )
521 std::swap( bndData.second, gridPtr_.bndParam_[ intersection.boundarySegmentIndex() ] );
529 bool contains (
int dim,
int codim )
const
531 assert( dim == dimension );
533 return (codim == 1) || ((codim == dimension) && (gridPtr_.nofVtxParam_ > 0)) || ((codim == 0) && (gridPtr_.nofElParam_ > 0));
536 bool fixedSize (
int dim,
int codim)
const {
return false; }
538 template<
class Entity >
539 std::size_t
size (
const Entity &entity )
const
541 std::size_t
size = 0;
546 assert( elData_[ idSet_.id( entity ) ].size() ==
static_cast< std::size_t
>( gridPtr_.nofElParam_ ) );
547 for(
double &v : elData_[ idSet_.id( entity ) ] )
548 size += dataSize( v );
553 assert( vtxData_[ idSet_.id( entity ) ].size() ==
static_cast< std::size_t
>( gridPtr_.nofVtxParam_ ) );
554 for(
double &v : vtxData_[ idSet_.id( entity ) ] )
555 size += dataSize( v );
560 const auto bndData = bndData_.find( idSet_.id( entity ) );
561 if( bndData != bndData_.end() )
562 size += dataSize( bndData->second.first ) + dataSize( bndData->second.second );
568 template<
class Buffer,
class Entity >
569 void gather ( Buffer &buffer,
const Entity &entity )
const
574 assert( elData_[ idSet_.id( entity ) ].size() ==
static_cast< std::size_t
>( gridPtr_.nofElParam_ ) );
575 for(
double &v : elData_[ idSet_.id( entity ) ] )
581 assert( vtxData_[ idSet_.id( entity ) ].size() ==
static_cast< std::size_t
>( gridPtr_.nofVtxParam_ ) );
582 for(
double &v : vtxData_[ idSet_.id( entity ) ] )
588 const auto bndData = bndData_.find( idSet_.id( entity ) );
589 if( bndData != bndData_.end() )
591 write( buffer, bndData->second.first );
592 write( buffer, bndData->second.second );
597 template<
class Buffer,
class Entity >
598 void scatter ( Buffer &buffer,
const Entity &entity, std::size_t n )
603 auto &p = elData_[ idSet_.id( entity ) ];
604 p.resize( gridPtr_.nofElParam_ );
606 read( buffer, v, n );
611 auto &p = vtxData_[ idSet_.id( entity ) ];
612 p.resize( gridPtr_.nofVtxParam_ );
614 read( buffer, v, n );
619 auto &bndData = bndData_[ idSet_.id( entity ) ];
620 read( buffer, bndData.first, n );
621 read( buffer, bndData.second, n );
629 static std::enable_if_t< std::is_trivially_copyable< T >::value, std::size_t > dataSize (
const T &value )
634 static std::size_t dataSize (
const std::string &s )
636 return dataSize( s.size() ) + s.size();
639 template<
class Buffer,
class T >
640 static std::enable_if_t< std::is_trivially_copyable< T >::value > write ( Buffer &buffer,
const T &value )
642 std::array< char,
sizeof( T ) > bytes;
643 std::memcpy( bytes.data(), &value,
sizeof( T ) );
644 for(
char &b : bytes )
648 template<
class Buffer >
649 static void write ( Buffer &buffer,
const std::string &s )
651 write( buffer, s.size() );
652 for(
const char &c : s )
656 template<
class Buffer,
class T >
657 static std::enable_if_t< std::is_trivially_copyable< T >::value > read ( Buffer &buffer, T &value, std::size_t &n )
659 assert( n >=
sizeof( T ) );
662 std::array< char,
sizeof( T ) > bytes;
663 for(
char &b : bytes )
665 std::memcpy( &value, bytes.data(),
sizeof( T ) );
668 template<
class Buffer >
669 static void read ( Buffer &buffer, std::string &s, std::size_t &n )
672 read( buffer, size, n );
683 const typename GridType::LocalIdSet &idSet_;
684 mutable std::map< typename GridType::LocalIdSet::IdType, std::vector< double > > elData_, vtxData_;
685 mutable std::map< typename GridType::LocalIdSet::IdType, std::pair< int, DGFBoundaryParameter::type > > bndData_;
689 mutable mygrid_ptr gridPtr_;
691 std::vector< std::vector< double > > elParam_;
692 std::vector< std::vector< double > > vtxParam_;
693 std::vector< DGFBoundaryParameter::type > bndParam_;
694 std::vector< int > bndId_;
695 std::vector< double > emptyParam_;
void scatter(MessageBufferImp &buff, const EntityType &e, size_t n)
unpack data from message buffer to user.
Definition: datahandleif.hh:167
size_t size(const EntityType &e) const
how many objects of type DataType have to be sent for a given entity
Definition: datahandleif.hh:142
void gather(MessageBufferImp &buff, const EntityType &e) const
pack data from user to message buffer
Definition: datahandleif.hh:153
Wrapper class for entities.
Definition: entity.hh:64
@ codimension
Know your own codimension.
Definition: entity.hh:105
Grid view abstract base class.
Definition: gridview.hh:60
Intersection of a mesh entity of codimension 0 ("element") with a "neighboring" element or with the d...
Definition: intersection.hh:162
size_t boundarySegmentIndex() const
index of the boundary segment within the macro grid
Definition: intersection.hh:246
MPI_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:178
static MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:186
Describes the parallel communication interface class for MessageBuffers and DataHandles.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
const IndexSet & indexSet() const
obtain the index set
Definition: gridview.hh:172
Helpers for dealing with MPI.
constexpr All all
PartitionSet for all partitions.
Definition: partitionset.hh:294
constexpr InteriorBorder interiorBorder
PartitionSet for the interior and border partitions.
Definition: partitionset.hh:285
Dune namespace.
Definition: alignedallocator.hh:10
This file implements the class shared_ptr (a reference counting pointer), for those systems that don'...
static const type & defaultValue()
default constructor
Definition: parser.hh:26
std::string type
type of additional boundary parameters
Definition: parser.hh:23
Class for constructing grids from DGF files.
Definition: gridptr.hh:61
GridPtr(std::istream &input, MPICommunicatorType comm=MPIHelper::getCommunicator())
constructor given a std::istream
Definition: gridptr.hh:137
const std::vector< double > & parameters(const Entity &entity) const
get parameters defined for each codim 0 und dim entity on the grid through the grid file
Definition: gridptr.hh:272
GridPtr(const GridPtr &org)
Copy constructor, copies internal auto pointer.
Definition: gridptr.hh:180
GridPtr()
Default constructor, creating empty GridPtr.
Definition: gridptr.hh:154
const GridType & operator*() const
return const reference to GridType instance
Definition: gridptr.hh:235
GridPtr(GridType *grd)
Constructor storing given pointer to internal auto pointer.
Definition: gridptr.hh:167
const DGFBoundaryParameter::type & parameters(const Intersection< GridImp, IntersectionImp > &intersection) const
get parameters for intersection
Definition: gridptr.hh:298
GridType & operator*()
return reference to GridType instance
Definition: gridptr.hh:225
int nofParameters(const Entity &) const
get parameters defined for given entity
Definition: gridptr.hh:258
GridPtr(const std::string &filename, MPICommunicatorType comm=MPIHelper::getCommunicator())
constructor given the name of a DGF file
Definition: gridptr.hh:120
GridPtr & operator=(const GridPtr &org)
assignment of grid pointer
Definition: gridptr.hh:193
int nofParameters(int cdim) const
get number of parameters defined for a given codimension
Definition: gridptr.hh:248
int nofParameters(const Intersection< GridImp, IntersectionImp > &intersection) const
get number of parameters defined for a given intersection
Definition: gridptr.hh:265
const GridType * operator->() const
return const pointer to GridType instance
Definition: gridptr.hh:240
GridType * release()
release pointer from internal ownership
Definition: gridptr.hh:245
GridType * operator->()
return pointer to GridType instance
Definition: gridptr.hh:230
implements the Deleter concept of shared_ptr without deleting anything
Definition: shared_ptr.hh:52
std::size_t fixedSize
The number of data items per index if it is fixed, 0 otherwise.
Definition: variablesizecommunicator.hh:245