Anasazi  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AnasaziBasicEigenproblem.hpp
Go to the documentation of this file.
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 
10 #ifndef ANASAZI_BASIC_EIGENPROBLEM_H
11 #define ANASAZI_BASIC_EIGENPROBLEM_H
12 
17 #include "AnasaziEigenproblem.hpp"
20 
26 namespace Anasazi {
27 
28  template<class ScalarType, class MV, class OP>
29  class BasicEigenproblem : public virtual Eigenproblem<ScalarType, MV, OP> {
30 
31  public:
32 
34 
35 
38 
40  BasicEigenproblem( const Teuchos::RCP<const OP>& Op, const Teuchos::RCP<MV>& InitVec );
41 
44 
47 
49  virtual ~BasicEigenproblem() {};
51 
53 
54 
61  void setOperator( const Teuchos::RCP<const OP>& Op ) { _Op = Op; _isSet=false; };
62 
65  void setA( const Teuchos::RCP<const OP>& A ) { _AOp = A; _isSet=false; };
66 
69  void setM( const Teuchos::RCP<const OP>& M ) { _MOp = M; _isSet=false; };
70 
73  void setPrec( const Teuchos::RCP<const OP>& Prec ) { _Prec = Prec; _isSet=false; };
74 
82  void setInitVec( const Teuchos::RCP<MV>& InitVec ) { _InitVec = InitVec; _isSet=false; };
83 
89  void setAuxVecs( const Teuchos::RCP<const MV>& AuxVecs ) { _AuxVecs = AuxVecs; _isSet=false; };
90 
92  void setNEV( int nev ){ _nev = nev; _isSet=false; };
93 
95 
98  void setHermitian( bool isSym ){ _isSym = isSym; _isSet=false; };
99 
115  bool setProblem();
116 
124 
126 
128 
129 
131  Teuchos::RCP<const OP> getOperator() const { return( _Op ); };
132 
134  Teuchos::RCP<const OP> getA() const { return( _AOp ); };
135 
137  Teuchos::RCP<const OP> getM() const { return( _MOp ); };
138 
140  Teuchos::RCP<const OP> getPrec() const { return( _Prec ); };
141 
144 
147 
149  int getNEV() const { return( _nev ); }
150 
152  bool isHermitian() const { return( _isSym ); }
153 
155  bool isProblemSet() const { return( _isSet ); }
156 
162  const Eigensolution<ScalarType,MV> & getSolution() const { return(_sol); }
163 
165 
167 
168 
177 
179 
180  protected:
181 
184 
187 
190 
193 
196 
199 
201  int _nev;
202 
204 
207  bool _isSym;
208 
210  bool _isSet;
211 
216 
219  };
220 
221 
222  //=============================================================================
223  // Implementations (Constructors / Destructors)
224  //=============================================================================
225  template <class ScalarType, class MV, class OP>
227  _nev(0),
228  _isSym(false),
229  _isSet(false)
230  {
231  }
232 
233 
234  //=============================================================================
235  template <class ScalarType, class MV, class OP>
237  _Op(Op),
238  _InitVec(InitVec),
239  _nev(0),
240  _isSym(false),
241  _isSet(false)
242  {
243  }
244 
245 
246  //=============================================================================
247  template <class ScalarType, class MV, class OP>
249  const Teuchos::RCP<MV>& InitVec ) :
250  _MOp(M),
251  _Op(Op),
252  _InitVec(InitVec),
253  _nev(0),
254  _isSym(false),
255  _isSet(false)
256  {
257  }
258 
259 
260  //=============================================================================
261  template <class ScalarType, class MV, class OP>
263  _AOp(Problem._AOp),
264  _MOp(Problem._MOp),
265  _Op(Problem._Op),
266  _Prec(Problem._Prec),
267  _InitVec(Problem._InitVec),
268  _nev(Problem._nev),
269  _isSym(Problem._isSym),
270  _isSet(Problem._isSet),
271  _sol(Problem._sol)
272  {
273  }
274 
275 
276  //=============================================================================
277  // SetProblem (sanity check method)
278  //=============================================================================
279  template <class ScalarType, class MV, class OP>
281  {
282  //----------------------------------------------------------------
283  // Sanity Checks
284  //----------------------------------------------------------------
285  // If there is no operator, then we can't proceed.
286  if ( !_AOp.get() && !_Op.get() ) { return false; }
287 
288  // If there is no initial vector, then we don't have anything to clone workspace from.
289  if ( !_InitVec.get() ) { return false; }
290 
291  // If we don't need any eigenvalues, we don't need to continue.
292  if (_nev == 0) { return false; }
293 
294  // If there is an A, but no operator, we can set them equal.
295  if (_AOp.get() && !_Op.get()) { _Op = _AOp; }
296 
297  // Clear the storage from any previous call to setSolution()
299  _sol = emptysol;
300 
301  // mark the problem as set and return no-error
302  _isSet=true;
303  return true;
304  }
305 
306  //=============================================================================
307  // Computes the residual vector
308  //=============================================================================
309  template <class ScalarType, class MV, class OP>
311  {
312  using Teuchos::RCP;
313 
314  TEUCHOS_TEST_FOR_EXCEPTION(!isHermitian(), std::invalid_argument,
315  "BasicEigenproblem::computeCurrResVec: This method is not currently "
316  "implemented for non-Hermitian problems. Sorry for any inconvenience.");
317 
318  const Eigensolution<ScalarType,MV> sol = getSolution();
319 
320  if(sol.numVecs <= 0)
321  return Teuchos::null;
322 
323  // Copy the eigenvalues
324  RCP<MV> X = sol.Evecs;
325  std::vector<ScalarType> Lambda(sol.numVecs);
326  for(int i = 0; i < sol.numVecs; i++)
327  Lambda[i] = sol.Evals[i].realpart;
328 
329  // Compute AX
330  RCP<MV> AX = MVT::Clone(*X,sol.numVecs);
331  if(_AOp != Teuchos::null)
332  OPT::Apply(*_AOp,*X,*AX);
333  else
334  OPT::Apply(*_Op,*X,*AX);
335 
336  // Compute MX if necessary
337  RCP<MV> MX;
338  if(_MOp != Teuchos::null)
339  {
340  MX = MVT::Clone(*X,sol.numVecs);
341  OPT::Apply(*_MOp,*X,*MX);
342  }
343  else
344  {
345  MX = MVT::CloneCopy(*X);
346  }
347 
348  // Compute R = AX - MX \Lambda
349  RCP<MV> R = MVT::Clone(*X,sol.numVecs);
350  MVT::MvScale(*MX,Lambda);
351  MVT::MvAddMv(1.0,*AX,-1.0,*MX,*R);
352 
353  return R;
354  }
355 
356 } // end Anasazi namespace
357 #endif
358 
359 // end AnasaziBasicEigenproblem.hpp
Teuchos::RCP< const MV > getAuxVecs() const
Get a pointer to the auxiliary vector.
Teuchos::RCP< const OP > getOperator() const
Get a pointer to the operator for which eigenvalues will be computed.
bool setProblem()
Specify that this eigenproblem is fully defined.
std::vector< Value< ScalarType > > Evals
The computed eigenvalues.
MultiVecTraits< ScalarType, MV > MVT
Type-definition for the MultiVecTraits class corresponding to the MV type.
int getNEV() const
Get the number of eigenvalues (NEV) that are required by this eigenproblem.
void setInitVec(const Teuchos::RCP< MV > &InitVec)
Set the initial guess.
bool isHermitian() const
Get the symmetry information for this eigenproblem.
This class defines the interface required by an eigensolver and status test class to compute solution...
Declaration of basic traits for the multivector type.
OperatorTraits< ScalarType, MV, OP > OPT
Type-definition for the OperatorTraits class corresponding to the OP type.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Virtual base class which defines basic traits for the operator type.
Teuchos::RCP< MV > Evecs
The computed eigenvectors.
Teuchos::RCP< const MV > _AuxVecs
Reference-counted pointer for the auxiliary vector of the eigenproblem .
bool _isSym
Symmetry of the eigenvalue problem.
int _nev
Number of eigenvalues requested.
Teuchos::RCP< MV > _InitVec
Reference-counted pointer for the initial vector of the eigenproblem .
Teuchos::RCP< const OP > _Op
Reference-counted pointer for the operator of the eigenproblem .
void setAuxVecs(const Teuchos::RCP< const MV > &AuxVecs)
Set auxiliary vectors.
int numVecs
The number of computed eigenpairs.
Teuchos::RCP< const MV > computeCurrResVec() const
Returns the residual vector corresponding to the computed solution.
Abstract base class which defines the interface required by an eigensolver and status test class to c...
Teuchos::RCP< const MV > getInitVec() const
Get a pointer to the initial vector.
Teuchos::RCP< const OP > getM() const
Get a pointer to the operator M of the eigenproblem .
Traits class which defines basic operations on multivectors.
Virtual base class which defines basic traits for the operator type.
void setSolution(const Eigensolution< ScalarType, MV > &sol)
Set the solution to the eigenproblem.
bool isProblemSet() const
If the problem has been set, this method will return true.
virtual ~BasicEigenproblem()
Destructor.
Teuchos::RCP< const OP > _MOp
Reference-counted pointer for M of the eigenproblem .
Struct for storing an eigenproblem solution.
Teuchos::RCP< const OP > getA() const
Get a pointer to the operator A of the eigenproblem .
void setPrec(const Teuchos::RCP< const OP > &Prec)
Set the preconditioner for this eigenvalue problem .
const Eigensolution< ScalarType, MV > & getSolution() const
Get the solution to the eigenproblem.
void setHermitian(bool isSym)
Specify the symmetry of this eigenproblem.
This provides a basic implementation for defining standard or generalized eigenvalue problems...
Teuchos::RCP< const OP > _Prec
Reference-counted pointer for the preconditioner of the eigenproblem .
void setM(const Teuchos::RCP< const OP > &M)
Set the operator M of the eigenvalue problem .
BasicEigenproblem()
Empty constructor - allows Anasazi::BasicEigenproblem to be described at a later time through &quot;Set Me...
void setA(const Teuchos::RCP< const OP > &A)
Set the operator A of the eigenvalue problem .
Teuchos::RCP< const OP > _AOp
Reference-counted pointer for A of the eigenproblem .
Teuchos::RCP< const OP > getPrec() const
Get a pointer to the preconditioner of the eigenproblem .
void setNEV(int nev)
Specify the number of eigenvalues (NEV) that are requested.
Eigensolution< ScalarType, MV > _sol
Solution to problem.
void setOperator(const Teuchos::RCP< const OP > &Op)
Set the operator for which eigenvalues will be computed.