3#ifndef DUNE_DGF_GRIDPTR_HH
4#define DUNE_DGF_GRIDPTR_HH
16#include <dune/grid/common/gridenums.hh>
19#include <dune/grid/io/file/dgfparser/dgfexception.hh>
20#include <dune/grid/io/file/dgfparser/entitykey.hh>
21#include <dune/grid/io/file/dgfparser/parser.hh>
23#include <dune/grid/common/intersection.hh>
32 struct DGFGridFactory;
34 template<
class Gr
idImp,
class IntersectionImp >
54 template<
class Gr
idType >
57 class mygrid_ptr :
public shared_ptr< GridType >
66 if( use_count() == 1 )
70 if( grd )
delete grd ;
74 void assignObj(
const mygrid_ptr& 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 params = nofElParam_ + nofVtxParam_;
309 if ( gridPtr_->comm().max( params ) > 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 for (
int v = 0; v < el.template count<dimension>(); ++v)
355 typename GridView::IndexSet::IndexType index = indexSet.subIndex(el,v,dimension);
356 if ( vtxParam_[ index ].empty() )
357 std::swap( vtxParam_[ index ], dgfFactory.parameter(*el.template subEntity<dimension>(v) ) );
358 assert( vtxParam_[ index ].size() == (
size_t)nofVtxParam_ );
361 if ( el.hasBoundaryIntersections() )
364 const IntersectionIterator iend = gridView.iend(el);
365 for( IntersectionIterator iiter = gridView.ibegin(el); iiter != iend; ++iiter )
369 if ( inter.boundary() && !inter.type().isNone() )
371 const int k = indexSet.subIndex(el,inter.indexInInside(),1);
372 bndId_[ k ] = dgfFactory.boundaryId( inter );
374 bndParam_[ inter.boundarySegmentIndex() ] = dgfFactory.boundaryParameter( inter );
381 template <
class Entity>
382 std::vector< double > ¶ms (
const Entity &entity )
384 typedef typename GridType::LevelGridView GridView;
385 GridView gridView = gridPtr_->levelGridView( 0 );
389 if( nofElParam_ > 0 ) {
390 if ( gridView.indexSet().index( entity ) >= elParam_.size() )
391 elParam_.resize( gridView.indexSet().index( entity ) );
392 return elParam_[ gridView.indexSet().index( entity ) ];
395 case GridType::dimension :
396 if( nofVtxParam_ > 0 ) {
397 if ( gridView.indexSet().index( entity ) >= vtxParam_.size() )
398 vtxParam_.resize( gridView.indexSet().index( entity ) );
399 return vtxParam_[ gridView.indexSet().index( entity ) ];
406 void setNofParams(
int cdim,
int nofP )
409 case 0 : nofElParam_ = nofP;
break;
410 case GridType::dimension : nofVtxParam_ = nofP;
break;
414 struct DataHandle :
public CommDataHandleIF<DataHandle,double>
416 DataHandle(
GridPtr& gridPtr) :
418 idSet_(gridPtr->localIdSet())
420 typedef typename GridType::LevelGridView GridView;
421 GridView gridView = gridPtr_->levelGridView( 0 );
425 typedef typename GridView::template Codim< 0 >::template Partition< partType >::Iterator Iterator;
426 const Iterator enditer = gridView.template end< 0, partType >();
427 for( Iterator iter = gridView.template begin< 0, partType >(); iter != enditer; ++iter )
429 const typename Iterator::Entity &el = *iter;
430 if ( gridPtr_.nofElParam_ > 0 )
431 std::swap( gridPtr_.elParam_[ indexSet.index(el) ], elData_[ idSet_.id(el) ] );
432 if ( gridPtr_.nofVtxParam_ > 0 )
434 for (
int v = 0; v < el.template count<dimension>(); ++v)
436 typename GridView::IndexSet::IndexType index = indexSet.subIndex(el,v,dimension);
437 if ( ! gridPtr_.vtxParam_[ index ].empty() )
438 std::swap( gridPtr_.vtxParam_[ index ], vtxData_[ idSet_.subId(el,v,dimension) ] );
446 typedef typename GridType::LevelGridView GridView;
447 GridView gridView = gridPtr_->levelGridView( 0 );
450 if ( gridPtr_.nofElParam_ > 0 )
451 gridPtr_.elParam_.resize( indexSet.size(0) );
452 if ( gridPtr_.nofVtxParam_ > 0 )
453 gridPtr_.vtxParam_.resize( indexSet.size(dimension) );
456 typedef typename GridView::template Codim< 0 >::template Partition< partType >::Iterator Iterator;
457 const Iterator enditer = gridView.template end< 0, partType >();
458 for( Iterator iter = gridView.template begin< 0, partType >(); iter != enditer; ++iter )
460 const typename Iterator::Entity &el = *iter;
461 if ( gridPtr_.nofElParam_ > 0 )
463 std::swap( gridPtr_.elParam_[ indexSet.index(el) ], elData_[ idSet_.id(el) ] );
464 assert( gridPtr_.elParam_[ indexSet.index(el) ].size() == (
unsigned int)gridPtr_.nofElParam_ );
466 if ( gridPtr_.nofVtxParam_ > 0 )
468 for (
int v = 0; v < el.template count<dimension>(); ++v)
470 typename GridView::IndexSet::IndexType index = indexSet.subIndex(el,v,dimension);
471 if ( gridPtr_.vtxParam_[ index ].empty() )
472 std::swap( gridPtr_.vtxParam_[ index ], vtxData_[ idSet_.subId(el,v,dimension) ] );
473 assert( gridPtr_.vtxParam_[ index ].size() == (
unsigned int)gridPtr_.nofVtxParam_ );
479 CommDataHandleIF<DataHandle,double> &interface()
484 bool contains (
int dim,
int codim)
const
486 return (codim==dim || codim==0);
489 bool fixedsize (
int dim,
int codim)
const
494 template<
class EntityType>
495 size_t size (
const EntityType& e)
const
497 return gridPtr_.nofParameters( (
int) e.codimension);
500 template<
class MessageBufferImp,
class EntityType>
501 void gather (MessageBufferImp& buff,
const EntityType& e)
const
503 const std::vector<double> &v = (e.codimension==0) ? elData_[idSet_.id(e)] : vtxData_[idSet_.id(e)];
504 const size_t s = v.size();
505 for (
size_t i=0; i<s; ++i)
507 assert( s == (
size_t)gridPtr_.nofParameters(e.codimension) );
510 template<
class MessageBufferImp,
class EntityType>
511 void scatter (MessageBufferImp& buff,
const EntityType& e,
size_t n)
513 std::vector<double> &v = (e.codimension==0) ? elData_[idSet_.id(e)] : vtxData_[idSet_.id(e)];
515 gridPtr_.setNofParams( e.codimension, n );
516 for (
size_t i=0; i<n; ++i)
521 typedef typename GridType::LocalIdSet IdSet;
524 mutable std::map< typename IdSet::IdType, std::vector<double> > elData_, vtxData_;
528 mutable mygrid_ptr gridPtr_;
530 std::vector< std::vector< double > > elParam_;
531 std::vector< std::vector< double > > vtxParam_;
532 std::vector< DGFBoundaryParameter::type > bndParam_;
533 std::vector< int > bndId_;
534 std::vector < double > emptyParam_;
Wrapper class for entities.
Definition: entity.hh:57
@ codimension
Know your own codimension.
Definition: entity.hh:99
Grid view abstract base class.
Definition: gridview.hh:57
const IndexSet & indexSet() const
obtain the index set
Definition: gridview.hh:158
Traits::IntersectionIterator IntersectionIterator
type of the intersection iterator
Definition: gridview.hh:76
Traits::IndexSet IndexSet
type of the index set
Definition: gridview.hh:70
Dune::Intersection< GridImp, IntersectionImp > Intersection
Type of Intersection this IntersectionIterator points to.
Definition: intersectioniterator.hh:107
Intersection of a mesh entities of codimension 0 ("elements") with a "neighboring" element or with th...
Definition: intersection.hh:161
size_t boundarySegmentIndex() const
index of the boundary segment within the macro grid
Definition: intersection.hh:259
MPI_Comm MPICommunicator
The type of the mpi communicator.
Definition: mpihelper.hh:174
static MPICommunicator getCommunicator()
get the default communicator
Definition: mpihelper.hh:182
A reference counting smart pointer.
Definition: shared_ptr.hh:64
shared_ptr & operator=(const shared_ptr< T1 > &pointer)
Assignment operator.
Describes the parallel communication interface class for MessageBuffers and DataHandles.
Dune namespace.
Definition: alignment.hh:14
PartitionIteratorType
Parameter to be used for the parallel level- and leaf iterators.
Definition: gridenums.hh:130
@ All_Partition
all entities
Definition: gridenums.hh:135
@ Interior_Partition
only interior entities
Definition: gridenums.hh:131
@ ForwardCommunication
communicate as given in InterfaceType
Definition: gridenums.hh:165
@ InteriorBorder_All_Interface
send interior and border, receive all entities
Definition: gridenums.hh:82
Helpers for dealing with MPI.
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:489