sillyCgSolve.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_SILLY_CG_SOLVE_HPP
00030 #define THYRA_SILLY_CG_SOLVE_HPP
00031 
00032 #include "Thyra_LinearOpBase.hpp"
00033 #include "Thyra_VectorStdOps.hpp"
00034 #include "Thyra_AssertOp.hpp"
00035 
00047 template<class Scalar>
00048 bool sillyCgSolve(
00049   const Thyra::LinearOpBase<Scalar>                              &A
00050   ,const Thyra::VectorBase<Scalar>                               &b
00051   ,const int                                                     maxNumIters
00052   ,const typename Teuchos::ScalarTraits<Scalar>::magnitudeType   tolerance
00053   ,Thyra::VectorBase<Scalar>                                     *x
00054   ,std::ostream                                                  *out          = NULL
00055   )
00056 {
00057   // Create some typedefs and some other stuff to make the code cleaner
00058   typedef Teuchos::ScalarTraits<Scalar> ST; typedef typename ST::magnitudeType ScalarMag;
00059   const Scalar one = ST::one(), zero = ST::zero(); using Thyra::NOTRANS;
00060   typedef Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar> > VectorSpacePtr;
00061   typedef Teuchos::RCP<Thyra::VectorBase<Scalar> > VectorPtr;
00062   // Validate input
00063   TEST_FOR_EXCEPT(x==NULL);
00064   THYRA_ASSERT_LINEAR_OP_VEC_APPLY_SPACES("sillyCgSolve()",A,Thyra::NOTRANS,*x,&b); // Does A*x - b agree?
00065   Teuchos::EVerbosityLevel vl = Teuchos::VERB_MEDIUM;
00066   if(out) *out << "\nStarting CG solver ...\n" << std::scientific << "\ndescribe A:\n"<<describe(A,vl)
00067                << "\ndescribe b:\n"<<describe(b,vl)<<"\ndescribe x:\n"<<describe(*x,vl)<<"\n";
00068   // Initialization
00069   VectorSpacePtr space = A.domain();
00070   VectorPtr r = createMember(space);
00071   V_V(&*r,b); apply(A,NOTRANS,*x,&*r,Scalar(-one),one); // r = -A*x + b
00072   const ScalarMag r0_nrm = norm(*r);
00073   if(r0_nrm==zero) return true;
00074   VectorPtr p = createMember(space), q = createMember(space);
00075   Scalar rho_old = -one;
00076   // Perform the iterations
00077   for( int iter = 0; iter <= maxNumIters; ++iter ) {
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       if(out) *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 iteration
00086     const Scalar rho = scalarProd(*r,*r);         // <r,r>              -> rho
00087     if(iter==0) V_V(&*p,*r);                      // r                  -> p   (iter == 0)
00088     else Vp_V( &*p, *r, Scalar(rho/rho_old) );    // r+(rho/rho_old)*p  -> p   (iter  > 0)
00089     apply(A,NOTRANS,*p,&*q);                      // A*p                -> q
00090     const Scalar alpha = rho/scalarProd(*p,*q);   // rho/<p,q>          -> alpha
00091     Vp_StV( x,   Scalar(+alpha), *p );            // +alpha*p + x       -> x
00092     Vp_StV( &*r, Scalar(-alpha), *q );            // -alpha*q + r       -> r
00093     rho_old = rho;                                // rho                -> rho_old (remember for next iter)
00094   }
00095   return false; // Failure
00096 } // end sillyCgSolve
00097 
00098 #endif // THYRA_SILLY_CG_SOLVE_HPP

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