Assembler.h 31.9 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 36 37 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
// ============================================================================
// ==                                                                        ==
// == 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"
#include "Operator.h"

namespace AMDiS {

  class ElInfo;
  class Element;
  class Assembler;
  class Quadrature;
  class FastQuadrature;
  class FiniteElemSpace;
  class ElementMatrix;
  class ElementVector;
  class BasisFunction;
  class Q00PsiPhi;
  class Q10PsiPhi;
  class Q01PsiPhi;
  class Q11PsiPhi;
  class Q0Psi;
  class Q1Psi;
  class Q2Psi;
  // class Operator;
  // class OperatorTerm;

  template<typename T> class DOFVectorBase;

  // enum FirstOrderType;

  // ============================================================================
  // ===== class SubAssembler ===================================================
  // ============================================================================

  /**
   * \ingroup Assembler
   * 
   * \brief
   * Base class for SecondOrderAssembler, FirstOrderAssembler, 
   * ZeroOrderAssembler. The task of a SubAssembler is to assemble a list of 
Thomas Witkowski's avatar
Thomas Witkowski committed
73
   * terms of a special order and add their contributions to a DOFMatrix or a 
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 113 114 115 116 117 118 119
   * DOFVector. An Assembler can consist of up to four SubAssemblers: one 
   * SecondOrderAssembler for second order terms, one ZeroOrderAssembler for 
   * terms of order zero, and two FirstOrderAssemblers. One for terms with
   * derivatives of the basis functions connected to to row DOFs and one for 
   * those connected to the column DOFs.
   */
  class SubAssembler
  {
  public:
    MEMORY_MANAGED(SubAssembler);

    /** \brief
     * Creates a SubAssembler belonging to assembler for the terms of given 
     * order of Operator op. If order is equal to one, type spezifies what kind 
     * of FirstOrderType are to assemble. During construction of a SubAssembler
     * the needs and properties of the terms are considered.
     */
    SubAssembler(Operator *op,
		 Assembler *assembler,
		 Quadrature *quadrat,
		 int order, 
		 bool optimized,
		 FirstOrderType type = GRD_PHI);

    /** \brief
     * Destructor
     */
    virtual ~SubAssembler() {};

    /** \brief
     * Calculates the element matrix for elInfo and adds it to mat. Memory
     * for mat must be provided by the caller.
     */
    virtual void calculateElementMatrix(const ElInfo *elInfo,
					ElementMatrix *mat) = 0;

    /** \brief
     * Calculates the element vector for elInfo and adds it to vec. Memory
     * for vec must be provided by the caller.
     */
    virtual void calculateElementVector(const ElInfo *elInfo,
					ElementVector *vec) = 0;

    /** \brief
     * Returns \ref terms
     */
120 121 122
    inline ::std::vector<OperatorTerm*> *getTerms() { 
      return &terms; 
    };
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 162 163 164 165 166 167 168 169 170 171

    /** \brief
     * Returns \ref quadrature.
     */ 
    inline Quadrature *getQuadrature() {
      return quadrature;
    };

    /** \brief
     * Sets \ref quadrature to q.
     */
    inline void setQuadrature(Quadrature* q) {
      quadrature = q;
    };
  
    /** \brief
     * Returns a vector with the world coordinates of the quadrature points
     * of \ref quadrature on the element of elInfo. 
     * Used by \ref OperatorTerm::initElement().
     */
    WorldVector<double>* getCoordsAtQPs(const ElInfo* elInfo, 
					Quadrature *quad = NULL);

    /** \brief
     * DOFVector dv evaluated at quadrature points.
     * Used by \ref OperatorTerm::initElement().
     */
    double* getVectorAtQPs(DOFVectorBase<double>* dv, const ElInfo* elInfo,
			   Quadrature *quad = NULL);

