Expressions.h 28.2 KB
Newer Older
1
2
3
4
5
6
7
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
8
 * Authors:
9
10
11
12
13
14
15
16
17
 * 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.
18
 *
19
20
21
22
 ******************************************************************************/



23
/** \file Expressions.h */
24

Praetorius, Simon's avatar
Praetorius, Simon committed
25
26
#ifndef AMDIS_EXPRESSIONS_BASE_H
#define AMDIS_EXPRESSIONS_BASE_H
27
28
29
30

#include "AMDiS_fwd.h"
#include "OperatorTerm.h"
#include "Functors.h"
31
#include "MatrixVectorOperations.h"
32
33

#include <boost/static_assert.hpp>
34
#include <boost/type_traits.hpp>
35
36

#include "expressions/LazyOperatorTerm.h"
37
38
#include "expressions/expressions.h"

39

40
/** Expressions provide an easy way of automated generation of
41
42
 * 'arbitrary' operator-terms out of some elementary operations, by using a
 * recursive definition of the term. All necessary data will be initialized
43
44
45
 * when an expression as part of the term uses this data.
 * Since no virtual functions, like in the AbstractFunction classes, are used
 * the overhead of a vtable is removed.
46
 *
47
 * usage:
48
 * addZOT(Operator, Term)
49
 *  ... add a zeroOrderTerm to Operator, i.e. (Term(x) * u, v)
50
51
52
 *
 * addFOT(Operator, Term, FirstOrderType)
 *  ... add a firstOrderTerm to Operator, (if Term::value_type = double)
53
54
55
56
57
 *      i.e. (Term(x) * 1 * grad(u), v), rsp. (Term(x) * 1 * u, grad(v))
 * addFOT(Operator, Term, FirstOrderType)
 *  ... add a firstOrderTerm to Operator, (if Term::value_type = WorldVector)
 *      i.e. (Term(x) * b * grad(u), v), rsp. (Term(x) * u, grad(v))
 * addFOT<I>(Operator, Term, FirstOrderType)
58
 *  ... add a firstOrderTerm to Operator,
59
 *      i.e. (Term(x) * e_I * grad(u), v), rsp. (Term(x) * e_I * u, grad(v))
60
 *
61
62
63
64
 * addSOT(Operator, Term)
 *  ... add a secondOrderTerm to Operator, i.e. (Term(x) * grad(u), grad(v))
 * addSOT<I,J>(Operator, Term)
 *  ... add a secondOrderTerm to Operator, i.e. (E_IJ * Term(x) * grad(u), grad(v))
65
 *
66
67
68
69
70
71
72
73
74
75
76
77
 * where Operator is eather a pointer or reference, FirstOrderType in {GRD_PHI, GRD_PSI}
 * and Term a componation of elementary terms by + - * /
 *  - constant(value) / value  ... a constant value
 *  - valueOf(DOFVector)       ... values of a DOFVector at QP
 *  - gradientOf(DOFVector)    ... gradient of a DOFVector at QP
 *  - derivative<I>(DOFVector) ... I'th partial derivative
 *  - X()                      ... coordinate at quadrature points
 *  - pow<I>(Term)             ... I'th power of a term
 *  - sqrt(Term)               ... square root of a term
 *  - Exp(Term)                ... exponential function of a term
 *  - function_<F>(Term)       ... evaluates F()(Term(iq))
 *  - function_(F f, Term)     ... evaluates f(Term(iq))
78
79
 *
 *
80
 * with F a functor that implements
81
 *   typedef (...) result_type;
82
 *   int getDegree(int d0);
83
 *   result_type operator()(const T0& v0) const;
84
 *
85
86
 * respective
 *   int getDegree(int d0, int d1);
87
 *   result_type operator()(const T0& v0, const T1& v1) const;
88
 *
89
90
 * respective
 *   int getDegree(int d0, int d1, int d2);
91
 *   result_type operator()(const T0& v0, const T1& v1, const T2& v2) const;
92
 *
93
94
95
96
97
 * where the d0, d1, d2 give the polynomial degrees of the v0, v1, v2 terms.
 * */

