dune-fem  2.4.1-rc
basesetlocalkeystorage.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_BASESETLOCALKEYSTORAGE_HH
2 #define DUNE_FEM_BASESETLOCALKEYSTORAGE_HH
3 
4 #include <utility>
5 
6 //- dune-common includes
7 #include <dune/common/exceptions.hh>
8 
9 //- dune-geometry includes
10 #include <dune/geometry/type.hh>
11 #include <dune/geometry/typeindex.hh>
12 
13 //- dune-fem includes
16 
17 
18 namespace Dune
19 {
20 
21  namespace Fem
22  {
23 
26  template< class Entry >
28  {
29  // interface class for factory
30  class FactoryIF
31  {
32  protected:
33  FactoryIF () {}
34  public:
35  virtual ~FactoryIF () {}
36  virtual Entry* getObject( const GeometryType& geomType ) const = 0;
37  virtual void removeObjects( std::vector<Entry*>& entryStorage ) const = 0;
38  virtual FactoryIF* clone() const = 0;
39  };
40 
41  // factory implementation depends on type of singleton provider
42  template <class SingletonProvider>
43  class FactoryImpl : public FactoryIF
44  {
45  public:
46  FactoryImpl() {}
47 
48  Entry* getObject( const GeometryType& geomType ) const
49  {
50  return & SingletonProvider :: getObject( geomType );
51  }
52 
53  void removeObjects( std::vector<Entry*>& entryStorage ) const
54  {
55  const size_t size = entryStorage.size();
56  for( size_t i=0; i<size; ++i )
57  {
58  if( entryStorage[ i ] )
59  {
60  SingletonProvider :: removeObject( *(entryStorage[ i ]) );
61  entryStorage[ i ] = 0 ;
62  }
63  }
64  }
65 
66  virtual FactoryIF* clone() const { return new FactoryImpl<SingletonProvider> (); }
67  };
68 
69  // pointer to apropriate factory
70  const FactoryIF* factory_;
71  // vector caching singleton pointers
72  std::vector< Entry* > entryStorage_;
73  public:
74  // export value type confomring to std::vector
75  typedef Entry value_type ;
76 
77  // default constructor
79  : factory_( 0 )
80  , entryStorage_()
81  {}
82 
85  : factory_( other.factory_ ? other.factory_->clone() : 0 )
86  , entryStorage_( other.entryStorage_.size(), ( Entry * ) 0 )
87  {
88  // make a copy of the vector
89  const size_t size = entryStorage_.size();
90  for( size_t i=0; i<size; ++i )
91  {
92  Entry* otherEntry = other.entryStorage_[ i ];
93  if( otherEntry )
94  {
95  // we need the interface method geometry
96  // (on base function sets and compiled local keys )
97  entryStorage_[ i ] = factory_->getObject( otherEntry->type() );
98  }
99  }
100  }
101 
104  : factory_( other.factory_ ),
105  entryStorage_( std::move( other.entryStorage_ ) )
106  {
107  other.factory_ = nullptr;
108  other.entryStorage_.clear();
109  }
110 
113  {
114  if( entryStorage_.size() > 0 )
115  {
116  factory_->removeObjects( entryStorage_ );
117  }
118 
119  delete factory_;
120  factory_ = 0;
121  }
122 
123  // get maxSize of compiled keys
124  unsigned int maxSize() const
125  {
126  unsigned int maxSize = 0;
127  const size_t size = entryStorage_.size() ;
128  for( size_t i=0; i<size; ++i)
129  {
130  if( entryStorage_[ i ] )
131  {
132  unsigned int enSize = entryStorage_[ i ]->size();
133  maxSize = std::max( enSize , maxSize );
134  }
135  }
136  return maxSize;
137  }
138 
140  template <class SingletonProvider>
141  bool insert( const GeometryType geomType )
142  {
143  // create factory if not existing yet
144  if( factory_ == 0 )
145  {
146  factory_ = new FactoryImpl< SingletonProvider > ();
147  }
148 
149  // check that type of factory is correct
150  assert( dynamic_cast< const FactoryImpl< SingletonProvider >* > ( factory_ ) != 0 );
151 
152  // get geometry type index
153  const size_t geomIndex = index( geomType ) ;
154 
155  if( entryStorage_.size() <= geomIndex )
156  entryStorage_.resize( geomIndex + 1, (Entry* ) 0 );
157 
158  assert( geomIndex < entryStorage_.size() );
159 
160  // if entry is still not used, insert it
161  if( entryStorage_[ geomIndex ] == 0 )
162  {
163  entryStorage_[ geomIndex ] = factory_->getObject( geomType );
164  return true ;
165  }
166  return false ;
167  }
168 
170  bool exists( const GeometryType& geomType ) const
171  {
172  if( index( geomType ) < static_cast< int >( entryStorage_.size() ) )
173  return (entryStorage_[ index( geomType ) ] != 0) ;
174  else
175  return false;
176  }
177 
179  const Entry& operator [] ( const GeometryType& geomType ) const
180  {
181  // assert( factory_ );
182  assert( index( geomType ) < static_cast< int >( entryStorage_.size() ) );
183  assert( entryStorage_[ index( geomType ) ] != 0 );
184  return *( entryStorage_[ index( geomType ) ]);
185  }
186  protected:
187  int index( const GeometryType& geomType ) const
188  {
189  return LocalGeometryTypeIndex::index( geomType );
190  }
191  };
192 
193 
194 
197  template< class CompiledLocalKey, unsigned int minPolOrder, unsigned int maxPolOrder >
199  {
200  public:
201  // type of compiled local key
202  typedef CompiledLocalKey CompiledLocalKeyType;
203 
206 
207  // vector containing storages for each polynomial order
208  typedef std::vector< LocalKeyStorageType > LocalKeyVectorType;
209 
210  protected:
211  enum { numOrders = maxPolOrder - minPolOrder + 1 };
212 
213  template <int pOrd>
215  {
222  {
223  public:
225  static CompiledLocalKeyType* createObject( const GeometryType& type )
226  {
227  return new CompiledLocalKeyType( type, pOrd );
228  }
229 
231  static void deleteObject( CompiledLocalKeyType* obj )
232  {
233  delete obj;
234  }
235  };
236 
237  static void apply( LocalKeyVectorType& compiledLocalKeys,
238  const GeometryType& geometryType )
239  {
240  const size_t k = pOrd ;
241 
243  typedef SingletonList
244  < GeometryType, CompiledLocalKeyType, CompiledLocalKeyFactory >
245  CompiledLocalKeySingletonProviderType;
246 
247  // insert compiled local key
248  compiledLocalKeys[ k - minPolOrder ].template insert< CompiledLocalKeySingletonProviderType > ( geometryType );
249  }
250  };
251 
252  protected:
253  // all lagrange point sets for available geometry types
254  LocalKeyVectorType compiledLocalKeys_ ;
255 
256  private:
258 
259  public:
260  template <class GridPart>
261  CompiledLocalKeyContainer( const GridPart& gridPart )
262  : compiledLocalKeys_( numOrders )
263  {
264  typedef typename GridPart :: IndexSetType IndexSetType ;
265  typedef typename GridPart :: GridType GridType ;
266  const IndexSetType &indexSet = gridPart.indexSet();
267 
268  // get all available geometry types
269  AllGeomTypes< IndexSetType, GridType > allGeometryTypes( indexSet );
270  const std :: vector< GeometryType >& geometryTypes
271  = allGeometryTypes.geomTypes( 0 );
272 
273  // create compiled local keys
274  for( unsigned int i = 0; i < geometryTypes.size(); ++i )
275  {
276  ForLoop< ConstructCompiledLocalKeys, minPolOrder, maxPolOrder > ::
277  apply( compiledLocalKeys_, geometryTypes[ i ] );
278  }
279  }
280 
287  inline const LocalKeyStorageType& compiledLocalKeys( const int order ) const
288  {
289  assert( order - minPolOrder >= 0 );
290  assert( int( order - minPolOrder ) < int( compiledLocalKeys_.size() ) );
291  return compiledLocalKeys_[ order - minPolOrder ];
292  }
293 
301  inline const CompiledLocalKeyType &compiledLocalKey( const GeometryType& type, const int order ) const
302  {
303  return compiledLocalKeys( order )[ type ];
304  }
305  };
306 
307  } // namespace Fem
308 
309 } // namespace Dune
310 
311 #endif // DUNE_FEM_BASESETLOCALKEYSTORAGE_HH
unsigned int maxSize() const
Definition: basesetlocalkeystorage.hh:124
BaseSetLocalKeyStorage< CompiledLocalKeyType > LocalKeyStorageType
type of storage class for compiled local keys
Definition: basesetlocalkeystorage.hh:205
CompiledLocalKey CompiledLocalKeyType
Definition: basesetlocalkeystorage.hh:202
static constexpr T max(T a)
Definition: utility.hh:65
Singleton list for key/object pairs.
Definition: singletonlist.hh:49
static void deleteObject(CompiledLocalKeyType *obj)
delete BaseFunctionSet
Definition: basesetlocalkeystorage.hh:231
const LocalKeyStorageType & compiledLocalKeys(const int order) const
provide access to all compiled local keys for a given polynomial order
Definition: basesetlocalkeystorage.hh:287
static CompiledLocalKeyType * createObject(const GeometryType &type)
create new BaseFunctionSet
Definition: basesetlocalkeystorage.hh:225
bool exists(const GeometryType &geomType) const
return true if an entry for this geometry type exists
Definition: basesetlocalkeystorage.hh:170
~BaseSetLocalKeyStorage()
destructor
Definition: basesetlocalkeystorage.hh:112
LocalKeyVectorType compiledLocalKeys_
Definition: basesetlocalkeystorage.hh:254
static void apply(LocalKeyVectorType &compiledLocalKeys, const GeometryType &geometryType)
Definition: basesetlocalkeystorage.hh:237
const CompiledLocalKeyType & compiledLocalKey(const GeometryType &type, const int order) const
provide access to the compiled local keys for a geometry type and polynomial order ...
Definition: basesetlocalkeystorage.hh:301
Definition: coordinate.hh:4
class for storage local keys for a given range of polynomial order and available geometry type ...
Definition: basesetlocalkeystorage.hh:198
BaseSetLocalKeyStorage(BaseSetLocalKeyStorage &&other)
move constructor
Definition: basesetlocalkeystorage.hh:103
BaseSetLocalKeyStorage(const BaseSetLocalKeyStorage &other)
copy constructor
Definition: basesetlocalkeystorage.hh:84
CompiledLocalKeyContainer(const GridPart &gridPart)
Definition: basesetlocalkeystorage.hh:261
STL namespace.
default implementation uses method geomTypes of given index set. Used in DiscreteFunctionSpaces.
Definition: allgeomtypes.hh:89
void move(ArrayInterface< T > &array, const unsigned int oldOffset, const unsigned int newOffset, const unsigned int length)
Definition: array_inline.hh:38
const Entry & operator[](const GeometryType &geomType) const
access to stored entry with given geometry type
Definition: basesetlocalkeystorage.hh:179
CompiledLocalKeyFactory method createObject and deleteObject for the SingletonList.
Definition: basesetlocalkeystorage.hh:221
bool insert(const GeometryType geomType)
insert entry to storage for given geometry type
Definition: basesetlocalkeystorage.hh:141
const std::vector< GeometryType > & geomTypes(unsigned int codim) const
returns vector with geometry tpyes this index set has indices for
Definition: allgeomtypes.hh:145
BaseSetLocalKeyStorage()
Definition: basesetlocalkeystorage.hh:78
Entry value_type
Definition: basesetlocalkeystorage.hh:75
int index(const GeometryType &geomType) const
Definition: basesetlocalkeystorage.hh:187
std::vector< LocalKeyStorageType > LocalKeyVectorType
Definition: basesetlocalkeystorage.hh:208
storage class for base function set pointer and compiled local key pointers
Definition: basesetlocalkeystorage.hh:27