Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #ifndef PLAYA_VECTORSPACEIMPL_HPP
00043 #define PLAYA_VECTORSPACEIMPL_HPP
00044
00045 #include "PlayaVectorSpaceDecl.hpp"
00046 #include "PlayaBlockVectorSpaceDecl.hpp"
00047 #include "PlayaVectorDecl.hpp"
00048 #include "PlayaExceptions.hpp"
00049 #include "Teuchos_Time.hpp"
00050 #include "Teuchos_TimeMonitor.hpp"
00051
00052 #ifndef HAVE_TEUCHOS_EXPLICIT_INSTANTIATION
00053 #include "PlayaBlockVectorSpaceImpl.hpp"
00054 #endif
00055
00056 using namespace Teuchos;
00057
00058 namespace Playa
00059 {
00060
00061
00062 static inline Time& createVecTimer()
00063 {
00064 static RCP<Time> rtn
00065 = TimeMonitor::getNewTimer("vector allocation");
00066 return *rtn;
00067 }
00068
00069
00070
00071 template <class Scalar> inline
00072 bool VectorSpace<Scalar>::operator==(const VectorSpace<Scalar>& other) const
00073 {
00074 return isCompatible(other);
00075 }
00076
00077
00078
00079 template <class Scalar> inline
00080 bool VectorSpace<Scalar>::operator!=(const VectorSpace<Scalar>& other) const
00081 {
00082 return !(operator==(other));
00083 }
00084
00085
00086
00087
00088 template <class Scalar> inline
00089 Vector<Scalar> VectorSpace<Scalar>::createMember() const
00090 {
00091 Vector<Scalar> rtn = this->ptr()->createMember(*this);
00092 rtn.setToConstant(0.0);
00093 return rtn;
00094 }
00095
00096
00097
00098
00099 template <class Scalar> inline
00100 int VectorSpace<Scalar>::baseGlobalNaturalIndex() const
00101 {
00102 return this->ptr()->baseGlobalNaturalIndex();
00103 }
00104
00105
00106 template <class Scalar> inline
00107 int VectorSpace<Scalar>::numLocalElements() const
00108 {
00109 return this->ptr()->numLocalElements();
00110 }
00111
00112
00113
00114
00115
00116 template <class Scalar> inline
00117 bool VectorSpace<Scalar>::isCompatible(const VectorSpace<Scalar>& vecSpc) const
00118 {
00119 TEUCHOS_TEST_FOR_EXCEPTION(vecSpc.ptr().get() == 0, std::runtime_error,
00120 "null argument in VectorSpace<Scalar>::isCompatible()");
00121 return this->ptr().get()->isCompatible(vecSpc.ptr().get());
00122 }
00123
00124
00125
00126
00127
00128
00129 template <class Scalar> inline
00130 bool VectorSpace<Scalar>::contains(const Vector<Scalar> &vec) const
00131 {
00132 return (operator==(vec.space()));
00133 }
00134
00135
00136
00137 template <class Scalar>
00138 int VectorSpace<Scalar>::numBlocks() const
00139 {
00140 return this->ptr()->numBlocks();
00141 }
00142
00143
00144
00145 template <class Scalar>
00146 bool VectorSpace<Scalar>::isBlockSpace() const
00147 {
00148 return dynamic_cast<const BlockVectorSpaceBase<Scalar>*>(this->ptr().get())!=0;
00149 }
00150
00151
00152
00153
00154 template <class Scalar> inline
00155 const VectorSpace<Scalar>& VectorSpace<Scalar>::getBlock(const int i) const
00156 {
00157 const BlockVectorSpaceBase<Scalar>* bvs =
00158 dynamic_cast<const BlockVectorSpaceBase<Scalar>* > (this->ptr().get());
00159 TEUCHOS_TEST_FOR_EXCEPTION(bvs == 0 && numBlocks()!=1, std::runtime_error,
00160 "getBlock called for a space that "
00161 "is not a BlockVectorSpace" << std::endl);
00162 if (bvs != 0)
00163 {
00164 return bvs->getBlock(i);
00165 }
00166 return *this;
00167 }
00168
00169
00170 template <class Scalar> inline
00171 const VectorSpace<Scalar>& VectorSpace<Scalar>
00172 ::getBlock(const BlockIterator<Scalar>& b) const
00173 {
00174
00175 TEUCHOS_TEST_FOR_EXCEPTION(b.atEnd(), RuntimeError,
00176 "Attempt to use a block iterator that's run off end");
00177
00178 return this->getBlock(b.blockIndex());
00179 }
00180
00181
00182
00183 template <class Scalar> inline
00184 const VectorSpace<Scalar>& VectorSpace<Scalar>
00185 ::getBlock(const std::deque<int>& b) const
00186 {
00187
00188 if (b.size()==0) return *this;
00189
00190 if (b.size()==1)
00191 {
00192 return this->getBlock(b.front());
00193 }
00194
00195 int b0 = b.front();
00196 std::deque<int> tmp = b;
00197 tmp.pop_front();
00198 return this->getBlock(b0).getBlock(tmp);
00199 }
00200
00201
00202
00203
00204
00205 template <class Scalar> inline
00206 BlockIterator<Scalar> VectorSpace<Scalar>::beginBlock() const
00207 {
00208 return BlockIterator<Scalar>(*this, false);
00209 }
00210
00211 template <class Scalar> inline
00212 BlockIterator<Scalar> VectorSpace<Scalar>::endBlock() const
00213 {
00214 return BlockIterator<Scalar>(*this, true);
00215 }
00216
00217
00218 template <class Scalar> inline
00219 int VectorSpace<Scalar>::mapToGNI(
00220 const BlockIterator<Scalar>& b,
00221 int indexWithinBlock) const
00222 {
00223 int rtn = this->baseGlobalNaturalIndex();
00224 if (this->numBlocks() > 1)
00225 {
00226 for (BlockIterator<Scalar> a=this->beginBlock(); a < b; a++)
00227 {
00228 rtn += getBlock(a).numLocalElements();
00229 }
00230 }
00231 return rtn + indexWithinBlock;
00232 }
00233
00234 template <class Scalar> inline
00235 bool VectorSpace<Scalar>::containsGNI(int gni) const
00236 {
00237 return (gni >= this->baseGlobalNaturalIndex()
00238 && gni < this->baseGlobalNaturalIndex()+this->numLocalElements());
00239 }
00240
00241 template <class Scalar> inline
00242 void VectorSpace<Scalar>::getBlockAndOffsetFromGNI(int gni,
00243 BlockIterator<Scalar>& block, int& indexWithinBlock) const
00244 {
00245 int low = this->baseGlobalNaturalIndex();
00246
00247 if (this->numBlocks() == 1)
00248 {
00249 indexWithinBlock = gni - low;
00250 }
00251 else
00252 {
00253 int sizeSum = low;
00254 for (BlockIterator<Scalar> b=this->beginBlock();
00255 b != this->endBlock(); b++)
00256 {
00257 if (this->getBlock(b).containsGNI(gni))
00258 {
00259 block = b;
00260 indexWithinBlock = gni - sizeSum;
00261 }
00262 sizeSum += this->getBlock(b).numLocalElements();
00263 }
00264 }
00265 }
00266
00267 }
00268
00269
00270
00271 #endif