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_amesos2_tpetra_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 #include "Thyra_Amesos2LinearOpWithSolveFactory.hpp"
47 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
49 #include "Thyra_TpetraLinearOp.hpp"
50 #include "Thyra_LinearOpTester.hpp"
51 #include "Thyra_LinearOpWithSolveBase.hpp"
52 #include "Thyra_LinearOpWithSolveTester.hpp"
53 #include "Thyra_MultiVectorStdOps.hpp"
54 #include "Thyra_VectorStdOps.hpp"
55 #include "MatrixMarket_Tpetra.hpp"
57 
58 bool Thyra::test_single_amesos2_tpetra_solver(
59  const std::string matrixFile
60  ,const int numRhs
61  ,const int numRandomVectors
62  ,const double maxFwdError
63  ,const double maxResid
64  ,const double maxSolutionError
65  ,const bool showAllTests
66  ,const bool dumpAll
67  ,Teuchos::ParameterList *amesos2LOWSFPL
68  ,Teuchos::FancyOStream *out_arg
69  ,const Teuchos::RCP<const Teuchos::Comm<int> >& comm
70  )
71 {
72  using Teuchos::rcp;
73  using Teuchos::OSTab;
74  bool result, success = true;
75 
77 
78  try {
79 
80  if(out.get()) {
81  *out << "\n***"
82  << "\n*** Testing Thyra::BelosLinearOpWithSolveFactory (and Thyra::BelosLinearOpWithSolve)"
83  << "\n***\n"
84  << "\nEchoing input options:"
85  << "\n matrixFile = " << matrixFile
86  << "\n numRhs = " << numRhs
87  << "\n numRandomVectors = " << numRandomVectors
88  << "\n maxFwdError = " << maxFwdError
89  << "\n maxResid = " << maxResid
90  << "\n showAllTests = " << showAllTests
91  << "\n dumpAll = " << dumpAll
92  << std::endl;
93  }
94 
95  if(out.get()) *out << "\nA) Reading in a tpetra matrix A from the file \'"<<matrixFile<<"\' ...\n";
96 
97  using Scalar = double;
99  using MAT = typename LOWS::MAT;
101  auto A_tpetra = Tpetra::MatrixMarket::Reader<MAT>::readSparseFile(matrixFile, comm);
102  auto domain_thyra = Thyra::tpetraVectorSpace<Scalar>(A_tpetra->getDomainMap());
103  auto range_thyra = Thyra::tpetraVectorSpace<Scalar>(A_tpetra->getRangeMap());
104  Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar>> domain_thyra_const = domain_thyra;
105  Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar>> range_thyra_const = range_thyra;
106  Teuchos::RCP<const Tpetra::Operator<Scalar>> A_tpetra_const = A_tpetra;
107 
108  auto A_thyra = Thyra::constTpetraLinearOp(range_thyra_const, domain_thyra_const, A_tpetra_const);
109 
110  if(out.get() && dumpAll) *out << "\ndescribe(A) =\n" << describe(*A_thyra,Teuchos::VERB_EXTREME);
111 
112  if(out.get()) *out << "\nB) Creating an Amesos2LinearOpWithSolveFactory object opFactory ...\n";
113 
115  lowsFactory;
116  {
118  amesos2LowsFactory = Teuchos::rcp(new LOWSF());
119  lowsFactory = amesos2LowsFactory;
120  }
121 
122  if(out.get()) {
123  *out << "\nlowsFactory.getValidParameters():\n";
124  lowsFactory->getValidParameters()->print(OSTab(out).o(),0,true,false);
125  *out << "\namesos2LOWSFPL before setting parameters:\n";
126  amesos2LOWSFPL->print(OSTab(out).o(),0,true);
127  }
128 
129  lowsFactory->setParameterList(Teuchos::rcp(amesos2LOWSFPL,false));
130 
131  if(out.get()) {
132  *out << "\namesos2LOWSFPL after setting parameters:\n";
133  amesos2LOWSFPL->print(OSTab(out).o(),0,true);
134  }
135 
136  if(out.get()) *out << "\nC) Creating a Amesos2LinearOpWithSolve object nsA from A ...\n";
137 
138  Teuchos::RCP<LinearOpWithSolveBase<Scalar> > nsA = lowsFactory->createOp();
139  Thyra::initializeOp<Scalar>(*lowsFactory, A_thyra, nsA.ptr());
140 
141  if(out.get()) *out << "\nD) Testing the LinearOpBase interface of nsA ...\n";
142 
143  LinearOpTester<Scalar> linearOpTester;
144  linearOpTester.check_adjoint(false);
145  linearOpTester.num_rhs(numRhs);
146  linearOpTester.num_random_vectors(numRandomVectors);
147  linearOpTester.set_all_error_tol(maxFwdError);
148  linearOpTester.set_all_warning_tol(1e-2*maxFwdError);
149  linearOpTester.show_all_tests(showAllTests);
150  linearOpTester.dump_all(dumpAll);
151  Thyra::seed_randomize<Scalar>(0);
152  result = linearOpTester.check(*nsA,Teuchos::Ptr<Teuchos::FancyOStream>(out.get()));
153  if(!result) success = false;
154 
155  if(out.get()) *out << "\nE) Testing the LinearOpWithSolveBase interface of nsA ...\n";
156 
157  LinearOpWithSolveTester<Scalar> linearOpWithSolveTester;
158  linearOpWithSolveTester.num_rhs(numRhs);
159  linearOpWithSolveTester.turn_off_all_tests();
160  linearOpWithSolveTester.check_forward_default(true);
161  linearOpWithSolveTester.check_forward_residual(true);
162  linearOpWithSolveTester.check_adjoint_default(false);
163  linearOpWithSolveTester.check_adjoint_residual(false);
164  linearOpWithSolveTester.set_all_solve_tol(maxResid);
165  linearOpWithSolveTester.set_all_slack_error_tol(maxResid);
166  linearOpWithSolveTester.set_all_slack_warning_tol(1e+1*maxResid);
167  linearOpWithSolveTester.forward_default_residual_error_tol(2*maxResid);
168  linearOpWithSolveTester.forward_default_solution_error_error_tol(maxSolutionError);
169  linearOpWithSolveTester.adjoint_default_residual_error_tol(2*maxResid);
170  linearOpWithSolveTester.adjoint_default_solution_error_error_tol(maxSolutionError);
171  linearOpWithSolveTester.show_all_tests(showAllTests);
172  linearOpWithSolveTester.dump_all(dumpAll);
173  Thyra::seed_randomize<Scalar>(0);
174  result = linearOpWithSolveTester.check(*nsA,out.get());
175  if(!result) success = false;
176 
177  if(out.get()) {
178  *out << "\namesos2LOWSFPL after solving:\n";
179  amesos2LOWSFPL->print(OSTab(out).o(),0,true);
180  }
181 
182  }
183  catch( const std::exception &excpt ) {
184  if(out.get()) *out << std::flush;
185  std::cerr << "*** Caught standard exception : " << excpt.what() << std::endl;
186  success = false;
187  }
188 
189  return success;
190 
191 }
Concrete LinearOpWithSolveBase subclass in terms of Amesos2.
T * get() const
basic_OSTab< char > OSTab
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Ptr< T > ptr() const