GenericOperatorTerm.h 25.5 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
/******************************************************************************
 *
 * 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 GenericOperatorTerm.h */

#ifndef AMDIS_GENERIC_OPERATOR_TERM_H
#define AMDIS_GENERIC_OPERATOR_TERM_H

#include "AMDiS_fwd.h"
#include "OperatorTerm.h"
#include "Functors.h"

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

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

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
69
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

/** generic operator-terms provide an easy way of automated generation of
 * '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
 *   typedef (...) value_type;
 *   int getDegree(int d0);
 *   value_type operator()(const T0& v0) const;
 * 
 * respective
 *   int getDegree(int d0, int d1);
 *   value_type operator()(const T0& v0, const T1& v1) const;
 * 
 * respective
 *   int getDegree(int d0, int d1, int d2);
 *   value_type operator()(const T0& v0, const T1& v1, const T2& v2) const;
 * 
 * where the d0, d1, d2 give the polynomial degrees of the v0, v1, v2 terms.
 * */

namespace AMDiS {

// _______ ZeroOrderTerms ______________________________________________________

template<typename Term>
struct GenericZeroOrderTerm : public ZeroOrderTerm
{
  Term term;
  GenericZeroOrderTerm(const Term& term_)
    : ZeroOrderTerm(term_.getDegree()), term(term_) 
  {
    term.insertFeSpaces(auxFeSpaces);
  }

  void initElement(const ElInfo* elInfo,
	    SubAssembler* subAssembler,
	    Quadrature *quad)
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
113
    term.initElement(this, elInfo, subAssembler, quad, NULL);
114
115
116
117
118
119
120
121
  }


  void initElement(const ElInfo* smallElInfo,
	    const ElInfo* largeElInfo,
	    SubAssembler* subAssembler,
	    Quadrature *quad)
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
122
    term.initElement(this, smallElInfo, largeElInfo, subAssembler, quad, NULL);
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
  }


  void getC(const ElInfo *elInfo, int nPoints, ElementVector& C)
  {
    for (int iq = 0; iq < nPoints; iq++)
      C[iq] += term(iq);
  }


  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++)
      result[iq] += fac * term(iq) * uhAtQP[iq];
  }
};

// _______ FirstOrderTerms _____________________________________________________


template<typename Term>
struct GenericFirstOrderTerm_1 : public FirstOrderTerm
{
  Term term;
  GenericFirstOrderTerm_1(const Term& term_)
    : FirstOrderTerm(term_.getDegree()), term(term_) 
  {
    term.insertFeSpaces(auxFeSpaces);
  }

  void initElement(const ElInfo* elInfo,
	    SubAssembler* subAssembler,
	    Quadrature *quad)
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
162
    term.initElement(this, elInfo, subAssembler, quad);
163
164
165
166
167
168
169
170
  }


  void initElement(const ElInfo* smallElInfo,
	    const ElInfo* largeElInfo,
	    SubAssembler* subAssembler,
	    Quadrature *quad)
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
171
    term.initElement(this, smallElInfo, largeElInfo, subAssembler, quad);
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
  }


  /// 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++)
      l1(grdLambda, Lb[iq], term(iq));
  }
};


template<int I, typename Term>
struct GenericFirstOrderTerm_i : public FirstOrderTerm
{
  Term term;
  GenericFirstOrderTerm_i(const Term& term_)
    : FirstOrderTerm(term_.getDegree()), term(term_) 
  {
    term.insertFeSpaces(auxFeSpaces);
    FirstOrderTerm::bOne = I;
  }
  
  GenericFirstOrderTerm_i(const Term& term_, int I0)
    : FirstOrderTerm(term_.getDegree()), term(term_) 
  {
    term.insertFeSpaces(auxFeSpaces);
    FirstOrderTerm::bOne = I0;
    TEST_EXIT_DBG( I < 0 && I0 >= 0 )("You yould specify eather template<int I>, or constructor(int I0)\n");
  }

  void initElement(const ElInfo* elInfo,
	    SubAssembler* subAssembler,
	    Quadrature *quad)
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
211
    term.initElement(this, elInfo, subAssembler, quad);
212
213
214
215
216
217
218
219
  }


  void initElement(const ElInfo* smallElInfo,
	    const ElInfo* largeElInfo,
	    SubAssembler* subAssembler,
	    Quadrature *quad)
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
220
    term.initElement(this, smallElInfo, largeElInfo, subAssembler, quad);
221
222
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
  }


  /// 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++)
      lb_one(grdLambda, Lb[iq], term(iq));
  }
};


template<typename Term>
struct GenericFirstOrderTerm_b : public FirstOrderTerm
{
  Term term;
  
