3 #ifndef DUNE_DGF_GRIDPTR_HH
4 #define DUNE_DGF_GRIDPTR_HH
14 #include <type_traits>
18 #include <dune/common/parallel/mpihelper.hh>
19 #include <dune/common/shared_ptr.hh>
40 struct DGFGridFactory;
42 template<
class Gr
idImp,
class IntersectionImp >
62 template<
class Gr
idType >
67 typedef std::shared_ptr< GridType > base_t ;
69 typedef null_deleter< GridType > emptydeleter_t ;
74 if( use_count() == 1 )
78 if( grd )
delete grd ;
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() ) {}
97 explicit mygrid_ptr( GridType* grd ) : base_t( grd, emptydeleter_t() ) {}
112 GridType* grd = this->get();
113 base_t ptr(( GridType * ) 0, emptydeleter_t() );
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 > )
147 DUNE_THROW(
GridError,
"GmshReader requires dimWorld <= 3." );
156 explicit GridPtr (
const std::string &filename,
170 if( fileExt ==
"dgf" )
175 else if( fileExt ==
"msh" )
178 readGmsh( filename, std::integral_constant<
bool, GridType::dimensionworld <= 3 > () );
180 else if( fileExt ==
"amc" || fileExt ==
"2d" || fileExt ==
"3d" )
183 DUNE_THROW( NotImplemented,
"GridPtr: file format '" << fileExt <<
"' not supported yet!" );
185 else if( fileExt ==
"vtu" )
188 DUNE_THROW( NotImplemented,
"GridPtr: file format '" << fileExt <<
"' not supported yet!" );
192 DUNE_THROW( NotImplemented,
"GridPtr: file format '" << fileExt <<
"' not supported yet!" );
308 template <
class Entity>
315 template<
class Gr
idImp,
class IntersectionImp >
322 template <
class Entity>
325 typedef typename GridType::LevelGridView
GridView;
332 assert( (
unsigned int)gridView.
indexSet().index( entity ) <
elParam_.size() );
336 case GridType::dimension :
348 template<
class Gr
idImp,
class IntersectionImp >
378 template<
class Range >
381 return range.begin() == range.end();
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 >();
396 gridView.comm().max( nofParams.data(), nofParams.size() );
409 DUNE_THROW(
DGFException,
"Number of parameters differs between processes" );
414 bndId_.resize( indexSet.size( 1 ) );
422 std::swap(
elParam_[ indexSet.index( element ) ], dgfFactory.
parameter( element ) );
423 assert(
elParam_[ indexSet.index( element ) ].size() ==
static_cast< std::size_t
>(
nofElParam_ ) );
428 for(
unsigned int v = 0, n = element.subEntities(
dimension ); v < n; ++v )
430 const auto index = indexSet.subIndex( element, v,
dimension );
432 std::swap(
vtxParam_[ index ], dgfFactory.
parameter( element.template subEntity< dimension >( v ) ) );
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 );
455 std::vector<int>& boundaryIds,
456 std::vector<int>& elementIds )
460 const auto& gridView =
gridPtr_->leafGridView();
461 const auto& indexSet = gridView.indexSet();
468 gridView.comm().max( nofParams.data(), nofParams.size() );
480 if(!boundaryIds.empty() || !elementIds.empty() )
483 elParam_.resize( elementIds.size(), std::vector<double>(1) );
484 for(
const auto& entity : elements( gridView ))
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>
505 const auto gridView =
gridPtr_->levelGridView( 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 :
517 if ( gridView.indexSet().index( entity ) >=
vtxParam_.size() )
518 vtxParam_.resize( gridView.indexSet().index( entity ) );
519 return vtxParam_[ gridView.indexSet().index( entity ) ];
538 : gridPtr_( gridPtr ), idSet_( gridPtr->localIdSet() )
540 const auto gridView = gridPtr_->levelGridView( 0 );
541 const auto &indexSet = gridView.indexSet();
546 std::swap( gridPtr_.
elParam_[ indexSet.index( element ) ], elData_[ idSet_.id( element ) ] );
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 ) ];
570 std::swap( bndData.second, gridPtr_.
bndParam_[ intersection.boundarySegmentIndex() ] );
581 const auto gridView = gridPtr_->levelGridView( 0 );
582 const auto &indexSet = gridView.indexSet();
585 gridPtr_.
elParam_.resize( indexSet.size( 0 ) );
588 gridPtr_.
bndId_.resize( indexSet.size( 1 ) );
590 gridPtr_.
bndParam_.resize( gridPtr_->numBoundarySegments() );
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_ ) );
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 ) ] );
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;
623 std::swap( bndData.second, gridPtr_.
bndParam_[ intersection.boundarySegmentIndex() ] );
640 template<
class Entity >
643 std::size_t totalSize = 0;
648 assert( elData_[ idSet_.id( entity ) ].size() ==
static_cast< std::size_t
>( gridPtr_.
nofElParam_ ) );
649 for(
double &v : elData_[ idSet_.id( entity ) ] )
650 totalSize += 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 totalSize += dataSize( v );
662 const auto bndData = bndData_.find( idSet_.id( entity ) );
663 if( bndData != bndData_.end() )
664 totalSize += dataSize( bndData->second.first ) + dataSize( bndData->second.second );
670 template<
class Buffer,
class Entity >
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 >
705 auto &p = elData_[ idSet_.id( entity ) ];
708 read( buffer, v, n );
713 auto &p = vtxData_[ idSet_.id( entity ) ];
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 & )
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_;
Describes the parallel communication interface class for MessageBuffers and DataHandles.
@ ForwardCommunication
communicate as given in InterfaceType
Definition: gridenums.hh:169
@ InteriorBorder_All_Interface
send interior and border, receive all entities
Definition: gridenums.hh:86
const IndexSet & indexSet() const
obtain the index set
Definition: common/gridview.hh:175
Include standard header files.
Definition: agrid.hh:58
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
Definition: dgfgridfactory.hh:36
const DGFBoundaryParameter::type & boundaryParameter(const Intersection< GG, II > &intersection) const
Definition: dgfgridfactory.hh:156
int boundaryId(const Intersection &intersection) const
Definition: dgfgridfactory.hh:100
std::vector< double > & parameter(const Element &element)
Definition: dgfgridfactory.hh:122
Grid * grid()
Definition: dgfgridfactory.hh:88
bool haveBoundaryParameters() const
Definition: dgfgridfactory.hh:149
Intersection of a mesh entity of codimension 0 ("element") with a "neighboring" element or with the d...
Definition: common/intersection.hh:162
size_t boundarySegmentIndex() const
index of the boundary segment within the macro grid
Definition: common/intersection.hh:234
CommDataHandleIF describes the features of a data handle for communication in parallel runs using the...
Definition: datahandleif.hh:76
Wrapper class for entities.
Definition: common/entity.hh:64
@ codimension
Know your own codimension.
Definition: common/entity.hh:105
Base class for exceptions in Dune grid modules.
Definition: exceptions.hh:18
virtual unsigned int insertionIndex([[maybe_unused]] const typename Codim< 0 >::Entity &entity) const
obtain an element's insertion index
Definition: common/gridfactory.hh:218
Provide a generic factory class for unstructured grids.
Definition: common/gridfactory.hh:312
virtual std::unique_ptr< GridType > createGrid()
Finalize grid creation and hand over the grid.
Definition: common/gridfactory.hh:370
Grid view abstract base class.
Definition: common/gridview.hh:63
exception class for IO errors in the DGF parser
Definition: dgfexception.hh:14
Class for constructing grids from DGF files.
Definition: gridptr.hh:64
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 & operator=(const GridPtr &org)
assignment of grid pointer
Definition: gridptr.hh:244
GridPtr(std::istream &input, MPICommunicatorType comm=MPIHelper::getCommunicator())
constructor given a std::istream
Definition: gridptr.hh:197
GridType * operator->()
return pointer to GridType instance
Definition: gridptr.hh:281
GridPtr()
Default constructor, creating empty GridPtr.
Definition: gridptr.hh:215
std::vector< double > & params(const Entity &entity)
Definition: gridptr.hh:503
bool haveBndParam_
Definition: gridptr.hh:801
static const int dimension
Definition: gridptr.hh:153
std::vector< int > bndId_
Definition: gridptr.hh:796
void setNofParams(int cdim, int nofP)
Definition: gridptr.hh:526
void loadBalance()
Definition: gridptr.hh:367
std::vector< DGFBoundaryParameter::type > bndParam_
Definition: gridptr.hh:795
void initialize(GridFactory< GridType > &factory, std::vector< int > &boundaryIds, std::vector< int > &elementIds)
Definition: gridptr.hh:454
GridPtr(GridType *grd)
Constructor storing given pointer to internal auto pointer.
Definition: gridptr.hh:228
int nofElParam_
Definition: gridptr.hh:799
GridType * release()
release pointer from internal ownership
Definition: gridptr.hh:296
const GridType * operator->() const
return const pointer to GridType instance
Definition: gridptr.hh:291
static bool isEmpty(Range &&range)
Definition: gridptr.hh:379
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
std::vector< std::vector< double > > elParam_
Definition: gridptr.hh:793
MPIHelper::MPICommunicator MPICommunicatorType
Definition: gridptr.hh:152
const GridType & operator*() const
return const reference to GridType instance
Definition: gridptr.hh:286
int nofParameters(int cdim) const
get number of parameters defined for a given codimension
Definition: gridptr.hh:299
const DGFBoundaryParameter::type & parameters(const Intersection< GridImp, IntersectionImp > &intersection) const
get parameters for intersection
Definition: gridptr.hh:349
int nofParameters(const Intersection< GridImp, IntersectionImp > &intersection) const
get number of parameters defined for a given intersection
Definition: gridptr.hh:316
std::vector< double > emptyParam_
Definition: gridptr.hh:797
mygrid_ptr gridPtr_
Definition: gridptr.hh:791
void readGmsh(const std::string &filename, std::integral_constant< bool, false >)
Definition: gridptr.hh:145
void readGmsh(const std::string &filename, std::integral_constant< bool, true >)
Definition: gridptr.hh:135
void initialize(DGFGridFactory< GridType > &dgfFactory)
Definition: gridptr.hh:384
GridPtr(const GridPtr &org)=default
Copy constructor, copies internal auto pointer.
std::string getFileExtension(const std::string &filename) const
Definition: gridptr.hh:120
void communicate()
Definition: gridptr.hh:358
GridType & operator*()
return reference to GridType instance
Definition: gridptr.hh:276
std::vector< std::vector< double > > vtxParam_
Definition: gridptr.hh:794
int nofVtxParam_
Definition: gridptr.hh:800
Definition: gridptr.hh:66
mygrid_ptr(GridType *grd)
Definition: gridptr.hh:97
mygrid_ptr & operator=(const mygrid_ptr &other)
Definition: gridptr.hh:103
~mygrid_ptr()
Definition: gridptr.hh:100
GridType * release()
Definition: gridptr.hh:110
mygrid_ptr(const mygrid_ptr &other)
Definition: gridptr.hh:95
mygrid_ptr()
Definition: gridptr.hh:93
Definition: gridptr.hh:536
DataHandle(const DataHandle &)=delete
~DataHandle()
Definition: gridptr.hh:579
DataHandle(GridPtr &gridPtr)
Definition: gridptr.hh:537
void gather(Buffer &buffer, const Entity &entity) const
Definition: gridptr.hh:671
bool fixedSize(int, int) const
Definition: gridptr.hh:638
std::size_t size(const Entity &entity) const
Definition: gridptr.hh:641
DataHandle(DataHandle &&)=delete
void scatter(Buffer &buffer, const Entity &entity, std::size_t n)
Definition: gridptr.hh:700
bool contains(int dim, int codim) const
Definition: gridptr.hh:631
CommDataHandleIF< DataHandle, char > & interface()
Definition: gridptr.hh:629
static const type & defaultValue()
default constructor
Definition: parser.hh:26
std::string type
type of additional boundary parameters
Definition: parser.hh:23
static std::unique_ptr< Grid > read(const std::string &fileName, bool verbose=true, bool insertBoundarySegments=true)
Definition: gmshreader.hh:869