namespace AMDiS {

98
/// helper class to adopt the correct OperatorTerm based on the term order
99
100
template<int Order>
struct GetTerm {
101
102
  typedef typename boost::mpl::if_c<Order == 0, ZeroOrderTerm,
	  typename boost::mpl::if_c<Order == 1, FirstOrderTerm,
103
104
105
106
	  typename boost::mpl::if_c<Order == 2, SecondOrderTerm,
						OperatorTerm
	  >::type >::type >::type type;
};
107

108
/// basic interface for OperatorTerms based on expressions
109
110
template<typename Term, int Order = -1>
struct GenericOperatorTerm : public GetTerm<Order>::type
111
{
112
  typedef typename GetTerm<Order>::type super;
113

114
  /// Expression term stored as copy
115
  Term term;
116

117
118
  /// constructor
  /// adds all feSpaces provided by the expression term to auxFeSpaces liste
119
  GenericOperatorTerm(const Term& term_)
120
    : super(term_.getDegree()), term(term_)
121
  {
122
    term.insertFeSpaces(this->auxFeSpaces);
123
124
125
#ifndef NDEBUG
    test_auxFeSpaces(this->auxFeSpaces);
#endif
126
127
  }

128
  /// calls initElement() for \ref term
129
  void initElement(const ElInfo* elInfo,
130
131
		   SubAssembler* subAssembler,
		   Quadrature *quad)
132
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
133
    term.initElement(this, elInfo, subAssembler, quad, NULL);
134
135
  }

136
  /// calls initElement() for \ref term
137
  void initElement(const ElInfo* smallElInfo,
138
139
140
		   const ElInfo* largeElInfo,
		   SubAssembler* subAssembler,
		   Quadrature *quad)
141
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
142
    term.initElement(this, smallElInfo, largeElInfo, subAssembler, quad, NULL);
143
  }
144

145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
  /// test for only one mesh allowed in expressions
  template<typename FeSpaceList>
  void test_auxFeSpaces(FeSpaceList const& auxFeSpaces)
  {
    typedef typename FeSpaceList::const_iterator fe_iter;
    if (auxFeSpaces.size() > 0) {
      Mesh* mesh0 = (*auxFeSpaces.begin())->getMesh();
      for (fe_iter it = auxFeSpaces.begin(); it != auxFeSpaces.end(); it++) {
	if ((*it)->getMesh() != mesh0) {
	  ERROR_EXIT("Only one mesh allowed in expression.\n");
	}
      }
    }
  }
};
160
161
162
163
164
165

template<typename Term>
struct GenericOperatorTerm<Term, -1> : public GenericOperatorTerm<Term, -2>
{
  typedef GenericOperatorTerm<Term, -2> super;
  GenericOperatorTerm(const Term& term_) : super(term_) { }
166

167
168
169
170
171
172
173
  void eval(int nPoints,
	    const mtl::dense_vector<double>& uhAtQP,
	    const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
	    const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
	    mtl::dense_vector<double>& result,
	    double factor) {};
};
174

175
// _______ ZeroOrderTerms ______________________________________________________
176

177
178
179
180
181
182
template<typename Term>
struct GenericZeroOrderTerm : public GenericOperatorTerm<Term, 0>
{
  GenericZeroOrderTerm(const Term& term_)
    : GenericOperatorTerm<Term,0>(term_)
  { }
183
184
185
186

  void getC(const ElInfo *elInfo, int nPoints, ElementVector& C)
  {
    for (int iq = 0; iq < nPoints; iq++)
187
      C[iq] += this->term(iq);
188
189
190
191
192
193
194
195
196
197
  }

  void eval( int nPoints,
	      const mtl::dense_vector<double>& uhAtQP,
	      const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
	      const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
	      mtl::dense_vector<double>& result,
	      double fac)
  {
    for (int iq = 0; iq < nPoints; iq++)
198
      result[iq] += fac * this->term(iq) * uhAtQP[iq];
199
200
201
202
203
204
205
  }
};

// _______ FirstOrderTerms _____________________________________________________


template<typename Term>
206
struct GenericFirstOrderTerm_1 : public GenericOperatorTerm<Term, 1>
207
208
{
  GenericFirstOrderTerm_1(const Term& term_)
209
    : GenericOperatorTerm<Term, 1>(term_)
210
  { }
211
212
213
214
215
216
217
218
219

  /// Implements FirstOrderTerm::getLb().
  void getLb(const ElInfo *elInfo,
	     std::vector<mtl::dense_vector<double> >& Lb) const
  {
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
    const int nPoints = static_cast<int>(Lb.size());

    for (int iq = 0; iq < nPoints; iq++)
220
      this->l1(grdLambda, Lb[iq], this->term(iq));
221
222
223
224
225
  }
};