  GenericFirstOrderTerm_b(const Term& term_)
    : FirstOrderTerm(term_.getDegree()), term(term_)
  {
    term.insertFeSpaces(auxFeSpaces);
  }

  void initElement(const ElInfo* elInfo,
	    SubAssembler* subAssembler,
	    Quadrature *quad)
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
252
    term.initElement(this, elInfo, subAssembler, quad);
253
254
255
256
257
258
259
260
  }


  void initElement(const ElInfo* smallElInfo,
	    const ElInfo* largeElInfo,
	    SubAssembler* subAssembler,
	    Quadrature *quad)
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
261
    term.initElement(this, smallElInfo, largeElInfo, subAssembler, quad);
262
263
264
265
266
267
268
269
270
271
272
  }


  /// 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++)
273
      lb(grdLambda, term(iq), Lb[iq], 1.0);
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
  }
};

// _______ SecondOrderTerms ____________________________________________________


template<typename Term>
struct GenericSecondOrderTerm_1 : public SecondOrderTerm
{
  Term term;
  
  GenericSecondOrderTerm_1(const Term& term_)
    : SecondOrderTerm(term_.getDegree()), term(term_) 
  {
    term.insertFeSpaces(auxFeSpaces);
    setSymmetric(true);
  }

  void initElement(const ElInfo* elInfo,
	    SubAssembler* subAssembler,
	    Quadrature *quad)
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
296
    term.initElement(this, elInfo, subAssembler, quad);
297
298
299
300
301
302
303
304
  }


  void initElement(const ElInfo* smallElInfo,
	    const ElInfo* largeElInfo,
	    SubAssembler* subAssembler,
	    Quadrature *quad)
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
305
    term.initElement(this, smallElInfo, largeElInfo, subAssembler, quad);
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
  }
  
  /// 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++) 
      l1lt(grdLambda, LALt[iq], term(iq));
  }

  /// Implenetation of SecondOrderTerm::eval().
  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];
	}
	result[iq] += resultQP * f * term(iq);
      }
    }
  }

  /// Implenetation of SecondOrderTerm::weakEval().
  void weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
		std::vector<WorldVector<double> > &result) 
  {
    int nPoints = grdUhAtQP.size();
    for (int iq = 0; iq < nPoints; iq++)
      axpy(term(iq), grdUhAtQP[iq], result[iq]);
  }
};



351
template<typename Term, bool symmetric = false>
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
struct GenericSecondOrderTerm_A : public SecondOrderTerm
{
  Term term;
  
  GenericSecondOrderTerm_A(const Term& term_)
    : SecondOrderTerm(term_.getDegree()), term(term_) 
  {
    term.insertFeSpaces(auxFeSpaces);
    setSymmetric(symmetric);
  }

  void initElement(const ElInfo* elInfo,
	    SubAssembler* subAssembler,
	    Quadrature *quad)
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
367
    term.initElement(this, elInfo, subAssembler, quad);
368
369
370
371
372
373
374
375
  }


  void initElement(const ElInfo* smallElInfo,
	    const ElInfo* largeElInfo,
	    SubAssembler* subAssembler,
	    Quadrature *quad)
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
376
    term.initElement(this, smallElInfo, largeElInfo, subAssembler, quad);
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
  }
  
  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++)
      lalt(grdLambda, term(iq), LALt[iq], symmetric, 1.0);
  }

  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;

      WorldMatrix<double> A = term(iq);

      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++) {
      A = term(iq);
      result[iq] += term(iq) * grdUhAtQP[iq];
    }    
  }
};



template<int I, int J, typename Term>
struct GenericSecondOrderTerm_ij : public SecondOrderTerm
{
  Term term;
  int row, col;
  
  GenericSecondOrderTerm_ij(const Term& term_)
    : SecondOrderTerm(term_.getDegree()), term(term_), row(I), col(J)
  {
    term.insertFeSpaces(auxFeSpaces);
    setSymmetric(row == col);
  }
  
  GenericSecondOrderTerm_ij(const Term& term_, int I0, int J0)
    : SecondOrderTerm(term_.getDegree()), term(term_), row(I0), col(J0)
  {
    term.insertFeSpaces(auxFeSpaces);
    setSymmetric(row == col);
    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 initElement(const ElInfo* elInfo,
	    SubAssembler* subAssembler,
	    Quadrature *quad)
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
455
    term.initElement(this, elInfo, subAssembler, quad);
456
457
458
459
460
461
462
463
  }


