functorN_expr.hpp 12.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/



/** \file functorN_expr.h */

#ifndef AMDIS_FUNCTOR_N_EXPRESSION_HPP
#define AMDIS_FUNCTOR_N_EXPRESSION_HPP

#include "AMDiS_fwd.h"
#include "LazyOperatorTerm.h"
#include "Functors.h"
31
// #include "expressions/functor_expr.hpp"
32
33
34
35
36
37
#include "operations/functors.hpp"

#include <boost/static_assert.hpp>

#include <tuple>
#include <utility> 
38
#include <functional>
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

namespace AMDiS 
{
  /// for_each for std::tuple
  template<std::size_t I = 0, typename FuncT, typename... Tp>
  inline typename std::enable_if<I == sizeof...(Tp), void>::type
  for_each(std::tuple<Tp...> &, FuncT) { }

  template<std::size_t I = 0, typename FuncT, typename... Tp>
  inline typename std::enable_if<I < sizeof...(Tp), void>::type
  for_each(std::tuple<Tp...>& t, FuncT f)
  {
    f(std::get<I>(t));
    for_each<I + 1, FuncT, Tp...>(t, f);
  }

  template<int I>
  using int_ = std::integral_constant<int, I>;
  
  
  namespace traits
  {
    /// get the degree of a functor by combining the degrees of the arguments
    template<typename F, typename Enable = void>
    struct functor_degree
    {
      template<typename... Int>
      static int eval(F f, Int... d) { return 0; }
    };
    
    template<typename F>
70
    struct functor_degree<F, typename enable_if< boost::is_base_of<FunctorBase, F> >::type >
71
72
73
74
    {
      template<typename... Int>
      static int eval(F f, Int... d) { return f.getDegree(d...); }
    };
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    
    // specialization for abstract-functions
    template<typename R, typename... Args>
    struct functor_degree<AbstractFunction<R, Args...> >
    {
      template<typename... Int>
      static int eval(AbstractFunction<R, Args...> const& fct, Int... d) { return fct.getDegree(); }
    };
    
    template<typename R, typename Arg0, typename Arg1>
    struct functor_degree<BinaryAbstractFunction<R, Arg0, Arg1> >
    {
      template<typename... Int>
      static int eval(BinaryAbstractFunction<R, Arg0, Arg1> const& fct, Int... d) { return fct.getDegree(); }
    };
    
    template<typename R, typename Arg0, typename Arg1, typename Arg2>
    struct functor_degree<TertiaryAbstractFunction<R, Arg0, Arg1, Arg2> >
    {
      template<typename... Int>
      static int eval(TertiaryAbstractFunction<R, Arg0, Arg1, Arg2> const& fct, Int... d) { return fct.getDegree(); }
    };
    
  } // end namespace traits
  
  namespace result_of
  {    
102
103
104
    template<class T>
    struct void_{ typedef void type; };
    
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    /// extract result type from function pointers
    template <class FPtr>
    struct Function;

    template <class R, class C, class... As>
    struct Function<R (C::*)(As...)>
    {
	typedef R result_type;
    };

    template<class R, class C, class... As>
    struct Function<R (C::*)(As...) const>
    {
	typedef R type;
    };
    
121
    template <class T>
122
123
124
    typename Function<T>::type function_helper(T);

    /// extract result type from functors
125
    template <class F, class Eanbled = void>
126
127
128
129
130
131
132
133
134
135
    struct Functor
    {
      typedef decltype(function_helper(&F::operator())) type;
    };
    
    template <class R, class... As>
    struct Functor<std::function<R(As...)> >
    {
      typedef R type;
    };
136
137
138
139
140
141
142
143
144
145
146
147
148

    template <class F>
    struct Functor<F, typename void_<typename F::value_type>::type>
    {
      typedef typename F::value_type type;
    };
    
    template <class F>
    struct Functor<F, typename void_<typename F::result_type>::type>
    {
      typedef typename F::result_type type;
    };
    
149
150
    
  } // end namespace result_of
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
  

  namespace detail 
  {
    /// Functor that initializes the feSpace list
    template<typename List>
    struct InsertFeSpaces 
    {
      List& feSpaces;
      InsertFeSpaces(List& feSpaces_) : feSpaces(feSpaces_) {};
      
      template<typename Term>
      void operator()(Term& term) {
	term.insertFeSpaces(feSpaces);
      }
    };
    
    /// Functor that is called on each term to initialize it on an element
    template<typename OT>
    struct InitElement 
    {
      OT* ot;
      const ElInfo *elInfo, *elInfo2;
      SubAssembler* subAssembler;
      Quadrature *quad;
      const BasisFunction *basisFct;
      
      InitElement(OT* ot_, const ElInfo* elInfo_, SubAssembler* subAssembler_, Quadrature *quad_, const BasisFunction *basisFct_)
	: ot(ot_), elInfo(elInfo_), elInfo2(NULL), subAssembler(subAssembler_),
	  quad(quad_), basisFct(basisFct_) {}
      