template<int I, typename Term>
226
struct GenericFirstOrderTerm_i : public GenericOperatorTerm<Term, 1>
227
228
{
  GenericFirstOrderTerm_i(const Term& term_)
229
    : GenericOperatorTerm<Term, 1>(term_)
230
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
231
    this->FirstOrderTerm::bOne = I;
232
  }
233

234
  GenericFirstOrderTerm_i(const Term& term_, int I0)
235
    : GenericOperatorTerm<Term, 1>(term_)
236
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
237
    this->FirstOrderTerm::bOne = I0;
238
239
240
241
242
243
244
245
246
247
248
    TEST_EXIT_DBG( I < 0 && I0 >= 0 )("You yould specify eather template<int I>, or constructor(int I0)\n");
  }

  /// Implements FirstOrderTerm::getLb().
  void getLb(const ElInfo *elInfo,
	     std::vector<mtl::dense_vector<double> >& Lb) const
  {
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
    const int nPoints = static_cast<int>(Lb.size());

    for (int iq = 0; iq < nPoints; iq++)
249
      this->lb_one(grdLambda, Lb[iq], this->term(iq));
250
251
252
253
254
  }
};


template<typename Term>
255
struct GenericFirstOrderTerm_b : public GenericOperatorTerm<Term, 1>
256
257
{
  GenericFirstOrderTerm_b(const Term& term_)
258
259
    : GenericOperatorTerm<Term, 1>(term_)
  { }
260
261
262
263
264
265
266
267
268

  /// Implements FirstOrderTerm::getLb().
  void getLb(const ElInfo *elInfo,
	     std::vector<mtl::dense_vector<double> >& Lb) const
  {
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
    const int nPoints = static_cast<int>(Lb.size());

    for (int iq = 0; iq < nPoints; iq++)
269
      this->lb(grdLambda, this->term(iq), Lb[iq], 1.0);
270
271
272
273
274
275
276
  }
};

// _______ SecondOrderTerms ____________________________________________________


template<typename Term>
277
struct GenericSecondOrderTerm_1 : public GenericOperatorTerm<Term, 2>
278
279
{
  GenericSecondOrderTerm_1(const Term& term_)
280
    : GenericOperatorTerm<Term, 2>(term_)
281
  {
282
    this->setSymmetric(true);
283
  }
284

285
286
287
288
289
290
  /// Implements SecondOrderTerm::getLALt().
  void getLALt(const ElInfo *elInfo, std::vector<mtl::dense2D<double> > &LALt) const
  {
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
    const int nPoints = static_cast<int>(LALt.size());

291
    for (int iq = 0; iq < nPoints; iq++)
292
      this->l1lt(grdLambda, LALt[iq], this->term(iq));
293
294
  }

295
  /// Implemetation of SecondOrderTerm::eval().
296
297
298
299
300
  void eval(int nPoints,
	    const mtl::dense_vector<double>& uhAtQP,
	    const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
	    const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
	    mtl::dense_vector<double>& result,
301
	    double f)
302
303
304
305
306
307
308
309
310
  {
    int dow = Global::getGeo(WORLD);

    if (num_rows(D2UhAtQP) > 0) {
      for (int iq = 0; iq < nPoints; iq++) {
	double resultQP = 0.0;
	for (int i = 0; i < dow; i++) {
	  resultQP += D2UhAtQP[iq][i][i];
	}
311
	result[iq] += resultQP * f * this->term(iq);
312
313
314
315
      }
    }
  }

316
  /// Implemetation of SecondOrderTerm::weakEval().
317
  void weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
318
		std::vector<WorldVector<double> > &result)
319
320
321
  {
    int nPoints = grdUhAtQP.size();
    for (int iq = 0; iq < nPoints; iq++)
322
      axpy(this->term(iq), grdUhAtQP[iq], result[iq]);
323
324
325
326
327
  }
};