    /** \brief
     * Gradients of DOFVector dv evaluated at quadrature points.
     * Used by \ref OperatorTerm::initElement().
     */
    WorldVector<double>* getGradientsAtQPs(DOFVectorBase<double>* dv, 
					   const ElInfo* elInfo,
					   Quadrature *quad = NULL);

    /** \brief
     * Called once for each ElInfo when \ref calculateElementMatrix() or
     * \ref calculateElementVector() is called for the first time for this
     * Element.
     */
    virtual void initElement(const ElInfo *elInfo,
			     Quadrature *quad = NULL);

    /** \brief
     * Returns \ref psiFast.
     */
172 173 174
    const FastQuadrature *getPsiFast() const { 
      return psiFast; 
    };
175 176 177 178

    /** \brief
     * Returns \ref phiFast.
     */
179 180 181
    const FastQuadrature *getPhiFast() const { 
      return phiFast; 
    };
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 211 212 213 214 215 216 217 218 219 220 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 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

  protected:
    /** \brief
     * Updates \ref psiFast and \ref phiFast.
     */
    FastQuadrature *updateFastQuadrature(FastQuadrature *quadFast,
					 const BasisFunction *psi,
					 Flag updateFlag);
  
  protected:
    /** \brief
     * Problem dimension
     */
    int dim;

    /** \brief
     * Number of rows of the element matrix and length of the element
     * vector. Is equal to the number of row basis functions
     */
    int nRow;

    /** \brief
     * Number of columns of the element matrix. Is equal to the number
     * of column basis functions
     */
    int nCol;

    /** \brief
     * Used for \ref getVectorAtQPs().
     */
    class ValuesAtQPs {
    public:
      Vector<double> values;
      bool valid;
    };

    /** \brief
     * Used for \ref getGradientsAtQPs().
     */
    class GradientsAtQPs {
    public:
      Vector<WorldVector<double> > values;
      bool valid;
    };

    /** \brief
     * Used for \ref getVectorAtQPs().
     */
    ::std::map<const DOFVectorBase<double>*, ValuesAtQPs*> valuesAtQPs;

    /** \brief
     * Used for \ref getGradientsAtQPs().
     */
    ::std::map<const DOFVectorBase<double>*, GradientsAtQPs*> gradientsAtQPs;

    /** \brief
     * Set and updated by \ref initElement() for each ElInfo. 
     * coordsAtQPs[i] points to the coordinates of the i-th quadrature point.
     */
    WorldVector<double> *coordsAtQPs;

    /** \brief
     * Used for \ref getCoordsAtQPs().
     */
    bool coordsValid;

    /** \brief
     * Needed Quadrature. Constructed in the constructor of SubAssembler
     */
    Quadrature *quadrature;

    /** \brief
     * FastQuadrature for row basis functions
     */
    FastQuadrature *psiFast;

    /** \brief
     * FastQuadrature for column basis functions
     */
    FastQuadrature *phiFast;

    /** \brief
     * Corresponding Assembler
     */
    Assembler* owner;

    /** \brief
     * Flag that specifies whether the element matrix is symmetric.
     */
    bool symmetric;

    /** \brief
     * List of all terms with a contribution to this SubAssembler
     */
    ::std::vector<OperatorTerm*> terms;

278 279 280
    /** \brief
     *
     */
281 282
    bool opt;

283 284 285
    /** \brief
     *
     */
286 287 288 289 290 291 292 293 294 295 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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 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 455 456 457 458 459 460 461 462 463 464 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 514 515 516 517 518 519 520 521 522 523 524 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 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 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 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 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 826 827 828 829 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 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
    bool firstCall;

    friend class Assembler;
  };

  // ============================================================================
  // ===== class ZeroOrderAssembler =============================================
  // ============================================================================

  /**
   * \ingroup Assembler
   * 
   * \brief
   * SubAssembler for zero order terms.
   */
  class ZeroOrderAssembler : public SubAssembler
  {
  public:
    MEMORY_MANAGED(ZeroOrderAssembler);

