Assembler.h 10.3 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 33 34 35
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  crystal growth group                                                  ==
// ==                                                                        ==
// ==  Stiftung caesar                                                       ==
// ==  Ludwig-Erhard-Allee 2                                                 ==
// ==  53175 Bonn                                                            ==
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  http://www.caesar.de/cg/AMDiS                                         ==
// ==                                                                        ==
// ============================================================================

/** \file Assembler.h */

/**
 * \defgroup Assembler Assembler module
 *
 * \brief
 * Contains the operator and assembler classes:
 * @{ <img src="assembler.png"> @}
 */

#ifndef AMDIS_ASSEMBLER_H
#define AMDIS_ASSEMBLER_H

#include <vector>
#include "FixVec.h"
#include "MemoryManager.h"
36 37 38 39
#include "ZeroOrderAssembler.h"
#include "FirstOrderAssembler.h"
#include "SecondOrderAssembler.h"
#include "ElInfo.h"
40
#include "OpenMP.h"
41 42 43 44 45 46 47

namespace AMDiS {

  class Element;
  class Quadrature;
  class ElementMatrix;
  class ElementVector;
48
  class Operator;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

  /**
   * \ingroup Assembler
   * 
   * \brief
   * Assembles element matrices and vectors for a given Operator. Uses
   * one SubAssembler for all second order terms of the Operator, one for all
   * first order terms, and one for all zero order terms.
   */
  class Assembler
  {
  public:
    MEMORY_MANAGED(Assembler);

    /** \brief
     * Constructor.
     */
66 67 68
    Assembler(Operator *op,
	      const FiniteElemSpace *rowFESpace,
	      const FiniteElemSpace *colFESpace = NULL);
69

Thomas Witkowski's avatar
Thomas Witkowski committed
70
    virtual ~Assembler() {}
71

72 73 74
    void initElementMatrix(ElementMatrix *elMat, 
			   const ElInfo *rowElInfo,
			   const ElInfo *colElInfo = NULL);
75 76


77 78
    void initElementVector(ElementVector *elVec,
			   const ElInfo *elInfo);
79 80 81 82

    /** \brief
     * Assembles the element matrix for the given ElInfo
     */
83 84 85 86 87 88
    void calculateElementMatrix(const ElInfo *elInfo, 
				ElementMatrix *userMat, 
				double factor = 1.0);

    void calculateElementMatrix(const ElInfo *rowElInfo,
				const ElInfo *colElInfo,
89 90
				const ElInfo *smallElInfo,
				const ElInfo *largeElInfo,
91 92
				ElementMatrix *userMat,
				double factor = 1.0);
93 94 95 96

    /** \brief
     * Assembles the element vector for the given ElInfo
     */
97 98 99
    void calculateElementVector(const ElInfo *elInfo, 
				ElementVector *userVec, 
				double factor = 1.0);
100

Thomas Witkowski's avatar
Thomas Witkowski committed
101 102 103 104 105 106 107 108

    void calculateElementVector(const ElInfo *mainElInfo, 
				const ElInfo *auxElInfo,
				const ElInfo *smallElInfo,
				const ElInfo *largeElInfo,
				ElementVector *userVec, 
				double factor = 1.0);

109 110 111
    /** \brief
     * Returns \ref rowFESpace.
     */
112 113
    inline const FiniteElemSpace* getRowFESpace() { 
      return rowFESpace; 
114
    }
115 116 117 118

    /** \brief
     * Returns \ref colFESpace.
     */
119 120
    inline const FiniteElemSpace* getColFESpace() { 
      return colFESpace; 
121
    }
122 123 124 125

    /** \brief
     * Returns \ref nRow.
     */
126 127
    inline int getNRow() { 
      return nRow; 
128
    }
129 130 131 132

    /** \brief
     * Returns \ref nCol.
     */
133 134
    inline int getNCol() { 
      return nCol; 
135
    }
136 137 138 139

