Ifpack_ex_BlockRelaxation.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_AdditiveSchwarz.h"
00045 #include "Ifpack_PointRelaxation.h"
00046 #include "Ifpack_BlockRelaxation.h"
00047 #include "Ifpack_SparseContainer.h"
00048 #include "Ifpack_Amesos.h"
00049 
00050 int main(int argc, char *argv[])
00051 {
00052   // initialize MPI and Epetra communicator
00053 #ifdef HAVE_MPI
00054   MPI_Init(&argc,&argv);
00055   Epetra_MpiComm Comm( MPI_COMM_WORLD );
00056 #else
00057   Epetra_SerialComm Comm;
00058 #endif
00059 
00060   Teuchos::ParameterList GaleriList;
00061 
00062   // The problem is defined on a 2D grid, global size is nx * nx.
00063   int nx = 30; 
00064   GaleriList.set("nx", nx);
00065   GaleriList.set("ny", nx * Comm.NumProc());
00066   GaleriList.set("mx", 1);
00067   GaleriList.set("my", Comm.NumProc());
00068   Teuchos::RefCountPtr<Epetra_Map> Map = Teuchos::rcp( Galeri::CreateMap("Cartesian2D", Comm, GaleriList) );
00069   Teuchos::RefCountPtr<Epetra_RowMatrix> A = Teuchos::rcp( Galeri::CreateCrsMatrix("Laplace2D", &*Map, GaleriList) );
00070 
00071   // =============================================================== //
00072   // 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 //
00073   // =============================================================== //
00074 
00075   Teuchos::ParameterList List;
00076 
00077   // builds an Ifpack_AdditiveSchwarz. This is templated with
00078   // the local solvers, in this case Ifpack_BlockRelaxation.
00079   // Ifpack_BlockRelaxation requires as a templated a container
00080   // class. A container defines
00081   // how to store the diagonal blocks. Two choices are avaiable:
00082   // Ifpack_DenseContainer (to store them as dense block,
00083   // than use LAPACK' factorization to apply the inverse of
00084   // each block), of Ifpack_SparseContainer (to store
00085   // the diagonal block as Epetra_CrsMatrix's). 
00086   // 
00087   // Here, we use Ifpack_SparseContainer, which in turn is
00088   // templated with the class to use to apply the inverse
00089   // of each block. For example, we can use Ifpack_Amesos.
00090  
00091   // We still have to decide the overlap among the processes,
00092   // and the overlap among the blocks. The two values
00093   // can be different. The overlap among the blocks is
00094   // considered only if block Jacobi is used.
00095   int OverlapProcs = 2;
00096   int OverlapBlocks = 0;
00097 
00098   // define the block below to use dense containers
00099 #if 0
00100   Ifpack_AdditiveSchwarz<Ifpack_BlockRelaxation<Ifpack_DenseContainer> > Prec(A, OverlapProcs);
00101 #else
00102   Ifpack_AdditiveSchwarz<Ifpack_BlockRelaxation<Ifpack_SparseContainer<Ifpack_Amesos> > > Prec(&*A, OverlapProcs);
00103 #endif
00104 
00105   List.set("relaxation: type", "symmetric Gauss-Seidel");
00106   List.set("partitioner: overlap", OverlapBlocks);
00107 #ifdef HAVE_IFPACK_METIS
00108   // use METIS to create the blocks. This requires --enable-ifpack-metis.
00109   // If METIS is not installed, the user may select "linear". 
00110   List.set("partitioner: type", "metis");
00111 #else
00112   // or a simple greedy algorithm is METIS is not enabled
00113   List.set("partitioner: type", "greedy");
00114 #endif
00115   // defines here the number of local blocks. If 1,
00116   // and only one process is used in the computation, then
00117   // the preconditioner must converge in one iteration. 
00118   List.set("partitioner: local parts", 4);
00119 
00120   // sets the parameters
00121   IFPACK_CHK_ERR(Prec.SetParameters(List));
00122 
00123   // initialize the preconditioner. 
00124   IFPACK_CHK_ERR(Prec.Initialize());
00125 
00126   // Builds the preconditioners.
00127   IFPACK_CHK_ERR(Prec.Compute());
00128 
00129   // =================================================== //
00130   // E N D   O F   I F P A C K   C O N S T R U C T I O N //
00131   // =================================================== //
00132 
00133   // At this point, we need some additional objects
00134   // to define and solve the linear system.
00135 
00136   // defines LHS and RHS
00137   Epetra_Vector LHS(A->OperatorDomainMap());
00138   Epetra_Vector RHS(A->OperatorDomainMap());
00139 
00140   LHS.PutScalar(0.0);
00141   RHS.Random();
00142 
00143   // need an Epetra_LinearProblem to define AztecOO solver
00144   Epetra_LinearProblem Problem(&*A,&LHS,&RHS);
00145 
00146   // now we can allocate the AztecOO solver
00147   AztecOO Solver(Problem);
00148 
00149   // specify solver
00150   Solver.SetAztecOption(AZ_solver,AZ_cg);
00151   Solver.SetAztecOption(AZ_output,32);
00152 
00153   // HERE WE SET THE IFPACK PRECONDITIONER
00154   Solver.SetPrecOperator(&Prec);
00155 
00156   // .. and here we solve
00157   // NOTE: with one process, the solver must converge in
00158   // one iteration.
00159   Solver.Iterate(1550,1e-5);
00160 
00161 #ifdef HAVE_MPI
00162   MPI_Finalize() ; 
00163 #endif
00164 
00165   return(EXIT_SUCCESS);
00166 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 10:05:34 2011 for Ifpack Package Browser (Single Doxygen Collection) by  doxygen 1.6.3