test/IC/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_CrsIct.h"
00050 #include "Ifpack.h"
00051 #include "Teuchos_RefCountPtr.hpp"
00052 
00053 // function for fancy output
00054 
00055 string toString(const int& x) {
00056   char s[100];
00057   sprintf(s, "%d", x);
00058   return string(s);
00059 }
00060 
00061 string toString(const double& x) {
00062   char s[100];
00063   sprintf(s, "%g", x);
00064   return string(s);
00065 }
00066 
00067 // main driver
00068 
00069 int main(int argc, char *argv[]) {
00070 
00071 #ifdef HAVE_MPI
00072   MPI_Init(&argc,&argv);
00073   Epetra_MpiComm Comm (MPI_COMM_WORLD);
00074 #else
00075   Epetra_SerialComm Comm;
00076 #endif
00077 
00078   int MyPID = Comm.MyPID();
00079   bool verbose = false; 
00080   if (MyPID==0) verbose = true;
00081 
00082   // The problem is defined on a 2D grid, global size is nx * nx.
00083   int nx = 30;
00084   Teuchos::ParameterList GaleriList;
00085   GaleriList.set("nx", nx);
00086   GaleriList.set("ny", nx * Comm.NumProc());
00087   GaleriList.set("mx", 1);
00088   GaleriList.set("my", Comm.NumProc());
00089   Teuchos::RefCountPtr<Epetra_Map> Map = Teuchos::rcp( Galeri::CreateMap("Cartesian2D", Comm, GaleriList) );
00090   Teuchos::RefCountPtr<Epetra_CrsMatrix> A = Teuchos::rcp( Galeri::CreateCrsMatrix("Laplace2D", &*Map, GaleriList) );
00091   Teuchos::RefCountPtr<Epetra_MultiVector> LHS = Teuchos::rcp( new Epetra_MultiVector(*Map, 1) );
00092   Teuchos::RefCountPtr<Epetra_MultiVector> RHS = Teuchos::rcp( new Epetra_MultiVector(*Map, 1) );
00093   LHS->PutScalar(0.0); RHS->Random();
00094 
00095   // ============================ //
00096   // Construct ILU preconditioner //
00097   // ---------------------------- //
00098 
00099   // I wanna test funky values to be sure that they have the same
00100   // influence on the algorithms, both old and new
00101   int    LevelFill = 2;
00102   double DropTol = 0.3333;
00103   double Condest;
00104   
00105   Teuchos::RefCountPtr<Ifpack_CrsIct> ICT;
00106   ICT = Teuchos::rcp( new Ifpack_CrsIct(*A,DropTol,LevelFill) );
00107   ICT->SetAbsoluteThreshold(0.00123);
00108   ICT->SetRelativeThreshold(0.9876);
00109   // Init values from A
00110   ICT->InitValues(*A);
00111   // compute the factors
00112   ICT->Factor();
00113   // and now estimate the condition number
00114   ICT->Condest(false,Condest);
00115   
00116   if( Comm.MyPID() == 0 ) {
00117     cout << "Condition number estimate (level-of-fill = "
00118    << LevelFill <<  ") = " << Condest << endl;
00119   }
00120 
00121   // Define label for printing out during the solve phase
00122   string label = "Ifpack_CrsIct Preconditioner: LevelFill = " + toString(LevelFill) + 
00123                                                  " Overlap = 0"; 
00124   ICT->SetLabel(label.c_str());
00125   
00126   // Here we create an AztecOO object
00127   LHS->PutScalar(0.0);
00128 
00129   int Niters = 1200;
00130 
00131   AztecOO solver;
00132   solver.SetUserMatrix(&*A);
00133   solver.SetLHS(&*LHS);
00134   solver.SetRHS(&*RHS);
00135   solver.SetAztecOption(AZ_solver,AZ_cg);
00136   solver.SetPrecOperator(&*ICT);
00137   solver.SetAztecOption(AZ_output, 16); 
00138   solver.Iterate(Niters, 5.0e-5);
00139 
00140   int OldIters = solver.NumIters();
00141 
00142   // now rebuild the same preconditioner using ICT, we expect the same
00143   // number of iterations
00144 
00145   Ifpack Factory;
00146   Teuchos::RefCountPtr<Ifpack_Preconditioner> Prec = Teuchos::rcp( Factory.Create("IC", &*A) );
00147 
00148   Teuchos::ParameterList List;
00149   List.get("fact: level-of-fill", 2);
00150   List.get("fact: drop tolerance", 0.3333);
00151   List.get("fact: absolute threshold", 0.00123);
00152   List.get("fact: relative threshold", 0.9876);
00153   List.get("fact: relaxation value", 0.0);
00154 
00155   IFPACK_CHK_ERR(Prec->SetParameters(List));
00156   IFPACK_CHK_ERR(Prec->Compute());
00157 
00158   // Here we create an AztecOO object
00159   LHS->PutScalar(0.0);
00160 
00161   solver.SetUserMatrix(&*A);
00162   solver.SetLHS(&*LHS);
00163   solver.SetRHS(&*RHS);
00164   solver.SetAztecOption(AZ_solver,AZ_cg);
00165   solver.SetPrecOperator(&*Prec);
00166   solver.SetAztecOption(AZ_output, 16); 
00167   solver.Iterate(Niters, 5.0e-5);
00168 
00169   int NewIters = solver.NumIters();
00170 
00171   if (OldIters != NewIters)
00172     IFPACK_CHK_ERR(-1);
00173 
00174 #ifdef HAVE_MPI
00175   MPI_Finalize() ;
00176 #endif
00177 
00178   return(EXIT_SUCCESS);
00179 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 10:05:33 2011 for Ifpack Package Browser (Single Doxygen Collection) by  doxygen 1.6.3