    /** \brief
     * Creates and returns the ZeroOrderAssembler for Operator op and
     * the given assembler. If all terms are piecewise constant precalculated 
     * integrals can be used while assembling and the returned 
     * ZeroOrderAssembler is of type Pre0. Otherwise a Quad0 object will
     * be returned.
     */
    static ZeroOrderAssembler* getSubAssembler(Operator *op,
					       Assembler *assembler,
					       Quadrature *quadrat,
					       bool optimized);


    /** \brief
     * Destructor.
     */
    virtual ~ZeroOrderAssembler() {};

  protected:
    /** \brief
     * Constructor.
     */
    ZeroOrderAssembler(Operator *op, 
		       Assembler *assembler, 
		       Quadrature *quadrat,
		       bool optimized);

  protected:
    /** \brief
     * List of all yet created optimized SubAssembler objects.
     */
    static ::std::vector<SubAssembler*> optimizedSubAssemblers;

    /** \brief
     * List of all yet created standard SubAssembler objects.
     */ 
    static ::std::vector<SubAssembler*> standardSubAssemblers;
  };

  // ============================================================================
  // ===== class Stand0 =========================================================
  // ============================================================================

  /**
   * \ingroup Assembler
   * 
   * \brief
   * Standard zero order assembler.
   */
  class Stand0 :  public ZeroOrderAssembler 
  {
  public:
    MEMORY_MANAGED(Stand0);

    /** \brief
     * Constructor.
     */
    Stand0(Operator *op, Assembler *assembler, Quadrature *quad);

    /** \brief
     * Implements SubAssembler::calculateElementMatrix().
     */
    void calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat);

