Ifpack Package Browser (Single Doxygen Collection) Development
Ifpack_ex_Factory.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                IFPACK
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include "Ifpack_ConfigDefs.h"
00030 
00031 #ifdef HAVE_MPI
00032 #include "Epetra_MpiComm.h"
00033 #else
00034 #include "Epetra_SerialComm.h"
00035 #endif
00036 #include "Epetra_CrsMatrix.h"
00037 #include "Epetra_MultiVector.h"
00038 #include "Epetra_LinearProblem.h"
00039 #include "Galeri_Maps.h"
00040 #include "Galeri_CrsMatrices.h"
00041 #include "Teuchos_ParameterList.hpp"
00042 #include "Teuchos_RefCountPtr.hpp"
00043 #include "AztecOO.h"
00044 #include "Ifpack.h"
00045 #include "Ifpack_AdditiveSchwarz.h"
00046 
00047 int main(int argc, char *argv[])
00048 {
00049 
00050 #ifdef HAVE_MPI
00051   MPI_Init(&argc,&argv);
00052   Epetra_MpiComm Comm( MPI_COMM_WORLD );
00053 #else
00054   Epetra_SerialComm Comm;
00055 #endif
00056 
00057   Teuchos::ParameterList GaleriList;
00058 
00059   // The problem is defined on a 2D grid, global size is nx * nx.
00060   int nx = 30; 
00061   GaleriList.set("n", nx * nx);
00062   GaleriList.set("nx", nx);
00063   GaleriList.set("ny", nx);
00064   Teuchos::RefCountPtr<Epetra_Map> Map = Teuchos::rcp( Galeri::CreateMap("Linear", Comm, GaleriList) );
00065   Teuchos::RefCountPtr<Epetra_RowMatrix> A = Teuchos::rcp( Galeri::CreateCrsMatrix("Laplace2D", &*Map, GaleriList) );
00066 
00067   // =============================================================== //
00068   // B E G I N N I N G   O F   I F P A C K   C O N S T R U C T I O N //
00069   // =============================================================== //
00070 
00071   Teuchos::ParameterList List;
00072 
00073   // allocates an IFPACK factory. No data is associated 
00074   // to this object (only method Create()).
00075   Ifpack Factory;
00076 
00077   // create the preconditioner. For valid PrecType values,
00078   // please check the documentation
00079   string PrecType = "ILU"; // incomplete LU
00080   int OverlapLevel = 1; // must be >= 0. If Comm.NumProc() == 1,
00081                         // it is ignored.
00082 
00083   Teuchos::RefCountPtr<Ifpack_Preconditioner> Prec = Teuchos::rcp( Factory.Create(PrecType, &*A, OverlapLevel) );
00084   assert(Prec != Teuchos::null);
00085 
00086   // specify parameters for ILU
00087   List.set("fact: drop tolerance", 1e-9);
00088   List.set("fact: level-of-fill", 1);
00089   // the combine mode is on the following:
00090   // "Add", "Zero", "Insert", "InsertAdd", "Average", "AbsMax"
00091   // Their meaning is as defined in file Epetra_CombineMode.h   
00092   List.set("schwarz: combine mode", "Add");
00093   // sets the parameters
00094   IFPACK_CHK_ERR(Prec->SetParameters(List));
00095 
00096   // initialize the preconditioner. At this point the matrix must
00097   // have been FillComplete()'d, but actual values are ignored.
00098   IFPACK_CHK_ERR(Prec->Initialize());
00099 
00100   // Builds the preconditioners, by looking for the values of 
00101   // the matrix.
00102   IFPACK_CHK_ERR(Prec->Compute());
00103 
00104   // =================================================== //
00105   // E N D   O F   I F P A C K   C O N S T R U C T I O N //
00106   // =================================================== //
00107 
00108   // At this point, we need some additional objects
00109   // to define and solve the linear system.
00110 
00111   // defines LHS and RHS
00112   Epetra_Vector LHS(A->OperatorDomainMap());
00113   Epetra_Vector RHS(A->OperatorDomainMap());
00114 
00115   // solution is constant
00116   LHS.PutScalar(1.0);
00117   // now build corresponding RHS
00118   A->Apply(LHS,RHS);
00119 
00120   // now randomize the solution
00121   RHS.Random();
00122 
00123   // need an Epetra_LinearProblem to define AztecOO solver
00124   Epetra_LinearProblem Problem(&*A,&LHS,&RHS);
00125 
00126   // now we can allocate the AztecOO solver
00127   AztecOO Solver(Problem);
00128 
00129   // specify solver
00130   Solver.SetAztecOption(AZ_solver,AZ_gmres);
00131   Solver.SetAztecOption(AZ_output,32);
00132 
00133   // HERE WE SET THE IFPACK PRECONDITIONER
00134   Solver.SetPrecOperator(&*Prec);
00135 
00136   // .. and here we solve
00137   Solver.Iterate(1550,1e-8);
00138 
00139   cout << *Prec;
00140 
00141 #ifdef HAVE_MPI
00142   MPI_Finalize() ; 
00143 #endif
00144 
00145   return(EXIT_SUCCESS);
00146 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines