Thyra_VectorDefaultBase.hpp

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //    Thyra: Interfaces and Support for Abstract Numerical Algorithms
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #ifndef THYRA_VECTOR_DEFAULT_BASE_HPP
00030 #define THYRA_VECTOR_DEFAULT_BASE_HPP
00031 
00032 // Define to make some verbose output
00033 //#define THYRA_VECTOR_VERBOSE_TO_ERROR_OUT
00034 
00035 #include "Thyra_VectorDefaultBaseDecl.hpp"
00036 #include "Thyra_VectorBase.hpp"
00037 #include "Thyra_MultiVectorDefaultBase.hpp"
00038 #include "Thyra_SingleRhsLinearOpBase.hpp"
00039 #include "Thyra_VectorStdOps.hpp"
00040 #include "Thyra_AssertOp.hpp"
00041 #include "Thyra_SerialVectorSpaceStd.hpp"
00042 #include "Teuchos_TestForException.hpp"
00043 
00044 namespace Thyra {
00045 
00046 // Overridden from LinearOpBase
00047 
00048 template<class Scalar>
00049 Teuchos::RefCountPtr< const VectorSpaceBase<Scalar> >
00050 VectorDefaultBase<Scalar>::range() const
00051 {
00052 #ifdef THYRA_VECTOR_VERBOSE_TO_ERROR_OUT
00053   std::cerr << "\nVector<Scalar>::range() called!\n";
00054 #endif
00055   return this->space();
00056 }
00057 
00058 template<class Scalar>
00059 Teuchos::RefCountPtr< const VectorSpaceBase<Scalar> >
00060 VectorDefaultBase<Scalar>::domain() const
00061 {
00062 #ifdef THYRA_VECTOR_VERBOSE_TO_ERROR_OUT
00063   std::cerr << "\nVector<Scalar>::domain() called!\n";
00064 #endif
00065   if(!domain_.get())
00066     const_cast<VectorDefaultBase<Scalar>*>(this)->domain_ = Teuchos::rcp(new SerialVectorSpaceStd<Scalar>(1));
00067   return domain_;
00068 }
00069 
00070 // Overridden from SingleRhsLinearOpBase
00071 
00072 template<class Scalar>
00073 bool VectorDefaultBase<Scalar>::opSupported(ETransp M_trans) const
00074 {
00075   typedef Teuchos::ScalarTraits<Scalar> ST;
00076   return ( ST::isComplex ? ( M_trans==NOTRANS || M_trans==CONJTRANS ) : true );
00077 }
00078 
00079 template<class Scalar>
00080 void VectorDefaultBase<Scalar>::apply(
00081   const ETransp                M_trans
00082   ,const VectorBase<Scalar>    &x
00083   ,VectorBase<Scalar>          *y
00084   ,const Scalar                alpha
00085   ,const Scalar                beta
00086   ) const
00087 {
00088   typedef Teuchos::ScalarTraits<Scalar> ST;
00089   // Validate input
00090 #ifdef _DEBUG
00091   THYRA_ASSERT_LINEAR_OP_VEC_APPLY_SPACES("VectorDefaultBase<Scalar>::apply()",*this,M_trans,x,y);
00092 #endif
00093   // Here M = m (where m is a column vector)
00094   if( M_trans == NOTRANS || (M_trans == CONJ && !ST::isComplex) ) {
00095     // y = beta*y + alpha*m*x  (x is a scalar!)
00096 #ifdef THYRA_VECTOR_VERBOSE_TO_ERROR_OUT
00097     std::cerr << "\nVector<Scalar>::apply(...) : y = beta*y + alpha*m*x  (x is a scalar!)\n";
00098 #endif
00099     Vt_S( y, beta );
00100     Vp_StV( y, Scalar(alpha*get_ele(x,1)), *this );
00101   }
00102   else if( M_trans == CONJTRANS || (M_trans == TRANS && !ST::isComplex) ) {
00103     // y = beta*y + alpha*m'*x  (y is a scalar!)
00104 #ifdef THYRA_VECTOR_VERBOSE_TO_ERROR_OUT
00105     std::cerr << "\nVector<Scalar>::apply(...) : y = beta*y + alpha*m'*x  (y is a scalar!)\n";
00106 #endif
00107     Scalar y_inout;
00108     if( beta == ST::zero() ) {
00109       y_inout = ST::zero();
00110     }
00111     else {
00112       y_inout = get_ele(*y,1);
00113       y_inout = beta*y_inout;
00114     }
00115     y_inout += alpha*this->space()->scalarProd(*this,x);
00116     set_ele(1,y_inout,y);
00117   }
00118   else {
00119     TEST_FOR_EXCEPTION(
00120       true,std::logic_error
00121       ,"VectorBase<"<<ST::name()<<">::apply(M_trans,...): Error, M_trans="<<toString(M_trans)<<" not supported!"
00122       );
00123   }
00124 }
00125 
00126 // Overridden from MultiVectorBase
00127 
00128 template<class Scalar>
00129 inline
00130 void VectorDefaultBase<Scalar>::validateColRng( const Range1D &col_rng ) const
00131 {
00132 #ifdef _DEBUG
00133   TEST_FOR_EXCEPT( !( col_rng.full_range() || ( col_rng.lbound() == 1 && col_rng.ubound() == 1) ) );
00134 #endif
00135 }
00136 
00137 template<class Scalar>
00138 inline
00139 void VectorDefaultBase<Scalar>::validateColIndexes(  const int numCols, const int cols[] ) const
00140 {
00141 #ifdef _DEBUG
00142   TEST_FOR_EXCEPT( numCols != 1 || cols == NULL || cols[0] != 1 );
00143 #endif
00144 }
00145 
00146 template<class Scalar>
00147 Teuchos::RefCountPtr<VectorBase<Scalar> >
00148 VectorDefaultBase<Scalar>::col(Index j)
00149 {
00150 #ifdef THYRA_VECTOR_VERBOSE_TO_ERROR_OUT
00151   std::cerr << "\nVector<Scalar>::col(j) called!\n";
00152 #endif
00153 #ifdef _DEBUG
00154   TEST_FOR_EXCEPT( j != 1 );
00155 #endif
00156   return Teuchos::rcp(this,false);
00157 }
00158 
00159 template<class Scalar>
00160 Teuchos::RefCountPtr<MultiVectorBase<Scalar> >
00161 VectorDefaultBase<Scalar>::clone_mv() const
00162 {
00163 #ifdef THYRA_VECTOR_VERBOSE_TO_ERROR_OUT
00164   std::cerr << "\nVector<Scalar>::clone_mv() called!\n";
00165 #endif
00166   return this->clone_v();
00167 }
00168 
00169 template<class Scalar>
00170 Teuchos::RefCountPtr<const MultiVectorBase<Scalar> >
00171 VectorDefaultBase<Scalar>::subView( const Range1D& col_rng ) const
00172 {
00173 #ifdef THYRA_VECTOR_VERBOSE_TO_ERROR_OUT
00174   std::cerr << "\nVector<Scalar>::subView(col_rng) const called!\n";
00175 #endif
00176   validateColRng(col_rng);
00177   return Teuchos::rcp(this,false);
00178 }
00179 
00180 template<class Scalar>
00181 Teuchos::RefCountPtr<MultiVectorBase<Scalar> >
00182 VectorDefaultBase<Scalar>::subView( const Range1D& col_rng )
00183 {
00184 #ifdef THYRA_VECTOR_VERBOSE_TO_ERROR_OUT
00185   std::cerr << "\nVector<Scalar>::subView(col_rng) called!\n";
00186 #endif
00187   validateColRng(col_rng);
00188   return Teuchos::rcp(this,false);
00189 }
00190 
00191 template<class Scalar>
00192 Teuchos::RefCountPtr<const MultiVectorBase<Scalar> >
00193 VectorDefaultBase<Scalar>::subView( const int numCols, const int cols[] ) const
00194 {
00195 #ifdef THYRA_VECTOR_VERBOSE_TO_ERROR_OUT
00196   std::cerr << "\nVector<Scalar>::subView(numCols,cols) const called!\n";
00197 #endif
00198   validateColIndexes(numCols,cols);
00199   return Teuchos::rcp(this,false);
00200 }
00201 
00202 template<class Scalar>
00203 Teuchos::RefCountPtr<MultiVectorBase<Scalar> >
00204 VectorDefaultBase<Scalar>::subView( const int numCols, const int cols[] )
00205 {
00206 #ifdef THYRA_VECTOR_VERBOSE_TO_ERROR_OUT
00207   std::cerr << "\nVector<Scalar>::subView(numCols,cols) called!\n";
00208 #endif
00209   validateColIndexes(numCols,cols);
00210   return Teuchos::rcp(this,false);
00211 }
00212 
00213 template<class Scalar>
00214 void VectorDefaultBase<Scalar>::getSubMultiVector(
00215   const Range1D                       &rowRng
00216   ,const Range1D                      &colRng
00217   ,RTOpPack::SubMultiVectorT<Scalar>  *sub_mv
00218   ) const
00219 {
00220 #ifdef THYRA_VECTOR_VERBOSE_TO_ERROR_OUT
00221   std::cerr << "\nVector<Scalar>::getSubMultiVector() const called!\n";
00222 #endif
00223 #ifdef _DEBUG
00224   TEST_FOR_EXCEPT(sub_mv==NULL);
00225 #endif
00226   validateColRng(colRng);
00227   RTOpPack::SubVectorT<Scalar> sv;
00228   getSubVector(rowRng,&sv);
00229 #ifdef _DEBUG
00230   TEST_FOR_EXCEPT( sv.stride() != 1 ); // Can't handle non-unit stride yet but we could
00231 #endif
00232   sub_mv->initialize( sv.globalOffset(), sv.subDim(), 0, 1, sv.values(), sv.subDim() );
00233 }
00234 
00235 template<class Scalar>
00236 void VectorDefaultBase<Scalar>::freeSubMultiVector( RTOpPack::SubMultiVectorT<Scalar>* sub_mv ) const
00237 {
00238 #ifdef THYRA_VECTOR_VERBOSE_TO_ERROR_OUT
00239   std::cerr << "\nVector<Scalar>::freeSubMultiVector() const called!\n";
00240 #endif
00241 #ifdef _DEBUG
00242   TEST_FOR_EXCEPT(sub_mv==NULL);
00243 #endif
00244   RTOpPack::SubVectorT<Scalar> sv(sub_mv->globalOffset(),sub_mv->subDim(),sub_mv->values(),1);
00245   freeSubVector(&sv);
00246   sub_mv->set_uninitialized();
00247 }
00248 
00249 template<class Scalar>
00250 void VectorDefaultBase<Scalar>::getSubMultiVector(
00251   const Range1D                                &rowRng
00252   ,const Range1D                               &colRng
00253   ,RTOpPack::MutableSubMultiVectorT<Scalar>    *sub_mv
00254   )
00255 {
00256 #ifdef THYRA_VECTOR_VERBOSE_TO_ERROR_OUT
00257   std::cerr << "\nVector<Scalar>::getSubMultiVector() called!\n";
00258 #endif
00259 #ifdef _DEBUG
00260   TEST_FOR_EXCEPT(sub_mv==NULL);
00261 #endif
00262   validateColRng(colRng);
00263   RTOpPack::MutableSubVectorT<Scalar> sv;
00264   getSubVector(rowRng,&sv);
00265 #ifdef _DEBUG
00266   TEST_FOR_EXCEPT( sv.stride() != 1 ); // Can't handle non-unit stride yet but we could
00267 #endif
00268   sub_mv->initialize( sv.globalOffset(), sv.subDim(), 0, 1, sv.values(), sv.subDim() );
00269 }
00270 
00271 template<class Scalar>
00272 void VectorDefaultBase<Scalar>::commitSubMultiVector( RTOpPack::MutableSubMultiVectorT<Scalar>* sub_mv )
00273 {
00274 #ifdef THYRA_VECTOR_VERBOSE_TO_ERROR_OUT
00275   std::cerr << "\nVector<Scalar>::commitSubMultiVector() called!\n";
00276 #endif
00277 #ifdef _DEBUG
00278   TEST_FOR_EXCEPT(sub_mv==NULL);
00279 #endif
00280   RTOpPack::MutableSubVectorT<Scalar> sv(sub_mv->globalOffset(),sub_mv->subDim(),sub_mv->values(),1);
00281   commitSubVector(&sv);
00282   sub_mv->set_uninitialized();
00283 }
00284 
00285 } // end namespace Thyra
00286 
00287 #endif  // THYRA_VECTOR_DEFAULT_BASE_HPP

Generated on Thu Sep 18 12:39:52 2008 for Thyra ANA Operator/VectorBase Interfaces and Related Software by doxygen 1.3.9.1