Operator.h 13.1 KB
Newer Older
1
2
3
4
5
6
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
7
// ==  TU Dresden                                                            ==
8
// ==                                                                        ==
9
10
11
// ==  Institut fr Wissenschaftliches Rechnen                               ==
// ==  Zellescher Weg 12-14                                                  ==
// ==  01069 Dresden                                                         ==
12
13
14
15
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
16
// ==  https://gforge.zih.tu-dresden.de/projects/amdis/                      ==
17
18
19
20
21
22
23
24
25
// ==                                                                        ==
// ============================================================================

/** \file Operator.h */

#ifndef AMDIS_OPERATOR_H
#define AMDIS_OPERATOR_H

#include <vector>
26
#include "AMDiS_fwd.h"
27
28
29
30
31
#include "FixVec.h"
#include "Flag.h"
#include "MatrixVector.h"
#include "ElInfo.h"
#include "AbstractFunction.h"
32
#include "OpenMP.h"
33
#include "SubAssembler.h"
34
35
36
37
#include "OperatorTerm.h"
#include "ZeroOrderTerm.h"
#include "FirstOrderTerm.h"
#include "SecondOrderTerm.h"
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

namespace AMDiS {

  /** \brief
   * An Operator holds all information needed to assemble the system matrix
   * and the right hand side. It consists of four OperatorTerm lists each storing
   * Terms of a specific order and type. You can define your own Operator by 
   * creating an empty Operator and than adding OperatorTerms to it.
   * By calling \ref getElementMatrix() or \ref getElementVector() one can 
   * initiate the assembling procedure. Therefor each Operator has its own
   * Assembler, especially created for this Operator, by the first call of
   * \ref getElementMatrix() or \ref getElementVector(). 
   */
  class Operator
  {
  public:
54
55
    /// Obsolete consructor. Calling this constructor yields an error. Will be removed
    /// in on of the next revisions.
56
    Operator(Flag operatorType,
57
58
	     const FiniteElemSpace *rowFeSpace,
	     const FiniteElemSpace *colFeSpace = NULL);
59

60
61
62
63
    /// Constructs an empty Operator of type operatorType for the given FiniteElemSpace.
    Operator(const FiniteElemSpace *rowFeSpace,
	     const FiniteElemSpace *colFeSpace = NULL);

64
    /// Destructor.
Thomas Witkowski's avatar
Thomas Witkowski committed
65
    virtual ~Operator() {}
66

67
    /// Sets \ref optimized.
68
69
    inline void useOptimizedAssembler(bool opt) 
    {
70
      optimized = opt;
Thomas Witkowski's avatar
Thomas Witkowski committed
71
    }
72

73
    /// Returns \ref optimized.
74
75
    inline bool isOptimized() 
    {
76
      return optimized;
Thomas Witkowski's avatar
Thomas Witkowski committed
77
    }
78

79
    /// Adds a SecondOrderTerm to the Operator
80
81
    template <typename T>
    void addSecondOrderTerm(T *term);
82

83
    /// Adds a FirstOrderTerm to the Operator
84
    template <typename T>
85
86
87
    void addFirstOrderTerm(T *term, FirstOrderType type = GRD_PHI);

    /// Adds a ZeroOrderTerm to the Operator
88
89
    template <typename T>
    void addZeroOrderTerm(T *term);
90
91
92
93
94
95
96


