Dune Core Modules (2.3.1)

gridptr.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_DGF_GRIDPTR_HH
4#define DUNE_DGF_GRIDPTR_HH
5
6#include <iostream>
7#include <string>
8#include <vector>
9//#include <memory>
10#include <map>
11#include <assert.h>
12
13//- Dune includes
16#include <dune/grid/common/gridenums.hh>
18
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>
22
23#include <dune/grid/common/intersection.hh>
24
25namespace Dune
26{
27
28 // External Forward Declarations
29 // -----------------------------
30
31 template < class G >
32 struct DGFGridFactory;
33
34 template< class GridImp, class IntersectionImp >
35 class Intersection;
36
37
38
39 // GridPtr
40 // -------
41
54 template< class GridType >
55 struct GridPtr
56 {
57 class mygrid_ptr : public shared_ptr< GridType >
58 {
59 typedef shared_ptr< GridType > base_t ;
60 // empty deleter to avoid deletion on release
61 typedef null_deleter< GridType > emptydeleter_t ;
62
63 void removeObj()
64 {
65 // if use count is only 1 delete object
66 if( use_count() == 1 )
67 {
68 // delete point here, since we use the empty deleter
69 GridType* grd = release();
70 if( grd ) delete grd ;
71 }
72 }
73
74 void assignObj( const mygrid_ptr& other )
75 {
76 removeObj();
77 base_t :: operator = ( other );
78 }
79 public:
80 using base_t :: get ;
81 using base_t :: swap ;
82 using base_t :: use_count ;
83
84 // default constructor
85 mygrid_ptr() : base_t( ( GridType * ) 0, emptydeleter_t() ) {}
86 // copy constructor
87 mygrid_ptr( const mygrid_ptr& other ) { assignObj( other ); }
88 // constructor taking pointer
89 explicit mygrid_ptr( GridType* grd ) : base_t( grd, emptydeleter_t() ) {}
90
91 // destructor
92 ~mygrid_ptr() { removeObj(); }
93
94 // assigment operator
95 mygrid_ptr& operator = ( const mygrid_ptr& other )
96 {
97 assignObj( other );
98 return *this;
99 }
100
101 // release pointer
102 GridType* release()
103 {
104 GridType* grd = this->get();
105 base_t ptr(( GridType * ) 0, emptydeleter_t() );
106 this->swap( ptr );
107 return grd ;
108 }
109 };
110
111 typedef MPIHelper::MPICommunicator MPICommunicatorType;
112 static const int dimension = GridType::dimension;
113
115 explicit GridPtr ( const std::string &filename,
116 MPICommunicatorType comm = MPIHelper::getCommunicator() )
117 : gridPtr_(),
118 elParam_(),
119 vtxParam_(),
120 bndParam_(),
121 bndId_(),
122 emptyParam_(),
123 nofElParam_( 0 ),
124 nofVtxParam_( 0 ),
125 haveBndParam_( false )
126 {
127 DGFGridFactory< GridType > dgfFactory( filename, comm );
128 initialize( dgfFactory );
129 }
130
132 explicit GridPtr ( std::istream &input,
133 MPICommunicatorType comm = MPIHelper::getCommunicator() )
134 : gridPtr_(),
135 elParam_(),
136 vtxParam_(),
137 bndParam_(),
138 bndId_(),
139 emptyParam_(),
140 nofElParam_( 0 ),
141 nofVtxParam_( 0 ),
142 haveBndParam_( false )
143 {
144 DGFGridFactory< GridType > dgfFactory( input, comm );
145 initialize( dgfFactory );
146 }
147
150 : gridPtr_(),
151 elParam_(),
152 vtxParam_(),
153 bndParam_(),
154 bndId_(),
155 emptyParam_(),
156 nofElParam_(0),
157 nofVtxParam_(0),
158 haveBndParam_( false )
159 {}
160
162 explicit GridPtr( GridType *grd )
163 : gridPtr_(grd),
164 elParam_(),
165 vtxParam_(),
166 bndParam_(),
167 bndId_(),
168 emptyParam_(),
169 nofElParam_(0),
170 nofVtxParam_(0),
171 haveBndParam_( false )
172 {}
173
175 GridPtr( const GridPtr &org )
176 : gridPtr_(org.gridPtr_),
177 elParam_(org.elParam_),
178 vtxParam_(org.vtxParam_),
179 bndParam_(org.bndParam_),
180 bndId_(org.bndId_),
181 emptyParam_( org.emptyParam_ ),
182 nofElParam_(org.nofElParam_),
183 nofVtxParam_(org.nofVtxParam_),
184 haveBndParam_(org.haveBndParam_)
185 {}
186
189 {
190 gridPtr_ = org.gridPtr_;
191 elParam_ = org.elParam_;
192 vtxParam_ = org.vtxParam_;
193 bndParam_ = org.bndParam_;
194 bndId_ = org.bndId_;
195 emptyParam_ = org.emptyParam_;
196
197 nofElParam_ = org.nofElParam_;
198 nofVtxParam_ = org.nofVtxParam_;
199 haveBndParam_ = org.haveBndParam_;
200 return *this;
201 }
202
204 GridPtr& operator = (GridType * grd)
205 {
206 gridPtr_ = mygrid_ptr( grd );
207 elParam_.resize(0);
208 vtxParam_.resize(0);
209 bndParam_.resize(0);
210 bndId_.resize(0);
211 emptyParam_.resize(0);
212
213 nofVtxParam_ = 0;
214 nofElParam_ = 0;
215 haveBndParam_ = false;
216 return *this;
217 }
218
220 GridType& operator*() {
221 return *gridPtr_;
222 }
223
225 GridType* operator->() {
226 return gridPtr_.operator -> ();
227 }
228
230 const GridType& operator*() const {
231 return *gridPtr_;
232 }
233
235 const GridType* operator->() const {
236 return gridPtr_.operator -> ();
237 }
238
240 GridType* release () { return gridPtr_.release(); }
241
243 int nofParameters(int cdim) const {
244 switch (cdim) {
245 case 0 : return nofElParam_; break;
246 case GridType::dimension : return nofVtxParam_; break;
247 }
248 return 0;
249 }
250
252 template <class Entity>
253 int nofParameters ( const Entity & ) const
254 {
255 return nofParameters( (int) Entity::codimension );
256 }
257
259 template< class GridImp, class IntersectionImp >
261 {
262 return parameters( intersection ).size();
263 }
264
266 template <class Entity>
267 const std::vector< double > &parameters ( const Entity &entity ) const
268 {
269 typedef typename GridType::LevelGridView GridView;
270 GridView gridView = gridPtr_->levelGridView( 0 );
271 switch( (int)Entity::codimension )
272 {
273 case 0 :
274 if( nofElParam_ > 0 )
275 {
276 assert( (unsigned int)gridView.indexSet().index( entity ) < elParam_.size() );
277 return elParam_[ gridView.indexSet().index( entity ) ];
278 }
279 break;
280 case GridType::dimension :
281 if( nofVtxParam_ > 0 )
282 {
283 assert( (unsigned int)gridView.indexSet().index( entity ) < vtxParam_.size() );
284 return vtxParam_[ gridView.indexSet().index( entity ) ];
285 }
286 break;
287 }
288 return emptyParam_;
289 }
290
292 template< class GridImp, class IntersectionImp >
294 {
295 // if no parameters given return empty vector
296 if ( !haveBndParam_ )
298
299 return bndParam_[ intersection.boundarySegmentIndex() ];
300 }
301
302 void loadBalance()
303 {
304 if ( gridPtr_->comm().size() == 1 )
305 return;
306 int params = nofElParam_ + nofVtxParam_;
307 if ( haveBndParam_ )
308 params += 1;
309 if ( gridPtr_->comm().max( params ) > 0 )
310 {
311 DataHandle dh(*this);
312 gridPtr_->loadBalance( dh.interface() );
313 gridPtr_->communicate( dh.interface(), InteriorBorder_All_Interface,ForwardCommunication);
314 } else
315 {
316 gridPtr_->loadBalance();
317 }
318 }
319
320 protected:
321 void initialize ( DGFGridFactory< GridType > &dgfFactory )
322 {
323 gridPtr_ = mygrid_ptr( dgfFactory.grid() );
324
325 typedef typename GridType::LevelGridView GridView;
326 GridView gridView = gridPtr_->levelGridView( 0 );
327 const typename GridView::IndexSet &indexSet = gridView.indexSet();
328
329 nofElParam_ = dgfFactory.template numParameters< 0 >();
330 nofVtxParam_ = dgfFactory.template numParameters< dimension >();
331 haveBndParam_ = dgfFactory.haveBoundaryParameters();
332
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) );
338 if ( haveBndParam_ )
339 bndParam_.resize( gridPtr_->numBoundarySegments() );
340
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 )
345 {
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_ );
350 }
351 if ( nofVtxParam_ > 0 )
352 {
353 for ( int v = 0; v < el.template count<dimension>(); ++v)
354 {
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_ );
359 }
360 }
361 if ( el.hasBoundaryIntersections() )
362 {
363 typedef typename GridView::IntersectionIterator IntersectionIterator;
364 const IntersectionIterator iend = gridView.iend(el);
365 for( IntersectionIterator iiter = gridView.ibegin(el); iiter != iend; ++iiter )
366 {
367 const typename IntersectionIterator::Intersection &inter = *iiter;
368 // dirty hack: check for "none" to make corner point grid work
369 if ( inter.boundary() && !inter.type().isNone() )
370 {
371 const int k = indexSet.subIndex(el,inter.indexInInside(),1);
372 bndId_[ k ] = dgfFactory.boundaryId( inter );
373 if ( haveBndParam_ )
374 bndParam_[ inter.boundarySegmentIndex() ] = dgfFactory.boundaryParameter( inter );
375 }
376 }
377 }
378 }
379 }
380
381 template <class Entity>
382 std::vector< double > &params ( const Entity &entity )
383 {
384 typedef typename GridType::LevelGridView GridView;
385 GridView gridView = gridPtr_->levelGridView( 0 );
386 switch( (int)Entity::codimension )
387 {
388 case 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 ) ];
393 }
394 break;
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 ) ];
400 }
401 break;
402 }
403 return emptyParam_;
404 }
405
406 void setNofParams( int cdim, int nofP )
407 {
408 switch (cdim) {
409 case 0 : nofElParam_ = nofP; break;
410 case GridType::dimension : nofVtxParam_ = nofP; break;
411 }
412 }
413
414 struct DataHandle : public CommDataHandleIF<DataHandle,double>
415 {
416 DataHandle( GridPtr& gridPtr) :
417 gridPtr_(gridPtr),
418 idSet_(gridPtr->localIdSet())
419 {
420 typedef typename GridType::LevelGridView GridView;
421 GridView gridView = gridPtr_->levelGridView( 0 );
422 const typename GridView::IndexSet &indexSet = gridView.indexSet();
423
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 )
428 {
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 )
433 {
434 for ( int v = 0; v < el.template count<dimension>(); ++v)
435 {
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) ] );
439 }
440 }
441 }
442 }
443
444 ~DataHandle()
445 {
446 typedef typename GridType::LevelGridView GridView;
447 GridView gridView = gridPtr_->levelGridView( 0 );
448 const typename GridView::IndexSet &indexSet = gridView.indexSet();
449
450 if ( gridPtr_.nofElParam_ > 0 )
451 gridPtr_.elParam_.resize( indexSet.size(0) );
452 if ( gridPtr_.nofVtxParam_ > 0 )
453 gridPtr_.vtxParam_.resize( indexSet.size(dimension) );
454
455 const PartitionIteratorType partType = All_Partition;
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 )
459 {
460 const typename Iterator::Entity &el = *iter;
461 if ( gridPtr_.nofElParam_ > 0 )
462 {
463 std::swap( gridPtr_.elParam_[ indexSet.index(el) ], elData_[ idSet_.id(el) ] );
464 assert( gridPtr_.elParam_[ indexSet.index(el) ].size() == (unsigned int)gridPtr_.nofElParam_ );
465 }
466 if ( gridPtr_.nofVtxParam_ > 0 )
467 {
468 for ( int v = 0; v < el.template count<dimension>(); ++v)
469 {
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_ );
474 }
475 }
476 }
477 }
478
479 CommDataHandleIF<DataHandle,double> &interface()
480 {
481 return *this;
482 }
483
484 bool contains (int dim, int codim) const
485 {
486 return (codim==dim || codim==0);
487 }
488
489 bool fixedsize (int dim, int codim) const
490 {
491 return false;
492 }
493
494 template<class EntityType>
495 size_t size (const EntityType& e) const
496 {
497 return gridPtr_.nofParameters( (int) e.codimension);
498 }
499
500 template<class MessageBufferImp, class EntityType>
501 void gather (MessageBufferImp& buff, const EntityType& e) const
502 {
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)
506 buff.write( v[i] );
507 assert( s == (size_t)gridPtr_.nofParameters(e.codimension) );
508 }
509
510 template<class MessageBufferImp, class EntityType>
511 void scatter (MessageBufferImp& buff, const EntityType& e, size_t n)
512 {
513 std::vector<double> &v = (e.codimension==0) ? elData_[idSet_.id(e)] : vtxData_[idSet_.id(e)];
514 v.resize( n );
515 gridPtr_.setNofParams( e.codimension, n );
516 for (size_t i=0; i<n; ++i)
517 buff.read( v[i] );
518 }
519
520 private:
521 typedef typename GridType::LocalIdSet IdSet;
522 GridPtr &gridPtr_;
523 const IdSet &idSet_;
524 mutable std::map< typename IdSet::IdType, std::vector<double> > elData_, vtxData_;
525 };
526
527 // grid auto pointer
528 mutable mygrid_ptr gridPtr_;
529 // element and vertex parameters
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_;
535
536 int nofElParam_;
537 int nofVtxParam_;
538 bool haveBndParam_;
539 }; // end of class GridPtr
540
541} // end namespace Dune
542
543#endif
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
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)