Debian Codegraph @

packages/stratimikos/adapters/amesos2/test/test_single_amesos2_tpetra_solver.cpp

raw
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 #include "test_single_amesos2_tpetra_solver.hpp"
11
12 #include "Thyra_Amesos2LinearOpWithSolveFactory.hpp"
13 #include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
14 #include "Thyra_TpetraVectorSpace.hpp"
15 #include "Thyra_TpetraLinearOp.hpp"
16 #include "Thyra_LinearOpTester.hpp"
17 #include "Thyra_LinearOpWithSolveBase.hpp"
18 #include "Thyra_LinearOpWithSolveTester.hpp"
19 #include "Thyra_MultiVectorStdOps.hpp"
20 #include "Thyra_VectorStdOps.hpp"
21 #include "MatrixMarket_Tpetra.hpp"
22 #include "Teuchos_ParameterList.hpp"
23
24 bool Thyra::test_single_amesos2_tpetra_solver(
25 const std::string matrixFile
26 ,const int numRhs
27 ,const int numRandomVectors
28 ,const double maxFwdError
29 ,const double maxResid
30 ,const double maxSolutionError
31 ,const bool showAllTests
32 ,const bool dumpAll
33 ,Teuchos::ParameterList *amesos2LOWSFPL
34 ,Teuchos::FancyOStream *out_arg
35 ,const Teuchos::RCP<const Teuchos::Comm<int> >& comm
36 )
37 {
38 using Teuchos::rcp;
39 using Teuchos::OSTab;
40 bool result, success = true;
41
42 Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::rcp(out_arg,false);
43
44 try {
45
46 if(out.get()) {
47 *out << "\n***"
48 << "\n*** Testing Thyra::BelosLinearOpWithSolveFactory (and Thyra::BelosLinearOpWithSolve)"
49 << "\n***\n"
50 << "\nEchoing input options:"
51 << "\n matrixFile = " << matrixFile
52 << "\n numRhs = " << numRhs
53 << "\n numRandomVectors = " << numRandomVectors
54 << "\n maxFwdError = " << maxFwdError
55 << "\n maxResid = " << maxResid
56 << "\n showAllTests = " << showAllTests
57 << "\n dumpAll = " << dumpAll
58 << std::endl;
59 }
60
61 if(out.get()) *out << "\nA) Reading in a tpetra matrix A from the file \'"<<matrixFile<<"\' ...\n";
62
63 using Scalar = double;
64 using LOWS = Thyra::Amesos2LinearOpWithSolve<Scalar>;
65 using MAT = typename LOWS::MAT;
66 using LOWSF = Thyra::Amesos2LinearOpWithSolveFactory<Scalar>;
67 auto A_tpetra = Tpetra::MatrixMarket::Reader<MAT>::readSparseFile(matrixFile, comm);
68 auto domain_thyra = Thyra::tpetraVectorSpace<Scalar>(A_tpetra->getDomainMap());
69 auto range_thyra = Thyra::tpetraVectorSpace<Scalar>(A_tpetra->getRangeMap());
70 Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar>> domain_thyra_const = domain_thyra;
71 Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar>> range_thyra_const = range_thyra;
72 Teuchos::RCP<const Tpetra::Operator<Scalar>> A_tpetra_const = A_tpetra;
73
74 auto A_thyra = Thyra::constTpetraLinearOp(range_thyra_const, domain_thyra_const, A_tpetra_const);
75
76 if(out.get() && dumpAll) *out << "\ndescribe(A) =\n" << describe(*A_thyra,Teuchos::VERB_EXTREME);
77
78 if(out.get()) *out << "\nB) Creating an Amesos2LinearOpWithSolveFactory object opFactory ...\n";
79
80 Teuchos::RCP<LinearOpWithSolveFactoryBase<Scalar> >
81 lowsFactory;
82 {
83 Teuchos::RCP<LOWSF>
84 amesos2LowsFactory = Teuchos::rcp(new LOWSF());
85 lowsFactory = amesos2LowsFactory;
86 }
87
88 if(out.get()) {
89 *out << "\nlowsFactory.getValidParameters():\n";
90 lowsFactory->getValidParameters()->print(OSTab(out).o(),0,true,false);
91 *out << "\namesos2LOWSFPL before setting parameters:\n";
92 amesos2LOWSFPL->print(OSTab(out).o(),0,true);
93 }
94
95 lowsFactory->setParameterList(Teuchos::rcp(amesos2LOWSFPL,false));
96
97 if(out.get()) {
98 *out << "\namesos2LOWSFPL after setting parameters:\n";
99 amesos2LOWSFPL->print(OSTab(out).o(),0,true);
100 }
101
102 if(out.get()) *out << "\nC) Creating a Amesos2LinearOpWithSolve object nsA from A ...\n";
103
104 Teuchos::RCP<LinearOpWithSolveBase<Scalar> > nsA = lowsFactory->createOp();
105 Thyra::initializeOp<Scalar>(*lowsFactory, A_thyra, nsA.ptr());
106
107 if(out.get()) *out << "\nD) Testing the LinearOpBase interface of nsA ...\n";
108
109 LinearOpTester<Scalar> linearOpTester;
110 linearOpTester.check_adjoint(false);
111 linearOpTester.num_rhs(numRhs);
112 linearOpTester.num_random_vectors(numRandomVectors);
113 linearOpTester.set_all_error_tol(maxFwdError);
114 linearOpTester.set_all_warning_tol(1e-2*maxFwdError);
115 linearOpTester.show_all_tests(showAllTests);
116 linearOpTester.dump_all(dumpAll);
117 Thyra::seed_randomize<Scalar>(0);
118 result = linearOpTester.check(*nsA,Teuchos::Ptr<Teuchos::FancyOStream>(out.get()));
119 if(!result) success = false;
120
121 if(out.get()) *out << "\nE) Testing the LinearOpWithSolveBase interface of nsA ...\n";
122
123 LinearOpWithSolveTester<Scalar> linearOpWithSolveTester;
124 linearOpWithSolveTester.num_rhs(numRhs);
125 linearOpWithSolveTester.turn_off_all_tests();
126 linearOpWithSolveTester.check_forward_default(true);
127 linearOpWithSolveTester.check_forward_residual(true);
128 linearOpWithSolveTester.check_adjoint_default(false);
129 linearOpWithSolveTester.check_adjoint_residual(false);
130 linearOpWithSolveTester.set_all_solve_tol(maxResid);
131 linearOpWithSolveTester.set_all_slack_error_tol(maxResid);
132 linearOpWithSolveTester.set_all_slack_warning_tol(1e+1*maxResid);
133 linearOpWithSolveTester.forward_default_residual_error_tol(2*maxResid);
134 linearOpWithSolveTester.forward_default_solution_error_error_tol(maxSolutionError);
135 linearOpWithSolveTester.adjoint_default_residual_error_tol(2*maxResid);
136 linearOpWithSolveTester.adjoint_default_solution_error_error_tol(maxSolutionError);
137 linearOpWithSolveTester.show_all_tests(showAllTests);
138 linearOpWithSolveTester.dump_all(dumpAll);
139 Thyra::seed_randomize<Scalar>(0);
140 result = linearOpWithSolveTester.check(*nsA,out.get());
141 if(!result) success = false;
142
143 if(out.get()) {
144 *out << "\namesos2LOWSFPL after solving:\n";
145 amesos2LOWSFPL->print(OSTab(out).o(),0,true);
146 }
147
148 }
149 catch( const std::exception &excpt ) {
150 if(out.get()) *out << std::flush;
151 std::cerr << "*** Caught standard exception : " << excpt.what() << std::endl;
152 success = false;
153 }
154
155 return success;
156
157 }