    /** \brief
     * Calculates the element matrix for this ElInfo and adds it multiplied by
     * factor to userMat.
     */
    virtual void getElementMatrix(const ElInfo *elInfo, 
97
				  ElementMatrix& userMat, 
98
99
				  double factor = 1.0);

100
101
    virtual void getElementMatrix(const ElInfo *rowElInfo,
				  const ElInfo *colElInfo,
102
103
				  const ElInfo *smallElInfo,
				  const ElInfo *largeElInfo,
104
				  ElementMatrix& userMat,
105
106
				  double factor = 1.0);

107
108
109
110
111
    /** \brief
     * Calculates the element vector for this ElInfo and adds it multiplied by
     * factor to userVec.
     */
    virtual void getElementVector(const ElInfo *elInfo, 
112
				  ElementVector& userVec, 
113
114
				  double factor = 1.0);

Thomas Witkowski's avatar
Thomas Witkowski committed
115
116
117
118
    virtual void getElementVector(const ElInfo *mainElInfo, 
				  const ElInfo *auxElInfo,
				  const ElInfo *smallElInfo,
				  const ElInfo *largeElInfo,
119
				  ElementVector& userVec,
Thomas Witkowski's avatar
Thomas Witkowski committed
120
				  double factor = 1.0);
121

Thomas Witkowski's avatar
Thomas Witkowski committed
122
123
    /// That function must be called after one assembling cycle has been finished.
    void finishAssembling();
124

125
126
    /// Returns \ref rowFeSpace.
    inline const FiniteElemSpace *getRowFeSpace() 
127
    { 
128
      return rowFeSpace; 
129
    }
130

131
132
    /// Returns \ref colFeSpace.
    inline const FiniteElemSpace *getColFeSpace() 
133
    { 
134
      return colFeSpace; 
135
    }
136

137
138
    /// Returns \ref auxFeSpaces.
    inline std::set<const FiniteElemSpace*>& getAuxFeSpaces()
139
    {
140
      return auxFeSpaces;
Thomas Witkowski's avatar
Thomas Witkowski committed
141
142
    }

143
    /// Sets \ref uhOld.
144
145
    void setUhOld(const DOFVectorBase<double> *uhOld);

146
    /// Returns \ref uhOld.
147
148
    inline const DOFVectorBase<double> *getUhOld() 
    {
149
      return uhOld;
150
    }    
151

Thomas Witkowski's avatar
Thomas Witkowski committed
152
    /// Returns \ref assembler
153
    Assembler *getAssembler(int rank = 0);
154

Thomas Witkowski's avatar
Thomas Witkowski committed
155
    /// Sets \ref assembler
156
    void setAssembler(int rank, Assembler *ass);
157

Thomas Witkowski's avatar
Thomas Witkowski committed
158
    /// Sets \ref fillFlag, the flag used for this Operator while mesh traversal.
159
160
    inline void setFillFlag(Flag f) 
    { 
161
      fillFlag = f; 
162
    }
163

Thomas Witkowski's avatar
Thomas Witkowski committed
164
    /// Sets \ref needDualTraverse.
165
166
    void setNeedDualTraverse(bool b) 
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
167
168
169
170
      needDualTraverse = b;
    }

    /// Returns \ref fillFlag
171
172
    inline Flag getFillFlag() 
    { 
173
      return fillFlag; 
174
    }
175

Thomas Witkowski's avatar
Thomas Witkowski committed
176
    /// Returns \ref needDualTraverse
177
178
    bool getNeedDualTraverse() 
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
179
180
181
182
      return needDualTraverse;
    }

    /// Initialization of the needed SubAssemblers using the given quadratures. 
183
184
    void initAssembler(int rank,
		       Quadrature *quad2,
185
186
187
188
189
		       Quadrature *quad1GrdPsi,
		       Quadrature *quad1GrdPhi,
		       Quadrature *quad0);


Thomas Witkowski's avatar
Thomas Witkowski committed
190
    /// Calculates the needed quadrature degree for the given order. 
191
192
    int getQuadratureDegree(int order, FirstOrderType firstOrderType = GRD_PHI);

Thomas Witkowski's avatar
Thomas Witkowski committed
193
194
195
    /// Evaluation of all terms in \ref zeroOrder. 
    void evalZeroOrder(int nPoints,
		       const double *uhAtQP,
196
197
198
199
200
		       const WorldVector<double> *grdUhAtQP,
		       const WorldMatrix<double> *D2UhAtQP,
		       double *result,
		       double factor) const
    {
201
202
      int myRank = omp_get_thread_num();

203
      std::vector<OperatorTerm*>::const_iterator termIt;
204
205
      for (termIt = zeroOrder[myRank].begin(); 
	   termIt != zeroOrder[myRank].end(); 
206
	   ++termIt)
Thomas Witkowski's avatar
Thomas Witkowski committed
207
	(*termIt)->eval(nPoints, uhAtQP, grdUhAtQP, D2UhAtQP, result, factor);
Thomas Witkowski's avatar
Thomas Witkowski committed
208
    }
209

210
    /// Evaluation of all terms in \ref firstOrderGrdPsi. 
Thomas Witkowski's avatar
Thomas Witkowski committed
211
212
    void evalFirstOrderGrdPsi(int nPoints,
			      const double *uhAtQP,
213
214
215
216
217
			      const WorldVector<double> *grdUhAtQP,
			      const WorldMatrix<double> *D2UhAtQP,
			      double *result,
			      double factor) const
    {
218
219
      int myRank = omp_get_thread_num();

220
      std::vector<OperatorTerm*>::const_iterator termIt;
221
222
      for (termIt = firstOrderGrdPsi[myRank].begin(); 
	   termIt != firstOrderGrdPsi[myRank].end(); 
223
	   ++termIt)
Thomas Witkowski's avatar
Thomas Witkowski committed
224
	(*termIt)->eval(nPoints, uhAtQP, grdUhAtQP, D2UhAtQP, result, factor);
Thomas Witkowski's avatar
Thomas Witkowski committed
225
    }
226

227
    /// Evaluation of all terms in \ref firstOrderGrdPhi. 
