Thyra Package Browser (Single Doxygen Collection) Version of the Day
Thyra_VectorSpaceDefaultBase_def.hpp
Go to the documentation of this file.
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_SPACE_DEFAULT_BASE_DEF_HPP
00030 #define THYRA_VECTOR_SPACE_DEFAULT_BASE_DEF_HPP
00031 
00032 #include "Thyra_VectorSpaceDefaultBase_decl.hpp"
00033 #include "Thyra_VectorSpaceFactoryBase.hpp"
00034 #include "Thyra_VectorSpaceBase.hpp"
00035 #include "Thyra_VectorBase.hpp"
00036 #include "Thyra_MultiVectorStdOps.hpp"
00037 #include "Thyra_DefaultColumnwiseMultiVector.hpp"
00038 
00039 
00040 namespace Thyra {
00041 
00042 
00043 // Helper classes
00044 
00045 
00046 template<class Scalar>
00047 class CopyVectorViewBack {
00048 public:
00049   CopyVectorViewBack( const VectorBase<Scalar> *v, const RTOpPack::SubVectorView<Scalar>  &raw_v )
00050     :v_(v), raw_v_(raw_v)
00051     {}
00052   ~CopyVectorViewBack()
00053     {
00054       RTOpPack::ConstSubVectorView<Scalar> sv;
00055       v_->acquireDetachedView(Range1D(),&sv);
00056       RTOpPack::assign_entries<Scalar>( Teuchos::outArg(raw_v_), sv );
00057       v_->releaseDetachedView(&sv);
00058     }
00059 private:
00060   const VectorBase<Scalar>                   *v_;
00061   const RTOpPack::SubVectorView<Scalar>  raw_v_;
00062 };
00063 
00064 
00065 template<class Scalar>
00066 class CopyMultiVectorViewBack {
00067 public:
00068   CopyMultiVectorViewBack( const MultiVectorBase<Scalar> *mv, const RTOpPack::SubMultiVectorView<Scalar>  &raw_mv )
00069     :mv_(mv), raw_mv_(raw_mv)
00070     {}
00071   ~CopyMultiVectorViewBack()
00072     {
00073       RTOpPack::ConstSubMultiVectorView<Scalar> smv;
00074       mv_->acquireDetachedView(Range1D(),Range1D(),&smv);
00075       RTOpPack::assign_entries<Scalar>( Teuchos::outArg(raw_mv_), smv );
00076       mv_->releaseDetachedView(&smv);
00077     }
00078 private:
00079   const MultiVectorBase<Scalar>                       *mv_;
00080   const RTOpPack::SubMultiVectorView<Scalar>  raw_mv_;
00081 };
00082 
00083 
00084 // Overridden protected functions from VectorSpaceBase
00085 
00086 
00087 template<class Scalar>
00088 Teuchos::RCP<MultiVectorBase<Scalar> > 
00089 VectorSpaceDefaultBase<Scalar>::createMembers(int numMembers) const
00090 {
00091   return Teuchos::rcp(
00092     new DefaultColumnwiseMultiVector<Scalar>(
00093       Teuchos::rcp(this,false),
00094       this->smallVecSpcFcty()->createVecSpc(numMembers)));
00095   // ToDo: Use the "self object-reference" idiom ot fix this!
00096 }
00097 
00098 
00099 template<class Scalar>
00100 Teuchos::RCP<VectorBase<Scalar> >
00101 VectorSpaceDefaultBase<Scalar>::createMemberView(
00102   const RTOpPack::SubVectorView<Scalar> &raw_v ) const
00103 {
00104 #ifdef TEUCHOS_DEBUG
00105   TEST_FOR_EXCEPT( raw_v.subDim() != this->dim() );
00106 #endif
00107   // Create a vector
00108   RCP<VectorBase<Scalar> > v = this->createMember();
00109   // Copy initial values in raw_v into vector
00110   RTOpPack::SubVectorView<Scalar> sv;
00111   v->acquireDetachedView(Range1D(),&sv);
00112   RTOpPack::assign_entries<Scalar>(
00113     Teuchos::ptr_implicit_cast<const RTOpPack::SubVectorView<Scalar> >(Teuchos::outArg(sv)),
00114     RTOpPack::ConstSubVectorView<Scalar>(raw_v) );
00115   v->commitDetachedView(&sv);
00116   // Setup smart pointer to vector to copy view back out just before vector is destroyed
00117   Teuchos::set_extra_data(
00118     Teuchos::rcp(new CopyVectorViewBack<Scalar>(&*v,raw_v)),
00119     "CopyVectorViewBack",
00120     Teuchos::outArg(v),
00121     Teuchos::PRE_DESTROY
00122     );
00123   return v;
00124 }
00125 
00126 
00127 template<class Scalar>
00128 Teuchos::RCP<const VectorBase<Scalar> >
00129 VectorSpaceDefaultBase<Scalar>::createMemberView( const RTOpPack::ConstSubVectorView<Scalar> &raw_v ) const
00130 {
00131 #ifdef TEUCHOS_DEBUG
00132   TEST_FOR_EXCEPT( raw_v.subDim() != this->dim() );
00133 #endif
00134   // Create a vector
00135   RCP<VectorBase<Scalar> > v = this->createMember();
00136   // Copy initial values in raw_v into vector
00137   RTOpPack::SubVectorView<Scalar> sv;
00138   v->acquireDetachedView(Range1D(),&sv);
00139   RTOpPack::assign_entries<Scalar>(
00140     Teuchos::ptr_implicit_cast<const RTOpPack::SubVectorView<Scalar> >(Teuchos::outArg(sv)),
00141     raw_v );
00142   v->commitDetachedView(&sv);
00143   return v;
00144 }
00145 
00146 
00147 template<class Scalar>
00148 Teuchos::RCP<MultiVectorBase<Scalar> >
00149 VectorSpaceDefaultBase<Scalar>::createMembersView(
00150   const RTOpPack::SubMultiVectorView<Scalar> &raw_mv ) const
00151 {
00152 #ifdef TEUCHOS_DEBUG
00153   TEST_FOR_EXCEPT( raw_mv.subDim() != this->dim() );
00154 #endif
00155   // Create a multi-vector
00156   RCP< MultiVectorBase<Scalar> > mv = this->createMembers(raw_mv.numSubCols());
00157   // Copy initial values in raw_mv into multi-vector
00158   RTOpPack::SubMultiVectorView<Scalar> smv;
00159   mv->acquireDetachedView(Range1D(),Range1D(),&smv);
00160   RTOpPack::assign_entries<Scalar>(
00161     Ptr<const RTOpPack::SubMultiVectorView<Scalar> >(Teuchos::outArg(smv)),
00162     raw_mv
00163     );
00164   mv->commitDetachedView(&smv);
00165   // Setup smart pointer to multi-vector to copy view back out just before multi-vector is destroyed
00166   Teuchos::set_extra_data(
00167     Teuchos::rcp(new CopyMultiVectorViewBack<Scalar>(&*mv,raw_mv)),
00168     "CopyMultiVectorViewBack",
00169     Teuchos::outArg(mv),
00170     Teuchos::PRE_DESTROY
00171     );
00172   return mv;
00173 }
00174 
00175 
00176 template<class Scalar>
00177 Teuchos::RCP<const MultiVectorBase<Scalar> >
00178 VectorSpaceDefaultBase<Scalar>::createMembersView(
00179   const RTOpPack::ConstSubMultiVectorView<Scalar> &raw_mv ) const
00180 {
00181 #ifdef TEUCHOS_DEBUG
00182   TEST_FOR_EXCEPT( raw_mv.subDim() != this->dim() );
00183 #endif
00184   // Create a multi-vector
00185   RCP< MultiVectorBase<Scalar> > mv =
00186     this->createMembers(raw_mv.numSubCols());
00187   // Copy values in raw_mv into multi-vector
00188   RTOpPack::SubMultiVectorView<Scalar> smv;
00189   mv->acquireDetachedView(Range1D(),Range1D(),&smv);
00190   RTOpPack::assign_entries<Scalar>(
00191     Ptr<const RTOpPack::SubMultiVectorView<Scalar> >(Teuchos::outArg(smv)),
00192     raw_mv );
00193   mv->commitDetachedView(&smv);
00194   return mv;
00195 }
00196 
00197 
00198 } // end namespace Thyra
00199 
00200 
00201 #endif // THYRA_VECTOR_SPACE_DEFAULT_BASE_DEF_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines