Belos Version of the Day
BelosOpAkx.hpp
Go to the documentation of this file.
00001 //@HEADER
00002 // ************************************************************************
00003 //
00004 //                 Belos: Block Linear Solvers Package
00005 //                  Copyright 2004 Sandia Corporation
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ************************************************************************
00040 //@HEADER
00041 
00042 #ifndef __Belos_OpAkx_hpp
00043 #define __Belos_OpAkx_hpp
00044 
00045 #include <BelosAkx.hpp>
00046 #include <BelosOperatorTraits.hpp>
00047 #include <Teuchos_OrdinalTraits.hpp>
00048 #include <Teuchos_SerialDenseVector.hpp>
00049 
00050 namespace Belos {
00051 
00067   template<class Scalar, class MV, class OP>
00068   class OpAkx : public Akx<Scalar, MV> {
00069   public:
00083     OpAkx (const Teuchos::RCP<const OP>& A, 
00084      const Teuchos::RCP<const OP>& M_left, 
00085      const Teuchos::RCP<const OP>& M_right,
00086      const int recommendedBasisLength) :
00087       A_ (A), 
00088       M_left_ (M_left), 
00089       M_right_ (M_right),
00090       recommendedBasisLength_ (validatedBasisLength (recommendedBasisLength))
00091     {}
00092   
00094     int maxCandidateBasisLength () const { 
00095       // We're not using a special matrix powers kernel implementation
00096       // here, just whatever operator(s) we were given, so we can
00097       // apply them as many times as we want.  Of course, this doesn't
00098       // account for memory or time constraints.
00099       return Teuchos::OrdinalTraits<int>::max(); 
00100     }
00101 
00103     int recommendedCandidateBasisLength () const {
00104       return recommendedBasisLength_;
00105     }
00106 
00107   protected:
00123     void 
00124     applyOp (const MV& v_prv, MV& v_cur) 
00125     {
00126       // We use v_scratch_ as scratch space, since
00127       // OperatorTraits::Apply() doesn't allow aliasing of the input
00128       // and output vectors when applying an operator.  We only
00129       // allocate v_scratch_ if necessary.
00130       if (! M_right_.is_null())
00131   {
00132     if (v_scratch_.is_null())
00133       v_scratch_ = MVT::Clone (v_cur);
00134     OPT::Apply (*M_right_, v_prv, *v_scratch_);
00135     OPT::Apply (*A_, *v_scratch_, v_cur);
00136     if (! M_left_.is_null())
00137       {
00138         MVT::Assign (v_cur, *v_scratch_);
00139         OPT::Apply (*M_left_, *v_scratch_, v_cur);
00140       }
00141   }
00142       else if (! M_left_.is_null())
00143   {
00144     if (v_scratch_.is_null())
00145       v_scratch_ = MVT::Clone (v_cur);
00146     OPT::Apply (*A_, v_prv, *v_scratch_);
00147     OPT::Apply (*M_left_, *v_scratch_, v_cur);
00148   }
00149       else
00150   OPT::Apply (*A_, v_prv, v_cur);
00151     }
00152 
00171     void
00172     applyFlexibleOp (const MV& v_prv, MV& z_cur, MV& v_cur)
00173     {
00174       TEST_FOR_EXCEPTION(! M_left_.is_null(), std::logic_error,
00175        "Flexible GMRES only works with a right "
00176        "preconditioner, not a left or split "
00177        "preconditioner.");
00178       if (! M_right_.is_null())
00179   { 
00180     OPT::Apply (*M_right_, v_prv, z_cur);
00181     OPT::Apply (*A_, z_cur, v_cur);
00182   }
00183       else
00184   { // In this case, the right preconditioner is the identity
00185     // operator, so we only have to copy v_prv into z_cur.
00186     MVT::Assign (v_prv, z_cur);
00187     OPT::Apply (*A_, z_cur, v_cur);
00188   }
00189     }
00190 
00191   private:
00193     int validatedBasisLength (const int len) const {
00194       if (len < 1)
00195   throw std::invalid_argument("Invalid recommended candidate basis length");
00196       else
00197   return len;
00198     }
00199 
00201     Teuchos::RCP<const OP> A_;
00203     Teuchos::RCP<const OP> M_left_;
00205     Teuchos::RCP<const OP> M_right_;
00216     Teuchos::RCP<const MV> v_scratch_;
00217 
00219     int recommendedBasisLength_
00220   };
00221 
00222 } // namespace Belos
00223 
00224 #endif // __Belos_OpAkx_hpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines