|
Belos Version of the Day
|
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 00070 template<class Scalar, class MV, class OP> 00071 class OpAkx : public Akx<Scalar, MV> { 00072 public: 00086 OpAkx (const Teuchos::RCP<const OP>& A, 00087 const Teuchos::RCP<const OP>& M_left, 00088 const Teuchos::RCP<const OP>& M_right, 00089 const int recommendedBasisLength) : 00090 A_ (A), 00091 M_left_ (M_left), 00092 M_right_ (M_right), 00093 recommendedBasisLength_ (validatedBasisLength (recommendedBasisLength)) 00094 {} 00095 00097 int maxCandidateBasisLength () const { 00098 // We're not using a special matrix powers kernel implementation 00099 // here, just whatever operator(s) we were given, so we can 00100 // apply them as many times as we want. Of course, this doesn't 00101 // account for memory or time constraints. 00102 return Teuchos::OrdinalTraits<int>::max(); 00103 } 00104 00106 int recommendedCandidateBasisLength () const { 00107 return recommendedBasisLength_; 00108 } 00109 00110 protected: 00126 void 00127 applyOp (const MV& v_prv, MV& v_cur) 00128 { 00129 // We use v_scratch_ as scratch space, since 00130 // OperatorTraits::Apply() doesn't allow aliasing of the input 00131 // and output vectors when applying an operator. We only 00132 // allocate v_scratch_ if necessary. 00133 if (! M_right_.is_null()) 00134 { 00135 if (v_scratch_.is_null()) 00136 v_scratch_ = MVT::Clone (v_cur); 00137 OPT::Apply (*M_right_, v_prv, *v_scratch_); 00138 OPT::Apply (*A_, *v_scratch_, v_cur); 00139 if (! M_left_.is_null()) 00140 { 00141 MVT::Assign (v_cur, *v_scratch_); 00142 OPT::Apply (*M_left_, *v_scratch_, v_cur); 00143 } 00144 } 00145 else if (! M_left_.is_null()) 00146 { 00147 if (v_scratch_.is_null()) 00148 v_scratch_ = MVT::Clone (v_cur); 00149 OPT::Apply (*A_, v_prv, *v_scratch_); 00150 OPT::Apply (*M_left_, *v_scratch_, v_cur); 00151 } 00152 else 00153 OPT::Apply (*A_, v_prv, v_cur); 00154 } 00155 00174 void 00175 applyFlexibleOp (const MV& v_prv, MV& z_cur, MV& v_cur) 00176 { 00177 TEUCHOS_TEST_FOR_EXCEPTION(! M_left_.is_null(), std::logic_error, 00178 "Flexible GMRES only works with a right " 00179 "preconditioner, not a left or split " 00180 "preconditioner."); 00181 if (! M_right_.is_null()) 00182 { 00183 OPT::Apply (*M_right_, v_prv, z_cur); 00184 OPT::Apply (*A_, z_cur, v_cur); 00185 } 00186 else 00187 { // In this case, the right preconditioner is the identity 00188 // operator, so we only have to copy v_prv into z_cur. 00189 MVT::Assign (v_prv, z_cur); 00190 OPT::Apply (*A_, z_cur, v_cur); 00191 } 00192 } 00193 00194 private: 00196 int validatedBasisLength (const int len) const { 00197 if (len < 1) 00198 throw std::invalid_argument("Invalid recommended candidate basis length"); 00199 else 00200 return len; 00201 } 00202 00204 Teuchos::RCP<const OP> A_; 00206 Teuchos::RCP<const OP> M_left_; 00208 Teuchos::RCP<const OP> M_right_; 00219 Teuchos::RCP<const MV> v_scratch_; 00220 00222 int recommendedBasisLength_ 00223 }; 00224 00225 } // namespace Belos 00226 00227 #endif // __Belos_OpAkx_hpp
1.7.4