  void initElement(const ElInfo* smallElInfo,
	    const ElInfo* largeElInfo,
	    SubAssembler* subAssembler,
	    Quadrature *quad)
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
464
    term.initElement(this, smallElInfo, largeElInfo, subAssembler, quad);
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
  }

  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++)
      lalt_kl(grdLambda, row, col, LALt[iq], term(iq));
  }  

  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++)
	result[iq] += D2UhAtQP[iq][row][col] * term(iq) * fac;
    }
  }

  void weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
				std::vector<WorldVector<double> > &result) 
  {
    int nPoints = grdUhAtQP.size();
    for (int iq = 0; iq < nPoints; iq++)
      result[iq][row] += grdUhAtQP[iq][col] * term(iq);
  }
};


// _____________________________________________________________________________

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

514
515
516
517
518
519
520
521
522
523
524
inline void addZOT(Operator* op, double term)
{
  addZOT(op, constant(term));
}

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


525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// .............................................................................
// Rosenbrock method

// template <typename Term>
// void addZOT_(Operator* op, const Term& term)
// {
//   if (op->getUhOld()) {
//     addZOT(op, jacobian(term,0) * ...
//     
// }

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


// _____________________________________________________________________________

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

560
561
562
563
564
565
566
567
568
569
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);
}

570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
// 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);
}

585
586
587
588
589
590
591
592
593
594
595
596
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);
}

597
598
599
600
601
602
603
604
605
606
607
608
609
610
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);
}

611
612
613
614
615
616
617
618
619
620
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);
}

621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// 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);
}

636
637
638
639
640
641
642
643
644
645
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);
}

646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
// _____________________________________________________________________________

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

662
663
664
665
666
667
668
669
670
671
inline void addSOT(Operator* op, WorldMatrix<double> term)
{
  addSOT(op, constant(term));
}

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

672
673
674
675
676
677
678
679
680
681
682
683
684
685
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));
}

686
687
688
689
690
691
692
693
694
695
696
697
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));
}

698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
// 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));
}

713
714
715
716
717
718
719
720
721
722
inline void addSOT(Operator* op, double term)
{
  addSOT(op, constant(term));
}

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

723
724
725
726
727
728
729
730
731
732
733
734
735
// 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));
}

736
737
738
739
740
741
742
743
744
745
746
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));
}

747
748
749
750
751
752
753
754
755
756
757
758
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));
}

759
760
761
762
763
764
765
766
767
768
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);
}

769

770
// =============================================================================
771

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

777
778
779
780
781
/// Integrate an expression over a domain.
/** IF the expression does not contain any DOFVector, a mesh must be given as second argument */
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);
782

783
// -----------------------------------------------------------------------------
784

785
786
787
788
/// 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);
789

790
/// Maximum of an expression at DOFs, using the \ref accumulate function.
Praetorius, Simon's avatar
Praetorius, Simon committed
791
template<typename Term>
792
793
794
795
796
797
798
799
800
801
802
inline typename boost::enable_if<typename traits::is_expr<Term>::type, typename Term::value_type>::type
max(Term term)
{
  typename Term::value_type value0 = -1.e25;
  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
803

804
/// Minimum of an expression at DOFs, using the \ref accumulate function.
Praetorius, Simon's avatar
Praetorius, Simon committed
805
template<typename Term>
806
807
808
809
810
811
812
813
814
815
816
inline typename boost::enable_if<typename traits::is_expr<Term>::type, typename Term::value_type>::type
min(Term term)
{
  typename Term::value_type value0 = 1.e25;
  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
817

818
/// Maximum of  absolute values of an expression at DOFs, using the \ref accumulate function.
Praetorius, Simon's avatar
Praetorius, Simon committed
819
template<typename Term>
820
821
822
823
824
825
826
827
828
829
inline typename boost::enable_if<typename traits::is_expr<Term>::type, typename Term::value_type>::type
abs_max(Term term)
{
  typename Term::value_type value0 = 0.0;
  value0 = accumulate(term, functors::abs_max<typename Term::value_type>(), value0);
  
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
  Parallel::mpi::globalMax(value0);
#endif
  return value0;
830
831
}

832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
/// 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)
{
  typename Term::value_type value0 = 1.e25;
  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, 
			    typename boost::is_convertible<typename Term::value_type, T>::type
			    >::type
  >::type
transformDOF(Term term, DOFVector<T>* result);

/// 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, 
			    typename boost::is_convertible<typename Term::value_type, T>::type
			    >::type,
  DOFVector<T>& >::type
operator<<(DOFVector<T>& result, const Term& term);

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

/// Print an expression to an output stream
template<typename Term>
typename boost::enable_if<typename traits::is_expr<Term>::type, 
			  std::ostream& >::type
operator<<(std::ostream& result, const Term& term);

} // end namespace AMDiS

Praetorius, Simon's avatar
Praetorius, Simon committed
876
877
#include "GenericOperatorTerm.hh"

878
#endif // AMDIS_GENERIC_OPERATOR_TERM_H