Skip to content
Snippets Groups Projects
BFGS_Precond.h 4.45 KiB
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  crystal growth group                                                  ==
// ==                                                                        ==
// ==  Stiftung caesar                                                       ==
// ==  Ludwig-Erhard-Allee 2                                                 ==
// ==  53175 Bonn                                                            ==
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  http://www.caesar.de/cg/AMDiS                                         ==
// ==                                                                        ==
// ============================================================================

/** \file BFGS_Precond.h */

#ifndef AMDIS_BFGS_PRECONDITIONER_H
#define AMDIS_BFGS_PRECONDITIONER_H

#include "Parameters.h"
#include "Preconditioner.h"
#include "QN_Precond.h"

namespace AMDiS
{

  template<typename T> class DOFVector;
  template<typename T> class OEMSolver;

  // ============================================================================
  // ===== class BFGS_Vectors ===================================================
  // ============================================================================

  // Auxiliar class por storing pairs of DOFVectors and their scalar products.
  class BFGS_Vectors
  {
  public:
    MEMORY_MANAGED(BFGS_Vectors);

    /** \brief
     * Constructor.
     */
  BFGS_Vectors(const FiniteElemSpace *fe_space)
    : s(fe_space, "BFGS->s"), y(fe_space, "BFGS->y")
      {};

    inline void setVectors(DOFVector<double> *s_, DOFVector<double> *y_,
			   double sy_=0.0)
    {
      s = *s_;
      y = *y_;

      if (!sy_)
	sy = s * y;
      else
	sy = sy_;

      return;
    };

  private:
    DOFVector<double> s, y;
    double sy;

    friend class BFGS_Precond;
  };

  // ============================================================================
  // ===== class BFGS_Precond ===================================================
  // ============================================================================

  /**
   * \ingroup Solver
   *
   * \brief
   * BFGS preconditioner.
   */
  class BFGS_Precond : public PreconditionerScal
  {
  public:
    MEMORY_MANAGED(BFGS_Precond);

    /** \brief
     * Creator class used in the PreconditionerMap.
     */
    class Creator : public PreconditionerScalCreator
    {
    public:
      MEMORY_MANAGED(Creator);

      /** \brief
       * Creates a new BFGS Preconditioner.
       */
      PreconditionerScal *create()
      {
	return NEW BFGS_Precond(size, row);
      };
    };

    /** \brief
     * Constructor.
     */
  BFGS_Precond(int size_ = 1, int row_ = 0) : PreconditionerScal(size_, row_) {
      k = 0;
      
      m = 2 * Global::getGeo(WORLD);
      GET_PARAMETER(1, "preconditioner->max. number of pairs", "%d", &m);
      m = std::max(0, std::min(m, 32));
    };
    
    /** \brief
     * Destructor.
     */
    virtual ~BFGS_Precond() {};

    /** \brief
     * realisation of Preconditioner::init
     */
    inline void init()
    {
      FUNCNAME("BFGS_Precond::init()");
      TEST_EXIT(matrix[row])("no matrix\n");
    };

    /** \brief
     * realisation of Preconditioner::precon
     */
    void precon(DOFVector<double> *vec);

    /** \brief
     * realisation of Preconditioner::exit
     */
    inline void exit() {};

    /* ----- Specific functions ----- */

    /** \brief
     * add pair of vectors
     */
    void addPair(DOFVector<double> *s, DOFVector<double> *y, double sy = 0.0);

  protected:

    /** \brief
     * list of pairs
     */
    std::deque<BFGS_Vectors> pairsVec;

    /** \brief
     * maximal number of pairs
     */
    int m;

    /** \brief
     * actual number of pairs
     */
    int k;
  };

}

#endif // AMDIS_BFGS_PRECONDITIONER_H