328
template<typename Term, bool symmetric = false>
329
struct GenericSecondOrderTerm_A : public GenericOperatorTerm<Term, 2>
330
{
331
  GenericSecondOrderTerm_A(const Term& term_)
332
    : GenericOperatorTerm<Term, 2>(term_)
333
  {
334
    this->setSymmetric(symmetric);
335
  }
336
337

  void getLALt(const ElInfo *elInfo,
338
339
340
341
342
343
	       std::vector<mtl::dense2D<double> > &LALt) const
  {
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
    const int nPoints = static_cast<int>(LALt.size());

    for (int iq = 0; iq < nPoints; iq++)
344
      this->lalt(grdLambda, this->term(iq), LALt[iq], symmetric, 1.0);
345
346
347
348
349
350
351
  }

  void eval(int nPoints,
	    const mtl::dense_vector<double>& uhAtQP,
	    const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
	    const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
	    mtl::dense_vector<double>& result,
352
	    double factor)
353
354
355
356
357
358
  {
    int dow = Global::getGeo(WORLD);

    for (int iq = 0; iq < nPoints; iq++) {
      double resultQP = 0.0;

359
      WorldMatrix<double> A = this->term(iq);
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378

      if (num_rows(D2UhAtQP) > 0)
	for (int i = 0; i < dow; i++)
	  for (int j = 0; j < dow; j++)
	    resultQP += A[i][j] * D2UhAtQP[iq][j][i];

//       if (num_rows(grdUhAtQP) > 0)
// 	resultQP += (*divFct)(A) * grdUhAtQP[iq];

      result[iq] += resultQP * factor;
    }
  }

  void weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
		std::vector<WorldVector<double> > &result)
  {
    int nPoints = grdUhAtQP.size();
    WorldMatrix<double> A;
    for (int iq = 0; iq < nPoints; iq++) {
379
      result[iq] += this->term(iq) * grdUhAtQP[iq];
380
    }
381
382
383
384
385
386
  }
};



template<int I, int J, typename Term>
387
struct GenericSecondOrderTerm_ij : public GenericOperatorTerm<Term, 2>
388
389
{
  int row, col;
390

391
  GenericSecondOrderTerm_ij(const Term& term_)
392
    : GenericOperatorTerm<Term, 2>(term_), row(I), col(J)
393
  {
394
    this->setSymmetric(row == col);
395
  }
396

397
  GenericSecondOrderTerm_ij(const Term& term_, int I0, int J0)
398
    : GenericOperatorTerm<Term, 2>(term_), row(I0), col(J0)
399
  {
400
    this->setSymmetric(row == col);
401
    TEST_EXIT_DBG( I < 0 && I0 >= 0 && J < 0 && J0 >= 0 )
402
403
404
      ("You yould specify eather template<int I, int J>, or constructor(int I0, int J0)\n");
  }

405
  void getLALt(const ElInfo *elInfo,
406
407
408
409
410
411
	       std::vector<mtl::dense2D<double> > &LALt) const
  {
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
    const int nPoints = static_cast<int>(LALt.size());

    for (int iq = 0; iq < nPoints; iq++)
412
      this->lalt_kl(grdLambda, row, col, LALt[iq], this->term(iq));
413
  }
414
415
416
417
418
419

  void eval(int nPoints,
	    const mtl::dense_vector<double>& uhAtQP,
	    const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
	    const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
	    mtl::dense_vector<double>& result,
420
	    double fac)
421
422
423
  {
    if (num_rows(D2UhAtQP) > 0) {
      for (int iq = 0; iq < nPoints; iq++)
424
	result[iq] += D2UhAtQP[iq][row][col] * this->term(iq) * fac;
425
426
427
428
    }
  }

  void weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
429
				std::vector<WorldVector<double> > &result)
430
  {
431
    int nPoints = (int)grdUhAtQP.size();
432
    for (int iq = 0; iq < nPoints; iq++)
433
      result[iq][row] += grdUhAtQP[iq][col] * this->term(iq);
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
  }
};


// _____________________________________________________________________________

template <typename Term>
void addZOT(Operator* op, const Term& term)
{
  op->addZeroOrderTerm(new GenericZeroOrderTerm<Term>(term));
}

template <typename Term>
void addZOT(Operator& op, const Term& term)
{
  op.addZeroOrderTerm(new GenericZeroOrderTerm<Term>(term));
}

452
453
454
455
456
457
458
459
460
461
inline void addZOT(Operator* op, double term)
{
  addZOT(op, constant(term));
}

inline void addZOT(Operator& op, double term)
{
  addZOT(op, constant(term));
}

462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// _____________________________________________________________________________

