Galeri Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LinearProblem.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Galeri: Finite Element and Matrix Generation Package
5 // Copyright (2006) ETHZ/Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions about Galeri? Contact Marzio Sala (marzio.sala _AT_ gmail.com)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #include "Galeri_Maps.h"
43 #include "Galeri_CrsMatrices.h"
44 #include "Galeri_Utils.h"
45 #ifdef HAVE_MPI
46 #include "Epetra_MpiComm.h"
47 #include "mpi.h"
48 #else
49 #include "Epetra_SerialComm.h"
50 #endif
51 #include "Epetra_Map.h"
52 #include "Epetra_Vector.h"
53 #include "Epetra_CrsMatrix.h"
54 #include "Epetra_LinearProblem.h"
56 
57 using namespace Galeri;
58 
59 // =========== //
60 // main driver //
61 // =========== //
62 
63 int main(int argc, char* argv[])
64 {
65 #ifdef HAVE_MPI
66  MPI_Init(&argc, &argv);
67  Epetra_MpiComm Comm(MPI_COMM_WORLD);
68 #else
69  Epetra_SerialComm Comm;
70 #endif
71 
72  // Here we create the linear problem
73  //
74  // Matrix * LHS = RHS
75  //
76  // with Matrix arising from a 5-point formula discretization.
77 
78  Epetra_Map* Map = 0;
79  Epetra_RowMatrix* Matrix = 0;
80 
81  Teuchos::ParameterList GaleriList;
82  // dimension of the problem is nx x ny
83  GaleriList.set("nx", 10 * Comm.NumProc());
84  GaleriList.set("ny", 10);
85  // total number of processors is mx x my
86  GaleriList.set("mx", Comm.NumProc());
87  GaleriList.set("my", 1);
88 
89  try
90  {
91 #ifndef GALERI_TEST_USE_LONGLONG_GO
92  Map = CreateMap("Cartesian2D", Comm, GaleriList);
93 #else
94  Map = CreateMap64("Cartesian2D", Comm, GaleriList);
95 #endif
96  Matrix = CreateCrsMatrix("Laplace2D", Map, GaleriList);
97  Epetra_Vector ExactSolution(*Map); ExactSolution.Random();
98  Epetra_Vector LHS(*Map); LHS.PutScalar(0.0);
99  Epetra_Vector RHS(*Map);
100 
101  Matrix->Multiply(false, ExactSolution, RHS);
102 
103  Epetra_LinearProblem Problem(Matrix, &LHS, &RHS);
104 
105  // at this point any object that understand Epetra_LinearProblem can be
106  // used, for example AztecOO, Amesos. IFPACK and ML can be used to define a
107  // preconditioner for Matrix. Here we use a simple solver, based on
108  // LAPACK, that is meant for simple testing only.
109 
110  Solve(Problem);
111 
112  // and we compute the norm of the true residual.
113  double ResidualNorm = ComputeNorm(Matrix, &LHS, &RHS);
114 
115  if (Comm.MyPID() == 0)
116  cout << ResidualNorm << endl;
117 
118  delete Map;
119  delete Matrix;
120  }
121  catch (Galeri::Exception& rhs)
122  {
123  if (Comm.MyPID() == 0)
124  {
125  cerr << "Caught exception: ";
126  rhs.Print();
127  }
128  }
129 
130 #ifdef HAVE_MPI
131  MPI_Finalize();
132 #endif
133 
134  return(EXIT_SUCCESS);
135 }
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
int MyPID() const
int NumProc() const
virtual int Multiply(bool TransA, const Epetra_MultiVector &X, Epetra_MultiVector &Y) const =0
int main(int argc, char *argv[])
Definition: CrsMatrix.cpp:61
int Solve(int, TYPE *, TYPE *, TYPE *)