Expressions.h 26 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
/******************************************************************************
 *
 * 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.
 * 
 ******************************************************************************/



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
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
70
71
72
73
74
75
76
77
78
79
80
 * 'arbitrary' operator-terms out of some elementary operations, by using a 
 * recursive definition of the term. All necessary data will be initialized 
 * 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.
 * 
 * usage:
 * addZOT(Operator, Term) 
 *  ... add a zeroOrderTerm to Operator, i.e. (Term(x) * u, v)
 * 
 * addFOT(Operator, Term, FirstOrderType) 
 *  ... add a firstOrderTerm to Operator, (if Term::value_type = double) 
 *      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)
 *  ... add a firstOrderTerm to Operator, 
 *      i.e. (Term(x) * e_I * grad(u), v), rsp. (Term(x) * e_I * u, grad(v))
 * 
 * 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))
 * 
 * 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))
 * 
 * 
 * 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
101
102
103
104
105
106
template<int Order>
struct GetTerm {
  typedef typename boost::mpl::if_c<Order == 0, ZeroOrderTerm, 
	  typename boost::mpl::if_c<Order == 1, FirstOrderTerm, 
	  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
113
  typedef typename GetTerm<Order>::type super;
  
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
120
  GenericOperatorTerm(const Term& term_)
    : 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
166
167
168
169
170
171
172
173
174
175

template<typename Term>
struct GenericOperatorTerm<Term, -1> : public GenericOperatorTerm<Term, -2>
{
  typedef GenericOperatorTerm<Term, -2> super;
  GenericOperatorTerm(const Term& term_) : super(term_) { }
  
  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) {};
};
	    
// _______ 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
210
    : GenericOperatorTerm<Term, 1>(term_) 
  { }
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
291
  }
  
  /// 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());

    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
301
302
303
304
305
306
307
308
309
310
  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 f) 
  {
    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
318
319
320
321
  void weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
		std::vector<WorldVector<double> > &result) 
  {
    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
330
struct GenericSecondOrderTerm_A : public GenericOperatorTerm<Term, 2>
{  
331
  GenericSecondOrderTerm_A(const Term& term_)
332
    : GenericOperatorTerm<Term, 2>(term_) 
333
  {
334
    this->setSymmetric(symmetric);
335
336
337
338
339
340
341
342
343
  }
  
  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());

    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
352
353
354
355
356
357
358
  }

  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) 
  {
    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
390
391
{
  int row, col;
  
  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
402
403
404
405
406
407
408
409
410
411
    TEST_EXIT_DBG( I < 0 && I0 >= 0 && J < 0 && J0 >= 0 ) 
      ("You yould specify eather template<int I, int J>, or constructor(int I0, int J0)\n");
  }

  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());

    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
420
421
422
423
  }  

  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) 
  {
    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
429
430
431
432
    }
  }

  void weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
				std::vector<WorldVector<double> > &result) 
  {
    int nPoints = grdUhAtQP.size();
    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
// =============================================================================
690

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

696
/// Integrate an expression over a domain.
697
/** If the expression does not contain any DOFVector, a mesh must be given as second argument */
698
699
700
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);
701

702
// -----------------------------------------------------------------------------
703

704
705
706
707
/// 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);
708

709
/// Maximum of an expression at DOFs, using the \ref accumulate function.
Praetorius, Simon's avatar
Praetorius, Simon committed
710
template<typename Term>
711
712
713
inline typename boost::enable_if<typename traits::is_expr<Term>::type, typename Term::value_type>::type
max(Term term)
{
714
  typename Term::value_type value0 = std::numeric_limits<typename Term::value_type>::min();
715
716
717
718
719
720
721
  value0 = accumulate(term, functors::max<typename Term::value_type>(), value0);
  
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
  Parallel::mpi::globalMax(value0);
#endif
  return value0;
}
Praetorius, Simon's avatar
Praetorius, Simon committed
722

723
/// Minimum of an expression at DOFs, using the \ref accumulate function.
Praetorius, Simon's avatar
Praetorius, Simon committed
724
template<typename Term>
725
726
727
inline typename boost::enable_if<typename traits::is_expr<Term>::type, typename Term::value_type>::type
min(Term term)
{
728
  typename Term::value_type value0 = std::numeric_limits<typename Term::value_type>::max();
729
730
731
732
733
734
735
  value0 = accumulate(term, functors::min<typename Term::value_type>(), value0);
  
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
  Parallel::mpi::globalMin(value0);
#endif
  return value0;
}
Praetorius, Simon's avatar
Praetorius, Simon committed
736

737
/// Maximum of  absolute values of an expression at DOFs, using the \ref accumulate function.
Praetorius, Simon's avatar
Praetorius, Simon committed
738
template<typename Term>
739
740
741
inline typename boost::enable_if<typename traits::is_expr<Term>::type, typename Term::value_type>::type
abs_max(Term term)
{
742
  typename Term::value_type value0 = 0;
743
744
745
746
747
748
  value0 = accumulate(term, functors::abs_max<typename Term::value_type>(), value0);
  
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
  Parallel::mpi::globalMax(value0);
#endif
  return value0;
749
750
}

751
752
753
754
755
/// 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)
{
756
  typename Term::value_type value0 = std::numeric_limits<typename Term::value_type>::max();
757
758
759
760
761
762
763
764
765
766
767
768
769
770
  value0 = accumulate(term, functors::abs_min<typename Term::value_type>(), value0);
  
#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<
  typename boost::mpl::and_<typename traits::is_expr<Term>::type, 
771
			    typename traits::is_convertible<typename Term::value_type, T>::type
772
773
774
775
			    >::type
  >::type
transformDOF(Term term, DOFVector<T>* result);

776
777
778
779
780
781
782
783
784
/// 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<
  typename boost::mpl::and_<typename traits::is_expr<Term>::type, 
			    typename traits::is_convertible<typename Term::value_type, T>::type
			    >::type
  >::type
transformDOF_mm(Term term, DOFVector<T>* result);

785
786
787
788
/// Assign an expression to a DOFVector
template<typename T, typename Term>
typename boost::enable_if<
  typename boost::mpl::and_<typename traits::is_expr<Term>::type, 
789
			    typename traits::is_convertible<typename Term::value_type, T>::type
790
791
			    >::type,
  DOFVector<T>& >::type
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
operator<<(DOFVector<T>& result, const Term& term)
{
  transformDOF(term, &result);
  return result;
}


/// Assign a constant value to a DOFVector
// template<typename T, typename S>
// typename boost::enable_if<
//   typename boost::mpl::or_<
//     typename boost::mpl::and_<
//       typename traits::is_scalar<T>::type,       
//       typename traits::is_scalar<S>::type,
//       typename boost::is_convertible<S, T>::type
//       >::type,
//     typename boost::mpl::and_<
//       typename traits::is_vector<T>::type,       
//       typename traits::is_vector<S>::type,
//       typename boost::is_convertible<typename S::value_type, typename T::value_type>::type
//       >::type,
//     typename boost::mpl::and_<
//       typename traits::is_matrix<T>::type,       
//       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;
// }
826
827
828
829
830
831
832

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

/// Print an expression to an output stream
template<typename Term>
typename boost::enable_if<typename traits::is_expr<Term>::type, 
			  std::ostream& >::type
833
834
835
836
837
operator<<(std::ostream& result, const Term& term)
{
  result << term.str();
  return result;
}
838
839
840

} // end namespace AMDiS

Praetorius, Simon's avatar
Praetorius, Simon committed
841
#include "Expressions.hh"
Praetorius, Simon's avatar
Praetorius, Simon committed
842

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