Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/eigen3/unsupported/Eigen/src/IterativeSolvers/IterationController.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
0005 
0006 /* NOTE The class IterationController has been adapted from the iteration
0007  *      class of the GMM++ and ITL libraries.
0008  */
0009 
0010 //=======================================================================
0011 // Copyright (C) 1997-2001
0012 // Authors: Andrew Lumsdaine <lums@osl.iu.edu> 
0013 //          Lie-Quan Lee     <llee@osl.iu.edu>
0014 //
0015 // This file is part of the Iterative Template Library
0016 //
0017 // You should have received a copy of the License Agreement for the
0018 // Iterative Template Library along with the software;  see the
0019 // file LICENSE.  
0020 //
0021 // Permission to modify the code and to distribute modified code is
0022 // granted, provided the text of this NOTICE is retained, a notice that
0023 // the code was modified is included with the above COPYRIGHT NOTICE and
0024 // with the COPYRIGHT NOTICE in the LICENSE file, and that the LICENSE
0025 // file is distributed with the modified code.
0026 //
0027 // LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.
0028 // By way of example, but not limitation, Licensor MAKES NO
0029 // REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY
0030 // PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS
0031 // OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS
0032 // OR OTHER RIGHTS.
0033 //=======================================================================
0034 
0035 //========================================================================
0036 //
0037 // Copyright (C) 2002-2007 Yves Renard
0038 //
0039 // This file is a part of GETFEM++
0040 //
0041 // Getfem++ is free software; you can redistribute it and/or modify
0042 // it under the terms of the GNU Lesser General Public License as
0043 // published by the Free Software Foundation; version 2.1 of the License.
0044 //
0045 // This program is distributed in the hope that it will be useful,
0046 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0047 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0048 // GNU Lesser General Public License for more details.
0049 // You should have received a copy of the GNU Lesser General Public
0050 // License along with this program; if not, write to the Free Software
0051 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301,
0052 // USA.
0053 //
0054 //========================================================================
0055 
0056 #include "../../../../Eigen/src/Core/util/NonMPL2.h"
0057 
0058 #ifndef EIGEN_ITERATION_CONTROLLER_H
0059 #define EIGEN_ITERATION_CONTROLLER_H
0060 
0061 namespace Eigen { 
0062 
0063 /** \ingroup IterativeLinearSolvers_Module
0064   * \class IterationController
0065   *
0066   * \brief Controls the iterations of the iterative solvers
0067   *
0068   * This class has been adapted from the iteration class of GMM++ and ITL libraries.
0069   *
0070   */
0071 class IterationController
0072 {
0073   protected :
0074     double m_rhsn;        ///< Right hand side norm
0075     size_t m_maxiter;     ///< Max. number of iterations
0076     int m_noise;          ///< if noise > 0 iterations are printed
0077     double m_resmax;      ///< maximum residual
0078     double m_resminreach, m_resadd;
0079     size_t m_nit;         ///< iteration number
0080     double m_res;         ///< last computed residual
0081     bool m_written;
0082     void (*m_callback)(const IterationController&);
0083   public :
0084 
0085     void init()
0086     {
0087       m_nit = 0; m_res = 0.0; m_written = false;
0088       m_resminreach = 1E50; m_resadd = 0.0;
0089       m_callback = 0;
0090     }
0091 
0092     IterationController(double r = 1.0E-8, int noi = 0, size_t mit = size_t(-1))
0093       : m_rhsn(1.0), m_maxiter(mit), m_noise(noi), m_resmax(r) { init(); }
0094 
0095     void operator ++(int) { m_nit++; m_written = false; m_resadd += m_res; }
0096     void operator ++() { (*this)++; }
0097 
0098     bool first() { return m_nit == 0; }
0099 
0100     /* get/set the "noisyness" (verbosity) of the solvers */
0101     int noiseLevel() const { return m_noise; }
0102     void setNoiseLevel(int n) { m_noise = n; }
0103     void reduceNoiseLevel() { if (m_noise > 0) m_noise--; }
0104 
0105     double maxResidual() const { return m_resmax; }
0106     void setMaxResidual(double r) { m_resmax = r; }
0107 
0108     double residual() const { return m_res; }
0109 
0110     /* change the user-definable callback, called after each iteration */
0111     void setCallback(void (*t)(const IterationController&))
0112     {
0113       m_callback = t;
0114     }
0115 
0116     size_t iteration() const { return m_nit; }
0117     void setIteration(size_t i) { m_nit = i; }
0118 
0119     size_t maxIterarions() const { return m_maxiter; }
0120     void setMaxIterations(size_t i) { m_maxiter = i; }
0121 
0122     double rhsNorm() const { return m_rhsn; }
0123     void setRhsNorm(double r) { m_rhsn = r; }
0124 
0125     bool converged() const { return m_res <= m_rhsn * m_resmax; }
0126     bool converged(double nr)
0127     {
0128       using std::abs;
0129       m_res = abs(nr); 
0130       m_resminreach = (std::min)(m_resminreach, m_res);
0131       return converged();
0132     }
0133     template<typename VectorType> bool converged(const VectorType &v)
0134     { return converged(v.squaredNorm()); }
0135 
0136     bool finished(double nr)
0137     {
0138       if (m_callback) m_callback(*this);
0139       if (m_noise > 0 && !m_written)
0140       {
0141         converged(nr);
0142         m_written = true;
0143       }
0144       return (m_nit >= m_maxiter || converged(nr));
0145     }
0146     template <typename VectorType>
0147     bool finished(const MatrixBase<VectorType> &v)
0148     { return finished(double(v.squaredNorm())); }
0149 
0150 };
0151 
0152 } // end namespace Eigen
0153 
0154 #endif // EIGEN_ITERATION_CONTROLLER_H