    /** \brief
     * Sets \ref rememberElMat.
     */
140 141
    inline void rememberElementMatrix(bool rem) { 
      rememberElMat = rem; 
142
    }
143 144 145 146

    /** \brief
     * Sets \ref rememberElVec.
     */
147 148
    inline void rememberElementVector(bool rem) { 
      rememberElVec = rem; 
149
    }
150 151 152 153 154 155

    /** \brief
     * Returns \ref zeroOrderAssembler.
     */
    inline ZeroOrderAssembler* getZeroOrderAssembler() {
      return zeroOrderAssembler;
156
    }
157 158 159 160 161

    /** \brief
     * Returns \ref firstOrderAssemblerGrdPsi or \ref firstOrderAssemblerGrdPhi
     * depending on type.
     */
162
    inline FirstOrderAssembler* getFirstOrderAssembler(FirstOrderType type = GRD_PSI) 
163
    {
164
      return (type == GRD_PSI) ? 
165 166
	firstOrderAssemblerGrdPsi : 
	firstOrderAssemblerGrdPhi;
167
    }
168 169 170 171 172 173

    /** \brief
     * Returns \ref secondOrderAssembler.
     */
    inline SecondOrderAssembler* getSecondOrderAssembler() {
      return secondOrderAssembler;
174
    }
175 176 177 178

    /** \brief
     * Returns \ref operat;
     */
179 180
    inline Operator* getOperator() { 
      return operat; 
181
    }
182 183 184 185 186

    /** \brief
     * Initialisation for the given ElInfo. The call is deligated to
     * the sub assemblers.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
187 188
    void initElement(const ElInfo *smallElInfo,
		     const ElInfo *largeElInfo = NULL,
189 190 191 192 193 194 195 196 197 198
		     Quadrature *quad = NULL);

    /** \brief
     * Sets quadratures of all sub assemblers.
     */
    void setQuadratures(Quadrature *quad2,
			Quadrature *quad1GrdPsi,
			Quadrature *quad1GrdPhi,
			Quadrature *quad0) 
    {
199
      if (secondOrderAssembler) {
200 201 202 203
	TEST_EXIT(!secondOrderAssembler->getQuadrature())
	  ("quadrature already existing\n");
	secondOrderAssembler->setQuadrature(quad2);
      }
204
      if (firstOrderAssemblerGrdPsi) {
205 206 207 208
	TEST_EXIT(!firstOrderAssemblerGrdPsi->getQuadrature())
	  ("quadrature already existing\n");
	firstOrderAssemblerGrdPsi->setQuadrature(quad1GrdPsi);
      }
209
      if (firstOrderAssemblerGrdPhi) {
210 211 212 213
	TEST_EXIT(!firstOrderAssemblerGrdPhi->getQuadrature())
	  ("quadrature already existing\n");
	firstOrderAssemblerGrdPhi->setQuadrature(quad1GrdPhi);
      }
214
      if (zeroOrderAssembler) {
215 216 217 218 219 220 221 222 223 224 225 226 227
	TEST_EXIT(!zeroOrderAssembler->getQuadrature())
	  ("quadrature already existing\n");
	zeroOrderAssembler->setQuadrature(quad0);
      }
    };

  protected:
    /** \brief
     * Vector assembling by element matrix-vector multiplication. 
     * Usefull if an element matrix was already calculated.
     */
    void matVecAssemble(const ElInfo *elInfo, ElementVector *vec);

Thomas Witkowski's avatar
Thomas Witkowski committed
228 229 230 231 232

    void matVecAssemble(const ElInfo *mainElInfo, const ElInfo *auxElInfo,
			const ElInfo *smallElInfo, const ElInfo *largeElInfo,
			ElementVector *vec);

233
    /// Checks whether this is a new travese.
234
    inline void checkForNewTraverse() {
235
      if (lastTraverseId != ElInfo::traverseId[omp_get_thread_num()]) {
236
	lastVecEl = lastMatEl = NULL;
237
	lastTraverseId = ElInfo::traverseId[omp_get_thread_num()];
238
      }
239
    }
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

