5#ifndef DUNE_DGF_GRIDPTR_HH
6#define DUNE_DGF_GRIDPTR_HH
20#include <dune/common/parallel/mpihelper.hh>
21#include <dune/common/shared_ptr.hh>
42 struct DGFGridFactory;
44 template<
class Gr
idImp,
class IntersectionImp >
64 template<
class Gr
idType >
69 typedef std::shared_ptr< GridType > base_t ;
71 typedef null_deleter< GridType > emptydeleter_t ;
76 if( use_count() == 1 )
80 if( grd )
delete grd ;
87 base_t :: operator = ( other );
92 using base_t :: use_count ;
95 mygrid_ptr() : base_t( ( GridType * ) 0, emptydeleter_t() ) {}
99 explicit mygrid_ptr( GridType* grd ) : base_t( grd, emptydeleter_t() ) {}
114 GridType* grd = this->get();
115 base_t ptr(( GridType * ) 0, emptydeleter_t() );
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 > )
149 DUNE_THROW(
GridError,
"GmshReader requires dimWorld <= 3." );
158 explicit GridPtr (
const std::string &filename,
172 if( fileExt ==
"dgf" )
177 else if( fileExt ==
"msh" )
180 readGmsh( filename, std::integral_constant<
bool, GridType::dimensionworld <= 3 > () );
182 else if( fileExt ==
"amc" || fileExt ==
"2d" || fileExt ==
"3d" )
185 DUNE_THROW( NotImplemented,
"GridPtr: file format '" << fileExt <<
"' not supported yet!" );
187 else if( fileExt ==
"vtu" )
190 DUNE_THROW( NotImplemented,
"GridPtr: file format '" << fileExt <<
"' not supported yet!" );
194 DUNE_THROW( NotImplemented,
"GridPtr: file format '" << fileExt <<
"' not supported yet!" );
310 template <
class Entity>
317 template<
class Gr
idImp,
class IntersectionImp >
324 template <
class Entity>
327 typedef typename GridType::LevelGridView
GridView;
334 assert( (
unsigned int)gridView.
indexSet().index( entity ) <
elParam_.size() );
338 case GridType::dimension :
350 template<
class Gr
idImp,
class IntersectionImp >
380 template<
class Range >
383 return range.begin() == range.end();
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 >();
398 gridView.comm().max( nofParams.data(), nofParams.size() );
411 DUNE_THROW(
DGFException,
"Number of parameters differs between processes" );
416 bndId_.resize( indexSet.size( 1 ) );
425 assert(
elParam_[ indexSet.index( element ) ].size() ==
static_cast< std::size_t
>(
nofElParam_ ) );
430 for(
unsigned int v = 0, n = element.subEntities(
dimension ); v < n; ++v )
432 const auto index = indexSet.subIndex( element, v,
dimension );
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 );
457 std::vector<int>& boundaryIds,
458 std::vector<int>& elementIds )
462 const auto& gridView =
gridPtr_->leafGridView();
463 const auto& indexSet = gridView.indexSet();
470 gridView.comm().max( nofParams.data(), nofParams.size() );
482 if(!boundaryIds.empty() || !elementIds.empty() )
485 elParam_.resize( elementIds.size(), std::vector<double>(1) );
486 for(
const auto& entity : elements( gridView ))
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>
507 const auto gridView =
gridPtr_->levelGridView( 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 :
519 if ( gridView.indexSet().index( entity ) >=
vtxParam_.size() )
520 vtxParam_.resize( gridView.indexSet().index( entity ) );
521 return vtxParam_[ gridView.indexSet().index( entity ) ];
540 : gridPtr_( gridPtr ), idSet_( gridPtr->localIdSet() )
542 const auto gridView = gridPtr_->levelGridView( 0 );
543 const auto &indexSet = gridView.indexSet();
548 std::swap( gridPtr_.
elParam_[ indexSet.index( element ) ], elData_[ idSet_.id( element ) ] );
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() )
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 ) ];
572 std::swap( bndData.second, gridPtr_.
bndParam_[ intersection.boundarySegmentIndex() ] );
583 const auto gridView = gridPtr_->levelGridView( 0 );
584 const auto &indexSet = gridView.indexSet();
587 gridPtr_.
elParam_.resize( indexSet.size( 0 ) );
590 gridPtr_.
bndId_.resize( indexSet.size( 1 ) );
592 gridPtr_.
bndParam_.resize( gridPtr_->numBoundarySegments() );
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_ ) );
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() )
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;
625 std::swap( bndData.second, gridPtr_.
bndParam_[ intersection.boundarySegmentIndex() ] );
642 template<
class Entity >
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 >
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 >
707 auto &p = elData_[ idSet_.id( entity ) ];
710 read( buffer, v, n );
715 auto &p = vtxData_[ idSet_.id( entity ) ];
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_;
Describes the parallel communication interface class for MessageBuffers and DataHandles.
@ ForwardCommunication
communicate as given in InterfaceType
Definition: gridenums.hh:171
@ InteriorBorder_All_Interface
send interior and border, receive all entities
Definition: gridenums.hh:88
const IndexSet & indexSet() const
obtain the index set
Definition: common/gridview.hh:191
void swap(Dune::PersistentContainer< G, T > &a, Dune::PersistentContainer< G, T > &b)
Definition: utility/persistentcontainer.hh:83
Include standard header files.
Definition: agrid.hh:60
constexpr All all
PartitionSet for all partitions.
Definition: partitionset.hh:296
constexpr InteriorBorder interiorBorder
PartitionSet for the interior and border partitions.
Definition: partitionset.hh:287
Definition: dgfgridfactory.hh:38
int boundaryId(const Intersection &intersection) const
Definition: dgfgridfactory.hh:102
const DGFBoundaryParameter::type & boundaryParameter(const Intersection< GG, II > &intersection) const
Definition: dgfgridfactory.hh:158
Grid * grid()
Definition: dgfgridfactory.hh:90
std::vector< double > & parameter(const Element &element)
Definition: dgfgridfactory.hh:124
bool haveBoundaryParameters() const
Definition: dgfgridfactory.hh:151
Intersection of a mesh entity of codimension 0 ("element") with a "neighboring" element or with the d...
Definition: common/intersection.hh:164
size_t boundarySegmentIndex() const
index of the boundary segment within the macro grid
Definition: common/intersection.hh:236
CommDataHandleIF describes the features of a data handle for communication in parallel runs using the...
Definition: datahandleif.hh:78
Wrapper class for entities.
Definition: common/entity.hh:66
static constexpr int codimension
Know your own codimension.
Definition: common/entity.hh:106
Base class for exceptions in Dune grid modules.
Definition: exceptions.hh:20
virtual unsigned int insertionIndex(const typename Codim< 0 >::Entity &entity) const
obtain an element's insertion index
Definition: common/gridfactory.hh:220
Provide a generic factory class for unstructured grids.
Definition: common/gridfactory.hh:314
virtual std::unique_ptr< GridType > createGrid()
Finalize grid creation and hand over the grid.
Definition: common/gridfactory.hh:372
Grid view abstract base class.
Definition: common/gridview.hh:66
exception class for IO errors in the DGF parser
Definition: dgfexception.hh:16
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
bool haveBndParam_
Definition: gridptr.hh:803
static const int dimension
Definition: gridptr.hh:155
std::vector< int > bndId_
Definition: gridptr.hh:798
void setNofParams(int cdim, int nofP)
Definition: gridptr.hh:528
void loadBalance()
Definition: gridptr.hh:369
std::vector< DGFBoundaryParameter::type > bndParam_
Definition: gridptr.hh:797
void initialize(GridFactory< GridType > &factory, std::vector< int > &boundaryIds, std::vector< int > &elementIds)
Definition: gridptr.hh:456
GridPtr(GridType *grd)
Constructor storing given pointer to internal auto pointer.
Definition: gridptr.hh:230
int nofElParam_
Definition: gridptr.hh:801
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
static bool isEmpty(Range &&range)
Definition: gridptr.hh:381
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
std::vector< std::vector< double > > elParam_
Definition: gridptr.hh:795
MPIHelper::MPICommunicator MPICommunicatorType
Definition: gridptr.hh:154
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
std::vector< double > emptyParam_
Definition: gridptr.hh:799
mygrid_ptr gridPtr_
Definition: gridptr.hh:793
void readGmsh(const std::string &filename, std::integral_constant< bool, false >)
Definition: gridptr.hh:147
void readGmsh(const std::string &filename, std::integral_constant< bool, true >)
Definition: gridptr.hh:137
std::vector< double > & params(const Entity &entity)
Definition: gridptr.hh:505
void initialize(DGFGridFactory< GridType > &dgfFactory)
Definition: gridptr.hh:386
GridPtr(const GridPtr &org)=default
Copy constructor, copies internal auto pointer.
std::string getFileExtension(const std::string &filename) const
Definition: gridptr.hh:122
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
void communicate()
Definition: gridptr.hh:360
std::vector< std::vector< double > > vtxParam_
Definition: gridptr.hh:796
GridType * operator->()
return pointer to GridType instance
Definition: gridptr.hh:283
int nofVtxParam_
Definition: gridptr.hh:802
Definition: gridptr.hh:68
mygrid_ptr(GridType *grd)
Definition: gridptr.hh:99
~mygrid_ptr()
Definition: gridptr.hh:102
GridType * release()
Definition: gridptr.hh:112
mygrid_ptr(const mygrid_ptr &other)
Definition: gridptr.hh:97
mygrid_ptr & operator=(const mygrid_ptr &other)
Definition: gridptr.hh:105
mygrid_ptr()
Definition: gridptr.hh:95
Definition: gridptr.hh:538
CommDataHandleIF< DataHandle, char > & interface()
Definition: gridptr.hh:631
DataHandle(const DataHandle &)=delete
~DataHandle()
Definition: gridptr.hh:581
DataHandle(GridPtr &gridPtr)
Definition: gridptr.hh:539
void gather(Buffer &buffer, const Entity &entity) const
Definition: gridptr.hh:673
bool fixedSize(int, int) const
Definition: gridptr.hh:640
std::size_t size(const Entity &entity) const
Definition: gridptr.hh:643
DataHandle(DataHandle &&)=delete
void scatter(Buffer &buffer, const Entity &entity, std::size_t n)
Definition: gridptr.hh:702
bool contains(int dim, int codim) const
Definition: gridptr.hh:633
static const type & defaultValue()
default constructor
Definition: parser.hh:28
std::string type
type of additional boundary parameters
Definition: parser.hh:25
static std::unique_ptr< Grid > read(const std::string &fileName, bool verbose=true, bool insertBoundarySegments=true)
Definition: gmshreader.hh:902