Belos Package Browser (Single Doxygen Collection) Development
MyOperator.hpp
Go to the documentation of this file.
00001 /*
00002 //@HEADER
00003 // ************************************************************************
00004 //
00005 //                 Belos: Block Linear Solvers Package
00006 //                  Copyright 2004 Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00039 //
00040 // ************************************************************************
00041 //@HEADER
00042 */
00043 
00044 #ifndef MY_OPERATOR_HPP
00045 #define MY_OPERATOR_HPP
00046 
00047 #include "BelosConfigDefs.hpp"
00048 #include "BelosOperator.hpp"
00049 #include "MyMultiVec.hpp"
00050 
00052 
00064 template <class ScalarType>
00065 class MyOperator : public Belos::Operator<ScalarType>
00066 {
00067 
00068 public:
00069   
00070   /* Constructs a square matrix with \c NumRows rows and columns.
00071    * The matrix is tridiagonal, and the computational stencil is 
00072    * [-1, 2, -1]
00073    */
00074   MyOperator(const int NumRows) :
00075     NumRows_(NumRows)
00076   {
00077     l_ = -1.0;
00078     d_ =  2.0;
00079     u_ = -1.0;
00080   }
00081 
00082   // Constructor for tridiagonal matrix.
00083   MyOperator(const int NumRows, std::vector<ScalarType> ldu) :
00084     NumRows_(NumRows)
00085   {
00086     l_ = ldu[0];
00087     d_ = ldu[1];
00088     u_ = ldu[2];    
00089   }
00090 
00091   // Constructor for a diagonal matrix with variable entries.
00092   MyOperator(std::vector<ScalarType> diag) :
00093     NumRows_(diag.size())
00094   {
00095     diag_.resize(diag.size());
00096     for(unsigned int i=0; i<diag_.size(); ++i)
00097       diag_[i] = diag[i];
00098   }
00099 
00101   ~MyOperator()
00102   {}
00103   
00105   void Apply(const Belos::MultiVec<ScalarType>& X, 
00106              Belos::MultiVec<ScalarType>& Y,
00107              Belos::ETrans trans = Belos::NOTRANS) const
00108   {
00109     const MyMultiVec<ScalarType>* MyX;
00110     MyX = dynamic_cast<const MyMultiVec<ScalarType>*>(&X); 
00111     assert (MyX != 0);
00112     
00113     MyMultiVec<ScalarType>* MyY;
00114     MyY = dynamic_cast<MyMultiVec<ScalarType>*>(&Y); 
00115     assert (MyY != 0);
00116     
00117     assert (X.GetNumberVecs() == Y.GetNumberVecs());
00118     assert (X.GetVecLength() == Y.GetVecLength());
00119    
00120     if (diag_.size() == 0)
00121     {
00122       // This is a tridiagonal matrix
00123       for (int v = 0 ; v < X.GetNumberVecs() ; ++v)
00124       {
00125         for (int i = 0 ; i < X.GetVecLength() ; ++i)
00126         {
00127           if (i == 0)
00128           {
00129             (*MyY)[v][i] = (d_ * (*MyX)[v][i] + u_ * (*MyX)[v][i + 1]);
00130           }
00131           else if (i == X.GetVecLength() - 1)
00132           {
00133             (*MyY)[v][i] = (d_ * (*MyX)[v][i] + l_ * (*MyX)[v][i-1]);
00134           }
00135           else
00136           {
00137             (*MyY)[v][i] = (d_ * (*MyX)[v][i] + l_ * (*MyX)[v][i-1] + u_ * (*MyX)[v][i+1]);
00138           }
00139         }
00140       }
00141     } 
00142     else
00143     {
00144       // This is a diagonal matrix
00145       for (int v = 0 ; v < X.GetNumberVecs() ; ++v)
00146       {
00147         for (int i = 0 ; i < X.GetVecLength() ; ++i)
00148         {
00149           (*MyY)[v][i] = diag_[i] * (*MyX)[v][i];
00150         }
00151       }      
00152     }
00153   }
00154   
00155 private:
00157   int NumRows_;
00159   ScalarType l_, d_, u_;
00161   std::vector<ScalarType> diag_;
00162 };
00163 
00164 #endif //MY_OPERATOR_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines