Belos Package Browser (Single Doxygen Collection) Development
Loading...
Searching...
No Matches
test_tfqmr_complex_hb.cpp
Go to the documentation of this file.
1//@HEADER
2// ************************************************************************
3//
4// Belos: Block Linear Solvers Package
5// Copyright 2004 Sandia Corporation
6//
7// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8// the U.S. Government retains certain rights in this software.
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// This driver reads a problem from a Harwell-Boeing (HB) file.
43// The right-hand-side from the HB file is used instead of random vectors.
44// The initial guesses are all set to zero.
45//
46// NOTE: No preconditioner is used in this case.
47//
48#include "BelosConfigDefs.hpp"
50#include "BelosTFQMRSolMgr.hpp"
51#include "Teuchos_CommandLineProcessor.hpp"
52#include "Teuchos_ParameterList.hpp"
53#include "Teuchos_StandardCatchMacros.hpp"
54
55#ifdef HAVE_MPI
56#include <mpi.h>
57#endif
58
59// I/O for Harwell-Boeing files
60#ifdef HAVE_BELOS_TRIUTILS
61#include "Trilinos_Util_iohb.h"
62#endif
63
64#include "MyMultiVec.hpp"
65#include "MyBetterOperator.hpp"
66#include "MyOperator.hpp"
67
68using namespace Teuchos;
69
70int main(int argc, char *argv[]) {
71 //
72 typedef std::complex<double> ST;
73 typedef ScalarTraits<ST> SCT;
74 typedef SCT::magnitudeType MT;
75 typedef Belos::MultiVec<ST> MV;
76 typedef Belos::Operator<ST> OP;
79 ST one = SCT::one();
80 ST zero = SCT::zero();
81
82 int info = 0;
83 bool norm_failure = false;
84
85 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
86 //
87 int MyPID = session.getRank();
88
89 using Teuchos::RCP;
90 using Teuchos::rcp;
91
92 bool success = false;
93 bool verbose = false;
94 try {
95 bool proc_verbose = false;
96 int frequency = -1; // how often residuals are printed by solver
97 int numrhs = 1;
98 std::string filename("mhd1280b.cua");
99 MT tol = 1.0e-5; // relative residual tolerance
100
101 CommandLineProcessor cmdp(false, true);
102 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
103 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
104 cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
105 cmdp.setOption("tol",&tol,"Relative residual tolerance used by TFQMR solver.");
106 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
107 if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) {
108 return EXIT_FAILURE;
109 }
110
111 proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */
112 if (proc_verbose) {
113 std::cout << Belos::Belos_Version() << std::endl << std::endl;
114 }
115 if (!verbose)
116 frequency = -1; // reset frequency if test is not verbose
117
118
119#ifndef HAVE_BELOS_TRIUTILS
120 std::cout << "This test requires Triutils. Please configure with --enable-triutils." << std::endl;
121 if (MyPID==0) {
122 std::cout << "End Result: TEST FAILED" << std::endl;
123 }
124 return EXIT_FAILURE;
125#endif
126
127 // Get the data from the HB file
128 int dim,dim2,nnz;
129 MT *dvals;
130 int *colptr,*rowind;
131 ST *cvals;
132 nnz = -1;
133 info = readHB_newmat_double(filename.c_str(),&dim,&dim2,&nnz,
134 &colptr,&rowind,&dvals);
135 if (info == 0 || nnz < 0) {
136 if (MyPID==0) {
137 std::cout << "Error reading '" << filename << "'" << std::endl;
138 std::cout << "End Result: TEST FAILED" << std::endl;
139 }
140 return EXIT_FAILURE;
141 }
142 // Convert interleaved doubles to std::complex values
143 cvals = new ST[nnz];
144 for (int ii=0; ii<nnz; ii++) {
145 cvals[ii] = ST(dvals[ii*2],dvals[ii*2+1]);
146 }
147 // Build the problem matrix
148 RCP< MyBetterOperator<ST> > A
149 = rcp( new MyBetterOperator<ST>(dim,colptr,nnz,rowind,cvals) );
150 //
151 // ********Other information used by block solver***********
152 // *****************(can be user specified)******************
153 //
154 int maxits = dim; // maximum number of iterations to run
155 //
156 ParameterList belosList;
157 belosList.set( "Maximum Iterations", maxits ); // Maximum number of iterations allowed
158 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
159 if (verbose) {
160 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
162 if (frequency > 0)
163 belosList.set( "Output Frequency", frequency );
164 }
165 else
166 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
167 //
168 // Construct the right-hand side and solution multivectors.
169 // NOTE: The right-hand side will be constructed such that the solution is
170 // a vectors of one.
171 //
172 RCP<MyMultiVec<ST> > soln = rcp( new MyMultiVec<ST>(dim,numrhs) );
173 RCP<MyMultiVec<ST> > rhs = rcp( new MyMultiVec<ST>(dim,numrhs) );
174 MVT::MvRandom( *soln );
175 OPT::Apply( *A, *soln, *rhs );
176 MVT::MvInit( *soln, zero );
177 //
178 // Construct an unpreconditioned linear problem instance.
179 //
180 RCP<Belos::LinearProblem<ST,MV,OP> > problem =
181 rcp( new Belos::LinearProblem<ST,MV,OP>( A, soln, rhs ) );
182 bool set = problem->setProblem();
183 if (set == false) {
184 if (proc_verbose)
185 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
186 return EXIT_FAILURE;
187 }
188 //
189 // *******************************************************************
190 // *************Start the TFQMR iteration***********************
191 // *******************************************************************
192 //
193 Belos::TFQMRSolMgr<ST,MV,OP> solver( problem, rcp(&belosList,false) );
194
195 //
196 // **********Print out information about problem*******************
197 //
198 if (proc_verbose) {
199 std::cout << std::endl << std::endl;
200 std::cout << "Dimension of matrix: " << dim << std::endl;
201 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
202 std::cout << "Max number of TFQMR iterations: " << maxits << std::endl;
203 std::cout << "Relative residual tolerance: " << tol << std::endl;
204 std::cout << std::endl;
205 }
206 //
207 // Perform solve
208 //
209 Belos::ReturnType ret = solver.solve();
210 //
211 // Compute actual residuals.
212 //
213 RCP<MyMultiVec<ST> > temp = rcp( new MyMultiVec<ST>(dim,numrhs) );
214 OPT::Apply( *A, *soln, *temp );
215 MVT::MvAddMv( one, *rhs, -one, *temp, *temp );
216 std::vector<MT> norm_num(numrhs), norm_denom(numrhs);
217 MVT::MvNorm( *temp, norm_num );
218 MVT::MvNorm( *rhs, norm_denom );
219 for (int i=0; i<numrhs; ++i) {
220 if (proc_verbose)
221 std::cout << "Relative residual "<<i<<" : " << norm_num[i] / norm_denom[i] << std::endl;
222 if ( norm_num[i] / norm_denom[i] > tol ) {
223 norm_failure = true;
224 }
225 }
226
227 // Clean up.
228 delete [] dvals;
229 delete [] colptr;
230 delete [] rowind;
231 delete [] cvals;
232
233 success = ret==Belos::Converged && !norm_failure;
234 if (success) {
235 if (proc_verbose)
236 std::cout << "End Result: TEST PASSED" << std::endl;
237 } else {
238 if (proc_verbose)
239 std::cout << "End Result: TEST FAILED" << std::endl;
240 }
241 }
242 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
243
244 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
245} // end test_tfqmr_complex_hb.cpp
Belos header file which uses auto-configuration information to include necessary C++ headers.
Class which describes the linear problem to be solved by the iterative solver.
The Belos::TFQMRSolMgr provides a solver manager for the TFQMR linear solver.
A linear system to solve, and its associated information.
Traits class which defines basic operations on multivectors.
Interface for multivectors used by Belos' linear solvers.
Class which defines basic traits for the operator type.
Alternative run-time polymorphic interface for operators.
The Belos::TFQMRSolMgr provides a powerful and fully-featured solver manager over the TFQMR linear so...
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
Simple example of a user's defined Belos::Operator class.
Simple example of a user's defined Belos::MultiVec class.
Definition: MyMultiVec.hpp:66
@ StatusTestDetails
Definition: BelosTypes.hpp:261
@ FinalSummary
Definition: BelosTypes.hpp:259
@ TimingDetails
Definition: BelosTypes.hpp:260
@ Warnings
Definition: BelosTypes.hpp:256
ReturnType
Whether the Belos solve converged for all linear systems.
Definition: BelosTypes.hpp:155
@ Converged
Definition: BelosTypes.hpp:156
std::string Belos_Version()
int main(int argc, char *argv[])