Thomas Witkowski's avatar
Thomas Witkowski committed
228
229
    void evalFirstOrderGrdPhi(int nPoints,
			      const double *uhAtQP,
230
231
232
233
234
			      const WorldVector<double> *grdUhAtQP,
			      const WorldMatrix<double> *D2UhAtQP,
			      double *result,
			      double factor) const
    {
235
236
      int myRank = omp_get_thread_num();

237
      std::vector<OperatorTerm*>::const_iterator termIt;
238
239
      for (termIt = firstOrderGrdPhi[myRank].begin(); 
	   termIt != firstOrderGrdPhi[myRank].end(); 
240
	   ++termIt)
Thomas Witkowski's avatar
Thomas Witkowski committed
241
	(*termIt)->eval(nPoints, uhAtQP, grdUhAtQP, D2UhAtQP, result, factor);
Thomas Witkowski's avatar
Thomas Witkowski committed
242
    }
243

244
    /// Evaluation of all terms in \ref secondOrder. 
Thomas Witkowski's avatar
Thomas Witkowski committed
245
246
    void evalSecondOrder(int nPoints,
			 const double *uhAtQP,
247
248
249
250
251
			 const WorldVector<double> *grdUhAtQP,
			 const WorldMatrix<double> *D2UhAtQP,
			 double *result,
			 double factor) const
    {
252
253
      int myRank = omp_get_thread_num();

254
      std::vector<OperatorTerm*>::const_iterator termIt;
255
256
      for (termIt = secondOrder[myRank].begin(); 
	   termIt != secondOrder[myRank].end(); 
257
	   ++termIt)
Thomas Witkowski's avatar
Thomas Witkowski committed
258
	(*termIt)->eval(nPoints, uhAtQP, grdUhAtQP, D2UhAtQP, result, factor);
Thomas Witkowski's avatar
Thomas Witkowski committed
259
    }
260

261
    /// Weak evaluation of all terms in \ref secondOrder. 
262
    void weakEvalSecondOrder(const std::vector<WorldVector<double> > &grdUhAtQP,
Thomas Witkowski's avatar
Thomas Witkowski committed
263
			     std::vector<WorldVector<double> > &result) const;
264
  
265
    /// Calls getLALt() for each term in \ref secondOrder and adds the results to LALt.
Thomas Witkowski's avatar
Thomas Witkowski committed
266
    void getLALt(const ElInfo *elInfo, int nPoints, DimMat<double> **LALt) const
267
    {
268
269
      int myRank = omp_get_thread_num();

270
      std::vector<OperatorTerm*>::const_iterator termIt;
271
272
      for (termIt = secondOrder[myRank].begin(); 
	   termIt != secondOrder[myRank].end(); 
273
	   ++termIt)
Thomas Witkowski's avatar
Thomas Witkowski committed
274
	static_cast<SecondOrderTerm*>(*termIt)->getLALt(elInfo, nPoints, LALt);
Thomas Witkowski's avatar
Thomas Witkowski committed
275
    }
276
  
277
278
279
280
    /// Calls getLb() for each term in \ref firstOrderGrdPsi and adds the results to Lb.
    void getLbGrdPsi(const ElInfo *elInfo, 
		     int nPoints, 
		     VectorOfFixVecs<DimVec<double> >& Lb) const
281
    {
282
283
      int myRank = omp_get_thread_num();

284
      std::vector<OperatorTerm*>::const_iterator termIt;
285
286
      for (termIt = firstOrderGrdPsi[myRank].begin(); 
	   termIt != firstOrderGrdPsi[myRank].end(); 
287
	   ++termIt)
Thomas Witkowski's avatar
Thomas Witkowski committed
288
	static_cast<FirstOrderTerm*>(*termIt)->getLb(elInfo, nPoints, Lb);
Thomas Witkowski's avatar
Thomas Witkowski committed
289
    }
290

291
292
293
294
    /// Calls getLb() for each term in \ref firstOrderGrdPhi and adds the results to Lb.
    void getLbGrdPhi(const ElInfo *elInfo, 
		     int nPoints, 
		     VectorOfFixVecs<DimVec<double> >& Lb) const
295
    {
296
297
      int myRank = omp_get_thread_num();

298
      std::vector<OperatorTerm*>::const_iterator termIt;
299
300
      for (termIt = firstOrderGrdPhi[myRank].begin(); 
	   termIt != firstOrderGrdPhi[myRank].end(); 
301
	   ++termIt)
Thomas Witkowski's avatar
Thomas Witkowski committed
302
	static_cast<FirstOrderTerm*>(*termIt)->getLb(elInfo, nPoints, Lb);
Thomas Witkowski's avatar
Thomas Witkowski committed
303
    }
304

305
    /// Calls getC() for each term in \ref zeroOrder and adds the results to c.
Thomas Witkowski's avatar
Thomas Witkowski committed
306
    void getC(const ElInfo *elInfo, int nPoints, std::vector<double> &c)
