3 #ifndef DUNE_DGF_GRIDPTR_HH 4 #define DUNE_DGF_GRIDPTR_HH 13 #include <type_traits> 17 #include <dune/common/parallel/mpihelper.hh> 18 #include <dune/common/shared_ptr.hh> 37 struct DGFGridFactory;
39 template<
class Gr
idImp,
class IntersectionImp >
59 template<
class Gr
idType >
64 typedef std::shared_ptr< GridType > base_t ;
66 typedef null_deleter< GridType > emptydeleter_t ;
71 if( use_count() == 1 )
75 if( grd )
delete grd ;
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() ) {}
94 explicit mygrid_ptr( GridType* grd ) : base_t( grd, emptydeleter_t() ) {}
109 GridType* grd = this->
get();
110 base_t ptr(( GridType * ) 0, emptydeleter_t() );
120 explicit GridPtr (
const std::string &filename,
121 MPICommunicatorType comm = MPIHelper::getCommunicator() )
138 MPICommunicatorType comm = MPIHelper::getCommunicator() )
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 );
281 assert( (
unsigned int)gridView.
indexSet().index( entity ) <
elParam_.size() );
285 case GridType::dimension :
297 template<
class Gr
idImp,
class IntersectionImp >
327 template<
class Range >
330 return range.begin() == range.end();
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 >();
345 gridView.comm().max( nofParams.data(), nofParams.size() );
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" );
361 vtxParam_.resize( nofVtxParam_ > 0 ? indexSet.size( dimension ) : 0 );
363 bndId_.resize( indexSet.size( 1 ) );
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 );
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 );
403 template <
class Entity>
406 const auto gridView =
gridPtr_->levelGridView( 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 :
418 if ( gridView.indexSet().index( entity ) >=
vtxParam_.size() )
419 vtxParam_.resize( gridView.indexSet().index( entity ) );
420 return vtxParam_[ gridView.indexSet().index( entity ) ];
439 :
gridPtr_( gridPtr ), idSet_( gridPtr->localIdSet() )
441 const auto gridView =
gridPtr_->levelGridView( 0 );
442 const auto &indexSet = gridView.indexSet();
447 std::swap(
gridPtr_.elParam_[ indexSet.index( element ) ], elData_[ idSet_.id( element ) ] );
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 ) ];
471 std::swap( bndData.second,
gridPtr_.bndParam_[ intersection.boundarySegmentIndex() ] );
482 const auto gridView =
gridPtr_->levelGridView( 0 );
483 const auto &indexSet = gridView.indexSet();
486 gridPtr_.elParam_.resize( indexSet.size( 0 ) );
488 gridPtr_.vtxParam_.resize( indexSet.size( dimension ) );
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_ ) );
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;
521 std::swap( bndData.second,
gridPtr_.bndParam_[ intersection.boundarySegmentIndex() ] );
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 >
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 >
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 >
603 auto &p = elData_[ idSet_.id( entity ) ];
606 read( buffer, v, n );
611 auto &p = vtxData_[ idSet_.id( entity ) ];
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_;
int nofParameters(const Intersection< GridImp, IntersectionImp > &intersection) const
get number of parameters defined for a given intersection
Definition: gridptr.hh:265
int nofParameters(int cdim) const
get number of parameters defined for a given codimension
Definition: gridptr.hh:248
mygrid_ptr & operator=(const mygrid_ptr &other)
Definition: gridptr.hh:100
GridPtr(std::istream &input, MPICommunicatorType comm=MPIHelper::getCommunicator())
constructor given a std::istream
Definition: gridptr.hh:137
GridType * operator->()
return pointer to GridType instance
Definition: gridptr.hh:230
Describes the parallel communication interface class for MessageBuffers and DataHandles.
GridPtr(GridType *grd)
Constructor storing given pointer to internal auto pointer.
Definition: gridptr.hh:167
~DataHandle()
Definition: gridptr.hh:480
std::vector< std::vector< double > > vtxParam_
Definition: gridptr.hh:692
void communicate()
Definition: gridptr.hh:307
void gather(Buffer &buffer, const Entity &entity) const
Definition: gridptr.hh:569
GridType * release()
Definition: gridptr.hh:107
GridType & operator*()
return reference to GridType instance
Definition: gridptr.hh:225
static const int dimension
Definition: gridptr.hh:117
const DGFBoundaryParameter::type & parameters(const Intersection< GridImp, IntersectionImp > &intersection) const
get parameters for intersection
Definition: gridptr.hh:298
int nofElParam_
Definition: gridptr.hh:697
mygrid_ptr()
Definition: gridptr.hh:90
Definition: gridptr.hh:62
CommDataHandleIF describes the features of a data handle for communication in parallel runs using the...
Definition: datahandleif.hh:75
exception class for IO errors in the DGF parser
Definition: dgfexception.hh:12
std::vector< double > & parameter(const Element &element)
Definition: dgfgridfactory.hh:129
std::size_t size(const Entity &entity) const
Definition: gridptr.hh:539
std::vector< std::vector< double > > elParam_
Definition: gridptr.hh:691
Wrapper class for entities.
Definition: common/entity.hh:63
int nofParameters(const Entity &) const
get parameters defined for given entity
Definition: gridptr.hh:258
const IndexSet & indexSet() const
obtain the index set
Definition: common/gridview.hh:172
const DGFBoundaryParameter::type & boundaryParameter(const Intersection< GG, II > &intersection) const
Definition: dgfgridfactory.hh:163
Class for constructing grids from DGF files.
Definition: gridptr.hh:60
void initialize(DGFGridFactory< GridType > &dgfFactory)
Definition: gridptr.hh:333
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
CommDataHandleIF< DataHandle, char > & interface()
Definition: gridptr.hh:527
GridPtr()
Default constructor, creating empty GridPtr.
Definition: gridptr.hh:154
std::vector< double > & params(const Entity &entity)
Definition: gridptr.hh:404
constexpr InteriorBorder interiorBorder
PartitionSet for the interior and border partitions.
Definition: partitionset.hh:285
GridPtr(const std::string &filename, MPICommunicatorType comm=MPIHelper::getCommunicator())
constructor given the name of a DGF file
Definition: gridptr.hh:120
constexpr All all
PartitionSet for all partitions.
Definition: partitionset.hh:294
const GridType & operator*() const
return const reference to GridType instance
Definition: gridptr.hh:235
Know your own codimension.
Definition: common/entity.hh:105
Grid * grid()
Definition: dgfgridfactory.hh:95
void loadBalance()
Definition: gridptr.hh:316
std::string type
type of additional boundary parameters
Definition: parser.hh:23
GridType * release()
release pointer from internal ownership
Definition: gridptr.hh:245
std::vector< int > bndId_
Definition: gridptr.hh:694
Intersection of a mesh entity of codimension 0 ("element") with a "neighboring" element or with the d...
Definition: albertagrid/dgfparser.hh:26
Grid view abstract base class.
Definition: common/gridview.hh:59
std::vector< DGFBoundaryParameter::type > bndParam_
Definition: gridptr.hh:693
DataHandle(GridPtr &gridPtr)
Definition: gridptr.hh:438
MPIHelper::MPICommunicator MPICommunicatorType
Definition: gridptr.hh:116
bool haveBoundaryParameters() const
Definition: dgfgridfactory.hh:156
size_t boundarySegmentIndex() const
index of the boundary segment within the macro grid
Definition: common/intersection.hh:246
void setNofParams(int cdim, int nofP)
Definition: gridptr.hh:427
mygrid_ptr(GridType *grd)
Definition: gridptr.hh:94
static bool isEmpty(Range &&range)
Definition: gridptr.hh:328
void scatter(Buffer &buffer, const Entity &entity, std::size_t n)
Definition: gridptr.hh:598
bool haveBndParam_
Definition: gridptr.hh:699
const GridType * operator->() const
return const pointer to GridType instance
Definition: gridptr.hh:240
bool fixedSize(int dim, int codim) const
Definition: gridptr.hh:536
mygrid_ptr gridPtr_
Definition: gridptr.hh:689
int boundaryId(const Intersection &intersection) const
Definition: dgfgridfactory.hh:107
GridPtr(const GridPtr &org)
Copy constructor, copies internal auto pointer.
Definition: gridptr.hh:180
bool contains(int dim, int codim) const
Definition: gridptr.hh:529
send interior and border, receive all entities
Definition: gridenums.hh:86
communicate as given in InterfaceType
Definition: gridenums.hh:169
std::vector< double > emptyParam_
Definition: gridptr.hh:695
~mygrid_ptr()
Definition: gridptr.hh:97
static const type & defaultValue()
default constructor
Definition: parser.hh:26
Definition: gridptr.hh:435
mygrid_ptr(const mygrid_ptr &other)
Definition: gridptr.hh:92
Include standard header files.
Definition: agrid.hh:58
int nofVtxParam_
Definition: gridptr.hh:698