<commit_before>// The libMesh Finite Element Library.
// Copyright (C) 2002-2014 <NAME>, <NAME>, <NAME>

// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA



#ifndef LIBMESH_EIGEN_SPARSE_LINEAR_SOLVER_H
#define LIBMESH_EIGEN_SPARSE_LINEAR_SOLVER_H

#include "libmesh/libmesh_common.h"

#ifdef LIBMESH_HAVE_EIGEN

// Eigen includes

// Local includes
#include "libmesh/linear_solver.h"
#include "libmesh/eigen_sparse_vector.h"
#include "libmesh/eigen_sparse_matrix.h"

// C++ includes

namespace libMesh
{



/**
 * This class provides an interface to Eigen
 * iterative solvers that is compatible with the \p libMesh
 * \p LinearSolver<>
 *
 * @author <NAME>, 2013
 */
template <typename T>
class EigenSparseLinearSolver : public LinearSolver<T>
{
public:
  /**
   *  Constructor. Initializes Eigen data structures
   */
  EigenSparseLinearSolver (const libMesh::Parallel::Communicator &comm_in
                           LIBMESH_CAN_DEFAULT_TO_COMMWORLD);

  /**
   * Destructor.
   */
  ~EigenSparseLinearSolver ();

  /**
   * Release all memory and clear data structures.
   */
  void clear ();

  /**
   * Initialize data structures if not done so already.
   */
  void init (const char* name=NULL);

  /**
   * Call the Eigen solver
   */
  std::pair<unsigned int, Real>
  solve (SparseMatrix<T>  &matrix,
         NumericVector<T> &solution,
         NumericVector<T> &rhs,
         const double tol,
         const unsigned int m_its);

  /**
   * Call the Eigen solver to solve A^T x = b
   */
  std::pair<unsigned int, Real>
  adjoint_solve (SparseMatrix<T>  &matrix,
                 NumericVector<T> &solution,
                 NumericVector<T> &rhs,
                 const double tol,
                 const unsigned int m_its);

  /**
   * Call the Eigen solver
   */
  std::pair<unsigned int, Real>
  solve (SparseMatrix<T>  &matrix,
         SparseMatrix<T>  &pc,
         NumericVector<T> &solution,
         NumericVector<T> &rhs,
         const double tol,
         const unsigned int m_its);

  /**
   * This function solves a system whose matrix is a shell matrix.
   */
  std::pair<unsigned int, Real>
  solve (const ShellMatrix<T>& shell_matrix,
         NumericVector<T>& solution_in,
         NumericVector<T>& rhs_in,
         const double tol,
         const unsigned int m_its);

  /**
   * This function solves a system whose matrix is a shell matrix, but
   * a sparse matrix is used as preconditioning matrix, this allowing
   * other preconditioners than JACOBI.
   */
  virtual std::pair<unsigned int, Real>
  solve (const ShellMatrix<T>& shell_matrix,
         const SparseMatrix<T>& precond_matrix,
         NumericVector<T>& solution_in,
         NumericVector<T>& rhs_in,
         const double tol,
         const unsigned int m_its);

  /**
   * Prints a useful message about why the latest linear solve
   * con(di)verged.
   */
  virtual void print_converged_reason() const;

  /**
   * Returns the solver's convergence flag
   */
  virtual LinearConvergenceReason get_converged_reason() const;

private:

  /**
   * Tells Eigen to use the user-specified preconditioner stored in
   * \p _preconditioner_type
   */
  void set_eigen_preconditioner_type ();

};


/*----------------------- functions ----------------------------------*/
template <typename T>
inline
EigenSparseLinearSolver<T>::EigenSparseLinearSolver(const libMesh::Parallel::Communicator &comm_in) :
  LinearSolver<T>(comm_in)
{
  // The GMRES iterative solver isn't supported by Eigen, so use BICGSTAB instead
  this->_solver_type = BICGSTAB;
}



template <typename T>
inline
EigenSparseLinearSolver<T>::~EigenSparseLinearSolver ()
{
  this->clear ();
}
<commit_msg>Add map between Eigen ComputationInfo enums and LinearConvergenceReasons.<commit_after>// The libMesh Finite Element Library.
// Copyright (C) 2002-2014 <NAME>, <NAME>, <NAME>

// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA



#ifndef LIBMESH_EIGEN_SPARSE_LINEAR_SOLVER_H
#define LIBMESH_EIGEN_SPARSE_LINEAR_SOLVER_H

#include "libmesh/libmesh_common.h"

#ifdef LIBMESH_HAVE_EIGEN

// Eigen includes

// Local includes
#include "libmesh/linear_solver.h"
#include "libmesh/eigen_sparse_vector.h"
#include "libmesh/eigen_sparse_matrix.h"

// C++ includes

namespace libMesh
{



/**
 * This class provides an interface to Eigen
 * iterative solvers that is compatible with the \p libMesh
 * \p LinearSolver<>
 *
 * @author <NAME>, 2013
 */
template <typename T>
class EigenSparseLinearSolver : public LinearSolver<T>
{
public:
  /**
   *  Constructor. Initializes Eigen data structures
   */
  EigenSparseLinearSolver (const libMesh::Parallel::Communicator &comm_in
                           LIBMESH_CAN_DEFAULT_TO_COMMWORLD);

