3#ifndef DUNE_DGF_GRIDPTR_HH
4#define DUNE_DGF_GRIDPTR_HH
17#include <dune/grid/common/gridenums.hh>
19#include <dune/grid/common/intersection.hh>
21#include <dune/grid/io/file/dgfparser/dgfexception.hh>
22#include <dune/grid/io/file/dgfparser/entitykey.hh>
23#include <dune/grid/io/file/dgfparser/parser.hh>
32 struct DGFGridFactory;
34 template<
class Gr
idImp,
class IntersectionImp >
54 template<
class Gr
idType >
57 class mygrid_ptr :
public std::shared_ptr< GridType >
59 typedef std::shared_ptr< GridType > base_t ;
66 if( use_count() == 1 )
70 if( grd )
delete grd ;
74 void assignObj(
const mygrid_ptr& other )
77 base_t :: operator = ( other );
81 using base_t :: swap ;
82 using base_t :: use_count ;
85 mygrid_ptr() : base_t( ( GridType * ) 0, emptydeleter_t() ) {}
87 mygrid_ptr(
const mygrid_ptr& other ) { assignObj( other ); }
89 explicit mygrid_ptr( GridType* grd ) : base_t( grd, emptydeleter_t() ) {}
92 ~mygrid_ptr() { removeObj(); }
95 mygrid_ptr&
operator = (
const mygrid_ptr& other )
104 GridType* grd = this->get();
105 base_t ptr(( GridType * ) 0, emptydeleter_t() );
112 static const int dimension = GridType::dimension;
115 explicit GridPtr (
const std::string &filename,
125 haveBndParam_( false )
127 DGFGridFactory< GridType > dgfFactory( filename, comm );
128 initialize( dgfFactory );
142 haveBndParam_( false )
144 DGFGridFactory< GridType > dgfFactory( input, comm );
145 initialize( dgfFactory );
158 haveBndParam_( false )
171 haveBndParam_( false )
176 : gridPtr_(org.gridPtr_),
177 elParam_(org.elParam_),
178 vtxParam_(org.vtxParam_),
179 bndParam_(org.bndParam_),
181 emptyParam_( org.emptyParam_ ),
182 nofElParam_(org.nofElParam_),
183 nofVtxParam_(org.nofVtxParam_),
184 haveBndParam_(org.haveBndParam_)
190 gridPtr_ = org.gridPtr_;
191 elParam_ = org.elParam_;
192 vtxParam_ = org.vtxParam_;
193 bndParam_ = org.bndParam_;
195 emptyParam_ = org.emptyParam_;
197 nofElParam_ = org.nofElParam_;
198 nofVtxParam_ = org.nofVtxParam_;
199 haveBndParam_ = org.haveBndParam_;
206 gridPtr_ = mygrid_ptr( grd );
211 emptyParam_.resize(0);
215 haveBndParam_ =
false;
226 return gridPtr_.operator -> ();
236 return gridPtr_.operator -> ();
240 GridType*
release () {
return gridPtr_.release(); }
245 case 0 :
return nofElParam_;
break;
246 case GridType::dimension :
return nofVtxParam_;
break;
252 template <
class Entity>
259 template<
class Gr
idImp,
class IntersectionImp >
266 template <
class Entity>
269 typedef typename GridType::LevelGridView
GridView;
270 GridView gridView = gridPtr_->levelGridView( 0 );
274 if( nofElParam_ > 0 )
276 assert( (
unsigned int)gridView.
indexSet().index( entity ) < elParam_.size() );
277 return elParam_[ gridView.
indexSet().index( entity ) ];
280 case GridType::dimension :
281 if( nofVtxParam_ > 0 )
283 assert( (
unsigned int)gridView.
indexSet().index( entity ) < vtxParam_.size() );
284 return vtxParam_[ gridView.
indexSet().index( entity ) ];
292 template<
class Gr
idImp,
class IntersectionImp >
296 if ( !haveBndParam_ )
304 if ( gridPtr_->comm().size() == 1 )
306 int nofParams = nofElParam_ + nofVtxParam_;
309 if ( gridPtr_->comm().max( nofParams ) > 0 )
311 DataHandle dh(*
this);
312 gridPtr_->loadBalance( dh.interface() );
316 gridPtr_->loadBalance();
321 void initialize ( DGFGridFactory< GridType > &dgfFactory )
323 gridPtr_ = mygrid_ptr( dgfFactory.grid() );
325 typedef typename GridType::LevelGridView GridView;
326 GridView gridView = gridPtr_->levelGridView( 0 );
329 nofElParam_ = dgfFactory.template numParameters< 0 >();
330 nofVtxParam_ = dgfFactory.template numParameters< dimension >();
331 haveBndParam_ = dgfFactory.haveBoundaryParameters();
333 if ( nofElParam_ > 0 )
334 elParam_.resize( indexSet.size(0) );
335 if ( nofVtxParam_ > 0 )
336 vtxParam_.resize( indexSet.size(dimension) );
337 bndId_.resize( indexSet.size(1) );
339 bndParam_.resize( gridPtr_->numBoundarySegments() );
342 typedef typename GridView::template Codim< 0 >::template Partition< partType >::Iterator Iterator;
343 const Iterator enditer = gridView.template end< 0, partType >();
344 for( Iterator iter = gridView.template begin< 0, partType >(); iter != enditer; ++iter )
346 const typename Iterator::Entity &el = *iter;
347 if ( nofElParam_ > 0 ) {
348 std::swap( elParam_[ indexSet.index(el) ], dgfFactory.parameter(el) );
349 assert( elParam_[ indexSet.index(el) ].size() == (
size_t)nofElParam_ );
351 if ( nofVtxParam_ > 0 )
353 const unsigned int subEntities = el.subEntities(dimension);
354 for (
unsigned int v = 0; v < subEntities; ++v)
356 typename GridView::IndexSet::IndexType index = indexSet.subIndex(el,v,dimension);
357 if ( vtxParam_[ index ].empty() )
358 std::swap( vtxParam_[ index ], dgfFactory.parameter(el.template subEntity<dimension>(v) ) );
359 assert( vtxParam_[ index ].size() == (
size_t)nofVtxParam_ );
362 if ( el.hasBoundaryIntersections() )
365 const IntersectionIterator iend = gridView.iend(el);
366 for( IntersectionIterator iiter = gridView.ibegin(el); iiter != iend; ++iiter )
370 if ( inter.boundary() && !inter.type().isNone() )
372 const int k = indexSet.subIndex(el,inter.indexInInside(),1);
373 bndId_[ k ] = dgfFactory.boundaryId( inter );
375 bndParam_[ inter.boundarySegmentIndex() ] = dgfFactory.boundaryParameter( inter );
382 template <
class Entity>
383 std::vector< double > ¶ms (
const Entity &entity )
385 typedef typename GridType::LevelGridView GridView;
386 GridView gridView = gridPtr_->levelGridView( 0 );
390 if( nofElParam_ > 0 ) {
391 if ( gridView.indexSet().index( entity ) >= elParam_.size() )
392 elParam_.resize( gridView.indexSet().index( entity ) );
393 return elParam_[ gridView.indexSet().index( entity ) ];
396 case GridType::dimension :
397 if( nofVtxParam_ > 0 ) {
398 if ( gridView.indexSet().index( entity ) >= vtxParam_.size() )
399 vtxParam_.resize( gridView.indexSet().index( entity ) );
400 return vtxParam_[ gridView.indexSet().index( entity ) ];
407 void setNofParams(
int cdim,
int nofP )
410 case 0 : nofElParam_ = nofP;
break;
411 case GridType::dimension : nofVtxParam_ = nofP;
break;
415 struct DataHandle :
public CommDataHandleIF<DataHandle,double>
417 DataHandle(
GridPtr& gridPtr) :
419 idSet_(gridPtr->localIdSet())
421 typedef typename GridType::LevelGridView GridView;
422 GridView gridView = gridPtr_->levelGridView( 0 );
426 typedef typename GridView::template Codim< 0 >::template Partition< partType >::Iterator Iterator;
427 const Iterator enditer = gridView.template end< 0, partType >();
428 for( Iterator iter = gridView.template begin< 0, partType >(); iter != enditer; ++iter )
430 const typename Iterator::Entity &el = *iter;
431 if ( gridPtr_.nofElParam_ > 0 )
432 std::swap( gridPtr_.elParam_[ indexSet.index(el) ], elData_[ idSet_.id(el) ] );
433 if ( gridPtr_.nofVtxParam_ > 0 )
435 for (
unsigned int v = 0; v < el.subEntities(dimension); ++v)
437 typename GridView::IndexSet::IndexType index = indexSet.subIndex(el,v,dimension);
438 if ( ! gridPtr_.vtxParam_[ index ].empty() )
439 std::swap( gridPtr_.vtxParam_[ index ], vtxData_[ idSet_.subId(el,v,dimension) ] );
447 typedef typename GridType::LevelGridView GridView;
448 GridView gridView = gridPtr_->levelGridView( 0 );
451 if ( gridPtr_.nofElParam_ > 0 )
452 gridPtr_.elParam_.resize( indexSet.size(0) );
453 if ( gridPtr_.nofVtxParam_ > 0 )
454 gridPtr_.vtxParam_.resize( indexSet.size(dimension) );
457 typedef typename GridView::template Codim< 0 >::template Partition< partType >::Iterator Iterator;
458 const Iterator enditer = gridView.template end< 0, partType >();
459 for( Iterator iter = gridView.template begin< 0, partType >(); iter != enditer; ++iter )
461 const typename Iterator::Entity &el = *iter;
462 if ( gridPtr_.nofElParam_ > 0 )
464 std::swap( gridPtr_.elParam_[ indexSet.index(el) ], elData_[ idSet_.id(el) ] );
465 assert( gridPtr_.elParam_[ indexSet.index(el) ].size() == (
unsigned int)gridPtr_.nofElParam_ );
467 if ( gridPtr_.nofVtxParam_ > 0 )
469 for (
unsigned int v = 0; v < el.subEntities(dimension); ++v)
471 typename GridView::IndexSet::IndexType index = indexSet.subIndex(el,v,dimension);
472 if ( gridPtr_.vtxParam_[ index ].empty() )
473 std::swap( gridPtr_.vtxParam_[ index ], vtxData_[ idSet_.subId(el,v,dimension) ] );
474 assert( gridPtr_.vtxParam_[ index ].size() == (
unsigned int)gridPtr_.nofVtxParam_ );
480 CommDataHandleIF<DataHandle,double> &interface()
485 bool contains (
int dim,
int codim)
const
487 return (codim==dim || codim==0);
490 bool fixedSize (
int dim,
int codim)
const
495 template<
class EntityType>
496 size_t size (
const EntityType& e)
const
498 return gridPtr_.nofParameters( (
int) e.codimension);
501 template<
class MessageBufferImp,
class EntityType>
502 void gather (MessageBufferImp& buff,
const EntityType& e)
const
504 const std::vector<double> &v = (e.codimension==0) ? elData_[idSet_.id(e)] : vtxData_[idSet_.id(e)];
505 const size_t s = v.size();
506 for (
size_t i=0; i<s; ++i)
508 assert( s == (
size_t)gridPtr_.nofParameters(e.codimension) );
511 template<
class MessageBufferImp,
class EntityType>
512 void scatter (MessageBufferImp& buff,
const EntityType& e,
size_t n)
514 std::vector<double> &v = (e.codimension==0) ? elData_[idSet_.id(e)] : vtxData_[idSet_.id(e)];
516 gridPtr_.setNofParams( e.codimension, n );
517 for (
size_t i=0; i<n; ++i)
522 typedef typename GridType::LocalIdSet IdSet;
525 mutable std::map< typename IdSet::IdType, std::vector<double> > elData_, vtxData_;
529 mutable mygrid_ptr gridPtr_;
531 std::vector< std::vector< double > > elParam_;
532 std::vector< std::vector< double > > vtxParam_;
533 std::vector< DGFBoundaryParameter::type > bndParam_;
534 std::vector< int > bndId_;
535 std::vector < double > emptyParam_;
Wrapper class for entities.
Definition: entity.hh:65
@ codimension
Know your own codimension.
Definition: entity.hh:107
Grid view abstract base class.
Definition: gridview.hh:60
Dune::Intersection< GridImp, IntersectionImp > Intersection
Type of Intersection this IntersectionIterator points to.
Definition: intersectioniterator.hh:105
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:264
MPI_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:173
static MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:181
Describes the parallel communication interface class for MessageBuffers and DataHandles.
const IndexSet & indexSet() const
obtain the index set
Definition: gridview.hh:173
Traits::IntersectionIterator IntersectionIterator
type of the intersection iterator
Definition: gridview.hh:87
Traits::IndexSet IndexSet
type of the index set
Definition: gridview.hh:81
Helpers for dealing with MPI.
Dune namespace.
Definition: alignment.hh:11
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:56
GridPtr(std::istream &input, MPICommunicatorType comm=MPIHelper::getCommunicator())
constructor given a std::istream
Definition: gridptr.hh:132
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:267
GridPtr(const GridPtr &org)
Copy constructor, copies internal auto pointer.
Definition: gridptr.hh:175
GridPtr()
Default constructor, creating empty GridPtr.
Definition: gridptr.hh:149
const GridType & operator*() const
return const reference to GridType instance
Definition: gridptr.hh:230
GridPtr(GridType *grd)
Constructor storing given pointer to internal auto pointer.
Definition: gridptr.hh:162
const DGFBoundaryParameter::type & parameters(const Intersection< GridImp, IntersectionImp > &intersection) const
get parameters for intersection
Definition: gridptr.hh:293
GridType & operator*()
return reference to GridType instance
Definition: gridptr.hh:220
int nofParameters(const Entity &) const
get parameters defined for given entity
Definition: gridptr.hh:253
GridPtr(const std::string &filename, MPICommunicatorType comm=MPIHelper::getCommunicator())
constructor given the name of a DGF file
Definition: gridptr.hh:115
GridPtr & operator=(const GridPtr &org)
assignment of grid pointer
Definition: gridptr.hh:188
int nofParameters(int cdim) const
get number of parameters defined for a given codimension
Definition: gridptr.hh:243
int nofParameters(const Intersection< GridImp, IntersectionImp > &intersection) const
get number of parameters defined for a given intersection
Definition: gridptr.hh:260
const GridType * operator->() const
return const pointer to GridType instance
Definition: gridptr.hh:235
GridType * release()
release pointer from internal ownership
Definition: gridptr.hh:240
GridType * operator->()
return pointer to GridType instance
Definition: gridptr.hh:225
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:238