Functors.h 8.64 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/******************************************************************************
 *
 * 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.
 * 
 ******************************************************************************/
20
21
22
23
24
25
26
27
28
29
30

#ifndef AMDIS_FUNCTORS_H
#define AMDIS_FUNCTORS_H

#include "AbstractFunction.h"

namespace AMDiS {

template<typename T>
struct Id : public AbstractFunction<T,T>
{
31
  Id(int degree = 1) : AbstractFunction<T,T>(degree) {}
32
33
34
35
36
37
  T operator()(const T &v) const { return v; }
};

template<typename T1,typename T2>
struct Const : public AbstractFunction<T1,T2>
{
38
  Const(T1 val_) : AbstractFunction<T1,T2>(0), val(val_) {}
39
40
41
42
43
  T1 operator()(const T2 &v) const { return val; }
private:
  T1 val;
};

44
template<typename T=double>
45
46
struct Factor : public AbstractFunction<T,T>
{
47
  Factor(double fac_, int degree = 1) : AbstractFunction<T,T>(degree), fac(fac_) {}
48
  T operator()(const T& x) const { return fac*x; }
49
50
51
52
private:
  double fac;
};

53
template<typename T=double>
54
55
struct Add : public BinaryAbstractFunction<T,T,T>
{
56
  Add(int degree = 1) : BinaryAbstractFunction<T,T,T>(degree) {}
57
58
59
  T operator()(const T &v1, const T &v2) const { return v1+v2; }
};

60
template<typename T=double>
61
62
struct AddFactor : public BinaryAbstractFunction<T,T,T>
{
63
  AddFactor(double factor_ = 1.0, int degree = 1) : BinaryAbstractFunction<T,T,T>(degree), factor(factor_) {}
64
65
66
67
68
  T operator()(const T &v1, const T &v2) const { return v1 + factor*v2; }
private:
  double factor;
};

69
template<typename T=double>
70
71
struct Subtract : public BinaryAbstractFunction<T,T,T>
{
72
  Subtract(int degree = 1) : BinaryAbstractFunction<T,T,T>(degree) {}
73
74
75
  T operator()(const T &v1, const T &v2) const { return v1-v2; }
};

76
template<typename T=double>
77
78
struct AddScal : public AbstractFunction<T,T>
{
79
  AddScal(T scal_, int degree = 1) : AbstractFunction<T,T>(degree), scal(scal_) {}
80
81
82
83
84
85
86
87
  T operator()(const T &v) const { return v+scal; }
private:
  T scal;
};

template<typename T>
struct Mult : public BinaryAbstractFunction<T,T,T>
{
88
  Mult(int degree = 2) : BinaryAbstractFunction<T,T,T>(degree) {}
89
90
91
92
93
94
  T operator()(const T &v1, const T &v2) const { return v1*v2; }
};

template<typename T1, typename T2, typename T3>
struct Mult2 : public BinaryAbstractFunction<T1,T2,T3>
{
95
  Mult2(int degree = 2) : BinaryAbstractFunction<T1,T2,T3>(degree) {}
96
97
98
99
  T1 operator()(const T2 &v1, const T3 &v2) const { return v1*v2; }
};

template<typename T>
Praetorius, Simon's avatar
Praetorius, Simon committed
100
struct MultScal : public BinaryAbstractFunction<T,T,T>
101
{
102
  MultScal(T scal_, int degree = 2) : BinaryAbstractFunction<T,T,T>(degree), scal(scal_) {}
Praetorius, Simon's avatar
Praetorius, Simon committed
103
  T operator()(const T &v1, const T &v2) const { return v1*v2*scal; }
104
105
106
107
108
109
110
private:
  T scal;
};

template<typename T>
struct Max : public BinaryAbstractFunction<T,T,T>
{
111
  Max(int degree = 1) : BinaryAbstractFunction<T,T,T>(degree) {}
112
113
114
115
116
117
  T operator()(const T &v1, const T &v2) const { return std::max(v1,v2); }
};

template<typename T>
struct Min : public BinaryAbstractFunction<T,T,T>
{
118
  Min(int degree = 1) : BinaryAbstractFunction<T,T,T>(degree) {}
119
120
121
122
123
124
  T operator()(const T &v1, const T &v2) const { return std::min(v1,v2); }
};

template<typename T>
struct Diff : public BinaryAbstractFunction<T,T,T>
{
125
  Diff(int degree = 1) : BinaryAbstractFunction<T,T,T>(degree) {}
126
127
128
  T operator()(const T &v1, const T &v2) const { return abs(v1-v2); }
};

129
template<typename T=double>
130
131
struct Abs : public AbstractFunction<T,T>
{
132
  Abs(int degree = 1) : AbstractFunction<T,T>(degree) {}
133
134
135
  T operator()(const T &v) const { return abs(v); }
};

136
template<typename T=double>
137
138
struct Signum : public AbstractFunction<T,T>
{
139
  Signum() : AbstractFunction<T,T>(0) {}
140
141
142
  T operator()(const T &v) const { return (v>0.0?1.0:(v<0.0?-1.0:0.0)); }
};

143
template<typename T=double>
144
145
struct Sqr : public AbstractFunction<T,T>
{
146
  Sqr(int degree = 2) : AbstractFunction<T, T>(degree) {}
147
148
149
  T operator()(const T &v) const { return sqr(v); }
};

150
template<typename T=double>
151
152
struct Sqrt : public AbstractFunction<T,T>
{
153
  Sqrt(int degree = 4) : AbstractFunction<T,T>(degree) {}
154
155
156
  T operator()(const T &v) const { return sqrt(v); }
};

157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
namespace detail {
  template<int p>
  struct Pow
  {
    template<typename T>
    static T eval(const T& v) { return v*Pow<p-1>::eval(v); }
  };
  
