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