Thyra_VectorImpl.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_IMPL_HPP
00030 #define THYRA_VECTOR_IMPL_HPP
00031 
00032 #include "Thyra_VectorDecl.hpp"
00033 #include "Thyra_VectorSpaceImpl.hpp"
00034 #include "Thyra_VectorStdOps.hpp"
00035 #include "Thyra_LinearCombinationImpl.hpp"
00036 #include "Thyra_ProductVectorBase.hpp"
00037 #include "Thyra_DetachedVectorView.hpp"
00038 
00039 namespace Thyra
00040 {
00041 
00042 //
00043 // ConstVector
00044 //
00045 
00046   template <class Scalar> inline
00047   Scalar ConstVector<Scalar>::operator[](Index globalIndex) const 
00048   {
00049     ConstDetachedVectorView<Scalar> view(this->constPtr(), Range1D(0, dim(*this)-1));
00050     return view[globalIndex];
00051   }
00052 
00053   template <class Scalar> inline
00054   bool ConstVector<Scalar>::containsVector(const Thyra::VectorBase<Scalar>* vec) const
00055   {
00056     return this->constPtr().get()==vec;
00057   }
00058 
00059   template <class Scalar> inline
00060   void ConstVector<Scalar>::evalInto(Vector<Scalar>& acceptor) const
00061   {
00062     acceptor.acceptCopyOf(*this);
00063   }
00064 
00065   template <class Scalar> inline
00066   void ConstVector<Scalar>::addInto(Vector<Scalar>& acceptor, LCSign sign) const
00067   {
00068     Scalar s = sign;
00069     Thyra::axpy(s, *this, acceptor);
00070   }
00071 
00072   template <class Scalar> inline
00073   std::ostream& operator<<(std::ostream& os, const ConstVector<Scalar>& v)
00074   {
00075     os << v.description() ;
00076     return os;
00077   }
00078 
00079 //
00080 // Vector
00081 //
00082 
00083   template <class Scalar> inline
00084   Vector<Scalar>::Vector( const VectorSpace<Scalar> &space )
00085   {
00086     *this = createMember(space);
00087   }
00088 
00089   template <class Scalar> inline
00090   Vector<Scalar>& Vector<Scalar>::acceptCopyOf(const ConstVector<Scalar>& other)
00091   {
00092     Thyra::VectorBase<Scalar>* p = this->ptr().get();
00093     const Thyra::VectorBase<Scalar>* px = other.constPtr().get();
00094     
00095     if (p==0) 
00096       {
00097         Vector<Scalar> me = space(other).createMember();
00098         this->ptr() = me.ptr();
00099       }
00100     Thyra::assign(p, *px);
00101     return *this;
00102   }
00103 
00104   template <class Scalar> inline
00105   Index dim(const ConstVector<Scalar>& x) 
00106   {
00107     return x.constPtr()->space()->dim();
00108   }
00109 
00110   template <class Scalar> inline
00111   VectorSpace<Scalar> space(const ConstVector<Scalar>& x) 
00112   {
00113     return x.constPtr()->space();
00114   }
00115 
00117   THYRA_UNARY_VECTOR_OP(copy, copyInto, assign, "copy")
00118 
00119   //===========================================================================
00120   template <class Scalar> inline
00121   int ConstVector<Scalar>::numBlocks() const
00122   {
00123     const Thyra::ProductVectorSpaceBase<Scalar>* pvs = 
00124       dynamic_cast <const Thyra::ProductVectorSpaceBase<Scalar>* >(space(*this).constPtr().get());
00125     if (pvs==0) 
00126       {
00127         return 1;
00128       }
00129     
00130     return pvs->numBlocks();
00131   }
00132 
00133   //===========================================================================
00134   template <class Scalar> inline
00135   ConstVector<Scalar> ConstVector<Scalar>::getBlock(int i) const
00136   {
00137     const Thyra::ProductVectorBase<Scalar>* pv = 
00138       dynamic_cast <const Thyra::ProductVectorBase<Scalar>* >(this->constPtr().get());
00139     if (pv==0) 
00140       {
00141         TEST_FOR_EXCEPTION(i != 0, runtime_error,
00142                            "Nonzero block index " << i << " into a vector that is not "
00143                            "a product vector");
00144         return *this;
00145       }
00146     Teuchos::RefCountPtr<const Thyra::VectorBase<Scalar> > b = pv->getVectorBlock(i);
00147     return b;
00148   }
00149 
00150   //===========================================================================
00151   template <class Scalar> inline
00152   Vector<Scalar> Vector<Scalar>::getBlock(int i) 
00153   {
00154     Thyra::ProductVectorBase<Scalar>* pv = 
00155       dynamic_cast <Thyra::ProductVectorBase<Scalar>* >(this->ptr().get());
00156     if (pv==0) 
00157       {
00158         TEST_FOR_EXCEPTION(i != 0, runtime_error,
00159                            "Nonzero block index " << i << " into a vector that is not "
00160                            "a product vector");
00161         return *this;
00162       }
00163     Teuchos::RefCountPtr<Thyra::VectorBase<Scalar> > b = pv->getNonconstVectorBlock(i);
00164     return b;
00165   }
00166 
00167   //===========================================================================
00168   template <class Scalar> inline
00169   void Vector<Scalar>::setBlock(int i, const ConstVector<Scalar>& b) 
00170   {
00171     Thyra::DefaultProductVector<Scalar>* pv = 
00172       dynamic_cast <Thyra::DefaultProductVector<Scalar>* >(this->ptr().get());
00173     TEST_FOR_EXCEPTION(pv == 0, runtime_error,
00174                        "setBlock() called on a vector that is not a default product vector");
00175     pv->setBlock(i, b.constPtr());
00176     
00177   }
00178 
00179   //===========================================================================
00180   template <class Scalar> inline
00181   void Vector<Scalar>::setBlock(int i, const Vector<Scalar>& b) 
00182   {
00183     Thyra::DefaultProductVector<Scalar>* pv = 
00184       dynamic_cast <Thyra::DefaultProductVector<Scalar>* >(this->ptr().get());
00185     TEST_FOR_EXCEPTION(pv == 0, runtime_error,
00186                        "setBlock() called on a vector that is not a default product vector");
00187     pv->setNonconstBlock(i, b.ptr());
00188     
00189   }
00190 
00191 }
00192 
00193 #endif // THYRA_VECTOR_IMPL_HPP

Generated on Thu Sep 18 12:32:32 2008 for Thyra Operator/Vector Support by doxygen 1.3.9.1