      InitElement(OT* ot_, const ElInfo* smallElInfo_, const ElInfo* largeElInfo_, SubAssembler* subAssembler_, Quadrature *quad_,  const BasisFunction *basisFct_)
	: ot(ot_), elInfo(smallElInfo_), elInfo2(largeElInfo_), subAssembler(subAssembler_),
	  quad(quad_), basisFct(basisFct_) {}
	  
      template<typename Term>
      void operator()(Term& term) {
	if (elInfo2)
	  term.initElement(ot, elInfo, elInfo2, subAssembler, quad, basisFct);
	else
	  term.initElement(ot, elInfo, subAssembler, quad, basisFct);
      }
    };
    
  } // end namespace detail
  

  /// Operator term with arbitrary number of sub-term (expressions)
199
  template <class... Terms>
200
201
202
203
  struct LazyOperatorTerms : public LazyOperatorTermBase
  {
    std::tuple<Terms...> term_tuple;
    
204
    template <class... Terms_>
205
    LazyOperatorTerms(Terms_... terms_)
206
207
      : term_tuple(terms_...) 
    { }
208
    
209
    template <class List>
210
211
212
213
214
    inline void insertFeSpaces(List& feSpaces)
    {
      for_each(term_tuple, detail::InsertFeSpaces<List>(feSpaces));
    }

215
    template <class OT>
216
217
218
219
220
221
222
    inline void initElement(OT* ot, const ElInfo* elInfo,
		    SubAssembler* subAssembler, Quadrature *quad, 
		    const BasisFunction *basisFct = NULL)
    {
      for_each(term_tuple, detail::InitElement<OT>(ot, elInfo, subAssembler, quad, basisFct));
    }

223
    template <class OT>
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
    inline void initElement(OT* ot, const ElInfo* smallElInfo, const ElInfo* largeElInfo,
		    SubAssembler* subAssembler, Quadrature *quad, 
		    const BasisFunction *basisFct = NULL)
    {
      for_each(term_tuple, detail::InitElement<OT>(ot, smallElInfo, largeElInfo, subAssembler, quad, basisFct));
    }
    
    inline double operator()(const int& iq) const;
  };

    
  // the expressions
  // _____________________________________________________________________________

  namespace expressions 
  {
    /// Functor that takes arbitrary number of arguments
241
    template <class F, class... Terms>
242
243
244
245
246
    struct FunctionN : public LazyOperatorTerms<Terms...>
    {
      typedef LazyOperatorTerms<Terms...> super;
      static const int N = sizeof...(Terms);
      
247
      typedef typename result_of::Functor<F>::type value_type;
248
249
250
251
      BOOST_STATIC_ASSERT_MSG( (!boost::is_same<value_type, traits::no_valid_type>::value), "********** ERROR: You have to define a result_type for your Functor **********" );
      
      F f; ///< the functor
      
252
253
      template <class... Terms_>
      FunctionN(F&& f_, Terms_... terms_)
254
255
256
	: super(terms_...), f(f_) {}
      
      // call f.getDegree() function    
257
258
      template<int I, class... Terms_>
      int getDegree(int_<I>, Terms_&&... terms) const
259
      {
260
	return getDegree(int_<I-1>(), std::get<I-1>(super::term_tuple), std::forward<Terms_>(terms)...);
261
262
      }
      
263
264
      template <class... Terms_>
      int getDegree(int_<0>, Terms_&&... terms) const
265
266
267
268
269
270
271
272
273
274
      {
	return traits::functor_degree<F>::eval(f, terms.getDegree()...);
      }
      
      int getDegree() const
      {
	return getDegree(int_<N>());
      }

      // call f.operator()(...)
275
276
      template <int I, class... Terms_>
      inline value_type eval(const int& iq, int_<I>, Terms_&&... terms) const
277
      {
278
	return eval(iq, int_<I-1>(), std::get<I-1>(super::term_tuple), std::forward<Terms_>(terms)...);
279
280
      }
      
281
282
      template <class... Terms_>
      inline value_type eval(const int& iq, int_<0>, Terms_&&... terms) const
283
284
285
286
287
288
289
      {
	return f(terms(iq)...);  // f(term1(iq), term2(iq), term3(iq),...)
      }
      
      inline value_type operator()(const int& iq) const { return eval(iq, int_<N>()); }
    };
    
290
291
292
293
294
295
296
297
298
299
300
301
302
303
    template<typename F, typename Term>
    using Function1 = FunctionN<F, Term>;
    
    template<typename F, typename Term1, typename Term2>
    using Function2 = FunctionN<F, Term1, Term2>;
    
    template<typename F, typename Term1, typename Term2, typename Term3>
    using Function3 = FunctionN<F, Term1, Term2, Term3>;
    
