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

/** \file BiCGStab2.h */

#ifndef AMDIS_BiCGStab2_H
#define AMDIS_BiCGStab2_H

#include "OEMSolver.h"
#include "MemoryManager.h"

namespace AMDiS
{

  // ============================================================================
  // ===== class BiCGStab2 ======================================================
  // ============================================================================

  /**
   * \ingroup Solver
   *
   * \brief
   * Solves a linear system by the stabilized BiCG method.
   * Can be used for general non-singular system matrices.
   */
  template<typename VectorType>
  class BiCGStab2 : public OEMSolver<VectorType>
  {
  public:
    MEMORY_MANAGED(BiCGStab2<VectorType>);

    /** \brief
     * Creator class used in the OEMSolverMap.
     */
    class Creator : public OEMSolverCreator<VectorType>
    {
    public:
      MEMORY_MANAGED(Creator);

      virtual ~Creator() {};

      /** \brief
       * Returns a new BiCGStab2 object.
       */
      OEMSolver<VectorType>* create()
      {
	return NEW BiCGStab2<VectorType>(this->name); 
      };
    };

    /** \brief
     * constructor
     */
    BiCGStab2(std::string name);

    /** \brief
     * destructor
     */
    ~BiCGStab2();

  protected:
    /** \brief
     * realisation of OEMSolver::solveSystem
     */
    int solveSystem(MatVecMultiplier<VectorType> *mv,
		    VectorType *x, VectorType *b, bool reuseMatrix);

    /** \brief
     * realisation of OEMSolver::init
     */
    void init();

    /** \brief
     * realisation of OEMSolver::exit
     */
    void exit();

  private:
    // pointer to memory needed for solveSystem
    VectorType *r, *rstar, *u, *v, *s, *w, *t, *xmin;
  };

}

#include "BiCGStab2.hh"

#endif // AMDIS_BiCGStab2_H