    /** \brief
     * Implements SubAssembler::calculateElementVector().
     */
    void calculateElementVector(const ElInfo *elInfo, ElementVector *vec);
  };

  // ============================================================================
  // ===== class Quad0 ==========================================================
  // ============================================================================

  /** 
   * \ingroup Assembler
   *
   * \brief
   * Zero order assembler using fast quadratures.
   */
  class Quad0 :  public ZeroOrderAssembler 
  {
  public:
    MEMORY_MANAGED(Quad0);

    /** \brief
     * Constructor.
     */
    Quad0(Operator *op, Assembler *assembler, Quadrature *quad);

    /** \brief
     * Implements SubAssembler::calculateElementMatrix().
     */
    void calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat);

    /** \brief
     * Implements SubAssembler::calculateElementVector().
     */
    void calculateElementVector(const ElInfo *elInfo, ElementVector *vec);
  };


  // ============================================================================
  // ===== class Pre0 ===========================================================
  // ============================================================================

  /**
   * \ingroup Assembler
   * 
   * \brief
   * Zero order assembler using precaculated integrals.
   */
  class Pre0 : public ZeroOrderAssembler
  {
  public:
    MEMORY_MANAGED(Pre0);

    /** \brief
     * Constructor.
     */
    Pre0(Operator *op, Assembler *assembler, Quadrature *quad);

    /** \brief
     * Implements SubAssembler::calculateElementMatrix().
     */
    void calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat);

    /** \brief
     * Implements SubAssembler::calculateElementVector().
     */
    void calculateElementVector(const ElInfo *elInfo, ElementVector *vec);

  protected:
    /** \brief
     * Integral of the product of psi and phi.
     */
    const Q00PsiPhi *q00;

    /** \brief
     * Integral of psi.
     */
    const Q0Psi *q0;
 
    friend class ZeroOrderAssembler;
  };

  // ============================================================================
  // ===== class FirstOrderAssembler ============================================
  // ============================================================================

  /**
   * \ingroup Assembler
   * 
   * \brief
   * SubAssembler for first order terms.
   */
  class FirstOrderAssembler : public SubAssembler
  {
  public:
    MEMORY_MANAGED(FirstOrderAssembler);

    /** \brief
     * Creates and returns the FirstOrderAssembler for Operator op and
     * the given assembler. If all terms are piecewise constant precalculated 
     * integrals can be used while assembling and the returned 
     * ZeroOrderAssembler is of type Pre0. Otherwise a Quad0 object will
     * be returned.
     */
    static FirstOrderAssembler* getSubAssembler(Operator *op,
						Assembler *assembler,
						Quadrature *quadrat,
						FirstOrderType type,
						bool optimized);
  
    /** \brief
     * Destructor.
     */
    virtual ~FirstOrderAssembler() {};

  protected:
    /** \brief
     * Constructor.
     */
    FirstOrderAssembler(Operator *op,
			Assembler *assembler,
			Quadrature *quadrat,
			bool optimized,
			FirstOrderType type);


  protected:
    /** \brief
     * List of all yet created optimized zero order assemblers for grdPsi.
     */
    static ::std::vector<SubAssembler*> optimizedSubAssemblersGrdPsi;

    /** \brief
     * List of all yet created standard zero order assemblers for grdPsi.
     */
    static ::std::vector<SubAssembler*> standardSubAssemblersGrdPsi;

    /** \brief
     * List of all yet created optimized zero order assemblers for grdPhi.
     */
    static ::std::vector<SubAssembler*> optimizedSubAssemblersGrdPhi;

    /** \brief
     * List of all yet created standard zero order assemblers for grdPhi.
     */
    static ::std::vector<SubAssembler*> standardSubAssemblersGrdPhi;
  };

  // ============================================================================
  // ===== class Stand10 ========================================================
  // ============================================================================

  /**
   * \ingroup Assembler
   * 
   * \brief
   * Standard first order assembler for grdPsi.
   */
  class Stand10 : public FirstOrderAssembler
  {
  public:
    MEMORY_MANAGED(Stand10);

    /** \brief
     * Constructor.
     */
    Stand10(Operator *op, Assembler *assembler, Quadrature *quad);

    /** \brief
     * Implements SubAssembler::calculateElementMatrix().
     */
    void calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat);

    /** \brief
     * Implements SubAssembler::calculateElementVector().
     */
    void calculateElementVector(const ElInfo *, ElementVector */*vec*/);
  };

  // ============================================================================
  // ===== class Stand10 ========================================================
  // ============================================================================

  /**
   * \ingroup Assembler
   * 
   * \brief
   * Standard first order assembler for grdPhi.
   */
  class Stand01 : public FirstOrderAssembler
  {
  public:
    MEMORY_MANAGED(Stand01);

    /** \brief
     * Constructor.
     */
    Stand01(Operator *op, Assembler *assembler, Quadrature *quad);

    /** \brief
     * Implements SubAssembler::calculateElementMatrix().
     */
    void calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat);

    /** \brief
     * Implements SubAssembler::calculateElementVector().
     */
    void calculateElementVector(const ElInfo *, ElementVector *) {
      ERROR_EXIT("should not be called\n");
    };
  };

  // ============================================================================
  // ===== class Quad10 =========================================================
  // ============================================================================

  /** \brief
   * First order assembler for grdPsi using fast quadratures.
   */
  class Quad10 : public FirstOrderAssembler
  {
  public:
    MEMORY_MANAGED(Quad10);

    /** \brief
     * Constructor.
     */
    Quad10(Operator *op, Assembler *assembler, Quadrature *quad);

    /** \brief
     * Implements SubAssembler::calculateElementMatrix().
     */
    void calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat);

    /** \brief
     * Implements SubAssembler::calculateElementVector().
     */
    void calculateElementVector(const ElInfo *, ElementVector */*vec*/);
  };

  // ============================================================================
  // ===== class Quad01 =========================================================
  // ============================================================================

  /** \brief
   * First order assembler for grdPhi using fast quadratures.
   */
  class Quad01 : public FirstOrderAssembler 
  {
  public:
    MEMORY_MANAGED(Quad01);

    /** \brief
     * Constructor.
     */
    Quad01(Operator *op, Assembler *assembler, Quadrature *quad);

    /** \brief
     * Implements SubAssembler::calculateElementMatrix().
     */
    void calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat);

    /** \brief
     * Implements SubAssembler::calculateElementVector().
     */
    void calculateElementVector(const ElInfo *, ElementVector */*vec*/) {
      ERROR_EXIT("should not be called\n");
    };
  };

  // ============================================================================
  // ===== class Pre10 ==========================================================
  // ============================================================================

  /** \brief
   * First order assembler for grdPsi using precalculated integrals
   */
  class Pre10 : public FirstOrderAssembler 
  {
  public:
    MEMORY_MANAGED(Pre10);

    /** \brief
     * Constructor.
     */
    Pre10(Operator *op, Assembler *assembler, Quadrature *quad);

    /** \brief
     * Implements SubAssembler::calculateElementMatrix().
     */
    void calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat);

    /** \brief
     * Implements SubAssembler::calculateElementVector().
     */
    void calculateElementVector(const ElInfo *, ElementVector */*vec*/);

  protected:
    /** \brief
     * Integral of the product of the derivative of psi and phi.
     */
    const Q10PsiPhi *q10;

    /** \brief
     * Integral of the derivative of psi.
     */
    const Q1Psi *q1;

    friend class FirstOrderAssembler;
  };

  // ============================================================================
  // ===== class Pre01 ==========================================================
  // ============================================================================

  /** \brief
   *  First order assembler for grdPhi using precalculated integrals
   */
  class Pre01 : public FirstOrderAssembler 
  {
  public:
    MEMORY_MANAGED(Pre01);

    /** \brief
     * Constructor.
     */
    Pre01(Operator *op, Assembler *assembler, Quadrature *quad);

    /** \brief
     * Implements SubAssembler::calculateElementMatrix().
     */
    void calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat);

    /** \brief
     * Implements SubAssembler::calculateElementVector().
     */
    void calculateElementVector(const ElInfo *, ElementVector */*vec*/) {
      ERROR_EXIT("should not be called\n");
    };

  protected:
    /** \brief
     * Integral of the product of psi and the derivative of phi.
     */
    const Q01PsiPhi *q01;

    /** \brief
     * Integral of the derivative of phi.
     */
    const Q1Psi *q1;

    friend class FirstOrderAssembler;
  };

  // ============================================================================
  // ===== class SecondOrderAssembler ===========================================
  // ============================================================================

  /**
   * \ingroup Assembler
   * 
   * \brief
   * SubAssembler for second order terms.
   */
  class SecondOrderAssembler : public SubAssembler
  {
  public:
    MEMORY_MANAGED(SecondOrderAssembler);

    /** \brief
     * Creates and returns the SecondOrderAssembler for Operator op and
     * the given assembler. If all terms are piecewise constant precalculated 
     * integrals can be used while assembling and the returned 
     * ZeroOrderAssembler is of type Pre0. Otherwise a Quad0 object will
     * be returned.
     */
    static SecondOrderAssembler* getSubAssembler(Operator *op,
						 Assembler *assembler,
						 Quadrature *quadrat,
						 bool optimized);

    /** \brief
     * Destructor.
     */
    virtual ~SecondOrderAssembler() {};

  protected:
    /** \brief
     * Constructor.
     */
    SecondOrderAssembler(Operator *op,
			 Assembler *assembler,
			 Quadrature *quadrat,
			 bool optimized);

  protected:
    /** \brief
     * List of all yet created optimized second order assemblers.
     */
    static ::std::vector<SubAssembler*> optimizedSubAssemblers;

    /** \brief
     * List of all yet created standard second order assemblers.
     */
    static ::std::vector<SubAssembler*> standardSubAssemblers;
  };

  // ============================================================================
  // ===== class Stand2 =========================================================
  // ============================================================================

  /**
   * \ingroup Assembler
   * 
   * \brief
   * Standard second order assembler
   */
  class Stand2 : public SecondOrderAssembler 
  {
  public:
    MEMORY_MANAGED(Stand2);

    /** \brief
     * Constructor.
     */
    Stand2(Operator *op, Assembler *assembler, Quadrature *quad);

    /** \brief
     * Implements SubAssembler::calculateElementMatrix().
     */
    void calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat);

    /** \brief
     * Implements SubAssembler::calculateElementVector().
     */
    void calculateElementVector(const ElInfo *, ElementVector */*vec*/) {
      ERROR_EXIT("should not be called\n");
    };
  };

  // ============================================================================
  // ===== class Quad2 ==========================================================
  // ============================================================================

  /** \brief
   * Second order assembler using fast quadratures.
   */
  class Quad2 : public SecondOrderAssembler 
  {
  public:
    MEMORY_MANAGED(Quad2);

    /** \brief
     * Constructor.
     */
    Quad2(Operator *op, Assembler *assembler, Quadrature *quad);

    /** \brief
     * Implements SubAssembler::calculateElementMatrix().
     */
    void calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat);

    /** \brief
     * Implements SubAssembler::calculateElementVector().
     */
    void calculateElementVector(const ElInfo *, ElementVector */*vec*/) {
      ERROR_EXIT("should not be called\n");
    };
  };

  // ============================================================================
  // ===== class Pre2 ===========================================================
  // ============================================================================

  /** \brief
   * Second order assembler using predefined integrals.
   */
  class Pre2 : public SecondOrderAssembler 
  {
  public:
    MEMORY_MANAGED(Pre2);

    /** \brief
     * Constructor.
     */
    Pre2(Operator *op, Assembler *assembler, Quadrature *quad);

    /** \brief
     * Implements SubAssembler::calculateElementMatrix().
     */
    void calculateElementMatrix(const ElInfo *elInfo, ElementMatrix *mat);

    /** \brief
     * Implements SubAssembler::calculateElementVector().
     */
    void calculateElementVector(const ElInfo *, ElementVector */*vec*/) {
      ERROR_EXIT("should not be called\n");
    };

  protected:
    /** \brief
     * Integral of the product of the derivative of psi and the derivative 
     * of phi.
     */
    const Q11PsiPhi *q11;

    friend class SecondOrderAssembler;
  };

  // ============================================================================
  // ===== class Assembler ======================================================
  // ============================================================================

  /**
   * \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.
     */
    Assembler(Operator  *op,
	      const FiniteElemSpace *rowFESpace_,
	      const FiniteElemSpace *colFESpace_=NULL);

    virtual ~Assembler() {};

    ElementMatrix *initElementMatrix(ElementMatrix *elMat, 
				     const ElInfo *elInfo);


    ElementVector *initElementVector(ElementVector *elVec,
				     const ElInfo *elInfo);

    /** \brief
     * Assembles the element matrix for the given ElInfo
     */
    virtual void calculateElementMatrix(const ElInfo *elInfo, 
					ElementMatrix *userMat, 
					double factor=1.0);

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

    /** \brief
     * Returns \ref rowFESpace.
     */
