Ifpack Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
aztecoo_solve.cpp
Go to the documentation of this file.
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack: Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2002) 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? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 */
42 
43 #include "Teuchos_GlobalMPISession.hpp"
44 #include "Teuchos_CommandLineProcessor.hpp"
45 #include "Teuchos_ParameterList.hpp"
46 #include "Teuchos_ParameterXMLFileReader.hpp"
47 #include "Teuchos_RefCountPtr.hpp"
48 #include "Teuchos_Time.hpp"
49 #include "Teuchos_Comm.hpp"
50 
51 #ifdef HAVE_MPI
52 #include "Epetra_MpiComm.h"
53 #else
54 #include "Epetra_SerialComm.h"
55 #endif
56 
57 #include "Epetra_LinearProblem.h"
58 #include "Epetra_CrsMatrix.h"
59 
60 #include "AztecOO.h"
61 
62 #include "ParameterHelper.hpp"
63 
64 #include "build_problem.hpp"
65 #include "build_solver.hpp"
66 
67 void process_command_line(int argc, char*argv[], std::string& xml_file);
68 
69 int main(int argc, char*argv[])
70 {
71  Teuchos::GlobalMPISession mpisess(&argc,&argv,&std::cout);
72 
73  Teuchos::Time timer("total");
74  timer.start();
75 
76 #ifdef HAVE_MPI
77  Epetra_MpiComm Comm(MPI_COMM_WORLD);
78 #else
79  Epetra_SerialComm Comm;
80 #endif
81 
82  //Just get one parameter from the command-line: the name of an xml file
83  //to get parameters from.
84 
85  std::string xml_file("xml_params.xml");
86  process_command_line(argc, argv, xml_file);
87 
88  //Read the contents of the xml file into a ParameterList. That parameter list
89  //should specify a matrix-file and optionally which Belos solver to use, and
90  //which Tifpack preconditioner to use, etc. If there are sublists of parameters
91  //for Belos and Tifpack, those will be passed to the respective destinations
92  //from within the build_problem and build_solver functions.
93 
94  std::cout << "Every proc reading parameters from xml_file: "
95  << xml_file << std::endl;
96  Teuchos::ParameterList test_params =
98 
99  //The build_problem function is declared in build_problem.hpp.
100  //Note that build_problem calls build_precond and sets a preconditioner on the
101  //linear-problem, if a preconditioner is specified.
102 
103  Teuchos::RCP<Epetra_LinearProblem> problem = build_problem(test_params, Comm);
104 
105  //The build_solver function is declared in build_solver.hpp:
106 
107  Teuchos::RCP<AztecOO> solver = build_solver(test_params, problem);
108 
109  int max_iters = solver->GetAllAztecOptions()[AZ_max_iter];
110  double tol = solver->GetAllAztecParams()[AZ_tol];
111 
112  Teuchos::Time prec_time("precond");
113  prec_time.start();
114  double cond_est = 0;
115  int err_code = -1;
116  if (solver->GetAllAztecOptions()[AZ_precond] != AZ_user_precond) {
117  err_code = solver->ConstructPreconditioner(cond_est);
118  }
119  prec_time.stop();
120  if (Comm.MyPID() == 0 && err_code == 0) {
121  std::cout << "Time to compute preconditioner: " << prec_time.totalElapsedTime() << "s"<<std::endl;
122  }
123 
124  int ret = solver->Iterate(max_iters, tol);
125 
126  if (err_code == 0) {
127  solver->DestroyPreconditioner();
128  }
129 
130  int actual_iters = (int)solver->GetAztecStatus()[AZ_its];
131 
132  if (Comm.MyPID() == 0) {
133  std::cout << "Converged in " << actual_iters << " iterations." << std::endl;
134  }
135 
136  if (problem->GetLHS()->NumVectors() > 1) {
137  throw std::runtime_error("ERROR: MultiVector->NumVectors()>1.");
138  }
139 
140  Epetra_MultiVector* x = problem->GetLHS();
141  Epetra_MultiVector* b = problem->GetRHS();
142  Epetra_Operator* A = problem->GetOperator();
144  A->Apply(*x, *r);
145  r->Update(1.0, *b, -1.0);
146  double norm = 0;
147  r->Norm2(&norm);
148 
149  if (Comm.MyPID() == 0) {
150  std::cout << "2-Norm of residual vec: " << norm << std::endl;
151  }
152 
153  //Clean up by destroying heap-allocated objects that are not
154  //held by Teuchos::RCP:
155  delete A;
156  delete x;
157  delete b;
158  Epetra_Operator* prec = solver->GetPrecOperator();
159  delete prec;
160 
161  //If the xml file specified a number of iterations to expect, then we will
162  //use that as a test pass/fail criteria.
163 
164  if (test_params.isParameter("expectNumIters")) {
165  int expected_iters = 0;
166  helper::GetParameter(test_params, "expectNumIters", expected_iters);
167  double eps = 2.e-6;
168  if (ret == 0 && actual_iters <= expected_iters && norm < eps) {
169  if (Comm.MyPID() == 0) {
170  std::cout << "End Result: TEST PASSED" << std::endl;
171  }
172  }
173  else {
174  if (Comm.MyPID() == 0) {
175  std::cout << "Actual iters("<<actual_iters
176  <<") > expected number of iterations ("
177  <<expected_iters<<"), or resid-norm(" << norm << ") >= "<<eps <<std::endl;
178  }
179  }
180  }
181 
182  timer.stop();
183  if (Comm.MyPID() == 0) {
184  std::cout << "proc 0 total program time: " << timer.totalElapsedTime() << std::endl;
185  }
186 
187  return 0;
188 }
189 
190 void process_command_line(int argc, char*argv[], std::string& xml_file)
191 {
192  Teuchos::CommandLineProcessor cmdp(false,true);
193  cmdp.setOption("xml_file", &xml_file, "XML Parameters file");
195  throw std::runtime_error("Error parsing command-line.");
196  }
197 }
198 
Epetra_MultiVector * GetLHS() const
Epetra_MultiVector * GetRHS() const
int MyPID() const
ParameterList getParameters() const
bool isParameter(const std::string &name) const
virtual int Apply(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const =0
void start(bool reset=false)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
const double tol
double stop()
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
int main(int argc, char *argv[])
void GetParameter(const Teuchos::ParameterList &params, const std::string &name, T &value)
Set a value from a ParameterList if a parameter with the specified name exists.
double totalElapsedTime(bool readCurrentTime=false) const
void process_command_line(int argc, char *argv[], std::string &xml_file)
Epetra_Operator * GetOperator() const
Teuchos::RCP< Epetra_LinearProblem > build_problem(Teuchos::ParameterList &test_params, const Epetra_Comm &comm)
Teuchos::RCP< AztecOO > build_solver(Teuchos::ParameterList &test_params, Teuchos::RCP< Epetra_LinearProblem > problem)