5#ifndef DUNE_DGF_GRIDPTR_HH
6#define DUNE_DGF_GRIDPTR_HH
23#include <dune/grid/common/gridenums.hh>
25#include <dune/grid/common/intersection.hh>
26#include <dune/grid/common/partitionset.hh>
27#include <dune/grid/common/rangegenerators.hh>
29#include <dune/grid/io/file/dgfparser/dgfexception.hh>
30#include <dune/grid/io/file/dgfparser/entitykey.hh>
31#include <dune/grid/io/file/dgfparser/parser.hh>
33#include <dune/grid/io/file/gmshreader.hh>
42 struct DGFGridFactory;
44 template<
class Gr
idImp,
class IntersectionImp >
64 template<
class Gr
idType >
67 class mygrid_ptr :
public std::shared_ptr< GridType >
69 typedef std::shared_ptr< GridType > base_t ;
76 if( use_count() == 1 )
80 if( grd )
delete grd ;
84 void assignObj(
const mygrid_ptr& other )
87 base_t :: operator = ( other );
91 using base_t :: swap ;
92 using base_t :: use_count ;
95 mygrid_ptr() : base_t( ( GridType * ) 0, emptydeleter_t() ) {}
97 mygrid_ptr(
const mygrid_ptr& other ) : base_t(
nullptr) { assignObj( other ); }
99 explicit mygrid_ptr( GridType* grd ) : base_t( grd, emptydeleter_t() ) {}
102 ~mygrid_ptr() { removeObj(); }
105 mygrid_ptr&
operator = (
const mygrid_ptr& other )
114 GridType* grd = this->
get();
115 base_t ptr(( GridType * ) 0, emptydeleter_t() );
122 std::string getFileExtension(
const std::string& filename )
const
125 auto extpos = filename.find_last_of(
".");
127 if( extpos != std::string::npos)
128 ext = filename.substr( extpos + 1 );
131 for(
auto& item : ext )
132 item = std::tolower( item );
137 void readGmsh(
const std::string& filename, std::integral_constant< bool, true > )
140 std::vector<int> boundaryIDs;
141 std::vector<int> elementsIDs;
143 initialize( gridFactory, boundaryIDs,elementsIDs);
147 void readGmsh(
const std::string& filename, std::integral_constant< bool, false > )
155 static const int dimension = GridType::dimension;
158 explicit GridPtr (
const std::string &filename,
168 haveBndParam_( false )
170 std::string fileExt = getFileExtension( filename );
172 if( fileExt ==
"dgf" )
174 DGFGridFactory< GridType > dgfFactory( filename, comm );
175 initialize( dgfFactory );
177 else if( fileExt ==
"msh" )
180 readGmsh( filename, std::integral_constant<
bool, GridType::dimensionworld <= 3 > () );
182 else if( fileExt ==
"amc" || fileExt ==
"2d" || fileExt ==
"3d" )
187 else if( fileExt ==
"vtu" )
209 haveBndParam_( false )
212 DGFGridFactory< GridType > dgfFactory( input, comm );
213 initialize( dgfFactory );
226 haveBndParam_( false )
239 haveBndParam_( false )
248 gridPtr_ = org.gridPtr_;
249 elParam_ = org.elParam_;
250 vtxParam_ = org.vtxParam_;
251 bndParam_ = org.bndParam_;
253 emptyParam_ = org.emptyParam_;
255 nofElParam_ = org.nofElParam_;
256 nofVtxParam_ = org.nofVtxParam_;
257 haveBndParam_ = org.haveBndParam_;
264 gridPtr_ = mygrid_ptr( grd );
269 emptyParam_.resize(0);
273 haveBndParam_ =
false;
284 return gridPtr_.operator -> ();
294 return gridPtr_.operator -> ();
298 GridType*
release () {
return gridPtr_.release(); }
303 case 0 :
return nofElParam_;
break;
304 case GridType::dimension :
return nofVtxParam_;
break;
310 template <
class Entity>
317 template<
class Gr
idImp,
class IntersectionImp >
324 template <
class Entity>
327 typedef typename GridType::LevelGridView
GridView;
328 GridView gridView = gridPtr_->levelGridView( 0 );
332 if( nofElParam_ > 0 )
334 assert( (
unsigned int)gridView.
indexSet().index( entity ) < elParam_.size() );
335 return elParam_[ gridView.
indexSet().index( entity ) ];
338 case GridType::dimension :
339 if( nofVtxParam_ > 0 )
341 assert( (
unsigned int)gridView.
indexSet().index( entity ) < vtxParam_.size() );
342 return vtxParam_[ gridView.
indexSet().index( entity ) ];
350 template<
class Gr
idImp,
class IntersectionImp >
354 if ( !haveBndParam_ )
362 if( gridPtr_->comm().size() > 1 )
364 DataHandle dh(*
this);
371 if( gridPtr_->comm().size() > 1 )
373 DataHandle dh(*
this);
374 gridPtr_->loadBalance( dh.interface() );
380 template<
class Range >
381 static bool isEmpty ( Range &&range )
383 return range.begin() == range.end();
386 void initialize ( DGFGridFactory< GridType > &dgfFactory )
388 gridPtr_ = mygrid_ptr( dgfFactory.grid() );
390 const auto gridView = gridPtr_->levelGridView( 0 );
391 const auto &indexSet = gridView.indexSet();
393 nofElParam_ = dgfFactory.template numParameters< 0 >();
394 nofVtxParam_ = dgfFactory.template numParameters< dimension >();
395 haveBndParam_ = dgfFactory.haveBoundaryParameters();
397 std::array< int, 3 > nofParams = {{ nofElParam_, nofVtxParam_,
static_cast< int >( haveBndParam_ ) }};
398 gridView.comm().max( nofParams.data(), nofParams.size() );
403 nofElParam_ = nofParams[ 0 ];
404 nofVtxParam_ = nofParams[ 1 ];
408 haveBndParam_ =
static_cast< bool >( nofParams[ 2 ] );
410 if( (nofElParam_ != nofParams[ 0 ]) || (nofVtxParam_ != nofParams[ 1 ]) )
411 DUNE_THROW( DGFException,
"Number of parameters differs between processes" );
413 elParam_.resize( nofElParam_ > 0 ? indexSet.size( 0 ) : 0 );
414 vtxParam_.resize( nofVtxParam_ > 0 ? indexSet.size( dimension ) : 0 );
416 bndId_.resize( indexSet.size( 1 ) );
418 bndParam_.resize( gridPtr_->numBoundarySegments() );
422 if( nofElParam_ > 0 )
424 std::swap( elParam_[ indexSet.index( element ) ], dgfFactory.parameter( element ) );
425 assert( elParam_[ indexSet.index( element ) ].size() ==
static_cast< std::size_t
>( nofElParam_ ) );
428 if( nofVtxParam_ > 0 )
430 for(
unsigned int v = 0, n = element.subEntities( dimension ); v < n; ++v )
432 const auto index = indexSet.subIndex( element, v, dimension );
433 if( vtxParam_[ index ].
empty() )
434 std::swap( vtxParam_[ index ], dgfFactory.parameter( element.template subEntity< dimension >( v ) ) );
435 assert( vtxParam_[ index ].
size() ==
static_cast< std::size_t
>( nofVtxParam_ ) );
439 if( element.hasBoundaryIntersections() )
441 for(
const auto &intersection : intersections( gridView, element ) )
444 if( !intersection.boundary() || intersection.type().isNone() )
447 const auto k = indexSet.subIndex( element, intersection.indexInInside(), 1 );
448 bndId_[ k ] = dgfFactory.boundaryId( intersection );
450 bndParam_[ intersection.boundarySegmentIndex() ] = dgfFactory.boundaryParameter( intersection );
456 void initialize ( GridFactory< GridType > &factory,
457 std::vector<int>& boundaryIds,
458 std::vector<int>& elementIds )
460 gridPtr_ = mygrid_ptr( factory.createGrid().release() );
462 const auto& gridView = gridPtr_->leafGridView();
463 const auto& indexSet = gridView.indexSet();
465 nofElParam_ = elementIds.empty() ? 0 : 1 ;
467 haveBndParam_ = boundaryIds.empty() ? 0 : 1 ;
469 std::array< int, 3 > nofParams = {{ nofElParam_, nofVtxParam_,
static_cast< int >( haveBndParam_ ) }};
470 gridView.comm().max( nofParams.data(), nofParams.size() );
475 nofElParam_ = nofParams[ 0 ];
479 haveBndParam_ =
static_cast< bool >( nofParams[ 2 ] );
482 if(!boundaryIds.empty() || !elementIds.empty() )
484 bndParam_.resize( boundaryIds.size() );
485 elParam_.resize( elementIds.size(), std::vector<double>(1) );
486 for(
const auto& entity : elements( gridView ))
488 elParam_[ indexSet.index( entity ) ][ 0 ] = elementIds[ factory.insertionIndex( entity ) ];
491 for(
const auto& intersection : intersections( gridView,entity) )
493 if(intersection.boundary())
496 bndParam_[intersection.boundarySegmentIndex()] = std::to_string(boundaryIds[factory.insertionIndex(intersection)]);
504 template <
class Entity>
505 std::vector< double > ¶ms (
const Entity &entity )
507 const auto gridView = gridPtr_->levelGridView( 0 );
511 if( nofElParam_ > 0 ) {
512 if ( gridView.indexSet().index( entity ) >= elParam_.size() )
513 elParam_.resize( gridView.indexSet().index( entity ) );
514 return elParam_[ gridView.indexSet().index( entity ) ];
517 case GridType::dimension :
518 if( nofVtxParam_ > 0 ) {
519 if ( gridView.indexSet().index( entity ) >= vtxParam_.size() )
520 vtxParam_.resize( gridView.indexSet().index( entity ) );
521 return vtxParam_[ gridView.indexSet().index( entity ) ];
528 void setNofParams(
int cdim,
int nofP )
531 case 0 : nofElParam_ = nofP;
break;
532 case GridType::dimension : nofVtxParam_ = nofP;
break;
537 :
public CommDataHandleIF< DataHandle, char >
539 explicit DataHandle (
GridPtr &gridPtr )
540 : gridPtr_( gridPtr ), idSet_( gridPtr->localIdSet() )
542 const auto gridView = gridPtr_->levelGridView( 0 );
543 const auto &indexSet = gridView.indexSet();
547 if( gridPtr_.nofElParam_ > 0 )
548 std::swap( gridPtr_.elParam_[ indexSet.index( element ) ], elData_[ idSet_.id( element ) ] );
550 if( gridPtr_.nofVtxParam_ > 0 )
552 for(
unsigned int v = 0, n = element.subEntities( dimension ); v < n; ++v )
554 const auto index = indexSet.subIndex( element, v, dimension );
555 if ( !gridPtr_.vtxParam_[ index ].empty() )
556 std::swap( gridPtr_.vtxParam_[ index ], vtxData_[ idSet_.subId( element, v, dimension ) ] );
560 if( element.hasBoundaryIntersections() )
562 for(
const auto &intersection : intersections( gridView, element ) )
565 if( !intersection.boundary() || intersection.type().isNone() )
568 const int i = intersection.indexInInside();
569 auto &bndData = bndData_[ idSet_.subId( element, i, 1 ) ];
570 bndData.first = gridPtr_.bndId_[ indexSet.subIndex( element, i, 1 ) ];
571 if( gridPtr_.haveBndParam_ )
572 std::swap( bndData.second, gridPtr_.bndParam_[ intersection.boundarySegmentIndex() ] );
578 DataHandle (
const DataHandle & ) =
delete;
579 DataHandle ( DataHandle && ) =
delete;
583 const auto gridView = gridPtr_->levelGridView( 0 );
584 const auto &indexSet = gridView.indexSet();
586 if( gridPtr_.nofElParam_ > 0 )
587 gridPtr_.elParam_.resize( indexSet.size( 0 ) );
588 if( gridPtr_.nofVtxParam_ > 0 )
589 gridPtr_.vtxParam_.resize( indexSet.size( dimension ) );
590 gridPtr_.bndId_.resize( indexSet.size( 1 ) );
591 if( gridPtr_.haveBndParam_ )
592 gridPtr_.bndParam_.resize( gridPtr_->numBoundarySegments() );
596 if( gridPtr_.nofElParam_ > 0 )
598 std::swap( gridPtr_.elParam_[ indexSet.index( element ) ], elData_[ idSet_.id( element ) ] );
599 assert( gridPtr_.elParam_[ indexSet.index( element ) ].size() ==
static_cast< std::size_t
>( gridPtr_.nofElParam_ ) );
602 if( gridPtr_.nofVtxParam_ > 0 )
604 for(
unsigned int v = 0; v < element.subEntities( dimension ); ++v )
606 const auto index = indexSet.subIndex( element, v, dimension );
607 if( gridPtr_.vtxParam_[ index ].empty() )
608 std::swap( gridPtr_.vtxParam_[ index ], vtxData_[ idSet_.subId( element, v, dimension ) ] );
609 assert( gridPtr_.vtxParam_[ index ].size() ==
static_cast< std::size_t
>( gridPtr_.nofVtxParam_ ) );
613 if( element.hasBoundaryIntersections() )
615 for(
const auto &intersection : intersections( gridView, element ) )
618 if( !intersection.boundary() || intersection.type().isNone() )
621 const int i = intersection.indexInInside();
622 auto &bndData = bndData_[ idSet_.subId( element, i, 1 ) ];
623 gridPtr_.bndId_[ indexSet.subIndex( element, i, 1 ) ] = bndData.first;
624 if( gridPtr_.haveBndParam_ )
625 std::swap( bndData.second, gridPtr_.bndParam_[ intersection.boundarySegmentIndex() ] );
633 bool contains (
int dim,
int codim )
const
635 assert( dim == dimension );
637 return (codim == 1) || ((codim == dimension) && (gridPtr_.nofVtxParam_ > 0)) || ((codim == 0) && (gridPtr_.nofElParam_ > 0));
640 bool fixedSize (
int ,
int )
const {
return false; }
642 template<
class Entity >
643 std::size_t
size (
const Entity &entity )
const
645 std::size_t totalSize = 0;
650 assert( elData_[ idSet_.id( entity ) ].size() ==
static_cast< std::size_t
>( gridPtr_.nofElParam_ ) );
651 for(
double &v : elData_[ idSet_.id( entity ) ] )
652 totalSize += dataSize( v );
657 assert( vtxData_[ idSet_.id( entity ) ].size() ==
static_cast< std::size_t
>( gridPtr_.nofVtxParam_ ) );
658 for(
double &v : vtxData_[ idSet_.id( entity ) ] )
659 totalSize += dataSize( v );
664 const auto bndData = bndData_.find( idSet_.id( entity ) );
665 if( bndData != bndData_.end() )
666 totalSize += dataSize( bndData->second.first ) + dataSize( bndData->second.second );
672 template<
class Buffer,
class Entity >
673 void gather ( Buffer &buffer,
const Entity &entity )
const
678 assert( elData_[ idSet_.id( entity ) ].size() ==
static_cast< std::size_t
>( gridPtr_.nofElParam_ ) );
679 for(
double &v : elData_[ idSet_.id( entity ) ] )
685 assert( vtxData_[ idSet_.id( entity ) ].size() ==
static_cast< std::size_t
>( gridPtr_.nofVtxParam_ ) );
686 for(
double &v : vtxData_[ idSet_.id( entity ) ] )
692 const auto bndData = bndData_.find( idSet_.id( entity ) );
693 if( bndData != bndData_.end() )
695 write( buffer, bndData->second.first );
696 write( buffer, bndData->second.second );
701 template<
class Buffer,
class Entity >
702 void scatter ( Buffer &buffer,
const Entity &entity, std::size_t n )
707 auto &p = elData_[ idSet_.id( entity ) ];
708 p.resize( gridPtr_.nofElParam_ );
710 read( buffer, v, n );
715 auto &p = vtxData_[ idSet_.id( entity ) ];
716 p.resize( gridPtr_.nofVtxParam_ );
718 read( buffer, v, n );
723 auto &bndData = bndData_[ idSet_.id( entity ) ];
724 read( buffer, bndData.first, n );
725 read( buffer, bndData.second, n );
733 static std::enable_if_t< std::is_trivially_copyable< T >::value, std::size_t > dataSize (
const T & )
738 static std::size_t dataSize (
const std::string &s )
740 return dataSize( s.size() ) + s.size();
743 template<
class Buffer,
class T >
744 static std::enable_if_t< std::is_trivially_copyable< T >::value > write ( Buffer &buffer,
const T &value )
746 std::array< char,
sizeof( T ) > bytes;
747 std::memcpy( bytes.data(), &value,
sizeof( T ) );
748 for(
char &b : bytes )
752 template<
class Buffer >
753 static void write ( Buffer &buffer,
const std::string &s )
755 write( buffer, s.size() );
756 for(
const char &c : s )
760 template<
class Buffer,
class T >
761 static std::enable_if_t< std::is_trivially_copyable< T >::value > read ( Buffer &buffer, T &value, std::size_t &n )
763 assert( n >=
sizeof( T ) );
766 std::array< char,
sizeof( T ) > bytes;
767 for(
char &b : bytes )
769 std::memcpy( &value, bytes.data(),
sizeof( T ) );
772 template<
class Buffer >
773 static void read ( Buffer &buffer, std::string &s, std::size_t &n )
776 read( buffer,
size, n );
787 const typename GridType::LocalIdSet &idSet_;
788 mutable std::map< typename GridType::LocalIdSet::IdType, std::vector< double > > elData_, vtxData_;
789 mutable std::map< typename GridType::LocalIdSet::IdType, std::pair< int, DGFBoundaryParameter::type > > bndData_;
793 mutable mygrid_ptr gridPtr_;
795 std::vector< std::vector< double > > elParam_;
796 std::vector< std::vector< double > > vtxParam_;
797 std::vector< DGFBoundaryParameter::type > bndParam_;
798 std::vector< int > bndId_;
799 std::vector< double > emptyParam_;
void scatter(MessageBufferImp &buff, const EntityType &e, size_t n)
unpack data from message buffer to user.
Definition: datahandleif.hh:143
void gather(MessageBufferImp &buff, const EntityType &e) const
pack data from user to message buffer
Definition: datahandleif.hh:129
Wrapper class for entities.
Definition: entity.hh:66
static constexpr int codimension
Know your own codimension.
Definition: entity.hh:106
static std::unique_ptr< Grid > read(const std::string &fileName, bool verbose=true, bool insertBoundarySegments=true)
Definition: gmshreader.hh:937
Base class for exceptions in Dune grid modules.
Definition: exceptions.hh:20
Provide a generic factory class for unstructured grids.
Definition: gridfactory.hh:275
Grid view abstract base class.
Definition: gridview.hh:66
Intersection of a mesh entity of codimension 0 ("element") with a "neighboring" element or with the d...
Definition: intersection.hh:164
size_t boundarySegmentIndex() const
index of the boundary segment within the macro grid
Definition: intersection.hh:236
MPI_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:192
static MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:200
Default exception for dummy implementations.
Definition: exceptions.hh:263
Describes the parallel communication interface class for MessageBuffers and DataHandles.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
const IndexSet & indexSet() const
obtain the index set
Definition: gridview.hh:177
Helpers for dealing with MPI.
constexpr All all
PartitionSet for all partitions.
Definition: partitionset.hh:295
constexpr InteriorBorder interiorBorder
PartitionSet for the interior and border partitions.
Definition: partitionset.hh:286
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<(sizeof...(II)==0)> empty(std::integer_sequence< T, II... >)
Checks whether the sequence is empty.
Definition: integersequence.hh:80
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
constexpr auto get(std::integer_sequence< T, II... >, std::integral_constant< std::size_t, pos >={})
Return the entry at position pos of the given sequence.
Definition: integersequence.hh:22
This file implements several utilities related to std::shared_ptr.
static const type & defaultValue()
default constructor
Definition: parser.hh:28
std::string type
type of additional boundary parameters
Definition: parser.hh:25
Class for constructing grids from DGF files.
Definition: gridptr.hh:66
GridPtr(std::istream &input, MPICommunicatorType comm=MPIHelper::getCommunicator())
constructor given a std::istream
Definition: gridptr.hh:199
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:325
GridPtr()
Default constructor, creating empty GridPtr.
Definition: gridptr.hh:217
const GridType & operator*() const
return const reference to GridType instance
Definition: gridptr.hh:288
GridPtr(GridType *grd)
Constructor storing given pointer to internal auto pointer.
Definition: gridptr.hh:230
const DGFBoundaryParameter::type & parameters(const Intersection< GridImp, IntersectionImp > &intersection) const
get parameters for intersection
Definition: gridptr.hh:351
GridType & operator*()
return reference to GridType instance
Definition: gridptr.hh:278
int nofParameters(const Entity &) const
get parameters defined for given entity
Definition: gridptr.hh:311
GridPtr(const std::string &filename, MPICommunicatorType comm=MPIHelper::getCommunicator())
constructor given the name of a DGF file
Definition: gridptr.hh:158
GridPtr & operator=(const GridPtr &org)
assignment of grid pointer
Definition: gridptr.hh:246
int nofParameters(int cdim) const
get number of parameters defined for a given codimension
Definition: gridptr.hh:301
int nofParameters(const Intersection< GridImp, IntersectionImp > &intersection) const
get number of parameters defined for a given intersection
Definition: gridptr.hh:318
GridPtr(const GridPtr &org)=default
Copy constructor, copies internal auto pointer.
const GridType * operator->() const
return const pointer to GridType instance
Definition: gridptr.hh:293
GridType * release()
release pointer from internal ownership
Definition: gridptr.hh:298
GridType * operator->()
return pointer to GridType instance
Definition: gridptr.hh:283
implements the Deleter concept of shared_ptr without deleting anything
Definition: shared_ptr.hh:49
std::size_t fixedSize
The number of data items per index if it is fixed, 0 otherwise.
Definition: variablesizecommunicator.hh:264