43 #include "sillyCgSolve.hpp"
46 #include "Thyra_DefaultSpmdVectorSpace.hpp"
55 int main(
int argc,
char *argv[])
70 MPI_Comm mpiComm = MPI_COMM_WORLD;
78 out = Teuchos::VerboseObjectBase::getDefaultOStream();
87 double diagScale = 1.001;
88 bool useWithNonEpetraVectors =
false;
89 double tolerance = 1e-4;
90 int maxNumIters = 300;
92 CommandLineProcessor clp(
false);
93 clp.setOption(
"verbose",
"quiet", &verbose,
"Determines if any output is printed or not." );
94 clp.setOption(
"global-dim", &globalDim,
"Global dimension of the linear system." );
95 clp.setOption(
"diag-scale", &diagScale,
"Scaling of the diagonal to improve conditioning." );
96 clp.setOption(
"use-with-non-epetra-vectors",
"use-with-epetra-vectors", &useWithNonEpetraVectors
97 ,
"Use non-epetra vectors with Epetra operator or not." );
98 clp.setOption(
"tol", &tolerance,
"Relative tolerance for linear system solve." );
99 clp.setOption(
"max-num-iters", &maxNumIters,
"Maximum of CG iterations." );
101 CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
102 if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL )
return parse_return;
106 if(verbose) *out <<
"\n***\n*** Running CG example using Epetra implementation\n***\n" << std::scientific;
118 if(verbose) *out <<
"\n(1) Constructing tridagonal Epetra matrix A_epetra of global dimension = " << globalDim <<
" ...\n";
125 ,diagScale,verbose,*out
128 RCP<const Thyra::LinearOpBase<double> >
129 A = Thyra::epetraLinearOp(A_epetra);
131 RCP<const Thyra::VectorSpaceBase<double> >
132 b_space = A->range();
134 RCP<Thyra::VectorBase<double> > b = createMember(b_space);
135 Thyra::seed_randomize<double>(0);
136 Thyra::randomize( -1.0, +1.0, b.ptr() );
138 RCP<Thyra::VectorBase<double> > x = createMember(A->domain());
139 Thyra::assign( x.ptr(), 0.0 );
143 result = sillyCgSolve(*A, *b, maxNumIters, tolerance, x.ptr(), *out);
144 if(!result) success =
false;
148 RCP<Thyra::VectorBase<double> > r = createMember(A->range());
149 Thyra::assign(r.ptr(),*b);
150 Thyra::apply(*A,Thyra::NOTRANS,*x,r.ptr(),-1.0,+1.0);
151 const double r_nrm = Thyra::norm(*r), b_nrm =Thyra:: norm(*b);
152 const double rel_err = r_nrm/b_nrm, relaxTol = 2.0*tolerance;
153 result = rel_err <= relaxTol;
154 if(!result) success =
false;
157 <<
"\n||b-A*x||/||b|| = "<<r_nrm<<
"/"<<b_nrm<<
" = "<<rel_err<<(result?
" <= ":
" > ")
158 <<
"2.0*tolerance = "<<relaxTol<<
": "<<(result?
"passed":
"failed")<<std::endl;
164 if(success) *out <<
"\nCongratulations! All of the tests checked out!\n";
165 else *out <<
"\nOh no! At least one of the tests failed!\n";
168 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[])