// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  TU Dresden                                                            ==
// ==                                                                        ==
// ==  Institut f�r Wissenschaftliches Rechnen                               ==
// ==  Zellescher Weg 12-14                                                  ==
// ==  01069 Dresden                                                         ==
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  https://gforge.zih.tu-dresden.de/projects/amdis/                      ==
// ==                                                                        ==
// ============================================================================

/** \file BoundaryManager.h */

#ifndef AMDIS_BOUNDARYMANAGER_H
#define AMDIS_BOUNDARYMANAGER_H

#include <map>

#include "AMDiS_fwd.h"
#include "Boundary.h"
#include "BoundaryCondition.h"

namespace AMDiS {

  typedef std::map<BoundaryType, BoundaryCondition*> BoundaryIndexMap;

  /**
   * \ingroup Assembler
   *
   * \brief
   * A BoundaryManager handles a set of boundary conditions and applies
   * this conditions to DOFVectorBase and DOFMatrix objects. Each DOFVectorBase
   * and each DOFMatrix has its own BoundaryManager.
   */
  class BoundaryManager
  {
  public:
    BoundaryManager(const FiniteElemSpace *feSpace);

    BoundaryManager(BoundaryManager &bm);

    ~BoundaryManager();

    /// Adds a local boundary condition to the list of managed conditions.
    void addBoundaryCondition(BoundaryCondition *localBC) 
    {
      FUNCNAME("BoundaryManager::addBoundaryCondition()");

      BoundaryType type = localBC->getBoundaryType();
      TEST_EXIT(localBCs[type] == NULL)
	("There is already a condition for this type.\n");
      localBCs[type] = localBC;

      std::vector<BoundaryCondition*>& boundMap = globalBoundaryMap[type];
      boundMap.push_back(localBC);
    }

    void initMatrix(DOFMatrix *matrix);

    void exitMatrix(DOFMatrix *matrix);

    void initVector(DOFVectorBase<double> *vector);

    void exitVector(DOFVectorBase<double> *vector);

    /** \brief
     * Calls DOFVectorBase::fillBoundaryCondition() for each local boundary condition
     * in \ref localBCs.
     */
    void fillBoundaryConditions(ElInfo *elInfo, DOFVectorBase<double> *vec);

    /** \brief
     * Calls DOFMatrix::fillBoundaryCondition() for each local boundary condition
     * in \ref localBCs.
     */
    void fillBoundaryConditions(ElInfo *elInfo, DOFMatrix *mat);

    /** \brief
     * Calls BoundaryCondition::boundResidual() for each boundary condition in 
     * \ref localBCs.
     */
    double boundResidual(ElInfo *elInfo, DOFMatrix *matrix, 
			 const DOFVectorBase<double> *dv);

    inline BoundaryCondition *getBoundaryCondition(BoundaryType type) 
    {
      return localBCs[type];
    }

    const BoundaryIndexMap& getBoundaryConditionMap() 
    {
      return localBCs;
    }

    void setBoundaryConditionMap(const BoundaryIndexMap& bcs) 
    {
      localBCs = bcs;
    }
    
    /** \brief
     * Returns true, if there is at least one boundary object with the given boundary
     * id, which implements a periodic boundary.
     */
    static bool isBoundaryPeriodic(BoundaryType b)
    {
      for (int i = 0; i < static_cast<int>(globalBoundaryMap[b].size()); i++)
	if (globalBoundaryMap[b][i]->isPeriodic())
	  return true;

      return false;
    }

  protected:
    /// Map of managed local boundary conditions.
    BoundaryIndexMap localBCs;

    /// Temporary thread-safe variable for functions fillBoundaryconditions.
    std::vector<BoundaryType*> localBounds;

    /// Temporary thread-safe variable for functions fillBoundaryconditions.
    std::vector<std::vector<DegreeOfFreedom> > dofIndices;

    /** \brief
     * Stores the number of byte that were allocated in the constructor for
     * each localBounds value. Is used to free the memory in the destructor.
     */
    int allocatedMemoryLocalBounds;

    /** \brief
     * For every boundary id we store here all possible boundary object (although
     * it's not clear if it is meaningful to have different boundary conditions on the
     * same boundary id). 
     *
     * We have to use this global variable, because the mesh traverse interface does 
     * not provide more information about traversed boundaries at elements than the
     * boundary id.
     *
     * TODO: Change interface such that mesh traverse returns the boundary objects
     * directly and we can remove this global variable. The biggest problem will be
     * than serialization and deserialization of the mesh.
     */
    static std::map<BoundaryType, std::vector<BoundaryCondition*> > globalBoundaryMap;
  };

}

#endif