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 #include "Common.hpp"
2 #include <cstdlib>
3 #include <iostream>
4 #include <vector>
5 
6 using std::cout;
7 using std::endl;
8 
9 template<class ScalarType>
10 void testSolvers () {
13  // To make the example simpler, we assume that ScalarType =
14  // NormType. For this to be correct, this would imply that
15  // ScalarType is real.
16  typedef ScalarType NormType;
17 
18  std::vector<std::pair<std::string, std::string> > solvers;
19  solvers.push_back (std::make_pair ("A", "1"));
20  solvers.push_back (std::make_pair ("A", "2"));
21  solvers.push_back (std::make_pair ("B", "3"));
22  solvers.push_back (std::make_pair ("B", "4"));
23  solvers.push_back (std::make_pair ("C", "5"));
24  solvers.push_back (std::make_pair ("C", "6"));
25 
26  for (size_t k = 0; k < solvers.size (); ++k) {
27  const std::string packageName = solvers[k].first;
28  const std::string solverName = solvers[k].second;
29  cout << "Package \"" << packageName << "\", solver \"" << solverName
30  << "\":" << endl;
32  Trilinos::Details::getLinearSolver<MV, OP, NormType> (packageName, solverName);
33  if (solver.get () == NULL) {
34  std::ostringstream err;
35  err << "Solver \"" << solvers[k].second << "\" from package \""
36  << solvers[k].first << "\" does not exist!";
37  throw std::logic_error (err.str ());
38  }
39 
40  Teuchos::RCP<OP> A = Teuchos::rcp (new OP ());
41  // your code for filling in the matrix A would go here
42 
43  solver->setMatrix (A);
44  solver->symbolic ();
45  solver->numeric ();
46 
47  MV X, B;
48  // your code for filling in X and B would go here
49 
50  solver->solve (X, B);
51  cout << "Finished solver->solve(X, B)" << endl << endl;
52 
53  // This is a proxy for a residual calculation. Some solvers
54  // compute the residual on their own, but you have to ask them.
55  A->apply (X, B);
56  }
57 }
58 
59 
60 int main () {
61  int err = EXIT_SUCCESS;
62 
63  cout << "Test ScalarType=float" << endl;
64  try {
65  testSolvers<float> ();
66  } catch (std::exception& e) {
67  cout << "testSolvers<float>() threw an exception: " << e.what () << endl;
68  return EXIT_FAILURE;
69  }
70 
71  cout << endl << "Test ScalarType=double" << endl;
72  try {
73  testSolvers<double> ();
74  } catch (std::exception& e) {
75  cout << "testSolvers<double>() threw an exception: " << e.what () << endl;
76  return EXIT_FAILURE;
77  }
78 
79  cout << endl << "Test ScalarType=int (should not work)" << endl;
80  try {
81  testSolvers<int> ();
82  cout << "testSolvers<int>() should not have worked!" << endl;
83  err = EXIT_FAILURE;
84  } catch (std::exception&) {
85  cout << "Of course testSolvers<int>() threw an exception: "
86  "no packages registered themselves for ScalarType=int. "
87  "This is correct behavior in that case." << endl;
88  }
89 
90  return err;
91 }
92 
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:10
int main(int argc, char *argv[])
Smart reference counting pointer class for automatic garbage collection.