functorN_expr.hpp 11 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    
    // 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
  {    
    /// 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;
    };
    
    template<class T>
    typename Function<T>::type function_helper(T);

    /// extract result type from functors
    template <class F>
    struct Functor
    {
      typedef decltype(function_helper(&F::operator())) type;
    };
    
    template <class R, class... As>
    struct Functor<std::function<R(As...)> >
    {
      typedef R type;
    };
    
  } // end namespace result_of
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  

  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)
  template<typename... Terms>
  struct LazyOperatorTerms : public LazyOperatorTermBase
  {
    std::tuple<Terms...> term_tuple;
    
    template<typename... Terms_>
    LazyOperatorTerms(Terms_... terms_)
    : term_tuple(terms_...) {}
    
    template<typename List>
    inline void insertFeSpaces(List& feSpaces)
    {
      for_each(term_tuple, detail::InsertFeSpaces<List>(feSpaces));
    }

    template<typename OT>
    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));
    }

    template<typename OT>
    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
    template<typename F, typename... Terms>
    struct FunctionN : public LazyOperatorTerms<Terms...>
    {
      typedef LazyOperatorTerms<Terms...> super;
      static const int N = sizeof...(Terms);
      
230
      typedef typename result_of::Functor<F>::type value_type;
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
      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
      
      template<typename... Terms_>
      FunctionN(const F& f_, Terms_... terms_)
	: super(terms_...), f(f_) {}
      
      // call f.getDegree() function    
      template<int I, typename... Terms_>
      int getDegree(int_<I>, const Terms_&... terms) const
      {
	return getDegree(int_<I-1>(), std::get<I-1>(super::term_tuple), terms...);
      }
      
      template<typename... Terms_>
      int getDegree(int_<0>, const Terms_&... terms) const
      {
	return traits::functor_degree<F>::eval(f, terms.getDegree()...);
      }
      
      int getDegree() const
      {
	return getDegree(int_<N>());
      }

      // call f.operator()(...)
      template<int I, typename... Terms_>
      inline value_type eval(const int& iq, int_<I>, const Terms_&... terms) const
      {
	return eval(iq, int_<I-1>(), std::get<I-1>(super::term_tuple), terms...);
      }
      
      template<typename... Terms_>
      inline value_type eval(const int& iq, int_<0>, Terms_... terms) const
      {
	return f(terms(iq)...);  // f(term1(iq), term2(iq), term3(iq),...)
      }
      
      inline value_type operator()(const int& iq) const { return eval(iq, int_<N>()); }
    };
    
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
    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
    template<typename TOut, typename TIn>
    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;
    };
    
303
304
305
306
307
308
  } // end namespace expressions


  namespace result_of
  {
    // result of generator-functions (used for enable_if constructs)
309
310
    template <typename F, typename... Terms>
    struct FunctionN : std::enable_if
311
      <
312
	and_< typename traits::is_valid_arg<Terms>::type... >::value,
313
314
315
	expressions::FunctionN< F, typename traits::to_expr<Terms>::type...> 
      > {};
      
316
317
318
319
320
321
322
    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> 
      > {};
      
323
324
325
326
327
328
329
  } // end namespace result_of

  // generator-functions
  // _____________________________________________________________________________

  template<typename F, typename... Terms>
  inline typename result_of::FunctionN<F, Terms...>::type
330
  function_(F&& f, Terms... ts) 
331
332
  {
    return expressions::FunctionN<F, typename traits::to_expr<Terms>::to::type...>
333
	    (std::forward<F>(f), traits::to_expr<Terms>::to::get(ts)...); 
334
335
336
337
  }

  template<typename F, typename... Terms>
  inline typename result_of::FunctionN<F, Terms...>::type
338
  func(F&& f, Terms... ts) 
339
340
  {
    return expressions::FunctionN<F, typename traits::to_expr<Terms>::to::type...>
341
	    (std::forward<F>(f), traits::to_expr<Terms>::to::get(ts)...); 
342
343
344
345
  }

  template<typename F, typename Term0, typename... Terms>
  inline typename result_of::FunctionN<F, Term0, Terms...>::type
346
  eval(F&& f, Term0 t0, Terms... ts) 
347
348
349
  {
    return expressions::FunctionN<F, typename traits::to_expr<Term0>::to::type, 
				     typename traits::to_expr<Terms>::to::type...>
350
      (std::forward<F>(f), traits::to_expr<Term0>::to::get(t0), traits::to_expr<Terms>::to::get(ts)...); 
351
352
  }

353
354
355
356
357
358
359

  // 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); }

360
361
362
} // end namespace AMDiS

#endif // AMDIS_FUNCTOR_N_EXPRESSION_HPP