// first order term using FirstOrderTerm::l1
template <typename Term>
inline typename boost::enable_if< typename boost::is_same<double, typename Term::value_type>::type >::type
addFOT(Operator* op, const Term& term, FirstOrderType type)
{
  op->addFirstOrderTerm(new GenericFirstOrderTerm_1<Term>(term), type);
}

template <typename Term>
inline typename boost::enable_if< typename boost::is_same<double, typename Term::value_type>::type >::type
addFOT(Operator& op, const Term& term, FirstOrderType type)
{
  op.addFirstOrderTerm(new GenericFirstOrderTerm_1<Term>(term), type);
}

479
480
481
482
483
484
485
486
487
488
inline void addFOT(Operator* op, double term, FirstOrderType type)
{
  addFOT(op, constant(term), type);
}

inline void addFOT(Operator& op, double term, FirstOrderType type)
{
  addFOT(op, constant(term), type);
}

489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// first order term using FirstOrderTerm::lb_one
template <int I, typename Term>
inline typename boost::enable_if< typename boost::is_same<double, typename Term::value_type>::type >::type
addFOT(Operator* op, const Term& term, FirstOrderType type)
{
  op->addFirstOrderTerm(new GenericFirstOrderTerm_i<I,Term>(term), type);
}

template <int I, typename Term>
inline typename boost::enable_if< typename boost::is_same<double, typename Term::value_type>::type >::type
addFOT(Operator& op, const Term& term, FirstOrderType type)
{
  op.addFirstOrderTerm(new GenericFirstOrderTerm_i<I,Term>(term), type);
}

504
505
506
507
508
509
510
511
512
513
514
515
template <int I>
inline void addFOT(Operator* op, double term, FirstOrderType type)
{
  addFOT<I>(op, constant(term), type);
}

template <int I>
inline void addFOT(Operator& op, double term, FirstOrderType type)
{
  addFOT<I>(op, constant(term), type);
}

516
517
518
519
520
521
522
523
524
525
526
527
528
529
template <typename Term>
inline typename boost::enable_if< typename boost::is_same<double, typename Term::value_type>::type >::type
addFOT(Operator* op, const Term& term, int I, FirstOrderType type)
{
  op->addFirstOrderTerm(new GenericFirstOrderTerm_i<-1,Term>(term,I), type);
}

template <typename Term>
inline typename boost::enable_if< typename boost::is_same<double, typename Term::value_type>::type >::type
addFOT(Operator& op, const Term& term, int I, FirstOrderType type)
{
  op.addFirstOrderTerm(new GenericFirstOrderTerm_i<-1,Term>(term,I), type);
}

530
531
532
533
534
535
536
537
538
539
inline void addFOT(Operator* op, double term, int I, FirstOrderType type)
{
  addFOT(op, constant(term), I, type);
}

inline void addFOT(Operator& op, double term, int I, FirstOrderType type)
{
  addFOT(op, constant(term), I, type);
}

540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
// first order term using FirstOrderTerm::lb
template <typename Term>
inline typename boost::enable_if< typename boost::is_same<WorldVector<double>, typename Term::value_type>::type >::type
addFOT(Operator* op, const Term& term, FirstOrderType type)
{
  op->addFirstOrderTerm(new GenericFirstOrderTerm_b<Term>(term), type);
}

template <typename Term>
inline typename boost::enable_if< typename boost::is_same<WorldVector<double>, typename Term::value_type>::type >::type
addFOT(Operator& op, const Term& term, FirstOrderType type)
{
  op.addFirstOrderTerm(new GenericFirstOrderTerm_b<Term>(term), type);
}

555
556
557
558
559
560
561
562
563
564
inline void addFOT(Operator* op, WorldVector<double> term, FirstOrderType type)
{
  addFOT(op, constant(term), type);
}

inline void addFOT(Operator& op, WorldVector<double> term, FirstOrderType type)
{
  addFOT(op, constant(term), type);
}

565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
// _____________________________________________________________________________

// second order term using matrix functions
template <typename Term>
inline typename boost::enable_if< typename boost::is_same<WorldMatrix<double>, typename Term::value_type>::type >::type
addSOT(Operator* op, const Term& term)
{
  op->addSecondOrderTerm(new GenericSecondOrderTerm_A<Term>(term));
}
template <typename Term>
inline typename boost::enable_if< typename boost::is_same<WorldMatrix<double>, typename Term::value_type>::type >::type
addSOT(Operator& op, const Term& term)
{
  op.addSecondOrderTerm(new GenericSecondOrderTerm_A<Term>(term));
}

