Belos Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MyOperator.hpp
Go to the documentation of this file.
1 /*
2 // @HEADER
3 // *****************************************************************************
4 // Belos: Block Linear Solvers Package
5 //
6 // Copyright 2004-2016 NTESS and the Belos contributors.
7 // SPDX-License-Identifier: BSD-3-Clause
8 // *****************************************************************************
9 // @HEADER
10 */
11 
12 #ifndef MY_OPERATOR_HPP
13 #define MY_OPERATOR_HPP
14 
15 #include "BelosConfigDefs.hpp"
16 #include "BelosOperator.hpp"
17 #include "MyMultiVec.hpp"
18 
20 
32 template <class ScalarType>
33 class MyOperator : public Belos::Operator<ScalarType>
34 {
35 
36 public:
37 
38  /* Constructs a square matrix with \c NumRows rows and columns.
39  * The matrix is tridiagonal, and the computational stencil is
40  * [-1, 2, -1]
41  */
42  MyOperator(const int NumRows) :
43  NumRows_(NumRows)
44  {
45  l_ = -1.0;
46  d_ = 2.0;
47  u_ = -1.0;
48  }
49 
50  // Constructor for tridiagonal matrix.
51  MyOperator(const int NumRows, std::vector<ScalarType> ldu) :
52  NumRows_(NumRows)
53  {
54  l_ = ldu[0];
55  d_ = ldu[1];
56  u_ = ldu[2];
57  }
58 
59  // Constructor for a diagonal matrix with variable entries.
60  MyOperator(std::vector<ScalarType> diag) :
61  NumRows_(diag.size())
62  {
63  diag_.resize(diag.size());
64  for(unsigned int i=0; i<diag_.size(); ++i)
65  diag_[i] = diag[i];
66  }
67 
70  {}
71 
75  Belos::ETrans trans = Belos::NOTRANS) const
76  {
77  const MyMultiVec<ScalarType>* MyX;
78  MyX = dynamic_cast<const MyMultiVec<ScalarType>*>(&X);
79  assert (MyX != 0);
80 
82  MyY = dynamic_cast<MyMultiVec<ScalarType>*>(&Y);
83  assert (MyY != 0);
84 
85  assert (X.GetNumberVecs() == Y.GetNumberVecs());
86  assert (X.GetGlobalLength() == Y.GetGlobalLength());
87 
88  if (diag_.size() == 0)
89  {
90  // This is a tridiagonal matrix
91  for (int v = 0 ; v < X.GetNumberVecs() ; ++v)
92  {
93  for (ptrdiff_t i = 0 ; i < X.GetGlobalLength() ; ++i)
94  {
95  if (i == 0)
96  {
97  (*MyY)[v][i] = (d_ * (*MyX)[v][i] + u_ * (*MyX)[v][i + 1]);
98  }
99  else if (i == X.GetGlobalLength() - 1)
100  {
101  (*MyY)[v][i] = (d_ * (*MyX)[v][i] + l_ * (*MyX)[v][i-1]);
102  }
103  else
104  {
105  (*MyY)[v][i] = (d_ * (*MyX)[v][i] + l_ * (*MyX)[v][i-1] + u_ * (*MyX)[v][i+1]);
106  }
107  }
108  }
109  }
110  else
111  {
112  // This is a diagonal matrix
113  for (int v = 0 ; v < X.GetNumberVecs() ; ++v)
114  {
115  for (ptrdiff_t i = 0 ; i < X.GetGlobalLength() ; ++i)
116  {
117  (*MyY)[v][i] = diag_[i] * (*MyX)[v][i];
118  }
119  }
120  }
121  }
122 
123 private:
125  int NumRows_;
127  ScalarType l_, d_, u_;
129  std::vector<ScalarType> diag_;
130 };
131 
132 #endif //MY_OPERATOR_HPP
ScalarType u_
Definition: MyOperator.hpp:127
MyOperator(const int NumRows, std::vector< ScalarType > ldu)
Definition: MyOperator.hpp:51
virtual int GetNumberVecs() const =0
The number of vectors (i.e., columns) in the multivector.
void Apply(const Belos::MultiVec< ScalarType > &X, Belos::MultiVec< ScalarType > &Y, Belos::ETrans trans=Belos::NOTRANS) const
Applies the tridiagonal or diagonal matrix to a multivector.
Definition: MyOperator.hpp:73
virtual ptrdiff_t GetGlobalLength() const =0
The number of rows in the multivector.
ETrans
Whether to apply the (conjugate) transpose of an operator.
Definition: BelosTypes.hpp:49
MyOperator(std::vector< ScalarType > diag)
Definition: MyOperator.hpp:60
Simple example of a user&#39;s defined Belos::MultiVec class.
Definition: MyMultiVec.hpp:33
std::vector< ScalarType > diag_
Elements on diagonal (for variable-diagonal case).
Definition: MyOperator.hpp:129
Alternative run-time polymorphic interface for operators.
~MyOperator()
Dtor.
Definition: MyOperator.hpp:69
ScalarType d_
Definition: MyOperator.hpp:127
int NumRows_
Number of rows and columns.
Definition: MyOperator.hpp:125
ScalarType l_
Elements on subdiagonal, diagonal, and superdiagonal.
Definition: MyOperator.hpp:127
Alternative run-time polymorphic interface for operators.
Simple example of a user&#39;s defined Belos::Operator class.
Definition: MyOperator.hpp:33
Interface for multivectors used by Belos&#39; linear solvers.
Belos header file which uses auto-configuration information to include necessary C++ headers...
MyOperator(const int NumRows)
Definition: MyOperator.hpp:42