| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Anasazi: Block Eigensolvers Package |
| 4 |
// |
| 5 |
// Copyright 2004 NTESS and the Anasazi contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
#ifndef MY_OPERATOR_HPP |
| 10 |
#define MY_OPERATOR_HPP |
| 11 |
|
| 12 |
#include "AnasaziConfigDefs.hpp" |
| 13 |
#include "AnasaziOperator.hpp" |
| 14 |
#include "MyMultiVec.hpp" |
| 15 |
|
| 16 |
//! Simple example of a user's defined Anasazi::Operator class. |
| 17 |
/*! |
| 18 |
* This is a simple, single processor example of user's defined |
| 19 |
* Anasazi::Operator-derived class. The class is templated with ScalarType; |
| 20 |
* possible choices are, for example, "float", "double", or |
| 21 |
* "complex<double>". |
| 22 |
* |
| 23 |
* This file can be easily extended to tackle more interesting cases. |
| 24 |
* |
| 25 |
* \author Oscar Chinallato (ETHZ/ICOS) and Marzio Sala (ETHZ/COLAB) |
| 26 |
* |
| 27 |
* \date Last modified on 01-Nov-05 |
| 28 |
*/ |
| 29 |
template <class ScalarType> |
| 30 |
class MyOperator : public Anasazi::Operator<ScalarType> |
| 31 |
{ |
| 32 |
|
| 33 |
public: |
| 34 |
|
| 35 |
/* Constructs a square matrix with \c NumRows rows and columns. |
| 36 |
* The matrix is tridiagonal, and the computational stencil is |
| 37 |
* [-1, 2, -1] |
| 38 |
*/ |
| 39 |
MyOperator(const int NumRows) : |
| 40 |
NumRows_(NumRows) |
| 41 |
{ |
| 42 |
l_ = -1.0; |
| 43 |
d_ = 2.0; |
| 44 |
u_ = -1.0; |
| 45 |
} |
| 46 |
|
| 47 |
// Constructor for tridiagonal matrix. |
| 48 |
MyOperator(const int NumRows, std::vector<ScalarType> ldu) : |
| 49 |
NumRows_(NumRows) |
| 50 |
{ |
| 51 |
l_ = ldu[0]; |
| 52 |
d_ = ldu[1]; |
| 53 |
u_ = ldu[2]; |
| 54 |
} |
| 55 |
|
| 56 |
// Constructor for a diagonal matrix with variable entries. |
| 57 |
MyOperator(std::vector<ScalarType> diag) : |
| 58 |
NumRows_(diag.size()) |
| 59 |
{ |
| 60 |
diag_.resize(diag.size()); |
| 61 |
for(unsigned int i=0; i<diag_.size(); ++i) |
| 62 |
diag_[i] = diag[i]; |
| 63 |
} |
| 64 |
|
| 65 |
//! Dtor |
| 66 |
~MyOperator() |
| 67 |
{} |
| 68 |
|
| 69 |
//! Applies the tridiagonal or diagonal matrix to a multivector. |
| 70 |
void Apply(const Anasazi::MultiVec<ScalarType>& X, |
| 71 |
Anasazi::MultiVec<ScalarType>& Y) const |
| 72 |
{ |
| 73 |
const MyMultiVec<ScalarType>* MyX; |
| 74 |
MyX = dynamic_cast<const MyMultiVec<ScalarType>*>(&X); |
| 75 |
assert (MyX != 0); |
| 76 |
|
| 77 |
MyMultiVec<ScalarType>* MyY; |
| 78 |
MyY = dynamic_cast<MyMultiVec<ScalarType>*>(&Y); |
| 79 |
assert (MyY != 0); |
| 80 |
|
| 81 |
assert (X.GetNumberVecs() == Y.GetNumberVecs()); |
| 82 |
assert (X.GetGlobalLength() == Y.GetGlobalLength()); |
| 83 |
|
| 84 |
if (diag_.size() == 0) |
| 85 |
{ |
| 86 |
// This is a tridiagonal matrix |
| 87 |
for (int v = 0 ; v < X.GetNumberVecs() ; ++v) |
| 88 |
{ |
| 89 |
for (int i = 0 ; i < X.GetGlobalLength() ; ++i) |
| 90 |
{ |
| 91 |
if (i == 0) |
| 92 |
{ |
| 93 |
(*MyY)[v][i] = (d_ * (*MyX)[v][i] + u_ * (*MyX)[v][i + 1]); |
| 94 |
} |
| 95 |
else if (i == X.GetGlobalLength() - 1) |
| 96 |
{ |
| 97 |
(*MyY)[v][i] = (d_ * (*MyX)[v][i] + l_ * (*MyX)[v][i-1]); |
| 98 |
} |
| 99 |
else |
| 100 |
{ |
| 101 |
(*MyY)[v][i] = (d_ * (*MyX)[v][i] + l_ * (*MyX)[v][i-1] + u_ * (*MyX)[v][i+1]); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
else |
| 107 |
{ |
| 108 |
// This is a diagonal matrix |
| 109 |
for (int v = 0 ; v < X.GetNumberVecs() ; ++v) |
| 110 |
{ |
| 111 |
for (int i = 0 ; i < X.GetGlobalLength() ; ++i) |
| 112 |
{ |
| 113 |
(*MyY)[v][i] = diag_[i] * (*MyX)[v][i]; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
private: |
| 120 |
//! Number of rows and columns |
| 121 |
int NumRows_; |
| 122 |
//! Elements on subdiagonal, diagonal, and superdiagonal. |
| 123 |
ScalarType l_, d_, u_; |
| 124 |
//! Elements on diagonal (for variable-diagonal case). |
| 125 |
std::vector<ScalarType> diag_; |
| 126 |
}; |
| 127 |
|
| 128 |
#endif //MY_OPERATOR_HPP |