    template<typename F, typename Term1, typename Term2, typename Term3, typename Term4>
    using Function4 = FunctionN<F, Term1, Term2, Term3, Term4>;
    
    
    /// A wrapper functor for AMDiS::AbstractFunctions
304
    template <class TOut, class TIn>
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
    struct Wrapper : public FunctorBase
    {
      typedef TOut result_type;
      Wrapper(AbstractFunction<TOut, TIn>* fct_) : fct(fct_) {}
      int getDegree(int degree) const { return fct->getDegree(); }
      
      TOut operator()(const TIn& x) const 
      {
	return (*fct)(x);
      }
      
    protected:
      AbstractFunction<TOut, TIn>* fct;
    };
    
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
    
    template <int D, class F>
    struct DegreeWrapper : public FunctorBase
    {
      typedef typename result_of::Functor<F>::type result_type;
      
      DegreeWrapper(F&& fct_) : fct(fct_) {}
      
      template <class... Int>
      int getDegree(Int... degrees) const { return D; }
      
      template <class... Ts>
      result_type operator()(Ts&&... args) const 
      {
	return fct(args...);
      }
      
    protected:
      F fct;
    };
    
    
    template <class F, class DegF>
    struct DegreeWrapper2 : public FunctorBase
    {      
      typedef typename result_of::Functor<F>::type result_type;
      
      DegreeWrapper2(F&& fct_, DegF&& degfct_) 
	: fct(fct_), degfct(degfct_) {}
      
      template <class... Int>
      int getDegree(Int... degrees) const 
      { 
	return degfct(degrees...); 
      }
      
      template <class... Ts>
      result_type operator()(Ts&&... args) const 
      {
	return fct(args...);
      }
      
    protected:
      F fct;
      DegF degfct;
    };
    
367
368
369
370
371
372
  } // end namespace expressions


  namespace result_of
  {
    // result of generator-functions (used for enable_if constructs)
373
374
    template <typename F, typename... Terms>
    struct FunctionN : std::enable_if
375
      <
376
	and_< typename traits::is_valid_arg<Terms>::type... >::value,
377
378
379
	expressions::FunctionN< F, typename traits::to_expr<Terms>::type...> 
      > {};
      
380
381
382
383
384
385
386
    template <typename F, typename Term>
    struct FunctionN<F, Term> : std::enable_if
      <
	traits::is_valid_arg<Term>::value,
	expressions::FunctionN< F, typename traits::to_expr<Term>::type> 
      > {};
      
387
388
389
390
391
392
393
  } // end namespace result_of

  // generator-functions
  // _____________________________________________________________________________

  template<typename F, typename... Terms>
  inline typename result_of::FunctionN<F, Terms...>::type
394
  function_(F&& f, Terms... ts) 
395
396
  {
    return expressions::FunctionN<F, typename traits::to_expr<Terms>::to::type...>
397
	    (std::forward<F>(f), traits::to_expr<Terms>::to::get(ts)...); 
398
399
400
401
  }

  template<typename F, typename... Terms>
  inline typename result_of::FunctionN<F, Terms...>::type
402
  func(F&& f, Terms... ts) 
403
404
  {
    return expressions::FunctionN<F, typename traits::to_expr<Terms>::to::type...>
405
	    (std::forward<F>(f), traits::to_expr<Terms>::to::get(ts)...); 
406
407
408
409
  }

  template<typename F, typename Term0, typename... Terms>
  inline typename result_of::FunctionN<F, Term0, Terms...>::type
410
  eval(F&& f, Term0 t0, Terms... ts) 
411
412
413
  {
    return expressions::FunctionN<F, typename traits::to_expr<Term0>::to::type, 
				     typename traits::to_expr<Terms>::to::type...>
414
      (std::forward<F>(f), traits::to_expr<Term0>::to::get(t0), traits::to_expr<Terms>::to::get(ts)...); 
415
416
  }

417
418
419
420
421
422
423

  // function wrapper for abstract functions
  // _____________________________________________________________________________
  template<typename TOut, typename TIn>
  inline expressions::Wrapper<TOut,TIn> wrap(AbstractFunction<TOut, TIn>* fct) 
  { return expressions::Wrapper<TOut,TIn>(fct); }

424
425
426
427
428
429
430
431
432
433
434
435
436
437
  template <int Degree, class F>
  inline expressions::DegreeWrapper<Degree, F>
  deg(F&& fct)
  {
    return expressions::DegreeWrapper<Degree, F>(std::forward<F>(fct));
  }
  
  template <class F, class DegF>
  inline expressions::DegreeWrapper2<F, DegF>
  deg(F&& fct, DegF&& degfct)
  {
    return expressions::DegreeWrapper2<F, DegF>(std::forward<F>(fct), std::forward<DegF>(degfct));
  }
  
438
439
440
} // end namespace AMDiS

#endif // AMDIS_FUNCTOR_N_EXPRESSION_HPP