MVOPTester/MVOPTesterEx.cpp

This is an example of how to use the Anasazi::TestMultiVecTraits() and Anasazi::TestOperatorTraits() methods.

00001 #include "AnasaziConfigDefs.hpp"
00002 #include "AnasaziEpetraAdapter.hpp"
00003 #include "AnasaziMVOPTester.hpp"
00004 #include "AnasaziBasicOutputManager.hpp"
00005 
00006 #include "Epetra_Map.h"
00007 #include "Epetra_CrsMatrix.h"
00008 #include "Epetra_Comm.h"
00009 
00010 #ifdef HAVE_MPI
00011 #include "mpi.h"
00012 #include "Epetra_MpiComm.h"
00013 #else
00014 #include "Epetra_SerialComm.h"
00015 #endif
00016 
00017 int main(int argc, char *argv[])
00018 {
00019 
00020 #ifdef HAVE_MPI
00021   // Initialize MPI and setup an Epetra communicator
00022   MPI_Init(&argc,&argv);
00023   Epetra_MpiComm Comm(MPI_COMM_WORLD);
00024 #else
00025   // If we aren't using MPI, then setup a serial communicator.
00026   Epetra_SerialComm Comm;
00027 #endif
00028 
00029   // Create an output manager to handle the I/O from the solver
00030   Teuchos::RCP<Anasazi::OutputManager<double> > MyOM = Teuchos::rcp( new Anasazi::BasicOutputManager<double>() );
00031   MyOM->setVerbosity( Anasazi::Warnings );
00032   MyOM->stream(Anasazi::Errors) << Anasazi::Anasazi_Version() << endl << endl;
00033 
00034   // Number of global elements
00035   int dim = 100;
00036   int blockSize = 5;
00037 
00038   // Construct a Map that puts approximately the same number of 
00039   // equations on each processor.
00040   Epetra_Map Map(dim, 0, Comm);
00041   
00042   // Get update list and number of local equations from newly created Map.
00043   int NumMyElements = Map.NumMyElements();
00044   int * MyGlobalElements = new int[NumMyElements];
00045   Map.MyGlobalElements(MyGlobalElements);
00046 
00047   // Create an integer vector NumNz that is used to build the Petra Matrix.
00048   // NumNz[i] is the Number of OFF-DIAGONAL term for the ith global equation 
00049   // on this processor
00050   int * NumNz = new int[NumMyElements];
00051 
00052   // We are building a tridiagonal matrix where each row has (-1 2 -1)
00053   // So we need 2 off-diagonal terms (except for the first and last equation)
00054   for (int i=0; i<NumMyElements; i++) {
00055     if (MyGlobalElements[i]==0 || MyGlobalElements[i] == dim-1) {
00056       NumNz[i] = 2;
00057     }
00058     else {
00059       NumNz[i] = 3;
00060     }
00061   }
00062 
00063   // Create an Epetra_Matrix
00064   Teuchos::RCP<Epetra_CrsMatrix> A 
00065     = Teuchos::rcp( new Epetra_CrsMatrix(Copy, Map, NumNz) );
00066   
00067   // Add  rows one-at-a-time
00068   // Need some vectors to help
00069   // Off diagonal Values will always be -1
00070   double *Values = new double[2];
00071   Values[0] = -1.0; Values[1] = -1.0;
00072   int *Indices = new int[2];
00073   double two = 2.0;
00074   int NumEntries;
00075   for (int i=0; i<NumMyElements; i++) {
00076     if (MyGlobalElements[i]==0) {
00077       Indices[0] = 1;
00078       NumEntries = 1;
00079     }
00080     else if (MyGlobalElements[i] == dim-1) {
00081       Indices[0] = dim-2;
00082       NumEntries = 1;
00083     }
00084     else {
00085       Indices[0] = MyGlobalElements[i]-1;
00086       Indices[1] = MyGlobalElements[i]+1;
00087       NumEntries = 2;
00088     }
00089     int ierr = A->InsertGlobalValues(MyGlobalElements[i],NumEntries,Values,Indices);
00090     assert(ierr==0);
00091     // Put in the diagonal entry
00092     ierr = A->InsertGlobalValues(MyGlobalElements[i],1,&two,MyGlobalElements+i);
00093     assert(ierr==0);
00094   }
00095    
00096   // Finish building the epetra matrix A
00097   int ierr = A->FillComplete();
00098   assert(ierr==0);
00099 
00100   // Issue several useful typedefs;
00101   // The MultiVecTraits class is for defining ....
00102   typedef Epetra_MultiVector MV;
00103   typedef Epetra_Operator OP;
00104   typedef Anasazi::OperatorTraits<double, MV, OP> OPT;
00105 
00106   // Create an Epetra_MultiVector for an initial vector to start the solver.
00107   // Note that this needs to have the same number of columns as the blocksize.
00108   Teuchos::RCP<MV> ivec 
00109     = Teuchos::rcp( new MV(Map, blockSize) );
00110   ivec->Random();
00111 
00112   // test the multivector class
00113   bool testret;
00114   testret = Anasazi::TestMultiVecTraits<double,MV>(MyOM,ivec);
00115   if (testret) {
00116     MyOM->print(Anasazi::Warnings,"*** PASSED TestMultiVecTraits() ***\n");
00117   }
00118   else {
00119     MyOM->print(Anasazi::Warnings,"*** FAILED TestMultiVecTraits() ***\n");
00120   }
00121 
00122 
00123   // test the operator class
00124   testret = Anasazi::TestOperatorTraits<double,MV,OP>(MyOM,ivec,A);
00125   if (testret) {
00126     MyOM->print(Anasazi::Warnings,"*** PASSED TestOperatorTraits() ***\n");
00127   }
00128   else {
00129     MyOM->print(Anasazi::Warnings,"*** FAILED TestOperatorTraits() ***\n");
00130   }
00131 
00132   // Release all objects
00133   delete [] NumNz;
00134   delete [] Values;
00135   delete [] Indices;
00136   delete [] MyGlobalElements;
00137 
00138 #ifdef HAVE_MPI
00139   MPI_Finalize();
00140 #endif
00141 
00142   return 0;
00143 }

Generated on Tue Oct 20 12:45:18 2009 for Anasazi by doxygen 1.4.7