Example #1
0
bool sillyCgSolve(
  const Thyra::LinearOpBase<Scalar> &A,
  const Thyra::VectorBase<Scalar> &b,
  const int maxNumIters,
  const typename Teuchos::ScalarTraits<Scalar>::magnitudeType tolerance,
  const Teuchos::Ptr<Thyra::VectorBase<Scalar> > &x,
  std::ostream &out
  )
{

  // Create some typedefs and some other stuff to make the code cleaner
  typedef Teuchos::ScalarTraits<Scalar> ST; typedef typename ST::magnitudeType ScalarMag;
  const Scalar one = ST::one(), zero = ST::zero();  using Teuchos::as;
  using Teuchos::RCP; using Thyra::VectorSpaceBase; using Thyra::VectorBase;
  using Thyra::NOTRANS; using Thyra::V_V; using Thyra::apply;
  

  // Validate input
  THYRA_ASSERT_LINEAR_OP_VEC_APPLY_SPACES("sillyCgSolve()", A, Thyra::NOTRANS, *x, &b);
  Teuchos::EVerbosityLevel vl = Teuchos::VERB_MEDIUM;

  out << "\nStarting CG solver ...\n" << std::scientific << "\ndescribe A:\n"<<describe(A, vl)
      << "\ndescribe b:\n"<<describe(b, vl)<<"\ndescribe x:\n"<<describe(*x, vl)<<"\n";

  // Initialization
  const RCP<const VectorSpaceBase<Scalar> > space = A.domain();
  const RCP<VectorBase<Scalar> > r = createMember(space);
  // r = -A*x + b
  V_V(r.ptr(), b); apply<Scalar>(A, NOTRANS, *x, r.ptr(), -one, one);
  const ScalarMag r0_nrm = norm(*r);
  if (r0_nrm==zero) return true;
  const RCP<VectorBase<Scalar> > p = createMember(space), q = createMember(space);
  Scalar rho_old = -one;

  // Perform the iterations
  for( int iter = 0; iter <= maxNumIters; ++iter ) {

    // Check convergence and output iteration
    const ScalarMag r_nrm = norm(*r);
    const bool isConverged = r_nrm/r0_nrm <= tolerance;
    if( iter%(maxNumIters/10+1) == 0 || iter == maxNumIters || isConverged ) {
      out << "Iter = " << iter << ", ||b-A*x||/||b-A*x0|| = " << (r_nrm/r0_nrm) << std::endl;
      if( r_nrm/r0_nrm < tolerance ) return true; // Success!
    }

    // Compute iteration
    const Scalar rho = inner(*r, *r);        // <r,r>              -> rho
    if (iter==0) V_V(p.ptr(), *r);           // r                  -> p   (iter == 0)
    else Vp_V( p.ptr(), *r, rho/rho_old );   // r+(rho/rho_old)*p  -> p   (iter  > 0)
    apply<Scalar>(A, NOTRANS, *p, q.ptr());  // A*p                -> q
    const Scalar alpha = rho/inner(*p, *q);  // rho/<p,q>          -> alpha
    Vp_StV( x, +alpha, *p );                 // +alpha*p + x       -> x
    Vp_StV( r.ptr(), -alpha, *q );           // -alpha*q + r       -> r
    rho_old = rho;                           // rho                -> rho_old (for next iter)

  }

  return false; // Failure

} // end sillyCgSolve
RCP<const Teuchos::Comm<Thyra::Ordinal> >
TpetraOperatorWrapper::getThyraComm(const Thyra::LinearOpBase<ST>& inOp) const
{
  RCP<const VectorSpaceBase<ST> > vs = inOp.domain();

  RCP<const SpmdVectorSpaceBase<ST> > spmd;
  RCP<const VectorSpaceBase<ST> > current = vs;
  while(current!=Teuchos::null) {
     // try to cast to a product vector space first
     RCP<const ProductVectorSpaceBase<ST> > prod
           = rcp_dynamic_cast<const ProductVectorSpaceBase<ST> >(current);

     // figure out what type it is
     if(prod==Teuchos::null) {
        // hopfully this is a SPMD vector space
        spmd = rcp_dynamic_cast<const SpmdVectorSpaceBase<ST> >(current);

        break;
     }
     else // get first convenient vector space
        current = prod->getBlock(0);
  }

  TEUCHOS_TEST_FOR_EXCEPTION(spmd==Teuchos::null, std::runtime_error, 
                     "TpetraOperatorWrapper requires std::vector space "
                     "blocks to be SPMD std::vector spaces");

  return spmd->getComm();
/*
  const Thyra::ConstLinearOperator<double> thyraOp = rcpFromRef(inOp); 

  RCP<Epetra_Comm> rtn;
  // VectorSpace<double> vs = thyraOp.domain().getBlock(0);
  RCP<const VectorSpaceBase<double> > vs = thyraOp.domain().getBlock(0).constPtr();

  // search for an SpmdVectorSpaceBase object
  RCP<const SpmdVectorSpaceBase<double> > spmd;
  RCP<const VectorSpaceBase<double> > current = vs;
  while(current!=Teuchos::null) {
     // try to cast to a product vector space first
     RCP<const ProductVectorSpaceBase<double> > prod
           = rcp_dynamic_cast<const ProductVectorSpaceBase<double> >(current);

     // figure out what type it is
     if(prod==Teuchos::null) {
        // hopfully this is a SPMD vector space
        spmd = rcp_dynamic_cast<const SpmdVectorSpaceBase<double> >(current);

        break;
     }
     else {
        // get first convenient vector space
        current = prod->getBlock(0);
     }
  }

  TEUCHOS_TEST_FOR_EXCEPTION(spmd==Teuchos::null, std::runtime_error, 
                     "TpetraOperatorWrapper requires std::vector space "
                     "blocks to be SPMD std::vector spaces");

  const SerialComm<Thyra::Ordinal>* serialComm 
    = dynamic_cast<const SerialComm<Thyra::Ordinal>*>(spmd->getComm().get());

#ifdef HAVE_MPI
  const MpiComm<Thyra::Ordinal>* mpiComm 
    = dynamic_cast<const MpiComm<Thyra::Ordinal>*>(spmd->getComm().get());

  TEUCHOS_TEST_FOR_EXCEPTION(mpiComm==0 && serialComm==0, std::runtime_error, 
                     "SPMD std::vector space has a communicator that is "
                     "neither a serial comm nor an MPI comm");

  if (mpiComm != 0)
    {
      rtn = rcp(new Epetra_MpiComm(MPI_COMM_WORLD));
    }
  else
    {
      rtn = rcp(new Epetra_SerialComm());
    }
#else
  TEUCHOS_TEST_FOR_EXCEPTION(serialComm==0, std::runtime_error, 
                     "SPMD std::vector space has a communicator that is "
                     "neither a serial comm nor an MPI comm");
  rtn = rcp(new Epetra_SerialComm());
  
#endif

  TEUCHOS_TEST_FOR_EXCEPTION(rtn.get()==0, std::runtime_error, "null communicator created");
  return rtn;
*/
}