581
582
583
584
585
586
587
588
589
590
inline void addSOT(Operator* op, WorldMatrix<double> term)
{
  addSOT(op, constant(term));
}

inline void addSOT(Operator& op, WorldMatrix<double> term)
{
  addSOT(op, constant(term));
}

591
592
593
594
595
596
597
598
599
600
601
602
603
604
template <bool Symmetric, typename Term>
inline typename boost::enable_if< typename boost::is_same<WorldMatrix<double>, typename Term::value_type>::type >::type
addSOT(Operator* op, const Term& term)
{
  op->addSecondOrderTerm(new GenericSecondOrderTerm_A<Term, Symmetric>(term));
}

template <bool Symmetric, typename Term>
inline typename boost::enable_if< typename boost::is_same<WorldMatrix<double>, typename Term::value_type>::type >::type
addSOT(Operator& op, const Term& term)
{
  op.addSecondOrderTerm(new GenericSecondOrderTerm_A<Term, Symmetric>(term));
}

605
606
607
608
609
610
611
612
613
614
615
616
template <bool Symmetric>
inline void addSOT(Operator* op, WorldMatrix<double> term)
{
  addSOT<Symmetric>(op, constant(term));
}

template <bool Symmetric>
inline void addSOT(Operator& op, WorldMatrix<double> term)
{
  addSOT<Symmetric>(op, constant(term));
}

617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
// second order term using scalar functions with identity matrix
template <typename Term>
inline typename boost::enable_if< typename boost::is_same<double, typename Term::value_type>::type >::type
addSOT(Operator* op, const Term& term)
{
  op->addSecondOrderTerm(new GenericSecondOrderTerm_1<Term>(term));
}

template <typename Term>
inline typename boost::enable_if< typename boost::is_same<double, typename Term::value_type>::type >::type
addSOT(Operator& op, const Term& term)
{
  op.addSecondOrderTerm(new GenericSecondOrderTerm_1<Term>(term));
}

632
633
634
635
636
637
638
639
640
641
inline void addSOT(Operator* op, double term)
{
  addSOT(op, constant(term));
}

inline void addSOT(Operator& op, double term)
{
  addSOT(op, constant(term));
}

642
643
644
645
646
647
648
649
650
651
652
653
654
// second order term using matrix=0 with matrix_ij = function value
template <int I, int J, typename Term>
void addSOT(Operator* op, const Term& term)
{
  op->addSecondOrderTerm(new GenericSecondOrderTerm_ij<I,J,Term>(term));
}

template <int I, int J, typename Term>
void addSOT(Operator& op, const Term& term)
{
  op.addSecondOrderTerm(new GenericSecondOrderTerm_ij<I,J,Term>(term));
}

655
656
657
658
659
660
661
662
663
664
665
template <int I, int J>
inline void addSOT(Operator* op, double term)
{
  addSOT<I,J>(op, constant(term));
}
template <int I, int J>
inline void addSOT(Operator& op, double term)
{
  addSOT<I,J>(op, constant(term));
}

666
667
668
669
670
671
672
673
674
675
676
677
template <typename Term>
void addSOT(Operator* op, const Term& term, int I, int J)
{
  op->addSecondOrderTerm(new GenericSecondOrderTerm_ij<-1,-1,Term>(term,I,J));
}

template <typename Term>
void addSOT(Operator& op, const Term& term, int I, int J)
{
  op.addSecondOrderTerm(new GenericSecondOrderTerm_ij<-1,-1,Term>(term,I,J));
}

678
679
680
681
682
683
684
685
686
687
inline void addSOT(Operator* op, double term, int I, int J)
{
  addSOT(op, constant(term), I, J);
}

inline void addSOT(Operator& op, double term, int I, int J)
{
  addSOT(op, constant(term), I, J);
}

688

689
#if AMDIS_HAS_CXX11
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762

template <class Term, class Result = void>
using ScalarTerm = typename boost::enable_if< boost::is_same<double, typename Term::value_type>, Result >::type;

template <class Term, class Result = void>
using VectorTerm = typename boost::enable_if< boost::is_same<WorldVector<double>, typename Term::value_type>, Result >::type;

template <class Term, class Result = void>
using MatrixTerm = typename boost::enable_if< boost::is_same<WorldMatrix<double>, typename Term::value_type>, Result >::type;



