Stratimikos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test_single_belos_thyra_solver.cpp
Go to the documentation of this file.
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Stratimikos: Thyra-based strategies for linear solvers
6 // Copyright (2006) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
45 
46 #ifndef SUN_CXX
47 
48 #include "Thyra_BelosLinearOpWithSolveFactory.hpp"
49 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
50 #include "Thyra_EpetraLinearOp.hpp"
51 #include "Thyra_LinearOpTester.hpp"
52 #include "Thyra_LinearOpWithSolveBase.hpp"
53 #include "Thyra_LinearOpWithSolveTester.hpp"
54 #include "Thyra_MultiVectorStdOps.hpp"
55 #include "Thyra_VectorStdOps.hpp"
56 #include "EpetraExt_readEpetraLinearSystem.h"
57 #include "Epetra_SerialComm.h"
59 #ifdef HAVE_BELOS_IFPACK
61 #endif
62 
63 #endif // SUN_CXX
64 
65 bool Thyra::test_single_belos_thyra_solver(
66  const std::string matrixFile
67  ,const bool testTranspose
68  ,const bool usePreconditioner
69  ,const int numRhs
70  ,const int numRandomVectors
71  ,const double maxFwdError
72  ,const double maxResid
73  ,const double maxSolutionError
74  ,const bool showAllTests
75  ,const bool dumpAll
76  ,Teuchos::ParameterList *belosLOWSFPL
77  ,Teuchos::ParameterList *precPL
78  ,Teuchos::FancyOStream *out_arg
79  )
80 {
81  using Teuchos::rcp;
82  using Teuchos::OSTab;
83  bool result, success = true;
84 
86 
87  try {
88 
89 #ifndef SUN_CXX
90 
91  if(out.get()) {
92  *out << "\n***"
93  << "\n*** Testing Thyra::BelosLinearOpWithSolveFactory (and Thyra::BelosLinearOpWithSolve)"
94  << "\n***\n"
95  << "\nEchoing input options:"
96  << "\n matrixFile = " << matrixFile
97  << "\n testTranspose = " << testTranspose
98  << "\n usePreconditioner = " << usePreconditioner
99  << "\n numRhs = " << numRhs
100  << "\n numRandomVectors = " << numRandomVectors
101  << "\n maxFwdError = " << maxFwdError
102  << "\n maxResid = " << maxResid
103  << "\n showAllTests = " << showAllTests
104  << "\n dumpAll = " << dumpAll
105  << std::endl;
106  }
107 
108  if(out.get()) *out << "\nA) Reading in an epetra matrix A from the file \'"<<matrixFile<<"\' ...\n";
109 
110  Epetra_SerialComm comm;
112  EpetraExt::readEpetraLinearSystem( matrixFile, comm, &epetra_A );
113 
114  Teuchos::RCP<const LinearOpBase<double> > A = epetraLinearOp(epetra_A);
115 
116  if(out.get() && dumpAll) *out << "\ndescribe(A) =\n" << describe(*A,Teuchos::VERB_EXTREME);
117 
118  if(out.get()) *out << "\nB) Creating a BelosLinearOpWithSolveFactory object opFactory ...\n";
119 
121  lowsFactory;
122  {
124  belosLowsFactory = Teuchos::rcp(new BelosLinearOpWithSolveFactory<double>());
125  lowsFactory = belosLowsFactory;
126  }
127 
128  if(out.get()) {
129  *out << "\nlowsFactory.getValidParameters() before setting preconditioner factory:\n";
130  lowsFactory->getValidParameters()->print(OSTab(out).o(),0,true,false);
131  }
132 
133  if(usePreconditioner) {
134 #ifdef HAVE_BELOS_IFPACK
135  if(out.get()) {
136  *out << "\nSetting an Ifpack preconditioner factory ...\n";
137  }
138  RCP<PreconditionerFactoryBase<double> >
139  precFactory = Teuchos::rcp(new IfpackPreconditionerFactory());
140  if (precPL)
141  precFactory->setParameterList(rcp(precPL,false));
142  lowsFactory->setPreconditionerFactory(precFactory,"Ifpack");
143 #else
144  TEUCHOS_TEST_FOR_EXCEPT(usePreconditioner);
145 #endif
146  }
147 
148  if(out.get()) {
149  *out << "\nlowsFactory.getValidParameters() after setting preconditioner factory:\n";
150  lowsFactory->getValidParameters()->print(OSTab(out).o(),0,true,false);
151  *out << "\nbelosLOWSFPL before setting parameters:\n";
152  belosLOWSFPL->print(OSTab(out).o(),0,true);
153  }
154 
155  lowsFactory->setParameterList(Teuchos::rcp(belosLOWSFPL,false));
156 
157  if(out.get()) {
158  *out << "\nbelosLOWSFPL after setting parameters:\n";
159  belosLOWSFPL->print(OSTab(out).o(),0,true);
160  }
161 
162  if(out.get()) *out << "\nC) Creating a BelosLinearOpWithSolve object nsA from A ...\n";
163 
164  Teuchos::RCP<LinearOpWithSolveBase<double> > nsA = lowsFactory->createOp();
165  Thyra::initializeOp<double>(*lowsFactory, A, nsA.ptr());
166 
167  if(out.get()) *out << "\nD) Testing the LinearOpBase interface of nsA ...\n";
168 
169  LinearOpTester<double> linearOpTester;
170  linearOpTester.check_adjoint(testTranspose);
171  linearOpTester.num_rhs(numRhs);
172  linearOpTester.num_random_vectors(numRandomVectors);
173  linearOpTester.set_all_error_tol(maxFwdError);
174  linearOpTester.set_all_warning_tol(1e-2*maxFwdError);
175  linearOpTester.show_all_tests(showAllTests);
176  linearOpTester.dump_all(dumpAll);
177  Thyra::seed_randomize<double>(0);
178  result = linearOpTester.check(*nsA,Teuchos::Ptr<Teuchos::FancyOStream>(out.get()));
179  if(!result) success = false;
180 
181  if(out.get()) *out << "\nE) Testing the LinearOpWithSolveBase interface of nsA ...\n";
182 
183  LinearOpWithSolveTester<double> linearOpWithSolveTester;
184  linearOpWithSolveTester.num_rhs(numRhs);
185  linearOpWithSolveTester.turn_off_all_tests();
186  linearOpWithSolveTester.check_forward_default(true);
187  linearOpWithSolveTester.check_forward_residual(true);
188  if(testTranspose) {
189  linearOpWithSolveTester.check_adjoint_default(true);
190  linearOpWithSolveTester.check_adjoint_residual(true);
191  }
192  else {
193  linearOpWithSolveTester.check_adjoint_default(false);
194  linearOpWithSolveTester.check_adjoint_residual(false);
195  }
196  linearOpWithSolveTester.set_all_solve_tol(maxResid);
197  linearOpWithSolveTester.set_all_slack_error_tol(maxResid);
198  linearOpWithSolveTester.set_all_slack_warning_tol(1e+1*maxResid);
199  linearOpWithSolveTester.forward_default_residual_error_tol(2*maxResid);
200  linearOpWithSolveTester.forward_default_solution_error_error_tol(maxSolutionError);
201  linearOpWithSolveTester.adjoint_default_residual_error_tol(2*maxResid);
202  linearOpWithSolveTester.adjoint_default_solution_error_error_tol(maxSolutionError);
203  linearOpWithSolveTester.show_all_tests(showAllTests);
204  linearOpWithSolveTester.dump_all(dumpAll);
205  Thyra::seed_randomize<double>(0);
206  result = linearOpWithSolveTester.check(*nsA,out.get());
207  if(!result) success = false;
208 
209  if(out.get()) {
210  *out << "\nbelosLOWSFPL after solving:\n";
211  belosLOWSFPL->print(OSTab(out).o(),0,true);
212  }
213 
214 #else // SUN_CXX
215 
216  if(out.get()) *out << "\nTest failed since is was not even compiled since SUN_CXX was defined!\n";
217  success = false;
218 
219 #endif // SUN_CXX
220 
221  }
222  catch( const std::exception &excpt ) {
223  if(out.get()) *out << std::flush;
224  std::cerr << "*** Caught standard exception : " << excpt.what() << std::endl;
225  success = false;
226  }
227 
228  return success;
229 
230 }
T * get() const
basic_OSTab< char > OSTab
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Ptr< T > ptr() const
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)