// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  crystal growth group                                                  ==
// ==                                                                        ==
// ==  Stiftung caesar                                                       ==
// ==  Ludwig-Erhard-Allee 2                                                 ==
// ==  53175 Bonn                                                            ==
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  http://www.caesar.de/cg/AMDiS                                         ==
// ==                                                                        ==
// ============================================================================

/** \file CGSolver.h */

#ifndef AMDIS_CGSOLVER_H
#define AMDIS_CGSOLVER_H

namespace AMDiS {
  
  #include "OEMSolver.h"
  #include "MemoryManager.h"

  // ============================================================================
  // ===== class CGSolver =======================================================
  // ============================================================================
  
  /**
   * \ingroup Solver
   * 
   * \brief
   * Solves a linear system by the conjugate gradient method and can be used for
   * symmetric positive definite system matrices.
   *
   * The implementation is based on the following book: "Numerik linearer
   * Gleichungssystene", 2. Auflage, Andreas Meister. The algorithm is described
   * on page 124. The extension to the preconditioned cg method (pcg) is described
   * on page 207 in the same book. We here use the same variable names, but without 
   * subscription.
   */
  template<typename VectorType>
  class CGSolver : public OEMSolver<VectorType>
  {
  public:
    MEMORY_MANAGED(CGSolver<VectorType>);

    /** \brief
     * Creator class used in the OEMSolverMap.
     */
    class Creator : public OEMSolverCreator<VectorType>
    {
    public:
      MEMORY_MANAGED(Creator);
      
      virtual ~Creator() {};
      
      /** \brief
       * Returns a new CGSolver object.
       */
      OEMSolver<VectorType>* create() { 
	return NEW CGSolver<VectorType>(this->name); 
      };
    };

    /** \brief
     * constructor
     */
    CGSolver(::std::string name);
    
    /** \brief
     * destructor
     */
    ~CGSolver();

  protected:
    /** \brief
     * Implements OEMSolver<VectorType>::init().
     */
    void init() {
      p = this->vectorCreator->create();
      r = this->vectorCreator->create();
      v = this->vectorCreator->create();
      z = this->vectorCreator->create();
    };
    
    /** \brief
     * Implements OEMSolver<VectorType>::exit().
     */
    void exit() {
      this->vectorCreator->free(p);
      this->vectorCreator->free(r);
      this->vectorCreator->free(v);
      this->vectorCreator->free(z);
    };

    /** \brief
     * Implements OEMSolver<VectorType>::solve().
     */
    int solveSystem(MatVecMultiplier<VectorType> *mv, VectorType *x, VectorType *b);

  private:
    VectorType *p, *r, *v, *z;
  };
}

#include "CGSolver.hh"

#endif // AMDIS_CGSOLVER_H