// === ZOT ===

template <class Term, class Result = GenericZeroOrderTerm<typename std::decay<Term>::type> >
ScalarTerm<typename std::decay<Term>::type, Result*>
make_zot(Term&& term)
{
  return new Result(std::forward<Term>(term));
}

// === FOT ===

template <class Term, class Result = GenericFirstOrderTerm_1<typename std::decay<Term>::type> >
ScalarTerm<typename std::decay<Term>::type, Result*>
make_fot(Term&& term)
{
  return new Result(std::forward<Term>(term));
}

template <class Term, class Result = GenericFirstOrderTerm_i<-1,typename std::decay<Term>::type> >
ScalarTerm<typename std::decay<Term>::type, Result*>
make_fot(Term&& term, int i)
{
  return new Result(std::forward<Term>(term), i);
}

template <class Term, class Result = GenericFirstOrderTerm_b<typename std::decay<Term>::type> >
VectorTerm<typename std::decay<Term>::type, Result*>
make_fot(Term&& term)
{
  return new Result(std::forward<Term>(term));
}


// === SOT ===

template <class Term, class Result = GenericSecondOrderTerm_1<typename std::decay<Term>::type> >
ScalarTerm<typename std::decay<Term>::type, Result*>
make_sot(Term&& term)
{
  return new Result(std::forward<Term>(term));
}

template <class Term, class Result = GenericSecondOrderTerm_ij<-1,-1,typename std::decay<Term>::type> >
ScalarTerm<typename std::decay<Term>::type, Result*>
make_sot(Term&& term, int i, int j)
{
  return new Result(std::forward<Term>(term), i, j);
}

template <class Term, class Result = GenericSecondOrderTerm_A<typename std::decay<Term>::type> >
MatrixTerm<typename std::decay<Term>::type, Result*>
make_sot(Term&& term)
{
  return new Result(std::forward<Term>(term));
}

#endif




763
// =============================================================================
764

765
/// Create an expression functor by wrapping an AbstractFunction and evaluate it a coordinates.
766
template<typename TOut>
767
inline expressions::Function1<expressions::Wrapper<TOut,WorldVector<double> >, expressions::Coords>
768
769
eval(AbstractFunction<TOut, WorldVector<double> >* fct) { return function_(wrap(fct), X()); }

770
/// Integrate an expression over a domain.
771
/** If the expression does not contain any DOFVector, a mesh must be given as second argument */
772
773
774
template<typename Term>
inline typename boost::enable_if<typename traits::is_expr<Term>::type, typename Term::value_type>::type
integrate(Term term, Mesh* mesh_ = NULL);
775

776
// -----------------------------------------------------------------------------
777

778
779
780
781
/// Accumulate the values of an expression at the Degrees of freedom
template<typename Term, typename Functor>
inline typename boost::enable_if<typename traits::is_expr<Term>::type, typename Term::value_type>::type
accumulate(Term term, Functor f, typename Term::value_type value0);
782

783
/// Maximum of an expression at DOFs, using the \ref accumulate function.
Praetorius, Simon's avatar
Praetorius, Simon committed
784
template<typename Term>
785
786
787
inline typename boost::enable_if<typename traits::is_expr<Term>::type, typename Term::value_type>::type
max(Term term)
{
788
  typename Term::value_type value0 = std::numeric_limits<typename Term::value_type>::min();
789
  value0 = accumulate(term, functors::max<typename Term::value_type>(), value0);
790

791
792
793
794
795
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
  Parallel::mpi::globalMax(value0);
#endif
  return value0;
}
Praetorius, Simon's avatar
Praetorius, Simon committed
796

797
/// Minimum of an expression at DOFs, using the \ref accumulate function.
Praetorius, Simon's avatar
Praetorius, Simon committed
798
template<typename Term>
799
800
801
inline typename boost::enable_if<typename traits::is_expr<Term>::type, typename Term::value_type>::type
min(Term term)
{
802
  typename Term::value_type value0 = std::numeric_limits<typename Term::value_type>::max();
803
  value0 = accumulate(term, functors::min<typename Term::value_type>(), value0);
804

805
806
807
808
809
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
  Parallel::mpi::globalMin(value0);
#endif
  return value0;
}
Praetorius, Simon's avatar
Praetorius, Simon committed
810

