| 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_BETTER_OPERATOR_HPP |
| 10 |
#define MY_BETTER_OPERATOR_HPP |
| 11 |
|
| 12 |
#include "AnasaziConfigDefs.hpp" |
| 13 |
#include "AnasaziOperator.hpp" |
| 14 |
#include "MyMultiVec.hpp" |
| 15 |
#include "Teuchos_BLAS.hpp" |
| 16 |
|
| 17 |
//! Simple example of a user's defined Anasazi::Operator class. |
| 18 |
/*! |
| 19 |
* This is a simple, single processor example of user's defined |
| 20 |
* Anasazi::Operator-derived class. The class is templated with ScalarType; |
| 21 |
* possible choices are, for example, "float", "double", or |
| 22 |
* "complex<double>". |
| 23 |
* |
| 24 |
* This class is based on the MyOperator class written by |
| 25 |
* Oscar Chinallato (ETHZ/ICOS) and Marzio Sala (ETHZ/COLAB) |
| 26 |
* |
| 27 |
* \author Christopher Baker (FSU/SCS,SNL/CSRI) |
| 28 |
* |
| 29 |
*/ |
| 30 |
template <class ScalarType> |
| 31 |
class MyBetterOperator : public Anasazi::Operator<ScalarType> |
| 32 |
{ |
| 33 |
|
| 34 |
public: |
| 35 |
|
| 36 |
//! Constructor |
| 37 |
MyBetterOperator(const int nrows, const int *colptr, |
| 38 |
const int nnz, const int *rowin, const ScalarType *vals) |
| 39 |
: _nr(nrows), _nnz(nnz), _cptr(nrows+1), _rind(nnz), _vals(nnz) |
| 40 |
{ |
| 41 |
std::copy<const int*,IntIter>(colptr,colptr+nrows+1,_cptr.begin()); |
| 42 |
std::copy<const int*,IntIter>(rowin,rowin+nnz,_rind.begin()); |
| 43 |
std::copy<const ScalarType*,STIter>(vals,vals+nnz,_vals.begin()); |
| 44 |
} |
| 45 |
|
| 46 |
//! Deconstructor |
| 47 |
~MyBetterOperator() |
| 48 |
{ } |
| 49 |
|
| 50 |
//! Applies the matrix to a multivector. |
| 51 |
void Apply(const Anasazi::MultiVec<ScalarType>& X, |
| 52 |
Anasazi::MultiVec<ScalarType>& Y) const |
| 53 |
{ |
| 54 |
const MyMultiVec<ScalarType>* MyX; |
| 55 |
MyX = dynamic_cast<const MyMultiVec<ScalarType>*>(&X); |
| 56 |
assert (MyX != 0); |
| 57 |
|
| 58 |
MyMultiVec<ScalarType>* MyY; |
| 59 |
MyY = dynamic_cast<MyMultiVec<ScalarType>*>(&Y); |
| 60 |
assert (MyY != 0); |
| 61 |
|
| 62 |
// Initialize output vector to zero. |
| 63 |
MyY->MvInit( Teuchos::ScalarTraits<ScalarType>::zero() ); |
| 64 |
|
| 65 |
assert (X.GetNumberVecs() == Y.GetNumberVecs()); |
| 66 |
assert (X.GetGlobalLength() == Y.GetGlobalLength()); |
| 67 |
|
| 68 |
int nv = X.GetNumberVecs(); |
| 69 |
|
| 70 |
// Apply operator |
| 71 |
int IA1, IA2, ri; |
| 72 |
ScalarType aval; |
| 73 |
int i,j,v; |
| 74 |
for (j=0; j<_nr; j++) { |
| 75 |
IA1 = _cptr[j]-1; |
| 76 |
IA2 = _cptr[j+1]-1; |
| 77 |
for (i=IA1; i<IA2; i++) { |
| 78 |
ri = _rind[i]-1; |
| 79 |
aval = _vals[i]; |
| 80 |
for (v=0; v<nv; v++) { |
| 81 |
(*MyY)[v][ri] += aval*(*MyX)[v][j]; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
private: |
| 88 |
typedef typename std::vector<ScalarType>::iterator STIter; |
| 89 |
typedef std::vector<int>::iterator IntIter; |
| 90 |
//! Number of rows and columns |
| 91 |
int _nr, _nnz; |
| 92 |
//! Column pointers |
| 93 |
std::vector<int> _cptr; |
| 94 |
//! Row indices |
| 95 |
std::vector<int> _rind; |
| 96 |
//! Values |
| 97 |
std::vector<ScalarType> _vals; |
| 98 |
}; |
| 99 |
|
| 100 |
#endif //MY_BETTER_OPERATOR_HPP |