Stratimikos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
belos_epetra_thyra_lowsf_hb.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stratimikos: Thyra-based strategies for linear solvers
4 //
5 // Copyright 2006 NTESS and the Stratimikos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 // This driver reads a problem from a Harwell-Boeing (HB) file into an
11 // Epetra_CrsMatrix. This matrix is then converted into a Thyra linear operator
12 // through the Thyra-Epetra adapters.
13 // The right-hand-side from the HB file is used instead of random vectors.
14 // The initial guesses are all set to zero.
15 //
16 //
17 
18 // Thyra includes
19 #include "Thyra_BelosLinearOpWithSolveFactory.hpp"
20 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
21 #include "Thyra_EpetraLinearOp.hpp"
22 
23 // Epetra includes
24 #include "Epetra_SerialComm.h"
25 #include "EpetraExt_readEpetraLinearSystem.h"
26 
27 // Ifpack includes
28 #ifdef HAVE_BELOS_IFPACK
30 #endif
31 
32 // Teuchos includes
35 
36 int main(int argc, char* argv[])
37 {
38  //
39  // Get a default output stream from the Teuchos::VerboseObjectBase
40  //
42  out = Teuchos::VerboseObjectBase::getDefaultOStream();
43  //
44  // Set the parameters for the Belos LOWS Factory and create a parameter list.
45  //
46  int blockSize = 2;
47  int maxIterations = 400;
48  int maxRestarts = 25;
49  int gmresKrylovLength = 25;
50  int outputFrequency = 1;
51  bool outputMaxResOnly = true;
52  double maxResid = 1e-6;
53 
55  belosLOWSFPL = Teuchos::rcp( new Teuchos::ParameterList() );
56 
57  belosLOWSFPL->set("Solver Type","Block GMRES");
58 
59  Teuchos::ParameterList& belosLOWSFPL_solver =
60  belosLOWSFPL->sublist("Solver Types");
61 
62  Teuchos::ParameterList& belosLOWSFPL_gmres =
63  belosLOWSFPL_solver.sublist("Block GMRES");
64 
65  belosLOWSFPL_gmres.set("Maximum Iterations",int(maxIterations));
66  belosLOWSFPL_gmres.set("Convergence Tolerance",double(maxResid));
67  belosLOWSFPL_gmres.set("Maximum Restarts",int(maxRestarts));
68  belosLOWSFPL_gmres.set("Block Size",int(blockSize));
69  belosLOWSFPL_gmres.set("Num Blocks",int(gmresKrylovLength));
70  belosLOWSFPL_gmres.set("Output Frequency",int(outputFrequency));
71  belosLOWSFPL_gmres.set("Show Maximum Residual Norm Only",bool(outputMaxResOnly));
72 
73 #ifdef HAVE_BELOS_IFPACK
74  //
75  // Set the parameters for the Ifpack Preconditioner Factory and create parameter list
76  //
77  Teuchos::ParameterList &ifpackPFSL = belosLOWSFPL->sublist("IfpackPreconditionerFactory");
78 
79  ifpackPFSL.set("Overlap",int(2));
80  ifpackPFSL.set("Prec Type","ILUT");
81 #endif
82 
83  // Whether the linear solver succeeded.
84  // (this will be set during the residual check at the end)
85  bool success = true;
86 
87  // Number of random right-hand sides we will be solving for.
88  int numRhs = 5;
89 
90  // Name of input matrix file
91  std::string matrixFile = "orsirr1.hb";
92 
93  // Read in the matrix file (can be *.mtx, *.hb, etc.)
94  Epetra_SerialComm comm;
96  EpetraExt::readEpetraLinearSystem( matrixFile, comm, &epetra_A );
97 
98  // Create a Thyra linear operator (A) using the Epetra_CrsMatrix (epetra_A).
100  A = Thyra::epetraLinearOp(epetra_A);
101 
102  // Get the domain space for the Thyra linear operator
104 
105  // Create the Belos LOWS factory.
108 
109 #ifdef HAVE_BELOS_IFPACK
110 
111  // Set the preconditioner factory for the LOWS factory.
112  belosLOWSFactory->setPreconditionerFactory(
114  ,"IfpackPreconditionerFactory"
115  );
116 #endif
117 
118  // Set the parameter list to specify the behavior of the factory.
119  belosLOWSFactory->setParameterList( belosLOWSFPL );
120 
121  // Set the output stream and the verbosity level (prints to std::cout by defualt)
122  belosLOWSFactory->setVerbLevel(Teuchos::VERB_LOW);
123 
124  // Create a BelosLinearOpWithSolve object from the Belos LOWS factory.
126  nsA = belosLOWSFactory->createOp();
127 
128  // Initialize the BelosLinearOpWithSolve object with the Thyra linear operator.
129  Thyra::initializeOp<double>( *belosLOWSFactory, A, &*nsA );
130 
131  // Create a right-hand side with numRhs vectors in it and randomize it.
133  b = Thyra::createMembers(domain, numRhs);
134  Thyra::seed_randomize<double>(0);
135  Thyra::randomize(-1.0, 1.0, &*b);
136 
137  // Create an initial std::vector with numRhs vectors in it and initialize it to zero.
139  x = Thyra::createMembers(domain, numRhs);
140  Thyra::assign(&*x, 0.0);
141 
142  // Perform solve using the linear operator to get the approximate solution of Ax=b,
143  // where b is the right-hand side and x is the left-hand side.
144  Thyra::SolveStatus<double> solveStatus;
145  solveStatus = Thyra::solve( *nsA, Thyra::NONCONJ_ELE, *b, &*x );
146 
147  // Print out status of solve.
148  *out << "\nBelos LOWS Status: "<< solveStatus << std::endl;
149 
150  //
151  // Compute residual and double check convergence.
152  //
153  std::vector<double> norm_b(numRhs), norm_res(numRhs);
155  y = Thyra::createMembers(domain, numRhs);
156 
157  // Compute the column norms of the right-hand side b.
158  Thyra::norms_2( *b, &norm_b[0] );
159 
160  // Compute y=A*x, where x is the solution from the linear solver.
161  A->apply( Thyra::NONCONJ_ELE, *x, &*y );
162 
163  // Compute A*x-b = y-b
164  Thyra::update( -1.0, *b, &*y );
165 
166  // Compute the column norms of A*x-b.
167  Thyra::norms_2( *y, &norm_res[0] );
168 
169  // Print out the final relative residual norms.
170  double rel_res = 0.0;
171  *out << "Final relative residual norms" << std::endl;
172  for (int i=0; i<numRhs; ++i) {
173  rel_res = norm_res[i]/norm_b[i];
174  if (rel_res > maxResid)
175  success = false;
176  *out << "RHS " << i+1 << " : "
177  << std::setw(16) << std::right << rel_res << std::endl;
178  }
179 
180  return ( success ? 0 : 1 );
181 }
Concrete preconditioner factory subclass based on Ifpack.
int main(int argc, char *argv[])
RCP< ParameterList > sublist(const RCP< ParameterList > &paramList, const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
LinearOpWithSolveFactoryBase subclass implemented in terms of Belos.