DUNE-FEM (unstable)

mapping.hh
1 #ifndef DUNE_FEM_MAPPING_HH
2 #define DUNE_FEM_MAPPING_HH
3 
4 #include <iostream>
5 #include <vector>
6 
7 namespace Dune
8 {
9 
10  namespace Fem
11  {
12 
13 
45  template<typename DFieldType,typename RFieldType, class DType, class RType>
46  class Mapping
47  {
48  protected:
51 
52  public:
57  typedef DType DomainType;
58 
63  typedef RType RangeType;
64 
66  typedef DFieldType DomainFieldType;
67 
69  typedef RFieldType RangeFieldType;
70 
73  : lincomb_(),
74  loopDetected_( false )
75  {
76  // store pointer to myself
77  lincomb_.push_back( term( *this, 1. ) );
78  }
79 
81  virtual ~Mapping ()
82  {
83  }
84 
90  {
91  const MappingType &m = dynamic_cast<const MappingType& >( mapping );
92 
93  lincomb_.erase( lincomb_.begin(), lincomb_.end() );
94  typedef typename std::vector<term>::const_iterator iterator;
95 
96  // reserve memory
97  lincomb_.reserve( m.lincomb_.size() );
98 
99  iterator end = m.lincomb_.end();
100  for (iterator it = m.lincomb_.begin(); it != end; it++ )
101  {
102  lincomb_.push_back( *it );
103  }
104  return *this;
105  }
106 
112  void operator() (const DomainType &arg, RangeType &dest ) const
113  {
114  int count = 0;
115  typedef typename std::vector<term>::const_iterator const_iterator;
116  const_iterator end = lincomb_.end();
117  for ( const_iterator it = lincomb_.begin(); it != end; ++it )
118  {
119  const term& current = (*it);
120  if ( count == 0 )
121  {
122  // call apply of term, see below
123  current.apply( arg, dest );
124  }
125  else
126  {
127  // call applyAdd of term, see below
128  current.applyAdd( arg, dest );
129  }
130  ++count;
131  }
132 
133  loopDetected_ = false ;
134  }
135 
136  private:
138  virtual void apply (const DomainType &arg, RangeType &dest) const
139  {
140  // do noting if apply method leads to myself
141  if( loopDetected_ )
142  {
143 #ifndef NDEBUG
144  std::cerr << "WARNING: Mapping::apply: Loop detected! Check overloading of operator() and apply! " << std::endl;
145 #endif
146  return ;
147  }
148 
149  // set applyLoop to true again
150  loopDetected_ = true ;
151 
152  operator()(arg, dest);
153  }
154 
156  struct term {
157  term() : v_(NULL), scalar_(1.0), scaleIt_(false) { }
158  term(const term& other)
159  : v_(other.v_), scalar_(other.scalar_), scaleIt_(other.scaleIt_) { }
160 
161  term(const MappingType &mapping, RangeFieldType scalar ) : v_(&mapping), scalar_(scalar), scaleIt_( true ) {
162  if ( scalar_ == 1. ) {
163  scaleIt_ = false;
164  }
165  }
166 
167  void apply(const DomainType &arg, RangeType &dest) const
168  {
169  v_->apply( arg, dest );
170  if ( scaleIt_ )
171  {
172  dest *= scalar_;
173  }
174  }
175 
176  void applyAdd(const DomainType &arg, RangeType &dest) const
177  {
178  // note, copying here might be costly
179  RangeType tmp( dest );
180 
181  v_->apply( arg, tmp );
182  if ( scalar_ == 1. )
183  {
184  dest += tmp;
185  }
186  else if ( scalar_ == -1. )
187  {
188  dest -= tmp;
189  }
190  else
191  {
192  tmp *= scalar_;
193  dest += tmp;
194  }
195  }
196 
197  protected:
198  const MappingType *v_;
199  RangeFieldType scalar_;
200  bool scaleIt_;
201 
202  // friendship for operations
203  friend struct MappingOperators;
204  };
205 
206 
208  std::vector<term> lincomb_;
209 
211  mutable bool loopDetected_ ;
212 
213  // friendship for operations
214  friend struct MappingOperators;
215  };
216 
217 
220  {
222  template<typename DFieldType,typename RFieldType, class DType, class RType>
223  static inline void
226  {
227  typedef Mapping<DFieldType,RFieldType,DType,RType> MappingType;
228  typedef typename std::vector< typename MappingType :: term > :: const_iterator iterator;
229 
230  // clear mapping entries
231  copy.lincomb_.clear();
232 
233  {
234  iterator end = org.lincomb_.end();
235  for ( iterator it = org.lincomb_.begin(); it != end; ++it )
236  {
237  copy.lincomb_.push_back( *it );
238  }
239  }
240  }
241 
243  template<typename DFieldType,typename RFieldType, class DType, class RType>
247  {
248  typedef Mapping<DFieldType,RFieldType,DType,RType> MappingType;
249  // new mapping
250  MappingType newMapping;
251 
252  // copy mapping
253  copyMapping(a, newMapping);
254 
255  typedef typename std::vector< typename MappingType :: term > :: const_iterator iterator;
256 
257  iterator end = b.lincomb_.end();
258  for ( iterator it = b.lincomb_.begin(); it != end; ++it )
259  {
260  newMapping.lincomb_.push_back( *it );
261  }
262  return newMapping;
263  }
264 
266  template<typename DFieldType,typename RFieldType, class DType, class RType>
270  {
271  typedef Mapping<DFieldType,RFieldType,DType,RType> MappingType;
272  typedef typename MappingType :: term term;
273  typedef typename std::vector< term > :: const_iterator iterator;
274  // new mapping
275  MappingType newMapping;
276 
277  // copy mapping
278  copyMapping(a, newMapping);
279 
280  iterator end = b.lincomb_.end();
281  for ( iterator it = b.lincomb_.begin(); it != end; ++it )
282  {
283  newMapping.lincomb_.push_back( term( *it->v_, -it->scalar_ ) );
284  }
285  return newMapping;
286  }
287 
289  template<typename DFieldType,typename RFieldType, class DType, class RType>
292  const RFieldType& scalar)
293  {
294  typedef Mapping<DFieldType,RFieldType,DType,RType> MappingType;
295  typedef typename MappingType :: term term;
296  typedef typename std::vector< term > :: iterator iterator;
297  // new mapping
298  MappingType newMapping;
299 
300  // copy mapping
301  copyMapping(a, newMapping);
302 
303  iterator end = newMapping.lincomb_.end();
304  for ( iterator it = newMapping.lincomb_.begin(); it != end; ++it )
305  {
306  it->scalar_ *= scalar;
307  }
308  return newMapping;
309  }
310 
312  template<typename DFieldType,typename RFieldType, class DType, class RType>
315  const RFieldType& scalar)
316  {
317  RFieldType factor = RFieldType(1)/scalar;
318  return multiplyMapping(a,factor);
319  }
320  };
321 
330  template<class DFieldType, class RFieldType, class DType, class RType>
331  static inline Mapping<DFieldType,RFieldType,DType,RType>
334  {
335  return MappingOperators::addMappings(a,b);
336  }
337 
344  template<class DFieldType, class RFieldType, class DType, class RType>
348  {
350  }
351 
358  template<class DFieldType, class RFieldType, class DType, class RType>
361  const RFieldType& factor)
362  {
363  return MappingOperators::multiplyMapping(mapping,factor);
364  }
365 
372  template<class DFieldType, class RFieldType, class DType, class RType>
374  operator *(const RFieldType& factor,
376  {
377  return MappingOperators::multiplyMapping(mapping,factor);
378  }
379 
386  template<class DFieldType, class RFieldType, class DType, class RType>
389  const RFieldType& factor)
390  {
391  return MappingOperators::divideMapping(mapping,factor);
392  }
393 
400  template<class DFieldType, class RFieldType, class DType, class RType>
402  operator /(const RFieldType& factor,
404  {
405  return MappingOperators::divideMapping(mapping,factor);
406  }
407 
408  } // namespace Fem
409 
410 } // namespace Dune
411 #endif // #ifndef DUNE_FEM_MAPPING_HH
A mapping from one vector space into another This class describes a general mapping from the domain v...
Definition: mapping.hh:47
DFieldType DomainFieldType
type of field the domain vector space, i.e. double
Definition: mapping.hh:66
RFieldType RangeFieldType
type of field the range vector space, i.e. double
Definition: mapping.hh:69
DType DomainType
domain vector space (for usage in derived classes) This can either be for example a discrete function...
Definition: mapping.hh:57
RType RangeType
range vector space (for usage in derived classes) This can either be for example a discrete function ...
Definition: mapping.hh:63
virtual ~Mapping()
delete linear combination if necessary
Definition: mapping.hh:81
MappingType & operator=(const MappingType &mapping)
assignment of mapping mapping
Definition: mapping.hh:89
Mapping< DFieldType, RFieldType, DType, RType > MappingType
type of mapping
Definition: mapping.hh:50
Mapping()
create Mappiung with empty linear combination
Definition: mapping.hh:72
void operator()(const DomainType &arg, RangeType &dest) const
Application operator that applies all operators in the linear combination stack.
Definition: mapping.hh:112
Dune namespace.
Definition: alignedallocator.hh:13
Implementation of Mapping +, -, *, / operations.
Definition: mapping.hh:220
static Mapping< DFieldType, RFieldType, DType, RType > addMappings(const Mapping< DFieldType, RFieldType, DType, RType > &a, const Mapping< DFieldType, RFieldType, DType, RType > &b)
add mappings
Definition: mapping.hh:245
static Mapping< DFieldType, RFieldType, DType, RType > divideMapping(const Mapping< DFieldType, RFieldType, DType, RType > &a, const RFieldType &scalar)
divide mapping
Definition: mapping.hh:314
static Mapping< DFieldType, RFieldType, DType, RType > multiplyMapping(const Mapping< DFieldType, RFieldType, DType, RType > &a, const RFieldType &scalar)
multiply mapping
Definition: mapping.hh:291
static Mapping< DFieldType, RFieldType, DType, RType > substractMappings(const Mapping< DFieldType, RFieldType, DType, RType > &a, const Mapping< DFieldType, RFieldType, DType, RType > &b)
substract mappings
Definition: mapping.hh:268
static void copyMapping(const Mapping< DFieldType, RFieldType, DType, RType > &org, Mapping< DFieldType, RFieldType, DType, RType > &copy)
copy mapping
Definition: mapping.hh:224
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 16, 22:29, 2024)