silliestCgSolve.hpp

Click here for a more detailed discussion of this example function.

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //    Thyra: Interfaces and Support for Abstract Numerical Algorithms
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #ifndef THYRA_SILLIEST_CG_SOLVE_HPP
00030 #define THYRA_SILLIEST_CG_SOLVE_HPP
00031 
00032 #include "Thyra_LinearOperator.hpp"
00033 #include "Thyra_VectorSpace.hpp"
00034 #include "Thyra_Vector.hpp"
00035 #include "Thyra_AssertOp.hpp"
00036 
00037 
00050 template<class Scalar>
00051 bool silliestCgSolve(
00052   Thyra::ConstLinearOperator<Scalar> const& A,
00053   Thyra::ConstVector<Scalar> const& b,
00054   const int maxNumIters,
00055   const typename Teuchos::ScalarTraits<Scalar>::magnitudeType tolerance,
00056   Thyra::Vector<Scalar> &x,
00057   std::ostream &out
00058   )
00059 {
00060  
00061  // Create some typedefs and inject some names into local namespace
00062   typedef Teuchos::ScalarTraits<Scalar> ST;
00063   typedef typename ST::magnitudeType ScalarMag;
00064   const Scalar one = ST::one(), zero = ST::zero();
00065   using Thyra::VectorSpace; using Thyra::Vector;
00066 
00067   // Initialization of the algorithm
00068   const VectorSpace<Scalar> space = A.domain();
00069   Vector<Scalar> r = b - A*x;
00070   ScalarMag r0_nrm = norm(r);
00071   if(r0_nrm==zero) return true;
00072   Vector<Scalar> p(space), q(space);
00073   Scalar rho_old = -one;
00074 
00075   // Perform the iterations
00076   for( int iter = 0; iter <= maxNumIters; ++iter ) {
00077 
00078     // Check convergence and output iteration
00079     const ScalarMag r_nrm = norm(r);
00080     const bool isConverged = ( (r_nrm/r0_nrm) <= tolerance );
00081     if( ( iter%(maxNumIters/10+1) == 0 || iter == maxNumIters || isConverged ) )
00082       out<<"Iter = "<<iter<<", ||b-A*x||/||b-A*x0|| = "<<(r_nrm/r0_nrm)<<std::endl;
00083     if( r_nrm/r0_nrm < tolerance ) return true; // Success!
00084 
00085     // Compute the iteration
00086     const Scalar rho = inner(r, r);
00087     if(iter==0) copyInto(r,p);
00088     else p = Scalar(rho/rho_old)*p + r;
00089     q = A*p;
00090     const Scalar alpha = rho/inner(p,q);
00091     x += Scalar(+alpha)*p;
00092     r += Scalar(-alpha)*q;
00093     rho_old = rho;
00094     
00095   }
00096   
00097   return false; // Failure
00098 
00099 } // end silliestCgSolve
00100 
00101 
00102 #endif // THYRA_SILLIEST_CG_SOLVE_HPP

Generated on Wed May 12 21:26:53 2010 for Thyra Operator/Vector Support by  doxygen 1.4.7