Thyra_VectorSpaceImpl.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_VECTORSPACEIMPL_HPP
00030 #define THYRA_VECTORSPACEIMPL_HPP
00031 
00032 #include "Thyra_ProductVectorSpaceBase.hpp"
00033 #include "Thyra_SpmdVectorSpaceBase.hpp"
00034 #include "Thyra_DefaultProductVectorSpace.hpp"
00035 #include "Thyra_VectorSpaceDecl.hpp"
00036 #include "Teuchos_Describable.hpp"
00037 
00038 namespace Thyra
00039 {
00040   using namespace Teuchos;
00041   using std::ostream;
00042 
00043   //========================================================================
00044   template <class Scalar> inline
00045   VectorSpace<Scalar>&
00046   VectorSpace<Scalar>::operator=(const RCP<VectorSpaceBase<Scalar> >& vs)
00047   {
00048     this->setRcp(vs);
00049     return *this;
00050   }
00051  
00052   //========================================================================
00053   template <class Scalar> inline
00054   bool VectorSpace<Scalar>::operator==(const VectorSpace<Scalar>& other) const 
00055   {
00056     return isCompatible(other);  
00057   }
00058 
00059   //========================================================================
00060   template <class Scalar> inline
00061   bool VectorSpace<Scalar>::operator!=(const VectorSpace<Scalar>& other) const 
00062   {
00063     return !(operator==(other));
00064   }
00065 
00066   //========================================================================
00067   template <class Scalar> inline
00068   Vector<Scalar> VectorSpace<Scalar>::createMember() const 
00069   {
00070     return Thyra::createMember(this->constPtr());
00071   }
00072 
00073   //========================================================================
00074   template <class Scalar> inline
00075   RCP<MultiVectorBase<Scalar> > VectorSpace<Scalar>::createMembers(int n) const 
00076   {
00077     return Thyra::createMembers(this->constPtr(), n);
00078   }
00079 
00080   //========================================================================
00081   template <class Scalar> inline
00082   bool VectorSpace<Scalar>::isCompatible(const VectorSpace<Scalar>& vecSpc) const 
00083   {
00084     TEST_FOR_EXCEPTION(vecSpc.constPtr().get() == 0, std::runtime_error,
00085                        "null argument in VectorSpace<Scalar>::isCompatible()");
00086     return this->constPtr().get()->isCompatible(*(vecSpc.constPtr().get()));
00087   }
00088 
00089   //========================================================================
00090   template <class Scalar> inline
00091   bool VectorSpace<Scalar>::contains(const Vector<Scalar> &vec) const
00092   {
00093     return (operator==(vec.space()));
00094   }
00095 
00096   //========================================================================
00097   template <class Scalar> inline
00098   int VectorSpace<Scalar>::numBlocks() const
00099   {
00100     const Thyra::ProductVectorSpaceBase<Scalar>* pvs = 
00101       dynamic_cast<const Thyra::ProductVectorSpaceBase<Scalar>* > (this->constPtr().get());
00102     if (pvs != 0)
00103       {
00104         return pvs->numBlocks();
00105       }
00106     return 1;
00107   }
00108 
00109   //========================================================================
00110   template <class Scalar> inline
00111   VectorSpace<Scalar> VectorSpace<Scalar>::getBlock(const int i) const
00112   {
00113     const Thyra::ProductVectorSpaceBase<Scalar>* pvs = 
00114       dynamic_cast<const Thyra::ProductVectorSpaceBase<Scalar>* > (this->constPtr().get());
00115     TEST_FOR_EXCEPTION(pvs == 0 && numBlocks()!=1, std::runtime_error,
00116                        "Space not a ProductVectorSpace" << std::endl);
00117     if (pvs != 0)
00118       {
00119         return pvs->getBlock(i);
00120       }
00121     return *this;
00122   }
00123 
00124   // //========================================================================
00125   // template <class Scalar>
00126   // void VectorSpace<Scalar>::setBlock(int i, 
00127   //           const VectorSpace<Scalar>& space)
00128   // {
00129   //   const Thyra::ProductVectorSpace<Scalar>*  pvs = 
00130   //     dynamic_cast<const Thyra::ProductVectorSpace<Scalar>* >  (this->constPtr().get());
00131 
00132   //   TEST_FOR_EXCEPTION(pvs == 0, std::runtime_error,
00133   //         "Can't set block of std::vector space that is " <<
00134   //         "not a ProductVectorSpace.");
00135 
00136   //   Thyra::ProductVectorSpace<Scalar>* pvsc = const_cast<ProductVectorSpace<Scalar>*> (pvs);
00137   //   pvsc->setBlock(i, space);
00138   // }
00139 
00140   //========================================================================
00141   template <class Scalar> inline
00142   Teuchos::RCP<const VectorSpaceBase<Scalar> > 
00143   productSpace(const Teuchos::Array<VectorSpace<Scalar> >& spaces)
00144   {
00145     Teuchos::Array<Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar> > > data(spaces.size());
00146     for (unsigned int i=0; i<spaces.size(); i++)
00147       {
00148         data[i] = spaces[i].constPtr();
00149       }
00150     return rcp(new Thyra::DefaultProductVectorSpace<Scalar>(data.size(), &(data[0])));
00151   }
00152 
00153   //========================================================================
00154   template <class Scalar> inline
00155   Teuchos::RCP<const VectorSpaceBase<Scalar> > 
00156   productSpace(VectorSpace<Scalar>& s1)
00157   {
00158     return productSpace<Scalar>(Teuchos::tuple(s1));
00159   }
00160 
00161   //========================================================================
00162   template <class Scalar> inline
00163   Teuchos::RCP<const VectorSpaceBase<Scalar> > 
00164   productSpace(VectorSpace<Scalar>& s1, 
00165                VectorSpace<Scalar>& s2)
00166   {
00167     return productSpace<Scalar>(Teuchos::tuple(s1, s2));
00168   }
00169 
00170   //========================================================================
00171   template <class Scalar> inline
00172   Teuchos::RCP<const VectorSpaceBase<Scalar> > 
00173   productSpace(VectorSpace<Scalar>& s1,VectorSpace<Scalar>& s2,
00174                VectorSpace<Scalar>& s3)
00175   {
00176     return productSpace<Scalar>(Teuchos::tuple(s1, s2, s3));
00177   }
00178 
00179   //========================================================================
00180   template <class Scalar> inline 
00181   int lowestLocallyOwnedIndex(const VectorSpace<Scalar>& s) 
00182   {
00183     TEST_FOR_EXCEPT(!isSPMD(s));
00184     RCP<const SpmdVectorSpaceBase<Scalar> > spmdSpace
00185       = rcp_dynamic_cast<const SpmdVectorSpaceBase<Scalar> >(s.constPtr());
00186     return spmdSpace->localOffset();
00187   }
00188 
00189   //========================================================================
00190   template <class Scalar> inline
00191   int numLocalElements(const VectorSpace<Scalar>& s) 
00192   {
00193     TEST_FOR_EXCEPT(!isSPMD(s));
00194     RCP<const SpmdVectorSpaceBase<Scalar> > spmdSpace
00195       = rcp_dynamic_cast<const SpmdVectorSpaceBase<Scalar> >(s.constPtr());
00196     return spmdSpace->localSubDim();
00197   }
00198 
00199   //========================================================================
00200   template <class Scalar> inline
00201   bool isSPMD(const VectorSpace<Scalar>& s) 
00202   {
00203     return dynamic_cast<const SpmdVectorSpaceBase<Scalar>* >(s.constPtr().get())!=0; 
00204   }
00205 
00206   //========================================================================
00207   template <class Scalar> inline
00208   bool indexIsLocal(const VectorSpace<Scalar>& s, Index i) 
00209   {
00210     Range1D range(i,i);
00211     return s.constPtr()->hasInCoreView(range, VIEW_TYPE_DIRECT, STRIDE_TYPE_UNIT);
00212   }
00213 
00214 } // namespace Thyra
00215 
00216 #endif

Generated on Wed May 12 21:26:54 2010 for Thyra Operator/Vector Support by  doxygen 1.4.7