DUNE-FEM (unstable)

combinedfunction.hh
1#ifndef DUNE_FEM_FUNCTION_COMBINEDFUNCTION_COMBINEDFUNCTION_HH
2#define DUNE_FEM_FUNCTION_COMBINEDFUNCTION_COMBINEDFUNCTION_HH
3
4#include <algorithm>
5#include <iostream>
6#include <string>
7
8#include <dune/fem/common/stackallocator.hh>
9#include <dune/fem/function/common/discretefunction.hh>
10#include <dune/fem/function/localfunction/mutable.hh>
11#include <dune/fem/space/combinedspace/combinedspace.hh>
12#include <dune/fem/space/common/dofmanager.hh>
13#include <dune/fem/space/common/dofstorage.hh>
14#include <dune/fem/storage/referencevector.hh>
15
16namespace Dune
17{
18
19 namespace Fem
20 {
21
22 //- Forward declarations
23 template< class ContainedDiscreteFunction, int N >
24 class CombinedDiscreteFunction;
25
26 template< class ContainedDiscreteFunction, int N >
27 class CombinedDiscreteFunctionDofIterator;
28
29
30 //- Class definitions
33 template< class ContainedDiscreteFunction, int N >
34 struct DiscreteFunctionTraits< CombinedDiscreteFunction< ContainedDiscreteFunction, N > >
35 {
36 typedef ContainedDiscreteFunction ContainedDiscreteFunctionType;
39
40 typedef typename ContainedDiscreteFunctionType :: DiscreteFunctionSpaceType
41 ContainedDiscreteFunctionSpaceType;
42 typedef CombinedSpace< ContainedDiscreteFunctionSpaceType, N, VariableBased >
43 DiscreteFunctionSpaceType;
44
45 typedef typename DiscreteFunctionSpaceType :: DomainFieldType DomainFieldType;
46 typedef typename DiscreteFunctionSpaceType :: RangeFieldType RangeFieldType;
47 typedef typename DiscreteFunctionSpaceType :: DomainType DomainType;
48 typedef typename DiscreteFunctionSpaceType :: RangeType RangeType;
49
50 typedef typename DiscreteFunctionSpaceType :: JacobianRangeType JacobianRangeType;
51
52 typedef RangeFieldType DofType;
53 typedef typename DiscreteFunctionSpaceType :: MapperType MapperType;
54 typedef typename DiscreteFunctionSpaceType :: GridType GridType;
55 typedef typename DiscreteFunctionSpaceType :: GridPartType GridPartType;
56
59 typedef ConstDofIteratorDefault< DofIteratorType > ConstDofIteratorType;
60
61 typedef typename ContainedDiscreteFunctionType :: DofBlockType DofBlockType;
62 typedef typename ContainedDiscreteFunctionType :: ConstDofBlockType
63 ConstDofBlockType;
64 typedef typename ContainedDiscreteFunctionType :: DofBlockPtrType
65 DofBlockPtrType;
66 typedef typename ContainedDiscreteFunctionType :: ConstDofBlockPtrType
67 ConstDofBlockPtrType;
68
70 typedef StackAllocator< DofType, LocalDofVectorStackType* > LocalDofVectorAllocatorType;
72
74 };
75
76
80 template <class ContainedDiscreteFunctionImp,int N >
82 : public DiscreteFunctionDefault< CombinedDiscreteFunction< ContainedDiscreteFunctionImp, N > >
83 {
87
88 public:
90 typedef ContainedDiscreteFunctionImp ContainedDiscreteFunctionType;
91 typedef ContainedDiscreteFunctionType SubDiscreteFunctionType;
92
95
97 typedef typename BaseType::GridType GridType;
98
101
104 typedef typename BaseType::DiscreteFunctionType
110 typedef typename Traits::ContainedDiscreteFunctionSpaceType
113 SubDiscreteFunctionSpaceType;
115 typedef typename BaseType::DofType DofType;
125 typedef typename Traits::MapperType MapperType;
126
130 typedef typename BaseType::ConstDofIteratorType
132
133 typedef typename BaseType :: DofBlockPtrType DofBlockPtrType;
134 typedef typename BaseType :: ConstDofBlockPtrType ConstDofBlockPtrType;
135
136 typedef typename BaseType :: LocalDofVectorAllocatorType LocalDofVectorAllocatorType;
137
138 using BaseType :: assign; // needs DofIterator!
139 using BaseType :: axpy;
140 using BaseType :: space;
141
144 : BaseType( "combined_"+func.name(), createSpace( func.space().gridPart() ), LocalDofVectorAllocatorType( &ldvStack_ ) ),
145 ldvStack_( std::max( sizeof( DofType ), sizeof( DofType* ) ) * space().blockMapper().maxNumDofs() * DiscreteFunctionSpaceType::localBlockSize )
146 {
147 for (int i=0; i<N; ++i)
148 func_[i] = new ContainedDiscreteFunctionType(func);
149 }
150
151 CombinedDiscreteFunction(const std::string &name,
153 : BaseType( "combined_"+name, createSpace( spc.gridPart() ), LocalDofVectorAllocatorType( &ldvStack_ ) ),
154 ldvStack_( std::max( sizeof( DofType ), sizeof( DofType* ) ) * space().blockMapper().maxNumDofs() * DiscreteFunctionSpaceType::localBlockSize )
155 {
156 for (int i=0; i<N; ++i)
157 func_[i] = new ContainedDiscreteFunctionType(name,spc);
158 }
159
160 CombinedDiscreteFunction(const std::string &name,
161 const DiscreteFunctionSpaceType& spc )
162 : BaseType( "combined_"+name, createSpace( spc.gridPart() ), LocalDofVectorAllocatorType( &ldvStack_) ),
163 ldvStack_( std::max( sizeof( DofType ), sizeof( DofType* ) ) * space().blockMapper().maxNumDofs() * DiscreteFunctionSpaceType::localBlockSize )
164 {
165 for (int i=0; i<N; ++i)
166 {
167 func_[i] = new ContainedDiscreteFunctionType(name,space().containedSpace());
168 }
169 }
170
174 : BaseType( other.name()+"_copy", createSpace( other.space().gridPart() ), LocalDofVectorAllocatorType( &ldvStack_ ) ),
175 ldvStack_( other.ldvStack_ )
176 {
177 for (int i=0; i<N; ++i)
178 func_[i] = new ContainedDiscreteFunctionType(other.subFunction(i));
179 }
180
183 {
184 for (int i=0; i<N; ++i)
185 delete func_[i];
186
187 delete &space();
188 }
189
190 CombinedDiscreteFunction() = delete;
191 ThisType& operator= ( const ThisType& ) = delete;
192 ThisType& operator= ( ThisType&& ) = delete;
193
195 void clear()
196 {
197 for (int i=0; i<N; ++i)
198 func_[i]->clear();
199 }
200
202 void assign( const ThisType &g )
203 {
204 for( int i=0; i<N; ++i)
205 func_[i]->assign( g.subFunction( i ) );
206 }
207
209 int size() const
210 {
211 return func_[0]->size()*N;
212 }
213
216 {
217 for (int i=0; i<N; ++i)
218 *func_[ i ] += g.subFunction( i );
219 return *this;
220 }
221
224 using BaseType::operator-=;
225 ThisType &operator-= ( const ThisType &g )
226 {
227 for( int i = 0; i < N; ++i )
228 *func_[ i ] -= g.subFunction( i );
229 return *this;
230 }
231
234 {
235 for (int i=0; i<N; ++i)
236 *func_[i] *= scalar;
237 return *this;
238 }
239
242 {
243 for (int i=0; i<N; ++i)
244 *func_[i] /= scalar;
245 return *this;
246 }
247
250 void addScaled( const ThisType &g, const RangeFieldType &s )
251 {
252 axpy( g, s );
253 }
254
257 void axpy( const RangeFieldType &s, const ThisType &g )
258 {
259 for (int i=0; i<N; ++i)
260 func_[i]->axpy( s, g.subFunction( i ) );
261 }
262
265 {
266 RangeFieldType ret( 0 );
267 for( int i = 0; i < N; ++i )
268 ret += func_[ i ]->scalarProductDofs( other.subFunction( i ) );
269 return ret;
270 }
271
273 template< class StreamTraits >
275 {
276 for (int i=0; i<N; ++i)
277 func_[i]->read(in);
278 }
280 template< class StreamTraits >
282 {
283 for (int i=0; i<N; ++i)
284 func_[i]->write(out);
285 }
286
288 void print( std :: ostream &out ) const
289 {
290 for (int i=0; i<N; ++i)
291 func_[i]->print(out);
292 }
293
295 bool dofsValid () const
296 {
297 bool ret = func_[0]->dofsValid();
298 for (int i=1;i<N;i++)
299 ret |= func_[i]->dofsValid();
300 return ret;
301 }
302
303 ConstDofBlockPtrType block ( unsigned int index ) const
304 {
305 // This is wrong with the current implementation of CombinedSpace
306 const int containedSize = func_[ 0 ]->space().blockMapper().size();
307 const int component = index / containedSize;
308 const int containedIndex = index % containedSize;
309 const ContainedDiscreteFunctionType& func = *(func_[ component ]);
310 return func.block( containedIndex );
311 }
312
313 DofBlockPtrType block ( unsigned int index )
314 {
315 // This is wrong with the current implementation of CombinedSpace
316 const int containedSize = func_[ 0 ]->space().blockMapper().size();
317 const int component = index / containedSize;
318 const int containedIndex = index % containedSize;
319 return func_[ component ]->block( containedIndex );
320 }
321
322 const RangeFieldType &dof(unsigned int index) const
323 {
324 int variable = index / func_[0]->size();
325 int point = index % func_[0]->size();
326 return func_[variable]->dof(point);
327 }
328
329 RangeFieldType &dof ( unsigned int index )
330 {
331 int variable = index / func_[0]->size();
332 int point = index % func_[0]->size();
333 return func_[variable]->dof(point);
334 }
335
338 {
340 }
343 {
344 return ConstDofIteratorType(DofIteratorType(false,*this));
345 }
348 {
349 return DofIteratorType(*this);
350 }
353 {
354 return DofIteratorType(false,*this);
355 }
356
357 ContainedDiscreteFunctionType& subFunction( const int i )
358 {
359 return *(func_[i]);
360 }
361
362 const ContainedDiscreteFunctionType& subFunction( const int i ) const
363 {
364 return *(func_[i]);
365 }
366
368 {
369 return space().containedSpace();
370 }
371
372 private:
373 const ThisType& interface() const
374 {
375 return *this;
376 }
377
378 DiscreteFunctionSpaceType& createSpace( GridPartType& gp )
379 {
380 // we need to delete the space in the destructor
381 return *(new DiscreteFunctionSpaceType( gp ));
382 }
383
384 typename Traits :: LocalDofVectorStackType ldvStack_;
386 friend class CombinedDiscreteFunctionDofIterator<ContainedDiscreteFunctionType,N>;
387 };
388
392 template <class ContainedDiscreteFunctionImp,int N>
394 : public DofIteratorDefault <
395 typename ContainedDiscreteFunctionImp::DofType ,
396 CombinedDiscreteFunctionDofIterator<ContainedDiscreteFunctionImp,N> >
397 {
398 public:
401 typedef typename Traits::DiscreteFunctionType DiscreteFunctionType;
402 typedef typename Traits::ContainedDiscreteFunctionType ContainedDiscreteFunctionType;
403 typedef typename ContainedDiscreteFunctionType::DofIteratorType ContainedDofIteratorType;
404 typedef typename ContainedDiscreteFunctionType::ConstDofIteratorType ContainedConstDofIteratorType;
405 typedef typename Traits::DofType DofType;
406
408 CombinedDiscreteFunctionDofIterator(bool end,const DiscreteFunctionType& df) :
409 df_(const_cast<DiscreteFunctionType&>(df)),
410 comp_(N-1),
411 iter_(df.func_[N-1]->dend()),
412 endIter_(df.func_[N-1]->dend())
413 {}
415 CombinedDiscreteFunctionDofIterator(const DiscreteFunctionType& df) :
416 df_(const_cast<DiscreteFunctionType&>(df)),
417 comp_(0),
418 iter_(df.func_[0]->dbegin()),
419 endIter_(df.func_[0]->dend())
420 {}
422 CombinedDiscreteFunctionDofIterator(bool end,DiscreteFunctionType& df) :
423 df_(df),
424 comp_(N-1),
425 iter_(df.func_[N-1]->dend()),
426 endIter_(df.func_[N-1]->dend())
427 {}
429 CombinedDiscreteFunctionDofIterator(DiscreteFunctionType& df) :
430 df_(df),
431 comp_(0),
432 iter_(df.func_[0]->dbegin()),
433 endIter_(df.func_[0]->dend())
434 {}
437 df_(other.df_),
438 comp_(other.comp_),
439 iter_(other.iter_),
440 endIter_(other.endIter_)
441 {}
442
445 {
446 df_ = other.df_;
447 comp_ = other.comp_;
448 iter_ = other.iter_;
449 endIter_ = other.endIter_;
450 return *this;
451 }
452
454 DofType& operator *()
455 {
456 return *iter_;
457 }
458
460 const DofType& operator * () const
461 {
462 return *iter_;
463 }
464
467 {
468 ++iter_;
469 if (iter_==endIter_ && comp_<N-1)
470 {
471 ++comp_;
472 iter_ = df_.func_[comp_]->dbegin();
473 endIter_ = df_.func_[comp_]->dend();
474 }
475 return *this;
476 }
477
479 bool operator == (const ThisType & I ) const
480 {
481 return (comp_ == I.comp_) && (iter_ == I.iter_);
482 }
483
485 bool operator != (const ThisType & I ) const
486 {
487 return !((*this) == I);
488 }
489
490 void reset()
491 {
492 comp_ = 0;
493 iter_ = df_.func_[ 0 ]->dbegin();
494 endIter_ = df_.func_[ 0 ]->dend();
495 }
496
497 private:
500 mutable int comp_;
501 mutable ContainedDofIteratorType iter_,endIter_;
502
503 };
504
505 } // namespace Fem
506
507} // namespace Dune
508
509#endif // #ifndef DUNE_FEM_FUNCTION_COMBINEDFUNCTION_COMBINEDFUNCTION_HH
Iterator over an array of dofs.
Definition: combinedfunction.hh:397
bool operator!=(const ThisType &I) const
compare
Definition: combinedfunction.hh:485
CombinedDiscreteFunctionDofIterator(DiscreteFunctionType &df)
Constructor.
Definition: combinedfunction.hh:429
ThisType & operator++()
go to next dof
Definition: combinedfunction.hh:466
CombinedDiscreteFunctionDofIterator(bool end, const DiscreteFunctionType &df)
End constructor.
Definition: combinedfunction.hh:408
CombinedDiscreteFunctionDofIterator(const DiscreteFunctionType &df)
Constructor (const)
Definition: combinedfunction.hh:415
DofType & operator*()
return dof
Definition: combinedfunction.hh:454
CombinedDiscreteFunctionDofIterator(const ThisType &other)
Copy Constructor.
Definition: combinedfunction.hh:436
bool operator==(const ThisType &I) const
compare
Definition: combinedfunction.hh:479
CombinedDiscreteFunctionDofIterator(bool end, DiscreteFunctionType &df)
End constructor.
Definition: combinedfunction.hh:422
ThisType & operator=(const ThisType &other)
Assignment operator.
Definition: combinedfunction.hh:444
Definition: combinedfunction.hh:83
CombinedDiscreteFunction(const ThisType &other)
Definition: combinedfunction.hh:173
RangeFieldType scalarProductDofs(const ThisType &other) const
Definition: combinedfunction.hh:264
BaseType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType
the combined discrete function type
Definition: combinedfunction.hh:108
DofIteratorType dend()
obtain an iterator pointing behind the last DoF (read-write)
Definition: combinedfunction.hh:352
DofIteratorType dbegin()
obtain an iterator pointing to the first DoF (read-write)
Definition: combinedfunction.hh:347
ThisType & operator+=(const ThisType &g)
Definition: combinedfunction.hh:215
void clear()
set all degrees of freedom to zero
Definition: combinedfunction.hh:195
void assign(const ThisType &g)
Definition: combinedfunction.hh:202
ConstDofIteratorType dbegin() const
obtain an iterator pointing to the first DoF (read-only)
Definition: combinedfunction.hh:337
int size() const
obtain total number of DoFs
Definition: combinedfunction.hh:209
BaseType::GridPartType GridPartType
GridPart implementation.
Definition: combinedfunction.hh:100
void axpy(const RangeFieldType &s, const ThisType &g)
axpy operation
Definition: combinedfunction.hh:257
BaseType::RangeType RangeType
Vector type used for the range field.
Definition: combinedfunction.hh:121
void read(InStreamInterface< StreamTraits > &in)
read the discrete function from a stream
Definition: combinedfunction.hh:274
Traits::MapperType MapperType
Mapper type (from the space)
Definition: combinedfunction.hh:125
bool dofsValid() const
check for NaNs
Definition: combinedfunction.hh:295
BaseType::ConstDofIteratorType ConstDofIteratorType
Read-only iterator over dof container.
Definition: combinedfunction.hh:131
void write(OutStreamInterface< StreamTraits > &out) const
write the discrete function into a stream
Definition: combinedfunction.hh:281
BaseType::RangeFieldType RangeFieldType
Intrinsic type used for range field (like DofType)
Definition: combinedfunction.hh:117
BaseType::DofIteratorType DofIteratorType
Iterator over dof container.
Definition: combinedfunction.hh:128
void addScaled(const ThisType &g, const RangeFieldType &s)
Definition: combinedfunction.hh:250
BaseType::DomainType DomainType
Vector type used for the domain field.
Definition: combinedfunction.hh:123
BaseType::GridType GridType
Grid implementation.
Definition: combinedfunction.hh:97
ConstDofIteratorType dend() const
obtain an iterator pointing behind the last DoF (read-only)
Definition: combinedfunction.hh:342
BaseType::DofType DofType
Intrinsic type used for dofs (typically a float type)
Definition: combinedfunction.hh:115
BaseType::DiscreteFunctionType DiscreteFunctionType
Definition: combinedfunction.hh:105
DiscreteFunctionTraits< ThisType > Traits
Traits class with all necessary type definitions.
Definition: combinedfunction.hh:94
Traits::ContainedDiscreteFunctionSpaceType ContainedDiscreteFunctionSpaceType
Contained discrete function space.
Definition: combinedfunction.hh:111
ContainedDiscreteFunctionImp ContainedDiscreteFunctionType
Discrete function this discrete function belongs to.
Definition: combinedfunction.hh:90
~CombinedDiscreteFunction()
Destructor.
Definition: combinedfunction.hh:182
BaseType::DomainFieldType DomainFieldType
Intrinsic type used for the domain field.
Definition: combinedfunction.hh:119
CombinedDiscreteFunction(const ContainedDiscreteFunctionType &func)
Constructor.
Definition: combinedfunction.hh:143
void print(std ::ostream &out) const
Definition: combinedfunction.hh:288
DiscreteFunctionType & operator/=(const RangeFieldType &scalar)
multiply all DoFs by a scalar factor
Definition: combinedfunction.hh:241
DiscreteFunctionType & operator*=(const RangeFieldType &scalar)
multiply all DoFs by a scalar factor
Definition: combinedfunction.hh:233
Definition: discretefunction.hh:584
const DiscreteFunctionSpaceType & space() const
obtain a reference to the corresponding DiscreteFunctionSpace
Definition: discretefunction.hh:709
BaseType::GridPartType GridPartType
type of the underlying grid part
Definition: discretefunction.hh:609
Traits::LocalDofVectorAllocatorType LocalDofVectorAllocatorType
type of LocalDofVector
Definition: discretefunction.hh:636
Traits::ConstDofIteratorType ConstDofIteratorType
type of the const dof iterator
Definition: discretefunction.hh:628
const std::string & name() const
obtain the name of the discrete function
Definition: discretefunction.hh:691
DiscreteFunctionSpaceType::RangeType RangeType
type of range vector
Definition: discretefunction.hh:614
Traits::DofIteratorType DofIteratorType
type of the dof iterator
Definition: discretefunction.hh:626
const GridPartType & gridPart() const
obtain a reference to the underlying grid part
Definition: discretefunction.hh:712
BaseType::DiscreteFunctionSpaceType DiscreteFunctionSpaceType
type of discrete function space
Definition: discretefunction.hh:606
DiscreteFunctionSpaceType::DomainType DomainType
type of domain vector
Definition: discretefunction.hh:612
DiscreteFunctionSpaceType::RangeFieldType RangeFieldType
type of range field (usually a float type)
Definition: discretefunction.hh:623
DiscreteFunctionSpaceType::DomainFieldType DomainFieldType
type of domain field (usually a float type)
Definition: discretefunction.hh:621
DiscreteFunctionSpaceType::GridType GridType
type of the underlying grid
Definition: discretefunction.hh:122
default implementation of DofManagerInterface
Definition: dofiterator.hh:143
An implementation of DenseVector which uses a std::vector of references as storage.
Definition: referencevector.hh:53
abstract interface for an input stream
Definition: streams.hh:190
abstract interface for an output stream
Definition: streams.hh:48
forward declaration
Definition: discretefunction.hh:51
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:484
Dune namespace.
Definition: alignedallocator.hh:13
STL namespace.
Traits class for a DiscreteFunction.
Definition: discretefunction.hh:61
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 12, 23:30, 2024)