  template<>
  struct Pow<1> {
    template<typename T>
    static T eval(const T& v) { return v; }
  };
  
  template<>
  struct Pow<0> {
    template<typename T>
    static T eval(const T& v) { return 1.0; }
  };
}

template<int p, typename T=double>
179
180
struct Pow : public AbstractFunction<T,T>
{
181
  Pow(double factor_=1.0, int degree = p) : AbstractFunction<T,T>(degree), factor(factor_) {}
182
  T operator()(const T &v) const { return factor * detail::Pow<p>::eval(v); }
183
private:
184
  double factor;
185
186
};

Praetorius, Simon's avatar
Praetorius, Simon committed
187
188
template<typename T1, typename T2 = ProductType<T1, T1> >
struct Norm2 : public AbstractFunction<T1, T2>
189
{
190
  Norm2(int degree = 4) : AbstractFunction<T1, T2>(degree) {}
191
192
193
194
  T1 operator()(const T2 &v) const { return sqrt(v*v); }
};

template<typename T1, typename T2>
Praetorius, Simon's avatar
Praetorius, Simon committed
195
struct Norm2Sqr : public AbstractFunction<T1, T2>
196
{
197
  Norm2Sqr(int degree = 2) : AbstractFunction<T1, T2>(degree) {}
198
199
200
201
202
203
  T1 operator()(const T2 &v) const { return v*v; }
};

template<typename T>
struct Norm2_comp2 : public BinaryAbstractFunction<T,T,T>
{
204
  Norm2_comp2(int degree = 4) : BinaryAbstractFunction<T,T,T>(degree) {}
205
206
207
208
209
210
  T operator()(const T &v1, const T &v2) const { return sqrt(sqr(v1)+sqr(v2)); }
};

template<typename T>
struct Norm2Sqr_comp2 : public BinaryAbstractFunction<T,T,T>
{
211
  Norm2Sqr_comp2(int degree = 2) : BinaryAbstractFunction<T,T,T>(degree) {}
212
213
214
215
216
217
  T operator()(const T &v1, const T &v2) const { return sqr(v1)+sqr(v2); }
};

template<typename T>
struct Norm2_comp3 : public TertiaryAbstractFunction<T,T,T,T>
{
218
  Norm2_comp3(int degree = 4) : TertiaryAbstractFunction<T,T,T,T>(degree) {}
219
220
221
222
223
224
  T operator()(const T &v1, const T &v2, const T &v3) const { return sqrt(sqr(v1)+sqr(v2)+sqr(v3)); }
};

template<typename T>
struct Norm2Sqr_comp3 : public TertiaryAbstractFunction<T,T,T,T>
{
225
  Norm2Sqr_comp3(int degree = 2) : TertiaryAbstractFunction<T,T,T,T>(degree) {}
226
227
228
  T operator()(const T &v1, const T &v2, const T &v3) const { return sqr(v1)+sqr(v2)+sqr(v3); }
};

229
230
231
232
233
234
235
236
237
238
239
240
template<typename T>
struct L1Diff : public BinaryAbstractFunction<T,T,T>
{
  T operator()(const T &v1, const T &v2) const { return abs(v1-v2); }
};

template<typename TOut, typename T=TOut>
struct L2Diff : public BinaryAbstractFunction<TOut,T,T>
{
  TOut operator()(const T &v1, const T &v2) const { return Norm2<TOut, T>()(v1-v2); }
};

241
242
243
244
245
246
247
248
249
template<typename T>
struct Vec1WorldVec : public AbstractFunction<WorldVector<T>,T>
{
  WorldVector<T> operator()(const T &v0) const {
    WorldVector<T> result;
    result[0]=v0;
    return result;
  }
};
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
template<typename T>
struct Vec2WorldVec : public BinaryAbstractFunction<WorldVector<T>,T,T>
{
  WorldVector<T> operator()(const T &v0, const T &v1) const {
    WorldVector<T> result;
    result[0]=v0; result[1]=v1;
    return result;
  }
};
template<typename T>
struct Vec3WorldVec : public TertiaryAbstractFunction<WorldVector<T>,T,T,T>
{
  WorldVector<T> operator()(const T &v0, const T &v1, const T &v2) const {
    WorldVector<T> result;
    result[0]=v0; result[1]=v1; result[2]=v2;
    return result;
  }
};
268
269
template<int c, typename T=double>
struct Component : public AbstractFunction<T, WorldVector<T> >
270
{
271
  Component(int degree = 1) : AbstractFunction<T, WorldVector<T> >(degree) {}
272
273
  T operator()(const WorldVector<T> &x) const {
    return x[c];
274
275
  }
};
276
277
278
279
280
281
282
283
284
285
template<typename T=double>
struct Component2 : public AbstractFunction<T, WorldVector<T> >
{
  Component2(int comp, int degree = 1) : AbstractFunction<T, WorldVector<T> >(degree), c_(comp) {}
  T operator()(const WorldVector<T> &x) const {
    return x[c_];
  }
private:
  int c_;
};
286

287
288
289
290
291
292
struct FadeOut : public TertiaryAbstractFunction<double, double, double ,double>
{
  double operator()(const double &v, const double &dist, const double &mean) const {
    return dist*mean+(1.0-dist)*v;
  }
};
293
294
295
296
297

struct Random : public AbstractFunction<double, WorldVector<double> >
{
  Random(double mean_, double amplitude_) : mean(mean_), amplitude(amplitude_)
  {
298
    std::srand(time(0));
299
300
301
302
  }
  
  double operator()(const WorldVector<double> &x) const
  {
303
    return mean + 2.0*amplitude * ((std::rand() / static_cast<double>(RAND_MAX)) - 0.5);
304
305
306
307
308
309
310
  }
  
private:
  double mean;
  double amplitude;
};

311
312
313
314
}

#endif // AMDIS_FUNCTORS_H