functorN_expr.hpp 7.91 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
31
32
33
34
35
36
37
38
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
/******************************************************************************
 *
 * 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"
#include "expressions/functor_expr.hpp"
#include "operations/functors.hpp"

#include <boost/static_assert.hpp>

#include <tuple>
#include <utility> 

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>
69
    struct functor_degree<F, typename enable_if< boost::is_base_of<FunctorBase, F> >::type >
70
71
72
73
74
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
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
    {
      template<typename... Int>
      static int eval(F f, Int... d) { return f.getDegree(d...); }
    };
  }
  

  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);
      
      typedef typename traits::functor_result_type<F>::type value_type;    
      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>()); }
    };
    
  } // end namespace expressions


  namespace result_of
  {
    // result of generator-functions (used for enable_if constructs)
    template<typename F, typename... Terms>
220
    struct FunctionN : enable_if
221
      <
222
	typename and_< typename traits::is_valid_arg<Terms>::type... >::type,
223
224
225
226
227
228
229
230
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
	expressions::FunctionN< F, typename traits::to_expr<Terms>::type...> 
      > {};
      
  } // end namespace result_of


  // generator-functions
  // _____________________________________________________________________________

  template<typename F, typename... Terms>
  inline typename result_of::FunctionN<F, Terms...>::type
  function_(const F& f, Terms... ts) 
  {
    return expressions::FunctionN<F, typename traits::to_expr<Terms>::to::type...>
	    (f, traits::to_expr<Terms>::to::get(ts)...); 
  }

  template<typename F, typename... Terms>
  inline typename result_of::FunctionN<F, Terms...>::type
  func(const F& f, Terms... ts) 
  {
    return expressions::FunctionN<F, typename traits::to_expr<Terms>::to::type...>
	    (f, traits::to_expr<Terms>::to::get(ts)...); 
  }

  template<typename F, typename Term0, typename... Terms>
  inline typename result_of::FunctionN<F, Term0, Terms...>::type
  eval(const F& f, Term0 t0, Terms... ts) 
  {
    return expressions::FunctionN<F, typename traits::to_expr<Term0>::to::type, 
				     typename traits::to_expr<Terms>::to::type...>
      (f, traits::to_expr<Term0>::to::get(t0), traits::to_expr<Terms>::to::get(ts)...); 
  }

} // end namespace AMDiS

#endif // AMDIS_FUNCTOR_N_EXPRESSION_HPP