Thyra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sillyCgSolve.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (bartlettra@ornl.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef THYRA_SILLY_CG_SOLVE_HPP
43 #define THYRA_SILLY_CG_SOLVE_HPP
44 
45 #include "Thyra_LinearOpBase.hpp"
46 #include "Thyra_VectorStdOps.hpp"
47 #include "Thyra_AssertOp.hpp"
48 
49 
61 template<class Scalar>
62 bool sillyCgSolve(
65  const int maxNumIters,
66  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType tolerance,
68  std::ostream &out
69  )
70 {
71 
72  // Create some typedefs and some other stuff to make the code cleaner
73  typedef Teuchos::ScalarTraits<Scalar> ST; typedef typename ST::magnitudeType ScalarMag;
74  const Scalar one = ST::one(), zero = ST::zero(); using Teuchos::as;
76  using Thyra::NOTRANS; using Thyra::V_V; using Thyra::apply;
77 
78 
79  // Validate input
80  THYRA_ASSERT_LINEAR_OP_VEC_APPLY_SPACES("sillyCgSolve()", A, Thyra::NOTRANS, *x, &b);
82 
83  std::ios::fmtflags fmt(out.flags());
84 
85  out << "\nStarting CG solver ...\n" << std::scientific << "\ndescribe A:\n"<<describe(A, vl)
86  << "\ndescribe b:\n"<<describe(b, vl)<<"\ndescribe x:\n"<<describe(*x, vl)<<"\n";
87 
88  // Initialization
89  const RCP<const VectorSpaceBase<Scalar> > space = A.domain();
90  const RCP<VectorBase<Scalar> > r = createMember(space);
91  // r = -A*x + b
92  V_V(r.ptr(), b); apply<Scalar>(A, NOTRANS, *x, r.ptr(), -one, one);
93  const ScalarMag r0_nrm = norm(*r);
94  if (r0_nrm==zero) return true;
95  const RCP<VectorBase<Scalar> > p = createMember(space), q = createMember(space);
96  Scalar rho_old = -one;
97 
98  // Perform the iterations
99  for( int iter = 0; iter <= maxNumIters; ++iter ) {
100 
101  // Check convergence and output iteration
102  const ScalarMag r_nrm = norm(*r);
103  const bool isConverged = r_nrm/r0_nrm <= tolerance;
104  if( iter%(maxNumIters/10+1) == 0 || iter == maxNumIters || isConverged ) {
105  out << "Iter = " << iter << ", ||b-A*x||/||b-A*x0|| = " << (r_nrm/r0_nrm) << std::endl;
106  if( r_nrm/r0_nrm < tolerance ) {
107  out.flags(fmt);
108  return true; // Success!
109  }
110  }
111 
112  // Compute iteration
113  const Scalar rho = inner(*r, *r); // <r,r> -> rho
114  if (iter==0) V_V(p.ptr(), *r); // r -> p (iter == 0)
115  else Vp_V( p.ptr(), *r, rho/rho_old ); // r+(rho/rho_old)*p -> p (iter > 0)
116  apply<Scalar>(A, NOTRANS, *p, q.ptr()); // A*p -> q
117  const Scalar alpha = rho/inner(*p, *q); // rho/<p,q> -> alpha
118  Vp_StV( x, +alpha, *p ); // +alpha*p + x -> x
119  Vp_StV( r.ptr(), -alpha, *q ); // -alpha*q + r -> r
120  rho_old = rho; // rho -> rho_old (for next iter)
121 
122  }
123 
124  out.flags(fmt);
125  return false; // Failure
126 } // end sillyCgSolve
127 
128 #endif // THYRA_SILLY_CG_SOLVE_HPP
void Vp_V(const Ptr< MultiVectorBase< Scalar > > &Z, const MultiVectorBase< Scalar > &X)
Z(i,j) += X(i,j), i = 0...Z-&gt;range()-&gt;dim()-1, j = 0...Z-&gt;domain()-&gt;dim()-1.
Use the non-transposed operator.
RCP< const LinearOpBase< Scalar > > zero(const RCP< const VectorSpaceBase< Scalar > > &range, const RCP< const VectorSpaceBase< Scalar > > &domain)
Create a zero linear operator with given range and domain spaces.
Abstract interface for objects that represent a space for vectors.
void V_V(const Ptr< VectorBase< Scalar > > &y, const VectorBase< Scalar > &x)
y(i) = x(i), i = 0...y-&gt;space()-&gt;dim()-1.
RCP< VectorBase< Scalar > > createMember(const RCP< const VectorSpaceBase< Scalar > > &vs, const std::string &label="")
Create a vector member from the vector space.
Abstract interface for finite-dimensional dense vectors.
Base class for all linear operators.
virtual RCP< const VectorSpaceBase< Scalar > > domain() const =0
Return a smart pointer for the domain space for this operator.
TypeTo as(const TypeFrom &t)
#define THYRA_ASSERT_LINEAR_OP_VEC_APPLY_SPACES(FUNC_NAME, M, M_T, X, Y)
This is a very useful macro that should be used to validate that the spaces for the vector version of...
void Vp_StV(const Ptr< VectorBase< Scalar > > &y, const Scalar &alpha, const VectorBase< Scalar > &x)
AXPY: y(i) = alpha * x(i) + y(i), i = 0...y-&gt;space()-&gt;dim()-1.
Teuchos::ScalarTraits< Scalar >::magnitudeType norm(const VectorBase< Scalar > &v)
Natural norm: result = sqrt(&lt;v,v&gt;).
Scalar inner(const VectorBase< Scalar > &x, const VectorBase< Scalar > &y)
Inner/Scalar product result = &lt;x,y&gt;.