Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
main.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Common.hpp"
11 #include <cstdlib>
12 #include <iostream>
13 #include <vector>
14 
15 using std::cout;
16 using std::endl;
17 
18 template<class ScalarType>
19 void testSolvers () {
22  // To make the example simpler, we assume that ScalarType =
23  // NormType. For this to be correct, this would imply that
24  // ScalarType is real.
25  typedef ScalarType NormType;
26 
27  std::vector<std::pair<std::string, std::string> > solvers;
28  solvers.push_back (std::make_pair ("A", "1"));
29  solvers.push_back (std::make_pair ("A", "2"));
30  solvers.push_back (std::make_pair ("B", "3"));
31  solvers.push_back (std::make_pair ("B", "4"));
32  solvers.push_back (std::make_pair ("C", "5"));
33  solvers.push_back (std::make_pair ("C", "6"));
34 
35  for (size_t k = 0; k < solvers.size (); ++k) {
36  const std::string packageName = solvers[k].first;
37  const std::string solverName = solvers[k].second;
38  cout << "Package \"" << packageName << "\", solver \"" << solverName
39  << "\":" << endl;
41  Trilinos::Details::getLinearSolver<MV, OP, NormType> (packageName, solverName);
42  if (solver.get () == NULL) {
43  std::ostringstream err;
44  err << "Solver \"" << solvers[k].second << "\" from package \""
45  << solvers[k].first << "\" does not exist!";
46  throw std::logic_error (err.str ());
47  }
48 
49  Teuchos::RCP<OP> A = Teuchos::rcp (new OP ());
50  // your code for filling in the matrix A would go here
51 
52  solver->setMatrix (A);
53  solver->symbolic ();
54  solver->numeric ();
55 
56  MV X, B;
57  // your code for filling in X and B would go here
58 
59  solver->solve (X, B);
60  cout << "Finished solver->solve(X, B)" << endl << endl;
61 
62  // This is a proxy for a residual calculation. Some solvers
63  // compute the residual on their own, but you have to ask them.
64  A->apply (X, B);
65  }
66 }
67 
68 
69 int main () {
70  int err = EXIT_SUCCESS;
71 
72  cout << "Test ScalarType=float" << endl;
73  try {
74  testSolvers<float> ();
75  } catch (std::exception& e) {
76  cout << "testSolvers<float>() threw an exception: " << e.what () << endl;
77  return EXIT_FAILURE;
78  }
79 
80  cout << endl << "Test ScalarType=double" << endl;
81  try {
82  testSolvers<double> ();
83  } catch (std::exception& e) {
84  cout << "testSolvers<double>() threw an exception: " << e.what () << endl;
85  return EXIT_FAILURE;
86  }
87 
88  cout << endl << "Test ScalarType=int (should not work)" << endl;
89  try {
90  testSolvers<int> ();
91  cout << "testSolvers<int>() should not have worked!" << endl;
92  err = EXIT_FAILURE;
93  } catch (std::exception&) {
94  cout << "Of course testSolvers<int>() threw an exception: "
95  "no packages registered themselves for ScalarType=int. "
96  "This is correct behavior in that case." << endl;
97  }
98 
99  return err;
100 }
101 
T * get() const
Get the raw C++ pointer to the underlying object.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
void testSolvers()
Definition: main.cpp:19
int main(int argc, char *argv[])
Smart reference counting pointer class for automatic garbage collection.