test/ILU/cxx_main.cpp

Go to the documentation of this file.
00001 
00002 // @HEADER
00003 // ***********************************************************************
00004 // 
00005 //            Trilinos: An Object-Oriented Solver Framework
00006 //                 Copyright (2001) Sandia Corporation
00007 // 
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 // 
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //  
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //  
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA
00025 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00026 // 
00027 // ***********************************************************************
00028 // @HEADER
00029 
00030 #include "Ifpack_ConfigDefs.h"
00031 
00032 #include "Epetra_ConfigDefs.h"
00033 #ifdef HAVE_MPI
00034 #include "mpi.h"
00035 #include "Epetra_MpiComm.h"
00036 #else
00037 #include "Epetra_SerialComm.h"
00038 #endif
00039 #include "Epetra_Comm.h"
00040 #include "Epetra_Map.h"
00041 #include "Epetra_Time.h"
00042 #include "Epetra_BlockMap.h"
00043 #include "Epetra_MultiVector.h"
00044 #include "Epetra_Vector.h"
00045 #include "Epetra_Export.h"
00046 #include "AztecOO.h"
00047 #include "Galeri_Maps.h"
00048 #include "Galeri_CrsMatrices.h"
00049 #include "Ifpack_CrsRiluk.h"
00050 #include "Ifpack.h"
00051 
00052 // function for fancy output
00053 
00054 string toString(const int& x) {
00055   char s[100];
00056   sprintf(s, "%d", x);
00057   return string(s);
00058 }
00059 
00060 string toString(const double& x) {
00061   char s[100];
00062   sprintf(s, "%g", x);
00063   return string(s);
00064 }
00065 
00066 // main driver
00067 
00068 int main(int argc, char *argv[]) {
00069 
00070 #ifdef HAVE_MPI
00071   MPI_Init(&argc,&argv);
00072   Epetra_MpiComm Comm (MPI_COMM_WORLD);
00073 #else
00074   Epetra_SerialComm Comm;
00075 #endif
00076 
00077   int MyPID = Comm.MyPID();
00078   bool verbose = false; 
00079   if (MyPID==0) verbose = true;
00080 
00081   Teuchos::ParameterList GaleriList;
00082   int nx = 30; 
00083   GaleriList.set("nx", nx);
00084   GaleriList.set("ny", nx * Comm.NumProc());
00085   GaleriList.set("mx", 1);
00086   GaleriList.set("my", Comm.NumProc());
00087   Epetra_Map* Map = Galeri::CreateMap("Cartesian2D", Comm, GaleriList);
00088   Epetra_CrsMatrix* A = Galeri::CreateCrsMatrix("Laplace2D", Map, GaleriList);
00089   Epetra_MultiVector* LHS = new Epetra_MultiVector(*Map, 1);
00090   Epetra_MultiVector* RHS = new Epetra_MultiVector(*Map, 1);
00091   LHS->PutScalar(0.0); RHS->Random();
00092 
00093   // ============================ //
00094   // Construct ILU preconditioner //
00095   // ---------------------------- //
00096 
00097   // I wanna test funky values to be sure that they have the same
00098   // influence on the algorithms, both old and new
00099   int    LevelFill = 2;
00100   double DropTol = 0.3333;
00101   double Athresh = 0.0123;
00102   double Rthresh = 0.9876;
00103   double Relax   = 0.1;
00104   int    Overlap = 2;
00105   
00106   Ifpack_IlukGraph* Graph = 0;
00107   Ifpack_CrsRiluk* RILU = 0;
00108 
00109   Graph = new Ifpack_IlukGraph(A->Graph(), LevelFill, Overlap);
00110   int ierr;
00111   ierr = Graph->ConstructFilledGraph();
00112   IFPACK_CHK_ERR(ierr);
00113 
00114   RILU = new Ifpack_CrsRiluk(*Graph);
00115   RILU->SetAbsoluteThreshold(Athresh);
00116   RILU->SetRelativeThreshold(Rthresh);
00117   RILU->SetRelaxValue(Relax);
00118   int initerr = RILU->InitValues(*A);
00119   if (initerr!=0) cout << Comm << "*ERR* InitValues = " << initerr;
00120 
00121   RILU->Factor();
00122 
00123   // Define label for printing out during the solve phase
00124   string label = "Ifpack_CrsRiluk Preconditioner: LevelFill = " + toString(LevelFill) +
00125                                                  " Overlap = " + toString(Overlap) +
00126                                                  " Athresh = " + toString(Athresh) +
00127                                                  " Rthresh = " + toString(Rthresh);
00128   // Here we create an AztecOO object
00129   LHS->PutScalar(0.0);
00130 
00131   int Niters = 1200;
00132 
00133   AztecOO solver;
00134   solver.SetUserMatrix(A);
00135   solver.SetLHS(LHS);
00136   solver.SetRHS(RHS);
00137   solver.SetAztecOption(AZ_solver,AZ_gmres);
00138   solver.SetPrecOperator(RILU);
00139   solver.SetAztecOption(AZ_output, 16); 
00140   solver.Iterate(Niters, 5.0e-5);
00141 
00142   int OldIters = solver.NumIters();
00143 
00144 
00145   if (RILU!=0) delete RILU;
00146                
00147   // now rebuild the same preconditioner using RILU, we expect the same
00148   // number of iterations
00149 
00150   Ifpack Factory;
00151   Ifpack_Preconditioner* Prec = Factory.Create("ILU", A, Overlap);
00152 
00153   Teuchos::ParameterList List;
00154   List.get("fact: level-of-fill", LevelFill);
00155   List.get("fact: drop tolerance", DropTol);
00156   List.get("fact: absolute threshold", Athresh);
00157   List.get("fact: relative threshold", Rthresh);
00158   List.get("fact: relax value", Relax);
00159 
00160   IFPACK_CHK_ERR(Prec->SetParameters(List));
00161   IFPACK_CHK_ERR(Prec->Compute());
00162 
00163   // Here we create an AztecOO object
00164   LHS->PutScalar(0.0);
00165 
00166   solver.SetUserMatrix(A);
00167   solver.SetLHS(LHS);
00168   solver.SetRHS(RHS);
00169   solver.SetAztecOption(AZ_solver,AZ_gmres);
00170   solver.SetPrecOperator(Prec);
00171   solver.SetAztecOption(AZ_output, 16); 
00172   solver.Iterate(Niters, 5.0e-5);
00173 
00174   int NewIters = solver.NumIters();
00175 
00176   if (OldIters != NewIters)
00177     IFPACK_CHK_ERR(-1);
00178 
00179   delete Prec;
00180   delete LHS;
00181   delete RHS;
00182   delete A;
00183   delete Map;
00184 
00185 #ifdef HAVE_MPI
00186   MPI_Finalize() ;
00187 #endif
00188 
00189   return(EXIT_SUCCESS);
00190 }

Generated on Thu Sep 18 12:37:21 2008 for Ifpack Package Browser (Single Doxygen Collection) by doxygen 1.3.9.1