  /**
   * Destructor.
   */
  ~EigenSparseLinearSolver ();

  /**
   * Release all memory and clear data structures.
   */
  void clear ();

  /**
   * Initialize data structures if not done so already.
   */
  void init (const char* name=NULL);

  /**
   * Call the Eigen solver
   */
  std::pair<unsigned int, Real>
  solve (SparseMatrix<T>  &matrix,
         NumericVector<T> &solution,
         NumericVector<T> &rhs,
         const double tol,
         const unsigned int m_its);

  /**
   * Call the Eigen solver to solve A^T x = b
   */
  std::pair<unsigned int, Real>
  adjoint_solve (SparseMatrix<T>  &matrix,
                 NumericVector<T> &solution,
                 NumericVector<T> &rhs,
                 const double tol,
                 const unsigned int m_its);

  /**
   * Call the Eigen solver
   */
  std::pair<unsigned int, Real>
  solve (SparseMatrix<T>  &matrix,
         SparseMatrix<T>  &pc,
         NumericVector<T> &solution,
         NumericVector<T> &rhs,
         const double tol,
         const unsigned int m_its);

  /**
   * This function solves a system whose matrix is a shell matrix.
   */
  std::pair<unsigned int, Real>
  solve (const ShellMatrix<T>& shell_matrix,
         NumericVector<T>& solution_in,
         NumericVector<T>& rhs_in,
         const double tol,
         const unsigned int m_its);

  /**
   * This function solves a system whose matrix is a shell matrix, but
   * a sparse matrix is used as preconditioning matrix, this allowing
   * other preconditioners than JACOBI.
   */
  virtual std::pair<unsigned int, Real>
  solve (const ShellMatrix<T>& shell_matrix,
         const SparseMatrix<T>& precond_matrix,
         NumericVector<T>& solution_in,
         NumericVector<T>& rhs_in,
         const double tol,
         const unsigned int m_its);

  /**
   * Prints a useful message about why the latest linear solve
   * con(di)verged.
   */
  virtual void print_converged_reason() const;

  /**
   * Returns the solver's convergence flag
   */
  virtual LinearConvergenceReason get_converged_reason() const;

private:

  /**
   * Tells Eigen to use the user-specified preconditioner stored in
   * \p _preconditioner_type
   */
  void set_eigen_preconditioner_type ();

  /**
   * Static map between Eigen ComputationInfo enumerations and libMesh
   * LinearConvergenceReason enumerations.
   */
  static std::map<Eigen::ComputationInfo, LinearConvergenceReason> _convergence_reasons;

  /**
   * Static function used to initialize _covergence_reasons map
   */
  static std::map<Eigen::ComputationInfo, LinearConvergenceReason> build_map()
    {
      std::map<Eigen::ComputationInfo, LinearConvergenceReason> ret;
      ret[Eigen::Success]        = CONVERGED_ITS;
      ret[Eigen::NumericalIssue] = DIVERGED_BREAKDOWN;
      ret[Eigen::NoConvergence]  = DIVERGED_ITS;
      ret[Eigen::InvalidInput]   = DIVERGED_NULL;
      return ret;
    }
};



// Call the class-static function to define the class-static member.
// Since it's a template class, you actually do this in the header,
// not the source file.
template <typename T>
std::map<Eigen::ComputationInfo, LinearConvergenceReason>
EigenSparseLinearSolver<T>::_convergence_reasons = EigenSparseLinearSolver<T>::build_map();



template <typename T>
inline
EigenSparseLinearSolver<T>::EigenSparseLinearSolver(const libMesh::Parallel::Communicator &comm_in) :
  LinearSolver<T>(comm_in)
{
  // The GMRES iterative solver isn't supported by Eigen, so use BICGSTAB instead
  this->_solver_type = BICGSTAB;
}



template <typename T>
inline
EigenSparseLinearSolver<T>::~EigenSparseLinearSolver ()
{
  this->clear ();
}

{'fraction_non_alphanumeric': 0.09907312049433574, 'fraction_numerical': 0.00576725025746653, 'mean_word_length': 3.1270718232044197, 'pattern_counts': {'":': 0, '<': 78, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4219708', 'n_tokens_mistral': 3092, 'n_tokens_neox': 2910, 'n_words': 930}