11 #include "sillyCgSolve.hpp"
14 #include "Thyra_DefaultSpmdVectorSpace.hpp"
23 int main(
int argc,
char *argv[])
38 MPI_Comm mpiComm = MPI_COMM_WORLD;
46 out = Teuchos::VerboseObjectBase::getDefaultOStream();
55 double diagScale = 1.001;
56 bool useWithNonEpetraVectors =
false;
57 double tolerance = 1e-4;
58 int maxNumIters = 300;
60 CommandLineProcessor clp(
false);
61 clp.setOption(
"verbose",
"quiet", &verbose,
"Determines if any output is printed or not." );
62 clp.setOption(
"global-dim", &globalDim,
"Global dimension of the linear system." );
63 clp.setOption(
"diag-scale", &diagScale,
"Scaling of the diagonal to improve conditioning." );
64 clp.setOption(
"use-with-non-epetra-vectors",
"use-with-epetra-vectors", &useWithNonEpetraVectors
65 ,
"Use non-epetra vectors with Epetra operator or not." );
66 clp.setOption(
"tol", &tolerance,
"Relative tolerance for linear system solve." );
67 clp.setOption(
"max-num-iters", &maxNumIters,
"Maximum of CG iterations." );
69 CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
70 if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL )
return parse_return;
74 if(verbose) *out <<
"\n***\n*** Running CG example using Epetra implementation\n***\n" << std::scientific;
86 if(verbose) *out <<
"\n(1) Constructing tridagonal Epetra matrix A_epetra of global dimension = " << globalDim <<
" ...\n";
93 ,diagScale,verbose,*out
96 RCP<const Thyra::LinearOpBase<double> >
97 A = Thyra::epetraLinearOp(A_epetra);
99 RCP<const Thyra::VectorSpaceBase<double> >
100 b_space = A->range();
102 RCP<Thyra::VectorBase<double> > b = createMember(b_space);
103 Thyra::seed_randomize<double>(0);
104 Thyra::randomize( -1.0, +1.0, b.ptr() );
106 RCP<Thyra::VectorBase<double> > x = createMember(A->domain());
107 Thyra::assign( x.ptr(), 0.0 );
111 result = sillyCgSolve(*A, *b, maxNumIters, tolerance, x.ptr(), *out);
112 if(!result) success =
false;
116 RCP<Thyra::VectorBase<double> > r = createMember(A->range());
117 Thyra::assign(r.ptr(),*b);
118 Thyra::apply(*A,Thyra::NOTRANS,*x,r.ptr(),-1.0,+1.0);
119 const double r_nrm = Thyra::norm(*r), b_nrm =Thyra:: norm(*b);
120 const double rel_err = r_nrm/b_nrm, relaxTol = 2.0*tolerance;
121 result = rel_err <= relaxTol;
122 if(!result) success =
false;
125 <<
"\n||b-A*x||/||b|| = "<<r_nrm<<
"/"<<b_nrm<<
" = "<<rel_err<<(result?
" <= ":
" > ")
126 <<
"2.0*tolerance = "<<relaxTol<<
": "<<(result?
"passed":
"failed")<<std::endl;
132 if(success) *out <<
"\nCongratulations! All of the tests checked out!\n";
133 else *out <<
"\nOh no! At least one of the tests failed!\n";
136 return success ? 0 : 1;
Teuchos::RCP< Epetra_Operator > createTridiagEpetraLinearOp(const int globalDim, const double diagScale, const bool verbose, std::ostream &out)
This function generates a tridiagonal linear operator using Epetra.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
int main(int argc, char *argv[])