// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ==  http://www.amdis-fem.org                                              ==
// ==                                                                        ==
// ============================================================================
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.



/** \file NonLinSolver.h */

#ifndef AMDIS_NONLINSOLVER_H
#define AMDIS_NONLINSOLVER_H

#if 0 

#include <string>

#include "Global.h"
#include "CreatorInterface.h"
#include "AMDiS_fwd.h"
#include "OEMSolver.h"

namespace AMDiS {

  template<typename VectorType> class NonLinUpdater;

  /** 
   * \ingroup Solver
   *
   * \brief
   * Pure virtual base class for Newton, NewtonS and Banach which all
   * solves non linear equation systems. Sub classes must implement the methods
   * \ref init, \ref exit and \ref nlsolve.
   */
  template<typename VectorType>
  class NonLinSolver
  {
  public:
    /** \brief
     * constructor.
     * \param name name of this solver
     */
    NonLinSolver(std::string name_, 
		 OEMSolver *linSolver_,
		 NonLinUpdater<VectorType> *updater);

    /// destructor
    virtual ~NonLinSolver() {}

    /// solves the non linear system. Uses sub class methods
    inline int solve(MatVecMultiplier<VectorType> *matVec,
		     VectorType *x, VectorType *rhs,		   
		     Preconditioner<VectorType> *lPrecon = NULL,
		     Preconditioner<VectorType> *rPrecon = NULL) 
    {
      TEST_EXIT(vectorCreator)("no vectorCreator\n");
      init();
      int result = nlsolve(matVec, x, rhs, lPrecon, rPrecon);
      exit();
      return result;
    }

    inline void setTolerance(double tol) 
    {
      tolerance = tol;
    }

    inline double getTolerance() 
    {
      return tolerance;
    }

    inline void setVectorCreator(CreatorInterface<VectorType> *creator) 
    {
      vectorCreator = creator;
    }

    inline OEMSolver *getLinearSolver() 
    { 
      return linSolver; 
    }

    inline void setNonLinUpdater(NonLinUpdater<VectorType> *up) 
    {
      nonLinUpdater = up;
    }

    inline NonLinUpdater<VectorType> *getNonLinUpdater() 
    {
      return nonLinUpdater;
    }

  protected:
    /// Allocates needed memory. Must be overriden in sub classes.
    virtual void init() = 0;

    /// Solves the non linear system. Must be overriden in sub classes.

    virtual int  nlsolve(MatVecMultiplier<VectorType> *matVec,
			 VectorType *x, VectorType *rhs,
			 Preconditioner<VectorType> *lPrecon = NULL,
			 Preconditioner<VectorType> *rPrecon = NULL) = 0;

    /// Frees needed memory. Must be overriden in sub classes.
    virtual void exit() = 0;
  
    virtual int solveLinearSystem(MatVecMultiplier<VectorType> *matVec,
				  VectorType *fh, VectorType *x,
				  Preconditioner<VectorType> *lPrecon = NULL,
				  Preconditioner<VectorType> *rPrecon = NULL)
    {
      FUNCNAME("NonLinSolver::solveLinearSystem()");
      TEST_EXIT(linSolver)("no solver\n");
      ERROR_EXIT("Not yet re-implemented!\n");
      return -1;
    }

  protected:
    std::string        name;             /**< \brief name of the solver */
    OEMSolver *linSolver;        /**< \brief linear solver*/
    NonLinUpdater<VectorType> *nonLinUpdater;    /**< \brief non linear updater */
    double             tolerance;        /**< \brief solver tolerance */
    int                maxIter;          /**< \brief maximal # of iterations */
    int                info;             /**< \brief info level */
    double             initial_residual; /**< \brief initial residual */
    double             residual;         /**< \brief current residual */
    Norm               usedNorm;         /**< \brief used norm */

    CreatorInterface<VectorType> *vectorCreator;
  };


  /// Interface for creators of concrete NonLinSolvers. 
  template<typename VectorType>
  class NonLinSolverCreator : public CreatorInterface<NonLinSolver<VectorType> >
  { 
  public:
    virtual ~NonLinSolverCreator() {}

    void setName(std::string name_) 
    { 
      name = name_; 
    }

    void setLinearSolver(OEMSolver *solver) 
    { 
      linearSolver = solver; 
    }

    void setNonLinUpdater(NonLinUpdater<VectorType> *updater) 
    {
      nonLinUpdater = updater;
    }

  protected:
    std::string name;
    OEMSolver *linearSolver;
    NonLinUpdater<VectorType> *nonLinUpdater;
  };

}

#include "NonLinSolver.hh"

#include "Newton.h"
#include "NewtonS.h"

#endif

#endif // AMDIS_NONLINSOLVER_H