811
/// Maximum of  absolute values of an expression at DOFs, using the \ref accumulate function.
Praetorius, Simon's avatar
Praetorius, Simon committed
812
template<typename Term>
813
814
815
inline typename boost::enable_if<typename traits::is_expr<Term>::type, typename Term::value_type>::type
abs_max(Term term)
{
816
  typename Term::value_type value0 = 0;
817
  value0 = accumulate(term, functors::abs_max<typename Term::value_type>(), value0);
818

819
820
821
822
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
  Parallel::mpi::globalMax(value0);
#endif
  return value0;
823
824
}

825
826
827
828
829
/// Minimum of  absolute values of an expression at DOFs, using the \ref accumulate function.
template<typename Term>
inline typename boost::enable_if<typename traits::is_expr<Term>::type, typename Term::value_type>::type
abs_min(Term term)
{
830
  typename Term::value_type value0 = std::numeric_limits<typename Term::value_type>::max();
831
  value0 = accumulate(term, functors::abs_min<typename Term::value_type>(), value0);
832

833
834
835
836
837
838
839
840
841
842
843
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
  Parallel::mpi::globalMin(value0);
#endif
  return value0;
}

// -----------------------------------------------------------------------------

/// Assign an expression to a DOFVector
template<typename T, typename Term>
inline typename boost::enable_if<
844
  typename boost::mpl::and_<typename traits::is_expr<Term>::type,
845
			    typename traits::is_convertible<typename Term::value_type, T>::type
846
847
848
849
			    >::type
  >::type
transformDOF(Term term, DOFVector<T>* result);

850
851
852
/// Assign an expression to a DOFVector (using multi-mesh if term and result vector are on different meshes)
template<typename T, typename Term>
inline typename boost::enable_if<
853
  typename boost::mpl::and_<typename traits::is_expr<Term>::type,
854
855
856
857
858
			    typename traits::is_convertible<typename Term::value_type, T>::type
			    >::type
  >::type
transformDOF_mm(Term term, DOFVector<T>* result);

859
860
861
/// Assign an expression to a DOFVector
template<typename T, typename Term>
typename boost::enable_if<
862
  typename boost::mpl::and_<typename traits::is_expr<Term>::type,
863
			    typename traits::is_convertible<typename Term::value_type, T>::type
864
865
			    >::type,
  DOFVector<T>& >::type
866
867
868
869
870
871
operator<<(DOFVector<T>& result, const Term& term)
{
  transformDOF(term, &result);
  return result;
}

872
873
874
875
876
877
878
template<typename T>
DOFVector<T>& operator<<(DOFVector<T>& result, T value)
{
  result.set(value);
  return result;
}

879
880
881
882
883
884

/// Assign a constant value to a DOFVector
// template<typename T, typename S>
// typename boost::enable_if<
//   typename boost::mpl::or_<
//     typename boost::mpl::and_<
885
//       typename traits::is_scalar<T>::type,
886
887
888
889
//       typename traits::is_scalar<S>::type,
//       typename boost::is_convertible<S, T>::type
//       >::type,
//     typename boost::mpl::and_<
890
//       typename traits::is_vector<T>::type,
891
892
893
894
//       typename traits::is_vector<S>::type,
//       typename boost::is_convertible<typename S::value_type, typename T::value_type>::type
//       >::type,
//     typename boost::mpl::and_<
895
//       typename traits::is_matrix<T>::type,
896
897
898
899
900
901
902
903
904
905
906
//       typename traits::is_matrix<S>::type,
//       typename boost::is_convertible<typename S::value_type, typename T::value_type>::type
//       >::type
//     >::type,
//   DOFVector<T>& // return type
//   >::type
// operator<<(DOFVector<T>& result, const S& value)
// {
//   result.set(value);
//   return result;
// }
907
908
909
910
911

// -----------------------------------------------------------------------------

/// Print an expression to an output stream
template<typename Term>
912
typename boost::enable_if<typename traits::is_expr<Term>::type,
913
			  std::ostream& >::type
914
915
916
917
918
operator<<(std::ostream& result, const Term& term)
{
  result << term.str();
  return result;
}
919
920
921

} // end namespace AMDiS

Praetorius, Simon's avatar
Praetorius, Simon committed
922
#include "Expressions.hh"
Praetorius, Simon's avatar
Praetorius, Simon committed
923

Praetorius, Simon's avatar
Praetorius, Simon committed
924
#endif // AMDIS_EXPRESSIONS_BASE_H