307
    {
308
309
      int myRank = omp_get_thread_num();

310
      std::vector<OperatorTerm*>::const_iterator termIt;
311
312
      for (termIt = zeroOrder[myRank].begin(); 
	   termIt != zeroOrder[myRank].end(); 
313
	   ++termIt)
Thomas Witkowski's avatar
Thomas Witkowski committed
314
	static_cast<ZeroOrderTerm*>(*termIt)->getC(elInfo, nPoints, c);
Thomas Witkowski's avatar
Thomas Witkowski committed
315
    }
316

Thomas Witkowski's avatar
Thomas Witkowski committed
317
    /// Returns true, if there are second order terms. Returns false otherwise.
318
319
    inline bool secondOrderTerms() 
    {
320
      return secondOrder[omp_get_thread_num()].size() != 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
321
    }
322

323
    /// Returns true, if there are first order terms (grdPsi). Returns false otherwise.
324
325
    inline bool firstOrderTermsGrdPsi() 
    {
326
      return firstOrderGrdPsi[omp_get_thread_num()].size() != 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
327
    }
328

329
    /// Returns true, if there are first order terms (grdPhi). Returns false otherwise.
330
331
    inline bool firstOrderTermsGrdPhi() 
    {
332
      return firstOrderGrdPhi[omp_get_thread_num()].size() != 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
333
    }
334

335
    /// Returns true, if there are zero order terms. Returns false otherwise.
336
337
    inline bool zeroOrderTerms() 
    {
338
      return zeroOrder[omp_get_thread_num()].size() != 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
339
    }
340
341

  public:
342
    /// Constant type flag for matrix operators. Obsolete, will be removed!
343
344
    static const Flag MATRIX_OPERATOR;

345
    /// Constant type flag for vector operators. Obsolete, will be removed!
346
347
348
    static const Flag VECTOR_OPERATOR;

  protected:
Thomas Witkowski's avatar
Thomas Witkowski committed
349
    /// FiniteElemSpace for matrix rows and element vector
350
    const FiniteElemSpace *rowFeSpace;
351

352
353
    /// FiniteElemSpace for matrix columns. Can be equal to \rowFeSpace.
    const FiniteElemSpace *colFeSpace;
354

Thomas Witkowski's avatar
Thomas Witkowski committed
355
    /// List of aux fe spaces, e.g., if a term is multiplied with a DOF vector
356
    std::set<const FiniteElemSpace*> auxFeSpaces;
Thomas Witkowski's avatar
Thomas Witkowski committed
357
358

    /// Number of rows in the element matrix
359
360
    int nRow;

Thomas Witkowski's avatar
Thomas Witkowski committed
361
    /// Number of columns in the element matrix
362
363
    int nCol;

Thomas Witkowski's avatar
Thomas Witkowski committed
364
    /// Flag for mesh traversal
365
366
    Flag fillFlag;

Thomas Witkowski's avatar
Thomas Witkowski committed
367
368
369
    /// If true, the operator needs a dual traverse over two meshes for assembling.
    bool needDualTraverse;

370
371
372
373
374
    /** \brief
     * Calculates the element matrix and/or the element vector. It is
     * created especially for this Operator, when \ref getElementMatrix()
     * or \ref getElementVector is called for the first time.
     */
375
    std::vector<Assembler*> assembler;
376

Thomas Witkowski's avatar
Thomas Witkowski committed
377
    /// List of all second order terms
378
    std::vector< std::vector<OperatorTerm*> > secondOrder;
379

Thomas Witkowski's avatar
Thomas Witkowski committed
380
    /// List of all first order terms derived to psi
381
    std::vector< std::vector<OperatorTerm*> > firstOrderGrdPsi;
382

Thomas Witkowski's avatar
Thomas Witkowski committed
383
    /// List of all first order terms derived to phi
384
    std::vector< std::vector<OperatorTerm*> > firstOrderGrdPhi;
385

Thomas Witkowski's avatar
Thomas Witkowski committed
386
    /// List of all zero order terms
387
    std::vector< std::vector<OperatorTerm*> > zeroOrder;
388
389

    /** \brief
390
391
     * Pointer to the solution of the last timestep. Can be used for a more efficient
     * assemblage of the element vector when the element matrix was already computed.
392
393
394
     */
    const DOFVectorBase<double> *uhOld;

Thomas Witkowski's avatar
Thomas Witkowski committed
395
    /// Spezifies whether optimized assemblers are used or not.
396
397
398
399
400
401
402
403
404
405
406
    bool optimized;

    friend class Assembler;
    friend class SubAssembler;
    friend class ZeroOrderAssembler;
    friend class FirstOrderAssembler;
    friend class SecondOrderAssembler;
  };

}

407
408
#include "Operator.hh"

409
#endif // AMDIS_OPERATOR_H