3#ifndef DUNE_DGF_GRIDPTR_HH
4#define DUNE_DGF_GRIDPTR_HH
21#include <dune/grid/common/gridenums.hh>
23#include <dune/grid/common/intersection.hh>
24#include <dune/grid/common/partitionset.hh>
25#include <dune/grid/common/rangegenerators.hh>
27#include <dune/grid/io/file/dgfparser/dgfexception.hh>
28#include <dune/grid/io/file/dgfparser/entitykey.hh>
29#include <dune/grid/io/file/dgfparser/parser.hh>
31#include <dune/grid/io/file/gmshreader.hh>
40 struct DGFGridFactory;
42 template<
class Gr
idImp,
class IntersectionImp >
62 template<
class Gr
idType >
65 class mygrid_ptr :
public std::shared_ptr< GridType >
67 typedef std::shared_ptr< GridType > base_t ;
74 if( use_count() == 1 )
78 if( grd )
delete grd ;
82 void assignObj(
const mygrid_ptr& other )
85 base_t :: operator = ( other );
89 using base_t :: swap ;
90 using base_t :: use_count ;
93 mygrid_ptr() : base_t( ( GridType * ) 0, emptydeleter_t() ) {}
95 mygrid_ptr(
const mygrid_ptr& other ) { assignObj( other ); }
97 explicit mygrid_ptr( GridType* grd ) : base_t( grd, emptydeleter_t() ) {}
100 ~mygrid_ptr() { removeObj(); }
103 mygrid_ptr&
operator = (
const mygrid_ptr& other )
112 GridType* grd = this->get();
113 base_t ptr(( GridType * ) 0, emptydeleter_t() );
120 std::string getFileExtension(
const std::string& filename )
const
123 auto extpos = filename.find_last_of(
".");
125 if( extpos != std::string::npos)
126 ext = filename.substr( extpos + 1 );
129 for(
auto& item : ext )
130 item = std::tolower( item );
135 void readGmsh(
const std::string& filename, std::integral_constant< bool, true > )
138 std::vector<int> boundaryIDs;
139 std::vector<int> elementsIDs;
141 initialize( gridFactory, boundaryIDs,elementsIDs);
145 void readGmsh(
const std::string& filename, std::integral_constant< bool, false > )
153 static const int dimension = GridType::dimension;
156 explicit GridPtr (
const std::string &filename,
166 haveBndParam_( false )
168 std::string fileExt = getFileExtension( filename );
170 if( fileExt ==
"dgf" )
172 DGFGridFactory< GridType > dgfFactory( filename, comm );
173 initialize( dgfFactory );
175 else if( fileExt ==
"msh" )
178 readGmsh( filename, std::integral_constant<
bool, GridType::dimensionworld <= 3 > () );
180 else if( fileExt ==
"amc" || fileExt ==
"2d" || fileExt ==
"3d" )
185 else if( fileExt ==
"vtu" )
207 haveBndParam_( false )
210 DGFGridFactory< GridType > dgfFactory( input, comm );
211 initialize( dgfFactory );
224 haveBndParam_( false )
237 haveBndParam_( false )
246 gridPtr_ = org.gridPtr_;
247 elParam_ = org.elParam_;
248 vtxParam_ = org.vtxParam_;
249 bndParam_ = org.bndParam_;
251 emptyParam_ = org.emptyParam_;
253 nofElParam_ = org.nofElParam_;
254 nofVtxParam_ = org.nofVtxParam_;
255 haveBndParam_ = org.haveBndParam_;
262 gridPtr_ = mygrid_ptr( grd );
267 emptyParam_.resize(0);
271 haveBndParam_ =
false;
282 return gridPtr_.operator -> ();
292 return gridPtr_.operator -> ();
296 GridType*
release () {
return gridPtr_.release(); }
301 case 0 :
return nofElParam_;
break;
302 case GridType::dimension :
return nofVtxParam_;
break;
308 template <
class Entity>
315 template<
class Gr
idImp,
class IntersectionImp >
322 template <
class Entity>
325 typedef typename GridType::LevelGridView
GridView;
326 GridView gridView = gridPtr_->levelGridView( 0 );
330 if( nofElParam_ > 0 )
332 assert( (
unsigned int)gridView.
indexSet().index( entity ) < elParam_.size() );
333 return elParam_[ gridView.
indexSet().index( entity ) ];
336 case GridType::dimension :
337 if( nofVtxParam_ > 0 )
339 assert( (
unsigned int)gridView.
indexSet().index( entity ) < vtxParam_.size() );
340 return vtxParam_[ gridView.
indexSet().index( entity ) ];
348 template<
class Gr
idImp,
class IntersectionImp >
352 if ( !haveBndParam_ )
360 if( gridPtr_->comm().size() > 1 )
362 DataHandle dh(*
this);
369 if( gridPtr_->comm().size() > 1 )
371 DataHandle dh(*
this);
372 gridPtr_->loadBalance( dh.interface() );
378 template<
class Range >
379 static bool isEmpty ( Range &&range )
381 return range.begin() == range.end();
384 void initialize ( DGFGridFactory< GridType > &dgfFactory )
386 gridPtr_ = mygrid_ptr( dgfFactory.grid() );
388 const auto gridView = gridPtr_->levelGridView( 0 );
389 const auto &indexSet = gridView.indexSet();
391 nofElParam_ = dgfFactory.template numParameters< 0 >();
392 nofVtxParam_ = dgfFactory.template numParameters< dimension >();
393 haveBndParam_ = dgfFactory.haveBoundaryParameters();
395 std::array< int, 3 > nofParams = {{ nofElParam_, nofVtxParam_,
static_cast< int >( haveBndParam_ ) }};
396 gridView.comm().max( nofParams.data(), nofParams.size() );
401 nofElParam_ = nofParams[ 0 ];
402 nofVtxParam_ = nofParams[ 1 ];
406 haveBndParam_ =
static_cast< bool >( nofParams[ 2 ] );
408 if( (nofElParam_ != nofParams[ 0 ]) || (nofVtxParam_ != nofParams[ 1 ]) )
409 DUNE_THROW( DGFException,
"Number of parameters differs between processes" );
411 elParam_.resize( nofElParam_ > 0 ? indexSet.size( 0 ) : 0 );
412 vtxParam_.resize( nofVtxParam_ > 0 ? indexSet.size( dimension ) : 0 );
414 bndId_.resize( indexSet.size( 1 ) );
416 bndParam_.resize( gridPtr_->numBoundarySegments() );
420 if( nofElParam_ > 0 )
422 std::swap( elParam_[ indexSet.index( element ) ], dgfFactory.parameter( element ) );
423 assert( elParam_[ indexSet.index( element ) ].size() ==
static_cast< std::size_t
>( nofElParam_ ) );
426 if( nofVtxParam_ > 0 )
428 for(
unsigned int v = 0, n = element.subEntities( dimension ); v < n; ++v )
430 const auto index = indexSet.subIndex( element, v, dimension );
431 if( vtxParam_[ index ].empty() )
432 std::swap( vtxParam_[ index ], dgfFactory.parameter( element.template subEntity< dimension >( v ) ) );
433 assert( vtxParam_[ index ].size() ==
static_cast< std::size_t
>( nofVtxParam_ ) );
437 if( element.hasBoundaryIntersections() )
439 for(
const auto &intersection : intersections( gridView, element ) )
442 if( !intersection.boundary() || intersection.type().isNone() )
445 const auto k = indexSet.subIndex( element, intersection.indexInInside(), 1 );
446 bndId_[ k ] = dgfFactory.boundaryId( intersection );
448 bndParam_[ intersection.boundarySegmentIndex() ] = dgfFactory.boundaryParameter( intersection );
454 void initialize ( GridFactory< GridType > &factory,
455 std::vector<int>& boundaryIds,
456 std::vector<int>& elementIds )
458 gridPtr_ = mygrid_ptr( factory.createGrid().release() );
460 const auto& gridView = gridPtr_->leafGridView();
461 const auto& indexSet = gridView.indexSet();
463 nofElParam_ = elementIds.empty() ? 0 : 1 ;
465 haveBndParam_ = boundaryIds.empty() ? 0 : 1 ;
467 std::array< int, 3 > nofParams = {{ nofElParam_, nofVtxParam_,
static_cast< int >( haveBndParam_ ) }};
468 gridView.comm().max( nofParams.data(), nofParams.size() );
473 nofElParam_ = nofParams[ 0 ];
477 haveBndParam_ =
static_cast< bool >( nofParams[ 2 ] );
480 if(!boundaryIds.empty() || !elementIds.empty() )
482 bndParam_.resize( boundaryIds.size() );
483 elParam_.resize( elementIds.size(), std::vector<double>(1) );
484 for(
const auto& entity : elements( gridView ))
486 elParam_[ indexSet.index( entity ) ][ 0 ] = elementIds[ factory.insertionIndex( entity ) ];
489 for(
const auto& intersection : intersections( gridView,entity) )
491 if(intersection.boundary())
494 bndParam_[intersection.boundarySegmentIndex()] = std::to_string(boundaryIds[factory.insertionIndex(intersection)]);
502 template <
class Entity>
503 std::vector< double > ¶ms (
const Entity &entity )
505 const auto gridView = gridPtr_->levelGridView( 0 );
509 if( nofElParam_ > 0 ) {
510 if ( gridView.indexSet().index( entity ) >= elParam_.size() )
511 elParam_.resize( gridView.indexSet().index( entity ) );
512 return elParam_[ gridView.indexSet().index( entity ) ];
515 case GridType::dimension :
516 if( nofVtxParam_ > 0 ) {
517 if ( gridView.indexSet().index( entity ) >= vtxParam_.size() )
518 vtxParam_.resize( gridView.indexSet().index( entity ) );
519 return vtxParam_[ gridView.indexSet().index( entity ) ];
526 void setNofParams(
int cdim,
int nofP )
529 case 0 : nofElParam_ = nofP;
break;
530 case GridType::dimension : nofVtxParam_ = nofP;
break;
535 :
public CommDataHandleIF< DataHandle, char >
537 explicit DataHandle (
GridPtr &gridPtr )
538 : gridPtr_( gridPtr ), idSet_( gridPtr->localIdSet() )
540 const auto gridView = gridPtr_->levelGridView( 0 );
541 const auto &indexSet = gridView.indexSet();
545 if( gridPtr_.nofElParam_ > 0 )
546 std::swap( gridPtr_.elParam_[ indexSet.index( element ) ], elData_[ idSet_.id( element ) ] );
548 if( gridPtr_.nofVtxParam_ > 0 )
550 for(
unsigned int v = 0, n = element.subEntities( dimension ); v < n; ++v )
552 const auto index = indexSet.subIndex( element, v, dimension );
553 if ( !gridPtr_.vtxParam_[ index ].empty() )
554 std::swap( gridPtr_.vtxParam_[ index ], vtxData_[ idSet_.subId( element, v, dimension ) ] );
558 if( element.hasBoundaryIntersections() )
560 for(
const auto &intersection : intersections( gridView, element ) )
563 if( !intersection.boundary() || intersection.type().isNone() )
566 const int i = intersection.indexInInside();
567 auto &bndData = bndData_[ idSet_.subId( element, i, 1 ) ];
568 bndData.first = gridPtr_.bndId_[ indexSet.subIndex( element, i, 1 ) ];
569 if( gridPtr_.haveBndParam_ )
570 std::swap( bndData.second, gridPtr_.bndParam_[ intersection.boundarySegmentIndex() ] );
576 DataHandle (
const DataHandle & ) =
delete;
577 DataHandle ( DataHandle && ) =
delete;
581 const auto gridView = gridPtr_->levelGridView( 0 );
582 const auto &indexSet = gridView.indexSet();
584 if( gridPtr_.nofElParam_ > 0 )
585 gridPtr_.elParam_.resize( indexSet.size( 0 ) );
586 if( gridPtr_.nofVtxParam_ > 0 )
587 gridPtr_.vtxParam_.resize( indexSet.size( dimension ) );
588 gridPtr_.bndId_.resize( indexSet.size( 1 ) );
589 if( gridPtr_.haveBndParam_ )
590 gridPtr_.bndParam_.resize( gridPtr_->numBoundarySegments() );
594 if( gridPtr_.nofElParam_ > 0 )
596 std::swap( gridPtr_.elParam_[ indexSet.index( element ) ], elData_[ idSet_.id( element ) ] );
597 assert( gridPtr_.elParam_[ indexSet.index( element ) ].size() ==
static_cast< std::size_t
>( gridPtr_.nofElParam_ ) );
600 if( gridPtr_.nofVtxParam_ > 0 )
602 for(
unsigned int v = 0; v < element.subEntities( dimension ); ++v )
604 const auto index = indexSet.subIndex( element, v, dimension );
605 if( gridPtr_.vtxParam_[ index ].empty() )
606 std::swap( gridPtr_.vtxParam_[ index ], vtxData_[ idSet_.subId( element, v, dimension ) ] );
607 assert( gridPtr_.vtxParam_[ index ].size() ==
static_cast< std::size_t
>( gridPtr_.nofVtxParam_ ) );
611 if( element.hasBoundaryIntersections() )
613 for(
const auto &intersection : intersections( gridView, element ) )
616 if( !intersection.boundary() || intersection.type().isNone() )
619 const int i = intersection.indexInInside();
620 auto &bndData = bndData_[ idSet_.subId( element, i, 1 ) ];
621 gridPtr_.bndId_[ indexSet.subIndex( element, i, 1 ) ] = bndData.first;
622 if( gridPtr_.haveBndParam_ )
623 std::swap( bndData.second, gridPtr_.bndParam_[ intersection.boundarySegmentIndex() ] );
631 bool contains (
int dim,
int codim )
const
633 assert( dim == dimension );
635 return (codim == 1) || ((codim == dimension) && (gridPtr_.nofVtxParam_ > 0)) || ((codim == 0) && (gridPtr_.nofElParam_ > 0));
638 bool fixedSize (
int dim,
int codim)
const {
return false; }
640 template<
class Entity >
641 std::size_t
size (
const Entity &entity )
const
643 std::size_t
size = 0;
648 assert( elData_[ idSet_.id( entity ) ].size() ==
static_cast< std::size_t
>( gridPtr_.nofElParam_ ) );
649 for(
double &v : elData_[ idSet_.id( entity ) ] )
650 size += dataSize( v );
655 assert( vtxData_[ idSet_.id( entity ) ].size() ==
static_cast< std::size_t
>( gridPtr_.nofVtxParam_ ) );
656 for(
double &v : vtxData_[ idSet_.id( entity ) ] )
657 size += dataSize( v );
662 const auto bndData = bndData_.find( idSet_.id( entity ) );
663 if( bndData != bndData_.end() )
664 size += dataSize( bndData->second.first ) + dataSize( bndData->second.second );
670 template<
class Buffer,
class Entity >
671 void gather ( Buffer &buffer,
const Entity &entity )
const
676 assert( elData_[ idSet_.id( entity ) ].size() ==
static_cast< std::size_t
>( gridPtr_.nofElParam_ ) );
677 for(
double &v : elData_[ idSet_.id( entity ) ] )
683 assert( vtxData_[ idSet_.id( entity ) ].size() ==
static_cast< std::size_t
>( gridPtr_.nofVtxParam_ ) );
684 for(
double &v : vtxData_[ idSet_.id( entity ) ] )
690 const auto bndData = bndData_.find( idSet_.id( entity ) );
691 if( bndData != bndData_.end() )
693 write( buffer, bndData->second.first );
694 write( buffer, bndData->second.second );
699 template<
class Buffer,
class Entity >
700 void scatter ( Buffer &buffer,
const Entity &entity, std::size_t n )
705 auto &p = elData_[ idSet_.id( entity ) ];
706 p.resize( gridPtr_.nofElParam_ );
708 read( buffer, v, n );
713 auto &p = vtxData_[ idSet_.id( entity ) ];
714 p.resize( gridPtr_.nofVtxParam_ );
716 read( buffer, v, n );
721 auto &bndData = bndData_[ idSet_.id( entity ) ];
722 read( buffer, bndData.first, n );
723 read( buffer, bndData.second, n );
731 static std::enable_if_t< std::is_trivially_copyable< T >::value, std::size_t > dataSize (
const T &value )
736 static std::size_t dataSize (
const std::string &s )
738 return dataSize( s.size() ) + s.size();
741 template<
class Buffer,
class T >
742 static std::enable_if_t< std::is_trivially_copyable< T >::value > write ( Buffer &buffer,
const T &value )
744 std::array< char,
sizeof( T ) > bytes;
745 std::memcpy( bytes.data(), &value,
sizeof( T ) );
746 for(
char &b : bytes )
750 template<
class Buffer >
751 static void write ( Buffer &buffer,
const std::string &s )
753 write( buffer, s.size() );
754 for(
const char &c : s )
758 template<
class Buffer,
class T >
759 static std::enable_if_t< std::is_trivially_copyable< T >::value > read ( Buffer &buffer, T &value, std::size_t &n )
761 assert( n >=
sizeof( T ) );
764 std::array< char,
sizeof( T ) > bytes;
765 for(
char &b : bytes )
767 std::memcpy( &value, bytes.data(),
sizeof( T ) );
770 template<
class Buffer >
771 static void read ( Buffer &buffer, std::string &s, std::size_t &n )
774 read( buffer, size, n );
785 const typename GridType::LocalIdSet &idSet_;
786 mutable std::map< typename GridType::LocalIdSet::IdType, std::vector< double > > elData_, vtxData_;
787 mutable std::map< typename GridType::LocalIdSet::IdType, std::pair< int, DGFBoundaryParameter::type > > bndData_;
791 mutable mygrid_ptr gridPtr_;
793 std::vector< std::vector< double > > elParam_;
794 std::vector< std::vector< double > > vtxParam_;
795 std::vector< DGFBoundaryParameter::type > bndParam_;
796 std::vector< int > bndId_;
797 std::vector< double > emptyParam_;
void scatter(MessageBufferImp &buff, const EntityType &e, size_t n)
unpack data from message buffer to user.
Definition: datahandleif.hh:205
size_t size(const EntityType &e) const
how many objects of type DataType have to be sent for a given entity
Definition: datahandleif.hh:180
void gather(MessageBufferImp &buff, const EntityType &e) const
pack data from user to message buffer
Definition: datahandleif.hh:191
Wrapper class for entities.
Definition: entity.hh:64
@ codimension
Know your own codimension.
Definition: entity.hh:105
static ToUniquePtr< Grid > read(const std::string &fileName, bool verbose=true, bool insertBoundarySegments=true)
Definition: gmshreader.hh:788
Base class for exceptions in Dune grid modules.
Definition: exceptions.hh:18
Provide a generic factory class for unstructured grids.
Definition: gridfactory.hh:269
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:182
static MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:190
Default exception for dummy implementations.
Definition: exceptions.hh:261
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:14
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:64
GridPtr(std::istream &input, MPICommunicatorType comm=MPIHelper::getCommunicator())
constructor given a std::istream
Definition: gridptr.hh:197
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:323
GridPtr()
Default constructor, creating empty GridPtr.
Definition: gridptr.hh:215
const GridType & operator*() const
return const reference to GridType instance
Definition: gridptr.hh:286
GridPtr(GridType *grd)
Constructor storing given pointer to internal auto pointer.
Definition: gridptr.hh:228
const DGFBoundaryParameter::type & parameters(const Intersection< GridImp, IntersectionImp > &intersection) const
get parameters for intersection
Definition: gridptr.hh:349
GridType & operator*()
return reference to GridType instance
Definition: gridptr.hh:276
int nofParameters(const Entity &) const
get parameters defined for given entity
Definition: gridptr.hh:309
GridPtr(const std::string &filename, MPICommunicatorType comm=MPIHelper::getCommunicator())
constructor given the name of a DGF file
Definition: gridptr.hh:156
GridPtr & operator=(const GridPtr &org)
assignment of grid pointer
Definition: gridptr.hh:244
int nofParameters(int cdim) const
get number of parameters defined for a given codimension
Definition: gridptr.hh:299
int nofParameters(const Intersection< GridImp, IntersectionImp > &intersection) const
get number of parameters defined for a given intersection
Definition: gridptr.hh:316
GridPtr(const GridPtr &org)=default
Copy constructor, copies internal auto pointer.
const GridType * operator->() const
return const pointer to GridType instance
Definition: gridptr.hh:291
GridType * release()
release pointer from internal ownership
Definition: gridptr.hh:296
GridType * operator->()
return pointer to GridType instance
Definition: gridptr.hh:281
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