929 930 931
    inline const FiniteElemSpace* getRowFESpace() { 
      return rowFESpace; 
    };
932 933 934 935

    /** \brief
     * Returns \ref colFESpace.
     */
936 937 938
    inline const FiniteElemSpace* getColFESpace() { 
      return colFESpace; 
    };
939 940 941 942

    /** \brief
     * Returns \ref nRow.
     */
943 944 945
    inline int getNRow() { 
      return nRow; 
    };
946 947 948 949

    /** \brief
     * Returns \ref nCol.
     */
950 951 952
    inline int getNCol() { 
      return nCol; 
    };
953 954 955 956

    /** \brief
     * Sets \ref rememberElMat.
     */
957 958 959
    inline void rememberElementMatrix(bool rem) { 
      rememberElMat = rem; 
    };
960 961 962 963

    /** \brief
     * Sets \ref rememberElVec.
     */
964 965 966
    inline void rememberElementVector(bool rem) { 
      rememberElVec = rem; 
    };
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997

    /** \brief
     * Returns \ref zeroOrderAssembler.
     */
    inline ZeroOrderAssembler* getZeroOrderAssembler() {
      return zeroOrderAssembler;
    };

    /** \brief
     * Returns \ref firstOrderAssemblerGrdPsi or \ref firstOrderAssemblerGrdPhi
     * depending on type.
     */
    inline FirstOrderAssembler* getFirstOrderAssembler(FirstOrderType type = 
						       GRD_PSI) 
    {
      return
	(type == GRD_PSI) ? 
	firstOrderAssemblerGrdPsi : 
	firstOrderAssemblerGrdPhi;
    };

    /** \brief
     * Returns \ref secondOrderAssembler.
     */
    inline SecondOrderAssembler* getSecondOrderAssembler() {
      return secondOrderAssembler;
    };

    /** \brief
     * Returns \ref operat;
     */