    /** \brief
     * Checks whether quadratures for sub assemblers are already set.
     * If not they will be created.
     */
    void checkQuadratures();

  protected:
    /** \brief
     * Operator this Assembler belongs to.
     */
    Operator *operat;

    /** \brief
     * Row FiniteElemSpace.
     */
    const FiniteElemSpace *rowFESpace;

    /** \brief
     * Column FiniteElemSpace.
     */
    const FiniteElemSpace *colFESpace;

    /** \brief
     * Number of rows.
     */
    int nRow;

    /** \brief
     * Number of columns.
     */
    int nCol;

    /** \brief
     * SubAssembler for the second order terms
     */
    SecondOrderAssembler *secondOrderAssembler;

    /** \brief
     * SubAssembler for the first order terms (grdPsi)
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
281
    FirstOrderAssembler *firstOrderAssemblerGrdPsi;
282 283 284 285

    /** \brief
     * SubAssembler for the first order terms (grdPhi)
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
286
    FirstOrderAssembler *firstOrderAssemblerGrdPhi;  
287 288 289 290

    /** \brief
     * SubAssembler for the zero order terms
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
291
    ZeroOrderAssembler *zeroOrderAssembler;
292

Thomas Witkowski's avatar
Thomas Witkowski committed
293 294 295
    /** \brief
     *
     */
296 297 298 299 300 301 302 303 304 305 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 351 352 353 354 355 356 357 358
    bool remember;

    /** \brief
     * Determines whether the element matrix should be stored locally.
     */
    bool rememberElMat;

    /** \brief
     * Determines whether the element vector should be stored locally.
     */
    bool rememberElVec;

    /** \brief
     * locally stored element matrix
     */
    ElementMatrix* elementMatrix;

    /** \brief
     * locally stored element vector
     */
    ElementVector* elementVector;
  
    /** \brief
     * Used to check whether \ref initElement() must be called, because
     * a new Element is visited.
     */
    Element* lastMatEl;

    /** \brief
     * Used to check whether \ref initElement() must be called, because
     * a new Element is visited.
     */
    Element* lastVecEl;

    /** \brief
     * Used to check for new traverse.
     */
    int lastTraverseId;

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

  // ============================================================================
  // ===== class StandardAssembler =============================================
  // ============================================================================

  /**
   * \ingroup Assembler
   *
   * \brief
   * Assembler using non optimized sub assemblers.
   */
  class StandardAssembler : public Assembler
  {
  public:
    MEMORY_MANAGED(StandardAssembler);

    /** \brief
     * Constructor.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
359
    StandardAssembler(Operator *op,
360 361 362 363
		      Quadrature *quad2,
		      Quadrature *quad1GrdPsi,
		      Quadrature *quad1GrdPhi,
		      Quadrature *quad0,
Thomas Witkowski's avatar
Thomas Witkowski committed
364 365
		      const FiniteElemSpace *rowFESpace,
		      const FiniteElemSpace *colFESpace = NULL);
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
  };

  // ============================================================================
  // ===== class OptimizedAssembler =============================================
  // ============================================================================

  /**
   * \ingroup Assembler
   *
   * \brief
   * Assembler using optimized sub assemblers.
   */
  class OptimizedAssembler : public Assembler
  {
  public:
    MEMORY_MANAGED(OptimizedAssembler);

    /** \brief
     * Constructor.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
386
    OptimizedAssembler(Operator *op,
387 388 389 390
		       Quadrature *quad2,
		       Quadrature *quad1GrdPsi,
		       Quadrature *quad1GrdPhi,
		       Quadrature *quad0,
Thomas Witkowski's avatar
Thomas Witkowski committed
391 392
		       const FiniteElemSpace *rowFESpace,
		       const FiniteElemSpace *colFESpace = NULL);
393 394 395 396 397
  };

}

#endif // AMDIS_ASSEMBLER_H