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

Generated on Tue Oct 20 12:46:59 2009 for Thyra Operator/Vector Support by doxygen 1.4.7