998 999 1000
    inline Operator* getOperator() { 
      return operat; 
    };
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016

    /** \brief
     * Initialisation for the given ElInfo. The call is deligated to
     * the sub assemblers.
     */
    void initElement(const ElInfo *elInfo,
		     Quadrature *quad = NULL);

    /** \brief
     * Sets quadratures of all sub assemblers.
     */
    void setQuadratures(Quadrature *quad2,
			Quadrature *quad1GrdPsi,
			Quadrature *quad1GrdPhi,
			Quadrature *quad0) 
    {
1017
      if (secondOrderAssembler) {
1018 1019 1020 1021
	TEST_EXIT(!secondOrderAssembler->getQuadrature())
	  ("quadrature already existing\n");
	secondOrderAssembler->setQuadrature(quad2);
      }
1022
      if (firstOrderAssemblerGrdPsi) {
1023 1024 1025 1026
	TEST_EXIT(!firstOrderAssemblerGrdPsi->getQuadrature())
	  ("quadrature already existing\n");
	firstOrderAssemblerGrdPsi->setQuadrature(quad1GrdPsi);
      }
1027
      if (firstOrderAssemblerGrdPhi) {
1028 1029 1030 1031
	TEST_EXIT(!firstOrderAssemblerGrdPhi->getQuadrature())
	  ("quadrature already existing\n");
	firstOrderAssemblerGrdPhi->setQuadrature(quad1GrdPhi);
      }
1032
      if (zeroOrderAssembler) {
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
	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);

    /** \brief
     * Checks whether this is a new travese.
     */
    inline void checkForNewTraverse() {
1050
      if (lastTraverseId != ElInfo::traverseId) {
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
	lastVecEl = lastMatEl = NULL;
	lastTraverseId = ElInfo::traverseId;
      }
    };

    /** \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)
     */
    FirstOrderAssembler  *firstOrderAssemblerGrdPsi;

    /** \brief
     * SubAssembler for the first order terms (grdPhi)
     */
    FirstOrderAssembler  *firstOrderAssemblerGrdPhi;  

    /** \brief
     * SubAssembler for the zero order terms
     */
    ZeroOrderAssembler   *zeroOrderAssembler;

    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.
     */
    StandardAssembler(Operator  *op,
		      Quadrature *quad2,
		      Quadrature *quad1GrdPsi,
		      Quadrature *quad1GrdPhi,
		      Quadrature *quad0,
		      const FiniteElemSpace *rowFESpace_,
		      const FiniteElemSpace *colFESpace_=NULL);
  };

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

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

    /** \brief
     * Constructor.
     */
    OptimizedAssembler(Operator  *op,
		       Quadrature *quad2,
		       Quadrature *quad1GrdPsi,
		       Quadrature *quad1GrdPhi,
		       Quadrature *quad0,
		       const FiniteElemSpace *rowFESpace_,
		       const FiniteElemSpace *colFESpace_=NULL);
  };

}

#endif // AMDIS_ASSEMBLER_H